#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

vector<long long> a;
vector<long long> tree;
int n;
vector<int> cnt;


void build(int v, int l, int r){
    if(l+1==r){
        tree[v]=a[l];
        return;
    }
    int m=(l+r)/2;
    build(2*v+1, l, m);
    build(2*v+2, m, r);
    tree[v]=max(tree[2*v+1], tree[2*v+2]);
}



long long get(int v, int l, int r, int ql, int qr){
    if(r<=ql || qr<=l){
        return 0;
    }
    if(qr>=r && l>=ql){
        return tree[v];
    }
    int m=(l+r)/2;
    return max(get(2*v+1, l, m, ql, qr), get(2*v+2, m, r, ql, qr));
}

int f(int l, int r)
{
    if (l>r)
        return 0;
    else
    {
        int i=find(a.begin(), a.end(), get(0, 0, n, l, r+1))-a.begin();
        return cnt[i]+min(f(l, i-1), f(i+1, r));
    }
}

int main()
{
    cin>>n;
    a.resize(n);
    tree.resize(4*n);
    for (int i=0; i<n; ++i){
        cin>>a[i];
    }
    build(0, 0, n);
    cnt.resize(n);

    for (int i=0; i<n; ++i){
        cin>>cnt[i];
    }
    cout << f(0, n-1);
    return 0;
}
