This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define _files
#define _multitest
#define _Debug
using namespace std;
void just_do_it();
int main() {
    #define task ""
    #ifdef _files_
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    #endif
    #ifdef _Debug
        ios_base::sync_with_stdio(0);
    #endif
    cin.tie(0);
    just_do_it();
    return 0;
}
#define __builtin_popcount __builtin_popcountll
#define BIT(x, i) (((x)>> (i))& (1LL))
#define MASK(x) (1LL<< (x))
#define MOD 1000000007
#define ll long long
const int maxm= (int)1e6, maxn= (int)1e5, maxb= (int)1e3;
ll INF= (ll)1e18;
int n, k, ans;
ll p[maxm+ 2];
pair<int, int> a[maxm+ 2];
bool used[maxm+ 2];
void input(){
}
void prepare(){
}
void solve(){
    cin>> n>> k;
    vector< vector<int> > r(n+ 1, vector<int>(k+ 1)), u(n+ 1, vector<int>(k+ 1));
    for(int i= 1; i<= n; i++){
        for(int j= 1; j<= k; j++){
            cin>> r[i][j];
        }
    }
    for(int i= 1; i<= n; i++){
        for(int j= 1; j<= k; j++){
            cin>> u[i][j];
        }
    }
    if(n== 1){
        for(int j= 1; j<= k; j++){
            if(r[1][j]> 0){
                cout<< 0;
                return;
            }
        }
        cout<< 1;
    }
    else if(k== 1){
        for(int i= 1; i<= n; i++){
            a[i]= make_pair(r[i][1], u[i][1]);
        }
        sort(a+ 1, a+ n+ 1);
        ll power= 0;
        ans= 0;
        for(int i= 1; i<= n; i++){
            if(power>= a[i].first){
                power+= a[i].second;
                ans++;
            }
        }
        cout<< ans;
    }
    else{
        ans= 0;
        for(int t= 1; t<= n; t++){
            for(int i= 1; i<= n; i++){
                if(used[i]) continue;
                int cnt= 0;
                for(int j= 1; j<= k; j++){
                    cnt+= (p[j]>= r[i][j]);
                }
                if(cnt== k){
                    for(int j= 1; j<= k; j++){
                        p[j]+= u[i][j];
                    }
                    used[i]= true;
                    ans++;
                }
            }
        }
        cout<< ans;
    }
}
void printans(){
}
void reset(){
}
void test(){
    input();
    prepare();
    solve();
    printans();
    reset();
}
void just_do_it() {
    int nTest= 1;
    #ifdef _multitest_
        cin>>nTest;
    #endif
    for (int it= 1; it <= nTest; it++) {
        test();
    }
}
| # | 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... |