#P13965. [VKOSHP 2024] Two-Story Advent Calendar

[VKOSHP 2024] Two-Story Advent Calendar

题目描述

The "General Passenger Company" is launching New Year's train excursions from Saint Petersburg to Veliky Ustyug. For all purchasers of this trip, a special gift has been developed---an advent calendar.

The advent calendar is a box shaped like the main carrier of the "General Passenger Company" --- a two-story train car. Inside the box, there are small boxes arranged in two levels, with each small box containing a candy. The upper level contains nn small boxes, while the lower level contains mm small boxes. Each small box is labeled with a natural number from 11 to n+mn+m, inclusive. The numbers on the boxes do not repeat.

For each small box, its length is known. The boxes can differ in length. It is guaranteed that the sums of the lengths of the boxes on the first and second levels of the advent calendar are equal.

To properly open the advent calendar, on the first day, you must take and open the box numbered 11, on the second day ---the box numbered 22, and so on, finishing the calendar with the box numbered n+mn+m, which should be taken and opened on the (n+m)(n+m)-th day. An example of the advent calendar is shown in the figure.

:::align{center}

Advent calendar with 88 cells. To properly open it and create a New Year's mood, you need to open cell 11 88 days before New Year, cell 22 77 days before, and so on. On the last day ---December 31 ---you need to open cell 88. :::

Designer and perfectionist Maya decided to take a train ride for the New Year and received a two-story advent calendar as a gift. Maya finds it inconvenient when she opens a candy box on the lower level and there is at least one unopened box on top of it on the upper level.

Maya became curious about how many boxes she needs to remove from the calendar in advance to make it convenient. At the same time, Maya wants to leave as many boxes as possible in the advent calendar. Help her determine the minimum number of boxes that need to be removed from the calendar in advance so that when opening a box on the lower level, there are no unopened boxes from the upper level on top of it. Boxes can be removed in advance from both the upper and lower levels.

输入格式

The first line of input contains an integer nn (1n1051 \le n \le 10^{5}) --- the number of boxes on the upper level of the calendar.

In the next nn lines, there are two numbers aia_i and xix_i (1ai1091 \le a_i \le 10^{9}, 1xin+m1 \le x_i \le n + m) --- the length of the ii-th box on the upper level of the calendar and the number written on it, respectively.

On the (n+1)(n+1)-th line of input, there is an integer mm (1m1051 \le m \le 10^{5}) --- the number of boxes on the lower level of the calendar.

In the next mm lines, there are two numbers bjb_j and yjy_j (1bj1091 \le b_j \le 10^{9}, 1yjn+m1 \le y_j \le n + m) --- the length of the jj-th box on the lower level of the calendar and the number written on it, respectively.

It is guaranteed that $a_1 + a_2 + \ldots + a_n = b_1 + b_2 + \ldots + b_m$. It is guaranteed that all numbers in the set {x1,x2,,xn,y1,y2,,ym}\{x_1, x_2, \ldots, x_n, y_1, y_2, \ldots, y_m\} are distinct.

输出格式

Output a single integer kk---the minimum number of boxes that need to be removed from the calendar to make it convenient.

3
1 1
1 2
1 3
3
1 4
1 5
1 6
0
3
4 1
3 8
3 6
5
2 2
3 3
1 5
2 7
2 4
2

提示

In the second example, you can remove the boxes numbered 44 and 88. After this, the calendar will look like the one shown below and will become convenient.

:::align{center} :::