Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
N = int(input())
S = list(list(input()) for i in range(N))
for i in range(N):
    for j in range(N):
        S[i][j] = int(S[i][j])
ans = 0
for i in range(N):
    V = [0]*N
    T = [[0]*N for i in range(N)]
    for i in range(N):
        for j in range(N):
            T[i][j] = S[i][j]
    V[i] = 1
    queue = deque([i])
    while queue:
        q = queue.popleft()
        for i in range(N):
            if T[q][i] == 1:
                queue.append(i)
                if not(V[i] == 0 or V[i] == V[q]+1):
                    print(-1)
                    exit()
                V[i] = V[q]+1
                T[i][q] = 0
                T[q][i] = 0
    ans = max(max(V),ans)
print(ans)


Fixed submission:
from collections import deque
N = int(input())
S = list(input() for i in range(N))
ans = -1
for i in range(N):
    queue = deque([i])
    V = [0]*N
    V[i] = 1
    while queue:
        q = queue.popleft()
        m = V[q]
        for i in range(N):
            if S[q][i] == "1":
                if V[i] == 0:
                    V[i] = m+1
                    queue.append(i)
                elif abs(V[i]-m) != 1:
                    print(-1)
                    exit()
    ans = max(max(V),ans)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N,M = map(int,input().split())
DP = [[10**9]*(2**N)for i in range(M+1)]
DP[0][0] = 0
key = []
for i in range(M):
    a,b = map(int,input().split())
    c = list(map(int,input().split()))
    rock = 0
    for j in c:
        rock += 2**(j-1)
    key.append([a,rock])
for i in range(M):
    for j in range(2**N):
        DP[i+1][j] = DP[i][j]
        DP[i+1][j] = min(DP[i+1][j],key[i][0]+DP[i][j - (j&key[i][1])])
ans = DP[-1][-1]
if ans >= 10**9:
    print(-1)
else:
    print(ans)

Fixed submission:
N,M = map(int,input().split())
box = [float('inf')]*(2**N)

for i in range(M):
    a,b = map(int,input().split())
    c = list(map(int,input().split()))
    k = 0
    for i in c:
        k += 2**(i-1)
    box[k] = min(box[k],a)
    for j in range(2**N):
        box[(k&j)+(k^j)] = min(box[(k&j)+(k^j)],box[k]+box[j])
if box[-1] == float('inf'):
    print(-1)
else:
    print(box[-1])
Buggy python submission (verdict: Time Limit Exceeded):
import re
N = int(input())
S = input()
for i in range(N//2,0,-1):
    for j in range(N-2*i+1):
        if re.search(S[j:j+i],S[j+i:]):
            print(i)
            exit()
print(0)

Fixed submission:
N = int(input())
S = input()
DP = [[0]*(N+1)for i in range(N+1)]
ans = 0
for i in range(N):
    for j in range(N):
        if S[i] == S[j] :
            DP[i+1][j+1] = DP[i][j]+1
            if j-DP[i+1][j+1]-i < 0:
                DP[i+1][j+1] = 0
print(max([max(i) for i in DP]))

Buggy python submission (verdict: Time Limit Exceeded):
import re
N = int(input())
S = input()
for i in range(N//2,0,-1):
    for j in range(N-2*i+1):
        if re.search(S[j:j+i],S[j+i:]):
            print(i)
            exit()
print(0)

Fixed submission:
N = int(input())
S = input()
DP = [[0]*(N+1)for i in range(N+1)]
ans = 0
for i in range(N):
    for j in range(N):
        if S[i] == S[j] :
            DP[i+1][j+1] = DP[i][j]+1
            if j-DP[i+1][j+1]-i < 0:
                DP[i+1][j+1] = 0
print(max([max(i) for i in DP]))

Buggy python submission (verdict: Time Limit Exceeded):
import re
N = int(input())
S = input()
for i in range(N//2,0,-1):
    for j in range(N-2*i+1):
        if re.search(S[j:j+i],S[j+i:]):
            print(i)
            exit()
print(0)

Fixed submission:
N = int(input())
S = input()
DP = [[0]*(N+1)for i in range(N+1)]
ans = 0
for i in range(N):
    for j in range(N):
        if S[i] == S[j] :
            DP[i+1][j+1] = DP[i][j]+1
            if j-DP[i+1][j+1]-i < 0:
                DP[i+1][j+1] = 0
print(max([max(i) for i in DP]))
Buggy python submission (verdict: Runtime Error):
N,K = map(int,input().split())
S = list(input())
i = 1
while K >= 1 and i < N:
    if S[i] != S[0]:
        while i<N and S[i] != S[0]:
            S[i] = S[0]
            i += 1
        K -= 1
    i += 1
ans = 0
for i in range(1,N-1):
    if S[i] == "L" and S[i-1] == "L":
        ans += 1
    elif S[i] == "R" and S[i+1] == "R":
        ans += 1
if S[0] == "R" and S[1] == S[0]:
    ans += 1
if S[-1] == "L" and S[-2] == S[-1]:
    ans += 1
print(ans)

Fixed submission:
N,K = map(int,input().split())
S = list(input())
i = 1
if N == 1:
    print(0)
    exit()
while K >= 1 and i < N:
    if S[i] != S[0]:
        while i<N and S[i] != S[0]:
            S[i] = S[0]
            i += 1
        K -= 1
    i += 1
ans = 0
for i in range(1,N-1):
    if S[i] == "L" and S[i-1] == "L":
        ans += 1
    elif S[i] == "R" and S[i+1] == "R":
        ans += 1
if S[0] == "R" and S[1] == S[0]:
    ans += 1
if S[-1] == "L" and S[-2] == S[-1]:
    ans += 1
print(ans)
Buggy python submission (verdict: Runtime Error):
import bisect
N = int(input())
S = list(map(int,input().split()))
S.sort(reverse=True)
slime = [S[0]]
S[0] = -1
for i in range(N):
    slime.sort()
    S.sort(reverse=True)
    ai = 0
    for j in range(2**N):
        while S[ai] != -1:
            if slime[j] > S[ai]:
                slime.append(S[ai])
                S[ai] = -1
                ai += 1
                break
            ai += 1
        if S[ai] == -1:
            print("No")
            exit()
print("Yes")


Fixed submission:
N = int(input())
S = list(map(int,input().split()))
S.sort(reverse=True)
slime = [S[0]]
S[0] = -1
for i in range(N):
    slime.sort(reverse = True)
    S.sort(reverse=True)
    ai = 0
    for j in range(2**i):
        while S[ai] != -1:
            if slime[j] > S[ai]:
                slime.append(S[ai])
                S[ai] = -1
                ai += 1
                break
            ai += 1
    if len(slime) < 2**(i+1):
        print("No")
        exit()
print("Yes")

Buggy python submission (verdict: Runtime Error):
import bisect
N = int(input())
S = list(map(int,input().split()))
S.sort(reverse = True)
for i in range(N):
    S[i] = -S[i]
for i in range(N):
    for j in range(2**i):
        if S[j] == S[j+2**i]:
            m = bisect.bisect_left(S[2**i:],S[j])+2**i
            if m == N:
                print("No")
                exit()
            else:
                S[j+2**i] , S[m] = S[m] ,S[j+2**i]
print("Yes")


Fixed submission:
N = int(input())
S = list(map(int,input().split()))
S.sort(reverse=True)
slime = [S[0]]
S[0] = -1
for i in range(N):
    slime.sort(reverse = True)
    S.sort(reverse=True)
    ai = 0
    for j in range(2**i):
        while S[ai] != -1:
            if slime[j] > S[ai]:
                slime.append(S[ai])
                S[ai] = -1
                ai += 1
                break
            ai += 1
    if len(slime) < 2**(i+1):
        print("No")
        exit()
print("Yes")

Buggy python submission (verdict: Runtime Error):
N = int(input())
S = list(map(int,input().split()))
S.sort(reverse = True)
for i in range(N):
    for i in range(2**i):
        if S[i] <= S[i+2**i]:
            print("No")
            exit()
print("Yes")

Fixed submission:
N = int(input())
S = list(map(int,input().split()))
S.sort(reverse=True)
slime = [S[0]]
S[0] = -1
for i in range(N):
    slime.sort(reverse = True)
    S.sort(reverse=True)
    ai = 0
    for j in range(2**i):
        while S[ai] != -1:
            if slime[j] > S[ai]:
                slime.append(S[ai])
                S[ai] = -1
                ai += 1
                break
            ai += 1
    if len(slime) < 2**(i+1):
        print("No")
        exit()
print("Yes")
Buggy python submission (verdict: Runtime Error):
N = int(input())
H = list(map(int,input().split()))
ans = 0
x = 0
for i in range(N-1):
  if x[i] > x[i+1]:
    x += 1
    ans = max(ans,x)
  else:
    x = 0
print(ans)

Fixed submission:
N = int(input())
H = list(map(int,input().split()))
ans = 0
x = 0
for i in range(N-1):
  if H[i] >= H[i+1]:
    x += 1
    ans = max(ans,x)
  else:
    x = 0
print(ans)

Buggy python submission (verdict: Runtime Error):
N=int(input())
A=list(list(map(int,input().split())))
ans=0
hoge=0
for i in range(N-1):
  if A[i] >A[i+1]:
    hoge+=1
    ans=max(ans,hoge)
    else:
       hoge=0
print(ans)


Fixed submission:
N = int(input())
H = list(map(int,input().split()))
ans = 0
x = 0
for i in range(N-1):
  if H[i] >= H[i+1]:
    x += 1
    ans = max(ans,x)
  else:
    x = 0
print(ans)

Buggy python submission (verdict: Runtime Error):
N=int(input())
A=list(list(map(int,input().split()))
ans=0
hoge=0
for i in range(N-1):
  if A[i] >A[i+1]:
    hoge+=1
    ans=max(ans,hoge)
    else:
       hoge=0
print(ans)


Fixed submission:
N = int(input())
H = list(map(int,input().split()))
ans = 0
x = 0
for i in range(N-1):
  if H[i] >= H[i+1]:
    x += 1
    ans = max(ans,x)
  else:
    x = 0
print(ans)

Buggy python submission (verdict: Runtime Error):
N=input()
A=list(list(map(int,input().split()))
ans=0
hoge=0
for i in range(N-1):
  if A[i] >A[i+1]:
    hoge+=1
    ans=max(ans,hoge)
print(ans)


Fixed submission:
N = int(input())
H = list(map(int,input().split()))
ans = 0
x = 0
for i in range(N-1):
  if H[i] >= H[i+1]:
    x += 1
    ans = max(ans,x)
  else:
    x = 0
print(ans)

Buggy python submission (verdict: Runtime Error):
N=input()
ans=0
hoge=0
for i in range(N-1):
  if A[i] >A[i+1]:
    hoge+=1
    ans=max(ans,hoge)
print(ans)

Fixed submission:
N = int(input())
H = list(map(int,input().split()))
ans = 0
x = 0
for i in range(N-1):
  if H[i] >= H[i+1]:
    x += 1
    ans = max(ans,x)
  else:
    x = 0
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import deque

input = sys.stdin.readline
sys.setrecursionlimit(1100000)
N = int(input())
side = [[[]for _ in range(N)]for _ in range(N)] 
node = [[1]*N for i in range(N)]
for i in range(N):
    A = list(map(int,input().split()))
    for j in range(N-2):
        a,b = min(i,A[j]-1),max(i,A[j]-1)
        c,d = min(i,A[j+1]-1),max(i,A[j+1]-1)
        side[a][b].append([c,d])
        node[c][d] = 0
def DFS(x,y,l):
    if l > N**2:
        print(-1)
        exit()
    ret = l
    for i,j in side[x][y]:
        ret = max(ret, DFS(i,j,l+1))
    return ret


ans = 0
for i in range(N):
    for j in range(i+1,N):
        if node[i][j]:
            ans = max(DFS(i,j,1),ans)
if ans:
    print(ans)
else:
    print(-1)


Fixed submission:
N = int(input())
A = list(list(map(int,input().split()))for _ in range(N))

c = [0]*(N)
p = []
for i in range(N):
    j = A[i][c[i]]-1
    if i == A[j][c[j]]-1 and i < j:
        p.append((i,j))
ans = 0
while p:
    q = []
    for i,j in p:
        c[i] += 1
        c[j] += 1
        if c[i] < N-1:
            k = A[i][c[i]]-1
            if A[k][c[k]]-1 == i:
                q.append((i,k))
        if c[j] < N-1:
            k = A[j][c[j]]-1
            if A[k][c[k]]-1 == j:
                q.append((j,k))
    ans += 1
    p = q[:]

if min(c) != N-1:
    print(-1)
else:
    print(ans)

Buggy python submission (verdict: Runtime Error):
import sys
from collections import deque
input = sys.stdin.readline
N = int(input())
side = [[[]for _ in range(N)]for _ in range(N)] 
node = [[1]*N for i in range(N)]
for i in range(N):
    A = list(map(int,input().split()))
    for j in range(N-2):
        a,b = min(i,A[j]-1),max(i,A[j]-1)
        c,d = min(i,A[j+1]-1),max(i,A[j+1]-1)
        side[a][b].append([c,d])
        node[c][d] = 0
def DFS(x,y,l):
    if l > N**2:
        print(-1)
        exit()
    ret = l
    for i,j in side[x][y]:
        ret = max(ret, DFS(i,j,l+1))
    return ret


ans = 0
for i in range(N):
    for j in range(i+1,N):
        if node[i][j]:
            ans = max(DFS(i,j,1),ans)
if ans:
    print(ans)
else:
    print(-1)


Fixed submission:
N = int(input())
A = list(list(map(int,input().split()))for _ in range(N))

c = [0]*(N)
p = []
for i in range(N):
    j = A[i][c[i]]-1
    if i == A[j][c[j]]-1 and i < j:
        p.append((i,j))
ans = 0
while p:
    q = []
    for i,j in p:
        c[i] += 1
        c[j] += 1
        if c[i] < N-1:
            k = A[i][c[i]]-1
            if A[k][c[k]]-1 == i:
                q.append((i,k))
        if c[j] < N-1:
            k = A[j][c[j]]-1
            if A[k][c[k]]-1 == j:
                q.append((j,k))
    ans += 1
    p = q[:]

if min(c) != N-1:
    print(-1)
else:
    print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
N,Q = map(int,input().split())
edge = [[]for i in range(N)]
for i in range(N-1):
    a,b = map(int,input().split())
    edge[a-1].append(b-1)
    edge[b-1].append(a-1)
tree = [0]*N
for i in range(Q):
    p,x = map(int,input().split())
    tree[p-1] += x
flag = [0]*N
flag[0] = 1
queue = deque([0])
while queue != deque([]):
    p = queue.popleft()
    for i in edge[p]:
        if flag[i] == 0:
            flag[i] = 1
            tree[i] += tree[p]
            queue.append(i)

for i in tree:
    print(i,end=" ")


Fixed submission:
from collections import deque
N,Q = map(int,input().split())
edge = [[]for i in range(N)]
for i in range(N-1):
    a,b = map(int,input().split())
    edge[a-1].append(b-1)
    edge[b-1].append(a-1)
tree = [0]*N
for i in range(Q):
    p,x = map(int,input().split())
    tree[p-1] += x
flag = [0]*N
flag[0] = 1
queue = deque([0])
while queue != deque([]):
    p = queue.popleft()
    for i in edge[p]:
        if flag[i] == 0:
            flag[i] = 1
            tree[i] += tree[p]
            queue.append(i)

for i in tree:
    print(i,end=" ")
Buggy python submission (verdict: Runtime Error):
import bisect
try:
    S,T = input(),input()
    lenS = len(S)
    alp = [[]for i in range(26)]
    for i in range(len(S)):
        alp[ord(S[i])-97].append(i)

    l = [alp[ord(T[0])-97][0]]
    for c in T[1:]:
        A = alp[ord(c)-97]
        l.append(A[bisect.bisect_left(A,l[-1])%len(A)])

    ans = 0
    for i in range(len(l)-1):
        if l[i] > l[i+1]:
            ans += lenS
    print(ans+l[-1]+1)
except IndexError:
    print(-1)

Fixed submission:
import bisect
S,T = input(),input()
lenS = len(S)
alp = [[]for i in range(26)]

for i in range(len(S)):
    alp[ord(S[i])-97].append(i)

if alp[ord(T[0])-97] == []:
    print(-1)
    exit()

l = [alp[ord(T[0])-97][0]]
for c in T[1:]:
    A = alp[ord(c)-97]
    if A == []:
        print(-1)
        exit()
    l.append(A[bisect.bisect_left(A,l[-1]+1)%len(A)])

ans = 0
for i in range(len(l)-1):
    if l[i] >= l[i+1]:
        ans += 1
print(ans*lenS+l[-1]+1)
Buggy python submission (verdict: Time Limit Exceeded):
N,M = map(int,input().split())
AB = list(list(map(int,input().split()))for i in range(N))
AB.append([0,0])
AB.sort()
p = 0
ans = 0
for i in range(M+1):
    while p<=N and (AB[p][0] <= i) :
        p += 1
    m = AB.index(max(AB[:p],key = lambda x:x[1]))
    ans += AB[m][1]
    AB[m] = [0,0]
print(ans)

Fixed submission:
import heapq
N,M = map(int,input().split())
AB = list(list(map(int,input().split()))for i in range(N))
AB.sort()
can = []
p = 0
ans = 0
for i in range(M+1):
    while p<N and (AB[p][0] <= i) :
        heapq.heappush(can,-AB[p][1])
        p += 1
        
    if can != []:
        ans += heapq.heappop(can)
print(-ans)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
N,M = map(int,input().split())
AB = list(list(map(int,input().split()))for i in range(N))
AB.sort()
can = []
p = 0
ans = 0
for i in range(M+1):
    while p<N and (AB[p][0] <= i) :
        index = bisect.bisect_left(can,AB[p][1])
        can.insert(index,AB[p][1])
        p += 1
        
    if can != []:
        ans += can.pop(-1)
print(ans)

Fixed submission:
import heapq
N,M = map(int,input().split())
AB = list(list(map(int,input().split()))for i in range(N))
AB.sort()
can = []
p = 0
ans = 0
for i in range(M+1):
    while p<N and (AB[p][0] <= i) :
        heapq.heappush(can,-AB[p][1])
        p += 1
        
    if can != []:
        ans += heapq.heappop(can)
print(-ans)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
N,M = map(int,input().split())
AB = list(list(map(int,input().split()))for i in range(N))
AB.sort()
can = []
p = 0
ans = 0
for i in range(M+1):
    while p<N and (AB[p][0] <= i) :
        index = bisect.bisect_left(can,AB[p][1])
        can.insert(index,AB[p][1])
        p += 1
        
    if can != []:
        ans += can.pop(-1)
print(ans)

Fixed submission:
import heapq
N,M = map(int,input().split())
AB = list(list(map(int,input().split()))for i in range(N))
AB.sort()
can = []
p = 0
ans = 0
for i in range(M+1):
    while p<N and (AB[p][0] <= i) :
        heapq.heappush(can,-AB[p][1])
        p += 1
        
    if can != []:
        ans += heapq.heappop(can)
print(-ans)
Buggy python submission (verdict: Runtime Error):
import numpy as np
N,K = map(int,input().split())
A = list(map(int,input().split()))
S = sum(A)
ans = 1
C = []
for i in range(1,int(S**0.5+1)):
    if S%i == 0:
        C.append(i)
        C.append(S//i)
for c in C:
    B = np.array(A) % c
    B.sort()
    hoge = 0
    for i in range(N):
        hoge += B[i]
        if hoge <= K and hoge == -sum(B[i+1:]-c):
            ans = max(ans,c)
print(ans)

Fixed submission:
N,K = map(int,input().split())
A = list(map(int,input().split()))
S = sum(A)
ans = 1
C = []
for i in range(1,int(S**0.5+1)):
    if S%i == 0:
        C.append(i)
        C.append(S//i)
for c in C:
    B = []
    for i in A:
        B.append(i%c)
    B.sort()
    hoge = 0
    sum_b = sum(B)
    for i in range(N):
        hoge += B[i]
        sum_b -= B[i]
        if hoge <= K and hoge == c*(N-i-1)-sum_b:
            ans = max(ans,c)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
N,K = map(int,input().split())
A = list(map(int,input().split()))
S = sum(A)
ans = 1
C = []
for i in range(1,int(S**0.5+1)):
    if S%i == 0:
        C.append(i)
        C.append(S//i)
for c in C:
    B = np.array(A) % c
    B.sort()
    hoge = 0
    for i in range(N):
        hoge += B[i]
        if hoge <= K and hoge == -sum(B[i+1:]-c):
            ans = max(ans,c)
print(ans)

Fixed submission:
N,K = map(int,input().split())
A = list(map(int,input().split()))
S = sum(A)
ans = 1
C = []
for i in range(1,int(S**0.5+1)):
    if S%i == 0:
        C.append(i)
        C.append(S//i)
for c in C:
    B = []
    for i in A:
        B.append(i%c)
    B.sort()
    hoge = 0
    sum_b = sum(B)
    for i in range(N):
        hoge += B[i]
        sum_b -= B[i]
        if hoge <= K and hoge == c*(N-i-1)-sum_b:
            ans = max(ans,c)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N,K = map(int,input().split())
A = list(map(int,input().split()))
S = []
for i in range(K):
    n = 0
    for j in A:
        n+=1
        if j in S:
            S = S[:S.index(j)]
        else:
            S.append(j)
    if S == []:
        break
K = K%(i+1)
for i in range(K):
    n = 0
    for j in A:
        n+=1
        if j in S:
            S = S[:S.index(j)]
        else:
            S.append(j)
for i in S:
    print(i,end = " ")
print()

Fixed submission:
N,K = map(int,input().split())
A = list(map(int,input().split()))
hoge = [[]for i in range(200001)]
for i in range(N):
    hoge[A[i]].append(i)
NEXT = [-1]*N
for i in hoge:
    for j in range(-len(i),0):
        NEXT[i[j]] = i[j+1]
i = 1
p = 0
while i < K:
    #print(i,p)
    if NEXT[p] <= p:
        i += 1
    if NEXT[p] == N-1:
        i += 1
    p = (NEXT[p]+1)%N
    if p == 0:
        i = (K//(i-1))*(i-1)+1
while p < N:
    if NEXT[p] <= p:
        print(A[p],end = " ")
        p += 1
    else:
        p = NEXT[p]+1
print()
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(int(input())for i in range(N))
H = [float('inf'),A[0]]
for i in range(1,N):
    if A[i] <= H[-1]:
        H.append(A[i])
    else:
        for j in range(len(H)-1,-1,-1):
            if H[j] >= A[i]:
                H[j+1] = A[i]
                break
print(len(H)-1)

Fixed submission:
N = int(input())
A = list(int(input())for i in range(N))
H = [A[0]]
for i in range(1,N):
    if A[i] <= H[-1]:
        H.append(A[i])
    else:
        l,r =0,len(H)
        while l != r :
            if H[(l+r)//2]>=A[i]:
                l = (l+r)//2+1
            else:
                r = (l+r)//2
        H[l] = A[i]
print(len(H))

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(int(input())for i in range(N))
H = [float('inf'),A[0]]
for i in range(1,N):
    if A[i] <= H[-1]:
        H.append(A[i])
    else:
        for j in range(len(H)-1,-1,-1):
            if H[j] >= A[i]:
                H[j+1] = A[i]
                break
print(len(H)-1)



Fixed submission:
N = int(input())
A = list(int(input())for i in range(N))
H = [A[0]]
for i in range(1,N):
    if A[i] <= H[-1]:
        H.append(A[i])
    else:
        l,r =0,len(H)
        while l != r :
            if H[(l+r)//2]>=A[i]:
                l = (l+r)//2+1
            else:
                r = (l+r)//2
        H[l] = A[i]
print(len(H))
Buggy python submission (verdict: Runtime Error):
N,K = map(int,input().split())
P = [0]*(N+1)
for i in range(3,N+1):
    P[i] = (i-1)*(i-2)//2
ans =[]
l = 0
for i in range(N,2,-1):
    while P[i] <= K:
        ans.append(i-1)
        K -= P[i]
        N -= i-2
        l += 1
if N < 2:
    print(-1)
else:
    M = sum(ans)-len(ans)+1
    print(M)
    S = [[0,0]for i in range(M)]
    l,n = 0,2
    hoge = 1
    for i in range(ans[0]):
        u,v = hoge,n
        n += 1
        S[l] = [u,v]
        l += 1
    for i in ans[1:]:
        hoge = n-1
        for j in range(i-1):
            u,v = hoge,n
            n += 1
            S[l] = [u,v]
            l += 1
    for u,v in S:
        print(u,v)


Fixed submission:
N,K = map(int,input().split())
Ni = (N-1)*(N-2)//2
if Ni < K:
    print(-1)
    exit()
M = N-1+Ni-K
print(M)
for i in range(2,N+1):
    print(1,i)
for i in range(2,N):
    for j in range(i+1,N+1):
        if Ni-K <= 0:
            exit()
        print(i,j)
        Ni -= 1
Buggy python submission (verdict: Time Limit Exceeded):
N,M = map(int,input().split())
S = list(map(int,input().split()))
T = list(map(int,input().split()))
dp = [[1]+[0]*(len(T))for i in range(len(S)+1)]
dp[0] = [1]*(len(T)+1)
dp_sum = [[1]+[0]*(len(T))for i in range(len(S)+1)]
dp_sum[0] = [1]*(len(T)+1)
for i in range(len(S)):
    for j in range(len(T)):
        if S[i] == T[j]:
            dp[i+1][j+1] = dp_sum[i][j]
        dp_sum[i+1][j+1] = (dp[i+1][j+1]+dp_sum[i+1][j]+\
            dp_sum[i][j+1]-dp_sum[i][j])%(10**9+7)
print(dp_sum[-1][-1])

Fixed submission:
N,M = map(int,input().split())
S = list(map(int,input().split()))
T = list(map(int,input().split()))
dp = [[1]+[0]*(len(T))for i in range(len(S)+1)]
dp[0] = [1]*(len(T)+1)
dp_sum = [[1]+[0]*(len(T))for i in range(len(S)+1)]
dp_sum[0] = [1]*(len(T)+1)
for i in range(len(S)):
    for j in range(len(T)):
        if S[i] == T[j]:
            dp[i+1][j+1] = dp_sum[i][j]
        dp_sum[i+1][j+1] = (dp[i+1][j+1]+dp_sum[i+1][j]+\
            dp_sum[i][j+1]-dp_sum[i][j])%(10**9+7)
print(dp_sum[-1][-1])