Cod sursa(job #1150589)

Utilizator ionut98Bejenariu Ionut Daniel ionut98 Data 23 martie 2014 12:39:22
Problema Energii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.07 kb
#include<fstream>
using namespace std;
ifstream f("energii.in");
ofstream g("energii.out");
long c[1001],e[1001];
long a[1001][5001],i,j,w,G;
int main()
{
    f>>G;
    f>>w;
    for(i=1;i<=G;i++)
    f>>e[i]>>c[i];
    for(i=1;i<=w;i++)
    {
        if(i<=e[1])
        {
             a[1][i]=c[1];
        }
    }
    for(i=1;i<=G;i++)
    {
        for(j=1;j<=w;j++)
        {
            if(a[i][j]==0)
            a[i][j]=6000000;
        }
    }
    for(i=2;i<=G;i++)
    {
        for(j=1;j<=w;j++)
        {
            if(j<=e[i])
            {
                    if(a[i-1][j]<c[i])
                       a[i][j]=a[i-1][j];
                    else
                       a[i][j]=c[i];
            }
            else
            {
                    if((j-e[i]>=1)&&a[i-1][j]>c[i]+a[i-1][j-e[i]])
                       a[i][j]=c[i]+a[i-1][j-e[i]];
                    else
                       a[i][j]=a[i-1][j];
            }
        }
    }
    if (a[G][w]==6000000)
       g<<-1;
    else
        g<<a[G][w];
    return 0;
}