Pagini recente » Cod sursa (job #1642418) | Cod sursa (job #2341143) | Cod sursa (job #341288) | Cod sursa (job #657499) | Cod sursa (job #2312780)
#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(){
for(int i = 1; i <= data.size(); i++){
for(int j = data[i-1].first; j <= Gmax; j++){
DP[2][j] = max(data[i-1].second + DP[1][j - data[i-1].first], DP[2][j]);
DP[2][j] = max(DP[2][j], DP[1][j]);
}
for(int j = 0; j <= Gmax; j++){
DP[1][j] = DP[2][j];
DP[2][j] = 0;
}
}
out << DP[1][Gmax] << '\n';
}
int main() {
read();
solve();
return 0;
}