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 MAX_N=1e6;
const int MAX_P=MAX_N*4;
const int MAX=1e9;
int val[MAX_N];
int tr[MAX_P];
void init(int node,int a,int b){
if(a==b){
tr[node]=val[a];
return;
}
int mid=(a+b)/2,le=2*node+1,ri=2*node+2;
init(le,a,mid);
init(ri,mid+1,b);
tr[node]=max(tr[le],tr[ri]);
}
int query(int node,int a,int b,int l,int r){
if(b<l || a>r) return -MAX;
if(l<=a && r>=b) return tr[node];
int mid=(a+b)/2,le=2*node+1,ri=2*node+2;
return max(query(le,a,mid,l,r),query(ri,mid+1,b,l,r));
}
int main(){
int n; cin>>n;
for(int i=0;i<n;i++){
int a; cin>>a;
val[i]=a;
}
init(0,0,n-1);
ll res=query(0,0,n-1,0,n-1)*n;
for(int i=1;i<n-1;i++){
ll q1=query(0,0,n-1,i,n-1);
ll q2=query(0,0,n-1,0,i-1);
ll d=n-i;
res=min(res,q1*d+q2*n);
}
cout<<res<<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... |