• I' m really stuck here

Question related to mission Loading Cargo

 

I' m really stuck here... I cannot get my algorithm to work, all my attempts fail on the 5th example... Any hint will be trully appreciated...