#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define nn '\n'
int dfs(vector<vector<int>>& r, vector<vector<int>>& u, vector<int>& p, vector<bool>& used, int n, int k) {
int max_completed = 0;
for (int i = 0; i < n; ++i) {
if (used[i]) continue;
bool can_do = true;
for (int j = 0; j < k; ++j) {
if (p[j] < r[i][j]) {
can_do = false;
break;
}
}
if (can_do) {
used[i] = true;
for (int j = 0; j < k; ++j)
p[j] += u[i][j];
max_completed = max(max_completed, 1 + dfs(r, u, p, used, n, k));
for (int j = 0; j < k; ++j)
p[j] -= u[i][j];
used[i] = false;
}
}
return max_completed;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k;
if (n == 1) {
vector<vector<int>> r(n, vector<int>(k));
vector<vector<int>> u(n, vector<int>(k));
for (int i = 0; i < n; i++)
for (int j = 0; j < k; j++)
cin >> r[i][j];
for (int i = 0; i < n; i++)
for (int j = 0; j < k; j++)
cin >> u[i][j];
bool can_do = true;
for (int j = 0; j < k; j++) {
if (r[0][j] > 0) {
can_do = false;
break;
}
}
cout << (can_do ? 1 : 0) << nn;
}
else if (k == 1) {
vector<int> a(n), b(n);
vector<pair<int, int>> p;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) cin >> b[i];
for (int i = 0; i < n; i++) p.pb({a[i], b[i]});
sort(p.begin(), p.end());
int cnt = 0, sum = 0;
for (int i = 0; i < n; i++) {
if (sum >= p[i].first) {
sum += p[i].second;
cnt++;
}
}
cout << cnt << nn;
}
else {
// General case: use recursion
vector<vector<int>> r(n, vector<int>(k));
vector<vector<int>> u(n, vector<int>(k));
for (int i = 0; i < n; i++)
for (int j = 0; j < k; j++)
cin >> r[i][j];
for (int i = 0; i < n; i++)
for (int j = 0; j < k; j++)
cin >> u[i][j];
vector<int> p(k, 0);
vector<bool> used(n, false);
cout << dfs(r, u, p, used, n, k) << nn;
}
}
| # | 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... |