Cod sursa(job #2895606)

Utilizator AlexMariMarinescu Alexandru AlexMari Data 29 aprilie 2022 11:45:20
Problema Energii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.71 kb
#include<bits/stdc++.h>
using namespace std;
ifstream fin("energii.in");
ofstream fout("energii.out");

long long n, g, ans, dp[100005];

int main()
{
    ans = LLONG_MAX;
    fin >> n >> g;
    for (int i = 1; i <= g - 1; i++) {
        dp[i] = LLONG_MAX;
    }

    for (long long i = 1; i <= n; i++) {
        long long x, y;
        fin >> x >> y;
        for (int j = g - 1; j >= 0; j--) {
            if (dp[j] == LLONG_MAX)
                continue;
            if (j + x >= g)
                ans = min(ans, dp[j] + y);
            else
                dp[j + x] = min(dp[j + x], dp[j] + y);
        }
    }

    if (ans == LLONG_MAX)
        fout << -1;
    else
        fout << ans;
}