#include <bits/stdc++.h>

#define pudge ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
//typedef tuple<ll, ll, ll> tll;

const ll INF = 2e18;
const double PI = acos(-1);
const ll MOD7 = 1e9 + (1000 - 7);
const ll MOD9 = 1e9 + 9;
const ll P = 31;
const ld E = 1e-6;
//const ll N = 6250001;
const ll N = 1000006;

vector<pll> tree;

void build(ll i, ll l, ll r, vector<ll> &a) {
    if (r - l == 1) {
        tree[i].first = a[l];
        tree[i].second = l;
        return;
    }
    ll mid = (l + r) / 2;
    build(2 * i + 1, l, mid, a);
    build(2 * i + 2, mid, r, a);
    if (tree[2 * i + 1] > tree[2 * i + 2])
        tree[i] = tree[2 * i + 1];
    else
        tree[i] = tree[2 * i + 2];
}

pll get_max(ll i, ll l, ll r, ll ql, ll qr) {
    if (ql >= r || qr <= l)
        return {-INF, -INF};
    if (ql <= l && r <= qr)
        return tree[i];
    ll mid = (l + r) / 2;
    pll a = get_max(2 * i + 1, l, mid, ql, qr);
    pll b = get_max(2 * i + 2, mid, r, ql, qr);
    if (a.first > b.first)
        return a;
    else
        return b;
}

ll solve(ll l, ll r, ll n, vector<ll> &c) {
    ll pos = get_max(0, 0, n, l, r).second;
    if (pos == l) {
        return c[l];
    } else if (pos == r - 1) {
        return c[r - 1];
    }
    ll a = solve(l, pos, n, c);
    ll b = solve(pos + 1, r, n, c);
    if (a > b)
        return b + c[pos];
    return a + c[pos];
}

int main() {
    pudge;

//    freopen("minmax.in", "r", stdin);
//    freopen("minmax.out", "w", stdout);

    ll n;
    cin >> n;
    vector<ll> h(n);
    vector<ll> c(n);
    tree.resize(4 * n);
    for (int i = 0; i < n; ++i) cin >> h[i];
    for (int i = 0; i < n; ++i) cin >> c[i];
    build(0, 0, n, h);
    cout << solve(0, n, n, c) << "\n";
    return 0;
}