Cod sursa(job #2478213)

Utilizator BogdanRazvanBogdan Razvan BogdanRazvan Data 21 octombrie 2019 19:22:38
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.63 kb
#include <bits/stdc++.h>

using namespace std;

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

const int N = 5005, M = 10005;

int w[N], p[N], d[2][M];

int main()
{
    ios::sync_with_stdio(false);
    fin.tie(0);

    int n, m;

    fin >> n >> m;
    for(int i = 1; i <= n; ++i) fin >> w[i] >> p[i];
    int phase = 1;
    for(int i = 1; i <= n; ++i, phase ^= 1) {
        for(int j = 0; j <= m; ++j) {
            d[phase ^ 1][j] = d[phase][j];
            if(j >= w[i]) d[phase ^ 1][j] = max(d[phase ^ 1][j], d[phase][j - w[i]] + p[i]);
        }
    }
    fout << d[phase][m];
    return 0;
}