Accepted
代码
#include<bits/stdc++.h>
using namespace std;
int n,s[50002],d[50002],ans[50002],ANS,a[50002],b[50002],r,i;
int main()
{
cin>>n;
for(i=1;i<=n;i++)scanf("%d%d",&a[i],&b[i]);
s[1]=a[1];d[1]=1;r=1;
for(i=2;i<=n;i++)
{
while(r!=0&&a[i]>s[r]){ans[i]+=b[d[r]];r--;}
r++;
s[r]=a[i];
d[r]=i;
}
s[1]=a[n];d[1]=n;r=1;
for(i=n-1;i>=1;i--)
{
while(r!=0&&a[i]>s[r]){ans[i]+=b[d[r]];r--;}
r++;
s[r]=a[i];
d[r]=i;
}
for(i=1;i<=n;i++)ANS=max(ANS,ans[i]);
cout<<ANS;
return 0;
}