#include <iostream>
#include <vector>
#include <algorithm>
#define int long long
using namespace std;
signed main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; i++) cin >> a[i];
    vector<int> k;
    vector<int> z(n);
    for (int i = 0; i < n; i++) {
        while (!k.empty() && a[*k.rbegin()] < a[i]) k.pop_back();
        if (k.empty()) z[i] = -1;
        else z[i] = *k.rbegin();
        k.push_back(i);
    }
    k.clear();
    vector<int> v(n);
    for (int i = n - 1; i >= 0; i--) {
        while (!k.empty() && a[*k.rbegin()] < a[i]) k.pop_back();
        if (k.empty()) v[i] = -1;
        else v[i] = *k.rbegin();
        k.push_back(i);
    }
    vector<vector<pair<int, int>>> left(n + 1);
    vector<int> c(n);
    for (int i = 0; i < n; i++) cin >> c[i];
    for (int i = 0; i < n; i++) {
        left[i + 1].push_back({z[i] + 1, c[i]});
    }
    for (int i = 0; i < n; i++) {
        if (v[i] == -1) {
            left[n].push_back({i, c[i]});
        }
        else left[v[i]].push_back({i, c[i]});
    }
    vector<int> dp(n + 1, LLONG_MAX);
    dp[0] = 0;
    for (int i = 1; i <= n; i++) {
        for (auto x : left[i]) {
            dp[i] = min(dp[i], x.second + dp[x.first]);
        }
    }
    cout << dp[n];
    return 0;
}
