#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];
int lg[N], sp[22][N];
map <int, int> idx;

int get(int l, int r) {
  int k = lg[r - l + 1];
  return max(sp[k][l], sp[k][r - (1 << k) + 1]);
}

void solve() {
  cin >> n;
  lg[1] = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sp[0][i] = a[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> c[i];
  }
  for (int j = 1; j <= lg[n]; j++) {
    for (int i = 1; i <= n - (1 << j) + 1; i++) {
      sp[j][i] = max(sp[j - 1][i], sp[j - 1][i + (1 << (j - 1))]);
    }
  }
  set <int> s;
  multiset <int> by;
  for (int i = 1; i <= n; i++) {
    int l = 1, r = i - 1;
    while (l <= r) {
      int m = l + r >> 1;
      if (get(m, i - 1) >= a[i]) l = m + 1;
      else r = m - 1;
    }
    dp[i][0] = dp[r][0];
    
//    cout << dp[i][0] << ' ' << r << '\n';
    int res = INF;
    if (sz(by)) res = *by.begin();
    dp[i][0] = c[i] + min(dp[i][0], res);
    dp[i][1] = c[i] + min(dp[i - 1][0], res);
    vector <int> cur;
    for (int it: s) {
      if (it <= a[i]) cur.pb(it);
      else break;
    }
    for (int it: cur) {
      s.erase(it);
      by.erase(idx[it]);
    }
    s.insert(a[i]);
    by.insert(dp[i][1]);
    idx[a[i]] = dp[i][1];
//    cout << dp[i][0] << ' ' << dp[i][1] << ' ' << res << '\n';
  }
  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;
}