Matrix Element Sum
EasyPractice library · +10 XPmatrixtraversalloops
Given a 2D matrix (grid) of integers, compute the sum of all elements that are located in rows with an even index. Row indices are 0-based.
Input
The first line contains two integers R and C, the number of rows and columns. The next R lines each contain C integers, representing the matrix elements.
Output
Print a single integer: the sum of elements in even-indexed rows.
Constraints
1 <= R, C <= 1000. Each matrix element is between -1000 and 1000.
Time limit 1 s (native code; slower languages get more)Memory 256 MB12 hidden tests
Example 1
Input
3 4 1 2 3 4 5 6 7 8 9 10 11 12
Output
52
Rows 0 and 2 are even-indexed. Sum = (1+2+3+4) + (9+10+11+12) = 10 + 42 = 52.
Example 2
Input
2 2 -1 0 0 -1
Output
-1
Only row 0 is even-indexed. Sum = -1 + 0 = -1.
Hints
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
Example 1 input
3 4 1 2 3 4 5 6 7 8 9 10 11 12
Example 2 input
2 2 -1 0 0 -1