The Haskell Program: G_profits/profits.hs
1
2
3
4
5
6
7
8 module Main where
9
10 import Text.Printf
11
12 main :: IO()
13 main = interact (showResults . map solve . readTestCases)
14
15 type InstanceType = [Int]
16 type ResultType = Int
17
18
19
20
21 showResults :: [ResultType] -> String
22 showResults = unlines . (map format)
23 where format n = printf "%d" n
24
25
26
27
28 readTestCases :: String -> [InstanceType]
29 readTestCases = parse0 . (map read) . words
30
31
32 parse0 :: [Int] -> [InstanceType]
33 parse0 [0] = []
34 parse0 (n:rest) = (take n rest) : (parse0 (drop n rest))
35 parse0 _ = error "Illegal input"
36
37
38
39
40 solve :: InstanceType -> ResultType
41 solve xs = snd (foldr combine (0,-100) xs)
42
43
44
45 combine :: Int -> (Int,Int) -> (Int,Int)
46 combine p (total,opt) =
47 let y=max (total+p) p in (y, max y opt)