Cod sursa(job #3223210)

Utilizator CastielGurita Adrian Castiel Data 12 aprilie 2024 17:50:53
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.81 kb
#include <bits/stdc++.h>
using namespace std;

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

const int MAXN = 5005;
const int MAXW = 10005;

long long n, w, g[MAXN], v[MAXN], val[MAXW], smax;
const int MOD = 1e9 + 7;

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

    fill(val, val + w + 1, 0); // Initialize val array with 0

    for(int k = 1; k <= n; k++) {
        for(int x = w; x >= g[k]; x--) { // Change condition to x >= g[k]
            val[x] = max(val[x], val[x - g[k]] + v[k]); // Update val[x] using the maximum of current val[x] and val[x - g[k]] + v[k]
        }
    }

    for(int i = 1; i <= w; i++) {
        smax = max(smax, val[i]);
    }
    fout << smax;

    fin.close();
    fout.close();
    return 0;
}