Problem p03074 (python)

Fixed submission:
from itertools import accumulate
N, K = map(int, input().split())
S = input()
nums = []
if S[0] == '0':
    nums.append(0)
i = 0
while i < N:
    j = i
    while j < N and S[i] == S[j]:
        j += 1
    nums.append(j - i)
    i = j
if S[-1] == '0':
    nums.append(0)

sums = [0] * (len(nums)+1)
for i in range(len(nums)):
    sums[i+1] = sums[i] + nums[i]
ans = 0
left = 0
while left < len(nums):
    right = left + 2 * K + 1
    if right > len(sums):
        right = len(sums) - 1
    ans = max(ans, sums[right]-sums[left])
    left += 2

print(ans)

Buggy submission (verdict: Runtime Error):
package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

var sc = bufio.NewScanner(os.Stdin)

func ini() {
	var buf = make([]byte, 10000)
	sc.Buffer(buf, 1000000)
}

func nextLine() string {
	sc.Scan()
	return sc.Text()
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func main() {
	ini()
	line1 := nextLine()
	NK := strings.Split(line1, " ")
	N, _ := strconv.Atoi(NK[0])
	K, _ := strconv.Atoi(NK[1])
	S := nextLine()
	l := make([]int, 0)
	if string(S[0]) == "0" {
		l = append(l, 0)
	}
	for i := 0; i < N; {
		j := i
		for j < N && S[j] == S[i] {
			j++
		}
		l = append(l, j-i)
		i = j
	}
	if string(S[N-1]) == "0" {
		l = append(l, 0)
	}

	sums := make([]int, len(l)+1)
	for i := 0; i < len(l); i++ {
		sums[i+1] = sums[i] + l[i]
	}
	ans := 0
	for left := 0; left < len(sums); left += 2 {
		right := left + 2*K + 1
		if right >= len(sums) {
			right = len(sums) - 1
		}
		ans = max(ans, sums[right]-sums[left])
	}
	fmt.Println(ans)
}

Buggy submission (verdict: Runtime Error):
package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

// An Item is something we manage in a priority queue.
type Item struct {
	value    []int
	priority int64
	// The index is needed by update and is maintained by the heap.Interface methods.
	index int // The index of the item in the heap.
}

// A PriorityQueue implements heap.Interface and holds Items.
type PriorityQueue []*Item

func (pq PriorityQueue) Len() int { return len(pq) }

func (pq PriorityQueue) Less(i, j int) bool {
	//We want Pop to give us the highest, not lowest, priority so we use greater than here.
	return pq[i].priority > pq[j].priority
}

func (pq PriorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
	pq[i].index = i
	pq[j].index = j
}

func (pq *PriorityQueue) Push(x interface{}) {
	n := len(*pq)
	item := x.(*Item)
	item.index = n
	*pq = append(*pq, item)
}

func (pq *PriorityQueue) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	item.index = -1 // for safety
	*pq = old[0 : n-1]
	return item
}

func Equal(a, b []int) bool {
	if len(a) != len(b) {
		return false
	}
	for i, v := range a {
		if v != b[i] {
			return false
		}
	}
	return true
}

type IntArray []int64

func (a IntArray) Len() int {
	return len(a)
}

func (a IntArray) Less(i, j int) bool {
	return a[i] > a[j]
}

func (a IntArray) Swap(i, j int) {
	a[i], a[j] = a[j], a[i]
}

var sc = bufio.NewScanner(os.Stdin)

func nextLine() string {
	sc.Scan()
	return sc.Text()
}

func exists(pq PriorityQueue, item *Item) bool {
	for i := 0; i < pq.Len(); i++ {
		if pq[i].priority == item.priority && Equal(pq[i].value, item.value) {
			return true
		}
	}
	return false
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func main() {
	line1 := nextLine()
	NK := strings.Split(line1, " ")
	N, _ := strconv.Atoi(NK[0])
	K, _ := strconv.Atoi(NK[1])
	S := nextLine()
	l := make([]int, 0)
	if string(S[0]) == "0" {
		l = append(l, 0)
	}
	for i := 0; i < N; i++ {
		j := i
		for j < N && S[j] == S[i] {
			j++
		}
		l = append(l, j-i)
		i = j
	}
	if string(S[N-1]) == "0" {
		l = append(l, 0)
	}

	sums := make([]int, len(l)+1)
	for i := 0; i < len(l); i++ {
		sums[i+1] = sums[i] + l[i]
	}

	ans := 0
	for left := 0; left < len(sums); left += 2 {
		right := left + 2*K + 1
		if right >= len(sums) {
			right = len(sums) - 1
		}
		ans = max(ans, sums[right]-sums[left])
	}
	fmt.Println(ans)
}