Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define ll long long
- #define el endl
- #define umi unordered_map<int, int>
- #define umll unordered_map<ll, ll>
- #define all(vect) vect.begin(), vect.end()
- #define reset(A) memset(A, 0, sizeof(A))
- #define approx(n) fixed << setprecision(n)
- const int mod = 1e9 + 7;
- using namespace std;
- void solve()
- {
- int n;
- cin >> n;
- int a[n + 1];
- for(int i = 1; i <= n; i++)
- {
- cin >> a[i];
- }
- int r[n + 1];
- int l[n + 1];
- reset(l);
- reset(r);
- stack<int> st, sh;
- st.push(1);
- for(int i = 2; i <= n; i++)
- {
- if(a[i] > a[st.top()])
- {
- l[i] = st.top();
- st.push(i);
- }
- else
- {
- while(a[st.top()] >= a[i])
- {
- st.pop();
- if(st.empty())
- break;
- }
- if(st.empty())
- l[i] = 0;
- else
- l[i] = st.top();
- st.push(i);
- }
- }
- // for(int i = 1; i <= n; i++)
- // cout << l[i] << " ";
- r[n] = n + 1;
- sh.push(n);
- for(int i = n - 1; i > 0; i--)
- {
- if(a[i] > a[sh.top()])
- {
- r[i] = sh.top();
- sh.push(i);
- }
- else
- {
- while(a[sh.top()] >= a[i])
- {
- sh.pop();
- if(sh.empty())
- break;
- }
- if(sh.empty())
- r[i] = n + 1;
- else
- r[i] = sh.top();
- sh.push(i);
- }
- }
- // cout << el;
- // for(int i = 1; i <= n; i++)
- // cout << r[i] << " ";
- int maxx = 0;
- for(int i = 1; i <= n; i++)
- {
- maxx = max(maxx, (r[i] - l[i] - 1) * a[i]);
- }
- cout << maxx << el;
- }
- int main()
- {
- int t = 1;
- cin >> t;
- // cin.ignore();
- while(t--)
- {
- solve();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement