結果

問題 No.1247 ブロック登り
ユーザー persimmon-persimmonpersimmon-persimmon
提出日時 2021-03-04 14:41:24
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,788 bytes
コンパイル時間 238 ms
コンパイル使用メモリ 82,516 KB
実行使用メモリ 383,632 KB
最終ジャッジ日時 2024-04-15 07:02:50
合計ジャッジ時間 7,397 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
383,632 KB
testcase_01 AC 45 ms
59,348 KB
testcase_02 AC 46 ms
61,164 KB
testcase_03 AC 42 ms
54,700 KB
testcase_04 AC 61 ms
72,820 KB
testcase_05 AC 50 ms
64,736 KB
testcase_06 AC 42 ms
55,644 KB
testcase_07 AC 629 ms
107,260 KB
testcase_08 AC 689 ms
113,940 KB
testcase_09 AC 435 ms
92,004 KB
testcase_10 AC 415 ms
91,556 KB
testcase_11 TLE -
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 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
def main1(n,k,a):
  inf=float('inf')
  #dp0=[[[-inf]*n for _ in range(n)] for _ in range(k)]
  dp0=defaultdict(lambda:-inf)
  # dp0[i][l][r]:高さi以上のブロックは構築済みで[l,r]が到達済みでlにいるときの最大得点
  #dp1=[[[-inf]*n for _ in range(n)] for _ in range(k)]
  dp1=defaultdict(lambda:-inf)
  # dp1[i][l][r]:高さi以上のブロックは構築済みで[l,r]が到達済みでrにいるときの最大得点
  for i in range(n):
    if i>0:dp0[k-1,i-1,i]=k*a[i]+(k-1)*a[i-1]
    if i<n-1:dp1[k-1,i,i+1]=k*a[i]+(k-1)*a[i+1]
  ans=[-inf]*n
  for j in range(k-1,0,-1):
    for l in range(n):
      for r in range(l,n):
        if k+1-j<r+1-l:continue
        if dp0[j,l,r]!=-inf:
          # dp0[j,l,r]からの遷移。
          if j-2>=0:dp0[j-2,l,r]=max(dp0[j-2,l,r],dp0[j,l,r])
          if j-1>=0 and l>0:dp0[j-1,l-1,r]=max(dp0[j-1,l-1,r],dp0[j,l,r]+a[l-1]*(j-1))
          # l->rへの移動
          if j-(r-l)>=0:
            dp1[j-(r-l),l,r]=max(dp1[j-(r-l),l,r],dp0[j,l,r])
          else:
            ans[l+j]=max(ans[l+j],dp0[j,l,r])

        if dp1[j,l,r]!=-inf:
          # dp2[j,l,r]からの遷移。
          if j-2>=0:dp1[j-2,l,r]=max(dp1[j-2,l,r],dp1[j,l,r])
          if j-1>=0 and r<n-1:dp1[j-1,l,r+1]=max(dp1[j-1,l,r+1],dp1[j,l,r]+a[r+1]*(j-1))
          # l<-rへの移動
          if j-(r-l)>=0:
            dp0[j-(r-l),l,r]=max(dp0[j-(r-l),l,r],dp1[j,l,r])
          else:
            ans[r-j]=max(ans[r-j],dp1[j,l,r])
  for l in range(n):
    for r in range(l,n):
      ans[l]=max(ans[l],dp0[0,l,r])
      ans[r]=max(ans[r],dp1[0,l,r])
  return ans

if __name__=='__main__':
  n,k=map(int,input().split())
  a=list(map(int,input().split()))
  ret1=main1(n,k,a)
  print(*ret1,sep='\n')
0