function [items_to_take, max_fitted_value, max_fitted_volume] = backpack( 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 will try all possible combinations (permutations) of items 
	% and find the best one
	% ideally we want to go through choose m object out of N sequence
	% for m=[1:N] but for simplicity. I choose very inefficient
	% algorithm.
	% this algorithm is highly inefficient but always provides right answer
	%
	% one source of inefficiency int he following
        % suppose items [1,2,3] fits to backpack
	% there	is no need to check all possible permutations of this since backpack
	% does not care about order
	%
	% also there is no need to include items which a larger then backpack 
	% into the possible items list

	N=length(volumes); % total number of items
	p=1:N;  % initial permutation set (order of items to choose)
	pold=N:-1:1; % some combo which for sure not equal to the first permutation list
	pbest=NaN; % this will hold the best combination if such exist
	max_fitted_value=-Inf; % the maximum value which fits in the backpack

	while ( any(pold ~=p) ) % notice any operator since we compare 2 vectors
		items_volume=0;
		items_value=0;
		for i=N:-1:1  % we try to fit maximum number of items first
			items_volume= sum(volumes(p(1:i)));
			items_value = sum(values(p(1:i)));
			if ( (items_volume <= backpack_size) && (items_value > max_fitted_value) )
				pbest=p(1:i);
				max_fitted_value=items_value;
				max_fitted_volume=items_volume;
				% no need to try smaller set since
				% it garanteed to have a smaller value
				break; 
			end
		end
		pold=p;
		p=permutation(pold);
	end
	items_to_take=pbest;
end
			




