#include<iostream>
#include <vector>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <iomanip>
#include <algorithm>
#include <map>
#include <deque>
#define int long long
#define double long double
using ll = long long;
using namespace std;
const int mod = 1e9 + 7;
const int INF = 1e9 + 7;
#define get_time (double)clock() / (double)(CLOCKS_PER_SEC)
#ifndef ONLINE_JUDGE
#define debug(x) cerr << (#x) << ": " << x << endl;
#else
#define debug(x)
#endif
ll max(ll a, ll b) {
    return (a > b) ? a : b;
}
ll min(ll a, ll b) {
    return (a < b) ? a : b;
}
ll bs_sqrt(ll x) {
    ll left = 0, right = 2000000123;
    while (right > left) {
        ll mid = (left + right) / 2;
        if (mid * mid > x) {
            right = mid;
            continue;
        }
        left = mid + 1;
    }
    return left - 1;
}
int gcd(int a, int b) {
    while (a > 0 && b > 0) {
        int c = a;
        a = b % a;
        b = c;
    }
    return a + b;
}
int bin_pow(int a, int n) {
    if (n == 0) return 1;
    if (n % 2 == 0) {
        ll t = bin_pow(a, n / 2);
        return (t * t);
    }
    else {
        ll t = bin_pow(a, n - 1);
        return (t * a);
    }
}
struct segtree {
    vector<ll> tree;
    int size = 1;
    void init(int n) {
        while (size < n) {
            size *= 2;
        }
        tree.resize(2 * size - 1, 1e18);
    }
    void update1(int i, int v, int x, int lx, int rx) {
        if (rx - lx == 1) {
            tree[x] = v;
            return;
        }
        int m = (rx + lx) / 2;
        if (i < m) {
            update1(i, v, 2 * x + 1, lx, m);
        }
        else {
            update1(i, v, 2 * x + 2, m, rx);
        }
        tree[x] = min(tree[2 * x + 1], tree[2 * x + 2]);
    }
    void update(int i, int v) {
        update1(i, v, 0, 0, size);
    }
    ll max1(int l, int r, int x, int lx, int rx) {
        if ((l >= rx) || (lx >= r)) {
            return 1e18;
        }
        if ((lx >= l) && (rx <= r)) {
            return tree[x];
        }
        int m = (rx + lx) / 2;
        ll s1 = max1(l, r, 2 * x + 1, lx, m);
        ll s2 = max1(l, r, 2 * x + 2, m, rx);
        return min(s1, s2);
    }
    long long mx(int l, int r) {
        return max1(l, r, 0, 0, size);
    }
};
void solve() {
    int n;
    cin >> n;
    vector<int> h(n), c(n);
    for (int i = 0; i < n; i++) {
        cin >> h[i];
    }
    for (int i = 0; i < n; i++) {
        cin >> c[i];
    }
    vector<int> big(n, -1), little(n);
    vector<int> st;
    for (int i = n - 1; i >= 0; i--) {
        while (st.size() > 0 && h[st.back()] <= h[i]) {
            big[st.back()] = i;
            st.pop_back();
        }
        st.push_back(i);
    }
    st.clear();
    segtree t;
    t.init(n + 1);
    t.update(0, 0);
    vector<int> dp(n + 1);
    multiset<int> to_min;
    dp[0] = 0;
    for (int i = 1; i <= n; i++) {
        while (st.size() > 0 && h[st.back()] <= h[i - 1]) {
            to_min.erase(to_min.lower_bound(dp[st.back()] + c[st.back()]));
            st.pop_back();
        }
        st.push_back(i - 1);
        int may = 1e18;
        if (to_min.size() > 0) {
            int x = *(to_min.begin());
            may = min(x, may);
        }
        int l = big[i - 1] + 1, r = i;
        int p = t.mx(l, r) + c[i - 1];
        may = min(may, p);
        t.update(i, may);
        to_min.insert(dp[i - 1] + c[i - 1]);
        dp[i] = may;
    }
    cout << dp[n];
}
signed main() {
    /*
    #ifdef ONLINE_JUDGE
    freopen("game.in", "r", stdin);
    freopen("game.out", "w", stdout);
    #endif
    */
    cout << fixed << setprecision(20);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    //cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}