#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <iomanip>

#define ll long long
#define ld long double
#define int ll
using namespace std;
vector <pair<int, int>>tmax;
vector <int> h, c;
int n;
void build(int tl, int tr, int v) {
    if (tr - tl == 1) {
        tmax[v].first = h[tl];
        tmax[v].second = tl;
    }
    else {
        int m = (tl + tr) / 2;
        build(tl, m, 2 * v);
        build(m, tr, 2 * v + 1);
        if (tmax[2 * v] > tmax[2 * v + 1]) {
            tmax[v].first = tmax[2 * v].first;
            tmax[v].second = tmax[2 * v].second;
        }
        else {
            tmax[v].first = tmax[2 * v + 1].first;
            tmax[v].second = tmax[2 * v + 1].second;
        }
    }
}
pair<int, int> getmax(int l, int r, int tl, int tr, int v) {
    if (tl >= r || tr <= l) return { -INT_MAX, -INT_MAX };
    if (tl >= l && tr <= r) {
        return { tmax[v].first, tmax[v].second };
    }
    else {
        int m = (tl + tr) / 2;
        pair<int, int> p1 = getmax(l, r, tl, m, 2 * v),
            p2 = getmax(l, r, m, tr, 2 * v + 1);
        if (p1.first > p2.first) {
            return p1;
        }
        else {
            return p2;
        }
    }
}
int setcolumn(int l, int r) {
    if (r - l < 1) return 0;
    int maxind = getmax(l, r, 0, n, 1).second;
    int v1 = setcolumn(maxind + 1, r), v2 = setcolumn(l, maxind);
    return min(v1, v2) + c[maxind];
}
signed main()
{
    cin >> n;
    tmax.resize(4 * n);
    h.resize(n);
    c.resize(n);
    for (int i = 0; i < n; i++) cin >> h[i];
    for (int i = 0; i < n; i++) cin >> c[i];
    build(0, n, 1);
    cout << setcolumn(0, n);
    return 0;
}