Cod sursa(job #2080523)

Utilizator SlevySlevoaca Stefan-Gabriel Slevy Data 3 decembrie 2017 01:58:25
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.9 kb
#include <iostream>
#include <fstream>
#include <cstring>
#define NMAX 5001
#define GMAX 10001
#define weight first
#define profit second
#define UNDEF -1

using namespace std;

pair<int, int> objs[NMAX];
int dp[2][GMAX], n, G;

void read() {

    int x, y;

    ifstream in("rucsac.in");
    in >> n >> G;

    for (int i = 1; i <= n; i++) {

        in >> x >> y;
        objs[i] = make_pair(x, y);
    }
    in.close();
}

int main()
{
    read();

    for (int i = 1; i <= n; i++) {

        for (int j = 1; j <= G; j++) {

            if (objs[i].weight > j)
                dp[1][j] = dp[0][j];
            else
                dp[1][j] = max(dp[0][j], dp[0][j - objs[i].weight] + objs[i].profit);
        }
        memcpy(dp[0], dp[1], sizeof(int) * GMAX);
    }

    ofstream out("rucsac.out");
    out << dp[0][G] << "\n";
    out.close();

    return 0;
}