Pagini recente » Cod sursa (job #3036870) | Rating Chiritoiu Andra Florentina (chiritoiu.andra06) | Cod sursa (job #3311828) | Cod sursa (job #3297467) | Cod sursa (job #3311472)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
const int nmax = 5000, gmax = 10000;
int w[nmax+1], p[nmax+1];
int dp[gmax+1];
// dp[i][j] (i este subinteles) = profitul maxim pe care il pot obtine din primele i elemente, stiind ca aceste elemente au greutatea exact j
void solve() {
int n, g, i, j;
fin >> n >> g;
for (i = 1; i<=n; i++)
fin >> w[i] >> p[i];
// stare initiala: dp[0][j] = 0 pentru toate 0 <= j <= g
for (i = 1; i<=n; i++)
for (j = g; j>=w[i]; j--)
dp[j] = max(dp[j], dp[j-w[i]] + p[i]);
int rasp = 0;
for (j = 0; j<=g; j++)
rasp = max(rasp, dp[j]);
fout << rasp;
}
int main() {
solve();
return 0;
}