# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
562766 | dattranxxx | Growing Vegetables is Fun 4 (JOI21_ho_t1) | C++11 | 0 ms | 0 KiB |
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;
using ll = long long;
void file() {
const string FILE_NAME = "FILE_NAME";
freopen((FILE_NAME + ".inp").c_str(), "r", stdin);
freopen((FILE_NAME + ".out").c_str(), "w", stdout);
}
const int N = 2e5 + 5;
int a[N], pre[N], suf[N];
int n;
int main() {
cin.tie(0)->sync_with_stdio(0); cout.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i)
cin >> a[i];
vector<int> hill;
a[0] = a[n+1] = -1e9;
for (int i = 1; i <= n; ++i)
pre[i] = pre[i-1] + max(0, a[i-1] + 1 - a[i]);
for (int i = n; i; --i)
suf[i] = suf[i+1] + max(0, a[i+1] + 1 - a[i]);
for (int i = 1; i <= n; ++i)
int res = 1e9;
for (int i = 1; i <= n; ++i)
res = min(res, max(pre[i], suf[i]));
cout << res;
return 0;
}