Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- //
- #define ll long long
- #define ull unsigned long long
- #define mx 100010
- #define mod 1000000007
- #define inf INT_MAX
- #define pi acos(-1.0)
- #define endl '\n'
- #define pb push_back
- #define pll pair<ll, ll>
- #define vll vector<ll>
- #define vpll vector<pll>
- #define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
- //
- int main() {
- ll n;
- cin >> n;
- ll ar[n];
- for (ll i = 0; i < n; i++) cin >> ar[i];
- sort(ar, ar + n);
- stack<ll> st, st1;
- ll sum = 0, sum1 = 0, cnt = 0, cnt1 = 0;
- for (ll i = n - 1; i >= 0; i--) {
- if (cnt < n / 2 && cnt1 < n / 2) {
- if (sum <= sum1) {
- st.push(ar[i]);
- cnt++;
- sum += ar[i];
- }
- else {
- st1.push(ar[i]);
- cnt1++;
- sum1 += ar[i];
- }
- }
- else if (cnt == n / 2) st1.push(ar[i]);
- else st.push(ar[i]);
- }
- while (!st.empty()) {
- cout << st.top() << " ";
- st.pop();
- }
- cout << endl;
- while (!st1.empty()) {
- cout << st1.top() << " ";
- st1.pop();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement