PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GL0000359. Vẻ đẹp của núi (Beauty of the Mountains)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Vẻ đẹp của núi (Beauty of the Mountains)

Beauty of the Mountains

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Consider an n×mn\times m grid of nonnegative integer heights aija_{ij}. A binary label sijs_{ij} indicates a mountain's type: 0 means snow-covered, while 1 means not snow-covered.

In one move, select any contiguous k×kk\times k subgrid and add the same arbitrary integer cc to every height in it. The value cc may be negative, and heights may become negative; labels do not change. You may perform any number of moves.

Determine whether the sum of heights of type-0 cells can equal the sum of heights of type-1 cells. If a type is absent, its total is zero.

PHOJ format: Each input file has one case, without the original leading test count tt.

Input

  • First line: n,m,kn,m,k.
  • Next nn lines: mm nonnegative heights per line.
  • Last nn lines: binary strings of length mm describing the labels.

Output

Print YES if equality is achievable; otherwise print NO.

Subtasks

  • Subtask 1 (20 points): 1<=n,m<=5;1<=k<=min(n,m);0<=aij<=201 <= n,m <= 5; 1 <= k <= min(n,m); 0 <= a_ij <= 20.
  • Subtask 2 (30 points): $1 <= n,m <= 50; 1 <= k <= min(n,m); 0 <= a_ij <= 10^5$.
  • Subtask 3 (50 points): $1 <= n,m <= 500; 1 <= k <= min(n,m); 0 <= a_ij <= 10^9$.

Every binary row sis_i consists of exactly mm characters from 0 and 1.

Examples

Example 1

Input

2 2 2
3 4
6 7
00
00

Output

YES

Explanation

All four cells are type 0, with total height 20. Adding c=-5 to the only 2x2 square makes its total 0, matching the absent type-1 total.

Example 2

Input

2 2 2
3 4
6 8
00
00

Output

NO

Explanation

The type-0 sum is 21, and each operation changes it by a multiple of 4, so equality with zero is impossible.

Example 3

Input

1 2 1
1 2
01

Output

YES

Explanation

With k=1, each cell can be changed independently. Increasing the type-0 cell from 1 to 2 equalizes the totals.