Pagini recente » Cod sursa (job #1937177) | Cod sursa (job #2372675) | Cod sursa (job #1676604) | Cod sursa (job #2436997) | Cod sursa (job #1511573)
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = null;
PrintWriter out = null;
try {
in = new Scanner(new FileReader("rucsac.in"));
int nbAnimals = in.nextInt();
int weightNeed = in.nextInt();
int[][] animals = new int[nbAnimals + 1][3];
String[] strs = new String[nbAnimals + 1];
int[] cost = new int[weightNeed + 1];
for(int i = 1; i <= nbAnimals; i++){
//trebuie modificat
animals[i][0] = in.nextInt();
animals[i][1] = in.nextInt();
animals[i][2] = i + 1;
}
for(int j = 0; j < animals[1][0]; j++)
cost[j] = 0;
for(int j = animals[1][0]; j <= weightNeed; j++)
cost[j] = animals[1][1];
for(int i = 2; i <= nbAnimals; i++){
for(int j = weightNeed; j >= animals[i][0]; j--)
cost[j] = Math.max(cost[j],
cost[j - animals[i][0]] + animals[i][1]);
}
out = new PrintWriter("rucsac.out", "UTF-8");
out.println(cost[weightNeed]);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if(in != null)
in.close();
if(out != null)
out.close();
}
}
}