Fridge Lockers (Codeforces 1255B)
Tags: Implement, graphs

Hanh lives in a shared apartment. There are $$$n$$$ people (including Hanh) living there, each has a private fridge. $$$n$$$ fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of $$$n$$$ people can open it.  For exampe, in the picture there are $$$n=4$$$ people and $$$5$$$ chains. The first person knows passcodes of two chains: $$$1-4$$$ and $$$1-2$$$. The fridge $$$1$$$ can be open by its owner (the person $$$1$$$), also two people $$$2$$$ and $$$4$$$ (acting together) can open it. The weights of these fridges are $$$a_1, a_2, \ldots, a_n$$$. To make a steel chain connecting fridges $$$u$$$ and $$$v$$$, you have to pay $$$a_u + a_v$$$ dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges. Hanh's apartment landlord asks you to create exactly $$$m$$$ steel chains so that all fridges are private. A fridge is private if and only if, among $$$n$$$ people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge $$$i$$$ is not private if there exists the person $$$j$$$ ($$$i \ne j$$$) that the person $$$j$$$ can open the fridge $$$i$$$.For example, in the picture all the fridges are private. On the other hand, if there are $$$n=2$$$ fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly $$$m$$$ chains, and if yes, output any solution that minimizes the total cost. 

Input: Each test contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \le T \le 10$$$). Then the descriptions of the test cases follow.The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \le n \le 1000$$$, $$$1 \le m \le n$$$) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively.The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^4$$$) — weights of all fridges.

Output: For each test case:  If there is no solution, print a single integer $$$-1$$$.  Otherwise, print a single integer $$$c$$$ — the minimum total cost. The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$), meaning that the $$$i$$$-th steel chain connects fridges $$$u_i$$$ and $$$v_i$$$. An arbitrary number of chains can be between a pair of fridges. If there are multiple answers, print any.


Sample Inputs:
3
4 4
1 1 1 1
3 1
1 2 3
3 3
1 2 3

Sample Outputs:
8
1 2
4 3
3 2
4 1
-1
12
3 2
1 2
3 1

Solution (Python):
for _ in range(int(input())):
    (n, m) = map(int, input().split())
    l = list(map(int, input().split()))
    if n < 3 or m < n:
        print(-1)
    else:
        (i, i1) = (-1, float('inf'))
        s = 0
        for x in range(n):
            if l[x] < i1:
                i1 = l[x]
                i = x
            s += l[x]
        (j, j1) = (-1, float('inf'))
        for x in range(n):
            if x != i and l[x] < j1:
                j1 = l[x]
                j = x
        print(2 * s + (m - n) * (i1 + j1))
        for x in range(n - 1):
            print(x + 1, x + 2)
        print(n, 1)
        for x in range(m - n):
            print(i + 1, j + 1)

Solution (Python):
t = int(input())
for o in range(t):
    (n, k) = list(map(int, input().split()))
    a = list(map(int, input().split()))
    d = list(sorted(a))
    min1 = a.index(d[0])
    min2 = a.index(d[1])
    if min1 == min2:
        min2 += 1
    res1 = []
    if n > k or n == 2:
        print(-1)
    else:
        for i in range(n):
            if i + 2 > n:
                res1.append(str(i + 1) + ' ' + str(1))
            else:
                res1.append(str(i + 1) + ' ' + str(i + 2))
        res = 2 * sum(a)
        while k > n:
            res1.append(str(min1 + 1) + ' ' + str(min2 + 1))
            res += d[0] + d[1]
            k -= 1
        print(res)
        for i in range(len(res1)):
            print(res1[i])

Solution (Python):
t = int(input())
for _ in range(t):
    (n, m) = map(int, input().split())
    lst = list(map(int, input().split()))
    if n == 2 or n > m:
        print(-1)
    else:
        print(sum(lst) * 2)
        for i in range(1, m):
            print(i, i + 1)
        print(1, n)