from collections import defaultdict, deque N = int(input()) A = list(map(int, input().split())) B = list(map(int, input().split())) adj = defaultdict(list) for a, b in zip(A, B): adj[a].append(b) ans = 1 used = set() q = deque([1]) while q: x = q.popleft() if x in used: continue used.add(x) ans = max(ans, x) for v in adj[x]: if x+v in used: continue q.append(x+v) print(ans)