#include <bits/stdc++.h>
#define int long long

using namespace std;

int n, t[200000 * 4];

void build (vector <int> &a, int v, int tl, int tr) {
    if (tl == tr)
        t[v] = a[tl];
    else {
        int tm = (tl + tr) / 2;
        build (a, v*2, tl, tm);
        build (a, v*2+1, tm+1, tr);
        t[v] = max(t[v*2], t[v*2+1]);
    }
}

int maxs (int v, int tl, int tr, int l, int r) {
    if (l > r)
        return 0;
    if (l == tl && r == tr)
        return t[v];
    int tm = (tl + tr) / 2;
    return max(maxs (v*2, tl, tm, l, min(r,tm)),
           maxs (v*2+1, tm+1, tr, max(l,tm+1), r));
}

int rec(vector <int> &h, vector <int> &c, map<int, int> indexes, int l, int r) {
    if (l > r or r >= n or l < 0) {
        return 0;
    }
    if (l == r) {
        return c[l];
    }
    int max_on_line = maxs(0, 0, n - 1, l, r);
    int max_index = indexes[max_on_line];
    return c[max_index] + min(rec(h, c, indexes, l, max_index - 1), rec(h, c, indexes, max_index + 1, r));
}

signed main () {
    cin >> n;
    vector <int> t(n * 4, 0);
    vector <int> h(n);
    vector <int> c(n);
    map<int, int> indexes;

    for (int i = 0; i < n; i++) {
        int num;
        cin >> num;
        h[i] = num;
        indexes[num] = i;
    }

    build(h, 0, 0, n - 1);

    for (int i = 0; i < n; i++) {
        cin >> c[i];
    }

    cout << rec(h, c, indexes, 0, n - 1);
}
