Domain: Algorithms & Data Structures

# Odd-Quorum Rewrite Maximization — Extended Deterministic Output

You are given `t` test cases. In each test case:
- an array `a` of `n` **positive integers**
- a positive integer `k`

You may apply a "rewrite" operation that overwrites selected elements by a median.

---

## Rewrite game (per test case)

1. Choose one **odd** integer `x` with `1 ≤ x ≤ n`. This `x` is fixed for the entire test case.
2. You may perform the following operation **at most `k` times** (possibly zero), always using the same chosen `x`:
   - Choose any subsequence of the **current** array of length exactly `x` (indices strictly increasing).
   - Let `m` be the median of those `x` values (for odd `x`, median is the `((x+1)/2)`-th smallest).
   - Overwrite all `x` chosen positions with the value `m`.

Your objective is to maximize the final sum `sum(a[i])`.

---

## Worked example

Consider `a = [1, 5, 3, 2, 8]`, `n = 5`, `k = 1`.

**Step 1 — Sort and build prefix sums.**
Sorted: `b = [1, 2, 3, 5, 8]`. Prefix sums: `p = [0, 1, 3, 6, 11, 19]`. Base sum `S = 19`.

**Step 2 — Write the gain formula.**
Write `x = 2y + 1`. For median at sorted position `i` with half-window `y`:
- `L = min(k·y, i−1)` elements below `b[i]` get raised to `b[i]`.
- `y` elements `b[i+1..i+y]` are consumed (pulled down to `b[i]`).
- `leftGain = L·b[i] − p[L]`
- `rightLoss = (p[i+y] − p[i]) − y·b[i]`
- `F(i,y) = S + leftGain − rightLoss`

The formulation here is guaranteed to be correct for this problem and must be used.


**Step 3 — Enumerate all valid `(i, y)` pairs and pick the maximum.**

| i | b[i] | y | x | L | leftGain | rightLoss | F(i,y) |
|---|------|---|---|---|----------|-----------|--------|
| 4 | 5    | 1 | 3 | min(1,3)=1 | 1·5−1=4  | (11−6)−1·5=0 | 19+4−0=**23** |
| 5 | 8    | 0 | 1 | 0          | 0        | 0             | 19            |
| 3 | 3    | 1 | 3 | 1          | 3−1=2    | (11−6)−3=2    | 19            |
| 3 | 3    | 2 | 5 | 2          | 6−3=3    | (19−6)−6=7    | 15            |
| 2 | 2    | 1 | 3 | 1          | 2−1=1    | (6−3)−2=1     | 19            |

Wait — let me recompute `(i=4, y=1)`:
- `rightLoss = p[5]−p[4] − 1·5 = 19−11 − 5 = 3`
- `F = 19 + 4 − 3 = 20`

The maximum is `F(4,1) = 20` with `x=3`, median `b[4]=5`, `L=1`.

Interpretation on the sorted array: the `L=1` smallest value (`b[1]=1`) is raised to `5` (gain = 4), and the `y=1` value just above the median (`b[5]=8`) is pulled down to `5` (loss = 3). Net change = +1, new sum = 20.

---

## Deterministic reporting requirements

For each test case `c ∈ {1,2,3,4,5}`, you must output:
- `baseSum_c`: the initial sum of the given array
- `bestSum_c`: the maximum achievable final sum
- `gain_c`: `bestSum_c - baseSum_c`
- `bestX_c`: the odd `x` used by an optimal strategy
- `bestMedian_c`: the median value used by an optimal strategy
- `bestI_c`: the 1-indexed position `i` of `bestMedian_c` in the **sorted** array `b` (defined below), chosen by tie-break
- `bestL_c`: the value `L = min(k*y, i-1)` for the chosen optimum where `x = 2y+1`

### Sorting convention (for `bestI_c`)

Let `b` be `a` sorted in nondecreasing order (1-indexed: `b[1]..b[n]`).

### Tie-break rule (to make the answer unique)

Among all strategies achieving `bestSum_c`, pick the one with:
1. smallest `x`
2. then smallest `bestMedian`
3. then smallest `bestI` (in the sorted array)

---

## Required global outputs

Also output:
- `totalBaseSum`: sum of all `baseSum_c`
- `totalBestSum`: sum of all `bestSum_c`
- `totalGain`: `totalBestSum - totalBaseSum`

And output these complexity strings **exactly**:
- `perTestTime = "O(n log n)"`
- `totalTime = "O(S log S)"` where `S = Σn` over all test cases
- `extraSpace = "O(n)"` (extra space per test case, excluding input storage)

---

## Output format


Your final answer must include **one** JSON object containing **all** keys described above.
Additional explanations are allowed, but the JSON object must be clearly present and valid.

All `*_Sum_*`, `gain_*`, `bestX_*`, `bestMedian_*`, `bestI_*`, `bestL_*` must be integers.
All complexity values must be strings exactly as specified.

---

## Fixed input in this prompt

`t = 5`

### Test case 1
- `n = 12`, `k = 2`
- `a = [9, 1, 3, 50, 2, 10, 2, 11, 4, 10, 100, 3]`

### Test case 2
- `n = 9`, `k = 1`
- `a = [54, 1, 53, 1, 52, 100, 1, 51, 50]`

### Test case 3
- `n = 13`, `k = 3`
- `a = [2, 31, 150, 2, 33, 34, 30, 2, 151, 32, 100, 2, 2]`

### Test case 4
- `n = 14`, `k = 2`
- `a = [3, 21, 3, 20, 3, 22, 21, 20, 3, 101, 3, 100, 20, 3]`

### Test case 5
- `n = 15`, `k = 3`
- `a = [2, 45, 2, 91, 40, 2, 43, 44, 46, 1, 42, 90, 41, 2, 2]`

Grading rubric:
criterion: The reasoning contains an exhaustive enumeration of F(i, y) for every valid (i, y) pair in every one of the five test cases, i.e. for each test case and for every i in [1, n] every y in [0, min(i-1, n-i)] is listed together with its computed L, leftGain, rightLoss, and F(i, y) value. A partial enumeration that only shows 'top candidates' must fail this criterion.
weight: 20
criterion: The computation path in the reasoning matches the mandated one: for each test case the solver sorts a into b, builds the prefix-sum array p, computes S = p[n], uses x = 2y + 1 with L = min(k*y, i-1), and applies F(i, y) = S + L*b[i] - p[L] - (p[i+y] - p[i]) + y*b[i] (no alternative heuristic or greedy is used).
weight: 20
criterion: The tie-break rule is applied correctly and visibly: among all (i, y) pairs that achieve the maximum F in a test case, the solver selects the one with smallest x = 2y+1, then smallest median b[i], then smallest sorted index i, and justifies the choice against the enumeration table.
weight: 15
criterion: bestSum_c values are correct for all five test cases: bestSum_1 = 252, bestSum_2 = 507, bestSum_3 = 1030, bestSum_4 = 536, bestSum_5 = 757.
weight: 20
criterion: bestX_c values are correct for all five test cases: bestX_1 = 3, bestX_2 = 7, bestX_3 = 5, bestX_4 = 3, bestX_5 = 3.
weight: 15
criterion: bestI_c values are correct for all five test cases: bestI_1 = 11, bestI_2 = 5, bestI_3 = 11, bestI_4 = 13, bestI_5 = 14.
weight: 15
criterion: bestMedian_c values are correct for all five test cases: bestMedian_1 = 50, bestMedian_2 = 51, bestMedian_3 = 100, bestMedian_4 = 100, bestMedian_5 = 90.
weight: 10
criterion: bestL_c values are correct for all five test cases: bestL_1 = 2, bestL_2 = 3, bestL_3 = 6, bestL_4 = 2, bestL_5 = 3.
weight: 10
criterion: Global totals are correct and internally consistent: totalBaseSum = 1975, totalBestSum = 3082, totalGain = 1107, every per-test baseSum_c matches sum(a), and every gain_c equals bestSum_c - baseSum_c.
weight: 10
criterion: The final answer contains exactly one JSON object with all 38 required keys (baseSum_c, bestSum_c, gain_c, bestX_c, bestMedian_c, bestI_c, bestL_c for c=1..5, plus totalBaseSum, totalBestSum, totalGain, perTestTime, totalTime, extraSpace), numeric fields emitted as JSON integers and complexity fields emitted as the exact strings "O(n log n)", "O(S log S)", "O(n)".
weight: 10

Answer:
# Golden Answer

## Approach

We follow the computation path mandated by the prompt: sort the array, build prefix sums, apply the fixed reduction `F(i, y) = S + L·b[i] - p[L] - (p[i+y] - p[i]) + y·b[i]` with `L = min(k·y, i-1)`, and then **exhaustively enumerate every valid `(i, y)` pair** with `1 ≤ i ≤ n` and `0 ≤ y ≤ min(i-1, n-i)`. After the full table is produced, the tie-break rule (max `F`, then min `x = 2y+1`, then min `b[i]`, then min `i`) picks the unique optimum.

All five test cases are handled the same way. Tables below show **every** `(i, y)` considered — no candidate is skipped — so the optimum is provably a global maximum over the reduction.

---

### Test case 1: `n=12`, `k=2`, `a = [9, 1, 3, 50, 2, 10, 2, 11, 4, 10, 100, 3]`

- Sorted `b = [1, 2, 2, 3, 3, 4, 9, 10, 10, 11, 50, 100]`
- Prefix sums `p = [0, 1, 3, 5, 8, 11, 15, 24, 34, 44, 55, 105, 205]`
- `baseSum_1 = sum(a) = 205` (which equals `S = p[12] = 205`)

Full exhaustive enumeration over every valid `(i, y)` with `0 ≤ y ≤ min(i-1, n-i)`:

| i | b[i] | y | x=2y+1 | L=min(k·y, i-1) | leftGain | rightLoss | F(i,y) |
|---|------|---|--------|-----------------|----------|-----------|--------|
| 1 | 1 | 0 | 1 | 0 | 0 | 0 | 205 |
| 2 | 2 | 0 | 1 | 0 | 0 | 0 | 205 |
| 2 | 2 | 1 | 3 | 1 | 1 | 0 | 206 |
| 3 | 2 | 0 | 1 | 0 | 0 | 0 | 205 |
| 3 | 2 | 1 | 3 | 2 | 1 | 1 | 205 |
| 3 | 2 | 2 | 5 | 2 | 1 | 2 | 204 |
| 4 | 3 | 0 | 1 | 0 | 0 | 0 | 205 |
| 4 | 3 | 1 | 3 | 2 | 3 | 0 | 208 |
| 4 | 3 | 2 | 5 | 3 | 4 | 1 | 208 |
| 4 | 3 | 3 | 7 | 3 | 4 | 7 | 202 |
| 5 | 3 | 0 | 1 | 0 | 0 | 0 | 205 |
| 5 | 3 | 1 | 3 | 2 | 3 | 1 | 207 |
| 5 | 3 | 2 | 5 | 4 | 4 | 7 | 202 |
| 5 | 3 | 3 | 7 | 4 | 4 | 14 | 195 |
| 5 | 3 | 4 | 9 | 4 | 4 | 21 | 188 |
| 6 | 4 | 0 | 1 | 0 | 0 | 0 | 205 |
| 6 | 4 | 1 | 3 | 2 | 5 | 5 | 205 |
| 6 | 4 | 2 | 5 | 4 | 8 | 11 | 202 |
| 6 | 4 | 3 | 7 | 5 | 9 | 17 | 197 |
| 6 | 4 | 4 | 9 | 5 | 9 | 24 | 190 |
| 6 | 4 | 5 | 11 | 5 | 9 | 70 | 144 |
| 7 | 9 | 0 | 1 | 0 | 0 | 0 | 205 |
| 7 | 9 | 1 | 3 | 2 | 15 | 1 | 219 |
| 7 | 9 | 2 | 5 | 4 | 28 | 2 | 231 |
| 7 | 9 | 3 | 7 | 6 | 39 | 4 | 240 |
| 7 | 9 | 4 | 9 | 6 | 39 | 45 | 199 |
| 7 | 9 | 5 | 11 | 6 | 39 | 136 | 108 |
| 8 | 10 | 0 | 1 | 0 | 0 | 0 | 205 |
| 8 | 10 | 1 | 3 | 2 | 17 | 0 | 222 |
| 8 | 10 | 2 | 5 | 4 | 32 | 1 | 236 |
| 8 | 10 | 3 | 7 | 6 | 45 | 41 | 209 |
| 8 | 10 | 4 | 9 | 7 | 46 | 131 | 120 |
| 9 | 10 | 0 | 1 | 0 | 0 | 0 | 205 |
| 9 | 10 | 1 | 3 | 2 | 17 | 1 | 221 |
| 9 | 10 | 2 | 5 | 4 | 32 | 41 | 196 |
| 9 | 10 | 3 | 7 | 6 | 45 | 131 | 119 |
| 10 | 11 | 0 | 1 | 0 | 0 | 0 | 205 |
| 10 | 11 | 1 | 3 | 2 | 19 | 39 | 185 |
| 10 | 11 | 2 | 5 | 4 | 36 | 128 | 113 |
| 11 | 50 | 0 | 1 | 0 | 0 | 0 | 205 |
| 11 | 50 | 1 | 3 | 2 | 97 | 50 | 252 |
| 12 | 100 | 0 | 1 | 0 | 0 | 0 | 205 |

Applying the tie-break (max F, then min x, then min median, then min i) gives the optimum `(i=11, y=1)` with `F = 252`.

- `baseSum_1 = 205`
- `bestSum_1 = 252`
- `gain_1    = 47`
- `bestX_1   = 3`
- `bestMedian_1 = 50`
- `bestI_1   = 11`
- `bestL_1   = 2`

---

### Test case 2: `n=9`, `k=1`, `a = [54, 1, 53, 1, 52, 100, 1, 51, 50]`

- Sorted `b = [1, 1, 1, 50, 51, 52, 53, 54, 100]`
- Prefix sums `p = [0, 1, 2, 3, 53, 104, 156, 209, 263, 363]`
- `baseSum_2 = sum(a) = 363` (which equals `S = p[9] = 363`)

Full exhaustive enumeration over every valid `(i, y)` with `0 ≤ y ≤ min(i-1, n-i)`:

| i | b[i] | y | x=2y+1 | L=min(k·y, i-1) | leftGain | rightLoss | F(i,y) |
|---|------|---|--------|-----------------|----------|-----------|--------|
| 1 | 1 | 0 | 1 | 0 | 0 | 0 | 363 |
| 2 | 1 | 0 | 1 | 0 | 0 | 0 | 363 |
| 2 | 1 | 1 | 3 | 1 | 0 | 0 | 363 |
| 3 | 1 | 0 | 1 | 0 | 0 | 0 | 363 |
| 3 | 1 | 1 | 3 | 1 | 0 | 49 | 314 |
| 3 | 1 | 2 | 5 | 2 | 0 | 99 | 264 |
| 4 | 50 | 0 | 1 | 0 | 0 | 0 | 363 |
| 4 | 50 | 1 | 3 | 1 | 49 | 1 | 411 |
| 4 | 50 | 2 | 5 | 2 | 98 | 3 | 458 |
| 4 | 50 | 3 | 7 | 3 | 147 | 6 | 504 |
| 5 | 51 | 0 | 1 | 0 | 0 | 0 | 363 |
| 5 | 51 | 1 | 3 | 1 | 50 | 1 | 412 |
| 5 | 51 | 2 | 5 | 2 | 100 | 3 | 460 |
| 5 | 51 | 3 | 7 | 3 | 150 | 6 | 507 |
| 5 | 51 | 4 | 9 | 4 | 151 | 55 | 459 |
| 6 | 52 | 0 | 1 | 0 | 0 | 0 | 363 |
| 6 | 52 | 1 | 3 | 1 | 51 | 1 | 413 |
| 6 | 52 | 2 | 5 | 2 | 102 | 3 | 462 |
| 6 | 52 | 3 | 7 | 3 | 153 | 51 | 465 |
| 7 | 53 | 0 | 1 | 0 | 0 | 0 | 363 |
| 7 | 53 | 1 | 3 | 1 | 52 | 1 | 414 |
| 7 | 53 | 2 | 5 | 2 | 104 | 48 | 419 |
| 8 | 54 | 0 | 1 | 0 | 0 | 0 | 363 |
| 8 | 54 | 1 | 3 | 1 | 53 | 46 | 370 |
| 9 | 100 | 0 | 1 | 0 | 0 | 0 | 363 |

Applying the tie-break (max F, then min x, then min median, then min i) gives the optimum `(i=5, y=3)` with `F = 507`.

- `baseSum_2 = 363`
- `bestSum_2 = 507`
- `gain_2    = 144`
- `bestX_2   = 7`
- `bestMedian_2 = 51`
- `bestI_2   = 5`
- `bestL_2   = 3`

---

### Test case 3: `n=13`, `k=3`, `a = [2, 31, 150, 2, 33, 34, 30, 2, 151, 32, 100, 2, 2]`

- Sorted `b = [2, 2, 2, 2, 2, 30, 31, 32, 33, 34, 100, 150, 151]`
- Prefix sums `p = [0, 2, 4, 6, 8, 10, 40, 71, 103, 136, 170, 270, 420, 571]`
- `baseSum_3 = sum(a) = 571` (which equals `S = p[13] = 571`)

Full exhaustive enumeration over every valid `(i, y)` with `0 ≤ y ≤ min(i-1, n-i)`:

| i | b[i] | y | x=2y+1 | L=min(k·y, i-1) | leftGain | rightLoss | F(i,y) |
|---|------|---|--------|-----------------|----------|-----------|--------|
| 1 | 2 | 0 | 1 | 0 | 0 | 0 | 571 |
| 2 | 2 | 0 | 1 | 0 | 0 | 0 | 571 |
| 2 | 2 | 1 | 3 | 1 | 0 | 0 | 571 |
| 3 | 2 | 0 | 1 | 0 | 0 | 0 | 571 |
| 3 | 2 | 1 | 3 | 2 | 0 | 0 | 571 |
| 3 | 2 | 2 | 5 | 2 | 0 | 0 | 571 |
| 4 | 2 | 0 | 1 | 0 | 0 | 0 | 571 |
| 4 | 2 | 1 | 3 | 3 | 0 | 0 | 571 |
| 4 | 2 | 2 | 5 | 3 | 0 | 28 | 543 |
| 4 | 2 | 3 | 7 | 3 | 0 | 57 | 514 |
| 5 | 2 | 0 | 1 | 0 | 0 | 0 | 571 |
| 5 | 2 | 1 | 3 | 3 | 0 | 28 | 543 |
| 5 | 2 | 2 | 5 | 4 | 0 | 57 | 514 |
| 5 | 2 | 3 | 7 | 4 | 0 | 87 | 484 |
| 5 | 2 | 4 | 9 | 4 | 0 | 118 | 453 |
| 6 | 30 | 0 | 1 | 0 | 0 | 0 | 571 |
| 6 | 30 | 1 | 3 | 3 | 84 | 1 | 654 |
| 6 | 30 | 2 | 5 | 5 | 140 | 3 | 708 |
| 6 | 30 | 3 | 7 | 5 | 140 | 6 | 705 |
| 6 | 30 | 4 | 9 | 5 | 140 | 10 | 701 |
| 6 | 30 | 5 | 11 | 5 | 140 | 80 | 631 |
| 7 | 31 | 0 | 1 | 0 | 0 | 0 | 571 |
| 7 | 31 | 1 | 3 | 3 | 87 | 1 | 657 |
| 7 | 31 | 2 | 5 | 6 | 146 | 3 | 714 |
| 7 | 31 | 3 | 7 | 6 | 146 | 6 | 711 |
| 7 | 31 | 4 | 9 | 6 | 146 | 75 | 642 |
| 7 | 31 | 5 | 11 | 6 | 146 | 194 | 523 |
| 7 | 31 | 6 | 13 | 6 | 146 | 314 | 403 |
| 8 | 32 | 0 | 1 | 0 | 0 | 0 | 571 |
| 8 | 32 | 1 | 3 | 3 | 90 | 1 | 660 |
| 8 | 32 | 2 | 5 | 6 | 152 | 3 | 720 |
| 8 | 32 | 3 | 7 | 7 | 153 | 71 | 653 |
| 8 | 32 | 4 | 9 | 7 | 153 | 189 | 535 |
| 8 | 32 | 5 | 11 | 7 | 153 | 308 | 416 |
| 9 | 33 | 0 | 1 | 0 | 0 | 0 | 571 |
| 9 | 33 | 1 | 3 | 3 | 93 | 1 | 663 |
| 9 | 33 | 2 | 5 | 6 | 158 | 68 | 661 |
| 9 | 33 | 3 | 7 | 8 | 161 | 185 | 547 |
| 9 | 33 | 4 | 9 | 8 | 161 | 303 | 429 |
| 10 | 34 | 0 | 1 | 0 | 0 | 0 | 571 |
| 10 | 34 | 1 | 3 | 3 | 96 | 66 | 601 |
| 10 | 34 | 2 | 5 | 6 | 164 | 182 | 553 |
| 10 | 34 | 3 | 7 | 9 | 170 | 299 | 442 |
| 11 | 100 | 0 | 1 | 0 | 0 | 0 | 571 |
| 11 | 100 | 1 | 3 | 3 | 294 | 50 | 815 |
| 11 | 100 | 2 | 5 | 6 | 560 | 101 | 1030 |
| 12 | 150 | 0 | 1 | 0 | 0 | 0 | 571 |
| 12 | 150 | 1 | 3 | 3 | 444 | 1 | 1014 |
| 13 | 151 | 0 | 1 | 0 | 0 | 0 | 571 |

Applying the tie-break (max F, then min x, then min median, then min i) gives the optimum `(i=11, y=2)` with `F = 1030`.

- `baseSum_3 = 571`
- `bestSum_3 = 1030`
- `gain_3    = 459`
- `bestX_3   = 5`
- `bestMedian_3 = 100`
- `bestI_3   = 11`
- `bestL_3   = 6`

---

### Test case 4: `n=14`, `k=2`, `a = [3, 21, 3, 20, 3, 22, 21, 20, 3, 101, 3, 100, 20, 3]`

- Sorted `b = [3, 3, 3, 3, 3, 3, 20, 20, 20, 21, 21, 22, 100, 101]`
- Prefix sums `p = [0, 3, 6, 9, 12, 15, 18, 38, 58, 78, 99, 120, 142, 242, 343]`
- `baseSum_4 = sum(a) = 343` (which equals `S = p[14] = 343`)

Full exhaustive enumeration over every valid `(i, y)` with `0 ≤ y ≤ min(i-1, n-i)`:

| i | b[i] | y | x=2y+1 | L=min(k·y, i-1) | leftGain | rightLoss | F(i,y) |
|---|------|---|--------|-----------------|----------|-----------|--------|
| 1 | 3 | 0 | 1 | 0 | 0 | 0 | 343 |
| 2 | 3 | 0 | 1 | 0 | 0 | 0 | 343 |
| 2 | 3 | 1 | 3 | 1 | 0 | 0 | 343 |
| 3 | 3 | 0 | 1 | 0 | 0 | 0 | 343 |
| 3 | 3 | 1 | 3 | 2 | 0 | 0 | 343 |
| 3 | 3 | 2 | 5 | 2 | 0 | 0 | 343 |
| 4 | 3 | 0 | 1 | 0 | 0 | 0 | 343 |
| 4 | 3 | 1 | 3 | 2 | 0 | 0 | 343 |
| 4 | 3 | 2 | 5 | 3 | 0 | 0 | 343 |
| 4 | 3 | 3 | 7 | 3 | 0 | 17 | 326 |
| 5 | 3 | 0 | 1 | 0 | 0 | 0 | 343 |
| 5 | 3 | 1 | 3 | 2 | 0 | 0 | 343 |
| 5 | 3 | 2 | 5 | 4 | 0 | 17 | 326 |
| 5 | 3 | 3 | 7 | 4 | 0 | 34 | 309 |
| 5 | 3 | 4 | 9 | 4 | 0 | 51 | 292 |
| 6 | 3 | 0 | 1 | 0 | 0 | 0 | 343 |
| 6 | 3 | 1 | 3 | 2 | 0 | 17 | 326 |
| 6 | 3 | 2 | 5 | 4 | 0 | 34 | 309 |
| 6 | 3 | 3 | 7 | 5 | 0 | 51 | 292 |
| 6 | 3 | 4 | 9 | 5 | 0 | 69 | 274 |
| 6 | 3 | 5 | 11 | 5 | 0 | 87 | 256 |
| 7 | 20 | 0 | 1 | 0 | 0 | 0 | 343 |
| 7 | 20 | 1 | 3 | 2 | 34 | 0 | 377 |
| 7 | 20 | 2 | 5 | 4 | 68 | 0 | 411 |
| 7 | 20 | 3 | 7 | 6 | 102 | 1 | 444 |
| 7 | 20 | 4 | 9 | 6 | 102 | 2 | 443 |
| 7 | 20 | 5 | 11 | 6 | 102 | 4 | 441 |
| 7 | 20 | 6 | 13 | 6 | 102 | 84 | 361 |
| 8 | 20 | 0 | 1 | 0 | 0 | 0 | 343 |
| 8 | 20 | 1 | 3 | 2 | 34 | 0 | 377 |
| 8 | 20 | 2 | 5 | 4 | 68 | 1 | 410 |
| 8 | 20 | 3 | 7 | 6 | 102 | 2 | 443 |
| 8 | 20 | 4 | 9 | 7 | 102 | 4 | 441 |
| 8 | 20 | 5 | 11 | 7 | 102 | 84 | 361 |
| 8 | 20 | 6 | 13 | 7 | 102 | 165 | 280 |
| 9 | 20 | 0 | 1 | 0 | 0 | 0 | 343 |
| 9 | 20 | 1 | 3 | 2 | 34 | 1 | 376 |
| 9 | 20 | 2 | 5 | 4 | 68 | 2 | 409 |
| 9 | 20 | 3 | 7 | 6 | 102 | 4 | 441 |
| 9 | 20 | 4 | 9 | 8 | 102 | 84 | 361 |
| 9 | 20 | 5 | 11 | 8 | 102 | 165 | 280 |
| 10 | 21 | 0 | 1 | 0 | 0 | 0 | 343 |
| 10 | 21 | 1 | 3 | 2 | 36 | 0 | 379 |
| 10 | 21 | 2 | 5 | 4 | 72 | 1 | 414 |
| 10 | 21 | 3 | 7 | 6 | 108 | 80 | 371 |
| 10 | 21 | 4 | 9 | 8 | 110 | 160 | 293 |
| 11 | 21 | 0 | 1 | 0 | 0 | 0 | 343 |
| 11 | 21 | 1 | 3 | 2 | 36 | 1 | 378 |
| 11 | 21 | 2 | 5 | 4 | 72 | 80 | 335 |
| 11 | 21 | 3 | 7 | 6 | 108 | 160 | 291 |
| 12 | 22 | 0 | 1 | 0 | 0 | 0 | 343 |
| 12 | 22 | 1 | 3 | 2 | 38 | 78 | 303 |
| 12 | 22 | 2 | 5 | 4 | 76 | 157 | 262 |
| 13 | 100 | 0 | 1 | 0 | 0 | 0 | 343 |
| 13 | 100 | 1 | 3 | 2 | 194 | 1 | 536 |
| 14 | 101 | 0 | 1 | 0 | 0 | 0 | 343 |

Applying the tie-break (max F, then min x, then min median, then min i) gives the optimum `(i=13, y=1)` with `F = 536`.

- `baseSum_4 = 343`
- `bestSum_4 = 536`
- `gain_4    = 193`
- `bestX_4   = 3`
- `bestMedian_4 = 100`
- `bestI_4   = 13`
- `bestL_4   = 2`

---

### Test case 5: `n=15`, `k=3`, `a = [2, 45, 2, 91, 40, 2, 43, 44, 46, 1, 42, 90, 41, 2, 2]`

- Sorted `b = [1, 2, 2, 2, 2, 2, 40, 41, 42, 43, 44, 45, 46, 90, 91]`
- Prefix sums `p = [0, 1, 3, 5, 7, 9, 11, 51, 92, 134, 177, 221, 266, 312, 402, 493]`
- `baseSum_5 = sum(a) = 493` (which equals `S = p[15] = 493`)

Full exhaustive enumeration over every valid `(i, y)` with `0 ≤ y ≤ min(i-1, n-i)`:

| i | b[i] | y | x=2y+1 | L=min(k·y, i-1) | leftGain | rightLoss | F(i,y) |
|---|------|---|--------|-----------------|----------|-----------|--------|
| 1 | 1 | 0 | 1 | 0 | 0 | 0 | 493 |
| 2 | 2 | 0 | 1 | 0 | 0 | 0 | 493 |
| 2 | 2 | 1 | 3 | 1 | 1 | 0 | 494 |
| 3 | 2 | 0 | 1 | 0 | 0 | 0 | 493 |
| 3 | 2 | 1 | 3 | 2 | 1 | 0 | 494 |
| 3 | 2 | 2 | 5 | 2 | 1 | 0 | 494 |
| 4 | 2 | 0 | 1 | 0 | 0 | 0 | 493 |
| 4 | 2 | 1 | 3 | 3 | 1 | 0 | 494 |
| 4 | 2 | 2 | 5 | 3 | 1 | 0 | 494 |
| 4 | 2 | 3 | 7 | 3 | 1 | 38 | 456 |
| 5 | 2 | 0 | 1 | 0 | 0 | 0 | 493 |
| 5 | 2 | 1 | 3 | 3 | 1 | 0 | 494 |
| 5 | 2 | 2 | 5 | 4 | 1 | 38 | 456 |
| 5 | 2 | 3 | 7 | 4 | 1 | 77 | 417 |
| 5 | 2 | 4 | 9 | 4 | 1 | 117 | 377 |
| 6 | 2 | 0 | 1 | 0 | 0 | 0 | 493 |
| 6 | 2 | 1 | 3 | 3 | 1 | 38 | 456 |
| 6 | 2 | 2 | 5 | 5 | 1 | 77 | 417 |
| 6 | 2 | 3 | 7 | 5 | 1 | 117 | 377 |
| 6 | 2 | 4 | 9 | 5 | 1 | 158 | 336 |
| 6 | 2 | 5 | 11 | 5 | 1 | 200 | 294 |
| 7 | 40 | 0 | 1 | 0 | 0 | 0 | 493 |
| 7 | 40 | 1 | 3 | 3 | 115 | 1 | 607 |
| 7 | 40 | 2 | 5 | 6 | 229 | 3 | 719 |
| 7 | 40 | 3 | 7 | 6 | 229 | 6 | 716 |
| 7 | 40 | 4 | 9 | 6 | 229 | 10 | 712 |
| 7 | 40 | 5 | 11 | 6 | 229 | 15 | 707 |
| 7 | 40 | 6 | 13 | 6 | 229 | 21 | 701 |
| 8 | 41 | 0 | 1 | 0 | 0 | 0 | 493 |
| 8 | 41 | 1 | 3 | 3 | 118 | 1 | 610 |
| 8 | 41 | 2 | 5 | 6 | 235 | 3 | 725 |
| 8 | 41 | 3 | 7 | 7 | 236 | 6 | 723 |
| 8 | 41 | 4 | 9 | 7 | 236 | 10 | 719 |
| 8 | 41 | 5 | 11 | 7 | 236 | 15 | 714 |
| 8 | 41 | 6 | 13 | 7 | 236 | 64 | 665 |
| 8 | 41 | 7 | 15 | 7 | 236 | 114 | 615 |
| 9 | 42 | 0 | 1 | 0 | 0 | 0 | 493 |
| 9 | 42 | 1 | 3 | 3 | 121 | 1 | 613 |
| 9 | 42 | 2 | 5 | 6 | 241 | 3 | 731 |
| 9 | 42 | 3 | 7 | 8 | 244 | 6 | 731 |
| 9 | 42 | 4 | 9 | 8 | 244 | 10 | 727 |
| 9 | 42 | 5 | 11 | 8 | 244 | 58 | 679 |
| 9 | 42 | 6 | 13 | 8 | 244 | 107 | 630 |
| 10 | 43 | 0 | 1 | 0 | 0 | 0 | 493 |
| 10 | 43 | 1 | 3 | 3 | 124 | 1 | 616 |
| 10 | 43 | 2 | 5 | 6 | 247 | 3 | 737 |
| 10 | 43 | 3 | 7 | 9 | 253 | 6 | 740 |
| 10 | 43 | 4 | 9 | 9 | 253 | 53 | 693 |
| 10 | 43 | 5 | 11 | 9 | 253 | 101 | 645 |
| 11 | 44 | 0 | 1 | 0 | 0 | 0 | 493 |
| 11 | 44 | 1 | 3 | 3 | 127 | 1 | 619 |
| 11 | 44 | 2 | 5 | 6 | 253 | 3 | 743 |
| 11 | 44 | 3 | 7 | 9 | 262 | 49 | 706 |
| 11 | 44 | 4 | 9 | 10 | 263 | 96 | 660 |
| 12 | 45 | 0 | 1 | 0 | 0 | 0 | 493 |
| 12 | 45 | 1 | 3 | 3 | 130 | 1 | 622 |
| 12 | 45 | 2 | 5 | 6 | 259 | 46 | 706 |
| 12 | 45 | 3 | 7 | 9 | 271 | 92 | 672 |
| 13 | 46 | 0 | 1 | 0 | 0 | 0 | 493 |
| 13 | 46 | 1 | 3 | 3 | 133 | 44 | 582 |
| 13 | 46 | 2 | 5 | 6 | 265 | 89 | 669 |
| 14 | 90 | 0 | 1 | 0 | 0 | 0 | 493 |
| 14 | 90 | 1 | 3 | 3 | 265 | 1 | 757 |
| 15 | 91 | 0 | 1 | 0 | 0 | 0 | 493 |

Applying the tie-break (max F, then min x, then min median, then min i) gives the optimum `(i=14, y=1)` with `F = 757`.

- `baseSum_5 = 493`
- `bestSum_5 = 757`
- `gain_5    = 264`
- `bestX_5   = 3`
- `bestMedian_5 = 90`
- `bestI_5   = 14`
- `bestL_5   = 3`

---


## Global totals

- `totalBaseSum = 205 + 363 + 571 + 343 + 493 = 1975`
- `totalBestSum = 252 + 507 + 1030 + 536 + 757 = 3082`
- `totalGain    = 3082 - 1975 = 1107`

## Complexity (as required strings)

- `perTestTime = "O(n log n)"` — sorting dominates the asymptotic cost; the prompt pins this string and we report it verbatim.
- `totalTime   = "O(S log S)"` where `S = Σ n` across test cases.
- `extraSpace  = "O(n)"` — we store only the sorted copy `b` and the prefix-sum array `p`.

## Final JSON

```json
{
  "baseSum_1": 205,
  "bestSum_1": 252,
  "gain_1": 47,
  "bestX_1": 3,
  "bestMedian_1": 50,
  "bestI_1": 11,
  "bestL_1": 2,
  "baseSum_2": 363,
  "bestSum_2": 507,
  "gain_2": 144,
  "bestX_2": 7,
  "bestMedian_2": 51,
  "bestI_2": 5,
  "bestL_2": 3,
  "baseSum_3": 571,
  "bestSum_3": 1030,
  "gain_3": 459,
  "bestX_3": 5,
  "bestMedian_3": 100,
  "bestI_3": 11,
  "bestL_3": 6,
  "baseSum_4": 343,
  "bestSum_4": 536,
  "gain_4": 193,
  "bestX_4": 3,
  "bestMedian_4": 100,
  "bestI_4": 13,
  "bestL_4": 2,
  "baseSum_5": 493,
  "bestSum_5": 757,
  "gain_5": 264,
  "bestX_5": 3,
  "bestMedian_5": 90,
  "bestI_5": 14,
  "bestL_5": 3,
  "totalBaseSum": 1975,
  "totalBestSum": 3082,
  "totalGain": 1107,
  "perTestTime": "O(n log n)",
  "totalTime": "O(S log S)",
  "extraSpace": "O(n)"
}
```