

#include <bits/stdc++.h>
#define int long long
    using namespace std;

int inf = 2e18;

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;

    vector<int> v(n + 2), c(n + 2), sos1(n + 2), sos2(n + 2);
    v[0] = v[n + 1] = inf;
    c[0] = c[n + 1] = inf;
    for (int i = 1; i < n + 1; i++) {
        cin >> v[i];
    }
    for (int i = 1; i < n + 1; i++) {
        cin >> c[i];
    }
    vector<int> dp(n + 2, inf);
    dp[0] = 0;
    deque<int> d;
    d.push_back(0);
    for (int i = 1; i < n + 1; i++) {
        while (v[d.back()] < v[i]) {
            d.pop_back();
        }
        sos1[i] = d.back();
        d.push_back(i);
    }
    d.clear();
    d.push_back(0);
    for (int i = 1; i < n + 2; i++) {
        while (!d.empty() && v[d.back()] <= v[i]) {
            sos2[d.back()] = i;
            d.pop_back();
        }
        d.push_back(i);
    }/*
    for (auto& i : sos1) {
        cout << i << ' ';
    }cout << '\n';
    for (auto& i : sos2) {
        cout << i << ' ';
    }cout << '\n';*/
    for (int i = 1; i < n + 1; i++) {
        dp[i] = min(dp[i], dp[sos1[i]] + c[i]);
        dp[sos2[i] - 1] = min(dp[sos2[i] - 1], dp[i - 1] + c[i]);
    }
    cout << dp[n];
}
