#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
    int n;
    cin >> n;
    vector<int> h(n), c(n), maxx(n), dp(n);
    int mx = 0;
    for (int i = 0; i < n; ++i) {
        cin >> h[i];
        mx = max(mx, h[i]);
        maxx[i] = mx;
    }
    for (int& x : c) {
        cin >> x;
    }

    dp[0] = c[0];

    for (int i = 1; i < n; ++i) {
        if (maxx[i] == h[0] || maxx[i] == h[i]) {
            dp[i] = max(c[i], c[0]);
        }
        else {
            dp[i] = c[i] + dp[i - 1];
        }
    }

    cout << dp.back();

    return 0;
}
