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>
#include<random>
using namespace std;
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...);}
#define dbg(...) cerr<<#__VA_ARGS__<<" = ";_do(__VA_ARGS__);
#define ss(n) fixed<<setprecision(n)
#define ll long long
#define int ll
#define IO ios::sync_with_stdio(false);cin.tie(0);
#define ld long double
#define pb push_back
#define pii pair<int,int>
#define rep(i,a) for(int i=1;i<=a;i++)
#define rep0(i,a) for(int i=0;i<a;i++)
#define F first
#define S second
ll gcd(ll a,ll b){if(b==0) return a; return gcd(b,a%b);}
const ld pi=3.14159265358979323846264338327950288419716939931;
const int lar=3e18;
const int mol1=1e9+7;
const int mol2=998244353;
void solve()
{
int n;
cin>>n;
int a[n+1];
rep(i,n) cin>>a[i];
int l=1,r=n;
while(l<=n-1&&a[l+1]>a[l]){
l++;
}
while(r>=2&&a[r-1]>a[r]){
r--;
}
int ans=0,pp=0;
while(l<r){
if(l==r-1){
if(a[l]==a[r]) ans++;
break;
}
while(r-1>l&&a[r-1]>a[r]) {
r--;
}
while(l<r-1&&a[l+1]>a[l]) {
l++;
}
if(l==r-1) continue;
int a1=a[l]-a[l+1],a2=a[r]-a[r-1];
int mn=min(a1,a2);
ans+=mn+1;
pp+=mn+1;
if(a1==mn){
l++;
a[r]-=mn+1;
}
else{
r--;
a[l]-=mn+1;
}
}
cout<<ans<<"\n";
}
signed main()
{
IO
solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |