Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <windows.h>
- using namespace std;
- int backpack(const int* weight, const int* cost, int V, int N)
- {
- int* array = new int[V+1];
- array[0] = 0;
- for(int i=1; i<=V; ++i) {
- array[i] = array[i-1];
- for(int j=0; j<N; ++j)
- if (weight[j]<=i)
- array[i] = max(array[i], array[i-weight[j]] + cost[j]);
- }
- int maxCost = array[V];
- delete[] array;
- return maxCost;
- }
- int main()
- {
- int N, V;
- cout << "Vvedite ob'em rukzaka: "; cin >> V;
- cout << "Vvedite kolichestvo predmetov: "; cin >> N;
- int* weight = new int[N];
- int* cost = new int[N];
- for(int i=0; i<N; ++i) {
- system("cls");
- cout << "Ostalos' predmetov: " << N-i;
- cout << "\n\nVvedite ves predmeta: "; cin >> weight[i];
- cout << "Vvedite stoimost: "; cin >> cost[i];
- }
- system("cls");
- std::cout << "Naibol'shaya stoimost' = " << backpack(weight, cost, V, N);
- delete[] weight;
- delete[] cost;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement