#include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <queue>
#include <map>
#include <stack>
#include <bitset>
#include <string>
#include <cstring>
#include <iterator>
#include <random>
#define ll long long
#define ld long double
#define inf (ll)(2*1e18)
#define sort(a) sort(a.begin(), a.end())
#define reverse(a) reverse(a.begin(), a.end())
#define pb push_back
#define endl "\n"
using namespace std;
void solve(){
    ll n, k, i, j, x, res=0;
    cin>>n>>k;
    vector<ll> p(k);
    vector<ll> cnt(n);
    vector<vector<pair<ll, ll>>> a(k);
    vector<vector<ll>> b(n, vector<ll>(k));
    queue<ll> next;
    for(i=0;i<n;++i){
        for(j=0;j<k;++j){
            cin>>x;
            if(x == 0){
                ++cnt[i];
            }else{
                a[j].pb({x, i});
            }
        }
        if(cnt[i] == k){
            next.push(i);
        }
    }
    for(i=0;i<k;++i){
        sort(a[i]);
        reverse(a[i]);
    }
    for(i=0;i<n;++i){
        for(j=0;j<k;++j){
            cin>>b[i][j];
        }
    }
    while(!next.empty()){
        ++res;
        x = next.front();
        next.pop();
        for(i=0;i<k;++i){
            p[i] += b[x][i];
            while(!a[i].empty() and a[i].back().first <= p[i]){
                ++cnt[a[i].back().second];
                if(cnt[a[i].back().second] == k){
                    next.push(a[i].back().second);
                }
                a[i].pop_back();
            }
        }
    }
    cout<<res<<endl;
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    srand(time(nullptr));
    ll t=1;
//    cin>>t;
    for(;t>0;--t){
        solve();
    }
    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... |