#include <iostream>
#include <stack>
#include <vector>
using namespace std;
int LargestRectangleArea(vector<int> &height)
{
stack<int> sLimit;
int size = height.size();
int width[size];
sLimit.push(-1);
for (int i = 0; i < size; ++i)
{
while(sLimit.top() >= 0)
{
if (height[i] > height[sLimit.top()])
break;
else
sLimit.pop();
}
width[i] = i - sLimit.top() - 1;
sLimit.push(i);
}
for (int i = 0; i < size; ++i)
cout << width[i] << ',';
cout << endl;
sLimit = stack<int>();
sLimit.push(size);
for (int i = size-1; i >= 0; --i)
{
while(sLimit.top() < size)
{
if (height[i] > height[sLimit.top()])
break;
else
sLimit.pop();
}
width[i] += sLimit.top() - i;
sLimit.push(i);
}
for (int i = 0; i < size; ++i)
cout << width[i] << ',';
cout << endl;
int maxArea = 0;
for (int i = 0; i < size; ++i)
{
int area = width[i] * height[i];
if (area > maxArea) maxArea = area;
}
return maxArea;
}
int main(int argc, char** argv)
{
int A[] = {2,1,2};
vector<int> Av;
for (int i = 0; i < sizeof(A)/sizeof(int); ++i)
Av.push_back(A[i]);
cout << LargestRectangleArea(Av);
return 0;
}