Pagini recente » Diferente pentru planificare/sedinta-20110509 intre reviziile 1 si 11 | Cod sursa (job #1909270) | Rezultatele filtrării | Rezultatele filtrării | Cod sursa (job #1362434)
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
InputStream inputStream = new FileInputStream("rucsac.in");
Scanner scanner = new Scanner(inputStream);
OutputStream outputStream = new FileOutputStream("rucsac.out");
PrintWriter writer = new PrintWriter(outputStream);
int N = scanner.nextInt();
int G = scanner.nextInt();
List<Integer> weights = new ArrayList<>();
List<Integer> profits = new ArrayList<>();
weights.add(0);
profits.add(0);
for (int i = 1; i <= N; ++i) {
weights.add(scanner.nextInt());
profits.add(scanner.nextInt());
}
int Max[][] = new int[2][G + 1];
for (int i = 1; i <= N; ++i) {
int w = weights.get(i);
int p = profits.get(i);
int d = i % 2;
Max[d][0] = 0;
for (int g = 1; g <= G; ++g) {
// I just take from before.
Max[d][g] = Max[1 - d][g];
// I take the object.
if (g >= w && Max[d][g] <= Max[1 - d][g - w] + p) {
Max[d][g] = Max[1 - d][g - w] + p;
}
}
}
writer.println(Max[N % 2][G]);
writer.flush();
inputStream.close();
scanner.close();
outputStream.close();
writer.close();
}
}