#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];
ll n;
int unir(int a,int b){
if(val[a]>val[b]) return a;
else{
if(val[a]==val[b]) return min(a,b);
else return b;
}
}
void init(int node,int a,int b){
if(a==b){
tr[node]=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]=unir(tr[le],tr[ri]);
}
ll query(int node,int a,int b,int l,int r){
if(b<l || a>r) return n;
if(l<=a && r>=b) return tr[node];
int mid=(a+b)/2,le=2*node+1,ri=2*node+2;
return unir(query(le,a,mid,l,r),query(ri,mid+1,b,l,r));
}
ll maxres(int l,int r){
if(l==r) return val[l]*n;
int i=query(0,0,n-1,l,r);
int d=(n-i);
int j=query(0,0,n-1,l,i-1);
if(val[i]*n<val[i]*d+val[j]*n){
return val[i]*n;
}
else{
return val[i]*d+maxres(l,i-1);
}
}
int main(){
cin>>n;
for(int i=0;i<n;i++){
int a; cin>>a;
val[i]=a;
}
val[n]=-MAX;
init(0,0,n-1);
cout<<maxres(0,n-1)<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
292 KB |
Output is correct |
2 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
387 ms |
13536 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
292 KB |
Output is correct |
2 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
292 KB |
Output is correct |
2 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |