Cod sursa(job #3277471)

Utilizator jumaracosminJumara Cosmin-Mihai jumaracosmin Data 16 februarie 2025 12:41:42
Problema Problema rucsacului Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.78 kb
#include <bits/stdc++.h>

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

const int SIZE = 5e3 + 5;
const int MAXWEIGHT = 1e4 + 5;

int n, W;
int profit[SIZE], weight[SIZE];
int dp[MAXWEIGHT];
int ans;

int64_t max(std::vector<int64_t> v){ return *std::max_element(v.begin(), v.end()); }
int64_t min(std::vector<int64_t> v){ return *std::min_element(v.begin(), v.end()); }

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

    for(int i = 1; i <= n; ++i)
        for(int j = W - weight[i]; j >= 0; --j)
        {
            dp[j + weight[i]] = max({dp[j + weight[i]], dp[j] + profit[i]});
            ans = max({ans, dp[j + weight[i]]});
        }
    
    fout << ans;

    return 0;
}