from collections import defaultdict, deque from atcoder.scc import SCCGraph def bsearch(low: int, high: int, pred) -> int: assert pred(low) lo = low hi = high res = low while lo <= hi: m = (lo + hi) // 2 if pred(m): res = max(res, m) lo = m + 1 else: hi = m - 1 return res def scc(n: int, adj: dict[int, list[int]]) -> list[list[int]]: """グラフを強連結成分分解する。 Args: n: 頂点数 adj: 隣接リスト {u: [v...]} Returns: 強連結成分のリスト (トポロジカル順) """ g = SCCGraph(n) for u, vs in adj.items(): for v in vs: g.add_edge(u, v) return g.scc() INF = 1 << 62 N = int(input()) A = list(map(int, input().split())) B = list(map(int, input().split())) C = list(map(lambda x: int(x)-1, input().split())) adj = defaultdict(list) for i, c in enumerate(C): adj[c].append(i) # m 本作れるか def can(m: int) -> bool: xs = [] # 本数の余り(不足) for a, b in zip(A, B): xs.append(a - b * m) # Functional Graph で交換可能な本数の余り(不足)を伝搬していく for g in cc: gset = set(g) x = 0 for v in g: x += xs[v] xs[v] = 0 xs[g[0]] = x # 一カ所にまとめる # 花が余っているなら(x > 0)、次の SCC へ伝搬 if x <= 0: continue moved = False for v in g: if moved: break for to in adj[v]: if to in gset: continue xs[to] += x # 伝搬 # xs[g[0]] = 0 moved = True break return all(x >= 0 for x in xs) # 全ノードで不足が存在しない cc = scc(N, adj) ans = bsearch(0, INF, can) print(ans)