Cod sursa(job #2312785)

Utilizator SirStevensIonut Morosan SirStevens Data 5 ianuarie 2019 15:26:09
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.8 kb
#include <iostream>
#include <algorithm>
#include <climits>
#include <vector>
#include <fstream>

using namespace std;

typedef pair <int, int> pereche;

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

vector<pereche> data;
int Gmax, DP[3][10001], n;

void read() {
    in >> n >> Gmax;
    int x, y;
    while(in >> x >> y){
        data.push_back({x, y});
    }
}

void solve(){

    int l = 1;
    for(int i = 1; i <= data.size(); i++, l = 3 -l){
        for(int j = 0; j <= Gmax; j++){
            DP[3-l][j] = DP[l][j];
            if(j >= data[i-1].first)
                DP[3-l][j] = max(data[i-1].second + DP[l][j - data[i-1].first], DP[3-l][j]);
     //       DP[3-l][j] = max(DP[3-l][j], DP[l][j]);
        }


    }

    out << DP[1][Gmax] << '\n';

}

int main() {
    read();
    solve();
    return 0;
}