I\'ll try to explain the problem in the math language.
Assume I have a set of items X = {x_1, x_2, ..., x_n}
. Each item of X
belongs to one of
Let's think of x_i as if it's interval with integer endpoints. f1 returns true if 2 intervals do not intersect and false otherwise. f2 compares sum lengths of intervals in subsets.
If I understand correctly, this means we can assign a value(its length) to each x_i from X. There is then no need to evaluate f2 on each possible solution / subset.
It's very unlikely that the smallest 5 x_i form the best subset. Depending on the actual data, the best subset might be the 5 biggest intervals. So I'd suggest sorting X by value. General idea is to start with the highest x and try adding more x's (highest first) till you got 5 nonoverlapping. Most likely you will find the best subset, before even generating a fraction of all the possible subsets (depends on the specific problem of course). But in worst case this is not faster than your solution.