← Home
For problem statement at 1000-1999/1900-1999/1960-1969/1965/problemD.txt this is a correct solution, but verifier at 1000-1999/1900-1999/1960-1969/1965/verifierD.go ends with All tests passed can you fix the verifier? package main

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

type IntHeap []int

func (h IntHeap) Len() int           { return len(h) }
func (h IntHeap) Less(i, j int) bool { return h[i] > h[j] }
func (h IntHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }
func (h *IntHeap) Push(x interface{}) { *h = append(*h, x.(int)) }
func (h *IntHeap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[:n-1]
	return x
}

func tryPn(Pn int, M []int, n int) []int {
	h := &IntHeap{}
	heap.Init(h)
	heap.Push(h, Pn)

	missingUsed := false
	k := 0
	i := 0
	P := make([]int, n/2+1)

	for k < n/2 && i < len(M) {
		E := -1
		if h.Len() > 0 {
			E = (*h)[0]
		}
		V := M[i]

		if E == V {
			heap.Pop(h)
			i++
		} else if E > V {
			if missingUsed {
				return nil
			}
			missingUsed = true
			heap.Pop(h)
		} else {
			k++
			P[k] = Pn - V
			if P[k] <= P[k-1] {
				return nil
			}
			heap.Push(h, V)
			for j := 1; j < k; j++ {
				heap.Push(h, Pn-P[k]-P[j])
				heap.Push(h, Pn-P[k]-P[j])
			}
			if k*2 < n {
				heap.Push(h, Pn-2*P[k])
			}
			i++
		}
	}

	if k < n/2 {
		return nil
	}

	a := make([]int, n)
	for j := 1; j <= n/2; j++ {
		a[j-1] = P[j] - P[j-1]
		a[n-j] = a[j-1]
	}
	if n%2 != 0 {
		mid := Pn - 2*P[n/2]
		if mid <= 0 {
			return nil
		}
		a[n/2] = mid
	}

	for _, val := range a {
		if val <= 0 {
			return nil
		}
	}

	return a
}

func verify(a []int, M []int) bool {
	n := len(a)
	sums := make([]int, 0, n*(n+1)/2)
	for i := 0; i < n; i++ {
		s := 0
		for j := i; j < n; j++ {
			s += a[j]
			sums = append(sums, s)
		}
	}
	sort.Sort(sort.Reverse(sort.IntSlice(sums)))

	missing := 0
	i, j := 0, 0
	for i < len(sums) && j < len(M) {
		if sums[i] == M[j] {
			i++
			j++
		} else if sums[i] > M[j] {
			missing++
			if missing > 1 {
				return false
			}
			i++
		} else {
			return false
		}
	}
	missing += len(sums) - i
	return missing == 1 && j == len(M)
}

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

	readInt := func() int {
		scanner.Scan()
		res := 0
		for _, b := range scanner.Bytes() {
			res = res*10 + int(b-'0')
		}
		return res
	}

	if !scanner.Scan() {
		return
	}
	t := 0
	for _, b := range scanner.Bytes() {
		t = t*10 + int(b-'0')
	}

	for tc := 0; tc < t; tc++ {
		n := readInt()
		numSums := n*(n+1)/2 - 1
		M := make([]int, numSums)
		for i := 0; i < numSums; i++ {
			M[i] = readInt()
		}
		sort.Sort(sort.Reverse(sort.IntSlice(M)))

		cands := []int{M[0]}
		if len(M) >= 3 && M[0] == M[1] {
			minA1 := M[0] - M[2]
			lastV := -1
			for i := len(M) - 1; i >= 0; i-- {
				v := M[i]
				if v == lastV {
					continue
				}
				lastV = v
				if v >= minA1 {
					cands = append(cands, M[0]+v)
				}
			}
		}

		for _, Pn := range cands {
			a := tryPn(Pn, M, n)
			if a != nil {
				if verify(a, M) {
					for i, val := range a {
						if i > 0 {
							fmt.Print(" ")
						}
						fmt.Print(val)
					}
					fmt.Println()
					break
				}
			}
		}
	}
}

func main() {
	solve()
}