#include <bits/stdc++.h>
// #pragma GCC optimize("Ofast")
using namespace std;
using ll = long long;
using ld = long double;
using ui = unsigned int;
using ull = unsigned long long;
struct segtree {
    struct node {
        ll minn;
    };
    node zero = {(ll)1e18 + 1};
    vector<node> tree;
    int size = 1;

    explicit segtree(int n) {
        while (size < n) size *= 2;
        tree.assign(2 * size - 1, zero);
    }

    node merge(node a, node b) {
        node res = {min(a.minn, b.minn)};
        return res;
    }

    void build() {
        for (int v = size - 2; v >= 0; v--) {
            tree[v] = merge(tree[2 * v + 1], tree[2 * v + 2]);
        }
    }

    void set(int i, ll x, int v, int tl, int tr) {
        if (tr == tl) {
            tree[v].minn = x;
            return;
        }
        int tm = (tr + tl) / 2;
        if (i <= tm) {
            set(i, x, 2 * v + 1, tl, tm);
        } else {
            set(i, x, 2 * v + 2, tm + 1, tr);
        }
        tree[v] = merge(tree[2 * v + 1], tree[2 * v + 2]);
    }

    void set(int i, ll x) { set(i, x, 0, 0, size - 1); }

    node get(int l, int r, int v, int tl, int tr) {
        if (l <= tl && tr <= r) {
            return tree[v];
        }
        if (r < tl || l > tr) {
            return zero;
        }
        int tm = (tr + tl) / 2;
        return merge(get(l, r, 2 * v + 1, tl, tm), get(l, r, 2 * v + 2, tm + 1, tr));
    }

    ll get(int l, int r) { return get(l, r, 0, 0, size - 1).minn; }
};

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    vector<int> h(n), c(n);
    vector<int> elems;
    for (int i = 0; i < n; i++) {
        cin >> h[i];
        elems.push_back(h[i]);
    }
    sort(elems.begin(), elems.end());
    for (int i = 0; i < n; i++) {
        h[i] = lower_bound(elems.begin(), elems.end(), h[i]) - elems.begin();
    }
    for (int i = 0; i < n; i++) cin >> c[i];
    vector<int> ansl(n, -1);
    for (int i = 1; i < n; i++) {
        int j = i - 1;
        while (j != -1 && h[i] > h[j]) {
            j = ansl[j];
        }
        ansl[i] = j;
    }
    segtree t1(n);
    vector<ll> dp(n);
    dp[0] = c[0];
    vector<pair<int, ll>> st_min;
    t1.set(0, 0);
    st_min.push_back({h[0], c[0]});
    for (int i = 1; i < n; i++) {
        while (!st_min.empty() && st_min.back().first < h[i]) {
            st_min.pop_back();
        }
        int idx = ansl[i];
        dp[i] = dp[i - 1] + c[i];
        dp[i] = min(dp[i], t1.get(idx + 1, i - 1) + c[i]);
        if (!st_min.empty()) {
            dp[i] = min(dp[i], st_min.back().second);
        }
        t1.set(i, dp[i - 1]);
        if (st_min.empty()) {
            st_min.push_back({h[i], dp[i - 1] + c[i]});
        } else {
            st_min.push_back({h[i], min(dp[i - 1] + c[i], st_min.back().second)});
        }
    }
    cout << dp[n - 1];
    return 0;
}