#ifndef HIDDEN_IN_VS // 折りたたみ用 // 警告の抑制 #define _CRT_SECURE_NO_WARNINGS // ライブラリの読み込み #include using namespace std; // 型名の短縮 using ll = long long; // -2^63 ~ 2^63 = 9 * 10^18(int は -2^31 ~ 2^31 = 2 * 10^9) using pii = pair; using pll = pair; using pil = pair; using pli = pair; using vi = vector; using vvi = vector; using vvvi = vector; using vvvvi = vector; using vl = vector; using vvl = vector; using vvvl = vector; using vvvvl = vector; using vb = vector; using vvb = vector; using vvvb = vector; using vc = vector; using vvc = vector; using vvvc = vector; using vd = vector; using vvd = vector; using vvvd = vector; template using priority_queue_rev = priority_queue, greater>; using Graph = vvi; // 定数の定義 const double PI = acos(-1); const vi DX = { 1, 0, -1, 0 }; // 4 近傍(下,右,上,左) const vi DY = { 0, 1, 0, -1 }; int INF = 1001001001; ll INFL = 4004004003104004004LL; // (int)INFL = 1010931620; // 入出力高速化 struct fast_io { fast_io() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } } fastIOtmp; // 汎用マクロの定義 #define all(a) (a).begin(), (a).end() #define sz(x) ((int)(x).size()) #define lbpos(a, x) (int)distance((a).begin(), std::lower_bound(all(a), x)) #define ubpos(a, x) (int)distance((a).begin(), std::upper_bound(all(a), x)) #define Yes(b) {cout << ((b) ? "Yes\n" : "No\n");} #define rep(i, n) for(int i = 0, i##_len = int(n); i < i##_len; ++i) // 0 から n-1 まで昇順 #define repi(i, s, t) for(int i = int(s), i##_end = int(t); i <= i##_end; ++i) // s から t まで昇順 #define repir(i, s, t) for(int i = int(s), i##_end = int(t); i >= i##_end; --i) // s から t まで降順 #define repe(v, a) for(const auto& v : (a)) // a の全要素(変更不可能) #define repea(v, a) for(auto& v : (a)) // a の全要素(変更可能) #define repb(set, d) for(int set = 0; set < (1 << int(d)); ++set) // d ビット全探索(昇順) #define repp(a) sort(all(a)); for(bool a##_perm = true; a##_perm; a##_perm = next_permutation(all(a))) // a の順列全て(昇順) #define smod(n, m) ((((n) % (m)) + (m)) % (m)) // 非負mod #define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} // 重複除去 #define EXIT(a) {cout << (a) << endl; exit(0);} // 強制終了 #define inQ(x, y, u, l, d, r) ((u) <= (x) && (l) <= (y) && (x) < (d) && (y) < (r)) // 半開矩形内判定 // 汎用関数の定義 template inline ll pow(T n, int k) { ll v = 1; rep(i, k) v *= n; return v; } template inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; } // 最大値を更新(更新されたら true を返す) template inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; } // 最小値を更新(更新されたら true を返す) template inline T get(T set, int i) { return (set >> i) & T(1); } // 演算子オーバーロード template inline istream& operator>>(istream& is, pair& p) { is >> p.first >> p.second; return is; } template inline istream& operator>>(istream& is, vector& v) { repea(x, v) is >> x; return is; } template inline vector& operator--(vector& v) { repea(x, v) --x; return v; } template inline vector& operator++(vector& v) { repea(x, v) ++x; return v; } #endif // 折りたたみ用 #if __has_include() #include using namespace atcoder; #ifdef _MSC_VER #include "localACL.hpp" #endif //using mint = modint1000000007; using mint = modint998244353; //using mint = modint; // mint::set_mod(m); namespace atcoder { inline istream& operator>>(istream& is, mint& x) { ll x_; is >> x_; x = x_; return is; } inline ostream& operator<<(ostream& os, const mint& x) { os << x.val(); return os; } } using vm = vector; using vvm = vector; using vvvm = vector; using vvvvm = vector; #endif #ifdef _MSC_VER // 手元環境(Visual Studio) #include "local.hpp" #else // 提出用(gcc) inline int popcount(int n) { return __builtin_popcount(n); } inline int popcount(ll n) { return __builtin_popcountll(n); } inline int lsb(int n) { return n != 0 ? __builtin_ctz(n) : -1; } inline int lsb(ll n) { return n != 0 ? __builtin_ctzll(n) : -1; } inline int msb(int n) { return n != 0 ? (31 - __builtin_clz(n)) : -1; } inline int msb(ll n) { return n != 0 ? (63 - __builtin_clzll(n)) : -1; } #define dump(...) #define dumpel(v) #define dump_list(v) #define dump_mat(v) #define input_from_file(f) #define output_to_file(f) #define Assert(b) { if (!(b)) while (1) cout << "OLE"; } #endif //【参照付きグラフの辺】 /* * int from : 始点 * int to : 終点 * int id : 辺番号 * bool dir : 順方向か */ struct IEdge { // verify : https://judge.yosupo.jp/problem/cycle_detection_undirected int from; // 始点 int to; // 終点 int id; // 辺番号 bool dir; // 順方向か IEdge() : from(-1), to(-1), id(-1), dir(true) {} IEdge(int from, int to, int id, bool dir = true) : from(from), to(to), id(id), dir(dir) {} // プレーングラフで呼ばれたとき用 operator int() const { return to; } #ifdef _MSC_VER friend ostream& operator<<(ostream& os, const IEdge& e) { os << '(' << e.from << "→" << e.to << ',' << "id:" << e.id << ',' << (e.dir ? "fwd" : "rev") << ')'; return os; } #endif }; //【参照付きグラフ】 /* * IGraph g * g[v] : 頂点 v から出る辺を並べたリスト * * verify : https://judge.yosupo.jp/problem/cycle_detection_undirected */ using IGraph = vector>; //【閉路抽出(有向グラフ)】O(n + m) /* * 有向グラフ g から極大個数の単純閉路を抽出し,各閉路に含まれる辺のリストを返す. */ vvi directed_cycles_detection(IGraph g) { // verify : https://yukicoder.me/problems/no/2464 int n = sz(g); vvi cycles; // 0 : 未探索,1 : 探索済,2 : 閉路の端点 vi seen(n); // 0 : 探索モード,2 : 閉路抽出モード int tp = 0; function dfs = [&](int s) { seen[s] = 1; while (!g[s].empty()) { // 同じ辺を二度見ないように削除しておく. auto t = g[s].back(); g[s].pop_back(); // 自己ループは単独の閉路として記録する. if (t == s) { cycles.push_back(vi{t.id}); continue; } // 探索済の頂点にたどり着いたなら閉路を検出できた. if (seen[t.to] == 1) { // t が閉路の端点であることを覚えておく. seen[t.to] = 2; // 閉路抽出モードに移行する. tp = 2; cycles.push_back(vi()); cycles.back().push_back(t.id); break; } dfs(t.to); // 閉路抽出モードなら,探索は一休みして閉路を抽出する. if (tp == 2) { cycles.back().push_back(t.id); // 閉路の端点まで抽出しきったのなら探索モードに移行する. if (seen[s] == 2) { reverse(all(cycles.back())); seen[s] = 1; tp = 0; continue; } break; } } // 頂点に関しては二度以上見る必要があるのでバックトラッキングする. seen[s] = 0; }; // 各頂点 s を通る閉路をできるかぎり抽出する. rep(s, n) while (!g[s].empty()) dfs(s); return cycles; } //【めぐる式二分探索】O(log|ok - ng|) /* * 条件 okQ() を満たす要素 ok と満たさない要素 ng との境界を二分探索する. * 境界に隣り合うような条件を満たす要素(ok 側)の位置を返す. */ template T meguru_search(T ok, T ng, const FUNC& okQ) { // 参考 : https://twitter.com/meguru_comp/status/697008509376835584 // verify : https://atcoder.jp/contests/typical90/tasks/typical90_a // 境界が決定するまで while (abs(ok - ng) > 1) { // 区間の中間 T mid = (ok + ng) / 2; // 中間が OK かどうかに応じて区間を縮小する. if (okQ(mid)) ok = mid; else ng = mid; } return ok; /* okQ の定義の雛形 auto okQ = [&](ll x) { return true || false; }; */ } //【グラフの出力】O(n + m) /* * グラフを【グラフの入力】で受け取る入力と同じ形式で出力する. * * undirected : 無向グラフか(省略すれば true) * one_indexed : 入力が 1-indexed か(省略すれば true) */ void write_Graph(const Graph& g, bool undirected = true, bool one_indexed = true) { // verify : https://www.codechef.com/problems/B_BRANCH int n = sz(g); // m : 辺の数 int m = 0; rep(s, n) m += sz(g[s]); if (undirected) m /= 2; cout << n << " " << m << endl; rep(s, n) repe(t, g[s]) { if (undirected && s > t) continue; int u = s + one_indexed, v = t + one_indexed; cout << u << " " << v << " " << endl; } } int main() { input_from_file("input.txt"); // output_to_file("output.txt"); int n; cin >> n; vl a(n), b(n); vi c(n); cin >> a >> b >> c; --c; IGraph g(n); rep(i, n) g[i].push_back({ i, c[i], i, true }); dumpel(g); auto cys = directed_cycles_detection(g); int K = sz(cys); dumpel(cys); vi id(n, -1); rep(k, K) repe(v, cys[k]) id[v] = k; dump(id); Graph gR(n); rep(s, n) if (id[s] == -1) gR[c[s]].push_back(s); dumpel(gR); // write_Graph(gR, false); auto okQ = [&](__int128 x) { dump("x:", x); vector<__int128> bx(n); rep(i, n) bx[i] = b[i] * x; dump(bx); function dfs = [&](int s) { repe(t, gR[s]) { dfs(t); bx[s] += max<__int128>(bx[t], 0); } bx[s] -= a[s]; }; rep(s, n) if (id[s] != -1) dfs(s); dump(bx); vector<__int128> bxsum(K); rep(s, n) if (id[s] != -1) bxsum[id[s]] += bx[s]; dump(bxsum); rep(k, K) if (bxsum[k] > 0) return false; return true; }; auto x = meguru_search<__int128>(0, 1e14 + 100, okQ); cout << (ll)x << endl; }