Skip to content

Instantly share code, notes, and snippets.

@m2ym
Created May 3, 2013 02:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m2ym/5506779 to your computer and use it in GitHub Desktop.
Save m2ym/5506779 to your computer and use it in GitHub Desktop.
import Data.Function (on)
import Data.List (sortBy)
import Control.Monad (forM_)
solve :: Int -> Int -> [Int] -> Int
solve e r vs = solve' 0 (sortBy (flip compare `on` snd) (zip [0..] vs)) []
where
solve' gain [] as = gain
solve' gain ((i,v):vs) as = solve' (gain + v * a) vs ((i,a,b):as)
where b = maximum (0:[b' + a - (j-i) * r | (j,a,b') <- as, i < j])
a = max 0 (minimum (e:[(i-j) * r | (j,_,_) <- as, j < i]) - b)
main :: IO ()
main = do
t <- readLn
forM_ ([1..t] :: [Int]) $ \i -> do
[e,r,_] <- (map read . words) `fmap` getLine
vs <- (map read . words) `fmap` getLine
putStrLn $ "Case #" ++ show i ++ ": " ++ show (solve e r vs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment