Cod sursa(job #2650441)

Utilizator redstonegamer22Andrei Ion redstonegamer22 Data 18 septembrie 2020 20:41:31
Problema Energii Scor 5
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.73 kb
#include <bits/stdc++.h>

using namespace std;

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

int costs[5005];

int main()
{
    for(int i = 0; i < 5005; i++) costs[i] = -1;
    int n, w; in >> n >> w;

    costs[0] = 0;

    int minim = 1000000000;
    for(int i = 0; i < n; i++)
    {
        int e, c; in >> e >> c;
        for(int last = w-1; last >= 0; last--)
        {
            if(costs[last] != -1)
            {
                if(last + e >= w)
                    minim = min(minim, costs[last] + c);
                else
                    costs[last + e] = min(costs[last + e], costs[last] + c);
            }
        }
    }

    if(minim == 1000000000) minim = -1;
    out << minim;
}