Problem p03113 (python)

Fixed submission:


N,K=list(map(int,input().split()))

a=list(map(int,input().split()))
b=[]
for i in range(N):
    b.append(max(0,K+1-a[i]))

if N==1:
    print(1)
    print(1)
elif sum(b)<=K:
    c=[]
    for i in range(N):
        c.append([b[i],i])
    c.append([K-sum(b),N])
    d=[]
    c=sorted(c,key=lambda x:x[0],reverse=True)
    prev=-1
    whole_flag=True
    for i in range(K):
        c=sorted(c,key=lambda x:x[0],reverse=True)
        flag=False
        for j in range(N+1):
            if c[j][1]!=prev or c[j][1]==N:
                if c[j][0]>0:
                    prev=c[j][1]
                    d.append(c[j][1])
                    c[j][0]-=1
                    flag=True
                    break
        if not flag:
            whole_flag=False
            break
    if not whole_flag:
        print(-1)
    else:
        ans=[]
        prev=-1
        for i in range(K):
            if d[i]!=N:
                e=list(range(1,N+1))
                e.remove(d[i]+1)
                if i!=0:
                    e.remove(prev+1)
                    e.append(prev+1)
                    e=e[::-1]
                ans+=e
                prev=d[i]
            else:
                if i!=0 and i!=K-1:
                    after=d[i+1]
                    if prev!=0 and after!=0:
                        e=list(range(1,N+1))
                        e.remove(1)
                        e.remove(prev+1)
                        e.append(prev+1)
                        e=e[::-1]
                        prev=0
                    elif prev!=1 and after!=1:
                        e=list(range(1,N+1))
                        e.remove(2)
                        e.remove(prev+1)
                        e.append(prev+1)
                        e=e[::-1]
                        prev=1
                    else:
                        e=list(range(1,N+1))
                        e.remove(3)
                        e.remove(prev+1)
                        e.append(prev+1)
                        e=e[::-1]
                        prev=2
                                                
                    ans+=e
                elif i!=K-1:
                    after=d[i+1]
                    e=list(range(1,N+1))
                    if after==0:
                        e.remove(2)
                        prev=1
                    else:
                        e.remove(1)
                        prev=0
                    ans+=e
                elif i!=0:
                    e=list(range(1,N+1))
                    if prev==0:
                        e.remove(2)
                        e.remove(prev+1)
                        e.append(prev+1)
                        e=e[::-1]
                        prev=1
                    else:
                        e.remove(1)
                        e.remove(prev+1)
                        e.append(prev+1)
                        e=e[::-1]
                        prev=0
                    ans+=e

                else:
                    e=list(range(1,N+1))
                    e.remove(1)
                    e=e[::-1]
                    prev=0
                    ans+=e
                    
                    
        if prev==-1:
            e=list(range(1,N+1))
            ans+=e
        else:
            e=list(range(1,N+1))
            e.remove(prev+1)
            e.append(prev+1)
            e=e[::-1]
            ans+=e

        print((N-1)*(K+1)+1)
        print(*ans)
else:
    print(-1)

Buggy submission (verdict: Runtime Error):

N,K=list(map(int,input().split()))

a=list(map(int,input().split()))
b=[]
for i in range(N):
    b.append(max(0,K+1-a[i]))

if N==1:
    print(1)
    print(1)
elif sum(b)<=K:
    c=[]
    for i in range(N):
        c.append([b[i],i])
    c.append([K+1-sum(b),N])
    d=[]
    c=sorted(c,key=lambda x:x[0],reverse=True)
    prev=-1
    whole_flag=True
    for i in range(K+1):
        c=sorted(c,key=lambda x:x[0],reverse=True)
        flag=False
        for j in range(N+1):
            if c[j][1]!=prev or c[j][1]==N:
                if c[j][0]>0:
                    prev=c[j][1]
                    d.append(c[j][1])
                    c[j][0]-=1
                    flag=True
                    break
        if not flag:
            whole_flag=False
            break
    if not whole_flag:
        print(-1)
    else:
        ans=[]
        prev=-1
        for i in range(K):
            if d[i]!=N:
                e=list(range(1,N+1))
                e.remove(d[i]+1)
                if prev!=-1:
                    e.remove(prev+1)
                    e.append(prev+1)
                    e=e[::-1]
                ans+=e
                prev=d[i]
            else:
                if prev==0:
                    e=list(range(1,N+1))
                    e.remove(2)
                    ans+=e
                    prev=1
                else:
                    e=list(range(1,N+1))
                    e.remove(1)
                    if prev!=-1:
                        e.remove(prev+1)
                        e.append(prev+1)
                        e=e[::-1]
                    ans+=e
                    prev=0
        if prev==-1:
            e=list(range(1,N+1))
            ans+=e
        else:
            e=list(range(1,N+1))
            e.remove(prev+1)
            e.append(prev+1)
            e=e[::-1]
            ans+=e

        print((N-1)*(K+1)+1)
        print(*ans)
else:
    print(-1)

Buggy submission (verdict: Runtime Error):

N,K=list(map(int,input().split()))

a=list(map(int,input().split()))
b=[]
for i in range(N):
    b.append(max(0,K+1-a[i]))

if sum(b)<=K:
    c=[]
    for i in range(N):
        c.append([b[i],i])
    c.append([K+1-sum(b),N])
    d=[]
    c=sorted(c,key=lambda x:x[0],reverse=True)
    prev=-1
    whole_flag=True
    for i in range(K+1):
        c=sorted(c,key=lambda x:x[0],reverse=True)
        flag=False
        for j in range(N+1):
            if c[j][1]!=prev or c[j][1]==N:
                if c[j][0]>0:
                    prev=c[j][1]
                    d.append(c[j][1])
                    c[j][0]-=1
                    flag=True
                    break
        if not flag:
            whole_flag=False
            break
    if not whole_flag:
        print(-1)
    else:
        ans=[]
        prev=-1
        for i in range(K):
            if d[i]!=N:
                e=list(range(1,N+1))
                e.remove(d[i]+1)
                if prev!=-1:
                    e.remove(prev+1)
                    e.append(prev+1)
                    e=e[::-1]
                ans+=e
                prev=d[i]
            else:
                if prev==0:
                    e=list(range(1,N+1))
                    e.remove(2)
                    ans+=e
                    prev=1
                else:
                    e=list(range(1,N+1))
                    e.remove(1)
                    if prev!=-1:
                        e.remove(prev+1)
                        e.append(prev+1)
                        e=e[::-1]
                    ans+=e
                    prev=0
        if prev==-1:
            e=list(range(1,N+1))
            ans+=e
        else:
            e=list(range(1,N+1))
            e.remove(prev+1)
            e.append(prev+1)
            e=e[::-1]
            ans+=e

        print((N-1)*(K+1)+1)
        print(*ans)
else:
    print(-1)

Buggy submission (verdict: Runtime Error):
N,K=list(map(int,input().split()))

a=list(map(int,input().split()))
b=[]
for i in range(N):
    b.append(max(0,K+1-a[i]))
    
if sum(b)<=K:
    ans=""

    for i in range(N):
        if b[i]>0:
            c=list(range(1,N+1))
            c.remove(i+1)
            c.append(i+1)
            c=list(map(str,c))
            " ".join(c)
            d = ""
            for j in range(b[i]):
                d+=c+" "
            ans+=d
            temp=i

    c=list(range(1,N+1))
    c=list(map(str,c))
    c=" ".join(c)

    for i in range(K-sum(b)):
        ans+=c+" "        
    c=list(range(1,N))
    c=list(map(str,c))
    c=" ".join(c)
    ans+=c
        
    print((K+1)*N - K)
    print(ans)
else:
    print(-1)

Buggy submission (verdict: Runtime Error):

N,K=list(map(int,input().split()))

a=list(map(int,input().split()))
b=[]
for i in range(N):
    b.append(max(0,K+1-a[i]))
    
if sum(b)<=K:
    ans=""
    temp=-1
    for i in range(N):
        if b[i]>0:
            c=list(range(1,N+1))
            c.remove(i+1)
            c.append(i+1)
            c=list(map(str,c))
            " ".join(c)
            d = ""
            for j in range(b[i]):
                d+=c+" "
            ans+=d
            temp=i
    if temp==-1:
        c=list(range(1,N+1))
        c=list(map(str,c))
        c=" ".join(c)

        for i in range(K-sum(b)):
            ans+=c+" "        
        c=list(range(1,N+1))
        c.remove(N)
        c=list(map(str,c))
        c=" ".join(c)
        ans+=c
    else:
        c=list(range(1,N+1))
        c.remove(temp+1)
        c.append(temp+1)
        c=list(map(str,c))
        c=" ".join(c)

        for i in range(K-sum(b)):
            ans+=c+" "        
        c=list(range(1,N+1))
        c.remove(temp+1)
        c=list(map(str,c))
        c=" ".join(c)
        ans+=c
        
    print((K+1)*N - K)
    print(ans)
else:
    print(-1)