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;
#define ll long long
#define dbg(...) cerr<<#__VA_ARGS__<<" = ";_do(__VA_ARGS__);
template<typename T> void _do(T x){cerr<<x<<"\n";}
template<typename T,typename ...U> void _do(T x,U ...y){cerr<<x<<", ";_do(y...);}
signed main(){
ios::sync_with_stdio(0), cin.tie(0);
int N;
cin >> N;
vector<ll> arr(N), cost(N), costR(N);
for (auto &i: arr) cin >> i;
cost[0] = 0;
for (int i = 1; i < N; i++) {
if (arr[i] > arr[i - 1]) cost[i] = cost[i - 1];
else cost[i] = cost[i - 1] + (arr[i - 1] - arr[i]) + 1;
}
costR[N - 1] = 0;
for (int i = N - 2; i >= 0; i--) {
if (arr[i] > arr[i + 1]) costR[i] = costR[i + 1];
else costR[i] = costR[i + 1] + (arr[i + 1] - arr[i]) + 1;
}
ll answer = 1e18;
for (int i = 0; i < N - 1; i++) {
answer = min(answer, max(cost[i], costR[i]));
}
cout << answer << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |