Pagini recente » Cod sursa (job #1011902) | Cod sursa (job #368920) | Cod sursa (job #303201) | Cod sursa (job #2602722) | Cod sursa (job #2478213)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("rucsac.in");
ofstream fout ("rucsac.out");
const int N = 5005, M = 10005;
int w[N], p[N], d[2][M];
int main()
{
ios::sync_with_stdio(false);
fin.tie(0);
int n, m;
fin >> n >> m;
for(int i = 1; i <= n; ++i) fin >> w[i] >> p[i];
int phase = 1;
for(int i = 1; i <= n; ++i, phase ^= 1) {
for(int j = 0; j <= m; ++j) {
d[phase ^ 1][j] = d[phase][j];
if(j >= w[i]) d[phase ^ 1][j] = max(d[phase ^ 1][j], d[phase][j - w[i]] + p[i]);
}
}
fout << d[phase][m];
return 0;
}