codeforces; solve "Domino Piling" from Beta Round #47