Language: C++

// C++ program to solve knapsack problem using 
// branch and bound 
#include <bits/stdc++.h> 
using namespace std; 

// Structure for Item which store weight and corresponding 
// value of Item 
struct Item 
{ 
	long double weight; 
	long long int value; 
}; 

// Node structure to store information of decision 
// tree 
struct Node 
{ 
	// level --> Level of node in decision tree (or index 
	//			 in arr[] 
	// profit --> Profit of nodes on path from root to this 
	//		 node (including this node) 
	// bound ---> Upper bound of maximum profit in subtree 
	//		 of this node/ 
	long long int level, profit, bound; 
	long double weight; 
}; 

// Comparison function to sort Item according to 
// val/weight ratio 
bool cmp(Item a, Item b) 
{ 
	double r1 = (double)a.value / a.weight; 
	double r2 = (double)b.value / b.weight; 
	return r1 > r2; 
} 

// Returns bound of profit in subtree rooted with u. 
// This function mainly uses Greedy solution to find 
// an upper bound on maximum profit. 
long long int bound(Node u, long long int n, long long int W, Item arr[]) 
{ 
	// if weight overcomes the knapsack capacity, return 
	// 0 as expected bound 
	if (u.weight >= W) 
		return 0; 

	// initialize bound on profit by current profit 
	long long int profit_bound = u.profit; 

	// start including items from index 1 more to current 
	// item index 
	long long int j = u.level + 1; 
	long long int totweight = u.weight; 

	// checking index condition and knapsack capacity 
	// condition 
	while ((j < n) && (totweight + arr[j].weight <= W)) 
	{ 
		totweight += arr[j].weight; 
		profit_bound += arr[j].value; 
		j++; 
	} 

	// If k is not n, include last item partially for 
	// upper bound on profit 
	if (j < n) 
		profit_bound += (W - totweight) * arr[j].value / 
										arr[j].weight; 

	return profit_bound; 
} 

// Returns maximum profit we can get with capacity W 
long long int knapsack(long long int W, Item arr[], long long int n) 
{ 
	// sorting Item on basis of value per unit 
	// weight. 
	sort(arr, arr + n, cmp); 

	// make a queue for traversing the node 
	queue<Node> Q; 
	Node u, v; 

	// dummy node at starting 
	u.level = -1; 
	u.profit = u.weight = 0; 
	Q.push(u); 

	// One by one extract an item from decision tree 
	// compute profit of all children of extracted item 
	// and keep saving maxProfit 
	long long int maxProfit = 0; 
	while (!Q.empty()) 
	{ 
		// Dequeue a node 
		u = Q.front(); 
		Q.pop(); 

		// If it is starting node, assign level 0 
		if (u.level == -1) 
			v.level = 0; 

		// If there is nothing on next level 
		if (u.level == n-1) 
			continue; 

		// Else if not last node, then increment level, 
		// and compute profit of children nodes. 
		v.level = u.level + 1; 

		// Taking current level's item add current 
		// level's weight and value to node u's 
		// weight and value 
		v.weight = u.weight + arr[v.level].weight; 
		v.profit = u.profit + arr[v.level].value; 

		// If cumulated weight is less than W and 
		// profit is greater than previous profit, 
		// update maxprofit 
		if (v.weight <= W && v.profit > maxProfit) 
			maxProfit = v.profit; 

		// Get the upper bound on profit to decide 
		// whether to add v to Q or not. 
		v.bound = bound(v, n, W, arr); 

		// If bound value is greater than profit, 
		// then only push into queue for further 
		// consideration 
		if (v.bound > maxProfit) 
			Q.push(v); 

		// Do the same thing, but Without taking 
		// the item in knapsack 
		v.weight = u.weight; 
		v.profit = u.profit; 
		v.bound = bound(v, n, W, arr); 
		if (v.bound > maxProfit) 
			Q.push(v); 
	} 

	return maxProfit; 
} 

// driver program to test above function 
int main() 
{ 
	long long int n,w;
  	cin>>n>>w;
  	Item arr[n+1];
  	for(long long int x=0; x<n+1; x++){
     	cin>>arr[x].weight<<arr[x].value; 
    }
	cout<< knapsack(w, arr, n+1)<<"\n"; 

	return 0; 
} 


Compiler output:
a.cc: In function 'int main()':
a.cc:152:27: error: no match for 'operator<<' (operand types are 'std::basic_istream<char>::__istream_type' {aka 'std::basic_istream<char>'} and 'long long int')
  152 |         cin>>arr[x].weight<<arr[x].value;
      |         ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
      |            |                       |
      |            |                       long long int
      |            std::basic_istream<char>::__istream_type {aka std::basic_istream<char>}
a.cc:152:27: note: candidate: 'operator<<(int, long long int)' (built-in)
  152 |         cin>>arr[x].weight<<arr[x].value;
      |         ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
a.cc:152:27: note:   no known conversion for argument 1 from 'std::basic_istream<char>::__istream_type' {aka 'std::basic_istream<char>'} to 'int'
In file included from /usr/include/c++/14/regex:68,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:181,
                 from a.cc:3:
/usr/include/c++/14/bits/regex.h:1715:5: note: candidate: 'template<class _Ch_type, class _Ch_traits, class _Bi_iter> std::basic_ostream<_CharT, _Traits>& std::__cxx11::operator<<(std::basic_ostream<_CharT, _Traits>&, const sub_match<_Bi_iter>&)'
 1715 |     operator<<(basic_ostream<_Ch_type, _Ch_traits>& __os,
      |     ^~~~~~~~
/usr/include/c++/14/bits/regex.h:1715:5: note:   template argument deduction/substitution failed:
a.cc:152:36: note:   'std::basic_istream<char>::__istream_type' {aka 'std::basic_istream<char>'} is not derived from 'std::basic_ostream<_CharT, _Traits>'
  152 |         cin>>arr[x].weight<<arr[x].value;
      |                                    ^~~~~
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:41:
/usr/include/c++/14/cstddef:125:5: note: candidate: 'template<class _IntegerType> constexpr std::__byte_op_t<_IntegerType> std::operator<<(byte, _IntegerType)'
  125 |     operator<<(byte __b, _IntegerType __shift) noexcept
      |     ^~~~~~~~
/usr/include/c++/14/cstddef:125:5: note:   template argument deduction/substitution failed:
a.cc:152:12: note:   cannot convert 'std::cin.std::basic_istream<char>::operator>>(arr[x].Item::weight)' (type 'std::basic_istream<char>::__istream_type' {aka 'std::basic_istream<char>'}) to type 'std::byte'
  152 |         cin>>arr[x].weight<<arr[x].value;
      |         ~~~^~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/bits/basic_string.h:47,
                 from /usr/include/c++/14/string:54,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52:
/usr/include/c++/14/string_view:763:5: note: candidate: 'template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(basic_ostream<_CharT, _Traits>&, basic_string_view<_CharT, _Traits>)'
  763 |     operator<<(basic_ostream<_CharT, _Traits>& __os,
      |     ^~~~~~~~
/usr/include/c++/14/string_view:763:5: note:   template argument deduction/substitution failed:
a.cc:152:36: note:   'std::basic_istream<char>::__istream_type' {aka 'std::basic_istream<char>'} is not derived from 'std::basic_ostream<_CharT, _Traits>'
  152 |         cin>>arr[x].weight<<arr[x].value;
      |                                    ^~~~~
/usr/include/c++/14/bits/basic_string.h:4077:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::basic_ostream<_CharT, _Traits>& std::operator<<(basic_ostream<_CharT, _Traits>&, const __cxx11::basic_string<_CharT, _Traits, _Allocator>&)'
 4077 |     operator<<(basic_ostream<_CharT, _Traits>& __os,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:4077:5: note:   template argument deduction/substitution failed:
a.cc:152:36: note:   'std::basic_istream<char>::__istream_type' {aka 'std::basic_istream<char>'} is not derived from 'std::basic_ostream<_CharT, _Traits>'
  152 |         cin>>arr[x].weight<<arr[x].value;
      |                                    ^~~~~
/usr/include/c++/14/bitset:1687:5: note: candidate: 'template<class _CharT, class _Traits, long unsigned int _Nb> std::basic_ostream<_CharT, _Traits>& std::operator<<(basic_ostream<_CharT, _Traits>&, const bitset<_Nb>&)'
 1687 |     operator<<(std::basic_ostream<_CharT, _Traits>& __os,
      |     ^~~~~~~~
/usr/include/c++/14/bitset:1687:5: note:   template argument deduction/substitution failed:
a.cc:152:36: note:   'std::basic_istream<char>::__istream_type' {aka 'std::basic_istream<char>'} is not derived from 'std::basic_ostream<_CharT, _Traits>'
  152 |         cin>>arr[x].weight<<arr[x].value;
      |                                    ^~~~~
In file included from /usr/include/c++/14/bits/ios_base.h:46,
                 from /usr/include/c++/14/streambuf:43,
                 from /usr/include/c++/14/bits/streambuf_iterator.h:35,
                 from /usr/include/c++/14/iterator:66,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:54:
/usr/include/c++/14/system_error:339:5: note: candidate: 'template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(basic_ostream<_CharT, _Traits>&, const error_code&)'
  339 |     operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __e)
      |     ^~~~~~~~
/usr/include/c++/14/system_error:339:5: note:   template argument deduction/substitution failed:
a.cc:152:36: note:   'std::basic_istream<char>::__istream_type' {aka 'std::basic_istream<char>'} is not derived from 'std::basic_ostream<_CharT, _Traits>'
  152 |         cin>>arr[x].weight<<arr[x].value;
      |                                    ^~~~~
In file included from /usr/include/c++/14/memory:80,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:56:
/usr/include/c++/14/bits/shared_ptr.h:70:5: note: candidate: 'template<class _Ch, class _Tr, class _Tp, __gnu_cxx::_Lock_policy _Lp> std::basic_ostream<_CharT, _Traits>& std::operator<<(basic_ostream<_CharT, _Traits>&, const __shared_ptr<_Tp, _Lp>&)'
   70 |     operator<<(std::basic_ostream<_Ch, _Tr>& __os,
      |     ^~~~~~~~
/usr/include/c++/14/bits/shared_ptr.h:70:5: note:   template argument deduction/substitution failed:
a.cc:152:36: note:   'std::basic_istream<char>::__istream_type' {aka 'std::basic_istream<char>'} is not derived from 'std::basic_ostream<_CharT, _Traits>'
  152 |         cin>>arr[x].weight<<arr[x].value;
      |                                    ^~~~~
In file included from /usr/include/c++/14/istream:41,
                 from /usr/include/c++/14/sstream:40,
                 from /usr/include/c++/14/complex:45,
                 from /usr/include/c++/14/ccomplex:39,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:127:
/usr/include/c++/14/ostream:563:5: note: candidate: 'template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(basic_ostream<_CharT, _Traits>&, _CharT)'
  563 |     operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
      |     ^~~~~~~~
/usr/include/c++/14/ostream:563:5: note:   template argument deduction/substitution failed:
a.cc:152:36: note:   'std::basic_istream<char>::__istream_type' {aka 'std::basic_istream<char>'} is not derived from 'std::basic_ostream<_CharT, _Traits>'
  152 |         cin>>arr[x].weight<<arr[x].value;
      |                                    ^~~~~
/usr/include/c++/14/ostream:573:5: note: candidate: 'template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(basic_ostream<_CharT, _Traits>&, char)'
  573 |     operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)
      |     ^~~~~~~~
/usr/include/c++/14/ostream:573:5: note:   template argument deduction/substitution failed:
a.cc:152:36: note:   'std::basic_istream<char>::__istream_type' {aka 'std::basic_istream<char>'} is not derived from 'std::basic_ostream<_CharT, _Traits>'
  152 |         cin>>arr[x].weight<<arr[x].value;
      |                                    ^~~~~
/usr/include/c++/14/ostream:579:5: note: candidate: 'template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(basic_ostream<char, _Traits>&, char)'
  579 |     operator<<(basic_ostream<char, _Traits>& __out, char __c)
      |     ^~~~~~~~
/usr/include/c++/14/ostream:579:5: note:   template argument deduction/substitution failed:
a.cc:152:36: note:   'std::basic_istream<char>::__istream_type' {aka 'std::basic_istream<char>'} is not derived from 'std::basic_ostream<char, _Traits>'
  152 |         cin>>arr[x].weight<<arr[x].value;
      |                                    ^~~~~
/usr/include/c++/14/ostream:590:5: note: candidate: 'template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(basic_ostream<char, _Traits>&, signed char)'
  590 |     operator<<(basic_ostream<char, _Traits>& __out, signed char __c)
      |     ^~~~~~~~
/usr/include/c++/14/ostream:590:5: note:   template argument deduction/substitution failed:
a.cc:152:36: note:   'std::basic_istream<char>::__istream_type' {aka 'std::basic_istream<char>'} is not derived from 'std::basic_ostream<char, _Traits>'
  152 |         cin>>arr[x].weight<<arr[x].value;
      |                                    ^~~~~
/usr/include/c++/14/ostream:595:5: note: candidate: 'template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(basic_ostream<char, _Traits>&, unsigned char)'
  595 |     operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)
      |     ^~~~~~~~
/usr/include/c++/14/ostream:595:5: note:   template argument deduction/substitution failed:
a.cc:152:36: note:   'std::basic_istream<char>::__istream_type' {aka 'std::basic_istream<char>'} is not derived from 'std::basic_ostream<char, _Traits>'
  152 |         cin>>arr[x].weight<<arr[x].value;
      |                                    ^~~~~
/usr/include/c++/14/ostream:654:5: note: candidate: 'template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(basic_ostream<_CharT, _Traits>&, const _CharT*)'
  654 |     operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
      |     ^~~~~~~~
/usr/include/c++/14/ostream:654:5: note:   template argument deduction/substitution failed:
a.cc:152:36: note:   'std::basic_istream<char>::__istream_type