#P8641. Ticket Prize Circle Game
Ticket Prize Circle Game
Ticket Prize Circle Game
An organization is holding a ticket prize game. You have a chance to win a number of tickets equal to the sum of the numbers on the cards you collect.
You are given a set of N cards arranged in a circle in clockwise order. Each card is labeled with a unique integer from \(1\) to \(N\). Before the game starts the host shuffles the cards and arranges them in a circle.
You can choose any card as your starting point. Then, moving clockwise, you count consecutively starting from \(1, 2, 3, \dots\). If the number you count equals the number written on the card you are at, you collect (remove) that card and add its number to your prize (i.e. the number of tickets won). Immediately after a removal, the counting is restarted at \(1\) beginning with the next card in clockwise order (the circle shrinks by one). Otherwise, if the count does not match the card's number, you simply move on to the next card (still in clockwise order) and the count increases by one.
The game continues until no more cards can be collected. A key observation is that since every card's value lies between \(1\) and \(N\), if the current count becomes greater than the maximum value among the remaining cards, no further card can ever be collected. Your task is: given the clockwise arrangement of the cards, determine the maximum number of tickets (i.e. the maximum sum of collected card numbers) you can win by choosing an optimal starting card.
Example 1:
Input: 3 1 2 3</p>Starting from the card with number 1, you immediately collect it (since 1 = 1) and then the counting resets. However, in the remaining circle you will never get a match. Thus, the prize sum is 1.
Example 2:
Input: 3 2 1 3</p>With a lucky choice of starting point, it is possible to collect all cards: 2 + 1 + 3 = 6.
inputFormat
The input consists of two lines:
- The first line contains a single integer \(N\) denoting the number of cards.
- The second line contains \(N\) space-separated integers representing the clockwise arrangement of the cards.
outputFormat
Output a single integer, the maximum sum of the card numbers (i.e. the number of tickets won) that can be obtained by choosing an optimal starting card.
sample
3
1 2 3
1