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:
r,c = map(int,input().split())
l = []
for i in range(r):
    l1 = list(map(int,input().split()))
    l.append(l1)
#给每一行加和
for i in range(r):
    tmp = 0
    for j in range(c):
        tmp += l[i][j]
        
    l[i].append(tmp)

tmpl = []
#给每一列加
for i in range(c+1):
    tmp = 0
    for j in range(r):
        tmp+=l[j][i]
    tmpl.append(tmp)
l.append(tmpl)

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

Submission status: Accepted

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

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 = [0 for i in range(r)]
for i in range(r):
  a[i] = list(map(int, input().split()))
sumA = 0
sumB = 0
sumC = 0

for i in range(r):
  for j in range(c):
    print(a[i][j], "", end="")
    sumA = sumA + a[i][j]
    if j == (c-1):
      print(sumA)
      sumA = 0

for i in range(c):
  for j in range(r):
    sumB = sumB + a[j][i]
    if j == (r-1):
      print(sumB, "", end="")
      sumC = sumC + sumB
      sumB = 0
  if i == (c-1):
    print(sumC)

Submission status: Accepted

Solution code:
r,c=map(int,input().split())
lit=[[0 for jjj in range(c)]for lll in range(r)]
for a in range(r):
	tet=list(map(int,input().split()))
	for b in range(c):
		lit[a][b]=tet[b]
for p in range (r):
	for q in range(c):
			print(str(lit[p][q])+' ',end='')
	print(sum(lit[p]))
ddd=0

for qq in range(c):
	unko=0
	for pp in range(r):
		unko+=lit[pp][qq]
	ddd+=unko
	print(str(unko)+' ',end='')
print(ddd)

Submission status: Accepted

Solution code:
def main():
    r, c = map(int, input().split())
    cs = [0] * (c + 1)
    t = []
    for _ in range(r):
        s = list(map(int, input().split()))
        s.append(sum(s))
        t.append(s)
        for i in range(c):
            cs[i] += s[i]
    cs[-1] = sum(cs)
    t.append(cs)
    for a in t:
        print(' '.join(map(str, a)))


main()

Submission status: Accepted

Solution code:
lmap = lambda f, s: list(map(f, s))
n, m = map(int, input().split())
a = [lmap(int, input().split()) for i in range(n)]
a = lmap(lambda xs: xs + [sum(xs)], a)
a.append(lmap(sum, zip(*a)))
for xs in a: print(*xs)

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))))

Submission status: Accepted

Solution code:
r, c = map(int, input().split())
listA = []
for i in range(r):
	listA.append(list(map(int, input().split())))
	listA[i].append(sum(listA[i]))
temp = []
for  i in range(c+1):
    number = 0
    for j in range(r):
        number += listA[j][i]
    temp.append(number)
# s = 0
# for i in range(r):
#     for j in range(c):
#         s += listA[i][j]
# temp.append(s)
listA.append(temp)

for line in listA:
    print(*line)

Submission status: Accepted

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

Submission status: Accepted

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

m = []

t = [ 0 for _ in range(c+1) ]

for _ in range(r):
    row = list(map(int, input().split()))
    l = row+[sum(row)]

    m.append(l)
    for i in range(len(l)):
        t[i] += l[i]

m.append(t)

for l in m:
    sep = ''
    for e in l:
        print(sep+str(e), end='')
        sep = ' '
    print()

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 line in table:
    line.append(sum(line))

table.append([sum([line[i] for line in table]) for i in range(c + 1)])

for line in table:
    print(*line)

Submission status: Accepted

Solution code:
if __name__ == "__main__":
    row_num, col_num = map(lambda x: int(x), input().split())
    table = [[0] * col_num] * row_num
    total_sum = 0

    for r in range(row_num):
        table[r] = list(map(lambda x: int(x), input().split()))

    for r in range(row_num):
        print(" ".join([str(elem) for elem in table[r] + [sum(table[r])]]))

    for c in range(col_num):
        col_sum = sum(map(lambda x: x[c], table))
        total_sum += col_sum
        print(f"{col_sum} ", end="")
    print(f"{total_sum}")

Submission status: Accepted

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

for i in range(c+1):
    x.append(0)

for i in range(r):
    b = [int(s) for s in input().split()]
    b.append(sum(b))
    a.append(b)


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


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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
[r,c]=map(int,input().split())
matrix=[[]for i in range(r)]
r_total=[0 for i in range(r)]
c_total=[0 for i in range(c)]
total=0

for i in range(r):
    matrix[i]=list(map(int,input().split()))
    for j in range(c):
        r_total[i]+=matrix[i][j]
        c_total[j]+=matrix[i][j]
        total+=matrix[i][j]
for i in range(r):
    for j in range(c):
        print(matrix[i][j],end = " ")
    print(r_total[i])
for i in range(c):
    print(c_total[i],end=" ")
print(total)

Submission status: Accepted

Solution code:
r, c = input().split()
r = int(r) # 行数
c = int(c) # 列数
list = [[0] * (c + 1) for i in range(r + 1)]
for i in range(r):
    l = input().split()
    for j in range(c):
        list[i][j] = int(l[j])
        list[i][c] += list[i][j]
        list[r][j] += list[i][j]
    list[r][c] += list[i][c]

# 結果出力
for i in range(r + 1):
    for j in range(c + 1):
        if j != c:
            print("%d " %list[i][j], end="")
        else:
            print(list[i][j])

Submission status: Accepted

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

total = 0
c_sum = [0 for i in range(c)]
for i in range(r):
    e = list(map(int, input().split()))
    total += sum(e)
    r_sum = 0
    for j in range(c):
        print(e[j], end=' ')
        r_sum += e[j]
        c_sum[j] += e[j]
    print(r_sum)
for i in range(c):
    print(c_sum[i], end=' ')
print(total)

Submission status: Accepted

Solution code:
r, c = map(int,input().split())
cells = []
for _ in range(r):
    li = list(map(int,input().split()))
    line_sum = sum(li)
    li.append(line_sum)
    cells.append(li)

sums = []
for i in range(c+1):
    s = 0
    for j in range(r):
        s += cells[j][i]
    sums.append(s)
cells.append(sums)

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

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:
#import pysnooper

#@pysnooper.snoop()
def main():
    r,c=map(int,input().split())
    a=[list(map(int,input().split())) for _ in range(r)]
    c1=[0 for _ in range(c)]
    s=0

    for i in range(r):
        print(*a[i],sum(a[i]))
        s+=sum(a[i])
        for j in range(c):
            c1[j]+=a[i][j]
    print(*c1,s)

main()

Submission status: Accepted

Solution code:
R,C = map(int,input().split())
table = [[0]*(C+1) for i in range(R+1)]
#table

for row in range(R):
  data = list(map(int,input().split()))
  for col in range(C):
    table[row][col]=data[col]
#table

for row in range(R):
  for col in range(C):
    table[row][C] += table[row][col]
    table[R][col] += table[row][col]
    table[R][C] += table[row][col]

for row in range(0,R+1):
  for col in range(0,C+1):
    if col==C:
      print("{:d}".format(table[row][col]))
    else:
      print("{:d}".format(table[row][col]),end=" ")

Submission status: Accepted

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

tmp = [0] * c
for i in range(r):
    print(' '.join(map(str, l[i])) + f' {sum(l[i])}')
    tmp = [a + b for a, b in zip(tmp, l[i])]

print(' '.join(map(str, tmp)) + f' {sum(tmp)}')

Submission status: Accepted

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

for i in range(r):
	row[i].append(sum(row[i]))

row.append([])


for j in range(c+1):
	sumc=0
	for i in range(r):
		sumc += row[i][j]
	row[r].append(sumc)
	
for i in range(r+1):
	print(" ".join(map(str,row[i])))

Submission status: Accepted

Solution code:
row, col = list(map(int, input().split()))
rc = []

for i in range(row):
    tmp_r = list(map(int, input().split()))
    tmp_sum = sum(tmp_r)
    rc.append(tmp_r)
    rc[i].append(tmp_sum)

new_row = []
for c in range(col+1):
    tmp = 0
    for r in range(row):
        tmp += rc[r][c]
    new_row.append(tmp)

rc.append(new_row)

for row_list in rc:
    for i, e in enumerate(row_list):
        if i == len(row_list)-1:
            print(e)
        else:
            print(e, end=" ")

Submission status: Accepted

Solution code:
r, c = map(int, input().split())
 
mat = [[0 for i in range(c+1)] for j in range(r+1)]
for i in range(r):
    mat[i] = list(map(int, input().split()))
 
for i in range(r):
    s = sum(mat[i])
    mat[i].append(s)
 
for j in range(c):
    s = 0
    for i in range(r):
        s += mat[i][j]
    mat[r][j] = s
 
mat[r][c] = (sum(mat[r]))
 
for i in range(r+1):
    for j in range(c+1):
        if j == c:
            print(str(mat[i][j]))
        else:
            print(str(mat[i][j]) + " ", end="")

Submission status: Accepted

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

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

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

for i in range(0, r):
  for j in range(0, c):
    B[i][j] = A[i][j]

for i in range(0,r):
  for j in range(0, c):
    B[i][c] += A[i][j] 

for i in range(0, c+1):
  for j in range(0, r):
    B[r][i] += B[j][i]
    
for i in range(0, r+1):
  print(*B[i])

Submission status: Accepted

Solution code:
#ITP1_7_C
r,c=map(int,input().split())
A=[[0 for i in range(c)]for j in range(r)]
for i in range(r):
  D=list(map(int,input().split()))
  for j in range(c):
    A[i][j]=D[j]
B=[[0 for i in range(c+1)]for j in range(r+1)]
for i in range(r):
  for j in range(c):
    B[i][j]=A[i][j]
for i in range(r):
  a=int(0)
  for j in range(c):
    a=a+A[i][j]
  B[i][j+1]=a
for i in range(c):
  b=int(0)
  for j in range(r):
    b=b+A[j][i]
  B[j+1][i]=b
  
f=int(0)
for j in range(c):
  f=f+B[r][j]
B[r][c]=f
for i in range(r+1):
  for j in range(c+1):
    print(B[i][j],end="")
    if j != c:
      print(" ",end="")
  print("")

Submission status: Accepted

Solution code:
lmap = lambda f,s: list(map(f,s))
n,m = map(int, input().split())
a = [lmap(int, input().split()) for i in range(n)]
a = lmap(lambda xs: xs + [sum(xs)], a)
a.append(lmap(sum, zip(*a)))
for xs in a: print(*xs)

Submission status: Accepted

Solution code:
r, c = map(int, input().split())   #r, cを読み込む
l = [[0 for i in range(c + 1)] for i in range(r + 1)]
#0 for i in range(n)   n個の要素を0で初期化
for i in range(r) :
 l[i] = list(map(int, input().split()))   #行ごとに読み込み
 
for i in range(r) :
 s = sum(l[i])   #行ごとの和を求める
 l[i].append(s)   #sをi行目の最後に追加
 
for j in range(c) :   #列ごとの和を求める 横に1つずつ動く
 s = 0
 for i in range(r) :   #1行ずつ進む
   s += l[i][j]
 l[r][j] = s   #0行目から始まってる→r(r+1)行目にc列目の和
 
l[r][c] = sum(l[r])   #l[r][c] = l[r]の和
 
for i in range(r + 1) :   #range→(r+1)回
 for j in range(c + 1) :
   if(j == c) :
     print(l[i][j])
   else :
     print(str(l[i][j]) + " ", end = "")

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())))
    table[i].append(sum(table[i]))

table.append([])
for j in range(c+1):
    sum = 0
    for i in range(r):
        sum += table[i][j]
    table[r].append(sum)

for i in range(r+1):
    print("%d"%table[i][0],end = "")
    for j in range(1, c+1):
        print(" %d"%table[i][j],end = "")
    print("")

Submission status: Accepted

Solution code:
r,c = map(int,input().split())
data = []
for i in range(r):
    data.append(list(map(int,input().split())))
data.append([0] * c)

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

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

for i in range(r+1):
    print(" ".join(map(str,data[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

###
### for atcorder program
###

import sys
import math
import array

# math class
class mymath:
    ### pi
    pi = 3.14159265358979323846264338

    ### Prime Number
    def pnum_eratosthenes(self, n):
        ptable = [0 for i in range(n+1)]
        plist = []

        for i in range(2, n+1):
            if ptable[i]==0:
                plist.append(i)
                for j in range(i+i, n+1, i):
                    ptable[j] = 1
        return plist

    ### GCD
    def gcd(self, a, b):
        if b == 0:
            return a
        return self.gcd(b, a%b)

    ### LCM
    def lcm(self, a, b):
        return (a*b)//self.gcd(a,b)

    ### Mat Multiplication
    def mul(self, A, B):
        ans = []
        for a in A:
            c = 0
            for j, row in enumerate(a):
                c += row*B[j]
            ans.append(c)
        return ans

mymath = mymath()

### output class
class output:
    ### list
    def list(self, l):
        l = list(l)
        #print(" ", end="")
        for i, num in enumerate(l):
            print(num, end="")
            if i != len(l)-1:
                print(" ", end="")
        print()

output = output()

### input sample
#i = input()
#A, B, C = [x for x in input().split()]
#N, K = [int(x) for x in input().split()]
#inlist = [int(w) for w in input().split()]
#R = float(input())
#A = [int(x) for x in input().split()]
#for line in sys.stdin.readlines():
#    x, y = [int(temp) for temp in line.split()]

### output sample
#print("{0} {1} {2:.5f}".format(A//B, A%B, A/B))
#print("{0:.6f} {1:.6f}".format(R*R*math.pi,R*2*math.pi))
#print(" {}".format(i), end="")

def main():
    r, c = [int(x) for x in input().split()]

    A = []
    for i in range(r):
        A.append([int(x) for x in input().split()])

    for i in A:
        i.append(sum(i))
    
    a = []
    for i in range(c):
        temp = 0
        for j in range(r):
            temp += A[j][i]
        a.append(temp)
    a.append(sum(a))

    A.append(a)

    for i in A:
        output.list(i)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

for j in range(r):

    for cnt, element in enumerate(list(map(int, input().split()))):
        matrix[j][cnt] = element


# CALC
for i in range(c):
    
    sum_cow = 0
    
    for j in range(r):

        sum_cow += matrix[j][i]

    matrix[r][i] = sum_cow


for j in range(r + 1):

    matrix[j][c] = sum(matrix[j])


# OUTPUT
for j in range(0, r + 1):
    print(f"{matrix[j][0]}", end="")
    
    for i in range(1, c + 1):
        print(f" {matrix[j][i]}", end="")

    print()

Submission status: Accepted

Solution code:
r, c = map(int, input().split())
data = [list(map(int, input().split())) for i in range(r)]
for x in data:
    x.append(sum(x))
    print(' '.join(map(str, x)))
else:
    subdata = [sum(data[i][j] for i in range(r)) for j in range(c)]
    subdata.append(sum(subdata))
    print(' '.join(
        map(str, subdata)
    ))

Submission status: Accepted

Solution code:
r, c = map(int, input().split())   #r, cを読み込む
l = [[0 for i in range(c + 1)] for i in range(r + 1)]
#0 for i in range(n)   n個の要素を0で初期化
for i in range(r) :
  l[i] = list(map(int, input().split()))   #行ごとに読み込み
    
for i in range(r) :
  s = sum(l[i])   #行ごとの和を求める
  l[i].append(s)   #sをi行目の最後に追加
    
for j in range(c) :   #列ごとの和を求める 横に1つずつ動く
  s = 0
  for i in range(r) :   #1行ずつ進む
    s += l[i][j]
  l[r][j] = s   #0行目から始まってる→r(r+1)行目にc列目の和
    
l[r][c] = sum(l[r])   #l[r][c] = l[r]の和
  
for i in range(r + 1) :   #range→(r+1)回
  for j in range(c + 1) :
    if(j == c) :
      print(l[i][j])
    else :
      print(str(l[i][j]) + " ", end = "")

Submission status: Accepted

Solution code:
r, c = map(int, input().split())   #r, cを読み込む
l = [[0 for i in range(c + 1)] for i in range(r + 1)]
#0 for i in range(n)   n個の要素を0で初期化
for i in range(r) :
  l[i] = list(map(int, input().split()))   #行ごとに読み込み
   
for i in range(r) :
  s = sum(l[i])   #行ごとの和を求める
  l[i].append(s)   #sをi行目の最後に追加
   
for j in range(c) :   #列ごとの和を求める 横に1つずつ動く
  s = 0
  for i in range(r) :   #1行ずつ進む
    s += l[i][j]
  l[r][j] = s   #0行目から始まってる→r(r+1)行目にc列目の和
   
l[r][c] = sum(l[r])   #l[r][c] = l[r]の和
 
for i in range(r + 1) :   #range→(r+1)回
  for j in range(c + 1) :
    if(j == c) :
      print(l[i][j])
    else :
      print(str(l[i][j]) + " ", end = "")

Submission status: Accepted

Solution code:
r, c = map(int, input().split())   #r, cを読み込む
l = [[0 for i in range(c + 1)] for i in range(r + 1)]
#0 for i in range(n)   n個の要素を0で初期化
for i in range(r) :
 l[i] = list(map(int, input().split()))   #行ごとに読み込み

for i in range(r) :
 s = sum(l[i])   #行ごとの和を求める
 l[i].append(s)   #sをi行目の最後に追加

for j in range(c) :   #列ごとの和を求める 横に1つずつ動く
 s = 0
 for i in range(r) :   #1行ずつ進む
   s += l[i][j]
 l[r][j] = s   #0行目から始まってる→r(r+1)行目にc列目の和

l[r][c] = sum(l[r])   #l[r][c] = l[r]の和

for i in range(r + 1) :   #range→(r+1)回
 for j in range(c + 1) :
   if(j == c) :
     print(l[i][j])
   else :
     print(str(l[i][j]) + " ", end = "")

Submission status: Accepted

Solution code:
RC = []
r, c = [int(e)for e in input().split()]
sum_col = [0]*(c+1)

for i in range(r):
    R = [int(e)for e in input().split()]
    R.append(sum(R))
    RC.append(R)
    for j in range(c+1):
        sum_col[j] += RC[i][j]

RC.append(sum_col)

for x in range(len(RC)):
    print(" ".join(map(str, RC[x])))

Submission status: Accepted

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

# 入力文字列を取得
for i in range(r):
    columns[i] = list(map(int, input().split()))

# 行の合計を各列のお尻につける
for i in range(r):
    s = sum(columns[i])
    columns[i].append(s)

# 列の合計をつくって一番下につくる
for j in range(c):
    s = 0
    for i in range(r):
        s += columns[i][j]
    columns[r][j] = s

# 行の合計の合計    
columns[r][c] = (sum(columns[r]))

# すべてを表示
for i in range(r+1):
    for j in range(c+1):
        if j == c:
            print(str(columns[i][j]))
        else:
            print(str(columns[i][j]) + " ", end="")

Submission status: Accepted

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

matrix = [[0] * (c + 1) for _ in range(r + 1)]

for i in range(r):
    row = [int(c) for c in input().split()]
    for j in range(c):
        matrix[i][j] = row[j]
    matrix[i][-1] = sum(row)

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

for row in matrix:
    print(' '.join([str(i) for i in row]))

Submission status: Accepted

Solution code:
r,c=map(int,input().split())
R=list()
L=list()
M=list()
sum=0
for i in range(r):
    R=list(map(int,input().split()))
    L.append(R)
for j in range(r):
    for k in range(c):
        sum+=L[j][k]
    L[j].append(sum)
    sum=0
for l in range(c):
    for m in range(r):
        sum+=L[m][l]
    M.append(sum)
    sum=0
for n in range(c):
    sum+=M[n]
M.append(sum)
L.append(M)
for o in range(r+1):
    print(" ".join(map(str,L[o])))

Submission status: Accepted

Solution code:
H,W = map(int,input().split())

table = [[0]*(W+1) for i in range(H+1)]


for row in range(H):
    work = list(map(int,input().split()))
    for col in range(W):
        table[row][col] = work[col]


for row in range(H):
    for col in range(W):
        table[row][W] += table[row][col]


for col in range(W+1):
    for row in range(H):
        table[H][col] += table[row][col]

for row in range(H+1):
    for col in range(W+1):
        if(col == W):
            print("{}".format(table[row][col]),end="\n")
        else:
            print("{}".format(table[row][col]),end=" ")

Submission status: Accepted

Solution code:
if __name__ == "__main__":
    # test()
    res = []
    r, c = map(int, input().split())
    for _ in range(r):
        res.append(list(map(int, input().split())))

    last_row = []

    #縦に足す
    for i in range(c):
        sum_of_i = 0
        for e in res:
            sum_of_i += e[i]
        last_row.append(sum_of_i)
    res.append(last_row)

    #横に足す
    for e in res:
        e.append(sum(e))

    #結果出力
    for r in res:
        print(*r)

Submission status: Accepted

Solution code:
r,c = [int(x) for x in input().split()]
       
a = [[0 for i in range(c)] for j in range(r)] #c:列 r:行
for i in range(r):
    a[i] = [int(x) for x in input().split()]


b = [[0 for row in range(c+1)]for line in range(r+1)]
for i in range(r):
    for j in range(c):
            b[i][j] = a[i][j]
            
for i in range(r):
    b[i][c] = sum(b[i])

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

for i in range (r + 1):
    print(" ".join(map(str,b[i])))

Submission status: Accepted

Solution code:
if __name__ == '__main__':
	n,m = map(int,input().split())
	d = []
	for i in range(n):
		d.append([int(s) for s in input().split()])
		sum = 0
		for k in range(m):
			sum += d[i][k]
		d[i].append(sum)
	
	for i in range(n):
		for k in range(m+1):
			if k == m:
				print("%d" % d[i][k])
			else:
				print("%d" % d[i][k], end=" ")
	for i in range(m+1):
		sum = 0
		for k in range(n):
			sum += d[k][i]
		if i == m:
			print("%d" % sum)
		else:
			print("%d" % sum, end=" ")

Submission status: Accepted

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

# initialize matrix
matrixA = []

# compute sum of row
for _ in range(r):
    row_list = list(map(int, input().split()))
    row_list.append(sum(row_list))
    matrixA.append(row_list)

# compute sum of column
col_sum = []
for i in range(c+1):
    tmp_sum = 0
    for j in range(r):
        tmp_sum += matrixA[j][i]
    col_sum.append(tmp_sum)

# add sum row
matrixA.append(col_sum)

# output
for k in range(r+1):
    for l in range(c+1):
        print(matrixA[k][l], end='')
        if l < c:
            print(' ', end='')
    print('')

Submission status: Accepted

Solution code:
r, c = map(int, input().split())
table = [[0 for j in range(c+1)] for i in range(r+1)]
for i in range(r):
  a = list(map(int, input().split()))
  sumr = 0
  for j in range(c):
    table[i][j] = a[j]
    sumr += a[j]
  table[i][c] = sumr
for j in range(c+1):
  sumc = 0
  for i in range(r):
    sumc += table[i][j]
  table[r][j] = sumc
for i in range(r+1):
  for j in range(c+1):
    if j == 0:
      print(table[i][j], end = "")
    else:
      print("", table[i][j], end = "")
  print()

Submission status: Accepted

Solution code:
numbers = list(map(int, input().split(" ")))

arr_a = []
arr_b = []
matrix = []

for i in range(numbers[0]):
    arr_a = list(map(int, input().split(" ")))
    arr_a.append(sum(arr_a))
    matrix.append(arr_a)
    
for i in range(numbers[1]):
    total = 0
    for j in range(numbers[0]):
        total += matrix[j][i]
    arr_b.append(total)

arr_b.append(sum(arr_b))
matrix.append(arr_b)

for i in range(numbers[0] + 1):
    for j in range(numbers[1] + 1):
        if j == numbers[1]:
            print(matrix[i][j])
            
        else:
            print(matrix[i][j], end="")
            print(" ", end="")

Submission status: Accepted

Solution code:
import sys
s = [0] * (int(next(sys.stdin).split()[1]) + 1)
for l in sys.stdin:
	a = list(map(int,l.split()))
	a.append(sum(a))
	print(*a)
	for i,v in enumerate(a):
		s[i] += v
print(*s)

Submission status: Accepted

Solution code:
a = []
gyou = []
retu = []
s = 0

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

for i in range(r):
    gyou.append(0)

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

for i in range(c):
        retu.append(sum(a[i]))

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

s = sum(gyou)

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

    if i == c - 1:
        print(" ".join(map(str, gyou)), s)

Submission status: Accepted

Solution code:
x = input()
list1 = x.split()
[r, c] = list1
r = int(r)
c = int(c)
list3 = []
list4 = []
for i in range(0, r):
    y = input()
    list2 = y.split()
    list2_i = [int(s) for s in list2]
    list3.append(list2_i)

for i in range(0, r):
    s = sum(list3[i])
    list3[i].append(s)

for p in range(0, c):
    z = 0
    
    for q in range (0, r):
        z = z + list3[q][p]
     
    list4.append(z)

s = sum(list4)
list4.append(s)

list3.append(list4)

for i in range(0, r + 1):
    list3[i] = [str(s) for s in list3[i]]

for i in range(0, r + 1):
    mojiretsu = " ".join(list3[i])
    print(mojiretsu)

Submission status: Accepted

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

table =[[] for i in range(r) ]
Csum = [0 for j in range(c+1)] #ここのインデックスを決める意味は？

for i in range(r):
	Tbls = input()
	table[i] = list(map(int,Tbls.split()))
	#print(Tbls, end = '')
	#print(' ', sum(table[i]), sep = ' ')
	print(Tbls,sum(table[i]),sep=' ')
	
	for j in range(c):
		Csum[j] += table[i][j]
		
	Csum[c] += sum(table[i])
	
print(' '.join(map(str,Csum)))

Submission status: Accepted

Solution code:
nums = list(map(int, input().split()))
table = []
for i in range(nums[0]):
    table.append(list(map(int, input().split())))
    table[i].append(sum(table[i][:]))
table.append([])
for i in range(nums[1]+1):
    t = 0
    for j in range(nums[0]):
        t += table[j][i]
    table[nums[0]].append(t)
for i in range(nums[0]+1):
    for j in range(nums[1]+1):
        if j < nums[1]:
            print(table[i][j], end = ' ')
        else:
            print(table[i][j])

Submission status: Accepted

Solution code:
N, M = map(int, input().split());
A = [[] for i in range(N+1)];
B = [0] * M;
SUM = 0;
for i in range(N):
    sum = 0;
    A[i] = list(map(int, input().split()));
    for j in range(M):
        sum = sum + A[i][j];
        B[j] = B[j] + A[i][j];
        print(A[i][j], end=" ");
    print(sum);
    SUM = SUM + sum;
for k in range(M):
    print(B[k], end=" ");
print(SUM);

Submission status: Accepted

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

for k in l:
    print(*k)

Submission status: Accepted

Solution code:
sheet_size = [int(i) for i in input().split()]
rows = sheet_size[0]
columns = sheet_size[1]

spread_sheet = []

for j in range(rows):
    row =  [int(i) for i in input().split()]
    row.append(sum(row))
    spread_sheet.append(row)

additional_row = []
for c in range(columns+1):
    column_value = 0
    for row in spread_sheet:
        column_value += row[c]
    additional_row.append(column_value)

spread_sheet.append(additional_row)


for row in spread_sheet:
    str_row = list(map(str, row))
    print(' '.join(str_row))

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)]
I = [0 for i in range(c+1)]
sum_c = 0
sum_r = 0

for a in range(r):
    S = input().strip().split()
    S = [int(s) for s in S]
    sum_c = sum(S)
    for b in range(c):
        A[a][b] = S[b]
        print(S[b],end=' ')
    A[a][c] = sum_c
    print(sum_c)
    
for i in range(c+1):
    for j in range(r):
        I[i] += A[j][i]
        if i == c and j == r-1:
            print(I[i])
        elif j == r-1:
            print(I[i],end=' ')

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
 
table = [[0]*(w+1) for i in range(h+1)]
 
for row in range(h):
    work = list(map(int,input().split()))
    for col in range(w):
        table[row][col] = work[col]
 
 
for row in range(h):
    for col in range(0,w):
        table[row][w] += table[row][col]
 
 
for col in range(0,w+1):
    for row in range(0,h):
        table[h][col] += table[row][col]
 
 
for row in range(0,h+1):
    print("%d"%(table[row][0]),end="")
    for col in range(1,w+1):
        print(" %d"%(table[row][col]),end = "")
    print()

Submission status: Accepted

Solution code:
n, m = map(int, input().split())

A = [[0]*(m+1) for i in range(n+1)]

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

for j in range(m+1):
    for i in range(n):
        A[n][j] += A[i][j]
        
for i in range(n+1):
    print(A[i][0], end='')
    for j in range(1, m+1):
        print(' %d'%A[i][j], end='')
    print()