Repairing Of String (Codeforces 795H)
Tags: Math, *special

Stepan had a favorite string s which consisted of the lowercase letters of the Latin alphabet. After graduation, he decided to remember it, but it was a long time ago, so he can't now remember it. But Stepan remembers some information about the string, namely the sequence of integers c1, c2, ..., cn, where n equals the length of the string s, and ci equals the number of substrings in the string s with the length i, consisting of the same letters. The substring is a sequence of consecutive characters in the string s.For example, if the Stepan's favorite string is equal to "tttesst", the sequence c looks like: c = [7, 3, 1, 0, 0, 0, 0].Stepan asks you to help to repair his favorite string s according to the given sequence c1, c2, ..., cn. 

Input: The first line contains the integer n (1 ≤ n ≤ 2000) — the length of the Stepan's favorite string.The second line contains the sequence of integers c1, c2, ..., cn (0 ≤ ci ≤ 2000), where ci equals the number of substrings of the string s with the length i, consisting of the same letters.It is guaranteed that the input data is such that the answer always exists.

Output: Print the repaired Stepan's favorite string. If there are several answers, it is allowed to print any of them. The string should contain only lowercase letters of the English alphabet. 

Note: In the first test Stepan's favorite string, for example, can be the string "kkrrrq", because it contains 6 substrings with the length 1, consisting of identical letters (they begin in positions 1, 2, 3, 4, 5 and 6), 3 substrings with the length 2, consisting of identical letters (they begin in positions 1, 3 and 4), and 1 substring with the length 3, consisting of identical letters (it begins in the position 3). 

Sample Inputs:
6
6 3 1 0 0 0

4
4 0 0 0

Sample Outputs:
kkrrrq

abcd

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
s = ''
c = 'a'
for i in range(n - 1, -1, -1):
    if a[i] > 0:
        cnt = a[i]
        for j in range(cnt):
            s += c * (i + 1)
            if c == 'a':
                c = 'b'
            else:
                c = 'a'
        for j in range(i + 1):
            a[j] -= cnt * (i - j + 1)
print(s)

Solution (Python):
from operator import mul
n = int(input())
arr = list(map(int, input().split()))
cnt = []
for v in reversed(arr):
    d = sum(map(mul, reversed(cnt), range(2, n + 2)))
    cnt.append(v - d)
cur = 0
for (i, v) in enumerate(reversed(cnt)):
    for j in range(v):
        print(chr(ord('a') + cur % 26) * (i + 1), end='')
        cur += 1
print()

Solution (Python):
n = int(input())
c = [0] + list(map(int, input().split())) + [0] * 10
real_count = [0] * (n + 10)
cur_letter = 'a'
for i in range(n, 0, -1):
    cur_len = i
    count = c[i]
    for j in range(i + 1, n + 1):
        count -= real_count[j] * (j - (cur_len - 1))
    real_count[i] = count
    if count < 0:
        continue
    for j in range(count):
        print(cur_letter * cur_len, end='')
        if cur_letter == 'a':
            cur_letter = 'b'
        else:
            cur_letter = 'a'