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 int long long
int n;
int arr[200007];
pair<int, int> solve(int tp){
int linc = 0;
for (int x = 1; x <= tp; x++){
if (arr[x-1] >= arr[x]){
//enforce an increase
linc += arr[x-1] - arr[x] + 1;
}
}
int rinc = 0;
for (int x = n-2; x >= tp; x--){
if (arr[x+1] >= arr[x]){
//enforce an increase
rinc += arr[x+1] - arr[x] + 1;
}
}
//cout << tp << ' ' << linc << ' ' << rinc << '\n';
return make_pair(linc, rinc);
}
main(){
cin >> n; for (int x = 0; x < n; x++) cin >> arr[x];
pair<int, int> lr = solve(0);
int ans = max(lr.first, lr.second);
for (int x = 1; x < n; x++) {
if (arr[x-1] >= arr[x]){
//enforce an increase
lr.first += arr[x-1] - arr[x] + 1;
}
if (x == n-1){
lr.second = 0;
}
else if (arr[x] >= arr[x-1]){
//enforce an increase
lr.second -= arr[x] - arr[x-1] + 1;
}
ans = min(ans, max(lr.first, lr.second));
}
cout << ans;
}
Compilation message (stderr)
Main.cpp:27:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
27 | main(){
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |