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 inf 0x3F3F3F3F3F3F3F3F
#define int long long
const int MXN = 2e5 + 5;
int n;
int a[MXN];
int p[MXN], s[MXN];
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
a[0] = -inf, a[n + 1] = -inf;
for (int i = 1; i <= n; i++) p[i] = p[i - 1] + max(0LL, a[i - 1] + 1 - a[i]);
for (int i = n; i >= 1; i--) s[i] = s[i + 1] + max(0LL, a[i + 1] + 1 - a[i]);
int res = inf;
for (int i = 1; i <= n; i++) res = min(res, max(p[i - 1], s[i + 1]) + max(0LL, max(a[i - 1], a[i + 1]) + 1 - a[i]));
cout << res << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |