Pistons | 프로그래밍의 벗 PivotOJ
PivotOJ

Pistons

시간 제한: 1000ms메모리 제한: 2048MB출처: ICPC Asia Tehran Regional Contest 2023BOJ 33186

문제

Maryam, a famous mathematician, recently has bought an old vintage car. This car uses a combustion engine to generate the power needed to move the car. Inside the engine, there are nn cylinders of length mm and inside each cylinder, there is a piston constantly moving up and down. All pistons move independently and at the same speed. At any given time, the position of a piston inside a cylinder can be shown with an integer from 00 to mm, which also describes the area of the cylinder occupied by the piston. A piston instantly changes its direction when it reaches the top (position mm) or bottom (position 00) of its cylinder.

Maryam managed to determine the position and direction of all the pistons at a specific time. Now she is curious about the maximum total area occupied by all the pistons. Help Maryam find out this value.

입력

The first line of input contains two integers nn and mm (1n1051 \le n \le 10^5, 1m1061 \le m \le 10^6), describing the number of pistons and the length of cylinders, respectively. Each of the next nn lines describe the position and direction of a single piston. The (i+1)(i + 1)th line of the input contains an integer pip_i (0pim0 \le p_i \le m), and a character did_i (di{d_i \in \{U, D}\}), the initial position of the iith piston and its direction (Up or Down), respectively.

출력

Print a single integer, the maximum total area occupied by all the pistons.

예제

예제 1

입력
2 5
2 U
5 D
출력
7

예제 2

입력
4 6
0 U
0 D
6 U
3 U
출력
15
코드를 제출하려면 로그인하세요.