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.
Take two values . Deleting them separately gives , while deleting them together gives . So pairing them changes the score by . Which element's contribution effectively disappeared?
Define . A singleton contributes . In a pair, the larger element contributes its usual , while the smaller element contributes . Thus every pair lets you replace one element's contribution from by .
There can be at most pairs, so at most contributions can be erased. Erasing is useful only when , and among all candidates you obviously want the smallest values.
Adjacency does not ruin the greedy choice. Mark the chosen smallest elements as discarded and everything else as kept. There are at least as many kept elements as discarded ones, and every kept value is at least every discarded value. While a discarded element remains, find any adjacent boundary between the two labels and pair those elements; removing one of each preserves the invariant.
Sort , let , and set . The answer is
In code, clamp the first shifted values to zero and sum everything. Complexity: .
The key is to stop thinking about a long sequence of deletions and instead ask what one pair operation actually does to the contributions of its two elements.
For every element, define
If is removed alone, its contribution is exactly .
Now consider a pair with values . Removing it contributes
We can rewrite this as
So the larger element contributes exactly what it would have contributed as a singleton, while the smaller element contributes zero.
Equivalently, compared with deleting both separately,
Pairing therefore erases the shifted contribution of the smaller element. It helps precisely when , because then that erased contribution was negative.
Suppose a complete strategy uses pair operations. Choose one smaller element from every pair, breaking ties arbitrarily, and call these elements discarded. Every other element is kept.
The final score is
where is the set of discarded elements and .
Each pair consumes two elements, so
If we temporarily ignore adjacency and the requirement that a discarded element needs a larger partner, the best choice is obvious:
Sort the elements so that
Subtracting the same from every value does not change their order, so sorting either or is equivalent. The resulting upper bound is
The first contributions are allowed to become zero when negative; all remaining contributions stay as they are.
Sorting is only used to identify which values we want to discard. We are not claiming that the operation can reorder the array. That would be rather illegal.
Let contain the negative values among the first sorted elements. Suppose . These are the globally smallest values, and . Let contain all other elements. Then:
Return to the elements' original order and label each one either D or K. While a D remains, a K also remains because there are at least as many K elements. Any linear sequence containing both labels has some adjacent pair with different labels. Remove that adjacent D-K pair.
The K value is at least the D value, so the pair contributes the shifted value of K, while D contributes zero, exactly as desired. Removing one element of each type preserves the inequality between their remaining counts. Repeat until no D remains, then remove every remaining K as a singleton.
Thus the supposedly relaxed upper bound is actually achievable. The changing adjacency created by concatenation is exactly what makes this work.
Lemma 1. In any strategy, every pair operation replaces the shifted contribution of one of its smaller elements by zero, while all other elements contribute .
Proof. For a pair , its contribution is . A singleton contributes its own value minus . Summing this identity over all operations gives the claim.
Lemma 2. No strategy scores more than
where the are sorted and .
Proof. By Lemma 1, a strategy can erase at most one shifted contribution per pair, hence at most contributions total. Among at most values, erasing the smallest negative ones gives the largest possible improvement. This is exactly what the formula for does. Any actual pairing restrictions can only make this relaxed optimum smaller, so every strategy is bounded by .
Lemma 3. There exists a valid sequence of operations scoring exactly .
Proof. Mark the negative values among the first sorted elements as discarded. They form a prefix of the sorted values, so every unmarked value is at least every marked value. There are at most marked elements and therefore at least as many unmarked elements.
As long as a marked element remains, choose an adjacent boundary between a marked and an unmarked element and remove that pair. The unmarked value is the maximum and contributes its ; the marked value contributes zero. Eventually every marked element is removed this way. Remove all remaining unmarked elements singly. The resulting contribution is zero for exactly the selected negative values and for every other value, totaling .
Theorem. The algorithm outputs the maximum possible final score.
Proof. Lemma 2 proves that the algorithm's value is an upper bound on every strategy. Lemma 3 constructs a valid strategy attaining that value. Therefore it is optimal.
Sorting costs time, and the final scan costs . The array uses memory. All arithmetic must use 64-bit integers: the answer can have magnitude around .
For example, with and , the sorted shifted values are . Since , only one negative contribution can be erased, giving . You cannot erase both negatives with one winner; pairs are useful, not magical.
#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;
ll c;
cin >> n >> c;
vector<ll> a(n);
for (ll &x : a) cin >> x;
sort(a.begin(), a.end());
ll answer = 0;
for (int i = 0; i < n; ++i) {
ll contribution = a[i] - c;
if (i < n / 2) contribution = max(0LL, contribution);
answer += contribution;
}
cout << answer << '\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;
ll c;
cin >> n >> c;
vector<ll> a(n);
for (ll &x : a) cin >> x;
sort(a.begin(), a.end());
ll answer = 0;
for (int i = 0; i < n; ++i) {
ll contribution = a[i] - c;
if (i < n / 2) contribution = max(0LL, contribution);
answer += contribution;
}
cout << answer << '\n';
}
return 0;
}