Pagini recente » Cod sursa (job #2890586) | Cod sursa (job #2812291) | Cod sursa (job #238407) | Cod sursa (job #234277) | Cod sursa (job #2242703)
#include <fstream>
#define NMAX 5005
#define WMAX 10001
#define MAX(A, B) (A >= B ? A : B)
using namespace std;
/*
* FILES
*/
ifstream f("rucsac.in");
ofstream g("rucsac.out");
/*
* DATA STRUCTURES AND VARS
*/
struct Obj{
int weight, value;
Obj(){weight = value = 0;}
Obj(int w, int v): weight{w}, value{v} {}
};
int n, w;
Obj objects[NMAX];
int dp[NMAX][WMAX];
/*
* Reading data
*/
void read_data(){
f >> n >> w;
for(int i = 1; i<=n; i++){
int weight, value;
f >> weight >> value;
Obj obj{weight, value};
objects[i] = obj;
}
}
/*
* DP function
*/
void dynamic(){
for(int i = 1; i<=n; i++){
for(int CW = 0; CW <= w; CW ++){
dp[i][CW] = dp[i - 1][CW];
if(objects[i].weight <= CW){
dp[i][CW] = MAX(dp[i][CW], dp[i - 1][CW - objects[i].weight] + objects[i].value);
}
}
}
g << dp[n][w];
}
int main(){
read_data();
dynamic();
return 0;
}