#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 |
1 |
Execution timed out |
1099 ms |
212 KB |
Time limit exceeded |
2 |
Execution timed out |
1085 ms |
212 KB |
Time limit exceeded |
3 |
Execution timed out |
1097 ms |
212 KB |
Time limit exceeded |
4 |
Execution timed out |
1068 ms |
212 KB |
Time limit exceeded |
5 |
Execution timed out |
1091 ms |
212 KB |
Time limit exceeded |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1045 ms |
212 KB |
Time limit exceeded |
2 |
Execution timed out |
1097 ms |
212 KB |
Time limit exceeded |
3 |
Execution timed out |
1093 ms |
212 KB |
Time limit exceeded |
4 |
Execution timed out |
1068 ms |
212 KB |
Time limit exceeded |
5 |
Execution timed out |
1092 ms |
212 KB |
Time limit exceeded |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1083 ms |
212 KB |
Time limit exceeded |
2 |
Execution timed out |
1088 ms |
212 KB |
Time limit exceeded |
3 |
Execution timed out |
1082 ms |
212 KB |
Time limit exceeded |
4 |
Execution timed out |
1096 ms |
212 KB |
Time limit exceeded |
5 |
Execution timed out |
1085 ms |
212 KB |
Time limit exceeded |
6 |
Execution timed out |
1093 ms |
212 KB |
Time limit exceeded |
7 |
Execution timed out |
1075 ms |
212 KB |
Time limit exceeded |
8 |
Execution timed out |
1085 ms |
212 KB |
Time limit exceeded |
9 |
Execution timed out |
1090 ms |
212 KB |
Time limit exceeded |
10 |
Execution timed out |
1091 ms |
212 KB |
Time limit exceeded |