Cod sursa(job #3241215)

Utilizator SilviuC25Silviu Chisalita SilviuC25 Data 27 august 2024 19:33:05
Problema Problema rucsacului Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.75 kb
#include <bits/stdc++.h>
using namespace std;

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

const int MAX_WEIGHT = 5e3;
const int MAX_OBJECTS = 1e4;

int n, g;
int w[MAX_OBJECTS + 1], p[MAX_OBJECTS + 1], maxProfit[MAX_WEIGHT + 1][MAX_OBJECTS + 1];
int answer;

void solve(int weight, int profit, int index) {
    if (weight > g || index > n) {
        return;
    }
    answer = max(answer, profit);
    solve(weight + w[index + 1], profit + p[index + 1], index + 1);
    solve(weight, profit, index + 1);
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    fin >> n >> g;
    for (int i = 1; i <= n; ++i) {
        fin >> w[i] >> p[i];
    }
    solve(0, 0, 0);
    fout << answer;
    return 0;
}