Pagini recente » Cod sursa (job #2680550) | Cod sursa (job #297737) | Cod sursa (job #472303) | Cod sursa (job #1183007) | Cod sursa (job #2576396)
#include <bits/stdc++.h>
#define gmaxxx 10002
#define nmax 5005
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
int n, gmax, dp[3][gmaxxx], greutati[nmax], profit[nmax];
int main()
{
int i, j;
fin >> n >> gmax;
for(i = 1; i <= n; i++)
fin >> greutati[i] >> profit[i];
for(i = 1; i <= n; i++)
{
for(j = 1; j < greutati[i]; j++)
dp[2][j] = dp[1][j];
for(j = greutati[i]; j <= gmax; j++)
dp[2][j] = max(profit[i] + dp[1][j - greutati[i]], dp[1][j]);
for(j = 0; j <= gmax; j++)
dp[1][j] = dp[2][j];
}
fout << dp[2][gmax];
return 0;
}