#P15847. [NOISG 2026 Finals] Monkeys

[NOISG 2026 Finals] Monkeys

Problem Description

Monkeyland is an infinite number line with nn monkeys, numbered from 1 to nn. The ii-th monkey is initially at position p[i]p[i] 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 dd of length nn where each character is either L or R. Let the ii-th character of dd be d[i]d[i].

Once a spell is cast, the ii-th monkey moves as follows:

  • If d[i]=Ld[i] = \text{L}, it moves to the left by one position.
  • If d[i]=Rd[i] = \text{R}, 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 kk 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 nn and kk.

The second line of input contains nn space-separated integers p[1],p[2],,p[n]p[1], p[2], \dots, p[n].

The third line of input contains a string dd of nn characters d[1],d[2],,d[n]d[1], d[2], \dots, d[n].

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 n=2n = 2 monkeys and Pan casts the spell for k=1k = 1 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 n=5n = 5 monkeys and Pan casts the spell for k=67k = 67 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 n=4n = 4 monkeys and Pan casts his spell for k=2k = 2 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 55 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 22 pairs of monkeys that are friends: Monkeys 22 and 33, as well as monkeys 33 and 44.

Subtasks

For all test cases, the input will satisfy the following bounds:

  • 1n2000001 \le n \le 200\,000
  • 1k1091 \le k \le 10^9
  • 1p[i]1091 \le p[i] \le 10^9 for all 1in1 \le i \le n
  • d[i]d[i] is either L or R for all 1in1 \le i \le n

Your program will be tested on input instances that satisfy the following restrictions:

Subtask Score Additional constraints
0 Sample test cases
1 6 n=2n = 2
2 13 d[1]=d[2]==d[n]d[1] = d[2] = \cdots = d[n]
3 10 n,k200n, k \le 200
4 22 n,k3000n, k \le 3000
5 18 n3000n \le 3000
6 31 No additional constraints