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 Rust (1.15.1) (judge verdict: Accepted)

#![allow(non_snake_case, unused)]
use std::collections::*;

////////// missing functions in 1.15.1 //////////
macro_rules! eprint {
	($($t:tt)*) => {{
		use ::std::io::Write;
		let _ = write!(::std::io::stderr(), $($t)*);
	}};
}
macro_rules! eprintln {
	() => { eprintln!(""); };
	($($t:tt)*) => {{
		use ::std::io::Write;
		let _ = writeln!(::std::io::stderr(), $($t)*);
	}};
}
macro_rules! dbg {
	($v:expr) => {{
		let val = $v;
		eprintln!("[{}:{}] {} = {:?}", file!(), line!(), stringify!($v), val);
		val
	}}
}
/////////////////////////////////////////////////
pub trait SetMinMax {
	fn setmin(&mut self, v: Self) -> bool;
	fn setmax(&mut self, v: Self) -> bool;
}
impl<T> SetMinMax for T where T: PartialOrd {
	fn setmin(&mut self, v: T) -> bool {
		*self > v && { *self = v; true }
	}
	fn setmax(&mut self, v: T) -> bool {
		*self < v && { *self = v; true }
	}
}

macro_rules! mat {
	($($e:expr),*) => { Vec::from(vec![$($e),*]) };
	($($e:expr,)*) => { Vec::from(vec![$($e),*]) };
	($e:expr; $d:expr) => { Vec::from(vec![$e; $d]) };
	($e:expr; $d:expr $(; $ds:expr)+) => { Vec::from(vec![mat![$e $(; $ds)*]; $d]) };
}

pub fn readln() -> String {
	let mut line = String::new();
	::std::io::stdin().read_line(&mut line).unwrap_or_else(|e| panic!("{}", e));
	line
}

macro_rules! read {
	($($t:tt),*; $n:expr) => {{
		let stdin = ::std::io::stdin();
		let ret = ::std::io::BufRead::lines(stdin.lock()).take($n).map(|line| {
			let line = line.unwrap();
			let mut it = line.split_whitespace();
			_read!(it; $($t),*)
		}).collect::<Vec<_>>();
		ret
	}};
	($($t:tt),*) => {{
		let line = readln();
		let mut it = line.split_whitespace();
		_read!(it; $($t),*)
	}};
}

macro_rules! _read {
	($it:ident; [char]) => {
		_read!($it; String).chars().collect::<Vec<_>>()
	};
	($it:ident; [u8]) => {
		Vec::from(_read!($it; String).into_bytes())
	};
	($it:ident; [$t:ty]) => {
		$it.map(|s| s.parse::<$t>().unwrap_or_else(|e| panic!("{}", e))).collect::<Vec<_>>()
	};
	($it:ident; $t:ty) => {
		$it.next().unwrap_or_else(|| panic!("input mismatch")).parse::<$t>().unwrap_or_else(|e| panic!("{}", e))
	};
	($it:ident; $($t:tt),+) => {
		($(_read!($it; $t)),*)
	};
}

pub fn main() {
	let _ = ::std::thread::Builder::new().name("run".to_string()).stack_size(32 * 1024 * 1024).spawn(run).unwrap().join();
}

/// point update + range query.
/// Associative: op(x, op(y, z)) = op(op(x, y), z).
pub struct SegTreePR<T, F> {
	n: usize,
	ts: Vec<T>,
	id: T,
	op: F
}

impl<T: Clone, F: Fn(T, T) -> T> SegTreePR<T, F> {
	pub fn new(n: usize, id: T, op: F) -> SegTreePR<T, F> {
		SegTreePR { n: n, ts: vec![id.clone(); n * 2], id: id, op: op }
	}
	pub fn construct(a: Vec<T>, id: T, op: F) -> SegTreePR<T, F> {
		let n = a.len();
		let mut ts = vec![id.clone(); n];
		ts.extend(a.into_iter());
		for i in (1..n).rev() {
			ts[i] = op(ts[i * 2].clone(), ts[i * 2 + 1].clone());
		}
		SegTreePR { n: n, ts: ts, id: id, op: op }
	}
	/// ts[i] <- t
	pub fn set(&mut self, mut i: usize, t: T) {
		i += self.n;
		self.ts[i] = t;
		while { i >>= 1; i > 0 } {
			self.ts[i] = (self.op)(self.ts[i * 2].clone(), self.ts[i * 2 + 1].clone());
		}
	}
	/// op(ts[s], ..., ts[t-1])
	pub fn query(&self, s: usize, t: usize) -> T {
		let (mut al, mut ar) = (self.id.clone(), self.id.clone());
		let (mut s, mut t) = (self.n + s, self.n + t);
		while s < t {
			if s & 1 != 0 {
				al = (self.op)(al, self.ts[s].clone());
				s += 1;
			}
			if t & 1 != 0 {
				ar = (self.op)(self.ts[t - 1].clone(), ar);
			}
			s >>= 1;
			t >>= 1;
		}
		(self.op)(al, ar)
	}
	#[inline]
	pub fn get(&self, i: usize) -> T {
		self.ts[self.n + i].clone()
	}
	pub fn get_all(&self) -> &[T] {
		&self.ts[self.n..]
	}
}

fn run() {
	let N = read!(usize);
	let hs = read!([usize]);
	let a = read!([i64]);
	let mut dp = SegTreePR::new(N + 1, 0, std::cmp::max);
	for i in 0..N {
		let t = dp.query(0, hs[i]) + a[i];
		dp.set(hs[i], t);
	}
	println!("{}", dp.query(0, N + 1));
}