Algorithm for making a sandwich