To read or not to read P47959


Statement
 

pdf   zip

The Great Masao has to read a lot to learn all he needs for his new job, so he has nn boxes (numbered 0 …n1n - 1) full of books. Every box ii is labelled with a number kik_i, which is the increment of knowlegde achieved after reading any of the books in ii. But be aware: reading some books can decrease your knowledge!

A box xx can be a prerequisite of another box yy. In this case, before Masao can read one or more books from box yy, he must have read at least one book from box xx.

Masao will do tt times:

  • Choose a box ii at random.

  • If he can read one of the books of ii (this will depend on the prerequisites) and if he decides to do so, he will read a book from ii, thus adding kik_i to his knowledge.

At every step, Masao will always take an optimal decision (to read or not to read). What is the maximum expected amount of knowledge that Masao can gain?

Input

Input is all integers, and consists of several cases. Every case begins with tt, nn, and the number of prerequisites pp. Follow the nn increments kik_i (each is an integer number with absolute value at most 10610^6). Follow pp different pairs of xx and yy (with xyx \ne y), indicating that xx is a prerequisite of yy. Books are numbered starting at zero. You can assume 0t500 \le t \le 50 and 1n101 \le n \le 10.

Output

For every case, print with two digits after the decimal point which is the maximum expected increment of knowledge of the Great Masao. The input cases have no precision issues.

Public test cases
  • Input

    1 2 0
    30 60
    
    50 10 0
    1000 1000 1000 1000 1000 1000 1000 1000 1000 1000
    
    17 6 5
    1000 -100 -100 -100 -100 -100
    1 0   2 0   3 0   4 0   5 0
    

    Output

    45.00
    50000.00
    301.23
    
  • Information
    Author
    Xavier Martínez
    Language
    English
    Official solutions
    C++
    User solutions
    C++