Cod sursa(job #2285537)

Utilizator Moise_AndreiMoise Andrei Moise_Andrei Data 18 noiembrie 2018 18:30:29
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.54 kb
#include <bits/stdc++.h>
using namespace std;
ifstream in("rucsac.in");
ofstream out("rucsac.out");
int dp[5][10005];
int main()
{
    int n, k;
    in >> n >> k;
    int l = 1, mx = 0;
    for(int i = 1; i <= n; i ++, l = 1 - l)
    {
        int a, b;
        in >> a >> b;
        for(int j = 0; j <= k; j ++)
        {
            dp[l][j] = dp[1 - l][j];
            if(j >= a)
                dp[l][j] = max(dp[l][j], dp[1 - l][j - a] + b);
            mx = max(mx, dp[l][j]);
        }
    }
    out << mx;
    return 0;
}