#include #include using namespace std; using namespace atcoder; //using mint = modint998244353; //const int mod = 998244353; //using mint = modint1000000007; //const int mod = 1000000007; //const int INF = 1e9; //const long long LINF = 1e18; #define rep(i, n) for (int i = 0; i < (n); ++i) #define rep2(i,l,r)for(int i=(l);i<(r);++i) #define rrep(i, n) for (int i = (n-1); i >= 0; --i) #define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i) #define all(x) (x).begin(),(x).end() #define allR(x) (x).rbegin(),(x).rend() #define P pair template inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; } vector> f(vector>&scc, vector>&to) { int n = to.size(); int m = scc.size(); vectorv(n); rep(i, m)rep(j, scc[i].size()) v[scc[i][j]] = i; vector>res(m); rep(i, m) { for (int j : scc[i]) { for (int k : to[j]) { int l = v[k]; if (i == l)continue; res[i].push_back(l); } } std::sort(res[i].begin(), res[i].end()); res[i].erase(unique(res[i].begin(), res[i].end()), res[i].end()); } return res; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vectora(n), b(n), c(n); rep(i, n)cin >> a[i]; rep(i, n)cin >> b[i]; scc_graph g(n); vector>to(n); rep(i, n) { int c; cin >> c; c--; to[c].push_back(i); g.add_edge(c, i); } auto scc = g.scc(); int sz = scc.size(); to = f(scc, to); long long ok = 0; long long ng = accumulate(all(a), 0LL) / accumulate(all(b), 0LL) + 1; while (1 < abs(ok - ng)) { long long mid = (ng + ok) / 2; auto check = [&]() { vectordp(sz); rrep(i, sz) { for (int j : to[i]) if (dp[j] < 0) dp[i] += dp[j], dp[j] = 0; for (int j : scc[i]) dp[i] += a[j] - mid * b[j]; } return all_of(all(dp), [](long long x) { return x >= 0; }); }; if (check()) ok = mid; else ng = mid; } cout << ok << endl; return 0; }