Cod sursa(job #2940059)

Utilizator victor_gabrielVictor Tene victor_gabriel Data 14 noiembrie 2022 18:59:38
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.77 kb
#include <fstream>

using namespace std;

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

const int DIM = 5001;
const int GMAX = 10001;

int N, W;
int dp[2][GMAX];
struct object {
    int w, p;
} v[DIM];

int main() {
    fin >> N >> W;
    for (int i = 1; i <= N; i++)
        fin >> v[i].w >> v[i].p;

    int line = 1;
    for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= W; j++) {
            int maxProfit = dp[1 - line][j];
            if (j - v[i].w >= 0)
                maxProfit = max(maxProfit, dp[1 - line][j - v[i].w] + v[i].p);
            dp[line][j] = maxProfit;
        }
        line = 1 - line;
    }

    int solution = 0;
    for (int i = 1; i <= W; i++)
        solution = max(solution, dp[1 - line][i]);
    fout << solution;

    return 0;
}