# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
527001 | lcs147 | Detecting Molecules (IOI16_molecules) | C++98 | 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<bits/stdc++.h>
using namespace std;
vector<int>find_subset(int l, int r, vector<int> w) {
if(l > r) return {};
vector<int>id(w.size());
iota(id.begin(), id.end(), 0);
sort(id.begin(), id.end(), [&](int a, int b){
return w[a] < w[b];
});
while(id.size() && w[id.back()] > r) id.pop_back();
int sum = 0;
vector<int>has_sum(r+1, -1);
int en = -1;
while(1) {
if(sum != 0) {
while(sum <= r && has_sum[sum]==-1) sum++;
}
if(sum > r) return {};
if(sum >= l) {
en = sum;
break;
}
for(int i = has_sum[sum]+1; i<id.size(); i++) {
int nsum = sum + w[id[i]];
if(nsum > r) break;
has_sum[nsum] = i;
}
sum++;
}
sum = en;
vector<int>res;
while(sum > 0) {
int rid = id[has_sum[sum]];
res.push_back(rid);
sum -= w[rid];
}
sort(res.begin(), res.end());
return res;
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
#define endl '\n'
int n, l, r; cin >> n >> l >> r;
vector<int>w(n);
for(int i=0; i<n; i++) {
cin >> w[i];
}
auto res = find_subset(l, r, w);
for(int x:res) cout << x << " ";
cout << endl;
return 0;
}