Cod sursa(job #3309097)

Utilizator alesiodemiriAlesio Demiri alesiodemiri Data 1 septembrie 2025 00:36:49
Problema Problema rucsacului Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.16 kb
#include <iostream>
#include <queue>
#include <algorithm>
#include <set>
#include <map>
#include <stack>
#include <vector>
#include <string>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <iomanip>

using namespace std;

#define ll long long

int n, capacity;
vector<int> weights, profits;

void ReadData() {
    cin >> n >> capacity;
    int val = 0;
    for(int i = 0; i < n; ++i){
        cin >> val;
        weights.push_back(val);
        cin >> val;
        profits.push_back(val);
    }
}

int dfs(int index, int cap){
    if(index == n) return 0;
    if (cap >= weights[index]){
        int left = profits[index] + dfs(index + 1, cap - weights[index]);
        int right = dfs(index + 1, cap);
        return max(left, right);
    }
    return dfs(index + 1, cap);
}

void Solve() {
    cout << dfs(0, capacity) << "\n";
    return;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    freopen("rucsac.in", "r", stdin);
    freopen("rucsac.out", "w", stdout);

    int t = 1;
    // cin >> t; // Uncomment for multiple test cases
    while (t--) {
        ReadData();
        Solve();   
    }
    return 0;
}