Cod sursa(job #1686215)

Utilizator hunix94Karaman Hunor hunix94 Data 12 aprilie 2016 09:41:01
Problema Problema rucsacului Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 0.82 kb
#include <iostream>
#include <fstream>

using namespace std;

int G, n;

int main() {
    ifstream input("rucsac.in");

    input >> n >> G;

    int W[G + 1];
    int sub[G + 1];

    for (int i = 0; i <= G; i++) {
        W[i] = 0;
    }

    for (int i = 0; i < n; i++) {

        for (int i = 0; i <= G; i++) {
            sub[i] = W[i];
        }

        int cw, cp;
        input >> cw >> cp;

        for (int y = 0; y <= G; y++) {
            int ww = y + cw;
            int pp = W[y] + cp;
            if (pp > W[ww]) sub[ww] = pp;
        }

        for (int i = 0; i <= G; i++) {
            W[i] = sub[i];
        }
    }

    int max = 0;

    for (int i = 1; i <= G; i++) {
        if(W[i] > max) max = W[i];
    }

    ofstream output("rucsac.out");
    output << max << '\n';
    output.close();

    return 0;
}