A field technician logs two aligned lists of sensor readings for n sensors, indexed 0 to n-1: raw[i] is a sensor's uncorrected reading, and target[i] is the reading it should have after a full recalibration. The current mismatch score is the sum, over all sensors, of |raw[i] - target[i]|. Before filing the report, the technician may perform at most one quick fix: pick a single index i and overwrite raw[i] with the value taken from raw[j] for some other index j (any value already present at another position in the original raw list may be reused this way, but at most one position may be changed, and the replacement value must come from the original list, not be invented). Determine the smallest mismatch score achievable — whether by making one such substitution or by making none at all — and print it modulo 1,000,000,007.
n, the number of sensors.n space-separated integers raw[0], ..., raw[n-1].n space-separated integers target[0], ..., target[n-1].Print a single integer: the minimum achievable mismatch score, taken modulo 1,000,000,007.
Example 1
Input
4 2 4 6 8 3 8 6 1
Expected
6
Explanation
With raw = [2, 4, 6, 8] and target = [3, 8, 6, 1], the baseline mismatch is |2-3| + |4-8| + |6-6| + |8-1| = 1 + 4 + 0 + 7 = 12. Overwriting raw[3] (currently 8) with the value 2 taken from raw[0] changes that term from |8-1|=7 to |2-1|=1, a reduction of 6 — the largest reduction available at any position. The new total is 12 - 6 = 6, and 6 mod 1,000,000,007 is 6.
Example 2
Input
3 10 20 30 25 25 25
Expected
15
Explanation
With raw = [10, 20, 30] and target = [25, 25, 25], the baseline mismatch is |10-25| + |20-25| + |30-25| = 15 + 5 + 5 = 25. Overwriting raw[0] (currently 10) with either 20 or 30 (both already present at other positions) drops its term from 15 down to 5, a reduction of 10 — the largest reduction available anywhere, since positions 1 and 2 are already as close as any other available value allows. The new total is 25 - 10 = 15.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →