結果
問題 | No.2139 K Consecutive Sushi |
ユーザー | Alex Wice |
提出日時 | 2022-12-04 14:59:36 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 195 ms / 2,000 ms |
コード長 | 798 bytes |
コンパイル時間 | 494 ms |
コンパイル使用メモリ | 81,920 KB |
実行使用メモリ | 131,732 KB |
最終ジャッジ日時 | 2024-11-27 19:52:11 |
合計ジャッジ時間 | 5,110 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 31 |
ソースコード
import collections class MaxQueue(collections.deque): def enqueue(self, val): count = 1 while self and self[-1][0] < val: count += self.pop()[1] self.append([val, count]) def dequeue(self): ans = self.max() self[0][1] -= 1 if self[0][1] <= 0: self.popleft() return ans def max(self): return self[0][0] if self else 0 readlist = lambda: list(map(int, input().split())) N, K = readlist() A = readlist() P = [0] for x in A: P.append(P[-1] + x) for x in A: P.append(P[-1]) dp = [0] * (N + K + 1) maxq = MaxQueue() length = 0 for i in range(N - 1, -1, -1): maxq.enqueue(dp[i+2] + P[i+1]) if i <= N-K: maxq.dequeue() dp[i] = max(dp[i+1], maxq.max() - P[i]) print(dp[0])