Ganesh1648

Apple Orchard

Aug 10th, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. #define pb push_back
  6. #define forn(i, n) for (int i = 0; i < n; i++)
  7. #define ll long long
  8. #define endl "\n"
  9.  
  10. const int N =1e5+1;
  11. void solve()
  12. {
  13.    int n;
  14.    cin>>n;
  15.    stack<int> s;
  16.    int a[n];
  17.    forn(i,n)
  18.     cin>>a[i];
  19.    
  20.    int ans=0;
  21.    int i=0;
  22.    for(i=0;i<n;i++)
  23.    {
  24.        while((!s.empty()) && (a[i]<a[s.top()]))
  25.        {
  26.            int top=s.top();
  27.            s.pop();
  28.            if(s.empty())
  29.             ans=max(ans,a[top]*i);
  30.            else
  31.             ans=max(ans,a[top]*(i-s.top()-1));
  32.        }
  33.        s.push(i);
  34.    }
  35.     while(!s.empty())
  36.     {
  37.         int top=s.top();
  38.         s.pop();
  39.         if(s.empty())
  40.             ans=max(ans,i*a[top]);
  41.            
  42.         else
  43.             ans=max(ans,a[top]*(i-s.top()-1));
  44.     }
  45.     cout<<ans<<endl;
  46. }
  47. int main()
  48. {
  49.     ios_base::sync_with_stdio(false);
  50.     cin.tie(NULL);
  51.     cout.tie(NULL);
  52.     solve();
  53. }
Advertisement
Add Comment
Please, Sign In to add comment