#include "bits/stdc++.h"
using namespace std;
const int inf = 1e9;
using ll = long long;
//using ld = long double;

struct segtree {
    vector<ll> tree;
    int size;

    void init(int n) {
        size = 1;
        while (size < n) size *= 2;
        tree.assign(2 * size - 1, inf);
    }

    void upd(int i, ll v, int x, int lx, int rx) {
        if (rx - lx == 1) {
            tree[x] = min(tree[x], v);
            return;
        }
        int m = (lx + rx) >> 1;
        if (i < m) upd(i, v, 2 * x + 1, lx, m);
        else upd(i, v, 2 * x + 2, m, rx);
    }

    void upd(int i, ll v) {
        upd(i, v, 0, 0, size);
    }

    ll get(int l, int r, int x, int lx, int rx) {
        if (l >= rx or lx >= r) return inf;
        if (l <= lx and rx <= r) return tree[x];
        int m = (lx + rx) >> 1;
        return min(get(l, r, 2 * x + 1, lx, m), get(l, r, 2 * x + 2, m, rx));
    }

    ll get(int l, int r) {
        return get(l, r + 1, 0, 0, size);
    }
};

int main() {
    cin.tie(0)->sync_with_stdio(false);

    int n;
    cin >> n;
    vector<int> h(n + 1);
    vector<ll> cost(n + 1);
    for (int i = 1; i <= n; i++) cin >> h[i];
    for (int i = 1; i <= n; i++) cin >> cost[i];
    stack<int> s;
    vector<int> left(n + 1), right(n + 1);
    for (int i = 1; i <= n; i++) {
        while (!s.empty() and h[s.top()] < h[i]) s.pop();
        left[i] = s.empty() ? 0 : s.top();
        s.push(i);
    }
    while (!s.empty()) s.pop();
    for (int i = n; i > 0; i--) {
        while (!s.empty() and h[s.top()] < h[i]) s.pop();
        right[i] = s.empty() ? n + 1 : s.top();
        s.push(i);
    }
    segtree sg; sg.init(n + 1);
    sg.upd(0, 0);
    vector<vector<ll>> out(n + 2);
    multiset<ll> have;
    for (int i = 1; i <= n; i++) {
//        cout << i << endl;
        for (ll x: out[i]) have.erase(have.find(x));
        ll now = sg.get(left[i], i - 1) + cost[i];
        if (!have.empty()) now = min(now, *have.begin());
        sg.upd(i, now);
        now = sg.get(i - 1, i - 1) + cost[i];
        have.insert(now);
        out[right[i]].emplace_back(now);
    }
    cout << sg.get(n, n);
}