#include <iostream>
using namespace std;
#define ll long long
#define fori(n) for(int i=1;i<=n;i++)
int main() {
int n; cin >> n;
int h[n+1];h[0]=0;
fori(n) cin >> h[i];
//precompute the cost so far if chose (i,n+1-i)
ll pre[n+1] = {0};
for(int i=2;i<=n;i++) pre[i] = pre[i-1] + max(0,h[i-1]-h[i]+1);
ll suf[n+1] = {0};
for(int i=n-1;i>=1;i--) suf[i] = suf[i+1] + max(0,h[i+1]-h[i]+1);
ll ans = min(pre[n],suf[1]);
fori(n-1) ans = min(ans,max(pre[i],suf[i])); //max pre[i] suf[i] to water range(i,n+1-i) and suffice both ends
cout << ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |