fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define int long long
  4. #define pb push_back
  5. #define all(a) (a).begin(),(a).end()
  6. #define sz(x) (int)x.size()
  7. #define endl "\n"
  8. #define inf 1000000007
  9. #define FOR(i, a, b) for(int i=a; i<b; i++)
  10. int dp[110] = {0};
  11.  
  12. void solve(){
  13. int n, m, cost=0, rem;
  14. cin>>n>>m;
  15. vector<int> adj[n];
  16. int arr[n];
  17. FOR(i, 0, n) cin>>arr[i];
  18. sort(arr, arr+n);
  19. if(m<n || n==2){
  20. cout<<"-1"<<endl;
  21. return;
  22. }
  23. FOR(i, 1, n){
  24. cost+=(arr[i]+arr[i-1]);
  25. }
  26. cost+=(arr[0]+arr[n-1]);
  27. rem=m-(n);
  28. cost+=rem*(arr[0]+arr[1]);
  29. cout << cost << endl;
  30. int a=1;
  31. for(int i=0;i<n-1;i++){
  32. cout <<a <<" "<< a+1<< endl;
  33. a++;
  34. }
  35. cout <<"1" <<" "<< n << endl;
  36. for(int i=0;i<(m-n);i++){
  37. cout <<"1" <<" "<<"2" << endl;
  38. }
  39. }
  40. signed main(){
  41. #ifdef _DEBUG
  42. freopen("input.txt", "r", stdin);
  43. freopen("output.txt", "w", stdout);
  44. #endif
  45.  
  46. ios::sync_with_stdio(false);
  47. cin.tie(0);
  48. int t=1;
  49. cin>>t;
  50. while(t--){
  51. solve();
  52. }
  53. return 0;
  54.  
  55. }
Success #stdin #stdout 0s 4412KB
stdin
4
4 6
1 1 1 1
3 1
1 2 3
3 4
1 2 3
4 6
1 2 3 4
stdout
12
1 2
2 3
3 4
1 4
1 2
1 2
-1
15
1 2
2 3
1 3
1 2
26
1 2
2 3
3 4
1 4
1 2
1 2