-
Notifications
You must be signed in to change notification settings - Fork 156
/
1142 - Advertisement.cpp
48 lines (44 loc) · 1.1 KB
/
1142 - 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
45
46
47
48
/*
Problem Name: Advertisement
Problem Link: https://cses.fi/problemset/task/1142
Author: Sachin Srivastava (mrsac7)
*/
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
signed main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#ifdef LOCAL
freopen("input.txt", "r" , stdin);
freopen("output.txt", "w", stdout);
#endif
int n; cin>>n;
int pre[n] = {0}, suf[n+1] = {0}, a[n];
int ans = 0;
stack<pair<int, int>> q;
for (int i = 0; i < n; i++) {
cin>>a[i];
while(!q.empty() && q.top().first >= a[i])
q.pop();
int x = -1;
if (!q.empty()) {
x = q.top().second;
}
pre[i] = 1 + i-x-1;
q.push({a[i], i});
}
while(!q.empty()) q.pop();
for (int i = n-1; i >= 0; i--) {
while(!q.empty() && q.top().first >= a[i])
q.pop();
int x = n;
if (!q.empty()) {
x = q.top().second;
}
suf[i] = 1 + x-i-1;
q.push({a[i], i});
ans = max(ans, (pre[i]+suf[i]-1)*a[i]);
}
cout<<ans;
}