#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
using namespace std;

struct Col {
    int h, c, p;

    bool operator < (const Col& o) {
        return this->h > o.h;
    }
};

int main()
{
    int n;
    cin >> n;
    vector<Col> v(n);
    int i = 1;
    for (auto& col : v) {
        cin >> col.h;
        col.p = i++;
    }
    for (auto& col : v) {
        cin >> col.c;
    }

    long long res = 0;
    sort(v.begin(), v.end());
    if (v.front().p == 1 || v.front().p == n) {
        cout << v.front().c;
        return 0;
    }
    int l = 1, r = n;
    for (int i = 0; i < n; ++i) {
        int h = v[i].h;
        int c = v[i].c;
        if (v[i].p <= n / 2) {
            r = v[i].p - 1;
        }
        else {
            l = v[i].p + 1;
        }
        res += c;
        if (l > r) {
            cout << res;
            return 0;
        }
    }

    throw 1;
}