Cod sursa(job #3167498)

Utilizator dragoncrackCandidatu Mario Luca dragoncrack Data 10 noiembrie 2023 19:19:07
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.73 kb
#include <fstream>
#define DIM 5005
#define maxG 10000

using namespace std;

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

int n, G;
int w[DIM], p[DIM];
int profitForWeight[maxG + 1];
int sol = 0;

int main()
{
    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; j > 0; j--) {
            if (profitForWeight[j] != 0 && w[i] + j <= G) {
                profitForWeight[j + w[i]] = max(profitForWeight[j + w[i]], profitForWeight[j] + p[i]);
                sol = max(sol, profitForWeight[j + w[i]]);
            }
        }
        profitForWeight[w[i]] = max(profitForWeight[w[i]], p[i]);
    }
    fout << sol;
}