Pagini recente » Cod sursa (job #2465121) | Cod sursa (job #2987214) | Cod sursa (job #2570481) | Cod sursa (job #2724536) | Cod sursa (job #1276741)
package prd;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException{
Scanner sc = new Scanner(new File("rucsac.in"));
PrintWriter pw = new PrintWriter("rucsac.out", "UTF-8") ;
int n = sc.nextInt(), g = sc.nextInt();
Element[] elements = new Element[n];
int[] bestSol = new int[g+1];
for(int i=0; i<n; ++i){
elements[i] = new Element(sc.nextInt(), sc.nextInt());
}
for(int i=0; i<n; ++i){
for(int j=g; j>=0; --j){
if(elements[i].weight <= j){
bestSol[j] = Math.max(bestSol[j], bestSol[j - elements[i].weight] + elements[i].profit);
}
}
}
pw.print(bestSol[g]);
sc.close(); pw.close();
}
}