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 all(s) s.begin(), s.end()
#define int long long
#define fr first
#define sc second
#define pb push_back
#define mk make_pair
const int N = 2e5 + 7;
const int INF = 1e9 + 7;
int a[N], sf[N], pr[N], m[N];
int n, ans;
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 = n - 1; i >= 1; i--){
sf[i] = sf[i + 1] + max(0ll, a[i + 1] - a[i] + 1);
m[i] = max(m[i], a[i + 1] - a[i] + 1);
}
ans = sf[1];
int mx = 0;
for (int i = 2; i < n; i++){
pr[i] = pr[i - 1] + max(0ll, a[i - 1] - a[i] + 1);
int f1 = pr[i - 1], f2 = sf[i + 1], tk = 0;
if (a[i] + f2 > a[i - 1]){
f1 -= min(mx, min(a[i] + f2 - a[i - 1] - 1, f2));
tk = min(mx, min(a[i] + f2 - a[i - 1] - 1, f2));
}
else {
f1 += a[i - 1] - a[i] - f2 + 1;
}
if (a[i] + f1 > a[i + 1]){
f2 -= min(f2 - tk, min(m[i + 1], min(a[i] + f1 - a[i + 1] - 1, f1)));
}
else {
f2 += a[i + 1] - a[i] - f1 + 1;
}
ans = min(ans, f1 + f2);
mx = max(mx, a[i - 1] - a[i] + 1);
}
pr[n] = pr[n - 1] + max(0ll, a[n - 1] - a[n] + 1);
ans = min(ans, pr[n]);
cout << ans << endl;
}
Compilation message (stderr)
Main.cpp:17:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
17 | main (){
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |