1340B.md (3157B)
1 Source: https://codeforces.com/problemset/problem/1340/B 2 3 B. Nastya and Scoreboard 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 standard input 16 17 output 18 19 standard output 20 21 Denis, after buying flowers and sweets (you will learn about this story in the next task), went to a date with Nastya to ask her to become a couple. Now, they are sitting in the cafe and finally... Denis asks her to be together, but ... Nastya doesn't give any answer. 22 23 The poor boy was very upset because of that. He was so sad that he punched some kind of scoreboard with numbers. The numbers are displayed in the same way as on an electronic clock: each digit position consists of $7$ segments, which can be turned on or off to display different numbers. The picture shows how all $10$ decimal digits are displayed: 24 25  26 27 After the punch, some segments stopped working, that is, some segments might stop glowing if they glowed earlier. But Denis remembered how many sticks were glowing and how many are glowing now. Denis broke exactly $k$ segments and he knows which sticks are working now. Denis came up with the question: what is the maximum possible number that can appear on the board if you turn on exactly $k$ sticks (which are off now)? 28 29 It is allowed that the number includes leading zeros. 30 31 Input 32 33 The first line contains integer $n$ $(1 \leq n \leq 2000)$ — the number of digits on scoreboard and $k$ $(0 \leq k \leq 2000)$ — the number of segments that stopped working. 34 35 The next $n$ lines contain one binary string of length $7$, the $i$-th of which encodes the $i$-th digit of the scoreboard. 36 37 Each digit on the scoreboard consists of $7$ segments. We number them, as in the picture below, and let the $i$-th place of the binary string be $0$ if the $i$-th stick is not glowing and $1$ if it is glowing. Then a binary string of length $7$ will specify which segments are glowing now. 38 39  40 41 Thus, the sequences "1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011" encode in sequence all digits from $0$ to $9$ inclusive. 42 43 Output 44 45 Output a single number consisting of $n$ digits — the maximum number that can be obtained if you turn on exactly $k$ sticks or $-1$, if it is impossible to turn on exactly $k$ sticks so that a correct number appears on the scoreboard digits. 46 47 Examples 48 49 Input 50 51 ``` 52 1 7 53 0000000 54 ``` 55 56 Output 57 58 ``` 59 8 60 ``` 61 62 Input 63 64 ``` 65 2 5 66 0010010 67 0010010 68 ``` 69 70 Output 71 72 ``` 73 97 74 ``` 75 76 Input 77 78 ``` 79 3 5 80 0100001 81 1001001 82 1010011 83 ``` 84 85 Output 86 87 ``` 88 -1 89 ``` 90 91 Note 92 93 In the first test, we are obliged to include all $7$ sticks and get one $8$ digit on the scoreboard. 94 95 In the second test, we have sticks turned on so that units are formed. For $5$ of additionally included sticks, you can get the numbers $07$, $18$, $34$, $43$, $70$, $79$, $81$ and $97$, of which we choose the maximum — $97$. 96 97 In the third test, it is impossible to turn on exactly $5$ sticks so that a sequence of numbers appears on the scoreboard.