Pagini recente » Cod sursa (job #2265609) | Cod sursa (job #826952) | Cod sursa (job #244308) | Cod sursa (job #708813) | Cod sursa (job #2567411)
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#define MaxNumberOfElements 5005
#define MaxWeight 10005
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
vector < pair < int , int > > arr(MaxNumberOfElements);
int dp[2][MaxWeight], N, G;
int main()
{
int weight, price, temp;
fin >> N >> G;
for(int i = 1; i <= N; i++){
fin >> weight >> price;
arr[i] = make_pair(weight,price);
}
temp = 0;
for(int i = 1; i <= N; i++, temp = 1 - temp){
for(int j = 0; j <= G; j++){
dp[1 - temp][j] = dp[temp][j];
if(arr[i].first <= j){
dp[1 - temp][j] = max(dp[1 - temp][j],dp[temp][j - arr[i].first] + arr[i].second);
}
}
}
fout << dp[temp][G] << "\n";
return 0;
}