Submission #1219969

#TimeUsernameProblemLanguageResultExecution timeMemory
1219969brito.joaoKnapsack (NOI18_knapsack)Java
Compilation error
0 ms0 KiB
import java.util.*;

public class Main {

    static int knapsack(int S, List<Item> items) {
        int[] dp = new int[S + 1];

        for (Item item : items) {
            int value = item.value;
            int weight = item.weight;
            int count = item.count;

            int k = 1;
            while (count > 0) {
                int take = Math.min(k, count);
                int totalValue = take * value;
                int totalWeight = take * weight;

                for (int j = S; j >= totalWeight; j--) {
                    dp[j] = Math.max(dp[j], dp[j - totalWeight] + totalValue);
                }

                count -= take;
                k <<= 1; // Multiply by 2
            }
        }

        int max = 0;
        for (int v : dp) max = Math.max(max, v);
        return max;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int S = sc.nextInt();
        int N = sc.nextInt();

        List<Item> items = new ArrayList<>();
        for (int i = 0; i < N; i++) {
            int v = sc.nextInt();
            int w = sc.nextInt();
            int k = sc.nextInt();
            items.add(new Item(v, w, k));
        }

        System.out.println(knapsack(S, items));
    }

    static class Item {
        int value, weight, count;

        Item(int value, int weight, int count) {
            this.value = value;
            this.weight = weight;
            this.count = count;
        }
    }
}

Compilation message (stderr)

knapsack.java:3: error: class Main is public, should be declared in a file named Main.java
public class Main {
       ^
1 error

=======