University of Waterloo Logo and CEMC Banner

2025 Canadian Computing Competition
Junior Problems

Problem J1: Roller Coaster Ride

Problem Description

You are spending the day at the CEMC’s funfair. One of the rides at the funfair is a roller coaster which has one train with a number of cars. Each car holds the same number of people.

When you arrive at the roller coaster, you see that there is a line. Your job is to determine whether or not you will be on the next train ride, assuming that every car is fully occupied for every ride.

Input Specification

The first line of input contains a positive integer, \(N\), representing your place in line. For example, if \(N\) is \(5\) then you are the fifth person in line.

The second line contains a positive integer, \(C\), representing the number of cars the train has.

The third line contains a positive integer, \(P\), representing the number of people a single car holds.

Output Specification

Output either yes or no, indicating whether or not you will be on the next train ride.

Sample Input 1

14
3
2

Output for Sample Input 1

no

Explanation of Output for Sample Input 1

The train has \(3\) cars and each car holds \(2\) people. Therefore, \(6\) people can ride the next train. Since you are the 14th person in line, you will not be on the next train ride.

Sample Input 2

12 
4 
3

Output for Sample Input 2

yes

Explanation of Output for Sample Input 2

The train has \(4\) cars and each car holds \(3\) people. Therefore, \(12\) people can ride the next train. Since you are the 12th person in line, you will be on the next train ride.

Problem J2: Donut Shop

Problem Description

The owner of a donut shop spends the day baking and selling donuts.

Given the events that happen over the course of the day, your job is to determine the number of donuts remaining when the shop closes.

Input Specification

The first line of input contains a non-negative integer, \(D\), representing the number of donuts available when the shop first opens.

The second line contains a positive integer, \(E\), representing the number of events that happen over the course of the day. The next \(E\) pairs of input lines describe these events.

The first line in the pair contains either the + (plus) symbol, indicating that donuts have been baked, or the - (minus) symbol, indicating that donuts have been sold. The second line in the pair contains a positive integer, \(Q\), representing the quantity of donuts associated with the event.

For each sale of donuts, the value of \(Q\) will be less than or equal to the number of donuts available at that time.

Output Specification

Output the non-negative integer, \(R\), which is the number of donuts remaining when the shop closes.

Sample Input

10
3
+
24
-
6
-

Output for Sample Input

16

Explanation of Output for Sample Input

The shop opened with \(10\) donuts and there were \(3\) events during the day. The owner first baked \(24\) donuts. Then the owner sold \(6\) donuts, followed by another \(12\). The number of donuts remaining is \(10+24-6-12=16\).

Problem J3: Product Codes

Problem Description

A store has hired the Code Cleaning Crew to help it update all of its product codes.

The original product codes are sequences of letters, positive integers, and negative integers. For example, cG23mH-9s is a product code that contains two uppercase letters, three lowercase letters, one positive integer, and one negative integer.

The new product codes are made by removing all lowercase letters, keeping all uppercase letters in order, and adding all the integers to form one new integer which is placed at the end of the code. For example, the new product code for cG23mH-9s is GH14.

Your job is to take a list of original product codes and determine the new product codes.

Input Specification

The first line of input contains a positive integer, \(N\), representing the number of original product codes that need to be updated. The following \(N\) lines each contain one original product code.

Each original product code contains at least one uppercase letter, at least one lowercase letter, and at least one integer. Also, a positive integer never immediately follows another integer. This means, for example, that 23 is the integer \(23\) instead of the integer \(2\) followed by the integer \(3\).

The following table shows how the available \(15\) marks are distributed:

Marks Description
\(2\) All the integers are positive and single-digit.
\(2\) All the integers are single-digit.
\(7\) Any positive integer may be multi-digit.
\(4\) Any integer may be multi-digit.

Output Specification

Output the \(N\) new product codes, one per line.

Sample Input 1

1
AbC3c2Cd9

Output for Sample Input 1

ACC14

Explanation of Output for Sample Input 1

For the single original product code, the uppercase letters A, C, and C are kept in order and the sum of the integers is \(3+2+9=14\).

Sample Input 2

3
Ahkiy-6ebvXCV1
393hhhUHkbs5gh6QpS-9-8
PL12N-2G1234Duytrty8-86tyaYySsDdEe

Output for Sample Input 2

AXCV-5
UHQS387
PLNGDYSDE1166

Explanation of Output for Sample Input 2

For the first original product code, the uppercase letters A, X, C, and V are kept in order and the sum of the integers is \(-6+1=-5\).

For the second and third original product codes, their uppercase letters are also kept in order and the sums of the integers are \(393+5+6-9-8=387\) and \(12-2+1234+8-86=1166\) respectively.

Problem J4: Sunny Days

Problem Description

There is a large amount of historical weather data for CEMCity. Each day in the data is listed as either a day with sunshine or a day with precipitation. Jeremy is interested in finding the record for the most consecutive days with sunshine. Unfortunately, the data is incorrect for exactly one day, but Jeremy doesn’t know which day this is.

Your job is to help Jeremy determine the maximum possible number of consecutive days with sunshine.

Input Specification

The first line of input contains a positive integer, \(N\), representing the number of days in the historical data. The following \(N\) lines each contain either the character S or the character P, representing a day with sunshine or a day with precipitation, respectively, in chronological order.

The following table shows how the available \(15\) marks are distributed:

Marks Description Bounds
\(2\) There are not many days in the historical data. The data consists of a single block of all S’s followed by a single block of all P’s. One of these blocks may be empty. \(N \leq 1000\)
\(4\) There are not many days in the historical data. The data contains S’s and P’s possibly in mixed order. \(N \leq 1000\)
\(9\) There are possibly many days in the historical data. \(N \leq 500\,000\)

Output Specification

Output the non-negative integer, \(M\), which is the maximum possible number of consecutive days with sunshine.

Sample Input

8
P
S
P
S
S
P
P
S

Output for Sample Input

4

Explanation of Output for Sample Input

If the data is incorrect for the third day, then there was sunshine from the second day to the fifth day which is four consecutive days with sunshine. This is the maximum possible number of consecutive days with sunshine. That is, no matter which day the data is incorrect for, there were not five (or more) consecutive days of sunshine.

Problem J5: Connecting Territories

Problem Description

Ava is playing a strategic game on a grid of tiles. Each tile has an associated cost. When the costs of the tiles are read from left to right, starting with the first row, a repeating pattern of the first \(M\) positive integers in increasing order is revealed: \(1, 2, 3, \ldots, M, 1, 2, 3, \ldots, M, 1, 2, 3, \ldots\). In this pattern, \(M\) represents the maximum cost of a tile. In the grid of tiles shown, \(M\) is equal to \(5\).

A grid with 5 rows and 8 columns. From left to right, the entries in the eight squares in the first row are 1, 2, 3, 4, 5, 1, 2, 3. The entries in the second row continue with 4, 5, 1, 2, 3, and so on.

Ava needs to purchase one tile in each row in order to make a connecting path from the upper territory (above the first row of tiles) to the lower territory (below the last row of tiles). The first tile purchased must be in the first row. Each subsequently purchased tile must share either an edge or a corner with the tile purchased previously. In the grid of tiles shown, the cost of Ava’s connecting path is \(9\).

One square in each of the five rows in the grid is highlighted: 6th square in the 1st row, 5th square in the 2nd row, 5th square in the 3rd row, 4th square in the 4th row, and 4th square in the 5th row. From top to bottom, the entries in the highlighted squares are 1, 3, 1, 3, and 1.

Given a grid of tiles, your job is to determine the minimum cost of a connecting path between the upper and lower territories.

Input Specification

The first line of input contains a positive integer, \(R\), representing the number of rows in the grid. The second line contains a positive integer, \(C\), representing the number of columns in the grid. The third line contains a positive integer, \(M\) where \(M \leq 100\,000\), representing the maximum cost of a tile.

The following table shows how the available \(15\) marks are distributed:

Marks Description Bounds
\(3\) There are two rows and at most ten columns. \(R=2\) and \(C \leq 10\)
\(8\) There are at most ten rows and at most ten columns. \(R \leq 10\) and \(C \leq 10\)
\(2\) There are at most \(100\) rows and at most \(100\) columns. \(R \leq 100\) and \(C \leq 100\)
\(2\) The grid may be very large. \(R \leq 20\,000\) and \(C \leq 20\,000\)

Output Specification

Output the positive integer, \(P\), which is the minimum cost of a connecting path between the upper and lower territories.

Sample Input

3
5
7

Output for Sample Input

6

Explanation of Output for Sample Input

The cost of each tile is shown. The sequence of tiles that Ava should purchase to minimize the cost of a connecting path is highlighted in green.

A grid with 3 rows and 5 columns. A square in each row is highlighted: 3rd square in the 1st row, 4th square in the 2nd row, and 5th square in the 3rd row. From top to bottom, the entries in the highlighted squares are 3, 2, and 1.