#P2887. Cow Tanning

    ID: 16145 Type: Default 1000ms 256MiB

Cow Tanning

Cow Tanning

Cows love to tan, but they don't want to get sunburned or miss out on their tan completely. There are \( C \) cows and \( L \) bottles of sunscreen available. The \( i\)th cow will only tan if the SPF of the sunscreen is in the range \( [minSPF_i,\ maxSPF_i] \). If the SPF is lower than \( minSPF_i \), she gets sunburned, and if it is higher than \( maxSPF_i \), she won't tan at all.

Each bottle of sunscreen has a fixed SPF value \( SPF_i \) and can cover a limited number of cows given by \( cover_i \). A cow may use lotion from only one bottle. The goal is to determine the maximum number of cows that can tan safely using the available bottles.

Constraints:

  • \( 1 \le C \le 2500 \)
  • \( 1 \le L \le 2500 \)
  • \( 1 \le minSPF_i \le 1000, \quad minSPF_i \le maxSPF_i \le 1000 \)
  • \( 1 \, \le SPF_i \le 1000 \)

inputFormat

The first line contains two integers \( C \) and \( L \), representing the number of cows and lotion bottles respectively.

The next \( C \) lines each contain two integers \( minSPF_i \) and \( maxSPF_i \), describing the acceptable SPF range for the \( i\)th cow.

The following \( L \) lines each contain two integers \( SPF_i \) and \( cover_i \), indicating the SPF rating of the lotion bottle and the number of cows it can cover.

outputFormat

Output a single integer representing the maximum number of cows that can safely tan by applying a suitable lotion bottle.

sample

3 3
2 4
1 5
3 6
3 1
2 2
4 1
3