<jupyter_start><jupyter_text><jupyter_code>from operator import itemgetter
# from memo import memo
# import sys
# sys.setrecursionlimit(150000)
i_file = open('a_example.txt', 'r')
line1 = i_file.readline()
line1_list = list(map(int, line1.strip().split(" ")))

B = line1_list[0]
L = line1_list[1]
D = line1_list[2]
line2 = i_file.readline()
line2_list = list(map(int, line2.strip().split(" ")))

S = line2_list
libraries = []

for j in range(L):
  L_line1 = i_file.readline()
  L_line1_list = list(map(int, L_line1.strip().split(" ")))
  N = L_line1_list[0]
  T = L_line1_list[1]
  M = L_line1_list[2]

  ratio = float(T)*float(M)/float(N)

  scan_days = M//N + (M%N>0)
  
  L_line2 = i_file.readline()
  L_line2_list = list(map(int, L_line2.strip().split(" ")))
  books = L_line2_list

  libraries.append([N, T, M, ratio, books, scan_days])
i_file.close()
# print (libraries)
libraries_sorted = sorted(libraries, key=itemgetter(3), reverse=False)
# libraries_sorted = libraries
# print (libraries_sorted)
# print (max([item[3] for item in libraries]))
# def fn(D, j, libraries_sorted):
#   if (D!=0):
#     if (j<=L-1):
#       if (libraries_sorted[j][5] != 0):
#         if (libraries_sorted[j][1] != 0):
#           libraries_sorted[j][1] -= 1
#           D -= 1
#           return fn(D, j, libraries_sorted)
#         else:
#           if ((j < L-1) and (libraries_sorted[j+1][1] < libraries_sorted[j][5])):
#             j += 1
#             return fn(D, j, libraries_sorted)
#           else:
#             libraries_sorted[j][5] -= 1
#             D -= 1
#             return fn(D, j, libraries_sorted)
#       else:
#         j += 1
#         return fn(D, j, libraries_sorted)
#   else:
#     return j

l_all_books = [i for i in range(len(S))]

l_all_books_sorted = [x for _,x in sorted(zip(S,l_all_books), reverse=True)]
# print(l_all_books_sorted)

for v in range(len(libraries_sorted)):
  libraries_sorted[v][4].sort(key=lambda x: l_all_books_sorted.index(x))

j = 0
lib_output = []
lib = [libraries.index(libraries_sorted[j])]
l_books = []
while (D >= 0):
  if (j <= L-1):
    if (libraries_sorted[j][5] != 0):
      if (libraries_sorted[j][1] != 0):
        libraries_sorted[j][1] -= 1
        D -= 1
      else:
        if ((j < L-1) and (libraries_sorted[j+1][1] >= libraries_sorted[j][5])):
          if (libraries_sorted[j][5] <= D):
            l_books1 = libraries_sorted[j][4]
            for i in range(len(l_books1)):
              if l_books1[i] in l_all_books:
                l_books.append(l_books1[i])
                l_all_books.remove(l_books1[i])
            libraries_sorted[j][5] = 0
          else:
            l_books1 = libraries_sorted[j][4][0:(D*libraries_sorted[j][2])]
            for i in range(len(l_books1)):
              if l_books1[i] in l_all_books:
                l_books.append(l_books1[i])
                l_all_books.remove(l_books1[i])
            libraries_sorted[j][5] -= D

          if (len(l_books) > 0):
            lib.append(l_books)
            lib_output.append(lib)
          j += 1
          lib = [libraries.index(libraries_sorted[j])]
          l_books = []
        else:
          for i in range(libraries_sorted[j][2]):
            if (len(libraries_sorted[j][4]) > 0):
              if libraries_sorted[j][4][0] in l_all_books:
                l_all_books.remove(libraries_sorted[j][4][0])
                l_books.append(libraries_sorted[j][4].pop(0))
              else:
                libraries_sorted[j][4].pop(0)
              libraries_sorted[j][5] -= 1
          D -= 1
          if ((D == 0) and (len(l_books) > 0)):
            lib.append(l_books)
            lib_output.append(lib)
    else:
      if (len(l_books) > 0):
        lib.append(l_books)
        lib_output.append(lib)
      j += 1
      lib = [libraries.index(libraries_sorted[j])]
      l_books = []
print (lib_output)
o_file = open('output.txt', 'w')
o_file.write(str(len(lib_output)))

for i in range(len(lib_output)):
  o_file.write("\n")
  o_file.write(str(lib_output[i][0]) + " " + str(len(lib_output[i][1])))
  o_file.write("\n")
  o_file.write(' '.join(map(str,lib_output[i][1])))

o_file.close()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16383233532934133, 'fraction_numerical': 0.026107784431137725, 'mean_word_length': 2.5814751286449398, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '100453', 'license': 'Apache-2.0', 'n_tokens_mistral': 1660, 'n_tokens_neox': 1626, 'n_words': 285, 'path': 'Hash_Code_2020_Online_Qualification_Round.ipynb', 'repo_name': 'thuvarahan97/Hash_Code_2020_Online_Qualification_Round', 'size': '4175'}