Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 2778 | Accepted: 1413 |
Description
Input
Output
Sample Input
3 1 2 3 4 1 2 3 3 4 1 2 3 4 -1
Sample Output
12 14
Source
#include<cstdio> #include<iostream> #include<algorithm> #include<vector> #include<cstring> #include<queue> #include<stack> using namespace std; const int maxn = 50000 + 5; const int INF = 2000000000; typedef pair<int, int> P; typedef long long LL; int w[maxn],h[maxn]; int l[maxn],r[maxn]; int sum[maxn]; stack<P> S; int main(){ int n; while(scanf("%d",&n)){ if(n == -1) break; for(int i = 0;i < n;i++){ scanf("%d%d",&w[i],&h[i]); } sum[0] = w[0]; for(int i = 1;i < n;i++) sum[i] = sum[i-1]+w[i]; while(!S.empty()) S.pop(); for(int i = 0;i < n;i++){ while(!S.empty() && h[i] <= S.top().first) S.pop(); if(S.size() == 0) l[i] = sum[i]; else l[i] = sum[i]-sum[S.top().second]; S.push(P(h[i],i)); } while(!S.empty()) S.pop(); for(int i = n-1;i >= 0;i--){ while(!S.empty() && h[i] <= S.top().first) S.pop(); if(S.size() == 0) r[i] = sum[n-1]-sum[i-1]; else r[i] = sum[S.top().second-1]-sum[i-1]; S.push(P(h[i],i)); } int ans = 0; for(int i = 0;i < n;i++){ ans = max(ans,h[i]*(l[i]+r[i]-w[i])); } printf("%d\n",ans); } return 0; }