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;
using ll = long long;
using ld = long double;
const int MAXN = 200000;
ll a[MAXN+5];
ll pref[MAXN+5];
ll suf[MAXN+5];
ll hpref[MAXN+5];
ll hsuf[MAXN+5];
const ll INF = 1000000000000000000LL;
int main(){
ios_base::sync_with_stdio(false), cin.tie(0);
cout.precision(10);
cout << fixed;
int n;
cin >> n;
for(int i=1; i<=n; i++){
cin >> a[i];
}
for(int i=1; i<=n; i++){
hpref[i] = max(hpref[i-1] + 1, a[i] + pref[i-1]);
pref[i] = pref[i-1] + max(0LL, hpref[i] - (a[i] + pref[i-1]));
}
for(int i=n; i>=1; i--){
hsuf[i] = max(hsuf[i+1] + 1, a[i] + suf[i+1]);
suf[i] = suf[i+1] + max(0LL, hsuf[i] - (a[i] + suf[i+1]));
}
ll res = INF;
for(int i=1; i<=n; i++){
res = min(res, max(pref[i-1], max(suf[i+1], max(hpref[i-1], hsuf[i+1]) + 1 - a[i])));
}
cout << res << "\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |