Cod sursa(job #734511)

Utilizator NitaMihaitavoidcube NitaMihaita Data 14 aprilie 2012 14:45:17
Problema Problema rucsacului Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.66 kb
#include<fstream>
#include<iostream>
using namespace std;
long long sum;
int Max,n,G;
int v[50000001];
int w[50000001];
void add(int px, int gx)
{
for(int i=sum-px;i>=0;--i)
	{
	v[i+px]|=v[i];
	if(v[i+px])
		{
		if(w[i+px])
			{
			if(i!=0)
				if(w[i+px]>gx+w[i] and w[i]!=0)
					w[i+px]=gx+w[i];
			if(i==0)
				if(w[i+px]>gx+w[i])
					w[i+px]=gx+w[i];
			}
		else w[i+px]=gx+w[i];
		}
	}
}
int main()
{
ifstream f("rucsac.in");
ofstream g("rucsac.out");
int px,gx,i;
f>>n>>G;
v[0]=1;
w[0]=0;
while(f>>gx>>px)
	{
	sum+=px;
	add(px,gx);
	}
for(i=sum;;--i)
	if(w[i]<=G and w[i])break;
g<<i<<"\n";
f.close();
g.close();
return 0;
}