結果

問題 No.1872 Dictionary Order
ユーザー ああいい
提出日時 2022-03-11 22:28:27
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 188 ms / 2,000 ms
コード長 793 bytes
コンパイル時間 347 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 107,904 KB
最終ジャッジ日時 2024-09-19 08:42:20
合計ジャッジ時間 4,571 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

N,M = map(int,input().split())
A = list(map(int,input().split()))
P = list(map(int,input().split()))

dp = [[0] * (M + 1) for _ in range(N+1)]
dp[N-1][0] = 1
dp[N-1][A[-1]] = 1
dp[-1][0] = 1
for i in reversed(range(N-1)):
    for j in range(M+1):
        dp[i][j] = dp[i+1][j]
    for j in range(M+1):
        if j + A[i] <= M:
            if dp[i+1][j]:
                dp[i][j+A[i]] = 1
        else:
            break
import sys
if dp[0][-1] == 0:
    print(-1)
    exit()
ans = []
now = M
left = -1
while now:
    _min = 10000
    index = -1
    for i in range(left+1,N):
        if A[i] <= now and dp[i+1][now-A[i]]:
            if P[i] < _min:
                _min = P[i]
                index = i
    ans.append(index+1)
    now -= A[index]
    left = index
print(len(ans))
print(*ans)
0