#include #include // WA(?) #define all(x) x.begin(), x.end() #define INF 1000000000000000000ll using namespace std; using namespace atcoder; using ll = long long; ll get_dp(ll mid, int n, vector& a, vector& b, vector >& route, vector& is_source, int x) { ll ret = a[x] - b[x] * mid; for (int k : route[x]) { ret += get_dp(mid, n, a, b, route, is_source, k); } if (ret > 0) { ret = 0; } return ret; } bool check(ll mid, int n, vector& a, vector& b, vector >& route, vector& is_source) { for (int i = 0; i < n; i++) { if (is_source[i] == false or get_dp(mid, n, a, b, route, is_source, i) >= 0) { continue; } return false; } return true; } int main(void){ int n; cin >> n; vector a(n), b(n); vector c(n); ll sa = 0, sb = 0; for (int i = 0; i < n; i++) { cin >> a[i]; sa += a[i]; } for (int i = 0; i < n; i++) { cin >> b[i]; sb += b[i]; } for (int i = 0; i < n; i++) { cin >> c[i]; c[i]--; } vector > route(n, vector({})); vector is_source(n, true); for (int i = 0; i < n; i++) { if (i == c[i]) { continue; } route[c[i]].push_back(i); is_source[i] = false; } ll ok = 0; ll ng = sa / sb + 1; while (ng - ok > 1) { ll mid = (ok + ng) >> 1ll; if (check(mid, n, a, b, route, is_source)) { ok = mid; } else { ng = mid; } } cout << ok << endl; return 0; }