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;
int main(){
int n; cin >> n;
vector<int> h(n);
for (int i = 0; i<n; i++){
cin >> h[i];
}
int res = 0;
int l = 0;
int r = n-1;
while (l < r){
while (h[l] < h[l+1]) l++;
while (h[r-1] > h[r]) r--;
if (l >= r) break;
if (l == r-1){
if (h[l] == h[r]) res++;
break;
}
if (h[l] - h[l+1] > h[r] - h[r-1]){
res += h[r] - h[r-1] + 1;
h[l] -= (h[r] - h[r-1] + 1);
h[r] = h[r-1] - 1;
} else {
res += h[l] - h[l+1] + 1;
h[r] -= h[l] - h[l+1] + 1;
h[l] = h[l+1] - 1;
}
}
cout << res << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |