#include #include using namespace std; using ll = long long; using mint = atcoder::modint998244353; using maxt = atcoder::modint1000000007; int ans = 0; vector> To(1000001, vector(0, 0)); vector seen(1000001, false); void dfs(int v) { ans = max(ans, v); seen.at(v) = true; for(int next_v : To.at(v)) { if(seen.at(next_v)) { continue; } dfs(next_v); } } int main() { int N; cin >> N; vector A(N); for(int &o : A) { cin >> o; } vector B(N); for(int &o : B) { cin >> o; } assert(N <= 2000); for(int i = 0; i < N; i++) { assert(A.at(i) <= 500000); assert(B.at(i) <= 500000); } for(int i = 0; i < N; i++) { To.at(A.at(i)).push_back(A.at(i) + B.at(i)); } dfs(1); cout << ans << endl; }