#include <bits/stdc++.h>
#define ll long long
#define int ll
#define pii pair<int, int>
#define F first
#define S second
#define pb push_back
#define ld long double
#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, h[N], c[N], dp[N], t1[4 * N], t2[4 * N], sp[22][N]; 
 
int get(int l, int r) { 
 int k = __lg(r - l + 1); 
 return max(sp[k][l], sp[k][r - (1 << k) + 1]); 
} 
 
void build(int v, int tl, int tr) { 
 if(tl == tr) { 
  t1[v] = t2[v] = INF; 
  return; 
 } 
 int tm = tl + tr >> 1; 
 build(v * 2, tl, tm); 
 build(v * 2 + 1, tm + 1, tr); 
} 
 
void update1(int v, int tl, int tr, int pos, int val) { 
 if(tl == tr) { 
  t1[v] = val; 
  return; 
 } 
 int tm = tl + tr >> 1; 
 if(pos <= tm) update1(v * 2, tl, tm, pos, val); 
 else update1(v * 2 + 1, tm + 1, tr, pos, val); 
 t1[v] = min(t1[v * 2], t1[v * 2 + 1]); 
} 
 
void update2(int v, int tl, int tr, int pos, int val) { 
 if(tl == tr) { 
  t2[v] = val; 
  return; 
 } 
 int tm = tl + tr >> 1; 
 if(pos <= tm) update2(v * 2, tl, tm, pos, val); 
 else update2(v * 2 + 1, tm + 1, tr, pos, val); 
 t2[v] = min(t2[v * 2], t2[v * 2 + 1]); 
} 
 
int get1(int v, int tl, int tr, int l, int r) { 
 if(tl > r || tr < l) return INF; 
 if(l <= tl && tr <= r) return t1[v]; 
 int tm = tl + tr >> 1; 
 return min(get1(v * 2, tl, tm, l, r), get1(v * 2 + 1, tm + 1, tr, l, r)); 
} 
 
int get2(int v, int tl, int tr, int l, int r) { 
 if(tl > r || tr < l) return INF; 
 if(l <= tl && tr <= r) return t2[v]; 
 int tm = tl + tr >> 1; 
 return min(get2(v * 2, tl, tm, l, r), get2(v * 2 + 1, tm + 1, tr, l, r)); 
} 
 
void solve() { 
 cin >> n; 
 for(int i = 1; i <= n; i++) { 
  cin >> h[i]; 
  sp[0][i] = h[i]; 
 } 
 for(int i = 1; i <= n; i++) { 
  cin >> c[i]; 
 } 
 for (int j = 1; j <= 21; 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))]); 
  } 
 } 
 dp[0] = 0; 
 build(1, 1, n); 
 update1(1, 1, n, 1, 0); 
 for(int i = 1; i <= n; i++) { 
  int l = 1, r = i, res = i; 
  while(l <= r) { 
   int mid = l + r >> 1; 
   if(get(mid, i) <= h[i]) { 
    res = mid; 
    r = mid - 1; 
   } 
   else l = mid + 1; 
  } 
  dp[i] = c[i] + get1(1, 1, n, res, i); 
  l = i, r = n, res = i; 
  while(l <= r) { 
   int mid = l + r >> 1; 
   if(h[i] >= get(i, mid)) { 
    res = mid; 
    l = mid + 1; 
   } 
   else r = mid - 1; 
  } 
  dp[i] = min(dp[i], c[i] + get2(1, 1, n, i, n)); 
  update1(1, 1, n, i + 1, dp[i]); 
  update2(1, 1, n, res, dp[i]); 
 } 
 cout << dp[n]; 
}

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