Algorithm


A. Ring road
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Nowadays the one-way traffic is introduced all over the world in order to improve driving safety and reduce traffic jams. The government of Berland decided to keep up with new trends. Formerly all n cities of Berland were connected by n two-way roads in the ring, i. e. each city was connected directly to exactly two other cities, and from each city it was possible to get to any other city. Government of Berland introduced one-way traffic on all n roads, but it soon became clear that it's impossible to get from some of the cities to some others. Now for each road is known in which direction the traffic is directed at it, and the cost of redirecting the traffic. What is the smallest amount of money the government should spend on the redirecting of roads so that from every city you can get to any other?

Input

The first line contains integer n (3 ≤ n ≤ 100) — amount of cities (and roads) in Berland. Next n lines contain description of roads. Each road is described by three integers aibici (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ ci ≤ 100) — road is directed from city ai to city bi, redirecting the traffic costs ci.

Output

Output single integer — the smallest amount of money the government should spend on the redirecting of roads so that from every city you can get to any other.

Examples
input
Copy
3
1 3 1
1 2 1
3 2 1
output
Copy
1
input
Copy
3
1 3 1
1 2 5
3 2 1
output
Copy
2
input
Copy
6
1 5 4
5 3 8
2 4 15
1 6 16
2 3 23
4 6 42
output
Copy
39
input
Copy
4
1 2 9
2 3 8
3 4 7
4 1 5
output
Copy
0



 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <cstdio>
#include <set>

int main(){

    int n; scanf("%d\n", &n);
    std::set<int> first, second;
    long totalCost(0), current(0);

    while(n--){
        int a, b, c; scanf("%d %d %d\n", &a, &b, &c);
        totalCost += c;
        if(first.count(a) || second.count(b)){current += c; first.insert(b); second.insert(a);}
        else{first.insert(a); second.insert(b);}
    }

    if(totalCost - current < current){current = totalCost - current;}
    printf("%ld\n", current);

    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
3
1 3 1
1 2 1
3 2 1

Output

x
+
cmd
1
Advertisements

Demonstration


Codeforces Solution-A. Ring road-Solution in C, C++, Java, Python

Previous
Codeforces solution 1080-B-B. Margarite and the best present codeforces solution
Next
CodeChef solution DETSCORE - Determine the Score CodeChef solution C,C+