Cod sursa(job #880317)

Utilizator MKLOLDragos Ristache MKLOL Data 16 februarie 2013 16:51:19
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.45 kb
#include<stdio.h>
#include<algorithm>
using namespace std;
int v[101010];
int N,K,x,y;
int rez;
int main()
{
freopen("rucsac.in","r",stdin);
freopen("rucsac.out","w",stdout);
scanf("%d%d",&N,&K);

for(int i=1;i<=K;++i)
	v[i]=-1;
for(int i=1;i<=N;++i)
{
	scanf("%d%d",&x,&y);
	for(int j=K;j>=0;--j)
	{
		if(v[j]!=-1 && j + x <=K)
			{
			v[j+x] = max(v[j+x],v[j]+y);
			rez=max(rez,v[j]+y);
			}
	}

}
printf("%d",rez);
return 0;
}