NOIP201504推销员

时间:2023-03-09 19:44:11
NOIP201504推销员
 #include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<queue>
#define LL long long
using namespace std;
LL n,s[],a[];
bool vis[];
LL read()
{
LL x=,f=;
char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
struct data
{
LL id,s1,p;
bool operator<(const data &b)const
{
if(p!=b.p) return p<b.p;
return s1>b.s1;
}
};
priority_queue<data> q1;
priority_queue<data> q2;
int main()
{
n=read();
for(int i=;i<=n;i++) s[i]=read();
for(int i=;i<=n;i++) a[i]=read();
for(int i=;i<=n;i++) q1.push((data){i,s[i],*s[i]+a[i]});
LL maxnow=,maxnow2=;
LL ans=;
for(int i=;i<=n;i++)
{
data now=(data){,,};
if(!q1.empty())
{
now=q1.top();
q1.pop();
while(now.s1<=maxnow&&!q1.empty()){now=q1.top();q1.pop();}
if(now.s1>maxnow&&q1.empty())now=(data){,,};
else now.p-=*maxnow;
}
data now2=(data){,,};
if(!q2.empty()){now2=q2.top();q2.pop();}
if(now.p<now2.p) now.p=now2.p;
ans+=now.p;
vis[now.id]=;
if(now.s1>maxnow)
{
maxnow=now.s1;
for(++maxnow2;maxnow2<=n&&s[maxnow2]<=maxnow;maxnow2++)
{
if(!vis[maxnow2])
q2.push((data){maxnow2,s[maxnow2],a[maxnow2]});
}
}
printf("%I64d\n",ans);
}
return ;
}