#C11607. Diagonal Sum of a Square Matrix

    ID: 40942 Type: Default 1000ms 256MiB

Diagonal Sum of a Square Matrix

Diagonal Sum of a Square Matrix

Given a square matrix \(A\) of size \(n \times n\), your task is to compute the sum of its main diagonal and anti-diagonal elements. The main diagonal consists of the elements \(a_{ii}\) for \(i = 0, 1, \dots, n-1\), and the anti-diagonal consists of the elements \(a_{i,n-i-1}\) for \(i = 0, 1, \dots, n-1\). If \(n\) is odd, the center element \(a_{\frac{n-1}{2},\frac{n-1}{2}}\) is counted twice; make sure to subtract it once.

The formula for the sum \(S\) is given by:

[ S = \sum_{i=0}^{n-1} \left(a_{i,i} + a_{i,n-i-1}\right) - \begin{cases} a_{\frac{n-1}{2},\frac{n-1}{2}}, & \text{if } n \text{ is odd} \ 0, & \text{if } n \text{ is even} \end{cases} ]

You are required to read the matrix from the standard input and output the computed sum to the standard output.

inputFormat

The first line contains a single integer \(n\) representing the size of the matrix. The following \(n\) lines each contain \(n\) space-separated integers representing the elements of the matrix.

For example:

3
1 2 3
4 5 6
7 8 9

outputFormat

Output a single integer, the sum of the main diagonal and anti-diagonal elements of the matrix, adjusting for any double-counting if necessary.

For the above example, the output should be:

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