Cod sursa(job #3222712)

Utilizator csamoilasamoila ciprian casian csamoila Data 11 aprilie 2024 14:30:40
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.77 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NMAX = 5001;
const int GMAX = 10001;

int N, G;
int P[NMAX];
int W[NMAX];
//int DP[NMAX][GMAX]; /// DP[i][j] = profitul maxim pt un rucsac de greutate j, luand in calcul primele i greutati
/// DP[i][j] = max{DP[i-1][j], DP[i-1][j - W[i]] + P[i]
///                 ^we don't take the ith object
///                                  ^we take it

int DP[GMAX];

int main()
{
    fin >> N >> G;
    for(int i = 1; i <= N; i++) {
        fin >> W[i] >> P[i];
    }

    for(int i = 1; i <= N; i++) {
        for(int j = G; j >= W[i]; j--) {
            DP[j] = max(DP[j], DP[j - W[i]] + P[i]);
        }
    }
    fout << DP[G];
    return 0;
}