Cod sursa(job #3275678)

Utilizator AndreiNicolaescuEric Paturan AndreiNicolaescu Data 11 februarie 2025 15:06:32
Problema Energii Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.76 kb
#include <bits/stdc++.h>
#define cin ci
#define cout co
using namespace std;
ifstream cin("energii.in");
ofstream cout("energii.out");
const int Wmax = 5001;
const int INF = 0x3F3F3F;
int n, w;
struct gen
{
    int weight, cost;
};
vector<gen> v;
vector<int> dp(Wmax);
int main()
{
    cin >> n >> w;
    v.resize(n+1);
    fill(dp.begin(), dp.end(), INF);
    for(int i=1; i<=n; i++)
        cin >> v[i].weight >> v[i].cost;
    for(int i=1; i<=n; i++)
        for(int j = Wmax - v[i].weight; j>=0; j--)
            dp[j + v[i].weight] = min(dp[j + v[i].weight], dp[j] + v[i].cost);
    for(int i=w + 1; i<=Wmax; i++)
        if(dp[i] != Wmax)
        {
            cout << i;
            return 0;
        }
    cout << "-1";

    return 0;
}