-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBCRECT-Hcn lon- Stack.cpp
More file actions
53 lines (46 loc) · 1.01 KB
/
BCRECT-Hcn lon- Stack.cpp
File metadata and controls
53 lines (46 loc) · 1.01 KB
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
45
46
47
48
49
50
51
52
53
#include<bits/stdc++.h>
using namespace std;
#define For(i, a, b) for(int i = a; i <= b; i++)
#define Ford(i, a, b) for(int i = a; i >= b; i--)
#define ll long long
#define here cout<<"i am here"<<endl;
const long long base= 1e9 + 7;
const int maxn = 1000000;
int m,n,h[maxn],pos_min[maxn],pos_max[maxn];
stack <int> St;
ll Asw;
void solve(){
St.push(0);
For(i, 1, n){
while(St.empty() == false && h[St.top()] >= h[i]) St.pop();
if(h[i]) pos_min[i] = St.top() + 1;
St.push(i);
}
St.pop();
St.push(n+1);
Ford(i, n, 1){
while(St.empty() == false && h[St.top()] >= h[i]) St.pop();
if(h[i]) pos_max[i] = St.top() - 1;
St.push(i);
}
For(i, 1, n){
if(h[i]){
ll val = 1ll * h[i] * (pos_max[i] - pos_min[i] + 1);
if(val > Asw) Asw = val;
}
}
}
int main(void){
ios::sync_with_stdio(false);
cin>>m>>n;
For(i, 1, n){
cin>>h[i];
}
solve();
For(i, 1, n){
h[i] = m - h[i];
}
solve();
cout<<Asw;
return 0;
}