Cod sursa(job #1671493)

Utilizator adystar00Bunea Andrei adystar00 Data 1 aprilie 2016 20:03:56
Problema Energii Scor 95
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.67 kb
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
int best[10010][5010];
int main()
{
    ifstream fin ("energii.in");
    ofstream fout ("energii.out");
    int n,e,c,i,j,k;
    fin>>n>>k;
    for(i=0; i<=n; i++)
        for(j=0; j<=k; j++)
            best[i][j]=20000000;
    for(i=1; i<=n; i++)
    {
        fin>>e>>c;
        for(j=k; j>=1; j--)
        {
            if(j<=e)
                best[i][j]=min(best[i-1][j],c);
            else
                best[i][j]=min(best[i-1][j],best[i-1][j-e]+c);
        }
    }
    if(best[n][k]==2000000000)
        fout<<"-1";
    else
        fout<<best[n][k];
    return 0;
}