Low Range-Sum Matrix

Time limit2sMemory limit512 MB

Summary
Flip signs of at most K cells in an N by M matrix (both at most 10) so that no horizontal or vertical contiguous segment sums to more than S.
Level

Hard9 of 10

Topics
Brute force, Dynamic programming, Bit manipulation, Implementation
Solved
No attempts yet

Problem

You received a card at a banquet. On the card, a matrix of NN rows and MM columns and two integers KK and SS are written. All the elements in the matrix are integers, and an integer at the ii-th row from the top and the jj-th column from the left is denoted by Ai,jA_{i,j}.

You can select up to KK elements from the matrix and invert the sign of the elements. If you can make a matrix such that there is no vertical or horizontal contiguous subsequence whose sum is greater than SS, you can exchange your card for a prize.

Your task is to determine if you can exchange a given card for a prize.

Input

The input consists of a single test case of the following form.

$N$ $M$ $K$ $S$
$A_{1,1}$ $A_{1,2}$ $\cdots$  $A_{1,M}$
$\vdots$
$A_{N,1}$ $A_{N,2}$ $\cdots$  $A_{N,M}$

The first line consists of four integers NN, MM, KK, and SS (1≤N,M≤101 \le N, M \le 10, 1≤K≤51 \le K \le 5, 1≤S≤1061 \le S \le 10^6). The following NN lines represent the matrix in your card. The (i+1)(i+1)-th line consists of MM integers Ai,1A_{i,1}, Ai,2A_{i,2}, …\ldots, Ai,MA_{i,M} (−105≤Ai,j≤105-10^5 \le A_{i,j} \le 10^5).

Output

If you can exchange your card for a prize, print Yes. Otherwise, print No.

Examples4

  1. Example 1

    Input
    3 3 2 10
    5 3 7
    2 6 1
    3 4 1
    
    Expected output
    Yes
    
  2. Example 2

    Input
    2 3 1 5
    4 8 -2
    -2 -5 -3
    
    Expected output
    Yes
    
  3. Example 3

    Input
    2 3 1 5
    9 8 -2
    -2 -5 -3
    
    Expected output
    No
    
  4. Example 4

    Input
    2 2 3 100
    0 0
    0 0
    
    Expected output
    Yes