Cod sursa(job #2286843)

Utilizator LivcristiTerebes Liviu Livcristi Data 20 noiembrie 2018 21:27:11
Problema Energii Scor 95
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.93 kb
#include <iostream>
#include <fstream>
#define NUM 1005
#define minim(a, b) ((a) < (b) ? (a) : (b))
int mat[NUM][NUM * 10];
int cost[NUM];
int energ[NUM];
int n, gr;
int inf = 0x3f3f3f;
using namespace std;
int main()
{
    ifstream f("energii.in");
    ofstream g("energii.out");
    f >> n >> gr;
    for(int i = 1; i <= n; ++i)
        f >> energ[i] >> cost[i];

    for(int i = 0; i <= n; ++i)
        for(int j = 0; j <= gr; ++j)
            mat[i][j] = inf;

    for(int i = 1; i <= n; ++i)
        for(int j = 0; j <= gr; ++j)
            {
                mat[i][j] = mat[i - 1][j];
                if(energ[i] >= j)
                    mat[i][j] = minim(mat[i][j], cost[i]);
                else
                    mat[i][j] = minim(mat[i][j], mat[i - 1][j - energ[i]] + cost[i]);
            }
    if(!mat[n][gr])
        g << "-1";
    else
        g << mat[n][gr];
    f.close();
    g.close();
}