Solve A and B tasks (round 265, div. 2)