-
Notifications
You must be signed in to change notification settings - Fork 1
/
DSA07019.cpp
50 lines (49 loc) · 1.06 KB
/
DSA07019.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
49
50
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++)
cin>>a[i];
stack<long long> st;
int i = 0;
long long mx = 0;
long long ans=0;
while(i<n)
{
if(st.empty() or a[st.top()]<=a[i])
{
st.push(i++);
}
else
{
int t = st.top();
st.pop();
if(st.empty())
{
mx = a[t]*i;
}
else
mx = a[t]*(i-st.top()-1);
ans = max(mx,ans);
}
}
while(!st.empty())
{
int t = st.top();
st.pop();
if(st.empty())
mx = a[t]*i;
else
mx = a[t]*(i-st.top()-1);
ans = max(mx,ans);
}
cout<<ans<<endl;
}
}