Cf 17B hierarchy

Source: Internet
Author: User

Nick's company employedNPeople. Now Nick needs to build a tree hierarchy? Supervisor-surbodinate? Relations in the company (this is to say that each employee, employee t one, has exactly one supervisor). There areMApplications written in the following form :? EmployeeAIIs ready to become a supervisor of employeeBIAt extra costCI?. The qualificationQJOf each employee is known, and for each application the following is true:QAI?>?QBI.

Wocould you help Nick calculate the minimum cost of such a hierarchy, or find out that it is impossible to build it.

Input

The first input line contains integerN(1? ≤?N? ≤? (1000)-amount of employees in the company. The following line containsNSpace-separated numbersQJ(0? ≤?QJ? ≤? 106)-The employees 'qualifications. the following line contains numberM(0? ≤?M? ≤? 10000)-amount of stored ed applications. The followingMLines contain the applications themselves, each of them in the form of three space-separated numbers:AI,BIAndCI(1? ≤?AI,?BI? ≤?N, 0? ≤?CI? ≤? 106 ). different applications can be similar, I. e. they can come from one and the same employee who offered to become a supervisor of the same person but at a different cost. for each applicationQAI?>?QBI.

Output

Output the only line-the minimum cost of building such a hierarchy, or-1 if it is impossible to build it.

Sample test (s) Input
47 2 3 141 2 52 4 13 4 11 3 5
Output
11
Input
31 2 323 1 23 1 3
Output
-1
Note

In the first sample one of the possible ways for building a hierarchy is to take applications with indexes 1, 2 and 4, which give 11 as the minimum total cost. in the second sample it is impossible to build the required hierarchy, so the answer is-1.

Preprocessing + greedy can be done, because there is only one root, so it is better to preprocess the minimum greedy value of the subnode during input =. =
# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <limits. h> using namespace STD; const int INF = 0x3ffffff; int f [1100]; int main () {int n, m; int temp, U, V, W; while (CIN> N) {for (INT I = 1; I <= N; I ++) {CIN> temp; F [I] = inf ;} cin> m; For (INT I = 1; I <= m; I ++) {CIN> U> V> W; if (F [v]> W) // minimum preprocessing value f [v] = W;} int flag = 1, k = 1; int ans = 0; for (INT I = 1; I <= N; I ++) // only one INF can exist, that is, the root node {If (F [I] = inf & K) {ANS-= inf; k = 0;} else if (F [I] = inf) {flag = 0; break;} ans + = f [I];} if (FLAG) cout <ans <Endl; else cout <-1 <Endl;} return 0 ;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.