#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <cmath>
#include <queue>
#include <iomanip>

using namespace std;
using ll = long long;
using ld = long double;

vector<ll> h;
vector<ll> c;
vector<vector<ll>> sptb;

ll deg2(ll n) {
    ll res = 0;
    while (n >> res) {
        res++;
    }
    return res - 1;
}

ll rek(ll l, ll r) { // полуинтервал
    if (l == r) {
        return 0;
    }
    if (r - l == 1) {
        return c[l];
    }
    ll d = deg2(r - l);
    ll m = sptb[l][d];
    if (h[m] < h[sptb[r - (1 << d)][d]]) {
        m = sptb[r - (1 << d)][d];
    }
    if (m == l || r - m == 1) {
        return c[m];
    }
    return c[m] + min(rek(l, m), rek(m + 1, r));
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    ll n;
    cin >> n;
    h.resize(n);
    c.resize(n);
    sptb.assign(n, vector<ll>(deg2(n) + 1, 0));
    for (int i = 0; i < n; ++i) {
        cin >> h[i];
        sptb[i][0] = i;
    }
    for (int i = 0; i < n; ++i) {
        cin >> c[i];
    }
    for (int i = 1; i <= deg2(n); ++i) {
        for (int j = 0; j < n; ++j) {
            sptb[j][i] = sptb[j][i - 1];
            if (j + (1 << (i - 1)) < n && h[sptb[j][i]] < h[sptb[j + (1 << (i - 1))][i - 1]]) {
                sptb[j][i] = sptb[j + (1 << (i - 1))][i - 1];
            }
        }
    }
    cout << rek(0, n);

    return 0;
}

