import sys

import math
from math import sqrt
import bisect

small = 10**5+1
big = 10**18+1

pos = []
f = pos.append
#pos.add(1)
j = 2
while True:
    #if sqrt(j).is_integer():
    #    j+=1
    #    continue
    j2 = j*j
    i = j*j2
    if i>=big:
        break
    #if int(i**0.5+0.5)**2==i:
    #    j+=1
    #    continue
    while i<big:
        f(i)
        i *= j2
    j += 1
pos2 = [p for p in pos if not sqrt(p).is_integer()]
pos2.sort()
prev = -1
pos = []
f = pos.append
for p in pos2:
    if p==prev:
        continue
    f(p)
    prev = p

q = int(input())
#inp = [int(x) for line in sys.stdin for x in line.split()]
#ii = 0
out = []
for line in sys.stdin:
    L,R = line.split()
    L = int(L)
    R = int(R)

    #ii+=2
    a = -1
    b = len(pos)

    while b-a>1:
        m = (a+b)//2
        if pos[m]<L:
            a=m
        else:
            b=m
    under = a
    #under = bisect.bisect_left(pos,L)
    #if under<len(pos) and pos[under]>=L:
    #    under -= 1

    a = -1
    b = len(pos)

    while b-a>1:
        m = (a+b)//2
        if pos[m]<=R:
            a = m
        else:
            b = m
    upper = a
    #upper = bisect.bisect_left(pos,R+1)-1
    Lupp = max(int(sqrt(L)-1),0)
    while Lupp*Lupp<L:
        Lupp+=1

    Rdown = int(sqrt(R))+1
    while Rdown*Rdown>R:
        Rdown-=1

    count = max(0,Rdown-Lupp+1)
    out.append(str(upper-under+count))
print('\n'.join(out))


import math
import bisect
import sys

def flrt(exp,x):
    l=max(0,math.floor(x**(1/exp))-3)

    r= math.floor(x**(1/exp))+3
    while l<r:
        mid=(l+r)//2
        if mid**exp<=x:
            l=mid+1
        else:
            r=mid
    return l-1

def c1(r):
    ans=set()
    pr = [5,7,11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61]
    for i in pr:
        x=2
        while x**i <= r:
            val=x**i
            b2=flrt(2,val)
            b3=flrt(3,val)
            if b2**2 != val and b3**3 != val:
                ans.add(val)
            x+=1
    return ans

def solve(r, pc):
    if r==0:
        return 0

    a=[2,3]
    ans=0
    for i in range(1,2**len(a)):
        tot=0
        mult=1
        for j,x in enumerate(a):
            if i&(1<<j):
                mult*=x
                tot+=1
        d=  flrt(mult,r)
        ans+= d if tot%2 else -d

    return ans + bisect.bisect_right(pc,r)

    lp=0
    rp=len(pc)
    while lp<rp:
        mid = (lp+rp)//2
        if pc[mid] <= r:
            lp = mid+1
        else:
            rp = mid

    return ans + lp
q = int(input())

pc= c1(1e18+1)

pca=list(pc)
pca.sort()

for i in range(q):
    l,r = [int(x) for x in sys.stdin.readline().split()]
    ans=solve(r,pca)-solve(l-1,pca)
    print(ans)


import math
import bisect
import sys

def flrt(exp,x):
    l=max(0,math.floor(x**(1/exp))-3)

    r= math.floor(x**(1/exp))+3
    while l<r:
        mid=(l+r)//2
        if mid**exp<=x:
            l=mid+1
        else:
            r=mid
    return l-1

def c1(r):
    ans=set()
    pr = [5,7,11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61]
    for i in pr:
        x=2
        while x**i <= r:
            val=x**i
            b2=flrt(2,val)
            b3=flrt(3,val)
            if b2**2 != val and b3**3 != val:
                ans.add(val)
            x+=1
    return ans

def solve(r, pc):
    if r==0:
        return 0

    a=[2,3]
    ans=0
    for i in range(1,2**len(a)):
        tot=0
        mult=1
        for j,x in enumerate(a):
            if i&(1<<j):
                mult*=x
                tot+=1
        d=  flrt(mult,r)
        ans+= d if tot%2 else -d

    return ans + bisect.bisect_right(pc,r)

    lp=0
    rp=len(pc)
    while lp<rp:
        mid = (lp+rp)//2
        if pc[mid] <= r:
            lp = mid+1
        else:
            rp = mid

    return ans + lp
q = int(input())

pc= c1(1e18+1)

pca=list(pc)
pca.sort()

for i in range(q):
    l,r = [int(x) for x in sys.stdin.readline().split()]
    ans=solve(r,pca)-solve(l-1,pca)
    sys.stdout.write(str(ans)+'\n')


# cook your dish here
n,p = map(int,input().split())
s = input()

d=s.count('d')
u = s.count('u')

# c = 1
# l = 1
# for i in range(1,n):

if(d<=p or u<=p):
    print(n)
else:
    if(d>=u):
        c = 0
        l = 0
        i = 0
        ii = -1
        pp = p
        while(i<n):
            if(s[i] == 'u' and ii == -1):
                ii = i
            if(pp == 0 and s[i] == 'u'):
                c = max(c,l)
                i = ii
                ii = -1
                l = -1
                pp = p
            elif(s[i]=='u'):
                pp-=1
            # print(l,i,ii)
            i += 1
            l += 1
        print(max(c,l))
    else:
        c = 0
        l = 0
        i = 0
        ii = -1
        pp = p
        while(i<n):
            if(s[i] == 'd' and ii == -1):
                ii = i
            if(pp == 0 and s[i] == 'd'):
                c = max(c,l)
                i = ii
                ii = -1
                l = -1
                pp = p
            elif(s[i]=='d'):
                pp-=1
            # print(l,i,ii)
            i += 1
            l += 1
        print(max(c,l))


N, P = map(int, input().split())
S = [i == 'd' for i in input().strip()]

def calcMax(ar):
	j = c = ans = 0
	for i in range(len(ar)):
		while j < len(ar) and c + ar[j] <= P:
			c += ar[j]
			j += 1
		ans = max(ans, j - i)
		c -= ar[i]
	return ans

print(max(calcMax(S), calcMax([not _ for _ in S])))


# cook your dish here
def findLen(A, n, k, ch):
	maxlen = 1
	cnt = 0
	l = 0
	r = 0

	# traverse the whole string
	while r < n:

		# if character is not same as ch
		# increase count
		if A[r] != ch:
			cnt += 1

		# While count > k traverse the string
		# again until count becomes less than k
		# and decrease the count when characters
		# are not same
		while cnt > k:
			if A[l] != ch:
				cnt -= 1
			l += 1

		# length of substring will be rightIndex -
		# leftIndex + 1. Compare this with the
		# maximum length and return maximum length
		maxlen = max(maxlen, r - l + 1)
		r += 1

	return maxlen

# function which returns
# maximum length of substring
def answer(A, n, k):
	maxlen = 1
	for i in range(26):
		maxlen = max(maxlen, findLen(A, n, k,
							chr(i + ord('A'))))
		maxlen = max(maxlen, findLen(A, n, k,
							chr(i + ord('a'))))

	return maxlen

n,k=map(int, input().split())
s=str(input())
print(answer(s, n, k))


N, I = list(map(int, input().split()))
A = sorted([int(a) for a in input().split()])
B = []
j = 0
for i in range(N):
    if i == 0 or A[i] == A[i-1]:
        B.append(j)
    else:
        j += 1
        B.append(j)

def calc(k):
    K = 1<<k
    i = 0
    j = 0
    ma = 0
    while j < N:
        if B[j]-B[i] <= K - 1:
            ma = max(ma, j-i+1)
            j += 1
        else:
            i += 1
    return N-ma

ans = 10**100
for i in range(31):
    if i * N <= 8 * I:
        ans = min(ans, calc(i))

print(ans)


from itertools import groupby as gb

N, I = map(int,input().split())
A = list(map(int,input().split()))
A = sorted(A)
l = []
G = gb(A)
for k, v in G:
    l.append(len(list(v)))

k = 100
while True:
    if N * k > 8 * I:
        k -= 1
    else:
        break
K = 2 ** k

NN = len(l)
if NN <= K:
    print(0)
    return

sm = sum(l[:K])
mx = sm

for i in range(K, NN):
    sm += l[i] - l[i - K]
    mx = max(mx, sm)

print(sum(l) - mx)


from math import log, floor
n,I = list(map(int, input().split()))
a = [int(s) for s in input().split()]
b = []
a.sort()
b.append([-1, 0])
for i in range(0, n):
    if a[i] == b[-1][0]:
        b[-1][1] += 1
    else:
        b.append([a[i], b[-1][1]+1])
maxk = I*8//n
maxK = 1<<maxk
# print(maxk, maxK)
ans = n
# print(b)
if len(b)-1 < maxK:
    ans = 0
else:
    for i in range(len(b)-maxK):
        chg = n-(b[i+maxK][1]-b[i][1])
        if chg < ans:
            ans = chg
print(ans)


import math

import sys
input = sys.stdin.readline

n, I = list(map(int, input().split()))
A = list(map(int, input().split()))

k = math.floor((I*8)/n)
K = 2**min(k, 20)

# find distinct
s = sorted(set(A))
dis = len(s)

# count occurence
count = {}

for i in range(n):
    if A[i] not in count:
        count[A[i]] = 1
    else:
        count[A[i]] += 1

# test eliminating
diff = max(dis - K, 0)

ct = 0
for i in range(diff):
    ct += count[s[i]]
ans = ct

for i in range(diff-1, -1, -1):
    ct -= count[s[i]]
    ct += count[s[i+K]]
    ans = min(ans, ct)

print(ans)


import sys
from collections import defaultdict
input = sys.stdin.readline

def main():
    n, I = list(map(int, input().split()))
    a = list(map(int, input().split()))
    K = 2**min(20, 8*I//n)

    a.sort()
    c = []
    pos = 0
    while pos < n:
        r = pos
        while r+1 < n and a[pos] == a[r+1]:
            r += 1
        c.append(r-pos+1)
        pos = r + 1

    if K > len(c):
        print(0)
    else:
        pref = c.copy()
        suff = c.copy()
        for i in range(1, len(c)):
            pref[i] += pref[i-1]
        for i in range(len(c)-2, -1, -1):
            suff[i] += suff[i+1]

        res = sum(c)
        l = 0
        r = K-1
        while r < len(c):
            s1 = 0 if l == 0 else pref[l-1]
            s2 = 0 if r == len(c)-1 else suff[r+1]
            res = min(res, s1+s2)
            l += 1
            r += 1

        print(res)

def __starting_point():
    main()

__starting_point()


R=lambda:map(int,input().split())
n,I=R()
a=sorted(R())
b=[0]+[i+1for i in range(n-1)if a[i]<a[i+1]]
print(n-max(y-x for x,y in zip(b,b[1<<8*I//n:]+[n])))


R=lambda:map(int,input().split())
n,I=R()
a=-1,*sorted(R())
b=[i for i in range(n)if a[i]<a[i+1]]
print(n-max(y-x for x,y in zip(b,b[1<<8*I//n:]+[n])))


R=lambda:map(int,input().split())
n,I=R()
a=[-1]+sorted(R())
b=[i for i in range(n)if a[i]<a[i+1]]
print(n-max(y-x for x,y in zip(b,b[1<<8*I//n:]+[n])))


from sys import stdin
input=stdin.readline
n,I=map(int,input().split())
a=sorted(list(map(int,input().split())))
key=[]
k=min(I*8//n,20)
K=min(2**k,n)
last=-1
for i in range(n):
    if a[i]==last:
        key[-1]+=1
    else:
        key.append(1)
        last=a[i]
now=0
best=0
for i in range(len(key)):
    if i<K:
        now+=key[i]
    else:
        now+=key[i]
        now-=key[i-K]
    best=max(now,best)
print(n-best)


from sys import stdin
input=stdin.readline
n,I=map(int,input().split())
a=sorted(list(map(int,input().split())))
key=[]
k=min(I*8//n,20)
K=min(2**k,n)
last=-1
for i in range(n):
    if a[i]==last:
        key[-1]+=1
    else:
        key.append(1)
        last=a[i]
now=best=0
for i in range(len(key)):
    if i<K:
        now+=key[i]
    else:
        now+=key[i]
        now-=key[i-K]
    best=max(now,best)
print(n-best)


import math
n,I=[int(x) for x in input().split()]
lis=[int(x) for x in input().split()]

dic=dict()

for i in lis:
    if i in dic:
        dic[i]+=1
    else:
        dic[i]=1

disLis=sorted(dic)
nowK=len(disLis)

if math.log2(nowK)>(I*8)//n:

    maxK=2**((I*8)//n)
    needMin=nowK-maxK

    minChange=0

    for i in range(needMin):
        minChange+=dic[disLis[i]]

    befChange=minChange

    for i in range(1,needMin+1):
        befChange=befChange-dic[disLis[needMin-i]]+dic[disLis[-i]]
        if befChange<minChange:
            minChange=befChange

    print(minChange)
else:
    print(0)


import math

n, I = map(int, input().split())
if I >= n*round(math.log(n,2)+0.5)/8:
    print(0)
else:
    a = sorted([x for x in map(int, input().split())])
    a=[-1]+a
    b=[i for i in range(n) if a[i] < a[i+1]]
    ans = set()
    for x in zip(b, b[2**(I*8//n):]+[n]):
        ans.add(x[1]-x[0])
    print(n-max(ans))


n,i=list(map(int,input().split()))
k=2**(8*i//n) if 8*i//n < 20 else n
a=[int(x) for x in input().split()]
a.sort()
freq = [1]
for i in range(1, len(a)):
	if a[i-1] == a[i]:
		freq[-1] += 1
	else:
		freq.append(1)
window = sum(freq[:k])
ans = window
for i in range(k, len(freq)):
	window += freq[i]
	window -= freq[i-k]
	ans = max(ans, window)

print(n-ans)


import math

n, m = list(map(int, input().split()))
lst = list(map(int, input().split()))

lst.sort()
last_val = -1
freq_list = []

for i in range(n):
	if lst[i] != last_val:
		last_val = lst[i]
		freq_list.append(1)
	else:
		freq_list[-1] += 1

k = len(freq_list)

for i in range(k, 0, -1):
	if n * math.ceil(math.log2(i)) <= m * 8:
		k = i
		break

mx = 0
tot = sum(freq_list[:k])
mx = max(tot, mx)
for i in range(k, len(freq_list)):
	tot = tot + freq_list[i] - freq_list[i-k]
	mx = max(mx, tot)

print(n-mx)


from itertools import groupby

def mp3(n, I, a):
    k = int((I * 8) / n)
    d = 1 << k
    c = [len(list(group)) for (key, group) in groupby(sorted(a))]
    chgs = sum(c[d:])
    ans = chgs
    for i in range(0, len(c) - d):
        chgs += c[i]
        chgs -= c[i + d]
        ans = min(ans, chgs)
    return ans

def __starting_point():
    nn, II = list(map(int, input().split()))
    aa = list(map(int, input().split()))
    print(mp3(nn, II, aa))
__starting_point()


from sys import stdin, stdout

n = int(stdin.readline())
s = stdin.readline().strip()

mins = []
packs = []

for i in range(len(s)):
    if s[i] == '*':
        mins.append(i)
    elif s[i] == 'P':
        packs.append(i)

l, r = -1, 2 * len(s) + 1
while r - l > 1:
    m = (l + r) >> 1

    test1 = mins[:]
    test2 = packs[:]

    while test2 and test1:
        cnt = m
        pos = test2.pop()

        if pos > test1[-1]:
            while test1 and abs(pos - test1[-1]) <= cnt:
                cnt -= abs(pos - test1[-1])
                pos = test1[-1]
                test1.pop()
        else:
            cntl, cntr = 0, 0

            if abs(test1[-1] - pos) > m:
                break

            lpos = (m + pos - test1[-1]) // 2
            rpos = m - 2 * abs(test1[-1] - pos)

            lb, rb = -1, len(test1)
            while rb - lb > 1:
                mb = (lb + rb) >> 1

                if pos - test1[mb] <= lpos:
                    rb = mb
                else:
                    lb = mb

            cntl = len(test1) - rb

            lb, rb = -1, len(test1)
            while rb - lb > 1:
                mb = (lb + rb) >> 1

                if pos - test1[mb] <= rpos:
                    rb = mb
                else:
                    lb = mb

            cntr = len(test1) - rb

            cnt = max(cntl, cntr)
            while test1 and cnt:
                test1.pop()
                cnt -= 1

    if not test1:
        r = m
    else:
        l = m

stdout.write(str(r))


import bisect

n = int(input())
s = input()
packmans = []
stars = []
for i in range(n):
    if s[i] == '*':
        stars.append(i)
    elif s[i] == 'P':
        packmans.append(i)

if len(stars) == 0:
    print(0)
    return

def check(t):
    first_to_eat = 0
    for i in range(len(packmans)):
        x = stars[first_to_eat]
        if packmans[i] > x:
            if packmans[i] - x > t:
                return False
            d1 = t - 2 * (packmans[i] - x)
            d2 = (t - (packmans[i] - x)) // 2
            first_to_eat = bisect.bisect_right(stars, packmans[i] + max(d1, d2))
            if first_to_eat < len(stars) and stars[first_to_eat] == packmans[i] + max(d1, d2):
                first_to_eat += 1

        else:
            j = bisect.bisect_right(stars, packmans[i] + t)
            if first_to_eat < len(stars) and stars[first_to_eat] == packmans[i] + t:
                first_to_eat += 1
            first_to_eat = max(j, first_to_eat)
        if first_to_eat >= len(stars):
            return True
    return first_to_eat >= len(stars)

l = 0
r = 2 * n + 1

while r - l > 1:
    m = (l + r) // 2
    if check(m):
        r = m
    else:
        l = m
print(r)


n, k = list(map(int, input().split()))
m = 0x3b9aca07
v = 500000004
r = 0
p = pow(2, n, m)
a = [1] + [0] * k
for i in range(k):
    for j in range(i, -1, -1):
        a[j + 1] += a[j]
        a[j] = a[j] * j % m
for i in range(k + 1):
    r = (r + p * a[i]) % m
    p = p * v * (n - i) % m
print(r)


n, k = map(int, input().split())
m = int(1e9 + 7)
r = 0
p = pow(2, n, m)
a = [1] + [0] * k
for i in range(k):
    for j in range(i, -1, -1):
        a[j + 1] += a[j]
        a[j] = a[j] * j % m
for i in range(k + 1):
    r = (r + p * a[i]) % m
    p = p * 500000004 * (n - i) % m
print(r)


n, k = map(int, input().split())
m = 0x3b9aca07
r = 0
p = pow(2, n, m)
a = [1] + [0] * k
for i in range(k):
    for j in range(i, -1, -1):
        a[j + 1] += a[j]
        a[j] = a[j] * j % m
for i in range(k + 1):
    r = (r + p * a[i]) % m
    p = p * 500000004 * (n - i) % m
print(r)


n, k = map(int, input().split())
m = 0x3b9aca07
r = 0
p = pow(2, n, m)
a = [1] + [0] * k
for i in range(k):
    for j in range(i, -1, -1):
        a[j + 1] += a[j]
        a[j] = a[j] * j % m
for i in range(k + 1):
    r += p * a[i]
    p = p * 500000004 * (n - i) % m
print(r % m)


n, k = map(int, input().split())
m = int(1e9+7)
r = 0
p = pow(2, n, m)
a = [1] + [0] * k
for i in range(k):
    for j in range(i, -1, -1):
        a[j+1] += a[j]
        a[j] = a[j]*j % m
for i in range(k + 1):
    r += p*a[i]
    p = p*500000004*(n - i) % m
print(r % m)


from collections import deque
n = int(input())
graph = [[] for i in range(n + 1)]
for _ in range(n - 1):
  i, j = map(int, input().split())
  graph[i].append(j)
  graph[j].append(i)
mod = 10 ** 9 + 7

def bfs(x):
  q = deque([(0, x, 0)])
  dist = {x: 0}
  while q:
    step, i, par = q.popleft()
    dist[i] = step
    for j in graph[i]:
      if j == par: continue
      q.append((step + 1, j, i))
  return [step, i, dist]

_, black, _ = bfs(1)
maxdist, white, b_dist = bfs(black)
_, _, w_dist = bfs(white)

mindls = float("-inf")
maxdls = [0] * n
for i in range(1, n + 1):
  if i in (white, black):
    continue
  mindls = max(mindls, min(w_dist[i], b_dist[i]))
  maxdls[max(w_dist[i], b_dist[i])] += 1
ans = pow(2, n - 1, mod) * maxdist % mod
pre = 0
for i in range(1, maxdist + 1):
  if i == maxdist and not maxdls[i]: continue
  maxdls[i] += maxdls[i - 1]
  if mindls > i: continue
  ans += (pow(2, maxdls[i], mod) - pre) * i * 2
  ans %= mod
  pre = pow(2, maxdls[i], mod)
print(ans)


MOD = 10**9+7
from collections import deque
N, = list(map(int, input().split()))
G = [set() for _ in range(N+1)]
for _ in range(N-1):
    a, b = list(map(int, input().split()))
    G[a].add(b)
    G[b].add(a)

queue=deque([1])
vs = set([1])
dist = [0] * (N+1)
mx = 0
mxv = 1
while queue:
    v = queue.popleft()
    for u in G[v]:
        if u in vs:
            continue
        vs.add(u)
        queue.append(u)
        dist[u] = dist[v] + 1
        if mx < dist[u]:
            mx = dist[u]
            mxv = u

queue=deque([mxv])
vs = set([mxv])
dist2 = [0] * (N+1)
mx2 = 0
mxv2 = 1
while queue:
    v = queue.popleft()
    for u in G[v]:
        if u in vs:
            continue
        vs.add(u)
        queue.append(u)
        dist2[u] = dist2[v] + 1
        if mx2 < dist2[u]:
            mx2 = dist2[u]
            mxv2 = u

queue=deque([mxv2])
vs = set([mxv2])
dist3 = [0] * (N+1)
while queue:
    v = queue.popleft()
    for u in G[v]:
        if u in vs:
            continue
        vs.add(u)
        queue.append(u)
        dist3[u] = dist3[v] + 1
#print(mxv, mxv2)
#print(dist2)
#print(dist3)
r = 0
for i in range(1, N+1):
    x = min(dist2[i], dist3[i])
    r = max(x, r)
# 絶対に良さはr以上になる
#　なぜならx = rとなるノードをどちらの色に塗っても、良さはrに決まるから...

X = [0]*(N+1)
for i in range(1, N+1):
    y = max(dist2[i], dist3[i])
    X[y] += 1

#print(r)
for i in range(N-1, r-1, -1):
    X[i] = X[i] + X[i+1]

#print(X)
# d[i] = dがi以下になる場合の数
X.append(0)
d = [0]*(N+1)
for i in range(N, r-1, -1):
    x = min(N-X[i+1]+1, N)
    d[i] = pow(2, x, MOD)
#print(d)

R = 0
for i in range(r, N+1):
    R = (R + i*(d[i]-d[i-1]))%MOD
print(R)


import sys
input = lambda: sys.stdin.readline().rstrip()

from collections import deque
N = int(input())
X = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = map(int, input().split())
    X[a-1].append(b-1)
    X[b-1].append(a-1)

def farthest(i):
    L = [-1] * N
    L[i] = 0
    d = 0
    post = [i]
    while len(post) > 0:
        d += 1
        pre = post
        post = []
        for j in pre:
            for k in X[j]:
                if L[k] < 0:
                    L[k] = d
                    post.append(k)

    return (pre[0], d - 1)

s, _ = farthest(0)
t, d = farthest(s)

def BFS_dist(n, E, i0=0):
    Q = deque([i0])
    D = [-1] * n
    D[i0] = 0
    while Q:
        x = Q.popleft()
        for c in E[x]:
            if D[c] == -1:
                D[c] = D[x] + 1
                Q.append(c)
    return D

D1 = BFS_dist(N, X, s)
D2 = BFS_dist(N, X, t)

Y = [0] * (d + 1)
ma = 0
for i in range(N):
    if i == s or i == t: continue
    a, b = sorted((D1[i], D2[i]))
    ma = max(ma, a)
    Y[b] += 1
Y[d] += 1

P = 10 ** 9 + 7
i2 = P + 1 >> 1
s = 1
ans = d
for i in range(d, ma, -1):
    if Y[i]: s = pow(i2, Y[i], P) * s % P
    ans -= s
    if ans < 0: ans += P
print(ans * pow(2, N, P) % P)


n = int(input())
ab = [list(map(int,input().split())) for i in range(n-1)]
graph = [[] for i in range(n+1)]
for a,b in ab:
  graph[a].append(b)
  graph[b].append(a)
mod = 10**9+7
start = 1
stack = [1]
s_dist = [0]+[0]+[10**9]*(n-1)
while stack:
  x = stack.pop()
  for y in graph[x]:
    if s_dist[y] > s_dist[x]:
      s_dist[y] = s_dist[x]+1
      stack.append(y)
maxdist = max(s_dist)
black = s_dist.index(maxdist)
b_dist = [10**9]*(n+1)
b_dist[0] = 0
b_dist[black] = 0
stack = [black]
while stack:
  x = stack.pop()
  for y in graph[x]:
    if b_dist[y] > b_dist[x]:
      b_dist[y] = b_dist[x]+1
      stack.append(y)
maxdist = max(b_dist)
white = b_dist.index(maxdist)
w_dist = [10**9]*(n+1)
w_dist[0] = 0
w_dist[white] = 0
stack = [white]
while stack:
  x = stack.pop()
  for y in graph[x]:
    if w_dist[y] > w_dist[x]:
      w_dist[y] = w_dist[x]+1
      stack.append(y)
mindls = [0]*n
maxdls = [0]*n
for i in range(1,n+1):
  if i in (white,black):
    continue
  mindls[min(w_dist[i],b_dist[i])] += 1
  maxdls[max(w_dist[i],b_dist[i])] += 1
ans = pow(2,n-1,mod)*maxdist%mod
number = 0
for i in range(1,maxdist+1):
  if i == maxdist and maxdls[i] == 0:
    continue
  maxdls[i] += maxdls[i-1]
  if i < maxdist and mindls[i+1]:
    continue
  ans += (pow(2,maxdls[i],mod)-number)*i*2
  ans %= mod
  number = pow(2,maxdls[i],mod)
print(ans)


n = int(input())
graph = [[] for i in range(n + 1)]
for _ in range(n - 1):
  i, j = map(int, input().split())
  graph[i].append(j)
  graph[j].append(i)
mod = 10 ** 9 + 7
start = 1
stack = [1]
s_dist = [0]+[0]+[10**9]*(n-1)
while stack:
  x = stack.pop()
  for y in graph[x]:
    if s_dist[y] > s_dist[x] + 1:
      s_dist[y] = s_dist[x]+1
      stack.append(y)
maxdist = max(s_dist)
black = s_dist.index(maxdist)
b_dist = [10**9]*(n+1)
b_dist[0] = 0
b_dist[black] = 0
stack = [black]
while stack:
  x = stack.pop()
  for y in graph[x]:
    if b_dist[y] > b_dist[x] + 1:
      b_dist[y] = b_dist[x]+1
      stack.append(y)
maxdist = max(b_dist)
white = b_dist.index(maxdist)
w_dist = [10**9]*(n+1)
w_dist[0] = 0
w_dist[white] = 0
stack = [white]
while stack:
  x = stack.pop()
  for y in graph[x]:
    if w_dist[y] > w_dist[x] + 1:
      w_dist[y] = w_dist[x]+1
      stack.append(y)
mindls = [0]*n
maxdls = [0]*n
for i in range(1,n+1):
  if i in (white,black):
    continue
  mindls[min(w_dist[i],b_dist[i])] += 1
  maxdls[max(w_dist[i],b_dist[i])] += 1
ans = pow(2,n-1,mod)*maxdist%mod
number = 0
for i in range(1,maxdist+1):
  if i == maxdist and maxdls[i] == 0:
    continue
  maxdls[i] += maxdls[i-1]
  if i < maxdist and mindls[i+1]:
    continue
  ans += (pow(2,maxdls[i],mod)-number)*i*2
  ans %= mod
  number = pow(2,maxdls[i],mod)
print(ans)


from collections import deque, defaultdict

def diameter(n, links):
    q = deque([0])
    stacked = {0}
    v = -1
    while q:
        v = q.popleft()
        for u in links[v]:
            if u not in stacked:
                q.append(u)
                stacked.add(u)
    v1 = v
    q = deque([(0, v1)])
    distances1 = [-1] * n
    distances1[v1] = 0
    while q:
        d, v = q.popleft()
        for u in links[v]:
            if distances1[u] == -1:
                q.append((d + 1, u))
                distances1[u] = d + 1
    v2 = v
    q = deque([(0, v2)])
    distances2 = [-1] * n
    distances2[v2] = 0
    while q:
        d, v = q.popleft()
        for u in links[v]:
            if distances2[u] == -1:
                q.append((d + 1, u))
                distances2[u] = d + 1
    return v1, v2, distances1, distances2

def solve(n, links):
    MOD = 10 ** 9 + 7
    v1, v2, distances1, distances2 = diameter(n, links)

    ans = distances1[v2] * pow(2, n - 2, MOD) % MOD

    farther = defaultdict(lambda: [0, 0])
    for i in range(n):
        if i == v1 or i == v2:
            continue
        d1 = distances1[i]
        d2 = distances2[i]
        mx = max(d1, d2)
        mn = min(d1, d2)
        farther[mx][0] += 1
        farther[mx][1] = max(farther[mx][1], mn)

    # print(v1, v2)
    # print(distances1)
    # print(distances2)
    # print(farther)

    m = n - 2
    max_smaller_d = 0
    for d in sorted(list(farther.keys()), reverse=True):
        if d <= max_smaller_d:
            ans = (ans + max_smaller_d * pow(2, m, MOD)) % MOD
            break
        cnt, mn = farther[d]
        m -= cnt
        ans = (ans + d * (pow(2, cnt, MOD) - 1) * pow(2, m, MOD)) % MOD
        max_smaller_d = max(max_smaller_d, mn)
    else:
        ans = (ans + max_smaller_d * pow(2, m, MOD)) % MOD

    return ans * 2 % MOD

n = int(input())
links = [set() for _ in range(n)]
for _ in range(n - 1):
    a, b = list(map(int, input().split()))
    a -= 1
    b -= 1
    links[a].add(b)
    links[b].add(a)

print((solve(n, links)))


from collections import deque
n = int(input())
graph = [[] for i in range(n + 1)]
for _ in range(n - 1):
  i, j = map(int, input().split())
  graph[i].append(j)
  graph[j].append(i)
mod = 10 ** 9 + 7

def bfs(x):
  q = deque([(0, x, 0)])
  while q:
    step, i, par = q.popleft()
    for j in graph[i]:
      if j == par: continue
      q.append((step + 1, j, i))
  return [step, i]

maxdist, black = bfs(1)

b_dist = [10**9]*(n+1)
b_dist[0] = 0
b_dist[black] = 0
stack = [black]
while stack:
  x = stack.pop()
  for y in graph[x]:
    if b_dist[y] > b_dist[x] + 1:
      b_dist[y] = b_dist[x]+1
      stack.append(y)
maxdist = max(b_dist)
white = b_dist.index(maxdist)
w_dist = [10**9]*(n+1)
w_dist[0] = 0
w_dist[white] = 0
stack = [white]
while stack:
  x = stack.pop()
  for y in graph[x]:
    if w_dist[y] > w_dist[x] + 1:
      w_dist[y] = w_dist[x]+1
      stack.append(y)
mindls = [0]*n
maxdls = [0]*n
for i in range(1,n+1):
  if i in (white,black):
    continue
  mindls[min(w_dist[i],b_dist[i])] += 1
  maxdls[max(w_dist[i],b_dist[i])] += 1
ans = pow(2,n-1,mod)*maxdist%mod
number = 0
for i in range(1,maxdist+1):
  if i == maxdist and maxdls[i] == 0:
    continue
  maxdls[i] += maxdls[i-1]
  if i < maxdist and mindls[i+1]:
    continue
  ans += (pow(2,maxdls[i],mod)-number)*i*2
  ans %= mod
  number = pow(2,maxdls[i],mod)
print(ans)


import sys
input = sys.stdin.readline
from collections import Counter

N=int(input())
E=[[] for i in range(N)]
for i in range(N-1):
    x,y=map(int,input().split())
    x-=1
    y-=1
    E[x].append(y)
    E[y].append(x)

mod=10**9+7

def dfs(x):
    DIS=[-1]*N
    DIS[x]=0
    Q=[x]

    while Q:
        x=Q.pop()
        for to in E[x]:
            if DIS[to]==-1:
                DIS[to]=DIS[x]+1
                Q.append(to)
    return DIS

D0=dfs(0)
L=D0.index(max(D0))
DL=dfs(L)
R=DL.index(max(DL))
DR=dfs(R)

MIN=[min(DL[i],DR[i]) for i in range(N)]
MAX=[max(DL[i],DR[i]) for i in range(N)]
C=Counter(MIN)

half=pow(2,mod-2,mod)%mod
ALL=pow(2,N,mod)
ANS=max(DR)*ALL*half%mod

MAX.sort()

dind=0
ANS=(ANS+max(MIN)*ALL*half)%mod

for d in range(max(MIN)+1,max(DR)+1):
    while dind<N and MAX[dind]<d:
        dind+=1
    ANS=(ANS+2*(ALL*half*half-pow(2,dind,mod)))%mod

print(ANS)


from collections import deque
n = int(input())
graph = [[] for i in range(n + 1)]
for _ in range(n - 1):
  i, j = map(int, input().split())
  graph[i].append(j)
  graph[j].append(i)
mod = 10 ** 9 + 7

def bfs(x):
  q = deque([(0, x, 0)])
  dist = {x: 0}
  while q:
    step, i, par = q.popleft()
    dist[i] = step
    for j in graph[i]:
      if j == par: continue
      q.append((step + 1, j, i))
  return [step, i, dist]

_, black, _ = bfs(1)
maxdist, white, b_dist = bfs(black)
_, _, w_dist = bfs(white)

mindls = [0] * n
maxdls = [0] * n
for i in range(1, n + 1):
  if i in (white, black):
    continue
  mindls[min(w_dist[i], b_dist[i])] += 1
  maxdls[max(w_dist[i], b_dist[i])] += 1
ans = pow(2, n - 1, mod) * maxdist % mod
pre = 0
for i in range(1, maxdist + 1):
  if i == maxdist and maxdls[i] == 0:
    continue
  maxdls[i] += maxdls[i - 1]
  if i < maxdist and mindls[i + 1]:
    continue
  ans += (pow(2, maxdls[i], mod) - pre) * i * 2
  ans %= mod
  pre = pow(2, maxdls[i], mod)
print(ans)


n = int(input())
graph = [[] for i in range(n + 1)]
for _ in range(n - 1):
  i, j = map(int, input().split())
  graph[i].append(j)
  graph[j].append(i)
mod = 10 ** 9 + 7
start = 1
stack = [1]
s_dist = [0]+[0]+[10**9]*(n-1)
while stack:
  x = stack.pop()
  for y in graph[x]:
    if s_dist[y] >= s_dist[x]:
      s_dist[y] = s_dist[x]+1
      stack.append(y)
maxdist = max(s_dist)
black = s_dist.index(maxdist)
b_dist = [10**9]*(n+1)
b_dist[0] = 0
b_dist[black] = 0
stack = [black]
while stack:
  x = stack.pop()
  for y in graph[x]:
    if b_dist[y] >= b_dist[x]:
      b_dist[y] = b_dist[x]+1
      stack.append(y)
maxdist = max(b_dist)
white = b_dist.index(maxdist)
w_dist = [10**9]*(n+1)
w_dist[0] = 0
w_dist[white] = 0
stack = [white]
while stack:
  x = stack.pop()
  for y in graph[x]:
    if w_dist[y] >= w_dist[x]:
      w_dist[y] = w_dist[x]+1
      stack.append(y)
mindls = [0]*n
maxdls = [0]*n
for i in range(1,n+1):
  if i in (white,black):
    continue
  mindls[min(w_dist[i],b_dist[i])] += 1
  maxdls[max(w_dist[i],b_dist[i])] += 1
ans = pow(2,n-1,mod)*maxdist%mod
number = 0
for i in range(1,maxdist+1):
  if i == maxdist and maxdls[i] == 0:
    continue
  maxdls[i] += maxdls[i-1]
  if i < maxdist and mindls[i+1]:
    continue
  ans += (pow(2,maxdls[i],mod)-number)*i*2
  ans %= mod
  number = pow(2,maxdls[i],mod)
print(ans)


from collections import deque
n = int(input())
graph = [[] for i in range(n + 1)]
for _ in range(n - 1):
  i, j = map(int, input().split())
  graph[i].append(j)
  graph[j].append(i)
mod = 10 ** 9 + 7

def bfs(x):
  q = deque([(0, x, 0)])
  dist = {x: 0}
  while q:
    step, i, par = q.popleft()
    dist[i] = step
    for j in graph[i]:
      if j == par: continue
      q.append((step + 1, j, i))
  return [step, i, dist]

_, black, _ = bfs(1)
maxdist, white, b_dist = bfs(black)
_, _, w_dist = bfs(white)

mindls = float("-inf")
maxdls = [0] * n
for i in range(1, n + 1):
  if i in (white, black):
    continue
  mindls = max(mindls, min(w_dist[i], b_dist[i]))
  maxdls[max(w_dist[i], b_dist[i])] += 1
ans = pow(2, n - 1, mod) * maxdist % mod
pre = 0
for i in range(1, maxdist + 1):
  if i == maxdist and maxdls[i] == 0:
    continue
  maxdls[i] += maxdls[i - 1]
  if mindls > i:
    continue
  ans += (pow(2, maxdls[i], mod) - pre) * i * 2
  ans %= mod
  pre = pow(2, maxdls[i], mod)
print(ans)


N=int(input())
E=[[] for i in range(N)]
for i in range(N-1):
    x,y=map(int,input().split())
    x-=1
    y-=1
    E[x].append(y)
    E[y].append(x)

mod=10**9+7

def dfs(x):
    DIS=[-1]*N
    DIS[x]=0
    Q=[x]

    while Q:
        x=Q.pop()
        for to in E[x]:
            if DIS[to]==-1:
                DIS[to]=DIS[x]+1
                Q.append(to)
    return DIS

D0=dfs(0)
L=D0.index(max(D0))
DL=dfs(L)
R=DL.index(max(DL))
DR=dfs(R)

MIN=[min(DL[i],DR[i]) for i in range(N)]
MAX=[max(DL[i],DR[i]) for i in range(N)]

half=pow(2,mod-2,mod)%mod
ALL=pow(2,N-1,mod)
ANS=max(DR)*ALL%mod

MAX.sort()

dind=0
ANS=(ANS+max(MIN)*ALL)%mod

for d in range(max(MIN)+1,max(DR)+1):
    while dind<N and MAX[dind]<d:
        dind+=1
    ANS=(ANS+2*(ALL*half-pow(2,dind,mod)))%mod

print(ANS)


"""

答えがX未満になる場合の数がわかればおk
→距離がX以上となるすべての点対間において違う色で塗られている

直径を考えてみる
端点を白とするとからX以上の距離は全て黒
もう片方の端点からX以上の距離は全て白にぬる
当然かぶったらout

かぶらなかったらok?
仮定する

実装は
各Xに関して、両方からX以内の頂点の数を数え上げる

"""

import sys
from sys import stdin

#重みのないグラフでの最短経路問題
#隣接リストと始点を与えると始点からの距離のリスト & 親のリストを返す
from collections import deque
def NC_Dij(lis,start):

    ret = [float("inf")] * len(lis)
    ret[start] = 0

    q = deque([start])
    plis = [i for i in range(len(lis))]

    while len(q) > 0:
        now = q.popleft()

        for nex in lis[now]:

            if ret[nex] > ret[now] + 1:
                ret[nex] = ret[now] + 1
                plis[nex] = now
                q.append(nex)

    return ret,plis

mod = 10**9+7

N = int(stdin.readline())

lis = [ [] for i in range(N) ]

for i in range(N-1):
    a,b = list(map(int,stdin.readline().split()))
    a -= 1
    b -= 1
    lis[a].append(b)
    lis[b].append(a)

D0,tmp = NC_Dij(lis,0)
p1 = 0
for i in range(N):
    if D0[i] > D0[p1]:
        p1 = i

D1,tmp = NC_Dij(lis,p1)
p2 = p1
for i in range(N):
    if D1[i] > D1[p2]:
        p2 = i

D2,tmp = NC_Dij(lis,p2)

DL1 = []
for i in range(N):
    DL1.append( (D1[i],i) )
DL2 = []
for i in range(N):
    DL2.append( (D2[i],i) )

DL1.sort()
DL1.reverse()
DL2.sort()
DL2.reverse()

anslis = [0] * N
visit = [0] * N
two  = 0
zero = N
for X in range(N):

    while len(DL1) > 0 and DL1[-1][0] == X:
        tmp,v = DL1[-1]
        del DL1[-1]

        if visit[v] == 0:
            zero -= 1
        elif visit[v] == 1:
            two += 1
        visit[v] += 1

    while len(DL2) > 0 and DL2[-1][0] == X:
        tmp,v = DL2[-1]
        del DL2[-1]

        if visit[v] == 0:
            zero -= 1
        elif visit[v] == 1:
            two += 1
        visit[v] += 1

    if two == N:
        anslis[X] = pow(2,N,mod)
    elif zero == 0:
        anslis[X] = 2 * pow(2,two,mod)

ans = 0
for i in range(1,N):
    ans += (anslis[i]-anslis[i-1]) * i
print((ans % mod))


n, k = map(int, input().split())
d = 1000000007
def f(a, b):
    if b == 0: return 1
    s, c = 0, b * a
    for i in range(1, b + 1):
        s += c * f(i, b - i)
        c = (a * c * (b - i)) // (i + 1)
    return s
print(k * f(1, k - 1) * pow(n - k, n - k, d) % d)


n, k = map(int, input().split())
d = 1000000007
print(pow(k, k - 1, d) * pow(n - k, n - k, d) % d)


MOD = 10 ** 9 + 7
n, k = map(int, input().split())
ans = pow(n - k, n - k, MOD) * pow(k, k - 1, MOD)
print(ans % MOD)


#cf288b.py
import sys
(n,k)=list(map(int,sys.stdin.readline().split()))
mod_prime=1000000007
f=[0]*(k+2)
#f(x)= the number of ways that a group of x elements eventually leads to 1 (1 is not part of the group and does not points to anywhere)
f[0]=1
f[1]=1
c=[[1 for j in range(i+1)] for i in range (k)]
for i in range(1,len(c)):
	for j in range(1,len(c[i])-1):
		c[i][j]=(c[i-1][j-1]+c[i-1][j])%mod_prime
#print (c)
for x in range(2,k):
	f[x]=0
	for i in range(0,x):
		f[x]+=c[x-1][i]*f[i]*f[x-1-i]*(i+1)%mod_prime
		#print("x={0},i={1},f[x]={2}".format(x,i,f[x]))

ans=(k*f[k-1]*(n-k)**(n-k))%mod_prime
#print(f)
print(ans)


n, k = map(int, input().split())
mod = 1000000007
print(pow(k, k-1, mod) * pow(n-k, n-k, mod) % mod)


n,k = list(map(int,input().split()))
mod = 10 ** 9 + 7
print(pow(k, k - 1, mod) * pow(n - k, n - k, mod) % mod)


n,k=list(map(int,input().split()));n-=k;print(k**~-k*n**n%(10**9+7))

# Made By Mostafa_Khaled


n, k = map(int, input().split())
m = 1000000007
print(pow(k, k - 1, m) * pow(n - k, n - k, m) % m)


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

m = 1000000007

print((pow(k,k-1,m)*pow(n-k,n-k,m))%m)


import sys
from collections import defaultdict

class MaxFlow(object):
    def __init__(self):
        self.edges = defaultdict(lambda: defaultdict(lambda: 0))

    def add_edge(self, u, v, capacity=float('inf')):
        self.edges[u][v] = capacity

    def bfs(self, s, t):
        open_q = [s]

        visited = set()
        parent = dict()
        while open_q:
            close_q = []
            for node in open_q:
                for v, capacity in list(self.edges[node].items()):
                    if v not in visited and capacity > 0:
                        close_q.append(v)
                        parent[v] = node
                        visited.add(v)
                        if v == t:
                            result = []
                            n2 = v
                            n1 = node
                            while n1 != s:
                                result.append((n1, n2))
                                n2 = n1
                                n1 = parent[n1]
                            result.append((n1, n2))
                            return result

            open_q = close_q

        return None

    def solve(self, s, t):
        flow = 0
        route = self.bfs(s, t)
        while route is not None:
            new_flow = float('inf')
            for _, (n1, n2) in enumerate(route):
                new_flow = min(new_flow, self.edges[n1][n2])
            for _, (n1, n2) in enumerate(route):
                self.edges[n1][n2] -= new_flow
                self.edges[n2][n1] += new_flow
            flow += new_flow

            route = self.bfs(s, t)

        return flow

    def __str__(self):
        result = "{ "
        for k, v in list(self.edges.items()):
            result += str(k) + ":" + str(dict(v)) + ", "
        result += "}"
        return result

def main():
    (n, m) = tuple([int(x) for x in input().split()])
    r = []
    xs = set()
    ys = set()
    for i in range(m):
        (x1, y1, x2, y2) = tuple(int(x) for x in input().split())
        r.append((x1, y1, x2, y2))
        xs.add(x1)
        xs.add(x2 + 1)
        ys.add(y1)
        ys.add(y2 + 1)

    xx = sorted(xs)
    yy = sorted(ys)
    xsize = len(xs)
    ysize = len(ys)
    grid = []
    for i in range(ysize):
        grid.append([False] * xsize)

    for rect in r:
        x1 = rect[0]
        y1 = rect[1]
        x2 = rect[2]
        y2 = rect[3]
        for i, y in enumerate(yy):
            for j, x in enumerate(xx):
                if x1 <= x and y1 <= y and x2 >= x and y2 >= y:
                    grid[i][j] = True

    f = MaxFlow()
    for i in range(len(yy)):
        for j in range(len(xx)):
            if grid[i][j]:
                f.add_edge(1 + i, len(yy) + 1 + j, float('inf'))
    for i in range(len(yy) - 1):
        f.add_edge(0, i + 1, yy[i + 1] - yy[i])
    for i in range(len(xx) - 1):
        f.add_edge(len(yy) + 1 + i, len(xx) + len(yy) + 1, xx[i + 1] - xx[i])

    # print(xx)
    # print(yy)
    # print(f)
    print(f.solve(0, len(xx) + len(yy) + 1))

def __starting_point():
    main()

__starting_point()


a,b=[int(i) for i in input().split()]
if(a==0):
    print(-b**2)
    print('x'*b)
elif(b==0):
    print(a**2)
    print('o'*a)
elif(b==1):
    print(a**2-1)
    print('x'+'o'*a)
else:
    ans=-float('inf')
    gr_no=None
    for i in range(2,min(a+2,b+1)):
        v1=(a+2-i)**2 + i-2
        quo=b//i
        rem=b%i
        v2=rem*((quo+1)**2) + (i-rem)*((quo**2))
        if(v1-v2>ans):
            gr_no=i
            ans=v1-v2
    quo=b//gr_no
    rem=b%gr_no
    if(rem>0):
        s='x'*(quo+1)+'o'*(a+2-gr_no)
        rem-=1
    else:
        s='x'*(quo)+'o'*(a+2-gr_no)
    gr_no-=1
    s1='x'*(quo+1)+'o'
    s2='x'*quo + 'o'
    for i in range(rem):
        s+=s1
    for i in range(gr_no-rem-1):
        s+=s2
    s+='x'*(quo)
    print(ans)
    print(s)


import itertools
import bisect

n, x = map(int, input().split())
l = list(map(int, input().split()))

a = list(itertools.accumulate(l, initial=0))
print(bisect.bisect_right(a, x))


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

l_list=[int(i) for i in input().split()]

d=0
count=1
for i in range(n):
    d=d+l_list[i]
    if d<=x:
        count+=1

print(count)


n, x = map(int, input().split())

l = list(map(int, input().split()))

ans = 0

d = 0

for i in range(n):
    if d <= x:
        ans += 1
    d += l[i]

if d <= x:
    ans += 1

print(ans)


import bisect
from itertools import accumulate
N, X = list(map(int, input().split()))
L = list(map(int, input().split()))
Lsum = list(accumulate(L))
ind = bisect.bisect_right(Lsum, X)
print((ind+1))


import bisect
n, x = map(int, input().split())
l = [0] + list(map(int, input().split()))
for i in range(n):
  l[i+1] += l[i]
print(bisect.bisect_left(l, x+1))


N,X=list(map(int,input().split()))
L=list(map(int,input().split()))
x=0
ans=1
for i in range(N):
    x+=L[i]
    if x<=X:
        ans+=1
print(ans)


n, x = list(map(int, input().split()))
l = list(map(int, input().split()))

l1 = [0]+l
for i in range(1, len(l)+1):
    l1[i] = l1[i-1]+l[i-1]
cnt = 0

for i in range(0, len(l1)):
    if (l1[i] <= x):
        cnt += 1
print(cnt)


N, M = map(int, input().split())
L = list(map(int, input().split()))
d = 0
ans = 1
for l in L:
  d += l
  if d <=M:
    ans += 1
  else:
    break
print(ans)


n, x = map(int, input().split())
l = list(map(int, input().split()))

d = [0]
for i in range(n):
    d.append(d[-1]+l[i])
print(len([i for i in d if i <= x]))


n, x = list(map(int,input().split()))
l = list(map(int,input().split()))
a = [0]
for i in range(n):
    tmp = a[i] + l[i]
    a.append(tmp)
ans = 0
for i in range(n + 1):
    if (a[i] <= x):
        ans = ans + 1

print(ans)


n,x = map(int, input().split())
L = list(map(int, input().split()))
cnt = 1
d = 0
for l in L:
  d += l
  if d <= x:
    cnt += 1
  else:
    break
print(cnt)


from itertools import accumulate
from bisect import *
N, X = list(map(int, input().split()))
L = list(tuple(map(int, input().split())))

acc = sorted(list(accumulate(L)) + [0])

n = bisect_right(acc,X)
print(n)


# AtCoder Beginner Contest 130
# B - Bounding
import bisect

N,X=map(int,input().split())
L=list(map(int,input().split()))

accumu_L=[0]

for i in range (N):
    accumu_L.append(accumu_L[-1]+L[i])

# print(accumu_L)

print(bisect.bisect_right(accumu_L,X))


n,x=map(int,input().split())
l=list(map(int,input().split()))+[10**5]

d,cnt=0,0
while d<=x and cnt<n+1:
  d+=l[cnt]
  cnt+=1

print(cnt)


N,X = list(map(int,input().split()))
li = list(map(int,input().split()))
d = 0
c = 1
for i in range(N):
  d = d + li[i]
  if d <=X:
    c = c + 1
print(c)


a,b=input().split()
a=int(a)
b=int(b)
c=list(map(int,input().split()))
d=0
e=0
for i in range(a):
  if d+c[i]<=b:
    d=d+c[i]
    e=e+1
  else:
    break
print(e+1)


n,x=map(int,input().split())
l=list(map(int,input().split()))
ans=1
d=0
for i in range(len(l)):
    d=d+l[i]
    if d<=x:
        ans+=1
print(ans)


n,x = map(int, input().split())
a = [int(num) for num in input().split()]
sum = 0
cont = 1
for i in range(n):
    sum += a[i]
    if sum <= x:
        cont += 1
print(cont)


from collections import defaultdict

def mapt(fn, *args):
    return tuple(map(fn, *args))

def Input():
    return mapt(int, input().split(" "))

def main():
    n, x = Input()
    l = Input()
    d = defaultdict(int)
    d[0] = 0
    ans = 0

    for i in range(1, n+1):
        d[i] = d[i-1] + l[i-1]

    for i in d.values():
        if i <= x: ans += 1

    print(ans)

main()


n,x=map(int,input().split())
Y = list(map(int,input().split()))
Z = [0]*n
a = 0
for i in range(n):
  a +=Y[i]
  Z[i]=a
import bisect
print(1+bisect.bisect_left(Z,x+1))


n,x=map(int,input().split())
L=list(map(int,input().split()))
ans = 1
cnt = 0
for l in L:
  cnt += l
  if x >= cnt:
    ans += 1
print(ans)


n, x = map(int, input().split())
l = list(map(int, input().split()))
bound = []
for i in range(n+1):
    if i == 0:
        bound += [0]
    else:
        bound += [bound[i-1] + l[i-1]]
    if bound[i] > x:
        print(len(bound) - 1)
        return
print(len(bound))


N, X = list(map(int, input().split()))
L = list(map(int, input().split()))

current = 0
cnt = 1
for l in L:
    current += l
    if current > X:
        break
    cnt += 1

print(cnt)


# Di = Di-1 + Li-1
n, x = list(map(int, input().split()))
L = list(map(int, input().split()))
ans = 0
ct = 1
for i in range(n):
    ans = ans + L[i]
    if ans > x:
        break
    else:
        ct += 1
print(ct)


N, X = map(int, input().split())
L = list(map(int, input().split()))

s = 0
cnt = 1
for i in range(N):
    if s + L[i] > X:
        break
    s += L[i]
    cnt += 1

print(cnt)


n,x =  map(int,input().split())
l = list(map(int,input().split()))
ans = 0
cnt = 1
for i in l:
  ans += i
  if ans > x:
    break
  cnt += 1

print(cnt)


import bisect

N, X = list(map(int, input().split()))
L = list(map(int, input().split()))

D = [0]
for i in range(len(L)):
    D.append(sum(L[:i+1]))
print((bisect.bisect_right(D, X)))


a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = 1
d = 0
for i in b:
  d += i
  if d <= a[1]:
    c += 1
print(c)


N,X = map(int,input().split())
L = [int(i) for i in input().split()]
D = 0
for i in range(N) :
    D = L[i]+D
 #   print(i,D)
    if D>X :
        print(i+1)
        return

print(N+1)


r=input().split()
N=int(r[0])
X=int(r[1])
d=[int(s) for s in input().split()]
ans=1
le=0
for i in range(N):
    le+=d[i]
    if le>X:
        print(ans)
        break
    elif i==N-1:
        print(N+1)
    else:
        ans+=1


n, x = map(int, input().split())
l = [int(s) for s in input().split()]

pos = 0
count = 1
for i in range(n):
    pos += l[i]
    if pos > x:
        break
    count += 1
print(count)


N, X  = map(int, input().split())
L = list(map(int, input().split()))
count = 1
distance = 0
for l in L:
  distance += l
  if distance <= X:
    count += 1
  else:
    break
print(count)


n, x = map(int, input().split())
l = list(map(int, input().split()))
xlist = [0]
num = 0
for i in l:
    num += i
    xlist.append(num)
cnt = 0
for j in xlist:
    if j <= x:
        cnt += 1
print(cnt)


n, X = map(int, input().split())
l = list(map(int, input().split()))

sumList = [0]
for i in range(n):
    s = sumList[i]+l[i]
    if s > X:
        break
    sumList.append(s)

print(len(sumList))


N, X = map(int, input().split())
L = list(map(int, input().split()))
S = [0]
for i in range(N):
  S.append(S[i] + L[i])
ans = 0
for j in range(N+1):
  if S[j] <= X:
    ans += 1
print(ans)


N,X=map(int,input().split())
L=list(map(int,input().split()))
ans=0

for i in range(1,N+1):
  if ans>=X:
    if ans==X:
      print(i)
      break
    else:
      print(i-1)
      break
  ans+=L[i-1]
else:
  print(N+1)


import sys

def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return input()
def LS(): return input().split()

INF = float('inf')

n, x = LI()
l = LI()
cnt = 1
d = 0
for i in range(n):
    # print(f'{(d, l[i])=}')
    if d + l[i] <= x:
        cnt += 1
        d += l[i]
    else:
        break
print(cnt)


N, X = map(int, input().split())
L = list(map(int, input().split()))
D = 0
count = 0
for i in range(N+1):
  if D <= X:
    count += 1
  if i != N:
    D = D + L[i]
print(count)


n, x = map(int, input().split())
l = list(map(int, input().split()))
L = [0] + l

a = 0
count = 1
for i in l:
    a += i
    if a <= x:
        count += 1
print(count)


n,x=map(int, input().split())
l=list(map(int, input().split()))
d=0
count=0
for i in range(n):
  d+=l[i]
  if d>x:
    print(i+1)
    break
  if i==n-1 and d<=x:
    print(n+1)


n,x = map(int,input().split())
l = list(map(int,input().split()))
d = 0
i = 0
while d <= x and i <= n:
    if i == n:
        i += 1
    else:
        d += l[i]
        i += 1
print(i)


n,x=map(int,input().split())
ans=1
p=0
l=[int(x) for x in input().split()]
for i in range(n):
    p+=l[i]
    if p > x:
        break
    else:
        ans+=1

print(ans)


x, y = list(map(int, input().split()))
num_list = list(map(int, input().split()))

i = 1
distance = 0
for _ in num_list:
    distance += _
    if distance > y:
        break
    i += 1

print(i)


n, x = map(int, input().split())
l = list(map(int, input().split()))
ans = 1
p = 0

for i in range(n):
    p += l[i]
    if p <= x:
        ans += 1
    else:
        break

print(ans)


n, x = list(map(int, input().split()))
l = list(map(int, input().split()))

ans = 0
bound = 0
for i in l:
    bound += i
    ans += 1
    if bound > x:
        break
else:
    ans = n + 1

print(ans)


n, x = list(map(int, input().split()))
L = list(map(int, input().split()))

d = [0]*(n+1)
cnt = 0
for i in range(n):
    d[i+1] += d[i]+L[i]

for i in range(n+1):
    if d[i] <= x:
        cnt += 1

print(cnt)


n,x = map(int,input().split())
a = list(map(int,input().split()))
c = 1
b = 0
for i in a:
    b = i+b
    if b <= x:
        c+=1
print(c)


n,x = map(int,input().split())
a = list(map(int,input().split()))
a.append(0)
count = 0
b = 0
for i in range(n+1):
    if b > x:
        break
    count+=1
    b+= a[i]
print(count)


import sys
sys.setrecursionlimit(10 ** 9)
# input = sys.stdin.readline    ####
def int1(x): return int(x) - 1
def II(): return int(input())
def MI(): return list(map(int, input().split()))
def MI1(): return list(map(int1, input().split()))
def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def MS(): return input().split()
def LS(): return list(input())
def LLS(rows_number): return [LS() for _ in range(rows_number)]
def printlist(lst, k=' '): print((k.join(list(map(str, lst)))))
INF = float('inf')
# from math import ceil, floor, log2
# from collections import deque, defaultdict
# from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product, permutations
# from heapq import heapify, heappop, heappush
# import numpy as np    # cumsum
# from bisect import bisect_left, bisect_right

def solve():
    N, X = MI()
    L = LI()

    A = [0] * (N+1)
    for i in range(1, N+1):
        A[i] = L[i-1] + A[i-1]
    ans = 0
    for a in A:
        if a > X:
            break
        ans += 1
    print(ans)

def __starting_point():
    solve()

__starting_point()


n,x=map(int,input().split())
l=list(map(int,input().split()))
list_d=[0]
ans=0
for i in range(1,n+1):
    list_d.append(list_d[i-1]+l[i-1])
for i in list_d:
    if i<=x:
        ans+=1
print(ans)


N, X = map(int, input().split())
L = list(map(int, input().split()))

bound = 1
d = 0
for i in L:
    d += i
    if d <= X:
        bound += 1
print(bound)


n,x = map(int, input().split())
l = list(map(int, input().split()))

ans = 1
t = 0
for i in l:
  t += i
  if t <= x: ans += 1
  if t >= x: break
print(ans)


n, x = list(map(int, input().split()))
l = [int(i) for i in input().split()]
p = 0
for i in range(n):
    p += l[i]
    if p > x:
        print((i + 1))
        break
else:
    print((n + 1))


n, x = map(int, input().split())
L = list(map(int, input().split()))

cnt = 0
pos = 0
for itr, l in enumerate(L):
  pos += l
  if pos > x:
    print(itr + 1)
    return

print(n + 1)


N,X=list(map(int,input().split()))
L=list(map(int,input().split()))
S=[0]
sum_L=0
for i in range(N):
  sum_L+=L[i]
  S.append(sum_L)
counter=0
for i in range(N+1):
  if S[i]<=X:
    counter+=1
print(counter)


n,x=map(int,input().split())
l=list(map(int,input().split()))
d=[0]
count=0
for i in range(n):
    d.append(d[i]+l[i])
for c in d:
    if c<=x:
        count+=1
print(count)


N,X = list(map(int,input().split()))
Li = list(map(int,input().split()))

Di = 0
count = 1
for i in Li:
    Di = Di + i
    if Di <= X:
        count += 1
    else:
        break
print(count)


n, x = map(int,input().split())
l = list(map(int,input().split()))
ans = 0
count = 1
for i in range(n):
  ans += l[i]
  if ans <= x:
    count += 1
print(count)


N,X = map(int,input().split())
L = list(map(int,input().split()))
kyori = 0

for i in range(N):
    kyori += L[i]
    if kyori > X:
        print(i+1)
        return

print(N+1)


N,X = list(map(int,input().split()))
L = list(map(int,input().split()))
List = [0]*(N+1)
for i in range(1,N+1):
    List[i] = List[i-1] + L[i-1]
import bisect
# A = [i for i in List if i <=X]
# print(len(A))
print((bisect.bisect_left(List,X+1)))


n, x = map(int, input().split())
a = list(map(int, input().split()))
s = 0
t = 1
for i in range(n):
  s = s + a[i]
  if s <= x:
    t += 1
print(t)


n,x=map(int,input().split())
L=list(map(int,input().split()))
ans=0
ct=1
for i in range(n):
  ans=ans+L[i]
  if ans>x:
    break
  else:
    ct+=1
print(ct)


n,x = map(int,input().split())
l = list(map(int,input().split()))
cnt = 0
for i in range(n+1):
    #print(sum(l[:i]))
    if sum(l[:i])<=x:
        cnt += 1
print(cnt)


# nは跳ねる回数　d1 = 1 dn+1 = dn + ln

n,x = list(map(int,input().split()))
l = list(map(int,input().split()))

d = 0
count = 1

for y in range(n):
  d = d + l[y]
  if d <= x:
    count += 1
  else:
    break

print(count)


n, x = (int(i) for i in input().split())
list_l = [int(j) for j in input().split()]
tmp = 0
count = 0
for i in range(0, n + 1):
    if i == 0:
        pass
    else:
        tmp += list_l[i - 1]
        if tmp > x:
            break
    count += 1
print(count)


n, x = map(int, input().split())
llist = list(map(int, input().split()))
dlist = [0]
d = 0
for i in llist:
    d += i
    dlist.append(d)
print(sum([i<=x for i in dlist]))


n,x = map(int,input().split())
l = list(map(int,input().split()))
dist = [0]*(n+1)
for i in range(1,n+1):
    dist[i] = dist[i-1] + l[i-1]

cnt = 0
for i in range(n+1):
    if dist[i]<= x:
        cnt += 1
print(cnt)


N,X = map(int,input().split())
L_List = list(map(int,input().split()))
ans = 0
ct = 1
for i in range(N):
    ans += L_List[i]
    if ans <= X:
        ct += 1
    else:
        break

print(ct)


N,X = map(int,input().split())
L = [int(i) for i in input().split()]
D = [0]

for i in range(N):
    a = D[i]+L[i]
    if(a <= X):
        D.append(a)
    else:
        break

print(len(D))


n, x = map(int, input().split())
L = list(map(int, input().split()))
val = 0
count = 1

for l in L:
    val += l
    if val <= x:
        count += 1
print(count)


N,X = map(int,input().split())
L = list(map(int,input().split()))
List = [0]*(N+1)
for i in range(1,N+1):
    List[i] = List[i-1] + L[i-1]
A = [i for i in List if i <=X]
print(len(A))


n, x = map(int, input().split())
l = list(map(int, input().split()))
ball = 0
c = 1
for i in range(n):
    ball += l[i]
    if ball <= x:
        c += 1
    else:
        break
print(c)


N, X = list(map(int, input().split()))
L = list(map(int, input().split()))
cnt = 1
total = 0
for i in range(N):
  total += L[i]
  if total <= X:
    cnt += 1
  else:
    break
print(cnt)


N,X=map(int,input().split())
L=list(map(int,input().split()))
l=[0]
s=0
for i in range(N):
   s+=L[i]
   if  s>X:
       break
   l.append(s)
print(len(l))


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

l = list(map(int, input().split()))
ans = 1
p = 0
for i in range(n):
    p += l[i]
    if (p > x):
        break
    else:
        ans += 1

print(ans)


N,X=list(map(int,input().split()))
ls1=list(map(int,input().split()))

x = 0
ii = 1
for i in ls1:
    x = x + i
    if x > X:
        break
    ii += 1
print(ii)


n,x = map(int,input().split())
l = list(map(int,input().split()))
ans = 1
d = [0]*(n+1)
for i in range(1,n+1):
  d[i] = d[i-1]+l[i-1]
  if d[i] <= x:
    ans += 1
print(ans)


n, x = list(map(int, input().split()))
arr = list(map(int, input().split()))
if x == 0:
    print((0))
    return
sum = 0
count = 1
i = 0
while i < n:
    sum += arr[i]
    i += 1
    if sum <= x:
        count += 1
    else:
        print(count)
        return
print(count)


n, x = map(int,input().split())

num_list = list(map(int,input().split()))
num_sum = 0
count = 1

for i in range(n):
    num_sum += num_list[i]
    if num_sum>x:
        break
    count += 1
print(count)


n, x = list(map(int, input().split()))
L = list(map(int, input().split()))
dist = 0
res = 0
for i, l in enumerate(L, 1):
    dist += l
    if dist > x:
        print(i)
        return
    elif dist == x:
        print((i + 1))
        return
print((n + 1))


N,X = map(int,input().split())
L = list(map(int,input().split()))

D = []
D.append(0)

for i in range(0,N):
  D.append(D[i]+L[i])

cnt = 0
for d in D:
  if d <= X:
    cnt += 1

print(cnt)


N, X = map(int, input().split())
L = list(map(int, input().split()))
ans = 1
total = 0
for l in L:
    total += l
    if total > X: break
    ans += 1
print(ans)


a,b=map(int,input().split())
l=list(map(int,input().split()))
c=[0]
count=1
for i in range(a):
    c.append(c[i]+l[i])
    if(c[i+1]<=b):
        count+=1
print(count)


N, X = list(map(int, input().split()))
L = list(map(int, input().split()))

num = 1
s = 0
for i in range(N):
  s += L[i]
  if s > X: break
  num += 1

print(num)


from typing import List

def next_coordinates(d: int, l: int) -> int:
    return d + l

def answer(n: int, x: int, l: List[int]) -> int:
    d = 0
    count = 1
    for i in l:
        d = next_coordinates(d, i)
        if x < d:
            return count
        count += 1

    return count

def main():
    n, x = map(int, input().split())
    l = list(map(int, input().split()))
    print(answer(n, x, l))

def __starting_point():
    main()
__starting_point()


import itertools
import bisect

n, x = map(int, input().split())
ll = list(map(int, input().split()))

dl = [0] + list(itertools.accumulate(ll))

print(bisect.bisect_right(dl, x))


n,x = list(map(int, input().split()))
a = list(map(int, input().split()))
cnt = 1
total = 0

for i in range(n):
    total += a[i]
    if total <= x:
        cnt += 1
print(cnt)


n,x = map(int,input().split())
li = list(map(int,input().split()))
lis = []
sum = 0
for i in range(n):
    sum += li[i]
    lis.append(sum)

cnt = 0
for i in lis:
    if i <= x:
        cnt += 1

print(cnt+1)


n, x = map(int, input().split())
from itertools import accumulate
L =[0] +  list(map(int, input().split()))
acc = list(accumulate(L))
print(sum([1 for i in acc if i <=x]))


A,B=list(map(int,input().split(' ')))
l=list(map(int,input().split(' ')))
ans=0
count=1
for i in l:
    ans+=i
    if ans<=B:
        count+=1
print(count)


N,X=map(int,input().split())
L=list(map(int, input().split()))

D=0

for i in range(N):
	D+=L[i]
	if D>X:
		print(i+1)
		return

print(i+2)


N, X = map(int, input().split())
L = list(map(int, input().split()))

D = 0
cnt = 1
for l in L:
    D = D + l
    if D > X:
        break
    cnt +=1

print(cnt)


n,x = map(int,input().split())
l = list(map(int,input().split()))
wa = 0
d = [0]*(n+3)
d[1]=0
d.pop(0)
wa = 1
for i in range(2,n+2):
  d[i]=d[i-1]+l[i-2]
  if(d[i]>x):
    break
  wa+=1
print(wa)


N, X = [int(i) for i in input().split()]
LS = [int(i) for i in input().split()]

s = 0
cnt = 1

for l in LS:
  s += l
  if s <= X:
    cnt += 1
  else:
    break

print(cnt)


n, x = map(int, input().split())
l = list(map(int, input().split()))
a = 1
d = 0
for i in range(n):
    d += l[i]
    if d <= x:
        a += 1
print(a)


N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
C = [int(i) for i in input().split()]
ans = 0
for i in range(N):
    ans += B[A[i]-1]
    if(A[i] == A[i-1]+1 and i != 0):
        ans += C[A[i]-2]
print(ans)


N = int(input())
A,B,C = [list(map(int, input().split())) for _ in range(3)]

ans = 0

for i in range(N-1):
    if A[i+1] == A[i] + 1:
        ans += C[A[i]-1]

print(sum(B) + ans)


N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))

S = 0
for i in range(N-1):
    if A[i+1] == A[i] + 1:
        S += B[A[i]-1] + C[A[i]-1]
    else:
        S += B[A[i]-1]

S += B[A[N-1]-1]
print(S)


n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))
ans=sum(b)

for i in range(n-1):
    if a[i]+1==a[i+1]:
        ans+=c[a[i]-1]

print(ans)


n = int(input())

an = list(map(int,input().split()))
bn = list(map(int,input().split()))
cn = list(map(int,input().split()))
sat = sum(bn)

for x in range(n-1):
    if an[x+1] == an[x] + 1:
        sat += cn[an[x]-1]

print(sat)


N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))
ans = 0
for i in range(N):
    ans += B[A[i]-1]
    if i > 0 and A[i] == A[i-1] + 1:
        ans += C[A[i]-2]
print(ans)


n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))
ans=sum(b)
for i in range(n-1):
    if a[i+1]-a[i]==1:
        ans+=c[a[i]-1]
print(ans)


N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))

ans = sum(B)
for i in range(len(A)-1):
    if A[i] + 1 == A[i+1]:
        ans += C[A[i]-1]

print(ans)


N = int(input())
lsA = list(map(int,input().split()))
lsB = [0]+list(map(int,input().split()))
lsC = [0]+list(map(int,input().split()))
sumB = sum(lsB)
for i in range(N-1):
    if lsA[i]+1 == lsA[i+1]:
        sumB += lsC[lsA[i]]
print(sumB)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))

ans = 0
for i in range(n):
    num = a[i] - 1
    ans += b[num]

    if i == 0:
        continue
    elif a[i - 1] + 1 == a[i]:
        ans += c[num - 1]

print(ans)


N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
C=list(map(int,input().split()))
x=sum(B)
for i in range(N-1):
    if A[i]+1==A[i+1]:
        x+=C[A[i]-1]
print(x)


n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = list(map(int,input().split()))
ans = 0
for i in range(n-1):
    if a[i]+1==a[i+1]:
        ans += c[a[i]-1]
ans += sum(b)
print(ans)


N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
C=list(map(int,input().split()))
s=0
for i in range(N):
    s+=B[A[i]-1]
    if A[i]-A[i-1]==1 and i>0:
        s+=C[A[i]-2]
print(s)


n = int(input())
a = [int(s) for s in input().split()]
b = [int(s) for s in input().split()]
c = [int(s) for s in input().split()]

ans = b[a[0] - 1]

for i in range(1, n):
    ans += b[a[i] - 1]
    if a[i] == a[i - 1] + 1:
        ans += c[a[i] - 2]

print(ans)


def L():
	return list(map(int, input().split()))

N = int(input())
A = L()
B = L()
C = L()
O = 0

for i in range(N):
	ai = A[i] - 1
	O += B[ai]
	if i > 0 and A[i] == A[i - 1] + 1:
		O += C[ai - 1]

print(O)


n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = list(map(int,input().split()))

for i in range(n):
  if (i == 0):
    ans =b[a[0] - 1]
    y = a[0]
  else:
    x = a[i]
    if (x - y == 1):
      ans = ans + b[x - 1] + c[y - 1]
    else:
      ans = ans + b[x - 1]
    y = x

print(ans)


n = int(input())
a = list(map(int, input().split(' ')))
b = list(map(int, input().split(' ')))
c = list(map(int, input().split(' ')))

ans = 0
for i in range(0, n):
    ans += b[a[i] - 1]
for i in range(0, n - 1):
    if a[i + 1] == a[i] + 1:
        ans += c[a[i] - 1]
print(ans)


n=int(input())
a=list(map(lambda x:int(x)-1, input().split()))
ans=sum(list(map(int,input().split())))
c=list(map(int,input().split()))

for i in range(n-1):
  if a[i]+1==a[i+1]:
    ans+=c[a[i]]

print(ans)


n= int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))

ans=0

for i in range(0,n):
    ans += b[a[i]-1]

    if i>=1:
        if a[i]==a[i-1]+1:

            ans+=c[a[i]-2]

print(ans)


n=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
C=list(map(int,input().split()))
ans=0
for i in range(n):
  ans += B[A[i]-1]
  if i<n-1 and A[i]+1 == A[i+1]:
    ans += C[A[i]-1]
print(ans)


N = int(input())
Alst = list(map(int, input().split()))
Blst = list(map(int, input().split()))
Clst = list(map(int, input().split()))

ans = sum(Blst)

for i in range(N-1):
    if Alst[i] + 1 == Alst[i+1]:
        ans += Clst[Alst[i]-1]

print(ans)


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

# 配列の添字をNと合わせる
B.insert(0,0)
C.insert(0,0)
satis = 0
tmp = 999
for i in range(N):
    satis += B[A[i]]
    if (tmp - A[i]) == -1:
        satis += C[tmp]
    tmp = A[i] #i+1かどうかを判定
print(satis)


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

s = 0
for i in range(N):
    s += B[A[i] - 1]
    if i < N - 1:
        if A[i] + 1 == A[i + 1]:
            s += C[A[i] - 1]

print(s)


from typing import List

def answer(n: int, a: List[int], b: List[int], c: List[int]) -> int:
    satisfaction = sum(b)
    for i in range(1, n):
        previous = a[i - 1]
        if a[i] == previous + 1:
            satisfaction += c[previous - 1]

    return satisfaction

def main():
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    c = list(map(int, input().split()))
    print((answer(n, a, b, c)))

def __starting_point():
    main()

__starting_point()


n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
c = [int(i) for i in input().split()]
ans = b[a[-1]-1]
for i in range(n-1):
  ans += b[a[i]-1]
  if a[i] + 1 == a[i+1]:
    ans += c[a[i]-1]
print(ans)


N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))

ans = 0
pre_a = -1

for a in A:
    ans += B[a-1]
    if a == pre_a + 1:
        ans += C[a-2]
    pre_a = a

print(ans)


n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))

ans=0
for i in range(n-1):
  if a[i+1]-a[i]==1:
    ans+=c[a[i]-1]

print(ans+sum(b))


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

ans = B[A[0]-1]
for i in range(1, N):
    ans += B[A[i]-1]
    if A[i]-A[i-1] == 1:
        ans += C[A[i-1]-1]

print(ans)


N = int(input())

A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

ans = 0

for i in range(N):
  ans += B[A[i]-1]

for j in range(0, N-1):
  if A[j+1] == A[j] + 1:
    ans = ans + C[A[j]-1]

print(ans)


N = int(input())
liA = list(map(int, input().split()))
liB = list(map(int, input().split()))
liC = list(map(int, input().split()))

Manzoku = sum(liB)

for i in range(N):
    if i < N - 1:
        if liA[i+1] - liA[i] == 1:
            Manzoku += liC[liA[i] - 1]

print((Manzoku))


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
ans = 0
for i in range(N):
  x = A[i] - 1
  ans += B[x]
  if i >= 1 and A[i] == A[i-1] + 1:
    ans += C[A[i-1]-1]
print(ans)


N = int(input())
A = list((int(x) for x in input().split()))
B = list((int(x) for x in input().split()))
C = list((int(x) for x in input().split()))
bonus = 0
for i in range(N-1):
  if A[i]+1 == A[i+1]:
    bonus += C[A[i]-1]
print(sum(B)+bonus)


n = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))
count = sum(B)
for i in range(n-1):
  if A[i+1]-A[i]==1:
    count+=C[A[i]-1]
print(count)


n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = list(map(int,input().split()))
ans = 0
for i in range(n):
    ans += b[a[i] - 1]
    if i != 0:
        if a[i] == a[i - 1] + 1:
            ans += c[a[i] - 2]
print(ans)


N=int(input())
A=list(map(int, input().split()))
B=list(map(int, input().split()))
C=list(map(int, input().split()))

ans=0

for i in range(N-1):
	now=A[i]
	next=A[i+1]
	ans+=B[now-1]
	if next==now+1:
		ans+=C[now-1]

ans+=B[A[N-1]-1]

print(ans)


n = int(input())
list_a = [int(i) for i in input().split()]
list_b = [int(j) for j in input().split()]
list_c = [int(k) for k in input().split()]
satisfied = 0
for i in range(0, len(list_a)):
    satisfied += list_b[list_a[i] - 1]
    if i > 0 and list_a[i] == list_a[i - 1] + 1:
        satisfied += list_c[list_a[i - 1] - 1]
print(satisfied)


n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
ans = 0

for i in range(n):
  ans += B[A[i]-1]
  if(i > 0 and A[i] == A[i-1]+1):
    ans += C[A[i]-2]

print(ans)


n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
res = 0
for i in range(n - 1):
    res += B[i]
    if A[i] + 1 == A[i + 1]:
        res += C[A[i] - 1]
print((res + B[-1]))


N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))

count = 0
for j in A:
    count += B[j-1]

#print(count)

for z in range(N-1):
    if A[z+1]- A[z] == 1:
        zz = A[z]
        count += C[zz-1]

print(count)


n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))
ans=0
for i in range(len(a)):
  ai=a[i]
  ans+=b[ai-1]
  if(ans!=b[ai-1]):
    if(ai==before+1):
      ans+=c[before-1]
  before=ai
print(ans)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
ans = 0

for i in range(n):
  ans += b[i]
for j in range(n-1):
  if a[j] == a[j+1] -1:
    ans += c[(a[j]-1)]

print(ans)


# 問題：https://atcoder.jp/contests/abc140/tasks/abc140_a

n = int(input())
a = list(map(int, input().strip().split()))
b = list(map(int, input().strip().split()))
c = list(map(int, input().strip().split()))

res = b[a[0]-1]
for i in range(1, n):
    res += b[a[i]-1]
    if a[i] - a[i-1] == 1:
        res += c[a[i-1]-1]

print(res)


N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))
ans = 0
cnt = 100
for i in range(N):
  if cnt + 1 == A[i]:
    ans += C[cnt-1]
  cnt = A[i]
  ans += B[A[i]-1]

print(ans)


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

# 配列の添字をNと合わせる
B.insert(0,0)
C.insert(0,0)
satis = 0
tmp = 999
for i in range(N):
    satis += B[A[i]]
    if (tmp - A[i]) == -1:
        satis += C[tmp]
    tmp = A[i] #i+1かどうかを判定
print(satis)


n=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
C=list(map(int,input().split()))
d=sum(B)
for i in range(n-1):
  if A[i+1]-A[i]==1:
    d+=C[A[i]-1]
print(d)


n = int(input())
a,b,c = [list(map(int,input().split())) for i in range(3)]
x = 0
d = -10
for i in range(n):
    y = b[a[i]-1]
    x += y
    if a[i]-1 == d:
        x+= c[d-1]
    d = a[i]
print(x)


n = int(input())
a = list(map(int, input().split()))
b = sum(map(int, input().split()))
c = list(map(int, input().split()))
s = 0
for i in range(n-1):
  if a[i+1] - a[i] == 1:
    s += c[a[i] - 1]
print(b + s)


n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))
ans=0
for i in range(n):
    ans+=b[a[i]-1]
    if i!=0:
        if a[i]==a[i-1]+1:
            ans+=c[a[i]-2]
print(ans)


n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))

addans=0

for i in range(n-1) :
  if a[i]+1 == a[i+1] :
    addans += c[a[i]-1]
print(sum(b)+addans)


n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))
s=0
s += sum(b)
for i in range(n-1):
    if a[i]+1==a[i+1]:
        s += c[a[i]-1]
print(s)


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

ans = 0
for i in range(N):
  ans += B[A[i]-1]
  if i < N-1 and A[i+1] - A[i] == 1: ans += C[A[i]-1]

print(ans)


n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))
d=sum(b)
for i in range(n-1):
  if a[i]-a[i+1]==-1:
    d=d+c[a[i]-1]
print(d)


n=int(input())

a_list=[int(i) for i in input().split()]
b_list=[int(i) for i in input().split()]
c_list=[int(i) for i in input().split()]

satisfaction=0

for i in range(n):
    satisfaction+=b_list[a_list[i]-1]
    if i<n-1:

        if a_list[i+1]==a_list[i]+1:
            satisfaction+=c_list[a_list[i]-1]

print(satisfaction)


n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))

cnt=0

for i in range(n):
  d=a[i]-1
  cnt+=b[d]

for j in range(n-1):
  if a[j]+1==a[j+1]:
    cnt+=c[a[j]-1]

print(cnt)


def resolve():
    n = int(input())
    a = tuple(map(int, input().split()))
    b = tuple(map(int, input().split()))
    c = tuple(map(int, input().split()))
    before_dinner = a[0]
    ans = sum(b)
    for i in range(1,n):
        if a[i] - before_dinner==1:
            ans += c[before_dinner-1]
        before_dinner = a[i]
    print(ans)
resolve()


N=int(input())
d=[[int(s) for s in input().split()] for i in range(3)]
ans=0
for i in range(N):
    ans+=d[1][d[0][i]-1]
    if i<=N-2 and d[0][i+1]-d[0][i]==1:
        ans+=d[2][d[0][i]-1]
print(ans)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
food = 0
manzoku = 0
bonus = 0
before = 10*3
ans = 0
for i in range(n):
    food = a[i]
    manzoku = b[food-1]
    if food == before+1:
        bonus = c[food-2]
        ans += manzoku + bonus
    else:
        ans += manzoku
    before = food
print(ans)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
m = -1
num = sum(b)
for i in a:
    if i == m+1:
        num += c[m-1]
    m = i
print(num)


i = int(input())

a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = list(map(int,input().split()))
res = sum(b)

for j in range(i-1):
    if a[j] + 1 == a[j+1]:
      res += c[a[j] - 1]
print(res)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
s = sum(b)
for i in range(n-1):
  if a[i]+1 == a[i+1]:
    s += c[a[i]-1]
print(s)


N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))
res = 0
for x in range(N-1):
  res += B[x]
  if A[x] + 1 == A[x+1]:
    res += C[A[x] - 1]
print(res + B[-1])


# ABC140
# Buffet
n = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))
ct = 0
h = 25
for i in range(n):
    ct += B[i]
    if h + 1 == A[i]:
        ct += C[h - 1]
        h = A[i]
    else:
        h = A[i]
print(ct)


N=int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))
S = 0

for i in range(N):
    S += B[A[i]-1]

for i in range(N-1):
    if A[i]-A[i+1] == -1:
        S += C[A[i]-1]
print(S)


n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))
ans=0
for i in range(n):
    ans+=b[a[i]-1]
    if i>0:
        if a[i]==a[i-1]+1:
            ans+=c[a[i-1]-1]
print(ans)


n=int(input())
ans=0
l=[list(map(int,input().split())) for i in range(3)]
for i in range(1,n+1):
    ans+=l[1][l[0][i-1]-1]
    if i!=n and l[0][i]==l[0][i-1]+1:
        ans+=l[2][l[0][i-1]-1]
print(ans)


n=int(input());ans=0
a=[*map(int,input().split())]
b={i+1:j for i,j in enumerate(map(int,input().split()))}
c={i+1:j for i,j in enumerate(map(int,input().split()))}
ans+=b[a[0]]
for i in range(1,n):
  if a[i]-(a[i-1])==1:
    ans+=c[a[i-1]]
  ans+=b[a[i]]
print(ans)


N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
C=list(map(int,input().split()))
s=0
for i in range(N):
  s+=B[A[i]-1]
  if i!=0 and A[i-1]+1==A[i]:
    s+=C[A[i]-2]
print(s)


n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))
eat_bef=0
ans=0
for i in range(len(a)):
    ans+=b[a[i]-1]
    if i>=1:
        if eat_bef+1==a[i]:
            ans+=c[eat_bef-1]
    eat_bef=a[i]
print(ans)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))

ans = sum(b)
for i in range(n-1):
    if a[i]+1 == a[i+1]:
        ans += c[a[i]-1]
print(ans)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))

ans = sum(b)
for i in range(n-1):
    if a[i+1] - a[i] == 1:
        ans += c[a[i]-1]
print(ans)


n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = list(map(int,input().split()))
ans = sum(b)
for i in range(1,n):
  if a[i-1]+1 == a[i]:
    ans += c[a[i-1]-1]
print(ans)


def main():
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    c = list(map(int, input().split()))
    point = sum(b)

    for i in range(1,n):
        if a[i-1] +1 == a[i]:
            point += c[a[i-1]-1]

    print(point)

def __starting_point():
    main()
__starting_point()


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

ans = sum(B)
for i in range(N-1):
  if A[i]+1 == A[i+1]:
    ans += C[A[i]-1]
print(ans)


def main():
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    c = list(map(int, input().split()))
    total_score = sum(b)

    for i in range(1, n):
        prev = a[i - 1]
        if prev + 1 == a[i]:
            total_score += c[prev - 1]

    print(total_score)

def __starting_point():
    main()

__starting_point()


N = int(input())
A = list(map(int, input().split()))
A = list([x - 1 for x in A])
B = list(map(int, input().split()))
C = list(map(int, input().split()))
cnt = 0

for i in range(N):
    cnt += B[A[i]]
    if A[i] == A[i-1] + 1 and i != 0:
        cnt += C[A[i-1]]
print(cnt)


N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
C=list(map(int,input().split()))

ans=B[A[0]-1]
mae=A[0]
for i in range(1,N):
    ans+=B[A[i]-1]
    if A[i]-mae==1:
        ans+=C[mae-1]
    mae=A[i]

print(ans)


N = int(input())
A = [0] + list(map(int, input().split()))
B = [0] + list(map(int, input().split()))
C = [0] + list(map(int, input().split()))
ans = 0
for i in range(1, N+1):
    ans += B[A[i]]
    if i > 1:
        if A[i] == A[i-1]+1:
            ans += C[A[i-1]]
print(ans)


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

ans = 0
for i in range(N):
  ans += B[A[i]-1]
  if i > 0 and A[i] - A[i-1] == 1: ans += C[A[i-1]-1]

print(ans)


n = int(input())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
c = [int(x) for x in input().split()]
res = b[a[0]-1]
for i in range(1,n):
  #print(i,res,a[i])
  res += b[a[i]-1]
  if a[i-1] + 1 == a[i]:
    res += c[a[i-1]-1]
print(res)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
d = 0
for i in range(n - 1):
    if a[i] + 1 == a[i + 1]:
        d += c[a[i] - 1]
print(d + sum(b))


n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

sumB = sum(B)

cnt = []

for i in range(n-1):
    if A[i]+1 == A[i+1]:
        cnt.append(A[i]-1)

sumC = 0
for i in cnt:
    sumC += C[i]

print((sumB+sumC))


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

ans = 0
num = 0
for i in A:
    ans += B[i-1]
    if num != 0 and i - num == 1:
        ans += C[i-2]
    num = i

print(ans)


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

ans = 0
for i in range(N):
    ans += B[A[i] - 1]
    if i>=1 and A[i] - A[i-1] == 1:
        ans += C[A[i-1] - 1]

print(ans)


N = int(input())
A_List = list(map(int,input().split()))
B_List = list(map(int,input().split()))
C_List = list(map(int,input().split()))
ans = 0

for i in range(len(A_List)):
    ans += B_List[A_List[i] - 1]
    if (i != 0) & (A_List[i] - A_List[i-1] == 1):
        ans += C_List[A_List[i-1]-1]
print(ans)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
res = b[-1]
for i in range(n - 1):
    res += b[i]
    if a[i] + 1 == a[i + 1]:
        res += c[a[i] - 1]
print(res)


def mapt(fn, *args):
    return tuple(map(fn, *args))

def Input():
    return mapt(int, input().split(" "))

def main():
    n = int(input())
    a = Input()
    b = Input()
    c = Input()
    ans = 0
    for i in range(n-1):
        if a[i+1] - a[i] == 1:
            ans += c[a[i]-1]
    ans += sum(b)
    print(ans)
main()


N = int(input())
dishes = list(map(int, input().split()))
satisfaction = list(map(int, input().split()))
additional = list(map(int, input().split()))

res = 0

for i in range(N-1):
    res += satisfaction[i]

    if dishes[i] + 1 == dishes[i+1]:
        res += additional[dishes[i] - 1]

print((res + satisfaction[-1]))


kind = int(input())

table_1 = input().split(' ')
table_2 = input().split(' ')
table_3 = input().split(' ')
total = 0
for i in range(kind):
    total += int(table_2[i])

for j in range(kind-1):
    if int(table_1[j]) == int(table_1[j + 1]) - 1:
        total += int(table_3[int(table_1[j]) - 1])
print(total)


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

ans = 0
prev = -1
for a in A:
    ans += B[a - 1]
    if prev == a - 1:
        ans += C[prev - 1]
    prev = a
print(ans)


n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = list(map(int,input().split()))
su = 0
n1 = len(a)
for i in a:
    su += b[i-1]

for i in range(n1-1):
    if a[i] == a[i+1] -1:
        su += c[a[i]-1]

print(su)


n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

ans = 0
x = -1

for i in A:
    ans += B[i-1]
    if x+1 == i:
        ans += C[x-1]
    x = i

print(ans)


N = int(input())
A = [int(n) for n in input().split()]
B = [int(n) for n in input().split()]
C = [int(n) for n in input().split()]

ans = sum(B)
for i in range(N-1):
    if A[i+1] - A[i] == 1:
        ans += C[A[i]-1]

print(ans)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))

ans = 0
i = -2
for j in range(n):
    if a[j]-1 == i+1:
        ans += c[i]
    i = a[j] - 1
    ans += b[i]
print(ans)


N = int(input())

A =list(map(int,input().split()))
B =list(map(int,input().split()))
C =list(map(int,input().split()))

b=sum(B)

for i in range (N-1):
    if (A[i]+1==A[i+1]):
        b=b+C[A[i]-1]

print(b)


n=int(input())
a=list(map(int, input().split()))
b=list(map(int, input().split()))
c=list(map(int, input().split()))
satisfy=0
for i in range(n):
  satisfy+=b[a[i]-1]
  if i>=1 and a[i-1]+1==a[i]:
    satisfy+=c[a[i]-2]
print(satisfy)


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

def main():
    score=0
    for i in range(N):
        score += B[A[i]-1]
        if A[i] == A[i-1] + 1 and i>=1:
            score += C[A[i-1]-1]

    return score
print(main())


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
ans = B[A[0]-1]

for i in range(1, N):
  ans += B[A[i]-1]
  if A[i] == A[i-1] + 1:
    ans += C[A[i-1]-1]
print(ans)


import sys

n = int(input())
a = [int(t) for t in input().split(' ')]
mx = [[] for _ in range(n)]

lines = sys.stdin.readlines()
for i in range(n-1):
    v1, v2 = (int(t) - 1 for t in lines[i].split(' '))
    mx[v1].append(v2)
    mx[v2].append(v1)

count = [[0, 0] for _ in range(n)]

total = [a.count(1), a.count(2)]
answer = 0

OBSERVE = 0
CHECK = 1

stack = [(OBSERVE, 0, -1)]
while len(stack):
    state, v, from_ = stack.pop()
    if state == OBSERVE:
        stack.append((CHECK, v, from_))
        for nv in mx[v]:
            if nv != from_:
                stack.append((OBSERVE, nv, v))
    else:
        for nv in mx[v]:
            if nv != from_:
                if count[nv][0] == total[0] and count[nv][1] == 0 or count[nv][1] == total[1] and count[nv][0] == 0:
                    answer += 1
                count[v][0] += count[nv][0]
                count[v][1] += count[nv][1]

        if a[v] != 0:
            count[v][a[v]-1] += 1

print(answer)


import sys
from collections import defaultdict
n = int(input())
#n,k = [int(__) for __ in raw_input().split()]
arr = [int(__) for __ in input().split()]
sactive = set()
sactive.add(0)
nums = [0] * n
d1 = defaultdict(set)
d2 = defaultdict(set)
d = defaultdict(set)
lines = sys.stdin.readlines()
for i in range(n-1):
    sactive.add(i+1)
    s,f = [int(__) for __ in lines[i].strip().split()]
    s -= 1
    f -= 1
    d[s].add(f)
    d[f].add(s)
    nums[f] += 1
    nums[s] += 1

leaves = set()

for i in range(n):
    if nums[i] == 1:
        leaves.add(i)
while len(leaves):
    x = leaves.pop()
    if arr[x] == 0:
        sactive.remove(x)
        nums[x] -= 1
        targ = d[x].pop()
        nums[targ] -= 1
        d[targ].remove(x)
        if nums[targ] == 1:
            leaves.add(targ)

sactive1 = sactive.copy()
for targ in d:
    d1[targ] = d[targ].copy()
nums1 = nums[:]
nums2 = nums[:]

for i in range(n):
    if nums1[i] == 1:
        leaves.add(i)
while len(leaves):
    x = leaves.pop()
    if arr[x] != 1:
        sactive1.remove(x)
        nums1[x] -= 1
        targ = d1[x].pop()
        nums1[targ] -= 1
        d1[targ].remove(x)
        if nums1[targ] == 1:
            leaves.add(targ)

sactive2 = sactive.copy()
for targ in d:
    d2[targ] = d[targ].copy()

for i in range(n):
    if nums2[i] == 1:
        leaves.add(i)
while len(leaves):
    x = leaves.pop()
    if arr[x] != 2:
        sactive2.remove(x)
        nums2[x] -= 1
        targ = d2[x].pop()
        nums2[targ] -= 1
        d2[targ].remove(x)
        if nums2[targ] == 1:
            leaves.add(targ)

if len(sactive1 & sactive2) > 0:
    print(0)
else:
    print(len(sactive) - len(sactive1) - len(sactive2) + 1)
#print(nums)
#print('both',sactive)
#print('1',sactive1)
#print('2',sactive2)
#print(d)


import sys
input=sys.stdin.readline
n = int(input())
a = [int(t) for t in input().split(' ')]
mx = [[] for _ in range(n)]
for i in range(n-1):
    v1, v2 = list(map(int,input().split()))
    mx[v1-1].append(v2-1)
    mx[v2-1].append(v1-1)
count = [[0, 0] for _ in range(n)]
total = [a.count(1), a.count(2)]
answer = 0
OBSERVE = 0
CHECK = 1
stack = [(OBSERVE, 0, -1)]
while len(stack):
    #print(stack,count)
    state, vertex, parent = stack.pop()
    if state == OBSERVE:
        stack.append((CHECK, vertex, parent))
        for child in mx[vertex]:
            #print(nv,v,from_)
            if child != parent:
                stack.append((OBSERVE, child, vertex))
    else:
        for child in mx[vertex]:
            if child != parent:
                #print(child,parent,count)
                if count[child][0] == total[0] and count[child][1] == 0 or count[child][1] == total[1] and count[child][0] == 0:
                    answer += 1
                count[vertex][0] += count[child][0]
                count[vertex][1] += count[child][1]

        if a[vertex] != 0:
            #print(count)
            count[vertex][a[vertex]-1] += 1
            #print(count)

print(answer)


from collections import deque
import sys
input = sys.stdin.readline

class Graph(object):
	"""docstring for Graph"""
	def __init__(self,n,d): # Number of nodes and d is True if directed
		self.n = n
		self.graph = [[] for i in range(n)]
		self.parent = [-1 for i in range(n)]
		self.directed = d

	def addEdge(self,x,y):
		self.graph[x].append(y)
		if not self.directed:
			self.graph[y].append(x)

	def bfs(self, root): # NORMAL BFS
		self.parent = [-1 for i in range(self.n)]
		queue = [root]
		queue = deque(queue)
		vis = [0]*self.n
		while len(queue)!=0:
			element = queue.popleft()
			vis[element] = 1
			for i in self.graph[element]:
				if vis[i]==0:
					queue.append(i)
					self.parent[i] = element

	def dfs(self, root, ans): # Iterative DFS
		stack=[root]
		vis=[0]*self.n
		stack2=[]
		while len(stack)!=0: # INITIAL TRAVERSAL
			element = stack.pop()
			if vis[element]:
				continue
			vis[element] = 1
			stack2.append(element)
			for i in self.graph[element]:
				if vis[i]==0:
					self.parent[i] = element
					stack.append(i)

		while len(stack2)!=0: # BACKTRACING. Modify the loop according to the question
			element = stack2.pop()
			m = [0,0]
			for i in self.graph[element]:
				if i!=self.parent[element]:
					m[0] += ans[i][0]
					m[1] += ans[i][1]
			if arr[element] == 1:
				m[0] += 1
			elif arr[element] == 2:
				m[1] += 1
			ans[element] = m
		return ans

	def shortestpath(self, source, dest): # Calculate Shortest Path between two nodes
		self.bfs(source)
		path = [dest]
		while self.parent[path[-1]]!=-1:
			path.append(parent[path[-1]])
		return path[::-1]

	def ifcycle(self):
		self.bfs(0)
		queue = [0]
		vis = [0]*n
		queue = deque(queue)
		while len(queue)!=0:
			element = queue.popleft()
			vis[element] = 1
			for i in graph[element]:
				if vis[i]==1 and i!=parent[element]:
					return True
				if vis[i]==0:
					queue.append(i)
					vis[i] = 1
		return False

	def reroot(self, root, ans):
		stack = [root]
		count = 0
		vis = [0]*self.n
		while len(stack)!=0:
			e = stack[-1]
			# print (e,ans)
			if vis[e]:
				stack.pop()
				if self.parent[e]!=-1:
					ans[self.parent[e]][0] += ans[e][0]
					ans[self.parent[e]][1] += ans[e][1]
					if self.parent[self.parent[e]]!=-1:
						ans[self.parent[e]][0] -= ans[self.parent[self.parent[e]]][0]
						ans[self.parent[e]][1] -= ans[self.parent[self.parent[e]]][1]
				continue
			vis[e]=1
			for i in self.graph[e]:
				if not vis[i]:
					stack.append(i)
			if self.parent[e]==-1:
				continue
			ans[self.parent[e]][0] -= ans[e][0]
			ans[self.parent[e]][1] -= ans[e][1]
			if self.parent[self.parent[e]]!=-1:
				ans[self.parent[e]][0] += ans[self.parent[self.parent[e]]][0]
				ans[self.parent[e]][1] += ans[self.parent[self.parent[e]]][1]
			if 0 in ans[e] and 0 in ans[self.parent[e]]:
				count+=1
		return count

n = int(input())
g = Graph(n,False)
arr = list(map(int,input().split()))
for i in range(n-1):
	x,y = list(map(int,input().split()))
	g.addEdge(x-1,y-1)
ans = [[0,0] for i in range(n)]
a = g.dfs(0,ans)
print(g.reroot(0,a))


d,l = list(map(int,input().split()))

a = list(map(int,input().split()))
letters = list(map(int,input().split()))

pos = [0] * (1+d)

for i,x in enumerate(a):
    pos[i+1] = pos[i] + x

res = []
i = 1
for letter in letters:
    while pos[i]<letter: i+=1
    res.append(str(i) + " " + str(letter - pos[i-1]))
print('\n'.join(res))


n, m = map(int, input().split(' '))
a = list(map(int, input().split(' ')))
b = list(map(int, input().split(' ')))
minus = 0
id = 0
for i in b:
    while 1:
        if i - minus > a[id]:
            minus += a[id]
            id += 1
        else:
            print(id + 1, i - minus)
            break


n, m = map(int, input().split())
hosts = [0]
hosts += list(map(int, input().split()))
for i in range(1, n + 1):
    hosts[i] += hosts[i - 1]
letters = list(map(int, input().split()))
j = 0
for i in range(m):
    while hosts[j] < letters[i]:
        j += 1
    print(j, letters[i] - hosts[j - 1])


n,m=(list(map(int,input().strip().split(' '))))
arr=list((list(map(int,input().strip().split(' ')))))
dp=[arr[0]]
for i in range(1,n):
  dp.append(dp[i-1]+arr[i])
brr=list((list(map(int,input().strip().split(' ')))))
start = 0
for i in range(m):
  x=brr[i]
  for j in range(start,n):
    if(dp[j]>=brr[i]):
      if(j!=0):
        print(j+1,x-dp[j-1])
      else:
        print(j+1,x)
      start = j
      break


from bisect import bisect_left
from itertools import accumulate

n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))

s_a = [0] + list(accumulate(a))
ans = list()

for el in b:
    ind = bisect_left(s_a, el)
    num = el - s_a[ind - 1]

    ans.append(f'{ind} {num}')

print('\n'.join(ans))


n,m = list(map(int,input().split()))
rooms = [int(i) for i in input().split()]
letters = [int(i) for i in input().split()]

room = rooms[0]
roomsum = 0
cnt = 1
for i in range(m):
    if letters[i]<=room:
        print(cnt,letters[i]-roomsum)
    else:
        while letters[i]>room:
            roomsum = room
            room += rooms[cnt]
            cnt += 1
        print(cnt,letters[i]-roomsum)


T = input().split(' ')
n = int(T[0])
m = int(T[1])
P = input().split(' ')
for i in range(len(P)):
    P[i] = int(P[i])
for i in range(1, len(P)):
    P[i] += P[i-1]
Q = input().split(' ')
for i in range(len(Q)):
    Q[i] = int(Q[i])
a = 0
b = 0
c = 0
d = 0
while c < len(Q):
    b = Q[c]
    if b > P[a]:
        d = P[a]
        a+=1
    else:
        print(a+1, end=' ')
        print(b - d)
        c+=1


n,m=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
j=0
mk=a[0]
i=0
while(i!=m):
    if b[i]>mk:
        j+=1
        mk+=a[j]
    else:
        print(j+1,a[j]-(mk-b[i]))
        i+=1


#!/usr/bin/env python3
from sys import stdin, stdout
from bisect import bisect, bisect_left

def rint():
    return list(map(int, stdin.readline().split()))
#lines = stdin.readlines()

n, m = rint()
a = list(rint())
b = list(rint())

c =[]
c.append(a[0])
for i in range(1,n):
    c.append(c[i-1]+a[i])

for i in range(m):
    ii = bisect_left(c, b[i])
    if ii == 0:
        print(1, b[i])
    else:
        print(ii+1,b[i] - c[ii-1])


from itertools import accumulate

def main():
	n, m = [int(_) for _ in input().split()]
	a = [int(_) for _ in input().split()]
	b = [int(_) for _ in input().split()]

	i = 0
	acc = 0
	for x in b:
		while acc + a[i] < x:
			acc += a[i]
			i += 1
		print(i + 1, x - acc)

def __starting_point():
	main()

__starting_point()


n, m = [int(i) for i in input().split()]
rooms = [0] + [int(i) for i in input().split()]
for i in range(2, n + 1):
	rooms[i] += rooms[i - 1]
letters = [int(i) for i in input().split()]
for i in range(m):
	p = 0
	q = n
	while True:
		position = (p + q) // 2
		if rooms[position] < letters[i]:
			p = position
		else:
			q = position
		if q - p <= 1:
			print(q, letters[i] - rooms[p])
			break


from bisect import bisect_left

a, b = list(map(int, input().split()))
flats = list(map(int, input().split()))
lets = list(map(int, input().split()))

nflats = []
s = 0
for x in flats:
    s += x
    nflats.append(s)

for x in lets:
    y = bisect_left(nflats, x)
    if y != 0:
        print(y+1, x - nflats[y-1])
    else:
        print(y+1, x)


from bisect import bisect_left

n, m = list(map(int, input().split()))
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
t = [0]
s = 0
for x in a:
    s += x
    t.append(s)

for x in b:
    p = bisect_left(t, x)
    print(p, x - t[p - 1])


n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
prefix = [0]
summ = 0
for i in range(n - 1):
	summ += a[i]
	prefix.append(summ)
def binsearch(x, a):
	left = 0
	right = len(prefix)
	while left != right - 1:
		mid = (left + right) // 2
		if (x[mid] < a):
			left = mid
		else:
			right = mid
	return left
for i in range(m):
	q = binsearch(prefix, b[i])
	v = b[i] - prefix[q]
	print(q + 1, v)


n,m=map(int,input().split())
arr1=list(map(int,input().split()))
arr=list(map(int,input().split()))
arr2=[]
sumx=0
for i in range(n):
	sumx+=arr1[i]
	arr2.append(sumx)
j=0
k=0
while(j<m):
	if(arr[j]<=arr2[k]):
		if(k==0):
			print(k+1,arr[j])
		else:
			print(k+1,arr[j]-arr2[k-1])
	else:
		while(arr[j]>arr2[k]):
			k+=1
		print(k+1,arr[j]-arr2[k-1])
	j+=1


import sys
import bisect

next(sys.stdin)

ais = list(map(int, next(sys.stdin).rstrip().split()))

rooms = list(map(int, next(sys.stdin).rstrip().split()))

borders = [ais[0]]
for a in ais[1:]:
    borders.append(borders[-1] + a)

result = []

for room in rooms:
    dorm_num = bisect.bisect_left(borders, room) + 1
    if dorm_num > 1:
        room -= borders[dorm_num - 2]
    result.append('%s %s' % (dorm_num, room))

for r in result:
    print(r)


R = lambda: list(map(int, input().split()))

n, m = R()
a = list(R())
a.append(10**6)

s = 0
f = 0

for b in R():
    while s < b:
        s += a[f]
        f += 1
    print(f, b - s + a[f-1])


import bisect,itertools
n,m=map(int,input().split())
aa=list(map(int,input().split()))
a,c=[0]+list(itertools.accumulate(aa)),0
for b in input().split():
    b=int(b)
    f=bisect.bisect_left(a,b,lo=c)
    k=b-a[f-1]
    print(f,k)
    c=max(c,f)


def main():
    n, m = list(map(int, input().split()))
    room = [int(x) for x in input().split()]
    letter = [int(x) for x in input().split()]

    room_num = 0
    j = 0
    for d in range(n):
        while j < m and letter[j] <= room_num + room[d]:
            r = letter[j] - room_num
            print(d+1, r)
            j += 1
        room_num += room[d]

def __starting_point():
    main()

__starting_point()


read=lambda:map(int,input().split())
n,m=read()
a=list(read())
b=list(read())
pos=0
pre=0
for i in b:
    d=0
    while True:
        d=i-1-pre
        if d>=a[pos]: pre+=a[pos];pos+=1
        else: break
    print(pos+1,d+1)


import bisect

def read_nums():
    return [int(x) for x in input().split()]

def main():
    n, m = read_nums()
    num_rums = read_nums()

    s = 0
    run_sums = [0]
    for run_num in num_rums:
        s += run_num
        run_sums.append(s)

    letters = read_nums()
    res = []
    for letter in letters:
        index = bisect.bisect_left(run_sums, letter)
        res.append((index, letter - run_sums[index - 1]))

    for r in res:
        print(r[0], r[1])

def __starting_point():
    main()

__starting_point()


n,m=[int(x) for x in input().strip().split(' ')]
a=[int(x) for x in input().strip().split(' ')]
b=[int(x) for x in input().strip().split(' ')]
s=[1]*(n+1)
for i in range(1,n+1):
    s[i]=s[i-1]+a[i-1]
idx=0
for i in range(m):
    while True:
        if b[i]<s[idx+1]:
            print(idx+1,b[i]-s[idx]+1)
            break
        else:
            idx+=1


n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
roomsT = 0
currD = -1
for x in range(m):
    toFind = b[x]
    while roomsT < b[x] and currD < n:
        currD += 1
        roomsT += a[currD]
    roomsT -= a[currD]
    currD -= 1
    tempR = toFind - roomsT
    print(currD +2, tempR)


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

alist=list(map(int,input().split()))

blist=list(map(int,input().split()))

prefixsum=[0]

for i in range(n):
	prefixsum.append(alist[i]+prefixsum[-1])
#print(prefixsum)
ptr=1
for i in range(m):
	while blist[i]>prefixsum[ptr]:
		ptr+=1

	room=blist[i]-prefixsum[ptr-1]
	print(ptr,room)


from bisect import bisect_left

n, l = list(map(int, input().split()))
fl = list(map(int, input().split()))
let = list(map(int, input().split()))

pref = [0]
for i in range(n):
    pref.append(fl[i] + pref[i])

prev = 0
for l in let:
    dorm = bisect_left(pref, l, lo=prev)
    prev = dorm
    print(dorm, l - pref[dorm - 1])


def solve():
    n = input()
    print(int(n) % 10 * 10 + len(n) * (len(n) + 1) // 2 - 10)
for i in range(int(input())):
    solve()