fork 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_others = 0;
  20.  
  21. for(int i = 0; i < n; i++) {
  22. cin >> a[i].ff;
  23. a[i].ss = i + 1;
  24. }
  25.  
  26. sort(all(a));
  27.  
  28. if (m == 0) {
  29. int current_king_hp = a[n-1].ff;
  30. for(int i = 0; i < n - 1; i++) sum_others += a[i].ff;
  31.  
  32. if (sum_others < current_king_hp) {
  33. cout << -1 << endl;
  34. return;
  35. }
  36.  
  37. vector<pair<int, int>> moves;
  38. for(int i = 0; i < n - 1; i++) {
  39. if (i == n - 2) {
  40. moves.pb({a[i].ss, a[n-1].ss});
  41. }
  42. else if (current_king_hp > a[i].ff) {
  43. moves.pb({a[i].ss, a[n-1].ss});
  44. current_king_hp -= a[i].ff;
  45. }
  46. else {
  47. moves.pb({a[i].ss, a[i+1].ss});
  48. }
  49. }
  50.  
  51. cout << sz(moves) << endl;
  52. for(auto p : moves) cout << p.ff << " " << p.ss << endl;
  53. return;
  54. }
  55.  
  56.  
  57. if (n < 2 * m) {
  58. cout << -1 << endl;
  59. return;
  60. }
  61.  
  62.  
  63. vector<pair<int, int>> moves;
  64.  
  65. int trash_count = n - 2 * m;
  66. int first_target_idx = n - 2 * m;
  67.  
  68. if (trash_count > 0) {
  69. for (int i = 0; i < trash_count; i++) {
  70. if (i == trash_count - 1) {
  71. moves.pb({a[i].ss, a[first_target_idx].ss});
  72. } else {
  73. moves.pb({a[i].ss, a[i+1].ss});
  74. }
  75. }
  76. }
  77.  
  78. for (int i = 0; i < m; i++) {
  79. int target_idx = n - 2 * m + i;
  80. int survivor_idx = n - m + i;
  81.  
  82. moves.pb({a[survivor_idx].ss, a[target_idx].ss});
  83. }
  84.  
  85. cout << sz(moves) << endl;
  86. for(auto p : moves) {
  87. cout << p.ff << " " << p.ss << endl;
  88. }
  89. }
  90.  
  91. int32_t main() {
  92. fast_io;
  93. int t;
  94. cin >> t;
  95. while(t--) {
  96. solve();
  97. }
  98. return 0;
  99. }
Success #stdin #stdout 0s 5308KB
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
4 1
2 3
-1
2
1 3
2 3
2
1 2
3 2
-1
3
1 2
2 3
4 3
5
3 2
4 2
6 1
1 5
5 2