Problem p03326 (python)

Fixed submission:
N,M = map(int,input().split())

ppp = []
mpp = []
pmp = []
ppm = []
pmm = []
mpm = []
mmp = []
mmm = []

for i in range(N):
    x,y,z = map(int,input().split())
    ppp.append(x+y+z)
    mpp.append(-x+y+z)
    pmp.append(x-y+z)
    ppm.append(x+y-z)
    pmm.append(x-y-z)
    mpm.append(-x+y-z)
    mmp.append(-x-y+z)
    mmm.append(-x-y-z)

ans = []
ppp.sort(reverse=True)
ans.append(sum(ppp[:M]))
mpp.sort(reverse=True)
ans.append(sum(mpp[:M]))
pmp.sort(reverse=True)
ans.append(sum(pmp[:M]))
ppm.sort(reverse=True)
ans.append(sum(ppm[:M]))
pmm.sort(reverse=True)
ans.append(sum(pmm[:M]))
mpm.sort(reverse=True)
ans.append(sum(mpm[:M]))
mmp.sort(reverse=True)
ans.append(sum(mmp[:M]))
mmm.sort(reverse=True)
ans.append(sum(mmm[:M]))

print(max(ans))

Buggy submission (verdict: Runtime Error):
N,M = map(int,input().split())
def listsum(a,n):
    res1 = 0
    res2 = 0
    res3 = 0
    tmp1 = []
    tmp2 = []
    tmp3 = []
    if n==1:##xについてのソート.0-M(nで指定)
        for i in range(0,M):
            res1+=a[i][0]
            res2+=a[i][1]
            res3+=a[i][2]
        i = M-1
        tmp2.append(abs(res2))
        tmp3.append(abs(res3))
        while a[i][0]==a[i+1][0]:
            ##print("unti")
            tmpt3 = res3 - a[M-1][2] + a[i+1][2]
            tmpt2 = res2 - a[M-1][1] + a[i+1][1]
            tmp2.append(abs(tmpt2))
            tmp3.append(abs(tmpt3))
            i += 1
            if i==N-1:
                break
        return abs(res1)+max(tmp2)+max(tmp3)
    if n==2:##xについてのソート.M-N(nで指定)
        for i in range(N-M,N):
            res1+=a[i][0]
            res2+=a[i][1]
            res3+=a[i][2]
        i = M-1
        tmp2.append(abs(res2))
        tmp3.append(abs(res3))
        while a[i][0]==a[i-1][0]:
            tmpt3 = res3 - a[M-1][2] + a[i-1][2]
            tmpt2 = res2 - a[M-1][1] + a[i-1][1]
            tmp2.append(abs(tmpt2))
            tmp3.append(abs(tmpt3))
            i -= 1
            if i == 0:
                break
        return abs(res1)+max(tmp2)+max(tmp3)
    if n==3:##yについてのソート.0-M(nで指定)
        for i in range(0,M):
            res1+=a[i][0]
            res2+=a[i][1]
            res3+=a[i][2]
        i = M-1
        tmp1.append(abs(res1))
        tmp3.append(abs(res3))
        while a[i][1]==a[i+1][1]:
            tmpt3 = res3 - a[M-1][2] + a[i+1][2]
            tmpt1 = res1 - a[M-1][0] + a[i+1][0]
            tmp1.append(abs(tmpt1))
            tmp3.append(abs(tmpt3))
            i += 1
            if i == N-1:
                break
        return max(tmp1)+abs(res2)+max(tmp3)
    if n==4:##yについてのソート.M-N(nで指定)
        for i in range(N-M,N):
            res1+=a[i][0]
            res2+=a[i][1]
            res3+=a[i][2]
        i = M-1
        tmp1.append(abs(res1))
        tmp3.append(abs(res3))
        while a[i][1]==a[i-1][1]:
            tmpt3 = res3 - a[M-1][2] + a[i-1][2]
            tmpt1 = res1 - a[M-1][0] + a[i-1][0]
            tmp1.append(abs(tmpt1))
            tmp3.append(abs(tmpt3))
            i -= 1
            if i==0:
                break
        return max(tmp1)+abs(res2)+max(tmp3)
    if n==5:##zについてのソート.0-M(nで指定)
        for i in range(0,M):
            res1+=a[i][0]
            res2+=a[i][1]
            res3+=a[i][2]
        i = M-1
        tmp1.append(abs(res1))
        tmp2.append(abs(res2))
        while a[i][2]==a[i+1][2]:
            tmpt2 = res2 - a[M-1][1] + a[i+1][1]
            tmpt1 = res1 - a[M-1][0] + a[i+1][0]
            tmp1.append(abs(tmpt1))
            tmp2.append(abs(tmpt2))
            i += 1
            if i==N-1:
                break
        return max(tmp1)+max(tmp2)+abs(res3)
    if n==6:##zについてのソート.M-N(nで指定)
        for i in range(N-M,N):
            res1+=a[i][0]
            res2+=a[i][1]
            res3+=a[i][2]
        i = M-1
        tmp1.append(abs(res1))
        tmp2.append(abs(res2))
        while a[i][2]==a[i-1][2]:
            tmpt2 = res2 - a[M-1][1] + a[i-1][1]
            tmpt1 = res1 - a[M-1][0] + a[i-1][0]
            tmp1.append(abs(tmpt1))
            tmp2.append(abs(tmpt2))
            i -= 1
            if i==0:
                break
        return max(tmp1)+max(tmp2)+abs(res3)


xyz = []
yzx = []
zxy = []

for i in range(N):
    x,y,z = map(int,input().split())
    xyz.append([x,y,z])
    yzx.append([y,z,x])
    zxy.append([z,x,y])

xyz.sort()
yzx.sort()
zxy.sort()

ans = []

ans.append(listsum(xyz,1))
ans.append(listsum(xyz,2))
ans.append(listsum(yzx,3))
ans.append(listsum(yzx,4))
ans.append(listsum(zxy,5))
ans.append(listsum(zxy,6))

print(max(ans))