function [items_to_take, max_fitted_value, max_fitted_volume] = backpack_binary( backpack_size, volumes, values )
	% returns list of items which fit in backpack and have maximum total value
	% backpack_size - total volume of backpack
	% volumes - vector of items volumes
	% values - vector of items values

	% we need to generate vector x which holds take/do not take values for each item
	% for example x=[1,0,0,1,1] means take 1st, 4th, and 5th item
	% to generate all possible cases go over all possible combos of 1 and 0
	% Easy to see similarity to binary system.
	% I will start with x=[0, 0, 0, ... ,1]
	% and add 1 to the last element according to the binary arithmetic rules
	% until I reach x=[1, 1, 1, ... ,1] and 
	% then x=[0, 0, 0, ... , 0] which is overfilled 111..1 +1  
	% this will sample all possible combinations.

	% nested function do analog to binary 1 addition
	function xout=add_one(x)
		xout = x;
		for i=N:-1:1
			xout(i)=x(i)+1;
			if (xout(i) == 1 ) 
				% we add 1 to 0 no overfill we can stop adding 1
				break;
			else
				% we add 1 to 1 according to binary it is equal to 10
				% we need to move overfilled 1 to next digit
				xout(i)=0; 
			end
		end
	end

	N=length(values);
	x=zeros(1, N); x(end)=1; % assigning 00000..001 initial choice set
	max_fitted_value=-Inf; % the maximum value which fits in the backpack
	xbest=NaN;

	while ( any(x~=0) )
		items_volume=sum(volumes .* x);
		items_value=sum(values  .* x);
		if ( (items_volume <= backpack_size) && (items_value > max_fitted_value) )
			xbest=x;
			max_fitted_value=items_value;
			max_fitted_volume=items_volume;
		end
		x=add_one(x);
	end	

	indexes=1:N;
	items_to_take=indexes(xbest==1); % think about this for a while
end





