Cod sursa(job #1276741)

Utilizator vlad.florescu94FMI Florescu Vlad - Adrian vlad.florescu94 Data 26 noiembrie 2014 19:50:54
Problema Problema rucsacului Scor 0
Compilator java Status done
Runda Arhiva educationala Marime 0.88 kb
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();
	}

}