Algorithm Tutoring
Time limit2sMemory limit1024 MB
Each student i accepts partners j with l_i <= |i-j| <= r_i; find the maximum |a_i - a_j| over mutually compatible pairs, or -1.
- Level
Medium7 of 10
- Topics
- Segment tree, Sorting, Array, Binary search
- Solved
- No attempts yet
Problem
The algorithm academy run by Jihwan has students, numbered through .
The academy uses a rating system to represent each student's skill level. Every student has a rating, and the rating of student is denoted .
To raise his students' ratings, Jihwan wants to pick students and secretly tutor them. However, a student dislikes others whose number differs from theirs by too much or too little. So student is only willing to be tutored together with students whose number differs from by at least and at most .
Find the maximum possible rating difference when choosing students that satisfy the above conditions.
Input
The first line gives the number of students in the algorithm academy, . ()
Lines through give information about the students. Line contains three integers , , separated by spaces, meaning that student has rating and is only willing to be tutored together with students whose number differs from by at least and at most . (, )
Output
Print the maximum rating difference over all choices of students that satisfy every condition in the problem.
If no such pair of students exists, print .