← Home
For problem statement at 2000-2999/2000-2099/2000-2009/2003/problemE2.txt this is a correct solution, but verifier at 2000-2999/2000-2099/2000-2009/2003/verifierE2.go ends with case 1 (sample-like) failed at test 2: expected 0 got 1
Input:
6
2 0
2 1
1 2
5 1
1 2
8 3
1 4
2 5
7 8
7 2
1 4
4 7
7 3
1 2
1 7
3 7


exit status 1 can you fix the verifier? ```go
package main

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

type FastScanner struct {
	r *bufio.Reader
}

func NewFastScanner() *FastScanner {
	return &FastScanner{r: bufio.NewReaderSize(os.Stdin, 1<<20)}
}

func (fs *FastScanner) NextInt() int {
	sign := 1
	val := 0
	c, err := fs.r.ReadByte()
	for (c < '0' || c > '9') && c != '-' {
		if err != nil {
			return 0
		}
		c, err = fs.r.ReadByte()
	}
	if c == '-' {
		sign = -1
		c, err = fs.r.ReadByte()
	}
	for c >= '0' && c <= '9' {
		val = val*10 + int(c-'0')
		c, err = fs.r.ReadByte()
		if err != nil {
			break
		}
	}
	return val * sign
}

func main() {
	fs := NewFastScanner()
	out := bufio.NewWriterSize(os.Stdout, 1<<20)
	defer out.Flush()

	t := fs.NextInt()
	const INF int64 = 1 << 60

	for ; t > 0; t-- {
		n := fs.NextInt()
		m := fs.NextInt()

		lArr := make([]int, m)
		rArr := make([]int, m)

		diff := make([]int, n+2)
		for i := 0; i < m; i++ {
			l := fs.NextInt()
			r := fs.NextInt()
			lArr[i] = l
			rArr[i] = r
			diff[l]++
			diff[r]--
		}

		cover := make([]bool, n) // edges between i and i+1, index i
		pref := 0
		for i := 1; i <= n-1; i++ {
			pref += diff[i]
			if pref > 0 {
				cover[i] = true
			}
		}

		compOf := make([]int, n+1)
		compStart := []int{0}
		compEnd := []int{0}
		compCnt := 0
		for i := 1; i <= n; i++ {
			if i == 1 || !cover[i-1] {
				compCnt++
				compStart = append(compStart, i)
			}
			compEnd = append(compEnd, i)
			compOf[i] = compCnt
		}

		lenComp := make([]int, compCnt+1)
		for id := 1; id <= compCnt; id++ {
			lenComp[id] = compEnd[id] - compStart[id] + 1
		}

		zlo := make([]int, compCnt+1)
		zhi := make([]int, compCnt+1)
		hasInt := make([]bool, compCnt+1)
		for id := 1; id <= compCnt; id++ {
			zlo[id] = 0
			zhi[id] = lenComp[id]
		}

		for i := 0; i < m; i++ {
			l := lArr[i]
			r := rArr[i]
			id := compOf[l]
			hasInt[id] = true
			loCand := l - compStart[id] + 1
			hiCand := r - compStart[id]
			if zlo[id] < loCand {
				zlo[id] = loCand
			}
			if zhi[id] > hiCand {
				zhi[id] = hiCand
			}
		}

		impossible := false
		for id := 1; id <= compCnt; id++ {
			if hasInt[id] {
				if zlo[id] > zhi[id] {
					impossible = true
					break
				}
			}
		}

		if impossible {
			fmt.Fprintln(out, -1)
			continue
		}

		dp := make([]int64, n+1)
		for i := 0; i <= n; i++ {
			dp[i] = INF
		}
		dp[0] = 0
		minS, maxS := 0, 0

		for id := 1; id <= compCnt; id++ {
			leni := lenComp[id]
			lo := zlo[id]
			hi := zhi[id]
			next := make([]int64, n+1)
			for i := 0; i <= n; i++ {
				next[i] = INF
			}
			for s := minS; s <= maxS; s++ {
				if dp[s] == INF {
					continue
				}
				for z := lo; z <= hi; z++ {
					newS := s + z
					ones := leni - z
					cost := int64(ones) * (int64(s) + int64(z))
					val := dp[s] + cost
					if val < next[newS] {
						next[newS] = val
					}
				}
			}
			minS += lo
			maxS += hi
			dp = next
		}

		minCost := INF
		for s := minS; s <= maxS; s++ {
			if dp[s] < minCost {
				minCost = dp[s]
			}
		}

		totalPairs := int64(n) * int64(n-1) / 2
		ans := totalPairs - minCost
		fmt.Fprintln(out, ans)
	}
}
```