Problem:
 Score : 400 points    Problem Statement Mr. Takahashi has a string s consisting of lowercase English letters.
He repeats the following operation on s exactly K times.   Choose an arbitrary letter on s and change that letter to the next alphabet. Note that the next letter of z is a .   For example, if you perform an operation for the second letter on aaz , aaz becomes abz .
If you then perform an operation for the third letter on abz , abz becomes aba .  Mr. Takahashi wants to have the lexicographically smallest string after performing exactly K operations on s .
Find the such string.      Constraints  1≤|s|≤10^5  All letters in s are lowercase English letters.  1≤K≤10^9         Input The input is given from Standard Input in the following format:  s  K       Output Print the lexicographically smallest string after performing exactly K operations on s .        Sample Input 1 xyz
4      Sample Output 1 aya  For example, you can perform the following operations: xyz , yyz , zyz , ayz , aya .       Sample Input 2 a
25      Sample Output 2 z  You have to perform exactly K operations.       Sample Input 3 codefestival
100      Sample Output 3 aaaafeaaivap

Solution (Python):
S = raw_input()
K = input()
M = ord('z')+1
ans = []
aa = ans.append
for s in S:
    o = ord(s)
    if s != 'a' and K>0 and M-o <= K:
        aa('a')
        K -= M-o
    else:
        aa(s)
if K > 0:
    K %= 26
    a = ord(ans[-1]) + K
    if a > ord('z'):
        a -= 26
    ans[-1] = chr(a)
print ''.join(ans)

Solution (Python):
s = raw_input()
K = input()

def dista(c):
    if(c=='a'):return 0
    return ord('z')-ord(c)+1

s = [c for c in s]

for i in range(len(s)):
    if(dista(s[i])<=K):
        K -= dista(s[i])
        s[i] = 'a'
    

if(K>0):
    last = s[-1]
    p = ord(last)-ord('a')
    p = (p+K)%26
    last = chr(p+ord('a'))
    s[-1] = last
    
print ''.join(s)

Solution (Python):
s = raw_input()
k = int(raw_input())

opreq = [(ord('z') - ord(c) + 1) % 26 for c in s]

s_ = []

if len(s) > 1:
    i = 0
    while i < len(s) - 1:
        if opreq[i] <= k:
            k = k - opreq[i]
            s_.append('a')
        else:
            s_.append(s[i])
        i += 1


last_c = chr((k + ord(s[-1]) - ord('a')) % 26 + ord('a'))
s_.append(last_c)

print("".join(s_))

Solution (Python):
s = list(raw_input())
k = int(raw_input())
 
for i in range(len(s)):
    numChanges = ord('z') - ord(s[i]) + 1
    if s[i] != 'a' and numChanges <= k:
        s[i] = 'a'
        k -= numChanges
 
    if i == len(s) - 1:
        s[i] = chr(ord(s[i]) + (k % 26))
 
print ''.join(s)

Solution (Python):
s = input()
s += " "
K = int(input())
alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
k = 0

k = K
for i in range(len(s) - 1):
	gap = (26 - alphabet.index(s[i])) % 26
	if K >= gap:
		K -= gap
		s = s[:i] + "a" + s[i+1:]
s = s[:-1]

if K:
	n = alphabet.index(s[-1])
	n += K
	n %= 26
	s = s[:-1] + alphabet[n]

print(s)

Solution (Python):

"""
Created on Sat Sep 24 21:28:20 2016

@author: dotha
"""



def move(s, k):
    if len(s) == 1:
        return chr(97+(ord(s) + k - 97)%26)
    if s[0] == 'a' or 123-ord(s[0]) > k:
        return s[0] + move(s[1:], k)
    else:
        return 'a' + move(s[1:], k-(123-ord(s[0])))

def move_(l,k):
    if l =='a' or 123-ord(l) > k:
        return (l,k)
    else:
        return ('a', k-(123-ord(l)))

s = input()
K = int(input())
answer=[]


for l in s:
    (x,K) = move_(l,K)
    answer.append(x)

answer[-1] = chr(97++(ord(answer[-1]) + K - 97)%26)
    
print(''.join(answer))

Solution (Python):
s = input()
K = int(input())

for i in range(0, len(s)):
	if (s[i] != 'a'):
		if (122 - ord(s[i]) + 1 <= K):
			K -= (122 - ord(s[i]) + 1)
			s = s[:i] + 'a' + s[i+1:]
			if (K == 0):
				break

if (K >= 0):
	K %= 26
	x = ord(s[len(s) - 1]) + K
	lc = chr(x) if x <= 122 else chr(x - 26)
	s = s[:len(s) - 1] + lc

print (s)

Solution (Python):
def needed_to_flip(c):
    return ord('z') - ord(c) + 1

s = list(raw_input())
K = int(raw_input())




for i, c in enumerate(s):
	if c == 'a' and i != len(s) - 1:
		continue
	needed = needed_to_flip(c)
	
	if K >= needed:
		K -= needed
		s[i] = 'a'
		
	
	if i == len(s) - 1:
		s[i] = chr(ord(s[i]) + (K % 26))

print ''.join(s)

Solution (Python):
s = input()
K = int(input())

def cord(c):
    return ord(c)-97

def cchr(n):
    return chr(n+97)

def smcheck(c, k):
    return 26-cord(c) <= k

def smcnt(c, k):
    return 26-cord(c)

ans = ''
for c in s:
    sc = smcnt(c, K)
    if sc <= K and sc < 26:
        K -= sc
        ans += cchr((cord(c) + sc)%26)
        
    else:
        ans += c
if K>26:
    K = K%26
if K>0:
    lc = ans[-1]
    ans = ans[:-1]
    ans += cchr((cord(lc)+K)%26)

print(ans)

Solution (Python):
s = raw_input()
K = int(raw_input())
s_list = list(s)
for i in range(len(s_list)):
    
    lag = ord('z') - ord(s_list[i]) + 1
    if s_list[i] != 'a' and K >= lag and i < len(s_list) - 1:
        
        s_list[i] = 'a'
        K -= lag
    elif i == len(s_list) - 1:
        s_list[i] = chr(ord('a') + ((ord(s_list[i]) - ord('a') + K) % 26))
print "".join(s_list)

Solution (Python):
import string


s = raw_input()
k = int(raw_input())

a_ls = list(string.ascii_lowercase)
a_len = len(a_ls)

end = ''

for c in s:
    idx = a_ls.index(c)
    for_overflow = a_len - idx
    if c is not 'a' and k - for_overflow >= 0:
        end += 'a'
        k -= for_overflow
    else:
        end += c

if k > 0:
    end = end[:len(end)-1] + a_ls[(a_ls.index(end[-1]) + k) % a_len]


print end

Solution (Python):
s = list(raw_input())
k = input()
for i in xrange(len(s)-1):
    if s[i] == 'a':
        continue
    need = ord('z') - ord(s[i]) + 1
    if need <= k:
        k -= need
        s[i] = 'a'
s[-1] = chr(((ord(s[-1]) + k - ord('a')) % 26) + ord('a'))
print ''.join(s)

Solution (Python):
def dist(c):
    return (ord('z') - ord(c) + 1) % 26

s = list(input())
k = int(input())
i = 0
while 0 < k and i < len(s):
    d = dist(s[i])
    if d <= k:
        k -= d
        s[i] = 'a'
    i += 1

s[-1] = chr((ord(s[-1]) + (k % 26)))
print(''.join(s))

Solution (Python):
S = list(raw_input())
K = input()

k = K

for i,s in enumerate(S):
    cost = ord('z')-ord(s)+1
    if cost <= k and s != "a":
        k-=cost
        S[i] = 'a'


c = chr((ord(S[-1])-ord('a')+k)%26+ord('a'))
S[-1] = c

print "".join(S)

Solution (Python):
s = raw_input()
k = int(raw_input())
albets = 'abcdefghijklmnopqrstuvwxyz'
dic = {}
for idx, letter in enumerate(albets):
    dic[letter] = 26 - idx
dic['a'] = 0
ans = ''
for l in s[:-1]:
    if k >= dic[l]:
        ans += 'a'
        k -= dic[l]
    else:
        ans += l
ans += albets[(26 - dic[s[-1]] + k) % 26]
print ans

Solution (Python):
o = ord('a')
s = [ord(c) - o for c in raw_input().strip()]
k = int(raw_input())
for i, c in enumerate(s):
    if c and 26 - c <= k:
        k -= 26 - c
        s[i] = 0
k %= 26
s[-1] += k
if s[-1] >= 26:
    s[-1] -= 26
print ''.join(chr(o + c) for c in s)

Solution (Python):


ORD_A = ord("a")


def str2list(s):
    return list(map(lambda c: ord(c) - ORD_A, s))


def list2str(xs):
    return "".join(map(lambda x: chr(x + ORD_A), xs))


def find_min(s, k):
    def to_a_cost(x):
        return (26 - x) % 26
    xs = str2list(s)
    n = len(s)
    for i in range(n):
        c = to_a_cost(xs[i])
        if c <= k:
            k -= c
            xs[i] = 0
    k %= 26
    for i in range(n)[::-1]:
        if xs[i] != 0:
            xs[i] += k
            break
    else:
        xs[-1] += k
    return list2str(xs)


def main():
    s = input()
    k = int(input())
    print(find_min(s, k))


if __name__ == '__main__':
    main()

Solution (Python):
N = 26
ord_a = ord('a')
S = [ord(s) - ord_a for s in input()]
K = int(input())


for i in range(len(S)):
    r = (N - S[i]) % N
    if K >= r:
        K -= r
        S[i] = 0


S[-1] += K % N
S[-1] %= N

answer = ''.join(chr(s + ord_a) for s in S)
print(answer)

Solution (Python):
def f(c):
    if c == "a":
        return 0
    return ord("z") - ord(c) + 1

s = input()
K = int(input())
ans = list(s)
for i, c in enumerate(s):
    n = f(c) 
    
    if n <= K:
        ans[i] = "a"
        K -= n
if K > 0:
    ans[-1] = chr((ord(ans[-1]) - ord("a") + K) % 26 + ord("a"))
print("".join(ans))

Solution (Python):
s=raw_input()
k=int(raw_input())
news=""
for i in xrange(len(s)):
    if s[i]=='a':
        news+="a"
        continue
    tmp=ord('z')-ord(s[i])+1
    if k-tmp>=0:
        news+="a"
        k-=tmp
    else:
        news+=s[i]
if k>0:
    news=news[0:len(s)-1]+chr((k+ord(news[-1])-ord('a'))%26+ord('a'))
print(news)

Solution (Python):
import string

s=raw_input()
K=int(raw_input())
ans=''
for c in s:
    ci=string.lowercase.index(c)
    m=(26-ci)%26
    if K>=m:
        K-=m
        ans+='a'
    else:
        ans+=string.lowercase[ci]
K%=26
last=ans[-1]
lasti=string.lowercase.index(last)
nl=string.lowercase[lasti+K]
ans=ans[:-1]+nl
print ans

Solution (Python):

s= input()
K=int(input())
def can_change_to_a(st,remains):
    if ord('z')+1 - ord(st) <= remains:
        return True
    return False

s_ans = list(s)
for i,ch in enumerate(s):
    if ch=='a':
        continue
    if K >= ord('z')+1-ord(ch):
        K -= ord('z')+1-ord(ch)
        s_ans[i]='a'

s_ans[-1]= chr(  (ord(s_ans[-1])-ord('a')+K )%26 + ord('a'))

print(''.join(s_ans))

Solution (Python):



from __future__ import division, print_function, absolute_import, unicode_literals

S = input()
k = int(input())

ans = ''
for s in S:
    if s == 'a':
        ans += s
        continue
    
    tmp = ord('z') - ord(s) + 1
    if k >= tmp:
        ans += 'a'
        k -= tmp
    else:
        ans += s
if k > 0:
    k = k % 26
    ans = ans[:-1] + chr(ord(ans[-1]) + k)
print(ans)

Solution (Python):
from string import lowercase as alpha

morph = { ch : chr(ord(ch) + 1) for ch in alpha }
morph['z'] = 'a'
s = raw_input().strip()
a = list(s)
k = int(raw_input())

z_next = ord('z') + 1
for i, ch in enumerate(a):
    if ch == 'a':
        continue
    z_dist = z_next - ord(ch) 
    if z_dist <= k:
        k -= z_dist
        a[i] = 'a'
k %= 26
a[-1] = chr(ord(a[-1]) + k)

print(''.join(a))

Solution (Python):
s = map(ord, raw_input())
k = int(raw_input())

for i in xrange(len(s)):
    if s[i] == 97:
        continue
    n = 123 - s[i]
    if n <= k:
        s[i] = 97
        k -= n

k %= 26
s[-1] += k

print ''.join(map(chr, s))

Solution (Python):

import sys,copy,math,heapq,itertools as it,fractions,re,bisect,collections as coll

alpha = "abcdefghijklmnopqrstuvwxyz"

s = raw_input()
K = int(raw_input())
dic = dict([alpha[i], i] for i in xrange(len(alpha)))

ans = [] 
for si in s:
    d = 26 - dic[si] if si != "a" else 0
    if d <= K:
        ans.append("a")
        K -= d
    else:
        ans.append(si)

if K > 0:
    ans[-1] = alpha[(dic[ans[-1]] + K) % 26]

print "".join(ans)

Solution (Python):

s = input()
k = int(input())
t = list(map(lambda c: ord(c)-ord('a'), s))
for i in range(len(t)):
    if t[i] != 0 and 26-t[i] <= k:
        k -= 26-t[i]
        t[i] = 0
if k:
    t[i] = (t[i]+k) % 26
    k = 0
ans = ''.join(map(lambda c: chr(c+ord('a')), t))
print(ans)

Solution (Python):
from sys import exit, setrecursionlimit
from functools import reduce
from itertools import *

def read():
  return int(input())

def reads():
  return [int(x) for x in input().split()]

setrecursionlimit(10000)

s = list(input())
K = read()

for i in range(len(s)):
  d = ord('z') - ord(s[i]) + 1
  if s[i] != 'a' and d <= K:
    s[i] = 'a'
    K -= d

K %= 26

s[-1] = chr(ord(s[-1]) + K)

print(''.join(s))

Solution (Python):
S = list(input())
K = int(input())
N = len(S)
for i in range(N):
    if S[i] == 'a':
        continue
    diff = ord('z') - ord(S[i]) + 1
    if diff <= K:
        K -= diff
        S[i] = 'a'
if K != 0:
    K %= 26
    S[i] = chr(ord(S[i]) + K)
print("".join(S))