Under construction

Still being built. Things may change

Blockade Duty Schedule

Time limit1sMemory limit512 MB

Summary
Find the largest M such that a daily duty schedule exists where at least M students are on duty at every moment, respecting each student's free periods and daily minute limit.
Level

Medium7 of 10

Topics
Intervals, Binary search, Greedy
Solved
No attempts yet

Problem

Students at a faculty fell out with the dean and decided to blockade the faculty building. Keeping the blockade means a fixed number of students must be physically present in the building for all 24 hours of the day. The organizers want to split the duty shifts as fairly as they can, so they agreed on these rules.

  • The duty schedule is the same every day.
  • A student can be on duty only while free of other obligations.
  • Each student sets a limit on the total time spent on duty in one day, from midnight to midnight.
  • To keep things simple, a duty starts and ends only on a full hour or a half hour. For example 16:00 or 16:30 is allowed, 16:15 is not.
  • A student must be free for the whole duty. For example, if a student is free only from 16:05, the schedule cannot put her on duty starting at 16:00.
  • The smallest number of students on duty over any part of the day is made as large as possible. That improves the chances the blockade holds.

Write a program that finds the largest MM for which a schedule satisfying all of the rules above exists and at least MM students are on duty at every moment of the day, from midnight to midnight.

Assume a change of duty is instantaneous. If two students leave the blockade and two others take their place, treat the blockade as having two students present for the whole changeover.

Input

The first line has an integer nn (1≤n≤501 \le n \le 50), the number of students who want to join the blockade. Then come nn blocks, one per student.

Each block starts with two integers kik_i (1≤ki≤501 \le k_i \le 50) and mim_i (1≤mi≤14401 \le m_i \le 1440). kik_i is the number of time periods in which student ii is free of other obligations and can join the blockade, and mim_i is the largest total number of minutes that student will spend on duty in one day.

The remaining kik_i lines of the block each hold the start time and the end time of one continuous period in which the student is free, separated by a space. Periods may overlap, and their union is the time the student can be present at the blockade.

Times use the format HH:MM (00≤HH≤2300 \le HH \le 23, 00≤MM≤5900 \le MM \le 59). Midnight is written 00:00. When the end time comes before the start time, the student can stay at the blockade past midnight. For example, the period 23:00 03:00 means the student can be present from 23:00 until 03:00 the next morning. When the start time and the end time are equal, the student can be at the blockade for all 24 hours.

Output

Print one integer, the largest number of students MM that can be at the blockade at every moment of the day under the assumptions above.

Examples3

  1. Example 1

    Input
    3
    1 540
    00:00 00:00
    3 480
    08:00 10:00
    09:00 12:00
    13:00 19:00
    1 420
    17:00 00:00
    
    Expected output
    1
    
  2. Example 2

    Input
    5
    1 720
    18:00 12:00
    1 1080
    00:00 23:00
    1 1080
    00:00 20:00
    1 1050
    06:00 00:00
    1 360
    18:00 00:00
    
    Expected output
    2
    
  3. Example 3

    Input
    3
    1 1440
    00:00 00:00
    1 720
    00:00 12:15
    1 720
    12:05 00:15
    
    Expected output
    1