Cod sursa(job #2567411)

Utilizator mirceatlxhaha haha mirceatlx Data 3 martie 2020 17:04:40
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.84 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#define MaxNumberOfElements 5005
#define MaxWeight 10005
using namespace std;

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

vector < pair < int , int > > arr(MaxNumberOfElements);
int dp[2][MaxWeight], N, G;  

int main()
{
    int weight, price, temp;
    fin >> N >> G;
    for(int i = 1; i <= N; i++){
        fin >> weight >> price;
        arr[i] = make_pair(weight,price);
    }
    temp = 0;
    for(int i = 1; i <= N; i++, temp = 1 - temp){
        for(int j = 0; j <= G; j++){
            dp[1 - temp][j] = dp[temp][j];
            if(arr[i].first <= j){
                dp[1 - temp][j] = max(dp[1 - temp][j],dp[temp][j - arr[i].first] + arr[i].second);
            }
        }
    }
    fout << dp[temp][G] << "\n";
    return 0;
}