#P6169. Molecule Subset Sum Detection
Molecule Subset Sum Detection
Molecule Subset Sum Detection
Peter works at a company that has manufactured a machine to detect molecules. Each molecule has a positive integer weight. The machine can detect a collection of molecules if and only if there exists a subset of these molecules such that the total weight of the subset lies in the detection range \([l,u]\), where \(l\) and \(u\) are positive integers and all operations involving these numbers adhere to the constraint \(l \leq w_{i_1}+\cdots+w_{i_m} \leq u\).
Formally, given \(n\) molecules with weights \(w_0, w_1, \dots, w_{n-1}\), if there exists an index set \(I = \{i_1, i_2, \dots, i_m\}\) (with all indices distinct) such that \[ l \leq w_{i_1}+w_{i_2}+\cdots+w_{i_m} \leq u, \] then the detection is successful.
An additional machine constraint guarantees that the range width satisfies \[ u - l \geq w_{\max} - w_{\min}, \] where \(w_{\max} = \max(w_0, w_1, \dots, w_{n-1})\) and \(w_{\min} = \min(w_0, w_1, \dots, w_{n-1})\).
Your task is to write a program that finds a subset of molecules whose total weight lies in the detection range \([l,u]\), or determines that no such subset exists.
Note: If a valid subset is found, output two lines: the first line contains the number of molecules in the subset, and the second line contains the 1-indexed positions of these molecules (separated by a space). If no valid subset exists, output a single line containing 0
.
inputFormat
The first line of input contains three positive integers: n
(the number of molecules), l
and u
(the lower and upper bounds of the detection range respectively). The second line contains n
positive integers representing the weights of the molecules.
outputFormat
If a valid subset exists, output two lines: the first line contains the number of molecules in the subset, and the second line contains the 1-indexed indices of the molecules (separated by spaces). If multiple answers exist, output any. If no such subset exists, output 0
in a single line.
sample
4 15 17
6 8 8 7
2
1 3
</p>