Cod sursa(job #2118085)

Utilizator PondorastiAlex Turcanu Pondorasti Data 29 ianuarie 2018 23:54:54
Problema Problema rucsacului Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.83 kb
#include <fstream>

using namespace std;

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

inline int max(int a, int b) {
    return a < b ? b : a;
}

struct Object {
    int weight, profit;
};

const int MAX_WEIGHT = 1e4, MAX_ITEMS = 5e3;
int dp[2][MAX_WEIGHT + 2];
int items, totalWeight;
Object a[MAX_ITEMS + 2];

int main() {
    in >> items >> totalWeight;
    for (int index = 1; index <= items; ++index) {
        in >> a[index].weight >> a[index].profit;
    }
    for (int index = 1; index <= items; ++index) {
        dp[index % 2][a[index].weight] = a[index].profit;
        for (int w = a[index].weight + 1; w <= totalWeight; ++w) {
            dp[index % 2][w] = max(dp[(index - 1) % 2][w], dp[(index - 1) % 2][w - a[index].weight] + a[index].profit);
        }
    }
    out << dp[items % 2][totalWeight] << "\n";
    return 0;
}