1 #include<iostream> 2 #include<cstring> 3 #include<algorithm> 4 #include<cmath> 5 #include<cstdio> 6 #include<queue> 7 #define LL long long 8 using namespace std; 9 LL n,s[1000001],a[1000001]; 10 bool vis[1000001]; 11 LL read() 12 { 13 LL x=0,f=1; 14 char ch=getchar(); 15 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} 16 while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} 17 return x*f; 18 } 19 struct data 20 { 21 LL id,s1,p; 22 bool operator<(const data &b)const 23 { 24 if(p!=b.p) return p<b.p; 25 return s1>b.s1; 26 } 27 }; 28 priority_queue<data> q1; 29 priority_queue<data> q2; 30 int main() 31 { 32 n=read(); 33 for(int i=1;i<=n;i++) s[i]=read(); 34 for(int i=1;i<=n;i++) a[i]=read(); 35 for(int i=1;i<=n;i++) q1.push((data){i,s[i],2*s[i]+a[i]}); 36 LL maxnow=0,maxnow2=0; 37 LL ans=0; 38 for(int i=1;i<=n;i++) 39 { 40 data now=(data){0,0,0}; 41 if(!q1.empty()) 42 { 43 now=q1.top(); 44 q1.pop(); 45 while(now.s1<=maxnow&&!q1.empty()){now=q1.top();q1.pop();} 46 if(now.s1>maxnow&&q1.empty())now=(data){0,0,0}; 47 else now.p-=2*maxnow; 48 } 49 data now2=(data){0,0,0}; 50 if(!q2.empty()){now2=q2.top();q2.pop();} 51 if(now.p<now2.p) now.p=now2.p; 52 ans+=now.p; 53 vis[now.id]=1; 54 if(now.s1>maxnow) 55 { 56 maxnow=now.s1; 57 for(++maxnow2;maxnow2<=n&&s[maxnow2]<=maxnow;maxnow2++) 58 { 59 if(!vis[maxnow2]) 60 q2.push((data){maxnow2,s[maxnow2],a[maxnow2]}); 61 } 62 } 63 printf("%I64d\n",ans); 64 } 65 return 0; 66 }
时间: 2024-10-13 01:55:23