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())

t = []

for i in range(len(s)):
    c=ord(s[i])-97
    if i!=len(s)-1:
        if c!=0:
            if K>=26-c:
                K-=26-c
                t.append("a")
            else:
                t.append(chr(97+c))
        else:
            t.append("a")
    else:
        t.append(chr(97+(c+K)%26))

print("".join(t))

Submission status: Accepted

Solution code:
s = list(input())
K = int(input())
for i in range(len(s)-1):
    if K == 0:
        break
    else:
        if s[i] == "a":
            continue
        else:
            if 123-ord(s[i]) <= K:
                K -= 123-ord(s[i])
                s[i] = "a"
K = K %26
if K > 0:
    if ord(s[-1])+K <= 122:
        s[-1] = chr(ord(s[-1])+K)
    else:
        s[-1] = chr(96+((ord(s[-1])+K-122)))

print(*s,sep="")

Submission status: Accepted

Solution code:
s = str(input())
K = int(input())
L = [0 for k in range(len(s))]

for k in range(len(s)):
    L[k] = (123-ord(s[k]))%26

now = 0
while K > 0 and now < len(s):
    if K >= L[now]:
        K -= L[now]
        L[now] = 0
        now += 1
    else:
        now += 1

ans = ""
for k in range(len(s)):
    if L[k] == 0:
        ans += "a"
    else:
        ans += chr(123-L[k])
print(ans[:-1]+chr(ord(ans[-1])+K%26))

Submission status: Accepted

Solution code:
s = input()
K = int(input())
ans = ''
for ind,i in enumerate(s):
    if(i=='a' and ind!=len(s)-1):
        ans+='a'
        continue
    d = 123-ord(i)
    if(ind==len(s)-1):
        K%=26
        if(K>=d):
            ans += chr(97+K-d)
            continue
        ans+=chr(ord(i)+K)
    elif(K>=d):
        K-=d
        ans+='a'
    else:
        ans+=i
print(ans)

Submission status: Accepted

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

ans = ''

for i in range(len(s)-1):
    a = 123-ord(s[i])
    if a==26:
        a=0
    if a<=k:
        ans += 'a'
        k-=a
    else:
        ans += s[i]

ans += chr((ord(s[-1])+k-97)%26+97)

print(ans)

Submission status: Accepted

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

ans = ''

for i in range(len(s)-1):
    a = 123-ord(s[i])
    if a==26:
        ans += s[i]
    elif a<=k:
        ans += 'a'
        k-=a
    else:
        ans += s[i]

ans += chr((ord(s[-1])+k%26-97)%26+97)

print(ans)

Submission status: Accepted

Solution code:
import string
alp=string.ascii_lowercase
s=input()
k=int(input())
ans=[]
i=0
while i<len(s)-1:
    t=s[i]
    d=(26-alp.index(t))%26
    if d<=k:
        k-=d
        ans.append('a')
    else:
        ans.append(t)
    i+=1
if k>0:
    ans.append(alp[(alp.index(s[-1])+k%26)%26])
else:
    ans.append(s[-1])
print(''.join(ans))

Submission status: Accepted

Solution code:
import string
alp=string.ascii_lowercase
s=input()
k=int(input())
ans=''
i=0
while i<len(s)-1:
    t=s[i]
    d=(26-alp.index(t))%26
    if d<=k:
        k-=d
        ans+='a'
    else:
        ans+=t
    i+=1
if k>0:
    ans+=alp[(alp.index(s[-1])+k%26)%26]
else:
    ans+=s[-1]
print(ans)

Submission status: Accepted

Solution code:
#n,x=map(int,input().split())
#a=[int(i) for i in input().split()]
import string
alp=string.ascii_lowercase
di={}
for i in range(26):
    di[alp[i]]=i
s=input()
a=[]
for i in s:
    a.append(i)
l=a.pop()
if a:
    a=a[::-1]
k=int(input())
ans=''
i=0
while a:
    t=a.pop()
    d=(26-di[t])%26
    if d<=k:
        k-=d
        ans+='a'
    else:
        ans+=t
    i+=1
kk=k%26
ans+=alp[(alp.index(l)+kk)%26]
print(ans)

Submission status: Accepted

Solution code:
s=input()
k=int(input())
A={}
for i in range(26):
  A[chr(ord('a')+i)]=26-i
C=[]
for ss in s:
  C.append(A[ss])
ans=""
for i in range(len(s)):
  if i==len(s)-1:
    k%=26
    if ord(s[-1])+k>122:
      ans+=chr(ord(s[-1])+k-26)
    else:
      ans+=chr(ord(s[-1])+k)
  elif s[i]!="a" and k>=C[i]:
    ans+="a"
    k-=C[i]
  else:
    ans+=s[i]
print(ans)

Submission status: Accepted

Solution code:
def main():
    s = input()
    K = int(input())
    a = ord("a")
    code = [ord(c) - a for c in s]
    for i in range(len(s)):
        x = (26 - code[i]) % 26
        if x <= K:
            code[i] = 0
            K -= x
    code[-1] = code[-1] + K % 26
    ans = "".join([chr(a + x) for x in code])
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# 入力
s = input()
K = int(input())

# 先頭文字から順に可能な限りaにする
# 余った操作回数はすべて末尾文字に適用する
t = list(s)
L = K
for i in range(len(s)):
    d = 123 - ord(s[i])
    if 1 <= d <= min(25, L):
        t[i] = 'a'
        L -= d
t[-1] = chr((ord(t[-1]) - 97 + L) % 26 + 97)
ans = ''.join(t)

# 出力
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
C - 次のアルファベット / Next Letter
https://atcoder.jp/contests/code-festival-2016-quala/tasks/codefestival_2016_qualA_c

"""
import sys

from string import ascii_lowercase

def solve(s, K):
    ans = []
    lut = 'a' + ascii_lowercase[::-1]
    for ch in s:
        distance = lut.index(ch)
        if distance <= K:
            K -= distance
            ans.append('a')
        else:
            ans.append(ch)
    ans[-1] = ascii_lowercase[(ord(ans[-1]) - ord('a') + K) % 26]
    return ''.join(ans)


def main(args):
    s = input()
    K = int(input())
    ans = solve(s, K)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
C - 次のアルファベット / Next Letter
https://atcoder.jp/contests/code-festival-2016-quala/tasks/codefestival_2016_qualA_c

"""
import sys

from string import ascii_lowercase

def solve(s, K):
    ans = []
    for ch in s:
        distance = ascii_lowercase[::-1].index(ch) + 1
        if distance <= K and ch != 'a':
            K -= distance
            ans.append('a')
        else:
            ans.append(ch)
    ans[-1] = ascii_lowercase[(ord(ans[-1]) - ord('a') + K) % 26]
    return ''.join(ans)


def main(args):
    s = input()
    K = int(input())
    ans = solve(s, K)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
d = {chr(122 - i):i + 1 for i in range(26)}
d['a'] = 0
s = list(input())
k = int(input())
l = []
for i in range(len(s)):
  l.append(d[s[i]])
for i in range(len(s)):
  if l[i] <= k:
    k -= l[i]
    s[i] = 'a'
s[-1] = chr((ord(s[-1]) - 97 + k) % 26 + 97)
print(''.join(s))

Submission status: Accepted

Solution code:
S = input()
T = ""
K = int(input())
for s in S:
    x = (123 - ord(s))%26
    if K >= x:
        s = "a"
        K -= x
    T += s

K = K%26
ans = ""
for t in T[::-1]:
    d = min(K, 122 - ord(s))
    K -= d
    ans += chr(ord(t)+d)

print(ans[::-1])

Submission status: Accepted

Solution code:
s=input()
l=[]
for i in s:l.append((123-ord(i))%26)
k=int(input())
for i,j in enumerate(l):
    if j<=k:l[i],k=26,k-j
l[-1]=(l[-1]-k%26)+[0,26][l[-1]-k%26<1]
for i,j in enumerate(l):l[i]=chr(123-j)
print(''.join(map(str,l)))

Submission status: Accepted

Solution code:
a ='abcdefghijklmnopqrstuvwxyz'
s = input()
k = int(input())
sl =len(s)
nl =[(123-ord(l)) % 26 for l in s]

i = 0
while k > 0 and i < sl:
    if nl[i] !=0 and k >=nl[i]:
        k -= nl[i]
        nl[i] = 0
        i += 1
    else:
        i += 1
if k > 0:
    nl[-1] += 26- k
print(''.join([a[(26-i) % 26] for i in nl]))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(20000000)
#input = sys.stdin.readline
s = input()
k = int(input())
ans = []
for i in range(len(s)-1):
    if s[i] == "a":
        ans.append("a")
        continue
    if ord("z")+1 - ord(s[i]) <= k:
        ans.append("a")
        k -= ord("z")+1 - ord(s[i])
    else:
        ans.append(s[i])
ans.append(chr((ord(s[-1])+k - ord("a"))%26+ord("a")))
print("".join(ans))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

S=list(input())
N=len(S)
K=INT()

for i in range(N):
    toA=(ord('z')+1-ord(S[i]))%26
    if K>=toA:
        S[i]='a'
        K-=toA

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

Submission status: Accepted

Solution code:
a = list(map(str, input()))
b = int(input())

for i in range(len(a)):
    if a[i] == 'a':
        continue
    else:
        s = ord(a[i]) - 96
        if 27 - s <= b:
            a[i] = 'a'
            b -= 27 - s

if b != 0:
    a[-1] = chr(((ord(a[-1]) - 97 + b) % 26) + 97)

print(''.join(a))

Submission status: Accepted

Solution code:
s,k=open(0)
k=int(k)
*l,=map(ord,s[:-1])
for i,t in enumerate(l):
  j=(19-t)%26
  if j<=k:k-=j;l[i]=97
l[-1]+=k%26
print(*map(chr,l),sep='')

Submission status: Accepted

Solution code:
s = input().strip()
k = int(input())

ans = ''
for _s in s[:-1]:
  if _s == 'a':
    ans += 'a'
  elif 26 - ord(_s) + ord('a') <= k:
    ans += 'a'
    k -= 26 - ord(_s) + ord('a')
  else:
    ans += _s

ans += chr(ord('a') + (ord(s[-1]) - ord('a') + k) % 26)

print(ans)

Submission status: Accepted

Solution code:
#CODEFESTIVAL2016qualA
alpha='abcdefghijklmnopqrstuvwxyz'
S=input()
K=int(input())
ans=''
for s in S:
    idx=alpha.index(s)
    if idx==0 or 26-idx>K:
        ans+=s
        continue
    else:
        K-=26-idx
        ans+='a'
if K>0:
    tar=ans[-1]
    idx=alpha.index(tar)
    ans=ans[:-1]+alpha[(idx+K)%26]
print(ans)

Submission status: Accepted

Solution code:
s=input();x=[i-96for i in map(ord,s)];n=int(input())
for k,v in enumerate(x):
    y=(27-v)%26
    if y<=n:n-=y;x[k]=1
x[-1]+=n%26;print("".join(map(chr,[i+96for i in x])))

Submission status: Accepted

Solution code:
s=input()
x=[i-96for i in map(ord,s)]
n=int(input())
for i in range(len(s)):
    y=(26-x[i]+1)%26
    if y<=n:
        n-=y
        x[i]=1
x[-1]+=n%26
print("".join(map(chr,[i+96 for i in x])))

Submission status: Accepted

Solution code:
s = input()
x = []
for i in s:
    x.append(ord(i) - ord('a'))
K = int(input())
for i in range(len(x)):
    if x[i] == 0:
        continue
    d = 26 - x[i]
    if K >= d:
        x[i] = 0
        K -= d
x[-1] = (x[-1] + K) % 26
for i in x:
    print(chr(i + ord('a')), end = "")
print()

Submission status: Accepted

Solution code:
import sys

if __name__ == '__main__':
    ord_a = ord('a')
    s = [ord(c) - ord_a for c in input()]
    k = int(input())
    for x in s[:-1]:
        if 25 - x < k:
            sys.stdout.write('a')
            k -= (26 - x) % 26
        else:
            sys.stdout.write(chr(ord_a + x))
    print(chr((s[-1] + k) % 26 + ord_a))

Submission status: Accepted

Solution code:
s=[(26-(ord(i)-ord("a")))%26for i in list(input())]
k=int(input())
c=""
for i in range(len(s)-1):
    if k>=s[i]:
        k-=s[i]
        c+="a"
    else:
        c+=chr((26-s[i])%26+ord("a"))
print(c+chr(((26-s[-1])%26+k)%26+ord("a")))

Submission status: Accepted

Solution code:
import sys
from collections import deque
import copy
import math
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline


def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    S = input_raw[0]
    input_raw = read_func().strip().split()
    K = int(input_raw[0])
    newS =""
    for s in S:
        new_s = s
        if ord(s) - ord('a') > 0 and (K - ((ord('a') - ord(s)) % 26)) >= 0:
            new_s = "a"
            K -= ((ord('a') - ord(s)) % 26)
        newS += new_s
    new_end_char = newS[len(S) - 1]
    if K > 0:
        t = ord(newS[len(S) - 1]) + (K % 26)
        if t > ord('z'):
            t = t - ord('z') + ord('a')
        new_end_char = chr(t)
    print newS[:len(S) - 1] + new_end_char


if __name__ == '__main__':
    main()

Submission status: Accepted

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

for i in range(N - 1):
	if s[i] != "a":
		if K >= 123 - ord(s[i]):
			K -= 123 - ord(s[i])
			s[i] = "a"
			
s[-1] = chr((ord(s[-1]) - 97 + K) % 26 + 97)

print("".join(s))

Submission status: Accepted

Solution code:
s=input()
K=int(input())
ans=[]

for c in s:
  if c=="a":
    ans.append(c)
    continue
  if ord("z")+1- ord(c)<=K:
    K-=ord("z")+1-ord(c)
    ans.append("a")
    continue
    
  ans.append(c)
    
if K:
  ans[-1]=chr( (ord(ans[-1])+K%26 -ord("a"))%26+ord("a") )
print("".join(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from string import ascii_lowercase

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

d = {}
d2 = {}
for i in range(len(ascii_lowercase)):
    c = ascii_lowercase[i]
    d[c] = (len(ascii_lowercase)-i)%len(ascii_lowercase)
    d2[c] = i

for i in range(n):
    if s[i]!="a" and d[s[i]]<=k:
        k -= d[s[i]]
        s[i] = "a"

if k>0:
    k %= len(ascii_lowercase)
    s[-1] = ascii_lowercase[(d2[s[-1]]+k)%len(ascii_lowercase)]

print("".join(s))

Submission status: Accepted

Solution code:
s = input()
k = int(input())
ans = ""
for i in s:
    c = ord(i)
    if i == "a":
        ans += i
        continue
    need = ord("z") - ord(i) + 1
    if k >= need:
        k -= need
        ans += "a"
    else:
        ans += i
if k:
    ans = ans[:len(s)-1] + chr(ord(ans[-1]) + k % 26)
print(ans)

Submission status: Accepted

Solution code:
ord_a = ord('a')

ss = input()
K = int(input())

As = [ord(s) - ord_a for s in ss]

anss = []
for A in As[:-1]:
    num = 26 - A
    if A == 0 or K < num:
        anss.append(A)
    else:
        anss.append(0)
        K -= num

anss.append((As[-1] + K) % 26)

print(''.join([chr(ans + ord_a) for ans in anss]))

Submission status: Accepted

Solution code:
s = input()
k = int(input())
n = len(s)
mod = 26
alpha = "abcdefghijklmnopqrstuvwxyz"
aldic = {}
ansstr = ""
for i, al in enumerate(alpha, 1):
    aldic[al] = i

for i in range(n - 1):
    if k >= mod - aldic[s[i]] + 1 and s[i] != "a":
        ansstr += "a"
        k -= mod - aldic[s[i]] + 1
    else:
        ansstr += s[i]
ansstr += alpha[(aldic[s[n - 1]] + k - 1) % mod]
print(ansstr)

Submission status: Accepted

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


Costs = {chr(98 + i): (25 - i) for i in range(25)}
Costs['a'] = 0
alphabets = [chr(97 + i) for i in range(26)]


ans = ""
for i in range(len(S)):
    s = S[i]
    if K >= Costs[s]:
        K -= Costs[s]
        ans += 'a'
    else:
        ans += s


ans = ans[:-1] + alphabets[(alphabets.index(ans[-1]) + K) % 26]
print(ans)

Submission status: Accepted

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


Costs = {chr(98 + i): (25 - i) for i in range(25)}
Costs['a'] = 0
alphabets = [chr(97 + i) for i in range(26)]


ans = ""
for i in range(len(S) - 1):
    s = S[i]
    if K >= Costs[s]:
        K -= Costs[s]
        ans += 'a'
    else:
        ans += s


ans += alphabets[(alphabets.index(S[-1]) + K) % 26]
print(ans)

Submission status: Accepted

Solution code:
d1={'a':0,
'b':25,
'c':24,
'd':23,
'e':22,
'f':21,
'g':20,
'h':19,
'i':18,
'j':17,
'k':16,
'l':15,
'm':14,
'n':13,
'o':12,
'p':11,
'q':10,
'r':9,
's':8,
't':7,
'u':6,
'v':5,
'w':4,
'x':3,
'y':2,
'z':1}
d2=['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','a']
s=list(input())
k=int(input())
lens=len(s)
ans=''
ind=1
for i in s:
  if ind==lens:
    ans+=d2[(d2.index(i)+k)%26]
  elif k>=d1[i]:
    k-=d1[i]
    ans+='a'
  else:
    ans+=i
  ind+=1
print(ans)

Submission status: Accepted

Solution code:
s = input()
k = int(input())
ans = []
for i in range(len(s)):
  toa =(ord('z')-ord(s[i]))+1
  if s[i] =='a':ans += s[i]
  else:
   if toa <= k:
     k -= toa 
     ans += 'a'
   else: ans += s[i]

if k == 0:print("".join(ans))
else: 
  rest = k%26
  tmp = ord(ans[-1])+rest
  if tmp >122:
    tmp -= 26
  ans[-1] = chr(tmp)
  print("".join(ans))

Submission status: Accepted

Solution code:
a=ord('a')
alpha=['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']

s=input()
K=int(input())
a=ord('a')
N=len(s)
L=[0]*N
t=K
for i in range(N):
    L[i]=ord(s[i])-a
i=0
while i<N:
    #print(L)
    if L[i]==0:
        i+=1
        continue
    if t>=26-L[i]:
        t=t-(26-L[i])
        L[i]=0
    i+=1
L[-1]=(L[-1]+t)%26
x=''
for i in range(N):
    x+=alpha[L[i]]
print(x)

Submission status: Accepted

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

Submission status: Accepted

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

abc = 'bcdefghijklmnopqrstuvwxyza'
abc_to_index = {abc[_]: _ for _ in range(26)}

ans = ''
for i in range(N):
    if 25 - abc_to_index[s[i]] > K or i == N - 1:
        ans += s[i]
    else:
        ans += 'a'
        K -= 25 - abc_to_index[s[i]]
ans = ans[:-1] + abc[(abc_to_index[ans[-1]] + K) % 26]
print(ans)

Submission status: Accepted

Solution code:
S=[ord(i)-97 for i in list(input())]
K=int(input())
N=len(S)
T=[chr(S[i]+97) for i in range(N)]
for i in range(N):
    if K>=(26-S[i]):
        K-=(26-S[i])%26
        T[i]='a'
        S[i]=0
    else:
        continue
S[-1]=(S[-1]+K)%26
T[-1]=chr(S[-1]+97)
ans=""
for i in range(N):
    ans=ans+T[i]
print(ans)

Submission status: Accepted

Solution code:
A=["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"]
Num=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]

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

S=[]
for i in range(len(s)):
    for j in range(26):
        if s[i]==A[j]:
            S.append(Num[j])

for a in range(len(S)-1):
    if (26-S[a])%26<=K:
        K-=(26-S[a])%26
        S[a]=(S[a]+(26-S[a]))%26

S[len(S)-1]=(S[len(S)-1]+K)%26
#print(S)

for x in range(len(S)):
    for y in range(26):
        if S[x]==Num[y]:
            S[x]=A[y]
print("".join(S))

Submission status: Accepted

Solution code:
s = input()
K = int(input())
ans = ''
for i in s[:-1]:
    if i == 'a':
        ans += 'a'
        continue
    if 123 - ord(i) <= K:
        ans += 'a'
        K -= 123 - ord(i)
    else:
        ans += i
ans += chr((ord(s[-1])-97+K)%26 +97)
print(ans)

Submission status: Accepted

Solution code:
s=input()
N=len(s)
K=int(input())
a="abcdefghijklmnopqrstuvwxyz"
S=[]
A=list(a)
for i in range(N-1):
  if s[i]!="a":
    k=A.index(s[i])
    if K>=26-k:
      S.append("a")
      K-=26-k
    else:
      S.append(s[i])
  else:
    S.append("a")
if K>=0:
  k=A.index(s[N-1])
  S.append(A[(k+K)%26])
print("".join(S))

Submission status: Accepted

Solution code:
s = list(input())
K = int(input())
AB = ["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"]
L = len(s)
Ans = ""
for i in range(L-1):
    if s[i] == "a": Ans += "a"
    else:
        move = 26 - AB.index(s[i])
        if move <= K:
            Ans += "a"
            K -= move
        else: Ans += s[i]

if s[L-1] == "a": Ans += AB[K%26]
else:
    move = 26 - AB.index(s[L-1])
    if move <= K:
        K -= move
        Ans += AB[K%26]
    else:
        Ans += AB[AB.index(s[L-1]) + K]
print(Ans)

Submission status: Accepted

Solution code:
s=input()
K=int(input())
S=list(ord(s[i]) for i in range(len(s)))

for i in range(len(s)):
    if i!=len(s)-1:
        if S[i]!=97 and 123-S[i]<=K:
            K-=123-S[i]
            S[i]=97
    if i==len(s)-1:
        S[i]+=K%26
        if S[i]>=123:
            S[i]=S[i]-123+97
T=""
for i in range(len(s)):
    T+=chr(S[i])
print(T)

Submission status: Accepted

Solution code:
s = input()
K = int(input())
ans = ''
alphas = {'a':0,'b':25,'c':24,'d':23,'e':22,'f':21,'g':20,'h':19,'i':18,'j':17,'k':16,'l':15,'m':14,'n':13,'o':12,'p':11,'q':10,'r':9,'s':8,'t':7,'u':6,'v':5,'w':4,'x':3,'y':2,'z':1}
a = 'abcdefghijklmnopqrstuvwxyz'
dict1 = {}
dict2 = {}
i = 1
while i <= 26:
  dict1[i%26] = a[i-1]
  dict2[a[i-1]] = i
  i += 1
for chara in s:
  if alphas[chara] <= K:
    ans = ans + 'a'
    K = K - alphas[chara]
  else:
    ans = ans + chara
K = K%26
if len(ans)>=2:
  ans = ans[:-1]+dict1[(dict2[ans[-1]]+K)%26]
else:
  ans = dict1[(dict2[ans]+K)%26]
print(ans)

Submission status: Accepted

Solution code:
s=list(input())
k=int(input())
al=list('abcdefghijklmnopqrstuvwxyz')
n=0
while k>0:
    if n==len(s)-1:k=k%26
    if 26-al.index(s[n])<=k and s[n]!='a':
        k-=26-al.index(s[n])
        s[n]='a'
        n=min(len(s)-1,n+1)
    else:
        if n<len(s)-1:n+=1
        else:
            s[n]=al[al.index(s[n])+k]
            k=0
print(''.join(s))

Submission status: Accepted

Solution code:
s=input()
k=int(input())
f=lambda x:(123-ord(x))%26 #cost to "a"
g=lambda x:chr((ord(x)-97+k)%26+97)

b=[]
for i in s:
  cost=f(i)
  if cost<=k:
    b.append("a")
    k-=cost
  else:
    b.append(i)
if k:b[-1]=g(b[-1])
print("".join(b))

Submission status: Accepted

Solution code:
s=input()
k=int(input())
f=lambda x:(123-ord(x))%26 #cost to "a"
g=lambda x:chr((ord(x)-97+k)%26+97)

b=[]
for i in s:
  cost=f(i)
  if cost<=k:
    b.append("a")
    k-=cost
  else:
    b.append(i)
if k:b[-1]=g(b[-1])
print("".join(b))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from sys import stdin

s_in = lambda: stdin.readline()[:-1] # s = s_in()
d_in = lambda: int(stdin.readline()) # N = d_in()
ds_in = lambda: list(map(int, stdin.readline().split())) # List = ds_in()

alp = 26
s_list = list(s_in())
K = d_in()
alp0 = ord('a') - 1

for i in range(len(s_list)):
	num = ord(s_list[i]) - alp0
	if K >= (alp - num + 1) % alp:
		s_list[i] = 'a'
		K -= (alp - num + 1) % alp
		
s_list[-1] = chr(alp0 + 1 + (ord(s_list[-1]) - alp0 + K - 1) % alp)

print(''.join(s_list))

Submission status: Accepted

Solution code:
def c_next_letter(S, K):
    s = list(S)
    for t, elem in enumerate(s):
        if t == len(s) - 1 and K > 0:
            # 末尾に来たときに操作回数が残っていたら、その回数だけ操作する
            s[t] = chr(ord('a') + (ord(elem) - ord('a') + K) % 26)
        elif elem == 'a':
            continue  # 文字が最初から'a'ならば、何もしなくていい
        elif ord('z') - ord(elem) + 1 <= K:
            # K回以下の操作で'a'にできるなら、そうする
            s[t] = 'a'
            K -= ord('z') - ord(elem) + 1
    ans = ''.join(s)
    return ans

S = input()
K = int(input())
print(c_next_letter(S, K))

Submission status: Accepted

Solution code:
alp = ["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"]
s = list(input())
k = int(input())
a = 0
while k > 0:
    for i in range(26):
        if alp[i] == s[a]:
            t = i
            break
    if a == len(s) - 1:
        s[a] = alp[(t + k) % 26]
        break
    elif s[a] == "a":
        a = a
    elif 26 - t <= k:
        s[a] = "a"
        k -= 26 - t
    a += 1
print("".join(s))

Submission status: Accepted

Solution code:
from string import ascii_lowercase as al
cost = dict(zip(al, [0] + list(range(25, 0, -1))))

s = input()
K = int(input())
ans = ''
for si in s:
    if K - cost[si] >= 0:
        K -= cost[si]
        ans += 'a'
    else:
        ans += si
print(ans[:-1] + (al * 2)[al.index(ans[-1]) + K % 26])

Submission status: Accepted

Solution code:
import string
alph = string.ascii_lowercase * 2
s = input()
k = int(input())

needed = 0
ind_list = []
for i in range(len(s)):
	ind_list.append(alph.index(s[i]))
	if ind_list[-1] != 0:
		needed += 26 - ind_list[-1]

if needed <= k:
	mod = (k - needed) % 26
	print("a" * (len(s) - 1) + alph[mod])
else:
	ans = []
	for i in range(len(s) - 1):
		if k == 0 or s[i] == "a":
			ans.append(s[i])
			continue
		if k >= 26 - ind_list[i]:
			ans.append("a")
			k -= 26 - ind_list[i]
		else:
			ans.append(s[i])

	if k == 0:
		ans.append(s[-1])
	else:
		ans.append(alph[(ind_list[-1] + k) % 26])

	print(*ans, sep="")

Submission status: Accepted

Solution code:
# 入力
import sys
stdin = sys.stdin

def li(): return [int(x) for x in stdin.readline().split()]
def li_(): return [int(x)-1 for x in stdin.readline().split()]
def lf(): return [float(x) for x in stdin.readline().split()]
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(ns())
def nf(): return float(ns())

# ここから処理
s = ns()
k = ni()

chl = list(s)
res = k

for i, c in enumerate(chl):
    if c == "a":
        continue
    
    elif res >= ord("z") - ord(c) + 1:
        chl[i] = "a"
        res -= (ord("z") - ord(c) + 1)

chl[-1] = chr(ord(chl[-1]) + res%26)

print("".join(chl))

Submission status: Accepted

Solution code:
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inpl(): return list(map(int, input().split()))
def inpl_s(): return list(input().split())

S = input()
K = int(input())
ans = ''

for s in S:
	num = (123 - ord(s))%26
	if num <= K:
		ans = ans + 'a'
		K -= num
	else:
		ans = ans + s

if K > 0:
	ans = ans[:-1] + (chr((ord(ans[-1])-97+K)%26+97))

print(ans)

Submission status: Accepted

Solution code:
s = list(input())
k = int(input())
n = len(s)
do = 0
while do<=n-1:
    rest = (97-ord(s[do]))%26
    if rest<=k:
        k -= rest
        s[do] = 'a'
        do+=1
    else:
        do+=1


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

print(''.join(s))

Submission status: Accepted

Solution code:
def main():
    s = input()
    k = int(input())

    ans = [None for i in range(len(s))]
    for i in range(len(s)):
        if (ord('a')-ord(s[i])) % 26 <= k:
            ans[i] = "a"
            k -= (ord('a')-ord(s[i])) % 26
        else:
            ans[i] = s[i]

    if k > 0:
        ans[-1] = chr(ord('a')+(ord(ans[-1])-ord('a')+k) % 26)

    print("".join(ans))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

for i, s in enumerate(S):
    if ord('z') - ord(s) + 1 <= k:
        S[i] = 'a'
        k -= (ord('z') - ord(s) + 1) % 26

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

print(str().join(S))

Submission status: Accepted

Solution code:
S = input()
K = int(input())
cost = [(ord("z") - ord(s) + 1) % 26 for s in S]
A = [chr(ord("a") + i) for i in range(26)]

ans = ""
for i in range(len(S)-1):
    if cost[i] <= K:
        K -= cost[i]
        ans += "a"
    else:
        ans += S[i]

ans += A[(A.index(S[-1]) + K) % 26]
print(ans)

Submission status: Accepted

Solution code:

aord = ord("a")
s = list(raw_input())
k = int(input())
i = 0

while k > 0 and i < len(s):
	if k >= ord('z') - ord(s[i]) + 1:
		if s[i] != "a":
			k -= ord('z') - ord(s[i]) + 1
			s[i] = "a"

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

Submission status: Accepted

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

sArr = list(s)
endpoint = len(sArr) - 1
result = ""

for i in range(len(sArr)):
    if K == 0:
        break
    else:
        if (123 - ord(sArr[i])) <= K:
            K -= (123 - ord(sArr[i])) % 26
            sArr[i] = "a"

if K > 0:
    K %= 26
    sArr[endpoint] = chr(ord(sArr[endpoint]) + K)

for j in range(len(sArr)):
    result += sArr[j]

print(result)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
try: from typing import Any, Union, List, Tuple, Dict
except ImportError: pass
def debug(*args): print(*args, file=sys.stderr)
def exit(): sys.exit(0)


s = input()
K = int(input())
n = len(s)


def code(s):
    return ord(s)-97

def char(c):
    c = c % 26
    return chr(c+97)


# ans = ""
ans_list = []
for p in range(n):
    c = code(s[p])
    if c == 0:
        # ans += "a"
        ans_list.append("a")
        continue
    if 26-c > K:
        # ans += s[p]
        ans_list.append(s[p])
        continue
    # ans += "a"
    ans_list.append("a")
    K -= 26-c
ans = "".join(ans_list)
ans = ans[:-1] + char(code(ans[-1])+K)

print(ans)

Submission status: Accepted

Solution code:
s=list(input())
N=int(input())
for i in range(len(s)):
    a=(123-ord(s[i]))%26
    if a<=N:
        N-=a
        s[i]="a"
s[-1]=chr((ord(s[i])+N-97)%26+97)        
print("".join(s))

Submission status: Accepted

Solution code:
s = input()
k = int(input())
num,ans = 0,[]
while num!=len(s):
	if s[num]=="a": ans.append("a")
	elif 123-ord(s[num])<=k:
		k -= 123-ord(s[num])
		ans.append("a")
	else: ans.append(s[num])
	num += 1
ans[-1] = chr(ord(ans[-1])+k%26)
print("".join(ans))

Submission status: Accepted

Solution code:
# coding : utf-8
s=list(map(lambda c:ord(c)-ord('a'),input()))
k=int(input())
for i in range(len(s)):
    if 26-s[i]<=k and s[i]!=0:
        k-=(26-s[i])
        s[i]=0
s[-1]=(s[-1]+k)%26
for n in s:
    print(chr(ord('a')+n),end='')
print()

Submission status: Accepted

Solution code:
def main():
    s = input()
    n = int(input())
    
    t = []
    a = []
    for i in s:
        ord_i = ord(i)
        t.append(((123 - ord_i)%26, ord_i))
    
    for i in t:
        if n >= i[0]:
            a.append('a')
            n -= i[0]
        else:
            a.append(chr(i[1]))
            
    a[-1] = chr(ord(a[-1]) + n%26)
    print(''.join(a))
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s = list(input())
n = int(input())
ret = ""
for i, ss in enumerate(s):
    a = 123 - ord(ss)
    if a == 26:
        a = 0
    if i == len(s) - 1:
        c = ord(ss) + (n % 26)
        if c > 122:
            c -= 26
        ret += chr(c)
        break
    if n < a:
        ret += ss
    else:
        n -= a
        ret += 'a'

print(ret)

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-
s = list(input())
k = int(input())
for tmp in range(len(s)):
    if k == 0:
        break
    a = 26-(ord(s[tmp]) - ord('a'))
    if s[tmp] != 'a' and k >= a: 
        k -= a
        s[tmp] = 'a'
    else:
        pass
if k > 0:
    s[len(s)-1] = chr((ord(s[len(s)-1])+k%26))
print(''.join(s))