#P9979. Bessie’s Target Shooting

    ID: 23123 Type: Default 1000ms 256MiB

Bessie’s Target Shooting

Bessie’s Target Shooting

Bessie is a bionic cow on a number line with \(T\) targets located at distinct positions. She starts at position \(0\) and follows a command sequence of length \(C\) consisting of the characters L, F and R:

  • L: move one unit to the left (i.e. \(-1\)).
  • R: move one unit to the right (i.e. \(+1\)).
  • F: fire a shot. If there is a target at Bessie's current position, that target is hit and destroyed (and cannot be hit again).

You are allowed to modify at most one command in the sequence before Bessie begins her journey. When a command is modified, it can be changed to any other command in \(\{L, F, R\}\).

For a given ordering of commands, let \(e_i\) be the effect of the i-th command (movement for L and R, and \(0\) for F). Bessie's position is updated as follows:

[ pos_0 = 0, \quad p_i = p_{i-1} + e_i, \quad 1 \le i \le C. ]

When Bessie fires (command F), she fires from her current position before executing that command. In the modified sequence, if a command is changed, note that:

  • The movement component of that command (if changed from a firing command or vice-versa) alters the positions for all subsequent commands by a constant offset \(d\). For example, if the original command was L (with effect \(-1\)) and is changed to F (effect \(0\)), then \(d = 0 - (-1) = 1\); similarly, if a command is changed from R to L, \(d = -1 - (+1) = -2\).

Your task is to determine the maximum number of targets Bessie can hit by modifying at most one command. A target is hit if at the time Bessie fires she is at the same position where a target exists and that target has not been hit before.

inputFormat

The first line contains two integers \(T\) and \(C\) \( (1 \le T, C \le 10^5)\) representing the number of targets and the length of the command sequence.

The second line contains \(T\) space-separated integers representing the positions of the targets. The positions are distinct and can be any integers.

The third line contains a string of length \(C\) consisting only of the characters L, R and F.

outputFormat

Output a single integer, the maximum number of targets that Bessie can hit by modifying at most one command.

sample

3 5
-1 0 1
FRFLF
2