#C9348. Row Sum Equalization via Flips
Row Sum Equalization via Flips
Row Sum Equalization via Flips
You are given an \(n \times n\) grid of integers. You are allowed to flip any row or column; that is, multiply every element of the chosen row or column by \(-1\) (i.e. perform the operation \(a_{ij} \rightarrow -a_{ij}\)). Determine whether it is possible to perform a series of such flips such that the sum of every row becomes identical.
Formally, let the grid be represented as \(A\), where \(A_{ij}\) are integers. A flip operation on a row or column multiplies all elements in that row or column by \(-1\). You need to check if there exists a sequence of these operations such that the row sums become equal. If it is possible, output YES
, otherwise output NO
.
Note: The input is provided via standard input (stdin) and the output should be printed to standard output (stdout).
inputFormat
The first line contains an integer \(n\) representing the number of rows (and columns) in the grid. The following \(n\) lines each contain \(n\) space-separated integers representing the grid.
For example, a grid of size 3 can be given as:
3 1 -1 1 0 0 0 1 -1 1
outputFormat
Output a single line containing either YES
if it is possible to make all row sums equal by applying the allowed flip operations, or NO
otherwise.
3
1 -1 1
0 0 0
1 -1 1
YES