This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Find the max number of rounds you need for each prefix, and each suffix, then for each position, check the max of the prefix and suffix.
#include <bits/stdc++.h>
using namespace std;
int N;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N;
vector<long long> arr(N+1);
for (int i = 1; i <= N; i++) {
cin >> arr[i];
}
vector<long long> prefixAdd(N+1), prefixVal(N+1);
for (int i = 1; i <= N; i++) {
prefixVal[i] = arr[i]+prefixAdd[i-1];
prefixAdd[i] = prefixAdd[i-1];
if (prefixVal[i] <= prefixVal[i-1]+1) {
prefixAdd[i] += (prefixVal[i-1]-prefixVal[i]+1);
prefixVal[i] = prefixVal[i-1]+1;
}
}
vector<long long> suffixAdd(N+2), suffixVal(N+2);
for (int i = N; i >= 1; i--) {
suffixVal[i] = arr[i]+suffixAdd[i+1];
suffixAdd[i] = suffixAdd[i+1];
if (suffixVal[i] <= suffixVal[i+1]+1) {
suffixAdd[i] += (suffixVal[i+1]-suffixVal[i]+1);
suffixVal[i] = suffixVal[i+1]+1;
}
}
long long ans = LLONG_MAX;
for (int i = 1; i <= N; i++) {
ans = min(ans, max(prefixAdd[i], suffixAdd[i]));
}
cout << ans << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |