Cod sursa(job #3241334)

Utilizator SilviuC25Silviu Chisalita SilviuC25 Data 29 august 2024 09:24:45
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.72 kb
#include <bits/stdc++.h>
using namespace std;

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

const int MAX_WEIGHT = 10005;
const int MAX_OBJECTS = 5005;

int n, g;
int w[MAX_OBJECTS], p[MAX_OBJECTS];
vector<int> maxProfit(MAX_WEIGHT, 0);

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    fin >> n >> g;
    for (int i = 0; i < n; ++i) {
        fin >> w[i] >> p[i];
    }
    for (int i = 0; i < n; ++i) {
        for (int j = g; j >= 0; --j) {
            if (j + w[i] <= g) {
                maxProfit[j + w[i]] = max(maxProfit[j + w[i]], maxProfit[j] + p[i]);
            }
        }
    }
    fout << *max_element(maxProfit.begin(), maxProfit.end());
    return 0;
}