#include #include #include using namespace std; using namespace atcoder; using mint = modint998244353; #define rep(i,n) for (int i = 0; i < (n); ++i) #define Inf32 1000000001 #define Inf64 4000000000000000001 int main(){ int n; cin>>n; vector a(n),b(n); rep(i,n)cin>>a[i]; rep(i,n)cin>>b[i]; vector c(n); scc_graph S(n); rep(i,n){ cin>>c[i]; c[i]--; S.add_edge(c[i],i); } auto s = S.scc(); vector pos(n); vector aa(s.size()),bb(s.size()); vector> E(s.size()); rep(i,s.size()){ rep(j,s[i].size()){ pos[s[i][j]] = i; aa[i] += a[s[i][j]]; bb[i] += b[s[i][j]]; } } rep(i,n){ if(pos[i]==pos[c[i]])continue; E[pos[i]].push_back(pos[c[i]]); } long long ok = 0,ng = 2000000000000000; while(ng-ok>1LL){ long long mid =(ok+ng)/2; bool f = true; auto ta = aa,tb = bb; vector<__int128> need(s.size()); for(int i=s.size()-1;i>=0;i--){ __int128 t = bb[i]; t *= mid; need[i] += t; if(ta[i]>=need[i]){ continue; } if(E[i].size()==0){ f = false; break; } need[E[i][0]] += need[i] - ta[i]; } if(f)ok = mid; else ng = mid; } cout<