4046.Replacing Digits

Time Limit: 1s Memory Limit: 256MB

You are given an integer a that consists of n digits. You are also given a sequence of digits s of length m. The digit in position j (1 \le j \le m) of sequence s means that you can choose an arbitrary position i (1 \le i \le n) in a and replace the digit in the chosen position i with sj. Each element in the sequence s can participate in no more than one replacing operation.

Your task is to perform such sequence of replacements, that the given number a gets maximum value. You are allowed to use not all elements from s.

Input Format(From the terminal/stdin)

The first line contains positive integer a. Its length n is positive and doesn't exceed 105. The second line contains sequence of digits s. Its length m is positive and doesn't exceed 105. The digits in the sequence s are written consecutively without any separators.

The given number a doesn't contain leading zeroes.

Output Format(To the terminal/stdout)

Print the maximum value that can be obtained from a after a series of replacements. You are allowed to use not all elements from s. The printed number shouldn't contain any leading zeroes.

Sample Input 1

Copy
1024
010
    \n
   \n

Sample Output 1

Copy
1124
    \n

Sample Input 2

Copy
987
1234567
   \n
       \n

Sample Output 2

Copy
987
   \n

Submit

请先 登录

© 2025 FAQs