import Data.List
import Data.Array  --変数衝突
import Data.Array.Unboxed
import Data.Array.MArray
import Data.Array.ST
import Control.Monad
import Control.Monad.ST

type Elem = Int
type SegT s = STUArray s Int Elem

{-
セグ木について
sup2によりnを２の冪にする
配列をdp[1]~[n] 変数はi'
セグ木をsegT[1]~[2*n-1] 変数はi
添え字iに対して、親ノードはi`div`2、子ノードは2*i,2*i+1
高さは (floor logBase 2 (fromIntegral i))+1
それぞれPは一点、Rは範囲の操作
-}
-- n+n/2+n/4+...<=2*n-1だが、子ノードの定義の仕方により2*n-1を超えることがある。

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

newSegT :: MArray a e m => Int -> e -> m (a Int e)
newSegT n e = newArray (1,2*n-1) e

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

--セグ木の更新,O(2*(logN))
--(left,right)の範囲で更新
modifyRSegT :: SegT s -> (Int,Int) -> (Int,Int) -> (Elem -> Elem) -> 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' <- readArray segT i
   writeArray segT i (f a')
  | otherwise  = do
   let mid = (l+r)`div`2
   modifyRSegT segT (left,right) (mid+1,r) f n (2*i+1)
   modifyRSegT segT (left,right) (l,mid) f n (2*i)

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 1
  dp_i segT n hs as

solve :: Int -> [Int] -> [Int] -> ST s Elem
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
