//#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define ent "\n"
#define f first
#define s second

const int maxn = 1e6 + 100;
const int inf = (int)1e9 + 100;
const ll INF = (ll)1e18;
const int MOD = 1e9 + 7;
const int maxl = 20;
const ll P = 31;

int n;
int a[maxn];
int b[maxn];
int l[maxn];
int r[maxn];
ll d[maxn];
ll t[maxn];

void build(int v = 1, int tl = 0, int tr = n){
    t[v] = INF;
    if(tl == tr){
        if(tl == 0) d[v] = 0;
        else d[v] = INF;
    } else{
        int mid = (tl + tr) >> 1;
        build(v<<1, tl, mid);
        build(v<<1|1, mid+1, tr);
        d[v] = min(d[v<<1], d[v<<1|1]);
    }
}

void pre(int v, ll x){
    d[v] = min(d[v], x);
    t[v] = min(t[v], x);
}

void push(int v, int tl, int tr){
    if(tl == tr) return;
    pre(v<<1, t[v]); pre(v<<1|1, t[v]);
    t[v] = INF;
}

void upd(int l, int r, int x, int v = 1, int tl = 0, int tr = n){
    push(v, tl, tr);
    if(tr < l || tl > r) return;
    if(l <= tl && tr <= r) pre(v, x);
    else{
        int mid = (tl + tr) >> 1;
        upd(l, r, x, v<<1, tl, mid);
        upd(l, r, x, v<<1|1, mid+1, tr);
        d[v] = min(d[v<<1], d[v<<1|1]);
    }
}

ll get(int l, int r, int v = 1, int tl = 0, int tr = n){
    push(v, tl, tr);
    if(tr < l || tl > r) return INF;
    if(l <= tl && tr <= r) return d[v];
    int mid = (tl + tr) >> 1;
    return min(get(l, r, v<<1, tl, mid),
               get(l, r, v<<1|1, mid+1, tr));
}

signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
    }
    for(int i = 1; i <= n; i++){
        cin >> b[i];
    }
    build();
    vector<int> v;
    for(int i = 1; i <= n; i++){
        l[i] = 1;
        while(v.size() && a[v.back()] <= a[i]) v.pop_back();
        if(v.size()) l[i] = v.back() + 1;
        v.push_back(i);
    }
    v.clear();
    for(int i = n; i > 0; i--){
        r[i] = n;
        while(v.size() && a[v.back()] <= a[i]) v.pop_back();
        if(v.size()) r[i] = v.back() - 1;
        v.push_back(i);
    }
    for(int i = 1; i <= n; i++){
        upd(i, i, get(l[i] - 1, i - 1) + b[i]);
        upd(i, r[i], get(i - 1, i - 1) + b[i]);
    }
    cout << get(n, n);
}
