master plan algorithm for optimal coat wearing