124B.md (1465B)
1 Source: https://codeforces.com/problemset/problem/124/B 2 3 B. Permutations 4 5 time limit per test 6 7 1 second 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 stdin 16 17 output 18 19 stdout 20 21 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. 22 23 Input 24 25 The first line contains integers *n* and *k* — the number and digit capacity of numbers correspondingly (1 ≤ *n*, *k* ≤ 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. 26 27 Output 28 29 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. 30 31 Examples 32 33 Input 34 35 ``` 36 6 4 37 5237 38 2753 39 7523 40 5723 41 5327 42 2537 43 ``` 44 45 Output 46 47 ``` 48 2700 49 ``` 50 51 Input 52 53 ``` 54 3 3 55 010 56 909 57 012 58 ``` 59 60 Output 61 62 ``` 63 3 64 ``` 65 66 Input 67 68 ``` 69 7 5 70 50808 71 36603 72 37198 73 44911 74 29994 75 42543 76 50156 77 ``` 78 79 Output 80 81 ``` 82 20522 83 ``` 84 85 Note 86 87 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). 88 89 In the second sample, if we swap the second digits and the first ones, we get integers 100, 99 and 102.