For problem statement at 2000-2999/2100-2199/2120-2129/2122/problemC.txt this is a correct solution, but verifier at 2000-2999/2100-2199/2120-2129/2122/verifierC.go ends with test 1 (sample) failed: test 2: mismatched total distance (expected 44, got 38)
input:
2
4
1 1
3 0
4 2
3 4
10
10 -1
-1 2
-2 -2
-2 0
0 2
2 -3
-4 -4
-4 0
0 1
-4 -2
expected:
1 3
2 4
3 2
7 5
10 9
4 1
8 6
got:
1 3
2 4
7 6
10 2
3 9
8 5
4 1
exit status 1 can you fix the verifier? Looking at this problem, I need to maximize the sum of Manhattan distances when pairing n points.
Key insight: Manhattan distance |x_a - x_b| + |y_a - y_b| can be decomposed. For each point, we can consider 4 "rotated" values: x+y, x-y, -x+y, -x-y. The Manhattan distance equals max over the 4 sign combinations of (±x_a ± y_a) - (±x_b ± y_b).
Actually, a cleaner approach: The sum of Manhattan distances is maximized by considering that each |x_a - x_b| + |y_a - y_b| = max(|(x+y)_a - (x+y)_b|, |(x-y)_a - (x-y)_b|) ... no that's not right either.
Let me think differently. The total sum = sum of |x_ai - x_bi| + |y_ai - y_bi|.
Key observation: Consider 4 "projections" of each point: p+q where (p,q) = (x+y), (x-y), (-x+y), (-x-y). Actually these are just ±x±y.
The optimal pairing strategy: For each of the 4 sign combinations (s1, s2) ∈ {+1,-1}², compute s1*x + s2*y for each point. The maximum sum of |s1*x_a + s2*y_a - (s1*x_b + s2*y_b)| over pairings is achieved by pairing max with min. The answer is max over all 4 sign combos.
For a fixed sign combo, pairing the point with largest value with point with smallest value, second largest with second smallest, etc. gives maximum sum = (sum of top n/2 values) - (sum of bottom n/2 values).
So: compute for each of 4 projections the best possible sum, pick the projection that gives the max, then output the corresponding pairs.
For projection v_i = s1*x_i + s2*y_i: sort points by v_i, pair index i (0-based) with index i+n/2 for i in 0..n/2-1. This gives max sum for this projection = sum(v[n/2..n-1]) - sum(v[0..n/2-1]).
Let me implement this:
```go
package main
import (
"bufio"
"fmt"
"os"
"sort"
)
var reader *bufio.Reader
var writer *bufio.Writer
func main() {
reader = bufio.NewReader(os.Stdin)
writer = bufio.NewWriter(os.Stdout)
defer writer.Flush()
var t int
fmt.Fscan(reader, &t)
for ; t > 0; t-- {
var n int
fmt.Fscan(reader, &n)
xs := make([]int, n)
ys := make([]int, n)
for i := 0; i < n; i++ {
fmt.Fscan(reader, &xs[i], &ys[i])
}
signs := [4][2]int{{1, 1}, {1, -1}, {-1, 1}, {-1, -1}}
bestSum := int64(-1 << 60)
bestPairs := [][2]int{}
for _, sg := range signs {
type pv struct {
v, idx int
}
pvs := make([]pv, n)
for i := 0; i < n; i++ {
pvs[i] = pv{sg[0]*xs[i] + sg[1]*ys[i], i}
}
sort.Slice(pvs, func(a, b int) bool {
return pvs[a].v < pvs[b].v
})
var s int64
for i := 0; i < n/2; i++ {
s += int64(pvs[i+n/2].v - pvs[i].v)
}
if s > bestSum {
bestSum = s
bestPairs = make([][2]int, n/2)
for i := 0; i < n/2; i++ {
bestPairs[i] = [2]int{pvs[i].idx + 1, pvs[i+n/2].idx + 1}
}
}
}
for _, p := range bestPairs {
fmt.Fprintf(writer, "%d %d\n", p[0], p[1])
}
}
}
```