请登录

2080.Permutations

Time Limit: 1s Memory Limit: 256MB

You are given n k-digit integers. You have to rearrange the digits in the integers so that the difference between the largest and the smallest number was minimum. Digits should be rearranged by the same rule in all integers.

Input Format(From the terminal/stdin)

The first line contains integers n and k - the number and digit capacity of numbers correspondingly (1 \le n,k \le 8). Next n lines contain k-digit positive integers. Leading zeroes are allowed both in the initial integers and the integers resulting from the rearranging of digits.

Output Format(To the terminal/stdout)

Print a single number: the minimally possible difference between the largest and the smallest number after the digits are rearranged in all integers by the same rule.

Sample Input 1

Copy
6 4
5237
2753
7523
5723
5327
2537
 · \n
    \n
    \n
    \n
    \n
    \n
    \n

Sample Output 1

Copy
2700
    \n

Sample Input 2

Copy
3 3
010
909
012
 · \n
   \n
   \n
   \n

Sample Output 2

Copy
3
 \n

Sample Input 3

Copy
7 5
50808
36603
37198
44911
29994
42543
50156
 · \n
     \n
     \n
     \n
     \n
     \n
     \n
     \n

Sample Output 3

Copy
20522
     \n

Hints

In the first sample, if we rearrange the digits in numbers as (3,1,4,2), then the 2-nd and the 4-th numbers will equal 5237 and 2537 correspondingly (they will be maximum and minimum for such order of digits).

In the second sample, if we swap the second digits and the first ones, we get integers 100, 99 and 102.

Submit

请先 登录

© 2025 FAQs