# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
870021 | Megumin2006 | Growing Vegetables is Fun 4 (JOI21_ho_t1) | C++14 | 26 ms | 12440 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>
#define int long long
using namespace std;
int n;
inline vector<int> get(vector<int> &a) {
vector<int> r(n + 1), st, ans(n + 1);
for(int i = n; i >= 1; --i) {
while(st.size() && a[ st.back() ] > a[i]) st.pop_back();
r[i] = st.size() ? st.back() : -1;
st.push_back(i);
}
for(int i = 1; i <= n; ++i) {
ans[i] += ans[i - 1];
if (r[i] == -1) continue;
ans[ r[i] ] += max(0ll, a[i] - a[ r[i] ] - ans[ r[i] ] + 1);
}
return ans;
}
void solve() {
cin >> n;
vector<int> a(n + 1);
for(int i = 1; i <= n; ++i) {
cin >> a[i];
}
vector<int> ans1 = get(a);
reverse(a.begin() + 1, a.end());
vector<int> ans2 = get(a);
reverse(ans2.begin() + 1, ans2.end());
int ans = min(ans1[n], ans2[1]);
for(int i = 1; i < n; ++i) {
ans = min(ans, max(ans1[i], ans2[i]));
}
cout << ans;
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0);
if (fopen("cf.inp", "r")) {
freopen("cf.inp", "r", stdin);
freopen("1.out", "w", stdout);
}
solve();
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |