#include namespace { #pragma GCC diagnostic ignored "-Wunused-function" #include #pragma GCC diagnostic warning "-Wunused-function" using namespace std; using namespace atcoder; #define rep(i,n) for(int i = 0; i < (int)(n); i++) #define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--) #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) template bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; } template bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; } using ll = long long; using P = pair; using VI = vector; using VVI = vector; using VL = vector; using VVL = vector; std::vector> scc_dag(std::vector>& scc, std::vector>& graph) { int n = graph.size(); int m = scc.size(); std::vector d(n); for(int i = 0; i < m; ++i) for(int v: scc[i]) d[v] = i; std::vector> to(m); std::vector added(m, -1); for(int i = 0; i < m; ++i) { added[i] = i; for(int u: scc[i]) for(int x: graph[u]) { int v = d[x]; if (added[v] != i) { added[v] = i; to[i].push_back(v); } } } return to; } } int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; VI a(n), b(n), c(n); rep(i, n) cin >> a[i]; rep(i, n) cin >> b[i]; scc_graph g(n); VVI to(n); rep(i, n) { int c; cin >> c; g.add_edge(c - 1, i); to[c-1].emplace_back(i); } auto scc = g.scc(); int sz = scc.size(); auto toc = scc_dag(scc, to); constexpr long long INF = 1002003004005006007; ll l = 0, r = INF; while(r - l > 1) { ll mid = (l + r) / 2; VL dp(sz); rrep(i, sz) { for(int j : toc[i]) if (dp[j] < 0) dp[i] += dp[j], dp[j] = 0; for(int x : scc[i]) { ll v; if (__builtin_mul_overflow(mid, b[x], &v)) dp[i] = -INF; else dp[i] += a[x] - v; } } bool ok = all_of(all(dp), [](ll x) { return x >= 0; }); (ok ? l : r) = mid; } cout << l << '\n'; }