I will try to provide an interesting way of breaking ties.
Let's assign another value to the items, let the value of the i_th item be called v[i].
Let there T items, the i-th item will have = , weight = .
Among all the subsets of maximum weight, we will look for the one that has the maximum accumulated value of the items. I have set the values as powers of two so as to guarantee that an item that comes first in the array is prioritized over all it's successors.
Here is a practical example:
Consider N = 8, as the limit weight that we have.
Items {8, 4, 2, 2}
Values {8, 4, 2, 1}
We will have two distinct subsets that have weight sum = 8, {8} and {4, 2, 2}.
But the first has accumulated value = 8, and the other one has accumulated_value = 7, so we will choose {8} over {4, 2, 2}.
The idea now is to formulate a dynamic programming that takes into account the total value.
= maximum accumulated value of among all subset of items from the interval [0, i] that have total weight = W.
I will give a pseudocode for the solution
Set all DP[i][j] = -infinity
DP[0][0] = 0
for(int weight = 0; weight <= N; ++weight )
{
for(int item = 0; item < T; ++item )
{
DP[item][weight] = DP[item - 1][weight]; // not using the current item
if( v[item] < weight ) continue;
else
{
DP[item][weight] = max( DP[item][weight], DP[item - 1][ weight - w[item]] + v[item])
}
}
}
How to recover the items is really trivial after running the algorithm.
DP[T][N] will be a sum of powers of two (or -infinity if it is not possible to select items that sum up to N) and the i-th item belongs to the answer if and only if, (DP[T][N] & v[i]) == v[i].