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 forn(i,n) for (int i=0; i<n; ++i)
void solve() {
int n; cin>>n;
vector<int> a(n); forn(i,n) cin>>a[i];
int ans=0;
int l=0,r=n-1;
int h=0;
while (l<r) {
if (a[l+1]>=h) {
if (a[r-1]>=h) {
if (a[l+1]<a[r-1]) {
ans+=a[l+1]-h;
h=a[l+1];
++l;
} else if (a[l+1]>a[r-1]) {
ans+=a[r-1]-h;
h=a[r-1];
--r;
} else {
ans+=a[l+1]-h;
h=a[l+1];
++l, --r;
}
} else {
if (a[l]<a[r-1]) {
ans+=h-a[r-1];
h=a[r-1];
--r;
} else if (a[l]>a[r-1]) {
ans+=h-a[l];
h=a[l];
if ((l==0) || a[l-1]>a[l]) {
cout<<"NO\n";
return;
} else --l;
} else {
ans+=h-a[l];
h=a[l];
--r;
}
}
} else {
if (a[r-1]<h) {
if (a[l+1]>a[r-1]) {
ans+=h-a[l+1];
h=a[l+1];
++l;
} else if (a[l+1]<a[r-1]) {
ans+=h-a[r-1];
h=a[r-1];
--r;
} else {
ans+=h-a[l+1];
h=a[l+1];
++l,--r;
}
} else {
if (a[r]<a[l+1]) {
ans+=h-a[l+1];
h=a[l+1];
++l;
} else if (a[r]>a[l+1]) {
ans+=h-a[r];
h=a[r];
if ((r==n-1) || a[r+1]>a[r]) {
cout<<"NO\n";
return;
} else ++r;
} else {
ans+=h-a[r];
h=a[r];
++l;
}
}
}
//cout<<l<<' '<<r<<' '<<ans<<' '<<h<<'\n';
}
cout<<ans;
}
int32_t main() {
solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |