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>
#define fi first
#define se second
#define mp make_pair
using namespace std;
const int N = 2e5 + 5;
const int oo = 2e9;
long long f[N];
long long g[N];
int a[N];
int n;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 2; i <= n; i++)
f[i] = f[i - 1] + max(0, a[i - 1] - a[i] + 1);
for (int i = n - 1; i >= 1; i--)
g[i] = g[i + 1] + max(0, a[i + 1] - a[i] + 1);
long long ans = 1e18;
for (int mid = 1; mid <= n; mid++) {
int cost = max(0, max(a[mid - 1], a[mid + 1]) - a[mid] + 1);
ans = min(ans, max(f[mid - 1], g[mid + 1]) + cost);
}
cout << ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |