이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
import sys
# Read input from stdin
n, k = map(int, input().split())
sequence = list(map(int, input().split()))
# Initialize a table to store the maximum total points for each subproblem
dp = [[0] * (n + 1) for _ in range(k + 2)]
# Initialize a table to store the split positions
split_positions = [[0] * (n + 1) for _ in range(k + 2)]
# Calculate the prefix sum of the sequence for quick sum calculations
prefix_sum = [0]
for num in sequence:
prefix_sum.append(prefix_sum[-1] + num)
# Dynamic programming loop
for i in range(1, k + 2):
for j in range(i, n + 1):
dp[i][j] = float('-inf')
for x in range(i - 1, j):
current_points = dp[i - 1][x] + (prefix_sum[j] - prefix_sum[x])
if current_points > dp[i][j]:
dp[i][j] = current_points
split_positions[i][j] = x
# Backtrack to find split positions
split_indices = []
i, j = k + 1, n
while i > 1:
split_indices.append(split_positions[i][j] + 1)
j = split_positions[i][j]
i -= 1
# Print the maximum total points and split positions to stdout
print(dp[k + 1][n])
print(*split_indices)
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |