← Home
For problem statement at 0-999/900-999/950-959/954/problemG.txt this is a correct solution, but verifier at 0-999/900-999/950-959/954/verifierG.go ends with All 100 tests passed can you fix the verifier? ```go
package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	// Use buffered I/O for efficient reading and writing
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	// Helper function to read int64 from scanner
	readInt64 := func() int64 {
		scanner.Scan()
		s := scanner.Bytes()
		var res int64
		for _, b := range s {
			res = res*10 + int64(b-'0')
		}
		return res
	}

	// Read n, handling the first scan manually to set up
	if !scanner.Scan() {
		return
	}
	var n int
	for _, b := range scanner.Bytes() {
		n = n*10 + int(b-'0')
	}

	// Read r and k
	r := int(readInt64())
	k := readInt64()

	// Read array a
	a := make([]int64, n)
	for i := 0; i < n; i++ {
		a[i] = readInt64()
	}

	// Calculate prefix sums to compute range sums quickly
	pref := make([]int64, n+1)
	for i := 0; i < n; i++ {
		pref[i+1] = pref[i] + a[i]
	}

	// Precompute initial defense levels for each section
	initDef := make([]int64, n)
	for i := 0; i < n; i++ {
		left := i - r
		if left < 0 {
			left = 0
		}
		right := i + r
		if right >= n {
			right = n - 1
		}
		initDef[i] = pref[right+1] - pref[left]
	}

	// Binary search for the maximum possible minimum defense level
	var low int64 = 0
	var high int64 = 4000000000000000000 // Sufficiently large upper bound (~4e18)
	var ans int64 = 0

	// Struct for tracking added archers and their expiration
	type item struct {
		expiry int
		amount int64
	}
	// Preallocate buffer for queue to avoid allocations in loop
	qBuf := make([]item, 0, n)

	for low <= high {
		mid := (low + high) / 2
		
		possible := true
		var used int64 = 0
		var currentBoost int64 = 0
		
		// Reset queue slice, reusing capacity
		q := qBuf[:0]
		head := 0 // Queue head pointer

		for i := 0; i < n; i++ {
			// Remove expired boosts
			// A boost expires if the current index i > expiry index
			for head < len(q) {
				if q[head].expiry < i {
					currentBoost -= q[head].amount
					head++
				} else {
					break
				}
			}

			curr := initDef[i] + currentBoost
			if curr < mid {
				needed := mid - curr
				
				// Check if we exceed k
				// Use subtraction to avoid overflow: used + needed > k => needed > k - used
				if needed > k-used {
					possible = false
					break
				}
				used += needed
				currentBoost += needed

				// Greedy placement: place archers as far right as possible
				// to cover index i. Max pos <= i+r, also pos < n.
				placePos := i + r
				if placePos >= n {
					placePos = n - 1
				}
				
				// Calculate when this boost expires
				// Placed at placePos, covers up to placePos + r
				q = append(q, item{expiry: placePos + r, amount: needed})
			}
		}

		if possible {
			ans = mid
			low = mid + 1
		} else {
			high = mid - 1
		}
	}

	fmt.Fprintln(out, ans)
}
```