Cod sursa(job #749555)
#include<stdio.h>
FILE *IN, *OUT;
int i,j;
int nr_obiecte,greutate_rucsac,greutate_obiect[50],cost_obiect[50];
int solutie, optim[100];
void CitesteDate(){
fscanf(IN, "%d", &nr_obiecte);
fscanf(IN, "%d", &greutate_rucsac);
for(i=1;i<=nr_obiecte;i++){
fscanf(IN, "%d", &greutate_obiect[i]);
fscanf(IN, "%d", &cost_obiect[i]);
}
}
void Solutie(){
for(i=1;i<=nr_obiecte;i++)
for(j= greutate_rucsac - greutate_obiect[i]; j >=0; j--){
if(optim[j+greutate_obiect[i]] < optim[j] + cost_obiect[i] ){
optim[j+greutate_obiect[i]]=optim[j]+cost_obiect[i];
if(optim[j+greutate_obiect[i]]>solutie){
solutie=optim[j+greutate_obiect[i]];
}
}
}
//salvam in fisier datele prelucrate
fprintf(OUT,"%d", solutie);
}
int main(){
IN=fopen("in.txt", "rt");
OUT=fopen("out.txt", "wt");
CitesteDate();
Solutie();
fcloseall();
}