Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
s = list(input())
ls = len(s)
ss = sorted(s, reverse=True)
cnt = [0, 0]
ans = 0
w = 0
while ss[w] == "W":
    w += 1

for i in range(ls):
    if s[i] == "B":
        ans += w + cnt[0] - i
    else:
        ans += i - cnt[1]
    cnt[s[i] != "B"] += 1

print(ans//2)


Fixed submission:
s = list(input())
ls = len(s)
ss = sorted(s, reverse=True)
cnt = [0, 0]
ans = 0
w = 0

while ss[w] == "W":
    w += 1
    if ls == w:
        break

for i in range(ls):
    if s[i] == "B":
        ans += w + cnt[0] - i
    else:
        ans += i - cnt[1]
    cnt[s[i] != "B"] += 1

print(ans//2)
Buggy python submission (verdict: Time Limit Exceeded):
from math import sqrt
q = int(input())
l, r = [], []
for i in range(q):
    a, b = map(int, input().split())
    l.append(a)
    r.append(b)


like = [0] * 100001
prime = set()


def isprime(n):
    global prime
    if (n in prime):
        return True
    else:
        f = False
        for i in range(2, int(sqrt(n)) + 1):
            f |= (n % i == 0)

        if not f:
            prime.add(n)
        return not f


def is2017like(n):
    if n % 2 == 0:
        return False
    return isprime(n) & isprime((n + 1) // 2)


for i in range(1, 100001):
    like[i] = like[i-1] + int(is2017like(i))


for i in range(q):
    print(like[r[i]]-like[max(1, l[i]-1)])


Fixed submission:
from math import sqrt
q = int(input())
l, r = [], []
for i in range(q):
    a, b = map(int, input().split())
    l.append(a)
    r.append(b)


like = [0] * 100001
prime = set()


def isprime(n):
    global prime
    if (n in prime):
        return True
    else:
        f = False
        for i in range(2, int(sqrt(n)) + 1):
            f |= (n % i == 0)

        if not f:
            prime.add(n)
        return not f


def is2017like(n):
    if n % 2 == 0:
        return False
    return isprime(n) & isprime((n + 1) // 2)


for i in range(1, 100001):
    like[i] = like[i-1] + int(is2017like(i))


for i in range(q):
    print(like[r[i]]-like[max(1, l[i]-1)])
Buggy python submission (verdict: Runtime Error):
s = list(input())
t = list(input())
ans = ["UNRESTORABLE"]
for i in range(len(s)):
    if s[i] != "?":
        for j in reversed(range(len(t))):
            if t[j] == s[i]:
                f = True
                for k in range(len(t)):
                    if not 0 <= i - j + k < len(t):
                        f = False
                        break
                    f %= (s[i - j + k] == "?" or s[i - j + k] == t[k])
                if f:
                    ans = s.copy()
                    for l in range(len(t)):
                        ans[i - j + l] = t[l]


print("".join(ans).replace("?", "a"))


Fixed submission:
s = list(input())
t = list(input())
ls = len(s)
lt = len(t)
ans = ["UNRESTORABLE"]

if "?" not in set(s):
    print("".join(s))
    exit()

for i in range(ls-lt+1):
    f = True
    for j in range(lt):
        f &= (s[i + j] == "?" or s[i + j] == t[j])

    if f:
        ans = s.copy()
        for j in range(lt):
            ans[i+j] = t[j]


print("".join(ans).replace("?", "a"))

Buggy python submission (verdict: Runtime Error):
s = list(input())
t = list(input())
ans = ["UNRESTORABLE"]
for i in range(len(s)):
    if s[i] != "?":
        for j in reversed(range(len(t))):
            if t[j] == s[i]:
                f = True
                for k in range(len(t)):
                    if not 0 <= i - j + k < len(t):
                        break
                    f %= (s[i - j + k] == "?" or s[i - j + k] == t[k])
                if f:
                    ans = s.copy()
                    for l in range(len(t)):
                        ans[i - j + l] = t[l]


print("".join(ans).replace("?", "a"))


Fixed submission:
s = list(input())
t = list(input())
ls = len(s)
lt = len(t)
ans = ["UNRESTORABLE"]

if "?" not in set(s):
    print("".join(s))
    exit()

for i in range(ls-lt+1):
    f = True
    for j in range(lt):
        f &= (s[i + j] == "?" or s[i + j] == t[j])

    if f:
        ans = s.copy()
        for j in range(lt):
            ans[i+j] = t[j]


print("".join(ans).replace("?", "a"))

Buggy python submission (verdict: Runtime Error):
s = list(input())
t = list(input())
ans = ["UNRESTORABLE"]
for i in range(len(s)):
    if s[i] != "?":
        for j in reversed(range(len(t))):
            if t[j] == s[i]:
                f = True
                for k in range(len(t)):
                    f &= (s[i - j + k] == "?" or s[i - j + k] == t[k])
                if f:
                    ans = s.copy()
                    for l in range(len(t)):
                        ans[i - j + l] = t[l]


print("".join(ans).replace("?", "a"))


Fixed submission:
s = list(input())
t = list(input())
ls = len(s)
lt = len(t)
ans = ["UNRESTORABLE"]

if "?" not in set(s):
    print("".join(s))
    exit()

for i in range(ls-lt+1):
    f = True
    for j in range(lt):
        f &= (s[i + j] == "?" or s[i + j] == t[j])

    if f:
        ans = s.copy()
        for j in range(lt):
            ans[i+j] = t[j]


print("".join(ans).replace("?", "a"))
Buggy python submission (verdict: Runtime Error):
s = list(input())
l, r = 0, len(s) - 1
ans = 0
while l != r:
    if s[l] == s[r]:
        l += 1
        r -= 1
    else:
        if s[l] == "x":
            l += 1
            ans += 1
        elif s[r] == "x":
            r -= 1
            ans += 1
        else:
            ans = -1
            break

print(ans)


Fixed submission:
s = list(input())
l, r = 0, len(s) - 1
ans = 0
while l < r:
    if s[l] == s[r]:
        l += 1
        r -= 1
    else:
        if s[l] == "x":
            l += 1
            ans += 1
        elif s[r] == "x":
            r -= 1
            ans += 1
        else:
            ans = -1
            break

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
S = input()
lS = len(S)
f = ""
ans = "YES"
i = 0
while(i < lS):
    tmp = S[i:i+5]
    if (tmp == "erase"):
        f = "r"
        i += 5
        continue
    if (tmp == "dream"):
        f = "er"
        i += 5
        continue
    if (f == ""):
        ans = "NO"
        break
    tmp = S[i:i+len(f)]
    if (tmp == f):
        i += len(f)
        f = ""

print(ans)
        


Fixed submission:
S = input()
S = S[::-1]
key = [
    "dream"[::-1],
    "erase"[::-1],
    "dreamer"[::-1],
    "eraser"[::-1]
]
f = False
while(S != ""):
    f = False
    for i in range(4):
        tmp = S[0:len(key[i])]
        if (tmp == key[i]):
            S = S[len(key[i]):]
            f = True
            break
    if not(f):
        break
        
if (f):
    print("YES")
else:
    print("NO")
Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())

v = {}
for i in range(m):
    a,b = map(int,input().split())
    if a not in v:
        v[a] = [b]
    else:
        v[a].append(b)

    if b not in v:
        v[b] = [a]
    else:
        v[b].append(a)

st = [1]

ans = [-1 for i in range(n)]
ans[0] = 0

while True:
    buf = []
    updates = {}
    for s in st:
        nex = v[s]
        for to in nex:
            if ans[to-1] != -1:
                continue
            else:
                buf.append(to)
                updates[to-1] = s
    for u in updates.keys():
        ans[u] = updates[u]
    st = buf
    if len(buf) == 0:
        break

print("Yes")
for i in range(1,n):
    print(ans[i])



Fixed submission:
n,m = map(int,input().split())

v = {}
for i in range(m):
    a,b = map(int,input().split())
    if a not in v:
        v[a] = [b]
    else:
        v[a].append(b)

    if b not in v:
        v[b] = [a]
    else:
        v[b].append(a)

st = [1]

ans = [-1 for i in range(n)]
ans[0] = 0

while True:
    buf = set()
    updates = {}
    for s in st:
        nex = v[s]
        for to in nex:
            if ans[to-1] != -1:
                continue
            else:
                buf.add(to)
                # buf.append(to)
                updates[to-1] = s
    for u in updates.keys():
        ans[u] = updates[u]
    st = buf
    # print(st)
    if len(buf) == 0:
        break

print("Yes")
for i in range(1,n):
    print(ans[i])
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
s = input()

# r_num = [0 for i in range(n)]
# g_num = [0 for i in range(n)]
# b_num = [0 for i in range(n)]

nums = [[0 for i in range(n)] for j in range(3)]

ans = 0

befs = [0 for i in range(3)]
for i in range(n):
    if s[i] == "R":
        befs[0] += 1
    elif s[i] == "G":
        befs[1] += 1
    else:
        befs[2] += 1
    for j in range(3):
        nums[j][i] = befs[j]

vac = [[-1,2,1],
       [2,-1,0],
       [1,0,-1]]
tr = {"R":0,"G":1,"B":2}

for i in range(n):
    for j in range(i+1,n-1):
        if s[i] == s[j]:
            continue
        else:
            v1 = tr[s[i]]
            v2 = tr[s[j]]
            nex = vac[v1][v2]
            # print(nex)
            tp = nums[nex][-1]-nums[nex][j+1]
            if tr[s[j+1]] == nex:
                tp += 1
            if j+j-i <= n-1 and tr[s[j+j-i]] == nex:
                tp -= 1
            ans += tp

print(ans)


Fixed submission:
n = int(input())
s = input()

# r_num = [0 for i in range(n)]
# g_num = [0 for i in range(n)]
# b_num = [0 for i in range(n)]

nums = [[0 for i in range(n)] for j in range(3)]

ans = 0

befs = [0 for i in range(3)]
for i in range(n):
    if s[i] == "R":
        befs[0] += 1
    elif s[i] == "G":
        befs[1] += 1
    else:
        befs[2] += 1
    for j in range(3):
        nums[j][i] = befs[j]

vac = [[-1,2,1],
       [2,-1,0],
       [1,0,-1]]
tr = {"R":0,"G":1,"B":2}

for i in range(n):
    for j in range(i+1,n-1):
        if s[i] == s[j]:
            continue
        else:
            v1 = tr[s[i]]
            v2 = tr[s[j]]
            nex = vac[v1][v2]
            # print(nex)
            tp = nums[nex][-1]-nums[nex][j+1]
            if tr[s[j+1]] == nex:
                tp += 1
            if j+j-i <= n-1 and tr[s[j+j-i]] == nex:
                tp -= 1
            ans += tp

print(ans)
Buggy python submission (verdict: Runtime Error):
import heapq

x,y,a,b,c = map(int,input().split())
r = list(map(int,input().split()))
g = list(map(int,input().split()))
w = list(map(int,input().split()))

r.sort(key = lambda x:-x)
g.sort(key = lambda x:-x)
w.sort(key = lambda x:-x)


rg = []
for i in range(x):
    rg.append(r[i])

for i in range(y):
    rg.append(g[i])

rg.sort(key = lambda x:-x)

ind = 0
ans = 0
for i in range(x+y):
    if w[ind] > rg[i]:
        ans += w[ind]
        ind += 1
    else:
        ans += rg[i]

print(ans)

Fixed submission:
import heapq

x,y,a,b,c = map(int,input().split())
r = list(map(int,input().split()))
g = list(map(int,input().split()))
w = list(map(int,input().split()))

r.sort(key = lambda x:-x)
g.sort(key = lambda x:-x)
w.sort(key = lambda x:-x)


rg = []
for i in range(x):
    rg.append(r[i])

for i in range(y):
    rg.append(g[i])

rg.sort(key = lambda x:x)


ind = 0
ans = 0
for i in range(x+y):
    if ind <= c-1 and w[ind] > rg[i]:
        ans += w[ind]
        ind += 1
    else:
        ans += rg[i]

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
h,w,kk = map(int,input().split())
arr = []
for i in range(h):
    arr.append(list(map(int,list(input()))))

h_loop = 2**(h-1)
ans = 100000000
for i in range(h_loop):
    sep = format(i,'b').zfill(h-1)
    ups = sum(map(int,list(sep)))
    slash = ups
    buf = [0 for j in range(ups+1)]
    temp_buf = [0 for j in range(ups+1)]
    is_sep = False
    # print(sep)
    for j in range(w):
        b_ind = 0
        is_sep = False
        for k in range(h):
            if k == 0:
                temp_buf[b_ind] += arr[k][j]
            else:
                if sep[k-1] == "1":
                    b_ind += 1
                    temp_buf[b_ind] += arr[k][j]
                else:
                    temp_buf[b_ind] += arr[k][j]

        for k in range(ups+1):
            if temp_buf[k] + buf[k] > kk:
                is_sep = True
        # print(buf)
        # print(temp_buf)
        # print(is_sep)
        if is_sep:
            slash += 1
            buf = temp_buf
        else:
            for k in range(ups+1):
                buf[k] += temp_buf[k]
        temp_buf = [0 for j in range(ups + 1)]

    # print(slash)
    if ans > slash:
        ans = slash

print(ans)




Fixed submission:
h,w,kk = map(int,input().split())
arr = []
for i in range(h):
    arr.append(list(map(int,list(input()))))

h_loop = 2**(h-1)
ans = 100000000
for i in range(h_loop):
    sep = format(i,'b').zfill(h-1)
    ups = sum(map(int,list(sep)))
    slash = ups
    buf = [0 for j in range(ups+1)]
    temp_buf = [0 for j in range(ups+1)]
    is_sep = False
    # print(sep)
    # print("########")
    # print(slash)
    for j in range(w):
        # print("#####")
        # print(j)
        b_ind = 0
        is_sep = False
        for k in range(h):
            if k == 0:
                temp_buf[b_ind] += arr[k][j]
            else:
                if sep[k-1] == "1":
                    b_ind += 1
                    temp_buf[b_ind] += arr[k][j]
                else:
                    temp_buf[b_ind] += arr[k][j]

        for k in range(ups+1):
            if temp_buf[k] + buf[k] > kk :
                is_sep = True
        for k in range(ups+1):
            if buf[k] > kk :
                slash = 1000000000
        # print(buf)
        # print(temp_buf)
        # print(is_sep)
        if is_sep:
            slash += 1
            buf = temp_buf
        else:
            for k in range(ups+1):
                buf[k] += temp_buf[k]

        temp_buf = [0 for j in range(ups + 1)]


    # print(slash)
    if ans > slash:
        ans = slash

print(ans)
Buggy python submission (verdict: Runtime Error):
a,b,m = map(int,input().split())

ref = list(map(int,input().split()))
mic = list(map(int,input().split()))

ans = min(ref)+min(mic)


for i in range(m):
    x,y,c = map(int,input().split())
    x-=1
    y-=1

    if ref[x]+ref[y]-c < ans:
        ans = ref[x]+ref[y]-c

print(ans)

Fixed submission:
a,b,m = map(int,input().split())

ref = list(map(int,input().split()))
mic = list(map(int,input().split()))

ans = min(ref)+min(mic)


for i in range(m):
    x,y,c = map(int,input().split())
    x-=1
    y-=1

    if ref[x]+mic[y]-c < ans:
        ans = ref[x]+mic[y]-c

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
n = int(input())
arr = list(map(int,input().split()))
buf = [0 for i in range(61)]

count = [[0,0] for i in range(61)]

for a in arr:
    txt = str(bin(a))[2:].zfill(61)

    for i in range(len(txt)):
        if txt[i] == "0":
            buf[i] += count[i][1]
            count[i][0] += 1
        else:
            buf[i] += count[i][0]
            count[i][1] += 1

m = 10**9 +7

buf.reverse()
ans = 0

for i in range(len(buf)):
    num = buf[i]
    base = np.power(2,i)
    base = base%m
    base = (base*num)%m

    ans += base
    ans = ans%m

# print(buf)
print(ans)





Fixed submission:
n = int(input())
arr = list(map(int,input().split()))
buf = [0 for i in range(61)]

count = [[0,0] for i in range(61)]

for a in arr:
    txt = str(bin(a))[2:].zfill(61)

    for i in range(len(txt)):
        if txt[i] == "0":
            buf[i] += count[i][1]
            count[i][0] += 1
        else:
            buf[i] += count[i][0]
            count[i][1] += 1

m = 10**9 +7

buf.reverse()
ans = 0

for i in range(len(buf)):
    num = buf[i]
    if i == 0:
        base = 1
    else:
        base = base*2
    b = base%m
    b = (b*num)%m

    ans += b
    ans = ans%m

# print(buf)
print(ans)
Buggy python submission (verdict: Runtime Error):


n,m = map(int,input().split())
s = input()


class segtree:

    def __init__(self, n,operator,identity):
        nb = bin(n)[2:]
        bc = sum([int(digit) for digit in nb])
        if bc == 1:
            self.num_leaves = 2**(len(nb)-1)
        else:
            self.num_leaves = 2**(len(nb))

        self.array = [identity for i in range(self.num_leaves * 2)]
        self.identity = identity
        self.operator =operator
        self.n = n

    def build(self,init_arr):
        for i in range(self.n):
            self.array[i+self.num_leaves] = init_arr[i]
        for i in range(self.num_leaves):
            pt = self.num_leaves-1-i
            self.array[pt] = self.operator(self.array[pt*2],self.array[pt*2+1])


    def update(self,x,val):
        actual_x = x+self.num_leaves
        # self.array[actual_x] = self.operator(self.array[actual_x],val)
        self.array[actual_x] = val
        while actual_x > 0 :
            actual_x = actual_x//2
            self.array[actual_x] = self.operator(self.array[actual_x*2],self.array[actual_x*2+1])

    def get(self,q_left,q_right,arr_ind=1,leaf_left=0,depth=0):
        width_of_floor = self.num_leaves//(2**depth)
        leaf_right = leaf_left+width_of_floor-1

        if leaf_left > q_right or leaf_right < q_left:
            return  self.identity

        elif leaf_left >= q_left and leaf_right <= q_right:
            return self.array[arr_ind]

        else:
            val_l = self.get(q_left,q_right,2*arr_ind,leaf_left,depth+1)
            val_r = self.get(q_left,q_right,2*arr_ind+1,leaf_left+width_of_floor//2,depth+1)
            return self.operator(val_l,val_r)

    def find(self, l, r):
        #findの方が高速?
        l += self.num_leaves
        r += self.num_leaves+1
        ret = self.identity
        while l < r:
            if l & 1:
                ret = self.operator(ret, self.array[l])
                l += 1
            if r & 1:
                r -= 1
                ret = self.operator(ret, self.array[r])
            l //= 2
            r //= 2
        return ret

st = segtree(n+1,min,1e10)

arr = [1e9 for i in range(n+1)]
arr[0] = 0

st.build(arr)

for i in range(1,n+1):
    if s[i] == "1":
        continue
    else:
        left = max(i-m,0)
        right = i-1
        tm = st.find(left,right)
        arr[i] = tm+1
        st.update(i,tm+1)


nowm = arr[-1]
ans = []
for i in range(2,n+2):
    if nowm > arr[-i]:
        nowm = arr[-i]
        ans.append(-i)
    elif nowm == arr[-i]:
        ans[-1] = -i

# print(arr)
# print(ans)

bef = -1
anss = ""
for a in ans:
    anss += str(bef-a)+" "
    bef = a
anss = anss[:-1]
print(anss[::-1])

Fixed submission:
n,m = map(int, input().split())
s = input()
now = 0
MIN = 0
ans = []
while now < n:
  for i in range(min(n-now,m),0,-1):
    nex = now + i
    if s[nex] == "0":
      now = nex
      MIN += 1
      break
  else:
    print(-1)
    exit()
now = 0
while now < n:
  for i in range(min(n-now,m),0,-1):
    nex = now + i
    if s[n-nex] == "0":
      now = nex
      ans.append(i)
      break
print(*reversed(ans))
Buggy python submission (verdict: Time Limit Exceeded):
n,t = map(int,input().split())

food = [[0,0] for i in range(n)]
for i in range(n):
  a,b = map(int,input().split())
  food[i][0] = a
  food[i][1] = b


dp = [[-10000000 for i in range((n+1)*3000)] for j in range(n+1)]
dp[0][0] = 0


for i in range(n):
  a = food[i][0]
  b = food[i][1]
  for j in range(t):
    eat = dp[i][j]+b
    next_time = j+a
    # print(j,next_time)
    if dp[i+1][next_time] < eat:
      dp[i+1][next_time] = eat
    if dp[i+1][j] < dp[i][j]:
      dp[i+1][j] = dp[i][j]

# for i in range(n):
#   for j in range(10,21):
#     print(dp[i][j],end=" ")
#   print()
print(max(dp[-1]))

Fixed submission:
n,t = map(int,input().split())
orders = []
for i in range(n):
    a,b = map(int,input().split())
    orders.append([a,b])

orders.sort(key=lambda x:(x[0],-x[1]))
# print(orders)

dp = [[-1e10 for i in range(t+1)] for j in range(n+1)]
dp[0][0] = 0

for i,od in enumerate(orders):
    for j in range(t+1):
        dp[i+1][j] = dp[i][j]
    for j in range(t):
        nt = j+od[0]
        if nt > t-1:
            nt = t
        if dp[i+1][nt] < dp[i][j]+od[1]:
            dp[i+1][nt] = dp[i][j]+od[1]

# for l in dp:
#     print(l[:11])
# print(dp[-1])
print(max(dp[-1]))
Buggy python submission (verdict: Time Limit Exceeded):
import bisect

n = int(input())
arr = list(map(int,input().split()))

arr.sort()
ans = 0
for i in range(n):
    for j in range(i+1,n):
        a,b = arr[i],arr[j]

        cmin = abs(a-b)
        cmax = a+b

        l = bisect.bisect_left(arr,cmin+1,lo=j+1)
        r = bisect.bisect_right(arr,cmax-1,lo=j+1)
        # print(cmin,l,cmax,r)
        if r-l > 0:
            ans += r-l

print(ans)


Fixed submission:
import bisect

n = int(input())
arr = list(map(int,input().split()))

arr.sort()
ans = 0
for i in range(n):
    for j in range(i+1,n):
        a,b = arr[i],arr[j]

        cmin = abs(a-b)
        cmax = a+b

        l = bisect.bisect_left(arr,cmin+1,lo=j+1)
        r = bisect.bisect_right(arr,cmax-1,lo=j+1)
        # print(cmin,l,cmax,r)
        if r-l > 0:
            ans += r-l

print(ans)
Buggy python submission (verdict: Runtime Error):

from bisect import bisect_left
s= input()
t = input()

d = {}
n = len(s)
ans = 0
for i in range(len(s)):
    if s[i] not in d:
        d[s[i]] = [i+1]
    else:
        d[s[i]].append(i+1)

flag = True
bef = 0
befchar = ""
for c in t:
    if c not in d:
        flag = False
        break
    else:
        if c == befchar:
            temp = bisect_left(d[c],bef)
            if temp == len(d[c]):
                temp = d[c][0]
                ans += temp + (n-bef)
                bef = temp
            else:
                temp = d[c][temp+1]
                ans += temp-bef
                bef = temp
        else:
            temp = bisect_left(d[c],bef)
            if temp == len(d[c]):
                temp = d[c][0]
                ans += temp + (n-bef)
                bef = temp
            else:
                temp = d[c][temp]
                ans += temp-bef
                bef = temp
    befchar=c
    # print(ans)
if flag:
    print(ans)
else:
    print(-1)




# arr = [0,2,4]
# print(bisect_left(arr,1))
# print(bisect_left(arr,5))
# print(bisect_left(arr,3))



Fixed submission:

from bisect import bisect_left
s= input()
t = input()

d = {}
n = len(s)
ans = 0
for i in range(len(s)):
    if s[i] not in d:
        d[s[i]] = [i+1]
    else:
        d[s[i]].append(i+1)

flag = True
bef = 0
befchar = ""
for c in t:
    if c not in d:
        flag = False
        break
    else:
        if c == befchar:
            temp = bisect_left(d[c],bef)
            # print(temp)
            if temp == len(d[c]):
                temp = d[c][0]
                ans += temp + (n-bef)
                bef = temp
            else:
                if temp +1 < len(d[c]):
                    temp = d[c][temp+1]
                    ans += temp-bef
                    bef = temp
                else:
                    temp = d[c][0]
                    ans += temp + (n - bef)
                    bef = temp
        else:
            temp = bisect_left(d[c],bef)
            if temp == len(d[c]):
                temp = d[c][0]
                ans += temp + (n-bef)
                bef = temp
            else:
                temp = d[c][temp]
                ans += temp-bef
                bef = temp
    # print(temp)
    befchar=c
    # print(ans)
# print(d)
if flag:
    print(ans)
else:
    print(-1)




# arr = [0,2,4]
# print(bisect_left(arr,2))
# print(bisect_left(arr,4))
# print(bisect_left(arr,5))