Pagini recente » Cod sursa (job #2970878) | Cod sursa (job #1256728) | Cod sursa (job #1875186) | Cod sursa (job #2310570) | Cod sursa (job #2716733)
#include<fstream>
#include<algorithm>
#define N 5050
using namespace std;
ifstream f("rucsac.in");
ofstream out("rucsac.out");
int g[N], p[N], v[10010],n, gmax, pmax=-10, smg, smp;
int main()
{
f>>n>>gmax;
for(int i =1; i<=n; ++i)
{
f>>g[i]>>p[i];
}
v[0] = 1;
for(int i =1; i<=n; ++i)
{
for(int j = gmax; j>=0; --j)
{
if(v[j] != 0 && j + g[i] <= gmax)
{
if(v[j+g[i]] == 0 || (v[j+g[i]] < v[j] + p[i]))
v[j+g[i]] = v[j] + p[i];
}
}
}
for(int i = 1; i<=gmax; ++i)
if(v[i] > pmax) pmax = v[i];
out<<(pmax-1);
}