import sys sys.setrecursionlimit(10**7) # https://atcoder.jp/contests/dp/submissions/6934013 class ConvexHullTrickAddMonotone: def __init__(self): self.AB = [] def check(self,a,b): a1,b1 = self.AB[-1][0],self.AB[-1][1] a2,b2 = self.AB[-2][0],self.AB[-2][1] lhs = (b2-b1)*(a -a1) rhs = (b1-b )*(a1-a2) return lhs>=rhs # 一次関数 y=a*x+b を追加 (傾きaは単調減少) def add_line(self,a,b): while 2<=len(self.AB) and self.check(a,b): self.AB.pop() self.AB.append((a,b)) return # min_i {a[i]*x+b[i]} def query(self,x): def eval(ab,x): return ab[0]*x+ab[1] l,r = -1, len(self.AB)-1 while l+1= R: return M = (L + R) // 2 calc(L, M) calc(M + 1, R) if L < M: cht = ConvexHullTrickAddMonotone() for l in range(M + 1, R + 1): cht.add_line(-2 * l, l * l + S[l]) MIN = 1e18 for k in range(L, M): MIN = min(MIN, cht.query(k) + k * k - S[k]) ans[k] = min(ans[k], MIN) if M < R: cht = ConvexHullTrickAddMonotone() for k in range(L, M + 1): cht.add_line(-2 * k, k * k - S[k]) MIN = 1e18 for l in range(M + 1, R + 1): tmp[l] = cht.query(l) + l * l + S[l] for l in range(R, M, -1): MIN = min(MIN, tmp[l]) ans[l - 1] = min(ans[l - 1], MIN) return calc(0, N) for i in range(N): print(ans[i])