結果

問題 No.2866 yuusaan's Knapsack
ユーザー katonyonkokatonyonko
提出日時 2024-09-10 18:29:08
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,324 ms / 2,000 ms
コード長 2,227 bytes
コンパイル時間 362 ms
コンパイル使用メモリ 82,552 KB
実行使用メモリ 434,916 KB
最終ジャッジ日時 2024-09-10 18:29:29
合計ジャッジ時間 21,207 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 114 ms
97,084 KB
testcase_01 AC 97 ms
83,808 KB
testcase_02 AC 155 ms
99,484 KB
testcase_03 AC 85 ms
81,780 KB
testcase_04 AC 82 ms
81,456 KB
testcase_05 AC 82 ms
81,808 KB
testcase_06 AC 996 ms
324,108 KB
testcase_07 AC 827 ms
281,132 KB
testcase_08 AC 941 ms
303,812 KB
testcase_09 AC 881 ms
291,316 KB
testcase_10 AC 952 ms
298,564 KB
testcase_11 AC 614 ms
234,332 KB
testcase_12 AC 866 ms
288,224 KB
testcase_13 AC 644 ms
243,108 KB
testcase_14 AC 855 ms
283,512 KB
testcase_15 AC 941 ms
312,980 KB
testcase_16 AC 664 ms
246,516 KB
testcase_17 AC 940 ms
295,152 KB
testcase_18 AC 623 ms
231,032 KB
testcase_19 AC 1,034 ms
328,804 KB
testcase_20 AC 851 ms
275,036 KB
testcase_21 AC 924 ms
292,880 KB
testcase_22 AC 863 ms
281,828 KB
testcase_23 AC 1,023 ms
331,836 KB
testcase_24 AC 714 ms
259,356 KB
testcase_25 AC 928 ms
290,008 KB
testcase_26 AC 1,324 ms
434,916 KB
testcase_27 AC 111 ms
86,800 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import io
import sys
from collections import defaultdict, deque, Counter
from itertools import permutations, combinations, accumulate
from heapq import heappush, heappop
from bisect import bisect_right, bisect_left
from math import gcd
import math

_INPUT = """\
6
5 10
4 3
2 1
5 5
4 4
3 3
2 0
-100 -100
500 100
6 -2
6 5
1 -6
6 4
6 6
-3 -5
7 -2
"""

def input():
  return sys.stdin.readline()[:-1]

inf=10**20
mod=998244353
def solve(test):
  N,W=map(int, input().split())
  bag=sorted([list(map(int, input().split())) for _ in range(N)],key=lambda x:x[1])
  def idx(i,j):
    return i*(20000+1)+j
  dp=[[-inf,0] for _ in range((N+1)*(20000+1))]
  dp[idx(0,10000)]=[0,1]
  # print(bag)
  for i in range(N):
    v,w=bag[i]
    for j in range(20000+1):
      if dp[idx(i+1,j)][0]<dp[idx(i,j)][0]:
        dp[idx(i+1,j)][1]=dp[idx(i,j)][1]
      elif dp[idx(i+1,j)][0]==dp[idx(i,j)][0]:
        dp[idx(i+1,j)][1]+=dp[idx(i,j)][1]
        dp[idx(i+1,j)][1]%=mod
      dp[idx(i+1,j)][0]=max(dp[idx(i+1,j)][0],dp[idx(i,j)][0])
      if 0<=j+w<=20000:
        if dp[idx(i+1,j+w)][0]<dp[idx(i,j)][0]+v:
          dp[idx(i+1,j+w)][1]=dp[idx(i,j)][1]
        elif dp[idx(i+1,j+w)][0]==dp[idx(i,j)][0]+v:
          dp[idx(i+1,j+w)][1]+=dp[idx(i,j)][1]
          dp[idx(i+1,j+w)][1]%=mod
        dp[idx(i+1,j+w)][0]=max(dp[idx(i+1,j+w)][0],dp[idx(i,j)][0]+v)
  m=max([dp[idx(N,j)][0] for j in range((W+10000+1))])
  print(m,sum([dp[idx(N,j)][1] for j in range((W+10000+1)) if dp[idx(N,j)][0]==m])%mod)

def random_input():
  from random import randint,shuffle
  N=randint(1,10)
  M=randint(1,N)
  A=list(range(1,M+1))+[randint(1,M) for _ in range(N-M)]
  shuffle(A)
  return (" ".join(map(str, [N,M]))+"\n"+" ".join(map(str, A))+"\n")*3

def simple_solve():
  return []

def main(test):
  if test==0:
    solve(0)
  elif test==1:
    sys.stdin = io.StringIO(_INPUT)
    case_no=int(input())
    for _ in range(case_no):
      solve(0)
  else:
    for i in range(1000):
      sys.stdin = io.StringIO(random_input())
      x=solve(1)
      y=simple_solve()
      if x!=y:
        print(i,x,y)
        print(*[line for line in sys.stdin],sep='')
        break

#0:提出用、1:与えられたテスト用、2:ストレステスト用
main(0)
0