#pragma GCC optimize("Ofast") #include using namespace std; typedef long long int ll; typedef unsigned long long int ull; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); ll myRand(ll B) { return (ull)rng() % B; } inline double time() { return static_cast(chrono::duration_cast(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9; } int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int n; cin >> n; vector a(n), b(n), c(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } for (int i = 0; i < n; ++i) { cin >> b[i]; } vector> g(n); vector deg(n); for (int i = 0; i < n; ++i) { cin >> c[i]; c[i] -= 1; deg[c[i]] += 1; g[i].push_back(c[i]); } vector odr, cyc; queue q; for (int i = 0; i < n; ++i) { if (deg[i] == 0) { q.push(i); } } while (q.size()) { int s = q.front(); q.pop(); odr.push_back(s); for (int t : g[s]) { deg[t] -= 1; if (deg[t] == 0) { q.push(t); } } } auto check = [&](ll x) -> bool { auto cp = a; vector used(n); for (int i : odr) { used[i] = true; if (b[i] > 1e17/x) return false; ll need = b[i]*x; if (cp[i] >= need) continue; ll dif = need - cp[i]; cp[c[i]] -= dif; if (cp[c[i]] < -(ll)(2e9)*(ll)n) return false; } for (int i = 0; i < n; ++i) { if (used[i]) continue; ll sa = 0, sb = 0; int j = i; while (!used[j]) { used[j] = true; sa += cp[j]; sb += b[j]; j = c[j]; } // cout << x << " " << sa << " " << sb << endl; if (sa < 0) return false; if (sb == 0) continue; if (sa/sb < x) return false; } return true; }; ll l = 0, r = 1e18; while (r-l > 1) { ll mid = (l+r)/2; if (check(mid)) { l = mid; } else { r = mid; } } cout << l << endl; }