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



Sample input:
xyz
4


Sample output:
aya

Submission status: Accepted

Solution code:
S = input()
K = int(input())
ans = ""
for i in range(len(S)):
    if(S[i] != 'a' and K >= (ord('a') - ord(S[i]))%26):
        ans += 'a'
        K -= (ord('a') - ord(S[i]))%26
    else:
        ans += S[i]

K %= 26
ans = ans[:-1] + chr(ord('a') + (ord(ans[-1])-ord('a') + K)%26)
print(ans)

Submission status: Accepted

Solution code:
def up(c, i):
    i %= 26
    b = ord('z')-ord(c)+1
    if i >= b:
        i -= b
        c = 'a'
    c = chr(ord(c)+i)
    return c



s = input()
K = int(input())

a = ord('a')
ans = ''

for i in range(len(s)):
    b = ord('z')-ord(s[i])+1
    if i == len(s)-1:
        ans += up(s[i], K)
    elif b == 26:
        ans += 'a'
    elif K >= b:
        K -= b
        ans += 'a'
    else:
        ans += s[i]

print(ans)

Submission status: Accepted

Solution code:
s = list(raw_input())
K = int(raw_input())
for i in xrange(len(s)):
  if s[i] == 'a': continue
  d = ord('z') - ord(s[i]) + 1
  if K >= d:
    s[i] = 'a'
    K -= d
    
K %= 26
s[-1] = chr((ord(s[-1]) - ord('a') + K)%26 + ord('a'))
  
print "".join(s)

Submission status: Accepted

Solution code:
dict_alp = dict([(chr(i), i - 97) for i in range(97, 97+26)])
alp = [chr(i) for i in range(97, 97+26)]

s = list(input())
k = int(input())
l = len(s)

i = 0
while k > 0:
    if i == l - 1:
        k %= 26
        s[i] = alp[(dict_alp[s[i]] + k) % 26]
        break

    if s[i] == alp[0]:
        i += 1
        continue

    if k < 26 - dict_alp[s[i]]:
        i += 1
        continue

    k -= (26 - dict_alp[s[i]])
    s[i] = alp[0]

    i += 1

print("".join(s))

Submission status: Accepted

Solution code:
S = input()
k = int(input())
goal = ord('z') + 1
ans = []
for c in S:
    if c == 'a':
        ans.append('a')
        continue
    d = goal - ord(c)
    if k >= d:
        k -= d
        ans.append('a')
    else:
        ans.append(c)
last = (k + ord(ans[-1]) - ord('a')) % 26
ans[-1] = chr(ord('a') + last)
print(''.join(ans))

Submission status: Accepted

Solution code:
s = list(input())
K = int(input())

for i, c in enumerate(s):
	if K <= 0:
		break
	diff = (123 - ord(c)) % 26
	if diff <= K:
		s[i] = 'a'
		K -= diff
s[-1] = chr(97 + (ord(s[-1]) - 97 + K) % 26)
print(''.join(s))

Submission status: Accepted

Solution code:
s = list(input())
K = int(input())

alphabet = "abcdefghijklmnopqrstuvwxyz"

for i in range(len(s)):
    if (26 - alphabet.index(s[i]) <= K) and s[i] != "a":
        K -= 26 - alphabet.index(s[i]) 
        s[i] = "a"
        
if K > 0:
    K = K % 26
    s[-1] = alphabet[(alphabet.index(s[i])+K)%26]
    
print("".join(s))

Submission status: Accepted

Solution code:
s = list(input())
K = int(input())

alphabet = "abcdefghijklmnopqrstuvwxyz"

for i in range(len(s)):
    if (26 - alphabet.index(s[i])) <= K and (s[i] != "a"):
        K -= (26 - alphabet.index(s[i]))
        s[i] = "a"
        
    if K <= 0:
        break
        
if K > 0:
    K = K % 26
  
    idx = alphabet.index(s[-1])+K
    s[-1] = alphabet[idx%26]
    
print("".join(s))

Submission status: Accepted

Solution code:
s = list(input())
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']

for i in range(len(s)):
    if K <=0 :
        break
    if (26 - alphabet.index(s[i])) <= K and s[i] != "a":
        
        K -= (26 - alphabet.index(s[i]))
        s[i] = "a"
#         print(K)

if K > 0:
    if K%26 + alphabet.index(s[-1]) > 25:
        s[-1] = alphabet[(K%26 - alphabet.index(s[-1]))]
    else:
        s[-1] = alphabet[(K%26+alphabet.index(s[-1]))]
print("".join(s))

Submission status: Accepted

Solution code:
s=list(input())
k=int(input())
for i in range(len(s)):
    if s[i] == 'a': continue
    c = ord('z') - ord(s[i]) + 1
    if c <= k:
        k -= c
        s[i] = 'a'
s[-1] = chr(ord('a') + ((ord(s[-1]) - ord('a') + k) % 26))
print(''.join(s))

Submission status: Accepted

Solution code:
s=list(input())
k=int(input())
for i in range(len(s)):
    c = (ord('z') - ord(s[i]) + 1)%26
    if c <= k:
        k -= c
        s[i] = 'a'
s[-1] = chr(ord('a') + ((ord(s[-1]) - ord('a') + k) % 26))
print(''.join(s))

Submission status: Accepted

Solution code:
s = [c for c in input()]
k = int(input())
l = len(s)
for i in range(l):
    t = (ord('z')-ord(s[i])+1)%26
    if t <= k:
        k -= t
        s[i] = 'a'
s[-1] = chr((ord(s[-1]) - ord('a') + k) % 26 + ord('a'))
print(''.join(s))

Submission status: Accepted

Solution code:
w="bcdefghijklmnopqrstuvwxyza"
d={}
for i in xrange(len(w)):
    d[w[i]]=25-i
ss=raw_input()
s=[]
for i in xrange(len(ss)):
    s.append(ss[i])
K=int(raw_input())
for i in xrange(len(s)):
    if d[s[i]]<=K:
        K -= d[s[i]]
        s[i]="a"
K%=26
s[len(s)-1]=w[(25-d[s[len(s)-1]]+K)%26]
print "".join(s)

Submission status: Accepted

Solution code:
import string

s = list(input())
k = int(input())
d = {c:(26-i)%26 for i, c in enumerate(string.ascii_lowercase)}
d_inv = {(26-i)%26:c for i, c in enumerate(string.ascii_lowercase)}

for i in range(len(s)):
    if k > 0:
        if d[s[i]] <= k:
            k -= d[s[i]]
            s[i] = "a"
    else:
        break
s[-1] = d_inv[(d[s[-1]]-k)%26]

print("".join(s))

Submission status: Accepted

Solution code:
#coding=UTF-8


s=input()
K=int(input())

num_s=[]

for moji in s:
    num_s.append(ord(moji)-ord('a'))

#a にしたら優勝
ans=''
for idx in range(0,len(num_s)-1,1):
    if num_s[idx]==0:
        ans=ans+'a'
    elif K >= (26 - num_s[idx]):
        K=K-(26 - num_s[idx])
        ans=ans+'a'
    else:
        #回せないと不利になる一方
        ans=ans+s[idx]

#残りは最後に
ans=ans+chr( (num_s[len(num_s)-1] + K)%26 + ord('a'))

print(ans)

Submission status: Accepted

Solution code:
s=raw_input()
K=int(raw_input())
ozp1=123



ls=list(s)
for i in range(len(ls)):
    nowwork=ozp1-ord(ls[i])
    if i==len(ls)-1:
        K%=26
        #print K,nowwork
        ls[i]=chr(ord(ls[i])+K)
        if ord(ls[i])>ord("z"):
            zu=ord(ls[i])-ord("z")-1
            #print zu
            ls[i]=chr(ord("a")+zu)
    if K==0:
        break

    if ls[i]=="a":
        continue
    if nowwork<=K and (len(ls)-1!=i):
        K-=nowwork
        ls[i]="a"

print "".join(ls)

Submission status: Accepted

Solution code:
jisho = ["z","y","x","w","v","u","t","s","r","q","p","o","n","m","l","k","j","i","h","g","f","e","d","c","b","a"]

s = list(input())
n = len(s)
k =int(input())
res = ""

for elem in s:
    if elem == "a":
        res += "a"
    elif jisho.index(elem)+1 <= k:
        res += "a"
        k -= jisho.index(elem)+1
    else:
        res += elem

res = res[0:n-1] + jisho[jisho.index(res[n-1])-k%26]
print(res)

Submission status: Accepted

Solution code:
s = bytearray(raw_input())
N = int(raw_input())
dis = [ord('z')-c for c in s]

for idx, d in enumerate(dis[:-1]):
    if d!=25 and d+1 <= N:
        s[idx] = 'a'
        N -= (d+1)

if N > 0:
    N %= 26
    t = (dis[-1]-N) % 26
    s[-1] = chr(ord('z') - t)
print s

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# !/usr/bin/env python
# vim: set fileencoding=utf-8 :

"""
#
# Author:   Noname
# URL:      https://github.com/pettan0818
# License:  MIT License
# Created:  2016-09-28
#

# Usage
#
"""
import sys

def input_single_line():
    """Receive Inputs."""
    return input()

def input_two_line():
    """Receive Two Lined Inputs.
    Like this.

    N
    1 2 3 4 5
    """
    n = sys.stdin.readline()
    n = n.rstrip("\n")
    target = sys.stdin.readline()
    target = target.rstrip("\n")
    # target = target.split(" ")

    return n, target


def check_a_becombility(one_str: str) -> int:
    """Check how can this str become "a"
    >>> check_a_becombility("a")
    0
    >>> check_a_becombility("z")
    1
    >>> check_a_becombility("b")
    25
    """
    if ord(one_str) == 97:
        return 0  # Because this is "a"

    return 123 - ord(one_str)


def make_suzuki_process(target_str :str, chance: int) -> str:
    """Suzuki Process.
    >>> make_suzuki_process("xyz", 4)
    'aya'
    >>> make_suzuki_process("a", 25)
    'z'
    >>> make_suzuki_process("codefestival", 100)
    'aaaafeaaivap'
    >>> make_suzuki_process("aaaa", 1000)
    'aaam'
    >>> make_suzuki_process("a", 26)
    'a'
    """
    target_str = list(target_str)
    costs = [check_a_becombility(i) for i in target_str]

    for pos, each_cost in enumerate(costs):
        if chance - each_cost >= 0:
            chance = chance - each_cost
            target_str[pos] = "a"

    if chance > 0:
        target_str[-1] = chr(ord(target_str[-1]) + chance % 26)

    return "".join(target_str)

if __name__ == "__main__":
    import doctest
    doctest.testmod()

    inputted = input_two_line()

    print(make_suzuki_process(inputted[0], int(inputted[1])))

Submission status: Accepted

Solution code:
s = list(input())
k = int(input())
ans=[]
for c in s:
    if (ord('a')-ord(c))%26 <= k:
        ans.append('a')
        k-=(ord('a')-ord(c))%26
    else:
        ans.append(c)
ans[-1]=chr(ord(ans[-1])+k%26)
print(''.join(ans))

Submission status: Accepted

Solution code:
s = list(map(ord, input()))
K = int(input())

asca = 97
ascz = 122
start = 0
lens = len(s)

loop = lambda x: asca if x == ascz else x+1

for k in range(K):
    if start == lens - 1:
        for i in range((K-k) % (ascz - asca + 1)):
            s[start] = loop(s[start])
        break
    for i in range(start, lens):
        c = s[i]
        if i == lens - 1:
            s[i] = loop(c)
            break
        if c == asca:
            start += 1
            continue
        v = c + (K-k)
        if v > ascz:
            s[i] = loop(c)
            break
        else:
            continue
print(''.join(map(chr, s)))

Submission status: Accepted

Solution code:
import string
import sys
s = input()
k = int(input())
a = [ord(_) for _ in s]
for i in range(len(s)-1):
    if a[i] != ord('a'):
        df = ord('z') - a[i] + 1
        if k >= df:
            a[i] = ord('a')
            k -= df
    if k == 0: break
if k != 0:
    k = k % 26
    a[len(a)-1] += k - 26 if ord('z') < k + a[len(a)-1] else k
[print(chr(_), end="") for _ in a]
print("")

Submission status: Accepted

Solution code:
s = map(lambda i: (ord('z') - ord(i) + 1) % 26, raw_input())
n = len(s)
k = int(raw_input())

for i in range(n):
    if s[i] <= k:
        k -= s[i]
        s[i] = 0

s[-1] -= k

s = map(lambda i: (26 - i) % 26 + ord('a'), s)
s = ''.join(map(chr, s))

print s

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf = 10**10
mod = 10**9 + 7


def f():
    s = input()
    k = int(input())
    a = [ord(_) - ord('a') for _ in s]
    l = len(s)
    for i in range(l):
        if k < 1:
            break
        c = a[i]
        if c == 0:
            continue
        d = 26 - c
        if k >= d:
            a[i] = 0
            k -= d
    a[-1] = (a[-1] + k) % 26
    return ''.join([chr(_ + ord('a')) for _ in a])

print(f())

Submission status: Accepted

Solution code:
import bisect
import sys
import math
import itertools
sys.setrecursionlimit(10000)

INF = float('inf')

# input macro
def i():
    return int(raw_input())
def ii():
    return map(int,raw_input().split(" "))
def s():
    return raw_input()
def ss():
    return raw_input().split(" ")
def slist():
    return list(raw_input())
#

def join(s):
    return ''.join(s)

#iterate macro
def piter(n,m):
    return itertools.permutations(n,m)
def citer(n,m):
    return itertools.combinations(n,m)

#modulo macro
def modc(a,b,m):
    c = 1
    for i in xrange(b):
        c = c * (a - i) % m
        c = c * modinv(i + 1,m) % m
    return c
 
def gcd(a, b):
    (x, lastx) = (0, 1)
    (y, lasty) = (1, 0)
    while b != 0:
        q = a // b
        (a, b) = (b, a % b)
        (x, lastx) = (lastx - q * x, x)
        (y, lasty) = (lasty - q * y, y)
    return (lastx, lasty, a)
 
def modinv(a, m):
    (inv, q, gcd_val) = gcd(a, m)
    return inv % m

#bisect macro
def index(a, x):
    #Locate the leftmost value exactly equal to x
    i = bisect_left(a, x)
    if i != len(a) and a[i] == x:
        return i
    return -1

#memoize macro
def memoize(f):
    cache = {}
    def helper(*args):
        if args not in cache:
            cache[(args)] = f(*args)
        return cache[args]
    return helper

@memoize
def nck(a,b,m):
    b = min([a-b,b])
    if (b>a or b<0 or a<0):
        return 0
    elif a == 0:
        return 1
    return (nck(a-1,b-1,m)+nck(a-1,b,m)) % m

nh=zip([1,0,-1,0],[0,1,0,-1])

###########

st=slist()
k=i()

for j in range(len(st)):
    o=ord(st[j])
    if k >= 123-o and st[j]!='a':
        k -= (123-o)
        st[j]='a'
        
n=97+(ord(st[len(st)-1])-97+k)%26

st[len(st)-1] = chr(n)
print join(st)

Submission status: Accepted

Solution code:
s=input()
k=int(input())
d = dict()
alphabet = "abcdefghijklmnopqrstuvwxyz"
for i in range(len(alphabet)):
    d[alphabet[i]]=i
    
def f(s,k):
    s = [s[i] for i in range(len(s))]
    res = s
    L = 0
    for i in range(len(s)):
        l = max((26 - d[s[i]]) % 26, 0)
        if i==len(s)-1:
            res[i] = alphabet[(d[s[i]] + (k-L)) % 26]
        else:
            if L + l <= k:
                res[i] = "a"
                L += l
            else:
                continue
    return(''.join(res))
print(f(s,k))

Submission status: Accepted

Solution code:
y=list(raw_input())
k=int(raw_input())
for i in range(len(y)):
	if y[i]=='a':
		continue
	else:
		if 123-ord(y[i])<=k:
			k-=(123-ord(y[i]))
			y[i]='a'
y[len(y)-1]=chr(ord(y[len(y)-1])+k%26)
print ''.join(y)

Submission status: Accepted

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


s_l = ['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']

def s_i(st):
    for i in range(26):
        if st == s_l[i]:
            return i + 1
            break

j = 0
while k > 0 and j <= len(s)-1:
    a = s_i(s[j])
    if s[j] == 'a' and j < len(s)-1:
        pass
    elif 27 - a <= k and j < len(s)-1:
        k = k - (27 - a)
        s = s[:j] + 'a' + s[j+1:]
    elif j == len(s) - 1:
        t = (a+k)%26 - 1
        if t >= 0:
            s = s[:j] + s_l[t]
        else:
            s = s[:j] + s_l[t + 26]
        break
    else:
        pass
    j += 1

print s

Submission status: Accepted

Solution code:
import sys
S = str(input())
K = int(input())

arr = [ord(S[i]) for i in range(len(S))]
count = K
for i in range(len(S)):
    if i == len(S) - 1:
        count %= 26
        if ord('z') - arr[i] + 1 <= count:
            arr[i] = ord('a')
            count -= ord('z') - ord(S[i]) + 1
        arr[i] += count
        break

    if ord('z') - arr[i] + 1 <= count and arr[i] != ord('a'):
        arr[i] = ord('a')
        count -= ord('z') - ord(S[i]) + 1

    if count == 0:
        break

for c in arr:
    sys.stdout.write(chr(c))

print('')

Submission status: Accepted

Solution code:
s = raw_input()
s = list(s)
K = int(raw_input())
# _s, _K = s[:], K
a = "abcdefghijklmnopqrstuvwxyz"
# ai = a.index("a")
zi = a.index("z") 
si = [zi - a.index(c) + 1 if c != "a" else 0  for c in s]

for i, (c, ci) in enumerate(zip(s, si)):
    if K == 0:
        break
    if c == "a":
        continue
    if ci <= K:
        s[i] = 'a'
        K = K - ci

K = K % len(a)
if K > 0:
    s[-1] = a[a.index(s[-1]) + K]

print("".join(s))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
def main():                                                                                                               
  # input
  s = raw_input()
  K = int(raw_input())
  ch = [123-ord(c) for c in s]
  for i in range(len(s)):
    if ch[i] == 26 and i != len(s)-1:
      continue
    elif ch[i] < K:
      K = K - ch[i]
      ch[i] = 26
    elif ch[i] == K:
      K = K - ch[i]
      ch[i] = 26
      break
    elif ch[i] > K and i != len(s)-1:
      continue
    else:
      tmp = ch[i]
      ch[i] = ch[i] - K
      K = K - tmp
      if ch[i] <= 0:
        ch[i] == 26
  if K > 0:
    ch[len(s)-1] = ch[len(s)-1] - K%26
  # output
  ch = [str(unichr((27-c)+96)) for c in ch]
  print ''.join(ch)
main()

Submission status: Accepted

Solution code:
#C-Next Letter
str = input()
K = int(input())
tempK = K
result = ""
for i in range(len(str)-1):
	changeNum = 122-ord(str[i])+1
	if (changeNum <= tempK and str[i] != "a"):
		result += "a"
		tempK -= changeNum
	else:
		result += str[i]
tempK %= 26
if (tempK > 0):
	changeNum = ord(str[-1]) + tempK
	if (changeNum > 122):
		changeNum = changeNum -122 + 97 -1
	else:
		changeNum = changeNum
	result += chr(changeNum)
else:
	result += str[-1]
print(result)

Submission status: Accepted

Solution code:
import sys 

s = sys.stdin.readline().strip()
K = int(sys.stdin.readline())
c = []
for i in s:
        c.append(ord(i)-ord('a'))

i =0
while i< len(c):
        path2a = (26-c[i] ) % 26
        if path2a <= K:                                                                                                                                                                                                               
                K -= path2a
                c[i] = 0 
        i+=1
c[-1] = (c[-1]+ K) %26 

res = ""
for i in c:
        res += chr(i+ord('a'))

sys.stdout.write( res)

Submission status: Accepted

Solution code:
s = list(input())
k = int(input())

znext = ord('z') + 1
for i in range(len(s)):
    if k == 0:
        break
    if s[i] == 'a':
        continue
    if k >= znext - ord(s[i]):
        k -= znext - ord(s[i])
        s[i] = 'a'
s[-1] = chr(ord(s[-1]) + k % 26)
print(''.join(s))

Submission status: Accepted

Solution code:
s = list(input())
k = int(input())

for i in range(len(s)):
    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))

Submission status: Accepted

Solution code:
# def makelist(n, m):
# 	return [[0 for i in range(m)] for j in range(n)]

# n = int(input())
# a, b = map(int, input().split())
# s = input()


s = input()
K = int(input())
l = len(s)
ans = ['a']*l

ORDa = 97

for i in range(l):
	now = ord(s[i]) - ORDa
	tmp = 26 - now
	
	if K == 0:
		ans[i] = s[i]
	elif tmp == 26:
		ans[i] = s[i]
	elif tmp <= K:
		ans[i] = 'a'
		K -= tmp
	else:
		ans[i] = s[i]

if K > 0:
	K = K % 26
	tmp = ord(ans[-1])
	if tmp + K > ord('z'):
		aa = tmp + K - (ord('z') + 1)
		ans[-1] = chr(aa + ORDa)
	else:
		ans[-1] = chr(tmp + K)
		
print("".join(ans))
# last shori

Submission status: Accepted

Solution code:
#read in input
s = map((lambda x: ord(x) - 96), raw_input())
k = int(raw_input())


for i in range(len(s)):
  sv = s[i]
  if sv == 1 or (27 - sv) > k:
    continue
  s[i] = 1
  k = k - (27 - sv)

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

print "".join(map((lambda x: chr(x + 96)), s))

Submission status: Accepted

Solution code:
def r_shift(c, i):
    return chr(ord('a') + int(((ord(c) + i) - ord('a'))) % 26)
s = input()
K = int(input())
r = ''
for c in s[:-1]:
    dis = 26 - (ord(c) - ord('a'))
    if dis <= K and c != 'a':
        K -= dis
        r += 'a'
    else:
        r += c
if K > 0:
    r += r_shift(s[-1], K)
else:
    r += s[-1]
print(r)

Submission status: Accepted

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

new_s = ""

# for i in range(len(s)):
#     c = s[i]
#     if i == len(s) - 1:
#         if cost_for_next_a(c) <= K:
#             new_s += chr(97 + (K - cost_for_next_a(c)) % 26)
#         else:
#             new_s += chr(ord(c) + K)
#     elif c == 'a':
#         new_s += 'a'
#     elif cost_for_next_a(c) <= K:
#         new_s += "a"
#         K -= cost_for_next_a(c)
#     else:
#         new_s += c

for i in range(len(s)):
    c = s[i]
    cost = 123 - ord(c)
    if i == len(s) - 1:
        if cost <= K:
            new_s += chr(97 + (K - cost) % 26)
        else:
            new_s += chr(ord(c) + K)
    elif c == 'a':
        new_s += 'a'
    elif cost <= K:
        new_s += "a"
        K -= cost
    else:
        new_s += c

print(new_s)

Submission status: Accepted

Solution code:
def f(s,K):
 if K==0:
  return s,"",0
 res=0
 str=""
 for i in range(len(s)-1):
  if s[i]=="a":
   str+="a"
   res+=1
   continue
  if K>=123-ord(s[i]):
   str+="a"
   res+=1
   K-=123-ord(s[i])
  else:
   break
 if res==0:
  skip=0
  for skip in range(len(s)):
   if K>=123-ord(s[skip]):
    break
  return s[:skip],s[skip:],K
 return str,s[res:],K

ss=input()
KK=int(input())
sol=""
while len(ss)>1:
 x,ss,KK=f(ss,KK)
 sol+=x
if len(ss)==1:
 KK%=26
 if ord(ss[0])+KK<123:
  sol+=chr(ord(ss[0])+KK)
 else:
  sol+=chr(ord(ss[0])+KK-26)
print(sol)

Submission status: Accepted

Solution code:
def num_to_char(s_num):
    ch = []
    for num in s_num:
        ch.append(alphabet[(num%26)-1])
    return ch

s = raw_input()
K = 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']
ans = []

s_num = []
for char in s:
    s_num.append(alphabet.index(char) + 1)

for i in xrange(len(s_num)):
    if 27 - s_num[i] <= K and (s_num[i] != 1) and (i != len(s_num) - 1):
        K = K - (27 - s_num[i])
        s_num[i] = 27
    elif (i == len(s_num) - 1):
        s_num[i] = s_num[i] + K
    else:
        pass
print "".join(num_to_char(s_num))

Submission status: Accepted

Solution code:
s = list(input())
K = int(input())

for i in range(len(s)):
    if s[i] == "a":
        pass
    elif 123 - ord(s[i]) <= K:
        K -= (123 - ord(s[i]))
        s[i] = 'a'

K = K % 26
while K > 0:
    K -= 1
    if s[-1] == "z":
        s[-1] = "a"
    else:
        s[-1] = chr(ord(s[-1]) + 1)
print("".join(s))

Submission status: Accepted

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

alpha = [chr(i) for i in range(97,97+26)]

i = 0
while K>=26 and i<len(s)-1:
	if alpha.index(s[i]) != 0:
		K = K - (26 - alpha.index(s[i]))
		s = s[:i] + 'a' + s[i+1:]
	i=i+1

for moji in range(i, len(s)):
	if alpha.index(s[moji]) != 0:
		if K >= 26-alpha.index(s[moji]):
			K = K - (26-alpha.index(s[moji]))
			s = s[:moji] + 'a' + s[moji+1:] 

if K>0:
	last_char = alpha[ alpha.index(s[-1]) + (K % 26) ]
	s = s[:-1] + last_char

print s

Submission status: Accepted

Solution code:
s = raw_input()
k = input()
i = 0
while True:
    if i == len(s)-1:
        # print "equal"
        t = ord(s[i])+k
        if t > ord("z"):
            t -= ord("a")
            t %= (ord("z")-ord("a") + 1)
            t += ord("a")
        s = s[:i] + chr(t)
        print s
        exit()
        # print s, k
    elif s[i] != "a" and ord(s[i]) + k > ord("z"):
        # print "carry"
        k = k - ord("z") + ord(s[i]) -1
        s = s[:i] + "a" + s[i+1:]        
        # print s,k
    i += 1

Submission status: Accepted

Solution code:
ring = raw_input()
num = input()

str = []
for i in range(len(ring)):
  str.append(ring[i])

for i in range(len(str)):
  if num-1 >= ord("z")-ord(str[i]) and str[i] != "a":
    num =  num - (ord("z")-ord(str[i])+1)
    str[i] = "a"

num = num % 26
while(num > 0):
  if str[-1] == "z":
    num -= 1
    str[-1] = "a"
    continue
  str[-1] = chr(ord(str[-1])+1)
  num -= 1

ans = ""
for i in str:
  ans += i
print ans

Submission status: Accepted

Solution code:
s = raw_input()
K = int(raw_input())
_s, _K = s[:], K
a = "abcdefghijklmnopqrstuvwxyz"
ai = a.index("a")
zi = a.index("z") 
si = [zi - a.index(c) + 1 if c != "a" else 0  for c in s]

for i, (c, ci) in enumerate(zip(s, si)):
    if K == 0:
        break
    if c == "a":
        continue
    if ci <= K:
        s = s[0:i] + "a" + s[i+1:]
        K = K - ci

K = K % len(a)
if K > 0:
    s = s[:-1] + a[a.index(s[-1]) + K]

print(s)

Submission status: Accepted

Solution code:
s=raw_input()
K=input()
s=list(s)
#a-z 97-122
for i in xrange(len(s)):
    if s[i]=='a':
        continue
    if ord(s[i])+K < 123:
        continue
    else:
        K-=(123-ord(s[i]))
        s[i]='a'
if K>0:
    asc=ord(s[-1])+K%26
    s[-1]=chr(asc)
print "".join(s)

Submission status: Accepted

Solution code:
def num_to_char(s_num):
    ch = []
    for num in s_num:
        ch.append(alphabet[(num%26)-1])
    return ch

s = raw_input()
K = 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']
ans = []

s_num = []
for char in s:
    s_num.append(alphabet.index(char) + 1)

for i in xrange(len(s_num)):
    if 27 - s_num[i] <= K and (s_num[i] != 1) and (i != len(s_num) - 1):
        K = K - (27 - s_num[i])
        s_num[i] = 27
    elif 27 - s_num[i] <= K and (s_num[i] == 1) and (i != len(s_num) - 1):
        pass
    elif 27 - s_num[i] > K and (s_num[i] != 1) and (i != len(s_num)-1):
        pass
    elif 27 - s_num[i] > K and (s_num[i] == 1) and (i != len(s_num) - 1):
        pass

    elif (i == len(s_num) - 1):
        s_num[i] = s_num[i] + K

print "".join(num_to_char(s_num))

Submission status: Accepted

Solution code:
s = list(raw_input())
K = input()
for i in xrange(len(s)):
    if 123-ord(s[i]) <= K and s[i] != 'a':
        K-=123-ord(s[i])
        s[i] = 'a'
if K > 0:
    s[-1] = chr((ord(s[-1])%97+K%26)%26+97)
print ''.join(s)

Submission status: Accepted

Solution code:
s = list(input())
K = int(input())

for i in range(len(s)):
    tmp = ord(s[i]) # asciiコードにする
    # 123もaを表していると考える
    if 1 <= 123 - tmp <= K and tmp != 97:
        # 'a'にできるものは先にaにしていく
        s[i] = "a"
        K -= (123 - tmp)

if K > 0:
    K = K % 26 # 26回で一回転
    tmp = (K + ord(s[-1]))
    s[-1] = chr(tmp)

def print_all(s):
    for t in s:
        print(t, end="")
    print()


print_all(s)

Submission status: Accepted

Solution code:
moji = {a: b for a, b in zip("abcdefghijklmnopqrstuvwxyz", range(26))}

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

for i, e in enumerate(s):
    if e != "a":
        if 26 - moji[e] <= k:
            s[i] = "a"
            k -= 26 - moji[e]

k = k % 26 if k >= 0 else 0
if k != 0:
    s[-1] = "abcdefghijklmnopqrstuvwxyz"[(moji[s[-1]] + k) if moji[s[-1]]+k < 26 else moji[s[-1]] + k - 25]

print "".join(s)

Submission status: Accepted

Solution code:
def f(s,i,m):
    m = m%26
    if ord(s[i])+m <= 122:
        s[i] = chr(ord(s[i])+m)
    else:
        t = ord(s[i])+m-122
        s[i] = chr(96+t)
    return s

def judge(c,n):
    if ord(c)+n > 122:
        return (123 - ord(c))%26
    else:
        return 0
        
s = list(input())
n = int(input())
l = len(s)

for i in range(0,l):
    if i == l-1:
        s = f(s,i,n)
    else:
        d = judge(s[i],n)
        s = f(s,i,d)
        n = n - d

print(''.join(s))

Submission status: Accepted

Solution code:
def cost_for_next_a(c):
    return 123 - ord(c)

s = input()
K = int(input())

new_s = ""


for i in range(len(s)):
    c = s[i]
    if i == len(s) - 1:
        if cost_for_next_a(c) <= K:
            new_s += chr(97 + (K - cost_for_next_a(c)) % 26)
        else:
            new_s += chr(ord(c) + K)
    elif c == 'a':
        new_s += 'a'
    elif cost_for_next_a(c) <= K:
        new_s += "a"
        K -= cost_for_next_a(c)
    else:
        new_s += c

print(new_s)

Submission status: Accepted

Solution code:
s=list(input())
k=int(input())

def toA(char):
    return (ord('z')-ord(char)+1)%26

def toC(char,k):
    if(ord('z')-ord(char)>=k%26):
        return chr(ord(char)+k%26)
    else:
        return chr(ord('a')+k-ord('z')-ord(char))
for i in range(len(s)):
    if(k==0):
        break
    if(k>=toA(s[i])):
        k-=toA(s[i])
        s[i]='a'
    if(i==len(s)-1) and k>0:
        s[i]= toC(s[i],k)
print("".join(s))

Submission status: Accepted

Solution code:
def cost_for_next_a(c):
    return 123 - ord(c)

s = input()
K = int(input())

new_s = ""


# for i in range(len(s)):
#     if cost_for_next_a(s[i]) <= K:
#         new_s += "a"
#         K -= cost_for_next_a(s[i])
#     elif i != len(s) - 1:
#         new_s += s[i]
#     else:
#         new_s += chr(ord(s[i]) + K)

for i in range(len(s)):
    if i == len(s) - 1:
        if cost_for_next_a(s[i]) <= K:
            new_s += chr(97 + (K - cost_for_next_a(s[i])) % 26)
        else:
            new_s += chr(ord(s[i]) + K)
    elif s[i] == 'a':
        new_s += 'a'
    elif cost_for_next_a(s[i]) <= K:
        new_s += "a"
        K -= cost_for_next_a(s[i])
    else:
        new_s += s[i]

print(new_s)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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 character
last_c = chr((k + ord(s[-1]) - ord('a')) % 26 + ord('a'))
s_.append(last_c)

print("".join(s_))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)