Problem:


Score : 100 points

Problem Statement

You are given two strings s and t consisting of lowercase English letters and an integer L.

We will consider generating a string of length L by concatenating one or more copies of s and t. Here, it is allowed to use the same string more than once.

For example, when s = at, t = code and L = 6, the strings atatat, atcode and codeat can be generated.

Among the strings that can be generated in this way, find the lexicographically smallest one. In the cases given as input, it is always possible to generate a string of length L.

Constraints

1 ≤ L ≤ 2 × 10^5

1 ≤ |s|, |t| ≤ L

s and t consist of lowercase English letters.

It is possible to generate a string of length L in the way described in Problem Statement.

Input

Input is given from Standard Input in the following format:

N
x_1 s_1
x_2 s_2
:
x_N s_N

Output

Print the lexicographically smallest string among the ones that can be generated in the way described in Problem Statement.

Sample Input 1

6
at
code

Sample Output 1

atatat

This input corresponds to the example shown in Problem Statement.

Sample Input 2

8
coding
festival

Sample Output 2

festival

It is possible that either s or t cannot be used at all in generating a string of length L.

Sample Input 3

8
same
same

Sample Output 3

samesame

It is also possible that s = t.

Sample Input 4

10
coin
age

Sample Output 4

ageagecoin



Sample input:
6
at
code


Sample output:
atatat

Submission status: Wrong Answer

Solution code:
n=int(input())
s=input()
t=input()
if s>t:
    s,t=t,s
for i in range(n//len(s),-1,-1):
    if (n-i*len(s))%len(t)==0:
        ans=s*i
        ans+=t*((n-i*len(s))//len(t))
        print(ans)
        exit()

Submission status: Wrong Answer

Solution code:
n=int(input())
a=sorted([input()for i in range(2)])
s,t=len(a[0]),len(a[1])
for i in range(n):
    if (n-i*t)%s==0:
        print(a[0]*((n-i*t)//s)+a[1]*i)
        break

Submission status: Wrong Answer

Solution code:
l = int(input())
s = input()
t = input()

if s == t:
    print(s*(l//len(s)))
else:
    s, t = sorted([s,t])
    ls, lt = len(s), len(t)
    l += lt
    for i in range(2*10**5+1):
        l -= lt
        if l % ls == 0:
            j = l // ls
            print(j*s + i*t)
            break

Submission status: Wrong Answer

Solution code:
l = int(input())
s = input()
t = input()

if s == t:
    print(s*(l//len(s)))
else:
    pass

Submission status: Wrong Answer

Solution code:
L = int(input())
Ss = input()
Ts = input()

if Ss > Ts:
    Ss, Ts = Ts, Ss

lenS, lenT = len(Ss), len(Ts)

for numS in reversed(range(L//lenS+1)):
    numT = (L-lenS*numS) / lenT
    if int(numT) == numT:
        ans = Ss*numS + Ts*int(numT)
        print(ans)
        break

Submission status: Wrong Answer

Solution code:
import sys

L = int(input())
s = str(input())
t = str(input())
tmps = s
tmpt = t
s = min(s,t)
sl = len(s)
t = max(tmps,tmpt)
tl = len(t)

p = L//sl
q = L%sl

for i in range(0,p+1):
    if (q + i*sl)%tl == 0:
        S = s*(p-i)
        print(S + t*((L-len(S))//tl))
        sys.exit()

Submission status: Wrong Answer

Solution code:
import sys

L = int(input())
s = str(input())
t = str(input())
tmps = s
tmpt = t
s = min(s,t)
sl = len(s)
t = max(tmps,tmpt)
tl = len(t)

p = L//sl
q = L%sl


for i in range(0,p+1):
    if (q + i*sl)%tl == 0:
        print(s*(p-i) + t*((L-(sl*(p-i)))//tl))
        sys.exit()

Submission status: Wrong Answer

Solution code:
l = int(input())
s = input()
t = input()

if (s[0] > t[0]):
    tmp = s
    s = t
    t = tmp

if ((s[0] == t[0]) and (s[-1] > t[-1])):
    tmp = s
    s = t
    t = tmp

if ((s[0] == t[0]) and (s[-1] == t[-1]) and s > t):    
    tmp = s
    s = t
    t = tmp

s_cnt = 0
t_cnt = 0

ltmp = l
while (not (ltmp % len(s) == 0)):
    # print(ltmp)
    ltmp -= len(t)
    t_cnt += 1

s_cnt = ltmp // len(s)

# print(s_cnt)
# print(t_cnt)

ans = ''
for i in range(s_cnt):
    ans += s
for i in range(t_cnt):
    ans += t
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

L = int(input())
s = str(input())
t = str(input())
tmps = s
tmpt = t
s = min(s,t)
sl = len(s)
t = max(tmps,tmpt)
tl = len(t)

p = L//sl
q = L%sl


for i in range(1,p):
    if (q + i*sl)%tl == 0:
        print(s*(p-i) + t*((L-(sl*(p-i)))//tl))
        sys.exit()

Submission status: Wrong Answer

Solution code:
import sys

L = int(input())
s = str(input())
t = str(input())
tmps = s
tmpt = t
s = min(s,t)
sl = len(s)
t = max(tmps,tmpt)
tl = len(t)

p = L//sl
q = L%sl

if q != 0 and q%tl == 0:
    print(s*p + t*(q//tl))
else:
    for i in range(1,p):
        if (q + i*sl)%tl == 0:
            print(s*(p-i) + t*((L-(sl*(p-i)))//tl))
            sys.exit()

Submission status: Wrong Answer

Solution code:
l = int(input())
s = input()
t = input()

if (s[0] > t[0]) or ((s[0] == t[0]) and (s[-1] > t[-1])):
    tmp = s
    s = t
    t = tmp

s_cnt = 0
t_cnt = 0

ltmp = l
while (not (ltmp % len(s) == 0)):
    # print(ltmp)
    ltmp -= len(t)
    t_cnt += 1

s_cnt = ltmp // len(s)

# print(s_cnt)
# print(t_cnt)

ans = ''
for i in range(s_cnt):
    ans += s
for i in range(t_cnt):
    ans += t
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

L = int(input())
s = str(input())
t = str(input())
tmps = s
tmpt = t
s = min(s,t)
sl = len(s)
t = max(tmps,tmpt)
tl = len(t)

p = L//sl
q = L%sl

if q == 0:
    print(s*p)
elif q%tl == 0:
    print(s*p + t*(q//tl))
else:
    for i in range(1,p+1):
        if (q + i*sl)%tl == 0:
            print(s*(p-i) + t*((L-(sl*(p-i)))//tl))
            sys.exit()

Submission status: Wrong Answer

Solution code:
import sys

L = int(input())
s = str(input())
t = str(input())
tmps = s
tmpt = t
s = min(s,t)
sl = len(s)
t = max(tmps,tmpt)
tl = len(t)

p = L//sl
q = L%sl

if q == 0:
    print(s*p)
elif q == tl:
    print(s*p + t*1)
else:
    for i in range(1,p+1):
        if (q + i*sl)%tl == 0:
            print(s*(p-i) + t*((L-sl*(p-i))//tl))
            sys.exit()

Submission status: Wrong Answer

Solution code:
l = int(input())
a = input()
b = input()

if len(a) > len(b):
    k = b
    b = a
    a = k

mina = None
maxa = None


def less(a, b):
    if a < b:
        if not b.startswith(a):
            return True
        else:
            c = b
            while c.startswith(a):
                c = c[len(a):]
            if a < c:
                return True
            else:
                return False


for i in range(l+ 1) :
    if i * len(a) > l:
        break
    if (l - i * len(a)) % len(b) == 0:
        if mina is None:
            mina = i
            maxb = (l - i * len(a)) // len(b)
        maxa = i
        minb = (l - i * len(a)) // len(b)


if less(a , b):
    print(a * maxa + b * minb)
else:
    print(b * maxb + a * mina)

Submission status: Wrong Answer

Solution code:
import sys

L = int(input())
s = str(input())
t = str(input())
tmps = s
tmpt = t
s = min(s,t)
sl = len(s)
t = max(tmps,tmpt)
tl = len(t)

p = L//sl
q = L%sl

if q == 0:
    print(s*p)
elif q == tl:
    print(s*p + t*1)
else:
    for i in range(1,p+1):
        if (q + i*sl) == tl:
            print(s*(p-i) + t*((L-sl*(p-i))//tl))
            sys.exit()

Submission status: Wrong Answer

Solution code:
import sys

L = int(input())
s = str(input())
t = str(input())
tmps = s
tmpt = t
s = min(s,t)
sl = len(s)
t = max(tmps,tmpt)
tl = len(t)

p = L//sl
q = L%sl

if q == 0:
    print(s*p)
elif q == tl:
    print(s*p + t*1)
else:
    for i in range(1,p+1):
        if (q + i*sl) == tl:
            print(s*(p-i) + t*1)
            sys.exit()

Submission status: Wrong Answer

Solution code:
l = int(input())
a = input()
b = input()


mina = None
maxa = None

for i in range(l+ 1) :
    if i * len(a) > l:
        break
    if (l - i * len(a)) % len(b) == 0:
        if mina is None:
            mina = i
            maxb = (l - i * len(a)) // len(b)
        maxa = i
        minb = (l - i * len(a)) // len(b)


if a < b:
    print(a * maxa + b * minb)
else:
    print(b * maxb + a * mina)

Submission status: Wrong Answer

Solution code:
l = int(input())
a = input()
b = input()


minl = None
maxl = None

for i in range(l+ 1) :
    if i * len(a) > l:
        break
    if (l - i * len(a)) % len(b) == 0:
        if minl is None:
            minl = i
        maxl = i


if a < b:
    print(a * maxl + b* ((l-maxl*len(a))//len(b) ))
else:
    print(b* ((l-minl*len(a))//len(b) ) + a* minl)

Submission status: Wrong Answer

Solution code:
l = int(input())
s = input()
t = input()

if s > t:
    tmp = s
    s = t
    t = tmp

s_cnt = 0
t_cnt = 0

ltmp = l
while (not (ltmp % len(s) == 0)):
    # print(ltmp)
    ltmp -= len(t)
    t_cnt += 1

s_cnt = ltmp // len(s)

# print(s_cnt)
# print(t_cnt)

ans = ''
for i in range(s_cnt):
    ans += s
for i in range(t_cnt):
    ans += t
print(ans)

Submission status: Wrong Answer

Solution code:
l = int(input())
a = input()
b = input()


minl = None
maxl = None

for i in range(l+ 1) :
    if i * len(a) > l:
        break
    if (l - i * len(a)) % len(b) == 0:
        if minl == None:
            minl = i
        maxl = i


if a < b:
    print(a * maxl + b* ((l-maxl*len(a))//len(b) ))
else:
    print(b* ((l-minl*len(a))//len(b) ) + a* minl)

Submission status: Wrong Answer

Solution code:
l = int(input())
a = input()
b = input()

minl = None
maxl = None

for i in range(l):
    if i * len(a) > l:
        break
    if (l - i * len(a)) % len(b) == 0:
        if minl == None:
            minl = i
        maxl = i

if a < b:
    print(a * maxl + b* ((l-maxl*len(a))//len(b) ))
else:
    print(b* ((l-minl*len(a))//len(b) ) + a* minl)

Submission status: Wrong Answer

Solution code:
from math import ceil


def solve(l, s, t):
    queue = [(1, 0), (0, 1)]

    def gcd(a, b):
        r = a % b
        if r:
            d = a // b
            sb = queue.pop()
            sa = queue.pop()
            queue.append(sb)
            queue.append(tuple(x - d * y for x, y in zip(sa, sb)))
            return gcd(b, r)
        else:
            return b

    ls, lt = len(s), len(t)
    if ls > lt:
        s, t, ls, lt = t, s, lt, ls
    m = ceil(lt / ls)
    if (s * m)[:lt] > t:
        s, t, ls, lt = t, s, lt, ls

    g = gcd(ls, lt)
    l //= g
    ls //= g
    lt //= g
    a, b = queue[-1]
    a *= l
    b *= l
    k = b // ls
    b -= ls * k
    a += lt * k
    return s * a + t * b


l = int(input())
s = input()
t = input()
print(solve(l, s, t))

Submission status: Wrong Answer

Solution code:
def solve(l, s, t):
    queue = [(1, 0), (0, 1)]

    def gcd(a, b):
        r = a % b
        if r:
            d = a // b
            sb = queue.pop()
            sa = queue.pop()
            queue.append(sb)
            queue.append(tuple(x - d * y for x, y in zip(sa, sb)))
            return gcd(b, r)
        else:
            return b

    if s > t:
        s, t = t, s
    ls, lt = len(s), len(t)
    g = gcd(ls, lt)
    l //= g
    ls //= g
    lt //= g
    a, b = queue[-1]
    a *= l
    b *= l
    k = b // ls
    b -= ls * k
    a += lt * k
    return s * a + t * b


l = int(input())
s = input()
t = input()
print(solve(l, s, t))

Submission status: Wrong Answer

Solution code:
def main():
    L = int(input())
    S, T = (input() for _ in [0] * 2)

    ans = []
    apnd = ans.append
    a = S
    b = T
    i = 0
    for _ in [0] * 2:
        while 1:
            x = L - (len(b) * i)
            div_, mod_ = divmod(x, len(a))
            if not mod_:
                apnd(a * div_ + b * i)
                break
            i += 1
        a, b = b, a
    print(min(ans))

main()

Submission status: Wrong Answer

Solution code:
def main():
    L = int(input())
    S, T = (input() for _ in [0] * 2)

    s = ''.join((S, S))
    t = ''.join((T, T))
    if s < t:
        a = S
        b = T
    else:
        a = T
        b = S

    i = 0
    while 1:
        x = L - (len(b) * i)
        div_, mod_ = divmod(x, len(a))
        if not mod_:
            ans = a * div_ + b * i
            break
        i += 1
    print(ans)

main()

Submission status: Wrong Answer

Solution code:
def main():
    L = int(input())
    S, T = (input() for _ in [0] * 2)

    if S < T:
        a = S
        b = T
    else:
        a = T
        b = S

    txt = []
    apnd = txt.append
    i = 0
    while 1:
        x = L - (len(b) * i)
        if x < 0:
            break
        div_, mod_ = divmod(x, len(a))
        if not mod_:
            apnd(a * div_ + b * i)
        i += 1

    print(min(txt))

main()

Submission status: Wrong Answer

Solution code:
def main():
    L = int(input())
    S, T = (input() for _ in [0] * 2)

    if S < T:
        a = S
        b = T
    else:
        a = T
        b = S

    i = 0
    while 1:
        x = L - (len(b) * i)
        div_, mod_ = divmod(x, len(a))
        if not mod_:
            ans = a * div_ + b * i
            break
        i += 1
    print(ans)

main()

Submission status: Wrong Answer

Solution code:
def main():
    L = int(input())
    S, T = (input() for _ in [0] * 2)

    if S < T:
        a = S
        b = T
    else:
        a = T
        b = S

    i = 0
    while 1:
        x = L - (len(b) * i)
        if not x % len(a):
            ans = a * (x // len(a)) + b * i
            break
        i += 1
    print(ans)

main()

Submission status: Accepted

Solution code:
n=int(input())
a=sorted([input()for i in range(2)])
s,t=len(a[0]),len(a[1])
c=[]
for i in range(n):
    if (n-i*t)%s==0:
        c.append(a[0]*((n-i*t)//s)+a[1]*i)
        break
for i in range(n):
    if (n-i*s)%t==0:
        c.append(a[1]*((n-i*s)//t)+a[0]*i)
        break
print(sorted(c)[0])

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

from fractions import gcd

L = int(readline())
S = readline().rstrip().decode('utf-8')
T = readline().rstrip().decode('utf-8')

ST = S + T
TS = T + S
if ST > TS:
    S,T = T,S

# Sを手前にした方が得

LS = len(S)
LT = len(T)

n = 0
for x in range(L+1):
    if (L - LS * x) % LT == 0:
        n = x
        break

n1, m1 = n, (L - LS * n) // LT
d = gcd(LS,LT)
n2, m2 = n1, m1
dn, dm = LT//d, LS//d
while m2 >= dm:
    m2 -= dm
    n2 += dn

x = S * n1 + T * m1
y = S * n2 + T * m2
answer = min(x,y)
print(answer)

Submission status: Accepted

Solution code:
L = int(input())
Ss = input()
Ts = input()

if Ss+Ts > Ts+Ss:
    Ss, Ts = Ts, Ss

lenS, lenT = len(Ss), len(Ts)

for numS in reversed(range(L//lenS+1)):
    numT = (L-lenS*numS) / lenT
    if int(numT) == numT:
        ans = Ss*numS + Ts*int(numT)
        print(ans)
        break

Submission status: Accepted

Solution code:
l = int(input())
a = input()
b = input()

if len(a) > len(b):
    k = b
    b = a
    a = k

mina = None
maxa = None
"""
f = [None] * (l + 1)

f[0] = ''
#print(f)
for i in range(l):
    tmpa = None
    tmpb = None
    if i + 1 >= len(a) and f[i + 1 - len(a)] is not None:
        tmpa = a + f[i + 1 - len(a)]
    if i + 1 >= len(b) and f[i + 1 - len(b)] is not None:
        tmpb = b + f[i + 1 - len(b)]
    if tmpa is None and tmpb is None:
        continue
    if tmpa is None and tmpb is not None:
        f[i+1] = tmpb
        continue
    if tmpb is None and tmpa is not None:
        f[i+1] = tmpa
        continue
    if tmpa < tmpb:
        f[i+1] = tmpa
    else:
        f[i+1] = tmpb
print(f[l])

def less(a, b):
    if a < b:
        if not b.startswith(a):
            return True
        else:
            c = b
            while c.startswith(a):
                c = c[len(a):]
            if a < c:
                return True
            else:
                return False

"""
for i in range(l+ 1) :
    if i * len(a) > l:
        break
    if (l - i * len(a)) % len(b) == 0:
        if mina is None:
            mina = i
            maxb = (l - i * len(a)) // len(b)
        maxa = i
        minb = (l - i * len(a)) // len(b)


ans1 = a * maxa + b * minb
ans2 = b * maxb + a * mina

if ans1 < ans2:
    print(ans1)
else:
    print(ans2)

Submission status: Accepted

Solution code:
def solve(l, s, t):
    queue = [(1, 0), (0, 1)]

    def gcd(a, b):
        r = a % b
        if r:
            d = a // b
            sb = queue.pop()
            sa = queue.pop()
            queue.append(sb)
            queue.append(tuple(x - d * y for x, y in zip(sa, sb)))
            return gcd(b, r)
        else:
            return b

    ls, lt = len(s), len(t)
    if ls > lt:
        s, t, ls, lt = t, s, lt, ls
    if (s * (lt // ls + 1) * 2)[:lt * 2] > t * 2:
        s, t, ls, lt = t, s, lt, ls
    g = gcd(ls, lt)
    l //= g
    ls //= g
    lt //= g
    a, b = queue[-1]
    a *= l
    b *= l
    k = b // ls
    b -= ls * k
    a += lt * k
    return s * a + t * b


l = int(input())
s = input()
t = input()
print(solve(l, s, t))

Submission status: Accepted

Solution code:
import sys
L = input()
s1,s2 = raw_input(),raw_input()
l1,l2 = len(s1), len(s2)

if l1 == l2:
  print( min(s1,s2) * (L//l1) )
  sys.exit()

cands = []
n1 = 0
while True:
  if (L - n1*l1) % l2 == 0:
    n2 = (L - n1*l1) // l2
    cands.append(s1*n1 + s2*n2)
    cands.append(s2*n2 + s1*n1)
    break
  n1 += 1

n2 = 0
while True:
  if (L - n2*l2) % l1 == 0:
    n1 = (L - n2*l2) // l1
    cands.append(s1*n1 + s2*n2)
    cands.append(s2*n2 + s1*n1)
    break
  n2 += 1

print(min(cands))

Submission status: Accepted

Solution code:
L = int(input())
s = input()
t = input()
if not s+t <= t+s:
    s, t = t, s
ls = len(s); lt = len(t)
for i in range(L // ls, -1, -1):
    if (L - ls*i) % lt == 0:
        ans = s * i + t * ((L - ls*i) // lt)
        break
print(ans)

Submission status: Accepted

Solution code:
def main():
    L = int(input())
    S, T = (input() for _ in [0] * 2)

    ans = []
    apnd = ans.append
    a = S
    b = T
    i = 0
    for _ in [0] * 2:
        while 1:
            x = L - (len(b) * i)
            div_, mod_ = divmod(x, len(a))
            if not mod_:
                apnd(a * div_ + b * i)
                break
            i += 1
        i = 0
        a, b = b, a
    print(min(ans))

main()

Submission status: Runtime Error

Solution code:
n=int(input())
s=input()
t=input()
if s>t:
    s,t=t,s
for i in range(n//len(s),-1,-1):
    if (n-i*len(s))%len(t)==0:
        ans=s*i
        ans+=t*((n-i*len(s)//len(t)))
        print(ans)
        exit()

Submission status: Runtime Error

Solution code:
from math import gcd
import sys

def lcm(a, b):
    return int(a*b/gcd(a,b))

L = int(input())
s = str(input())
t = str(input())
tmps = s
tmpt = t
t = min(s,t)
tl = len(t)

l_n = lcm(len(tmps), len(tmpt))
s = min(tmps*l_n,tmpt*l_n)
sl = len(s)

p = L//sl
q = L%sl

for i in range(0,p+1):
    if (q + i*sl)%tl == 0:
        S = s*(p-i)
        print(S + t*((L-len(S))//tl))
        sys.exit()

Submission status: Runtime Error

Solution code:
from math import gcd
import sys

def lcm(a, b):
    return a*b/gcd(a,b)

L = int(input())
s = str(input())
t = str(input())
tmps = s
tmpt = t
t = min(s,t)
tl = len(t)

l_n = lcm(len(tmps), len(tmpt))
s = min(tmps*l_n,tmpt*l_n)
sl = len(s)

p = L//sl
q = L%sl

for i in range(0,p+1):
    if (q + i*sl)%tl == 0:
        S = s*(p-i)
        print(S + t*((L-len(S))//tl))
        sys.exit()

Submission status: Runtime Error

Solution code:
from math import gcd
import sys

def lcm(a, b):
    return a*b/gcd(a,b)

L = int(input())
s = str(input())
t = str(input())
tmps = s
tmpt = t
t = min(s,t)
tl = len(t)

l_n = lcm(len(tmps), len(tmpt))
s = max(tmps*l_n,tmpt*l_n)
sl = len(t)

p = L//sl
q = L%sl

for i in range(0,p+1):
    if (q + i*sl)%tl == 0:
        S = s*(p-i)
        print(S + t*((L-len(S))//tl))
        sys.exit()

Submission status: Runtime Error

Solution code:
from math import gcd
import sys

def lcm(a, b):
    return a*b/gcd(a,b)

L = int(input())
s = str(input())
t = str(input())
tmps = s
tmpt = t
t = min(s,t)
tl = len(t)

l_n = lcm(len(tmps), len(tmpt))
s = max(tmps*l_n,tmpt*l_n)
sl = len(t)

p = L//sl
q = L%sl

for i in range(0,p+1):
    if (q + i*sl)%tl == 0:
        S = s*(p-i)
        print(S + t*((L-len(S))//tl))
        sys.exit()

Submission status: Runtime Error

Solution code:
l = int(input())
a = input()
b = input()

if len(a) > len(b):
    k = b
    b = a
    a = k

mina = None
maxa = None

f = [None] * (l + 1)

f[0] = ''
#print(f)
for i in range(l):
    tmpa = None
    tmpb = None
    if i + 1 >= len(a) and f[i + 1 - len(a)] is not None:
        tmpa = a + f[i + 1 - len(a)]
    if i + 1 >= len(b) and f[i + 1 - len(b)] is not None:
        tmpb = b + f[i + 1 - len(b)]
    if tmpa is None and tmpb is None:
        continue
    if tmpa is None and tmpb is not None:
        f[i+1] = tmpb
        continue
    if tmpb is None and tmpa is not None:
        f[i+1] = tmpa
        continue
    if tmpa < tmpb:
        f[i+1] = tmpa
    else:
        f[i+1] = tmpb
print(f[l])

Submission status: Runtime Error

Solution code:
import sys

L = int(input())
s = str(input())
t = str(input())
tmps = s
tmpt = t
s = min(s,t)
sl = len(s)
t = max(tmps,tmpt)
tl = len(t)

p = L//sl
q = L%sl

if q == 0:
    print(s*p)
else:
    for i in range(0,p+1):
        if (q + i*sl) == tl:
            print(s*(p-i) + t*i)
            sys.exit()

Submission status: Runtime Error

Solution code:
import sys

L = int(input())
s = str(input())
t = str(input())
tmps = s
tmpt = t
s = min(s,t)
sl = len(s)
t = max(tmps,tmpt)
tl = len(t)

p = L//sl
q = L%sl

if q == 0:
    print(s*p)
else:
    for i in range(1,p+1):
        if (q + i*sl) == tl:
            print(s*(p-i) + t*i)
            sys.exit()

Submission status: Runtime Error

Solution code:
def main():
    L = int(input())
    S, T = (input() for _ in [0] * 2)

    s = S * len(T)
    t = T * len(S)
    if s < t:
        a = S
        b = T
    else:
        a = T
        b = S

    i = 0
    while 1:
        x = L - (len(b) * i)
        div_, mod_ = divmod(x, len(a))
        if not mod_:
            ans = a * div_ + b * i
            break
        i += 1
    print(ans)

main()