Cod sursa(job #3247372)

Utilizator lefterache_stefanLefterache Stefan lefterache_stefan Data 7 octombrie 2024 14:11:15
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.8 kb
#include <bits/stdc++.h>
using namespace std;

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

const unsigned int MAXIM = 10001;

unsigned int M0[MAXIM], M1[MAXIM], valoare[MAXIM], greutate[MAXIM];
unsigned int n, greutate_maxima;

static inline void citire() {
    fin >> n >> greutate_maxima;
    for (unsigned int i = 1; i <= n; i++) {
        fin >> greutate[i] >> valoare[i];
    }
}

int main() {
    citire();
    unsigned int *pointer0 = M0, *pointer1 = M1;
    for (unsigned int i = 1; i <= n; i++) {
        for (unsigned int j = 1; j <= greutate_maxima; j++) {
            pointer1[j] = greutate[i] > j ? pointer0[j] : max(pointer0[j], valoare[i] + pointer0[j-greutate[i]]);
        }
        swap(pointer0, pointer1);
    }
    fout << max(pointer0[greutate_maxima], pointer1[greutate_maxima]);
}