Advertisement
allia

рюкзак 1.0

Nov 19th, 2020
468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. #include<iostream>
  2. #include<string>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.  int N, W;
  9.  cin >> N >> W;
  10.  
  11.  int *arr = new int[N];
  12.  
  13.  for (int i = 0; i< N; i++)
  14.    cin >> arr[i];
  15.  
  16. int **result = new int*[N+1];
  17.  
  18. for (int i = 0; i<= N; i++)
  19.    result[i] = new int[W+1];
  20.  
  21. for (int j=0; j <= W; j++)
  22.    result[0][j] = 0;
  23.  
  24. for(int s=1 ;s <= N; ++s)      
  25.     for (int n=0; n <= W; ++n)  
  26.     {
  27.         if ( n >= arr[s-1] && ( result[s-1][n-arr[s-1]]+arr[s-1] > result[s-1][n]) )
  28.             result[s][n] = result[s-1][n-arr[s-1]]+arr[s-1];
  29.         else result[s][n] = result[s-1][n];
  30.     }
  31. cout << result[N][W];
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement