// Online C++ compiler to run C++ program online
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int>vt(n + 1);
vector<int>v(n + 1);
for(int i = 1; i <= n; i++){
cin >> vt[i];
v[i] = vt[i];
}
vector<int>a(n + 1, 0);
vector<int>b(n + 1, 0);
for(int i = 2;i <= n; i++){
a[i] = a[i - 1];
if(v[i] <= v[i - 1]){
a[i] += v[i - 1] + 1 - v[i];
}
}
for(int i = n - 1;i >= 1; i--){
b[i] = b[i + 1];
if(vt[i] <= vt[i + 1]){
b[i] += vt[i + 1] + 1 - vt[i];
}
}
long long ans = LLONG_MAX;
for(int i = 1;i <= n; i++){
ans = min(ans, (long long)max(a[i], b[i]));
}
cout << ans << endl;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |