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];
}
if(n==2) {
cout<<"0\n";
return;
}
long long b[n-1];
for(int i=1; i<n; ++i) {
b[i-1] = a[i]-a[i-1];
}
int l = 0, r = n-2;
long long ans = 0;
while(l<r) {
while(l<r && b[l]>0) ++l;
while(l<r && b[r]<0) --r;
if(l==r) {
ans += b[l]==0;
break;
}
long long cur = min(-b[l], b[r])+1;
b[l] += cur;
b[r] -= cur;
ans += cur;
}
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... |