Pagini recente » Cod sursa (job #106687) | Cod sursa (job #2483087) | Cod sursa (job #1878019) | Cod sursa (job #2237943) | Cod sursa (job #3691)
Cod sursa(job #3691)
#include <stdio.h>
void main()
{
int i, j, g, w;
int* eg;
int* cg;
int* c;
int p = -1;
freopen("energii.in", "rt", stdin);
freopen("energii.out", "wt", stdout);
scanf("%d%d", &g, &w);
eg = new int[g];
cg = new int[g];
c = new int[ (2 * w) + 1 ];
c[0] = 1;
for(i = 0; i < g; i++)
{
scanf("%d%d", &eg[i], &cg[i]);
}
for(i = 0; i < g; i++)
{
for(j = 2 * w; j >= eg[i]; j--)
{
if(c[j - eg[i]] && (!c[j] || (c[j - eg[i]] + cg[i] < c[j])))
{
c[j] = c[j - eg[i]] + cg[i];
if(j >= w) p = j;
}
}
}
if(p)
{
printf("%d\n", c[p] - 1);
}
else
{
printf("-1\n");
}
}