Cod sursa(job #3153043)

Utilizator Mihnea4.Hoisan Mihnea Mihnea4. Data 27 septembrie 2023 19:34:07
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.62 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");

struct obiect {
    int greutate;
    int valoare;
};

int dp[10005] = {0};
obiect o[5001];
int main() {
    int n, g;
    fin >> n >> g;

   

    for (int i = 0; i < n; i++) {
        fin >> o[i].greutate >> o[i].valoare;
    }

    

    for (int i = 0; i < n; i++) {
        int w = o[i].greutate;
        int p = o[i].valoare;

        for (int j = g; j >= w; j--) {

            dp[j] = max(dp[j], dp[j - w] + p);
        }
    }

    fout << dp[g] << endl;

    return 0;
}