Blistavost

시간 제한4초메모리 제한1024 MB

요약
1m/s로 움직이는 수호자가 N개의 구간에 속한 모든 수정을 각 구간의 마감 시각 t_i 전에 만지도록 하는 최소 시간을 구한다.
난이도

어려움10점 중 8점

유형
동적 계획법, 정렬, 그리디
정답자
아직 제출이 없습니다

문제

In the heart of the Glass Valley lies a mysterious star temple, a place known for its collection of magical crystals that shine like stars. Each crystal holds special power and emits a radiant glow that illuminates the entire valley, as long as it remains untouched.

The temple guardian’s nightly task is to touch ONLY the crystals located within the specified ranges of the valley’s residents, while honoring all of their requirements. Each resident’s request tells the guardian which range of crystals must NOT stop shining BEFORE their bedtime, as they fear the darkness.

The guardian starts his journey at the temple entrance and must carefully coordinate their movements to dim the crystals such that they stop shining at the correct moment. The crystals are arranged in a line, spaced one meter apart from each other (the first crystal is one meter away from the entrance). The guardian can move at a speed of one meter per second and may stop when needed. The time it takes for the guardian to touch and dim a crystal is negligible. Given the residents’ requests, the temple guardian wants to know the minimum number of seconds required to fulfill all requests (the guardian does not need to return to the starting position).

입력

In the first line is an integer NN (1≤N≤50001 ≤ N ≤ 5000), number of residents’ requests.

In the next NN lines are integers l_il\_i, r_ir\_i, t_it\_i (1≤l_i≤r_i≤10181 ≤ l\_i ≤ r\_i ≤ 10^{18}, 1≤t_i≤10181 ≤ t\_i ≤ 10^{18}), representing the left and right bounds of the crystal range and the resident’s bedtime, respectively.

출력

In the first and only line output the minimum time in seconds required for the guardian to fulfill all the requests.

예제3

  1. 예제 1

    입력
    3
    1 1 1
    3 3 5
    5 5 3
    
    예상 출력
    7
    
  2. 예제 2

    입력
    3
    1 2 1
    1 1 5
    1 3 4
    
    예상 출력
    6
    
  3. 예제 3

    입력
    3
    6 6 6
    8 8 7
    9 9 9
    
    예상 출력
    9