Cod sursa(job #2854464)

Utilizator bestman4111Tiberiu Niculae bestman4111 Data 21 februarie 2022 13:59:09
Problema Problema rucsacului Scor 25
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.91 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{
                if(j - W[i] >= 0)
                    dp[i][j] = max(dp[i-1][j], dp[i-1][j-W[i]] + P[i]);
                else
                    dp[i][j] = dp[i-1][j];
            }
        }
    }

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

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

    return 0;
}