Roulette Score (APCS 2025-06 Intermediate)

Points 100 1.0s 256M

You have \(m\) roulettes. Each roulette contains \(n\) positions, and every position is labeled with one lowercase English letter. Each input string of length \(n\) lists the currently visible letters of one roulette from top to bottom.

The game has \(k\) rounds. In every round, each roulette is rotated by a given integer distance:

  • A positive distance \(d\) rotates the roulette clockwise by \(d\) positions, which is a cyclic right shift of its string by \(d\) positions.
  • A negative distance \(d\) rotates the roulette counterclockwise by \(-d\) positions, which is a cyclic left shift by \(-d\) positions.
  • A distance of 0 leaves the roulette unchanged.

For example, for the length-6 roulette apcsie:

  • A clockwise rotation by 1 produces eapcsi.
  • A counterclockwise rotation by 2 produces csieap.

A rotation distance may exceed one full turn and must be interpreted cyclically. More importantly, each round starts from the state at the end of the previous round. The roulettes are not reset to their initial strings.

After all roulettes have been rotated in one round, score each position separately. At a position, inspect the \(m\) aligned letters and find the largest number of occurrences of any one letter. This number is the score of that position. The score of the round is the sum of the scores of all \(n\) positions.

Output the sum of the scores over all \(k\) rounds.

Input

The first line contains three positive integers \(m\), \(n\), and \(k\): the number of roulettes, the number of positions on each roulette, and the number of rounds.

Each of the next \(m\) lines contains a string of exactly \(n\) lowercase English letters, describing the initial state of one roulette.

Each of the next \(k\) lines contains \(m\) integers. The \(j\)-th integer on the \(r\)-th such line is the rotation distance of the \(j\)-th roulette in round \(r\).

Constraints

  • \(1\le m,n,k\le30\)
  • Every roulette string contains exactly \(n\) letters from a to z
  • \(-100\le d\le100\) for every rotation distance \(d\)

Output

Output one integer: the total score over all \(k\) rounds.

Scoring

Subtask Score Additional constraints
1 60 \(m=3\)
2 40 No additional constraints

Sample Input 1

3 6 2
apcsie
taiwan
icpeda
1 0 -4
7 -3 2

Sample Output 1

17

Sample Explanation 1

Round 1 starts from the three initial strings:

  1. Rotate apcsie clockwise by 1 to obtain eapcsi.
  2. Leave taiwan unchanged.
  3. Rotate icpeda counterclockwise by 4 to obtain daicpe.

The six positions are scored as follows:

Position Aligned letters Maximum frequency
0 e, t, d 1
1 a, a, a 3
2 p, i, i 2
3 c, w, c 2
4 s, a, p 1
5 i, n, e 1

The score of round 1 is \(1+3+2+2+1+1=10\).

Round 2 must continue from the strings at the end of round 1:

  1. Rotate eapcsi clockwise by 7. Since \(7\bmod6=1\), it becomes ieapcs.
  2. Rotate taiwan counterclockwise by 3 to obtain wantai.
  3. Rotate daicpe clockwise by 2 to obtain pedaic.
Position Aligned letters Maximum frequency
0 i, w, p 1
1 e, a, e 2
2 a, n, d 1
3 p, t, a 1
4 c, a, i 1
5 s, i, c 1

The score of round 2 is \(1+2+1+1+1+1=7\), so the total is \(10+7=17\).

Sample Input 2

4 3 3
abc
bab
cbc
abc
-1 -6 -6 -7
5 -3 4 0
-7 4 2 8

Sample Output 2

21

Sample Explanation 2

The states and scores after each round are shown below. Every row continues from the state in the previous row.

Round Roulette states after rotation Position scores Round score
1 bca, bab, cbc, bca 3, 2, 2 7
2 cab, bab, ccb, bca 2, 2, 3 7
3 abc, bba, cbc, cab 2, 3, 2 7

Therefore, the total score is \(7+7+7=21\).

Source

APCS June 2025, Programming Task 2; ZeroJudge q837, Roulette Score; explanation by Yi-Che Wang.

Problem page help

Keyboard shortcuts

Main features

  • Sample tests — Runs the sample cases bundled with the problem and auto-compares against the expected output.
  • Custom test — Run your code with your own stdin. Optionally tick the "Compare with expected (diff)" box to verify against expected output line-by-line.
  • Template — Paste the default code template you set on your profile page.
  • Collab — Edit this problem together with classmates in real time.
  • Auto-draft — Editor contents auto-save to your browser every 1.5 seconds (per account / problem / language).
  • Submit — Send your code to the judge for grading; returns AC / WA / TLE etc.

Limits

  • Source code: at most 65,536 characters
  • Custom test stdin and expected output: at most 1 MB each (≈1 million characters)
  • Custom test and sample test share the sandbox; about 1 request per 3 s per user (sample test: 1 per 1 s)
  • Custom test and sample test both have a 15 second wall-clock cap (the official judge still uses the problem time limit)
  • Interactive problems do not offer custom test (cannot simulate interaction with the judge).
  • Submitting has no rate limit, but rapid repeated submissions on the same problem are treated as score farming.