Cod sursa(job #3294443)

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

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

#define cin fin
#define cout fout
#define int long long

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

int dp[WMAX];

signed 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;
    int answer = INF;

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

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

    fout << (dp[w] == INF ? -1 : answer);
    return 0;
}