Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cmath>
- #include <iostream>
- #include <stack>
- #include <vector>
- using std::cin;
- using std::cout;
- using std::max;
- using std::min;
- using std::stack;
- using std::string;
- using std::vector;
- using Ll = long long;
- using Pll = std::pair<long long, long long>;
- int main() {
- int num;
- cin >> num;
- vector<Ll> arr(num);
- for (Ll& el : arr) {
- cin >> el;
- }
- vector<int> rt(num);
- vector<int> lt(num);
- rt[num - 1] = num;
- lt[0] = -1;
- stack<Pll> st;
- st.push({arr[num - 1], num - 1});
- for (int i = num - 2; i >= 0; --i) {
- while (!st.empty() && arr[i] <= st.top().first) {
- st.pop();
- }
- if (st.empty()) {
- rt[i] = num;
- } else {
- rt[i] = st.top().second;
- }
- st.push({arr[i], i});
- }
- while (!st.empty()) {
- st.pop();
- }
- st.push({arr[0], 0});
- for (int i = 1; i < num; ++i) {
- while (!st.empty() && arr[i] <= st.top().first) {
- st.pop();
- }
- if (!st.empty()) {
- lt[i] = st.top().second;
- } else {
- lt[i] = -1;
- }
- st.push({arr[i], i});
- }
- Ll ans = -1;
- for (int i = 0; i < num; ++i) {
- Ll now = (rt[i] - lt[i] - 1) * arr[i];
- ans = max(ans, now);
- }
- cout << ans;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement