#K61027. Sum of Diagonal Elements
Sum of Diagonal Elements
Sum of Diagonal Elements
Given an m x n matrix (not necessarily square) represented in row-major order, your task is to compute the sum of the main diagonal elements. The main diagonal of a matrix consists of elements at positions \( (i,i) \) for \( i = 0, 1, \ldots, \min(m,n)-1 \). If the matrix is not square, only consider the elements up to the smaller dimension.
Input Format: The first line contains two integers \( m \) and \( n \) representing the number of rows and columns respectively. The next \( m \) lines each contain \( n \) space-separated integers representing the matrix rows.
Output Format: Print a single integer - the sum of the main diagonal elements.
inputFormat
The input is given via standard input (stdin) with the following format:
m n row1_element1 row1_element2 ... row1_elementn ... rowm_element1 rowm_element2 ... rowm_elementn
Here, \( m \) and \( n \) denote the number of rows and columns, respectively, followed by \( m \) lines of matrix elements.
outputFormat
Output a single integer to standard output (stdout) representing the sum of the main diagonal elements of the matrix. For a non-square matrix, only the diagonal up to the minimum of \( m \) and \( n \) is considered.
## sample3 3
1 2 3
4 5 6
7 8 9
15