#include <bits/stdc++.h>
#include <intrin.h>
#include<unordered_map>
#include<unordered_set>
using namespace std;


#define F first
#define S second
#define endl "\n"
#define pb push_back
#define mk make_pair
#define ll long long
#define p push
#define popb pop_back

#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
const ll N = 1e9;
const ll mod = 1e9 + 7;




int main() {

    IOS;

    ll n; cin >> n;

    vector<ll> a(n); vector<ll> b(n);

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


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

    vector<ll> lef, rig;
    vector<ll> l(n, 0), r(n, 0);


    for (int i = 0; i < n; i++) {
        while (!lef.empty() && a[i] > a[lef.back()]) {
            lef.popb();
        }
        if (!lef.empty()) {
            l[i] = l[lef.back()] + b[i];
        }
        else {
            l[i] = b[i];
        }
        lef.pb(i);
    }



    for (int i = n - 1; i >= 0; i--) {
        while (!rig.empty() && a[i] > a[rig.back()]) {
            rig.popb();
        }
        if (!rig.empty()) {
            r[i] = r[rig.back()] + b[i];
        }
        else {
            r[i] = b[i];
        }
        rig.pb(i);
    }

    ll mc = min(l[n - 1], r[0]);

    for (int i = 0; i < n - 1; i++) {
        mc = min(mc, l[i] + r[i + 1]);
    }

    cout << mc << endl;

}

/* по модулю
+) (a + b) % mod = (a % mod + b % mod) % mod;
*) (a * b) % mod = (a % mod * b % mod) % mod;
-) (a - b) % mod = (a % mod - b % mod + mod) % mod;
/) (a / b) % mod = ((a % mod) * (binpow(b , mod - 2) % mod)) % mod;
*/
/*
ll binpow(ll a, ll n) {
    if (n == 0) return 1;
    else if (n % 2 == 1) return a * binpow(a, n - 1);
    else {
        ll b = binpow(a, n / 2);
        return b * b;
    }
}

bool was[N];
ll prost(ll n) {
    was[1] = true;
    for (int i = 2; i <= n; i++) {
        if (!was[i]) {
            for (int j = i * i; j <= n; j += i) {
                was[j] = true;
            }
        }
    }
    return !was[n];
}*/


/*
int gcd(int a, int b) { // НОД
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
int lcd(int a, int b) { // НОК
    return (a / gcd(a, b) * b);
}*/
