Language: C++

// Here is the top-down approach of dynamic programming 
// to solve the question, it's a c++ implementation 
#include <bits/stdc++.h> 
using namespace std; 
#define ll long long

/* Returns the value of maximum profit */
int knapSackRec(int W, ll wt[], ll val[], int i, ll** dp) 
{ 
	// base condition 
	if (i < 0) 
		return 0; 
	if (dp[i][W] != -1) 
		return dp[i][W]; 

	if (wt[i] > W) { 

		// store the value of function call stack in 
		// table before return 
		dp[i][W] = knapSackRec(W, wt, val, i - 1, dp); 
		return dp[i][W]; 
	} 
	else { 

		// store value in a table before return 
		dp[i][W] = max(val[i] + knapSackRec(W - wt[i], wt, val, i - 1, dp), 
					knapSackRec(W, wt, val, i - 1, dp)); 

		// return value of table after storing 
		return dp[i][W]; 
	} 
} 

int knapSack(int W, ll wt[], ll val[], int n) 
{ 
	ll** dp; // double pointer to declare the table dynamically 
	dp = new ll*[n]; 

	// loop to create the table dynamically 
	for (int i = 0; i < n; i++) 
		dp[i] = new ll[W + 1];	 

	// loop to initially filled the table with -1 
	for (int i = 0; i < n; i++) 
		for (int j = 0; j < W + 1; j++) 
			dp[i][j] = -1; 
	return knapSackRec(W, wt, val, n-1, dp); 
} 

int main() 
{ 
	ll val[100]; 
	ll wt[100]; 
	int W ; 
	int n;
  	cin>>n>>W;
  	for(int i=0;i<n;i++)
    {
      cin>>wt[i]>>val[i];
    }
	cout << knapSack(W, wt, val, n); 
	return 0; 
} 


Compiler output:
a.cc: In function 'int knapSackRec(int, long long int*, long long int*, int, long long int**)':
a.cc:26:31: error: no matching function for call to 'max(long long int, int)'
   26 |                 dp[i][W] = max(val[i] + knapSackRec(W - wt[i], wt, val, i - 1, dp),
      |                            ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   27 |                                         knapSackRec(W, wt, val, i - 1, dp));
      |                                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:3:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:26:31: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   26 |                 dp[i][W] = max(val[i] + knapSackRec(W - wt[i], wt, val, i - 1, dp),
      |                            ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   27 |                                         knapSackRec(W, wt, val, i - 1, dp));
      |                                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:26:31: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   26 |                 dp[i][W] = max(val[i] + knapSackRec(W - wt[i], wt, val, i - 1, dp),
      |                            ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   27 |                                         knapSackRec(W, wt, val, i - 1, dp));
      |                                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~