#include<bits/stdc++.h>
#define fi first
#define se second
#define ll long long
using namespace std;
const ll N = 2e5;
ll n, h[N + 1], c[N + 1], pw[N + 1];
pair<ll, ll> mx[N + 1][20];
void build() {
    for (ll i = 2; i <= N; i++)
        pw[i] = pw[i / 2] + 1;
    for (ll i = 1; i <= n; i++)
        mx[i][0] = {h[i], i};
    for (ll i = 1; i < 20; i++)
        for (ll j = 1; j <= n - (1 << i) + 1; j++)
            mx[j][i] = max(mx[j][i - 1], mx[j + (1 << (i - 1))][i - 1]);
}
ll get_max(ll l, ll r) {
    ll num = pw[r - l + 1];
    return max(mx[l][num], mx[r - (1 << num) + 1][num]).se;
}
ll rec(ll l, ll r) {
    if (l > r)
        return 0ll;
    ll pos = get_max(l, r);
    return c[pos] + min(rec(l, pos - 1), rec(pos + 1, r));
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cin >> n;
    for (ll i = 1; i <= n; i++)
        cin >> h[i];
    for (ll i = 1; i <= n; i++)
        cin >> c[i];
    build();
    cout << rec(1, n);
    return 0;
}
