제출 #404761

#제출 시각아이디문제언어결과실행 시간메모리
404761ScarletSGrowing Vegetables is Fun 4 (JOI21_ho_t1)C++17
100 / 100
36 ms6168 KiB
#include <bits/stdc++.h> #define ll long long using namespace std; const int N = 2e5+7; int a[N]; ll dp[N][2]; ll max(ll x, ll y) { if (x>y) return x; return y; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin>>n; ll ans=1e18; for (int i=1;i<=n;++i) cin>>a[i]; for (int i=1;i<=n;++i) dp[i][0]=dp[i-1][0]+max(a[i-1]+1-a[i],0); for (int i=n;i;--i) dp[i][1]=dp[i+1][1]+max(a[i+1]+1-a[i],0); for (int i=1;i<=n;++i) ans=min(ans,max(dp[i-1][0],dp[i+1][1])+max(0,max(a[i-1]+1-a[i]-max(0,dp[i][1]-dp[i][0]),a[i+1]+1-a[i]-max(0,dp[i][0]-dp[i][1])))); cout<<ans; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...