Problem:
 Score : 1000 points    Problem Statement Snuke participated in a magic show.  A magician prepared N identical-looking boxes.
He put a treasure in one of the boxes, closed the boxes, shuffled them, and numbered them 1 through N .  Since the boxes are shuffled, now Snuke has no idea which box contains the treasure.
Snuke wins the game if he opens a box containing the treasure.
You may think that if Snuke opens all boxes at once, he can always win the game.
However, there are some tricks:   Snuke must open the boxes one by one. After he opens a box and checks the content of the box, he must close the box before opening the next box.  He is only allowed to open Box i at most a_i times.  The magician may secretly move the treasure from a closed box to another closed box, using some magic trick.
For simplicity, assume that the magician never moves the treasure while Snuke is opening some box.
However, he can move it at any other time (before Snuke opens the first box, or between he closes some box and opens the next box).  The magician can perform the magic trick at most K times.   Can Snuke always win the game, regardless of the initial position of the treasure and the movements of the magician?      Constraints  2 \leq N \leq 50  1 \leq K \leq 50  1 \leq a_i \leq 100         Input Input is given from Standard Input in the following format:  N  K  a_1  a_2  \cdots  a_N       Output If the answer is no, print a single -1 .  Otherwise, print one possible move of Snuke in the following format:  Q  x_1  x_2  \cdots  x_Q   It means that he opens boxes x_1, x_2, \cdots, x_Q in this order.  In case there are multiple possible solutions, you can output any.        Sample Input 1 2 1
5 5      Sample Output 1 7
1 1 2 1 2 2 1  If Snuke opens the boxes 7 times in the order 1 \rightarrow 1 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 2 \rightarrow 1 , he can always find the treasure regardless of its initial position and the movements of the magician.       Sample Input 2 3 50
5 10 15      Sample Output 2 -1

Solution (Python):
s=sorted
n,k,*a=map(int,open(i:=0).read().split())
l=s([-a,i:=i+1]for a in a)
_,r=zip(*l)
exec('*_,b=r\nfor t in s(l):\n if t[1]-b:t[t[0]>-2<exit(print(-1))]+=1;r+=t[1],\n'*k)
print(len(r),*r)

Solution (Python):
S=sorted
n,k,*a=map(int,open(i:=0).read().split())
l=S([-a,i:=i+1]for a in a)
_,r=zip(*l)
while k:
 *_,b=r;k-=1
 for t in S(l):
  if t[1]-b:t[t[0]>-2<exit(print(-1))]+=1;r+=t[1],
print(len(r),*r)

Solution (Python):
S=sorted
n,k,*t=map(int,open(i:=0).read().split())
l=S([a,i:=i+1]for a in t)[::-1]
_,r=zip(*l)
while k:
 *_,b=r;k-=1
 for t in S(l)[::-1]:
  if t[1]-b:t[t[0]<2<exit(print(-1))]-=1;r+=t[1],
print(len(r),*r)

Solution (Python):
P,S=print,sorted
n,k,*t=map(int,open(i:=0).read().split())
l=S([a-1,i:=i+1]for a in t)[::-1]
_,r=zip(*l)
while k:
 *_,b=r;k-=1
 for t in S(l)[::-1]:
  if t[1]-b:t[t[0]<1<exit(P(-1))]-=1;r+=t[1],
P(len(r),*r)

Solution (Python):
P,S=print,sorted
n,k,*t=map(int,open(i:=0).read().split())
l=S([a-1,a<1<exit(P(-1)),i:=i+1]for a in t)[::-1]
*_,r=zip(*l)
while k:
 *_,b=r;k-=1
 for t in S(l)[::-1]:
  if t[2]-b:t[t[0]<1<exit(P(-1))]-=1;r+=t[2],
P(len(r),*r)

Solution (Python):
P,S=print,sorted
n,k,*t=map(int,open(i:=0).read().split())
l=S([a<1<exit(P(-1)),a-1,i:=i+1]for a in t)[::-1]
*_,r=zip(*l)
while k:
 *_,b=r;k-=1
 for t in S(l)[::-1]:
  if t[2]-b:t[1]or exit(P(-1));t[1]-=1;r+=t[2],
P(len(r),*r)

Solution (Python):
P,S=print,sorted
n,k,*t=map(int,open(i:=0).read().split())
l=S([a<1<exit(P(-1)),a-1,i:=i+1]for a in t)[::-1]
*_,r=zip(*l)
while k:
 b=r;k-=1
 for t in S(l)[::-1]:
  if t[2]-b[-1]:t[1]or exit(P(-1));t[1]-=1;r+=t[2],
P(len(r),*r)

Solution (Python):
P,S=print,sorted
n,k,*t=map(int,open(i:=0).read().split())
l=S([a<1<exit(P(-1)),a-1,i:=i+1]for a in t)[::-1]
*_,r=zip(*l)
b=r
while k:
 for t in S(l)[::-1]:
  if t[2]-b[-1]:t[1]or exit(P(-1));t[1]-=1;r+=t[2],
 b=r;k-=1
P(len(r),*r)

Solution (Python):
P,S=print,sorted
n,k,*t=map(int,open(i:=0).read().split())
l=S([a<1<exit(P(-1)),a-1,i:=i+1]for a in t)[::-1]
*_,r=zip(*l)
*_,b=r
while k:
 for t in S(l)[::-1]:
  if t[2]-b:t[1]or exit(P(-1));t[1]-=1;r+=t[2],
 b=r[-1];k-=1
P(len(r),*r)

Solution (Python):
P,S=print,sorted
n,k,*t=map(int,open(i:=0).read().split())
l=S([a<1<exit(P(-1)),a-1,i:=i+1]for a in t)[::-1]
*_,b=r=[*zip(*l)][2]
while k:
 for t in S(l)[::-1]:
  if t[2]-b:t[1]or exit(P(-1));t[1]-=1;r+=t[2],
 b=r[-1];k-=1
P(len(r),*r)

Solution (Python):
P,S=print,sorted
n,k,*t=map(int,open(i:=0).read().split())
l=S([a<1<exit(P(-1)),a-1,i:=i+1]for a in t)[::-1]
*_,b=r=[*zip(*l)][2]
for i in range(k):
 for t in S(l)[::-1]:
  if t[2]-b:t[1]or exit(P(-1));t[1]-=1;r+=t[2],
 b=r[-1]
P(len(r),*r)

Solution (Python):
import sys
n,k=map(int,input().split())
a=list(map(int,input().split()))
for i in range(n):
    a[i]=[a[i],i+1]

one=-1
ans=[]
a.sort(reverse=True)
for _ in range(k+1):
    a.sort(reverse=True)
    for j in range(n):
        if a[j][1]!=one:
            a[j][0]-=1
            ans.append(a[j][1])
    one=ans[-1]
    a.sort(reverse=True)

for x in a:
    if x[0]<0:
        print(-1)
        sys.exit()
print(len(ans))
print(*ans)

Solution (Python):
N, K = list(map(int, input().split()))
a = list(map(int, input().split()))

def nasu(x, L):
  t = 1000
  a = -1
  for i in range(len(L)):
    if i == x: continue
    if L[i] < t:
      t = L[i]
      a = i
  return a

L = []
to = nasu(-1, a)
fr = -1
for i in range(K + 1):
  for j in range(N):
    if j == to: continue
    if j == fr: continue
    a[j] -= 1
    L.append(j + 1)
  if a[to] == 0:
    print(-1)
    exit()
  a[to] -= 1
  L.append(to + 1)
  fr = to
  to = nasu(to, a)

print(len(L))
for i in L:
  print(i, end = " ")
print("")

Solution (Python):
import sys

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

a = list(map(int,input().split()))

a2 = []

for i in range(N):
    a2.append([a[i],i+1])

ans = []
mind = 0

for i in range(K+1):

    a2.sort()
    a2.reverse()

    omind = mind

    if i == 0:
        for j in range(N):
            a2[j][0] -= 1
            ans.append(a2[j][1])

            if a2[j][0] < 0:
                print (-1)
                sys.exit()

        mind = a2[-1][1]

    else:

        for j in range(N):

            if a2[j][1] != mind:

                a2[j][0] -= 1
                ans.append(a2[j][1])

            if a2[j][0] < 0:
                print (-1)
                sys.exit()

        a2.sort()
        a2.reverse()
        if mind == a2[-1][1]:
            mind = a2[-2][1]
        else:
            mind = a2[-1][1]

print (len(ans))
print (" ".join(map(str,ans)))

Solution (Python):
n, k = map(int, input().split())
a = list(map(int, input().split()))
ans = []
now = -1
for i in range(k+1):
    mi = 9999
    pos = 0
    for j in range(n):
        if now != j and a[j] <= mi:
            pos = j
            mi = a[j]
    for j in range(n):
        if j != now and j != pos:
            ans.append(j+1)
            a[j] -= 1
            if a[j] < 0:
                print(-1)
                exit()
    ans.append(pos+1)
    a[pos] -= 1
    if a[pos] < 0:
        print(-1)
        exit()
    now = pos
print(len(ans))
print(*ans)

Solution (Python):
N,K=map(int,input().split())
A=[]
ans=[]
for i,a in enumerate(input().split()):
  A.append((int(a),i))
unuse=N
for s in range(K+1):
  A.sort(reverse=True)
  for i in range(N):
    a,k=A[i]
    if k==unuse:
      continue
    if a==0:
      print(-1)
      exit()
    ans.append(str(k+1))
    A[i]=(a-1,k)
  unuse=int(ans[-1])-1
 
print(len(ans))
print(" ".join(ans))

Solution (Python):
N,K = map(int,input().split())
A = list(map(int,input().split()))

mina = min(A)
mini = A.index(mina)

ans = []
for i in range(N):
    if i == mini: continue
    ans.append(i+1)
    A[i] -= 1
ans.append(mini + 1)
A[mini] -= 1

for _ in range(K):
    tmpa = 9999
    tmpi = -1
    for i in range(N):
        if i == mini: continue
        if A[i] < tmpa:
            tmpa = A[i]
            tmpi = i
    for i in range(N):
        if i == mini or i == tmpi: continue
        ans.append(i+1)
        A[i] -= 1
        if A[i] < 0:
            print(-1)
            exit()
    ans.append(tmpi + 1)
    A[tmpi] -= 1
    if A[tmpi] < 0:
        print(-1)
        exit()
    mini = tmpi

print(len(ans))
print(*ans)

Solution (Python):
from operator import itemgetter
from functools import reduce
n, k = list(map(int, input().split()))
a = []
for i, v in enumerate(input().split(), 1):
    a.append([int(v) - 1, i])
a.sort(reverse=True)
ans = list(map(itemgetter(1), a))
last = ans[-1]
for _ in range(k):
    for item in a:
        if item[1] == last:
            continue
        item[0] -= 1
        ans.append(item[1])
    a.sort(reverse=True)
    last = ans[-1]

ok = reduce(lambda x, y: x and y[0] >= 0, a, True)
if ok:
    print(len(ans))
    print(*ans)
else:
    print(-1)

Solution (Python):
n, k = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]

B = []
for i, a in zip(range(1, n+1), A):
    B.append([a, i])

B.sort(reverse=True)
ans = []

for i in range(n):
    B[i][0] -= 1
    B[i].append(i)
    ans.append(B[i][1])

last = n - 1
for i in range(k):
    mixi = min(B[:last] + B[last+1:])[2]
    cnt = 1
    for j in range(n):
        if j == last or j == mixi:
            continue
        B[j][0] -= 1
        if B[j][0] < 0:
            print(-1)
            exit()
        ans.append(B[j][1])

    ans.append(B[mixi][1])
    B[mixi][0] -= 1
    if B[mixi][0] < 0:
        print(-1)
        exit()
    last = mixi

print(len(ans))
print(*ans)

Solution (Python):
I=lambda:map(int,input().split())
n,k=I()
l=[[a-1,i]for i,a in enumerate(I(),1)]
for a,_ in l:
  if a<0:
    print(-1)
    exit()
l.sort(reverse=1)
r=[i for _,i in l]
b=r[-1]
for i in range(k):
  for t in l:
    if t[1]==b:continue
    if not t[0]:
      print(-1)
      exit()
    t[0]-=1
    r.append(t[1])
  l.sort(reverse=1)
  b=r[-1]
print(len(r))
print(*r)

Solution (Python):
I=lambda:map(int,input().split())
n,k=I()
l=[[a-1,i]for i,a in enumerate(I(),1)]
for a,_ in l:
  if a<0:
    print(-1)
    exit()
l.sort(reverse=1)
r=[i for _,i in l]
b=r[-1]
for i in range(k):
  for t in l:
    if t[1]==b:continue
    if not t[0]:
      print(-1)
      exit()
    t[0]-=1
    r.append(t[1])
  l.sort(reverse=1)
  b=r[-1]
print(len(r))
print(*r)

Solution (Python):


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)

Solution (Python):
n, k = map(int, input().split())
a = list(map(int, input().split()))
if min(a) <= 0:
    print(-1)
    exit()
s = sorted(enumerate(a), key = lambda x: x[1], reverse = True)
def check(s, k, ans, exc):
    news = []
    if k < 0:
        return ans
    for i in range(n):
        if s[i][0] == exc:
            news.append(s[i])
        else:
            if s[i][1] <= 0:
                print(-1)
                exit()
            news.append((s[i][0], s[i][1] - 1))
            ans.append(s[i][0] + 1)
    news.sort(key = lambda x: x[1], reverse = True)
    if exc == news[n-1][0]:
        exc = news[n-2][0]
    else:
        exc = news[n-1][0]
    return check(news, k-1, ans, exc)
ans = check(s, k, [], n + 1)
print(n + (n-1) * k)
for ansint in ans:
    print(ansint, end = ' ')
print()

Solution (Python):
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())


N, K = inpl()
aa = inpl()

ans = []
min_ind_before = -1

for i in range(K+1):
    if i != 0:
        aa[min_ind_before] += 1000

    MIN = min(aa)
    if MIN <= 0:
        print(-1)
        sys.exit()

    min_ind = aa.index(MIN)

    if i != 0:
        aa[min_ind_before] -= 1000

    for j in range(N):
        if j == min_ind or j == min_ind_before:
            continue
        else:
            ans.append(j+1)
            aa[j] -= 1

    ans.append(min_ind+1)
    aa[min_ind] -= 1
    min_ind_before = min_ind




print(len(ans))
print (' '.join(map(str,ans)))

Solution (Python):
N,K = map(int, raw_input().split())
a = map(int, raw_input().split())

ans = []
p = -1
for k in range(K+1):
  m = -1
  for i in range(N):
    if i!=p and (m==-1 or a[i]<a[m]):
      m = i
  for i in range(N):
    if i!=p and i!=m:
      if a[i]==0:
        print -1
        exit(0)
      ans += [i]
      a[i] -= 1
  if a[m]==0:
    print -1
    exit(0)
  ans += [m]
  a[m] -= 1
  p = m
print len(ans)
for x in ans:
  print x+1,
print

Solution (Python):




def main():
  _, move = map(int, input().split())
  remains = [(i, v) for i, v in enumerate(map(int, input().split()))]
  remains.sort(key=lambda x:x[1])

  lasts = [remains[0][0]]
  remains = [(i, v - 1) for i, v in remains]

  for _ in range(move):
    remains.sort(key=lambda x:x[1])

    if remains[0][0] == lasts[-1]:
      lasts.append(remains[1][0])
    else:
      lasts.append(remains[0][0])

    remains = [(i, v - 1 if i != lasts[-2] else v) for i, v in remains]

  remains.sort(key=lambda x:x[1])

  if remains[0][1] < 0:
    print('-1', flush=True)
    return

  opens = []
  last = -1

  for i in range(len(lasts)):
    opens.extend(j for j in range(len(remains)) if j != lasts[i] and j != last)
    opens.append(lasts[i])
    last = lasts[i]

  print(len(opens), flush=True)
  print(' '.join(str(v + 1) for v in opens), flush=True)


if __name__ == '__main__':
  main()

Solution (Python):
def solve(n, k, aaa):
    buf = []
    prev_last = -1

    for t in range(k + 1):
        ma, mi = min((a, i + 1) for i, a in enumerate(aaa) if i + 1 != prev_last)
        others = set(range(1, n + 1)) - {prev_last, mi}
        buf.extend(others)
        buf.append(mi)
        

        for i in range(n):
            if i + 1 != prev_last:
                aaa[i] -= 1
                if aaa[i] < 0:
                    print(-1)
                    return

        prev_last = mi

    print(len(buf))
    print(*buf)


n, k = map(int, input().split())
aaa = list(map(int, input().split()))
solve(n, k, aaa)