Route Design
Time limit1sMemory limit128 MB
Given two banks of valued sites and a set of non-crossing routes, find the maximum total value of a tour that alternates between banks without intersecting routes.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Sorting, Segment tree, Combinatorics
- Solved
- No attempts yet
Problem
Bessie has opened a travel agency along the Amoozon river. Tourist sites lie on both banks of the river: there are sites on the left bank and sites on the right bank, and each site has an integer value describing how interesting it is.
Every route crosses the river, connecting a site on the left bank to a site on the right bank; no route connects two sites on the same bank. A tour is a sequence of sites in which every pair of adjacent sites is joined by a route, so a tour alternates between the two banks. Bessie may begin and end a tour at any site on either bank. The value of a tour is the sum of the values of the distinct sites it visits, and she wants a tour of maximum value.
Because several tours may run at the same time, no two routes used by a single tour may intersect. Number the left-bank sites to from one end, and the right-bank sites to from the same end. A route joining left site to right site and a route joining left site to right site intersect exactly when at least one of these holds: and ; or and ; or and .
Find the maximum value of a tour.
Input
- The first line contains three integers , , and (, , ): the number of left-bank sites, the number of right-bank sites, and the number of routes.
- Each of the next lines contains one integer (): the value of the -th left-bank site.
- Each of the next lines contains one integer (): the value of the -th right-bank site.
- Each of the next lines contains two integers and (, ): a route between left-bank site and right-bank site .
Output
- Print one integer: the maximum value attainable by a single tour.
Notes
In the first example the left bank has three sites with values , , and , the right bank has two sites with values and , and there are four routes. The best tour starts at left site , goes to right site , and ends at left site ; their values total .