// Author: Tyler Wang

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdlib>
#include <deque>
#include <functional>
#include <initializer_list>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <type_traits>
#include <utility>
#include <vector>

using namespace std;

template <typename T>
constexpr int countl_zero(T x) noexcept {  // C++20
  if (sizeof(T) <= sizeof(unsigned)) {
    return __builtin_clz(x) - (std::numeric_limits<unsigned>::digits -
                               std::numeric_limits<T>::digits);
  } else if (sizeof(T) <= sizeof(unsigned long)) {
    return __builtin_clzl(x) - (std::numeric_limits<unsigned long>::digits -
                                std::numeric_limits<T>::digits);
  } else {
    return __builtin_clzll(x) -
           (std::numeric_limits<unsigned long long>::digits -
            std::numeric_limits<T>::digits);
  }
}

template <typename T>
constexpr T bit_width(T x) noexcept {  // C++20
  return (T)(std::numeric_limits<T>::digits - countl_zero(x));
}

template <typename T>
constexpr T bit_ceil(T x) noexcept {  // C++20
  return (T)(x <= 1 ? 1 : (T)1 << bit_width((T)(x - 1)));
}

template <typename T, typename Join>
class seg_tree {
 public:
  seg_tree(int n, const T& val = T(), const Join& join = Join())
      : join_(join),
        n_(n),
        n_ceil_(bit_ceil((unsigned)n_)),
        data_(2 * n_ceil_, val) {
    for (int first = n_ceil_, last = n_ceil_ + n_ - 1; first < last;
         first >>= 1, last >>= 1) {
      data_[last >> 1] = data_[last];
      for (int i = first; i < last; i += 2) {
        data_[i >> 1] = join_(data_[i], data_[i + 1]);
      }
    }
  }

  seg_tree(const std::vector<T>& data, const Join& join = Join())
      : join_(join),
        n_((int)data.size()),
        n_ceil_(bit_ceil((unsigned)n_)),
        data_(2 * n_ceil_) {
    std::copy(data.begin(), data.end(), data_.begin() + n_ceil_);
    for (int first = n_ceil_, last = n_ceil_ + n_ - 1; first < last;
         first >>= 1, last >>= 1) {
      data_[last >> 1] = data_[last];
      for (int i = first; i < last; i += 2) {
        data_[i >> 1] = join_(data_[i], data_[i + 1]);
      }
    }
  }

  int size() const { return n_; }

  template <typename NodeUpdate>
  void update(int pos, const T& val, NodeUpdate node_update) {
    node_update(data_[pos += n_ceil_], val);
    for (int last = n_ceil_ + n_ - 1; (pos >>= 1) != 0; last >>= 1) {
      if (2 * pos + 1 <= last) {
        data_[pos] = join_(data_[2 * pos], data_[2 * pos + 1]);
      } else {
        data_[pos] = data_[2 * pos];
      }
    }
  }

  T get(int pos) const { return data_[pos + n_ceil_]; }

  // Queries the closed range [first, last].
  T get(int first, int last) const {
    assert(first <= last);
    T left = -1, right = -1;
    for (first += n_ceil_, last += n_ceil_; first <= last;
         first >>= 1, last >>= 1) {
      if ((first & 1) == 1) {
        left = join_(left, data_[first++]);
      }
      if ((last & 1) == 0) {
        right = join_(data_[last--], right);
      }
    }
    return join_(left, right);
  }

  template <typename Contains>
  int find(T val, Contains contains) const {
    if (!contains(data_[1], val)) {
      return n_;
    }
    int pos = 1;
    while (pos < n_ceil_) {
      if (!contains(data_[pos <<= 1], val)) {
        ++pos;
      }
    }
    return pos - n_ceil_;
  }

  friend std::ostream& operator<<(std::ostream& os, const seg_tree& tree) {
    os << '{';
    for (int i = 0; i < tree.size(); ++i) {
      if (i != 0) {
        os << ", ";
      }
      os << tree.get(i);
    }
    return os << '}';
  }

 private:
  Join join_;
  int n_, n_ceil_;
  std::vector<T> data_;
};

int main_ac() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::cout.precision(12);
  std::cerr.precision(12);
  std::mt19937 rng((std::uint_fast32_t)std::chrono::high_resolution_clock::now()
                       .time_since_epoch()
                       .count());
  int n;
  cin >> n;
  vector<int> h(n), a(n);
  for (int i = 0; i < n; ++i) {
    cin >> h[i];
  }
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  auto join = [](long long x, long long y) { return max(x, y); };
  auto update = [](long long& x, long long y) { x = y; };
  seg_tree<long long, decltype(join)> tree(n + 1, 0, join);
  for (int i = 0; i < n; ++i) {
    tree.update(h[i], a[i] + tree.get(0, h[i] - 1), update);
  }
  cout << tree.get(0, n) << '\n';
  return 0;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::cout.precision(12);
  std::cerr.precision(12);
  std::mt19937 rng((std::uint_fast32_t)std::chrono::high_resolution_clock::now()
                       .time_since_epoch()
                       .count());
  int n;
  cin >> n;
  vector<int> h(n), a(n);
  for (int i = 0; i < n; ++i) {
    cin >> h[i];
  }
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  map<int, long long> mp;
  mp[0] = 0;
  for (int i = 0; i < n; ++i) {
    long long best = a[i] + prev(mp.lower_bound(h[i]))->second;
    mp[h[i]] = best;
    auto it = mp.upper_bound(h[i]);
    while (it != mp.end() && best >= it->second) {
      it = mp.erase(it);
    }
  }
  cout << mp.rbegin()->second << '\n';
  return 0;
}

Measured memory_bytes: 14336