Cod sursa(job #2717314)

Utilizator witekIani Ispas witek Data 7 martie 2021 01:50:42
Problema Problema rucsacului Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.75 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

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

struct obiect {
    int g, val;
};

int n, gmax, row = 1;
vector <obiect> v;
int a[3][5005];

int otherRow(int l) {
    return 3 - l;
}

void rucsac() {
    for(int i = 1; i <= n; ++i, row = otherRow(row))
        for(int j = 1; j <= gmax; ++j)
            if(v[row].g > j)
                a[row][j] = a[otherRow(row)][j];
            else a[row][j] = max(a[otherRow(row)][j], a[otherRow(row)][j - v[i].g] + v[i].val);
}

int main()
{
    f >> n >> gmax;
    v = vector <obiect> (n + 1);
    for(int i = 1; i <= n; ++i)
        f >> v[i].g >> v[i].val;
    rucsac();
    g << a[otherRow(row)][gmax];
}