#pragma GCC optimize("unroll-loops")
#pragma gcc optimize("Ofast")
#pragma GCC optimization("Ofast")
#pragma optimize(Ofast)
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define str string
#define fastio ios::sync_with_stdio(0), cin.tie(0);
#define fs first
#define ss second
#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define len(x) x.size()

#define print(a)          \
    for (auto &x : a)     \
        cout << fixed << setprecision(10) << x << " "; \
    cout << endl;

#define printmp(a)    \
    for (auto &x : a) \
        cout << x.fs << " " << x.ss << endl;

const int mod = 1e9 + 7;

struct node {
    int mx, i;
};

struct SegTree {
    int size = 1;
    vector<node> t;

    void init(int n) {
        while (size < n)
            size <<= 1;
        t.assign(size << 1, {0, 0});
    }

    void build(vector<int> &a, int x, int lx, int rx){
        if(rx == lx + 1){
            if(lx < a.size())
                t[x] = {a[lx], lx};
            return;
        }
        int m = (lx + rx) >> 1;
        build(a, (x << 1) + 1, lx, m);
        build(a, (x << 1) + 2, m, rx);
        if(t[(x << 1) + 1].mx > t[(x << 1) + 2].mx)
            t[x].mx = t[(x << 1) + 1].mx, t[x].i = t[(x << 1) + 1].i;
        else
            t[x].mx = t[(x << 1) + 2].mx, t[x].i = t[(x << 1) + 2].i;
    }

    node ans(int l, int r, int x, int lx, int rx){
        if(l <= lx and rx <= r){
            return t[x];
        }
        if(r <= lx or rx <= l)
            return {0, 0};
        int m = (lx + rx) >> 1;
        node z = ans(l, r, (x << 1) + 1, lx, m), y = ans(l, r, (x << 1) + 2, m, rx);
        if(z.mx > y.mx)
            return z;
        return y;
    }

    int ans(int l, int r){
        return ans(l, r, 0, 0, size).i;
    }
};

vector<int> a, b;
SegTree s;

int rec(int l, int r){
    int x = s.ans(l, r + 1);
    if(x == r)
        return b[r];
    if(x == l)
        return b[l];
    if(l == r)
        return b[l];
    int left = rec(l, x - 1), right = rec(x + 1, r);
    return min(left, right) + b[x];
}

void solve(){
    int n;
    cin >> n;
    a.resize(n);
    b.resize(n);
    for(int i = 0; i < n; i ++)
        cin >> a[i];
    for(int i = 0; i < n; i ++)
        cin >> b[i];
    s.init(n);
    s.build(a, 0, 0, n);
    cout << rec(0, n - 1);
}

signed main()
{
    fastio int t = 1;
    // cin >> t;
    while (t--)
    {
        solve();
        cout << endl;
    }
}