405337: GYM101908 G Gasoline

Memory Limit:1024 MB Time Limit:0 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

G. Gasolinetime limit per test0.5 secondsmemory limit per test1024 megabytesinputstandard inputoutputstandard output

After the end of the truck drivers' strike, you and the rest of Nlogônia logistics specialists now have the task of planning the refueling of the gas stations in the city. For this, we collected information on stocks of $$$R$$$ refineries and about the demands of $$$P$$$ gas stations. In addition, there are contractual restrictions that some refineries cannot supply some gas stations; When a refinery can provide a station, the shorter route to transport fuel from one place to another is known.

The experts' task is to minimize the time all stations are supplied, satisfying their demands. The refineries have a sufficiently large amount of trucks, so that you can assume that each truck will need to make only one trip from a refinery to a gas station. The capacity of each truck is greater than the demand of any gas station, but it may be necessary to use more than one refinery.

Input

The first line of the input contains three integers $$$P, R, C$$$, respectively the number of gas stations, the number of refineries and the number of pairs of refineries and gas stations whose time will be given ($$$1 \leq P, R \leq 1000$$$; $$$1 \leq C \leq 20000$$$). The second line contains $$$P$$$ integers $$$D_i$$$ ($$$1 \leq D_i \leq 10^4$$$), representing the demands in liters of gasoline of the gas stations $$$i = 1, 2, \dots, P$$$, in that order. The third line contains $$$R$$$ integers $$$E_i$$$ ($$$1 \leq E_i \leq 10^4$$$), representing stocks, in liters of gasoline, of refineries $$$i = 1, 2, \dots, R$$$, in that order. Finally, the latest $$$C$$$ lines describe course times, in minutes, between stations and refineries. Each of these rows contains three integers, $$$I, J, T$$$ ($$$1 \leq I \leq P$$$; $$$1 \leq J \leq R$$$; $$$1 \leq T \leq 10^6$$$), where $$$I$$$ is the ID of a post, $$$J$$$ is the ID of a refinery and $$$T$$$ is the time in the course of a refinery truck $$$J$$$ to $$$I$$$. No pair $$$(J, I)$$$ repeats. Not all pairs are informed; If a pair is not informed, contractual restrictions prevents the refinery from supplying the station.

Output

Print an integer that indicates the minimum time in minutes for all stations to be completely filled up. If this is not possible, print −1.

ExamplesInput
3 2 5
20 10 10
30 20
1 1 2
2 1 1
2 2 3
3 1 4
3 2 5
Output
4
Input
3 2 5
20 10 10
25 30
1 1 3
2 1 1
2 2 4
3 1 2
3 2 5
Output
5
Input
4 3 9
10 10 10 20
10 15 30
1 1 1
1 2 1
2 1 3
2 2 2
3 1 10
3 2 10
4 1 1
4 2 2
4 3 30
Output
-1
Input
1 2 2
40
30 10
1 1 100
1 2 200
Output
200

加入题单

上一题 下一题 算法标签: