Cod sursa(job #2333987)
| Utilizator | Data | 2 februarie 2019 10:09:48 | |
|---|---|---|---|
| Problema | Problema rucsacului | Scor | 0 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.48 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream in("rucsac.in");
ofstream out("rucsac.out");
const int N = 5005;
const int G = 10005;
int dp[2][G],w[N],p[N];
int main()
{
int n,g;
in >> n >> g;
for (int i = 1; i<=n; i++)
in >> w[i] >> p[i];
bool x = 1;
for (int i = 1; i<=n; i++, x = !x)
for (int j = 0; j<=g; j++)
if (w[i]<=j)
dp[x][j] = max(dp[!x][j],dp[!x][j-w[i]]+p[i]);
out << dp[!x][g];
}
