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;
typedef long long ll;
int main(){
ll n; cin >> n;
vector<ll> v(n);
for(auto &x : v)cin >> x;
if(n == 2){
if(v[0] == v[1]){
cout << 1 << "\n";
}
else cout << 0 << "\n";
return 0;
}
ll l = 1, r = n-2;
ll ans = 0;
while(l <= r){
while(l <= n-1 && v[l] > v[l-1])l++;
while(r >= 0 && v[r] > v[r+1])r--;
if(l > r)break;
ll x = min(v[r+1]-v[r]+1,v[l-1]-v[l]+1);
v[l-1]-=x;
v[r+1]-=x;
ans+=x;
}
if(abs(l-r) == 1)ans++;
cout << ans << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |