Cod sursa(job #1988924)

Utilizator DavidLDavid Lauran DavidL Data 5 iunie 2017 12:08:17
Problema Energii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <fstream>
#define MAXG 1001
#define MAXW 5001
#define INF 1000000000
using namespace std;
ifstream fi("energii.in");
ofstream fo("energii.out");

int dp[2][MAXW+12001],G,W,e[MAXG],c[MAXG];
int main()
{
    fi>>G>>W;
    for (int i=1; i<=G; i++)
        fi>>e[i]>>c[i];
    for (int j=1; j<=W+12000; j++)
        dp[0][j]=INF;
    int curent=0;
    for (int i=1; i<=G; i++)
    {
        curent=1-curent;
        for (int j=1; j<=W+12000; j++)
        {
            dp[curent][j]=dp[1-curent][j];
            if (j-e[i]>=0)
                dp[curent][j]=min(dp[curent][j],dp[1-curent][j-e[i]]+c[i]);
        }
    }
    int rez=INF;
    for (int j=W; j<=W+12000; j++)
        rez=min(rez,dp[curent][j]);
    if (rez==INF)
        fo<<-1;
    else
        fo<<rez;
    fi.close();
    fo.close();
    return 0;
}