Cod sursa(job #2220406)

Utilizator Alex03Runcan Alexandru Alex03 Data 11 iulie 2018 17:01:50
Problema Energii Scor 25
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.79 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin  ("energii.in");
ofstream fout ("energii.out");
int n,w,Energy[1001],Cost[1001],Dp[5001];// n is the number of generators and w is the energy needed

int main ()
{
    fin >> n >> w;
    for (int i = 1; i <= n; i++)
        fin >> Energy[i] >> Cost[i];
    int sol = INT_MAX;
    Dp[0] = 1;
    for (int i = 1; i <= n; i++)
        for (int j = w; j >= 0; j--)
            if (Dp[j])
                if (j + Energy[i] < w)
                    if (Dp[j + Energy[i]]) Dp [j + Energy[i]] = min (Dp[j + Energy[i]] , Dp[j] + Cost[i]);
                    else Dp[j + Energy[i]] = Dp[i] + Cost[i];
                else sol = min (sol , Dp[j] + Cost[i]);
    if (sol == INT_MAX) fout << "-1";
    else fout << sol - 1;
    return 0;
}