chiyeuemthoi
Senior Member
![angry :angry: :angry:](https://data.voz.vn/styles/next/xenforo/smilies/popopo/angry.png?v=01)
![angry :angry: :angry:](https://data.voz.vn/styles/next/xenforo/smilies/popopo/angry.png?v=01)
C++:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
int a[n];
for (int &x : a)
cin >> x;
stack<int> idx, hei;
idx.push(0);
hei.push(a[0]);
ll res = 0;
for (int i = 1; i < n; i++)
{
if (hei.top() > a[i])
{
int idxTmp;
while (idx.size() && hei.size() && hei.top() > a[i])
{
res = max(res, 1LL * hei.top() * (i - idx.top()));
hei.pop();
idxTmp = idx.top();
idx.pop();
}
hei.push(a[i]);
idx.push(idxTmp);
}
else
{
hei.push(a[i]);
idx.push(i);
}
}
res = max(res, 1LL * hei.top());
int idx1 = idx.top();
hei.pop();
idx.pop();
while (hei.size() && idx.size())
{
int hei2 = hei.top();
int idx2 = idx.top();
res = max(res, 1LL * hei2 * (idx1 - idx2 + 1));
hei.pop();
idx.pop();
}
cout << res << endl;
}
}
}