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

using namespace std;

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


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



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

long long f(int l, int r)
{
    if (l>r)
        return 0;
    else
    {
        int i=get(0, 0, n, l, r+1).second;
        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;
}
