Something like Element,Id[Element]Element,Id[Element] for each of activity individually. Now you can sort all three activities. The ways you can form combinations are max(A)max(A), max(B):(id[B]≠id[A])max(B):(id[B]≠id[A]), max(C):id[C]≠id[A],id[C]≠id[B]max(C):...