Route Design

Time limit1sMemory limit128 MB

Summary
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 NN sites on the left bank and MM 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 11 to NN from one end, and the right-bank sites 11 to MM from the same end. A route joining left site aa to right site xx and a route joining left site bb to right site yy intersect exactly when at least one of these holds: a<ba < b and y<xy < x; or b<ab < a and x<yx < y; or a=ba = b and x=yx = y.

Find the maximum value of a tour.

Input

  • The first line contains three integers NN, MM, and RR (1≤N≤400001 \le N \le 40000, 1≤M≤400001 \le M \le 40000, 0≤R≤1000000 \le R \le 100000): the number of left-bank sites, the number of right-bank sites, and the number of routes.
  • Each of the next NN lines contains one integer LiL_i (0≤Li≤400000 \le L_i \le 40000): the value of the ii-th left-bank site.
  • Each of the next MM lines contains one integer RiR_i (0≤Ri≤400000 \le R_i \le 40000): the value of the ii-th right-bank site.
  • Each of the next RR lines contains two integers II and JJ (1≤I≤N1 \le I \le N, 1≤J≤M1 \le J \le M): a route between left-bank site II and right-bank site JJ.

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 11, 11, and 55, the right bank has two sites with values 22 and 22, and there are four routes. The best tour starts at left site 11, goes to right site 11, and ends at left site 33; their values 1+2+51 + 2 + 5 total 88.

Examples2

  1. Example 1

    Input
    3 2 4
    1
    1
    5
    2
    2
    1 1
    2 1
    3 1
    2 2
    
    Expected output
    8
    
  2. Example 2

    Input
    2 2 4
    10
    10
    10
    10
    1 1
    1 2
    2 1
    2 2
    
    Expected output
    40