#include #include using namespace std; using ll = long long; using ull = unsigned long long; using mint = atcoder::modint998244353; using maxt = atcoder::modint1000000007; vector Era(int N) { vector ans(0, 0); vector isprime(N + 1, true); isprime.at(1) = false; for(int i = 1; i <= N; i++) { if(isprime.at(i)) { ans.push_back(i); for(int j = 2 * i; j <= N; j += i) { isprime.at(j) = false; } } } return ans; } ll POW(ll a, int N) { if(N == 1) { return a; } ll ans = POW(a, N / 2) * POW(a, N / 2); if(N % 2 == 1) { ans *= a; } return ans; } //ライブラリここまで vector> To(1000001, vector(0, 0)); vector dist(1000001, 0); int main() { int N; cin >> N; vector A(N); for(int &o : A) { cin >> o; } vector B(N); for(int &o : B) { cin >> o; } for(int i = 0; i < N; i++) { To.at(A.at(i)).push_back(A.at(i) + B.at(i)); } int ans = 1; queue Todo; Todo.push(1); while(!(Todo.empty())) { int v = Todo.front(); Todo.pop(); for(int next_v : To.at(v)) { if(dist.at(next_v) == 0 && next_v != 1) { Todo.push(next_v); dist.at(next_v) = dist.at(v) + 1; ans = max(ans, next_v); } } } cout << ans << endl; }