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 int long long
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n; cin>>n;
vector<int> v(n); for(int& i : v) cin>>i;
int s = v[0];
int last = 0;
int mx = v[0];
for(int i = 1; i<n; i++){
if(v[i] <= mx){
s += mx;
}
else{
int tmp = (i-last) * (v[i]-mx) + v[i]; // keep segment
int tmp1 = (n-i-1)*mx + v[i];
mx = v[i];
// cerr<<tmp<<" "<<tmp1<<" "<<last<<endl;
if(tmp <= tmp1){
s+=tmp;
}
else{
s+=tmp1; last = i;
}
}
}
cout<<s<<endl;
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |