Pagini recente » Cod sursa (job #531047) | Cod sursa (job #2202551) | Cod sursa (job #2346278) | Cod sursa (job #2756026) | Cod sursa (job #1853810)
#include <stdio.h>
#include <stdlib.h>
int prof[10001]; /*vom retine profitul maxim pe fiecare greutate*/
int main()
{
FILE*fin,*fout;
int n,g,i,max=-1,w,p,j;
fin = fopen("rucsac.in" ,"r");
fout = fopen("rucsac.out" ,"w");
fscanf(fin, "%d%d" ,&n,&g);
for(i=1;i<=g;i++)
prof[i]=-1;
prof[0]=0;
for(i=0;i<n;i++){
fscanf(fin, "%d%d" ,&w,&p);
for(j=g-w;j>=0;j--){
if(prof[j]!=-1 && prof[j+w]<prof[j]+p){
prof[j+w]=prof[j]+p;
if(prof[j+w]>max)
max=prof[j+w];
}
}
}
fprintf(fout, "%d\n" ,max);
fclose(fin);
fclose(fout);
return 0;
}