#include using namespace std; template istream& operator >> (istream& is, vector& vec) { for(T& x : vec) is >> x; return is; } int main(){ ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector a(n), b(n); cin >> a >> b; vector> g(500001); vector used(500001); for(int i = 0; i < n; i++){ g[a[i]].emplace_back(a[i] + b[i]); } queue que; que.emplace(1); int ans = 1; while(!que.empty()){ int v = que.front(); que.pop(); for(auto &&u : g[v]){ ans = max(ans, u); if(u <= 500000 && !used[u]){ used[u] = true; que.emplace(u); } } } cout << ans << '\n'; }