## COCI '19 Contest 5 #4 Putovanje

View as PDF

Points: 15 (partial)
Time limit: 1.0s
Memory limit: 512M

Problem types
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig

Little Fabijan loves bars and travels. He wishes to drink beer coffee in each of the towns in his country conveniently numbered from to . The towns are connected via bidirectional roads such that each town is reachable from any other town by traversing some of the roads. Fabijan decided to drink coffee in every town in order from town number to town number . Therefore, he starts from town number (where he drinks his first coffee) and travels to town number for his next cup of coffee. During that travel he might pass through a number of different towns but he won’t make a coffee stop in those towns. After drinking coffee in town , he will proceed to travel to town , and so on until he finally reaches town where he will drink his last coffee.

In order to traverse a certain road, he needs to have a valid ticket. The -th road can be traversed if you have either a single-pass ticket which costs euros or a multi-pass ticket which costs euros. For each road, Fabijan can decide to buy a single-pass ticket each time he needs to traverse that road or he might opt to buy a multi-pass ticket once.

Write a program that computes the smallest number of euros Fabijan needs to spend on tickets in order to successfully complete his travels.

#### Input

The first line contains an integer from task description.

In -th of the next lines there are four integers which represent that towns and are connected with a road with ticket prices and .

#### Output

In a single line output the smallest cost of travel.

#### Scoring

Each town will be directly connected with at most two other towns.

#### Sample Input 1

4
1 2 3 5
1 3 2 4
2 4 1 3

#### Sample Output 1

10

#### Explanation for Sample Output 1

Fabijan first travels from town to town and it is optimal for him to buy a multi-pass ticket ( euros) for the road which connects them. Then he travels from town to town via town . He already has a multi-pass ticket that takes him from to and he buys a single-pass ticket from town to town ( euros). On his travels from town to town he buys another single-pass ticket that takes him from town back to town ( euros) and after that buys a single-pass ticket that takes him from town to town ( euro). In total, he spent euros.

#### Sample Input 2

4
1 4 5 5
3 4 4 7
2 4 2 6

#### Sample Output 2

16

#### Sample Input 3

5
1 2 2 3
1 3 2 3
1 4 2 3
1 5 2 3

#### Sample Output 3

11