Cod sursa(job #3278984)
| Utilizator | Data | 21 februarie 2025 16:55:58 | |
|---|---|---|---|
| Problema | Problema rucsacului | Scor | 50 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.58 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
int n, Gmax, greutate[1001]{}, profit[1001]{}, dp[1001][10001]{}, x;
int main(){
fin >> n >> Gmax;
for(int i = 1; i <= n; i++)
fin >> greutate[i] >> profit[i];
for(int i = 1; i <= n; i++){
for(int j = 1; j <= Gmax; j++){
if(greutate[i] > j)
dp[i][j] = dp[i-1][j];
else
dp[i][j] = max(dp[i-1][j], dp[i-1][j-greutate[i]] + profit[i]);
}
}
fout << dp[n][Gmax];
}