PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GD0000182. Ếch vượt sông (Dynamic Frog)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Ếch vượt sông (Dynamic Frog)

Dynamic Frog

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

Fred the frog starts on the left bank of a river of width DD meters. There are NN stones at distinct positions between the banks, listed in increasing order. A big stone (B) can be used repeatedly. A small stone (S) sinks immediately after Fred lands on it, and can be used at most once during the entire round trip. Fred must travel from the left bank to the right bank and back, without touching the water. He may skip stones. Minimize the maximum distance of any single jump in the whole journey.

Input

Line 1 contains two integers NN and DD: the number of stones and the distance between the banks in meters.

Line 2 contains exactly NN stone descriptions in increasing order of position (empty when N=0N=0). Each description has the form B-M or S-M, where the letter indicates stone type and MM is its integer distance from the left bank. Positions are distinct and lie strictly between the banks.

Output

Print one integer: the minimum possible maximum jump length over the complete round trip.

Subtasks

  • Subtask 1 (20 points): 0≤N≤80\le N\le 8; N+1≤D≤30N+1\le D\le 30; 0<M1<⋯<MN<D0<M_1<\cdots<M_N<D (when N>0N>0); every stone is either B or S.
  • Subtask 2 (30 points): 0≤N≤500\le N\le 50; N+1≤D≤100000N+1\le D\le 100000; 0<M1<⋯<MN<D0<M_1<\cdots<M_N<D (when N>0N>0); every stone is either B or S.
  • Subtask 3 (50 points): 0≤N≤1000\le N\le 100; N+1≤D≤1000000000N+1\le D\le 1000000000; 0<M1<⋯<MN<D0<M_1<\cdots<M_N<D (when N>0N>0); every stone is either B or S.

Examples

Example 1

Input

2 10
B-3 S-6

Output

7

Explanation

The banks are at 00 and 1010, with a big stone at 33 and a small stone at 66. On the outward trip Fred can jump 0→3→6→100\to3\to6\to10, of lengths 3,3,43,3,4. The small stone sinks. The return trip must jump 10→3→010\to3\to0, including a jump of length 77. If the small stone is reserved for the return trip, the outward jump 3→103\to10 is instead 77. Thus 77 is optimal.

Example 2

Input

1 10
S-5

Output

10

Explanation

There is only one small stone at the midpoint. It can be used on at most one leg. The other leg requires a direct jump between the banks of length 1010, which is therefore unavoidable and optimal.