The greedy procedure for resource allocation problems: Necessary and sufficient conditions for optimality