fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. // Speed
  5. #define fast_io ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
  6. // Typedefs
  7. #define int long long
  8. #define pb push_back
  9. #define ff first
  10. #define ss second
  11. #define all(x) (x).begin(), (x).end()
  12. #define sz(x) ((int)(x).size())
  13. #define endl '\n'
  14.  
  15. void solve() {
  16. int n, m;
  17. cin >> n >> m;
  18. vector<pair<int, int>> a(n);
  19. int sum_all = 0;
  20. for(int i = 0; i < n; i++) {
  21. cin >> a[i].ff;
  22. a[i].ss = i + 1; // Store 1-based index
  23. sum_all += a[i].ff;
  24. }
  25.  
  26. sort(all(a));
  27.  
  28. // Case 1: Target is 0 survivors
  29. if (m == 0) {
  30. // The strongest elf (a[n-1]) must take damage >= their health.
  31. // Damage comes from all other n-1 elves attacking them.
  32. int damage_avail = sum_all - a[n-1].ff;
  33.  
  34. if (damage_avail >= a[n-1].ff) {
  35. cout << n - 1 << endl;
  36. // Everyone attacks the King
  37. for(int i = 0; i < n - 1; i++) {
  38. cout << a[i].ss << " " << a[n-1].ss << endl;
  39. }
  40. } else {
  41. cout << -1 << endl;
  42. }
  43. return;
  44. }
  45.  
  46.  
  47. if (n == m) {
  48. cout << 0 << endl;
  49. return;
  50. }
  51.  
  52. vector<pair<int, int>> moves;
  53.  
  54. for (int i = 0; i < n - m - 1; i++) {
  55. moves.pb({a[i].ss, a[i+1].ss});
  56. }
  57.  
  58. moves.pb({a[n-m-1].ss, a[n-1].ss});
  59.  
  60. cout << sz(moves) << endl;
  61. for(auto p : moves) {
  62. cout << p.ff << " " << p.ss << endl;
  63. }
  64. }
  65.  
  66. int32_t main() {
  67. fast_io;
  68. int t;
  69. cin >> t;
  70. while(t--) {
  71. solve();
  72. }
  73. return 0;
  74. }
Success #stdin #stdout 0.01s 5280KB
stdin
7
4 2
1 4 2 3
2 2
6 7
3 0
1 2 3
3 1
1 2 3
3 2
1 2 3
4 1
2 3 4 5
6 0
998244353 1000000000 314159265 676767677 999999999 987654321
stdout
2
1 3
3 2
0
2
1 3
2 3
2
1 2
2 3
1
1 3
3
1 2
2 3
3 4
5
3 2
4 2
6 2
1 2
5 2