Ants on a Log

medium stack simulation arrays 1000 ms · 256 MB

Ants on a Log

A fallen log lies across the forest floor, and n ants are standing on it at n different points. They are numbered 1 to n from the left end of the log to the right end, so ant i stands strictly to the left of ant i + 1. Ant i has size s_i and faces either left or right.

At the same moment every ant starts walking in the direction it faces. All ants walk at exactly the same constant speed, and an ant never turns around or stops on its own. The log is long enough that no ant walks off either end while another ant could still reach it.

Two ants walking in the same direction keep the same distance forever, so they never meet. When two ants walking towards each other meet, they fight:

  • if one is strictly larger than the other, the smaller ant is eaten, and the larger one keeps walking in its direction at the same speed, with its size unchanged;
  • if they have the same size, both are eaten.

An ant can meet ants again and again, as long as it survives. It can be shown that which ants are eaten does not depend on the exact distances between the starting points, only on their order.

Which ants are still alive once no more fights can happen?

Input

The first line contains an integer n (1 ≤ n ≤ 2·10^5), the number of ants.

The second line contains n integers s_1, s_2, …, s_n (1 ≤ s_i ≤ 10^9), the sizes of the ants from left to right.

The third line contains a string of exactly n characters, each L or R. Its i-th character is L if ant i walks to the left and R if it walks to the right.

Output

On the first line print k, the number of ants that are never eaten.

On the second line print the numbers of those k ants in increasing order, separated by spaces. If k = 0, print an empty second line (or nothing).

Notes

In the first example, ant 3 (size 4, walking left) first meets ant 2 (size 2) and eats it, then meets ant 1 (size 5) and is eaten. Ant 4 walks right, away from everyone, and ant 5 meets ant 6: both have size 3, so both are eaten. Ants 1 and 4 survive.

In the second example, every ant walking left is to the left of every ant walking right, so no two ants ever meet.

Example 1

Input

6
5 2 4 1 3 3
RRLRRL

Output

2
1 4

Example 2

Input

5
3 1 4 1 5
LLRRR

Output

5
1 2 3 4 5

Hints · reveal one at a time if you're stuck

Hint 1

Ants moving in the same direction never meet, so only a right-walker with a left-walker to its right can fight. Since distances don't matter, scan the ants from left to right: what do you need to remember about right-walkers that are still alive?

Hint 2

Keep a stack of surviving right-walkers, with the nearest on top. When a left-walker arrives, it fights the top one, and the outcome depends only on the two sizes. A left-walker can keep eating in a row.

Hint 3

Handle the cases carefully: a smaller top is popped and the left-walker goes on, an equal top means both die, and a larger top kills the left-walker. A left-walker that survives everything, and every right-walker still on the stack, are the survivors, so output their numbers in order.

report an issue Prefer your own editor? sooks prep 49 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.