/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
List<List<Integer>> sodaPacks(int[] packs, int n, int index) {
List<List<Integer>> result = new ArrayList<>();
// Base case: we have one way to make an empty list
if (n == 0) {
result.add(new ArrayList<>());
return result;
}
// Recursive step: we solve a smaller problem
for (int i = index; i < packs.length; i++) {
if (n - packs[i] >= 0) {
List<List<Integer>> ways = sodaPacks(packs, n - packs[i], i);
for (List<Integer> way : ways) {
// Tack on our solution:
way.add(packs[i]);
result.add(way);
}
}
}
return result;
}
{
Scanner sc
= new Scanner
(System.
in); int n = sc.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
int sodas = sc.nextInt();
Ideone solution = new Ideone();
System.
out.
println(solution.
sodaPacks(a, sodas,
0)); sc.close();
}
}
LyogcGFja2FnZSB3aGF0ZXZlcjsgLy8gZG9uJ3QgcGxhY2UgcGFja2FnZSBuYW1lISAqLwoKaW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CmltcG9ydCBqYXZhLmlvLio7CgovKiBOYW1lIG9mIHRoZSBjbGFzcyBoYXMgdG8gYmUgIk1haW4iIG9ubHkgaWYgdGhlIGNsYXNzIGlzIHB1YmxpYy4gKi8KY2xhc3MgSWRlb25lCnsKCUxpc3Q8TGlzdDxJbnRlZ2VyPj4gc29kYVBhY2tzKGludFtdIHBhY2tzLCBpbnQgbiwgaW50IGluZGV4KSB7CgkJTGlzdDxMaXN0PEludGVnZXI+PiByZXN1bHQgPSBuZXcgQXJyYXlMaXN0PD4oKTsKCSAgCgkgIC8vIEJhc2UgY2FzZTogd2UgaGF2ZSBvbmUgd2F5IHRvIG1ha2UgYW4gZW1wdHkgbGlzdAoJICBpZiAobiA9PSAwKSB7CgkgIAlyZXN1bHQuYWRkKG5ldyBBcnJheUxpc3Q8PigpKTsKCSAgCXJldHVybiByZXN1bHQ7CgkgIH0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