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

import Queue
import copy

H, W = map(int, raw_input().split())
S = []

for i in range(H):
    S.append(list(raw_input()))


def bfs(start, goal, S2):
    q = Queue.Queue()
    q.put(start)
    candidates = [[0, 1], [1, 0], [0, -1], [-1, 0]]
    dist = [[0]*W for _ in range(H)]
    while not q.empty():
        i = q.get()
        x = i[0]
        y = i[1]
        S2[x][y] = "#"

        for candi in candidates:
            x_new = x + candi[0]
            y_new = y + candi[1]

            if x_new < H and x_new >= 0:
                if y_new < W and y_new >=0:
                    if S2[x_new][y_new] == ".":
                        dist[x_new][y_new] = dist[x][y] + 1
                        q.put([x_new, y_new])

    return dist[goal[0]][goal[1]]


ans = 0
for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            for k in range(H):
                for l in range(W):
                    S2 = copy.deepcopy(S)
                    ans = max(ans, bfs([i, j], [k, l], S2))

print ans



### FIXED CODE ###

import Queue
import copy

def main():
    H, W = map(int, raw_input().split())
    S = []

    for i in range(H):
        S.append(list(raw_input()))


    def bfs(start, S2):
        q = Queue.Queue()
        q.put(start)
        candidates = [[0, 1], [1, 0], [0, -1], [-1, 0]]
        dist = [[0]*W for _ in range(H)]
        S2[start[0]][start[1]] = "#"
        while not q.empty():
            i = q.get()
            x = i[0]
            y = i[1]

            for candi in candidates:
                x_new = x + candi[0]
                y_new = y + candi[1]

                if x_new < H and x_new >= 0:
                    if y_new < W and y_new >=0:
                        if S2[x_new][y_new] == ".":
                            dist[x_new][y_new] = dist[x][y] + 1
                            S2[x_new][y_new] = "#"
                            q.put([x_new, y_new])

        ans = 0
        for i in range(H):
            ans = max(ans, max(dist[i]))

        return ans

    ans = 0
    for i in range(H):
        for j in range(W):
            if S[i][j] == ".":
                S2 = copy.deepcopy(S)
                ans_temp = bfs([i, j], S2)
#                print i, j, ans_temp                                                                                                                     
                ans = max(ans, ans_temp)


    print ans


if __name__ == "__main__":
    main()
### BUGGY CODE ###

N=int(input())
S=[]
count=0
for i in range(N):
  s="".join(sorted(input()))
  count +=S.count(s)
  S +=[s]
print(count)  


### FIXED CODE ###

from collections import defaultdict
N=int(input())
S={}
S_counter = defaultdict(int)
count=0
for i in range(N):
  s="".join(sorted(input()))
  if s in S:
    count += S[s]
    S[s] += 1
  else:
    dict={s:1}
    S.update(dict)
print(count)
### BUGGY CODE ###

def shellsort(a):
    l = len(a)
    intervals = [n for n in (40, 13, 4, 1) if n <= l]
    count = 0
    for interval in intervals:
        for i in range(interval, l):
            v = a[i]
            j = i-interval
            while j >= 0 and v < a[j]:
                a[j+interval] = a[j]
                j -= interval
                count += 1
            a[j+interval] = v

    print(len(intervals))
    print(" ".join([str(n) for n in intervals]))
    print(count)
    print("\n".join([str(n) for n in a]))

shellsort([int(input()) for _ in range(int(input()))])

### FIXED CODE ###

def shellsort(a):
    l = len(a)
    gaps = [1]
    while gaps[0]*3+1 < (l/9 if l>100 else l) and len(gaps) <= 100:
        gaps.insert(0, gaps[0]*3+1)
    count = 0
    for g in gaps:
        for i in range(g, l):
            v = a[i]
            j = i-g
            while j >= 0 and v < a[j]:
                a[j+g] = a[j]
                j -= g
                count += 1
            a[j+g] = v

    print(len(gaps))
    print(" ".join([str(n) for n in gaps]))
    print(count)
    print("\n".join([str(n) for n in a]))

shellsort([int(input()) for _ in [None]*(int(input()))])
### BUGGY CODE ###

A, B, K = [int(n) for n in input().split()]
for i in range(K):
    if A>=1:
        A -= 1
    elif B>=1:
        B -= 1
print(A,B)

### FIXED CODE ###

A, B, K = [int(n) for n in input().split()]
if K>A:
    K -= A
    if K>B:
        print(0,0)
    else:
        print(0, B-K)
else:
    print(A-K,B)
### BUGGY CODE ###

def bfs(n,e,fordfs):
    W = [-1]*n
    W[e] = 0
    P = [-1]*n
    que = deque()
    que.append(e)
    P[e] = 0
    while que:
        now = que.popleft()
        nowW = W[now]
        for ne in fordfs[now]:
            if W[ne] == -1:
                W[ne] = nowW + 1
                que.append(ne)
                P[ne] = now + 1
            else:
                W[ne] = nowW + 1
                P[ne] = now + 1
                que.append(ne)
    return P
def examD():
    ans = 0
    N, M = LI()
    V = [[] for _ in range(N)]
    d = defaultdict(bool)
    for i in range(N+M-1):
        a, b = LI()
        #逆に張る
        V[a-1].append(b-1)
        d[b-1] = True
    for i in range(N):
        if not d[i]:
            start = i
            ans = bfs(N, start, V)
            break
    for v in ans:
        print(v)
#    print(V)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

if __name__ == '__main__':
    examD()


### FIXED CODE ###

def examD():
    N, M = LI()
    V = [[] for _ in range(N)]
    ans = [0]*N
    Cnt=[0]*(N+1); Cnt[N]=1
    for i in range(N+M-1):
        a, b = LI()
        V[a-1].append(b-1)
        Cnt[b-1] += 1
    root = Cnt.index(min(Cnt))
    que = [root]
#    print(Cnt)
#    print(root)
    while que:
        v = que.pop()
        for u in V[v]:
            if Cnt[u] == 1:
                ans[u] = v+1
                que.append(u)
            else:
                Cnt[u] -= 1
    print(*ans, sep='\n')
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

if __name__ == '__main__':
    examD()
### BUGGY CODE ###

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

if N < M : 

    X = sorted(list(map(int, input().split())))
    
    move = []
    
    for i in range(len(X)-1) : 
        move.append(X[i+1] -X[i])
        
    # print(move)
    
    for i in range(N-1) : 
        move.pop(move.index(max(move)))
    
    # print(move)
    
    sum = 0
    
    for i in move : 
        sum += i

else : 
    sum = 0
    
print(sum)

### FIXED CODE ###

from collections import deque

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

if N < M : 

    X = sorted(list(map(int, input().split())))
    
    move = []
    
    for i in range(len(X)-1) : 
        move.append(X[i+1] -X[i])
        
    # print(move)

    move = sorted(move)    

    # print(move)
    
    sum = 0
    
    for i in range(M-N) : 
        sum += move[i]

else : 
    sum = 0
    
print(sum)
### BUGGY CODE ###

def main():
	s = int(input())
	a = list(map(int,input().split()))
	from operator import mul
	from functools import reduce
	
	def cmb(n, r):
		if n - r < r: r = n - r
		if r == 0: return 1
		if r == 1: return n

		numerator = [n - r + k + 1 for k in range(r)]
		denominator = [k + 1 for k in range(r)]

		for p in range(2,r+1):
			pivot = denominator[p - 1]
			if pivot > 1:
				offset = (n - r) % p
				for k in range(p-1,r,p):
					numerator[k - offset] /= pivot
					denominator[k] /= pivot

		result = 1
		for k in range(r):
			if numerator[k] > 1:
				result *= int(numerator[k])

		return result
	# print(cmb(3,2))
	import collections
	for i in range(s):
		# print("i is {}".format(i))
		c = collections.Counter(a[0:i] + a[i+1:s])
		Set = set(a[0:i] + a[i+1:s])
		# print("Set is {}".format(Set),"c is {}".format(c))
		ans = 0
		for t in Set:
			# print("t is {}".format(t))
			if c[t] > 1:
				ans += cmb(int(c[t]),2)
		print(ans)

if __name__ == '__main__':
	main()

### FIXED CODE ###

def main():
	import collections
	s = int(input())
	a = list(map(int,input().split()))
	ac = collections.Counter(a)
	ans = 0
	for i in set(ac):
		at = ac[i]
		ans += (at * (at-1))/2
	for t in a:
		ans_print = ans -((ac[t] * (ac[t] -1))/2) + (((ac[t]-1) * ((ac[t]-1)-1))/2)
		print(int(ans_print))
if __name__ == '__main__':
	main()
### BUGGY CODE ###

n, k = map(int, input().split())
A = list(map(int, input().split()))
for i in range(k):
  C = [0]*(n+1)
  for j in range(n):
    C[max(0, j-A[j])] += 1
    C[min(n-1, j+A[j])+1] -= 1
  v = 1
  for j in range(n):
    C[j+1]=C[j+1]+C[j]
    if C[j]!=n:
      v = 0
  if v:
    break
  A = C
C = [str(c) for c in C[:-1]]
print(' '.join(C))

### FIXED CODE ###

n, k = map(int, input().split())
A = list(map(int, input().split()))
for i in range(k):
  C = [0]*(n+1)
  for j in range(n):
    p = max(0, j-A[j])
    q = min(n-1, j+A[j])
    C[p] += 1
    C[q+1] -= 1
  v = 1
  for j in range(n):
    C[j+1]=C[j+1]+C[j]
    if C[j]!=n:
      v = 0
  if v:
    break
  A = C
C = [str(c) for c in C[:-1]]
print(' '.join(C))
### BUGGY CODE ###

n,q=map(int,input().split())
ab=[list(map(int,input().split())) for i in range(n-1)]
px=[list(map(int,input().split())) for i in range(q)]

#グラフの状態を調べる
def Graph(ab):
    G=[[] for i in range(n)]
    #up_G=[0]*n#up_G[i]=頂点iの親
    for a,b in ab:
        G[a-1].append(b-1)
        G[b-1].append(a-1)
    return G

from collections import deque
def bfs(G,v,p):
    #(G,v,p)=(グラフ(木の情報),探索中の頂点,vの親)
    #vの子の頂点を決めて、vをpに移す
    q=deque()
    q.append((v,p))
    while q:
        V,P=q.popleft()
        for new_v in G[V]:
            #new_vが頂点の時はアウト
            if new_v==P:continue
            ans[new_v]+=ans[V]
            q.append((new_v,V))

G=Graph(ab)
ans=[0]*n
#print(G)

for p,x in px:
    ans[p-1]+=x
bfs(G,0,-1)
print(*ans)


### FIXED CODE ###

n,q=map(int,input().split())
ab=[list(map(int,input().split())) for i in range(n-1)]
px=[list(map(int,input().split())) for i in range(q)]

#グラフの状態を調べる
def Graph(ab):
    G=[[] for i in range(n)]
    #up_G=[0]*n#up_G[i]=頂点iの親
    for a,b in ab:
        G[a-1].append(b-1)
        G[b-1].append(a-1)
    return G

from collections import deque
def bfs(G,v,p):
    #(G,v,p)=(グラフ(木の情報),探索中の頂点,vの親)
    #vの子の頂点を決めて、vをpに移す
    q=deque()
    q.append((v,p))
    while q:
        V,P=q.popleft()
        for new_v in G[V]:
            #new_vが頂点の時はアウト
            if new_v==P:continue
            ans[new_v]+=ans[V]
            q.append((new_v,V))

G=Graph(ab)
ans=[0]*n
#print(G)

for p,x in px:
    ans[p-1]+=x
bfs(G,0,-1)
print(*ans)
### BUGGY CODE ###

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

def main():
    H, W, K = map(int, input().split())
    grid = [list(map(int, list(input().strip()))) for _ in range(H)]

    white_count = sum(sum(x) for x in grid)

    # 折れる箇所全部で折った数が最大値
    ans = (H - 1) * (W - 1)

    # 横線で割るパターン全てについて縦線での降り方を考える
    for i in range(2 ** (H - 1)):

        # 横方向に割った後のチョコの状態を生成
        snap = 0
        board = [[grid[0]]]
        for j in range(H-1):
            if ((i >> j) & 1):
                board.append([grid[j + 1]])
                snap += 1
            else:
                board[-1].append(grid[j + 1])

        w_count_board = [[] for _ in board]
        for x in range(W):
            # 縦方向に折るかのフラグ
            snap_flag = False
            for y, hori in enumerate(board):
                # その位置までおらなかった場合のチョコの個数を生成
                w_choco = sum(row[x] for row in hori)
                w_count_board[y].append(w_choco)

                # 上限超えたらおる
                if sum(w_count_board[y]) > K:
                    snap_flag = True

            # フラグがたってたら、x -1 まではおるので無視
            # チョコの個数カウントをx始まりで更新
            if snap_flag:
                w_count_board = [[wcb[-1]] for wcb in w_count_board]
                snap += 1

        ans = min(ans, snap)

    print(ans)


if __name__ == "__main__":
    main()


### FIXED CODE ###

# 足し合わせしすぎてるのでTLE
import sys
input = sys.stdin.readline

def main():
    H, W, K = map(int, input().split())
    grid = [list(map(int, list(input().strip()))) for _ in range(H)]

    # 折れる箇所全部で折った数が最大値
    ans = (H - 1) * (W - 1)

    # 横線で割るパターン全てについて縦線での降り方を考える
    for i in range(2 ** (H - 1)):
        snap = 0

        hori_grid = [grid[0]]
        for j in range(H - 1):
            if ((i >> j) & 1):
                hori_grid.append(grid[j+1])
                snap += 1
            else:
                hori_grid[-1] = [a + b for a,b in zip(hori_grid[-1], grid[j+1])]

        wc_count = [hori_grid[y][0] for y in range(snap + 1)]

        # 最初の列で超えてる場合はスキップ
        if max(wc_count) > K:
            continue

        for x in range(1, W):
            now_wc_count = [hori_grid[y][x] for y in range(len(wc_count))]
            next_wc_count = [a+b for a,b in zip(wc_count, now_wc_count)]

            if max(next_wc_count) > K:
                snap += 1
                wc_count = now_wc_count
            else:
                wc_count = next_wc_count

        ans = min(ans, snap)

    print(ans)


if __name__ == "__main__":
    main()
### BUGGY CODE ###

import os
import sys
from atexit import register
from io import BytesIO
sys.stdin = BytesIO(os.read(0, os.fstat(0).st_size))
sys.stdout = BytesIO()
register(lambda: os.write(1, sys.stdout.getvalue()))
input = lambda: sys.stdin.readline().rstrip('\r\n')
raw_input = lambda: sys.stdin.readline().rstrip('\r\n')

n,x,m = (int(x) for x in input().split())
lst = []
ans = 0
cnt = 0
i = 0
visited = [-1 for _ in xrange(m)]
stop_c = False
while i < n:
    ret = pow(x,2**i,m)

    if visited[ret] == -1:
        visited[ret] = i
    else:
        stop_c = True
        # i += 1
        break
    ans += ret
    lst.append(ret)
    i += 1

if stop_c:
    cycle_len = len(lst[visited[ret]:])
    cycle_sum = sum(lst[visited[ret]:])
    remsum  = sum(lst[visited[ret]:][:(n-visited[ret]) % cycle_len])
    print((ans -cycle_sum) + (cycle_sum * ((n-visited[ret]) // cycle_len)) + remsum)
else:
    print(ans)

### FIXED CODE ###

import os
import sys
from atexit import register
from io import BytesIO
sys.stdin = BytesIO(os.read(0, os.fstat(0).st_size))
sys.stdout = BytesIO()
register(lambda: os.write(1, sys.stdout.getvalue()))
input = lambda: sys.stdin.readline().rstrip('\r\n')
raw_input = lambda: sys.stdin.readline().rstrip('\r\n')

n,x,m = (int(x) for x in input().split())
lst = []
ans = 0
cnt = 0
i = 0
visited = [-1 for _ in xrange(m)]
stop_c = False
while i < n:
    if i == 0:
        ret = pow(x,1,m)
    else:
        ret = pow(x,2,m)
    x = ret
    if visited[ret] == -1:
        visited[ret] = i
    else:
        stop_c = True
        # i += 1
        break
    ans += ret
    lst.append(ret)
    i += 1

if stop_c:
    cycle_len = len(lst[visited[ret]:])
    cycle_sum = sum(lst[visited[ret]:])
    remsum  = sum(lst[visited[ret]:][:(n-visited[ret]) % cycle_len])
    print((ans -cycle_sum) + (cycle_sum * ((n-visited[ret]) // cycle_len)) + remsum)
else:
    print(ans)
### BUGGY CODE ###

N, Q = map(int, input().split())
S = str(input())
for i in range(Q):
    l, r = map(int, input().split())
    slice = S[l-1:r]
    qw=len(slice)
    po=slice.replace("AC","")
    le=len(po)
    print(int((qw-le)/2))


### FIXED CODE ###

N, Q = map(int, input().split())
S = str(input())
wa=[0]*N
p=0
for j in range(1,N):
    if S[j-1]=="A" and S[j]=="C":
        p+=1
        wa[j]=p
    else:
        wa[j]=wa[j-1]
for i in range(Q):
    k =input().split()
    l=int(k[0])
    r=int(k[1])
    print(wa[r-1]-wa[l-1])
### BUGGY CODE ###

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

listl.sort(reverse=True)

#print(listl)
ans=0
"""if n==3:
	if listl[0]>=listl[1]+listl[2]:
		ans=0
	else:
		ans=1
else:"""
for i in range(n-2):
	for j in range(i+1,n-1):
		if listl[j]<=(listl[i]/2):
			break
		for k in range(j+1,n):
		#	print(listl[i],listl[j],listl[k])
			if listl[i]<listl[j]+listl[k]:
				ans=ans+1
			else:
				break	
print(ans)
				 
	

### FIXED CODE ###

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

"""listl.sort(reverse=True)

#print(listl)
	ans=0
for i in range(n-2):
	for j in range(i+1,n-1):
		if listl[j]<=(listl[i]/2):
			break
		for k in range(j+1,n):

			if listl[i]<listl[j]+listl[k]:
				ans=ans+1
			else:
				break	

print(ans)"""
from bisect import bisect_right				 
listl.sort()

ans=0

for i in range(n-1,1,-1):
	a=listl[i]
	for j in range(i-1,0,-1):
		b=listl[j]
		c=bisect_right(listl,a-b)
		#print(c)
		num = j-c
		if num>0:
			ans+=num
print(ans)
### BUGGY CODE ###

import fractions
import sys
input = sys.stdin.readline


N = int(input())
A = tuple(map(int, input().split()))


def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)


mod = 10**9+7
lcm = 1
B = [0]*N

for i in range(N):
    lcm = (lcm // fractions.gcd(lcm, A[i])) * A[i]


ans = 0

for i in range(N):
    ans += (lcm // A[i])
    ans %= mod

print(ans)


### FIXED CODE ###

import fractions
import sys
input = sys.stdin.readline


P = 10**9 + 7
N = (10**6)+1
inv_t = [0]+[1]
for i in range(2, N):
    inv_t += [inv_t[P % i] * (P - int(P / i)) % P]

N = int(input())
A = tuple(map(int, input().split()))

mod = 10**9+7
lcm = 1
B = [0]*N

for i in range(N):
    lcm = (lcm // fractions.gcd(lcm, A[i])) * A[i]

ans = 0

lcm %= mod
for i in range(N):
    ans += (lcm * inv_t[A[i]]) % mod
    ans %= mod

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

N, M, L = (int(i) for i in input().split())

import heapq
def Dijkstra(graph, start):
    N = len(graph)
    d = {}
    prev = {}
    for i in range(N):
        prev[i] = None
        if i == start:
            d[i] = 0
        else:
            d[i] = float('inf')
    Q = []
    heapq.heappush(Q, (0, start))
    while Q:
        d_u, u = heapq.heappop(Q)
        if d[u] < d_u:#ダブりを回避
            continue
        for i in range(N):
            if graph[u][i]:
                temp = d_u + graph[u][i]
                if d[i] > temp:
                    d[i] = temp
                    prev[i] = u
                    heapq.heappush(Q, (d[i], i))
    return (d, prev)

graph = [[0]*N for i in range(N)]
for i in range(M):
    a, b, c = (int(i) for i in input().split())
    graph[a-1][b-1] = c
    graph[b-1][a-1] = c

new_graph = [[0]*N for i in range(N)]
for i in range(N):
    d, prev = Dijkstra(graph, i)
    for j in range(N):
        if d[j] <= L:
            new_graph[i][j] = 1
            new_graph[j][i] = 1

ans = []
for i in range(N):
    d, prev = Dijkstra(new_graph, i)
    ans.append(d)

Q = int(input())
for i in range(Q):
    s, t = (int(i) for i in input().split())
    if ans[s-1][t-1] == float("inf"):
        print("-1")
    else:
        print(ans[s-1][t-1]-1)

### FIXED CODE ###

N, M, L = (int(i) for i in input().split())

import numpy as np
from scipy.sparse.csgraph import floyd_warshall
from scipy.sparse import csr_matrix

graph = [[0]*N for i in range(N)]
for i in range(M):
    a, b, c = (int(i) for i in input().split())
    graph[a-1][b-1] = c
    graph[b-1][a-1] = c

graph = csr_matrix(graph)
d = floyd_warshall(csgraph=graph, directed=False)

new_graph = [[0]*N for i in range(N)]
for i in range(N):
    for j in range(N):
        if d[i][j] <= L:
            new_graph[i][j] = 1
            new_graph[j][i] = 1

graph = csr_matrix(new_graph)
d = floyd_warshall(csgraph=graph, directed=False)
Q = int(input())
for i in range(Q):
    s, t = (int(i) for i in input().split())
    if d[s-1][t-1] == float("inf"):
        print("-1")
    else:
        print(int(d[s-1][t-1]-1))
### BUGGY CODE ###

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


def pow_k(x, n):
    """
    O(log n)
    """
    if n == 0:
        return 1

    K = 1
    while n > 1:
        if n % 2 != 0:
            K *= x
        x *= x
        n //= 2

    return K * x


total = pow_k(2, n) - 1

now = 1
div = 1
ans = 0
for i in range(max(a, b) + 1):
    now *= (n - i)
    div *= i + 1
    if i == a-1 or i == b-1:
        total -= now // div
print(total % (10 ** 9 + 7))

### FIXED CODE ###

n, a, b = map(int, input().split())
MOD = 10 ** 9 + 7
# Pythonのpowは繰り返し二乗法で実装されている
ans = pow(2, n, MOD) - 1
product = 1
div = 1
for i in range(1, b + 1):
    product = product * (n - i + 1) % MOD
    div = div * i % MOD
    if i == a or i == b:
        # 逆元を求める
        ans -= product * pow(div, MOD - 2, MOD)
print(ans % MOD)
### BUGGY CODE ###

import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())

S = input()

from functools import *

@lru_cache(None)
def rec(i, j):
    if abs(i - j) <= 1:
        return 0

    res = 0
    if S[i] != S[j - 1] and rec(i + 1, j - 1) == j - i - 2:
        res = j - i

    for m in range(i + 1, j):
        res = max(res, rec(i, m) + rec(m, j))
    return res

print(rec(0, len(S)))

### FIXED CODE ###

S = input()

ones = S.count('1')
zeros = S.count('0')
print(min(ones, zeros) * 2)
### BUGGY CODE ###

import math
from functools import reduce

N = int(input())

def gcd_list(nums):
    return reduce(math.gcd, nums)

total = 0

for a in range(N):
    for b in range(N):
        for c in range(N):
            total += gcd_list([a+1, b+1, c+1])

print(total)

### FIXED CODE ###

import math
from functools import reduce

N = int(input())

def gcd_list(nums):
    return reduce(math.gcd, nums)

total = 0

for a in range(N):
    for b in range(N):
        for c in range(N):
            total += gcd_list([a+1, b+1, c+1])

print(total)
### BUGGY CODE ###

import bisect
import numpy as np
n = int(input())
abc = [sorted(list(map(int, input().split()))) for i in range(3)]

unko = np.zeros(n)
for c in abc[2]:
  unko[:bisect.bisect_left(abc[1], c)] += 1
s = 0
for i,b in enumerate(abc[1]):
  s += bisect.bisect_left(abc[0], b)*unko[i]
  
print(int(s))

### FIXED CODE ###

import bisect
n = int(input())
abc = [sorted(list(map(int, input().split()))) for i in range(3)]
print(sum([bisect.bisect_left(abc[0], b)*(n-bisect.bisect_right(abc[2], b)) for b in abc[1]]))
### BUGGY CODE ###

#!/usr/bin/env python3
import sys
readline = sys.stdin.buffer.readline

N = int(readline())
A = list(map(int, readline().split()))
Q = int(readline())
answer = [0] * Q

for i in range(Q):
    B, C = map(int, readline().split())

    A = [(C if num == B else num) for num in A]
    answer[i] = sum(A)

print(*answer, sep='\n')


### FIXED CODE ###

#!/usr/bin/env python3
import sys
from collections import Counter
readline = sys.stdin.buffer.readline

N = int(readline())
A = list(map(int, readline().split()))
Q = int(readline())
d = Counter(A)

total = sum(A)

for _ in range(Q):
    B, C = map(int, readline().split())

    total -= B * d[B]
    total += C * d[B]

    d[C] += d[B]
    d[B] = 0

    print(total)