Problem:
Low Range-Sum Matrix
You received a card at a banquet. On the card, a matrix of $N$ rows and $M$ columns and two integers $K$ and $S$ are written. All the elements in the matrix are integers, and an integer at the $i$-th row from the top and the $j$-th column from the left is denoted by $A_{i,j}$.
You can select up to $K$ 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 $S$, 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}$ ... $A_{1,M}$
:
$A_{N,1}$ $A_{N,2}$ ... $A_{N,M}$
The first line consists of four integers $N, M, K$ and $S$ ($1 \leq N, M \leq 10, 1 \leq K \leq 5, 1 \leq S \leq 10^6$). The following $N$ lines represent the matrix in your card. The ($i+1$)-th line consists of $M$ integers $A_{i,1}, A_{i,2}, ..., A_{i, M}$ ($-10^5 \leq A_{i,j} \leq 10^5$).
Output
If you can exchange your card for a prize, print '
Yes
'. Otherwise, print '
No
'.
Sample Input 1
3 3 2 10
5 3 7
2 6 1
3 4 1
Output for Sample Input 1
Yes
The sum of a horizontal contiguous subsequence from $A_{1,1}$ to $A_{1,3}$ is $15$. The sum of a vertical contiguous subsequence from $A_{1,2}$ to $A_{3,2}$ is $13$. If you flip the sign of $A_{1,2}$, there is no vertical or horizontal contiguous subsequence whose sum is greater than $S$.
Sample Input 2
2 3 1 5
4 8 -2
-2 -5 -3
Output for Sample Input 2
Yes
Sample Input 3
2 3 1 5
9 8 -2
-2 -5 -3
Output for Sample Input 3
No
Sample Input 4
2 2 3 100
0 0
0 0
Output for Sample Input 4
Yes

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = int;
// const ll INF = 1LL << 60;
#define rep(i, a) for(ll i = 0; i < (a); i++)
#define all(a) begin(a), end(a)
#define sz(a) (a).size()
bool chmin(auto &a, auto b) { return a > b ? a = b, 1 : 0; }
bool chmax(auto &a, auto b) { return a < b ? a = b, 1 : 0; }
#define out(a) cout << (a) << endl
using vll = vector<ll>;
using vvll = vector<vll>;

int main()
{
  ll N, M, K, S; cin >> N >> M >> K >> S;
  vvll A(N, vll(M));
  rep(i, N) rep(j, M) cin >> A[i][j];
  vector<vvll> dp1(K + 1);
  dp1[0].push_back(vll(M, 0));
  auto dp2 = dp1;
  rep(i, N/2)
  {
    vector<vvll> ndp(K + 1);
    rep(k, K + 1)
    {
      ll r = min(K - k, M);
      for(auto &mxs : dp1[k])
      {
        for(ll d = 0; d <= r; d++)
        {
            vll P(M, 0);
            rep(j, d) P[M - j - 1] = 1;
            do
            {
            vll nxv = A[i];
            rep(j, M) if(P[j]) nxv[j] = -nxv[j];
            bool ok = true;
            ll tmp = 0;
            rep(j, M)
            {
                if(nxv[j] > S || nxv[j] + mxs[j] > S || tmp + nxv[j] > S)
                {
                ok = false;
                break;
                }
                tmp = max(tmp + nxv[j], max(nxv[j], (ll)0));
            }
            if(!ok) continue;
            // ndp.insert(make_pair(d + k, nxv));
            rep(j, M) nxv[j] = max(max(nxv[j], (ll)0), nxv[j] + mxs[j]);
            ndp[d + k].push_back(nxv);
            } while (next_permutation(all(P)));
        }
      }
    }
    dp1.swap(ndp);
    for(auto &vec : dp1)
    {
        sort(all(vec));
        vec.erase(unique(all(vec)), vec.end());
    }
  }
  for(ll i = N - 1; i >= N/2; i--)
  {
    vector<vvll> ndp(K + 1);
    rep(k, K + 1)
    {
      ll r = min(K - k, M);
      for(auto &mxs : dp2[k])
      {
        for(ll d = 0; d <= r; d++)
        {
            vll P(M, 0);
            rep(j, d) P[M - j - 1] = 1;
            do
            {
            vll nxv = A[i];
            rep(j, M) if(P[j]) nxv[j] = -nxv[j];
            bool ok = true;
            ll tmp = 0;
            rep(j, M)
            {
                if(nxv[j] > S || nxv[j] + mxs[j] > S || tmp + nxv[j] > S)
                {
                ok = false;
                break;
                }
                tmp = max(tmp + nxv[j], max(nxv[j], (ll)0));
            }
            if(!ok) continue;
            // ndp.insert(make_pair(d + k, nxv));
            rep(j, M) nxv[j] = max(max(nxv[j], (ll)0), nxv[j] + mxs[j]);
            ndp[d + k].push_back(nxv);
            } while (next_permutation(all(P)));
        }
      }
    }
    dp2.swap(ndp);
    for(auto &vec : dp2)
    {
        sort(all(vec));
        vec.erase(unique(all(vec)), vec.end());
    }
  }
  for(ll k = 0; k <= K; k++)
  {
    for(auto &v1 : dp1[k]) for(auto &v2 : dp2[K - k])
    {
        bool ok = true;
        rep(j, M)
        {
            if(v1[j] + v2[j] > S || v1[j] > S || v2[j] > S)
            {
                ok = false;
                break;
            }
        }
        if(ok)
        {
            // for(auto e : v1) cout << e << " ";
            // out("");
            // for(auto e : v2) cout << e << " ";
            // out("");
            out("Yes");
            return 0;
        }
    }
  }
  out("No");
}
accuracy: 1
time_ms: 1270
memory_kb: 189008
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = int;
// const ll INF = 1LL << 60;
#define rep(i, a) for(ll i = 0; i < (a); i++)
#define all(a) begin(a), end(a)
#define sz(a) (a).size()
bool chmin(auto &a, auto b) { return a > b ? a = b, 1 : 0; }
bool chmax(auto &a, auto b) { return a < b ? a = b, 1 : 0; }
#define out(a) cout << (a) << endl
using vll = vector<ll>;
using vvll = vector<vll>;

int main()
{
  ll N, M, K, S; cin >> N >> M >> K >> S;
  vvll A(N, vll(M));
  rep(i, N) rep(j, M) cin >> A[i][j];
  vector<vvll> dp1(K + 1);
  dp1[0].push_back(vll(M, 0));
  auto dp2 = dp1;
  rep(i, N/2)
  {
    vector<vvll> ndp(K + 1);
    rep(k, K + 1)
    {
      ll r = min(K - k, M);
      for(auto &mxs : dp1[k])
      {
        for(ll d = 0; d <= r; d++)
        {
            vll P(M, 0);
            rep(j, d) P[M - j - 1] = 1;
            do
            {
            vll nxv = A[i];
            rep(j, M) if(P[j]) nxv[j] = -nxv[j];
            bool ok = true;
            ll tmp = 0;
            rep(j, M)
            {
                if(nxv[j] > S || nxv[j] + mxs[j] > S || tmp + nxv[j] > S)
                {
                ok = false;
                break;
                }
                tmp = max(tmp + nxv[j], max(nxv[j], (ll)0));
            }
            if(!ok) continue;
            // ndp.insert(make_pair(d + k, nxv));
            rep(j, M) nxv[j] = max(max(nxv[j], (ll)0), nxv[j] + mxs[j]);
            ndp[d + k].push_back(nxv);
            } while (next_permutation(all(P)));
        }
      }
    }
    dp1.swap(ndp);
    for(auto &vec : dp1)
    {
        sort(all(vec));
        vec.erase(unique(all(vec)), vec.end());
    }
  }
  for(ll i = N - 1; i >= N/2; i--)
  {
    vector<vvll> ndp(K + 1);
    rep(k, K + 1)
    {
      ll r = min(K - k, M);
      for(auto &mxs : dp2[k])
      {
        for(ll d = 0; d <= r; d++)
        {
            vll P(M, 0);
            rep(j, d) P[M - j - 1] = 1;
            do
            {
            vll nxv = A[i];
            rep(j, M) if(P[j]) nxv[j] = -nxv[j];
            bool ok = true;
            ll tmp = 0;
            rep(j, M)
            {
                if(nxv[j] > S || nxv[j] + mxs[j] > S || tmp + nxv[j] > S)
                {
                ok = false;
                break;
                }
                tmp = max(tmp + nxv[j], max(nxv[j], (ll)0));
            }
            if(!ok) continue;
            // ndp.insert(make_pair(d + k, nxv));
            rep(j, M) nxv[j] = max(max(nxv[j], (ll)0), nxv[j] + mxs[j]);
            ndp[d + k].push_back(nxv);
            } while (next_permutation(all(P)));
        }
      }
    }
    dp2.swap(ndp);
    for(auto &vec : dp2)
    {
        sort(all(vec));
        vec.erase(unique(all(vec)), vec.end());
    }
  }
  for(ll k = 0; k <= K; k++)
  {
    for(auto &v1 : dp1[k]) for(auto &v2 : dp2[k])
    {
        bool ok = true;
        rep(j, M)
        {
            if(v1[j] + v2[j] > S || v1[j] > S || v2[j] > S)
            {
                ok = false;
                break;
            }
        }
        if(ok)
        {
            // for(auto e : v1) cout << e << " ";
            // out("");
            // for(auto e : v2) cout << e << " ";
            // out("");
            out("Yes");
            return 0;
        }
    }
  }
  out("No");
}
accuracy: 0.7727272727272727
time_ms: 1260
memory_kb: 189948
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = int;
// const ll INF = 1LL << 60;
#define rep(i, a) for(ll i = 0; i < (a); i++)
#define all(a) begin(a), end(a)
#define sz(a) (a).size()
bool chmin(auto &a, auto b) { return a > b ? a = b, 1 : 0; }
bool chmax(auto &a, auto b) { return a < b ? a = b, 1 : 0; }
#define out(a) cout << (a) << endl
using vll = vector<ll>;
using vvll = vector<vll>;

int main()
{
  ll N, M, K, S; cin >> N >> M >> K >> S;
  vvll A(N, vll(M));
  rep(i, N) rep(j, M) cin >> A[i][j];
//   set<pair<ll, vll>> dp;
//   map<ll, vvll> dp1;
  vector<vvll> dp1(min(K, M) + 1);
  dp1[0].push_back(vll(M, 0));
  auto dp2 = dp1;
  rep(i, N/2)
  {
    // set<pair<ll, vll>> ndp;
    vector<vvll> ndp(min(K, M) + 1);
    rep(k, min(K, M) + 1)
    {
      ll r = min(K - k, M);
      for(auto &mxs : dp1[k])
      {
        for(ll d = 0; d <= r; d++)
        {
            vll P(M, 0);
            rep(i, d) P[M - i - 1] = 1;
            do
            {
            vll nxv = A[i];
            rep(j, M) if(P[j]) nxv[j] = -nxv[j];
            bool ok = true;
            ll tmp = 0;
            rep(j, M)
            {
                if(nxv[j] > S || nxv[j] + mxs[j] > S || tmp + nxv[j] > S)
                {
                ok = false;
                break;
                }
                tmp = max(tmp + nxv[j], nxv[j]);
            }
            if(!ok) continue;
            // ndp.insert(make_pair(d + k, nxv));
            ndp[d + k].push_back(nxv);
            } while (next_permutation(all(P)));
        }
      }
    }
    dp1.swap(ndp);
    for(auto &vec : dp1)
    {
        sort(all(vec));
        vec.erase(unique(all(vec)), vec.end());
    }
  }
  for(ll i = N - 1; i >= N/2; i--)
  {
    vector<vvll> ndp(min(K, M) + 1);
    rep(k, min(K, M) + 1)
    {
      ll r = min(K - k, M);
      for(auto &mxs : dp2[k])
      {
        for(ll d = 0; d <= r; d++)
        {
            vll P(M, 0);
            rep(i, d) P[M - i - 1] = 1;
            do
            {
            vll nxv = A[i];
            rep(j, M) if(P[j]) nxv[j] = -nxv[j];
            bool ok = true;
            ll tmp = 0;
            rep(j, M)
            {
                if(nxv[j] > S || nxv[j] + mxs[j] > S || tmp + nxv[j] > S)
                {
                ok = false;
                break;
                }
                tmp = max(tmp + nxv[j], nxv[j]);
            }
            if(!ok) continue;
            // ndp.insert(make_pair(d + k, nxv));
            ndp[d + k].push_back(nxv);
            } while (next_permutation(all(P)));
        }
      }
    }
    dp2.swap(ndp);
    for(auto &vec : dp2)
    {
        sort(all(vec));
        vec.erase(unique(all(vec)), vec.end());
    }
  }
  for(ll k = 0; k <= min(K, M); k++)
  {
    for(auto &v1 : dp1[k]) for(auto &v2 : dp2[k])
    {
        bool ok = true;
        rep(j, M)
        {
            if(v1[j] + v2[j] > S)
            {
                ok = false;
                break;
            }
        }
        if(ok)
        {
            out("Yes");
            return 0;
        }
    }
  }
  out("No");
}
accuracy: 0.7727272727272727
time_ms: 30
memory_kb: 8312
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define vl vector<ll>
#define vvl vector<vl>
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define loop(i, a, b) for (ll i = a; i <= b; ++i)

ll h,w,s;

bool dfs(ll i,ll j,ll k,vvl & a){
	if(k<0)return false;

	while(1){
		//今のi,jに矛盾があるか調べる
		
		//横
		loop(sj,0,j){
			ll sum = 0;
			loop(z,sj,j){
				sum+=a[i][z];
			}
			if(sum>s){
				loop(z,sj,j){
					if(a[i][z]<=0)continue;
					a[i][z]*=-1;
					if(dfs(i,j,k-1,a))return true;
					a[i][z]*=-1;
				}
				return false;
			}
		}

		//縦
		rep(si,i){
			ll sum = 0;
			loop(z,si,i){
				sum+=a[z][j];
			}
			if(sum>s){
				loop(z,si,i){
					if(a[z][j]<=0)continue;
					a[z][j]*=-1;
					if(dfs(i,j,k-1,a))return true;
					a[z][j]*=-1;
				}
				return false;
			}
		}

		//問題ないので次へ
		j++;
		if(j==w){
			j=0;
			i++;
		}
		if(h==i){
			//デバッグ
			return true;
		}
	}
}

int main() {
	ll k;
	cin>>h>>w>>k>>s;
	vvl a;
	a = vvl(h,vl(w));
	rep(i,h)rep(j,w)cin>>a[i][j];

	if(dfs(0,0,k,a))cout<<"Yes"<<endl;
	else cout<<"No"<<endl;

    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3584
final_rank: 2

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;
const int MOD = 1000000007;
#define rep(i,n) for(ll i=0; i<ll(n); i++)

ll A[10][10];
bool rows[10][1024];
bool cols[10][1024];

void solve(){
    ll N, M, K, S; cin >> N >> M >> K >> S;
    rep(i,N) rep(j,M) cin >> A[i][j];
    rep(i,N) rep(b,1<<M){
        ll T[11]; T[0] = 0;
        rep(d,M) T[d+1] = T[d] + A[i][d] * ((b&(1<<d)) ? -1 : 1);
        rows[i][b] = true;
        rep(a,M+1) rep(q,a) if(T[a] - T[q] > S) rows[i][b] = false;
    }
    rep(i,M) rep(b,1<<N){
        ll T[11]; T[0] = 0;
        rep(d,N) T[d+1] = T[d] + A[d][i] * ((b&(1<<d)) ? -1 : 1);
        cols[i][b] = true;
        rep(a,N+1) rep(q,a) if(T[a] - T[q] > S) cols[i][b] = false;
    }
    vector<pair<int,int>> ps;
    rep(i,N) rep(j,M) if(!rows[i][0] || !cols[j][0]) ps.push_back({i,j});
    int cnt = 0;
    rep(i,N) if(!rows[i][0]) cnt++;
    if(cnt > K){ cout << "No\n"; return; }
    cnt = 0;
    rep(i,M) if(!cols[i][0]) cnt++;
    if(cnt > K){ cout << "No\n"; return; }
    bool ok = false;
    int mrows[10] = {};
    int mcols[10] = {};
    auto dfs = [&](auto& dfs, int k, int p) -> void {
        if(ok) return;
        if(k == 0){
            rep(j,N) if(!rows[j][mrows[j]]) return;
            rep(j,M) if(!cols[j][mcols[j]]) return;
            ok = true;
            return;
        }
        if(p+1 < int(ps.size())){
            dfs(dfs, k, p+1);
            auto [a,b] = ps[p]; if(A[a][b] <= 0) return;
            mrows[a] += 1 << b;
            mcols[b] += 1 << a;
            dfs(dfs, k-1, p+1);
            mrows[a] -= 1 << b;
            mcols[b] -= 1 << a;
        }
    };
    rep(k,K+1) dfs(dfs, k, 0);
    cout << (ok ? "Yes\n" : "No\n");
}

int main(){
    solve();
    return 0;
}
accuracy: 1
time_ms: 150
memory_kb: 3460
final_rank: 7

code_snippet: // AOJ #2851
// Low Range-Sum Matrix 2025.2.5

#include <bits/stdc++.h>
using namespace std;
 
int N, M, K;
long long S;
vector<vector<int>> orig;
 
unordered_set<string> memo;
 
bool isValid(const string &state) {
    // 行のチェック
    for (int i = 0; i < N; i++){
        for (int start = 0; start < M; start++){
            long long sum = 0;
            for (int j = start; j < M; j++){
                int idx = i * M + j;
                int sign = (state[idx]=='1' ? -1 : 1);
                sum += (long long)orig[i][j] * sign;
                if(sum > S) return false;
            }
        }
    }
    // 列のチェック
    for (int j = 0; j < M; j++){
        for (int start = 0; start < N; start++){
            long long sum = 0;
            for (int i = start; i < N; i++){
                int idx = i * M + j;
                int sign = (state[idx]=='1' ? -1 : 1);
                sum += (long long)orig[i][j] * sign;
                if(sum > S) return false;
            }
        }
    }
    return true;
}
 
bool dfs(string &state) {
    if(isValid(state))
        return true;
    int flippedCount = 0;
    for (char c : state)
        if(c=='1') flippedCount++;
    if(flippedCount == K) return false;
    
    if(memo.find(state) != memo.end()) return false;
    memo.insert(state);
    
    for (int i = 0; i < N; i++){
        for (int start = 0; start < M; start++){
            long long sum = 0;
            for (int j = start; j < M; j++){
                int idx = i * M + j;
                int sign = (state[idx]=='1' ? -1 : 1);
                sum += (long long)orig[i][j] * sign;
                if(sum > S){
                    vector<int> candidates;
                    for (int k = start; k <= j; k++){
                        int idx2 = i * M + k;
                        if(state[idx2]=='0'){
                            int val = orig[i][k];
                            if(val > 0)
                                candidates.push_back(idx2);
                        }
                    }
                    if(candidates.empty()) return false;

                    for (int cand : candidates){
                        state[cand] = '1';
                        if(dfs(state)) return true;
                        state[cand] = '0';
                    }
                    return false;
                }
            }
        }
    }

    for (int j = 0; j < M; j++){
        for (int start = 0; start < N; start++){
            long long sum = 0;
            for (int i = start; i < N; i++){
                int idx = i * M + j;
                int sign = (state[idx]=='1' ? -1 : 1);
                sum += (long long)orig[i][j] * sign;
                if(sum > S){
                    vector<int> candidates;
                    for (int i2 = start; i2 <= i; i2++){
                        int idx2 = i2 * M + j;
                        if(state[idx2]=='0'){
                            int val = orig[i2][j];
                            if(val > 0) candidates.push_back(idx2);
                        }
                    }
                    if(candidates.empty()) return false;
                    for (int cand : candidates){
                        state[cand] = '1';
                        if(dfs(state)) return true;
                        state[cand] = '0';
                    }
                    return false;
                }
            }
        }
    }
    return true;
}
 
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
 
    cin >> N >> M >> K >> S;
    orig.assign(N, vector<int>(M,0));
    for (int i = 0; i < N; i++){
        for (int j = 0; j < M; j++) cin >> orig[i][j];
    }
    string state(N * M, '0');
    memo.clear();
    bool res = dfs(state);
    cout << (res ? "Yes" : "No") << endl;
    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 5112
final_rank: 6

code_snippet: //tekitou hash demo tooru to uresii
#include<bits/stdc++.h>
using namespace std;

using ll=long long;
#define rep(i,n) for(int i=0;i<n;i++)
#define rep2(i,s,t) for(int i=s;i<t;i++)

using vi=vector<int>;

bool is_ok(vector<int> V,int S){
    int n=V.size();
    int m=0,s=0;
    rep(i,n){
        s+=V[i];
        m=min(m,s);
        if(S<s-m)return 0;
    }
    return 1;
}

vector<int> vert(vector<vector<int>> &A,int m){
    int n=A.size();
    vector<int> R(n);
    rep(i,n)R[i]=A[i][m];
    return R;
}

int cnt(vector<int> &V,int S,int maxK){
    int n=V.size();
    for(int j=0;j<=maxK;j++){
        if(j>n)break;
        vector<int> c(n,0);
        rep(k,j)c[n-1-k]=1;
        do{
            vector<int> CV=V;
            bool valid=1;
            rep(k,n)if(c[k]){
                if(CV[k]<=0)valid=0;
                CV[k]*=-1;
            }

            if(valid&&is_ok(CV,S))return j;
        }while(next_permutation(c.begin(),c.end()));
    }
    return maxK+1;
}


int main() {
    
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int N,M,K,S;
    cin>>N>>M>>K>>S;
    vector<vector<int>> A(N,vector<int>(M));
    rep(i,N)rep(j,M)cin>>A[i][j];
    vector<int> NGH,NGW;
    rep(i,N)if(!is_ok(A[i],S))NGH.push_back(i);
    rep(i,M)if(!is_ok(vert(A,i),S))NGW.push_back(i);
    int hn=NGH.size();
    int wn=NGW.size();
    if(max(hn,wn)>K){
        cout<<"No"<<endl;
        return 0;
    }
    rep(k,K+1){
        if(k>hn*wn)break;
        vector<int> change(hn*wn,0);
        rep(j,k)change[j]=1;
        sort(change.begin(),change.end());
        do{
            auto B=A;
            bool valid=1;
            rep(d,hn*wn){
                if(change[d]==1){
                    if(B[NGH[d/wn]][NGW[d%wn]]<=0)valid=0;
                    B[NGH[d/wn]][NGW[d%wn]]*=-1;
                }
            }
            if(!valid)continue;
            int ZK=K-k;
            rep(i,N){
                if(ZK<0)break;
                ZK-=cnt(B[i],S,ZK);
            }
            rep(i,M){
                if(ZK<0)break;
                vector<int> Bi=vert(B,i);
                ZK-=cnt(Bi,S,ZK);
            }
            if(ZK>=0){
                cout<<"Yes"<<endl;
                return 0;
            }

        }while(next_permutation(change.begin(),change.end()));
    }

    cout<<"No"<<endl;
}
accuracy: 1
time_ms: 40
memory_kb: 3508
final_rank: 3

code_snippet: //tekitou hash demo tooru to uresii
#include<bits/stdc++.h>
using namespace std;

using ll=long long;
#define rep(i,n) for(int i=0;i<n;i++)
#define rep2(i,s,t) for(int i=s;i<t;i++)

using vi=vector<int>;

bool is_ok(vector<int> V,int S){
    int n=V.size();
    int m=0,s=0;
    rep(i,n){
        s+=V[i];
        m=min(m,s);
        if(S<s-m)return 0;
    }
    return 1;
}

vector<int> vert(vector<vector<int>> &A,int m){
    int n=A.size();
    vector<int> R(n);
    rep(i,n)R[i]=A[i][m];
    return R;
}

int cnt(vector<int> &V,int S,int maxK){
    int n=V.size();
    for(int j=0;j<=maxK;j++){
        if(j>n)break;
        vector<int> c(n,0);
        rep(k,j)c[n-1-k]=1;
        do{
            vector<int> CV=V;
            bool valid=1;
            rep(k,n)if(c[k]){
                if(CV[k]<=0)valid=0;
                CV[k]*=-1;
            }

            if(valid&&is_ok(CV,S))return j;
        }while(next_permutation(c.begin(),c.end()));
    }
    return maxK+1;
}


int main() {
    
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int N,M,K,S;
    cin>>N>>M>>K>>S;
    vector<vector<int>> A(N,vector<int>(M));
    rep(i,N)rep(j,M)cin>>A[i][j];
    vector<int> NGH,NGW;
    rep(i,N)if(!is_ok(A[i],S))NGH.push_back(i);
    rep(i,M)if(!is_ok(vert(A,i),S))NGW.push_back(i);
    int hn=NGH.size();
    int wn=NGW.size();
    if(max(hn,wn)>K){
        cout<<"No"<<endl;
        return 0;
    }
    rep(k,K+1){
        if(k>hn*wn)break;
        vector<int> change(hn*wn,0);
        rep(j,k)change[j]=1;
        sort(change.begin(),change.end());
        do{
            auto B=A;
            bool valid=1;
            rep(d,hn*wn){
                if(change[d]==1){
                    if(B[NGH[d/wn]][NGW[d%wn]]<=0)valid=0;
                    B[NGH[d/wn]][NGW[d%wn]]*=-1;
                }
            }
            if(!valid)continue;
            int ZK=K;
            rep(i,N){
                if(ZK<0)break;
                ZK-=cnt(B[i],S,ZK);
            }
            rep(i,M){
                if(ZK<0)break;
                vector<int> Bi=vert(B,i);
                ZK-=cnt(Bi,S,ZK);
            }
            if(ZK>=0){
                cout<<"Yes"<<endl;
                return 0;
            }

        }while(next_permutation(change.begin(),change.end()));
    }

    cout<<"No"<<endl;
}
accuracy: 0.9545454545454546
time_ms: 110
memory_kb: 3472
final_rank: 17

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

/**
 * @brief template
 */

int N, M, K, S;

vector<int> FindCand(vector<vector<int>> A) {
    vector<int> Ret;
    rep(i,0,N) {
        int L = 0, R = 0, Cur = 0;
        for (; R<M;) {
            Cur += A[i][R];
            R++;
            if (Cur > S) {
                rep(j,L,R) Ret.push_back(i*10+j);
                return Ret;
            }
            if (Cur < 0) L = R, Cur = 0;
        }
    }
    rep(j,0,M) {
        int L = 0, R = 0, Cur = 0;
        for (; R<N;) {
            Cur += A[R][j];
            R++;
            if (Cur > S) {
                rep(i,L,R) Ret.push_back(i*10+j);
                return Ret;
            }
            if (Cur < 0) L = R, Cur = 0;
        }
    }
    return Ret;
}

bool DFS(vector<vector<int>> A, int Dep) {
    auto V = FindCand(A);
    if (V.empty()) return true;
    if (Dep >= K) return false;
    for (int x : V) {
        int i = x/10, j = x%10;
        if (A[i][j] <= 0) continue;
        A[i][j] = -A[i][j];
        if (DFS(A,Dep+1)) return true;
        A[i][j] = -A[i][j];
    }
    return false;
}

int main() {
    cin >> N >> M >> K >> S;
    vector<vector<int>> A(N,vector<int>(M));
    rep(i,0,N) rep(j,0,M) cin >> A[i][j];
    cout << (DFS(A,0) ? "Yes" : "No") << endl;
}
accuracy: 1
time_ms: 50
memory_kb: 3440
final_rank: 5

code_snippet: #line 1 "library/Template/template.hpp"
#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}
#line 2 "library/Utility/fastio.hpp"
#include <unistd.h>
namespace fastio {
static constexpr uint32_t SZ = 1 << 17;
char ibuf[SZ];
char obuf[SZ];
char out[100];
// pointer of ibuf, obuf

uint32_t pil = 0, pir = 0, por = 0;

struct Pre {
    char num[10000][4];
    constexpr Pre() : num() {
        for (int i = 0; i < 10000; i++) {
            int n = i;
            for (int j = 3; j >= 0; j--) {
                num[i][j] = n % 10 | '0';
                n /= 10;
            }
        }
    }
} constexpr pre;

inline void load() {
    memmove(ibuf, ibuf + pil, pir - pil);
    pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);
    pil = 0;
    if (pir < SZ)
        ibuf[pir++] = '\n';
}

inline void flush() {
    fwrite(obuf, 1, por, stdout);
    por = 0;
}

void rd(char &c) {
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
}

void rd(string &x) {
    x.clear();
    char c;
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
    do {
        x += c;
        if (pil == pir)
            load();
        c = ibuf[pil++];
    } while (!isspace(c));
}

template <typename T> void rd_real(T &x) {
    string s;
    rd(s);
    x = stod(s);
}

template <typename T> void rd_integer(T &x) {
    if (pil + 100 > pir)
        load();
    char c;
    do
        c = ibuf[pil++];
    while (c < '-');
    bool minus = 0;
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (c == '-') {
            minus = 1, c = ibuf[pil++];
        }
    }
    x = 0;
    while ('0' <= c) {
        x = x * 10 + (c & 15), c = ibuf[pil++];
    }
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (minus)
            x = -x;
    }
}

void rd(int &x) {
    rd_integer(x);
}
void rd(ll &x) {
    rd_integer(x);
}
void rd(i128 &x) {
    rd_integer(x);
}
void rd(uint &x) {
    rd_integer(x);
}
void rd(ull &x) {
    rd_integer(x);
}
void rd(u128 &x) {
    rd_integer(x);
}
void rd(double &x) {
    rd_real(x);
}
void rd(long double &x) {
    rd_real(x);
}

template <class T, class U> void rd(pair<T, U> &p) {
    return rd(p.first), rd(p.second);
}
template <size_t N = 0, typename T> void rd_tuple(T &t) {
    if constexpr (N < std::tuple_size<T>::value) {
        auto &x = std::get<N>(t);
        rd(x);
        rd_tuple<N + 1>(t);
    }
}
template <class... T> void rd(tuple<T...> &tpl) {
    rd_tuple(tpl);
}

template <size_t N = 0, typename T> void rd(array<T, N> &x) {
    for (auto &d : x)
        rd(d);
}
template <class T> void rd(vector<T> &x) {
    for (auto &d : x)
        rd(d);
}

void read() {}
template <class H, class... T> void read(H &h, T &...t) {
    rd(h), read(t...);
}

void wt(const char c) {
    if (por == SZ)
        flush();
    obuf[por++] = c;
}
void wt(const string s) {
    for (char c : s)
        wt(c);
}
void wt(const char *s) {
    size_t len = strlen(s);
    for (size_t i = 0; i < len; i++)
        wt(s[i]);
}

template <typename T> void wt_integer(T x) {
    if (por > SZ - 100)
        flush();
    if (x < 0) {
        obuf[por++] = '-', x = -x;
    }
    int outi;
    for (outi = 96; x >= 10000; outi -= 4) {
        memcpy(out + outi, pre.num[x % 10000], 4);
        x /= 10000;
    }
    if (x >= 1000) {
        memcpy(obuf + por, pre.num[x], 4);
        por += 4;
    } else if (x >= 100) {
        memcpy(obuf + por, pre.num[x] + 1, 3);
        por += 3;
    } else if (x >= 10) {
        int q = (x * 103) >> 10;
        obuf[por] = q | '0';
        obuf[por + 1] = (x - q * 10) | '0';
        por += 2;
    } else
        obuf[por++] = x | '0';
    memcpy(obuf + por, out + outi + 4, 96 - outi);
    por += 96 - outi;
}

template <typename T> void wt_real(T x) {
    ostringstream oss;
    oss << fixed << setprecision(15) << double(x);
    string s = oss.str();
    wt(s);
}

void wt(int x) {
    wt_integer(x);
}
void wt(ll x) {
    wt_integer(x);
}
void wt(i128 x) {
    wt_integer(x);
}
void wt(uint x) {
    wt_integer(x);
}
void wt(ull x) {
    wt_integer(x);
}
void wt(u128 x) {
    wt_integer(x);
}
void wt(double x) {
    wt_real(x);
}
void wt(long double x) {
    wt_real(x);
}

template <class T, class U> void wt(const pair<T, U> val) {
    wt(val.first);
    wt(' ');
    wt(val.second);
}
template <size_t N = 0, typename T> void wt_tuple(const T t) {
    if constexpr (N < std::tuple_size<T>::value) {
        if constexpr (N > 0) {
            wt(' ');
        }
        const auto x = std::get<N>(t);
        wt(x);
        wt_tuple<N + 1>(t);
    }
}
template <class... T> void wt(tuple<T...> tpl) {
    wt_tuple(tpl);
}
template <class T, size_t S> void wt(const array<T, S> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}
template <class T> void wt(const vector<T> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}

void print() {
    wt('\n');
}
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
    wt(head);
    if (sizeof...(Tail))
        wt(' ');
    print(forward<Tail>(tail)...);
}
void __attribute__((destructor)) _d() {
    flush();
}
} // namespace fastio

using fastio::flush;
using fastio::print;
using fastio::read;

inline void first(bool i = true) {
    print(i ? "first" : "second");
}
inline void Alice(bool i = true) {
    print(i ? "Alice" : "Bob");
}
inline void Takahashi(bool i = true) {
    print(i ? "Takahashi" : "Aoki");
}
inline void yes(bool i = true) {
    print(i ? "yes" : "no");
}
inline void Yes(bool i = true) {
    print(i ? "Yes" : "No");
}
inline void No() {
    print("No");
}
inline void YES(bool i = true) {
    print(i ? "YES" : "NO");
}
inline void NO() {
    print("NO");
}
inline void Yay(bool i = true) {
    print(i ? "Yay!" : ":(");
}
inline void Possible(bool i = true) {
    print(i ? "Possible" : "Impossible");
}
inline void POSSIBLE(bool i = true) {
    print(i ? "POSSIBLE" : "IMPOSSIBLE");
}

/**
 * @brief Fast IO
 */
#line 3 "sol.cpp"

int main() {
    int H, W, K, S;
    read(H, W, K, S);
    vector a(H, vector<int>(W));
    read(a);

    auto dfs = [&](auto &dfs, int i, int j, int k) -> bool {
        if (i == H) {
            show(a);
            return true;
        }
        int ni = i, nj = j + 1;
        if (nj == W)
            ni++, nj = 0;
        rep(add, 0, 2) {
            bool ch = k + add <= K;
            // up
            int sum = 0;
            rrep(x, 0, i + 1) {
                sum += a[x][j];
                if (sum > S) {
                    ch = 0;
                    break;
                }
            }
            // left
            if (ch) {
                sum = 0;
                rrep(y, 0, j + 1) {
                    sum += a[i][y];
                    if (sum > S) {
                        ch = 0;
                        break;
                    }
                }
            }
            if (ch) {
                if (dfs(dfs, ni, nj, k + add))
                    return 1;
            }
            a[i][j] = -a[i][j];
        }
        return 0;
    };
    Yes(dfs(dfs, 0, 0, 0));
    return 0;
}
accuracy: 1
time_ms: 1040
memory_kb: 3508
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)
#define all(x) begin(x), end(x)
template <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }
template <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    int N, M, K, S;
    cin >> N >> M >> K >> S;
    vector<vector<int>> A(N, vector<int>(M));
    for (auto& row : A) for (auto& x : row) cin >> x;

    int sumrow = 0;
    vector<int> sumcol(M);

    auto dfs = [&](auto& dfs, int i, int j, int k) -> bool {
        if (i == N) return true;
        if (j == M) {
            sumrow = 0;
            return dfs(dfs, i+1, 0, k);
        }

        rep(f,0,2) {
            int tmpr = sumrow, tmpc = sumcol[j];
            int x = f ? -A[i][j] : A[i][j];
            sumrow = max(sumrow+x, 0);
            sumcol[j] = max(sumcol[j]+x, 0);
            if (sumrow <= S && sumcol[j] <= S && k+f <= K) {
                if (dfs(dfs, i, j+1, k+f)) return true;
            }
            sumrow = tmpr;
            sumcol[j] = tmpc;
        }
        return false;
    };

    cout << (dfs(dfs, 0, 0, 0) ? "Yes" : "No")  << endl;
}
accuracy: 1
time_ms: 610
memory_kb: 3468
final_rank: 9

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)
using ll = long long;
template<class T> bool chmin(T &a,const T b){if(a>b){a=b;return 1;}return 0;}
template<class T> bool chmax(T &a,const T b){if(a<b){a=b;return 1;}return 0;}
const int INF = (1<<30)-1;
const ll  ILL = 1e15;
#define all(p) p.begin(),p.end()
const int mod=998244353;
using ld = long double;

int main(){
	int N,M,K,S;
	cin>>N>>M>>K>>S;
	vector<vector<int>> A(N,vector<int>(M));
	vector<int> X(N),Y(M);
	int P=0,Q=0;
	auto ch1=[&](int ind)->void{
		int tmp=0,sum=0;
		X[ind]=0;
		rep(i,0,M){
			sum+=A[ind][i];
			chmin(tmp,sum);
			chmax(X[ind],sum-tmp);
		}
		if(X[ind]>S) P++;
	};
	auto ch2=[&](int ind)->void{
		int tmp=0,sum=0;
		Y[ind]=0;
		rep(i,0,N){
			sum+=A[i][ind];
			chmin(tmp,sum);
			chmax(Y[ind],sum-tmp);
		}
		if(Y[ind]>S) Q++;
	};
	rep(i,0,N) rep(j,0,M){
		cin>>A[i][j];
	}
	rep(i,0,N) ch1(i);
	rep(i,0,M) ch2(i);
	if(max(P,Q)>K){
		cout<<"No\n";
		return 0;
	}
	auto op=[&](int x,int y)->void{
		if(X[x]>S) P--;
		if(Y[y]>S) Q--;
		A[x][y]*=-1;
		ch1(x);
		ch2(y);
	};
	vector<pair<int,int>> p;
	rep(i,0,N) rep(j,0,M) if(max(X[i],Y[j])) p.push_back({i,j});
	auto f=[&](auto self,int ind,int fil)->void{
		//cout<<ind<<" "<<fil<<" "<<P<<" "<<Q<<endl;
		if(max(P,Q)==0){
			cout<<"Yes\n";
			exit(0);
		}
		if(fil==K||ind==(int)p.size()) return;
		rep(i,ind,(int)p.size()){
			int x=p[i].first,y=p[i].second;
			if(X[x]<=S&&Y[y]<=S) continue;
			op(x,y);
			self(self,i+1,fil+1);
			op(x,y);
		}
	};
	f(f,0,0);
	cout<<"No\n";
}
accuracy: 1
time_ms: 740
memory_kb: 3456
final_rank: 12

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)
using ll = long long;
template<class T> bool chmin(T &a,const T b){if(a>b){a=b;return 1;}return 0;}
template<class T> bool chmax(T &a,const T b){if(a<b){a=b;return 1;}return 0;}
const int INF = (1<<30)-1;
const ll  ILL = 1e15;
#define all(p) p.begin(),p.end()
const int mod=998244353;
using ld = long double;

int main(){
	int N,M,K,S;
	cin>>N>>M>>K>>S;
	vector<vector<int>> A(N,vector<int>(M));
	vector<int> X(N),Y(M);
	int P=0,Q=0;
	auto ch1=[&](int ind)->void{
		int tmp=0,sum=0;
		X[ind]=0;
		rep(i,0,M){
			sum+=A[ind][i];
			chmin(tmp,sum);
			chmax(X[ind],sum-tmp);
		}
		if(X[ind]>S) P++;
	};
	auto ch2=[&](int ind)->void{
		int tmp=0,sum=0;
		Y[ind]=0;
		rep(i,0,N){
			sum+=A[i][ind];
			chmin(tmp,sum);
			chmax(Y[ind],sum-tmp);
		}
		if(Y[ind]>S) Q++;
	};
	rep(i,0,N) rep(j,0,M){
		cin>>A[i][j];
	}
	rep(i,0,N) ch1(i);
	rep(i,0,M) ch2(i);
	if(max(P,Q)>K){
		cout<<"No\n";
		return 0;
	}
	auto op=[&](int x,int y)->void{
		if(X[x]>S) P--;
		if(Y[y]>S) Q--;
		A[x][y]*=-1;
		ch1(x);
		ch2(y);
	};
	auto f=[&](auto self,int ind,int fil)->void{
		//cout<<ind<<" "<<fil<<" "<<P<<" "<<Q<<endl;
		if(max(P,Q)==0){
			cout<<"Yes\n";
			exit(0);
		}
		if(fil==K||ind==N*M) return;
		rep(i,ind,N*M){
			int x=i/M,y=i%M;
			if(X[x]<=S&&Y[y]<=S) continue;
			op(x,y);
			self(self,i+1,fil+1);
			op(x,y);
		}
	};
	f(f,0,0);
	cout<<"No\n";
}
accuracy: 1
time_ms: 800
memory_kb: 3448
final_rank: 13

code_snippet: #ifdef noimi
#include <stdc++.h>
#else
#include <bits/stdc++.h>
#endif

#define ll long long
#define rep(i, n) for(ll i = 0; i < (n); i++)
#define rep2(i, a, b) for(ll i = (a); i < (b); i++)
#define vi vector<int>
using namespace std;

int main() {
    int n, m, k, s;
    cin >> n >> m >> k >> s;

    vector a(n, vi(m));
    rep(i, n) rep(j, m) cin >> a[i][j];

    vi need(n);
    int C = 0;
    rep(i, n) {
        int sum = 0;
        int mi = 0;
        rep(j, m) {
            sum += a[i][j];
            if(sum > mi + s) need[i] = true;
            mi = min(mi, sum);
        }
        C += need[i];
    }

    if(C > k) { cout << "No" << endl, exit(0); }

    auto check = [&] {
        rep(i, n) {
            int sum = 0;
            int mi = 0;
            rep(j, m) {
                sum += a[i][j];
                if(sum > mi + s) return false;
                mi = min(mi, sum);
            }
        }
        rep(j, m) {
            int sum = 0, mi = 0;
            rep(i, n) {
                sum += a[i][j];
                if(sum > mi + s) return false;
                mi = min(mi, sum);
            }
        }
        return true;
    };

    auto dfs = [&](auto &&f, int i, int j, bool ok) -> void {
        if(j == m) {
            if(!need[i] or ok) f(f, i + 1, 0, false);
            return;
        }
        if(i == n) {
            if(check()) { cout << "Yes" << endl, exit(0); }
            return;
        }
        f(f, i, j + 1, ok);
        if(k) {
            k--;
            a[i][j] = -a[i][j];
            f(f, i, j + 1, true);
            a[i][j] = -a[i][j];
            k++;
        }
    };
    dfs(dfs, 0, 0, false);
    cout << "No" << endl;
}
accuracy: 1
time_ms: 330
memory_kb: 3448
final_rank: 8

code_snippet: /**
 *    author:  otera
**/
#include<bits/stdc++.h>
using namespace std;

// #define int long long

using ll = long long;
using ld = long double;
using ull = unsigned long long;
using int128_t = __int128_t;
#define repa(i, n) for(int i = 0; i < n; ++ i)
#define repb(i, a, b) for(int i = a; i < b; ++ i)
#define repc(i, a, b, c) for(int i = a; i < b; i += c)
#define overload4(a, b, c, d, e, ...) e
#define overload3(a, b, c, d, ...) d
#define rep(...) overload4(__VA_ARGS__, repc, repb, repa)(__VA_ARGS__)
#define rep1a(i, n) for(int i = 0; i <= n; ++ i)
#define rep1b(i, a, b) for(int i = a; i <= b; ++ i)
#define rep1c(i, a, b, c) for(int i = a; i <= b; i += c)
#define rep1(...) overload4(__VA_ARGS__, rep1c, rep1b, rep1a)(__VA_ARGS__)
#define rev_repa(i, n) for(int i=n-1;i>=0;i--)
#define rev_repb(i, a, b) assert(a > b);for(int i=a;i>b;i--)
#define rev_rep(...) overload3(__VA_ARGS__, rev_repb, rev_repa)(__VA_ARGS__)
#define rev_rep1a(i, n) for(int i=n;i>=1;i--)
#define rev_rep1b(i, a, b) assert(a >= b);for(int i=a;i>=b;i--)
#define rev_rep1(...) overload3(__VA_ARGS__, rev_rep1b, rev_rep1a)(__VA_ARGS__)
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define eb emplace_back
#define fr first
#define sc second
#define all(c) c.begin(),c.end()
#define rall(c) c.rbegin(), c.rend()
#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define Sort(a) sort(all(a))
#define Rev(a) reverse(all(a))
#define Uniq(a) sort(all(a));a.erase(unique(all(a)),end(a))
#define si(c) (int)(c).size()
inline ll popcnt(ull a){ return __builtin_popcountll(a); }
#define kth_bit(x, k) ((x>>k)&1)
#define unless(A) if(!(A))
ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }
ll intpow(ll a, ll b, ll m) {ll ans = 1; while(b){ if(b & 1) (ans *= a) %= m; (a *= a) %= m; b /= 2; } return ans; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define ULL(...) ull __VA_ARGS__;in(__VA_ARGS__)
#define STR(...) string __VA_ARGS__;in(__VA_ARGS__)
#define CHR(...) char __VA_ARGS__;in(__VA_ARGS__)
#define DBL(...) double __VA_ARGS__;in(__VA_ARGS__)
#define LD(...) ld __VA_ARGS__;in(__VA_ARGS__)
#define vec(type,name,...) vector<type>name(__VA_ARGS__)
#define VEC(type,name,size) vector<type>name(size);in(name)
#define vv(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define VV(type,name,h,w) vector<vector<type>>name(h,vector<type>(w));in(name)
#define vvv(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))
template <class T> using vc = vector<T>;
template <class T> using vvc = vector<vc<T>>;
template <class T> using vvvc = vector<vvc<T>>;
template <class T> using vvvvc = vector<vvvc<T>>;
template <class T> using pq = priority_queue<T>;
template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;
template <class T, class U> using umap = unordered_map<T, U>;
template<class T> void scan(T& a){ cin >> a; }
template<class T> void scan(vector<T>& a){ for(auto&& i : a) scan(i); }
void in(){}
template <class Head, class... Tail> void in(Head& head, Tail&... tail){ scan(head); in(tail...); }
void print(){ cout << ' '; }
template<class T> void print(const T& a){ cout << a; }
template<class T> void print(const vector<T>& a){ if(a.empty()) return; print(a[0]); for(auto i = a.begin(); ++i != a.end(); ){ cout << ' '; print(*i); } }
int out(){ cout << '\n'; return 0; }
template<class T> int out(const T& t){ print(t); cout << '\n'; return 0; }
template<class Head, class... Tail> int out(const Head& head, const Tail&... tail){ print(head); cout << ' '; out(tail...); return 0; }
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0,a1,a2,a3,a4,x,...) x
#define debug_1(x1) cout<<#x1<<": "<<x1<<endl
#define debug_2(x1,x2) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<endl
#define debug_3(x1,x2,x3) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<endl
#define debug_4(x1,x2,x3,x4) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<endl
#define debug_5(x1,x2,x3,x4,x5) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<", "#x5<<": "<<x5<<endl
#ifdef DEBUG
#define debug(...) CHOOSE((__VA_ARGS__,debug_5,debug_4,debug_3,debug_2,debug_1,~))(__VA_ARGS__)
#define dump(...) { print(#__VA_ARGS__); print(":"); out(__VA_ARGS__); }
#else
#define debug(...)
#define dump(...)
#endif

struct io_setup {
    io_setup(int precision = 20) {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cout << fixed << setprecision(precision);
    }
} io_setup_ {};

void solve() {
    INT(n, m, k, s);
    VV(int, a, n, m);
    int cnt = 0;
    auto dfs = [&](auto&&dfs, int i, int j) -> void {
        rep(_, 2) {
            if(_ == 1) {
                if(cnt >= k) continue;
                ++ cnt;
                a[i][j] = -a[i][j];
            }
            bool ok = 1;
            {
                int sum = 0;
                for(int x = i; x >= 0; -- x) {
                    sum += a[x][j];
                    if(sum > s) ok = 0;
                }
            }
            {
                int sum = 0;
                for(int y = j; y >= 0; -- y) {
                    sum += a[i][y];
                    if(sum > s) ok = 0;
                }
            }
            if(ok) {
                if(i == n - 1 && j == m - 1) {
                    out("Yes");
                    exit(0);
                }
                int ni = (j == m - 1 ? i + 1 : i);
                int nj = (j == m - 1 ? 0 : j + 1);
                dfs(dfs, ni, nj);
            }
            if(_ == 1) {
                -- cnt;
                a[i][j] = -a[i][j];
            }
        }
    };
    dfs(dfs, 0, 0);
    out("No");
}

signed main() {
    int testcase = 1;
    // in(testcase);
    while(testcase--) solve();
    return 0;
}
accuracy: 1
time_ms: 730
memory_kb: 3460
final_rank: 11

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define eps 1e-9
#define INF 2000000000              // 2e9
#define LLINF 2000000000000000000ll // 2e18 (llmax:9e18)
#define all(x) (x).begin(), (x).end()
#define sq(x) ((x) * (x))

#define rep(i, x) for (int i = 0; i < (int)(x); i++)
#define drep(i, x) for (int i = ((int)(x)-1); i >= 0; i--)

#define popcount __builtin_popcount
#define next __next
#define prev __prev

#ifndef LOCAL
#define dmp(...) ;
#else
#define dmp(...)                                                               \
  cerr << "[ " << #__VA_ARGS__ << " ] : " << dump_str(__VA_ARGS__) << endl
#endif

// ---------------- Utility ------------------

template <class T>
bool chmin(T &a, const T &b) {
  if (a <= b) return false;
  a = b;
  return true;
}

template <class T>
bool chmax(T &a, const T &b) {
  if (a >= b) return false;
  a = b;
  return true;
}

template <class T>
using MaxHeap = priority_queue<T>;

template <class T>
using MinHeap = priority_queue<T, vector<T>, greater<T>>;

template <class T>
vector<T> vect(int len, T elem) {
  return vector<T>(len, elem);
}

// ----------------- Input -------------------

template <class T, class U>
istream &operator>>(istream &is, pair<T, U> &p) {
  return is >> p.first >> p.second;
}

template <class T>
istream &operator>>(istream &is, vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) is >> vec[i];
  return is;
}

// ----------------- Output ------------------

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << p.first << ',' << p.second;
}

template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (const T &e : v) os << e << " ";
  return os;
}

template <class T>
ostream &operator<<(ostream &os, const deque<T> &d) {
  for (const T &e : d) os << e << " ";
  return os;
}

template <class T>
ostream &operator<<(ostream &os, const set<T> &s) {
  os << "{ ";
  for (const T &e : s) os << e << " ";
  return os << "}";
}

template <class T, class U>
ostream &operator<<(ostream &os, const map<T, U> &m) {
  os << "{ ";
  for (const auto &[key, val] : m) os << "( " << key << " -> " << val << " ) ";
  return os << "}";
}

template <class TupleTy, size_t... I>
void dump_tuple(ostream &os, const TupleTy t, std::index_sequence<I...>) {
  (..., (os << (I == 0 ? "" : ",") << std::get<I>(t)));
}

template <class... Args>
ostream &operator<<(ostream &os, const tuple<Args...> &t) {
  os << "(";
  dump_tuple(os, t, std::make_index_sequence<sizeof...(Args)>());
  return os << ")";
}

void dump_str_rec(ostringstream &) {}

template <class Head, class... Tail>
void dump_str_rec(ostringstream &oss, Head &&head, Tail &&... tail) {
  oss << ", " << head;
  dump_str_rec(oss, forward<Tail>(tail)...);
}

template <class T, class... U>
string dump_str(T &&arg, U &&... args) {
  ostringstream oss;
  oss << arg;
  dump_str_rec(oss, forward<U>(args)...);
  return oss.str();
}

// --------------- Fast I/O ------------------

void fastio() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout << fixed << setprecision(20);
}

// ------------------ ACL --------------------

// #include <atcoder/modint>
// constexpr int MOD = 998244353;
// constexpr int MOD = 1000000007;
// using mint = atcoder::static_modint<MOD>;
// ostream &operator<<(ostream &os, const mint &v) {
//   return os << v.val();
// }

// ------------ End of template --------------

#define endl "\n"
using ll = long long;
using pii = pair<int, int>;

const bool multitest = false;

int N, M, K, S;
int cnt = 0;

void solve() {
  cin >> N >> M >> K >> S;
  auto A = vect(N, vect(M, 0));
  cin >> A;

  bool ans = false;

  auto check = [&]() {
    for (int i = 0; i < N; i++) {
      int mi = 0, rui = 0;
      for (int j = 0; j < M; j++) {
        rui += A[i][j];
        chmin(mi, rui);
        if (rui - mi > S) return i;
      }
    }
    for (int j = 0; j < M; j++) {
      int mi = 0, rui = 0;
      for (int i = 0; i < N; i++) {
        rui += A[i][j];
        chmin(mi, rui);
        if (rui - mi > S) return N + j;
      }
    }
    return -1;
  };

  function<void(int)> dfs = [&](int rem) {
    int state = check();
    if (state == -1) {
      ans = true;
      return;
    }
    if (rem == 0) return;
    if (state < N) {
      int i = state;
      for (int j = 0; j < M; j++) {
        A[i][j] *= -1;
        dfs(rem - 1);
        A[i][j] *= -1;
      }
    }
    if (state >= N) {
      int j = state - N;
      assert(j >= 0 && j < M);
      for (int i = 0; i < N; i++) {
        A[i][j] *= -1;
        dfs(rem - 1);
        A[i][j] *= -1;
      }
    }
    return;
  };

  dfs(K);

  if (ans)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;

  return;
}

int main() {
  fastio();
  if (!multitest) {
    solve();
  } else {
    cerr << "[Warning] Multi testcase mode on" << endl;
    int t;
    cin >> t;
    while (t--) solve();
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3512
final_rank: 1

code_snippet: #pragma GCC ta g("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define DEBUG
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for(int i = 0; i < (int)v.size(); i++) {
        if(i) { os << ','; }
        os << v[i];
    }
    os << '}';
    return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
    cerr << " " << x;
    debugg(args...);
}
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = (1<<30)-1;
const ll linf = 1LL<<61;
const int MAX = 510000;
int dy[8] = {0,1,0,-1,1,-1,-1,1};
int dx[8] = {-1,0,1,0,1,-1,1,-1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    int sz = a.size();
    for(auto itr = a.begin(); itr != a.end(); itr++){
		cout << *itr;
        sz--;
        if(sz) cout << " ";
	}
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){
	cout << a << " " << b << "\n";
}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << "\n";
}
void mark() {cout << "#" << "\n";}
ll pcount(ll x) {return __builtin_popcountll(x);}
//#include <atcoder/maxflow>
//using namespace atcoder;
//const int mod = 1e9 + 7;
const int mod = 998244353;

int main(){
    int n,m,K,s; cin >> n >> m >> K >> s;
    vector<vector<int>> a(n,vector<int>(m));
    rep(i,n) rep(j,n) cin >> a[i][j];
    bool yes = false;
    rep(k,K){
        vector<int> nxt(n*m,0);
        rep(i,min(n*m,k)) nxt[n*m-i-1] = 1;
        do{
            rep(i,n) rep(j,m) if(nxt[i*m+j]) a[i][j] = -a[i][j];
            bool f = true;
            int rowid = -1, rowrest = -1;
            int colid = -1, colrest = -1;
            rep(i,n){
                int sum = 0;
                rep(j,m) sum += a[i][j];
                if(sum > s){
                    if(rowid != -1){
                        f = false;
                        break;
                    }
                    rowid = i;
                    rowrest = sum - s;
                }
            }
            rep(j,m){
                int sum = 0;
                rep(i,n) sum += a[i][j];
                if(sum > s){
                    if(colid != -1){
                        f = false;
                        break;
                    }
                    colid = j;
                    colrest = sum - s;
                }
            }
            if(f){
                if(rowid == -1){
                    if(colid == -1) yes = true;
                    else{
                        int mx = -inf;
                        rep(i,n) chmax(mx, a[i][colid]);
                        if(colrest - mx*2 <= 0) yes = true;
                    }
                }else{
                    if(colid == -1){
                        int mx = -inf;
                        rep(j,m) chmax(mx, a[rowid][j]);
                        if(rowrest - mx*2 <= 0) yes = true; 
                    }else{
                        if(rowrest - a[rowid][colid]*2 <= 0 && colrest - a[rowid][colid]*2 <= 0) yes = true;
                    }
                }
                if(yes) break;
            }
            rep(i,n) rep(j,m) if(nxt[i*m+j]) a[i][j] = -a[i][j];
        }while(next_permutation(all(nxt)));
    }
    if(yes) cout << "Yes\n";
    else cout << "No\n";
}
accuracy: 0.36363636363636365
time_ms: 1060
memory_kb: 3424
final_rank: 20

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=998244353,MAX=13;
const ll INF=1LL<<62;

int H,W,K,S;
int A[MAX][MAX],tate[MAX][MAX],yoko[MAX][MAX];

void solve(int h,int w,int rem){
    int lh,lw;
    if(w==1){
        lh=h-1;
        lw=W;
    }else{
        lh=h;
        lw=w-1;
    }
    for(int j=0;j<lw;j++) if(yoko[lh][lw]-yoko[lh][j]>S) return;
    for(int i=0;i<lh;i++) if(tate[lh][lw]-tate[i][lw]>S) return;
    
    if(h==H+1){
        cout<<"Yes\n";
        exit(0);
    }
    
    if(rem){
        tate[h][w]=tate[h-1][w]-A[h][w];
        yoko[h][w]=yoko[h][w-1]-A[h][w];
        if(w==W) solve(h+1,1,rem-1);
        else solve(h,w+1,rem-1);
    }
    tate[h][w]=tate[h-1][w]+A[h][w];
    yoko[h][w]=yoko[h][w-1]+A[h][w];
    if(w==W) solve(h+1,1,rem);
    else solve(h,w+1,rem);
}

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    cin>>H>>W>>K>>S;
    for(int i=1;i<=H;i++){
        for(int j=1;j<=W;j++){
            cin>>A[i][j];
        }
    }
    
    solve(1,1,K);
    
    cout<<"No\n";
}
accuracy: 1
time_ms: 670
memory_kb: 3468
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define R cin>>
#define Z class
#define ll long long
#define ln cout<<'\n'
#define in(a) insert(a)
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\n",a)
#define mem(a) memset(a,0,sizeof(a))
#define all(c) (c).begin(),(c).end()
#define iter(c) __typeof((c).begin())
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define REP(i,m,n) for(int i=(int)(m);i<(int)(n);i++)
#define rep(i,n) REP(i,0,n)
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
template<Z A>void pr(A a){cout<<a;ln;}
template<Z A,Z B>void pr(A a,B b){cout<<a<<' ';pr(b);}
template<Z A,Z B,Z C>void pr(A a,B b,C c){cout<<a<<' ';pr(b,c);}
template<Z A,Z B,Z C,Z D>void pr(A a,B b,C c,D d){cout<<a<<' ';pr(b,c,d);}
template<Z A>void PR(A a,ll n){rep(i,n){if(i)cout<<' ';cout<<a[i];}ln;}
ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}
const ll MAX=1000000007,MAXL=1LL<<61,dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
typedef pair<int,int> P;
int n,m,t,s;
int v[11];
int a[11][11],d[11],c[11],d2[11],c2[11],cnt,cnt2,ans,ans2;

void dfs(int k,int l) {
  if(l==t) return;
  REP(i,k,n*m) {
    v[l]=i;
    int x=v[l]/m,y=v[l]%m;
    bool f1=d[x]>s,f2=c[y]>s,ff1=d2[x]>s,ff2=c2[y]>s;
    d[x]-=a[x][y]*2;
    c[y]-=a[x][y]*2;
    int z=0,z2=0;
    z-=(f1&&d[x]<=s)+(f2&&c[y]<=s);
    z+=(!f1&&d[x]>s)+(!f2&&c[y]>s);
    cnt+=z;
    d2[x]-=a[x][y];
    c2[y]-=a[x][y];
    z2-=(ff1&&d2[x]<=s)+(ff2&&c2[y]<=s);
    z2+=(!ff1&&d2[x]>s)+(!ff2&&c2[y]>s);
    cnt2+=z2;
    if(cnt<=0) ans=1;
    if(cnt2<=0) ans2=1;
    dfs(i+1,l+1);
    cnt-=z;
    d[x]+=a[x][y]*2;
    c[y]+=a[x][y]*2;
    cnt2-=z2;
    d2[x]+=a[x][y];
    c2[y]+=a[x][y];
  }
}
void Main() {
  cin >> n >> m >> t >> s;
  int x=0;
  rep(i,n) {
    rep(j,m) {
      R a[i][j];
      d[i]+=a[i][j];
      c[j]+=a[i][j];
      d2[i]+=a[i][j];
      c2[j]+=a[i][j];
      if(a[i][j]>s) x++;
    }
  }
  if(x>t) {
    pr("No");
    return;
  }
  rep(i,n) cnt+=d[i]>s;
  rep(i,m) cnt+=c[i]>s;
  cnt2=cnt;
  if(cnt<=0) ans=1;
  dfs(0,0);
  random_device r;
  int ans3=r()%2?ans:ans2;
  if(ans3) pr("Yes");
  else pr("No");
}

int main(){ios::sync_with_stdio(0);cin.tie(0);Main();return 0;}
accuracy: 1
time_ms: 1050
memory_kb: 3464
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;
bool dfs(vector<vector<int>> &A, int K, int S){
  int N = A.size();
  int M = A[0].size();
  for (int i = 0; i < 2; i++){
    for (int j = 0; j < N; j++){
      int dp = 0;
      for (int k = 0; k < M; k++){
        dp = max(dp + A[j][k], 0);
        if (dp > S){
          if (K == 0){
            return false;
          } else {
            for (int l = 0; l <= k; l++){
              if (A[j][l] > 0){
                vector<vector<int>> A2 = A;
                A2[j][l] *= -1;
                if (dfs(A2, K - 1, S)){
                  return true;
                }
              }
            }
            return false;
          }
        }
      }
    }
    vector<vector<int>> A2(M, vector<int>(N));
    for (int j = 0; j < N; j++){
      for (int k = 0; k < M; k++){
        A2[k][j] = A[j][k];
      }
    }
    swap(N, M);
    swap(A, A2);
  }
  return true;
}
int main(){
  int N, M, K, S;
  cin >> N >> M >> K >> S;
  vector<vector<int>> A(N, vector<int>(M));
  for (int i = 0; i < N; i++){
    for (int j = 0; j < M; j++){
      cin >> A[i][j];
    }
  }
  if (dfs(A, K, S)){
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
}
accuracy: 1
time_ms: 50
memory_kb: 3132
final_rank: 4