#include <bits/stdc++.h>
using namespace std;
long long arr[200100], arr2[200100];
void f(long long l, long long r, long long &sum)
{
    if(r - l < 0)
        return;
    long long ind, mx = -1;
    for(long long i = l; i <= r; i++)
    {
        if(arr[i] > mx)
            ind = i;
        mx = max(mx, arr[i]);
    }
    sum += arr2[ind];
    long long s1 = 0, s2 = 0;
    f(l, ind - 1, s1);
    f(ind + 1, r, s2);
    if(s1 > s2)
        l = ind + 1;
    else
        r = ind - 1;
    sum += min(s1, s2);
    return;
}
void solve()
{
    long long n;
    cin >>n;
    for(long long i = 1; i <= n; i++)
        cin >>arr[i];
    for(long long i = 1; i <= n; i++)
        cin >>arr2[i];
    long long ans = 0;
    f(1, n, ans);
    cout <<ans;
}
int main()
{
    // ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    long long t;
    for(t = 1; t--; solve());
}