#K54757. Magic Square Verification

    ID: 29824 Type: Default 1000ms 256MiB

Magic Square Verification

Magic Square Verification

You are given 9 integers representing a 3x3 grid in row-major order. Your task is to determine whether this grid forms a magic square. A magic square is defined as a square where the sums of the numbers in each row, each column, and both main diagonals are all equal.

Let \(S\) denote the magic constant (i.e. \(S = \text{sum of the first row}\)). Then for a 3x3 grid with elements \(a_{ij}\) (where \(i,j=1,2,3\)), the grid is magic if:

  • \(a_{11}+a_{12}+a_{13} = S\), \(a_{21}+a_{22}+a_{23} = S\), \(a_{31}+a_{32}+a_{33} = S\)
  • \(a_{11}+a_{21}+a_{31} = S\), \(a_{12}+a_{22}+a_{32} = S\), \(a_{13}+a_{23}+a_{33} = S\)
  • \(a_{11}+a_{22}+a_{33} = S\), \(a_{13}+a_{22}+a_{31} = S\)

Output "YES" if the grid is a magic square, otherwise output "NO".

inputFormat

The input consists of 9 space-separated integers representing the 3x3 grid in row-major order.

For example:

2 7 6 9 5 1 4 3 8

outputFormat

Output a single string "YES" if the grid forms a magic square. Otherwise, output "NO".

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