#include <bits/stdc++.h>

#define _FORTIFY_SOURCE 0
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("fast-math")

using namespace std;

typedef long long ll;
typedef long double ld;
#define int ll
#define double ld

#ifdef LOCAL
#   define dbg(x) cout << #x " = " << (x) << '\n';
#   define dbgf printf
#else
#   define dbg(x)
#   define dbgf(...)
#endif

#define all(x) std::begin(x), std::end(x)
#define rall(x) std::rbegin(x), std::rend(x)

template<typename T1, typename T2>
ostream &operator<<(ostream &out, pair<T1, T2> &p) {
    return out << p.first << ' ' << p.second;
}

template<typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
    return in >> p.first >> p.second;
}

const auto boostIO = []() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cout << fixed;
    cout.precision(12);
    return 0;
} ();

struct segtree {
    int sz;
    vector<int> tree;
    const int DEF_MN = 1e12;
    vector<int> push;
    void init(int n) {
        sz = 1;
        while (sz < n) {
            sz *= 2;
        }
        tree.resize(2 * sz - 1, DEF_MN);
        push.resize(2 * sz - 1, DEF_MN);
    }

    void set(int l, int r, int v) {
        set(l, r, v, 0, 0, sz);
    }

    void propagate(int x, int lx, int rx) {
        if (rx - lx == 1 || push[x] == DEF_MN) {
            return;
        }
        tree[2 * x + 1] = tree[2 * x + 2] = push[x];
        push[2 * x + 1] = push[2 * x + 2] = push[x];
        push[x] = DEF_MN;
    }

    void set(int l, int r, int v, int x, int lx, int rx) {
        if (rx <= l || lx >= r) {
            return;
        }
        if (l <= lx && r >= rx) {
            tree[x] = v;
            push[x] = v;
            return;
        }
        int m = (lx + rx) / 2;
        propagate(x, lx, rx);
        set(l, r, v, 2 * x + 1, lx, m);
        set(l, r, v, 2 * x + 2, m, rx);
        tree[x] = min(tree[2 * x + 1], tree[2 * x + 2]);
    }

    int get(int l, int r) {
        return get(l, r, 0, 0, sz);
    }

    int get(int l, int r, int x, int lx, int rx) {
        propagate(x, lx, rx);
        if (rx <= l || lx >= r) {
            return DEF_MN;
        }
        if (l <= lx && r >= rx) {
            return tree[x];
        }
        int m = (lx + rx) / 2;
        return min(get(l, r, 2 * x + 1, lx ,m), get(l, r, 2 * x + 2,m,rx));
    }
};

void solve() {
    int n;
    cin >> n;
    vector<int> h(n), c(n);
    map<int, int> hashh;
    vector<int> u;
    for (int i = 0; i < n; i++) {
        cin >> h[i];
        u.push_back(h[i]);
    }
    sort(all(u));
    for (auto i : u) {
        if (hashh.count(i)) {
            hashh[i] = hashh.size();
        }
    }
    for (int i = 0; i < n; i++) {
        cin >> c[i];
    }
    segtree tr;
    tr.init(hashh.size());
    deque<pair<int, int>> mx;
    set<pair<int, int>> d_mx;
    vector<int> dp(n + 1, 1e12);
    dp[0] = 0;
    for (int i = 1; i <= n; i++) {
        while (!mx.empty() && h[i - 1] > mx.back().first) {
            d_mx.erase({mx.back().second, mx.back().first});
            mx.pop_back();
        }
        mx.push_back({h[i - 1], dp[i - 1] + c[i - 1]});
        d_mx.insert({mx.back().second, mx.back().first});
        dp[i] = min(dp[i], d_mx.begin()->first);
        dp[i] = min(dp[i], tr.get(0, hashh[h[i - 1]]) + c[i - 1]);
        tr.set(0, hashh[h[i - 1]], tr.DEF_MN);
        tr.set(hashh[h[i - 1]], hashh[h[i - 1]] + 1, dp[i]);
//        cout << "DP FOR " << h[i - 1] << " is " << dp[i] << '\n';
    }
    cout << dp[n] << '\n';

}

signed main() {

    solve();

    return 0;
}