Cod sursa(job #2854440)

Utilizator bestman4111Tiberiu Niculae bestman4111 Data 21 februarie 2022 13:32:32
Problema Problema rucsacului Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.81 kb
#include<iostream>
#include<fstream>
#include<vector>
using namespace std;

int N, G, x, y;
vector<int> W, P;

int dp[5001][10001];

ifstream cit("rucsac.in");
ofstream afis("rucsac.out");

int main()
{
    cit>>N>>G;
    for(int i = 0; i < N; i++){
        cit>>x>>y;
        W.push_back(x);
        P.push_back(y);
    }

    for(int i = 0; i < N; i++){
        for(int j = 1; j <= G; j++){
            if(i == 0){
                if(j >= W[i]){
                    dp[i][j] = P[i];
                }
                else{
                    dp[i][j] = 0;
                }
            }
            else{
                dp[i][j] = max(dp[i-1][j], dp[i-1][j-W[i]] + P[i]);
            }
        }
    }

    afis<<dp[N-1][G];

    cit.close();
    afis.close();

    return 0;
}