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

using namespace std;

template <class T>
class SegmentTree {
private:
    const T ID = 0;
    std::vector<T> t;
    int size = 0;
private:

    T comb(T x, T y) {
        return max(x, y);
    }

    int mid(int l, int r) {
        return l + (r - l) / 2;
    }

    void update(int i, int l, int r, int pos, T val) {
        if(l == r) t[i] = val;
        else {
            int m = mid(l, r);
            if(pos <= m) update(i * 2, l, m, pos, val);
            else update(i * 2 + 1, m + 1, r, pos, val);
            t[i] = comb(t[i * 2], t[i * 2 + 1]);
        }
    }

    T range_comb(int i, int l, int r, int tl, int tr) {
        if(l == tl && r == tr) return t[i];
        if(tl > tr) return 0;
        int m = mid(l, r);
        return comb(range_comb(i * 2, l, m, tl, std::min(tr, m)), range_comb(i * 2 + 1, m + 1, r, std::max(tl, m + 1), tr));
    }
public:
    SegmentTree(int n) : t(n * 4, ID), size(n) {}

    void update(int pos, T val) {
        update(1, 1, size, pos, val);
    }

    T range_comb(int l, int r) {
        return range_comb(1, 1, size, l, r);
    }
};

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

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

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

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

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