//
//  main.cpp
//
 
#include <algorithm>
#include <array>
#include <assert.h>
#include <iomanip>
#include <iostream>
#include <limits>
#include <math.h>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
 
using namespace std;
 
using ll = int64_t;
using ull = uint64_t;
 
template <typename T> vector<T> make_vec_nd(T init, size_t size) {
  return vector<T>(size, init);
}
template <typename T, typename... Args>
auto make_vec_nd(T init, size_t size, Args... rest) {
  auto inner = make_vec_nd(init, rest...);
  return vector<decltype(inner)>(size, inner);
}
 
template <typename Monoid> struct SegmentTree {
  using Op = function<Monoid(const Monoid &lhs, const Monoid &rhs)>;
  size_t width;
  vector<Monoid> node;
  Op op;
  Monoid unit;
 
  SegmentTree(const vector<Monoid> &elems, Op op, Monoid unit)
      : op(op), unit(unit) {
    width = 1;
    while (width < elems.size()) {
      width *= 2;
    }
    node.resize(2 * width - 1, unit);
    for (size_t i = 0; i < elems.size(); i++) {
      node[i + width - 1] = elems[i];
    }
    for (size_t i = width - 2; i != numeric_limits<size_t>::max(); i--) {
      node[i] = op(node[2 * i + 1], node[2 * i + 2]);
    }
  }
  void update(size_t idx, Monoid elem) {
    idx += width - 1;
    node[idx] = elem;
    while (idx >= 1) {
      idx = (idx - 1) / 2;
      node[idx] = op(node[2 * idx + 1], node[2 * idx + 2]);
    }
  }
  Monoid query(size_t a, size_t b, size_t root) const {
    assert(0 <= a && b <= width);
    size_t left = root, right = root;
    while (left < width - 1) {
      left = 2 * left + 1;
    }
    left -= (width - 1);
    while (right < width - 1) {
      right = 2 * right + 2;
    }
    right -= (width - 1);
    right++;
    if (right <= a || b <= left) {
      return unit;
    }
    if (a <= left && right <= b) {
      return node[root];
    }
    Monoid ans_l = query(a, b, 2 * root + 1);
    Monoid ans_r = query(a, b, 2 * root + 2);
    return op(ans_l, ans_r);
  }
};
 
int main() {
    ll N;
    cin >> N;
    struct HeightInfo {
        ll idx;
        ll height;
    };
    vector<HeightInfo> his;
    for (ll i = 0; i < N; i++) {
        ll h;
        cin >> h;
        his.push_back(HeightInfo{i, h});
    }
    vector<ll> ais(N);
    for (ll i = 0; i < N; i++) {
        cin >> ais[i];
    }
    sort(his.begin(), his.end(),
         [](HeightInfo lhs, HeightInfo rhs) { return lhs.height < rhs.height; });
    unordered_map<ll, ll> height_map; // i番目の花が高さが何番めか
    for (ll i = 0; i < N; i++) {
        height_map[his[i].idx] = i;
    }
    vector<ll> dp(N, numeric_limits<ll>::min());
    SegmentTree<ll> tree(dp, [](ll lhs, ll rhs) { return max(lhs, rhs); },
                         numeric_limits<ll>::min());
    for (ll i = 0; i < N; i++) {
        ll max_upto = tree.query(0, height_map[i], 0);
        if (max_upto == numeric_limits<ll>::min()) {
            dp[i] = ais[i];
        } else {
            dp[i] = ais[i] + max_upto;
        }
        tree.update(height_map[i], dp[i]);
    }
    cout << tree.query(0, N, 0) << endl;
}

Measured memory_bytes: 22508