The Greedy Procedure for Resource Allocation Problems: Necessary and Sufficient Conditions for Optimality