1552 - Road constructions

Time Limit : 1 Second

Memory Limit : 128 MB

Submission: 2

Solved: 1

Description
N cities are required to connect with each other by a new transportation system. After several rounds of bidding, we have selected M constructions companies and decided which section is assigned to which company, the associated cost and the direction of each road. 



Due to the insufficiency of national fiscal revenue and special taxation system (the tax paid by each company pays  is a fixed amount and tax payment occurs at the beginning of the construction of the project)



The government wishes to complete the project in several years and collects as much tax as possible to support the public expense



For the restrictions of construction and engineering techniques, if a company is required to start the construction, then itself and its associated companies have to complete all the tasks they commit (if company A constructs a road from city 1 to city 2, company B constructs a road from city 2 to city 3, company C constructs a road from city 1 to city 3, we call companies A and B are associated and other company pairs have no such relationship, pay attention, in this example and a are not associated, in other words,’ associated' is a directed relationship). 



Now the question is what the maximum income the government can obtain in the first year is?
Input
There are multiple cases (no more than 50).

Each test case starts with a line, which contains 2 positive integers, n and m (1<=n<=1000, 1<=m<=5000).

The next line contains m integer which means the tax of each company.

The Third line has an integer k which indicates the number of the roads.

Then k lines fellow, each contains 4 integers, the start of the roads, the end of the road, the company is responsible for this road and the cost of the road.

The end of the input with two zero
Output
For each test case output the maximum income in a separate line, and if you can not get any income, please output 0.
sample input
4 2
500 10
4
1 2 1 10
2 3 1 20
4 3 1 30
1 4 2 60
4 2
500 100
5
1 2 1 10
2 3 1 20
4 3 1 30
4 3 2 10
1 4 2 60
3 1
10
3
1 2 1 100
2 3 1 100
3 1 1 100
0 0
sample output
440
470
0
hint
for second test case, if you choose company 2 responsible ways, then you must choose the path of responsible company 1, but if you choose company 1, then you do not have to choose company 2.
source
Zhang Zheng | 2011 Multi-University Training Contest 8 - Host by HUST
© 2015 HUST ACMICPC TEAM. All Right Reserved.