module Main where

import MyArray

solve (a, b) = solve' (count a) (toArray $ count b)

solve' my [] = 0
solve' my ((b, bn) : list) = bn * (comp 0 b my) + solve' my list

solve'' value current my =
  if (current > getLength my) then 0 else
  get current my + solve'' value (current + value) my

count list = foldr (\val -> \arr -> inc val arr) (makeMyArray 1000001) list

parse (a:b:c:d:_) = (parse' b, parse' d)

parse' :: String -> [Int]
parse' s = map read $ words s

main = do
  s <- readFile "deposits.in"
  writeFile "deposits.out" $ (show $ solve $ parse $ lines s) ++ "\n"
