#include <bits/stdc++.h>
#ifndef Local
#define debug(...) 1337
#define endl '\n'
#endif

using namespace std;

#define int long long

typedef long long ll;
typedef long double ld;

#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()

template <typename T, typename U>
bool smin(T& a, U b) {
	if (a > b) {
		a = b;
		return true;
	}
	return false;
}

template <typename T, typename U>
bool smax(T& a, U b) {
	if (a < b) {
		a = b;
		return true;
	}
	return false;
}

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> st;
	vector<int> l(n + 1);
	for (int i = n; i >= 1; --i) {
		while (sz(st) && h[st.back()] < h[i])
			l[st.back()] = i, st.pop_back();
		st.push_back(i);
	}
	debug(l);
	while (sz(st))
		l[st.back()] = 0, st.pop_back();
	vector<array<int, 3>> dp(n + 1);
	const int inf = 1e17;
	dp[0] = {inf, 0, inf};
	dp[1] = {inf, c[1], c[1]};
	for (int i = 2; i <= n; ++i) {
		dp[i][0] = min(dp[l[i]][0], dp[l[i]][2]);
		dp[i][1] = min(dp[l[i]][0], dp[l[i]][1]) + c[i];
		dp[i][2] = min(dp[l[i]][0], dp[l[i]][2]) + c[i];
	}
	cout << min({dp[n][0], dp[n][1], dp[n][2]}) << endl;
}

signed main() {
	ios::sync_with_stdio(false), cin.tie(nullptr);

	int tt = 1;
	// cin >> tt;
	while (tt--)
		solve();

	return 0;
}
