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

using namespace std;

#define all(x) x.begin(), x.end()
#define ll long long
#define en endl;
#define pb push_back
#define ppb pop_back

int n;
vector<ll> h(200000), w(200000), l(200000, 0), r(200000, 0);

int main () {
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> h[i];
    }
    for (int i = 0; i < n; i++) {
        cin >> w[i];
    }
    vector<ll> left;
    vector<ll> right;
    for (int i = 0; i < n; i++) {
        while (!left.empty() && h[i] > h[left.back()]) {
            left.ppb();
        }
        if (!left.empty()) {
            l[i] = l[left.back()] + w[i];
        } else {
            l[i] = w[i];
        }
        left.pb(i);
    }
    for (int i = n - 1; i >= 0; i--) {
        while (!right.empty() && h[i] > h[right.back()]) {
            right.ppb();
        }
        if (!right.empty()) {
            r[i] = r[right.back()] + w[i];
        } else {
            r[i] = w[i];
        }
        right.pb(i);
    }

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

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

    cout << minx << endl;
}
