Problem:


Spreadsheet

  Your task is to perform a simple table calculation.

  Write a program which reads the number of rows r, columns c and a table of r &times; c elements, and prints a new table, which includes the total sum for each row and column. 

Input

  In the first line, two integers r and c are given. Next, the table is given by r lines, each of which consists of c integers separated by space characters.

Output

  Print the new table of (r+1) &times; (c+1) elements. Put a single space character between adjacent elements. For each row, print the sum of it's elements in the last column. For each column, print the sum of it's elements in the last row. Print the total sum of the elements at the bottom right corner of the table.

Constraints

 1 ≤ r, c ≤ 100

 0 ≤ an element of the table ≤ 100

Sample Input

4 5
1 1 3 4 5
2 2 2 4 5
3 3 0 1 1
2 3 4 4 6

Sample Output

1 1 3 4 5 14
2 2 2 4 5 15
3 3 0 1 1 8
2 3 4 4 6 19
8 9 9 13 17 56



Sample input:
4 5
1 1 3 4 5
2 2 2 4 5
3 3 0 1 1
2 3 4 4 6


Sample output:
1 1 3 4 5 14
2 2 2 4 5 15
3 3 0 1 1 8
2 3 4 4 6 19
8 9 9 13 17 56

Submission status: Accepted

Solution code:
import sys
write=sys.stdout.write

number_of_numbers=[]
numbers=[]

number_of_numbers=input().split()
number_of_numbers[0]=int(number_of_numbers[0])
number_of_numbers[1]=int(number_of_numbers[1])



for i in range(0,number_of_numbers[0]):
    line_nunmbers=[]
    line_numbers=input().split()
    for j in range(0,len(line_numbers)):
        line_numbers[j]=int(line_numbers[j])
    numbers.append(line_numbers)

right_end_numbers=[]
under_end_numbers=[]

for i in range(0,len(numbers)):
    right_end_numbers.append(sum(numbers[i]))

for i in range(0,len(numbers[0])):
    under_end_number=0
    for j in range(0,len(numbers)):
        under_end_number+=numbers[j][i]
    under_end_numbers.append(under_end_number)

for i in range(0,len(numbers)):
    for j in range(0,len(numbers[0])):
        write(str(numbers[i][j]))
        write(' ')
    print(right_end_numbers[i])

for i in range(0,len(numbers[0])):
    write(str(under_end_numbers[i]))
    write(' ')

print(sum(under_end_numbers))

Submission status: Accepted

Solution code:
r,c=map(int,input().split())
a,b=[],[0]*c
for i in range(r):
    x=list(map(int,input().split()))
    print(*x, end=' ')
    print(sum(x))
    for j in range(c):
        b[j]+=x[j]
print(*b, end=' ')
print(sum(b))

Submission status: Accepted

Solution code:
r,c=map(int,input().split())
a=[0]*c
for i in range(r):
    x=list(map(int,input().split()))
    for j in range(c):
        a[j]+=x[j]
    print(*x, end=' ')
    print(sum(x))
print(*a, end=' ')
print(sum(a))

Submission status: Accepted

Solution code:
r,c = map(int,input().split())

a = [list(map(int,input().split(" "))) for i in range(r)]

for i in range(r):
    r_total = sum(a[i])
    a[i].append(r_total)

c_total = []

for j in range(c+1):
    s = 0
    for k in range(r):
        s += a[k][j]
    c_total.append(s)

a.append(c_total)

for z in range(r+1):
    for w in range(c+1):
        print(a[z][w],end="")
        if w != c:
            print(" ",end="")
    print()

Submission status: Accepted

Solution code:
N, M=map(int, input().split())
s_row = [0 for x in range(M+1)]
for n in range(N):
 arr = list(map(int, input().split()))
 arr.append(sum(arr))
 s_row = [x+y for x,y in zip(s_row, arr)]
 print(" ".join([str(x) for x in arr]))
print(" ".join([str(x) for x in s_row]))

Submission status: Accepted

Solution code:
r, c = map(int, input().split())
array = [[0 for i in range(c+1)] for j in range(r+1)]
for i in range(r):
    for j, num in enumerate(input().split()):
        array[i][j] = int(num)
for i in range(r):
    array[i][c] = sum(array[i])
for i in range(c+1):
    for j in range(r):
        array[r][i] += array[j][i]
for i in range(r+1):
    for j in range(c+1):
        if j == c:
            print(array[i][j])
        else:
            print(array[i][j], end=' ')

Submission status: Accepted

Solution code:
r, c = map(int, input().split())
M, C = [list(map(int, input().split())) for i in range(r)], [0 for j in range(c)]
for i in range(r):
    for j in range(c):
        C[j] += M[i][j]
        print('%d' %(M[i][j]), end=' ')
    print(sum(M[i]))
for j in range(c):
    print('%d' %(C[j]), end=' ')
print(sum(C))

Submission status: Accepted

Solution code:
# (c) midandfeed
r, c = [int(x) for x in input().split()]
q = []
sumr = [0]*c
tempr = 0
for i in range(r):
	col = [int(x) for x in input().split()]
	for j in range(c):
		sumr[j] += col[j]
	col.append(sum(col))
	q.append(col)
sumr.append(sum(sumr))
q.append(sumr)
for i in range(r+1):
	for j in range(c+1):
		print(q[i][j], end='')
		if (j != (c)):
			print(' ', end='')
	# if (i != (r) ):
	print()

Submission status: Accepted

Solution code:
row, col = map(int, raw_input().split())

mat = []
for i in range(row):
    line = map(int, raw_input().split())
    mat.append(line + [sum(line)])

row2 = [0 for i in range(col+1)]
for i in range(col+1):
    for j in range(row):
        row2[i] += mat[j][i]
mat.append(row2)
for i in mat:
    print " ".join(map(str,i))

Submission status: Accepted

Solution code:
(r, c) = map(int, input().split())
a = [[v for v in list(map(int, input().split()))] for r in range(r)]
add_c = [sum(l) for l in a]
for i in range(r):
    a[i].append(add_c[i])
add_r = [sum([a[i][j] for i in range(r)]) for j in range(c + 1)]
a.append(add_r)
for i in range(r + 1):
    print(' '.join(map(str, a[i])))

Submission status: Accepted

Solution code:
(r, c) = map(int, input().split())
a = [[v for v in list(map(int, input().split()))] for r in range(r)]
for i in range(r):
    a[i].append(sum(a[i]))
add_r = [sum([a[i][j] for i in range(r)]) for j in range(c + 1)]
a.append(add_r)
for i in range(r + 1):
    print(' '.join(map(str, a[i])))

Submission status: Accepted

Solution code:
def main():
    r, c = map(int, input().split())
    
    A = []
    for _ in range(r):
        A.append(list(map(int, input().split())))

    for x in range(r):
        hoge = 0
        for y in range(c):
            hoge += A[x][y]
        A[x].append(hoge)

    B = []
    for x in range(c):
        hoge = 0
        for y in range(r):
            hoge += A[y][x]
        B.append(hoge)

    total = 0
    for b in B:
        total += b
    
    B.append(total)
    A.append(B)

    for x in range(len(A)):
        for y in range(len(A[x])):
            if y != len(A[x]) - 1:
                print(A[x][y], end = " ")
            else:
                print(A[x][y])

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import copy
import sys

array_data = map(int, raw_input().split())
r = array_data[0]
c = array_data[1]

array = []
for i in range(r):
	partial_array = map(int, raw_input().split())
	array.append(partial_array)

ans = copy.deepcopy(array)
for i in range(r):
	ans[i].append(sum(array[i]))
ans.append([])
for j in range(c+1):
	wa = 0
	for i in range(r):
		wa += ans[i][j]
	ans[r].append(wa)

for i in range(len(ans)):
	for j in range(len(ans[i])):
		if j != len(ans[i])-1:
			sys.stdout.write("{:} ".format(ans[i][j]))
		else:
			print(ans[i][j])

Submission status: Accepted

Solution code:
from itertools import chain

def main():
    inputs = []
    r, _ = map(int, input().split())
    for i in range(r):
        inputs.append(tuple(map(int, input().split())))

    row_sums = map(lambda row: sum(row), inputs)
    column_sums = map(lambda column: sum(column), map(lambda *x: x, *inputs))
    all_sum = sum(chain.from_iterable(inputs))

    for i, s in zip(inputs, row_sums):
        print(" ".join(map(lambda n: str(n), i)) + " {}".format(s))
    else:
        print(" ".join(map(lambda n: str(n), column_sums)) + " {}".format(all_sum))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def main():

    r,c = map(int, input().split())
    chart = [list(map(int, input().split())) for i in range(r)]
    total = [0 for i in range(c)]

    for row in chart:
        print((' '.join(map(str, row))) + ' ' + str(sum(row)))
        total = [x + y for (x, y) in zip(total, row)]

    print((' '.join(map(str, total))) + ' ' + str(sum(total)))


main()

Submission status: Accepted

Solution code:
r,c=list(map(int,input().split()))
s=[0 for i in range(c+1)]

for i in range(r):
    l=list(map(int,input().split()))
    l.append(sum(l))
    print(' '.join(map(str,l)))
    s=[s[i]+l[i] for i in range(c+1)]
print(' '.join(map(str,s)))

Submission status: Accepted

Solution code:
r, c = [int(s) for s in input().split()]
rows = [[0 for i in range(c + 1)] for j in range(r + 1)]

for rc in range(r):
    in_row = [int(s) for s in input().split()]
    for cc, val in enumerate(in_row):
        rows[rc][cc] = val
        rows[rc][-1] += val
        rows[-1][cc] += val
        rows[-1][-1] += val
    
for row in rows:
    print(' '.join([str(i) for i in row]))

Submission status: Accepted

Solution code:
r, c = map(int, input().split())

sumOfCols = [0] * c

for i in range(r):
    cols = list(map(int, input().split()))
    s = sum(cols)
    sumOfCols = [p + q for p,q in zip(sumOfCols, cols)]
    print(' '.join(map(str, cols)), end=' ')
    print(s)


s = sum(sumOfCols)
print(' '.join(map(str, sumOfCols)), end=' ')
print(s)

Submission status: Accepted

Solution code:
# -*-coding:utf-8

def main():

    r, c = map(int, input().split())

    #A = [[0 for i2 in range(c+1)] for i1 in range(r+1)]
    A = []
    for i in range(r):
        tokens = list(map(int, input().split()))
        tokens.append(sum(tokens))
        A.append(tokens)

    rowSumList = []
    for i in range(c+1):
        rowSum = 0
        for j in range(r):
            rowSum += A[j][i]
        rowSumList.append(rowSum)

    A.append(rowSumList)

    for i in range(r+1):
        for j in range(c+1):
            if(j == c):
                print('%d' % A[i][j])
            else:
                print('%d ' % A[i][j], end='')

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
r,c=map(int,input().split())
q=range
b=[]
for i in q(r):
    w=[int(i) for i in input().split()]
    w.append(sum(w))
    b.append(w)
    print(*w)
l=[]
for i in q(c+1):
    x=0
    for a in b:x+=a[i]
    l.append(x)
print(*l)

Submission status: Accepted

Solution code:
import sys
s=input().split()
r=int(s[0]);c=int(s[1])
a=[[0 for j in range(c)] for i in range(r)]
b=[[0 for j in range(c+1)]for i in range(r+1)]
for i in range(r):
    t=input().split()
    for j in range(c):
        a[i][j]=int(t[j])
        b[i][j]=a[i][j]
        b[r][j]+=a[i][j]
        b[i][c]+=a[i][j]
        b[r][c]+=a[i][j]
for i in range(r+1):
    for j in range(c+1):
        sys.stdout.write("{0}".format(b[i][j]))
        if j==c:
            sys.stdout.write("\n")
        else:
            sys.stdout.write(" ")

Submission status: Accepted

Solution code:
def main():
    r, c = map(int, input().split())

    col_sums = [0] * c
    rows = [
        input()
        for n_r in range(r)
    ]

    for row in rows:
        row_sum = 0
        cols = map(int, row.split())
        for idx, col in enumerate(cols):
            col_sums[idx] += col
            row_sum += col

        print(row, end="")
        print("", row_sum)

    last_line = " ".join(map(str, col_sums))
    print(last_line, end="")
    print("", sum(col_sums))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
r,c=map(int,input().split())
m=[[int(_) for _ in input().split()] for i in range(r)]
for i in range(r):m[i].append(sum(m[i]))
m.append([sum([m[i][j] for i in range(r)]) for j in range(c+1)])
for i in range(r+1):print(*m[i])

Submission status: Accepted

Solution code:
r,c=map(int,input().split())
m=[[int(_) for _ in input().split()] for i in range(r)]
for i in range(r):m[i].append(sum(m[i]))
m.append([sum([m[i][j] for i in range(r)]) for j in range(c+1)])
[print(*m[i]) for i in range(r+1)]

Submission status: Accepted

Solution code:
r,_=map(int, input().split())
m=[list(map(int,input().split()))for i in range(r)]
[s.append(sum(s))or print(*s)for s in m]
print(*[sum(c) for c in zip(*m)])

Submission status: Accepted

Solution code:
r,_=map(int,input().split())
m=[list(map(int,input().split()))for _ in range(r)]
[s.append(sum(s))or print(*s)for s in m]
print(*[sum(c) for c in zip(*m)])

Submission status: Accepted

Solution code:
r,_=map(int,input().split())
m=[list(map(int,input().split()))for _ in range(r)]
[s.append(sum(s))or print(*s)for s in m]
print(*[sum(c)for c in zip(*m)])

Submission status: Accepted

Solution code:
r,_=map(int, input().split())
a=[list(map(int, input().split())) for _ in range(r)]
[s.append(sum(s))or print(*s)for s in a]
print(*[sum(c)for c in zip(*a)])

Submission status: Accepted

Solution code:
n, p = [int(i) for i in input().split()]
a = [[] for _ in range(p+1)]

for _ in range(n):
    l = [int(i) for i in input().split()]
    l.append(sum(l))
    [a[i].append(j) for i, j in enumerate(l)]
    print(*l)

for h,i in enumerate(a):
    if h == len(a) - 1:
        print(sum(i))
    else:
        print(sum(i),end=' ')

Submission status: Accepted

Solution code:
l = input().split()
r = int(l[0])
c = int(l[1])

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

for i in range(len(a)):
    a[i].insert(c, sum(a[i]))

b = []
a.append(b)

for i in range(c): #4
    s = 0
    for j in range(r): #5
        s += a[j][i]
    a[-1].append(s)
a[-1].append(sum(a[-1]))

for i in range(len(a)):
    print(*a[i])

Submission status: Accepted

Solution code:
r,c=map(int,input().split())
table=list()
for i in range(r):
    table.append(list(map(int,input().split())))
for i in range(r):
    for j in range(c):
        print(table[i][j],"",end="")
    print(sum(table[i]),sep="")
bottom=list()
for i in range(c):
    tmp=0
    for j in range(r):
        tmp+=table[j][i]
    bottom.append(tmp)
for i in bottom:
    print(i,"",end="")
print(sum(bottom))

Submission status: Accepted

Solution code:
r,c = map(int,input().split())
line = []
array = [[0 for i in range(c+1)]for j in range(r+1)]
for i in range(0,r):
    line = input().split()
    for j in range(c):
        array[i][j]=int(line[j])
sum = 0
for i in range(r):
    for j in range(c):
        sum = sum + array[i][j]
    array[i][c]=sum
    sum = 0
for i in range(c):
    for j in range(r):
        sum = sum + array[j][i]
    array[r][i]=sum
    sum = 0
sum = 0
for j in range(r):
    sum = sum + array[j][c]
array[r][c] = sum    
for i in range(r+1):
    for j in range(c+1):
        if j == c:
            print("{}".format(array[i][j])) 
        else:    
            print("{}".format(array[i][j]),end=" ")

Submission status: Accepted

Solution code:
r, c = map(int, raw_input().split())
a = []
for _ in range(r):
	a.append(map(int, raw_input().split()))

for i in range(r):
	a[i].append(sum(a[i]))
	print ' '.join(map(str, a[i]))

b = [0 for _ in range(c+1)]
for j in range(c+1):
	for i in range(r):
		b[j] += a[i][j]
print " ".join(map(str, b))

Submission status: Accepted

Solution code:
r, c = map(int, input().split())

a, btm = [], []

for i in range(r):
    a.append(list(map(int, input().split())))
    a[i].append(sum(a[i]))
    print(*a[i])

rotate = list(zip(*a))
for i in range(len(rotate)):
    btm.append(sum(rotate[i]))

print(*btm)

Submission status: Accepted

Solution code:
# coding:utf-8
r,c = map(int, raw_input().split())
array = [[0 for i in range(c)] for j in range(r)]
for i in range(r):
    array[i] = map(int, raw_input().split())
for i in range(r):
    for j in range(c):
        print array[i][j],
    else:
        print sum(array[i])
total = 0
total2 = 0
for i in range(c):
    for j in range(r):
        total += array[j][i]
    else:
        print total,
        total2 += total
        total = 0
else:
    print total2

Submission status: Accepted

Solution code:
r, c = map(int, input().split())
MAP = [[] for _ in range(r)]
for i in range(r):
    MAP[i].extend(list(map(int, input().split())))
    MAP[i].append(sum(MAP[i]))
    print(*MAP[i])
li = []
for Z in zip(*MAP):
    li.append(sum(Z))
print(*li)

Submission status: Accepted

Solution code:
r, c = map(int, input().split())
a = [[0 for i in range(c + 1)] for j in range(r + 1)]

for i in range(r):
    sumC = 0
    ar = input().split()
    for j in range(c):
        a[i][j] = int(ar[j])
        sumC += int(ar[j])
    a[i][c] = sumC

for k in range(c + 1):
    for m in range(r):
        a[r][k] += a[m][k]


for i in a:
    print(*i)

Submission status: Accepted

Solution code:
row, col = map(int, raw_input().split())

ret = []
for _ in range(row):
    _row = map(int, raw_input().split())
    _row += [sum(_row)]
    ret.append(_row)

for i in ret:
    print ' '.join(map(str, i))

ret_sum = []
for i in range(col+1):
    work = 0
    for j in ret:
        work += j[i]
    ret_sum.append(work)

print ' '.join(map(str, ret_sum))

Submission status: Accepted

Solution code:
r,_=map(int,input().split())
m=[list(map(int,input().split()))for _ in range(r)]
for s in m:s.append(sum(s))or print(*s)
print(*[sum(c)for c in zip(*m)])

Submission status: Accepted

Solution code:
# coding: UTF-8
r,c = map(int,raw_input().split(" "))
mtrix = [map(int,raw_input().split()) for i in range(r)]
last_line = [0 for i in range(c+1)]
for i in range(r):
    mtrix[i].append(sum(mtrix[i]))
    last_line = [x+y for (x,y) in zip(mtrix[i],last_line)]
mtrix.extend([last_line])
for i in range(r+1):
    print " ".join(map(str,mtrix[i]))

Submission status: Accepted

Solution code:
r,_=map(int,input().split())
m=[list(map(int,input().split()))for _ in[0]*r]
for s in m:s.append(sum(s))or print(*s)
print(*[sum(c)for c in zip(*m)])

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
matrix = list()
for i in range(n):
    hoge = list()
    for elem in map(int, input().split()):
        hoge.append(elem)
    hoge.append(sum(hoge))
    matrix.append(hoge)
last_raw = list()
for j in range(m+1):
    hoge = 0
    for i in range(n):
        hoge += matrix[i][j]
    last_raw.append(hoge)
matrix.append(last_raw)
for i in range(n+1):
    print (' '.join(map(str, matrix[i])))

Submission status: Accepted

Solution code:
data = list(map(int, input().split()))
row = data[0]+1
column = data[1]+1
table = [[0 for j in range(column)] for i in range(row)]
for i in range(row-1):
    data_in = list(map(int, input().split()))
    for j in range(column-1):
        table[i][j] = data_in[j]
for i in range(row):
    sum_row = 0
    for j in range(column):
        sum_row += table[i][j]
    table[i][column-1] = sum_row
for i in range(column):
    sum_column = 0
    for j in range(row):
        sum_column += table[j][i]
    table[row-1][i] = sum_column
for i in range(row):
    for j in range(column):
        if(j==column-1):
            print(str(table[i][j]))
        else:
            print("{0}".format(table[i][j]), end=" ")

Submission status: Accepted

Solution code:
table = []
r,c = [int(x) for x in input().split( )]
for i in range(r):
    mini_table = [int(x) for x in input().split( )]
    table.append(mini_table)
for i in table:
    i.append(sum(i))
gyou = []
for i in range(c+1):
    counter = 0
    for j in range(r):
        counter += table[j][i]
    gyou.append(counter)
table.append(gyou)
for i in table:
    print(' '.join(str(j) for j in i))

Submission status: Accepted

Solution code:
a, b = map(int, input().split())
sheet = [list(map(int, input().split())) for i in range(a)]
for row in sheet:
    print(' '.join(map(str, row + [sum(row)])))
lastrow = [sum([j[i] for j in sheet]) for i in range(b)]
print(' '.join(map(str, lastrow + [sum(lastrow)])))

Submission status: Accepted

Solution code:
import sys
from functools import reduce

(row, column) = sys.stdin.readline().rstrip('\r\n').split(' ')
row = int(row)
column = int(column)

a = []

for ii in range(row):
  a.append([int(x) for x in input().rstrip('\r\n').split(' ')])

for ii in range(len(a)):
  for aa in a[ii]:
    print(str(aa) + ' ', end='')
  print(reduce(lambda sum, x: sum + x, a[ii]))

total = 0
for ii in range(column):
  sum = 0
  for jj in range(row):
    sum += a[jj][ii]
    total += a[jj][ii]
  print(str(sum) + ' ', end='')
print(total)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
mat = [[0 for x in range(m+1)] for y in range(n+1)]

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

for i in range(n):
    for j in range(m):
        mat[i][-1] += mat[i][j]
        mat[-1][j] += mat[i][j]
    mat[-1][-1] += mat[i][-1]

for i in range(n+1):
    print(*mat[i])

Submission status: Accepted

Solution code:
r,c=map(int,input().split())
s=[]
t=[]
for i in range(r):
    s.append(list(map(int,input().split())))
    s[i].append(sum(s[i]))
for j in range(c+1):
    u = 0
    for k in range(r):
        u += s[k][j]
    t.append(u)
    s.append(t)
for i in range(r+1):
    w = str(s[i][0])
    for j in range(1,c+1):
        w += " "+str(s[i][j])
    print(w)

Submission status: Accepted

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

import sys

r, c = map(int, sys.stdin.readline().split())
matrix = list()

for i in range(r):
    _line = list(map(int, sys.stdin.readline().split()))
    _line.append(sum(_line))
    matrix.append(_line)

_line = list()
for col in range(c+1):  # include sum column
    val = 0
    for row in range(r):
        val += matrix[row][col]
    _line.append(val)

matrix.append(_line)
for _line in matrix:
    str_x = list(map(str, _line))
    print(' '.join(str_x))

Submission status: Accepted

Solution code:
(r,c) = tuple(map(int,input().split()))
graph = []
for i in range(r):
  lst = list(map(int,input().split()))
  lst.append(sum(lst))
  graph.append(lst)
lsf = []
for i in range(c + 1):
  s = 0
  for j in range(r):
    s += graph[j][i]
  lsf.append(s)
for l in graph:
  print(' '.join(map(str,l)))
print(' '.join(map(str,lsf)))

Submission status: Accepted

Solution code:
r, c = map(int, input().split())

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

c_line_sum = []
for i in range(c): # 列の総和の計算
    t = []
    for j in range(r):
        t.append(x[j][i])
    c_line_sum.append(sum(t))

for i in range(r): # 行の総和の計算
    x[i].append(sum(x[i]))

c_line_sum.append(sum(c_line_sum))
x.append(c_line_sum)

for m in x:
    print(*m)

Submission status: Accepted

Solution code:
from operator import add
from functools import reduce

r,c = map(int,input().split())

x = [list(map(int, input().split())) for i in range(r)]


#import numpy as np


#x=[[3,4,5],[1,2,3]]
#a=np.array(x)
#print(a)
#b=reduce(add, a)
#print(b)
#b=list(b)

#x=list(x[1])
#x.append(b)

#print(x)


b = []

for j in range(c):
    a = [x[i][j] for i in range(len(x))]
    b.append(reduce(add,a))

#print(b)

x.append(b)

for i in range(len(x)):
    y = reduce(add,x[i])
    x[i].append(y)
    
    
z = [list(map(str,x[i])) for i in range(len(x))]

#print(z)
#print(z)

w = [' '.join(z[i]) for i in range(len(x))]

[print(w[i]) for i in range(len(w))]

Submission status: Accepted

Solution code:
def calc_sums(tbl):
    """calculate the total sum for each row and column

    >>> calc_sums([[1, 2], [2, 1]])
    [[1, 2, 3], [2, 1, 3], [3, 3, 6]]
    """
    new_tbl = [row + [0] for row in tbl] + [[0] * (len(tbl[0])+1)]

    for i, row in enumerate(tbl):
        for j, val in enumerate(row):
            new_tbl[i][-1] += val
            new_tbl[-1][j] += val

    new_tbl[-1][-1] = sum(new_tbl[-1][:-1])

    return new_tbl


def run():
    n, m = [int(v) for v in input().split()]
    table = []

    for i in range(n):
        table.append([int(v) for v in input().split()])

    for row in calc_sums(table):
        print(" ".join([str(i) for i in row]))


run()

Submission status: Accepted

Solution code:
r, c = map(int, input().split())
a = []
for i in range(r) :
    a.append(list(map(int, input().split())))
    a[i] += [sum(a[i])]
    print(*a[i], sep = ' ')
a = list(zip(*a))
for i in range(c) :
    print(sum(a[i]), end = ' ')
print(sum(a[c]))

Submission status: Accepted

Solution code:
r, c = map(int, input().split())
table = []
for i in range(r):
    table.append(list(map(int, input().split())))
for i in table:
    i.append(sum(i))
b = []
for i in range(c):
    x = 0
    for k in range(r):
        x += table[k][i]
    b.append(x)
b.append(sum(b))
for i in table:
    print(" ".join(map(str, i)))
print(" ".join(map(str, b)))

Submission status: Accepted

Solution code:
ans = ''
r, c = map(int, input().split(' '))
i = 0
L = [0] * (c + 1)
while i < r:
    a = list(map(int, input().split(' ')))
    atot = 0
    j = 0
    while j < c:
        atot += a[j]
        L[j] += a[j]
        j += 1
    ans += ' '.join(map(str, a)) + ' ' + str(atot) + '\n'
    L[c] += atot
    i += 1
if ans != '':
    ans += ' '.join(map(str, L))
    print(ans)

Submission status: Accepted

Solution code:
r, c = map(int, input().split())
args = []

for i in range(r):
    args.append(list(map(int, input().split())))
    r_sum = 0
    for j in args[i]:
        r_sum += j
    args[i].append(r_sum)

l_row = []

for k in range(c + 1):
    c_sum = 0
    for l in range(r):
        c_sum += args[l][k]
    l_row.append(c_sum)        

for m in range(r):
    print(' '.join(map(str, args[m])))

print(' '.join(map(str, l_row)))

Submission status: Accepted

Solution code:
#coding: utf-8

r, c = (int(i) for i in input().split())

s = [[0 for i in range(c+1)] for j in range(r+1)]

for i in range(r):
    s[i] = input().split()
    s[i].append(0)

for i in range(r):
    for j in range(c):
        s[i][j] = int(s[i][j])

for i in range(r):
    x = 0
    for j in range(c):
        x += s[i][j]
    s[i][-1] = x

for i in range(c+1):
    x = 0
    for j in range(r):
        x += s[j][i]
    s[-1][i] = x

for i in range(r+1):
    for j in range(c+1):
        print(s[i][j], end = ' ' if j != c else '\n')

Submission status: Accepted

Solution code:
r, c = map(int, input().split())
output = []
for _ in range(r):
  l = list(map(int, input().split()))
  s = sum(l)
  l.append(s)
  output.append(l)
final = []
for i in range(c):
  t = 0
  for j in range(r):
    t += output[j][i]
  final.append(t)
s = sum(final)
final.append(s)
output.append(final)
for i in output:
  print(*i)

Submission status: Accepted

Solution code:
r, c = map(int, input().split())
l = []
for i in range(r):
  l.append(list(map(int, input().split())))
  l[i] += [sum(l[i])]
  print(*l[i])
l = list(zip(*l))
for i in range(c): print(sum(l[i]), end = " ")
print(sum(l[c]))

Submission status: Accepted

Solution code:
import sys

l = []
for i in sys.stdin:
    l.append(i.split())

for i in range(0,len(l)):
    for j in range(0,len(l[i])):
        l[i][j] = int(l[i][j])

for i in range(1,len(l)):
    l[i].append(sum(l[i]))

result = []
for i in range(0,l[0][1]+1):
    count = 0
    for j in range(1,l[0][0]+1):
        count += l[j][i]
    result.append(count)
l.append(result)

for i in range(1,len(l)):
    for j in range(0,len(l[i])):
        print(l[i][j],end='')
        if j < len(l[i]) - 1:
            print(" ",end='')
    print()

Submission status: Accepted

Solution code:
r, c = map(int, input().split())
mathlist = []
for x in range(r):
    mathlist.append(list(map(int, input().split())))

mathlist_change = list(map(list, zip(*mathlist)))
mathlist_last = 0

for y in mathlist:
    for z in y:
        print('{a} '.format(a=z), end='')
    print(sum(y))

for i in mathlist_change:
    print('{b} '.format(b=sum(i)), end='')
    mathlist_last += sum(i)

print(mathlist_last)

Submission status: Accepted

Solution code:
r,c = map(int,input().split())
num=[]
r_sum =[]
for i in range(r):
    num.append([int(i) for i in input().split()])
for i in range(r):
    line_sum = 0
    for j in range(c):
        line_sum += num[i][j]
    num[i].append(line_sum)
for i in range(c+1):
    row_sum = 0
    for j in range(r):
        row_sum += num[j][i]
    r_sum.append(row_sum)
num.append(r_sum)
for i in range(len(num)):
    print(' '.join(str(i) for i in num[i]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

r, c = list(map(int, input().split()))
a = []
line_sum = 0

for i in range(r):
    a.append(list(map(int, input().split())))

for i in range(r):
    for j in range(c + 1):
        if j == c:
            print('{0}'.format(sum(a[i])))
            line_sum += sum(a[i])
        else:
            print('{0} '.format(a[i][j]), end='')

for i in range(c):
    column_sum = 0
    for j in range(r):
        column_sum += a[j][i]

    print('{0} '.format(column_sum), end='')

print(line_sum)

Submission status: Accepted

Solution code:
import functools, operator
r,c = tuple(int(n) for n in input().split())
A = [[int(a) for a in input().split()] for i in range(r)]
for a in A:
    a.append(sum(a))
R = [functools.reduce(operator.add, x) for x in zip(*A)]
A.append(R)
for j in A:
    print(" ".join(map(str,j)))

Submission status: Accepted

Solution code:
r, c= map(int, input().split())
list = [list(map(int,input().split())) for i in range(r)]
for i in range(r):
    list[i].append(sum(list[i]))
for i in range(r):
    for j in range(c):
        print(list[i][j], end = ' ')
    print(list[i][c])
for i in range(c):
    a = 0
    for j in range(r):
        a += list[j][i]
    print(a, end = ' ')
a = 0
for j in range(r):
    a += list[j][c]
print(a)

Submission status: Accepted

Solution code:
r, c = map(int, input().split())
matrix = [[0 for i in range(c+1)] for j in range(r+1)]
for i in range(r):
    a = list(map(int, input().split()))
    for j in range(c):
        matrix[i][j] = a[j]
    matrix[i][c] = sum(a)
for l in range(c+1):
    for m in range(r):
        matrix[r][l] += matrix[m][l]

for cv in range(r+1):
    for fv in range(c+1):
        print(matrix[cv][fv], end='')
        if fv != c:
            print(" ", end='')
    print()

Submission status: Accepted

Solution code:
#! python3
# spreadsheet.py

r, c = [int(x) for x in input().split(' ')]

sheet = [[int(x) for x in input().split(' ')] for i in range(r)]
for i in range(r):
    sheet[i].append(0)
sheet.append([0 for i in range(c+1)])

for i in range(r):
    for j in range(c):
        sheet[i][c] += sheet[i][j]
        sheet[r][j] += sheet[i][j]
        sheet[r][c] += sheet[i][j]

for i in range(r+1):
    print(' '.join([str(x) for x in sheet[i]]))

Submission status: Accepted

Solution code:
r=int(input().split()[0])
m=[list(map(int,input().split()))for _ in[0]*r]
for s in m:s.append(sum(s))or print(*s)
print(*[sum(c)for c in zip(*m)])

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
matrix =[[0 for i in range(m+1)] for j in range(n+1)]

for i in range(n):
    a = list(map(int,input().split()))
    for j in range(m):
        matrix[i][j] = a[j]
        matrix[i][m] += a[j]
        matrix[n][j] += a[j]
        matrix[n][m] += a[j]

for i in range(0,n+1):
    print(' '.join(str(x) for x in matrix[i]))

Submission status: Accepted

Solution code:
r,c = map(int,input().split())
t = 0
A = [[]for i in range(r+1)]
for i in range(r):
	A[i]=list(map(int,input().split()))
for i in range(r):
	A[i].append(sum(A[i]))
for i in range(c+1):
	t=0
	for j in range(r):
		t += A[j][i]
	A[r].append(t)
for i in range(r+1):
	print(' '.join(map(str,A[i])))

Submission status: Accepted

Solution code:
r, c = map(int, input().split())
a = []
rsum = []

for i in range(r):
    a.append(list(map(int, input().split())))
    a[i].append(sum(a[i]))

for i in range(r):
    print(" ".join(list(map(str, a[i]))))

csum = []
for j in range(c+1):
    su = 0
    for i in range(r):
        su += a[i][j]
    csum.append(su)

print(" ".join(list(map(str, csum))))