tuki2501

set-subtraction.cpp

Jan 10th, 2022
842
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long ll;
  5.  
  6. signed main() {
  7.   cin.tie(0)->sync_with_stdio(0);
  8.   int n; cin >> n;
  9.   vector<int> a(n * 2);
  10.   for (auto &i : a) cin >> i;
  11.   sort(a.begin(), a.end());
  12.   for (int i = 0; i + 1 < n * 2; i++) {
  13.     int d = a.back() - a[i];
  14.     multiset<int> s(a.begin(), a.end());
  15.     vector<int> res;
  16.     bool flag = false;
  17.     for (int i = n * 2 - 1; i > 0; i--) {
  18.       auto it = s.find(a[i]);
  19.       if (it == s.end()) continue;
  20.       res.push_back(a[i]);
  21.       s.erase(it);
  22.       it = s.find(a[i] - d);
  23.       if (it == s.end()) {
  24.         flag = true;
  25.         break;
  26.       }
  27.       s.erase(it);
  28.     }
  29.     if (flag || s.size()) continue;
  30.     cout << d << '\n';
  31.     for (auto &i : res) cout << i << ' ';
  32.     cout << '\n';
  33.     return 0;
  34.   }
  35.   cout << -1 << '\n';
  36. }
  37.  
Advertisement
Add Comment
Please, Sign In to add comment