Cod sursa(job #3294441)

Utilizator theshadowcodertheshadowcoder theshadowcoder Data 23 aprilie 2025 15:25:27
Problema Energii Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <bits/stdc++.h>
using namespace std;

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

#define cin fin
#define cout fout

const int WMAX = 5007;
const int INF = 1e9;

int dp[WMAX];

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n, w;
    cin >> n >> w;

    for (int i = 0; i < WMAX; ++i)
    {
        dp[i] = INF;
    }

    dp[0] = 0;

    for (int i = 0; i < n; ++i)
    {
        int energy, cost;
        cin >> energy >> cost;

        for (int j = WMAX - energy - 1; j >= 0; --j)
        {
            if (dp[j] != INF)
            {
                dp[j + energy] = min(dp[j + energy], dp[j] + cost);
            }
        }
    }

    int answer = INF;
    for (int i = w; i < WMAX; ++i)
    {
        answer = min(answer, dp[i]);
    }

    if (answer == INF)
    {
        cout << -1;
    }
    else
    {
        cout << answer;
    }
    return 0;
}