#P15847. [NOISG 2026 Finals] Monkeys
[NOISG 2026 Finals] Monkeys
Problem Description
Monkeyland is an infinite number line with monkeys, numbered from 1 to . The -th monkey is initially at position on the number line. It is possible that multiple monkeys share the same initial position.
Pan can make every monkey move with his enchanting spell! How each monkey moves is determined by a string of length where each character is either L or R. Let the -th character of be .
Once a spell is cast, the -th monkey moves as follows:
- If , it moves to the left by one position.
- If , it moves to the right by one position.
Each day, Pan will cast his spell exactly once. If two monkeys are at the same position on any day (even initially), they become friends. If Pan were to cast his spell for days, determine the number of pairs of monkeys that will become friends.
Input Format
Your program must read from standard input.
The first line of input contains two space-separated integers and .
The second line of input contains space-separated integers .
The third line of input contains a string of characters .
Output Format
Your program must print to standard output.
Output one integer, the number of pairs of monkeys that will become friends.
The output should contain only a single integer. Do not print any additional text such as Enter a number or The answer is.
2 1
1 3
RL
1
5 67
1 2 3 4 5
RRRRR
0
6 7
1 1 8 16 18 22
RRLRLL
3
10 30
9 46 27 8 12 100 56 96 6 7
LRLRRLRRLR
5
4 2
3 4 4 6
LLRL
2
Hint
Sample Test Case 1 Explanation
There are monkeys and Pan casts the spell for day only.
On the first day, monkey 1 moves right from position 1 to position 2 while monkey 2 moves left from position 3 to position 2. Since they end up at the same position on the first day, they become friends. Hence, there is exactly 1 pair of monkeys that become friends.
Sample Test Case 2 Explanation
There are monkeys and Pan casts the spell for consecutive days.
Since all monkeys have distinct initial positions and each monkey moves one position to the right each day when a spell is cast, no two monkeys will be at the same position on any day. Hence, no pairs of monkeys can become friends.
Sample Test Case 5 Explanation
There are monkeys and Pan casts his spell for consecutive days.
Each figure below depicts Monkeyland as a number line showing positions from 1 to 6 only. The arrow above each monkey indicates the direction in which it will move after a spell is cast.
On the 0-th day, the initial positions of all monkeys are shown in the figure below. Monkeys 2 and 3 which are already at position 4 become friends.
:::align{center}
:::
After the spell has been cast on the 1-st day, the positions of all monkeys are shown in the figure below. Monkeys 3 and 4 meet at position and become friends.
:::align{center}
:::
After the spell has been cast on the 2-nd day, the positions of all monkeys are shown in the figure below. No two monkeys meet on this day.
:::align{center}
:::
Therefore, there are a total of pairs of monkeys that are friends: Monkeys and , as well as monkeys and .
Subtasks
For all test cases, the input will satisfy the following bounds:
- for all
- is either L or R for all
Your program will be tested on input instances that satisfy the following restrictions:
| Subtask | Score | Additional constraints |
|---|---|---|
| 0 | Sample test cases | |
| 1 | 6 | |
| 2 | 13 | |
| 3 | 10 | |
| 4 | 22 | |
| 5 | 18 | |
| 6 | 31 | No additional constraints |