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())
matrix = []
for i in range(r):
    inp = list(map(int,input().split()))
    matrix.append(inp)

for row in matrix:
    c_sum = 0
    for col in row:
        c_sum += col
    row.append(c_sum)

r_sum_list =[]
for col in range(c+1):
    r_sum = 0
    for row in range(r):
        r_sum += matrix[row][col]
    r_sum_list.append(r_sum)
matrix.append(r_sum_list)

for i in matrix:
    map_result = map(str,i)
    result = " ".join(map_result)
    print(result)

Submission status: Accepted

Solution code:
n,m = map(int, input().split())
l = [list(map(int, input().split())) for _ in range(n)]
r = [0]*(m+1)
for i in l:
    i.append(sum(i))
for j in range(m+1):
    r[j] = sum([x[j] for x in l])
l.append(r)
for i in range(n+1):
    print(" ".join(map(str,l[i])))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m = map(int, input().split())
A = [[]for i in range(n)]
b = [0 for i in range(m)]

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

for i in range(m):
    for j in range(n):
        b[i] += A[j][i]

b.append(sum(b))

for i in range(n):
    print(' '.join(map(str, A[i])))
print(' '.join(map(str, b)))

Submission status: Accepted

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

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

for i in range(r):
    buff = input()
    lst = [int(x) for x in buff.split()]
    for j in range(c):
        ss[i][j] = lst[j]
    ss[i][c] = sum(lst)
    ss[r][c] += ss[i][c]

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

for row in ss:
    print(" ".join(map(str, row)))

Submission status: Accepted

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

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

for i in range(n):
  data = input().split()

  for j in range(m):
    A[i][j] = int(data[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])

Submission status: Accepted

Solution code:
# r, c = map(int, input().split())
# elements = []
# total = 0

# for i in range(r):
#     element = list(map(int, input().split()))
#     elements.append(element)

# for i in range(r + 1):
#     for j in range(c + 1):
#         if j == c:
#             print(sum(elements[i]))
#         elif i == r:
#             for k in range(r):
#                 total += int(element[k][j])
#                 print(total, end=" ")
#         else:
#             print(elements[i], end=" ")
#     print()
# print()

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

for i in range(r + 1):
    for j in range(c + 1):
        if i == r and j == c:
            print(total)
        elif j == c:
            total += sum(elements[i])
            print(sum(elements[i]))
        elif i == r:
            total2 = 0
            for k in range(r):
                total2 += elements[k][j]
            print(total2, end=" ")
        else:
            print(elements[i][j], end=" ")

Submission status: Accepted

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

for i in range(init[0]):
    row=list(map(int,input().split()))
    sum=0
    for j in range(init[1]):
        a[i][j]=row[j]
        sum+=row[j]
    a[i][init[1]]=sum


for i in range(init[1]+1):
    sum=0
    for j in range(init[0]):
        sum+=a[j][i]
    a[init[0]][i]=sum

for i in range(init[0]+1):
    for j in range(init[1]):
        print(a[i][j],end=' ')
    print(a[i][init[1]])

Submission status: Accepted

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

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


for i in range(r):
    s=sum(rc[i])
    rc[i].append(s)


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

for i in range(r+1):
    print(*rc[i])

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())) #★★直にtableに代入すると、列数が変わってしまう!!!★★★
    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:
r, c = map(int, input().split(' '))
sheet = [[0 for col in range(c+1)] for row in range(r+1)]
for row in range(r):
    l = list(map(int, input().split(' ')))
    for col in range(c):
        sheet[row][col] = l[col]
    
for row in range(r):
    for col in range(c):
        sheet[row][c] += sheet[row][col]
        
for col in range(c):
    for row in range(r):
        sheet[r][col] += sheet[row][col]
    
for row in range(r):
    for col in range(c):
        sheet[r][c] += sheet[row][col]

for l in sheet:
    l = list(map(str, l))
    print(' '.join(l))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
x, y =(int(a) for a in input().split())
w = [[] for j in range(x+1)]
for i in range(x):
    num = 0
    k = [int(a) for a in input().split()]
    for j in  range(len(k)):
        w[i].append(k[j])
        num += k[j]
    else:
        w[i].append(num)
for i in range(y + 1):
    e = 0
    for j in range(x):
        e += w[j][i]
    else:
        w[x].append(e)
for i in range(x+1):
    for j in range(y):
        print(w[i][j],end = " ")
    else:
        print(w[i][j+1])

Submission status: Accepted

Solution code:
a, b = map(int,input().split())
lst = []
for i in range(a):
  c = 0
  tmp = list(map(int, input().split()))
  for j in tmp:
    c += j
  tmp.append(c)
  lst.append(tmp)

last = []
for i in range(len(lst[0])):
  c = 0
  for ls in lst:
    c += ls[i]
  last.append(c)

lst.append(last)
for line in lst:
  print(' '.join(map(str, line)))

Submission status: Accepted

Solution code:
rc = input()
rc_list = rc.split(" ")
r = int(rc_list[0])
c = int(rc_list[1])
all_sum = 0
list = [[0 for l in range(c)] for m in range(r)]

for i in range(r):

    row = input()
    row_list = row.split(" ")
    tmp = 0
    row_ans = ""
    for j in range(c):
        list[i][j] = int(row_list[j])
    for k in list[i]:
        tmp += k
        row_ans += str(k) + " "
    row_ans += str(tmp)
    print(row_ans)
    all_sum += tmp

column_list = ""
for m in range(c):
    column = 0
    for n in range(r):
        column += list[n][m]
    column_list += str(column) + " "
print(column_list + str(all_sum))

Submission status: Accepted

Solution code:
r, c = map(int, input().split())
tbl = [[] for _ in range(r)]
cSum = [0 for _ in range(c+1)]

for i in range(r):
    tblS = input()
    tbl[i] = list(map(int, tblS.split()))
    print(tblS, end="")
    print(" ", sum(tbl[i]), sep="")
    
    for j in range(c):
        cSum[j] += tbl[i][j]
    cSum[c] += sum(tbl[i])
    
print(" ".join(map(str, cSum)))

Submission status: Accepted

Solution code:
# coding: utf-8
line = input().split()
r = int(line[0])
c = int(line[1])
val = [[0 for i in range(c+1)] for j in range(r+1)]
for i in range(r):
    line = input().split()
    for j in range(c):
        val[i][j] = int(line[j])

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

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

Submission status: Accepted

Solution code:
h, w = map(int, input().split())

all_total = 0
col_total = [0]*w

for _ in range(h):
    nums = list(map(int, input().split()))
    row_total = sum(nums)
    col_total = list(map(sum, zip(nums, col_total)))
    all_total += row_total
    print(' '.join(map(str, nums+[row_total])))

print(' '.join(map(str, col_total+[all_total])))

Submission status: Accepted

Solution code:
r, c = map(int, input().split())
out = [[]] * (r + 1)
sumrow = [0 for i in range(c + 1)]
for line in range(0, r):
    vals = list(map(int, input().split()))
    vals.append(sum(vals))
    for itm in vals[:-1]:
        print(itm, end = ' ')
    print(vals[-1])
    for ids in range(c + 1):
        sumrow[ids] += vals[ids]
for itm in sumrow[:-1]:
    print(itm,end = ' ')
print(sumrow[-1])

Submission status: Accepted

Solution code:
a=list(map(int,input().split()))
#initlalize 2D array
table = [[0 for i in range(a[1]+1)] for j in range(a[0]+1)]

#input
for i in range(a[0]):
    values = list(map(int,input().split()))
    for j in range(a[1]):
        table[i][j] = values[j]

#sum of a row
for i in range(a[0]):
    for j in range(a[1]):
        table[i][a[1]] += table[i][j]
#sum of a column
for i in range(a[1]+1):
    for j in range(a[0]):
        table[a[0]][i] += table[j][i]
#print
for i in range(a[0]+1):
    print(*table[i])

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:
r, c = map(int, input().split())
table = []
for x in range(r):
    tmplist = list(map(int, input().split()))
    sum = 0
    for y in tmplist:
        sum += y
    tmplist.append(sum)
    table.append(tmplist)

sumlist = []
for y in range(c + 1):
    sum = 0
    for x in range(r):
        sum += table[x][y]
    sumlist.append(sum)
table.append(sumlist)

for x in table:
    string = ""
    flag = False
    for y in x:
        if flag:
            string += " "
        else:
            flag = True
        string += str(y)
    print(string)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
r, c = map(int, input().split())
arr = []
for i in range(r):
    arr.append(list(map(int, input().split())))
 
for line in arr:
    line.append(sum(line))
     
arr.append([sum([line[i] for line in arr]) for i in range(c  + 1)])

for line in arr:
    print(*line)

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

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

finalRow = []

for col in range(c + 1):
    sumCol = 0
    for row in range(r):
        sumCol += spreadSheet[row][col]
    finalRow.append(sumCol)

spreadSheet.append(finalRow)

for row in range(r + 1):
    print(*spreadSheet[row])

Submission status: Accepted

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

Submission status: Accepted

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

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

for row in range(H):
    a = list(map(int,input().split()))
    for col in range(W):
        table[row][col]= a[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:
r, c = map(int, input().split())
mtx = [[int(i) for i in input().split()] for j in range(r)]
for m in mtx:
    m.append(sum(m))
    print(' '.join(list(map(str, m))))
print(' '.join(list(map(str, [sum([j[i] for j in mtx]) for i in range(c + 1)]))))

Submission status: Accepted

Solution code:
r,c=map(int,input().split())
a=[];s=[0]*(c+1)
for _ in [0]*r:a+=[list(map(int,input().split()))];a[-1]+=[sum(a[-1])];s=[x+y for x,y in zip(s,a[-1])]
for b in a+[s]:print(*b)

Submission status: Accepted

Solution code:
r, c = map(int, input().split())
li = []
for _ in range(r):
    li.append(list(map(int, input().split())))
for i in range(r):
    li[i].append(sum(li[i]))
    print(" ".join(map(str, li[i])))
li.append([])
for i in range(c):
    li[-1].append(sum([li[j][i] for j in range(r)]))
li[-1].append(sum(li[-1]))
print(" ".join(map(str, li[-1])))

Submission status: Accepted

Solution code:
num1, num2 = map(int, input().split())

matrix = []

for i in range(num1):
    rows = list(map(int, input().split()))
    sum_rows = sum(rows)
    rows.append(sum_rows)
    matrix.append(rows)

#print(matrix[0][5])

row = []
for j in range(num2 + 1):
    sum_columns = 0
    for k in range(num1):
        sum_columns += matrix[k][j]
    row.append(sum_columns)

matrix.append(row)

for row in matrix:
    print(*row)

Submission status: Accepted

Solution code:
r,c = [int(i) for i in input().split()]
mat = [0 for i in range(r+1)]
for i in range(r):
    mat[i] = [int(i) for i in input().split()]
for i in range(r):
    mat[i].append(sum(mat[i]))
for j in range(c):
    mat[r] = [sum([mat[i][j] for i in range(r)]) for j in range(c+1)]
for i in range(r+1):
    print(*mat[i],sep=" ")

Submission status: Accepted

Solution code:
h,w=map(int, input().split())
a=[list(map(int, input().split())) for i in range(h)]
for i in range(h):
    a[i].append(sum(a[i]))
b=[]
for i in range(w+1):
    s=0
    for j in range(h):
        s+=a[j][i]
    b.append(s)
for i in a:print(*i)
print(*b)

Submission status: Accepted

Solution code:
import copy

def print_tab(tab):
    for row in tab:
        print(" ".join(map(str, row)))

def tab_calc(tab):
    for row in tab:
        row.append(sum(row))
    last_row = []
    for i in range(len(tab[0])):
        last_row.append(sum((r[i] for r in tab)))
    tab.append(last_row)

if __name__ == '__main__':
    r, c = [int(s) for s in input().split()]
    tab = []
    for _ in range(r):
        row = [int(s) for s in input().split()]
        assert len(row) == c
        tab.append(row)
    tab_calc(tab)
    print_tab(tab)

Submission status: Accepted

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

Submission status: Accepted

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

r = int(r)
c1 = int(c)+1

table = [[0]*c1 for i in range(r+1)]


num = []

for a in range(r):
    
    num = input().split()

    for b in range(c1):

        if b != c1-1:
            table[a][b] = int(num[b])

        else:
            table[a][b] = sum(table[a])
    
    
for C in range(c1):
    total = 0
    for d in range(r):
        total += table[d][C]

    table[r][C] = total
        
for e in range(r+1):
    print(" ".join(map(str,table[e])))

Submission status: Accepted

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

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

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

for i in range(len(L)) :
    for j in range(len(L[i])) :
        L[i][j] = str(L[i][j])
for i in range(len(L)) :
    ans = " ".join(L[i])
    print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
# coding: Shift_JIS
# coding: EUC-JP
# coding: cp932
import math

r,c = map(int,input().split())
ans = [[0 for j in range(c+1)] for i in range(r+1)]
for i in range(r):
    sum = 0
    li = list(map(int,input().split()))
    for j in range(c):
        ans[i][j] = li[j]
        ans[r][j] += li[j]
        sum += li[j]
    ans[i][c] = sum
    ans[r][c] += sum

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

Submission status: Accepted

Solution code:
import sys

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

col_sums = [0] * (c + 1)

for _ in range(r):
    line = sys.stdin.readline().strip()
    sum = 0
    for col, val in enumerate(map(int, line.split())):
        col_sums[col] += val
        sum += val
    col_sums[c] += sum
    print(f'{line} {sum}')

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

Submission status: Accepted

Solution code:
line = input()
num_lst = list(map(int, line.split()))
r = num_lst[0] #4
c = num_lst[1] #5
#print(r, c)
array = []
last_row = []
for i in range(r):
    line = input()
    lst = list(map(int, line.split()))
    #print(line)
    num = sum(lst)
    lst.append(num)
    array.append(lst)
#print(array)

s = 0
for i in range(c+1):
    for j in range(r):
        s += array[j][i]
    last_row.append(s)
    s = 0
#print(last_row)
array.append(last_row)
#print(array)
for line in array:
    t = " ".join([str(n) for n in line])
    print(t)

Submission status: Accepted

Solution code:
r, c = map(int, input().split())
sheet = [[0 for x in range(c+1)] for y in range(r+1)]
for j in range(r):
    for i,x in enumerate(list(map(int, input().split()))):
        sheet[j][i] = x
        sheet[-1][i] += x
    sheet[j][-1] = sum(sheet[j][:-1])

sheet[-1][-1] = sum(sheet[-1][:-1])
print('\n'.join([' '.join(map(str,x)) for x in sheet]))

Submission status: Accepted

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

for i in range(r):
    l.append([int(j) for j in input().split()])
    l[i].append(sum(l[i]))

l.append([sum(k) for k in zip(*l)])

for v in l:
    print(*v)

Submission status: Accepted

Solution code:
def main():
	row, column = (int(a) for a in input().split())
	sheet = []
	for i in range(row):
		c = []
		for m in input().split():
			c.append(int(m))
		sheet.insert(i, c)
	for i in range(row):
		sheet[i].append(sum(sheet[i]))
		string = map(str, sheet[i])
		printColumn(sheet[i])
	l = []
	for m in range(column + 1):
		s = 0
		for r in range(row):
			s += sheet[r][m]
		l.append(s)
	printColumn(l)

def printColumn(list):
	string = map(str, list)
	print(' '.join(string))

main()

Submission status: Accepted

Solution code:
rc=list(map(int,input().split()))
r=rc[0]
c=rc[1]

data=[]
data_retu=[]

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

data_retu=[[data[i][j] for i in range(r)] for j in range(c)]
retu_sum=[sum(data_retu[i]) for i in range(c)]
data.append(retu_sum)
for i in range(r+1):
    for j in range(c):
        print(str(data[i][j])+" ",end="")
    print(sum(data[i]))

Submission status: Accepted

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

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

for i in range(r):
  r_sum = 0
  for j in range(c):
    r_sum += table[i][j]
  table[i][c] = r_sum

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

for i in range(r+1):
  print(*table[i])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from functools import reduce

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

l = []
for i in range(0,r):
    t = list(map(int,input().split()))
    t.append(reduce(lambda a,b:a+b,t))
    l.append(t)

l2 = []
for i in list(map(list, zip(*l))):
    l2.append(reduce(lambda a,b:a+b,i))
l.append(l2)

for i in range(0,r+1):
    print (' '.join(map(str,l[i])))

Submission status: Accepted

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

l2=[]
for i in range(c):
    l1=[]
    for j in range(r):
        l1.append(l[j][i])
    l2.append(l1)
l3=[sum(l2[i]) for i in range(c)]

l.append(l3)
for i in range(r+1):
    l[i].append(sum(l[i]))
    print(" ".join(map(str,l[i])))

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())
sum_list = [0]*(c+1)
for i in range(r):
    r_i = list(map(int, input().split()))
    r_i.append(sum(r_i))
    sum_list = [a+b for a, b in zip(r_i, sum_list)]
    print(" ".join([str(k) for k in r_i]))
print(" ".join([str(k) for k in sum_list]))

Submission status: Accepted

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

col_sum = [0 for i in range(c)]
for i in range(r):
    b = input()
    list = b.split(' ')
    
    row_sum = 0
    sum_str = ''
    for j in range(c):
        row_sum += int(list[j])
        sum_str = sum_str + list[j] + ' '
        col_sum[j] += int(list[j])
    sum_str = sum_str + str(row_sum)
    print(sum_str)
    
sum = 0
sum_str = ''
for i in range(c):
    sum += col_sum[i]
    sum_str = sum_str + str(col_sum[i]) + ' '
sum_str = sum_str + str(sum)
print(sum_str)

Submission status: Accepted

Solution code:
(row, col) = map(int,input().split())
matrix=[0]*row
last = [0]*(col+1)
for r in range(row):
    line = list(map(int, input().split()))
    line.append(sum(line))
    print(*line, sep=" ")
    for j in range(col+1):
        last[j]+=line[j]
        
#print(output)
print(*last, sep=" ")

Submission status: Accepted

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

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

col_sum_list = [0]*(c+1)
for i in range(c+1):
    for j in range(r):
        col_sum_list[i] += num_list[j][i]

num_list.append(col_sum_list)
for num in num_list:
    ans = [str(chrs) for chrs in num]
    print(' '.join(ans))

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
A=[[0]*m for i in range(0,n)]
for i in range(0,n):
    A[i]=list(map(int,input().split()))
for i in range(0,n):
    sum=0
    for j in range(0,m):
        sum+=A[i][j]
    A[i].append(sum)
x=[0]*(m+1)
total=0
for j in range(0,m):
    sum=0
    for i in range(0,n):
        sum+=A[i][j]
    x[j]=sum
    total+=sum
x[m]=total
A.append(x)
for i in range(0,n+1):
    print(' '.join(map(str,A[i])))

Submission status: Accepted

Solution code:
row, col = map(int, input().split())
li = [list(map(int, input().split())) for i in range(row)]
last_row = []

for i in range(col):
    s = 0
    for j in range(row):
        s += li[j][i]
    last_row.append(s)

for r in range(row):
    print(" ".join(map(str, li[r])), sum(li[r]))
print(" ".join(map(str, last_row)), sum(last_row))

Submission status: Accepted

Solution code:
r, c = map(int, input().split())
t = [list(map(int, input().split())) for i in range(r)]
t.append([0]*c)
for i in t:
    i.append(sum(i))
for co in range(c+1):
    la = 0
    for ro in range(r):
        la += t[ro][co]
    t[r][co] = la
for l in t:
    print(*l)

Submission status: Accepted

Solution code:
import sys
 
BIG_NUM = 2000000000
MOD = 1000000007
EPS = 0.000000001
 
 
 
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())) #★★直にtableに代入すると、列数が変わってしまう!!!
    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:
r, c = map(int, input().split())
cells = [list(map(int, input().split())) for _ in range(r)]

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

for c in cells:
    print(' '.join(map(str, c)))

Submission status: Accepted

Solution code:
num=list(map(int,input().split()))
sheet=[[0 for i in range(num[1])]for j in range(num[0]+1)]
for k in range(num[0]):
	sum=0
	kazu=list(map(int,input().split()))
	for l in range(num[1]):
		sheet[k][l]=kazu[l]
		sum+=kazu[l]
	sheet[k].append(sum)	
sum_ma=0
for m in range(num[1]):
	sum_b=0
	for n in range(num[0]):
		sum_b+=sheet[n][m]
	sheet[num[0]][m]=sum_b
	sum_ma+=sheet[num[0]][m]
sheet[num[0]].append(sum_ma)
for o in range(num[0]+1):
	for p in range(num[1]+1):
		print(sheet[o][p],end='')
		if p==(num[1]):
			print('')
		else:
			print(' ',end='')

Submission status: Accepted

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


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

# 最後に各列の合計を書き加えるための行を追加。
ary.append([0]*c)

# 合計を出すための一時データとして変数tmpを初期化
tmp = 0

# 各列の合計を求めて、最終行に加えた数値を書き換える
for i2 in range(c):
    for i1 in range(r):
        tmp += ary[i1][i2]
    ary[i1+1][i2] = tmp
    tmp = 0


# 各行の行末に、各行の合計をappendで追加

for i1 in range(r+1):
    for i2 in range(c):
        tmp += ary[i1][i2]
    ary[i1].append(tmp)
    tmp = 0

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

Submission status: Accepted

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

for i in range(0, r):
    temp = input().split()
    temp.append(0)
    A.append(temp)

col_sum = []
for i in range(0 , c + 1):
    col_sum.append(0)

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

A.append(col_sum)

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

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

# ITP1_7_C

y,x=map(int,input().split())

M=list()
for i in range(y):
    R=list(map(int,input().split()))
    M.append(R)

for m in M:
    m.append(sum(m))

T=list()
for k in range(x+1):
    sum=0
    for j in range(y):
        sum+=M[j][k]
        
    # print(sum)
    T.append(sum)

M.append(T)
    
# print(M)

for s in range(y+1):
    print(" ".join(list(map(str,M[s]))))

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(r):
    table[i] = list(map(int, input().split()))
    table[i].append(0)
    table[i][c] = sum(table[i])

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

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

Submission status: Accepted

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

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

column = [list(ary) for ary in zip(*array)]
row = [sum(col) for col in column]
array.append(row)

for ary in array:
    print(' '.join(map(str, ary)))

Submission status: Accepted

Solution code:
a, b = map(int, input().split())
L = []
for i in range(a):
    A = list(map(int, input().split()))
    L.append(A)

for i in range(a):
    X = 0
    for k in range(b):
        X = X +L[i][k]
    L[i].append(X)

l = []
for k in range(b):
    X = 0
    for i in range(a):
        X = X +L[i][k]
    l.append(X)

Sum = sum(l)
for i in range(a):
    Sum = Sum + L[i][b]
l.append(Sum//2)

L.append(l)
for i in range(a+1):
    word = str(L[i][0])
    for k in range(1,b+1):
        if k < b+1:
            word = word +" "+str(L[i][k])
        else:
            word = word + str(L[i][k])
    print(word)