class Manager: class node: def __init__(self, a, b, c) -> None: self.i = a self.m = b self.limit = c def __init__(self, D) -> None: self.n = len(D) self.C = D self.data = [] assert len(D) + 1 == len(D[0]) def add(self, i, m) -> None: if not self.data: self.data.append(self.node(i, m, self.n)) return else: while self.data[-1].limit < i + 1: self.data.pop() d = self.data[-1] if self.C[d.i][i + 1] + d.m < self.C[i][i + 1] + m: return pushed = False while self.data: d = self.data[-1] if self.C[d.i][d.limit] + d.m > self.C[i][d.limit] + m: self.data.pop() else: ok = i + 1 ng = d.limit while ng - ok > 1: mid = (ok + ng) >> 1 if self.C[d.i][mid] + d.m > self.C[i][mid] + m: ok = mid else: ng = mid self.data.append(self.node(i, m, ok)) pushed = True break if not pushed: self.data.append((i, m, self.n)) def get_min(self, x) -> int: while self.data[-1].limit < x: self.data.pop() d = self.data[-1] return self.C[d.i][x] + d.m def clear_log(self) -> None: while self.data: self.data.pop() N = int(input()) P = list(map(int, input().split())) D = [[0] * (N + 1) for _ in range(N)] for i in range(N - 1): R = [0] * (N + 1) for j in range(i + 1): R[P[j]] += 1 for j in range(1, N + 1): R[j] += R[j - 1] sm = 0 for j in range(i + 1, N): sm += R[N] - R[P[j]] D[i + 1][j + 1] = sm m = Manager(D) dp = [[N * N] * (N + 1) for _ in range(N + 1)] dp[0][0] = 0 for j in range(N): m.add(j, dp[j][j]) for i in range(j + 1, N + 1): dp[i][j + 1] = m.get_min(i) if i != N: m.add(i, dp[i][j]) m.clear_log() for i in range(1, N + 1): print(dp[N][i])