Mai intai trebuie sa te autentifici.
Cod sursa(job #494902)
| Utilizator | Data | 23 octombrie 2010 12:35:30 | |
|---|---|---|---|
| Problema | Energii | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva de probleme | Marime | 0.98 kb |
#include <stdio.h>
#include <assert.h>
#define Wmax 5000
#define INF 2000000000
#define Gmax 1000
int G, W, cost[2*Wmax+1], req[Gmax+1], gain[Gmax+1];
void read()
{
int i;
freopen("energii.in","r",stdin);
scanf("%d%d",&G,&W);
for(i=1;i<=G;i++)
scanf("%d %d",&gain[i],&req[i]);
fclose(stdin);
for(i=1;i<=W;i++)
cost[i]=INF;
}
void solve()
{
int i, j;
for(i=1;i<=G;i++)
for(j=W;j>=0;j--)
if(cost[j]!=INF)
{
if(j+gain[i]>=W && cost[W]>cost[j]+req[i])
cost[W]=cost[j]+req[i];
else if(j+gain[i]<W && cost[j+gain[i]]>cost[j]+req[i])
cost[j+gain[i]]=cost[j]+req[i];
}
}
void write()
{
freopen("energii.out","w",stdout);
if(cost[W]==INF)
printf("-1\n");
else
printf("%d\n",cost[W]);
fclose(stdout);
}
int main()
{
read();
solve();
write();
return 0;
}
