#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 small boxes, while the lower level contains small boxes. Each small box is labeled with a natural number from to , 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 , on the second day ---the box numbered , and so on, finishing the calendar with the box numbered , which should be taken and opened on the -th day. An example of the advent calendar is shown in the figure.
:::align{center}
Advent calendar with cells. To properly open it and create a New Year's mood, you need to open cell days before New Year, cell days before, and so on. On the last day ---December 31 ---you need to open cell . :::
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 () --- the number of boxes on the upper level of the calendar.
In the next lines, there are two numbers and (, ) --- the length of the -th box on the upper level of the calendar and the number written on it, respectively.
On the -th line of input, there is an integer () --- the number of boxes on the lower level of the calendar.
In the next lines, there are two numbers and (, ) --- the length of the -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 are distinct.
输出格式
Output a single integer ---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 and . After this, the calendar will look like the one shown below and will become convenient.
:::align{center}
:::