Under construction

Still being built. Things may change

Exam Study Planning

Interview

Time limit1sMemory limit1024 MB

Summary
Given exams with start, early-exit, and full end times plus study needs, find the most exams you can pass by studying in free time before each exam starts.
Level

Medium6 of 10

Topics
Dynamic programming, Greedy
Solved
No attempts yet

Problem

You have a lot of exams today, and you have not prepared for any of them yet. From experience, you know that if you study enough for an exam, you will pass it quickly, well within the time limit. You also know exactly how much study time each exam needs to pass within its time limit. If you study less than that, you will fail.

Your university requires you to attend all of your exams. You may leave an exam early only if you know for sure that you passed it.

You may start studying at time 00, but you can only study while you are not taking an exam. Studying for an exam does not have to happen in one contiguous block. You may stop and resume it.

Given the full exam schedule, including when each exam starts, how long it takes, how long you need to study for it, and how quickly you can finish an exam you studied for, how many exams can you pass at most?

Input

The input consists of:

  • One line with an integer nn (1≤n≤20001 \leq n \leq 2000), the number of exams you have to attend.

  • nn lines, the iith of which contains four integers describing an exam:

    • sis_i (0≤si0 \leq s_i), the start time of the iith exam,
    • pip_i (si<pis_i < p_i), the end time of the iith exam if you prepare for it,
    • eie_i (pi≤ei≤109p_i \leq e_i \leq 10^9), the end time of the iith exam if you do not prepare for it,
    • aia_i (1≤ai≤1091 \leq a_i \leq 10^9), the time needed to prepare the iith exam.

The exams are given in ascending order of start time and do not overlap, i.e. ei≤si+1e_i \leq s_{i+1} holds for 1≤i<n1 \leq i < n.

Output

Output the maximum number of exams you can pass.

Examples2

  1. Example 1

    Input
    3
    10 20 30 5
    30 50 100 15
    100 101 200 50
    
    Expected output
    3
    
  2. Example 2

    Input
    3
    1000 1001 1002 1000
    1003 1004 1005 500
    1006 1007 1008 500
    
    Expected output
    2