#C796. Diagonal Sum of a Square Matrix

    ID: 51888 Type: Default 1000ms 256MiB

Diagonal Sum of a Square Matrix

Diagonal Sum of a Square Matrix

Given a square matrix of order \( n \), compute the sum of its main and secondary diagonal elements. The main diagonal consists of the elements \( A_{i,i} \) for \( i=1,2,\dots,n \), and the secondary diagonal consists of the elements \( A_{i,n-i+1} \) for \( i=1,2,\dots,n \). Note that if \( n \) is odd, the center element (which appears in both diagonals) should be counted only once.

Mathematical Formulation:

Let \( S_m = \sum_{i=1}^{n} A_{i,i} \) and \( S_s = \sum_{i=1}^{n} A_{i,n-i+1} \). Then, if \( n \) is odd, the final answer is:

\[ S = S_m + S_s - A_{\lceil n/2 \rceil, \lceil n/2 \rceil} \]

Otherwise, if \( n \) is even, simply \( S = S_m + S_s \).

inputFormat

The input is read from standard input. The first line contains an integer \( n \) representing the order of the matrix. Each of the following \( n \) lines contains \( n \) space-separated integers representing a row of the matrix.

Example:
3
1 2 3
4 5 6
7 8 9

outputFormat

Print the diagonal sum computed as described above to the standard output.

Example:
25
## sample
3
1 2 3
4 5 6
7 8 9
25