#ifdef LOCAL
    #define _GLIBCXX_DEBUG
#endif // LOCAL

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <unordered_map>
#include <map>
#include <set>
#include <unordered_set>
#include <bitset>
#include <queue>
#include <iomanip>
#include <iterator>
#include <fstream>
#include <cstdlib>
#include <random>
#include <climits>
#include <chrono>
#include <stack>

using namespace std;

#define int long long
#define f first
#define s second
#define fi first
#define se second
#define p_b push_back
#define e_b emplace_back
#define pw(x) (1LL<<((int)x))
#define sz(x) (ll)x.size()
#define m_p make_pair
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()

using ll = long long;
using ul = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using ld = long double;

mt19937 rnd(time(0));

const int inf = 1e15;
const int N = 2e5 + 10;
int n;
int h[N], c[N], t[N * 4], dp[N], fb[N];
vector<int> compar(1);
unordered_map<int, bool> used;

void update(int v, int tl, int tr, int pos, int delta) {
    if (pos < tl || pos > tr)
        return;
    if (tl == pos && pos == tr) {
        t[v] = delta;
        return;
    }

    int tm = (tl + tr) / 2;
    update(v * 2, tl, tm, pos, delta);
    update(v * 2 + 1, tm + 1, tr, pos, delta);
    t[v] = min(t[v * 2], t[v * 2 + 1]);
}

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

void solve() {
    cin >> n;
    for (int i = 1; i <= n; i ++) {
        cin >> h[i];
        if (!used[h[i]]) {
            used[h[i]] = true;
            compar.push_back(h[i]);
        }
    }
    sort(all(compar));
    int m = sz(compar) + 5;
    for(int i = 0; i <= n; i ++)
        h[i] = lower_bound(all(compar), h[i]) - compar.begin();
    for (int i = 1; i <= n; i ++)
        cin >> c[i];

    stack<pair<int, int> > go;
    for (int i = n; i >= 0; i --) {
        while (!go.empty() && h[i] > go.top().first) {
            fb[go.top().second] = i;
            go.pop();
        }
        go.push({h[i], i});
    }

//    for (int i = 0; i <= n; i ++)
//        cout << fb[i] << ' ';
//    cout << '\n';

    dp[0] = 0;
    dp[1] = c[1];
    update(1, 0, n, 1, dp[1]);
//    update2(1, 0, n, h[1], dp2[h[1]]);
//    for (int j = 0; j <= n; j ++)
//        cout << dp[j] << ' ';
    while (!go.empty())
        go.pop();
    multiset<int> have;
    go.push({h[1], c[1]});
    have.insert(dp[0] + c[1]);
    for (int i = 2; i <= n; i ++) {
//        cout << i << " | " << fb[i] << ' ' << i - 1 << ' ' << get(1, 0, n, fb[i], i - 1) << "\n";
//        for (int j = 0; j <= n; j ++)
//            cout << dp[j] << ' ';
//        cout << '\n';
        while (!go.empty() && h[i] >= go.top().first) {
            have.erase(have.find(go.top().second));
            go.pop();
        }
//        cout << i << " : " << fb[i] << ' ' << i - 1 << '\n';
//        cout << i << " | " << get(1, 0, n, fb[i], i - 1) + c[i] << ' ';
        dp[i] = get(1, 0, n, fb[i], i - 1) + c[i];
//        cout << ": " << get2(1, 0, m, h[i] + 1, m) << '\n';
        if (!go.empty()) {
//            cout << " ___ " << *have.begin();
            dp[i] = min(dp[i], *have.begin());
        }

//        cout << '\n';
//        dp2[h[i]] = dp[i - 1] + c[i];
        update(1, 0, n, i, dp[i]);
        go.push({h[i], dp[i - 1] + c[i]});
        have.insert({dp[i - 1] + c[i]});
//        cout << "update with: " << h[i] << ' ' << dp2[h[i]] << '\n';
//        update2(1, 0, n, h[i], dp2[h[i]]);

    }

//    for (int i = 0; i <= n; i ++)
//        cout << dp[i] << ' ';
//    cout << '\n';

    cout << dp[n];
}

main() {
    #ifdef LOCAL
//        freopen("input.txt", "r", stdin);
//        freopen("output.txt", "w", stdout);
    #else
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
    #endif // LOCAL
    int t = 1;
//    cin >> t;
    while (t--) {
        solve();
    }
    #ifdef LOCAL
        cout << "\n\n===\nTime : " << (long double)clock() / CLOCKS_PER_SEC << '\n';
    #endif // LOCAL
}

/*
9
3 1 2 4 3 2 1 1 2
7 5 9 3 11 5 6 1 2

10
2 3 6 5 2 2 4 3 1 3
1 5 10 3 1 1 7 8 5 5

6
4 3 5 2 1 4
5 6 15 1 3 7
*/
