#Q92. 「一本通 3.4 练习 2」布局 Layout
「一本通 3.4 练习 2」布局 Layout
Description
Original source: USACO 2005 Dec. Gold
FJ has cows , numbered . The cows will line up in order of their numbers (multiple cows may occupy the same position). In other words, assuming the -th cow is at position , then .
Some cows are good buddies and want the distance between them to be less than or equal to a certain number. Some cows are rivals and want the distance between them to be greater than or equal to a certain number.
Given pairs of good buddies, along with the maximum distance they desire between each other; and pairs of rivals, along with the minimum distance they desire between each other .
Calculate: If all the above conditions are satisfied, what is the maximum possible distance between cow and cow ().
Input Format
The first line: Three integers , separated by spaces.
Lines : Each line contains three integers , separated by spaces, indicating .
Lines : Each line contains three integers , separated by spaces, indicating .
It is guaranteed that .
Output Format
One line with an integer. If there is no valid arrangement, output . If there is a valid arrangement but the distance between cow and cow can be infinitely large, output . Otherwise, output the maximum distance between cow and cow .
Sample 1
The four cows are located at respectively.
4 2 1
1 3 10
2 4 20
2 3 3
27
Data Range and Hints
For all data, $2\le N\le 1000,1\le M_L,M_D\le 10^4,1\le L,D\le 10^6$.