11556 | 2024-10-26 19:25:34 | balint | Tulipán csokrok | python3 | Time limit exceeded 25/100 | 1.625s | 411492 KiB |
from collections import deque
def max_beauty_sum(N, K, T):
# dp[i] will store the max sum of the minimums when partitioning first i tulips into up to K bouquets
dp = [[-float('inf')] * (K + 1) for _ in range(N + 1)]
# Base case: 0 tulips, 0 bouquets has a sum of 0
dp[0][0] = 0
# Iterate over the number of bouquets
for k in range(1, K + 1):
# We want to keep track of the minimum value efficiently using a deque
# min_deque will store tuples (min_value, index)
for i in range(k, N + 1):
current_min = float('inf')
min_deque = deque()
for j in range(i, k - 1, -1):
# Update current_min
current_min = min(current_min, T[j - 1])
# Maintain the deque to ensure it's strictly increasing
while min_deque and min_deque[-1][0] >= T[j - 1]:
min_deque.pop()
# Add the current element to the deque
min_deque.append((T[j - 1], j - 1))
# The minimum element is always at the front of the deque
current_min = min_deque[0][0]
# Update dp[i][k] by considering this partition
dp[i][k] = max(dp[i][k], dp[j - 1][k - 1] + current_min)
# The answer is dp[N][K] which stores the maximum possible sum
return dp[N][K]
# Input parsing
N, K = map(int, input().split())
T = list(map(int, input().split()))
# Output the result
print(max_beauty_sum(N, K, T))
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 18ms | 3384 KiB | ||||
2 | Accepted | 19ms | 3576 KiB | ||||
subtask2 | 0/12 | ||||||
3 | Accepted | 18ms | 3400 KiB | ||||
4 | Accepted | 28ms | 3376 KiB | ||||
5 | Time limit exceeded | 1.577s | 21932 KiB | ||||
subtask3 | 25/25 | ||||||
6 | Accepted | 19ms | 3388 KiB | ||||
7 | Accepted | 18ms | 3384 KiB | ||||
8 | Accepted | 19ms | 3384 KiB | ||||
9 | Accepted | 18ms | 3384 KiB | ||||
10 | Accepted | 20ms | 3568 KiB | ||||
11 | Accepted | 18ms | 3384 KiB | ||||
subtask4 | 0/25 | ||||||
12 | Accepted | 237ms | 3636 KiB | ||||
13 | Time limit exceeded | 1.587s | 3740 KiB | ||||
14 | Time limit exceeded | 1.587s | 3976 KiB | ||||
15 | Time limit exceeded | 1.587s | 3892 KiB | ||||
16 | Time limit exceeded | 1.58s | 3896 KiB | ||||
17 | Time limit exceeded | 1.583s | 4164 KiB | ||||
18 | Time limit exceeded | 1.585s | 4152 KiB | ||||
19 | Time limit exceeded | 1.585s | 4828 KiB | ||||
20 | Time limit exceeded | 1.588s | 5700 KiB | ||||
subtask5 | 0/38 | ||||||
21 | Time limit exceeded | 1.605s | 396856 KiB | ||||
22 | Time limit exceeded | 1.603s | 398448 KiB | ||||
23 | Time limit exceeded | 1.603s | 403228 KiB | ||||
24 | Time limit exceeded | 1.603s | 411492 KiB | ||||
25 | Time limit exceeded | 1.588s | 215780 KiB | ||||
26 | Time limit exceeded | 1.58s | 98476 KiB | ||||
27 | Time limit exceeded | 1.57s | 28108 KiB | ||||
28 | Time limit exceeded | 1.575s | 52140 KiB | ||||
29 | Time limit exceeded | 1.595s | 176768 KiB | ||||
30 | Time limit exceeded | 1.608s | 333296 KiB | ||||
31 | Time limit exceeded | 1.623s | 400692 KiB | ||||
32 | Time limit exceeded | 1.625s | 399100 KiB |