Cod sursa(job #742151)

Utilizator visanrVisan Radu visanr Data 28 aprilie 2012 18:46:56
Problema Energii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.91 kb
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;


int e[1005], c[1005], cost[1005][5005], g, w;


int main()
{
    freopen("energii.in","r",stdin);
    freopen("energii.out","w",stdout);
    int i, j;
    scanf("%i %i", &g, &w);
    for(i = 1; i <= g; ++i)
    {
          scanf("%i %i", &e[i], &c[i]);
    }
    for(j = 0; j <= w; ++j) cost[0][j] = 1 << 30;
    for(i = 0; i <= g; ++i) cost[i][0] = 1 << 30;
    for(i = 1; i <= g; ++i)
    {
          for(j = 1; j<= w; ++j)
          {
                if(j > e[i])
                {
                     cost[i][j] = min(cost[i-1][j], cost[i-1][j-e[i]] + c[i]);
                }else
                {
                     cost[i][j] = min(cost[i-1][j], c[i]);
                }
          }
    }
    if(cost[g][w] != 1 << 30) printf("%i\n", cost[g][w]);
    else printf("-1\n");
    return 0;
}