Problem:
Score : 100 points

Problem Statement

There are N flowers arranged in a row.
For each i (1 \leq i \leq N), the height and the beauty of the i-th flower from the left is h_i and a_i, respectively.
Here, h_1, h_2, \ldots, h_N are all distinct.

Taro is pulling out some flowers so that the following condition is met:

The heights of the remaining flowers are monotonically increasing from left to right.

Find the maximum possible sum of the beauties of the remaining flowers.

Constraints

All values in input are integers.

1 \leq N \leq 2 × 10^5

1 \leq h_i \leq N

h_1, h_2, \ldots, h_N are all distinct.

1 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
h_1 h_2 \ldots h_N
a_1 a_2 \ldots a_N

Output

Print the maximum possible sum of the beauties of the remaining flowers.

Sample Input 1

4
3 1 4 2
10 20 30 40

Sample Output 1

60

We should keep the second and fourth flowers from the left.
Then, the heights would be 1, 2 from left to right, which is monotonically increasing, and the sum of the beauties would be 20 + 40 = 60.

Sample Input 2

1
1
10

Sample Output 2

10

The condition is met already at the beginning.

Sample Input 3

5
1 2 3 4 5
1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 3

5000000000

The answer may not fit into a 32-bit integer type.

Sample Input 4

9
4 2 5 8 3 6 1 7 9
6 8 8 4 6 3 5 7 5

Sample Output 4

31

We should keep the second, third, sixth, eighth and ninth flowers from the left.

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef long double ld;
const int inf=1e9+7;
const ll longinf=1LL<<60;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
#define F first
#define S second

const int mx=100010;
const ll mod=1e9+7;

template<typename T>
struct SegmentTree{
  struct Node{
    T val;
    Node *lch,*rch;
    Node(T val):val(val),lch(nullptr),rch(nullptr){}
  };

  Node *root;
  ll sz;
  T id;

  T f(T x,T y){ 
    return max(x,y);  //changeable 
//    return x+y;      //RSQ
  }

  T g(T x,T y){ 
    return x;
    //return x+y;
  }
  
  T val(Node* t){return t ? t->val : id;}
  
  SegmentTree(ll sz_,T id_=0):root(nullptr),id(id_){
    sz=1;
    while(sz<sz_)sz*=2;
  }

  void erase(Node* t){
    if(t->lch)erase(t->lch);
    if(t->rch)erase(t->rch);
    delete t;
  }

  void erase(){
    erase(root);
  }

  Node* update(Node* t,ll k, ll l, ll r, T x){
    //assert(l<=k&&k<r);
    if(!t)t=new Node(id);
    if(r-l==1){
      t->val=g(x,t->val);
      return t;
    }
    if(k<(l+r)/2)t->lch=update(t->lch,k, l, (l+r)>>1, x);
    else t->rch=update(t->rch,k, (l+r)>>1, r, x);
    t->val=f(val(t->lch), val(t->rch));
    return t;
  }

  T get(Node* t, ll a,ll b,ll l, ll r){
    if(!t)return id;
    if(b<=l||r<=a)return id;
    if(a<=l&&r<=b)return t->val;
    T lx=get(t->lch, a, b, l, (l+r)>>1);
    T rx=get(t->rch, a, b, (l+r)>>1, r);
    return f(lx,rx);
  }
  
  void update(ll k,T x){
    root = update(root, k, 0, sz, x);
  }

  T get(ll a, ll b){
    return get(root, a, b, 0, sz);
  }
};


int main(){
  int n;
  cin >> n;
  vector<ll> h(n),a(n);
  rep(i,n){ cin >> h[i]; }
  rep(i,n){ cin >> a[i]; }
  SegmentTree<ll> sg(n+5);
 
  rep(i,n){
    ll ret = sg.get(0,h[i])+a[i];
    sg.update(h[i], ret);
  }
  cout << sg.get(0,n+1) << endl;
  return 0;
}