Problem:


Score : 300 points

Problem Statement

Gotou just received a dictionary. However, he doesn't recognize the language used in the dictionary. He did some analysis on the dictionary and realizes that the dictionary contains all possible diverse words in lexicographical order.

A word is called diverse if and only if it is a nonempty string of English lowercase letters and all letters in the word are distinct. For example, atcoder, zscoder and agc are diverse words while gotou and connect aren't diverse words.

Given a diverse word S, determine the next word that appears after S in the dictionary, i.e. the lexicographically smallest diverse word that is lexicographically larger than S, or determine that it doesn't exist.

Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.

Constraints

1 \leq |S| \leq 26

S is a diverse word.

Input

Input is given from Standard Input in the following format:

S

Output

Print the next word that appears after S in the dictionary, or -1 if it doesn't exist.

Sample Input 1

atcoder

Sample Output 1

atcoderb

atcoderb is the lexicographically smallest diverse word that is lexicographically larger than atcoder. Note that atcoderb is lexicographically smaller than b.

Sample Input 2

abc

Sample Output 2

abcd

Sample Input 3

zyxwvutsrqponmlkjihgfedcba

Sample Output 3

-1

This is the lexicographically largest diverse word, so the answer is -1.

Sample Input 4

abcdefghijklmnopqrstuvwzyx

Sample Output 4

abcdefghijklmnopqrstuvx



Sample input:
atcoder


Sample output:
atcoderb

Submission status: Runtime Error

Solution code:
from copy import copy, deepcopy
# from functools import reduce
# from heapq import heapify, heappop, heappush
# from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product
# import math
# import numpy as np  # Pythonのみ！
# from operator import xor
# import re
# from scipy.sparse.csgraph import connected_components  # Pythonのみ！
# ↑cf.  https://note.nkmk.me/python-scipy-connected-components/
# from scipy.sparse import csr_matrix
# import statistics # Pythonのみ
# import string
import sys
sys.setrecursionlimit(10 ** 5 + 10)


def input(): return sys.stdin.readline().strip()


def resolve():
    def main():

        def next_permutation(a):
            """Generate the lexicographically next permutation inplace.

            https://en.wikipedia.org/wiki/Permutation#Generation_in_lexicographic_order
            Return false if there is no next permutation.
            """
            # Find the largest index i such that a[i] < a[i + 1]. If no such
            # index exists, the permutation is the last permutation
            for i in reversed(range(len(a) - 1)):
                if a[i] < a[i + 1]:
                    break  # found
            else:  # no break: not found
                return False  # no next permutation

            # Find the largest index j greater than i such that a[i] < a[j]
            j = next(j for j in reversed(range(i + 1, len(a))) if a[i] < a[j])

            # Swap the value of a[i] with that of a[j]
            a[i], a[j] = a[j], a[i]

            # Reverse sequence from a[i + 1] up to and including the final element a[n]
            a[i + 1:] = reversed(a[i + 1:])
            return True

        alpha = list(chr(ord('a')+i) for i in range(26))

        S = input()
        if len(S) < 26:
            for i in range(len(S)):
                alpha.remove(S[i])
            return S+alpha[0]
        else:
            S = list(S)
            P = S.deepcopy()
            next_permutation(P)
            for i in range(26):
                if P[i] != S[i]:
                    return ''.join(P[:i+1])
            return -1
    print(main())

resolve()

Submission status: Runtime Error

Solution code:
n=int(input())
# Disadvantages of not reading qn properly in the beginning, all of them must be distinct
if n==3:
    print(2, 5 ,63)
elif n==4:
    print(2 ,5 ,20, 63)
elif n==5:
    print(2,5,20,30,63)
else:
    ans=[]
    k=0
    while len(ans)<n:
        ans.append(6*k+2)
        ans.append(6*k+3)
        ans.append(6*k+4)
        ans.append(6*k+6)
        k+=1
    # print(ans)
    ans=sorted(ans)[:n]
    # print(ans)
    if sum(ans)%6==1:
        ans.remove(8)
        ans.append((k+1)*6)
    elif sum(ans)%6==3:
        ans.remove(9)
        ans.append((k+1)*6)
    elif sum(ans)%6==5:
        ans.remove(9)
        ans.append((k+1)*6+4)
    print(" ".join(list(map(str,ans))))
# ans=[3,2]*(n//2)
# if n%2==1:
#     ans.append(5)

Submission status: Runtime Error

Solution code:
from string import*
from itertools import*
b = ascii_lowercase
s = input()
if len(s) < 26:
    x = z = s;y = b;d = 0
else:
    d = -[p*len(list(k)) for p, k in groupby([i < j for i, j in zip(s[-1::-1], s[-2::-1])])][0] - 2
    x, y, z = s[:d], s[d+1:], b[:ord(s[d]) - 97]
print(-(d < -26) or x + sorted(set(y) - set(z))[0])

Submission status: Runtime Error

Solution code:
from string import*
from itertools import*
b = ascii_lowercase
s = input()
if len(s) < 26:
    x = z = s, y = b, d = 0
else:
    d = -[p*len(list(k)) for p, k in groupby([i < j for i, j in zip(s[-1::-1], s[-2::-1])])][0] - 2
    x, y, z = s[:d], s[d+1:], b[:ord(s[d]) - 97]
print(-(d < -26) or x + sorted(set(y) - set(z))[0])

Submission status: Runtime Error

Solution code:
from string import*
from itertools import*
b = ascii_lowercase
s = input()
if len(s) < 26:
    x = z = s, y = b, d = 0
else:
    d = [p*len(list(k)) for p, k in groupby([i < j for i, j in zip(s[-1::-1], s[-2::-1])])][0] - 2
    x, y, z = s[:d], s[d+1:], b[:ord(s[d]) - 97]
print(-(d < -26) or x + sorted(set(y) - set(z))[0])

Submission status: Runtime Error

Solution code:
from string import*
from itertools import*
b = ascii_lowercase
s = input()
if len(s) < 26:
    x = z = s, y = b, d = 0
else:
    d = [p*len(list(k)) for p, k in groupby([i < j for i, j in zip(s[-1::-1], t[-2::-1])])][0] - 2
    x, y, z = s[:d], s[d+1:], b[:ord(s[d]) - 97]
print(-(d < -26) or x + sorted(set(y) - set(z))[0])

Submission status: Runtime Error

Solution code:
from string import*
from itertools import*
b = ascii_lowercase
s = input()
if len(s) < 26:
    print(s + sorted(set(b) - set(s))[0])
else:
    d = [p,len(list(k)) for p, k in groupby([i < j for i, j in zip(s[-1::-1], t[-2::-1])])][0] - 2
    print(-(d < -26) or s[:d] + sorted(set(s[d+1:]) - set(b[:ord(s[d]) - 97]))[0])

Submission status: Runtime Error

Solution code:
A=input()
l=set(chr(ord('a') + i) for i in range(26))
l2=list(l-set(A))
l2.sort()
if len(A)!=26:
   print(A+l2[0])
else:
   l=[chr(ord('a') + i) for i in range(26)]
   A=A[::-1]
   for i in range(1,26):
      if A[i-1] < A[i]:
         pass
      else:
         if A[i-1] == "z":
            S=l[-1*i:]
            S.sort()
            A=A[::-1]
            print(A[:-1*i-1]+S[0])
            exit()
         else:
            A[i-1],A[i]=A[i],A[i-1]
            print(A[::-1])
            exit()

Submission status: Runtime Error

Solution code:
from string import*
from itertools import*
b = ascii_lowercase
s = input()
l = len(s)
if l < 26:
    print(s + sorted(set(b) - set(s))[0])
else:
    d = [p,*len(list(k)) for p, k in groupby([i < j for i, j in zip(s[-1::-1], t[-2::-1])])][0] - 2
    print(-(d < -26) or s[:d] + sorted(set(s[d+1:]) - set(b[:ord(s[d]) - 97]))[0])

Submission status: Runtime Error

Solution code:
from string import*;from itertools import*;b=ascii_lowercase;r=s=input();d=l=len(s);t=s[::-1];o=1
if l>25:d=-[p*len(list(k))for p,k in groupby([i<j for i,j in zip(s[::-1],s[-2::-1])])][0]-2;s,b,o=s[:d],s[d+1:],0
print(-(d<-26)or s+sorted(set(b)-set(s*o or b[:ord(s[d])-97]))[0])

Submission status: Runtime Error

Solution code:
from string import*;from itertools import*
b=ascii_lowercase;r=s=input();d=l=len(s);t=s[::-1]
if l>25:d=-[p*len(list(k))for p,k in groupby([i<j for i,j in zip(s[::-1],s[-2::-1])])][0]-2;s,b,o=s[:d],s[d+1:],1
print(-(d<-26)or s+sorted(set(b)-set([s,b[:ord(s[d])-97]][o]))[0])

Submission status: Runtime Error

Solution code:
from string import*;from itertools import*;b=ascii_lowercase;r=s=input();d=l=len(s);t=s[::-1]
if l>25:d=-[p*len(list(k))for p,k in groupby([i<j for i,j in zip(s[::-1],s[-2::-1])])][0]-2;s,b,o=s[:d],s[d+1:],1
print(-(d<-26)or s+sorted(set(b)-set([s,b[:ord(s[d])-97]][o]))[0])

Submission status: Runtime Error

Solution code:
s = input()

if s == 'zyxwvutsrqponmlkjihgfedcba':
    print(-1)
    exit()
# ans = 0
chrs = [chr(i) for i in range(97, 97+26)]
if len(s) == 26:
    for i in range(26):
        if chrs[i] != s[i]:
            ans = s[:i-1] + chrs[i]
            break
else:
    for c in chrs:
        if c not in s:
            ans = s + c
            break
print(ans)

Submission status: Runtime Error

Solution code:
s = input()

if s == 'zyxwvutsrqponmlkjihgfedcba':
    print(-1)
    exit()

chrs = [chr(i) for i in range(97, 97+26)]
if len(s) == 26:
    for i in range(26):
        if chrs[i] != s[i]:
            ans = s[:i] + chrs[i]
            break
else:
    for c in chrs:
        if c not in s:
            ans = s + c
            break
print(ans)

Submission status: Runtime Error

Solution code:
import sys
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    s = readline().decode('utf-8')
    if len(s) < 26:
        al = [chr(ord('a') + i) for i in range(26)]
        for i in al:
            if not (i in s):
                print(s + i)
                exit()
    if s == "zyxwvutsrqponmlkjihgfedcba":
        print(-1)
        exit()
    for i in range(25):
        if s[i] < s[i + 1]:
            k = i
    a = "z"
    for i in range(k + 1, 26):
        if s[k] < s[i]:
            a = min(a, s[i])
    print(s[:k] + a)
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import collections
s = list(input())
sset = set(s)
if len(sset) != 26:
    for i in range(97,123):
        if chr(i) in sset:
            continue
        else:
            tmp = chr(i)
            s.append(tmp)
            break

    print(''.join(s))
    exit()
else:
    tmp = []
    flg = False
    s = s[::-1]
    for i in range(1,26):
        if ord(s[i-1]) < ord(s[i]) and not flg:
            flg = True
            tmp.append(ord(s[i-1]))
            tmp.append((ord(s[i])))
            continue
        if flg and ord(s[i-1]) > ord(s[i]):
            top = i
            break
        if ord(s[i-1]) < ord(s[i]) and flg:
            tmp.append(ord(s[i]))

    if len(tmp) == 26:
        print(-1)
        exit()
    ans = s[top:]
    ans = ans[::-1]
    ans.append(chr(min(tmp)))
    print(''.join(ans))

Submission status: Runtime Error

Solution code:
from sys import stdin
import sys
# import itertools
# import math

S = input()
if S == 'zyxwvutsrqponmlkjihgfedcba':
    print(-1)
    sys.exit()
a = [chr(i) for i in range(97, 97+26)]
b = [0 for i in range(26)]
d = dict(zip(a, b))
d1 = d
for i, v in enumerate(S):
    d[v] = d.get(v, 0)+1
for i, v in enumerate(a):
    if d[v] != 1:
        print(S+v)
        sys.exit()
temp = []
temp1 = []
x = 10/0
for i in range(26):
    temp1 = []
    temp.append(S[-1-i])
    # print(S[-1-i])
    for i, v in enumerate(temp):
        if S[-1-i-1] < v:
            temp1.append(v)
    if len(temp1) > 0:
        print(S[:-1-i-1]+min(temp1))
        sys.exit()

Submission status: Runtime Error

Solution code:
from sys import stdin
import sys
# import itertools
# import math

S = input()
if S == 'zyxwvutsrqponmlkjihgfedcba':
    print(-1)
    sys.exit()
a = [chr(i) for i in range(97, 97+26)]
b = [0 for i in range(26)]
d = dict(zip(a, b))
d1 = d
for i, v in enumerate(S):
    d[v] = d.get(v, 0)+1
for i, v in enumerate(a):
    if d[v] != 1:
        print(S+v)
        sys.exit()
temp = []
for i in range(26):
    temp.append(S[-1-i])
    if S[-1-i-1] < min(temp):
        print(S[:-1-i-1]+min(temp))
        sys.exit()

Submission status: Runtime Error

Solution code:
S=input()
if len(S)<26:
    raise TypeError
    print(S+sorted(list({chr(i+97) for i in range(26)}-set(S)))[0])
else:
    tmp = [S[25]]
    for i in range(25):
        tmp.append(S[24-i])
        if S[24-i] < S[25-i]:
            print(S[:26-len(tmp)]+sorted(tmp)[1])
            exit()
    print(-1)

Submission status: Runtime Error

Solution code:
S=input()
if len(S)<26:
    raise TypeError:
    print(S+sorted(list({chr(i+97) for i in range(26)}-set(S)))[0])
else:
    tmp = [S[25]]
    for i in range(25):
        tmp.append(S[24-i])
        if S[24-i] < S[25-i]:
            print(S[:26-len(tmp)]+sorted(tmp)[1])
            exit()
    print(-1)

Submission status: Runtime Error

Solution code:
s=input()
if len(s)<26:
    al=[chr(ord('a') + i) for i in range(26)]
    for i in al:
        if not(i in s):
            print(s+i)
            exit()
if s=="zyxwvutsrqponmlkjihgfedcba":
    print(-1)
    exit()
for i in range(25):
    if s[i]>s[i+1]:
        k=i-1

a="z"
for i in range(k+2,26):
    if s[k+1]<s[i]:
        a=min(a,s[i])
print(s[:k]+a)

Submission status: Runtime Error

Solution code:
s=input()
if len(s)<26:
    al=[chr(ord('a') + i) for i in range(26)]
    for i in al:
        if not(i in s):
            print(s+i)
            exit()
if s=="zyxwvutsrqponmlkjihgfedcba":
    print(-1)
    exit()
for i in range(25):
    if s[i]>s[i+1]:
        k=i-1

a="z"
for i in range(k+2,26):
    if s[k]<s[i]:
        a=min(a,s[i])
print(s[:k]+a)

Submission status: Runtime Error

Solution code:
s=input()
if len(s)<26:
    al=[chr(ord('a') + i) for i in range(26)]
    for i in al:
        if not(i in s):
            print(s+i)
            exit()
if s=="zyxwvutsrqponmlkjihgfedcba":
    print(-1)
    exit()
for i in range(25):
    if s[i]>s[i+1]:
        k=i-1
        break
a="z"
for i in range(k+2,26):
    if s[k]<s[i]:
        a=min(a,s[i])
print(s[:k]+a)

Submission status: Runtime Error

Solution code:
s=input()
if len(s)<26:
    al=[chr(ord('a') + i) for i in range(26)]
    for i in al:
        if not(i in s):
            print(s+i)
            exit()
if s=="zyxwvutsrqponmlkjihgfedcba":
    print(-1)
    exit()
for i in range(26):
    if s[i]>s[i+1]:
        k=i-1
        break
a="z"
for i in range(k,26):
    if s[k]<s[i]:
        a=min(a,s[i])
print(s[:k]+a)

Submission status: Runtime Error

Solution code:
import string
ABC=list(string.ascii_lowercase)
s=input()
S=s[::-1]

if s=='zyxwvutsrqponmlkjihgfedcba':
    print(-1)
else:
    if len(s)==26:
        for i in range(len(s)):
            n=ABC.index(S[i])
            if not set(ABC[n+1:])<=set(S[i+1:]):
                j=i
                break
        L=list(set(ABC[n+1])-set(S[j+1:]))
        L.sort()
        sgyku=L[0:1]+S[j+1:]
        print("".join(sgyku[::-1]))
    else:
        L=list(set(ABC)-set(s))
        L.sort()
        print(s+L[0])

Submission status: Runtime Error

Solution code:
s = input()

sx = sorted(s)


if(sx[0] == "a"):
    for i in range(len(sx) - 1):
        if (ord(sx[i + 1]) - ord(sx[i]) > 1):
            st = ord(sx[i])+1
            break
else:
    st = ord("a")

print(s+chr(st))

Submission status: Runtime Error

Solution code:
S = input()
N = [chr(i) for i in range(97,123) if chr(i) not in S]

if len(S) == 26:
  if S == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
  else:
    LS = list(S)
    sLS = sorted(LS)
    for i in range(0,27):
      if LS[i] != sLS[i]:
        S = S[:i - 1] + sLS[i]
        break
else:
  S = S + N[0]
  print(S)

Submission status: Runtime Error

Solution code:
S = input()
N = [chr(i) for i in range(97,123) if chr(i) not in S]

if len(S) == 26:
  if S == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
  else:
    LS = list(S)
    sLS = sorted(LS)
    print(LS)
    print(sLS)
    for i in range(0,27):
      if LS[i] != sLS[i]:
        S = S[:i - 1] + sLS[i]
        break
else:
  S = S + N[0]
  
print(S)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys

def main():
    S = input()
    T = 'abcdefghijklmnopqrstuvwxyz'
    if len(S) < 26:
        U = set(T) - set(S)
        print(S + sorted(U)[0])
    else:
        if S == T[::-1]:
            print(-1)
        else:
            count = 1
            for i in range(25):
                if S[25-i] > S[24-i]:
                    break
                else:
                    count += 1
            t = T.index(S[25-count])
            print(S[:25-count] + T[t+1])

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
s = input()
str_list = [chr(i) for i in range(97, 97+26)]
if s == "zyxwvutsrqponmlkjihgfedcba":
  print(-1)
elif len(s) != 26:
  for _ in s:
    str_list.remove(_)
  str_list.sort()
  print(s+str_list[0])
  
else:
  stock = []
  for i in range(25,2,-1):
    if ord(s[i]) < ord(s[i-1]):
      stock.append(s[i])
      
    if i == 1 :
      print(chr(ord(s[0]) + 1))
      break
    else:
      stock.sort()
      print(s[:i-3] +  stock[0] )
      break

Submission status: Runtime Error

Solution code:
s=str(input())
a=list("zyxwvutsrqponmlkjihgfedcba")
a=a[::-1]
#print(a)


if "zyxwvutsrqponmlkjihgfedcba"==s:
    print(-1)


if len(s)==26:
    s=s[::-1]#後ろから見ていく
    for i in range(len(s)):
        for j in range(i+1):
           # print(i,j,s[i+1],s[j+1])
            if s[i+1]<s[j+1]:#xyzの降順の流れが切れたところを変える
                s=s[::-1]
                print(s[:-(i+1)]+s[-(j+1)])
                exit()
else:
    for i in a:
        if i not in s:
            s+=i
            break
    print(s)

Submission status: Runtime Error

Solution code:
s = input()
alphabet = "abcdefghijklmnopqrstuvwxyz"
ans = ""

if len(s) == 26:
    if s == "zyxwvutsrqponmlkjihgfedcba":
        print(-1)
        exit()
    for i in range(26):
        if s[i]  == alphabet[i]:
            ans += s[i]
        else:
            ans = ans.replace(ans[i-1], alphabet[i])
            print(ans)
            exit()
    pront(ans)
for i in range(len(s)):
    used = s[i]
    alphabet = alphabet.strip(used)

ans = s + alphabet[0]
print(ans)

Submission status: Runtime Error

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
from collections import Counter
def main():
    s=input()
    zyx="zyxwvutsrqponmlkjihgfedcba"
    if s==zyx:
        print(-1)
        sys.exit()
    if len(s)==26:
        for i,sx in enumerate(zyx):
            if s[i]!=sx:
                sxi=s.index(sx)
                s=s[:sxi]
                sy=s[-1]
                for j in range(26):
                    sz=chr(ord(sy)+1)
                    if not(sz in s):     
                        print(s[:-1]+sz)
                        sys.exit()
    else:
        for i in range(26):
            if chr(ord('a')+i) in s:
                continue
            print(s+chr(ord('a')+i))
            break
    


if __name__=='__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
from collections import Counter
def main():
    s=input()
    zyx="zyxwvutsrqponmlkjihgfedcba"
    if s==zyx:
        print(-1)
        sys.exit()
    ss=set(s)
    if len(ss)==26:
        for i,sx in enumerate(zyx):
            print(i,sx,s)
            if s[i]!=sx:
                sxi=s.index(sx)
                s=s[:sxi]
                sy=s[-1]
                for j in range(26):
                    sz=chr(ord(sy)+1)
                    if not(sz in s):     
                        print(s[:-1]+sz)
                        sys.exit()
    else:
        for i in range(26):
            if chr(ord('a')+i) in s:
                continue
            print(s+chr(ord('a')+i))
            break
    


if __name__=='__main__':
    main()

Submission status: Runtime Error

Solution code:
S = input()

if len(S) < 26:
    for c in [chr(i) for i in range(ord('a'), ord('a') + 26)]:
        # print(c)
        if c not in S:
            ans = S + c
            break
elif S == 'zyxwvutsrqponmlkjihgfedcba':
    ans = -1
else:
    for i in range(25, -1, -1):
        # print(S[i], S[-1])
        if ord(S[i]) < ord(S[-1]):
            ans = S[:i] + S[-1]
            break
print(ans)

Submission status: Runtime Error

Solution code:
s = input()

a = list('abcdefghijklmnopqrstuvwxyz')

if s == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
elif len(s) < 26:
    for x in a:
        if s.count(x) == 0:
            print(s + x)
            break
else:
    print(c)
    #for i in range(len(s)):
    #    if a[i] != s[i]:
    #        ans = s[:i-1] + chr(ord(s[i-1])+1)
    #        break
    #print(ans)

Submission status: Runtime Error

Solution code:
s = input()

a = list('abcdefghijklmnopqrstuvwxyz')

if s == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
elif len(s) < 26:
    for x in a:
        if s.count(x) == 0:
            print(s + x)
            break
else:
    for i in range(len(s)):
        if a[i] != s[i]:
            ans = s[:i-1] + chr(ord(s[i-1])+1)
            break
    print(ans)

Submission status: Runtime Error

Solution code:
s = input()

a = list('abcdefghijklmnopqrstuvwxyz')

if s == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
elif len(s) < 26:
    for x in a:
        if s.count(x) == 0:
            print(s + x)
            break
else:
    for i in range(26):
        if a[i] != s[i]:
            ans = s[:i-1] + chr(ord(s[i-1])+1)
            break
    print(ans)

Submission status: Runtime Error

Solution code:
import sys


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(10 ** 9)


def main():
    s = input()
    S = list(s)
    A = list(reversed(list("zyxwvutsrqponmlkjihgfedcba")))
    for i in range(26):
        if A[i] not in S:
            S.append(A[i])
            print(*S, sep="")
            return
    else:
        B = []
        for s in S:
            B.append(ord(s))
        for i in range(25):
            if B[25 - i] > B[25 - i - 1]:
                continue
            else:
                k = 25 - i - 1
        if k == 0:
            print(-1)
            return
        a = S[k - 1]
        S = S[: k - 1]
        for i in range(26):
            if A[i] not in S:
                if ord(A[i]) > ord(a):
                    S.append(A[i])
                    print(*S, sep="")
                    return


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import sys


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(10 ** 9)


def main():
    s = input()
    S = list(s)
    A = list(reversed(list("zyxwvutsrqponmlkjihgfedcba")))
    for i in range(26):
        if A[i] not in S:
            S.append(A[i])
            print(*S, sep="")
            return
    else:
        A = []
        for s in S:
            A.append(ord(s))
        for i in range(25):
            if A[25 - i] > A[25 - i - 1]:
                continue
            else:
                k = 25 - i - 1
        if k == 0:
            print(-1)
            return
        S = S[: k - 1]
        S.append(chr(min(A[k:])))
        print(*S, sep="")


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
# import bisect
# from collections import Counter, deque # https://note.nkmk.me/python-scipy-connected-components/
# from copy import copy, deepcopy
# from fractions import gcd
# from functools import reduce
# from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product
# import math
# import numpy as np  # Pythonのみ！
# from operator import xor
# import re
# from scipy.sparse.csgraph import connected_components  # Pythonのみ！
# ↑cf.  https://note.nkmk.me/python-scipy-connected-components/
# from scipy.sparse import csr_matrix
# import string
import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()

def resolve():

    # アルファベット小文字→(97, 123)を入力に入れる
    # アルファベット大文字→(65, 91)
    # 半角数字→(48, 58)
    # ひらがな→(12353, 12436)
    # カタカナ→(12449, 12532+1)
    # 全角数字→(65296, 65306)

    def moji_list(*args):
        moji = []
        for i in range(len(args)):
            moji.extend([chr(j) for j in range(args[i][0], args[i][1])])
        return moji
    moji=moji_list((97,123))

    S=str(input())
    ls=len(S)
    for i in range(ls-1):
        temp=S[i]
        moji.remove(S[i])
        if S[i+1]!=chr(ord(temp)+1):
            ans=i+1
            anss=chr(ord(temp)+1)

    if ls < 26:
        print(S +moji[0])
    else:
        if S[-1]=='a':
            print(-1)
        else:
            S=S[:i]+anss
            print(S)



resolve()

Submission status: Runtime Error

Solution code:
def rem(x,r):
  if (x,r) == (1,0):
    return [[1]]
  if x <= 2*r:
    return -1
  ret = []
  for i in range(1,x+1):
    if x%i == r:
      ret.append([i])
    else:
      s = rem(x%i,r)
      if s == -1:
        continue
      else:
        for j in s:
          if j == -1:
            continue
          elif j[-1] < i:
            ret.append([i]+j)
          else:
            ret.append([i]+j)
  return ret
n = int(input())
x = list(map(int,input().split()))
y = list(map(int,input().split()))
for xx,yy in zip(x,y):
  if xx != yy and rem(xx,yy) == -1:
    print(-1)
    exit()
ls = [rem(xx,yy) if xx != yy else 0 for xx,yy in zip(x,y)]
flg = [0]*n
def findmx(lsi,ansst,mx):
  ret = 100
  if lsi == 0:
    return 0
  for lsls in lsi:
    for j in lsls:
      if j > mx:
        return min(j,ret)
      elif j in ansst:
        continue
      else:
        ret = min(j,ret)
        break
    else:
      return 0
  else:
    return ret
    
mxls = [ls[i][0][0] if type(ls[i]) == list else 0 for i in range(n)]
mx = max(mxls)
ansst = set((mx,))
mxcand = mx
while mxcand:
  for i in range(n):
    t = findmx(ls[i],ansst,mx)
    mxls[i] = t
    if t == 0:
      flg[i] = 1
  mxcand = max(mxls)
  ansst.add(mxcand)
  if flg.count(1) == n:
    break
ans = 0
for b in ansst:
  if b:
    ans += 2**b
print(ans)

Submission status: Runtime Error

Solution code:
def rem(x,r):
  if (x,r) == (1,0):
    return [[1]]
  if x <= 2*r:
    return -1
  ret = []
  for i in range(1,x+1):
    if x%i == r:
      ret.append([i])
    else:
      s = rem(x%i,r)
      if s == -1:
        continue
      else:
        for j in s:
          if j == -1:
            continue
          elif j[-1] < i:
            ret.append([i]+j)
          else:
            ret.append([i]+j)
  return ret
n = int(input())
x = list(map(int,input().split()))
y = list(map(int,input().split()))
for xx,yy in zip(x,y):
  if xx != yy and rem(xx,yy) == -1:
    print(-1)
    exit()
ls = [rem(xx,yy) if xx != yy else 0 for xx,yy in zip(x,y)]
flg = [0]*n
def findmx(lsi,ansst,mx):
  ret = 100
  if lsi == 0:
    return 0
  for lsls in lsi:
    for j in lsls:
      if j > mx:
        return min(j,ret)
      elif j in ansst:
        continue
      else:
        ret = min(j,ret)
        break
    else:
      return 0
  else:
    return 0
mxls = [ls[i][0][0] if type(ls[i]) == list else 0 for i in range(n)]
mx = max(mxls)
ansst = set((mx,))
mxcand = mx
while mxcand:
  for i in range(n):
    t = findmx(ls[i],ansst,mx)
    mxls[i] = t
    if t == 0:
      flg[i] = 1
  mxcand = max(mxls)
  ansst.add(mxcand)
  if flg.count(1) == n:
    break
ans = 0
for b in ansst:
  if b:
    ans += 2**b
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
if n == 3:
    print(2, 5, 63)
elif n == 4:
    print(2, 3, 4, 9)
else:
    l = [2, 3, 4, 9]
    a = 8
    b = 10
    while b <= 30000 and len(l) + 1 < n:
        l.append(a)
        l.append(b)
        a += 6
        b += 6
    c = 15
    d = 21
    while d <= 30000 and len(l) + 1 < n:
        l.append(c)
        l.append(d)
        c += 12
        d += 12
    t = 6
    while t <= 30000 and len(l) < n:
        l.append(t)
        t += 6
    print(' '.join(map(str, l)))

Submission status: Runtime Error

Solution code:
S = input()
S_set = set(list(S))
C_set = set([chr(ord("a") + i) for i in range(26)])

if len(S_set) == len(C_set):
    S = list(S)
    p = None
    for i in range(len(S) - 2, -1, -1):
        if S[i] < S[i + 1]:
            p = i
            break
    else:
        print(-1)
    ans = S[:p].copy()
    rest = C_set - set(S[: p + 1])
    rest = sorted(rest)
    for r in rest:
        if S[p] < r:
            ans.append(r)
            break
    print(*ans, sep="")
else:
    diff = C_set - S_set
    diff = sorted(diff)
    print(S + diff[0])

Submission status: Runtime Error

Solution code:
S = input()
S_set = set(list(S))
C_set = set([chr(ord("a") + i) for i in range(26)])

if len(S_set) == len(C_set):
    S = list(S)
    p = None
    for i in range(len(S) - 2, -1, -1):
        if S[i] < S[i + 1]:
            p = i
            break

    if p == 0:
        print(-1)
    else:
        ans = S[:p].copy()
        rest = C_set - set(S[: p + 1])
        rest = sorted(rest)
        ans.append(rest[0])
        print(*ans, sep="")
else:
    diff = C_set - S_set
    diff = sorted(diff)
    print(S + diff[0])

Submission status: Runtime Error

Solution code:
#3
#入力
S=input()

#処理
t=0
data='abcdefghijklmnopqrstuvwxyz'
dataz=data

l=len(S)
A=[]
for i in range(l):
    A.append(S[i])
A.sort()

#print(A)
if S=="zyxwvutsrqponmlkjihgfedcba":
    print(-1)
elif len(S)==len(set(S)):
    if len(S)!=26:
        xx='a'
        for i in A:
            if i==data[0]:
                data=data[1:]
                xx=data[0]
            else:
                break
        print(S+xx)
    else:
        ll=0
        for i in range(len(S)):
            if S[i]==data[0]:
                ll+=1
                data=data[1:]
            else:
                break
        print(S[:ll-1]+data[0])
            

else:
    print(-1)

Submission status: Runtime Error

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

if ''.join(S) == 'zyxwvutsrqponmlkjihgfedcba':
    print(-1)
    exit(0)

if len(S) != 26:
    for a in alp:
        if not(a in S):
            print(''.join(S) + a)
            exit(0)

for i in range(len(S)):
    if alp.index(S[i]) > alp.index(S[i+1]):
        #S[i] = S[i+1]
        print(''.join(S[:i-1])+alp[i])
        exit(0)

Submission status: Runtime Error

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7

s = STR()
if len(s) < 26:
    for i in range(26):
        tmp = chr(i + ord('a'))
        if not tmp in s:
            print(s + tmp)
            exit()
else:
    for i in reversed(26):
        for j in range(ord(s[i]) + 1, ord('z') + 1):
            if not chr(j) in s[:i]:
                print(s[:i] + chr(j))
                exit()
    print(-1)
    exit()