Pistons

시간 제한1초메모리 제한2048 MB

요약
길이 m인 실린더에서 왕복하는 n개 피스톤의 위치 합이 최대가 되는 순간을 구한다.
난이도

보통10점 중 7점

유형
수학, 구현
정답자
아직 제출이 없습니다

문제

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 (1≤n≤1051 \le n \le 10^5, 1≤m≤1061 \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 p_ip\_i (0≤p_i≤m0 \le p\_i \le m), and a character d_id\_i (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.

예제2

  1. 예제 1

    입력
    2 5
    2 U
    5 D
    
    예상 출력
    7
    
  2. 예제 2

    입력
    4 6
    0 U
    0 D
    6 U
    3 U
    
    예상 출력
    15