結果
| 問題 | No.2694 The Early Bird Catches The Worm | 
| コンテスト | |
| ユーザー |  norioc | 
| 提出日時 | 2024-03-23 00:28:03 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 296 ms / 2,000 ms | 
| コード長 | 1,619 bytes | 
| コンパイル時間 | 383 ms | 
| コンパイル使用メモリ | 81,904 KB | 
| 実行使用メモリ | 147,604 KB | 
| 最終ジャッジ日時 | 2024-09-30 12:50:08 | 
| 合計ジャッジ時間 | 15,662 ms | 
| ジャッジサーバーID (参考情報) | judge3 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 72 | 
ソースコード
from collections import deque
N, H = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
class D:
    def __init__(self):
        self.q = deque()
        self.a = 0
        self.b = 0
        self.s = 0
    def append(self, a, b):
        self.s += b * (len(self.q) + 1)
        self.q.append((a, b))
        self.a += a
        self.b += b
    def popleft(self):
        self.s -= self.b
        a, b = self.q.popleft()
        self.a -= a
        self.b -= b
    def score(self):
        if self.s > H: return 0
        return self.a
# a : 入力リスト
# pred : 条件
# op : 右端を伸ばす演算
# invop : 左端を縮める演算
def shakutori(a: list, pred, op, invop, identity):
    n = len(a)
    res = identity()
    hi = -1
    for lo in range(n):
        if lo > hi:
            hi = lo - 1
            res = identity()
        # 必要な分だけ伸ばす
        while hi+1 < n and pred(res, a[hi+1]):
            res = op(res, a[hi+1])
            hi += 1
        if lo <= hi:
            yield res, lo, hi
        # lo を一つ進める
        res = invop(res, a[lo])
# res に x を追加できるか
def pred(res: D, x):
    a, b = x
    s = res.s + b * (len(res.q) + 1)
    return s <= H
# 伸ばす(res に x を追加)
def op(res: D, x):
    a, b = x
    res.append(a, b)
    return res
# 縮める(res から x を削除)
def invop(res: D, x):
    if len(res.q) > 0:
        res.popleft()
    return res
ans = 0
for res, lo, hi in shakutori(list(zip(A, B)), pred, op, invop, D):
    ans = max(ans, res.score())
print(ans)
            
            
            
        