Component Testing

Time limit20sMemory limit128 MB

Summary
Given component classes each needing a set number of distinct reviewers per component, and engineer ranks each able to review a limited number of components, decide if all components can be reviewed.
Level

Medium7 of 10

Topics
Greedy, Sorting, Math, Implementation
Solved
No attempts yet

Problem

The engineers at INU Electronics have developed some new electronic components, and over the next two months they plan to thoroughly review and test them.

Each component is sorted into one of several classes according to its complexity and importance. Every component in the same class needs the same number of reviewers, while different classes may need different numbers of reviewers. A single class may contain many components. The reviewers of any one component must all be distinct (the same engineer cannot review a single component twice).

INU Electronics has several ranks. Each engineer holds exactly one rank, and all engineers of the same rank can review the same maximum number of components. Every engineer is able to review any component. An engineer may review several components of the same class or components of different classes, but may never review the same component more than once.

Determine whether all of the components can be tested within two months.

Input

The input consists of several test cases.

The first line of each test case contains two integers nn (1≤n≤10,0001 \le n \le 10{,}000) and mm (1≤m≤10,0001 \le m \le 10{,}000), where nn is the number of component classes and mm is the number of engineer ranks.

Each of the next nn lines describes one class with two integers jj (1≤j≤100,0001 \le j \le 100{,}000) and cc (0≤c≤100,0000 \le c \le 100{,}000): jj is the number of components in that class, and cc is the number of distinct reviewers required to review each component of that class.

Each of the next mm lines describes one rank with two integers kk (1≤k≤100,0001 \le k \le 100{,}000) and dd (0≤d≤100,0000 \le d \le 100{,}000): kk is the number of engineers holding that rank, and dd is the maximum number of components a single engineer of that rank can review.

The input ends with a line containing two zeros.

Output

For each test case, print 11 on its own line if every component can be reviewed, and 00 otherwise.

Examples3

  1. Example 1

    Input
    3 2
    2 3
    1 2
    2 1
    2 2
    2 3
    5 2
    1 1
    1 3
    1 1
    1 3
    1 1
    1 20
    1 4
    0 0
    
    Expected output
    1
    0
    
  2. Example 2

    Input
    1 1
    1 0
    1 0
    0 0
    
    Expected output
    1
    
  3. Example 3

    Input
    1 1
    1 2
    1 5
    0 0
    
    Expected output
    0