/ SeriousOJ /

Record Detail

Accepted


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

Code

def max_possible_number(N, K, A):
    A = list(map(str, A))  # Convert all numbers to strings for easy concatenation
    
    for _ in range(K):
        max_num = ''
        index = -1
        
        # Find the best pair to concatenate
        for i in range(N-1):
            new_num = A[i] + A[i+1]
            if new_num > max_num:
                max_num = new_num
                index = i
        
        if index == -1:
            break
        
        # Replace the pair with the new concatenated number
        A[index] = max_num
        A.pop(index+1)
        N -= 1
    
    # Return the maximum number 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(max_possible_number(N, K, A))

Information

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