115562024-10-26 19:25:34balintTulipán csokrokpython3Time limit exceeded 25/1001.625s411492 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))
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted18ms3384 KiB
2Accepted19ms3576 KiB
subtask20/12
3Accepted18ms3400 KiB
4Accepted28ms3376 KiB
5Time limit exceeded1.577s21932 KiB
subtask325/25
6Accepted19ms3388 KiB
7Accepted18ms3384 KiB
8Accepted19ms3384 KiB
9Accepted18ms3384 KiB
10Accepted20ms3568 KiB
11Accepted18ms3384 KiB
subtask40/25
12Accepted237ms3636 KiB
13Time limit exceeded1.587s3740 KiB
14Time limit exceeded1.587s3976 KiB
15Time limit exceeded1.587s3892 KiB
16Time limit exceeded1.58s3896 KiB
17Time limit exceeded1.583s4164 KiB
18Time limit exceeded1.585s4152 KiB
19Time limit exceeded1.585s4828 KiB
20Time limit exceeded1.588s5700 KiB
subtask50/38
21Time limit exceeded1.605s396856 KiB
22Time limit exceeded1.603s398448 KiB
23Time limit exceeded1.603s403228 KiB
24Time limit exceeded1.603s411492 KiB
25Time limit exceeded1.588s215780 KiB
26Time limit exceeded1.58s98476 KiB
27Time limit exceeded1.57s28108 KiB
28Time limit exceeded1.575s52140 KiB
29Time limit exceeded1.595s176768 KiB
30Time limit exceeded1.608s333296 KiB
31Time limit exceeded1.623s400692 KiB
32Time limit exceeded1.625s399100 KiB