Cod sursa(job #2331498)
Utilizator | Data | 29 ianuarie 2019 17:25:43 | |
---|---|---|---|
Problema | Problema rucsacului | Scor | 65 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 0.54 kb |
#include <fstream>
using namespace std;
ifstream in("rucsac.in");
ofstream out("rucsac.out");
const int N=5005;
int n, k, maxi;
int profit[N], p[N], g[N];
void citire()
{
in>>n>>k;
for(int i=1; i<=n; i++) in>>g[i]>>p[i];
}
int main()
{
citire();
for(int i=1; i<=n; i++) profit[i]=-1;
for(int i=1; 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++) maxi=max(maxi, profit[i]);
out<<maxi;
return 0;
}