#include using namespace std; //* ATCODER #include using namespace atcoder; typedef modint998244353 mint; //*/ /* BOOST MULTIPRECISION #include using namespace boost::multiprecision; //*/ typedef long long ll; #define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++) #define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--) template bool chmin(T &a, const T &b) { if (a <= b) return false; a = b; return true; } template bool chmax(T &a, const T &b) { if (a >= b) return false; a = b; return true; } template T max(vector &a){ assert(!a.empty()); T ret = a[0]; for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]); return ret; } template T min(vector &a){ assert(!a.empty()); T ret = a[0]; for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]); return ret; } template T sum(vector &a){ T ret = 0; for (int i=0; i<(int)a.size(); i++) ret += a[i]; return ret; } // defcomp template vector compress(vector &X) { vector vals = X; sort(vals.begin(), vals.end()); vals.erase(unique(vals.begin(), vals.end()), vals.end()); return vals; } // ----- // importbisect template int bisect_left(vector &X, T v){ return lower_bound(X.begin(), X.end(), v) - X.begin(); } template int bisect_right(vector &X, T v){ return upper_bound(X.begin(), X.end(), v) - X.begin(); } // ----- int main(){ int n; cin >> n; vector a(n), b(n); vector c(n); rep(i,0,n) cin >> a[i]; rep(i,0,n) cin >> b[i]; rep(i,0,n){ cin >> c[i]; c[i]--; } ll inf = 2e18 + 5; ll g = sum(b); auto can = [&](ll t) -> bool { mf_graph mf(n+2); rep(i,0,n){ mf.add_edge(n, i, a[i]); } rep(i,0,n){ mf.add_edge(c[i], i, inf); } rep(i,0,n){ mf.add_edge(i, n+1, b[i]*t); } //cout << t << ' ' << mf.flow(2*n, 2*n+1) << ' ' << g*t << endl; if (mf.flow(n, n+1) == g*t){ return true; }else{ return false; } }; ll ub = sum(a) / g + 1; ll lb = 0; //cout << lb << ' ' << ub << endl; while(ub - lb > 1){ ll t = (ub + lb) / 2; if (can(t)){ lb = t; }else{ ub = t; } } cout << lb << '\n'; }