-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFridgeLockers.java
More file actions
60 lines (49 loc) · 1.51 KB
/
FridgeLockers.java
File metadata and controls
60 lines (49 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class FridgeLockers
{
public static void main( String[] args )
{
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while(t-->0)
{
int n= s.nextInt();
int m = s.nextInt();
int sum=0;
List<Vertex> a = new ArrayList<Vertex>();
for(int i=0;i<n;i++)
{
Vertex v= new Vertex();
v.weight = s.nextInt();
v.index = i+1;
sum+=v.weight;
a.add( v );
}
Collections.sort( a,(o1,o2)->o1.weight-o2.weight );
if(n<=2 || n>m)
{
System.out.println( -1 );
continue;
}
else
{
int sumOfTwoWeights = a.get( 0 ).weight+a.get(1).weight;
int minCost = 2*(sum)+(m-n)*sumOfTwoWeights;
System.out.println( minCost );
for(int i=1;i<n;i++)
System.out.println( a.get( i-1 ).index+" "+a.get( i ).index );
System.out.println( a.get( 0 ).index+" "+a.get( n-1 ).index );
for(int i=0;i<m-n;i++)
System.out.println( a.get( 0 ).index+" "+a.get( 1 ).index );
}
}
}
}
class Vertex
{
int index;
int weight;
}