#P16198. [ROIR 2014 Day 2] Cond 空调

[ROIR 2014 Day 2] Cond 空调

Problem Description

In the “Smart Campus” project, it was decided to install a new-generation air conditioner in every classroom in the school for automatic cooling and ventilation. According to the design, each classroom can have only one air conditioner, and the air conditioner’s power must match the classroom size—the larger the classroom, the higher the required power.

The school’s classrooms are numbered consecutively from 11 to nn. It is known that classroom ii needs an air conditioner with power at least aia_i watts.

The school administration provides mm different air conditioner models for purchase. Each model has a corresponding power and price. Please write a program to compute the minimum total cost to equip all classrooms with air conditioners.

Input Format

The first line contains an integer n (1≤n≤50 000)n\ (1 \le n \le 50\,000), the number of classrooms.

The second line contains nn integers ai (1≤ai≤1000)a_i\ (1 \le a_i \le 1000), where aia_i is the minimum required power (in watts) for the air conditioner in classroom ii.

The third line contains an integer m (1≤m≤50 000)m\ (1 \le m \le 50\,000), the number of available air conditioner models.

The next mm lines each contain two integers bjb_j and cj (1≤bj≤1000,1≤cj≤1000)c_j\ (1 \le b_j \le 1000, 1 \le c_j \le 1000), where bjb_j is the power (in watts) of model jj and cjc_j is its price.

Output Format

Output one integer, the minimum total cost to equip all classrooms with air conditioners. It is guaranteed that there is at least one feasible solution that meets the requirements of all classrooms.

1
800
1
800 1000

1000

3
1 2 3
4
1 10
1 5
10 7
2 3

13

Hint

In the first sample, you can only buy the only available air conditioner, which costs 10001000 yuan.

In the second sample, the optimal solution is to install model 44 in classrooms 11 and 22, and model 33 in classroom 33, for a total cost of 1313 yuan (3+3+73 + 3 + 7).

Scoring

For the 5050-point testdata, n,m≤1000n, m \le 1000.

Translation source: GPT 4.1 mini.

Translated by ChatGPT 5