Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stack>
- using namespace std;
- int main() {
- int i,j,t,n;
- cin>>t;
- while(t--)
- {
- cin>>n;
- long long a[n];
- for(i=0;i<n;i++)cin>>a[i];
- stack<int>s;
- long long sm,maxx=0;
- i=0;
- while(i<n)
- {
- if(s.empty()||a[s.top()]<=a[i])s.push(i++);
- else{
- j=s.top();
- s.pop();
- sm=a[j]*(s.empty()?i:i-s.top()-1);
- maxx=maxx>sm?maxx:sm;
- }
- }
- while(!s.empty())
- {
- j=s.top();
- s.pop();
- sm=a[j]*(s.empty()?i:i-s.top()-1);
- maxx=maxx>sm?maxx:sm;
- }
- cout<<maxx<<endl;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment