import Data.Function (on) import Data.List (sortBy, groupBy, minimumBy) import Data.Ord (comparing) (|>) x f = f x solve xs ys = let partialSum s (y,x) = s - x *2 + 1 in zip ys xs |> sortBy (comparing fst) |> groupBy (on (==) fst) |> scanl (\s x -> (fst s + foldl partialSum 0 x, fst $ head x)) (foldl1 (+) xs, minimum ys - 1) |> minimumBy (comparing fst) |> snd x1 = [0, 0, 0, 1, 1, 1] y1 = [6, 3, 4, 4, 4, 2.1] main = print $ solve x1 y1