#include <bits/stdc++.h>
#define endl '\n'
#define int ll
#define ve vector
#define pb push_back
#define all(x) x.begin() , x.end()
#define pii pair <int , int>
#define F first
#define S second
#define bpp __builtin_popcount

using namespace std;
typedef long long ll;
typedef long double ld;

const int N = 2e5 + 10;

int st = 0;
int pr[N];
stack <pii> q;

struct seg_tree
{
    ve <int> t;
    void fill()
    {
        t.assign(N * 4 , 1e18);
    }
    void upd(int v , int cl , int cr , int pos , int x)
    {
        if(cl == cr)
        {
            t[v] = x;
            return;
        }
        ll mid = (cl + cr) / 2;
        if(pos <= mid)
        {
            upd(v * 2 , cl , mid , pos , x);
        }
        else
        {
            upd(v * 2 + 1 , mid + 1 , cr , pos , x);
        }
        t[v] = min(t[v * 2] , t[v * 2 + 1]);
    }
    ll get(int v , int cl , int cr , int l , int r)
    {
        if(cl >= l && cr <= r)
        {
            return t[v];
        }
        if(cl > r || cr < l)
        {
            return 1e18;
        }
        ll mid = (cl + cr) / 2;
        return min(get(v * 2 , cl , mid , l , r) , get(v * 2 + 1 , mid + 1 , cr , l , r));
    }
};

int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    #ifdef LOCAL
        freopen("input.txt" , "r" , stdin);
        freopen("output.txt" , "w" , stdout);
    #endif // LOCAL
    int n;
    cin >> n;
    int a[n + 1] , b[n + 1];
    ve <int> all_x;
    all_x.pb(1e18);
    all_x.pb(0);
    a[0] = 0;
    for(int i = 0;i < n;++ i)
    {
        cin >> a[i + 1];
        all_x.pb(a[i + 1]);
    }
    pr[0] = 0;
    q.push({a[0] , 0});
    for(int i = 1;i <= n;++ i)
    {
        while(!q.empty() && q.top().F < a[i])
        {
            q.pop();
        }
        if(q.empty())
        {
            pr[i] = -1;
        }
        else
        {
            pr[i] = q.top().S;
        }
        q.push({a[i] , i});
    }
    for(int i = 0;i < n;++ i)
    {
        cin >> b[i + 1];
    }
    sort(all(all_x));
    map <int , int> nom;
    for(int i = 0;i < all_x.size();++ i)
    {
        nom[all_x[i]] = i;
    }
    while((1 << st) <= all_x.size())
    {
        ++ st;
    }
    seg_tree dp_big;
    dp_big.fill();
    seg_tree dp;
    dp.fill();
    ll ans[n + 1];
    fill(ans , ans + n + 1 , 0);
    dp.upd(1 , 0 , (1 << st) - 1 , 0 , 0);
    for(int i = 1;i <= n;++ i)
    {
        ll now1 = dp.get(1 , 0 , (1 << st) - 1 , pr[i] + 1 , i) + b[i];
        ll now2 = dp_big.get(1 , 0 , (1 << st) - 1 , nom[a[i]] , all_x.size());
        ans[i] = min(now1 , now2);
        dp_big.upd(1 , 0 , (1 << st) - 1 , nom[a[i]] , ans[i - 1] + b[i]);
        dp.upd(1 , 0 , (1 << st) - 1 , i , ans[i - 1]);
    }
    cout << ans[n];

    return 0;

}
