atcoder#ABC188E. [ABC188E] Peddler
[ABC188E] Peddler
Score : points
Problem Statement
In Takahashi Kingdom, there are towns, called Town through Town . There are also roads in the kingdom, called Road through Road . By traversing Road , you can travel from Town to Town , but not vice versa. Here, it is guaranteed that . Gold is actively traded in this kingdom. At Town , you can buy or sell kilogram of gold for yen (the currency of Japan).
Takahashi, a traveling salesman, plans to buy kilogram of gold at some town, traverse one or more roads, and sell kilogram of gold at another town. Find the maximum possible profit (that is, the selling price minus the buying price) in this plan.
Constraints
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the answer.
4 3
2 3 1 5
2 4
1 2
1 3
3
We can achieve the profit of yen, as follows:
- At Town , buy one kilogram of gold for yen.
- Traverse Road to get to Town .
- Traverse Road to get to Town .
- At Town , sell one kilogram of gold for yen.
5 5
13 8 3 15 18
2 4
1 2
4 5
2 3
1 3
10
We can achieve the profit of yen, as follows:
- At Town , buy one kilogram of gold for yen.
- Traverse Road to get to Town .
- Traverse Road to get to Town .
- At Town , sell one kilogram of gold for yen.
3 1
1 100 1
2 3
-99
Note that we cannot sell gold at the town where we bought the gold, which means the answer may be negative.