Pagini recente » Cod sursa (job #2293755) | Cod sursa (job #10976) | Cod sursa (job #2331054) | Cod sursa (job #392238) | Cod sursa (job #734511)
Cod sursa(job #734511)
#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;
}