Cod sursa(job #2781350)

Utilizator andcovAndrei Covaci andcov Data 9 octombrie 2021 11:16:33
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.3 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 din0[10001];
int din1[10001];
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 _) {
    bool zero = true;
    for(int j = 1; j <= n; ++j) {
        for(int i = 1; i <= maxw; ++i) {
            if(zero) {
                int aux = (i >= items[j - 1].first) ? din0[i - items[j - 1].first] + items[j - 1].second : 0;
                din1[i] = max(din0[i], aux);
            } else {
                int aux = (i >= items[j - 1].first) ? din1[i - items[j - 1].first] + items[j - 1].second : 0;
                din0[i] = max(din1[i], aux);
            }
        }
        zero = !zero;
    }

    if(zero)
        return din0[maxw];

    return din1[maxw];
}

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

    out << res;

    out.close();
}


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

    return 0;
}