/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 17ms 2.812 MiB

Code

def concatenate_and_maximize(A, N, K):
    A = list(map(str, A))  # Convert to strings for concatenation

    for _ in range(K):
        max_value = ''
        best_index = -1

        # Find the best adjacent pair to concatenate
        for i in range(len(A) - 1):
            new_value = A[i] + A[i + 1]
            if new_value > max_value:
                max_value = new_value
                best_index = i

        if best_index == -1:
            break
        
        # Perform the concatenation
        A[best_index] = max_value
        del A[best_index + 1]  # Remove the second part of the concatenated pair

    # After all operations, find and return the maximum value in the array
    return max(A)

# Reading input
T = int(input())
for _ in range(T):
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    print(concatenate_and_maximize(A, N, K))

Information

Submit By
Type
Pretest
Problem
P1083 Number concatenation
Language
Python 3 (Python 3.12.3)
Submit At
2024-08-16 16:06:37
Judged At
2024-08-16 16:06:37
Judged By
Score
10
Total Time
17ms
Peak Memory
2.812 MiB