Exploding Robot

Time limit: 0.2s Memory limit: 256MB Input: Output:

While Luca was looking through old boxes in his attic, he found its old nokia phone, still working! So he decides to play his favorite game on it: Exploding Robot.

Luca playing the game..\text{Luca playing the game.}.

The main character of the game is a robot, that starts at the top-left corner of a grid with NN rows and MM columns, and moves according to a given sequence of commands, each indicating a step to the right, down, left or up. The robot never leaves the grid. The game forces you to place KK bombs in distinct cells of the grid (but not on the starting cell) before the robot starts moving.

When the robot visits a cell containing a bomb, it explodes and the game ends. You like to watch the robot move, so you want to place the bombs in such a way that allows the robot to make as many moves as possible before it explodes (or he runs out of moves).

Task

What is the maximum number of moves the robot can make, if you place the bombs optimally? Note that the move that makes the robot step on a bomb is not counted.

Input data

The input file consists of:

  • a line containing integers NN, MM and KK, the dimensions of the grid and the number of bombs you must place.
  • a line containing string PP, the list of moves the robot will make.

Output data

The output file must contain a single line consisting of integer ans\mathtt{ans}.

Constraints and clarifications

  • 2N100,2M1002 \le N \le 100, 2 \le M \le 100.
  • 0K<NM0 \le K < N \cdot M.
  • 1P5 0001 \le |P| \le 5 \ 000.
  • PP contains only characters R\text{R}, D\text{D}, L\text{L} and U\text{U}, indicating a step right, down, left or up.
  • The robot never leaves the grid while executing the moves in PP.
# Score Constraints
1 0 Examples
2 25 N4,M4N \le 4, M \le 4
3 40 PP only contains only characters R\text{R} and D\text{D}.
4 35 No additional restrictions

Example 1

stdin

2 2 2
RDL

stdout

1

Explanation

The first sample case is shown in the figure below.

The blue line shows the path of the robot. By placing the bombs like in the figure, the robot can make 11 move before stepping on a bomb.

Example 2

stdin

5 5 2
DDDRD

stdout

5

Explanation

The second sample case is shown in the figure below.

The figure shows a possible placement of the bombs that allows the robot to make 55 moves.

Example 3

stdin

3 4 7
RDDRLUURRDDLUU

stdout

7

Log in or sign up to be able to send submissions!