Cod sursa(job #3318281)

Utilizator AndreiCod123Sitaru Mircea AndreiCod123 Data 27 octombrie 2025 19:53:14
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.71 kb
#include<bits/stdc++.h>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");

vector<int> weight, profit, dp;
int n, g, ans, i, j;

int main()
{
    fin>> n>> g;
    dp.assign(g+1, -1);
    weight.resize(n+1);
    profit.resize(n+1);

    for(i = 1; i <= n; i++)
        fin>> weight[i]>> profit[i];

    dp[0] = 0;
    for(i = 1; i <= n; i++)
    {
        for(j = g-weight[i]; j >= 0; j--)
        {
            if(dp[j] != -1 && dp[j+weight[i]] < dp[j] + profit[i])
                dp[j+weight[i]] = dp[j] + profit[i];
        }
    }

    for(i = 1; i <= g; i++)
        ans = max(ans, dp[i]);
    fout<< ans;
    fin.close();
    fout.close();
    return 0;
}