#include <bits/stdc++.h>
using namespace std;

int main() {
    int n; cin >> n;
    vector <int> arr(n), brr(n);
    for (int &i : arr) {cin >> i;}
    for (int &i : brr) {cin >> i;}
    int sum = 0, pos, pos2;
    int mx = 0;
    for (int i = 0; i < n; i++)
    {
        if (arr[i] == *max_element(arr.begin(), arr.end()))
        {
            sum += brr[i];
            pos = i + 1;
            break;
        }
    }
    for (int i = pos; i < n; i++)
    {
        if (max(mx, arr[i]) == arr[i])
        {
            mx = arr[i];
            pos2 = i;
        }
    }
    cout<<(n != 1 ? sum + brr[pos2] : sum);
}
