given a matrix of M rows & N columns, you need to find the smallest number in each row & print the sum of the smallest numbers from all rows.
explanation: M=3. given matrix is
1 2 3
4 5 6
7 8 9
the minimum elements of each row 1, 4, 7 => sum is 1+4+7=12
I/p: 3 3
1 2 3
4 5 6
7 8 9
O/p: 12
I/p: 2 3
0 12 -13
-10 0 10
O/p: -23
def readMat():
line = input()
m, n = [int(s) for s in line.split()]
A = []
for i in range(m):
line = input()
row = [int(s) for s in line.split()]
A.append(row)
return A
def calc(A):
s = 0
for row in A:
s += min(row)
return s
def main():
A = readMat()
s = calc(A)
print(s)
if __name__ == '__main__':
main()
Comments
Leave a comment