#P10951. 最优高铁环

最优高铁环

Background

Phantom Country has built the most advanced high-speed rail system in the world. Its trains are divided into the following types:

  • SS—high-speed photon-powered train—speed 1000kmh11000\,km\cdot h^{-1}
  • GG—high-speed EMU—speed 500kmh1500\,km\cdot h^{-1}
  • DD—EMU set—speed 300kmh1300\,km\cdot h^{-1}
  • TT—express—speed 200kmh1200\,km\cdot h^{-1}
  • KK—fast—speed 150kmh1150\,km\cdot h^{-1}

A train service number starts with one of the letters above, followed by a positive integer (1000)(\le 1000).

Background

Phantom Country has built the most advanced high-speed rail system in the world. Its trains are divided into the following types:

  • SS—high-speed photon-powered train—speed 1000kmh11000\,km\cdot h^{-1}
  • GG—high-speed EMU—speed 500kmh1500\,km\cdot h^{-1}
  • DD—EMU set—speed 300kmh1300\,km\cdot h^{-1}
  • TT—express—speed 200kmh1200\,km\cdot h^{-1}
  • KK—fast—speed 150kmh1150\,km\cdot h^{-1}

A train service number starts with one of the letters above, followed by a positive integer (1000)(\le 1000).

Problem Description

Because the terrain of the country is uneven, the suitable operating speed of railways differs from place to place.

Therefore, each travel route in the country consists of KK train services.

For example, when K=5K=5, one route is: T120D135S1G12K856T120-D135-S1-G12-K856.

When the last service of one route is the same as the first service of another route, these two routes can be connected into a longer travel route.

Obviously, connecting several routes may form a cycle.

If there are 33 travel routes as follows:

x1x2x3x_1-x_2-x_3
x3x4x_3-x_4
x4x5x1x_4-x_5-x_1

The speeds of services x1x5x_1 \sim x_5 are v1v5v_1 \sim v_5, respectively.

Define the value of a high-speed rail cycle as the average of (the sum of speeds on each travel route in the cycle), i.e.:

(v1+v2+v3)+(v3+v4)+(v4+v5+v1)3\dfrac{(v_1+v_2+v_3)+(v_3+v_4)+(v_4+v_5+v_1)}3

The maximum value among all high-speed rail cycles is called the value of the optimal high-speed rail cycle.

Given MM travel routes, find the value of the optimal high-speed rail cycle.

Input Format

The first line contains the number of travel routes MM.

The next MM lines each contain one travel route, consisting of several train services separated by -. The numbering rule of train services is as described above.

The input is guaranteed to be valid.

Output Format

Output the value of the optimal high-speed rail cycle, rounded to the nearest integer.

If no such cycle exists, output 1-1.

3
T120-D135-S1
S1-G12
G12-K856-T120
1283

Hint

Sample Explanation

$\dfrac{(200+300+1000)+(1000+500)+(500+150+200)}3=1283$

Constraints

For 50%50\% of the testdata, 0<M10000<M\le1000.

For 100%100\% of the testdata, 0<M500000<M\le50000, and the number of train services in each travel route does not exceed 2020.

The input is large. Please use read and scanf. The testdata does not guarantee that using cin will pass within the time limit.

To avoid precision issues, please use double. The testdata guarantees that the result does not exceed 21474836472147483647.

Translated by ChatGPT 5