#ifdef Local
#define _GLIBCXX_DEBUG
#endif

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef long double ld;


template<typename T>
istream &operator>>(istream &in, vector<T> &a) {
    for (auto &i: a)in >> i;
    return in;
}

template<typename T>
ostream &operator<<(ostream &out, vector<T> &a) {
    for (auto &i: a)out << i << ' ';
    return out;
}

ll rec(ll w, ll h) {
    if (w == h && w == 1) {
        return 1;
    }
    if (w % 2 == 0) {
        return 2 * rec(w / 2, h) + w * h;
    }
    if (h % 2 == 0) {
        return 2 * rec(w, h / 2) + w * h;
    }
    if (w < h) {
        swap(h, w);
    }
    return rec(w / 2, h) + rec(w / 2 + 1, h) + w * h;
}

void solve2() {
    ld r;
    int n, m;
    cin >> n >> m >> r;
    vector<pair<ld, int>> x(n);
    for (int i = 0; i < n; ++i) {
        ld w;
        int d;
        cin >> d >> w;
        x[i] = {w, d};
    }
    x[0].first *= r;
    for (int i = 1; i < n; ++i) {
        x[i].first = x[i - 1].first * (1 - r) + r * x[i].first;
        //cout << x[i].first << ' ';
    }
    for (int i = 0; i < m; ++i) {
        int lf = -1, rg = n, mid;
        int q;
        cin >> q;
        while (rg - lf > 1) {
            mid = (lf + rg) / 2;
            if (x[mid].second <= q) lf = mid;
            else rg = mid;
        }
        if (lf > -1 and x[lf].second == q) cout << x[lf].first << ' ';
        else if (lf == -1) cout << 0 << ' ';
        else cout << x[lf].first * (1 - r) << ' ';
    }
}

void solve() {
    ll n, m;
    cin >> n >> m;
    vector<ll> v(m);
    cin >> v;
    vector<ll> ans;
    map<ll, ll> mp;
    for (int i = 0; i < m; i++) {
        mp[v[i]]++;
    }
    for (auto [x, y]: mp) {
        //cout<<x<<" "<<y<<"\n";
        if (y >= n) {
            ans.push_back(x);
        }
    }
    cout << ans.size() << "\n";
    cout << ans << "\n";
}

struct node {
    int v;
    int mx;
    node *l = nullptr;
    node *r = nullptr;

    explicit node(int v_ = 0, int mx_ = 0) : v(v_), mx(mx_) {}

    node(node *l_, node *r_) {
        l=l_;
        r=r_;
        if (l_->mx > r_->mx) {
            mx = l->mx;
            v = l->v;
        }else{
            mx = r->mx;
            v = r->v;
        }
    }
};

struct tree {
    int n;
    node *root;
    vector<ll> a;

    explicit tree(vector<ll> a_) {
        n = a_.size();
        root = build(a_, 0, n);
        a = a_;
    }

    int get(int ql, int qr) {
        return get(root, 0, n, ql, qr);
    }

private:
    node *build(const vector<ll> &a, int tl, int tr) {
        if (tl + 1 == tr)return new node(tl, a[tl]);
        int tm = (tl + tr) / 2;
        return new node(build(a, tl, tm), build(a, tm, tr));
    }

    int get(node *t, int tl, int tr, int ql, int qr) {
        if (tl == ql && tr == qr)return t->v;
        int tm = (tl + tr) / 2;
        if (ql >= tm)return get(t->r, tm, tr, ql, qr);
        if (qr <= tm)return get(t->l, tl, tm, ql, qr);
        int ans1 = get(t->l, tl, tm, ql, tm);
        int ans2 = get(t->r, tm, tr, tm, qr);
        if (a[ans1] > a[ans2])return ans1;
        else return ans2;
    }
};

void solve3() {
    int n;
    cin >> n;
    vector<ll> h(n);
    cin >> h;
    vector<ll> c(n);
    tree tri(h);
    cin >> c;
    function<ll(int, int)> gen = [&](int l, int r) {
        if (l + 1 == r)return c[l];
        int mx = tri.get(l, r);
        if (mx == l)return c[l];
        if (mx == r - 1)return c[r - 1];
        ll ans1 = gen(l, mx);
        ll ans2 = gen(mx + 1, r);
        return c[mx] + min(ans1, ans2);
    };
    cout << gen(0, n);
}

signed main() {
#ifdef Local
    freopen("in.in","r",stdin);
    freopen("out.out","w",stdout);
#else
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
#endif
    int t = 1;
    if (t == -1)cin >> t;
    for (int i = 0; i < t; i++)solve3();
}

