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>
typedef long long ll;
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
n += 2;
vector <ll> a(n);
a[0] = a[n - 1] = 0;
for (int i = 1; i < n - 1; i++) cin >> a[i];
vector <ll> d(n);
for (int i = 1; i < n; i++) d[i] = a[i] - a[i - 1];
vector <ll> pref(n - 1);
for (int i = 0; i < n - 1; i++) {
pref[i] = max(0ll, -d[i + 1] + 1);
if (i != 0) pref[i] += pref[i - 1];
}
vector <ll> suf(n - 1);
for (int i = n - 2; i >= 0; i--) {
suf[i] = max(0ll, d[i + 1] + 1);
if (i != n - 2) suf[i] += suf[i + 1];
}
ll ans = 1e18;
for (int i = 0; i < n - 2; i++) ans = min(ans, max(pref[i], suf[i + 1]));
cout << ans << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |