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

#define ll long long
#define ld long double

const ll N = 2e5 + 7;

int h[N], c[N], pref[N];

void solve() {
    int n; cin >> n;
    for (int i = 1; i <= n; ++i) {
        cin >> h[i];

    }
    for (int i = 1; i <= n; ++i) {
        cin >> c[i];
        pref[i] = pref[i - 1] + c[i];
    }

    int l = 1, r = n;
    int ans = 0;
    while (l <= r) {
        int mx = l;
        for (int i = l; i <= r; ++i) {
            if (h[mx] < h[i])
                mx = i;
        }
        if (pref[mx - 1] > pref[n] - pref[mx])
            l = mx + 1;
        else
            r = mx - 1;

        ans += c[mx];
    }

    cout << ans;
}

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

	int T = 1;
//	cin >> T;
	while (T--) {
		solve();
	}
	return 0;
}

