#C8586. Magic Square Verification

    ID: 52584 Type: Default 1000ms 256MiB

Magic Square Verification

Magic Square Verification

A magic square is a \(n \times n\) grid populated with distinct integers. The grid is considered magic if the sum of the numbers in each row, each column, and both main diagonals are equal to the magic constant. The magic constant is defined as \( M = \frac{n(n^2+1)}{2} \).

Your task is to determine whether the given sequence of \(n^2\) numbers, when arranged in row-major order into an \(n \times n\) grid, forms a magic square.

Note: The input numbers are distinct. The grid is formed by taking the first \(n\) numbers as the first row, the next \(n\) numbers as the second row, and so on.

inputFormat

The input consists of two lines:

  1. The first line contains an integer \(n\), representing the size of the square.
  2. The second line contains \(n^2\) space-separated integers representing the elements of the square in row-major order.

outputFormat

Output a single line containing YES if the square is a magic square, or NO otherwise.

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