Under construction

Still being built. Things may change

Unterwave Distance

Time limit5sMemory limit1024 MB

Summary
Find the minimum gap between the cubed gravities of an alien and a human system, where one optional device shifts gravity in a neighborhood.
Level

Medium6 of 10

Topics
Math, Sorting, Implementation
Solved
No attempts yet

Problem

The year is 2312, and humanity's rush to the stars is now 200 years old. Real-time communication between star systems is still an open problem, and the answer is the unterwave.

Unterwave (UW) communication works as follows. Direct UW links exist between certain pairs of star systems. Two systems without a direct link communicate by hopping along existing links through intermediate systems. No system has more than 20 direct links, but any two systems can communicate, directly or indirectly.

Your company must choose two star systems for its new home offices: one in a human system and one in an alien system. Choose them so that the UW distance, a measure of communication latency, is minimized.

Every star system has a gravity value that affects UW communication. No two star systems have the same gravity. Suppose a communication path passes through the sequence G=g1,g2,…,gnG = g_1, g_2, \ldots, g_n of gravity values, including its source and destination. Then three sequences of n−1n-1 terms are defined.

cap⁡(G)=g2+g1, g3+g2, …, gn+gn−1(Capacitance)\operatorname{cap}(G) = g_2+g_1,\ g_3+g_2,\ \ldots,\ g_n+g_{n-1} \quad \text{(Capacitance)}

pot⁡(G)=g2−g1, g3−g2, …, gn−gn−1(Potential)\operatorname{pot}(G) = g_2-g_1,\ g_3-g_2,\ \ldots,\ g_n-g_{n-1} \quad \text{(Potential)}

ind⁡(G)=g2⋅g1, g3⋅g2, …, gn⋅gn−1(Inductance)\operatorname{ind}(G) = g_2\cdot g_1,\ g_3\cdot g_2,\ \ldots,\ g_n\cdot g_{n-1} \quad \text{(Inductance)}

For sequences A=a1,…,amA=a_1, \ldots, a_m and B=b1,…,bmB=b_1, \ldots, b_m, A⋅BA\cdot B and A−BA-B denote the termwise product and difference.

The UW distance is the absolute value of the sum of the terms of

pot⁡(G)⋅([cap⁡(G)⋅cap⁡(G)]−ind⁡(G))\operatorname{pot}(G)\cdot\left(\left[\operatorname{cap}(G)\cdot\operatorname{cap}(G)\right]-\operatorname{ind}(G)\right)

The UW distance can be quite large. Fortunately, your company has a gravity dispersal device. It may be placed in any single system, where it stays. It reduces that system's gravity by 1 and increases by 1 the gravity of every system directly linked to it. Using the device is optional.

Input

The first line has one integer nn (2≤n≤50 0002 \leq n \leq 50\,000), the number of star systems. The next nn lines describe systems 1 to nn in the form g d. Here gg is the gravity value (1≤g≤1 000 0001 \leq g \leq 1\,000\,000), and dd is h for a human system or a for an alien system. There is at least one system of each type, and all values of gg are distinct.

The next line has one positive integer ee, the number of direct links. Each of the next ee lines contains two system numbers separated by a space, meaning those systems are directly linked. Links are bidirectional, and no link appears more than once. No system links directly to more than 20 other systems.

Output

Output the minimum UW distance that can be achieved between an alien system and a human system.

Examples2

  1. Example 1

    Input
    9
    377 a
    455 h
    180 a
    211 a
    134 a
    46 h
    111 h
    213 h
    17 a
    15
    1 2
    1 4
    1 6
    2 3
    2 4
    2 5
    3 5
    4 6
    4 7
    4 9
    5 7
    5 8
    6 9
    7 9
    7 8
    
    Expected output
    85293
    
  2. Example 2

    Input
    3
    20 h
    21 h
    19 a
    3
    1 2
    2 3
    3 1
    
    Expected output
    0