Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

from collections import Counter

N, M = map(int, input().split())
Alist = list(map(int, input().split()))

Blist = [sum(Alist[:i])%M for i in range(len(Alist)+1)]
c = Counter(Blist)

ans = 0
for key, val in c.items():
    ans += val * (val - 1) / 2

print(int(ans))

### FIXED CODE ###

from collections import Counter

N, M = map(int, input().split())
Alist = list(map(int, input().split()))
Blist = [0]
for i in range(len(Alist)):
    Blist.append((Blist[-1] + Alist[i])%M)

c = Counter(Blist)

ans = 0
for key, val in c.items():
    ans += val * (val - 1) / 2

print(int(ans))
### BUGGY CODE ###

def z_argorithm(s):
    s_len=len(s)
    a=[0]*s_len
    c=0
    for i in range(1,s_len):
        if i+a[i-c] < c+a[c]:
            a[i]=a[i-c]
        else:
            j=max(0,c+a[c]-i)
            while i+j<s_len and s[j]==s[j+i]:
                j += 1
            a[i]=j
            c=i
    return a
def main():
    n=int(input())
    s=input()
    ans = 0
    for i in range(n-1):
        a = z_argorithm(s[i:])
        A=len(a)
        for i in range(A):
            ans=max(ans,min(i,a[i]))
    print(ans)
if __name__ == '__main__':
    main()


### FIXED CODE ###

import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def Z(s):
    """
    s: str
    return list of int with length len(s)
    """
    # initialization
    n=len(s)
    z=[0]*n
    z[0]=n
    # construct
    L,R=0,0
    for i in range(1,n):
        if i>=R: # 過去の結果が全く使えない
            L=R=i
            while(R<n and s[R-L]==s[R]): R+=1
            z[i]=R-L
        elif z[i-L]<R-i: # 過去の結果が全て使える
            z[i]=z[i-L]
        else: # 過去の結果が一部使える
            L=i
            while(R<n and s[R-L]==s[R]): R+=1
            z[i]=R-L
    return z
def resolve():
    n=int(input())
    s=input()
    ans=0
    for i in range(n):
        z=Z(s[i:])
        k=len(z) # k=n-i
        for j in range(k):
            ans=max(ans,min(j,z[j]))
    print(ans)
resolve()
### BUGGY CODE ###

def v():
 for e in iter(input,'0 0'):
  r=int(e.split()[0])
  d=[int(''.join(x),2)for x in zip(*[input().split()for _ in[0]*r])]
  a=0
  for m in range(1<<~-r):
   t=0
   for s in d:
    c=sum(map(int,bin(m^s)[2:]))
    t+=c if c>r//2 else r-c
   if a<t:a=t
  print(a)
if'__main__'==__name__:v()


### FIXED CODE ###

def v():
 for e in iter(input,'0 0'):
  r=int(e.split()[0])
  d=[int(''.join(x),2)for x in zip(*[input().split()for _ in[0]*r])]
  a=0
  for m in range(1<<~-r):
   t=0
   for s in d:
    c=bin(m^s).count('1')
    t+=max(c,r-c)
   if a<t:a=t
  print(a)
if'__main__'==__name__:v()
### BUGGY CODE ###

n = int(input())
ai = [int(x) for x in input().split()]
bn = [0] * n
cn = [0] * n

for i in range(0,n):
    j = n - 1 - i
    if (n//(j+1)<1):
        bn[j] = ai[j]
    else:
        bn[j] = (cn[j] + ai[j]) % 2

    if bn[j] == 1 and not(n//(j+1) < 1):
        for k in range(0,((j+1)//2)+1):
            if (j+1)%(k+1) == 0:
                cn[k] += 1

m = sum(bn)
print(m)
for i in range(0,n):
    if bn[i] == 1:
        print(i+1, end=" ")



### FIXED CODE ###

n = int(input())
ai = [int(x) for x in input().split()]
bn = [0] * n

for i in range(0,n):
    j = n - 1 - i
    c = 0
    for k in range(2, n+1):
        idx = k * (j + 1) - 1
        if  idx > n-1:
            break
        c += bn[idx]
    
    bn[j] = (c + ai[j]) % 2


m = sum(bn)
print(m)
for i in range(0,n):
    if bn[i] == 1:
        print(i+1, end=" ")
### BUGGY CODE ###

A,B,N = map(int,input().split())
S =[]
for i in range(N+1):
    S.append(int(A*i/B)-A*(int(i/B)))
print(max(S))

### FIXED CODE ###

A,B,N = map(int,input().split())
if B>N:
    print(int(A*N/B)-A*(int(N/B)))
else:
    print(int(A*(B-1)/B)-A*(int((B-1)/B)))
### BUGGY CODE ###

mod=10**9+7
import math
import sys
from collections import deque
import heapq
import copy
import itertools
from itertools import permutations
from itertools import combinations
import bisect
def mi() : return map(int,sys.stdin.readline().split())
def ii() : return int(sys.stdin.readline().rstrip())
def i() : return sys.stdin.readline().rstrip()
a,b=mi()
l=list(mi())
dp=[0]*a
dp[0]=0
for i in range(1,a):
    dp[i]=min(abs(l[i]-l[i-j-1])+dp[i-j-1] for j in range(min(i,b)))
print(dp[-1])

### FIXED CODE ###

mod=10**9+7
import math
import sys
from collections import deque
import heapq
import copy
import itertools
from itertools import permutations
from itertools import combinations
import bisect
def mi() : return map(int,sys.stdin.readline().split())
def ii() : return int(sys.stdin.readline().rstrip())
def i() : return sys.stdin.readline().rstrip()
a,b=mi()
l=list(mi())
dp=[sys.maxsize]*a
dp[0]=0
for i in range(1,a):
  for j in range(min(i,b)):
    dp[i]=min(dp[i],abs(l[i]-l[i-j-1])+dp[i-j-1])
print(dp[-1])
### BUGGY CODE ###

s = list(input())
s.reverse()

while s:
    if s[:5] == list("maerd"):
        del s[:5]
        continue
    if s[:7] == list("remaerd"):
        del s[:7]
        continue
    if s[:5] == list("esare"):
        del s[:5]
        continue
    if s[:6] == list("resare"):
        del s[:6]
        continue
    break

if s == []:
    print("YES")
else:
    print("NO")

### FIXED CODE ###

a = list(input())
 
while len(a) >= 5:
    if a[-5:] == list('dream'):
        del a[-5:]
        continue
    if a[-7:] == list('dreamer'):
        del a[-7:]
        continue
    if a[-5:] == list('erase'):
        del a[-5:]
        continue
    if a[-6:] == list('eraser'):
        del a[-6:]
        continue
    break
 
if a == []:
    print('YES')
else:
    print('NO')
### BUGGY CODE ###

def main():
    N, Q = map(int, input().split())
    S = input()
    l = [0] * Q
    r = [0] * Q
    count = []
    for i in range(Q):
        l[i], r[i] = map(int, input().split())
    for i in range(Q):
        sub_S = S[l[i]-1:r[i]]
        print(sub_S.count('AC'))
main()

### FIXED CODE ###

N, Q = map(int, input().split())
S = input()
l = [0] * Q
r = [0] * Q
for i in range(Q):
    l[i], r[i] = map(int, input().split())
counter = [0] * N

for i in range(N-1):
    if S[i:i+2] == 'AC':
        counter[i+1] += counter[i] + 1
    else:
        counter[i+1] += counter[i]

for i in range(Q):
    print(counter[r[i]-1] - counter[l[i]-1])
### BUGGY CODE ###

import numpy as np
[n,m]=[int(x) for x in input().split()]
LR=[[int(x)-1 for x in input().split()] for _ in range(m)]

ans=np.zeros(n)
for c in LR:
  ans[c[0]:c[1]+1]=ans[c[0]:c[1]+1]+1
print(np.sum(ans==m))


### FIXED CODE ###

import numpy as np
[n,m]=[int(x) for x in input().split()]
LR_T=np.array([[int(x)-1 for x in input().split()] for _ in range(m)]).T

print(max(np.min(LR_T[1])-np.max(LR_T[0])+1,0))
### BUGGY CODE ###

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

from itertools import permutations, combinations,combinations_with_replacement,product

C = list(combinations(L,3))

ans = 0

for i in C:
    a, b, c = i
    if a<b+c and b<c+a and c<a+b:
        ans += 1

print(ans)

### FIXED CODE ###

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

ans = 0

import bisect

for a in range(N):
    for b in range(a):
        A=L[a]
        B=L[b]
        c=bisect.bisect_left(L, A+B)
        ans+=c-a-1

print(ans)
### BUGGY CODE ###

def nCr(n,r):
  c = min(r,n-r)
  bunshi = 1
  bunbo = 1
  for i in range(1,c+1):
    bunshi *= (n+1-i)%(10**9+7)
    bunbo *= i%(10**9+7)
  return bunshi*pow(bunbo,10**9+5,10**9+7)%(10**9+7)
  

#入力
n,a,b = map(int,input().split())

ans = pow(2,n,10**9+7)-1
ans -= (nCr(n,a)+nCr(n,b))
ans = ans%(10**9+7)
print(ans)

### FIXED CODE ###

n,a,b = map(int,input().split())
m = 10**9+7

def nCr(n_, r_, m_):
  c = min(r_ , n_ - r_)
  bunshi = 1
  bunbo = 1
  for i in range(1,c+1):
    bunshi = bunshi * (n_+1-i) % m_
    bunbo = bunbo * i % m_
  return bunshi * pow(bunbo, m_-2, m_) % m_


ans = pow(2,n,m)-1
ans -= nCr(n,a,m)
ans %= m
ans -= nCr(n,b,m)
ans %= m
print(ans)
### BUGGY CODE ###

import numpy as np
import sys
sys.setrecursionlimit(10 ** 7)

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

S = input()
S = S[::-1]
LS = len(S)

MOD = 10 ** 9 + 7


dp = [np.zeros(13, dtype=np.int64) for _ in range(LS + 1)]
dp[0][0] = 1

for i in range(LS):
    #print("i == ", i, "S[i]==", S[i])
    #print(dp[i])
    if S[i] != '?':
        d = int(S[i])
        md = (d * pow(10, i, 13)) % 13

        if md == 0:
            dp[i + 1] = dp[i]
        else:
            dp[i + 1][md:] = dp[i][:-md]
            dp[i + 1][:md] = dp[i][-md:]
        dp[i + 1] %= MOD
    else:
        t = pow(10, i, 13)
        for k in range(10):
            md = (k * t) % 13

            if md == 0:
                dp[i + 1] += dp[i]
            else:
                dp[i + 1][md:] += dp[i][:-md]
                dp[i + 1][:md] += dp[i][-md:]
            dp[i + 1] %= MOD

#print("i == LS")
#print(dp[LS])
print(dp[LS][5])


### FIXED CODE ###

import sys
sys.setrecursionlimit(10 ** 7)

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

S = input()
S = S[::-1]
LS = len(S)

MOD = 10 ** 9 + 7


dp = [[0] * 13 for _ in range(LS+1)]
dp[0][0] = 1

for i in range(LS):
    if S[i] != '?':
        d = int(S[i])
        md = (d * pow(10, i, 13)) % 13

        if md == 0:
            dp[i + 1] = dp[i]
        else:
            dp[i + 1][md:] = dp[i][:-md]
            dp[i + 1][:md] = dp[i][-md:]
    else:
        t = pow(10, i, 13)
        for k in range(10):
            md = (k * t) % 13

            for j in range(13):
                nj = (j + md) % 13
                dp[i + 1][nj] += dp[i][j]
                dp[i + 1][nj] %= MOD

print(dp[LS][5])
### BUGGY CODE ###

n = int(input())
a = []

for i in range(n):
    inp = input()
    if "-1" in inp:
        inp = inp.replace("-1","10000")
    a.append(list(map(int, inp.split())))

w = 0
v = set(range(1,n))

edge    = [ 0, 0]
min_w   = [-1,-1]
min_idx = [-1,-1]

while v:
    min_w[0] = min(a[edge[0]])
    min_w[1] = min(a[edge[1]])
    min_idx[0] = a[edge[0]].index(min_w[0])
    min_idx[1] = a[edge[1]].index(min_w[1])
    if min_w[0] <= min_w[1]:
        k = 0
    else:
        k = 1
    if min_idx[k] not in v:
        a[edge[k]][min_idx[k]] = 10000
        continue
    else:
        w += min_w[k]
        v.remove(min_idx[k])
        edge[k] = min_idx[k]

print(w)

### FIXED CODE ###

n = int(input())
a = []

for i in range(n):
    inp = input()
    if "-1" in inp:
        inp = inp.replace("-1","10000")
    a.append(list(map(int, inp.split())))

w = 0
v = set()
v.add(0)

while len(v) < n:
    min_w   = 10000
    min_idx = 0
    i = 0
    for node in v:
        if min_w > min(a[node]):
            min_w = min(a[node])
            i = node
            min_idx = a[i].index(min_w)
    if min_idx in v:
        a[i][min_idx] = 10000
        continue
    else:
        w += min_w
        v.add(min_idx)

print(w)
### BUGGY CODE ###

m = 10**9+7

def nCr(n,r,m):
  c = min(r,n-r)
  bunshi = 1
  bunbo = 1
  for i in range(1,c+1):
    bunshi *= (n+1-i)%m
    bunbo *= i%m
  return bunshi*pow(bunbo,m-2,m)%m

#入力
n,a,b = map(int,input().split())

ans = pow(2,n,m)-1
ans -=nCr(n,a,m)
ans = ans%m
ans -=nCr(n,b,m)
ans = ans%m
print(ans)

### FIXED CODE ###

n,a,b = map(int,input().split())
m = 10**9+7

def nCr(n_, r_, m_):
  c = min(r_ , n_ - r_)
  bunshi = 1
  bunbo = 1
  for i in range(1,c+1):
    bunshi = bunshi * (n_+1-i) % m_
    bunbo = bunbo * i % m_
  return bunshi * pow(bunbo, m_-2, m_) % m_


ans = pow(2,n,m)-1
ans -= nCr(n,a,m)
ans %= m
ans -= nCr(n,b,m)
ans %= m
print(ans)
### BUGGY CODE ###

from scipy.sparse.csgraph import connected_components
from scipy.sparse import csr_matrix

N, M = map(int, input().split())

cities = [[0] * N for i in range(N)]
# print(cities)

for i in range(M):
    i1, i2 = map(int, input().split())
    cities[i1-1][i2-1] = 1
    cities[i2-1][i1-1] = 1
#     print(cities)
    
# print(cities)

n, labels = connected_components(csr_matrix(cities))
print(n-1)

### FIXED CODE ###


N, M = map(int, input().split())

# cities = [[0] * N for i in range(N)]
# print(cities)
cities = {}
 
for a in range(N):
    cities[a+1] = []
# print(cities)
 
for i in range(M):
    i1, i2 = map(int, input().split())
    cities[i1].append(i2)
    cities[i2].append(i1)
# print(cities)
def connected_components(graph):
    seen = set()
    def component(n):
        nodes = set([n])
        while nodes:
            n = nodes.pop()
            seen.add(n)
            nodes |= set(graph[n]) - seen
            yield n
    for n in graph:
        if n not in seen:
            yield component(n)
print(len([list(x) for x in connected_components(cities)])-1)
### BUGGY CODE ###

import collections
h,w = map(int, input().split())
s=[input()for _ in range(h)]
r=[[1,0],[-1,0],[0,1],[0,-1]]
def bfs(i,j):
    global h,w,r
    f=[-1 for _ in range(h*w)]
    que = collections.deque([[i,j,0]])
    while que:
        i,j,k = que.popleft()
        f[i*w+j] =k
        for sr in r:
            ti = i + sr[0]
            tj = j + sr[1]
            if ti>=0 and tj>=0 and ti<h and tj<w:
                if s[ti][tj] == ".":
                    if f[ti*w+tj] == -1:
                        que.append([ti,tj,k+1])
    return k

res = 0
for i in range(h):
    for j in range(w):
        if s[i][j] ==".":
            tres = bfs(i,j)
            res = max(res,tres)
print(res)

### FIXED CODE ###

import collections

h, w = map(int, input().split())
s = [input() for _ in range(h)]
r = [[1, 0], [-1, 0], [0, 1], [0, -1]]


def bfs(i, j):
    global h, w, r
    f = [-1 for _ in range(h * w)]
    que = [[] for _ in range(h * w)]
    que[0].append([i, j])
    for k in range(h * w):
        if que[k]:
            res = k
            for i, j in que[k]:
                f[i * w + j] = k
                for sr in r:
                    ti = i + sr[0]
                    tj = j + sr[1]
                    if ti >= 0 and tj >= 0 and ti < h and tj < w:
                        if s[ti][tj] == ".":
                            if f[ti * w + tj] == -1:
                                que[k + 1].append([ti, tj])
    return res


fl = 0
for i in range(h):
    if "#" in s[i]:
        fl = 1
        break
if fl == 0:
    print(h + w - 2)
else:
    res = 0
    for i in range(h):
        for j in range(w):
            if s[i][j] == ".":
                res = max(res, bfs(i, j))
    print(res)
### BUGGY CODE ###

X,Y,Z,K = map(int,input().split())
list_a = list(map(int,input().split()))
list_b = list(map(int,input().split()))
list_c = list(map(int,input().split()))

list_a.sort(reverse=True)
list_b.sort(reverse=True)
list_c.sort(reverse=True)

list = []

for la in list_a:
    for lb in list_b:
        list.append([la,lb,la+lb])

list.sort(key=lambda x:x[2],reverse=True)

list2 = []

for a,b,n in list:
    for lc in list_c:
        list2.append(n+lc)

list2.sort(reverse=True)

cnt = 0

for i in list2:
    print(i)
    cnt += 1
    if cnt == K:
        exit()

### FIXED CODE ###

X,Y,Z,K = map(int,input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)

sums = []

for i in range(X):
    for j in range(Y):
        for k in range(Z):
            if (i+1)*(j+1)*(k+1) > K:
                break
            sums.append(A[i]+B[j]+C[k])

for s in sorted(sums,reverse=True)[:K]:
    print(s)
### BUGGY CODE ###

import sys
input = sys.stdin.readline
import numpy as np

N = int(input())
A = [int(input()) for _ in range(N)]

p1 = 0
p2 = 0
P1 = [0]
P2 = [0]
for a in A:
    if a == 0:
        p1 -= 2
        p2 += 1
    elif a % 2 == 0:
        p2 -= 1
    elif a % 2 == 1:
        p1 -= 1
        p2 += 1
    p1 += a
    P1.append(p1)
    P2.append(p2)

S = sum(A)
"""
dp = [[0, 0, 0, 0] for _ in range(N+1)]

dp[N] = [P1[N], P1[N]+P2[N], -P1[N], 0]

for i in reversed(range(N)):
    dp[i][0] = max(dp[i+1][0], P1[i])
    dp[i][1] = max(dp[i+1][1], P2[i]+dp[i][0])
    dp[i][2] = min(dp[i+1][2], P2[i]-dp[i][1])
    dp[i][3] = min(dp[i+1][3], P1[i]+dp[i][2])
"""

P1 = P1[::-1]
P2 = P2[::-1]

dp = np.zeros((N+1, 4), dtype=int)
dp[0, :] = np.array([P1[0], P1[0]+P2[0], -P1[0], 0])
for i, (p1, p2) in enumerate(zip(P1, P2)):
    if i == 0: continue
    dp[i, 0] = np.maximum(dp[i-1, 0], p1)
    dp[i, 1] = np.maximum(dp[i-1, 1], p2+dp[i, 0])
    dp[i, 2] = np.minimum(dp[i-1, 2], p2-dp[i, 1])
    dp[i, 3] = np.minimum(dp[i-1, 3], p1+dp[i, 2])

print(dp[N, 3]+S)


### FIXED CODE ###

import sys
input = sys.stdin.readline

N = int(input())
A = [int(input()) for _ in range(N)]

p1 = 0
p2 = 0
P1 = [0]
P2 = [0]
for a in A:
    if a == 0:
        p1 -= 2
        p2 += 1
    elif a % 2 == 0:
        p2 -= 1
    elif a % 2 == 1:
        p1 -= 1
        p2 += 1
    p1 += a
    P1.append(p1)
    P2.append(p2)

S = sum(A)
dp = [[0, 0, 0, 0] for _ in range(N+1)]

dp[N] = [P1[N], P1[N]+P2[N], -P1[N], 0]

for i in reversed(range(N)):
    dp[i][0] = max(dp[i+1][0], P1[i])
    dp[i][1] = max(dp[i+1][1], P2[i]+dp[i][0])
    dp[i][2] = min(dp[i+1][2], P2[i]-dp[i][1])
    dp[i][3] = min(dp[i+1][3], P1[i]+dp[i][2])

print(dp[0][3]+S)
### BUGGY CODE ###

def main():
    
    h,w = map(int,input().split())
    l = [list(input()) for i in range(h)]

    c = [[[-1]*2 for i in range(w)] for i in range(h)]

    for i in range(h):
        for j in range(w):
            if c[i][j][0] == -1:
                n = 0
                while i+n < h and l[i+n][j] == ".":
                    n += 1
                for k in range(n):
                    c[i+k][j][0] = n
                
            if c[i][j][1] == -1:
                n = 0
                while j+n < w and l[i][j+n] == ".":
                    c[i][j+n][1] = 1
                    n += 1
                for k in range(n):
                    c[i][j+k][1] = n


            
    ans = 0
    for i in range(h):
        for j in range(w):
            if l[i][j] == ".":
                ans = max(ans,c[i][j][0]+c[i][j][1])
    print(ans-1)
if __name__ == "__main__":
    main()

### FIXED CODE ###


h,w = map(int,input().split())
l = [list(input()) for i in range(h)]

c1 = [[-1 for i in range(w)] for i in range(h)]
c2 = [[-1 for i in range(w)] for i in range(h)]
ans = 0
for i in range(h):
    for j in range(w):
        if c1[i][j] == -1:
            n = 0
            while i+n < h:
                if l[i+n][j] == ".":
                    n += 1
                else:
                    break
            for k in range(n):
                c1[i+k][j] = n
            
        if c2[i][j] == -1:
            n = 0
            while j+n < w:
                if l[i][j+n] == ".":
                    n += 1
                else:
                    break
            for k in range(n):
                c2[i][j+k] = n


        ans = max(ans,c1[i][j]+c2[i][j])
print(ans-1)
### BUGGY CODE ###

from heapq import heappush,heappop

N,M,s,t=map(int,input().split())
table=[[] for i in range(N)]
rtable=[[] for i in range(N)]
for i in range(M):
    u,v,a,b=map(int,input().split())
    table[u-1].append([a,v-1])
    table[v-1].append([a,u-1])
    rtable[u-1].append([b,v-1])
    rtable[v-1].append([b,u-1])

def dijk(s,table):
    dij=[]
    heappush(dij,[0,s])
    val=[10**16]*N
    val[s]=0
    while dij:
        q,x=heappop(dij)
        if val[x]<q:
            continue
        for d,y in table[x]:
            if val[y]>val[x]+d:
                val[y]=val[x]+d
                heappush(dij,[val[y],y])
    return val
val=dijk(s-1,table)
rval=dijk(t-1,rtable)
score=[0]*N
mod=pow(10,15)
for i in range(N):
    score[i]=mod-(val[i]+rval[i])
amax=[mod]*N
amax[-1]=score[-1]
for i in range(N-2,-1,-1):
    amax[i]=max(amax[i+1],score[i])
for i in range(N):
    print(amax[i])

### FIXED CODE ###

from heapq import heappush,heappop
from collections import deque
N,M,s,t=map(int,input().split())
table=[[] for i in range(N)]
rtable=[[] for i in range(N)]
for i in range(M):
    u,v,a,b=map(int,input().split())
    table[u-1].append([a,v-1])
    table[v-1].append([a,u-1])
    rtable[u-1].append([b,v-1])
    rtable[v-1].append([b,u-1])
dij=[]
heappush(dij,[0,s-1])
val=[10**16]*N
val[s-1]=0
while dij:
    q,x=heappop(dij)
    if val[x]<q:
        continue
    for d,y in table[x]:
        if val[y]>val[x]+d:
            val[y]=val[x]+d
            heappush(dij,[val[y],y])
rdij=[]
heappush(rdij,[0,t-1])
rval=[10**16]*N
rval[t-1]=0
while rdij:
    q,x=heappop(rdij)
    if rval[x]<q:
        continue
    for d,y in rtable[x]:
        if rval[y]>rval[x]+d:
            rval[y]=rval[x]+d
            heappush(rdij,[rval[y],y])
ans=deque()
t=val[N-1]+rval[N-1]
mod=pow(10,15)
ans.append(mod-t)
for i in range(N-2,-1,-1):
    t=min(t,val[i]+rval[i])
    ans.appendleft(mod-t)
print('\n'.join(map(str,ans)))
### BUGGY CODE ###

a = input()
n = int(input())
t = 1
ar = []
while True:
    for i in range(len(a) - t + 1):
        if a[i:i+t] not in ar:
            ar.append(a[i:i+t])
    t += 1
    if t > len(a):
        break
ar.sort()
print(ar[n-1])

### FIXED CODE ###

a = input()
n = int(input())
t = 1
ar = []
while True:
    for i in range(len(a) - t + 1):
        if a[i:i+t] not in ar:
            ar.append(a[i:i+t])
    t += 1
    if t > len(a) or t > n:
        break
ar.sort()
print(ar[n-1])
### BUGGY CODE ###

def cmb2(n):
	result = n * (n - 1) / 2

	return int(result)

if __name__ == "__main__":

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

	A_set = set(A)

	ans_d = {}
	for a_s in A_set:
		ans_d[str(a_s)] = A.count(a_s)

	for a in A:
		ans_d[str(a)] -= 1
		ans = 0
		for a_n in ans_d.values():
			if a_n <= 1:
				continue
			else:
				ans += cmb2(a_n)
		print(ans)
		ans_d[str(a)] += 1

### FIXED CODE ###

import sys
from collections import Counter

def cmb2(n):
	if n < 2:
		return 0

	return n * (n-1) // 2

if __name__ == "__main__":

	N = int(input())
	A = [int(i) for i in input().split()]
	# A = list(map(int, input().split()))

	c = Counter(A)
	s = 0
	for v in c.values():
		s += cmb2(v)

	for a in A:
		print(s - cmb2(c[a]) + cmb2(c[a] - 1))
### BUGGY CODE ###

n, m = map(int, input().split())
py = [tuple(map(int, input().split())) for _ in range(m)]

t = [[] for _ in range(n)]

for p, y in py:
  t[p-1].append(y)

for ti in t:
  ti.sort()
  
for p, y in py:
  kami = p
  shimo = t[p-1].index(y) + 1
  print('{0:06d}{1:06d}'.format(kami, shimo))

### FIXED CODE ###

import bisect

n, m = map(int, input().split())
py = [tuple(map(int, input().split())) for _ in range(m)]

t = [[] for _ in range(n)]

for p, y in py:
  t[p-1].append(y)

for ti in t:
  ti.sort()
  
for p, y in py:
  kami = p
  shimo = bisect.bisect_left(t[p-1], y) + 1
  print('{0:06d}{1:06d}'.format(kami, shimo))
### BUGGY CODE ###

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

ans = 1
for i in range(n):
  ans *= a[i]
if ans > 10**18:
  print(-1)
else:
  print(ans)

### FIXED CODE ###

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

if 0 in a:
    print(0)
else:
    ans = 1
    for i in range(n):
        ans *= a[i]
        if ans > 10**18:
            print(-1)
            break
    else:
      print(ans)
### BUGGY CODE ###

s = input()
t = input()
dp = [[0]*(len(t)+1) for _ in range(len(s)+1)]
for i in range(1, len(s)+1):
    for j in range(1, len(t)+1):
        dp[i][j] = max(dp[i][j], dp[i-1][j])
        dp[i][j] = max(dp[i][j], dp[i][j-1])
        if s[i-1] == t[j-1]:
            dp[i][j] = max(dp[i][j], dp[i-1][j-1]+1)

ans = ""
i = len(s)
j = len(t)
while 0 < i and 0 < j:
    if dp[i][j] == dp[i-1][j]:
        i -= 1
    elif dp[i][j] == dp[i][j-1]:
        j -= 1
    else:
        i -= 1
        j -= 1
        ans += s[i]
print(ans[::-1])

### FIXED CODE ###

s = input()
t = input()
dp = [[0]*(len(t)+1) for _ in range(len(s)+1)]
for i in range(1, len(s)+1):
    for j in range(1, len(t)+1):
        dp[i][j] = max(dp[i][j], dp[i-1][j])
        dp[i][j] = max(dp[i][j], dp[i][j-1])
        if s[i-1] == t[j-1]:
            dp[i][j] = max(dp[i][j], dp[i-1][j-1]+1)

ans = ""
i = len(s)
j = len(t)
while 0 < i and 0 < j:
    if dp[i][j] == dp[i-1][j]:
        i -= 1
    elif dp[i][j] == dp[i][j-1]:
        j -= 1
    else:
        i -= 1
        j -= 1
        ans += s[i]
print(ans[::-1])