結果

問題 No.409 ダイエット
ユーザー ayaoni
提出日時 2023-05-24 00:42:21
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 246 ms / 2,000 ms
コード長 2,338 bytes
コンパイル時間 225 ms
コンパイル使用メモリ 82,240 KB
実行使用メモリ 126,080 KB
最終ジャッジ日時 2024-12-23 09:02:52
合計ジャッジ時間 13,471 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 92
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque
sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def LS2(): return list(sys.stdin.readline().rstrip())


class ConvexHullTrick:
    """
    「1次関数の追加」「あるxにおける1次関数群の最小値の取得」を高速に計算できる。
    傾きa_iは単調、xは昇順と仮定している。
    """
    def __init__(self):
        self.lines = deque()

    # y = a_1 * x + b1 がminを考えるとき無駄であるか否か(a_0 > a_1 > a_2)
    def check(self, a0, b0, a1, b1, a2, b2):
        return (b2 - b1) * (a0 - a1) <= (b1 - b0) * (a1 - a2)

    # y = a * x + b を追加(a_iが降順のとき)
    def add(self, a, b):
        while len(self.lines) >= 2:
            a0, b0 = self.lines[-2]
            a1, b1 = self.lines[-1]
            if self.check(a0, b0, a1, b1, a, b):
                self.lines.pop()
            else:
                break
        self.lines.append((a, b))

    # y = a * x + b を追加(a_iが昇順のとき)
    def add2(self, a, b):
        while len(self.lines) >= 2:
            a1, b1 = self.lines[0]
            a2, b2 = self.lines[1]
            if self.check(a, b, a1, b1, a2, b2):
                self.lines.popleft()
            else:
                break
        self.lines.appendleft((a, b))

    # x における1次関数群の最小値を求める
    def get_min(self, x):
        a, b = self.lines[0]
        res = a * x + b
        while len(self.lines) >= 2:
            a1, b1 = self.lines[1]
            y1 = a1 * x + b1
            if y1 < res:
                res = y1
                self.lines.popleft()
            else:
                break
        return res


N, A, B, W = MI()
D = [0] + LI() + [0]
inf = 10 ** 18

dp = [inf] * (N + 2)
dp[0] = 0
CHT = ConvexHullTrick()
for i in range(1, N + 2):
    CHT.add(-B * (i - 1), dp[i - 1] + i * (i - 1) // 2 * B + A * (i - 1))
    dp[i] = D[i] - A * (i - 1) + i * (i - 1) // 2 * B + CHT.get_min(i)

ans = W + dp[-1]
print(ans)
0