← Home
For problem statement at 1000-1999/1800-1899/1830-1839/1837/problemF.txt this is a correct solution, but verifier at 1000-1999/1800-1899/1830-1839/1837/verifierF.go ends with All tests passed can you fix the verifier? ```go
package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

// MaxNodes sets the size of the preallocated node pool.
// N <= 3*10^5, depth ~20, so 6*10^6 nodes is typical max usage.
// 8*10^6 provides a safety margin.
const MaxNodes = 8000000

type Node struct {
	sum       int64
	l, r, cnt int32
}

var (
	nodes   []Node
	nodeIdx int32
	scanner *bufio.Scanner
	writer  *bufio.Writer
)

func init() {
	nodes = make([]Node, MaxNodes)
	scanner = bufio.NewScanner(os.Stdin)
	// Increase buffer size for large inputs
	scanner.Buffer(make([]byte, 1024*1024), 64*1024*1024)
	scanner.Split(bufio.ScanWords)
	writer = bufio.NewWriter(os.Stdout)
}

func scanInt() int {
	scanner.Scan()
	x, _ := strconv.Atoi(scanner.Text())
	return x
}

func main() {
	defer writer.Flush()
	if scanner.Scan() {
		t, _ := strconv.Atoi(scanner.Text())
		for i := 0; i < t; i++ {
			solve()
		}
	}
}

func solve() {
	n := scanInt()
	k := scanInt()
	a := make([]int, n)
	vals := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = scanInt()
		vals[i] = a[i]
	}

	// Coordinate compression
	sort.Ints(vals)
	uniqueVals := vals[:0]
	if len(vals) > 0 {
		uniqueVals = append(uniqueVals, vals[0])
		for i := 1; i < len(vals); i++ {
			if vals[i] != vals[i-1] {
				uniqueVals = append(uniqueVals, vals[i])
			}
		}
	}
	vals = uniqueVals

	// Helper to find rank of value
	getRank := func(val int) int {
		l, r := 0, len(vals)-1
		for l <= r {
			mid := (l + r) >> 1
			if vals[mid] == val {
				return mid
			} else if vals[mid] < val {
				l = mid + 1
			} else {
				r = mid - 1
			}
		}
		return -1
	}

	// Reset node allocator
	nodeIdx = 1
	nodes[0] = Node{} // Ensure null node is empty
	roots := make([]int32, n+1)
	roots[0] = 0

	m := len(vals)

	// Function to update PST
	var build func(int32, int, int, int, int64) int32
	build = func(prev int32, l, r, vIdx int, vReal int64) int32 {
		idx := nodeIdx
		nodeIdx++
		nodes[idx] = nodes[prev]
		nodes[idx].cnt++
		nodes[idx].sum += vReal
		if l == r {
			return idx
		}
		mid := (l + r) >> 1
		if vIdx <= mid {
			nodes[idx].l = build(nodes[prev].l, l, mid, vIdx, vReal)
		} else {
			nodes[idx].r = build(nodes[prev].r, mid+1, r, vIdx, vReal)
		}
		return idx
	}

	for i := 0; i < n; i++ {
		rank := getRank(a[i])
		roots[i+1] = build(roots[i], 0, m-1, rank, int64(a[i]))
	}

	// Query function: sum of k smallest elements in the range
	var query func(int32, int32, int, int, int) int64
	query = func(nodeL, nodeR int32, l, r, k int) int64 {
		if k <= 0 {
			return 0
		}
		if l == r {
			return int64(k) * int64(vals[l])
		}
		mid := (l + r) >> 1
		cntL := nodes[nodes[nodeR].l].cnt - nodes[nodes[nodeL].l].cnt
		if int32(k) <= cntL {
			return query(nodes[nodeL].l, nodes[nodeR].l, l, mid, k)
		} else {
			sumL := nodes[nodes[nodeR].l].sum - nodes[nodes[nodeL].l].sum
			return sumL + query(nodes[nodeL].r, nodes[nodeR].r, mid+1, r, k-int(cntL))
		}
	}

	ans := int64(2e18) // Initialize with a large value
	x := 0

	// Iterate over all possible split points p
	for p := 0; p <= n; p++ {
		// Valid range for x (number of problems Monocarp takes from first p)
		minX := k - (n - p)
		if minX < 0 {
			minX = 0
		}
		maxX := k
		if p < maxX {
			maxX = p
		}

		if minX > maxX {
			continue
		}

		if x < minX {
			x = minX
		}
		if x > maxX {
			x = maxX
		}

		// Calculate cost for current x
		curM := query(roots[0], roots[p], 0, m-1, x)
		curP := query(roots[p], roots[n], 0, m-1, k-x)
		curMax := curM
		if curP > curMax {
			curMax = curP
		}

		// Try to improve by increasing x
		// x is non-decreasing as p increases
		for x < maxX {
			nextM := query(roots[0], roots[p], 0, m-1, x+1)
			nextP := query(roots[p], roots[n], 0, m-1, k-(x+1))
			nextMax := nextM
			if nextP > nextMax {
				nextMax = nextP
			}

			if nextMax <= curMax {
				x++
				curMax = nextMax
				curM, curP = nextM, nextP
			} else {
				break
			}
		}

		if curMax < ans {
			ans = curMax
		}
	}
	fmt.Fprintln(writer, ans)
}
```