AtCoder ARC #055 : B - せんべい

まさかABC3問解くのが6人とは。今回Bが難しいのでBから書くことにする。 http://arc055.contest.atcoder.jp/tasks/arc055_b