Sum of rows and columns
Given an matrix, our task is to find the sum of all the elements in each row, as well as the sum of all the elements in each column of that matrix.
The first line of the input contains a single integer
n
. The next n
lines contain n
integers which represent the matrix.The output should contain 2 rows:
- The first row should contain the sum of elements in each row, separated by space.
- The second row should contain the sum of elements in each column, separated by space.
Input | Output |
3
0 1 2
1 8 3
-1 3 10 | 3 12 12
0 12 15 |
3
0 -1 0
0 0 0
1 0 1 | -1 0 2
1 -1 1 |
Explanation for the 1st example:
- First line (sum of rows):
- 0 + 1 + 2 = 3
- 1 + 8 + 3 = 12
- -1 + 3 + 10 = 12
- Second line (sum of columns):
- 0 + 1 + (-1) = 0
- 1 + 8 + 3 = 12
- 2 + 3 + 10 = 15
Explanation for the 2nd example:
- First line (sum of rows):
- 0 + (-1) + 0 = -1
- 0 + 0 + 0 = 0
- 1 + 0 + 1 = 2
- Second line (sum of columns)
- 0 + 0 + 1 = 1
- -1 + 0 + 0 = -1
- 0 + 0 + 1 = 1
Constraints
Time limit: 2 seconds
Memory limit: 512 MB
Output limit: 1 MB