Tanks a Lot

Time limit1sMemory limit128 MB

Summary
Given gas stations on a circular track whose fuel exactly covers one lap, list every station and direction from which a full lap can be completed.
Level

Medium6 of 10

Topics
Prefix sum, Greedy, Simulation
Solved
No attempts yet

Problem

Imagine a car with a fuel tank large enough to hold any amount of gas you might need. The car travels along a circular route that has several gas stations on it. The total amount of gas across all stations is exactly enough to drive around the circuit once. Whenever you reach a station, you pour all of that station's gas into your tank.

Starting with an empty tank, it can be shown that there is always at least one station and one direction (clockwise or counterclockwise) from which you can complete a full lap and return to where you started.

Given the length of the circuit, the locations of the stations, and how many miles each station's gas lets you drive, determine every station and direction from which a full lap can be completed.

Input

The input contains a sequence of test cases. Each test case begins with a line containing two positive integers cc and ss: the total circumference of the circle in miles and the number of gas stations.

Next come ss pairs of integers tt and mm describing the stations. In each pair, tt (with 0≤t≤c−10 \le t \le c-1) is the clockwise distance of the station from a fixed reference point on the circle, and mm is the number of miles you can drive using all of that station's gas. All station locations are distinct, and c≤100000c \le 100000.

Within a test case, the sum of all mm values equals cc.

The list of test cases ends with a line containing two zeros.

Output

For each test case, print Case X:, where XX is the test case number starting from 1. Then, for every station from which a full lap can be completed, append the pair i d, where ii is the station's location and dd is:

  • C if the lap can be completed only in the clockwise direction,
  • CC if it can be completed only in the counterclockwise direction,
  • CCC if it can be completed in either direction.

List the qualifying stations in order of increasing location, separated by spaces.

Examples1

  1. Example 1

    Input
    10 4
    2 3 4 3 6 1 9 3
    5 5
    0 1 4 1 2 1 3 1 1 1
    0 0
    
    Expected output
    Case 1: 2 C 4 CC 9 C
    Case 2: 0 CCC 1 CCC 2 CCC 3 CCC 4 CCC