This page is still under construction.

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

ICPC Kingdom

Time limit2sMemory limit1024 MB

Summary
Pick exactly k roads that form no cycle and can each be assigned to a different worker, maximizing the sum of floor(sqrt(a_u + a_v)) for each k.
Level

Hard8 of 10

Topics
Graph, Shortest path, Union-find, Greedy
Solved
No attempts yet

Problem

The ICPC kingdom has nn cities numbered from 11 to nn, and mm roads numbered from 11 to mm that connect these cities. Each road connects two cities, and people can travel along the roads in both directions. City ii has aia_i residents. Road jj connects city uju_j and city vjv_j. The economic benefit of road jj is ⌊auj+avj⌋\left\lfloor \sqrt{a_{u_j} + a_{v_j}} \right\rfloor.

One day, an enemy invaded the ICPC kingdom and destroyed all roads. Fortunately, the ICPC troops defeated the enemy and stopped the invasion. To recover from the war, the king of ICPC hired ww workers to fix the roads. Each worker can fix at most one road, and the ii-th worker can only fix one of the roads whose indices are among b1,b2,…,bxib_1, b_2, \dots, b_{x_i}.

The king does not want the repair plan to contain any useless road. A plan contains a useless road if there exist cities pp and qq with more than one simple path between them. A simple path is a sequence of distinct roads c1,c2,…,czc_1, c_2, \dots, c_z such that a trip along c1,c2,…,czc_1, c_2, \dots, c_z visits exactly z+1z + 1 distinct cities.

The king asks you to calculate the maximum economic benefit when exactly kk roads are fixed without useless roads. Calculate it for each kk from 11 to n−1n - 1.

Input

The first line contains nn and mm separated by a space. nn is the number of cities, and mm is the number of roads. The second line contains a1,a2,…,ana_1, a_2, \dots, a_n, the number of residents in cities 1,2,…,n1, 2, \dots, n. The next mm lines each contain uju_j and vjv_j, meaning road jj connects cities uju_j and vjv_j. The next line contains ww, the number of workers. The following ww lines describe the roads each worker can fix. The (3+i)(3 + i)-th line starts with xix_i, the number of roads the ii-th worker can fix, followed by xix_i distinct integers b1,b2,…,bxib_1, b_2, \dots, b_{x_i}. The ii-th worker can fix only one of these roads.

Output

Print n−1n - 1 numbers. The ii-th number is the maximum economic benefit when the repair plan fixes exactly ii roads without useless roads. If no such plan exists, print -1 for that ii.

Constraints

  • 1≤n≤1001 \le n \le 100
  • 0≤m≤1000 \le m \le 100
  • 1≤ai≤1091 \le a_i \le 10^9
  • 1≤ui,vi≤n1 \le u_i, v_i \le n
  • ui≠viu_i \ne v_i
  • 0≤w≤1000 \le w \le 100

Examples1

  1. Example 1

    Input
    5 4
    1 2 2 1 4
    1 2
    2 3
    3 1
    4 1
    3
    2 2 4
    3 1 2 3
    2 1 3
    
    Expected output
    2 3 4 -1