Cod sursa(job #3354805)

Utilizator n0bmasterMihut Matei n0bmaster Data 20 mai 2026 19:15:34
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.66 kb
#include<bits/stdc++.h>
using namespace std;
ifstream fcin("rucsac.in");
ofstream fcout("rucsac.out");
typedef struct {
    int weight;
    int price;
} item;

int main(void) {
    int n, wmax;
    fcin >> n >> wmax;

    vector<item> items(n + 2, {0, 0});  
    for(int i = 1; i <= n; i++)
        fcin >> items[i].weight >> items[i].price;

    // dp[i] = valoarea maxima la capacity i
    vector<int> dp(wmax + 1, 0);

    for(int j = 1; j <= n; j++)
        for(int i = wmax; i >= items[j].weight; i--)  // descrescator: fiecare item luat o singura data
            dp[i] = max(dp[i], dp[i - items[j].weight] + items[j].price);

    fcout << dp[wmax] << "\n";
    return 0;
}