Google Code Jam/Text Messaging Outrage

From HaskellWiki
< Google Code Jam
Revision as of 14:45, 15 December 2008 by Mnislaih (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Solution

main = (enumFromTo (1::Int) <$> readLn) >>= mapM_ go
  where go i = do
          [p,k,l] <- map read . words <$> getLine
          nn <- map read . words <$> getLine
          printf "Case #%i: %i\n" i (solve k nn)

solve :: Int -> [Integer] -> Integer
solve k l = let rounds = map sum $ chunks k $ sortBy (flip compare) l
              in sum $ zipWith (*) [1..] rounds
chunks n [] = []
chunks n as = bs : chunks n cs where (bs,cs) = splitAt n as