// soab
#include <bits/stdc++.h>
using namespace std;
#define int long long 
#define nl '\n'
void io() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
}   
const int maxn = 1e6 + 1;
int n, k, p[maxn] = {0};
int cnt = 0, st;
bool valid;
int sub1() {
    return valid? 1 : 0;
}
int sub2(vector<vector<int>>& r, vector<vector<int>>& u) {
    int ans = 0;
    bool run = 1;
    vector<bool> vis(maxn, 0);
    vector<int> p(k + 1, 0);
    while(run) {
        run = 0;
        for(int i = 1; i <= n; i++) {
            bool flg = 1;
            if(vis[i]) continue;
            for(int j = 1; j <= k; j++) {
                if(r[i][j] > p[j]) {
                    flg = 0;
                    break;
                }
            }
            if(flg) {
                run = 1;
                vis[i] = 1;
                ans++;
                for(int j = 1; j <= k; j++) p[j] += u[i][j];
            } 
        }
    }
    return ans;
}
int sub3(vector<vector<int>>& r, vector<vector<int>>& u) {
    int ans, p = 0;
    sort(r.begin(), r.end());
    for(int i = 1; i <= n; i++) {
        if(r[i][1] <= p) {
            p += u[i][0];
            ans++;
        }
    }
    return ans;
}
signed main() {
    io();
    cin >> n >> k;
    vector<vector<int>> r(n + 1, vector<int>(k + 1));
    vector<vector<int>> u(n + 1, vector<int>(k + 1));
    // int r[n + 1][k + 1], u[n + 1][k + 1];
    for(int i = 1; i <= n; i++) {
        bool temp = 1;
        for(int j = 1; j <= k; j++) {
            cin >> r[i][j];
            if(r[i][j] != 0) temp = 0;
        }
        valid = valid || temp;
    }
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= k; j++) cin >> u[i][j];
    }
    if(!valid) return cout << 0, 0;
    if(n == 1) {
        cout << sub1();
    } else {
        if(!valid) return cout << 0, 0;
        if(k == 1) {
            sub3(r, u);
        } else if(n <= 100 && k <= 100) {
            cout << sub2(r, u);
        }
    }
    return 0;
}   
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |