Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
Do not try to make every pair directly friends. The statement only asks for connectivity, so a friendship chain is enough.
If two positive grumpiness values are paired, Steve pays , and the smaller one becomes . A component containing a villager is valuable because it can connect to another component for free.
Think about the first paid operation that involves each villager. Before that first operation, their grumpiness is still exactly their original .
So the paid part is equivalent to grouping villagers into pairs costing , plus possibly one leftover singleton costing its own value. After that, all groups can be glued together using zero-grumpiness villagers.
Sort the grumpiness values. Pair the two largest together, then the next two largest, and so on. If one villager is left over, it is the smallest one. Therefore the answer is every other sorted value: positions with the same 1-indexed parity as .
The statement has one trap: Steve does not need to make every pair directly friends. He only needs the friendship graph to be connected. So we want a cheap connected graph, not a complete graph. Big difference. One is sane; the other is financial crimes.
Suppose two current grumpiness values are .
The operation costs:
and the new values become:
So every positive-positive operation creates at least one villager with grumpiness .
That matters because if two components each contain a villager, Steve can connect those two components for free: choose the two zero-grumpiness villagers, pay , and add a friendship edge.
So the real goal is:
Make every villager belong to some component that contains a zero-grumpiness villager. Then connect those components together for free.
Turning the problem into pairing
Look at any villager before their first operation. Their grumpiness has not changed yet, so it is still their original value .
For a villager's first useful operation, there are two possibilities.
If their values are and , this costs , and both villagers end up in a component containing a zero. Great: one operation handles two villagers.
That component already has a zero villager, so the cheapest way is to connect this villager to that zero. If their value is , this costs .
Using a nonzero villager from that component cannot be cheaper, because the operation would cost at least anyway. So no magic discount hiding there.
Therefore the paid operations are equivalent to this simpler problem:
There is no reason to have two singletons, because paying for two singletons is never better than pairing them for . So:
How to pair optimally
Sort the values:
The key greedy fact is: in an optimal grouping, the two largest remaining values can be paired together.
Why?
Let the largest be and the second largest be .
If is paired with some smaller value , and is paired with , then the old cost from those two groups is:
If we instead pair with , that group still costs . Then pair with , which costs at most because both are no larger than .
So the cost does not increase.
If or was the singleton in the odd case, the same swap works: make a pair, and the smaller displaced value becomes the singleton. Again, cost does not increase.
So we can always pair the two largest, remove them, and repeat.
That gives:
In other words, after sorting, sum every other element, starting from index:
Construction check
For even , pair:
Each pair costs its larger value, so the total is:
Each pair creates a zero-grumpiness villager. Then connect all those zero villagers together for free.
For odd , leave alone first, and pair:
Then connect to any zero-grumpiness villager, costing . Total:
That matches the formula.
Complexity
Sorting dominates:
per test case, with total , easily fine.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<ll> g(n);
for (ll &x : g) cin >> x;
sort(g.begin(), g.end());
ll ans = 0;
int start = (n % 2 == 0 ? 1 : 0);
for (int i = start; i < n; i += 2) {
ans += g[i];
}
cout << ans << '\n';
}
return 0;
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<ll> g(n);
for (ll &x : g) cin >> x;
sort(g.begin(), g.end());
ll ans = 0;
int start = (n % 2 == 0 ? 1 : 0);
for (int i = start; i < n; i += 2) {
ans += g[i];
}
cout << ans << '\n';
}
return 0;
}