This problem is in Contest(s) below:
Contest 1510:ACM挑战赛热身赛
Contest 1536:第十届程序设计大赛赛前模拟