#P1073. [NOIP 2009 提高组] 最优贸易
[NOIP 2009 提高组] 最优贸易
Description
Country has major cities and roads, each connecting a pair of these cities. Between any two cities, there is at most one direct road. Among these roads, some are one-way and some are two-way. A two-way road is still counted as road.
Because country is vast and resources vary by region, the price of the same commodity may differ between cities. However, the buy price and the sell price of the same commodity are always identical within the same city.
A merchant, A-Long, is traveling in country . After learning that the price of the same commodity may differ between cities, he decides to earn some travel money from price differences while touring. The cities in country are labeled from to . A-Long will start from city and eventually end his trip in city . During the trip, he may pass through any city multiple times, and he is not required to visit all cities. He will earn travel money in this way: he chooses a city he passes to buy his favorite commodity, a crystal ball, and later sells it in another city he passes, using the price difference as his travel funds. Since A-Long mainly came to travel, he will perform this trade at most once. If there is no profitable opportunity, he can choose not to trade.
Suppose there are major cities in country , with the city indices and road connections shown below. A single arrow indicates a one-way road, and a double arrow indicates a two-way road.

Assume the crystal ball prices in cities are .
A-Long can choose the route , buy a crystal ball in city at price , and sell it in city at price , earning .
A-Long can also choose the route , buy a crystal ball the first time he reaches city at price , and sell it the second time he reaches city at price , earning .
Given the crystal ball prices of cities and the information of roads (the indices of the two cities connected by each road and whether the road is one-way or two-way), please tell A-Long the maximum travel money he can earn.
Input Format
The first line contains positive integers and , separated by a space, representing the number of cities and the number of roads.
The second line contains positive integers, separated by spaces, in index order, representing the commodity prices of the cities.
The following lines each contain positive integers , separated by spaces. If , the road is a one-way road from city to city ; if , the road is a two-way road between cities and .
Output Format
Output a single integer, the maximum travel money that can be earned. If no trade is performed, output .
5 5
4 3 5 6 1
1 2 1
1 4 1
2 3 2
3 5 1
4 5 2
5
Hint
Constraints
- It is guaranteed that city can reach city .
- For of the testdata, .
- For of the testdata, .
- For of the testdata, there is no tour route that starts from a city and returns to the same city.
- For of the testdata: , , , , and city prices .
NOIP 2009 Senior, Problem 3.
Translated by ChatGPT 5
京公网安备 11011102002149号