#include<bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define int long long
#define ll long long
#define con continue
#define fir first
#define sec second
#define yes cout<<"YES"<<endl;
#define no cout<<"NO"<<endl;
#define rev reverse
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define endl "\n"
using namespace std;

main()
{
    ll n, i, ans = 0, k;
    cin >> n;
    ll a;
    vector <pair<ll, ll>> vc(n);
    for (i = 0; i < n; i++){
        cin >> a;
        vc[i].first = a;
    }
    for (i = 0; i < n; i++){
        cin >> k;
        vc[i].second = k;
    }
    if (n == 1){
        cout << vc[0].second << endl;
    }
    else {
    sort (vc.begin(), vc.end());
    cout << vc[n - 1].second + vc[n - 2].second << endl;
    }
}
