fork(3) download
  1. f = g 0 [] where
  2. g acc _ [] = acc
  3. g acc [] (x:xs) = g acc [x] xs
  4. g acc ys (x:xs) = if x > head ys
  5. then g (acc + q) (x : map (`max` x) ys) xs
  6. else g acc (x:ys) xs
  7. where
  8. q = sum $ filter (> 0) $ map (x `min` last ys -) ys
  9.  
  10. main = readLn >>= (print . f)
Success #stdin #stdout 0s 6344KB
stdin
[0, 5, 0, 1, 0, 2, 1, 0]
stdout
5