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;
const int MN=1e6+2;
int N,l[MN],r[MN],a[MN],is[MN];
ll ans=0;
bool cmp(int i,int j) {
return a[i]<a[j];
}
int main() {
ios::sync_with_stdio(0);cin.tie(0);
cin>>N;
a[0]=a[N+1]=INT_MAX;
for(int i=1;i<=N;++i) {
is[i]=i;
l[i]=i-1;
r[i]=i+1;
cin>>a[i];
}
sort(is+1,is+1+N,cmp);
for(int x=1;x<N;++x) {
r[l[is[x]]]=r[is[x]];
l[r[is[x]]]=l[is[x]];
ans+=min(a[l[is[x]]],a[r[is[x]]]);
}
cout<<ans<<'\n';
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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |