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

n, m = map(int,input().split())
sp = [input().split() for i in range(m)]
#print(sp)
x = [] #正解記録リスト
y = []
z = 0 #ペナルティ数カウンター

for i in range(m):
  if sp[i][1] == "AC" and int(sp[i][0]) not in x: #初正解の場合
    x.append(int(sp[i][0]))
    y.append(i)
    #print(x)

for i in range(m):
  if sp[i][1] == "WA": #不正解の場合
    if int(sp[i][0]) in x: #正解していたら
      a = x.index(int(sp[i][0]))
      if i < y[a]: #正解前だったら
        z = z + 1 #ペナルティとしてカウント

print(len(x), z)

### FIXED CODE ###

n, m = map(int,input().split())
li = [0] * n
wa = 0
ac = 0

for i in range(m):
    p, s = map(str, input().split())
    p = int(p)

    if s == "WA" and li[p-1] >= 0: #WAかつ正解前
        li[p-1] += 1 #その問題をWAした数+1
    
    elif s == "AC" and li[p-1] >= 0: #ACかつ正解前
        wa += li[p-1] #それまでにWAした数をカウントに追加
        li[p-1] = -1 #ステータスを正解済に変更
        ac += 1 #AC+1

print(ac, wa)
### BUGGY CODE ###

import heapq
def SME():
    A = []
    ANS = [None]*N
    C = set([i for i in range(N)])
    D = set([])
    parameter = 0.9997
    count = 0
    for i,x in enumerate(Whole):
        heapq.heappush(A,[x,i])
    for j in range(N):
        a = heapq.heappop(A)
        if None not in ANS[a[1]:a[1]+L] : continue
        for i in range(L):
            idx = a[1] + i
            try :
                if ANS[idx] : continue
                else :
                    ANS[idx] = a[0]
                    D.add(idx)
                    count += 1
            except IndexError : continue
        if count > N * parameter : break
    for i in C.difference(D):
        if i < L:
            ANS[i] = min(Whole[:i+1])
        else :
            ANS[i] = min(Whole[i+1-L:i+1])
    return ANS

[N,L] = [int(x) for x in input().split()]
Whole = [int(x) for x in input().split()]
ANS = SME()
for i in ANS[L-1:-1]:print(i, end=" ")
print(ANS[-1])

### FIXED CODE ###

def ascend(a,b):
    mini = min(A[a:a+L])
    ans[a] = mini
    for i in range(a+1,b):
        if A[i+L-1] <= mini : mini = A[i+L-1]
        elif A[i-1] == mini : mini = min(A[i:i+L])
        ans[i] = mini

def descend(a,b):
    mini = min(A[b-1:b+L-1])
    ans[b-1] = mini
    for i in range(b-2,a-1,-1):
        if A[i] <= mini : mini = A[i]
        elif A[i+L] == mini: mini = min(A[i:i+L])
        ans[i] = mini
    
[N,L] = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
ans = [None]*(N-L+1)
count = 0;
if N > 10 ** 4:
    for i in range(100):
        if A[i+1] > A[i] : count += 1
    if count > 80 : descend(0, N-L+1)
    else : ascend(0,N-L+1)
else :
    ascend(0, N-L+1)
for i in ans[:-1] : print(i, end=" ")
print(ans[-1])
### BUGGY CODE ###

N = int(input())
a = list(map(int, input().split()))
s = 0

for x in a:
    s = s ^ x

ans = ''
for x in a:
    ans = ans + str(s ^ x) + ' '
print(ans)

### FIXED CODE ###

N = int(input())
a = list(map(int, input().split()))
s = 0

for x in a:
    s ^= x

ans = ''
for x in a:
    ans += str(s ^ x) + ' '
print(ans)
### BUGGY CODE ###

from heapq import heappop, heappush

N, K = map(int, input().split())
td = sorted([list(map(int, input().split()))
             for _ in range(N)], key=lambda x: x[1], reverse=True)
q = []
v = []
cnt = 0
for t, d in td[:K]:
    cnt += d
    if t in v:
        heappush(q, d)
    else:
        v.append(t)
cnt += len(v) ** 2
ans = cnt
for t, d in td[K:]:
    if t not in v and len(q) != 0:
        x = heappop(q)
        cnt += d + 2 * len(v) + 1 - x
        v.append(t)
        ans = max(ans, cnt)
print(ans)


### FIXED CODE ###

from heapq import heappop, heappush

N, K = map(int, input().split())
td = sorted([list(map(int, input().split()))
             for _ in range(N)], key=lambda x: x[1], reverse=True)
q = []
v = set()
cnt = 0
for t, d in td[:K]:
    cnt += d
    if t in v:
        heappush(q, d)
    else:
        v.add(t)
cnt += len(v) ** 2
ans = cnt
for t, d in td[K:]:
    if t not in v and len(q) != 0:
        x = heappop(q)
        cnt += d + 2 * len(v) + 1 - x
        v.add(t)
        ans = max(ans, cnt)
print(ans)
### BUGGY CODE ###

import numpy as np


tflag = False
if tflag:
  N = 2000
  m = 1000000000
  x = np.random.randint(m)
  A = np.random.randint(m,size=(N))
#  st = time.time()
else:
  a = map(int,raw_input().split())
  N = a[0]
  x = a[1]
  A = map(int,raw_input().split())

E = np.zeros(N)
B = 1000000000*np.ones((N,N))
B[0] = np.array(A)
E[0] = sum(B[0])
for idx in xrange(1,N):
  B[idx] = np.array([min(B[idx-1][i], A[i-idx]) for i in range(N)])
  E[idx] = sum(B[idx])+idx*x

print int(min(E))

### FIXED CODE ###

import time
import numpy as np
import copy

tflag = False
if tflag:
  N = 2000
  m = 1000000000
  x = np.random.randint(m)
  A = list(np.random.randint(m,size=(N)))
  st = time.time()
else:
  N, x = map(int,raw_input().split())
  A = map(int,raw_input().split())

#B = copy.deepcopy(A)
B = A
A = A + A
m = sum(B)
for idx in xrange(1,N):
  for jdx in xrange(N):
    if B[jdx] > A[jdx+N-idx]:
      B[jdx] = A[jdx+N-idx]
#  A = np.append(A, A[0])
#  A = np.delete(A, 0)
#  B = np.fmin(B,A)
  m = min(sum(B)+idx*x, m)

print int(m)
### BUGGY CODE ###

from math import gcd
from functools import reduce
def gcd_list(xs):
    return reduce(gcd, xs)

n = int(input())
a = 0
for i in range(1, n+1):
    for j in range(1, n+1):
        for k in range(1, n+1):
            a += gcd_list([i,j,k])
print(a)

### FIXED CODE ###

from math import gcd

n = int(input())
a = 0
for i in range(1, n+1):
    for j in range(1, n+1):
        b = gcd(i, j)
        for k in range(1, n+1):
            a += gcd(k, b)
print(a)
### BUGGY CODE ###

import sys
input = sys.stdin.readline

D, G = [int(x) for x in input().strip().split()]
P = [''] * D
for d in range(D):
    p, c = [int(x) for x in input().strip().split()]
    P[d] = [0] + [(d + 1) * 100 * (i + 1) for i in range(p)]
    P[d][-1] += c

def f(P, x, D, a, c=[]):
    if x == D:
        if sum(c) >= G:
            return a
        else:
            return 10 ** 9 + 7
    ret = 10 ** 9 + 7
    for n in range(len(P[x])):
        t = c[:]
        t.append(P[x][n])
        ret = min(ret, f(P, x+1, D, a+n,t))
    return ret

print(f(P, 0, D, 0))

### FIXED CODE ###

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

D, G = [int(x) for x in input().strip().split()]
P = [''] * D
for d in range(D):
    p, c = [int(x) for x in input().strip().split()]
    P[d] = [0] + [(d + 1) * 100 * (i + 1) for i in range(p)]
    P[d][-1] += c

d = defaultdict(int)
def f(P, D, qnum, trial, d, flag=False, score=[]):
    if (qnum, trial, sum(score)) in d:
        return d[(qnum, trial, sum(score))]
    #print('qnum={}, trial={}, score={}'.format(qnum,trial,score))
    if qnum == D:
        if sum(score) >= G:
            #print('  OK : {}'.format(trial))
            return trial
        else:
            #print('  NG')
            return 10 ** 9 + 7
    ret = 10 ** 9 + 7
    if flag:
        N = [0, len(P[qnum])-1]
    else:
        N = range(len(P[qnum]))
    for n in N:
        flag2 = flag
        t = score[:]
        t.append(P[qnum][n])
        if not flag and n != len(P[qnum]) - 1 and n != 0:
            flag2 = True
        ret = min(ret, f(P, D, qnum+1, trial+n, d, flag2, t))
    d[(qnum, trial, sum(score))] = ret
    return ret

print(f(P, D, 0, 0, d))
### BUGGY CODE ###

def dijkstra(s,n,w,cost):
    #始点sから各頂点への最短距離
    #n:頂点数,　w:辺の数, cost[u][v] : 辺uvのコスト(存在しないときはinf)
    d = [float("inf")] * n
    used = [False] * n
    d[s] = 0
    
    while True:
        v = -1
        #まだ使われてない頂点の中から最小の距離のものを探す
        for i in range(n):
            if (not used[i]) and (v == -1):
               v = i
            elif (not used[i]) and d[i] < d[v]:
                v = i
        if v == -1:
               break
        used[v] = True
               
        for j in range(n):
               d[j] = min(d[j],d[v]+cost[v][j])
    return d

# n,w = map(int,input().split()) #n:頂点数　w:辺の数
n = int(input()) #n:頂点数　w:辺の数
w = n - 1

cost = [[float("inf") for i in range(n)] for i in range(n)] 
#cost[u][v] : 辺uvのコスト(存在しないときはinf この場合は10**10)
for i in range(w):
    x,y = map(int,input().split())
    x -= 1
    y -= 1
    cost[x][y] = 1
    cost[y][x] = 1

d0 = dijkstra(0,  n,w,cost)
d1 = dijkstra(n-1,n,w,cost)

f = 0
s = 0
for i in range(1,n-1):
    if d0[i] <= d1[i]:
        f +=1
    else:
        s += 1

if f > s:
    print('Fennec')
else:
    print('Snuke')
    




### FIXED CODE ###

import heapq

def dijkstra_heap(s):
    #始点sから各頂点への最短距離
    d = [float("inf")] * n
    used = [True] * n #True:未確定
    d[s] = 0
    used[s] = False
    edgelist = []
    for e in edge[s]:
        heapq.heappush(edgelist,e)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        #まだ使われてない頂点の中から最小の距離のものを探す
        if not used[minedge[1]]:
            continue
        v = minedge[1]
        d[v] = minedge[0]
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,[e[0]+d[v],e[1]])
    return d

# n,w = map(int,input().split()) #n:頂点数　w:辺の数
n = int(input()) #n:頂点数　w:辺の数
w = n - 1

edge = [[] for i in range(n)]
#edge[i] : iから出る道の[重み,行先]の配列
for i in range(w):
    x,y = map(int,input().split())
    x -= 1
    y -= 1
    edge[x].append([1,y])
    edge[y].append([1,x]) 

d0 = dijkstra_heap(0)
d1 = dijkstra_heap(n-1)

f = 0
s = 0
for i in range(1,n-1):
    if d0[i] <= d1[i]:
        f +=1
    else:
        s += 1

if f > s:
    print('Fennec')
else:
    print('Snuke')
### BUGGY CODE ###

N=int(input())
A=list(map(int,input().split()))
B=[A[i]-(i+1) for i in range(N)]

B.sort()
ans=float('inf')
for i in range(B[0],B[N-1]+1):
  val=0
  for j in range(N):
    val+=abs(B[j]-i)
  ans=min(ans,val)
print(ans)

### FIXED CODE ###

N=int(input())
A=list(map(int,input().split()))
B=[A[i]-(i+1) for i in range(N)]

B.sort()
b=B[N//2]
ans=0
for i in range(N):
  ans+=abs(B[i]-b)
print(ans)
### BUGGY CODE ###

n = int(input())
data = []
count = 0
for i in range(n):
    s = sorted(str(input()))
    if s in data:
        count += data.count(s)
    data.append(s)
print(count)

### FIXED CODE ###

from collections import Counter
n = int(input())
m = []
count = 0
for i in range(n):
    m.append("".join(sorted(str(input()))))
m = Counter(m)
ans = 0
for j in m.values():
    ans += j * (j-1) / 2
print(int(ans))
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10**8)
num,weight=map(int,input().split())
lv=[]
lw=[]
for i in range(num):
  a,b=map(int,input().split())
  lv.append(b)
  lw.append(a)
dp=[[-1 for i in range(weight+1)] for j in range(num)]
for i in range(weight+1):
  if i<lw[0]:
    dp[0][i]=0
  else:
    dp[0][i]=lv[0]
def nap(n,w):
  if dp[n][w]!=-1:
    return dp[n][w]
  else:
    res=0
    if lw[n]>w:
      res=nap(n-1,w)
    else:
      m1=nap(n-1,w)
      m2=nap(n-1,w-lw[n])+lv[n]   
      res=max(m1,m2)
    dp[n][w]=res
    return res
nap(num-1,weight)
print(dp[num-1][weight])


### FIXED CODE ###

import sys
sys.setrecursionlimit(10**8)
def knapsack(N,W,weight,value):
  #初期化
  inf=float("inf")
  dp=[[-inf for i in range(W+1)] for j in range(N+1)]
  for i in range(W+1): dp[0][i]=0

  #DP
  for i in range(N):
    for w in range(W+1):
      if weight[i]<=w: #dp[i][w-weight[i]の状態にi番目の荷物が入る可能性がある
        dp[i+1][w]=max(dp[i][w-weight[i]]+value[i], dp[i][w])
      else: #入る可能性はない
        dp[i+1][w]=dp[i][w]
  return dp[N][W]
n1,w1=map(int,input().split())
lw=[]
lv=[]
for i in range(n1):
  w,v=map(int,input().split())
  lw.append(w)
  lv.append(v)
print(knapsack(n1,w1,lw,lv))
### BUGGY CODE ###

N, M = map(int, input().split())
a = []
mod = 1000000007

for i in range(M):
    a.append(int(input()))

DP = [0]*(N+1)
DP[0] = 1
if 1 in a:
    DP[1] = 0
else:
    DP[1] = 1

for i in range(2, N+1):
    if(i in a):
        continue
    else:
        DP[i] = (DP[i-1]+DP[i-2])%mod
        
print(DP[N])

### FIXED CODE ###

import sys
input = sys.stdin.readline

N, M = map(int, input().split())
a = [0]*M
mod = 10**9+7

for i in range(M):
    a[i] = int(input())

a = set(a)
DP = [0]*(N+1)
DP[0] = 1
if 1 in a:
    DP[1] = 0
else:
    DP[1] = 1

for i in range(2, N+1):
    if(i not in a):
        DP[i] = (DP[i-1]+DP[i-2])%mod
        
print(DP[-1])
### BUGGY CODE ###

from collections import Counter
from itertools import groupby


def read_int():
    return int(input().strip())


def read_ints():
    return list(map(int, input().strip().split(' ')))


def ilen(ll):
    return sum(1 for _ in ll)


def solve():
    H, W, M = read_ints()
    hw = []
    for _ in range(M):
        hw.append(read_ints())
    hw.sort()

    max_ws = {}
    max_count_ws = 0
    for h, ws in groupby(hw, key=lambda pair: pair[0]):
        ws = list(ws)
        max_count_ws = max(max_count_ws, len(ws))
        max_ws[h] = [w for _, w in ws]

    ks = list(max_ws.keys())
    for k in ks:
        if len(max_ws[k]) < max_count_ws:
            del max_ws[k]

    hw.sort(key=lambda pair: pair[1])
    max_hs = {}
    max_count_hs = 0
    for w, hs in groupby(hw, key=lambda pair: pair[1]):
        hs = list(hs)
        max_count_hs = max(max_count_hs, len(hs))
        max_hs[w] = [h for h, _ in hs]

    ks = list(max_hs.keys())
    for k in ks:
        if len(max_hs[k]) < max_count_hs:
            del max_hs[k]

    counter = Counter()
    for v in max_hs.values():
        counter += Counter(v)
    for k in max_ws.keys():
        if counter[k] != len(max_hs):
            return max_count_ws+max_count_hs
    return max_count_ws+max_count_hs-1


if __name__ == '__main__':
    print(solve())


### FIXED CODE ###

from collections import Counter
from itertools import groupby


def read_int():
    return int(input().strip())


def read_ints():
    return list(map(int, input().strip().split(' ')))


def ilen(ll):
    return sum(1 for _ in ll)


def solve():
    H, W, M = read_ints()
    hw = []
    for _ in range(M):
        hw.append(tuple(read_ints()))
    hw.sort()

    rows = set()
    max_count_row = 0
    for h, ws in groupby(hw, key=lambda pair: pair[0]):
        count = ilen(ws)
        if count == max_count_row:
            rows.add(h)
        elif count > max_count_row:
            rows = set([h])
            max_count_row = count

    hw.sort(key=lambda pair: pair[1])
    cols = set()
    max_count_col = 0
    for w, hs in groupby(hw, key=lambda pair: pair[1]):
        count = ilen(hs)
        if count == max_count_col:
            cols.add(w)
        elif count > max_count_col:
            cols = set([w])
            max_count_col = count

    hw = set(hw)
    for h in rows:
        for w in cols:
            if (h, w) not in hw:
                return max_count_row+max_count_col
    return max_count_row+max_count_col-1


if __name__ == '__main__':
    print(solve())
### BUGGY CODE ###

import sys

stdin = sys.stdin

ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split()))
rs = lambda: stdin.readline().rstrip()  # ignore trailing spaces

#１回のクエリで半分に空席がある場所を絞る
end = ri() - 1
print(0, flush=True)
S = input().rstrip()[0]
if S == 'V':
    exit()
start = 0
cur = S
for _ in range(19):
    target = start + max(2, (end - start) // 4 * 2)
    print(target, flush=True)
    s = input().rstrip()[0]
    if s == 'V':
        exit()
    if cur != s:
        end = target
    else:
        start = target
print(start + 1, flush=True)
s = input().rstrip()[0]
if s == 'V':
    exit()
else:
    print(start + 2, flush=True):
    exit()
#24


### FIXED CODE ###

N = int(input())

def ask(n):
    print(n, flush=True)
    resp = input().rstrip()[0]
    if resp == 'V':
        exit()
    return resp

def condition(L,R,resp_L,resp_R):
    if (R-L)&1:
        return resp_L == resp_R
    else:
        return resp_L != resp_R

resp = ask(0)

L = 0
R = N
resp_L = resp
resp_R = resp
for _ in range(19):
    M = (L + R) // 2
    resp_M = ask(M)
    if condition(L,M,resp_L,resp_M):
        R,resp_R = M,resp_M
    else:
        L,resp_L = M,resp_M
### BUGGY CODE ###

N = int(input())
A = list(map(int,input().split()))
P = 10**9+7
ans = 0

for i in range(N):
    temp = 0
    for j in range(i+1,N):
        temp += A[j]
    temp *= A[i]
    ans += temp%P
print(ans%P)

### FIXED CODE ###

N = int(input())
A = list(map(int,input().split()))
P = 10**9+7
ans = 0
S = sum(A)

for i in range(N):
    S -= A[i]
    ans += A[i]*S%P
print(ans%P)
### BUGGY CODE ###

s = int(input())

x1, y1 = 0, 0
x2, y2, x3, y3 = 0, 0, 0, 0

while True:
    if s >= 10e9:
        for i in range(min(int(s**0.5), 10e9), 0, -1):
            if s % i == 0 and i <= 10e9 and s / i <= 10e9:
                x2 = i
                y3 = s//i
                break
    else:
        for i in range(1, min(int(s**0.5), 10e9) + 1):
            if s % i == 0 and i <= 10e9 and s / i <= 10e9:
                x2 = i - x1
                y3 = s//i
                break

    if x2 == 0 and y3 == 0:
        y2 = -1
        x3 += 1
        s -= x3
        continue
    break

print(x1, y1, x2, y2, x3, y3)


### FIXED CODE ###

s = int(input())

e = 10**9
if s == e**2:
    print(0, 0, e, 1, 0, e)
else:
    y = s//e + 1
    x = y*e - s
    print(0, 0, e, 1, x, y)
### BUGGY CODE ###

N = int(input())
p = 1
for i in range(1,N+1):
	p = p*i
print(p%(10**9+7))
	

### FIXED CODE ###

N = int(input())
p = 1
for i in range(1,N+1):
	p = (p*i)%(10**9+7)
print(p%(10**9+7))
### BUGGY CODE ###

n, k = map(int, input().split())
a = list(map(int, input().split()))
s = 1
for i in range(k):
    s *= a[i]
# s = sum(a[:k])
pre = s
for i in range(1,n-k+1):
    s /= a[i-1]
    s *= a[i+k-1]
    if s > pre:
        print("Yes")
    else:
        print("No")
    pre = s




### FIXED CODE ###

n, k = map(int, input().split())
a = list(map(int, input().split()))
# s = 1
# for i in range(k):
#     s *= a[i]
# # s = sum(a[:k])
# pre = s
for i in range(1,n-k+1):
    if a[i+k-1]/a[i-1]>1:
        print("Yes")
    else:
        print("No")
### BUGGY CODE ###

N = input()
A = list(map(int,input().split()))
ans = A[0]
for i in range(1,len(A)):
  ans = ans * A[i]
if ans <= 10 ** 18:
  print(ans)
else:
  print('-1')

### FIXED CODE ###

def main ():
  N = int(input())
  A = list(map(int,input(). split ()))
  if 0 in A:
    print(0)
    return
  
  prod = 1
  for a in A:
    prod *= a
    if prod > 1000000000000000000:
      print(-1)
      return
  print(prod)
  
main ()
### BUGGY CODE ###

N, K = map(int, input().split())
H = [int(N) for N in input().split()]
for i in range(0, K):
    if H[H.index(max(H))] == 0:
        break
    H[H.index(max(H))] = 0
print(sum(filter(lambda x: x != 0, H)))


### FIXED CODE ###

N, K = map(int, input().split())
H = [int(N) for N in input().split()]
H.sort(reverse=True)
if N <= K:
    print(0)
else:
    for i in range(0, K):
        H[i] = 0
    print(sum(H))
### BUGGY CODE ###

N, W = map(int, input().split())
dp = [[0 for _ in range(W + 1)] for _ in range(N + 1)]

for i in range(1, N + 1):
    w, v = map(int, input().split())
    for j in range(0, W + 1):
        if j < w:
            dp[i][j] = dp[i - 1][j]
        else:
            dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w] + v)

# print(dp)
print(max(dp[N]))


### FIXED CODE ###

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

dp = [[0 for _ in range(W + 1)] for _ in range(N + 1)]
# dp[n][w] = n番目までの品物による重量w以下の価値の総和の最大値

for i in range(1, N + 1):
    w, v = map(int, input().split())
    for j in range(W + 1):
        if j - w >= 0:
            dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w] + v)
        else:
            dp[i][j] = dp[i - 1][j]

print(dp[N][W])
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10 ** 9)

N, M = map(int, input().split())
root = [-1] * N

def parent(x):
    if root[x] < 0:
        return x
    return parent(root[x])


def union(x, y):
    x_parent = parent(x)
    y_parent = parent(y)
    if x_parent == y_parent:
        return
    root[x_parent] += root[y_parent]
    root[y_parent] = x


def size(x):
    return -root[x]
    

for i in range(M):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    union(a, b)


max_size = 0
for i in range(N):
    max_size = max(size(i), max_size)

print(max_size)

### FIXED CODE ###

import sys
sys.setrecursionlimit(10 ** 9)

n, m = map(int, input().split())
root = [-1] * n

def r(x):
    if root[x] < 0:
        return x
    else:
        root[x] = r(root[x])
        return root[x]
    

def unite(x, y):
    x = r(x)
    y = r(y)
    if x == y:
        return
    root[x] += root[y]
    root[y] = x
    

def size(x):
    x = r(x)
    return -root[x]


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

"""
max_size = 0
for i in range(n):
    max_size = max(size(i), max_size)
"""
max_size = max(map(size, range(n)))

print(max_size)
### BUGGY CODE ###

def main():

    s = input()
    q = int(input())

    query = []
    flag = 0
    ctn = 0
    for _ in range(q):
        temp = input().split()
        if len(temp) == 1:
            if flag == 0:
                flag = 1
            else:
                flag = 0
            ctn += 1
        else:
            query.append(temp + [flag])

    for q in query:
        if q[-1] == 0:
            if q[1] == "1":
                s = q[2] + s
            else:
                s = s + q[2]
        else:
            if q[1] == "1":
                s = s + q[2]
            else:
                s = q[2] + s

    if ctn % 2 == 1:
        print(s[::-1])
    else:
        print(s)

main()


### FIXED CODE ###

from collections import deque

def main():

    s = deque(input())
    q = int(input())

    flag = 0
    query = []

    for _ in range(q):
        temp = input().split()
        if len(temp) == 1:
            flag += 1
        else:
            query.append(temp + [flag%2])

    for q in query:
        if q[-1] == 0:
            if q[1] == "1":
                s.appendleft(q[2])
            else:
                s.append(q[2])
        else:
            if q[1] == "1":
                s.append(q[2])
            else:
                s.appendleft(q[2])

    if flag % 2 == 1:
        print("".join(list(s)[::-1]))
    else:
        print("".join(s))

main()
### BUGGY CODE ###

# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

R,G,B,N=MAP()

cnt=0
for x in range(3001):
    for y in range(3001):
        z=(N-R*x-G*y)/B
        # zが0以上の整数になっていればOK
        if z>=0 and z.is_integer():
            cnt+=1
print(cnt)


### FIXED CODE ###

# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

R,G,B,N=MAP()

cnt=0
for x in range(3001):
    for y in range(3001):
        z=(N-R*x-G*y)/B
        # zが0以上の整数になっていればOK
        if z>=0 and z.is_integer():
            cnt+=1
print(cnt)
### BUGGY CODE ###

size, jumps = map(int, input().split())
array = list(map(int, input().split()))
dp = [999999999] * size
dp[0]= 0
dp[1] = abs(array[0]- array[1])
for i in range(2,size):
    dp[i] = min(abs(array[i] - array[j]) + dp[j] for j in range(max(0, i - jumps), i))
print(dp[size-1])

### FIXED CODE ###

import sys


def input():
    return sys.stdin.readline().strip()


def main():
    size, jumps = map(int, input().split())
    array = list(map(int, input().split()))
    dp = [999999999] * size
    dp[0]= 0
    dp[1] = abs(array[0]- array[1])
    for i in range(2,size):
        dp[i] = min(abs(array[i] - array[j]) + dp[j] for j in range(max(0, i - jumps), i))
    print(dp[size-1])

if __name__ == "__main__":
    main()