Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<string>
- using namespace std;
- int main()
- {
- int N, W;
- 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)
- for (int n=0; n <= W; ++n)
- {
- if ( n >= arr[s-1] && ( result[s-1][n-arr[s-1]]+arr[s-1] > result[s-1][n]) )
- result[s][n] = result[s-1][n-arr[s-1]]+arr[s-1];
- else result[s][n] = result[s-1][n];
- }
- cout << result[N][W];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement