optimal merge pattern using greedy method