Advertisement
Egor_1425

Untitled

Aug 1st, 2024
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.61 KB | None | 0 0
  1. from math import log2
  2.  
  3. for t in range(int(input())):
  4.     n, m = map(int, input().split())
  5.     c = [0] * 61
  6.     s = 0
  7.     for x in map(int, input().split()):
  8.         c[int(log2(x))] += 1
  9.         s += x
  10.  
  11.     if s < n:
  12.         print(-1)
  13.         continue
  14.  
  15.     i, res = 0, 0
  16.     while i < 60:
  17.         if (1<<i)&n != 0:
  18.             if c[i] > 0:
  19.                 c[i] -= 1
  20.             else:
  21.                 while i < 60 and c[i] == 0:
  22.                     i += 1
  23.                     res += 1
  24.                 c[i] -= 1
  25.                 continue
  26.         c[i + 1] += c[i] // 2
  27.         i += 1
  28.  
  29.     print(res)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement