Cod sursa(job #2641543)

Utilizator Razvan48Capatina Razvan Nicolae Razvan48 Data 11 august 2020 20:10:11
Problema Energii Scor 5
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.85 kb
#include <fstream>
#include <cstring>
using namespace std;

const int WMAX=5000;

int INF = 999999999;

int dp[2][1+WMAX];

int energie,pret;

int main()
{
    int g,w,i,j,lin=1;
    ifstream in("energii.in");
    ofstream out("energii.out");

    in>>g>>w;

    for(i=1;i<=w;i++)
    {
        dp[1-lin][i]=INF;
    }

    for(i=1;i<=g;i++,lin=1-lin)
    {
        in>>energie>>pret;
        for(j=0;j<=w;j++)
        {
            dp[lin][j]=dp[1-lin][j];
            if(j<energie)
            {
                dp[lin][j]=min(dp[lin][j],energie);
            }
            else
            {
                dp[lin][j]=min(dp[lin][j],dp[1-lin][j-energie]+pret);
            }
        }
    }

    if(dp[1-lin][w]==INF)
    {
        out<<-1;
    }
    else
    {
        out<<dp[1-lin][w];
    }

    return 0;
}