結果
| 問題 | No.59 鉄道の旅 | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2022-11-07 00:53:03 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                RE
                                 
                             | 
| 実行時間 | - | 
| コード長 | 810 bytes | 
| コンパイル時間 | 206 ms | 
| コンパイル使用メモリ | 81,948 KB | 
| 実行使用メモリ | 78,140 KB | 
| 最終ジャッジ日時 | 2024-07-20 11:25:01 | 
| 合計ジャッジ時間 | 1,994 ms | 
| ジャッジサーバーID (参考情報) | judge1 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 2 RE * 10 | 
ソースコード
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))
        
            
    
    
    
            
            
            
        