#P3686. [CERC2016] 爵士之旅 Jazz Journey

[CERC2016] 爵士之旅 Jazz Journey

Description

Ivan is planning a grand European tour for his jazz band. There are nn cities in Europe, labeled 1n1 \sim n. Ivan plans to hold dd concerts in cities a1,a2,,ada_1, a_2, \dots, a_d in this exact order, and he will never perform in the same city twice in a row (that is, aiai+1a_i \ne a_{i+1}). However, over the whole tour, he may visit the same city multiple times. In the end, he will return to the starting city to perform (that is, a1=ada_1 = a_d).

Each time, Ivan takes a direct flight from aia_i to ai+1a_{i+1}. He wants to be smart and minimize the total cost of tickets. As you know, prices depend on supply and demand: for example, a one-way ticket might even be more expensive than a round-trip ticket for the same pair of cities.

There are two types of tickets available:

  1. A one-way ticket from aa to bb, which allows exactly one flight from aa to bb, but not from bb to aa.

  2. A round-trip ticket from aa to bb: buying one allows one flight from aa to bb, and then one flight from bb back to aa, but you are not allowed to use it in the reverse order. Of course, you may choose not to fly back after going from aa to bb.

You are given the set of available tickets, with unlimited supply of each type. Please help Ivan find the minimum possible total cost. You may assume that at least one valid plan exists.

Input Format

The first line contains two positive integers n,dn, d (2n,d3×1052 \le n, d \le 3 \times 10^5), the number of cities and the number of concerts.

The second line contains dd positive integers a1,a2,,ada_1, a_2, \dots, a_d (1ain,aiai+1,a1=ad1 \le a_i \le n, a_i \ne a_{i+1}, a_1 = a_d), indicating the city of each concert in order.

The next line contains a positive integer mm (3m3×1053 \le m \le 3 \times 10^5), the number of ticket types.

Each of the next mm lines contains two positive integers si,dis_i, d_i (1si,din,sidi1 \le s_i, d_i \le n, s_i \ne d_i), the start and end cities; followed by a character tit_i indicating the ticket type, where "O" means a one-way ticket and "R" means a round-trip ticket; and finally a positive integer pip_i (1pi1091 \le p_i \le 10^9), the ticket price.

Output Format

Output a single integer: the minimum total ticket cost to complete the tour.

2 5
1 2 1 2 1
4
1 2 R 6
1 2 O 3
2 1 O 3
1 2 R 5
10

Hint

Translated by ChatGPT 5