Cod sursa(job #2491009)
| Utilizator | Data | 11 noiembrie 2019 17:15:58 | |
|---|---|---|---|
| Problema | Problema rucsacului | Scor | 0 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.61 kb |
#include<bits/stdc++.h>
using namespace std;
ifstream in("rucsac.in");
ofstream out("rucsac.out");
int weight[5000],profit[5000];
int n,max_weight;
int dp[10001][5001];
void read()
{
in>>n>>max_weight;
for(int i=0; i<n; i++)
{
in>>weight[i]>>profit[i];
}
}
int knapsack()
{
for(int i=1; i<=n; i++)
{
for(int g=0; g<=max_weight; g++)
{
if(weight[i-1]<=g)
{
dp[i][g]=max(dp[i-1][g],dp[i-1][g-weight[i-1]]+profit[i-1]);
}
}
}
return dp[n][max_weight];
}
int main()
{
read();
out<<knapsack();
}
