题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1657
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=5e4+;
int n,h[N],c[N],q[N],he,tl,cnt[N],ans;
int rdn()
{
int ret=;bool fx=;char ch=getchar();
while(ch>''||ch<''){if(ch=='-')fx=;ch=getchar();}
while(ch>=''&&ch<='') ret=(ret<<)+(ret<<)+ch-'',ch=getchar();
return fx?ret:-ret;
}
int main()
{
n=rdn();for(int i=;i<=n;i++)h[i]=rdn(),c[i]=rdn();
he=;
for(int i=;i<=n;i++)
{
while(he<=tl&&h[i]>h[q[tl]])cnt[i]+=c[q[tl]],tl--;
q[++tl]=i;
}
he=;tl=;
for(int i=n;i;i--)
{
while(he<=tl&&h[i]>h[q[tl]])cnt[i]+=c[q[tl]],tl--;
q[++tl]=i;
}
for(int i=;i<=n;i++)ans=max(ans,cnt[i]);
printf("%d\n",ans);
return ;
}