//#pragma GCC optimize("0fast")
//#pragma GCC optimize("popcnt, bm2")

#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    int n;
    cin >> n;
    vector<int> h(n + 2);
    for (int i = 1; i <= n; i++) cin >> h[i];
    h[0] = h[n + 1] = 1e9 + 10;
    vector<int64_t> cost(n + 1);
    for (int i = 1; i <= n; i++) cin >> cost[i];
    vector<int> st1 = {0};
    vector<int> l(n + 1);
    for (int i = 1; i <= n; i++) {
        while (h[st1.back()] <= h[i]) {
            st1.pop_back();
        }
        l[i] = st1.back();
        st1.push_back(i);
    }
    vector<int> st2 = {n + 1};
    vector<int> r(n + 1);
    for (int i = n; i >= 1; i--) {
        while (h[st2.back()] <= h[i]) {
            st2.pop_back();
        }
        r[i] = st2.back() - 1;
        st2.push_back(i);
    }
    vector<int64_t> dp(n + 1, 1e17);
    dp[0] = 0;
    for (int i = 1; i <= n; i++) {
        dp[i] = fmin(dp[i], dp[l[i]] + cost[i]);
        dp[r[i]] = fmin(dp[r[i]], dp[i - 1] + cost[i]);
    }
    cout << dp[n];
    return 0;
}