Advertisement
fahad005

Untitled

Feb 13th, 2022
1,056
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. //
  4. #define ll long long
  5. #define ull unsigned long long
  6. #define mx 100010
  7. #define mod 1000000007
  8. #define inf INT_MAX
  9. #define pi acos(-1.0)
  10. #define endl '\n'
  11. #define pb push_back
  12. #define pll pair<ll, ll>
  13. #define vll vector<ll>
  14. #define vpll vector<pll>
  15. #define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
  16. //
  17. int main() {
  18.     ll n;
  19.     cin >> n;
  20.     ll ar[n];
  21.     for (ll i = 0; i < n; i++) cin >> ar[i];
  22.     sort(ar, ar + n);
  23.     stack<ll> st, st1;
  24.     ll sum = 0, sum1 = 0, cnt = 0, cnt1 = 0;
  25.     for (ll i = n - 1; i >= 0; i--) {
  26.         if (cnt < n / 2 && cnt1 < n / 2) {
  27.             if (sum <= sum1) {
  28.                 st.push(ar[i]);
  29.                 cnt++;
  30.                 sum += ar[i];
  31.             }
  32.             else {
  33.                 st1.push(ar[i]);
  34.                 cnt1++;
  35.                 sum1 += ar[i];
  36.             }
  37.         }
  38.         else if (cnt == n / 2) st1.push(ar[i]);
  39.         else st.push(ar[i]);
  40.     }
  41.     while (!st.empty()) {
  42.         cout << st.top() << " ";
  43.         st.pop();
  44.     }
  45.     cout << endl;
  46.     while (!st1.empty()) {
  47.         cout << st1.top() << " ";
  48.         st1.pop();
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement