Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- int main() {
- int numbers_amount;
- std::cin >> numbers_amount;
- std::vector<int> histogram(numbers_amount + 2, -1);
- for (int index = 1; index <= numbers_amount; ++index) {
- std::cin >> histogram[index];
- }
- std::vector<int> right(numbers_amount + 2);
- std::vector<int> stack(1, 0);
- for (int index = 1; index <= numbers_amount + 1; ++index) {
- while (histogram[index] < histogram[stack.back()]) {
- right[stack.back()] = index;
- stack.pop_back();
- }
- stack.push_back(index);
- }
- int64_t max_square = 0;
- std::vector<int> left(numbers_amount + 2, 0);
- for (int index = numbers_amount; index >= 0; --index) {
- while (histogram[index] < histogram[stack.back()]) {
- int a = stack.back();
- stack.pop_back();
- left[a] = index;
- int64_t current_square = static_cast<int64_t>(histogram[a]) * (right[a] - left[a] - 1);
- if (current_square > max_square) {
- max_square = current_square;
- }
- }
- stack.push_back(index);
- }
- std::cout << max_square;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement