calculate knapstack