Intervals

Time limit1sMemory limit128 MB

Summary
Given n integer intervals each needing at least c_i chosen points inside it, find the smallest set of integers satisfying all requirements.
Level

Hard8 of 10

Topics
Greedy, Sorting, Prefix sum, Intervals
Solved
No attempts yet

Problem

You are given nn closed integer intervals [ai,bi][a_i, b_i] together with nn integers c1,c2,…,cnc_1, c_2, \dots, c_n.

Write a program that:

  • reads the number of intervals nn, the two endpoints of each interval, and the integers c1,…,cnc_1, \dots, c_n from standard input,
  • computes the minimum size of a set ZZ of integers that shares at least cic_i common elements with the interval [ai,bi][a_i, b_i] for every i=1,2,…,ni = 1, 2, \dots, n,
  • writes that value to standard output.

In other words, minimize ∣Z∣|Z| subject to ∣Z∩[ai,bi]∣≥ci|Z \cap [a_i, b_i]| \ge c_i for every ii.

Input

The first line contains the number of intervals nn (1≤n≤50000)(1 \le n \le 50000).

Each of the next nn lines describes one interval. The (i+1)(i+1)-th line contains three integers aia_i, bib_i, and cic_i separated by single spaces, with 0≤ai≤bi≤500000 \le a_i \le b_i \le 50000 and 1≤ci≤bi−ai+11 \le c_i \le b_i - a_i + 1.

Output

Output a single integer: the minimum size of a set ZZ that shares at least cic_i elements with the interval [ai,bi][a_i, b_i] for every i=1,2,…,ni = 1, 2, \dots, n.

Examples5

  1. Example 1

    Input
    5
    3 7 3
    8 10 3
    6 8 1
    1 3 1
    10 11 1
    
    Expected output
    6
    
  2. Example 2

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

    Input
    2
    0 5 3
    3 8 3
    
    Expected output
    3
    
  4. Example 4

    Input
    2
    0 2 2
    5 7 2
    
    Expected output
    4
    
  5. Example 5

    Input
    3
    1 4 2
    3 6 2
    5 8 2
    
    Expected output
    4