import           Control.Monad.Writer
import qualified Data.ByteString.Char8 as BS
import           Data.Char
import           Data.List

main = do
  BS.getLine
  a <- unfoldr (BS.readInt . BS.dropWhile isSpace) <$> BS.getLine
  b <- unfoldr (BS.readInt . BS.dropWhile isSpace) <$> BS.getLine
  print $ solve $ Cities a b

solve :: Cities -> Int
solve cities = getSum killResult
  where
    (_, killResult) = runWriter $ killThemAll cities

type MonsterCount = Int

type SavePower = Int

type KillResult = Sum Int

data Cities =
  Cities
    { monsterCounts :: [MonsterCount]
    , saverPowers   :: [SavePower]
    }

killThemAll :: Cities -> Writer KillResult ()
killThemAll Cities {saverPowers = []} = writer ((), Sum 0)
killThemAll cities = do
  cities1 <- firstSaverSaveFirstCity cities
  cities2 <- firstSaverSaveSecondCity cities1
  killThemAll $ restCities cities2
  where
    firstSaverSaveFirstCity :: Cities -> Writer KillResult Cities
    firstSaverSaveFirstCity Cities {monsterCounts = m:ms, saverPowers = s:ss} =
      writer (Cities (mAfter : ms) (sAfter : ss), killCount)
      where
        (mAfter, sAfter, killCount) = kill m s
    firstSaverSaveSecondCity :: Cities -> Writer KillResult Cities
    firstSaverSaveSecondCity Cities { monsterCounts = m1:m2:ms
                                    , saverPowers = s:ss
                                    } =
      writer (Cities (m1 : m2After : ms) (sAfter : ss), killCount)
      where
        (m2After, sAfter, killCount) = kill m2 s
    kill :: MonsterCount -> SavePower -> (MonsterCount, SavePower, KillResult)
    kill monsterCount savePower =
      (monsterCount - killCount, savePower - killCount, Sum killCount)
      where
        killCount = min monsterCount savePower
    restCities :: Cities -> Cities
    restCities Cities {monsterCounts = m:ms, saverPowers = s:ss} = Cities ms ss
