Pagini recente » Cod sursa (job #171393) | Cod sursa (job #824555) | Cod sursa (job #2399627) | Cod sursa (job #60336) | Cod sursa (job #2635320)
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
#define mmax 10005
int best[mmax][mmax], n, p[mmax], w[mmax],wmax;
int main()
{
fin >> n >> wmax;
for (int i = 1; i <= n; i++)
{
fin >> w[i] >> p[i];
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= wmax; j++)
{
best[i][j] = best[i - 1][j];
if (w[i]<=j)
best[i][j] = max(best[i - 1][j], best[i - 1][j - w[i]] + p[i]);
}
}
fout << best[n][wmax];
}