We need to solve the classic largest rectangle in histogram problem. Given N up to 1e5, heights up to 1e9. Compute maximum area rectangle that can be formed under the histogram, with the rectangle aligned with bars, i.e., width = number of contiguous bars, height = minimum height among those bars.
We need to output area.
We can solve using stack (monotonic increasing) O(N). The algorithm: maintain a stack of indices with increasing heights. Iterate through each bar (i from 0 to N-1). While stack not empty and height[stack.top] > height[i], pop top. For popped element, we compute area: height[top] * (i - stack.top_after_pop - 1) if stack not empty, else i. Actually careful: when we pop, we consider the bar's height extending to the left as far as possible until a lower bar. We can compute width as (i - left - 1) where left is index of new top after popping, or -1 if stack empty. Then after processing all, we need to process remaining stack with i = N.
Edge cases: heights can be zero. Should work.
Also note: if N=0? but constraints N>=1.
Implementation in C++? Problem likely from some contest; but we just need answer. We'll write solution in C++.
Let's produce final solution.
Examples:
Input: 8 2 1 3 5 3 4 2 1 -> output 12
Input: 3 2 0 1 -> output 2
Check: histogram heights [2,0,1] largest rectangle: could be bar 0 height 2 width1 ->2, bar2 height1 width1 ->1, but rectangle spanning bars 0 and 2? cannot because bar1 is 0. Actually maybe rectangle of height 0? area 0. So max area is 2. That matches.
We'll implement.
Consider using long long for area because heights up to 1e9 and N up to 1e5 so area up to 1e14, fits in 64-bit.
Now produce answer.
But also there is alternative solution: we can compute left smaller and right smaller arrays. That's also O(N). But stack method is simpler.
Let's write code:
Pseudo:
int N;
cin >> N;
vector h(N);
for(auto &x: h) cin >> x;
stack s;
long long max_area = 0;
int i = 0;
while(i < N) {
if(s.empty() || h[s.top()] <= h[i]) {
s.push(i++);
} else {
int tp = s.top(); s.pop();
long long area = h[tp] * (s.empty() ? i : i - s.top() - 1);
max_area = max(max_area, area);
}
}
while(!s.empty()) {
int tp = s.top(); s.pop();
long long area = h[tp] * (s.empty() ? N : N - s.top() - 1);
max_area = max(max_area, area);
}
cout << max_area << endl;
But careful: we need to handle equality: if heights equal, we can push; but we need to ensure proper width when popping equal heights? Actually if we push equal heights, then when we encounter a later bar with same height, if we pop earlier because condition > (strict), we won't pop equal heights, so the width for equal heights will be determined when the later equal height gets popped. That's fine.
Alternatively we could compute left and right boundaries.
We'll produce code.
Now consider input format: first line N, second line N integers. Could be multiple lines? Actually they said "Input is given in the following format. N then h1 h2 ... hN". So we can read accordingly.
Potential pitfalls: using int for indices is fine.
We'll output as long long.
We'll provide final solution with explanation.
Let's write it in a proper style.
We'll produce final answer. ```cpp
#include
#include
#include
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int N;
cin >> N;
vector<ll> h(N);
for (int i = 0; i < N; ++i) cin >> h[i];
stack<int> stk;
ll max_area = 0;
for (int i = 0; i < N; ++i) {
// Pop bars that are taller than the current one.
// For each popped bar, compute the rectangle area that uses its height.
while (!stk.empty() && h[stk.top()] > h[i]) {
int tp = stk.top();
stk.pop();
// Width = (current index) - (index of previous lower bar) - 1
// If stack becomes empty, the bar can extend all the way to the left (index 0).
ll width = stk.empty() ? i : i - stk.top() - 1;
max_area = max(max_area, h[tp] * width);
}
stk.push(i);
}
// Process the remaining bars in the stack (they have no smaller bar to the right).
while (!stk.empty()) {
int tp = stk.top();
stk.pop();
ll width = stk.empty() ? N : N - stk.top() - 1;
max_area = max(max_area, h[tp] * width);
}
cout << max_area << '\n';
return 0;
}