115572024-10-26 19:26:43balintTulipán csokrokpython3Time limit exceeded 25/1001.626s410328 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))
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted17ms3128 KiB
2Accepted16ms3128 KiB
subtask20/12
3Accepted16ms3004 KiB
4Accepted20ms3164 KiB
5Time limit exceeded1.6s20624 KiB
subtask325/25
6Accepted16ms3120 KiB
7Accepted17ms3132 KiB
8Accepted17ms3128 KiB
9Accepted17ms3144 KiB
10Accepted16ms3320 KiB
11Accepted17ms3128 KiB
subtask40/25
12Accepted135ms3044 KiB
13Accepted893ms3384 KiB
14Accepted1.189s3588 KiB
15Time limit exceeded1.6s3832 KiB
16Time limit exceeded1.578s3896 KiB
17Time limit exceeded1.585s3896 KiB
18Time limit exceeded1.585s3988 KiB
19Time limit exceeded1.582s4664 KiB
20Time limit exceeded1.587s5492 KiB
subtask50/38
21Time limit exceeded1.611s396344 KiB
22Time limit exceeded1.61s397944 KiB
23Time limit exceeded1.61s402440 KiB
24Time limit exceeded1.608s410328 KiB
25Time limit exceeded1.597s214444 KiB
26Time limit exceeded1.588s97128 KiB
27Time limit exceeded1.585s26784 KiB
28Time limit exceeded1.588s50860 KiB
29Time limit exceeded1.593s175276 KiB
30Time limit exceeded1.621s332028 KiB
31Time limit exceeded1.626s399968 KiB
32Time limit exceeded1.623s398552 KiB