Cod sursa(job #3344097)
| Utilizator | Data | 1 martie 2026 13:22:55 | |
|---|---|---|---|
| Problema | Problema rucsacului | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.5 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
const int lim=1e4+10;
int n,g,i,w[lim],p[lim],dp[lim][3],k,mx;
int main()
{
fin>>n>>g;
for(i=1;i<=n;i++)
{
fin>>w[i]>>p[i];
}
for(i=1;i<=n;i++)
for(k=1;k<=g;k++)
{
if(k<w[i])dp[k][i&1]=dp[k][(i-1)&1];
else dp[k][i&1]=max(dp[k][(i-1)&1],p[i]+dp[k-w[i]][(i-1)&1]);
mx=max(mx,dp[g][i&1]);
}
fout<<mx;
return 0;
}
