#include <bits/stdc++.h>

using namespace std;

#define int long long
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
#define ll long long
#define ff first
#define ss second
#define endl '\n'
#define fr first
#define sc second
#define ld long double

template <typename _T>
bool chmin(_T &a, const _T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename _T>
bool chmax(_T &a, const _T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

const int mod = 998244353;
const int MOD = 1e9 + 7;
const int inf = 1e9 + 7;
const int INF = 1e18 + 7;
const double eps = 1e-6;
const double EPS = 1e-9;

vector<int> t[3];

void upd(int v, int tl, int tr, int i, int x, int p) {
  if (tl == tr) {
    t[p][v] = x;
    return;
  }
  int tm = (tl + tr) >> 1;
  if (tm >= i) {
    upd(v << 1, tl, tm, i, x, p);
  } else {
    upd(v << 1 | 1, tm + 1, tr, i, x, p);
  }
  t[p][v] = min(t[p][v << 1], t[p][v << 1 | 1]);
}

int get(int v, int tl, int tr, int l, int r, int p) {
  if (tl > r || tr < l) {
    return INF;
  }
  if (tl >= l && tr <= r) {
    return t[p][v];
  }
  int tm = (tl + tr) >> 1;
  return min(get(v << 1, tl, tm, l, r, p), get(v << 1 | 1, tm + 1, tr, l, r, p));
}

void solve() {
  int n;
  cin >> n;
  t[0].assign(n << 2, INF);
  t[1].assign(n << 2, INF);
  t[2].assign(n << 2, INF);
  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> dp(n + 1, INF);
  dp[1] = c[1];
  upd(1, 1, n, 1, c[1], 0);
  upd(1, 1, n, 1, 0, 1);
  upd(1, 1, n, 1, h[1], 2);
  multiset<pair<int, int>> st;
  st.insert({h[1], 1});
  for (int i = 2; i <= n; ++i) {
    dp[i] = get(1, 1, n, 1, i - 1, 0);
    upd(1, 1, n, i, dp[i - 1] + c[i], 0);
    upd(1, 1, n, i, dp[i - 1], 1);
    upd(1, 1, n, i, h[i], 2);
    while (!st.empty() && st.begin()->first <= h[i]) {
      upd(1, 1, n, st.begin()->second, INF, 0);
      st.erase(st.begin());
    }
    int lo = 0, hi = i;
    while (lo + 1 < hi) {
      int mid = (lo + hi) >> 1;
      if (get(1, 1, n, mid, i, 2) < h[i]) {
        hi = mid;
      } else {
        lo = mid;
      }
    }
    chmin(dp[i], dp[hi - 1] + c[i]);
  }
  cout << dp[n];
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int tt = 1;
  //cin >> tt;
  while(tt--) {
    solve();
  }
}

