This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int inf = 1e18;
signed main(){
int n; cin>>n;
vector<int> v(n); for(int& i : v) cin>>i;
// for(int i : v) cout<<i<<" "; cout<<endl;
vector<int> dp(n,inf), dp1(n,inf), mx(n+1,0), mx1(n+1,0);
dp[0] = 0;
mx[0] = 0;
vector<int> last(n+1,0), last1(n+1,0); // just add max(between act and last)
for(int i = 1; i<n; i++){
// cerr<<i<<" "<<v[i-1]<<" "<<dp[i-1]<<" "<<mx[i-1]<<endl;
if(v[i] > v[i-1]){
dp[i] = dp[i-1]; continue;
}
dp[i] = dp[i-1] + max(0ll, abs(v[i] - v[i-1])) + 1;
}
dp1[n-1] = 0;
mx1[n] = 0;
for(int i = n-2; i>=0; i--){
// cerr<<"dp1 "<<i<<" "<<v[i]<<" "<<dp1[i+1]<<" "<<mx1[i+1]<<" ";
if(v[i] > v[i+1]){
dp1[i] = dp1[i+1]; continue;
}
dp1[i] = dp1[i+1] + max(0ll,(v[i+1]- v[i])) + 1;
}
// mx1[n] = v[n-1]; mx[0] = v[0];
// for(int i : mx1) cout<<i<<" "; cout<<endl;
int ans = min(dp[n-1],dp1[0]);
// cerr<<dp[n]<<" "<<dp1[0]<<endl;
for(int i = 1; i<n; i++){
// cerr<<dp[i]<<" "<<dp1[i]<<endl;
ans = min(ans, max(dp[i], dp1[i]));
}
cout<<ans<<endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |