weird greedy using a nested loop based on an arbitrary one-dimensional array