public List <int> Run(Backpack backpack, ref double sumCost)
        {
            sumCost = 0;
            BackpackInfo result = new BackpackInfo()
            {
                SelectedItems = new List <bool>(),
                SummCost      = 0,
                SummMass      = 0
            };

            BackpackInfo startBackpackInfo = new BackpackInfo()
            {
                SelectedItems = new List <bool>(),
                SummCost      = 0,
                SummMass      = 0
            };

            Rec(backpack, startBackpackInfo, ref result);

            List <int> resultNums = new List <int>();

            for (int i = 0; i < result.SelectedItems.Count; i++)
            {
                if (result.SelectedItems[i])
                {
                    sumCost += backpack.Items[i].Cost;
                    resultNums.Add(backpack.Items[i].Num);
                }
            }

            return(resultNums);
        }
        public List <int> Run(Backpack backpack, ref double sumCost)
        {
            sumCost = 0;
            BackpackInfo result = new BackpackInfo()
            {
                Items    = new List <bool>(),
                SummCost = 0,
                SummMass = 0
            };

            long    maxIter = (long)Math.Pow(2, backpack.Items.Count);
            BinMask binMask = new BinMask(backpack.Items.Count);

            for (long i = 0; i < maxIter; i++)
            {
                BackpackInfo curBackpack = new BackpackInfo()
                {
                    Items    = new List <bool>(),
                    SummCost = 0,
                    SummMass = 0
                };

                for (int j = 0; j < binMask.Mask.Count; j++)
                {
                    curBackpack.Items.Add(binMask.Mask[j]);
                    if (binMask.Mask[j])
                    {
                        curBackpack.SummCost += backpack.Items[j].Cost;
                        curBackpack.SummMass += backpack.Items[j].Mass;
                    }
                }
                binMask++;

                if (curBackpack.SummMass <= backpack.MaxMass && curBackpack.SummCost > result.SummCost)
                {
                    result = curBackpack;
                }
            }

            List <int> resultNums = new List <int>();

            for (int i = 0; i < result.Items.Count; i++)
            {
                if (result.Items[i])
                {
                    sumCost += backpack.Items[i].Cost;
                    resultNums.Add(backpack.Items[i].Num);
                }
            }

            return(resultNums);
        }
        private void Rec(Backpack backpack, BackpackInfo curBackpack, ref BackpackInfo bestBackpack)
        {
            if (curBackpack.SelectedItems.Count == backpack.Items.Count)
            {
                if (curBackpack.SummCost > bestBackpack.SummCost && curBackpack.SummMass <= backpack.MaxMass)
                {
                    bestBackpack = curBackpack.Copy();
                }
            }
            else
            {
                int indexCurItem = curBackpack.SelectedItems.Count;

                BackpackInfo unselectItem = curBackpack.Copy();
                unselectItem.SelectedItems.Add(false);
                Rec(backpack, unselectItem, ref bestBackpack);

                BackpackInfo selectItem = curBackpack.Copy();
                selectItem.SelectedItems.Add(true);
                selectItem.SummCost += backpack.Items[indexCurItem].Cost;
                selectItem.SummMass += backpack.Items[indexCurItem].Mass;
                Rec(backpack, selectItem, ref bestBackpack);
            }
        }