Problem list
Log in
#r03518 · Original

Parade Lineup

Platinum V 2.0 s 256 MB

$N$ marching units stand on a straight road, numbered $1$ through $N$ from the back of the parade to the front. Unit $i$ stands at an integer position $x_i$ (in metres), and the parade order must be respected: $x_1 \le x_2 \le \dots \le x_N$. Several units may share the same position.

There are $M$ friendly pairs: friends $a$ and $b$ ($a < b$) insist on staying close, so $x_b - x_a \le c$ must hold. There are also $P$ rival pairs: rivals $a$ and $b$ ($a < b$) refuse to march near each other, so $x_b - x_a \ge c$ must hold.

Arrange the parade so that every condition holds and the distance $x_N - x_1$ between the first and the last unit is as large as possible.

Input

The first line contains $N$, $M$ and $P$ ($2 \le N \le 1000$, $0 \le M, P$, $M + P \le 10,000$). Each of the next $M$ lines contains a friendly pair $a\ b\ c$ ($1 \le a < b \le N$, $1 \le c \le 10^6$): $x_b - x_a \le c$. Each of the last $P$ lines contains a rival pair $a\ b\ c$ (same ranges): $x_b - x_a \ge c$.

Output

Print the maximum possible value of $x_N - x_1$. If no arrangement satisfies every condition, print -1. If the distance can be made arbitrarily large, print -2. The answer can exceed 32-bit range — use 64-bit integers.

Example 1

Input
4 2 1
1 3 8
2 4 4
2 3 2
Output
10

Example 2

Input
3 1 1
1 3 3
1 2 4
Output
-1

Example 3

Input
3 0 1
1 2 5
Output
-2
Source
Original
Appears in
Not part of a ladder, contest or set yet.
Accepted 1 Submissions 1 Acceptance 100% Solvers 1

Discussion

Threads about this problem. Spoilers are collapsed; wrap hints in ||double bars||.

No discussion yet. Start a thread if something here needs explaining.

Recent submissions Open with filters
Code