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

#define int long long

void solve() {
	int n;
	cin >> n;
	vector<int> h(n + 1), c(n + 1);
	for (int i = 1; i <= n; i++) {
		cin >> h[i];
	}
	for (int i = 1; i <= n; i++) {
		cin >> c[i];
	}
	vector<int> dp(n + 1);
	dp[0] = 0;
	stack<pair<int, int>> st;
	for (int i = 1; i <= n; i++) {
		while (!st.empty() && st.top().first < h[i]) {
			st.pop();
		}
		if (st.empty()) {
			dp[i] = c[i];
		} else {
			int j = st.top().second;
			dp[i] = min(c[i] + dp[j], c[j] + dp[j - 1]);
		}
		st.emplace(h[i], i);
	}
	cout << dp[n];
}

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