Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<string>
- using namespace std;
- void Print(int s, int n, int **result, int *arr, int razmer)
- {
- int max = 0;
- while (s < razmer && max < n)
- {
- if (result[s+1][n] == result[s][n])
- s++;
- else
- {
- max += arr[s-1];
- cout << s << endl;
- s++;
- }
- }
- }
- int main()
- {
- int N, W, min = INT8_MAX;
- cin >> N >> W;
- int *arr = new int[N];
- for (int i = 0; i< N; i++)
- cin >> arr[i];
- int **result = new int*[N+1];
- for (int i = 0; i<= N; i++)
- result[i] = new int[W+1];
- for (int j=0; j <= W; j++)
- result[0][j] = 0;
- for(int s=1 ;s <= N; ++s) // s - максимальный номер предмета,
- { // который можно использовать
- for (int n=1; n <= W; ++n) // n - вместимости рюкзака
- {
- result[s][n] = result[s-1][n];
- if ( n >= arr[s] && ( result[s-1][n-arr[s]]+arr[s] > result[s][n]) )
- result[s][n] = result[s-1][n-arr[s]]+arr[s];
- }
- }
- Print(1, W, result, arr, N);
- for (int i=0; i<=N; i++)
- {
- for (int j=0; j<=W; j++)
- {
- cout.width(3);
- cout << result[i][j] << " ";
- }
- cout << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement