Cod sursa(job #2523755)

Utilizator ililogIlinca ililog Data 14 ianuarie 2020 18:37:17
Problema Problema rucsacului Scor 65
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.62 kb
using namespace std;
#include<iostream>
#include<fstream>

int n, gmax;
int w[5001], p[5001];
int dp[2][5001];

int main() {

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

    fin >> n >> gmax;

    for (int i = 1; i<=n; i++) {
        fin >> w[i] >> p[i];
    }

    for (int i = 1; i<=n; i++) {
        for (int cw = 0; cw <= gmax; cw++) {
            dp[1][cw] = dp[0][cw];

            if (cw >= w[i]) {
                dp[0][cw] = max(dp[0][cw], dp[1][cw-w[i]]+p[i]);
            }
        }
    }

    fout << dp[0][gmax];

    fin.close();
    fout.close();

    return 0;
}