{-# LANGUAGE BangPatterns #-}
import           Control.Exception           (assert)
import           Control.Monad
import           Control.Monad.Primitive
import qualified Control.Monad.ST            as ST
import qualified Data.Array.IO               as IO
import qualified Data.Array.ST               as ST
import qualified Data.Array.Unboxed          as A
import           Data.Bits
import qualified Data.ByteString.Char8       as BS
import           Data.Char
import           Data.Foldable
import           Data.List
import qualified Data.Map.Strict             as Map
import           Data.Maybe
import qualified Data.Sequence               as Seq
import qualified Data.Set                    as Set
import qualified Data.Vector                 as VB
import qualified Data.Vector.Mutable         as VBM
import qualified Data.Vector.Unboxed         as V
import qualified Data.Vector.Unboxed.Mutable as VM
import           Debug.Trace

readInt = fst . fromJust . BS.readInt
readIntList = map readInt . BS.words
getInt = readInt <$> BS.getLine
getIntList = readIntList <$> BS.getLine
getIntVec n = V.unfoldrN n (BS.readInt . BS.dropWhile isSpace) <$> BS.getLine

readInteger = fst . fromJust . BS.readInteger
readIntegerList = map readInteger . BS.words
getInteger = readInteger <$> BS.getLine
getIntegerList = readIntegerList <$> BS.getLine

inf :: Int
inf = 10^18

quickSortVec l u arr
  | l >= u    = return ()
  | otherwise = do
      VM.unsafeSwap arr l ((u+l)`div`2)
      t <- VM.unsafeRead arr l
      let i = l
          j = u + 1
      nj <- loopVec t u i j arr
      VM.unsafeSwap arr l nj
      quickSortVec l (nj-1) arr
      quickSortVec (nj+1) u arr

loopVec !t !u !i !j arr = do
  ni <- doWhile i (+1)         (< t)
  nj <- doWhile j (subtract 1) (> t)
  if ni > nj
    then return nj
    else do VM.unsafeSwap arr ni nj
            loopVec t u ni nj arr
              where
                {-# INLINE doWhile #-}
                doWhile k op p
                  | nk > u    = return nk
                  | otherwise = do
                      x <- VM.unsafeRead arr nk
                      if p x then
                        doWhile nk op p
                        else
                        return nk
                        where
                          nk = op k

main = do
  [n, m] <- getIntList
  v <- V.replicateM n $ do
    [x, y, z] <- getIntList
    return (x, y, z)

  d0 <- VM.new n
  d1 <- VM.new n
  d2 <- VM.new n
  d3 <- VM.new n
  d4 <- VM.new n
  d5 <- VM.new n
  d6 <- VM.new n
  d7 <- VM.new n

  forM_ [0..(n-1)] $ \i -> do
    let (x, y, z) = v V.! i
    VM.write d0 i (   x + y + z, i)
    VM.write d1 i (   x + y - z, i)
    VM.write d2 i (   x - y + z, i)
    VM.write d3 i (   x - y - z, i)
    VM.write d4 i ((-x) + y + z, i)
    VM.write d5 i ((-x) + y - z, i)
    VM.write d6 i ((-x) - y + z, i)
    VM.write d7 i ((-x) - y - z, i)

  quickSortVec 0 (n-1) d0
  quickSortVec 0 (n-1) d1
  quickSortVec 0 (n-1) d2
  quickSortVec 0 (n-1) d3
  quickSortVec 0 (n-1) d4
  quickSortVec 0 (n-1) d5
  quickSortVec 0 (n-1) d6
  quickSortVec 0 (n-1) d7

  let score d i sx sy sz
        | i == m = return (abs sx + abs sy + abs sz)
        | otherwise = do
            (_, j) <- VM.read d (n-i-1)
            let (x, y, z) = v V.! j
            score d (i+1) (sx+x) (sy+y) (sz+z)

      solve :: Int -> Int -> IO Int
      solve i res
        | i == 8 = return res
        | otherwise = do
            let d | i == 0 = d0
                  | i == 1 = d1
                  | i == 2 = d2
                  | i == 3 = d3
                  | i == 4 = d4
                  | i == 5 = d5
                  | i == 6 = d6
                  | i == 7 = d7
            s <- score d 0 0 0 0
            solve (i+1) (max res s)

  ans <- solve 0 0
  print ans

Measured memory_bytes: 3452