#P9691. [GDCPC 2023] Base Station Construction

[GDCPC 2023] Base Station Construction

题目描述

China Mobile Shenzhen Branch was registered in 19991999. Four years later, Guangdong Collegiate Programming Contest was held for the first time. China Mobile Shenzhen Branch, along with Guangdong Collegiate Programming Contest, witnesses the prosperity and development of the computer industry in Guangdong.

During the construction of a communication line, it is critical to carefully choose the locations for base stations. The distance from west to east of a city is nn kilometers. The engineers have investigated the cost to build a base station at 1,2,,n1, 2, \cdots, n kilometers from west to east, which are a1,a2,,ana_1, a_2, \cdots, a_n respectively.

To ensure communication quality for the residents, the locations of base stations also need to meet mm requirements. The ii-th requirement can be represented as a pair of integers lil_i and rir_i (1lirin1 \le l_i \le r_i \le n), indicating that there must be at least 11 base station between lil_i kilometers and rir_i kilometers (both inclusive) from west to east.

As the chief engineer, you need to decide the number of base stations to build and their locations, and finally calculate the minimum total cost to satisfy all requirements.

输入格式

There are multiple test cases. The first line of the input contains an integer TT indicating the number of test cases. For each test case:

The first line contains an integer nn (1n5×1051 \le n \le 5 \times 10^5) indicating the distance from west to east of the city.

The second line contains nn integers a1,a2,,ana_1, a_2, \cdots, a_n (1ai1091 \le a_i \le 10^9) where aia_i indicates the cost to build a base station at ii kilometers from west to east.

The third line contains an integer mm (1m5×1051 \le m \le 5 \times 10^5) indicating the number of requirements.

For the following mm lines, the ii-th line contains two integers lil_i and rir_i (1lirin1 \le l_i \le r_i \le n) indicating that there must be at least 11 base station between lil_i kilometers and rir_i kilometers (both inclusive) from west to east.

It's guaranteed that neither the sum of nn nor the sum of mm of all test cases will exceed 5×1055 \times 10^5.

输出格式

For each test case output one line containing one integer indicating the minimum total cost to satisfy all requirements.

2
5
3 2 4 1 100
3
1 3
2 4
5 5
5
7 3 4 2 2
3
1 4
2 3
4 5
102
5

提示

For the first sample test case the optimal solution is to build base stations at 22 kilometers and 55 kilometers from west to east. The total cost is 2+100=1022 + 100 = 102.

For the second sample test case the optimal solution is to build base stations at 22 kilometers and 44 kilometers from west to east. The total cost is 3+2=53 + 2 = 5.