Cod sursa(job #2608688)

Utilizator FlorinHajaFlorin Gabriel Haja FlorinHaja Data 1 mai 2020 17:50:56
Problema Problema rucsacului Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.71 kb
#include<bits/stdc++.h>

using namespace std;

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

int mat[5005][10005];
int vet[5005];
int profit[5005];

int main() {
    int n, g, i, j, max = 0;
    in >> n >> g;

    mat[0][0] = 1;
    for (i = 1; i <= n; i++) {
        in >> vet[i] >> profit[i];

        for (j = vet[i]; j <= g; j++) {
            if (mat[i - 1][j] > mat[i - 1][j - vet[i]] + profit[i] && mat[i - 1][j] > 0)
                mat[i][j] = mat[i - 1][j];
            else if (mat[i - 1][j - vet[i]] > 0)
                mat[i][j] = mat[i - 1][j - vet[i]] + profit[i];
            if (i == n && mat[i][j] > max)
                max = mat[i][j];
        }
    }
    out << max - 1;
}