Ankit_132

D

Dec 16th, 2023
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define ll     long long
  6. #define _test   int _TEST; cin>>_TEST; while(_TEST--)
  7. #define pb     push_back
  8.  
  9. int main()
  10. {
  11.     _test
  12.     {
  13.         int i,j,k,n;
  14.         cin>>n;
  15.         deque<int> que;
  16.         for(int i=0; i<n; i++)
  17.         {
  18.             int z;
  19.             cin>>z;
  20.             que.pb(z);
  21.         }
  22.  
  23.         while(que.front()!=0)
  24.         {
  25.             ll t = que.back();
  26.             que.pop_back();
  27.             que.push_front(t);
  28.         }
  29.  
  30.         vector<int> a;
  31.         while(que.size())
  32.         {
  33.             a.pb(que.front());
  34.             que.pop_front();
  35.         }
  36.  
  37.         vector<ll int> vals(n,n);
  38.         stack<ll> s;
  39.         vector<int> ind(n);
  40.         s.push(0);
  41.  
  42.         for(int i=1; i<n; i++)
  43.         {
  44.             while(s.size() && a[s.top()]>a[i])
  45.                 s.pop();
  46.  
  47.             ind[i] = s.top();
  48.             s.push(i);
  49.         }
  50.  
  51.         for(int i=1; i<n; i++)
  52.             vals[i] = vals[ind[i]] + (i-ind[i])*1ll*a[i];
  53.  
  54.         cout<<*max_element(vals.begin(), vals.end())<<"\n";
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment