# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1172519 | versesrev | Detecting Molecules (IOI16_molecules) | C++20 | 0 ms | 0 KiB |
// 21:19
#include <vector>
int[] find_subset(int low, int up, int w[]) {
int n = sizeof(w) / sizeof(int);
std::vector<std::vector<bool>> back(n, std::vector<int>(up + 1, -1));
back[0][0] = 0;
if (w[0] <= up) back[0][w[0]] = 0;
for (int i = 1; i < n; ++i) {
for (int j = 0; j <= up; ++j) {
if (w[i] <= j and back[i - 1][j - w[i]] != -1) {
back[i][j] = j - w[i];
}
else if (back[i - 1][j] != -1) {
back[i][j] = j;
}
else back[i][j] = -1;
}
}
std::vector<int> ans;
int start_j = -1;
int start_i = -1;
for (int i = 0; i < n and start_i == -1; ++i) {
for (int j = low ; start_j == -1 and j <= up; ++j) {
if (back[i][j] != -1) {
start_i = i;
start_j = j;
}
}
}
if (start_i == -1) {
int ary[0];
return ary;
}
int cur_i = start_i;
int cur_j = start_j;
while (cur_j != 0) {
if (back[cur_i][cur_j] == cur_j) {
cur_i -= 1;
}
else {
ans.push_back(cur_i);
cur_j = back[cur_i][cur_j];
cur_i -= 1;
}
}
int ary[ans.size()];
for (int i = 0; i < ans.size(); ++i) ary[i] = ans[i];
return ary;
}