結果

問題 No.2546 Many Arithmetic Sequences
ユーザー ShirotsumeShirotsume
提出日時 2023-07-24 01:15:00
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 1,518 bytes
コンパイル時間 238 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 144,544 KB
最終ジャッジ日時 2023-11-25 01:26:36
合計ジャッジ時間 15,670 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
56,140 KB
testcase_01 AC 41 ms
56,140 KB
testcase_02 AC 70 ms
73,408 KB
testcase_03 AC 384 ms
89,896 KB
testcase_04 AC 581 ms
125,484 KB
testcase_05 AC 505 ms
107,752 KB
testcase_06 AC 206 ms
83,792 KB
testcase_07 AC 299 ms
92,084 KB
testcase_08 AC 154 ms
81,520 KB
testcase_09 AC 252 ms
104,056 KB
testcase_10 AC 372 ms
123,560 KB
testcase_11 AC 400 ms
131,052 KB
testcase_12 AC 236 ms
95,316 KB
testcase_13 AC 416 ms
111,048 KB
testcase_14 AC 395 ms
128,044 KB
testcase_15 AC 195 ms
92,232 KB
testcase_16 AC 379 ms
124,372 KB
testcase_17 AC 496 ms
114,240 KB
testcase_18 AC 316 ms
104,180 KB
testcase_19 AC 358 ms
102,828 KB
testcase_20 AC 249 ms
99,176 KB
testcase_21 AC 314 ms
89,108 KB
testcase_22 AC 409 ms
109,428 KB
testcase_23 AC 658 ms
144,160 KB
testcase_24 AC 657 ms
142,200 KB
testcase_25 AC 663 ms
144,544 KB
testcase_26 AC 683 ms
142,244 KB
testcase_27 AC 674 ms
143,772 KB
testcase_28 AC 480 ms
143,616 KB
testcase_29 AC 474 ms
143,384 KB
testcase_30 AC 465 ms
143,384 KB
testcase_31 AC 480 ms
143,608 KB
testcase_32 AC 475 ms
143,384 KB
testcase_33 AC 43 ms
56,140 KB
testcase_34 AC 532 ms
142,244 KB
testcase_35 AC 48 ms
58,208 KB
testcase_36 AC 414 ms
143,864 KB
testcase_37 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys, time, random, heapq
from collections import deque, Counter, defaultdict
input = lambda: sys.stdin.readline().rstrip()
ii = lambda: int(input())
mi = lambda: map(int, input().split())
li = lambda: list(mi())
inf = 2 ** 63 - 1
mod = 998244353
#https://tjkendev.github.io/procon-library/python/convex_hull_trick/deque.html
deq = deque()
def check(f1, f2, f3):
    return (f2[0] - f1[0]) * (f3[1] - f2[1]) >= (f2[1] - f1[1]) * (f3[0] - f2[0])
def f(f1, x):
    return f1[0]*x + f1[1]

# add f_i(x) = a*x + b
def add_line(a, b):
    f1 = (a, b)
    while len(deq) >= 2 and check(deq[-2], deq[-1], f1):
        deq.pop()
    deq.append(f1)

# min f_i(x)
def query(x):
    while len(deq) >= 2 and f(deq[0], x) >= f(deq[1], x):
        deq.popleft()
    return f(deq[0], x)

n, m = mi()
AD = [li() for _ in range(n)]
plus = []
minus = []

for a, d in AD:
    if d > 0:
        plus.append((a, d))
    else:
        minus.append((a, d))

p = [-inf] * (m + 1)
p[0] = 0
plus.sort(key = lambda x: x[1])
for a, d in plus:
    add_line(-d, -2 * a + d)
if plus:
    for i in range(1, m + 1):
        p[i] = -query(i) * i // 2


q = [-inf] * (m + 1)
q[0] = 0
H = []

for a, d in minus:
    heapq.heappush(H, (-a, d))

for i in range(1, m + 1):
    a, d = heapq.heappop(H)
    a = -a
    q[i] = q[i - 1] + a
    heapq.heappush(H, (-(a + d), d))
ans = -inf
maxi = -1
for i in range(0, m + 1):
    if ans <= p[i] + q[m - i]:
        ans = max(ans, p[i] + q[m - i])
        maxi = i

print(ans)
print(m, maxi, file=sys.stderr)
0