Guest User

Untitled

a guest
Oct 24th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.55 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.    
  7.     int n,i,j;
  8.     while(true)
  9.     {
  10.     cin>>n;
  11.     if(n==0)
  12.     break;
  13.     int ar[n],l[n],r[n];
  14.     for(i=0;i<n;++i)
  15.     cin>>ar[i],l[i]=-1,r[i]=n;
  16.    
  17.     stack<int> s;
  18.    
  19.     s.push(0);
  20.    
  21.     for(i=1;i<n;++i)
  22.     {
  23.         while(!s.empty() && ar[s.top()]>ar[i])
  24.         {
  25.             r[s.top()]=i;
  26.             s.pop();
  27.         }
  28.         if(!s.empty())
  29.         l[i]=s.top();
  30.         s.push(i);
  31.     }
  32.    
  33.     long long maxi=0;
  34.     for(i=0;i<n;++i)
  35.     {
  36.     maxi=max(maxi,1LL*(r[i]-l[i]-1)*ar[i]);
  37.     }
  38.     cout<<maxi<<endl;
  39.     }
  40.     return 0;
  41. }
Add Comment
Please, Sign In to add comment