1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| #include <bits/stdc++.h> using namespace std; using ll = long long; const int maxn = 1e5+10; const int inf = 0x3f3f3f3f;
struct node { int index,val,par,ch[2]; node() {} node(int a,int b,int c):index(a),val(b),par(c) { ch[0] = ch[1] = 0; } bool operator< (node &temp) { return this->index<temp.index; } } tree[maxn];
int root,top; ll ans;
int build(int n) { for (int i=1; i<=n; ++i) { int k = i-1; while (tree[k].val > tree[i].val) k = tree[k].par; tree[i].ch[0] = tree[k].ch[1]; tree[k].ch[1] = i; tree[i].par = k; int i_right = tree[i].ch[0]; tree[i_right].par = i; } return tree[0].ch[1]; }
int dfs(int x) { if (!x) return 0; int left = tree[x].ch[0]; int right = tree[x].ch[1]; int size = dfs(left); size += dfs(right); ans = max(ans,(size+1) * (ll)tree[x].val); return size+1; }
int main() { int n,hi; while (cin >> n && n) { tree[0] = node(0,0,0); for (int i=1; i<=n; ++i) { cin >> hi; tree[i] = node(i,hi,0); } root = build(n); ans = 0; dfs(root); cout << ans << endl; } return 0; }
|