p500 greedy O(n)