-
//考场代码...结果脑子一抽考场上用了堆维护第一维关系... 所以这个代码效率是O(N log^2 N)
-
#include <bits/stdc++.h>
-
#define pa pair<long long,long long>
-
using namespace std;
-
inline long long read(){
-
long long s=0,w=1;
-
char ch=getchar();
-
while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)w=-1;ch=getchar();}
-
while(ch>=‘0‘&&ch<=‘9‘) s=s*10+ch-‘0‘,ch=getchar();
-
return s*w;
-
}
-
int N;
-
long long Tree[4005005];
-
void Change(int Now,int K,int l,int r,long long Ned)
-
{
-
int mid=(l+r)/2;
-
if (l==r&&l==K)
-
{
-
Tree[Now]=Ned;
-
return;
-
}
-
if (K<=mid)
-
Change(Now*2,K,l,mid,Ned);
-
if (mid<K)
-
Change(Now*2+1,K,mid+1,r,Ned);
-
Tree[Now]=max(Tree[Now*2],Tree[Now*2+1]);
-
}
-
long long query(int Now,int L,int R,int l,int r)
-
{
-
int mid=(l+r)/2;
-
if (L<=l&&r<=R)
-
return Tree[Now];
-
long long qwqq=0;
-
if (L<=mid)
-
qwqq=max(query(Now*2,L,R,l,mid),qwqq);
-
if (mid<R)
-
qwqq=max(query(Now*2+1,L,R,mid+1,r),qwqq);
-
return qwqq;
-
}
-
long long dp[1000005],a[1000005],t[1000005],Score[1000005];
-
priority_queue<pa,vector<pa>,greater<pa> > qwqq;
-
int main()
-
{
-
freopen("fc.in","r",stdin);
-
freopen("fc.out","w",stdout);
-
N=read();
-
for (int i=1;i<=N;i++)
-
t[i]=read();
-
for (int i=1;i<=N;i++)
-
a[i]=read();
-
for (int i=1;i<=N;i++)
-
Score[i]=a[i]*t[i];
-
for (int i=1;i<=N;i++)
-
{
-
pa r;
-
if (!qwqq.empty()) r=qwqq.top();
-
while (!qwqq.empty()&&r.first<=i)
-
{
-
Change(1,r.second,1,N,dp[r.second]);
-
qwqq.pop();
-
r=qwqq.top();
-
}
-
long long R=0;
-
if (i-t[i]>0)
-
R=query(1,1,i-t[i],1,N);
-
dp[i]=R+Score[i];
-
pa Now;
-
Now.first=i+t[i];
-
Now.second=i;
-
qwqq.push(Now);
-
}
-
long long ans=0;
-
for (int i=1;i<=N;i++)
-
ans=max(ans,dp[i]);
-
printf("%lld",ans);
-
return 0;
-
}