Cod sursa(job #3367228)
| Utilizator | Data | 6 octombrie 2026 19:24:58 | |
|---|---|---|---|
| Problema | Problema rucsacului | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.65 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
int n, maxG;
int greutate[5001];
int profit[5001];
array<int, 10001> dp1;
array<int, 10001> dp2;
int main() {
fin>>n>>maxG;
for(int i = 1; i <= n; i++){
fin>>greutate[i];
fin>>profit[i];
}
for(int i = 1; i <= n; i++){
for(int j = 0; j <= maxG; j++){
if(greutate[i] > j)
dp1[j] = dp2[j];
else
dp1[j] = max(dp2[j], dp2[j - greutate[i]] + profit[i]);
}
dp2 = dp1;
}
fout<<dp1[maxG];
return 0;
}
