Cod sursa(job #2869514)

Utilizator cezar_titianuTitianu Cezar cezar_titianu Data 11 martie 2022 16:29:16
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.64 kb
#include <fstream>
#include <cmath>

int dp[2][10005];

int main() {
    std::ifstream fin("rucsac.in");
    std::ofstream fout("rucsac.out");
    int nrn, nrg;
    int wgh, price;
    fin >> nrn >> nrg;
    for(int index = 1; index <= nrn; index++){
        fin >> wgh >> price;
        for(int index2 = 1; index2 < wgh; index2++){
            dp[index & 1][index2] = dp[!(index & 1)][index2];
        }
        for(int index2 = wgh; index2 <= nrg; index2++){
            dp[index & 1][index2] = std::max(dp[!(index & 1)][index2], dp[!(index & 1)][index2 - wgh] + price);
        }
    }
    fout << dp[nrn & 1][nrg];
    return 0;
}