← Home
 ```go
package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
	"strconv"
)

type IntMaxHeap []int

func (h IntMaxHeap) Len() int { return len(h) }
func (h IntMaxHeap) Less(i, j int) bool { return h[i] > h[j] }
func (h IntMaxHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }

func (h *IntMaxHeap) Push(x interface{}) {
	*h = append(*h, x.(int))
}

func (h *IntMaxHeap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)

	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	scanner.Scan()
	k, _ := strconv.Atoi(scanner.Text())
	scanner.Scan()
	x, _ := strconv.Atoi(scanner.Text())

	a := make([]int, n)
	var totalSum int64 = 0
	for i := 0; i < n; i++ {
		scanner.Scan()
		v, _ := strconv.Atoi(scanner.Text())
		a[i] = v
		totalSum += int64(v)
	}

	var lo int64 = 0
	var hi int64 = totalSum

	for lo < hi {
		mid := (lo + hi) / 2
		if check(a, n, k, x, mid) {
			hi = mid
		} else {
			lo = mid + 1
		}
	}
	fmt.Println(lo)
}

func check(a []int, n, k, x int, limit int64) bool {
	removals := 0
	var sum int64 = 0
	count := 0
	h := &IntMaxHeap{}
	heap.Init(h)

	for i := 0; i < n; i++ {
		heap.Push(h, a[i])
		sum += int64(a[i])
		count++

		if count == x {
			for sum > limit && h.Len() > 0 {
				maxVal := heap.Pop(h).(int)
				sum -= int64(maxVal)
				removals++
				count--
			}
			if count == x {
				sum = 0
				count = 0
				h = &IntMaxHeap{}
				heap.Init(h)
			}
		}
		
		if removals > k {
			return false
		}
	}

	for sum > limit && h.Len() > 0 {
		maxVal := heap.Pop(h).(int)
		sum -= int64(maxVal)
		removals++
		if removals > k {
			return false
		}
	}

	return removals <= k
}
```