11557 | 2024-10-26 19:26:43 | balint | Tulipán csokrok | python3 | Time limit exceeded 25/100 | 1.626s | 410328 KiB |
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):
# Monotonic stack to keep track of minimums efficiently
stack = []
for i in range(k, N + 1):
current_min = float('inf')
# Calculate dp[i][k] as we scan from i backwards
for j in range(i, k - 1, -1):
current_min = min(current_min, T[j - 1])
# Update dp[i][k] using the previous state dp[j-1][k-1]
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 | 17ms | 3128 KiB | ||||
2 | Accepted | 16ms | 3128 KiB | ||||
subtask2 | 0/12 | ||||||
3 | Accepted | 16ms | 3004 KiB | ||||
4 | Accepted | 20ms | 3164 KiB | ||||
5 | Time limit exceeded | 1.6s | 20624 KiB | ||||
subtask3 | 25/25 | ||||||
6 | Accepted | 16ms | 3120 KiB | ||||
7 | Accepted | 17ms | 3132 KiB | ||||
8 | Accepted | 17ms | 3128 KiB | ||||
9 | Accepted | 17ms | 3144 KiB | ||||
10 | Accepted | 16ms | 3320 KiB | ||||
11 | Accepted | 17ms | 3128 KiB | ||||
subtask4 | 0/25 | ||||||
12 | Accepted | 135ms | 3044 KiB | ||||
13 | Accepted | 893ms | 3384 KiB | ||||
14 | Accepted | 1.189s | 3588 KiB | ||||
15 | Time limit exceeded | 1.6s | 3832 KiB | ||||
16 | Time limit exceeded | 1.578s | 3896 KiB | ||||
17 | Time limit exceeded | 1.585s | 3896 KiB | ||||
18 | Time limit exceeded | 1.585s | 3988 KiB | ||||
19 | Time limit exceeded | 1.582s | 4664 KiB | ||||
20 | Time limit exceeded | 1.587s | 5492 KiB | ||||
subtask5 | 0/38 | ||||||
21 | Time limit exceeded | 1.611s | 396344 KiB | ||||
22 | Time limit exceeded | 1.61s | 397944 KiB | ||||
23 | Time limit exceeded | 1.61s | 402440 KiB | ||||
24 | Time limit exceeded | 1.608s | 410328 KiB | ||||
25 | Time limit exceeded | 1.597s | 214444 KiB | ||||
26 | Time limit exceeded | 1.588s | 97128 KiB | ||||
27 | Time limit exceeded | 1.585s | 26784 KiB | ||||
28 | Time limit exceeded | 1.588s | 50860 KiB | ||||
29 | Time limit exceeded | 1.593s | 175276 KiB | ||||
30 | Time limit exceeded | 1.621s | 332028 KiB | ||||
31 | Time limit exceeded | 1.626s | 399968 KiB | ||||
32 | Time limit exceeded | 1.623s | 398552 KiB |