#include <bits/stdc++.h>

using namespace std;
class segment_tree
{
  struct node
  {
    long sum = 0;
    long max = 0;
    int from = 0;
    int to = -1;
    bool pending = false;
    long pending_val;
    int size() const
    {
      return to - from + 1;
    }
  };
  vector<node> heap;
  int heap_size;

  inline int left (int p) { return p << 1; }
  inline int right(int p) { return (p << 1) + 1; }
  inline bool contains(int from1, int to1, int from2, int to2)
  {
    return from1 <= from2 && to2 <= to1;
  }
  inline bool intersects(int from1, int to1, int from2, int to2)
  {
    return (from1 <= from2 && to1 >= from2) ||
      (from1 >= from2 && from1 <= to2);
  }

public:
  segment_tree(int N)
  {
      heap_size = 4 * N;//2 * (1 << ((int)log2(N + 1)));
      heap.resize(heap_size);
      build(1, 0, N - 1);
  }

private:
  void build(int heap_index, int from, int to)
  {
    node& n = heap[heap_index];
    n.from = from;
    n.to = to;
    if (from == to)
      {
          n.sum = 0;
          n.max = 0;
      }
    else
      {
          int middle = from + (to - from) / 2;
          build(left(heap_index), from, middle);
          if (middle + 1 <= to) build(right(heap_index), middle + 1, to);
          n.sum = 0;
          n.max = 0;
      }
  }

public:
  long get_sum(int from, int to)
  {
    return get_sum(1, from, to);
  }

private:
  long get_sum(int heap_index, int from, int to)
  {
    node& n = heap[heap_index];
    if (n.pending && contains(n.from, n.to, from, to))
      return (to - from + 1) * n.pending_val;

    if (contains(from, to, n.from, n.to))
      return n.sum;

    if (intersects(from, to, n.from, n.to))
      {
	propagate(heap_index);
	return get_sum(left(heap_index), from, to) + get_sum(right(heap_index), from, to);
      }
    return 0;
  }

public:
  long get_max(int from, int to)
  {
    return get_max(1, from, to);
  }

private:
  long get_max(int heap_index, int from, int to)
  {
    node& n = heap[heap_index];
    if (n.pending && contains(n.from, n.to, from, to))
      return n.pending_val;

    if (contains(from, to, n.from, n.to))
      return n.max;

    if (intersects(from, to, n.from, n.to))
      {
          propagate(heap_index);
          return std::max(get_max(left(heap_index), from, to), get_max(right(heap_index), from, to));
      }
    return 0;
  }

public:
  void update(int from, int to, long value)
  {
    update(1, from, to, value);
  }

private:
  void update(int heap_index, int from, int to, long value)
  {
    node& n = heap[heap_index];
    if (contains(from, to, n.from, n.to))
    {
        change(n, value);
    }
    else if (intersects(from, to, n.from, n.to))
      {
          propagate(heap_index);
          update(left(heap_index), from, to, value);
          update(right(heap_index), from, to, value);
          n.sum = heap[left(heap_index)].sum + heap[right(heap_index)].sum;
          n.max = std::max(heap[left(heap_index)].max, heap[right(heap_index)].max);
      }
  }

    void propagate(int heap_index)
        {
            node& n = heap[heap_index];
            if (n.pending)
            {
                if (n.size() != 1)
                {
                    change(heap[left(heap_index)], n.pending_val);
                    change(heap[right(heap_index)], n.pending_val);
                }
                n.pending = false;
            }
        }

    void change(node& n, long value)
        {
            n.pending = true;
            n.pending_val = value;
            n.sum = n.size() * value;
            n.max = value;
        }
};

struct flower {
    int h;
    long v;
};

int main(int argc, char *argv[])
{
    int N;
    cin >> N;
    vector<flower> flowers(N);
    for (int i = 0; i < N; i++) {
        cin >> flowers[i].h;
    }
    for (int i = 0; i < N; i++) {
        cin >> flowers[i].v;
    }
    segment_tree st(2e5 + 7);
    long res = 0;
    for (int i = 0; i < N; i++) {
        long v = st.get_max(0, flowers[i].h);
        //cout << flowers[i].h << " " << v << endl;
        if (v + flowers[i].v > res) {
            res = v + flowers[i].v;
        }
        st.update(flowers[i].h, flowers[i].h, v + flowers[i].v);
    }
    cout << res << endl;
    return 0;
}


Measured memory_bytes: 34688