CodeEff/ECCO huggingface dataset:
# Original solution
N,M = list(map(int,input().split()))
xs,ys,zs =[],[],[]
comps1 = []
comps2 = []
comps3 = []
comps4 = []
comps5 = []
comps6 = []
comps7 = []
comps8 = []
for i in range(N):
    x,y,z = list(map(int,input().split()))
    raw = [x+y+z,x+y-z,x-y+z,x-y-z,-x+y+z,-x+y-z,-x-y+z,-x-y-z,]
    comps1.append(raw[0])
    comps2.append(raw[1])
    comps3.append(raw[2])
    comps4.append(raw[3])
    comps5.append(raw[4])
    comps6.append(raw[5])
    comps7.append(raw[6])
    comps8.append(raw[7])

ans = 0
###
dp = [[0 for _ in range(M+1)] for _ in range(N+1)]
for i in range(1,N+1):
    for j in range(1,M+1):
        dp[i][j] = max(dp[i-1][j],dp[i-1][j-1]+comps1[i-1])
cand = dp[N][M]
ans = max(ans,cand)
###
dp = [[0 for _ in range(M+1)] for _ in range(N+1)]
for i in range(1,N+1):
    for j in range(1,M+1):
        dp[i][j] = max(dp[i-1][j],dp[i-1][j-1]+comps2[i-1])
cand = dp[N][M]
ans = max(ans,cand)
###
dp = [[0 for _ in range(M+1)] for _ in range(N+1)]
for i in range(1,N+1):
    for j in range(1,M+1):
        dp[i][j] = max(dp[i-1][j],dp[i-1][j-1]+comps3[i-1])
cand = dp[N][M]
ans = max(ans,cand)
###
dp = [[0 for _ in range(M+1)] for _ in range(N+1)]
for i in range(1,N+1):
    for j in range(1,M+1):
        dp[i][j] = max(dp[i-1][j],dp[i-1][j-1]+comps4[i-1])
cand = dp[N][M]
ans = max(ans,cand)
###
dp = [[0 for _ in range(M+1)] for _ in range(N+1)]
for i in range(1,N+1):
    for j in range(1,M+1):
        dp[i][j] = max(dp[i-1][j],dp[i-1][j-1]+comps5[i-1])
cand = dp[N][M]
ans = max(ans,cand)
###
dp = [[0 for _ in range(M+1)] for _ in range(N+1)]
for i in range(1,N+1):
    for j in range(1,M+1):
        dp[i][j] = max(dp[i-1][j],dp[i-1][j-1]+comps6[i-1])
cand = dp[N][M]
ans = max(ans,cand)
###
dp = [[0 for _ in range(M+1)] for _ in range(N+1)]
for i in range(1,N+1):
    for j in range(1,M+1):
        dp[i][j] = max(dp[i-1][j],dp[i-1][j-1]+comps7[i-1])
cand = dp[N][M]
ans = max(ans,cand)
###
dp = [[0 for _ in range(M+1)] for _ in range(N+1)]
for i in range(1,N+1):
    for j in range(1,M+1):
        dp[i][j] = max(dp[i-1][j],dp[i-1][j-1]+comps8[i-1])
cand = dp[N][M]
ans = max(ans,cand)

print(ans)

# Optimized solution
N,M = list(map(int,input().split()))
comp = [[] for _ in range(8)]
for i in range(N):
    x,y,z = list(map(int,input().split()))
    raw = [x+y+z,x+y-z,x-y+z,x-y-z,-x+y+z,-x+y-z,-x-y+z,-x-y-z]
    for j in range(8):
        comp[j].append(raw[j])
ans = 0
for k in range(8):
    comp[k].sort(reverse=True)
    sum = 0
    for l in range(M):
        sum += comp[k][l]
    ans = max(ans,sum)


print(ans)
# Original solution
n, m = list(map(int, input().split()))
xyz = [tuple(map(int, input().split())) for _ in range(n)]
def isum(i, x, y, z):
    s = 0
    s += x if (i>>0)&1 else -x
    s += y if (i>>1)&1 else -y
    s += z if (i>>2)&1 else -z
    return s
ans = 0
for i in range(2**3):
    dp = [0 for _ in range(m+1)]
    for k in range(n):
        x, y, z = xyz[k]
        ndp = [0 for _ in range(m+1)]
        for j in range(1, m+1):
            # iを含める場合は、一つ前のj-1から
            # iを含めない場合は、一つ前のjから
            sumj_1 = dp[j-1]
            sj_1 = sumj_1 + isum(i, x, y, z)
            noinc_sum = dp[j]
            ndp[j] = max(sj_1, noinc_sum)

        dp = ndp
    ans = max(ans, dp[-1])
print(ans)


# Optimized solution
n, m = list(map(int, input().split()))
xyz = [tuple(map(int, input().split())) for _ in range(n)]
ans = 0
from itertools import product
for p in product([-1, 1], repeat=3):
    xyz_ = [l[0]*p[0] + l[1]*p[1] + l[2]*p[2] for l in xyz]
    xyz_ = sorted(xyz_, reverse=True)
    ans = max(ans, sum(xyz_[:m]))
print(ans)
# Original solution
N,M = list(map(int,input().split()))
xyz = [list(map(int,input().split())) for _ in range(N)]
ans = [[] for _ in range(8)]
for x,y,z in xyz:
    for i in range(8):
        ans[i].append(x*((-1)**i)+y*((-1)**(i//2))+z*((-1)**(i//4)))
out = []
for i in range(8):
    ans[i].sort(reverse=True)
    out.append(sum(ans[i][:M]))
print((max(out)))

# Optimized solution
N,M = list(map(int,input().split()))
XYZ = [list(map(int,input().split())) for _ in [0]*N]
W = [[0]*N for _ in [0]*4]
for i,xyz in enumerate(XYZ):
    x,y,z = xyz
    W[0][i] = x+y+z
    W[1][i] = x+y-z
    W[2][i] = x-y+z
    W[3][i] = x-y-z
ans = 0
for i in range(4):
    W[i].sort()
    ans = max(ans,
             abs(sum(W[i][:M])),
             abs(sum(W[i][-M:])))
if M==0:ans=0
print(ans)
# Original solution
import functools
[N, M] = [int(x) for x in input().split()]
XYZ = []
for i in range(N):
  XYZ.append([int(x) for x in input().split()])
result = 0
for x in range(-1, 2, 2):
  for y in range(-1, 2, 2):
    for z in range(-1, 2, 2):
      contribute = [x, y, z]
      score = [functools.reduce(lambda a, b: a + b, [xyz[i] * contribute[i] for i in range(3)]) for xyz in XYZ]
      score.sort(reverse=True)
      S = functools.reduce(lambda a, b: a + b, score[:M]) if M else 0
      result = max(result, S)
print(result)

# Optimized solution
N, M = list(map(int, input().split()))
Cakes = [[] for i in range(N)]
for i in range(N):
  Cakes[i] = [int(x) for x in input().split()]
ans = 0
for s in range(8):
  sx = 1 if s % 2 else -1
  sy = 1 if s // 2 % 2 else -1
  sz = 1 if s // 4 % 2 else -1
  ans = max(sum(sorted([sx * x + sy * y + sz * z for x, y, z in Cakes], reverse=True)[:M]), ans)
print(ans)
# Original solution
# coding: utf-8

n, m = [int(x) for x in input().split()]
xx = []
yy = []
zz = []

for _ in range(n):
    x, y, z = [int(x) for x in input().split()]
    xx.append(x)
    yy.append(y)
    zz.append(z)

result_points = []

for k in range(8):
    max_point = 0
    selected_indexes = []
    for i in range(m):
        max_plus_point = 0
        max_index = 0
        for j in range(n):
            if j in selected_indexes:
                continue
            plus_point = 0
            if k % 2 == 1:
                plus_point += xx[j]
            else:
                plus_point -= xx[j]
            if (k // 2) % 2 == 1:
                plus_point += yy[j]
            else:
                plus_point -= yy[j]
            if (k // 4) % 2 == 1:
                plus_point += zz[j]
            else:
                plus_point -= zz[j]
            if plus_point > max_plus_point:
                max_index = j
                max_plus_point = plus_point
        max_point += max_plus_point
        selected_indexes.append(max_index)
    result_points.append(max_point)

print((max(result_points)))


# Optimized solution
# coding: utf-8

n, m = [int(x) for x in input().split()]
xx = []
yy = []
zz = []

for _ in range(n):
    x, y, z = [int(x) for x in input().split()]
    xx.append(x)
    yy.append(y)
    zz.append(z)

result_points = []

results = []

for i in range(8):
    results.append([])
    for j in range(n):
        plus_point = 0
        if i % 2 == 1:
            plus_point += xx[j]
        else:
            plus_point -= xx[j]
        if (i // 2) % 2 == 1:
            plus_point += yy[j]
        else:
            plus_point -= yy[j]
        if (i // 4) % 2 == 1:
            plus_point += zz[j]
        else:
            plus_point -= zz[j]
        results[i].append(plus_point)
    results[i].sort()
    results[i].reverse()

result_points = []

for i in range(8):
    result_points.append(sum(results[i][0:m]))

print((max(result_points)))
# Original solution
import itertools

N, M = list(map(int, input().split()))
l = [list(map(int, input().split())) for _ in range(N)]

r = 0
for c in itertools.combinations(l, M):
  s = sum(abs(sum(v[i] for v in c)) for i in range(3))
  if s > r:
    r = s

print(r)

# Optimized solution
N, M = list(map(int, input().split()))
l = [list(map(int, input().split())) for _ in range(N)]

a = 0

for xs in (1, -1):
  for ys in (1, -1):
    for zs in (1, -1):
      l_s = [x*xs + y*ys + z*zs for x, y, z in l]
      l_s.sort(reverse=True)
      a = max(sum(l_s[:M]), a)

print(a)
# Original solution
from itertools import combinations

def score (cids):
    x = abs(sum(data[cid][0] for cid in cids))
    y = abs(sum(data[cid][1] for cid in cids))
    z = abs(sum(data[cid][2] for cid in cids))
    return x + y + z

n, m = list(map(int, input().split()))
data = list([int(x) for x in input().split()] for _ in range(n))
print((max(score(x) for x in combinations(list(range(n)), m))))

# Optimized solution
n, m = list(map(int, input().split()))
data = [[int(x) for x in input().split()] for _ in range(n)]

pats = [(x,y,z) for x in [-1,1] for y in [-1,1] for z in [-1,1]] # +++,++-,...,---
score = lambda sgns, vals: sum([s * x for(s, x) in zip(sgns, vals)])  # i.e.x+y-z
result = max(sum(sorted((score(pat, row) for row in data), reverse=True )[:m]) for pat in pats)
print(result)
# Original solution
N, M = list(map(int, input().split()))

cakes = []
for _ in range(N):
    cakes.append(list(map(int, input().split())))

memo = dict()

def pick_cake(picked):
    key = ','.join([str(p) for p in sorted(picked)])
    if key in memo:
        return memo[key]

    if len(picked) == M:
        memo[key] = sum([abs(sum([cakes[i][j] for i in picked])) for j in range(3)])
        return memo[key]

    answer = 0
    for i in range(N):
        if i in picked:
            continue
        answer = max(answer, pick_cake(picked + [i]))

    memo[key] = answer
    return answer

print((pick_cake([])))

# Optimized solution
from itertools import product

N, M = list(map(int, input().split()))

cakes = []
for _ in range(N):
    cakes.append(list(map(int, input().split())))

def _eval1(op):
    def _eval2(x):
        return eval(''.join(['{}{}'.format(op[i], x[i]) for i in range(3)]))

    return _eval2

answer = 0
for op in product('+-', repeat=3):
    answer = max(answer, sum([abs(sum([cake[j] for cake in sorted(cakes, key=_eval1(op))[:M]])) for j in range(3)]))

print(answer)
# Original solution
N,M=list(map(int,input().split()))
xyz=[list(map(int,input().split()))for i in range(N)]
ans=0
for i in range(8):
    i=bin(i)[2:].zfill(3)
    k=[]
    for j in range(3):
        if i[j]=="0":
            k.append(1)
        else:
            k.append(-1)
    l=[k[0]*x+k[1]*y+k[2]*z for x,y,z in xyz]
    l.sort(reverse=True)
    ans=max(ans,sum(l[:M]))
print(ans)


# Optimized solution
N,M=list(map(int,input().split()))
xyz=[]
for i in range(N):
    x,y,z=list(map(int,input().split()))
    xyz.append([x,y,z])
ans=0
for k in range(8):
    A=[k>>2 &1,k>>1 &1,k>>0 &1]
    l=[]
    for i in range(N):
        t=0
        for j in range(3):
            if A[j]==0:
                t+=xyz[i][j]
            else:
                t-=xyz[i][j]
        l.append(t)
    l.sort(reverse=True)
    ans=max(ans,sum(l[:M]))
print(ans)
# Original solution
import itertools

if __name__ == '__main__':

	n,m = list(map(int,input().split()))

	A =[]
	for i in range(n):
		A.append(list(map(int,input().split())))

	maxval = 0
	for v in itertools.combinations(A,m):
		a = abs(sum(list([x[0] for x in v])))
		b = abs(sum(list([x[1] for x in v])))
		c = abs(sum(list([x[2] for x in v])))

		maxval = max(maxval,a+b+c)

	print(maxval)


# Optimized solution
import itertools

if __name__ == '__main__':

	n,m = list(map(int,input().split()))

	A =[]
	for i in range(n):
		A.append(list(map(int,input().split())))

	P = list(itertools.product([1,-1],repeat=3))

	maxval = 0

	for p in P:
		B=[]
		for a in A:
			val = (a[0] * p[0]) + (a[1] * p[1]) + (a[2] * p[2])
			B.append(val)

		#結果をソートして上位ｍ番目までの合計を計算する	
		C = sorted(B,reverse=True)
		maxval = max(maxval,sum(C[:m]))

	print(maxval)
# Original solution
#おわり、むり

N,W = list(map(int,input().split()))

X,Y,Z = [],[],[]
for i in range(N):
    x,y,z = list(map(int,input().split()))
    X.append(x)
    Y.append(y)
    Z.append(z)  
    
dp = [[[0,0,0] for i in range(W+1)] for j in range(N+1)]
for i in range(W+1):
    dp[0][i][0]= 0 #X
    dp[0][i][1]= 0 #Y
    dp[0][i][2]= 0 #Z
    
ans = 0
for fgo in range(0,8):
    FGO = [fgo%2,(fgo//2)%2,(fgo//4)%2]
    for i in range(N):
        for w in range(W+1):
            if w>=1:
                x = dp[i][w-1][0]+X[i]
                y = dp[i][w-1][1]+Y[i]
                z = dp[i][w-1][2]+Z[i]
                NOW = (x*pow(-1,FGO[0]))+(y*pow(-1,FGO[1]))+(z*pow(-1,FGO[2]))
                BEFORE = sum([dp[i][w][f]*pow(-1,FGO[f]) for f in range(3)])
                if NOW > BEFORE:
                    dp[i+1][w][0] = x
                    dp[i+1][w][1] = y
                    dp[i+1][w][2] = z
                else:
                    dp[i+1][w][0] = dp[i][w][0]
                    dp[i+1][w][1] = dp[i][w][1]
                    dp[i+1][w][2] = dp[i][w][2]
            else:
                dp[i+1][w][0] = dp[i][w][0]
                dp[i+1][w][1] = dp[i][w][1]
                dp[i+1][w][2] = dp[i][w][2]
    MAX_ABS = abs(dp[N][W][0])+abs(dp[N][W][1])+abs(dp[N][W][2])
    if MAX_ABS > ans: ans = MAX_ABS
    #print(MAX_ABS,FGO,dp)

print(ans)

# Optimized solution
N,W = list(map(int,input().split()))

L = []
for i in range(N):
    L.append(list(map(int,input().split())))
    
MAX_ANS = 0
for fgo in range(0,8):
    FGO = [pow(-1,fgo%2),pow(-1,(fgo//2)%2),pow(-1,(fgo//4)%2)]
    L = sorted(L,key=lambda x:x[0]*FGO[0]+x[1]*FGO[1]+x[2]*FGO[2],reverse=True)
    ans = 0
    for m in L[:W]:
        ans += m[0]*FGO[0] + m[1]*FGO[1] + m[2]*FGO[2]
    if ans > MAX_ANS: MAX_ANS = ans

print(MAX_ANS)
# Original solution
import itertools

[N,M]=list(map(int,input().split()
              )
          )
XYZ=[[int(i) for i in input().split()] for i in range(N)] 
maxabs=0
for indextupl in list(itertools.permutations(list(range(N)), M)):
  maxabs=max(sum([abs(sum([XYZ[i][j] for i in indextupl])) for j in range(3)]),
             maxabs
            )
  
print(maxabs)

# Optimized solution
[N,M]=list(map(int,input().split()
              )
          )
XYZ=[[int(i) for i in input().split()] for i in range(N)] 
maxabs=0

for i in range(8):
  pm=list(map(int,format(i, 'b')))
  pm=[0,]*(3-len(pm))+pm
  pm=[(-1)**item for item in pm]
  
  newXYZ=sorted([[item[0]*pm[0],item[1]*pm[1],item[2]*pm[2]] for item in XYZ],
                key=lambda x:sum(x),
                reverse=True)
  
  maxabs=max(sum([abs(sum([newXYZ[m][j] for m in range(M)]
                             )
                         ) for j in range(3)
                 ]
                ),
             maxabs
            )


print(maxabs)
# Original solution
import itertools


def resolve():
	n,m=list(map(int,input().split()))
	l=[]
	for i in range(n):
		x,y,z=list(map(int,input().split()))
		l.append([x,y,z])
	lst=list(itertools.combinations(l,m))
	ms=0
	for i in lst:
		xx=abs(sum([p[0] for p in i]))
		yy=abs(sum([p[1] for p in i]))
		zz=abs(sum([p[2] for p in i]))
		ms=max(ms,xx+yy+zz)
	print(ms)
resolve()

# Optimized solution
def resolve():
	ans=0
	n,m=list(map(int,input().split()))
	xyz=[list(map(int,input().split())) for i in range(n)]
	t=[(1,1,1),(1,1,-1),(1,-1,1),(1,-1,-1),(-1,1,1),(-1,1,-1),(-1,-1,1),(-1,-1,-1)]
	for k in t:
		l=[k[0]*x+k[1]*y+k[2]*z for x,y,z in xyz ]
		l.sort(reverse=True)
		ans=max(ans,sum(l[:m]))
	print(ans)

resolve()
# Original solution
N,M = list(map(int, input().split()))
xs,ys,zs = [], [], []
"""
sum000 +++
sum001 ++-
sum010 +-+
sum011 +--
sum100 -++
sum101 -+-
sum110 --+
sum111 ---
"""


sum000, sum001, sum010,sum011 = [], [], [], []
sum100, sum101, sum110,sum111 = [], [], [], []
sums = [[] for i in range(8)]
for i in range(N):
    x,y,z = list(map(int, input().split()))
    sums[0].append(x+y+z)
    sums[1].append(x+y-z)
    sums[2].append(x-y+z)
    sums[3].append(x-y-z)
    sums[4].append(-x+y+z)
    sums[5].append(-x+y-z)
    sums[6].append(-x-y+z)
    sums[7].append(-x-y-z)

ans = 0
for s in sums:
    s1 = sorted(s,reverse=False)
    s2 = sorted(s,reverse=False)
    x = max(abs(sum(s1[:M])),abs(sum(s2[:M])))
    ans = max(ans,x)
print(ans)

# Optimized solution
N,M = list(map(int, input().split()))

"""
 xyz
0+++
1++-
2+-+
3+--
4-++
5-+-
6--+
7---

"""
dp = [[0]*N for i in range(8)]


for i in range(N):
    x,y,z = list(map(int, input().split()))
    dp[0][i] = x+y+z
    dp[1][i] = x+y-z
    dp[2][i] = x-y+z
    dp[3][i] = x-y-z
    dp[4][i] = -x+y+z
    dp[5][i] = -x+y-z
    dp[6][i] = -x-y+z
    dp[7][i] = -x-y-z

ans = 0
for i in range(8):
    sums = sorted(dp[i], reverse=True)
    ans = max(sum(sums[:M]),ans)
print(ans)
# Original solution
import itertools
N,M=list(map(int,input().split()))
x=[0 for i in range(N)]
y=[0 for i in range(N)]
z=[0 for i in range(N)]
for i in range(N):
    x[i],y[i],z[i]=list(map(int,input().split()))
w=[[0 for i in range(N)] for i in range(8)]
S=list(itertools.product([-1,1],repeat=3))
def num(inp):
    res=0
    for i in range(3):
        res+=2**i*(inp[i]+1)//2
    return res
for sg in S:
    for i in range(N):
        w[num(sg)][i]=x[i]*sg[0]+y[i]*sg[1]+z[i]*sg[2]
tmp=[0 for i in range(8)]
for sg in S:
    w[num(sg)].sort(reverse=True)
    for i in range(M):
        tmp[num(sg)]+=w[num(sg)][i]
print((max(tmp)))

# Optimized solution
import itertools
N,M=list(map(int,input().split()))
x=[0 for i in range(N)]
y=[0 for i in range(N)]
z=[0 for i in range(N)]
for i in range(N):
    x[i],y[i],z[i]=list(map(int,input().split()))
S=list(itertools.product([-1,1],repeat=3))
def num(seq):
    res=0
    for i in range(3):
        if seq[i]==1:
            res+=2**i
    return res
ans=0
for seq in S:
    A=[seq[0]*x[i]+seq[1]*y[i]+seq[2]*z[i] for i in range(N)]
    A.sort(reverse=True)
    tmp=0
    for i in range(M):
        tmp+=A[i]
    if tmp>ans:
        ans=tmp
print(ans)
# Original solution
from itertools import combinations

def inpl(): return input().split()

n, m = list(map(int, inpl()))
M = []
for i in range(n):
    M.append(list(map(int, inpl())))

cake_sets = combinations(M, m)
ans = -1
for c_set in cake_sets:
    val = 0
    for typ in range(3):#x, y, z
        t_val = 0
        for i in range(m):
            t_val += c_set[i][typ]
        val += abs(t_val)
    ans = max(ans, val)
print(ans)
        

# Optimized solution
N, M = list(map(int, input().split()))
XYZs = [list(map(int, input().split())) for _ in range(N)]

ans = 0
for op in range(1 << 3):
    lst = []
    for xyz in XYZs:
        v = 0
        for j in range(3):
            v += xyz[j] * ((-1) ** ((op >> j) & 1))
        lst.append(v)
    ans = max(ans, sum(sorted(lst, reverse=True)[:M]))

print(ans)
# Original solution
N, M = list(map(int, input().split()))
XYZs = [list(map(int, input().split())) for _ in range(N)]

ans = 0
for op in range(1 << 3):
    lst = []
    for xyz in XYZs:
        v = 0
        for j in range(3):
            v += xyz[j] * ((-1) ** ((op >> j) & 1))
        lst.append(v)
    ans = max(ans, sum(sorted(lst, reverse=True)[:M]))

print(ans)

# Optimized solution
N, M = list(map(int, input().split()))
XYZs = [tuple(map(int, input().split())) for _ in range(N)]

ans = 0
for i in range(1 << 3):
    signs = []
    for j in range(3):
        if i >> j & 1:
            signs.append(1)
        else:
            signs.append(-1)
    t = []
    for xyz in XYZs:
        x, y, z = xyz
        t.append(x * signs[0] + y * signs[1] + z * signs[2])
    ans = max(ans, sum(sorted(t, reverse=True)[:M]))
print(ans)
# Original solution
import itertools
n = list(map(int, input().split()))
a = [list(map(int, input().split())) for _ in range(n[0])]

c = n[0]
d = n[1]

e = [0+i for i in range(c)]
f = list(itertools.combinations(e, d))

ans = []
for i in f:
    x, y, z = (0, 0, 0)
    for j in i:
        g = a[j]
        x, y, z = (x + g[0], y + g[1], z + g[2])
    ans.append(sum([abs(x), abs(y), abs(z)]))

print((max(ans)))

# Optimized solution
n = list(map(int, input().split()))
a = [list(map(int, input().split())) for _ in range(n[0])]

c = n[0]
d = n[1]
e = [1, -1]
f = []
for i in e:
    for j in e:
        for k in e:
         f.append([i,j,k])

ans = []
for i in f:
    g = []
    for j in a:
        g.append(sum([j[k]*i[k] for k in range(3)]))
    g.sort()
    g.reverse()
    ans.append(sum(g[:d]))

print((max(ans)))
# Original solution
n,m = list(map(int,input().split()))
bit = [bin(i)[2:].zfill(3) for i in range(2**m)]
ans = -int(1e10)
cake = []
for i in range(n):
    a,b,c = list(map(int,input().split()))
    cake.append((a,b,c))
for i in range(2**m):
    u = []
    cond = bit[i]
    for j in range(n):
        cnt = 0
        for k in range(3):
            if cond[k] == "0":
                cnt += cake[j][k]
            else:
                cnt -= cake[j][k]
        u.append(cnt)
    u.sort(reverse=True)
    ans = max(ans, sum(u[:m]))
print(ans)




# Optimized solution
n,m = list(map(int,input().split()))
bit = [bin(i)[2:].zfill(3) for i in range(2**3)]
ans = -int(1e10)
cake = []
for i in range(n):
    a,b,c = list(map(int,input().split()))
    cake.append((a,b,c))
for i in range(2**3):
    u = []
    cond = bit[i]
    for j in range(n):
        cnt = 0
        for k in range(3):
            if cond[k] == "0":
                cnt += cake[j][k]
            else:
                cnt -= cake[j][k]
        u.append(cnt)
    u.sort(reverse=True)
    ans = max(ans, sum(u[:m]))
print(ans)
# Original solution
from itertools import product

INF = 10**18

N, M = list(map(int, input().split()))
XYZ = [list(map(int, input().split())) for _ in range(N)]

# i番目までのケーキの内、j個食べた時のpointの最大値
# (ただしpointの計算時には、8通りのmaskを乗算する)

def mul_mask(l, mask):
    return [a*b for a, b in zip(l, mask)]

dp_std = [[-INF for _ in range(M+1)] for _ in range(N+1)]
for i in range(N+1):
    dp_std[i][0] = 0
# print(dp_std)

max_p = -INF
for mask in product([-1, 1], repeat=3):
    dp = dp_std.copy()
    for i in range(1, N+1):
        score = sum(mul_mask(XYZ[i-1], mask))
        for j in range(1, M+1):
            # print(i, j, XYZ[i-1], mask)
            dp[i][j] = max(
                dp[i-1][j],
                dp[i-1][j-1] + score
            )
            # for dp_i in dp:
            #     print(dp_i)
    p = dp[N][M]
    max_p = max(max_p, p)
    # print(p)

print(max_p)

# Optimized solution
# from itertools import product

INF = 10**18

N, M = list(map(int, input().split()))
XYZ = [list(map(int, input().split())) for _ in range(N)]

# i番目までのケーキの内、j個食べた時のpointの最大値
# (ただしpointの計算時には、8通りのmaskを乗算する)

def mul_mask(l, mask):
    p = []
    for i, m in enumerate(l):
        p.append(m*mask[i])
        # print(p)
    return p
    # return [a*b for a, b in zip(l, mask)]

dp_std = [[-INF for _ in range(M+1)] for _ in range(N+1)]
for i in range(N+1):
    dp_std[i][0] = 0
# print(dp_std)

max_p = -INF
# masks = product([-1, 1], repeat=3)
masks = []
for i in [1, -1]:
    for j in [1, -1]:
        for k in [1, -1]:
            masks.append([i, j, k])
# print(masks)
for mask in masks:
    dp = dp_std[:]
    for i in range(1, N+1):
        score = sum(mul_mask(XYZ[i-1], mask))
        for j in range(1, M+1):
            # print(i, j, XYZ[i-1], mask)
            dp[i][j] = max(
                dp[i-1][j],
                dp[i-1][j-1] + score
            )
            # for dp_i in dp:
            #     print(dp_i)
    p = dp[N][M]
    max_p = max(max_p, p)
    # print(p)

print(max_p)
# Original solution
import itertools

def main():
    # 8通りの方法を調べて最大値　-,-,- から +,+,+
    N, M = list(map(int, input().split()))

    data = [[0] * 8 for _ in range(M+1)]
    p = list(itertools.product([-1, 1], repeat=3))

    for i in range(N):
        #print(data)
        x, y, z = list(map(int, input().split()))
        for k in range(M-1, -1, -1):
            for j in range(8):
                tmp = x * p[j][0] + y * p[j][1] + z * p[j][2]
                if data[k+1][j] < data[k][j] + tmp:
                    data[k+1][j] = data[k][j] + tmp
    answer = 0
    for d in data[M]:
        if abs(d) > answer:
            answer = abs(d)
    print(answer)

if __name__ == '__main__':
    main()

# Optimized solution
import itertools

def main():
    # 8通りの方法を調べて最大値　-,-,- から +,+,+
    N, M = list(map(int, input().split()))

    data = [[] for i in range(8)]
    p = list(itertools.product([-1, 1], repeat=3))

    for i in range(N):
        #print(data)
        x, y, z = list(map(int, input().split()))
        for j in range(8):
            tmp = x * p[j][0] + y * p[j][1] + z * p[j][2]
            data[j].append(tmp)
               
    answer = 0
    for d in data:
        d.sort(reverse=True)
        s = sum(d[:M])
        if abs(s) > answer:
            answer = abs(s)
    print(answer)

if __name__ == '__main__':
    main()