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++ (GCC 9.2.1) (judge verdict: Runtime Error)

#include<bits/stdc++.h>
using namespace std;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifndef ONLINE_JUDGE
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
using ll = long long;
const int N = 2e5+500;
vector<ll> t(2*N);
void update(int k, ll value, int v, int tl, int tr) {
	if(tl==tr) {
		t[v] = value;
		return;
	} 
	int mid = tl + (tr-tl)/2;
	if(k<=mid) {
		update(k,value, v*2,tl,mid);
	} else {
		update(k,value,v*2+1,mid+1,tr);
	}
	t[v] = max(t[v*2],t[v*2+1]);
}
int query(int v, int tl, int tr, int l, int r) {
	if(l>r) return 0;
	if(l==tl && r==tr) {
		return t[v];
	} 
	int mid = tl + (tr-tl)/2;
	return max(query(v*2,tl,mid,l,min(mid,r)), query(v*2+1,mid+1,tr,max(l,mid+1),r));
}
int main() {
	int n;
	cin>>n;
	vector<ll> h(n), a(n);
	for(int i=0;i<n;i++) {
		cin>>h[i];
	}
	for(int i=0;i<n;i++) {
		cin>>a[i];
	}
	vector<ll> dp(n+1); //max possible score taking the last tree of height i
	int root = 1, tl = 0, tr = n;
	for(int i=0;i<n;i++) {
		// commented code works fine but gives TLE
		//for(int j=0;j<h[i];j++) {
			//dp[h[i]] = max(dp[h[i]], dp[j] + a[i]);
		//}
		ll maxed = query(root,tl,tr,0,h[i]);
		debug()<<imie(maxed);
		dp[h[i]] = a[i] + maxed;
		update(h[i],dp[h[i]], root, tl ,tr);
	}
	debug()<<imie(dp);
	ll ans = -1;
	for(ll it:dp) {
		ans = max(ans,it);
	}
	cout<<ans;
}