import Data.List
import Data.Array.Unboxed
import Data.Array.MArray
import Data.Array.ST
import Control.Monad.Primitive (PrimState)
import qualified Data.Vector.Unboxed as V
import qualified Data.Vector.Mutable as MV
import Control.Monad
import Control.Monad.ST
import Debug.Trace

type Elem = Int --要素の型
type SegT s = MV.MVector (PrimState (ST s)) Elem

{-
セグ木について
sup2によりnを２の冪にする
配列dp[0]~[2*n-2]
添え字iに対して、親ノードは(i-1)`div`2、子ノードは2*i+1,2*i+2
高さは (floor logBase 2 (fromIntegral i))+1
関数名についている1は葉を表している
-}

--nを超える最小の２の冪
sup2 :: Int -> Int
sup2 n = 2^(ceiling $ logBase 2 (fromIntegral n :: Double) :: Int)

-- a == 1 を想定
newSegT :: Int -> Elem -> ST s (SegT s)
newSegT n e = MV.replicate (2*n-1) e

--logN
readPSegT :: SegT s -> Int -> Int -> Int -> ST s Elem
readPSegT segT n i max0 = readPSegT' segT (n+i-2) max0
readPSegT' :: SegT s -> Int -> Int -> ST s Int
readPSegT' segT i max0
  | 0 > i = return max0
  | otherwise = do
    a <- MV.read segT i
    readPSegT' segT ((i-1)`div`2) (max a max0)

--セグ木の更新,O(2*(logN))
--(left,right)の範囲で更新
modifyRSegT :: SegT s -> (Int,Int) -> (Int,Int) -> (Int -> Int) -> Int -> Int -> ST s ()
modifyRSegT segT (left,right) (l,r) f n i
  | r < left || right < l = return ()
  | 2*n-1 < i   = error $ "modifyRSegT Error : i = " ++ (show i) ++ " " ++ (show ((left,right), (l,r))) ---Error??
  | left <= l && r <= right = do
   a' <- MV.read segT i
   MV.write segT i (f a')
  | otherwise  = do
   let mid = (l+r)`div`2
   modifyRSegT segT (left,right) (mid+1,r) f n (2*i+2)
   modifyRSegT segT (left,right) (l,mid) f n (2*i+1)

dp_i :: SegT s -> Int -> [Int] -> [Int] -> ST s ()
dp_i segT n [] [] = return ()
dp_i segT n (h:hs) (a:as) = do
  nowa <- readPSegT segT n h 0
  modifyRSegT segT (h,n) (1,n) (max $ nowa+a) n 0
  dp_i segT n hs as

solve :: Int -> [Int] -> [Int] -> ST s Int
solve n hs as = do
  segT <- newSegT n 0
  dp_i segT n hs as
  readPSegT segT n n 0

main :: IO ()
main = do
 n  <- read <$> getLine
 hs <- map read.words <$> getLine
 as <- map read.words <$> getLine
 putStrLn $ show $ runST $ solve (sup2 n) hs as
