Cod sursa(job #2780282)

Utilizator andcovAndrei Covaci andcov Data 6 octombrie 2021 17:11:58
Problema Problema rucsacului Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1 kb
//
// Created by Andrei Covaci on 03.09.2021.
//

#include <fstream>
#include <vector>

using namespace std;

#define INPUT "rucsac.in"
#define OUTPUT "rucsac.out"
//#define INPUT "input.in"
//#define OUTPUT "output.out"

#define usi unsigned short int

int maxw, n;
//     [weight][items]
int din[10001][5001];
vector<pair<usi, usi>> items;

int read() {
    ifstream in(INPUT);

    int w, p;

    in >> n >> maxw;

    for(int i = 0; i < n; ++i) {
        in >> w >> p;
        items.emplace_back(w, p);
    }

    in.close();
    return 0;
}

int solve(int _) {
    for(int i = 1; i <= maxw; ++i) {
        for(int j = 1; j <= n; ++j) {
            int aux = (i >= items[j - 1].first) ? din[i - items[j - 1].first][j - 1] + items[j - 1].second : 0;
            din[i][j] = max(din[i][j - 1], aux);
        }
    }


    return din[maxw][n];
}

void print(int res) {
    ofstream out(OUTPUT);

    out << res;

    out.close();
}


int main() {
    auto in = read();
    auto out = solve(in);
    print(out);

    return 0;
}