-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTreats for the Cows.hs
50 lines (44 loc) · 1.48 KB
/
Treats for the Cows.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
{-# LANGUAGE BangPatterns #-}
module Main where
import Data.Array
task = "http://www.spoj.com/problems/TRT/"
main :: IO ()
main' = print $ most' [1..2000]
main = do
x <- readLn
interact $ flip (++) n . proccess . take x . lines
proccess :: [String] -> String
proccess ss = show . most . fmap read $ ss
--most :: [Integer] -> Integer
most [] = 0
most is = fst . foldl bar (last begin, begin) $ [1..len]
where
len :: Integer
len = fromIntegral $ length is
prices = listArray (0, len-1) is
begin = 0 : zipWith3 (\i1 i2 i3 -> i1 + i2 * i3) begin (reverse is) [1..]
foo _ _ _ [_] = []
foo ix1 ix2 i1 (i2:ii) = res : foo ix1 (ix2+1) res ii
where
age = ix1 + ix2
bar (best, [x]) i = (max best x, [])
bar (best, (x:xx)) i = (max best best', list')
where
start = x + prices ! (i-1) * i
most' :: [Int] -> Int
most' is = maximum $ fmap (\i -> memfoo ! (i, (len - i))) [0..len]
where
prices = listArray (0, len) is
len = length is
foo 0 0 = 0
foo i 0 = memfoo ! (i-1, 0) + prices ! (i-1) * i
foo 0 i = memfoo ! (0, i-1) + prices ! (len-i) * i
foo i1 i2
| age > len = -1
| otherwise = max
(memfoo ! (i1-1, i2) + prices ! (i1-1) * age)
(memfoo ! (i1, i2-1) + prices ! (len-i2) * age)
where
age = i1 + i2
memfoo :: Array (Int, Int) Int
memfoo = listArray ((0, 0), (len, len)) [foo i1 i2 | i1 <- [0..len], i2 <- [0..len]]