Cod sursa(job #3215210)

Utilizator IvanAndreiIvan Andrei IvanAndrei Data 14 martie 2024 18:56:54
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.04 kb
#include <bits/stdc++.h>

using namespace std;

const int max_size = 1e4 + 20, INF = 2e9 + 1;

int dp[max_size], pr[max_size];

void solve ()
{
    int n, wmax;
    cin >> n >> wmax;
    dp[0] = 1;
    for (int i = 1; i <= n; i++)
    {
        int w, cost;
        cin >> w >> cost;
        for (int j = wmax; j >= w; j--)
        {
            if (dp[j - w] == 1)
            {
                dp[j] = 1;
                pr[j] = max(pr[j], pr[j - w] + cost);
            }
        }
    }
    int ans = 0;
    for (int i = 0; i <= wmax; i++)
    {
        ans = max(ans, pr[i]);
    }
    cout << ans;
    cout << '\n';
}

signed main ()
{
#ifdef LOCAL
    freopen("test.in", "r", stdin);
    freopen("test.out", "w", stdout);
#else
    freopen("rucsac.in", "r", stdin);
    freopen("rucsac.out", "w", stdout);
#endif // LOCAL
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    long long tt;
    //cin >> tt;
    tt = 1;
    while (tt--)
    {
        solve();
    }
    return 0;
}