greedy solver optimized by making it not repeat the calculations for same combination