#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const long long INF = 1e18;
long double binpow(long double x, long long power) {
	long double ans = 1;
	long double num = x;
	while (power > 0) {
		if (power & 1) {
			ans *= num;
			power--;
		} else {
			num *= num;
			power >>= 1;
		}
	}
	return ans;
}
int main() {
	int n;
	cin >> n;
	vector<long long> h(n);
	for (int i = 0; i < n; ++i) {
		cin >> h[i];
	}
	vector<long long> c(n);
	for (int i = 0; i < n; ++i) {
		cin >> c[i];
	}
	vector<int> mostright(n);
	vector<int> mostleft(n);
	vector<pair<int, int>> stackk;
	for (int i = 0; i < n; ++i) {
		while (!stackk.empty() && stackk.back().first <= h[i]) {
			stackk.pop_back();
		}
		if (stackk.empty()) {
			mostleft[i] = -1;
		} else {
			mostleft[i] = stackk.back().second;
		}
		stackk.push_back({h[i], i});
	}
	stackk.clear();
	for (int i = n - 1; i >= 0; --i) {
		while (!stackk.empty() && stackk.back().first <= h[i]) {
			stackk.pop_back();
		}
		if (stackk.empty()) {
			mostright[i] = n;
		} else {
			mostright[i] = stackk.back().second;
		}
		stackk.push_back({h[i], i});
	}
	vector<long long> dp(n + 1, INF);
	dp[0] = 0;
	for (int i = 0; i < n; ++i) {
		dp[i + 1] = min(dp[i + 1], dp[mostleft[i] + 1] + c[i]);
		dp[mostright[i]] = min(dp[mostright[i]], dp[i] + c[i]);
	}
	cout << dp[n] << '\n';
}