Cod sursa(job #1427865)

Utilizator ialexia_ioanaAlexia Ichim ialexia_ioana Data 3 mai 2015 10:18:16
Problema Energii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.06 kb
#include <cstdio>

using namespace std;

int d[10011005];

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