Pagini recente » Cod sursa (job #2513774) | Cod sursa (job #2288038) | Cod sursa (job #323113) | Cod sursa (job #806369) | Cod sursa (job #2312785)
#include <iostream>
#include <algorithm>
#include <climits>
#include <vector>
#include <fstream>
using namespace std;
typedef pair <int, int> pereche;
ifstream in("rucsac.in");
ofstream out("rucsac.out");
vector<pereche> data;
int Gmax, DP[3][10001], n;
void read() {
in >> n >> Gmax;
int x, y;
while(in >> x >> y){
data.push_back({x, y});
}
}
void solve(){
int l = 1;
for(int i = 1; i <= data.size(); i++, l = 3 -l){
for(int j = 0; j <= Gmax; j++){
DP[3-l][j] = DP[l][j];
if(j >= data[i-1].first)
DP[3-l][j] = max(data[i-1].second + DP[l][j - data[i-1].first], DP[3-l][j]);
// DP[3-l][j] = max(DP[3-l][j], DP[l][j]);
}
}
out << DP[1][Gmax] << '\n';
}
int main() {
read();
solve();
return 0;
}