Problem:


Matrix-like Computation

Your task is to develop a tiny little part of spreadsheet software.

Write a program which adds up columns and rows of given table as shown in the following figure:

Input

The input consists of several datasets. Each dataset consists of:

n (the size of row and column of the given table)
1st row of the table
2nd row of the table
 :
 :
nth row of the table

The input ends with a line consisting of a single 0.

Output

For each dataset, print the table with sums of rows and columns. Each item of the table should be aligned to the right with a margin for five digits. Please see the sample output for details.

Sample Input

4
52 96 15 20
86 22 35 45
45 78 54 36
16 86 74 55
4
52 96 15 20
86 22 35 45
45 78 54 36
16 86 74 55
0

Output for the Sample Input

   52   96   15   20  183
   86   22   35   45  188
   45   78   54   36  213
   16   86   74   55  231
  199  282  178  156  815
   52   96   15   20  183
   86   22   35   45  188
   45   78   54   36  213
   16   86   74   55  231
  199  282  178  156  815



Sample input:
4
52 96 15 20
86 22 35 45
45 78 54 36
16 86 74 55
4
52 96 15 20
86 22 35 45
45 78 54 36
16 86 74 55
0


Sample output:
   52   96   15   20  183
   86   22   35   45  188
   45   78   54   36  213
   16   86   74   55  231
  199  282  178  156  815
   52   96   15   20  183
   86   22   35   45  188
   45   78   54   36  213
   16   86   74   55  231
  199  282  178  156  815

Submission status: Accepted

Solution code:
if __name__ == '__main__':

	while True:

		n = int(input())
		
		if n == 0:
			break

		A = [[0 for _ in range(n+1)]for _ in range(n)]
		B = [0 for _ in range(n+1)]

		for i in range(n):
			tmp = list(map(int,input().split()))
			
			for j,k in enumerate(tmp):
				A[i][j] += k
				B[j] += k

			A[i][n] = sum(A[i])
			B[n] = sum(B[:n])
		
		for m in A:
			tmp2 = ""
			for g in m:
				tmp2 += '{:>5}'.format(str(g))
			print(tmp2)
		tmp3 = ""
		for y in B:
			tmp3 += '{:>5}'.format(str(y))
		print(tmp3)

Submission status: Accepted

Solution code:
while 1:
    n=int(input())
    if n==0:break
    ans=[0]*(n+1)**2
    for i in range(0,n):
        keep=list(map(int,input().split()))
        s_keep=0
        for j in range(n):
            ans[i*(n+1)+j]=keep[j]
            s_keep+=keep[j]
        ans[(i+1)*(n+1)-1]=s_keep
    for i in range(n+1):
        s_keep=0
        for j in range(n+1):
            s_keep+=ans[i+j*(n+1)]
        ans[i+n*(n+1)]=s_keep
    for i in range(n+1):
        for j in range(n+1):
            print(f"{ans[i*(n+1)+j]:>5}",end="")
        print()

Submission status: Accepted

Solution code:
while 1:
    n = int(input())
    if n == 0:
        break

    mat = [[0 for i in range(n)] for j in range(n)]
    for i in range(n):
        mat[i] = list(map(int, input().split()))

    for i in range(n):
        tmp = 0
        for j in range(n):
            tmp += mat[i][j]
        mat[i].append(tmp)

    col = [0] * n
    for i in range(n):
        tmp = 0
        for j in range(n):
            tmp += mat[j][i]
        col[i] = tmp

    ans = sum(col)
    col.append(ans)

    for i in range(n):
        print(''.join(str(ans).rjust(5) for ans in mat[i]))

    print(''.join(str(ans).rjust(5) for ans in col))

Submission status: Accepted

Solution code:
while True:
    n = int(input())
    if n == 0:
        break
    ans = []
    sum_c = [0 for i in range(n + 1)]
    for i in range(n):
        r = list(map(int, input().split()))
        r.append(sum(r))
        ans.append(r)
        for j in range(n + 1):
            sum_c[j] += r[j]
    ans.append(sum_c)
    for i in range(n + 1):
        [print("{:>5}".format(str(ans[i][j])),end="") for j in range(n + 1)]
        print()

Submission status: Accepted

Solution code:
while True:
    n = int(input())
    if n == 0: break
    table = []
    for _ in range(n):
        num = list(map(int, input().split()))
        num.append(sum(num))
        table.append(num)
    vertical = []
    for t in zip(*table):
        vertical.append(sum(t))
    table.append(vertical)
    for tab in table:
        for t in tab:
            print('{:>5d}'.format(t), end='')
        print()

Submission status: Accepted

Solution code:
while True:
    n = int(input())
    if n == 0:
        break
    mat = []
    for i in range(n):
        mat.append(list(map(int, input().split())))
        mat[i].append(sum(mat[i]))
    mat.append([0]*(n+1))
    for i in range(n+1):
        for j in range(n):
            mat[n][i]+=mat[j][i]
    for i in mat:
        for j in range(n+1):
            print("%5d" % (i[j]), end="")
        print()

Submission status: Accepted

Solution code:
n = int(input())
while (n != 0):
    nlist = []
    for i in range(n):
        nn = list(map(int, input().split()))
        nn.append(sum(nn))
        nlist.append(nn)
    lastn = []
    for i in range(n+1):
        last = 0
        for k in range(n):
            last += nlist[k][i]
        lastn.append(last)
    nlist.append(lastn)
    for i in range(n+1):
        for k in range(n+1):
            nnn = nlist[i][k]
            l = len(str(nnn))
            cn = 5 - l
            if (k == n):
                print(' '*cn + '{}'.format(nnn))
            else:
                print(' '*cn + '{}'.format(nnn), end = '')
    
    n = int(input())

Submission status: Accepted

Solution code:
while(True):
    n = int(input())
    if n == 0:
         break
    arr = []
    for i in range(n):
        arr.append(list(map(int, input().split())))
    arr = [ l+[sum(l)] for l in arr]
    brr = [0]*(n+1)
    for i in range(n):
        for j in range(n+1):
            brr[j] += arr[i][j]
    crr = arr+[brr]
    for c in crr:
        for d in c:
            print("%5d"%d,end="")
        print()

Submission status: Runtime Error

Solution code:
data=[]
while True:
    n = int(input())
    if n==0:
        break
    for i in range(n):
        spam=list(map(int,input().split()))
        spam.append(sum(spam))
        data.append(spam)
        for j in spam:
            print(str(j)+' ',end='')
        print('')
    for i in range(len(data)+1):
        print(str(sum(x[i] for x in data))+' ',end='')
    print('')

Submission status: Runtime Error

Solution code:
data=[]
while True:
    try:
        n = int(input())
    except:
        break
    if n==0:
        break
    for i in range(n):
        spam=list(map(int,input().split()))
        spam.append(sum(spam))
        data.append(spam)
        for j in spam:
            print(str(j)+' ',end='')
        print('')
    for i in range(len(data)+1):
        print(str(sum(x[i] for x in data))+' ',end='')
    print('')

Submission status: Runtime Error

Solution code:
data=[]
while True:
    try:
        n = int(input())
    except:
        break
    if n==0:
        break
    for i in range(n):
        spam=list(map(int,input().split()))
        spam.append(sum(spam))
        data.append(spam)
        for j in spam:
            print('{:5}'.format(j),end='')
        print('')
    for i in range(len(data)+1):
        print('{:5}'.format(sum(x[i] for x in data)),end='')
    print('')

Submission status: Runtime Error

Solution code:
data=[]
while True:
    n = int(input())
    if n==0:
        break
    for i in range(n):
        spam=list(map(int,input().split()))
        spam.append(sum(spam))
        data.append(spam)
    spam=[]
    for i in range(n+1):
        spam.append(sum(x[i] for x in data))
    data.append(spam)
    for i in range(n+1):
        for j in range(n+1):
            print('{:5}'.format(data[i][j]),end='')
        print('')

Submission status: Runtime Error

Solution code:
while True:
	n = input()
	x = []
	y = [0 for i in range(n+1)]
	for i in range(n):
		a = map(int, raw_input().split())
		a.append(sum(a))
		x.append(a)
		for j in range(n+1):
			y[j] += a[j]
	x.append(y)
	for a in x:
		print "".join([str(e).rjust(5) for e in a])

Submission status: Runtime Error

Solution code:
while True:
    num = input()
    if num == 0: break

    sht = []
    for i in range(num):
        sht.append(map(int, raw_input().split()))
        sht[i].append(sum(sht[i]))
        print " ".join(map(str, sht[i]))

    row = []
    for i in range(num + 1):
        sum = 0
        for j in range(num):
            sum += sht[j][i]
        row.append(sum)
    print " ".join(map(str, row))

Submission status: Runtime Error

Solution code:
while 1:
	temp = input()
	if (temp==0):
		break
	N = temp
	a = []
	for i in range(N):
	    a.append(map(int, raw_input().split())+[0])
	a.append([0 for i in range(0,temp+1)])	 
	for i in range(0,temp):#????????¢??????
		for j in range(0,temp):#????????¢??????
			a[i][temp] += a[i][j]
	for k in range(0,temp+1):#????????¢??????
		for l in range(0,temp):#????????¢??????
			a[temp][k] += a[l][k]
	for m in range(temp+1):
		print " ".join(map(str,a[m]))

Submission status: Runtime Error

Solution code:
import sys
while 1:
	temp = input()
	if (temp==0):
		break
	N = temp
	a = []
	for i in range(N):
	    a.append(map(int, raw_input().split())+[0])
	a.append([0 for i in range(0,temp+1)])	 
	for i in range(0,temp):#行に関して
		for j in range(0,temp):#列に関して
			a[i][temp] += a[i][j]
	for k in range(0,temp+1):#列に関して
		for l in range(0,temp):#行に関して
			a[temp][k] += a[l][k]
	for m in range(0,temp+1):
		for n in range(0,temp+1):
			sys.stdout.write(str(a[m][n]).rjust(5))
		print ""

Submission status: Runtime Error

Solution code:
    import sys
    while 1:
    temp = input()
    if (temp==0):
    break
    N = temp
    a = []
    for i in range(N):
    a.append(map(int, raw_input().split())+[0])
    a.append([0 for i in range(0,temp+1)])	
    for i in range(0,temp):#行に関して
    for j in range(0,temp):#列に関して
    a[i][temp] += a[i][j]
    for k in range(0,temp+1):#列に関して
    for l in range(0,temp):#行に関して
    a[temp][k] += a[l][k]
    for m in range(0,temp+1):
    for n in range(0,temp+1):
    sys.stdout.write(str(a[m][n]).rjust(5))
    print ""

Submission status: Runtime Error

Solution code:
    import sys
    while 1:
    temp = input()
    if (temp==0):
    break
    N = temp
    a = []
    for i in range(N):
    a.append(map(int, raw_input().split())+[0])
    a.append([0 for i in range(0,temp+1)])	
    for i in range(0,temp):#行に関して
    for j in range(0,temp):#列に関して
    a[i][temp] += a[i][j]
    for k in range(0,temp+1):#列に関して
    for l in range(0,temp):#行に関して
    a[temp][k] += a[l][k]
    for m in range(0,temp+1):
    for n in range(0,temp+1):
    sys.stdout.write(str(a[m][n]).rjust(5))
    print ""
pass

Submission status: Runtime Error

Solution code:
    import sys
    while 1:
    temp = input()
    if (temp==0):
    break
    N = temp
    a = []
    for i in range(N):
    a.append(map(int, raw_input().split())+[0])
    a.append([0 for i in range(0,temp+1)])	
    for i in range(0,temp):#行に関して
    for j in range(0,temp):#列に関して
    a[i][temp] += a[i][j]
    for k in range(0,temp+1):#列に関して
    for l in range(0,temp):#行に関して
    a[temp][k] += a[l][k]
    for m in range(0,temp+1):
    for n in range(0,temp+1):
    sys.stdout.write(str(a[m][n]).rjust(5))
    print ""
print ""

Submission status: Runtime Error

Solution code:
import sys
while 1:
	try:
		temp = input()
		if (temp==0):
			break
		N = temp
		a = []
		for i in range(N):
			a.append(map(int, raw_input().split())+[0])
		a.append([0 for i in range(0,temp+1)])	 
		for i in range(0,temp):#????????¢??????
			for j in range(0,temp):#????????¢??????
				a[i][temp] += a[i][j]
		for k in range(0,temp+1):#????????¢??????
			for l in range(0,temp):#????????¢??????
				a[temp][k] += a[l][k]
		for m in range(0,temp+1):
			for n in range(0,temp+1):
				sys.stdout.write(str(a[m][n]).rjust(5))
			print ""
	except(RuntimeError):
		pass

Submission status: Runtime Error

Solution code:
    import sys
    while 1:
    try:
    temp = input()
    if (temp==0):
    break
    N = temp
    a = []
    for i in range(N):
    a.append(map(int, raw_input().split())+[0])
    a.append([0 for i in range(0,temp+1)])	
    for i in range(0,temp):#行に関して
    for j in range(0,temp):#列に関して
    a[i][temp] += a[i][j]
    for k in range(0,temp+1):#列に関して
    for l in range(0,temp):#行に関して
    a[temp][k] += a[l][k]
    for m in range(0,temp+1):
    for n in range(0,temp+1):
    sys.stdout.write(str(a[m][n]).rjust(5))
    print ""
    except(RuntimeError):
    pass

Submission status: Runtime Error

Solution code:
import sys
while 1:
	try:
		temp = input()
		if (temp==0):
			break
		N = temp
		a = []
		for i in range(N):
			a.append(map(int, raw_input().split())+[0])
		a.append([0 for i in range(0,temp+1)])	 
		for i in range(0,temp):#行に関して
			for j in range(0,temp):#列に関して
				a[i][temp] += a[i][j]
		for k in range(0,temp+1):#列に関して
			for l in range(0,temp):#行に関して
				a[temp][k] += a[l][k]
		for m in range(0,temp+1):
			for n in range(0,temp+1):
				sys.stdout.write(str(a[m][n]).rjust(5))
			print ""
	except(RuntimeError):
		pass

Submission status: Runtime Error

Solution code:
import sys
while 1:
    try:
        temp = input()
        if (temp==0):
            break
            N = temp
            a = []
            for i in range(N):
                a.append(map(int, raw_input().split())+[0])
            a.append([0 for i in range(0,temp+1)])	 
            for i in range(0,temp):#行に関して
                for j in range(0,temp):#列に関して
                    a[i][temp] += a[i][j]
            for k in range(0,temp+1):#列に関して
                for l in range(0,temp):#行に関して
                    a[temp][k] += a[l][k]
            for m in range(0,temp+1):
                for n in range(0,temp+1):
                    sys.stdout.write(str(a[m][n]).rjust(5))
                print ""
    except(RuntimeError):
        pass

Submission status: Runtime Error

Solution code:
import sys
while 1:
    try:
        temp = input()
        if (temp==0):
            break
        N = temp
        a = []
        for i in range(N):
            a.append(map(int, raw_input().split())+[0])
        a.append([0 for i in range(0,temp+1)])	 
        for i in range(0,temp):#行に関して
            for j in range(0,temp):#列に関して
                a[i][temp] += a[i][j]
        for k in range(0,temp+1):#列に関して
            for l in range(0,temp):#行に関して
                a[temp][k] += a[l][k]
        for m in range(0,temp+1):
            for n in range(0,temp+1):
                sys.stdout.write(str(a[m][n]).rjust(5))
            print ""
    except(RuntimeError):
        pass

Submission status: Runtime Error

Solution code:
import sys
while 1:
    try:
        temp = input()
        if (temp==0):
            break
        N = temp
        a = []
        for i in range(N):
            a.append(map(int, raw_input().split())+[0])
        a.append([0 for i in range(0,temp+1)])	 
        for i in range(0,temp):#????????¢??????
            for j in range(0,temp):#????????¢??????
                a[i][temp] += a[i][j]
        for k in range(0,temp+1):#????????¢??????
            for l in range(0,temp):#????????¢??????
                a[temp][k] += a[l][k]
        for m in range(0,temp+1):
            for n in range(0,temp+1):
                sys.stdout.write(str(a[m][n]).rjust(5))
            print ""
    except(RuntimeError):
        pass

Submission status: Runtime Error

Solution code:
import sys
try:
	while 1:
		temp = input()
		if (temp==0):
			break
		N = temp
		a = []
		for i in range(N):
			a.append(map(int, raw_input().split())+[0])
		a.append([0 for i in range(0,temp+1)])	 
		for i in range(0,temp):#????????¢??????
			for j in range(0,temp):#????????¢??????
				a[i][temp] += a[i][j]
		for k in range(0,temp+1):#????????¢??????
			for l in range(0,temp):#????????¢??????
				a[temp][k] += a[l][k]
		for m in range(0,temp+1):
			for n in range(0,temp+1):
				sys.stdout.write(str(a[m][n]).rjust(5))
			print ""
except(RuntimeError):
	pass

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3

import sys

input_lines = [list(map(int, i.split(' '))) for i in sys.stdin.readlines()]

#print(input_lines)
cnt = 0
lastline = []
output = []
for i in input_lines:
	if len(i) == 1:
		cnt = i[0]
		lastline = [0 for i in range(cnt+1)]

	else:
		i.append(sum(i))
		for k in range(len(i)):
			lastline[k] += i[k]
		cnt -= 1
		output.append(i)

	if cnt == 0:
		output.append(lastline)
		
s = ""
for i in output[:-1]:
	for j in i:
		s += "{:>5}".format(j)
	s+='\n'

print(s[:-1])

Submission status: Runtime Error

Solution code:
n = int(raw_input())
while n != 0:
    input_list = []
    for x in range(n):
        input_list.append(map(int, raw_input().split(' ')))
    last_list_row = []
    for x in range(n):
        input_list[x].append(sum(input_list[x]))
        print ''.join(map(lambda n:"{:>5}".format(n), input_list))
        last_list.append(input_list[x][-1])

    input_len = len(input_list[0])
    column_list = []
    for x in range(input_len):
        tmp = 0
        for y in range(n):
            tmp += input_list[y][x]
        column_list.append(tmp)
    print ''.join(map(lambda n:"{:>5}".format(n), column_list))
    n = int(raw_input())

Submission status: Runtime Error

Solution code:
import functools

n = input()
while(n):
    list = []
    bottom_record = [0]*(n+1)

    for i in range(0, n):
        l = map(int, raw_input().split())
        l.append(functools.reduce(lambda x, y: x + y, l))
        bottom_record = [x + y for(x,y) in zip(bottom_record, l)]
        print(' '.join(map(str, l))) 
    print(' '.join(map(str, bottom_record)))
    n = input()

Submission status: Runtime Error

Solution code:
import sys

l = []
index = 1
for input in sys.stdin:
     l.append(input.split())
a = 0
for i in range(0,len(l),int(l[a][0])+1):
    res = []
    for j in range(0,int(l[i][0])+1):
        res.append(0)
    for j in range(i+1,int(l[i][0])+index):
        temp = 0
        print ' ',
        for k in range(0,int(l[i][0])):
            res[k] += int(l[j][k])
            temp += int(l[j][k])
            print int(l[j][k]),
        res[int(l[i][0])] += temp
        print temp
    print ' ',
    for j in range(0,int(l[i][0])+1):
        print res[j],
    if(i + int(l[a][0])+1 < len(l)-1):
        print
    index += int(l[i][0])+1
    a = int(l[a][0])+1

Submission status: Runtime Error

Solution code:
import numpy

Submission status: Runtime Error

Solution code:
while 1:
 n=int(input())
 if n:
  a=[list(map(int,input().split()))for _ in[0]*n]
  for r in a:
   for s in r:print(f"{s:5}",end='')
   print(f"{sum(r):5}")
  for c in zip(*a):print(f"{sum(c):5}",end='')
  print(f"{sum([sum(r)for r in a]):5}")

Submission status: Runtime Error

Solution code:
while 1:
 n=int(input())
 if n==0:break;
 a=[]
 for i in range(n):
  a[i]=[list(map(int,input().split()))]
  a[i]+=[sum(a[i])]
  for s in a[i]:print("%5d"%s,end='')
  print()
 for c in zip(*a):print("%5d"%sum(c),end='')
 print()

Submission status: Runtime Error

Solution code:
while True:
  count = int(input())
  if count == 0: break 
  thing,money=[],[]
  for i in range(count):
    hoge = map(str,raw_input().split())
    t = int(hoge[1])
    thing.append(hoge[0])
    money.append(t)
  count = int(input())
  if count == 0:
    hoge = raw_input()
    if hoge not in thing:
      print 0
      continue
    t = thing.index(hoge)
    print money[t]
  else:
    for i in range(count):
      hoge = map(str,raw_input().split())
      if hoge[0] not in thing:
        print 0
        break
      t = int(hoge[1])
      x,y = 2,0
      for j in range(t):
        r = thing.index(hoge[x])
        y += money[r]
        x+=1
      e = thing.index(hoge[0])
      money[e]= y
    g = raw_input()
    ans = thing.index(g)
    print money[ans]

Submission status: Runtime Error

Solution code:
while True:
    n = int(raw_input())
    if n == 0: break
    
    a = []
    for i in range(n):
        a.append(map(int, raw_input().split()))
    
    for i in range(n):
        a[i].append(sum(a[i]))
    
    col_sum = []
    for col in range(len(a[0])):
        sum = 0
        for row in range(len(a)):
            sum += a[row][col];
        col_sum.append(sum)
    a.append(col_sum)

    for row in range(len(a)):
        for col in range(len(a[0])):
            print "%5d" % a[row][col],
        print

Submission status: Runtime Error

Solution code:
n = -1
while n != 0:
	n = int(raw_input())
	a = [map(int,raw_input().split()) for i in xrange(n)]
	a = [l + [sum(l)] for l in a]
	a.append([sum([l[i] for l in a]) for i in xrange(len(a[0]))])
	print '\n'.join([''.join([str(i).rjust(5) for i in l]) for l in a])

Submission status: Runtime Error

Solution code:
while True:
    n = input()
    ans = []
    if n == 0: break
    tmp = [0] * (n+1)
    for i in range(n):
        ans.append(map(int, raw_input().split()))
        ans[i].append(sum(ans[i]))
        for j in range(len(ans[i])):
            tmp[j] += ans[i][j]
    ans.append(tmp)
    for a in ans:
        print " ".join(map(lamda x: str(x).rjust(5), a))

Submission status: WA: Presentation Error

Solution code:
while 1:
  n=input()+1
  if n==1: break
  x=[]
  y=[0]*n
  N=range(n)
  for i in N[:-1]:
    a=map(int,raw_input().split(' '))
    a.append(sum(a))
    x.append(a)
    for j in N: y[j]+=a[j]
  x.append(y)
  for i in N:
    for e in x[i]: print str(e).rjust(5),
    print

Submission status: WA: Presentation Error

Solution code:
f=0
while 1:
  n=input()+1
  if n==1: break
  x=[]
  y=[0]*n
  N=range(n)
  for i in N[:-1]:
    a=map(int,raw_input().split(' '))
    a.append(sum(a))
    x.append(a)
    for j in N: y[j]+=a[j]
  x.append(y)
  for i in N:
    if f: print
    for e in x[i]: print str(e).rjust(5),
    f=1

Submission status: WA: Presentation Error

Solution code:
while True:
	n=int(input())
	if not n: break
	a=[list(map(int,input().split())) for _ in range(n)]
	t=[[0 for _ in range(n+1)] for _ in range(n+1)]
	for i in range(n):
		for j in range(n):
			t[i][j]=a[i][j]
			t[i][n]+=a[i][j]
			t[n][j]+=a[i][j]
			t[n][n]+=a[i][j]
	for i in range(n+1):
		print(" ".join(map(str,t[i])))

Submission status: WA: Presentation Error

Solution code:
#!/usr/bin/python
# -*- coding: utf-8 -*-

def main():
    while(True):
        # 入力
        N = int(raw_input())
        if N == 0:
            break

        inp = []
        for x in range(N):
            input_list = map(int, raw_input().split())
            inp.append(input_list)

        printAns(inp)

def printAns(num_list):
    add_list = []
    for x in num_list:
        x.append(sum(x))
        add_list.append(x)

    total_list = [sum(x) for x in zip(*add_list)]
    add_list.append(total_list)
    print5digit(add_list)

def print5digit(num_list):
    for x in num_list:
        for y in x:
            print "{0:5}".format(y),
        else:
            print

if __name__=='__main__':
    main()

Submission status: WA: Presentation Error

Solution code:
#!/usr/bin/python
# -*- coding: utf-8 -*-

def main():
    while(True):
        # 入力
        N = int(raw_input())
        if N == 0:
            break

        inp = []
        for x in range(N):
            input_list = map(int, raw_input().split())
            inp.append(input_list)

        printAns(inp)

def printAns(num_list):
    add_list = []
    for x in num_list:
        x.append(sum(x))
        add_list.append(x)

    total_list = [sum(x) for x in zip(*add_list)]
    add_list.append(total_list)
    print5digit(add_list)

def print5digit(num_list):
    for x in num_list:
        for y in x:
            print("{0:4d}".format(y)),
        else:
            print

if __name__=='__main__':
    main()

Submission status: WA: Presentation Error

Solution code:
while True:
    n = int(raw_input())
    if n == 0:
        break
    else: # n > 0
        table = []
        for m in range(n):
            line = []
            
            line = map(int,raw_input().split())

            line_sum = sum(line)
            line.append(line_sum)
            table.append(line)
            
        
        
        vert_sum_line=[]
        for k in range(n):
            vert_sum = 0
            for j in range(n):
                vert_sum += table[j][k]
            vert_sum_line.append(vert_sum)
        
        total = sum(vert_sum_line)
        vert_sum_line.append(total)
    

        for line in table:
            print " ".join(map(str,line))
        print " ".join(map(str,vert_sum_line))

Submission status: WA: Presentation Error

Solution code:
while True:
    n = int(raw_input())
    if n == 0:
        break
    else: # n > 0
        table = []
        for m in range(n):
            line = []
            
            line = map(int,raw_input().split())

            line_sum = sum(line)
            line.append(line_sum)
            table.append(line)
            
        
        
        vert_sum_line=[]
        for k in range(n):
            vert_sum = 0
            for j in range(n):
                vert_sum += table[j][k]
            vert_sum_line.append(vert_sum)
        
        total = sum(vert_sum_line)
        vert_sum_line.append(total)
    

        for line in table:
            print " ".join(map(str,line)).strip()
        print " ".join(map(str,vert_sum_line)).strip()

Submission status: WA: Presentation Error

Solution code:
while True:
    num = input()
    if num == 0: break

    sht = []
    for i in range(num):
        sht.append(map(int, raw_input().split()))
        sht[i].append(sum(sht[i]))
        print " ".join(map(str, sht[i]))

    row = []
    for i in range(num + 1):
        sm = 0
        for j in range(num):
            sm += sht[j][i]
        row.append(sm)
    print " ".join(map(str, row))

Submission status: WA: Presentation Error

Solution code:
while True:
    N = int(raw_input())
    if N == 0: break
    total = None
    result = []
    for i in range(N):
        lis = list(map(int, raw_input().split(' ')))
        lis.append(sum(lis))
        result.append(lis)
        if total == None:
            total = lis[:]
        else:
            for i in range(len(total)):
                total[i] = total[i]+lis[i]
    else:
        result.append(total)
        for line in result:
            ans = []
            for v in line:
                ans.append('%05s' % v)
            else:
                print ' '.join(ans)

Submission status: WA: Presentation Error

Solution code:
while True:
    n = int(input())
    if n == 0:
        break

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

    last = [0 for i in range(n)]
    for row in m:
        print(' '.join(map(str, row)), sum(row))
        for (i, e) in enumerate(row):
            last[i] += e

    print(' '.join(map(str, last)), sum(last))

Submission status: WA: Presentation Error

Solution code:
if __name__ == "__main__":
    while True:
        flag = input()
        if flag == 0:
            break
        num, ans = [], [0]*(flag + 1)
        for i in xrange(flag):
            num.append(map(int, raw_input().split()))
        for i in xrange(len(num)):
            for j in xrange(len(num[i])):
                print ("     " + str(num[i][j]))[-5:],
                ans[j] += num[i][j]
            print ("     " + str(sum(num[i])))[-5:]
            ans[j+1] += sum(num[i])

        for i in xrange(flag + 1):
            if i == flag:
                print ("     " + str(ans[i]))[-5:]
            else:
                print ("     " + str(ans[i]))[-5:],

Submission status: WA: Presentation Error

Solution code:
from sys import stdin

if __name__ == '__main__':
    is_begin = False
    for n in stdin:
        if int(n) == 0:
            break
        else:
            if is_begin:
                print()
        is_begin = True

        bottom_total = []
        right_total = []
        for _ in range(int(n)):
            line = input().split(' ')
            total = 0
            for i, v in enumerate(line):
                print(repr(int(v)).rjust(5), end='')
                total += int(v)
                if len(bottom_total) < i + 1:
                    bottom_total.append(int(v))
                else:
                    bottom_total[i] += int(v)
            right_total.append(total)
            print(repr(right_total[_]).rjust(5))

        for num in bottom_total:
            print(repr(num).rjust(5), end='')
        print(repr(sum(bottom_total)).rjust(5), end='')

Submission status: WA: Presentation Error

Solution code:
while True:
    n = int(input())
    if n != 0:
        sum_col = [0 for i in range(n+1)]
        for i in range(n):
            list_row = list(map(int,input().split(" ")))
            sum_row = sum(list_row)
            list_row.append(sum_row)
            for j in range(len(list_row)):
                sum_col[j] += list_row[j]


            print((" ").join(list(map(str,list_row))))
        print((" ").join(list(map(str,sum_col))))
    else:
        break

Submission status: WA: Presentation Error

Solution code:
while True:
    n = int(input())
    if n != 0:
        sum_col = [0 for i in range(n+1)]
        for i in range(n):
            list_row = list(map(int,input().split(" ")))
            sum_row = sum(list_row)
            list_row.append(sum_row)
            for j in range(len(list_row)):
                sum_col[j] += list_row[j]


            print("{:<10}".format("%s")%((" ").join(list(map(str,list_row)))))
        print("{:<10}".format("%s") % ((" ").join(list(map(str, sum_col)))))
    else:
        break

Submission status: WA: Presentation Error

Solution code:
while 1:
    n=int(raw_input())
    if n==0:break
    a=[map(int,raw_input().split()) for _ in xrange(n)]
    ans=[[0]*(n+1) for _ in xrange(n+1)]
    for i in xrange(n+1):
        for j in xrange(n+1):
            if i==n :
                if j==n:
                    ans[n][n]+=sum(ans[n])
                else:
                    for k in xrange(n):
                        ans[n][j]+=a[k][j]
            else:
                if j==n:
                    for k in xrange(n):
                        ans[i][n]+=a[i][k]
                else:
                    ans[i][j]=a[i][j]
    for i in xrange(n+1):
        print(' '.join(map(str,ans[i])))

Submission status: WA: Presentation Error

Solution code:
while 1:
    n=int(raw_input())
    if n==0:break
    a=[map(int,raw_input().split()) for _ in xrange(n)]
    ans=[[0]*(n+1) for _ in xrange(n+1)]
    for i in xrange(n+1):
        for j in xrange(n+1):
            if i==n :
                if j==n:
                    ans[n][n]+=sum(ans[n])
                else:
                    for k in xrange(n):
                        ans[n][j]+=a[k][j]
            else:
                if j==n:
                    for k in xrange(n):
                        ans[i][n]+=a[i][k]
                else:
                    ans[i][j]=a[i][j]
    for i in xrange(n+1):
        for j in xrange(n+1):
            if j==n:
                print "%5s" % str(ans[i][j])
            else:
                print "%5s" % str(ans[i][j]),

Submission status: WA: Presentation Error

Solution code:
while True:
    n = int(input())
    if n == 0:
        break
    row = n
    tab = []
    for i in range(row):
        r = list(map(int, input().split()))
        r.append(sum(r))
        tab.append(r)

    bel = []
    for i in range(row):
        tmp = 0
        for j in range(row):
            tmp += tab[j][i]
        bel.append(tmp)
    bel.append(sum(bel))

    for i in range(row):
        for j in range(row+1):
            print(str(tab[i][j])+" ", end="")
        print()
        
    for i in range(row+1):
        print(str(bel[i])+" ", end="")
    print()

Submission status: WA: Presentation Error

Solution code:
while 1:
    n=int(input())
    if n==0:break
    a = []
    for i in range(n):
        a+=[list(map(int, input().split()))]
        a[i]+=[sum(a[i])]
        print(*a[i])
    a=list(zip(*a[::-1]))
    for i in range(n):print(sum(a[i]),end=' ')
    print(sum(a[n]))