#P10685. [COTS 2024] 兔子 Zečevi

[COTS 2024] 兔子 Zečevi

Background

Translated from Izborne Pripreme 2024 (Croatian IOI/CEOI Team Selection) D2T3。8s,512M\texttt{8s,512M}

Please do not abuse the judging system for this problem. Abusing it will result in your account being banned.

Problem Description

There are NN rabbits on the number line. Rabbit ii is at position xix_i. Initially, rabbit ii has energy pip_i.

Every second, the following events happen:

  • If there exists at least one rabbit whose energy is 00, the process ends.
  • Otherwise, every rabbit jumps 11 unit to the right, and its energy decreases by 11.

There are MM carrots on the number line. Carrot ii is at position yiy_i and has mass tit_i kilograms. When a rabbit is on a position with a carrot, it may choose to eat aa kilograms of that carrot, where a[0,y]a\in [0, y], and yy is the carrot's mass. After eating aa kilograms, the rabbit's energy increases by aa, and the carrot's mass decreases by aa.

Clearly, once the rabbits stop jumping, they will never jump again. Under optimal decisions, what is the maximum number of seconds the rabbits can keep jumping?

Input Format

The first line contains two integers N,MN,M, as described above.

The next NN lines each contain two integers xi,pix_i,p_i.

The next MM lines each contain two integers yi,tiy_i,t_i.

Output Format

Output one line with one integer, representing the answer under optimal decisions.

3 5
2 4
7 3
9 5
3 2
8 1
10 2
6 3
1 3
5
5 1
2 6
3 7
5 4
1 10
7 2
8 27
11

Hint

Sample Explanation

Explanation for sample 11:

We use a pair (xi,pi)(x_i,p_i) to represent a rabbit's position and energy.

After three jumps, the three rabbits are in states (5,1),(10,0),(6,2)(5,1),(10,0),(6,2). The second rabbit eats 22 kilograms of carrot, becoming (5,1),(10,2),(6,2)(5,1),(10,2),(6,2).

After the next jump, the three rabbits are in states (6,0),(11,1),(7,1)(6,0),(11,1),(7,1). The first rabbit eats 33 kilograms of carrot, becoming (6,3),(11,1),(7,1)(6,3),(11,1),(7,1).

After the next jump, the three rabbits are in states (7,2),(12,0),(8,0)(7,2),(12,0),(8,0). Since the second rabbit cannot eat any carrot, the jumping process terminates.

It can be proven that this is the optimal answer.

Constraints

For 100%100\% of the testdata, it is guaranteed that:

  • 1N,M1051\le N,M\le 10^5
  • 0xi,yi1090\le x_i,y_i\le 10^9
  • 0pi,ti1090\le p_i,t_i\le 10^9
Subtask ID Score Constraints
11 99 N=1N=1
22 1212 M=1M=1
33 2626 N,M1000N,M\le 1\, 000
44 3434 N,Q50000N,Q\le 50\, 000
55 1919 No additional constraints

Translated by ChatGPT 5