import Data.List
import Data.Tree
import Data.Array  --変数衝突
import Data.Array.Unboxed
import Data.Array.MArray
import Data.Array.ST
import qualified Data.Vector.Unboxed as V (last,(!),fromList,Vector)
import Control.Monad
import Control.Monad.ST

type Bs = V.Vector Int
type DP s = STUArray s Int Int
{-
dp_i :: DP s -> Int -> Bs -> ST s ()
dp_i dparr h as = do
  lows <- forM [1..h-1] $ readArray dparr
  writeArray dparr h $ maximum lows
-}
dp_i :: DP s -> Int -> [Int] -> [Int] -> ST s ()
dp_i dparr n [] [] = return ()
dp_i dparr n (h:hs) (a:as) = do
  nowa <- readArray dparr h
  let a' = a+nowa
  forM_ [h..n] $ \h' -> do
    a'' <- readArray dparr h'
    if a'' > a' then return ()
      else writeArray dparr h' a'
  dp_i dparr n hs as

solve :: Int -> [Int] -> [Int] -> ST s Int
solve n hs as = do
  dparr <- newArray (1,n) 0
  dp_i dparr n hs as
  readArray dparr n

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

mod0 :: Int -> Int
mod0 n = if n < 1000000007 then n else mod n 1000000007
