Safari

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

요약
각 동물이 정해진 시간 구간에 나타나고 L1 거리로 이동할 때, 동물을 관찰한 시간의 합의 최댓값을 구한다.
난이도

어려움10점 중 8점

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

문제

Safari is a journey that involves going into nature to watch wild animals. Typically, safari participants travel through vast grasslands in four-wheel-drive cars, shortly 4WD cars. Imagine you are on safari in a 4WD car CC.

Animals appear in specific places on the grassland, where you can consider the places as the points on the plane. There are nn animals, indicated as 11 to nn, in which the animal ii appears at the point p_ip\_i in the plane. Each animal appears only in its own certain time interval. Specifically, the animal ii appears in the time interval \[a_i,b_i]\[a\_i, b\_i]. So, when the car CC stays at p_ip\_i for the duration \[v,w]\[v, w], you have the opportunity to observe the animal ii for the time period \[v,w]∩\[a_i,b_i]\[v, w] \cap \[a\_i , b\_i]. Note that if you observe an animal during \[α,β]\[\alpha, \beta], the length of time when you observe it is ∣β−α∣|\beta − \alpha|.

The car CC departs from the starting point s=(0,0)s = (0, 0) at time 00 and it moves at speed 11. Thus time dd has passed when CC moves distance dd. The distance is measured in the L1L^1-metric. That is, the distance d(p_1,p_2)d(p\_1, p\_2) between points p_1=(x_1,y_1)p\_1 = (x\_1, y\_1) and p_2=(x_2,y_2)p\_2 = (x\_2, y\_2) is ∣x_1−x_2∣+∣y_1−y_2∣|x\_1 - x\_2 | + |y\_1 - y\_2|. It always takes as long as the distance d(p_1,p_2)d(p\_1, p\_2) while the car moves from p_1p\_1 to p_2p\_2. Also, the car may stay at a point as long as you need, if necessary. When your safari tour ends at the last sighting point of an animal, you want to know the longest possible time for which you observe the animals.

For example, the figure below shows six animals, indicated as 11 to 66, which appear at the coordinates (1,2)(1, 2), (2,1)(2, 1), (2,4)(2, 4), (4,1)(4, 1), (5,3)(5, 3), (5,5)(5, 5), respectively, of points in the plane. Let us also indicate as 11 to 66 the points of animals. The time intervals when the animals appear are also displayed. First, consider the case the car CC is driving along the blue path. The car departs from ss at time 00 and arrives at the point 22 at time 33. Departing from it immediately at time 33, the length of time when you observe the animal 22 is 00. Afterward, you arrive at the point 33 at time 66 and stay there during \[6,8]\[6, 8]. Next, you arrive at the point 66 at time 1212 and observe the animal 66 during \[12,16]\[12, 16]. Then the total length of time when you observe the animals is 0+2+4=60 + 2 + 4 = 6. Secondly, consider the case the car CC is driving along the red path. At first, you arrive at the point 11 at time 33 and stay during \[3,6]\[3, 6]. Departing from it at time 66, you arrive at the point 44 at time 1010 and stay during \[10,12]\[10, 12]. Then you arrive at the point 55 at time 1515 and observe the animal 55 during \[15,18]\[15, 18]. In this case, the total length of time when you observe the animals is 2+2+3=72 + 2 + 3 = 7, which is longer than the blue path and actually, the length of the longest time when you can observe the animals.

Given nn coordinates of points and nn time intervals for the appearances of animals, write a program to output the length of the longest possible time when you observe the animals.

입력

Your program is to read from standard input. The input starts with a line containing one integer nn (1≤n≤5,0001 ≤ n ≤ 5\\,000), where nn is the number of animals. The animals are numbered from 11 to nn. In the following nn lines, the ii-th line contains two integers x_ix\_i and y_iy\_i that represent the coordinate (x_i,y_i)(x\_i , y\_i) of the point p_ip\_i in the plane where the animal ii appears (0≤x_i,y_i≤1060 ≤ x\_i , y\_i ≤ 10^6). Note that the car CC is located at (0,0)(0, 0) at time 00. The given points containing (0,0)(0, 0) are all distinct. In the following nn lines, the ii-th line contains two integers v_iv\_i and w_iw\_i that represent the duration \[v_i,w_i]\[v\_i , w\_i] when the animal ii appears at p_ip\_i (0≤v_i<w_i≤1090 ≤ v\_i < w\_i ≤ 10^9).

출력

Your program is to write to standard output. Print exactly one line. The line should contain the length of the longest time when you can observe the animals.

예제2

  1. 예제 1

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

    입력
    6
    1 2
    2 1
    2 4
    4 1
    5 3
    5 5
    3 5
    1 3
    6 9
    10 13
    15 18
    12 16
    
    예상 출력
    7