Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- class TBag {
- private:
- unsigned amount;
- unsigned capacity;
- std::vector<unsigned> mass;
- std::vector<unsigned> cost;
- std::vector<std::vector<unsigned>> total_cost;
- public:
- TBag(unsigned a, unsigned c) : amount(a), capacity(c), mass(amount), cost(amount), total_cost(amount + 1, std::vector<unsigned>(capacity + 1)) {}
- void ReadFromStream(std::istream& stream) {
- for (auto iter = mass.begin(); iter != mass.end(); ++iter) {
- stream >> *iter;
- }
- for (auto iter = cost.begin(); iter != cost.end(); ++iter) {
- stream >> *iter;
- }
- }
- void Fill() {
- for (unsigned i = 0; i <= amount; ++i) {
- total_cost[i][0] = 0;
- }
- for (unsigned j = 0; j <= capacity; ++j) {
- total_cost[0][j] = 0;
- }
- for (unsigned i = 1; i <= amount; ++i) {
- for (unsigned j = 1; j <= capacity; ++j) {
- if (j >= mass[i - 1]) {
- total_cost[i][j] = std::max(total_cost[i - 1][j], total_cost[i - 1][j - mass[i - 1]] + cost[i - 1]);
- } else {
- total_cost[i][j] = total_cost[i - 1][j];
- }
- }
- }
- }
- unsigned MaxValue() {
- Fill();
- return total_cost[amount][capacity];
- }
- };
- TBag create(std::istream& stream) {
- unsigned n, m;
- stream >> n >> m;
- TBag t(n, m);
- t.ReadFromStream(stream);
- return t;
- }
- int main() {
- TBag b = create(std::cin);
- std::cout << b.MaxValue() << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement