# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
416884 | snasibov05 | Detecting Molecules (IOI16_molecules) | C++14 | 0 ms | 0 KiB |
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 "molecules.h"
#include <algorithm>
using namespace std;
#define pb push_back
bool dp[2][10000][500000];
void knapsack(vector<int>& w, int sum){
int n = w.size();
if (w[0] <= sum) dp[1][0][w[0]] = true;
dp[0][0][0] = true;
for (int i = 0; i < n - 1; ++i) {
for (int j = 0; j <= sum; ++j) {
if (dp[0][i][j] || dp[1][i][j]){
dp[0][i+1][j] = true;
if (j + w[i+1] <= sum) dp[1][i+1][j + w[i+1]] = true;
}
}
}
}
vector<int> find_subset(int l, int u, vector<int> w) {
int n = w.size();
knapsack(w, u);
int res = -1;
for (int i = l; i <= u; ++i) {
if (dp[1][n-1][i] || dp[0][n-1][i]) {
res = i;
break;
}
}
vector<int> ans;
if (res == -1) return ans;
for (int i = n-1; i >= 0; --i){
if (dp[1][i][res]){
ans.pb(i);
res -= w[i];
}
}
reverse(ans.begin(), ans.end());
return ans;
}