Advertisement
allia

рюкзак 2.0

Nov 19th, 2020
499
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.69 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.  for (int i = 0; i < N; i++)
  13.    cin >> arr[i];
  14.  
  15.  int *w = new int[N];
  16.  for (int i = 0; i < N; i++)
  17.    cin >> w[i];
  18.  
  19. int **result = new int*[N+1];
  20.  
  21. for (int i = 0; i <= N; i++)
  22.    result[i] = new int[W+1];
  23.  
  24. for (int j=0; j <= W; j++)
  25.    result[0][j] = 0;
  26.  
  27. for(int s=1 ;s <= N; ++s)      
  28.     for (int n=0; n <= W; ++n)  
  29.     {
  30.         if ( n >= w[s-1] && ( result[s-1][n-w[s-1]]+arr[s-1] > result[s-1][n]) )
  31.             result[s][n] = result[s-1][n-w[s-1]]+arr[s-1];
  32.         else result[s][n] = result[s-1][n];
  33.  
  34.     }
  35.  
  36.  cout << result[N][W];
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement