Codility: MaxZeroProduct - complexity issues
You don't need to perform a full search.  Instead, we can transform it into a simple 2D geometry problem.  Consider the values as vectors in the "two" and "five" directions (which is equivalent to what you've already done).  For the example [25, 10, 25, 10, 32], we have two vectors 0,2, two of 1,1, and one of 5,0.

Now, you're looking for the sum of vectors that maximises the smaller dimension.  We can start by choosing the value with the largest minimum dimension (that's one of the 1,1 vectors).  From there, choose the next value that maximises the minimum dimension (the other 1,1).  From there, neither of the alternatives increases the minimum dimension, so our first partial result is 2.

Next, backtrack and choose the next-best value (5,0).  Now, the best value to add to this (to exceed the current best) is one with a large "five" dimension, namely (0,2).

This reasoning should now lead you to a way to order the array (or multiple copies of the array) so that we can short-circuit as many branches of this backtracking as possible (i.e. be able to say "with this position, we'll never see a value that can improve our score; finish this branch now").