Solved ABCD of Codeforces 276.