#BF0000039. Hàng chờ triển lãm thế giới (World Exhibition)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Hàng chờ triển lãm thế giới (World Exhibition)
World Exhibition
Source: HDU
Version: Phuoc Hung OJ Extended
Problem Statement
There are people standing in index order along a line, and ties in position are allowed. Upper-bound constraints limit how far liking pairs may be; lower-bound constraints separate disliking pairs. Find the maximum distance from person to person , print -1 if infeasible or -2 if unbounded.
All input triples satisfy . Two people may stand at the same coordinate.
This Extended version removes the leading count of independent test cases.
Input
First line . Then triples (upper bounds) and triples (lower bounds).
Output
Print the greatest distance, -1 for impossible, or -2 for unbounded.
Subtasks
- Subtask 1 (20 points): $2\le n\le12; 0\le X,Y\le20; X+Y\ge1; 1\le A<B\le n; 1\le C\le10^6$.
- Subtask 2 (30 points): $2\le n\le150; 0\le X,Y\le500; X+Y\ge1; 1\le A<B\le n; 1\le C\le10^6$.
- Subtask 3 (50 points): $2\le n\le1000; 0\le X,Y\le10000; X+Y\ge1; 1\le A<B\le n; 1\le C\le10^6$.
Examples
Input
3 2 1
1 2 5
2 3 3
1 3 4
Output
8
Explanation
The upper constraints imply . The lower constraint is . Coordinates attain , the largest possible distance.