eolymp
bolt
Try our new interface for solving problems
Problems

Paper Route

Paper Route

As a poor, tuition-ridden student, you've decided to take up a part time job as a paperboy/papergirl. You've just been handed your paper route: a set of addresses (conveniently labelled \textbf{1} to \textbf{N}). Every morning, you start at the newspaper office (which happens to be address number \textbf{0}). You have to plan a route to deliver a newspaper to every address - and you also want to get to class right after you're done. Conveniently, there are only \textbf{N} roads in your area connecting the addresses, and each of them takes a known time to traverse. Also, you've precalculated the time it takes to get to Waterloo campus from each address, including the newspaper office (through some combination of biking, busing, or hitching a ride). How soon can you be done delivering papers and be in your seat at school? \InputFile First, there will be a single integer \textbf{N} (the number of addresses, \textbf{1} ≤ \textbf{N} ≤ \textbf{100,000}). Next, there will be \textbf{N+1} lines, each with an integer \textbf{c_i} (starting with \textbf{i} = \textbf{0}, \textbf{0} ≤ \textbf{c_i} ≤ \textbf{1,000,000,000}), the time it takes to get from location \textbf{i} to campus. Finally, the input will contain \textbf{N} lines, each with three integers \textbf{a}, \textbf{b}, \textbf{c} (\textbf{0} ≤ \textbf{a}, \textbf{b} ≤ \textbf{N}, \textbf{a} ≠ \textbf{b}, \textbf{0} ≤ \textbf{c} ≤ \textbf{1,000}). Each of these lines describes a road between locations \textbf{a} and \textbf{b} taking \textbf{c} minutes to traverse. It is guaranteed that you will be able to reach all the addresses. (Remember that location \textbf{0} is the newspaper office.) \OutputFile Output the minimum time it will take to deliver all the papers and get to class. \textit{Note}: It's actually better to visit all the addresses, go back to the office, and get to school from there. An example route: 0 -> 1 -> 0 -> 2 -> 0 -> school = 1 + 1 + 2 + 2 + 1 = 7.
Time limit 1 second
Memory limit 64 MiB
Input example #1
2
1
3
5
0 1 1
0 2 2
Output example #1
7
Source 2011 Waterloo ACM Programming Contest, October 2, Problem A