import sys input = lambda :sys.stdin.readline()[:-1] ni = lambda :int(input()) na = lambda :list(map(int,input().split())) yes = lambda :print("yes");Yes = lambda :print("Yes");YES = lambda : print("YES") no = lambda :print("no");No = lambda :print("No");NO = lambda : print("NO") ####################################################################### m = 5 * 10 ** 5 + 10 n = ni() a = na() b = na() f = [[] for i in range(m)] for i in range(n): f[a[i]].append(b[i] + a[i]) dp = [i for i in range(m)] for i in range(m-1, 0, -1): for j in f[i]: if j >= m: dp[i] = max(dp[i], j) else: dp[i] = max(dp[i], dp[j]) print(dp[1])