#include <bits/stdc++.h>

int main() {
    int a;
    std::cin >> a;
    std::vector<int> b(a), c(a);

    for (int i = 0; i < a; i++) {
        std::cin >> b[i];
    }

    for (int i = 0; i < a; i++) {
        std::cin >> c[i];
    }

    int counter = 0;
    bool l = true;

    while (l) {
        int high = *std::max_element(b.begin(), b.end());
        int f = std::distance(b.begin(), std::max_element(b.begin(), b.end()));

        counter += c[f];

        std::vector<int> b1(b.begin(), b.begin() + f);
        std::vector<int> b2(b.begin() + f + 1, b.end());

        std::vector<int> c1(c.begin(), c.begin() + f);
        std::vector<int> c2(c.begin() + f + 1, c.end());

        b.erase(b.begin() + f);

        if (std::accumulate(c1.begin(), c1.end(), 0) == std::accumulate(c2.begin(), c2.end(), 0)) {
            counter += std::accumulate(c1.begin(), c1.end(), 0);
            l = false;
            break;
        }

        int high1 = *std::max_element(b1.begin(), b1.end());
        int high2 = *std::max_element(b2.begin(), b2.end());

        if (high1 < high2) {
            f = std::distance(b.begin(), std::max_element(b2.begin(), b2.end()));
        } else if (high1 > high2) {
            f = std::distance(b.begin(), std::max_element(b1.begin(), b1.end()));
        }

        counter += c[f];

        b1.erase(b1.begin() + f);
        b2.erase(b2.begin() + f);

        c1.erase(c1.begin() + f);
        c2.erase(c2.begin() + f);
    }

    std::cout << counter << std::endl;

    return 0;
}
