Cod sursa(job #1599935)

Utilizator DeehoroEjkoliPop Darian DeehoroEjkoli Data 14 februarie 2016 15:47:56
Problema Problema rucsacului Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.01 kb
#include <fstream>
#include <algorithm>
#include <vector>
#include <cmath>
#define nmax 10005
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");

int matrix[nmax][nmax], n, g;
vector < pair < int, int > > v;

void assign_first_position() {
    v.push_back(make_pair(0, 0));
}

void read_input() {
    assign_first_position();
    fin >> n >> g;
    for (int i = 1; i <= n; ++i) {
        int x, y;
        fin >> x >> y;
        v.push_back(make_pair(x, y));
    }
    sort(v.begin(), v.end());
}

void ghiozdan() {
    for (int i = 1; i <= v.size() - 1; ++i) {
        for (int w = 1; w <= g; ++w) {
            int c1, c2;
            c1 = matrix[i - 1][w];
            if (w >= v[i].first)
                c2 = v[i].second + matrix[i - 1][w - v[i].first];
            matrix[i][w] = max(c1, c2);
            if (i == v.size() - 1 and w == g)
                fout << matrix[i][w];
        }
    }
}

int main()
{
    read_input();
    ghiozdan();
    return 0;
}