Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <bits/stdc++.h>
- using namespace std;
- #define pb push_back
- #define forn(i, n) for (int i = 0; i < n; i++)
- #define ll long long
- #define endl "\n"
- const int N =1e5+1;
- void solve()
- {
- int n;
- cin>>n;
- stack<int> s;
- int a[n];
- forn(i,n)
- cin>>a[i];
- int ans=0;
- int i=0;
- for(i=0;i<n;i++)
- {
- while((!s.empty()) && (a[i]<a[s.top()]))
- {
- int top=s.top();
- s.pop();
- if(s.empty())
- ans=max(ans,a[top]*i);
- else
- ans=max(ans,a[top]*(i-s.top()-1));
- }
- s.push(i);
- }
- while(!s.empty())
- {
- int top=s.top();
- s.pop();
- if(s.empty())
- ans=max(ans,i*a[top]);
- else
- ans=max(ans,a[top]*(i-s.top()-1));
- }
- cout<<ans<<endl;
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- cout.tie(NULL);
- solve();
- }
Advertisement
Add Comment
Please, Sign In to add comment