AlgoLensArena
ContestlərProblemlərOtaqlar
Vizualizator
← Məsələlər

DAPC23-097.Monorail

Medium · 2 s · 256 MB

  • icpc
  • dapc
  • 2023

You were just about to go on a nice long holiday to the south, but as always, the trains are delayed. This time, a cargo train derailed in the Gotthard Base Tunnel through the Alps, completely taking one of the two tubes out of service for several months. Luckily, after some initial repairs, the other tube is in service again for cargo traffic.

Since it is now only a one-track connection, multiple trains in the same direction can closely follow each other, but trains in opposite directions can not pass each other. This also means that trains going north can only enter once all trains going south have exited the tunnel, and vice versa.

Today, there are nnn cargo trains that want to drive through the tunnel. Each train arrives at one of the ends at a given time, and takes exactly ddd minutes to drive through the tunnel at a constant speed.

Even though this is not your responsibility, you decide to make a schedule for today's trains. You will decide for each train how long is has to wait at its entrance portal before it can enter the tunnel. Your goal is to minimize the sum of waiting times at the entrance portals over all trains.

For simplicity, you assume that trains are short compared to the length of the tunnel and can be approximated by points travelling over a line segment.

Giriş formatı

The input consists of:

• One line with an integer nnn and ddd (1≤n≤5001\leq n\leq 5001≤n≤500, 1≤d≤1091 \leq d \leq 10^91≤d≤109), the number of cargo trains and the duration that each train needs to drive through the tunnel, in minutes.

• nnn lines, each containing a character sss and an integer ttt (s∈{‘N’,‘S’}s \in \{\text{`\texttt{N}'},\text{`\texttt{S}'}\}s∈{‘N’,‘S’}, 0≤t≤1090 \leq t \leq 10^90≤t≤109), indicating whether this train starts at the north or south portal, and the number of minutes after the start time at which this train arrives.

It is guaranteed that trains are sorted by arrival time. For trains with equal arrival time, the trains coming from the north are listed before the trains coming from the south.

Çıxış formatı

Output the minimal sum of waiting times at the tunnel entrance portals over all trains, in minutes.

Qeyd

Source: Delft Algorithm Programming Contest 2023 · Author: Ragnar Groot Koerkamp · License: CC BY-SA 4.0 · Original: https://dapc.wisv.ch · Statement adapted for AlgoLens and shared under the same license. · 70 of the 190 original test cases (the largest) are omitted here to keep the problem lightweight.

Nümunələr

Giriş #1

3 5
N 0
S 4
N 8

Çıxış #1

3

Giriş #2

4 10
N 5
N 10
S 10
N 15

Çıxış #2

15

Giriş #3

4 10
S 0
N 10
N 10
S 20

Çıxış #3

0

Giriş #4

4 10
N 0
S 5
S 5
S 5

Çıxış #4

15