Pagini recente » Cod sursa (job #3287227) | Cod sursa (job #2852459) | Cod sursa (job #495333) | Cod sursa (job #2761460) | Cod sursa (job #760989)
Cod sursa(job #760989)
#include<fstream>
#define nmax 5007
#define max(a,b) (a>b ? a : b)
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
int Gmax , p[nmax], w[nmax], N;
bool pm[nmax * 2];
int sum[nmax * 2];
void solve()
{
pm[0] = true;
int last = 0, nrmax = 0;
for(int i = 1; i <= N ;i++)
{
last = nrmax ;nrmax = 0;
for( int j = last; j >=0 ;j-- )
if(j + w[i] <= Gmax && pm[j] == true)
{
nrmax= max(nrmax, j +w[i]);
pm[j + w[i]] =true;
sum[j + w[i]] = max(sum[j + w[i]] , sum[j] + p[i]);
}
}
}
void read()
{
fin >> N >>Gmax;
for(int i = 1; i <= N ;i++)
{
fin >> w[i] >>p[i];
}
solve();
}
int main()
{
read();
int p_max = 0;
for(int i = 0 ;i <= Gmax ; i++)
if(p_max<sum[i] )
p_max = sum[i];
fout <<p_max;
fin.close();
return 0;
}