Cod sursa(job #2971187)
| Utilizator | Data | 26 ianuarie 2023 19:47:21 | |
|---|---|---|---|
| Problema | Problema rucsacului | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.6 kb |
#include <fstream>
using namespace std;
ifstream in("rucsac.in");
ofstream out("rucsac.out");
int profit[10001], p[5001], g[5001];
int main()
{
int n, k, vmax=0;
in>>n>>k;
for(int j=1; j<=k; j++)
profit[j]=-1;
profit[0]=0;
for(int i=0; i<n; i++)
in>>g[i]>>p[i];
for(int i=0; i<n; i++)
for(int j=k-g[i]; j>=0; j--)
if(profit[j]!=-1 && profit[j]+p[i]>profit[j+g[i]])
profit[j+g[i]]=profit[j]+p[i];
for(int i=1; i<=k; i++)
if(profit[i]>vmax)
vmax=profit[i];
out<<vmax;
return 0;
}
