/* Problem URL: https://codeforces.com/contest/1483/problem/C */

#include <bits/stdc++.h>

using namespace std;

#define V vector

#define rmin(a, b) a = min(a, b)
#define rmax(a, b) a = max(a, b)

#define rep(i, lim) for (int i = 0; i < (lim); i++)
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)

#define repv(i, v) for (auto &i : (v))
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
#define sortv(v) sort(v.begin(), v.end())
#define all(v) (v).begin(), (v).end()

using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vvvvi = vector<vvvi>;

using ll = long long;

using vl = vector<ll>;
using vvl = vector<vl>;
using vvvl = vector<vvl>;
using vvvvl = vector<vvvl>;

template<class v>
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
    os << vec[0];
    for (size_t i = 1; i < vec.size(); i++) {
        os << ' ' << vec[i];
    }
    os << '\n';
    return os;
}

template<class v>
auto operator>>(istream &is, vector<v> &vec)->istream& {
    for (auto &i : vec) {
        is >> i;
    }
    return is;
}

template<class v>
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
    for (auto &i : vec) {
        os << i[0];
        for (size_t j = 1; j < i.size(); j++) {
            os << ' ' << i[j];
        }
        os << '\n';
    }
    return os;
}

template<class v>
auto operator>>(istream  &is, vector<vector<v>> &vec)->istream& {
    for (auto &i : vec) {
        for (auto &j : i) {
            is >> j;
        }
    }
    return is;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;

    vi h(n);
    vl v(n);
    cin >> h >> v;

    vi left(n);
    vi right(n);

    stack<pair<int, int>> s;
    rep(i, n) {
        while (!s.empty() && s.top().first > h[i]) {
            s.pop();
        }

        if (s.empty()) {
            left[i] = -1;
        } else {
            left[i] = s.top().second;
        }

        s.emplace(h[i], i);
    }

    while (!s.empty()) {
        s.pop();
    }

    for (int i = n - 1; i >= 0; i--) {
        while (!s.empty() && s.top().first > h[i]) {
            s.pop();
        }

        if (s.empty()) {
            right[i] = -1;
        } else {
            right[i] = s.top().second;
        }

        s.emplace(h[i], i);
    }

    while (__builtin_popcount(v.size()) != 1) {
        v.push_back(0);
    }

    V<ll> seg(v.size() * 2);
    V<bool> segzero(v.size() * 2);

    nrep(i, v.size(), v.size() * 2) {
        seg[i] = v[i - v.size()];
    }

    for (int i = v.size() - 1; i > 0; i--) {
        seg[i] = seg[i * 2] + seg[i * 2 + 1];
    }

    function<void(int, int, int, int, int)> update = [&](int i, int l, int r, int tl, int tr) {
        if (l > tr || r < tl) {
            return; 
        }

        if (l >= tl && r <= tr) {
            segzero[i] = true;
            seg[i] = 0;
            return;
        }

        int mid = (l + r) / 2;

        update(i * 2, l, mid, tl, tr);
        update(i * 2 + 1, mid + 1, r, tl, tr);

        segzero[i] = segzero[i * 2] && segzero[i * 2 + 1];
        seg[i] = (segzero[i * 2] ? 0 : seg[i * 2]) + (segzero[i * 2 + 1] ? 0 : seg[i * 2 + 1]);
    };

    function<ll(int, int, int, int, int)> get = [&](int i, int l, int r, int tl, int tr) -> ll {
        if (l > tr || r < tl || segzero[i]) {
            return 0;
        }

        if (l >= tl && r <= tr) {
            return seg[i];
        }

        int mid = (l + r) / 2;

        return get(i * 2, l, mid, tl, tr) + get(i * 2 + 1, mid + 1, r, tl, tr);
    };

    priority_queue<pair<ll, int>, V<pair<ll, int>>, greater<>> pq;
    rep(i, n) {
        ll r = INT64_MAX >> 1;
        ll l = INT64_MAX >> 1;

        if (right[i] != -1) {
            r = get(1, 0, v.size() - 1, i, right[i] - 1);
        }

        if (left[i] != -1) {
            l = get(1, 0, v.size() - 1, left[i] + 1, i);
        }

        pq.emplace(min(r, l), i);
    }

    // vi ans(n + 1);
    while (!pq.empty()) {
        auto [vi, i] = pq.top();
        pq.pop();

        if (get(1, 0, v.size() - 1, i, i) >= 0) {
            continue;
        }

        ll r = 0;
        if (right[i] != -1) {
            // r = pref[actright] - pref[i];
            r = get(1, 0, v.size() - 1, i, right[i] - 1);
        }

        ll l = 0;
        if (left[i] != -1) {
            // l = pref[i + 1] - pref[actleft + 1];
            l = get(1, 0, v.size() - 1, left[i] + 1, i);
        }

        if (r >= 0 && l >= 0) {
            continue;
        }

        if (r < l) {
            // ans[i]++;
            // ans[actright]--;
            update(1, 0, v.size() - 1, i, right[i] - 1);
            continue;
        }

        // ans[i + 1]--;
        // ans[actleft + 1]++;
        update(1, 0, v.size() - 1, left[i] + 1, i);
    }

    // ll act = ans[0] == 0 ? v[0] : 0;
    // ll act = !iszero(0) ? v[0] : 0;
    // cout << get(0) << ' ';
    // ll act = 0;
    // rep(i, n) {
        // ans[i] += ans[i - 1];
        // if (ans[i] == 0) {
        // if (!iszero(i)) {
        //     act += v[i];
        // }
        // cout << get(i) << ' ';
    // }
    // cout << '\n';

    cout << seg[1] << '\n';
}
