fork download
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. int main() {
  6. // your code goes here
  7. int t;cin>>t;
  8. while(t--){
  9. int n,m;cin>>n>>m;
  10. int count=0;
  11. vector <pair<int,int>> arr(n);
  12. for(int i=0;i<n;i++) {cin>>arr[i].first;
  13. arr[i].second=i+1;
  14. }
  15. sort(arr.begin(),arr.end());
  16. if(m<n) cout<<"-1"<<endl;
  17. else{
  18. int tep=m;
  19. for(int i=0;i<n-1;i++){
  20. count=count+(arr[i].first+arr[i+1].first);
  21. // cout<<i+1<<" "<<i+2<<endl;
  22. m--;
  23. }
  24. count=count+(arr[0].first+arr[n-1].first);
  25. //cout<<1<<" "<<n-1<<endl;
  26. m--;
  27. //cout<<count<<endl;
  28. if(m>0){
  29. count=count+(m*(arr[0].first+arr[1].first));
  30. // cout<<1<<" "<<2<<endl;
  31. }
  32. cout<<count<<endl;
  33. for(int i=0;i<n-1;i++){
  34. // count=count+(arr[i].first+arr[i+1].first);
  35. cout<<arr[i].second<<" "<<arr[i+1].second<<endl;
  36. tep--;
  37. }
  38. //count=count+(arr[0].first+arr[n-1].first);
  39. cout<<arr[0].second<<" "<<arr[n-1].second<<endl;
  40. tep--;
  41. //cout<<count<<endl;
  42. while(tep>0){
  43. // count=count+(m*(arr[0].first+arr[1].first));
  44. tep--;
  45. cout<<arr[0].second<<" "<<arr[1].second<<endl;
  46. }
  47. }
  48. }
  49. return 0;
  50. }
Success #stdin #stdout 0s 4560KB
stdin
3
4 6
1 1 1 1
3 1
1 2 3
3 4
1 2 3
stdout
12
1 2
2 3
3 4
1 4
1 2
1 2
-1
15
1 2
2 3
1 3
1 2