#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
vector<ll> a (n, 0);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<ll> sp (n, 0), ss (n, 0);
for (int i = 1; i < n; i++) {
sp[i] = sp[i-1]+max(0ll, a[i-1]-a[i]+1);
}
for (int i = n-2; i >= 0; i--) {
ss[i] = ss[i+1]+max(0ll, a[i+1]-a[i]+1);
}
ll w = 1e18;
for (int i = 0; i < n; i++) {
w = min(w, sp[i]+ss[i]);
}
cout << w << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |