Salvens

F

Jul 27th, 2023
901
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.02 KB | None | 0 0
  1. #include <array>
  2. #include <iostream>
  3. #include <vector>
  4. #include <stack>
  5. #include <deque>
  6.  
  7. using namespace std;
  8.  
  9. #define int long long
  10.  
  11. const long long INF = 1e18 + 7;
  12. const int MAXN = 2e5 + 10;
  13. const int N = 2e5;
  14.  
  15. signed main() {
  16.     ios_base::sync_with_stdio(false);
  17.     cin.tie(nullptr);
  18.     cout.tie(nullptr);
  19.     int n;
  20.     cin >> n;
  21.     vector<int> a(n);
  22.     for (int i = 0; i < n; ++i) {
  23.         cin >> a[i];
  24.     }
  25.     vector<int> l(n, -1), r(n, n);
  26.     vector<int> st;
  27.     for (int i = n - 1; i >= 0; --i) {
  28.         while (!st.empty() && a[i] < a[st.back()]) {
  29.             l[st.back()] = i;
  30.             st.pop_back();
  31.         }
  32.         st.push_back(i);
  33.     }
  34.  
  35.     st.clear();
  36.     for (int i = 0; i < n; ++i) {
  37.         while(!st.empty() && a[i] < a[st.back()]) {
  38.             r[st.back()] = i;
  39.             st.pop_back();
  40.         }
  41.         st.push_back(i);
  42.     }
  43.     int ans = 0;
  44.     for (int i = 0; i < n; ++i) {
  45.         ans = max(ans, a[i] * (r[i] - (l[i] + 1)));
  46.     }
  47.     cout << ans << '\n';
  48. }
Advertisement
Add Comment
Please, Sign In to add comment