Cod sursa(job #3155900)

Utilizator ShaneVladMogaVlad ShaneVlad Data 10 octombrie 2023 09:47:06
Problema Energii Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.09 kb
#include <fstream>
#define M 10000000

using namespace std;

ifstream cin ("energii.in");
ofstream cout ("energii.out");

unsigned int w, n, g[M], wmax;
pair <unsigned int,unsigned int> v[1005];

int main()
{
    cin >> n >> w;
    for (int i = 1; i <= n; i++)
        cin >> v[i].first >> v[i].second;

    for (int i = 1; i <= n; i++)
    {
        wmax += v[i].first;
        for(int j = wmax; j >= 1; j--)
            if (g[j])
            {
                if(g[j+v[i].first]==0)
                    g[j+v[i].first]=g[j]+v[i].second;
                else
                    g[j+v[i].first] = min(g[j]+v[i].second, g[j+v[i].first]);
            }
        if(g[v[i].first] == 0)
            g[v[i].first]=v[i].second;
        else
            g[v[i].first]=min(g[v[i].first],v[i].second);

    }

    if(g[w])
    {
        cout << g[w];
        return 0;
    }

    else
    {
        for (int i = w; i <= M; i++)
            if (g[i])
            {
                cout << g[w];
                return 0;
            }

    }
    cout << -1;
    return 0;
}