#P8272. Apple Catching on the Number Line

    ID: 21451 Type: Default 1000ms 256MiB

Apple Catching on the Number Line

Apple Catching on the Number Line

Farmer John's cows are trying to catch apples falling from the sky! At certain moments, a number of apples will fall onto a number line. At other moments, some of the cows arrive on the number line and start catching apples.

If an apple falls onto the number line when no cow is available to catch it, the apple vanishes forever. However, if a cow and an apple arrive at the same time, the cow catches the apple immediately. Each cow moves at a speed of 1 unit per second. Once a cow catches an apple, she leaves the number line and cannot catch any more apples.

More formally, assume there is an apple event at time \(t_a\) at position \(x_a\) dropping \(a\) apples, and a cow event at time \(t_c\) at position \(x_c\) with \(b\) cows arriving. A cow from the cow event can catch an apple from the apple event if and only if \(t_a \ge t_c\) and \[ |x_a - x_c| \le t_a - t_c. \]

Each cow can catch at most one apple. Given all apple and cow events, if the cows cooperate optimally, what is the maximum number of apples they can catch?

inputFormat

The input begins with two integers \(A\) and \(C\) representing the number of apple events and cow events respectively. It is followed by \(A\) lines, each containing three integers \(t_a\), \(x_a\), and \(a\) that denote an apple event (the time the apples fall, the position on the number line, and the number of apples). This is followed by \(C\) lines, each containing three integers \(t_c\), \(x_c\), and \(b\) representing a cow event (the time the cows arrive, the position on the number line, and the number of cows).

You may assume that all times and positions are integers.

outputFormat

Output a single integer: the maximum number of apples that can be caught by the cows.

sample

1 1
5 5 10
2 3 5
5

</p>