#include <bits/stdc++.h>
using namespace std;
int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    long long n, h[200500], a[205000], summ = 0, x = 1, sum = 0;
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> h[i];
    }
    for(int i = 1; i <= n; i++){
        cin >> a[i];
    }
    if(n == 1){
        cout << a[1];
        return 0;
    }
    for(int i = 1; i <= n; i++){
        while(h[x] >= h[i]){
            i++;
        }
        summ += a[x];
        x++;
    }
    x = 1;
    reverse(h + 1, h + n + 1);
    reverse(a + 1, a + n + 1);
    for(int i = 1; i <= n; i++){
        while(h[x] >= h[i]){
            i++;
        }
        sum += h[x];
        x++;
    }
    cout << min(sum, summ);
}
