結果

問題 No.2869 yuusaan's Knapsacks
ユーザー PNJPNJ
提出日時 2024-09-02 10:58:31
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 383 ms / 4,500 ms
コード長 2,675 bytes
コンパイル時間 653 ms
コンパイル使用メモリ 82,104 KB
実行使用メモリ 109,240 KB
最終ジャッジ日時 2024-09-02 10:58:39
合計ジャッジ時間 7,919 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,444 KB
testcase_01 AC 41 ms
60,220 KB
testcase_02 AC 44 ms
60,064 KB
testcase_03 AC 165 ms
84,912 KB
testcase_04 AC 164 ms
85,016 KB
testcase_05 AC 38 ms
53,728 KB
testcase_06 AC 377 ms
107,884 KB
testcase_07 AC 171 ms
86,200 KB
testcase_08 AC 73 ms
69,348 KB
testcase_09 AC 68 ms
68,020 KB
testcase_10 AC 243 ms
92,536 KB
testcase_11 AC 129 ms
82,288 KB
testcase_12 AC 316 ms
101,512 KB
testcase_13 AC 129 ms
82,240 KB
testcase_14 AC 197 ms
88,668 KB
testcase_15 AC 189 ms
88,296 KB
testcase_16 AC 201 ms
90,540 KB
testcase_17 AC 358 ms
108,816 KB
testcase_18 AC 285 ms
96,140 KB
testcase_19 AC 238 ms
94,836 KB
testcase_20 AC 68 ms
68,040 KB
testcase_21 AC 260 ms
96,732 KB
testcase_22 AC 266 ms
92,504 KB
testcase_23 AC 142 ms
82,380 KB
testcase_24 AC 301 ms
103,136 KB
testcase_25 AC 104 ms
75,668 KB
testcase_26 AC 263 ms
92,276 KB
testcase_27 AC 38 ms
53,012 KB
testcase_28 AC 383 ms
109,240 KB
testcase_29 AC 370 ms
107,724 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 67280421310721

def popcount(n):
  c=(n&0x5555555555555555)+((n>>1)&0x5555555555555555)
  c=(c&0x3333333333333333)+((c>>2)&0x3333333333333333)
  c=(c&0x0f0f0f0f0f0f0f0f)+((c>>4)&0x0f0f0f0f0f0f0f0f)
  c=(c&0x00ff00ff00ff00ff)+((c>>8)&0x00ff00ff00ff00ff)
  c=(c&0x0000ffff0000ffff)+((c>>16)&0x0000ffff0000ffff)
  c=(c&0x00000000ffffffff)+((c>>32)&0x00000000ffffffff)
  return c

def zeta_transform(f,upper = 0):
  n = (len(f) - 1).bit_length()
  if upper:
    for i in range(n):
      k = 1 << i
      for bit in range(1 << n):
        if bit & k == 0:
          f[bit] = (f[bit] + f[bit ^ k]) % mod
    return
  for i in range(n):
    k = 1 << i
    for bit in range(1 << n):
      if bit & k:
        f[bit] = (f[bit] + f[bit ^ k]) % mod
  return

def mobius_transform(f,upper = 0):
  n = (len(f) - 1).bit_length()
  if upper:
    for i in range(n):
      k = 1 << i
      for bit in range(1 << n):
        if bit & k == 0:
          f[bit] = (f[bit] - f[bit ^ k]) % mod
    return
  for i in range(n):
    k = 1 << i
    for bit in range(1 << n):
      if bit & k:
        f[bit] = (f[bit] - f[bit ^ k]) % mod
  return

def or_convolution(a,b):
  f = a[:]
  g = b[:]
  zeta_transform(f)
  zeta_transform(g)
  for i in range(len(f)):
    f[i] = f[i] * g[i] % mod
  mobius_transform(f)
  for i in range(len(f)):
    if f[i] > 0:
      f[i] = 1
  return f

def and_convolution(a,b):
  f = a[:]
  g = b[:]
  zeta_transform(f,1)
  zeta_transform(g,1)
  for i in range(len(f)):
    f[i] = f[i] * g[i] % mod
  mobius_transform(f,1)
  return f

N,M = map(int,input().split())
E = list(map(int,input().split()))
V,W = [0] * M,[0] * M
for i in range(M):
  V[i],W[i] = map(int,input().split())

SW = [0 for _ in range(1 << M)]
SV = [0 for _ in range(1 << M)]
for s in range(1 << M):
  w,v = 0,0
  for j in range(M):
    if (s >> j) & 1:
      w += W[j]
      v += V[j]
  SW[s] = w
  SV[s] = v

dp = [[0 for s in range(1 << M)] for n in range(N)]
for n in range(N):
  if n == 0:
    for s in range(1 << M):
      if SW[s] <= E[n]:
        dp[n][s] = 1
    continue
  B = [0 for s in range(1 << M)]
  for s in range(1 << M):
    if SW[s] <= E[n]:
      B[s] = 1
  dp[n] = or_convolution(dp[n - 1],B)

ans = 0
s = 0
for ss in range(1 << M):
  if dp[-1][ss]:
    if SV[ss] > ans:
      ans = SV[ss]
      s = ss

res = [0 for i in range(N)]
for n in range(N - 1,0,-1):
  for ss in range(1 << M):
    if (s & ss) != ss:
      continue
    if dp[n - 1][s ^ ss] & (SW[ss] <= E[n]):
      res[n] = ss
      s ^= ss
      break
res[0] = s

print(ans)
for n in range(N):
  X = []
  X.append(popcount(res[n]))
  for j in range(M):
    if (res[n] >> j) & 1:
      X.append(j + 1)
  print(*X)
0