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;
void PlayGround() {
int n;
cin>>n;
int a[n];
for(int i=0; i<n; ++i) {
cin>>a[i];
}
long long b[n];
b[0] = a[0];
for(int i=1; i<n; ++i) {
b[i] = a[i]-a[i-1];
}
long long ans = LLONG_MAX;
long long candidate = 0;
for(int i=1; i<n; ++i) {
if(b[i]<=0) candidate += abs(b[i])+1;
}
ans = min(ans, candidate);
vector<long long>A(n), B(n);
for(int i=0; i<n; ++i) {
A[i] = max(0LL, 1-b[i]);
B[i] = max(0LL, b[i]+1);
if(i) A[i] += A[i-1];
}
for(int i=n-2; i>=0; --i) {
B[i] += B[i+1];
}
for(int i=0; i<n-1; ++i) {
ans = min(ans, max(A[i],B[i+1]));
}
cout<<ans<<'\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
PlayGround();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |