**A. Towers**

The total number of towers is equal to number of different numbers in this set. To get the maximum height of the tower, it was possible to calculate for each length the number of bars with this length, and from these numbers is to choose the maximum.

**B. Computer Game**

Constraints in the problem allows us to solve it this way: we keep the current number of health from the boss, and current summary damage from used scrolls per second. At the next step, we choose which scrolls we can use in the current second. Of all these, we find the scroll, which causes the most damage, and apply it. If at some point we could not use any of the scrolls, and the current damage in one second does not exceed regeneration, we deduce that there are no answers. Otherwise, continue to iterate the algorithm until the number hit points will be nonnegative.

**C. Old Berland Language**

One of the easiest to understand solutions of this problem is as follows: sort the words in ascending order of length, while remembering their positions in the source list. We will consistently build our set, starting with the short strings: strings of length one can only be strings "0" and "1". If the number of words of length one in a set are more than two, hence there are no answers. Add the desired number of strings of length one to answer, and remove it from the current list. Then look at the string of length two: each of the remaining strings of length one can be extended in two ways (having added to each of these symbols 0 and 1). Add the desired number of strings of length two in our answer, and then increase the length of the remaining strings by one. Continue this process, until we get all words from the input set. You can see that if at some moment the number of allowable words exceeded the number of remaining, the extra words can be ignored and solution takes O (N * the maximum length of input set) time.

**D. Lesson Timetable**

state of dynamics will be a pair of numbers - the number of current room and number of groups which have first lesson in the room with a number not exceeding the current and for which the second room is not defined yet. For each state check all possible number of groups for which the second lesson will be held in the current classroom. When you add an answer from the new state, it must be multiplied by the corresponding binomial coefficients (the number of ways to select groups which have the first lesson in next room -

*X*

_{i}+ 1, and the number of ways to select groups which have the second lesson in the current classroom).

**E. Trial for Chief**

*D*). It is easy to see that we can construct a sequence of

*D*+ 1 repainting leads to the desired coloring:

- The first step of color all the cells at a distance less than or equal to
*D*in black color

- At the second step color all the cells at a distance less than or equal to
*D*- 1 in white

- Etc.

Of all the cells, choose the one for which this distance is minimal, and this distance increased by one will be the answer to the problem.

First, compress all vertices between which distance is equal to zero in one component (get graph of connected components).

Note that if we repaint some cell, it can be repainted and all its component, as a result coloring will not change.

1) we can prove that any coloring can be reduced to a form such that each subsequent repainting is a subset of the previous one.

2) the last repainting will consist of exactly one vertex (possibly the whole monochromatic connected region of the original board).

Therefore, one of the best answers will be of the form described in the review. If as the initial vertex, we take a cell, lying in the last repainting of the optimal sequence, the algorithm finds the solution not worse than optimal.

http://ideone.com/il8Do

sis the last word written out, and its length isk. Then all strings that have lengthkand are lexicographically not larger thans, are 'bad' (each of them has a prefix that is already written out). So, to find out thek-prefix of the next word we need to increasesby 1 (as a binary number). Obviously, ifsconsists of 1's it's impossible, since we will get a word of lengthk+1. Now we need to find lexicographically smallest encoding of the next word. To do that, just add zeros to the increased value ofs.http://ideone.com/lHtSZ