#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<string>
#include<set>
#include<map>
#include<iomanip>
#include<bitset>
#include<unordered_set>
#include<unordered_map>
#include<queue>
#include<stack>
#include<deque>
using namespace std;
#define int long long
#define all(x) x.begin(), x.end()

const long long INF = 1000000000000000000ll;

void solve() {
	int n;
	cin >> n;
	vector<int> h(n + 1, 0);
	vector<int> c(n + 1, 0);
	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> big_right(n + 1, n + 1);
	vector<int> big_left(n + 1, 0);
	for (int i = 1; i <= n; ++i) {
		while (st.size() > 0 && h[st.back()] <= h[i]) {
			big_right[st.back()] = i;
			st.pop_back();
		}
		st.push_back(i);
	}
	for (int i = n; i >= 1; --i) {
		while (st.size() > 0 && h[st.back()] <= h[i]) {
			big_left[st.back()] = i;
			st.pop_back();
		}
		st.push_back(i);
	}
	vector<int> dpl(n + 2, 0);
	vector<int> dpr(n + 2, 0);
	for (int i = 1; i <= n; ++i) {
		dpl[i] = dpl[big_left[i]] + c[i];
	}
	for (int i = n; i > 0; --i) {
		dpr[i] = dpr[big_right[i]] + c[i];
	}
	int ans = INF;
	for (int i = 0; i <= n; ++i) {
		ans = min(ans, dpl[i] + dpr[i + 1]);
	}
	cout << ans << '\n';
}

signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	int t = 1;
	// cin >> t;
	while (t--) {
		solve();
	}
}