Cod sursa(job #3354581)

Utilizator leoebunLeonard Neacsa leoebun Data 19 mai 2026 09:27:00
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.81 kb
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <fstream>

using namespace std;


int main() {

    std::ios::sync_with_stdio(false);

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

    int n, w;

    fin >> n >> w;

    vector<pair<int, int>> items;
    items.push_back({0, 0});
    for (int i = 0; i < n; i++) {
        int x, y;
        fin >> x >> y;
        items.push_back({x, y});
    }

    vector<int> dp(w + 1, 0);

    for (int i = 1; i <= n; i++) {
        auto [weight, price] = items[i];
        for (int j = w; j >= 0; j--) {
            int x = j - weight;
            if (x >= 0) {
                dp[j] = max(dp[j], dp[x] + price);
            }

        }
    }


    fout << dp[w] << ' ';

    fin.close();
    fout.close();
    return 0;
}