#![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));
}


Measured memory_bytes: 17660