Advertisement
amine99

Untitled

Nov 19th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int n, m, t, a[2005];
  5. pair<int, int> b[2005];
  6.  
  7. int main() {
  8. cin >> t;
  9. while(t--) {
  10. cin >> n >> m;
  11. for(int i = 0; i < n; i++) {
  12. cin >> a[i];
  13. b[i] = {a[i], i + 1};
  14. }
  15. m -= (n * (n - 1)) / 2;
  16. if(m < 0) {
  17. cout << -1 << "\n";
  18. continue;
  19. }
  20. int cost = 0;
  21. vector<pair<int,int>> v;
  22. for(int i = 1; i <= n; i++) {
  23. for(int j = i + 1; j <= n; j++) {
  24. if(i == j) continue;
  25. cost += a[j] + a[i];
  26. v.push_back({i , j});
  27. }
  28. }
  29. sort(b, b + n);
  30. while(m--) {
  31. v.push_back({b[0].second , b[1].second});
  32. cost += b[0].first + b[1].first;
  33. }
  34. cout << cost << "\n";
  35. for(auto p : v)
  36. cout << p.first << " " << p.second << "\n";
  37. }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement