JUGSANDCUPS Editorial

Problem Explanation

We are given an array of positive and negative integers, we have to divide these integers into two teams based on the absolute value of their power. And then output the absolute difference of total power of both the teams.

Approach

We use the inbuilt sort function with our comparator function which compares the absolute value of integers. Then we just put alternate values in different teams. Then output the absolute difference of sum of the power of both teams.

Code

#include <bits/stdc++.h>
using namespace std;

int main() {
    long long N;
    cin >> N;
    vector<long long> powers(N);
    for (long long i = 0; i < N; i++) {
        cin >> powers[i];
    }
    long long teamAPower = 0;
    long long teamBPower = 0;
    sort(powers.begin(), powers.end(), [](long long a, long long b) {
        return abs(a) > abs(b) || (abs(a) == abs(b) && a > b);
    });
    bool ironmanTurn = true;
    for (long long i = 0; i < N; i++) {
        if (ironmanTurn) {
            teamAPower += powers[i];
        }
        else {
            teamBPower += powers[i];
        }
        ironmanTurn = !ironmanTurn;
    }
    long long powerDifference = abs(teamAPower - teamBPower);
    cout << powerDifference << endl;
    return 0;
}