#include <bits/stdc++.h>
#define ll long long
#define int ll
#define pii pair<int, int>
#define ff first
#define ss second
#define pb push_back
#define in insert
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)x.size()
#define deb(x) cout << #x << " = " << x << '\n';
#define file(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout)
using namespace std;

// const int dx[] = {0, 1, 0, -1};
// const int dy[] = {1, 0, -1, 0};

const int inf = 1e9;
const ll INF = 1e18;
const int mod = 1e9 + 7;
const int N = 2e5 + 5;

int n, a[N], c[N], dp[N][2];

void solve() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> c[i];
  }
  for (int i = 1; i <= n; i++) {
    int j = i - 1;
    while (j > 0 && a[j] < a[i]) j--;
    dp[i][0] = dp[j][0];
    int res = INF;
    int mx = -INF;
    for (int j = i - 1; j; j--) {
      if (a[j] > mx) res = min(res, dp[j][1]);
      mx = max(mx, a[j]);
    }
    dp[i][0] = c[i] + min(dp[i][0], res);
    dp[i][1] = c[i] + min(dp[i - 1][0], res);
  }
  int ans = dp[n][0];
  int mx = -INF;
  for (int i = n; i; i--) {
    if (a[i] > mx) ans = min(ans, dp[i][1]);
    mx = max(mx, a[i]);
  }
  cout << ans;
}

signed main() {
	ios_base::sync_with_stdio(0), cin.tie(0);
	int TT = 1;
	// cin >> TT;
	while(TT--) {
		solve();
	}
	return 0;
}