fork(31) download
  1. public class Main {
  2. public static void main(String[] args) {
  3. final int[] weights = {1, 1, 1, 5, 13, 3}, costs = {1, 1, 1, 5, 10, 12};
  4. final int minWeight = 15;
  5. int maxWeight = 0;
  6. for(final int weight: weights){
  7. maxWeight += weight;
  8. }
  9. final int[] minCost = new int[maxWeight + 1];
  10. for(int i = 1; i <= maxWeight; i++){
  11. minCost[i] = Integer.MAX_VALUE;
  12. }
  13. for(int i = 0; i < weights.length; i++){
  14. for(int j = maxWeight; j >= weights[i]; j--){
  15. if(minCost[j - weights[i]] != Integer.MAX_VALUE){
  16. minCost[j] = Math.min(minCost[j], minCost[j - weights[i]] + costs[i]);
  17. }
  18. }
  19. }
  20. int answer = Integer.MAX_VALUE;
  21. for(int i = minWeight; i <= maxWeight; i++){
  22. answer = Math.min(answer, minCost[i]);
  23. }
  24. System.out.println(answer);
  25. }
  26. }
Success #stdin #stdout 0.07s 32380KB
stdin
Standard input is empty
stdout
12