Pagini recente » Cod sursa (job #2743628) | Cod sursa (job #1480188) | Cod sursa (job #2896175) | Cod sursa (job #3310221) | Cod sursa (job #3355555)
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
class Main {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(new File("rucsac.in"));
PrintWriter pw = new PrintWriter("rucsac.out");
int n = sc.nextInt();
int g = sc.nextInt();
int[] dp = new int[g + 1];
// Procesăm fiecare obiect pe rând
for (int i = 0; i < n; i++) {
int w = sc.nextInt(); // greutatea curentă
int p = sc.nextInt(); // profitul curent
// TRUCUL 1D: Parcurgem INVERS greutățile
// Ne oprim direct la 'w', pentru că la j < w nu putem adăuga obiectul oricum
for (int j = g; j >= w; j--) {
dp[j] = Math.max(dp[j], dp[j - w] + p);
}
}
pw.println(dp[g]);
pw.close();
}
}