Cod sursa(job #3155895)

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

using namespace std;

ifstream cin ("pusculita.in");
ofstream cout ("pusculita.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;
}