For problem statement at 1000-1999/1000-1099/1070-1079/1070/problemG.txt this is a correct solution, but verifier at 1000-1999/1000-1099/1070-1079/1070/verifierG.go ends with wrong answer on test 1
input:
5 3
2 2
2 2
3 2
2 2 0 -3 -4
expected:
1
3 2 1
got:
1
2 3
exit status 1 can you fix the verifier? ```go
package main
import (
"bufio"
"fmt"
"os"
)
type Move struct {
heroID int
ni, nj int
}
func main() {
reader := bufio.NewReader(os.Stdin)
var n, m int
if _, err := fmt.Fscan(reader, &n, &m); err != nil {
return
}
heroPos := make([]int, m+1)
heroHP := make([]int64, m+1)
heroAt := make([]int, n+1)
for i := 1; i <= m; i++ {
fmt.Fscan(reader, &heroPos[i], &heroHP[i])
heroAt[heroPos[i]] = i
}
A := make([]int, n+1)
for i := 1; i <= n; i++ {
fmt.Fscan(reader, &A[i])
}
canReachRight := make([][]bool, m+1)
canReachLeft := make([][]bool, m+1)
for h := 1; h <= m; h++ {
canReachRight[h] = make([]bool, n+1)
canReachLeft[h] = make([]bool, n+1)
S := heroPos[h]
hp := heroHP[h]
canReachRight[h][S] = true
curr := hp
for pos := S + 1; pos <= n; pos++ {
if A[pos] < 0 {
curr += int64(A[pos])
if curr < 0 {
break
}
} else if A[pos] > 0 {
curr += int64(A[pos])
}
canReachRight[h][pos] = true
}
canReachLeft[h][S] = true
curr = hp
for pos := S - 1; pos >= 1; pos-- {
if A[pos] < 0 {
curr += int64(A[pos])
if curr < 0 {
break
}
} else if A[pos] > 0 {
curr += int64(A[pos])
}
canReachLeft[h][pos] = true
}
}
memo := make([][]int, n+2)
nextMove := make([][]Move, n+2)
for i := range memo {
memo[i] = make([]int, n+2)
nextMove[i] = make([]Move, n+2)
}
for X := 1; X <= n; X++ {
inP := make([]bool, n+1)
for h := 1; h <= m; h++ {
inP[heroPos[h]] = true
}
inP[X] = true
P := make([]int, 0, n+1)
for pos := 1; pos <= n; pos++ {
if inP[pos] {
P = append(P, pos)
}
}
K := len(P)
idx := -1
for i := 0; i < K; i++ {
if P[i] == X {
idx = i
break
}
}
for i := 0; i < K; i++ {
for j := 0; j < K; j++ {
memo[i][j] = 0
}
}
var dfs func(i, j int) bool
dfs = func(i, j int) bool {
if i == 0 && j == K-1 {
return true
}
if memo[i][j] != 0 {
return memo[i][j] == 1
}
for k := i - 1; k >= 0; k-- {
h := heroAt[P[k]]
if h != 0 {
if canReachRight[h][P[i]-1] {
if dfs(k, j) {
memo[i][j] = 1
nextMove[i][j] = Move{heroID: h, ni: k, nj: j}
return true
}
}
}
}
for k := j + 1; k < K; k++ {
h := heroAt[P[k]]
if h != 0 {
if canReachLeft[h][P[j]+1] {
if dfs(i, k) {
memo[i][j] = 1
nextMove[i][j] = Move{heroID: h, ni: i, nj: k}
return true
}
}
}
}
memo[i][j] = -1
return false
}
found := false
var firstHero, firstI, firstJ int
for k := 0; k < K; k++ {
h := heroAt[P[k]]
if h == 0 {
continue
}
if P[k] <= X {
if canReachRight[h][X] {
if dfs(k, idx) {
found = true
firstHero = h
firstI, firstJ = k, idx
break
}
}
} else {
if canReachLeft[h][X] {
if dfs(idx, k) {
found = true
firstHero = h
firstI, firstJ = idx, k
break
}
}
}
}
if found {
fmt.Println(X)
sequence := make([]int, 0, m)
moved := make([]bool, m+1)
addAbsorbed := func(L, R int) {
for pos := L; pos <= R; pos++ {
h := heroAt[pos]
if h != 0 && !moved[h] {
moved[h] = true
sequence = append(sequence, h)
}
}
}
moved[firstHero] = true
sequence = append(sequence, firstHero)
L := P[firstI]
R := P[firstJ]
addAbsorbed(L, R)
currI, currJ := firstI, firstJ
for currI != 0 || currJ != K-1 {
move := nextMove[currI][currJ]
h := move.heroID
moved[h] = true
sequence = append(sequence, h)
currI, currJ = move.ni, move.nj
L = P[currI]
R = P[currJ]
addAbsorbed(L, R)
}
for i, h := range sequence {
if i > 0 {
fmt.Print(" ")
}
fmt.Print(h)
}
fmt.Println()
return
}
}
fmt.Println("-1")
}
```