Cod sursa(job #2720720)

Utilizator LuBoNILuput Bogdan LuBoNI Data 11 martie 2021 10:52:53
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.68 kb
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
int n, g, w[5001], p[5001], d[10001], sol;
int main()
{
    ifstream fin("rucsac.in");
    ofstream fout("rucsac.out");
    fin >> n >> g;
    for (int i = 1; i <= n; i++)
        fin >> w[i] >> p[i];

    for (int i = 1; i <= n; i++)
        {
            for (int j = g - w[i]; j >= 0; j--)
        {
            if (d[j + w[i]] < d[j] + p[i])
            {
                d[j + w[i]] = d[j] + p[i];
                if (d[j + w[i]] > sol)
                    sol = d[j + w[i]];
            }
        }
        }
    fout << sol;
    return 0;
}
/*
6 10
3 7
3 4
1 2
1 9
2 4
1 5
*/