import java.util.*;
class KnapsackProblem {
public static double getMaxKnapsackValue(int[] weights, int[] values, int capacity) {
int itemNumber = weights.length;
Item[] items = new Item[itemNumber];
// 1. Create an array of items with weight and value pairs
for (int i = 0; i < itemNumber; i++) {
items[i] = new Item(weights[i], values[i]);
}
// 2. Sort items based on the value per weight ratio
Comparator<Item> comparator = Comparator.comparingDouble(Item::getRatio).reversed();
Arrays.sort(items, comparator);
double totalValue = 0.0;
int remainingCapacity = capacity;
// 3. Fill the knapsack with items
for (Item item : items) {
if (remainingCapacity >= item.weight) {
// 3.1 Put the whole item
// !!! DO NOT CHANGE THE CODE ABOVE !!!
totalValue = ???
remainingCapacity = ???
// !!! DO NOT CHANGE THE CODE BELOW !!!
} else {
// 3.2 Take a fraction possible capacity of the item and put it
// Break the cycle when put last fractional item
double fractionCapacity = (double) remainingCapacity / item.weight;
totalValue += fractionCapacity * item.value;
break;
}