Write your pattern overview here...
💡 Use two pointers moving towards each other or in the same direction based on the problem.
1// Your code here2#include <iostream>3#include <vector>4using namespace std;56int knapsack(int W, vector<int> &wt, vector<int> &val, int n) {7 vector<vector<int>> dp(n + 1, vector<int>(W + 1, 0));89 for (int i = 1; i <= n; i++) {10 for (int w = 1; w <= W; w++) {11 if (wt[i - 1] <= w) {12 dp[i][w] = max(13 val[i - 1] + dp[i - 1][w - wt[i - 1]],14 dp[i - 1][w]15 );16 } else {17 dp[i][w] = dp[i - 1][w];18 }19 }20 }2122 return dp[n][W];23}2425int main() {26 int n, W;27 cin >> n;2829 vector<int> wt(n), val(n);3031 for (int i = 0; i < n; i++)32 cin >> wt[i];3334 for (int i = 0; i < n; i++)35 cin >> val[i];3637 cin >> W;3839 cout << knapsack(W, wt, val, n);4041 return 0;42}