Problem p03326 (python)

Fixed submission:
import sys
sys.setrecursionlimit(1000000)
#def input():
#    return sys.stdin.readline()[:-1]

'''
n=int(input())
for i in range(n):
    a[i]=int(input())
    a[i],b[i]=map(int,input().split())
a=[int(x) for x in input().split()]
n,m=map(int,input.split())
from operator import itemgetter
a = [(1, "c", 1), (1, "b", 3), (2, "a", 0), (1, "a", 2)]
print(sorted(a))  # 0 番目の要素でソート、先頭の要素が同じなら 1 番目以降の要素も見る
print(sorted(a, key=itemgetter(0)))  # 0 番目の要素だけでソート
print(sorted(a, key=itemgetter(0, 2)))  # 0 番目と 2 番目の要素でソート
print(sorted(a, key=lambda x: x[0] * x[2]))  # 0 番目の要素 * 2 番目の要素でソート
print(sorted(a, reverse=True))  # 降順にソート
a.sort()  # 破壊的にソート、sorted() よりも高速
try: # エラーキャッチ list index out of range
    for i in range():
        k=b[i]
except IndexError as e:
    print(i)
'''

test_data1 = '''\
2 3 -10
1 2 3
3 2 1
1 2 2
'''

test_data2 = '''\
5 3
1 -2 3
-4 5 -6
7 -8 -9
-10 11 -12
13 -14 15
'''

test_data3 = '''\
10 5
10 -80 21
23 8 38
-94 28 11
-26 -2 18
-69 72 79
-26 -86 -54
-72 -50 59
21 65 -32
40 -94 87
-62 18 82
'''
td_num=3

def GetTestData(index):
    if index==1:
        return test_data1
    if index==2:
        return test_data2
    if index==3:
        return test_data3
    
if False:
    with open("../test.txt", mode='w') as f:
        f.write(GetTestData(td_num))
    with open("../test.txt") as f:
        # Start Input code ---------------------------------------
        n,m=map(int,f.readline().split())
        a=[[0 for i in range(n)] for j in range(8)]
        for i in range(n):
            x,y,z=map(int,f.readline().split())
            t=0
            for p in range(2):
                for q in range(2):
                    for r in range(2):
                        a[t][i]=x*(1-2*p)+y*(1-2*q)+z*(1-2*r)
                        t+=1

        # End Input code ---------------------------------------
else:
    # Start Input code ---------------------------------------
    n,m=map(int,input().split())
    a=[[0 for i in range(n)] for j in range(8)]
    for i in range(n):
        x,y,z=map(int,input().split())
        t=0
        for p in range(2):
            for q in range(2):
                for r in range(2):
                    a[t][i]=x*(1-2*p)+y*(1-2*q)+z*(1-2*r)
                    t+=1
    # End Input code ---------------------------------------

ans=0
for i in range(8):
    b=a[i]
    b.sort(reverse=True)
    ans=max(ans,sum(b[:m]))
print(ans)
#print('n,m=',n,m,' a=',a,' b=',b,' c=',c)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(1000000)
#def input():
#    return sys.stdin.readline()[:-1]

'''
n=int(input())
for i in range(n):
    a[i]=int(input())
    a[i],b[i]=map(int,input().split())
a=[int(x) for x in input().split()]
n,m=map(int,input.split())
from operator import itemgetter
a = [(1, "c", 1), (1, "b", 3), (2, "a", 0), (1, "a", 2)]
print(sorted(a))  # 0 番目の要素でソート、先頭の要素が同じなら 1 番目以降の要素も見る
print(sorted(a, key=itemgetter(0)))  # 0 番目の要素だけでソート
print(sorted(a, key=itemgetter(0, 2)))  # 0 番目と 2 番目の要素でソート
print(sorted(a, key=lambda x: x[0] * x[2]))  # 0 番目の要素 * 2 番目の要素でソート
print(sorted(a, reverse=True))  # 降順にソート
a.sort()  # 破壊的にソート、sorted() よりも高速
try: # エラーキャッチ list index out of range
    for i in range():
        k=b[i]
except IndexError as e:
    print(i)
'''

test_data1 = '''\
2 3 -10
1 2 3
3 2 1
1 2 2
'''

test_data2 = '''\
5 3
1 -2 3
-4 5 -6
7 -8 -9
-10 11 -12
13 -14 15
'''

test_data3 = '''\
10 5
10 -80 21
23 8 38
-94 28 11
-26 -2 18
-69 72 79
-26 -86 -54
-72 -50 59
21 65 -32
40 -94 87
-62 18 82
'''
td_num=3

def GetTestData(index):
    if index==1:
        return test_data1
    if index==2:
        return test_data2
    if index==3:
        return test_data3
    
if False:
    with open("../test.txt", mode='w') as f:
        f.write(GetTestData(td_num))
    with open("../test.txt") as f:
        # Start Input code ---------------------------------------
        n,m=map(int,f.readline().split())
        a=[[0 for i in range(n)] for j in range(8)]
        for i in range(n):
            x,y,z=map(int,f.readline().split())
            t=0
            for p in range(2):
                for q in range(2):
                    for r in range(2):
                        a[t][i]=x*(1-2*p)+y*(1-2*q)+z*(1-2*r)
                        t+=1

        # End Input code ---------------------------------------
else:
    # Start Input code ---------------------------------------
    n,m=map(int,input.split())
    a=[[0 for i in range(n)] for j in range(8)]
    for i in range(n):
        x,y,z=map(int,input.split())
        t=0
        for p in range(2):
            for q in range(2):
                for r in range(2):
                    a[t][i]=x*(1-2*p)+y*(1-2*q)+z*(1-2*r)
                    t+=1
    # End Input code ---------------------------------------

ans=0
for i in range(8):
    b=a[i]
    b.sort(reverse=True)
    ans=max(ans,sum(b[:m]))
print(ans)
#print('n,m=',n,m,' a=',a,' b=',b,' c=',c)