/* 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 i) {
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 each coin, you either include it or exclude it
// Include it:
if (n - packs[i] >= 0) {
List<List<Integer>> include = sodaPacks(packs, n - packs[i], i);
// If we include it, we must tack it on:
for (List<Integer> list : include) {
// Tack on our solution:
list.add(packs[i]);
result.add(list);
}
}
// Exclude it:
if (i + 1 < packs.length) {
List<List<Integer>> exclude = sodaPacks(packs, n, i + 1);
result.addAll(exclude);
}
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();
}
}
LyogcGFja2FnZSB3aGF0ZXZlcjsgLy8gZG9uJ3QgcGxhY2UgcGFja2FnZSBuYW1lISAqLwoKaW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CmltcG9ydCBqYXZhLmlvLio7CgovKiBOYW1lIG9mIHRoZSBjbGFzcyBoYXMgdG8gYmUgIk1haW4iIG9ubHkgaWYgdGhlIGNsYXNzIGlzIHB1YmxpYy4gKi8KY2xhc3MgSWRlb25lCnsKCUxpc3Q8TGlzdDxJbnRlZ2VyPj4gc29kYVBhY2tzKGludFtdIHBhY2tzLCBpbnQgbiwgaW50IGkpIHsKCQlMaXN0PExpc3Q8SW50ZWdlcj4+IHJlc3VsdCA9IG5ldyBBcnJheUxpc3Q8PigpOwoJICAKCSAgLy8gQmFzZSBjYXNlOiB3ZSBoYXZlIG9uZSB3YXkgdG8gbWFrZSBhbiBlbXB0eSBsaXN0CgkgIGlmIChuID09IDApIHs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