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.
You can only decrease numbers. So any original value that ends up assigned to some target value must be at least that target value. No magic upward movement, Papyrus is not that generous.
Think of the reorder operation as choosing a matching between elements of and positions of . After that, each matched pair contributes decrement operations.
If a valid matching exists, the total decrement cost is always no matter which valid matching you used. The matching only decides feasibility, not the decrement count.
There are only two meaningful cases: do not reorder at all, or reorder once. Multiple reorders collapse into one final permutation, so paying more than once is just lighting seconds on fire.
To test whether some reorder can work, sort both arrays increasingly and check for every . If the sorted version works, one reorder plus the total decrement difference is enough.
Key Observation
Each element of can only go down. So if some value from is assigned to become some value in , we need , and the cost for that pair is .
The reorder operation lets us choose which original elements of go to which positions of . Since one reorder can produce any permutation, using more than one reorder is never useful. Two arbitrary shuffles still just mean “some final shuffle,” except now you paid extra for the privilege. Brutal deal.
So the whole problem has only two real possibilities:
Case 1: No Reorder
If we do not reorder, position in must become position in .
That is possible exactly when
for every .
If it is possible, the cost is
Call this value .
Case 2: Use One Reorder
Now we may match elements of to elements of however we want. A matching is valid if every chosen source value is at least its target value.
Here is the sneaky part: if a valid matching exists, the decrement cost does not depend on which matching we picked.
Every element of is used exactly once, and every element of is produced exactly once, so the total number of decrements is always
So optimization is not about minimizing decrements. That part is fixed. The only question is whether a valid matching exists.
To test that, sort both arrays increasingly:
Then a valid matching exists exactly when
for every .
Why? Match the smallest available target with the smallest available source. If the smallest source cannot cover the smallest target, it cannot cover anything, so the whole thing is impossible. If it can, pairing them is safe, and the same logic repeats. This greedy matching is the classic sorted-pairing check.
If this sorted check passes, the cost with reorder is
Putting It Together
Let
Technically, you can compute both candidates and take the minimum, but since , if the original order works, it is always better than paying for a reorder.
Complexity
Sorting dominates:
per test case.
With , this is basically instant. The CPU barely wakes up.
#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, c;
cin >> n >> c;
vector<int> a(n), b(n);
ll diff = 0;
for (int &x : a) {
cin >> x;
diff += x;
}
for (int &x : b) {
cin >> x;
diff -= x;
}
bool direct = true;
for (int i = 0; i < n; i++) {
if (a[i] < b[i]) direct = false;
}
if (direct) {
cout << diff << '\n';
continue;
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
bool canReorder = true;
for (int i = 0; i < n; i++) {
if (a[i] < b[i]) canReorder = false;
}
if (!canReorder) cout << -1 << '\n';
else cout << diff + c << '\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, c;
cin >> n >> c;
vector<int> a(n), b(n);
ll diff = 0;
for (int &x : a) {
cin >> x;
diff += x;
}
for (int &x : b) {
cin >> x;
diff -= x;
}
bool direct = true;
for (int i = 0; i < n; i++) {
if (a[i] < b[i]) direct = false;
}
if (direct) {
cout << diff << '\n';
continue;
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
bool canReorder = true;
for (int i = 0; i < n; i++) {
if (a[i] < b[i]) canReorder = false;
}
if (!canReorder) cout << -1 << '\n';
else cout << diff + c << '\n';
}
return 0;
}