fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <set>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. vector<int> check(int n, vector<int> a, int x)
  9. {
  10. multiset<int> s;
  11. for (auto e : a)
  12. s.insert(e);
  13. vector<int> res;
  14. for (int i = 0; i < n; i++)
  15. {
  16. auto it1 = s.end();
  17. it1--;
  18. int y = x - *it1;
  19. // s.erase(it1); Original Position in Igorl solution
  20. auto it2 = s.find(y);
  21. s.erase(it1); // Shifted Position
  22. if (it2 == s.end())
  23. return {};
  24. res.push_back(x - y);
  25. res.push_back(y);
  26. x = max(x - y, y);
  27. s.erase(it2);
  28. }
  29. return res;
  30. }
  31.  
  32. void solve()
  33. {
  34. int n;
  35. cin >> n;
  36. vector<int> a(2 * n);
  37. for (int i = 0; i < 2 * n; i++)
  38. cin >> a[i];
  39. sort(a.begin(), a.end());
  40. for (int i = 0; i < 2 * n - 1; i++)
  41. {
  42. int x = a[i] + a[2 * n - 1];
  43. vector<int> res = check(n, a, x);
  44. if (res.size())
  45. {
  46. cout << "YES\n";
  47. cout << x << "\n";
  48. for (int j = 0; j < n; j++)
  49. {
  50. cout << res[2 * j] << " " << res[2 * j + 1] << "\n";
  51. }
  52. return;
  53. }
  54. }
  55. cout << "NO\n";
  56. }
  57.  
  58. int main()
  59. {
  60. int t;
  61. cin >> t;
  62. for (int c = 0; c < t; c++)
  63. {
  64. solve();
  65. }
  66. }
Runtime error #stdin #stdout 0s 4820KB
stdin
4
2
3 5 1 2
3
1 1 8 8 64 64
2
1 1 2 4
5
1 2 3 4 5 6 7 14 3 11
stdout
YES
6
5 1
3 2
NO