Question - CodeChef: Practical coding for everyone
#include <bits/stdc++.h>
#define endl “\n”
#define ll long long int
using namespace std;
const ll M = 1000000007;
void solve()
{
ll i, j, k, n, a, b, m, c, l, r, mid, sum, f;
ll ans = 0;
cin >> n;
vector<ll> o, e;
for (i = 0; i < n; i++)
{
cin >> a;
if (i % 2 == 0)
{
o.push_back(a);
}
else
{
e.push_back(a);
}
}
sort(o.begin(), o.end(), greater<ll>());
sort(e.begin(), e.end());
for (i = 1; i < o.size(); i++)
{
o[i] += o[i - 1];
}
for (i = 0; i < e.size(); i++)
{
ans += (e[i] * o[i]);
}
for (i = 0; i < min(o.size(), e.size()); i++)
{
if (i > 0)
{
cout << (o[i] - o[i - 1]) << " " << e[i] << " ";
}
else
{
cout << o[i] << " " << e[i] << " ";
}
}
if ((n > 1) && (n % 2 != 0))
{
cout << (o[i] - o[i - 1])<<" ";
}
cout << endl;
cout << ans;
cout << endl;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen(“input.txt”, “r”, stdin);
freopen(“output.txt”, “w”, stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t;
cin >> t;
while (t--)
{
solve();
}
return 0;
}