• + 1 comment
    n=int(input())
    k=[]
    l=[]
    f=0
    z=[]
    for i in range(n):
        a,b=input().split()
        for j in range(1,2*int(a)):
            if j<int(a):
                k.append(j)
            elif j==int(a):
                k.append(j)
                k.append(-j)
            elif j>int(a):
                x=j-2*int(a)
                k.append(x)
        if int(b)>0:
            l.append(int(b))
            f=k[int(b)+int(b)-1]
            while f in k and l.count(f)<=1:
                if f>0:
                    if f not in l:
                        l.append(f)
                    f=k[f+f-1]
                elif f<0:
                    if f not in l:
                        l.append(f)
                    f=k[k[f]+f]
                    l.append(f)
        l=list(dict.fromkeys(l))
        if int(b)<0:
            l.append(int(b))
            f=k[k[int(b)]+int(b)]
            while f in k and l.count(f)<=1:
                if f>0:
                    if f not in l:
                        l.append(f)
                    f=k[f+f-1]
                elif f<0:
                    if f not in l:
                        l.append(f)
                    f=k[k[f]+f]
                    l.append(f)
        l=list(dict.fromkeys(l))
        k.sort()
        l.sort()
        for s in k:
                if s not in l:
                    z.append(s)
        u=0
        for i in z:
            u+=1
        print(u)
    		
    		My test cases 0 and 1 ran successfully, but for the remaining test cases, it says terminated due to timeout. Please suggest edits to my code.
        k=[]
        l=[]
        f=0
        z=[]