-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAdvertisement.cpp
44 lines (38 loc) · 969 Bytes
/
Advertisement.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<ll,ll>
#define F first
#define S second
const int MAX = 1e9+5;
const int N = 200005;
pii segtree[4*N];
int ara[N],n;
void build(int node,int l,int r ){
if( l == r ){
segtree[node] = {ara[l],l};
return;
}
int mid = (l+r)/2;
build(node*2,l,mid);
build(node*2+1,mid+1,r);
segtree[node] = min( segtree[node*2],segtree[node*2+1] );
}
pii query(int node,int L,int R,int l,int r){
if( l>R or r<L ) return {MAX,-1};
if( l>=L and r<=R ) return segtree[node];
int mid = (l+r)/2;
return min( query(node*2,L,R,l,mid), query(node*2+1,L,R,mid+1,r) );
}
ll getRect(int l,int r){
if( l>r ) return 0;
auto pp = query(1,l,r,0,n-1);
ll res = (r-l+1)*pp.F;
return max({ res, getRect(l,pp.S-1),getRect(pp.S+1,r) });
}
int main(){
cin>>n;
for(int i=0;i<n;i++) cin>>ara[i];
build(1,0,n-1);
cout<<getRect(0,n-1);
}