Train Scheduling

시간 제한2초메모리 제한1024 MB

요약
두 역을 잇는 단일 선로에서 마주 오는 열차가 겹치지 않도록 N개 열차의 출발 시각을 미루어 총 지연 시간을 최소화한다.
난이도

어려움10점 중 8점

유형
동적 계획법, 정렬, 그리디
정답자
아직 제출이 없습니다

문제

Bessie has taken on a new job as a train dispatcher! There are two train stations: AA and BB. Due to budget constraints, there is only a single track connecting the stations. If a train departs a station at time tt, then it will arrive at the other station at time t+Tt+T (1≤T≤10121\le T\le 10^{12}).

There are NN (1≤N≤50001\le N\le 5000) trains whose departure times need to be scheduled. The iith train must leave station s_is\_i at time t_it\_i or later (s_i∈A,B,0≤t_i≤1012s\_i\in \\{A, B\\}, 0\le t\_i\le 10^{12}). It is not permitted to have trains going in opposite directions along the track at the same time (since they would crash). However, it is permitted to have many trains on the track going in the same direction at the same time (assume trains have negligible size).

Help Bessie schedule the departure times of all trains such that there are no crashes and the total delay is minimized. If train ii is scheduled to leave at time a_i≥t_ia\_i\ge t\_i, the total delay is defined as ∑_i=1N(a_i−t_i)\sum\_{i=1}^N(a\_i-t\_i).

입력

The first line contains NN and TT.

Then NN lines follow, where the iith line contains the station s_is\_i and time t_it\_i corresponding to the iith train.

출력

The minimum possible total delay over all valid schedules.

예제4

  1. 예제 1

    입력
    1 95
    B 63
    
    예상 출력
    0
    
  2. 예제 2

    입력
    4 1
    B 3
    B 2
    A 1
    A 3
    
    예상 출력
    1
    
  3. 예제 3

    입력
    4 10
    A 1
    B 2
    A 3
    A 21
    
    예상 출력
    13
    
  4. 예제 4

    입력
    8 125000000000
    B 17108575619
    B 57117098303
    A 42515717584
    B 26473500855
    A 108514697534
    B 110763448122
    B 117731666682
    A 29117227954
    
    예상 출력
    548047356974