• + 0 comments

    Haskell

    module Main where
    
    import Control.Monad (replicateM)
    import Data.List (partition, sortBy, splitAt)
    
    solve :: Int -> [[Int]] -> Int
    solve k trials = sum pluses - sum minuses + sum uvals
      where
        (important, unimportant) = partition (\(_ : t : _) -> t == 1) trials
        ivals = sortBy (flip compare) $ map head important
        uvals = map head unimportant
        (pluses, minuses) = splitAt k ivals
    
    main :: IO ()
    main = do
        [n, k] <- fmap (map read . words) getLine :: IO [Int]
        trials <- replicateM n $ fmap (map read . words) getLine :: IO [[Int]]
        print $ solve k trials