Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
MOD = 10 **9 + 7
INF = 10 ** 10

def main():
    n,m,l = map(int,input().split())
    dist = [[INF] * n for _ in range(n)]  
    for _ in range(m):
        a,b,c = map(int,input().split())
        a -= 1
        b -= 1
        dist[a][b] = c
        dist[b][a] = c

    for k in range(n):
        for i in range(n):
            for j in range(n):
                dist[i][j] = min(dist[i][j],dist[i][k] + dist[k][j])

    cnt = [[INF] * n for _ in range(n)]   
    for i in range(n):
        for j in range(n):
            if dist[i][j] <= l:
                cnt[i][j] = 1

    for k in range(n):
        for i in range(n):
            for j in range(n):
                cnt[i][j] = min(cnt[i][j],cnt[i][k] + cnt[k][j])
    
    q = int(input())
    for _ in range(q):
        s,t = map(int,input().split())
        s -= 1
        t -= 1
        print(cnt[s][t] - 1 if cnt[s][t] != INF  else -1)       
if __name__ == '__main__':
    main()

Fixed submission:
MOD = 10 **9 + 7
INF = 10 ** 10

def main():
    n,m,l = map(int,input().split())
    dist = [[INF] * n for _ in range(n)]  
    for _ in range(m):
        a,b,c = map(int,input().split())
        a -= 1
        b -= 1
        dist[a][b] = c
        dist[b][a] = c

    for k in range(n):
        for i in range(n):
            for j in range(n):
                dist[i][j] = min(dist[i][j],dist[i][k] + dist[k][j])

    cnt = [[INF] * n for _ in range(n)]   
    for i in range(n):
        for j in range(n):
            if dist[i][j] <= l:
                cnt[i][j] = 1

    for k in range(n):
        for i in range(n):
            for j in range(n):
                cnt[i][j] = min(cnt[i][j],cnt[i][k] + cnt[k][j])
    
    q = int(input())
    for _ in range(q):
        s,t = map(int,input().split())
        s -= 1
        t -= 1
        print(cnt[s][t] - 1 if cnt[s][t] != INF  else -1)       
if __name__ == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
MOD = 10 ** 9 + 7
INF = 10 ** 10
import sys
sys.setrecursionlimit(100000000)

def main():
    N = int(input())
    G = [[] for _ in range(N)]
    dist = []
    for i in range(N):
        route = [int(i) for i in input()]
        for j in range(N):
            if route[j]:
                G[i].append(j)
            elif i != j:
                route[j] = INF
        dist.append(route)
    
    color = [-1]*N
    color[0] = 0
    stack = [0]
    possible = True
    while stack:
        p = stack.pop()
        for e in G[p]:
            if color[e] >= 0:
                if color[e] == color[p]:
                    possible = False
            else:
                color[e] = color[p]^1
                stack.append(e)
        if not possible:
            break
    
    if possible:
        ans = 0
        for k in range(N):
            for i in range(N):
                for j in range(N):
                    dist[i][j] = min(dist[i][j],dist[i][k] + dist[k][j])
        for i in range(N):
            for j in range(N):
                ans = max(ans,dist[i][j])
        print(ans + 1)
    else:
        print(-1)

if __name__ == '__main__':
    main()

Fixed submission:
MOD = 10 ** 9 + 7
INF = 10 ** 10
import sys
sys.setrecursionlimit(100000000)

def main():
    N = int(input())
    G = [[] for _ in range(N)]
    dist = []
    for i in range(N):
        route = [int(i) for i in input()]
        for j in range(N):
            if route[j]:
                G[i].append(j)
            elif i != j:
                route[j] = INF
        dist.append(route)
    
    color = [-1]*N
    color[0] = 0
    stack = [0]
    possible = True
    while stack:
        p = stack.pop()
        for e in G[p]:
            if color[e] >= 0:
                if color[e] == color[p]:
                    possible = False
            else:
                color[e] = color[p]^1
                stack.append(e)
        if not possible:
            break
    
    if possible:
        ans = 0
        for k in range(N):
            for i in range(N):
                for j in range(N):
                    dist[i][j] = min(dist[i][j],dist[i][k] + dist[k][j])
        for i in range(N):
            for j in range(N):
                ans = max(ans,dist[i][j])
        print(ans + 1)
    else:
        print(-1)

if __name__ == '__main__':
    main()
Buggy python submission (verdict: Runtime Error):
n=int(input())
A=[[int(i)] for i in input().split()]

for i in range(n):
  A[i].append(i)
A=sorted(A, key=lambda x:x[0])
B=[A[i][1]+1 for i in range(n)]

print(*B)

Fixed submission:
n=int(input())
ans=(n/2+(n%2)*0.5)/n
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    INF=10**9
    n,m=map(int,input().split())
    key=[0]*m
    keycost=[0]*m

    for i in range(m):
        a,b=map(int,input().split())
        keycost[i]=a
        c=[pow(2,int(i)-1) for i in input().split()]
        key[i]=sum(c)
    
    dp=[[INF]*(m+1) for _ in range(1<<n)]
    dp[0]=[0]*(1+m)
    
    for s in range(1<<n):
        for i in range(m):
            dp[s][i+1]=min(dp[(s&key[i])^s][i]+keycost[i],dp[s][i])
    
    if dp[-1][m]==INF:
        print(-1)
    else:
        print(dp[-1][m])
 
if __name__=='__main__':
    main()

Fixed submission:
def main():
    INF=10**9
    n,m=map(int,input().split())
    key=[0]*m
    keycost=[0]*m

    for i in range(m):
        a,b=map(int,input().split())
        keycost[i]=a
        c=[pow(2,int(i)-1) for i in input().split()]
        key[i]=sum(c)
    
    dp=[[INF]*(m+1) for _ in range(1<<n)]
    dp[0]=[0]*(1+m)
    
    for s in range(1<<n):
        for i in range(m):
            dp[s][i+1]=min(dp[(s&key[i])^s][i]+keycost[i],dp[s][i])
    
    if dp[-1][m]==INF:
        print(-1)
    else:
        print(dp[-1][m])
 
if __name__=='__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
A=sorted([int(i) for i in input().split()])

for i in range(m):
  A[-1]=A[-1]//2
  A.sort()
  
ans=sum(A)
print(ans)


Fixed submission:
import heapq

n,m=map(int,input().split())
A=[int(i)*(-1) for i in input().split()]
heapq.heapify(A)

for i in range(m):
  M=heapq.heappop(A)
  heapq.heappush(A,-((-M)//2))
  
ans=-sum(A)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
A=sorted([int(i) for i in input().split()])

for i in range(m):
  A[-1]=A[-1]//2
  A.sort()
  
ans=sum(A)
print(ans)


Fixed submission:
import heapq

n,m=map(int,input().split())
A=[int(i)*(-1) for i in input().split()]
heapq.heapify(A)

for i in range(m):
  M=heapq.heappop(A)
  heapq.heappush(A,-((-M)//2))
  
ans=-sum(A)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input=sys.stdin.readline   
sys.setrecursionlimit(100000000)
from functools import lru_cache
INF = 1000000000
MOD = 10**9+7

def main():
    n=int(input())
    s=input()

    dp=[[0]*(1+n) for _ in range(n)]
    for i in range(n-1):
        for j in range(i+1,n):
            if s[i]==s[j]:
                dp[i+1][j+1]=min(dp[i][j]+1,j-i)          
    
    ans=0
    for i in range(n+1):
        for j in range(i+1,n+1):
            ans=max(ans,dp[i][j])
    print(ans)

if __name__=='__main__':
    main()

Fixed submission:
import sys
input=sys.stdin.readline   
sys.setrecursionlimit(100000000)
from functools import lru_cache
INF = 1000000000
MOD = 10**9+7

def main():
    n=int(input())
    s=input()

    dp=[[0]*(1+n) for _ in range(n)]
    for i in range(n-1):
        for j in range(i+1,n):
            if s[i]==s[j]:
                dp[i+1][j+1]=min(dp[i][j]+1,j-i)          
    
    ans=0
    for i in range(n+1):
        for j in range(i+1,n+1):
            ans=max(ans,dp[i][j])
    print(ans)

if __name__=='__main__':
    main()
Buggy python submission (verdict: Runtime Error):
n=int(input())
heights=[int(i) for i in input().split()]

ans=[]
trans=0

for i in range(n-1):
  if heights[i]>=heights[i+1]:
    trans+=1
    ans.append(trans)
  else:
    trans=0
    ans.append(trans)
       
print(max(ans))

Fixed submission:
n=int(input())
H=[int(i) for i in input().split()]


cnt=0
ans=[]
for i in range(n-1):
    if H[i]>=H[i+1]:
        cnt+=1
        ans.append(cnt)
    else:
        cnt=0
        ans.append(cnt)

if ans:
  print(max(ans))

else:
  print(0)

Buggy python submission (verdict: Runtime Error):
n=int(input())
H=[int(i) for i in input().split()]

cnt=0
ans=[]
for i in range(n-1):
    if H[i]>=H[i+1]:
        cnt+=1
        ans.append(cnt)
    else:
        cnt=0
print(max(ans))

Fixed submission:
n=int(input())
H=[int(i) for i in input().split()]


cnt=0
ans=[]
for i in range(n-1):
    if H[i]>=H[i+1]:
        cnt+=1
        ans.append(cnt)
    else:
        cnt=0
        ans.append(cnt)

if ans:
  print(max(ans))

else:
  print(0)
Buggy python submission (verdict: Runtime Error):
def main():
    import sys
    input=sys.stdin.readline
    
    n,q=map(int,input().split())
    tree=[[] for _ in range(n)]
    for _ in range(n-1):
        a,b=map(int,input().split())
        a-=1
        b-=1
        tree[a].append(b)
        tree[b].append(a)
    
    cnt=[0]*n
    for _ in range(q):
        p,x=map(int,input().split())
        p-=1
        cnt[p]+=x
    
    def dfs(i,before=-1):
        for e in tree[i]:
            if e!=before:
                cnt[e]+=cnt[i]
                dfs(e,i)
    dfs(0)   
    print(*cnt)

if __name__=='__main__':
    main()



Fixed submission:
import sys
input=sys.stdin.readline

n,q=map(int,input().split())
tree=[[] for i in range(n)]

for i in range(n-1):
  a,b=map(int,input().split())
  a,b=a-1,b-1
  tree[a].append(b)
  tree[b].append(a)

ans=[0]*n
for i in range(q):
  p,x=map(int,input().split())
  ans[p-1]+=x

from collections import deque
q=deque([0])
while q:
  i=deque.popleft(q)
  for leaf in tree[i]:
    q.append(leaf)
    ans[leaf]+=ans[i]
    tree[leaf].remove(i)

print(*ans)
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    MOD=10**9+7

    mod13=[1]*(10**5+1)
    for i in range(10**5):
        mod13[i+1]=mod13[i]*10%13
    
    s=list(input())[::-1]
    l=len(s)
    
    dp=[[0]*13 for _ in range(1+l)]

    if s[0]=='?':
        for num in range(10):
            t=num%13
            dp[1][t]+=1
    else:
        num=int(s[0])
        t=num%13
        dp[1][t]=1

    for i in range(1,l):
        if s[i]=='?':
            for j in range(13):
                for num in range(10):
                    t=(j+mod13[i]*num)%13
                    dp[i+1][t]+=dp[i][j]
                    dp[i+1][t]%=MOD
        
        else:
            num=int(s[i])
            for j in range(13):
                t=(j+mod13[i]*num)%13
                dp[i+1][t]+=dp[i][j]
                dp[i+1][t]%=MOD
    
    print(dp[-1][5])
 
if __name__=='__main__':
    main()

Fixed submission:
def main():
    MOD=10**9+7

    mod13=[1]*(10**5+1)
    for i in range(10**5):
        mod13[i+1]=mod13[i]*10%13
    
    s=list(input())[::-1]
    l=len(s)
    
    dp=[[0]*13 for _ in range(1+l)]

    if s[0]=='?':
        for num in range(10):
            t=num%13
            dp[1][t]+=1
    else:
        num=int(s[0])
        t=num%13
        dp[1][t]=1

    for i in range(1,l):
        if s[i]=='?':
            for j in range(13):
                for num in range(10):
                    t=(j+mod13[i]*num)%13
                    dp[i+1][t]+=dp[i][j]
                    dp[i+1][t]%=MOD
        
        else:
            num=int(s[i])
            for j in range(13):
                t=(j+mod13[i]*num)%13
                dp[i+1][t]+=dp[i][j]
                dp[i+1][t]%=MOD
    
    print(dp[-1][5])
 
if __name__=='__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    from bisect import bisect_left
    n=int(input())
    a=[int(input()) for _ in range(n)]

    color=[]
    for num in a:
        idx=bisect_left(color,num)
        if idx==0:
            color=[num]+color
        else:
            color[idx-1]=num
    
    print(len(color))    
if __name__=='__main__':
    main()

Fixed submission:
def main():
    from bisect import bisect_right
    n=int(input())
    a=[int(input()) for _ in range(n)]

    color=[]
    for i in range(n-1,-1,-1):
        num=a[i]
        idx=bisect_right(color,num)
        if idx==len(color):
            color.append(num)
        else:
            color[idx]=num
    
    print(len(color))    
if __name__=='__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
def main():
  import numpy as np
  n=int(input())
  rain=[int(i)*2 for i in input().split()]
  
  matrix=[[0]*n for _ in range(n)]
  for i in range(n):
    if i!=n-1:
      matrix[i][i],matrix[i][i+1]=1,1
    else:
      matrix[i][i],matrix[i][0]=1,1
      
  matrix=np.array(matrix,dtype=np.int64)
  print(*list(map(int,np.linalg.solve(matrix,rain))))
 
if __name__=='__main__':
  main()

Fixed submission:
def main():
  n=int(input())
  rain=[int(i) for i in input().split()]
  
  test=0
  for i in range(n):
    test=rain[i]-test
  ans=test
  
  fall=[0]*n
  fall[0]=test
  for i in range(n-1):
    tmp=rain[i]-fall[i]//2
    fall[i+1]=2*tmp
  
  print(*fall)
if __name__=='__main__':
  main()

Buggy python submission (verdict: Runtime Error):
import numpy as np

n=int(input())
rain=np.array([int(i)*2 for i in input().split()])

a=np.eye(n,dtype=np.int64)
for i in range(n-1):
  a[i][i+1]=1
a[-1][0]=1

ans=np.linalg.solve(a,rain)
ans=ans.astype(np.int64)
print(*ans)

Fixed submission:
def main():
  n=int(input())
  rain=[int(i) for i in input().split()]
  
  test=0
  for i in range(n):
    test=rain[i]-test
  ans=test
  
  fall=[0]*n
  fall[0]=test
  for i in range(n-1):
    tmp=rain[i]-fall[i]//2
    fall[i+1]=2*tmp
  
  print(*fall)
if __name__=='__main__':
  main()
Buggy python submission (verdict: Runtime Error):
n,k=map(int,input().split())
mod=10**9+7

#x**t%mod
def pow(x,t):
    if t==0:
        return 1
    a=pow(x,t>>1)
    return a*a*(x**(t&1))%mod

#n!%mod
fact=[0]*n
fact[0]=1
for i in range(1,n):
    fact[i]=fact[i-1]*i
    fact[i]%=mod

#1/n!%mod
ifact=[0]*n
ifact[n-1]=pow(fact[n-1],mod-2)
for i in range(n-1,0,-1):
    ifact[i-1]=ifact[i]*i
    ifact[i-1]%=mod


def nCr(n,r):
    if r<0 or r>n:
        return 0
    return fact[n]*ifact[r]*ifact[n-r]%mod


for i in range(1,k+1):
  ans=nCr(k-1,i-1)*nCr(n-k+1,i)
  ans%=mod
  print(ans)


Fixed submission:
n,k=map(int,input().split())
mod=10**9+7

#x**t%mod
def pow(x,t):
    if t==0:
        return 1
    a=pow(x,t>>1)
    return a*a*(x**(t&1))%mod

#n!%mod
fact=[0]*(n+1)
fact[0]=1
for i in range(1,n+1):
    fact[i]=fact[i-1]*i
    fact[i]%=mod

#1/n!%mod
ifact=[0]*(n+1)
ifact[n]=pow(fact[n],mod-2)
for i in range(n,0,-1):
    ifact[i-1]=ifact[i]*i
    ifact[i-1]%=mod


def nCr(n,r):
    if r<0 or r>n:
        return 0
    return fact[n]*ifact[r]*ifact[n-r]%mod


for i in range(1,k+1):
  ans=nCr(k-1,i-1)*nCr(n-k+1,i)
  ans%=mod
  print(ans)
Buggy python submission (verdict: Runtime Error):
n,x=map(int,input().split())
Len=[int(i) for i in input().split()]

test=0
for i in range(n):
  test+=Len[i]
  if test<=x:
    continue
  else:
    ans=i+1
    break
    
print(ans)

Fixed submission:
n,x=map(int,input().split())
Len=[int(i) for i in input().split()]

ans=n+1
test=0
for i in range(n):
  test+=Len[i]
  if test<=x:
    continue
  else:
    ans=i+1
    break
    
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
MOD = 10 ** 9 + 7
INF = 10 ** 10
import sys
sys.setrecursionlimit(100000000)
dy = (-1,0,1,0)
dx = (0,1,0,-1)
input = sys.stdin.readline
def main():
    n,m = map(int,input().split())
    s = list(map(int,input().split()))
    t = list(map(int,input().split()))
    
    dp = [[0] * (m + 1) for _ in range(n + 1)]
    if s[0] == t[0]:
        dp[1][1] = 1
    else:
        dp[1][1] = 0
    for i in range(n):
        for j in range(m):
            if i == j == 0:
                continue
            if s[i] == t[j]:
                dp[i + 1][j + 1] = (dp[i][j + 1] + dp[i + 1][j] + 1)%MOD
            else:
                dp[i + 1][j + 1] = (dp[i][j + 1] + dp[i + 1][j] - dp[i][j])%MOD
    print((dp[-1][-1] + 1)%MOD)

if __name__ =='__main__':
    main()   


Fixed submission:
MOD = 10 ** 9 + 7
INF = 10 ** 10
import sys
sys.setrecursionlimit(100000000)
dy = (-1,0,1,0)
dx = (0,1,0,-1)

def main():
    n,m = map(int,input().split())
    s = list(map(int,input().split()))
    t = list(map(int,input().split()))
    
    dp = [[0] * (m + 1) for _ in range(n + 1)]
    if s[0] == t[0]:
        dp[1][1] = 1
    else:
        dp[1][1] = 0
    for i in range(n):
        for j in range(m):
            if i == j == 0:
                continue
            if s[i] == t[j]:
                dp[i + 1][j + 1] = (dp[i][j + 1] + dp[i + 1][j] + 1)%MOD
            else:
                dp[i + 1][j + 1] = (dp[i][j + 1] + dp[i + 1][j] - dp[i][j])%MOD
    print((dp[-1][-1] + 1)%MOD)

if __name__ =='__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
MOD = 10 ** 9 + 7
INF = 10 ** 10
import sys
sys.setrecursionlimit(100000000)
dy = (-1,0,1,0)
dx = (0,1,0,-1)

def main():
    n,m = map(int,input().split())
    s = list(map(int,input().split()))
    t = list(map(int,input().split()))
    
    dp = [[0] * (m + 1) for _ in range(n + 1)]
    if s[0] == t[0]:
        dp[1][1] = 1
    else:
        dp[1][1] = 0
    for i in range(n):
        for j in range(m):
            if i == j == 0:
                continue
            if s[i] == t[j]:
                dp[i + 1][j + 1] = (dp[i][j + 1] + dp[i + 1][j] + 1)%MOD
            else:
                dp[i + 1][j + 1] = (dp[i][j + 1] + dp[i + 1][j] - dp[i][j])%MOD
    print(dp[-1][-1] + 1)

if __name__ =='__main__':
    main()   


Fixed submission:
MOD = 10 ** 9 + 7
INF = 10 ** 10
import sys
sys.setrecursionlimit(100000000)
dy = (-1,0,1,0)
dx = (0,1,0,-1)

def main():
    n,m = map(int,input().split())
    s = list(map(int,input().split()))
    t = list(map(int,input().split()))
    
    dp = [[0] * (m + 1) for _ in range(n + 1)]
    if s[0] == t[0]:
        dp[1][1] = 1
    else:
        dp[1][1] = 0
    for i in range(n):
        for j in range(m):
            if i == j == 0:
                continue
            if s[i] == t[j]:
                dp[i + 1][j + 1] = (dp[i][j + 1] + dp[i + 1][j] + 1)%MOD
            else:
                dp[i + 1][j + 1] = (dp[i][j + 1] + dp[i + 1][j] - dp[i][j])%MOD
    print((dp[-1][-1] + 1)%MOD)

if __name__ =='__main__':
    main()
Buggy python submission (verdict: Runtime Error):
def main():
    import sys
    input=sys.stdin.readline   
    sys.setrecursionlimit(100000000)

    n=int(input())
    point=[tuple(map(int,input().split())) for _ in range(n)]
    dic={i:j for i,j in zip(point,range(n))}
    

    ans=10**9
    for i in range(n-1):
        for j in range(i+1,n):
            p=point[j][0]-point[i][0]
            q=point[j][1]-point[i][1]
            cnt=0
            visited=[True]*n

            def dfs(x,y,p,q):
                if (x+p,y+q) in dic:
                    idx=dic[(x+p,y+q)]
                    if visited[idx]:
                        visited[idx]=False
                        dfs(x+p,y+q,p,q)
                if (x-p,y-q) in dic:
                    idx=dic[(x-p,y-q)]
                    if visited[idx]:
                        visited[idx]=False
                        dfs(x-p,y-q,p,q)

            for k in range(n):
                if visited[k]:
                    cnt+=1
                    dfs(point[k][0],point[k][1],p,q)      

            ans=min(ans,cnt)  
    print(cnt)

if __name__=='__main__':
    main()

Fixed submission:
def main():
    import sys
    input=sys.stdin.readline   
    sys.setrecursionlimit(100000000)

    n=int(input())
    point=[tuple(map(int,input().split())) for _ in range(n)]
    dic={i:j for i,j in zip(point,range(n))}
    
    if n==1:
        print(1)
        exit()
        
    ans=10**9
    for i in range(n-1):
        for j in range(i+1,n):
            p=point[j][0]-point[i][0]
            q=point[j][1]-point[i][1]
            cnt=0
            visited=[True]*n

            def dfs(x,y):
                if (x+p,y+q) in dic:
                    idx=dic[(x+p,y+q)]
                    if visited[idx]:
                        visited[idx]=False
                        dfs(x+p,y+q)
                if (x-p,y-q) in dic:
                    idx=dic[(x-p,y-q)]
                    if visited[idx]:
                        visited[idx]=False
                        dfs(x-p,y-q)

            for k in range(n):
                if visited[k]:
                    visited[k]=False
                    cnt+=1
                    dfs(point[k][0],point[k][1])      
        
            ans=min(ans,cnt)  
    print(ans)

if __name__=='__main__':
    main()
Buggy python submission (verdict: Runtime Error):
import sys
INF = 10 ** 11
MOD = 10 ** 9 + 7
from collections import deque
sys.setrecursionlimit(100000000)
from heapq import heapify,heappush,heappop   

def main():
    n = int(input())
    a = list(map(int,input().split()))
    m = []
    p = []
    z = 0
    for num in a:
        if num < 0:
            m.append(num)
        elif num > 0:
            p.append(num)
        else:
            z += 1
    
    ans = []
    if len(p) * len(m) > 0:
        num1 = p[-1]
        p.pop()
        num2 = m[-1]
        m.pop()
        for i in range(len(m)):
            ans.append((num1,m[i]))
            num1 -= m[i]
        for i in range(len(p)):
            ans.append((num2,p[i]))
            num2 -= p[i]
        ans.append((num1,num2))
        num1 -= num2
    
    elif len(p) == 0:
        m.sort()
        num1 = m[-1]
        m.pop()
        for i in range(len(m)):
            ans.append((num1,m[i]))
            num1 -= m[i]
    
    elif len(m) == 0:
        p.sort()
        num1 = p[0]
        for i in range(1,len(p) - 1):
            ans.append((num1,p[i]))
            num1 -= p[i]
        ans.append((p[-1],num1))
        num1 = p[-1] - num1
    
    for i in range(z):
        ans.append((num1,0))
    print(num1)
    for i in range(n - 1):
        print(*ans[i])

if __name__=='__main__':
    main()

Fixed submission:
import sys
INF = 10 ** 11
MOD = 10 ** 9 + 7
from collections import deque
sys.setrecursionlimit(100000000)
from heapq import heapify,heappush,heappop   

def main():
    n = int(input())
    a = list(map(int,input().split()))
    m = []
    p = []
    z = 0
    for num in a:
        if num < 0:
            m.append(num)
        elif num > 0:
            p.append(num)
        else:
            z += 1
    
    ans = []
    
    if z == n:
        print(0)
        for i in range(n - 1):
            print(0,0)
        return

    if len(p) == 0:
        if z:
            num1 = 0
            z -= 1
        else:
            m.sort()
            num1 = m[-1]
            m.pop()
        for i in range(len(m)):
            ans.append((num1,m[i]))
            num1 -= m[i]
    
    elif len(m) == 0:
        if z:
            num1 = 0
            z -= 1
            idx = 0
        else:
            p.sort()
            num1 = p[0]
            idx = 1
        for i in range(idx,len(p) - 1):
            ans.append((num1,p[i]))
            num1 -= p[i]
        ans.append((p[-1],num1))
        num1 = p[-1] - num1
    
    elif len(p) * len(m) > 0:
        num1 = p[-1]
        p.pop()
        num2 = m[-1]
        m.pop()
        for i in range(len(m)):
            ans.append((num1,m[i]))
            num1 -= m[i]
        for i in range(len(p)):
            ans.append((num2,p[i]))
            num2 -= p[i]
        ans.append((num1,num2))
        num1 -= num2
    
    for i in range(z):
        ans.append((num1,0))
    print(num1)
    for i in range(n - 1):
        print(*ans[i])

if __name__=='__main__':
    main()