結果

問題 No.2869 yuusaan's Knapsacks
ユーザー PNJPNJ
提出日時 2024-09-02 10:40:41
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,637 bytes
コンパイル時間 343 ms
コンパイル使用メモリ 82,492 KB
実行使用メモリ 281,208 KB
最終ジャッジ日時 2024-09-02 10:40:53
合計ジャッジ時間 11,458 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
62,400 KB
testcase_01 AC 54 ms
64,884 KB
testcase_02 AC 74 ms
74,508 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 40 ms
54,192 KB
testcase_06 TLE -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 998244353
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 ranked_zeta_transform(f):
  n = (len(f) - 1).bit_length()
  Rf = [[0 for _ in range(n + 1)] for _ in range(1 << n)]
  for s in range(1 << n):
    Rf[s][popcount(s)] = f[s]
  for i in range(n):
    w = 1 << i
    for p in range(0,1 << n,2 * w):
      for s in range(p,p + w):
        t = s | (1 << i)
        for d in range(n + 1):
          Rf[t][d] = (Rf[t][d] + Rf[s][d]) % mod
  return Rf

def ranked_mobius_transform(Rf):
  n = (len(Rf) - 1).bit_length()
  for i in range(n):
    w = 1 << i
    for p in range(0,1 << n,2 * w):
      for s in range(p,p + w):
        t = s | (1 << i)
        for d in range(n + 1):
          Rf[t][d] = (Rf[t][d] - Rf[s][d]) % mod
  f = [0 for _ in range(1 << n)]
  for s in range(1 << n):
    f[s] = Rf[s][popcount(s)]
  return f

def subset_convolution(A,B):
  RA = ranked_zeta_transform(A)
  RB = ranked_zeta_transform(B)
  n = (len(RA) - 1).bit_length()
  for s in range(1 << n):
    for d in range(n,-1,-1):
      x = 0
      for i in range(d + 1):
        x = (x + RA[s][i] * RB[s][d - i] % mod) % mod
      RA[s][d] = x
  C = ranked_mobius_transform(RA)
  for i in range(len(C)):
    if C[i] > 0:
      C[i] = 1
  return C

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
  C = subset_convolution(dp[n - 1],B)
  dp[n] = C[:]

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 << N):
    if dp[n - 1][s ^ ss]:
      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