eolymp
bolt
Try our new interface for solving problems
Problems

World Trip

World Trip

Kita_masa is planning a trip around the world. This world has \textbf{N} countries and the country \textbf{i} has \textbf{M_i} cities. Kita_masa wants to visit every city exactly once, and return back to the starting city. In this world, people can travel only by airplane. There are two kinds of airlines: domestic and international lines. Since international airports require special facilities such as customs and passport control, only a few cities in each country have international airports. You are given a list of all flight routes in this world and prices for each route. Now it's time to calculate the cheapest route for Kita_masa's world trip! \InputFile \textit{N} \textit{K} \textit{N} \textit{i} \textit{M}_i \textit{i} \textit{N} \textit{i} \textit{F}_i \textit{i} \textit{K} The first line contains two integers and , which represent the number of countries and the number of flight routes, respectively. The second line contains integers, and the -th integer represents the number of cities in the country . The third line contains integers too, and the -th integer represents the number of international airports in the country . Each of the following lines contains five integers \[Country 1\] \[City 1\] \[Country 2\] \[City 2\] \[Price\]. This means that, there is a bi-directional flight route between the \[City 1\] in \[Country 1\] and the \[City 2\] in \[Country 2\], and its price is \[Price\]. \textit{F}_i \textit{c}_1 \textit{n}_1 \textit{c}_2 \textit{n}_2 \textit{n}_1≠\textit{n}_2 \textit{c}_1≤\textit{F}_n1 \textit{c}_2≤\textit{F}_n2 Note that cities in each country are numbered from 1, and the cities whose city number is smaller or equal to have international airports. That is, if there is a flight route between the city in the country and the city in the country with , it must be and . You can assume that there's no flight route which departs from one city and flies back to the same city, and that at most one flight route exists in this world for each pair of cities. The following constraints hold for each dataset: \begin{itemize} \item 1 ≤ \textit{N }≤ 15 \item 1 ≤ \textit{M}_i ≤ 15 \item 1 ≤\textit{ F}_i ≤ 4 \item \textit{sum}(\textit{F}_i) ≤ 15 \item 1 ≤\textit{ Price} ≤ 10,000 \end{itemize} \OutputFile Print a line that contains a single integer representing the minimum price to make a world trip. If such a trip is impossible, print \textbf{-1} instead.
Time limit 5 seconds
Memory limit 256 MiB
Input example #1
4 6
1 1 1 1
1 1 1 1
1 1 2 1 1
1 1 3 1 2
1 1 4 1 1
2 1 3 1 1
2 1 4 1 2
3 1 4 1 1
Output example #1
4
Source Japan Alumni Group Winter Contest 2012