結果

問題 No.59 鉄道の旅
ユーザー tobusakanatobusakana
提出日時 2022-11-07 00:53:03
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 810 bytes
コンパイル時間 275 ms
コンパイル使用メモリ 86,712 KB
実行使用メモリ 79,880 KB
最終ジャッジ日時 2023-09-27 17:05:22
合計ジャッジ時間 3,748 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
71,916 KB
testcase_01 AC 73 ms
71,980 KB
testcase_02 AC 74 ms
72,204 KB
testcase_03 AC 71 ms
72,092 KB
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 AC 108 ms
79,420 KB
testcase_12 AC 145 ms
79,228 KB
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

class FenwickTree: # (BinaryIndexedTree)
  def __init__(self, n):
    self.size = n
    self.tree = [0] * (n + 1)
 
  def sum(self, i):
    out = 0
    while i > 0:
      out += self.tree[i]
      i -= (i & -i)
    return out
 
  def add(self, i, x):
    while i <= self.size:
      self.tree[i] += x
      i += (i & -i)
      
N,K = map(int,input().split())
M = 100001

BIT = FenwickTree(M)
for _ in range(N):
    W = int(input())
    if W > 0: # 荷物を積む
        cnt = BIT.sum(M) - BIT.sum(W - 1) # その荷物以上の荷物の重さ
        if cnt < K: # 積める
            BIT.add(W, 1)
    else: # 荷物を下ろす
        W *= (-1)
        cnt = BIT.sum(W) - BIT.sum(W - 1)
        if cnt > 0:
            BIT.add(W, -1)
            
print(BIT.sum(M))
        
            
    
    
    

0