Pagini recente » Cod sursa (job #1641428) | Cod sursa (job #2944781) | Cod sursa (job #1840623) | Cod sursa (job #2399855) | Cod sursa (job #2505795)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
int N, G;
int p[1001], g[1001], dp[1001][1001];
int main()
{
fin >> N >> G;
for(int i = 1; i <= N; i++)
{
fin >> g[i] >> p[i];
}
for(int i = 1; i <= N; i++)
{
for(int j = 1; j <= G; j++)
{
dp[i][j] = -1;
}
}
dp[1][g[1]] = p[1];
for(int i = 2; i <= N; i++)
{
for(int j = 1; j <= G; j++)
{
if(dp[i-1][j - g[i]] != -1)
{
dp[i][j] = max(dp[i-1][j], dp[i-1][j - g[i]] + p[i]);
}
else
{
dp[i][j] = dp[i-1][j];
}
}
}
/*for(int i = 1; i <= N; i++)
{
for(int j = 1; j <= G; j++)
{
fout << dp[i][j] << ' ' ;
}
fout << '\n';
}
*/
fout << dp[N][G];
return 0;
}