#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <bitset>
#include <unordered_set>
#include <unordered_map>
#include <cassert>
#include <random>
#include <chrono>
#include <tuple>

#pragma GCC optimize("03")
#pragma GCC target("avx2")

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using ii = int;

const ll INF = 1e9;
const ll MOD = 1e9 + 7;

using namespace std;
vector <ii> h, c;

ll func(ii l, ii r) {

    if (l >= r) {
        return 0;
    }

    if (l + 1 == r) {
        return c[l];
    }

    ii mx = -1, imx = l;
    for (ii i = l; i < r; ++i) {
        if (mx < h[i]) {
            mx = h[i];
            imx = i;
        }
    }

    ll ans = mx;
    ll lmx = -1, rmx = -1;
    ii ilmx = l, irmx = r - 1;
    for (ii i = l; i < imx; ++i) {
        if (lmx < h[i]) {
            lmx = h[i];
            ilmx = i;
        }
    }

    ll summl = 0;
    ilmx = l;
    for (ii i = ilmx; i < imx; ++i) {
        summl += c[i];
    }

    for (ii i = imx + 1; i < r; ++i) {
        if (rmx < h[i]) {
            rmx = h[i];
            irmx = i;
        }
    }

    ll summr = 0;
    irmx = r - 1;
    for (ii i = imx + 1; i <= irmx; ++i) {
        summr += c[i];
    }

    if (summr > summl) {
        return c[imx] + func(l, imx);
    }
    else {
        return c[imx] + func(imx + 1, r);
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    ii n;
    cin >> n;
    ii mx = -1, imx = 0;
    h.resize(n);
    c.resize(n);
    for (ii i = 0; i < n; ++i) {
        cin >> h[i];
    }

    for (ii i = 0; i < n; ++i) {
        cin >> c[i];
    }

    cout << func(0, n);

    return 0;
}
