N=int(input()) A=list(map(int,input().split())) B=list(map(int,input().split())) dp=[0]*1000005 dp[1]=1 use=[[] for i in range(1000005)] for i in range(N): use[A[i]].append(B[i]) ans=0 for i in range(1000005): if dp[i]==0: continue ans=i for j in use[i]: dp[i+j]=1 print(ans)