So i just wanted to know the reason why is it happening. This gives TLE
public:
    int knapsack(vector<pair<int,int>> a , int wght , int i , vector<vector<int>> &dp){
        if(i==a.size() || wght == 0){
        return 0;
    }
    if(dp[i][wght] != -1) return dp[i][wght];
    else if(a[i].second > wght){
      return dp[i][wght] = knapsack(a , wght , i+1 ,dp);
    }
    else {
      int include = a[i].first + knapsack(a , wght - a[i].second , i+1 ,dp);
      int exclude = knapsack(a , wght , i+1 ,dp);
      dp[i][wght] = max(include , exclude);
      return dp[i][wght];
      }
    }
    public:
    int knapSack(int W, int wt[], int val[], int n) {
        vector<pair<int,int>> a;
        //memset(dp , -1 , sizeof(dp));
        
    vector<vector<int>> dp(1005, vector<int> (1005 , -1));
        for(int i=0;i<n;i++){
            a.push_back(make_pair(val[i],wt[i]));
        }
        return knapsack(a , W , 0 ,dp);
    }
while this one gets accepted
public:
    int dp[1005][1005];
    int knapsack(vector<pair<int,int>> a , int wght , int i){
        if(i==a.size() || wght == 0){
        return 0;
    }
    if(dp[i][wght] != -1) return dp[i][wght];
    else if(a[i].second > wght){
      return dp[i][wght] = knapsack(a , wght , i+1);
    }
    else {
      int include = a[i].first + knapsack(a , wght - a[i].second , i+1);
      int exclude = knapsack(a , wght , i+1);
      dp[i][wght] = max(include , exclude);
      return dp[i][wght];
    }
    }
    public:
    int knapSack(int W, int wt[], int val[], int n) {
        vector<pair<int,int>> a;
        memset(dp , -1 , sizeof(dp));
        for(int i=0;i<n;i++){
            a.push_back(make_pair(val[i],wt[i]));
        }
        return knapsack(a , W , 0);
    }
I know for a fact that the algo is correct since it is getting accepted , I also researched about vectors being slow but can't find the convincing answers. Please tell my why it is happening?
 
    