#include <bits/stdc++.h>
using namespace std;

#define int long long

int n;
vector<int> h, c;
unordered_map<int, int> mp;

struct sparsetable {

    int size;
    vector<vector<int>> table;

    void init(const vector<int> &a) {
        size = a.size();
        int logn = __lg(size);
        table.resize(logn + 1);
        table[0] = a;
        for (int l = 1; l <= logn; l++) {
            table[l].resize(size - (1 << l) + 1);
            for (int i = 0; i + (1 << l) <= size; i++) {
                table[l][i] = max(table[l - 1][i], table[l - 1][i + (1 << (l - 1))]);
            }
        }
    }

    int rmq(int l, int r) {
    	r++;
        int logn = __lg(r - l);
        return max(table[logn][l], table[logn][r - (1 << logn)]);
    }

};

sparsetable st;

int f(int i, int j) {
	// [i, j]
	if (i > j) {
		return 0;
	}
	if (i == j) {
		return c[i];
	}
	int mx = mp[st.rmq(i, j)];
	return c[mx] + min(f(i, mx - 1), f(mx + 1, j));
} 

//8
//1 2 3 4 5 6 7 10
//1 1 1 1 1 1 1 5

void solve() {
	cin >> n;
	h.resize(n);
	c.resize(n);
	for (int i = 0; i < n; i++) {
		cin >> h[i];
		mp[h[i]] = i;
	}
	for (int i = 0; i < n; i++) {
		cin >> c[i];
	}
	st.init(h);
	// cout << mp[st.rmq(0, n - 1)] << endl;
	cout << f(0, n - 1);
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int tests = 1;
    // cin >> tests;
    while (tests--) {
    	solve();
	}
    return 0;
}
