Problem 489
Matrix Diagonal Sum
Easy · 10 pts · Matrix
Problem
Given a square N x N matrix, add up the elements on its primary diagonal (from the top-left to the bottom-right corner) and on its secondary diagonal (from the top-right to the bottom-left corner). When N is odd, the centre element lies on both diagonals and must be counted only once. Print the total.
Input
First line: N. Next N lines: N integers each.
Output
Print the diagonal sum.
Constraints
1 <= N <= 100; 1 <= value <= 100
Example input
3 1 2 3 4 5 6 7 8 9
Example output
25
Explanation
For 1 2 3 / 4 5 6 / 7 8 9, the diagonals are 1 5 9 and 3 5 7. The centre 5 is counted once: 1 + 5 + 9 + 3 + 7 = 25.