Pagini recente » Cod sursa (job #2668909) | Cod sursa (job #2057455) | Cod sursa (job #2815091) | Cod sursa (job #2780068) | Cod sursa (job #2337856)
#include <iostream>
#include <utility>
#include <fstream>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
int m[2][10002];
pair <int, int> date[5001];
int main()
{
int gmax, n, x, y, i, j;
fin >> n >> gmax;
for(i = 0; i < n; i++)
{
fin >> x >> y;
if (x > gmax)
i--, n--;
else
date[i].first = x, date[i].second = y;
}
for(i = 0; i < n; i++)
{
for (j = 0; j <= gmax; j++)
m[0][j] = m[1][j], m[1][j] = 0;
j = 1;
while(j < date[i].first)
m[1][j] = m[0][j], j++;
while(j <= gmax)
m[1][j] = max(m[0][j-date[i].first] + date[i].second, m[0][j]), j++;
}
fout << m[1][gmax];
return 0;
}