Cod sursa(job #2475224)

Utilizator maramihaliMara Mihali maramihali Data 16 octombrie 2019 16:04:29
Problema Energii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.77 kb
#include <bits/stdc++.h>

using namespace std;

int g,w,p[10003],e[5003],cost[5003],s;

ifstream in("energii.in");
ofstream out("energii.out");

void knapsack()
{
    if(s < w)
    {
        out << -1;
        return;
    }
    for(int i = 1; i <= w; i++)
    {
        p[i] = 100000000001;
    }
    for(int i = 1; i <= g; i++)
    {
        for(int j = w; j >= 0; j--)
        {
            if(j + e[i] > w)
            {
                p[w] = min(p[w],p[j] + cost[i]);
            }
            else
            {
                p[j + e[i]] = min(p[j] + cost[i],p[j +e [i]]);
            }
        }
    }
    out << p[w];
}

int main()
{
    in >> g >> w;
    for(int i = 1; i <= g; i++)
    {
        in >> e[i] >> cost[i];
        s += e[i];
    }
    knapsack();
    return 0;
}