This page is still under construction.

Parts of this page are still being built. What you see may change.

Binary Robots

Time limit1sMemory limit128 MB

Summary
Assign each chosen robot to one distinct job it can do, with at most two jobs per robot, to maximize total rental price.
Level

Medium6 of 10

Topics
Greedy, Graph, Union-find, Sorting
Solved
No attempts yet

Problem

The hottest product on the robot market right now is the "binary robot". A binary robot is always designed so that it can perform two kinds of work (for example sewing and unstitching, or eating and philosophizing), but it can never do both at the same time. Rarely, because of a hardware fault, a robot is able to perform only a single kind of work.

Bajtazar runs a company that rents out binary robots. He owns nn robots; each robot ii has a fixed set of jobs it can perform and a rental price wiw_i. Bajtazar has received mm rental requests, each for a different job. When a robot is rented out it takes on exactly one of the jobs it is able to do, and each job (request) may be assigned to at most one robot. Bajtazar does not have to rent out every robot, nor accept every request.

Every robot that is rented out earns its rental price. Write a program that computes the maximum total revenue Bajtazar can earn.

Input

The first line contains three integers nn, mm, qq (1≤n,m≤1 000 0001 \le n, m \le 1\,000\,000, 0≤q≤2n0 \le q \le 2n): the number of robots, the number of jobs (requests) to be done, and the total number of robot skills, respectively. Robots are numbered from 11 to nn and jobs from 11 to mm.

The second line contains nn integers w1,w2,…,wnw_1, w_2, \dots, w_n (1≤wi≤1 000 000 0001 \le w_i \le 1\,000\,000\,000), the rental price of each robot.

Each of the following qq lines contains two integers aia_i, bib_i (1≤ai≤n1 \le a_i \le n, 1≤bi≤m1 \le b_i \le m), meaning that robot aia_i can perform job bib_i. No pair (ai,bi)(a_i, b_i) appears more than once. Moreover, for every x=1,2,…,nx = 1, 2, \dots, n a pair of the form (x,y)(x, y) appears exactly once or twice; that is, every robot can perform one or two jobs.

Output

Print a single integer: the maximum total revenue Bajtazar can earn.

Examples1

  1. Example 1

    Input
    3 2 4
    3 1 4
    1 1
    2 1
    2 2
    3 2
    
    Expected output
    7