/* 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
{
public static void main
(String[] args
) {
Set<Integer> unv = new HashSet<>();
unv.add(1);
unv.add(2);
unv.add(3);
unv.add(4);
unv.add(5);
Set<Integer> s1 = new HashSet<>();
s1.add(4);
s1.add(1);
s1.add(3);
Set<Integer> s2 = new HashSet<>();
s2.add(2);
s2.add(5);
Set<Integer> s3 = new HashSet<>();
s3.add(1);
s3.add(4);
s3.add(3);
s3.add(2);
{
s1, s2, s3
};
Map
<Set, Integer
> costs
= new HashMap
<>(); costs.put(s1, 5);
costs.put(s2, 10);
costs.put(s3, 30);
System.
out.
println(minCostCollection
(unv, sets, costs,
new ArrayList
<Set
>(), sets.
length - 1)); }
public static int minCostCollection
(Set
<Integer
> unv, Set
<Integer
>[] sets, Map
<Set, Integer
> costs, List
<Set
> list,
int pos)
{
if (unv.size() == 0)
{
int cost = 0;
{
cost = cost + costs.get(s);
}
return cost;
}
if (pos < 0)
Set<Integer> unvCopy = new HashSet<>(unv);
List<Set> list1 = new ArrayList<>(list);
list.add(sets[pos]);
{
unv.remove(elem);
}
int cost1 = minCostCollection(unv, sets, costs, list, pos - 1);
int cost2 = minCostCollection(unvCopy, sets, costs, list1, pos - 1);
return Math.
min(cost1, cost2
); }
}
LyogcGFja2FnZSB3aGF0ZXZlcjsgLy8gZG9uJ3QgcGxhY2UgcGFja2FnZSBuYW1lISAqLwoKaW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CmltcG9ydCBqYXZhLmlvLio7CgovKiBOYW1lIG9mIHRoZSBjbGFzcyBoYXMgdG8gYmUgIk1haW4iIG9ubHkgaWYgdGhlIGNsYXNzIGlzIHB1YmxpYy4gKi8KY2xhc3MgSWRlb25lCnsKICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKQogIHsKICAgIFNldDxJbnRlZ2VyPiB1bnYgPS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