import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Price {
public static void main(String[] args) {
cal(11000, 2384, 1460, 2620, 1400, 1400, 5480);
}
private static void cal(int target, Integer... prices) {
var list = cal(target, Stream.of(prices).sorted().collect(Collectors.toList()));
System.out.println(list);
System.out.println(list.stream().mapToInt(Integer::intValue).sum());
}
private static List<Integer> cal(int target, List<Integer> remain) {
var remainSum=remain.stream().mapToInt(Integer::intValue).sum();
if (remainSum < target) {
return null;
}
var first = remain.remove(0);
if (first >= target) {
List<Integer> out = new ArrayList<>();
out.add(first);
return out;
}
var yes = cal(target-first, new ArrayList<>(remain));
var no = cal(target, new ArrayList<>(remain));
if (null == yes) {
return no;
}
yes.add(first);
if(null==no){
return yes;
}
if (yes.stream().mapToInt(Integer::intValue).sum() > no.stream().mapToInt(Integer::intValue).sum()) {
return no;
} else {
return yes;
}
}
}
10-04
742
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)