Harryxun/stf_run huggingface dataset:
import sys
from collections import defaultdict(int)

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n, m = map(int, input().split())
    X = list(map(int, input().split()))

    MOD = [defaultdict(int) for _ in range(m + 1)]
    cnt = defaultdict(int)
    for x in X:
        MOD[x % m][x] += 1
        cnt[x % m] += 1

    res = cnt[0] // 2
    if m % 2 == 0:
        res += cnt[m // 2] // 2
        
    for i in range(1, (m + 1) // 2):
        res += min(cnt[i], cnt[m - i])

        if cnt[i] > cnt[m - i]:
            diff = (cnt[i] - cnt[m - i]) // 2
            for v in MOD[i].values():
                if diff == 0:
                    break
                t = min(diff, v // 2)
                diff -= t
                res += t
        elif cnt[i] < cnt[m - i]:
            diff = (cnt[m - i] - cnt[i]) // 2
            for v in MOD[m - i].values():
                if diff == 0:
                    break
                t = min(diff, v // 2)
                diff -= t
                res += t

    print(res)


if __name__ == '__main__':
    resolve()


### FIXED CODE ###

import sys
from collections import defaultdict

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n, m = map(int, input().split())
    X = list(map(int, input().split()))

    MOD = [defaultdict(int) for _ in range(m + 1)]
    cnt = defaultdict(int)
    for x in X:
        MOD[x % m][x] += 1
        cnt[x % m] += 1

    res = cnt[0] // 2
    if m % 2 == 0:
        res += cnt[m // 2] // 2

    for i in range(1, (m + 1) // 2):
        res += min(cnt[i], cnt[m - i])

        if cnt[i] > cnt[m - i]:
            diff = (cnt[i] - cnt[m - i]) // 2
            for v in MOD[i].values():
                if diff == 0:
                    break
                t = min(diff, v // 2)
                diff -= t
                res += t
        elif cnt[i] < cnt[m - i]:
            diff = (cnt[m - i] - cnt[i]) // 2
            for v in MOD[m - i].values():
                if diff == 0:
                    break
                t = min(diff, v // 2)
                diff -= t
                res += t

    print(res)


if __name__ == '__main__':
    resolve()
import collections

n = input()
line = input().split()
odd = line[::2]
even = line[1::2]
o = collections.Counter(odd)
e = collections.Counter(even)
maxoddcnt = o.most_common()[0][1]
maxevencnt = e.most_common()[0][1]

#すべての値が同じときは半分を置換する
if odd == even:
    print(len(odd))
    exit(0)

#奇数列，偶数列それぞれの最頻の文字が同じ場合は，いずれかを2番目に多い文字に置換しなければならない
if o.most_common()[0][0] == e.most_common()[0][0]:
    if len(o.most_common()) > 1 and len(e.most_common()) <= 1:
        maxoddcnt = o.most_common()[1][1]
    if len(o.most_common()) <= 1 and len(e.most_common()) > 1:
        maxevencnt = e.most_common()[1][1]
    if o.most_common()[1][1] + e.most_common()[0][1] >= o.most_common()[0][1] + e.most_common()[1][1]:
        maxoddcnt = o.most_common()[1][1]
        input()
    else: #if o.most_common()[1][1] + e.most_common()[0][1] <= o.most_common()[0][1] + e.most_common()[1][1] :
        maxevencnt = e.most_common()[1][1]

print(len(odd)-maxoddcnt+len(even)-maxevencnt)


### FIXED CODE ###

import collections

n = input()
line = input().split()
odd = line[::2]
even = line[1::2]
o = collections.Counter(odd)
e = collections.Counter(even)
maxoddcnt = o.most_common()[0][1]
maxevencnt = e.most_common()[0][1]

#すべての値が同じときは半分を置換する
if odd == even and len(o.most_common()) == 1:
    print(len(odd))
    exit(0)

#奇数列，偶数列それぞれの最頻の文字が同じ場合は，いずれかを2番目に多い文字に置換しなければならない
if o.most_common()[0][0] == e.most_common()[0][0]:
    if len(o.most_common()) > 1 and len(e.most_common()) <= 1:
        maxoddcnt = o.most_common()[1][1]
    if len(o.most_common()) <= 1 and len(e.most_common()) > 1:
        maxevencnt = e.most_common()[1][1]
    if o.most_common()[1][1] + e.most_common()[0][1] >= o.most_common()[0][1] + e.most_common()[1][1]:
        maxoddcnt = o.most_common()[1][1]
    else: #if o.most_common()[1][1] + e.most_common()[0][1] <= o.most_common()[0][1] + e.most_common()[1][1] :
        maxevencnt = e.most_common()[1][1]

print(len(odd)-maxoddcnt+len(even)-maxevencnt)
n=int(input())
a=list(map(int,input().split()))
import numpy as np
dp=np.zeros((3,n+1),np.int64)
dp[:,0]=0
dp[1,1]=a[0]
for i in range(2,n+1):
    l,r=n//2-(n-i+1)//2,i//2+1
    if i%3==2:
      now,pre,prepre=2,1,0
    elif i%3==0:
      now,pre,prepre=0,2,1
    elif i%3==1:
      now,pre,prepre=1,0,2
    dp[now,l:r]=np.maximum(dp[pre,l:r],dp[prepre,l-1:r-1]+a[i-1])
print(dp[now,n//2])

### FIXED CODE ###

from collections import defaultdict

n=int(input())
a=list(map(int,input().split()))
#n=2*pow(10,5)
#a=range(1,pow(10,5)+1)
# dp[i][j]:i番目の数字の中からj個数字を取った時の最大。(i//2)-1<=j<=(i+1)//2
# 求めるのはdp[n][n//2]
dp=defaultdict(lambda:-pow(10,18))
dp[(1,1)]=a[0]
for i in range(2,n+1): #i番目の数字はa[i-1]
      for j in range(i//2-1,(i+1)//2+1):
            if j==1:
                  dp[(i,j)]=max(dp[(i-1,1)],a[i-1])
            elif j==0:
                  dp[(i,j)]=0
            else:
                  dp[(i,j)]=max(dp[(i-2,j-1)]+a[i-1],dp[(i-1,j)])
print(dp[(n,n//2)])
length_list = []
for _ in range(n):
    x = int(input().split()[0])
    length_list.append(x)

print(len(list(set(length_list))))


### FIXED CODE ###

n = int(input().split()[0])
length_list = []
for _ in range(n):
    x = int(input().split()[0])
    length_list.append(x)

print(len(list(set(length_list))))
import numpy as np

X,N = map(int,input().split())
p = input()

if N != 0:
    
    p_list = [int(n) for n in p.split()]
    
    X_near = np.array(list(range(int(np.ceil(X - N/2)), int(np.floor(X + N/2)) + 1)))
 
    remain_list = []
 
    for i in X_near:
        if i not in p_list:
            remain_list.append(i)
        

    remain_array = np.array(remain_list)
    min_bool = np.abs(X - remain_array).min() == np.abs(X - remain_array)
    print(remain_array[min_bool].min())
    
else:
    print(X)

### FIXED CODE ###

import numpy as np
 
X,N = map(int,input().split())
p = input()
 
if N != 0:
    
    p_list = [int(n) for n in p.split()]
    
    X_near = np.array(list(range(int(np.ceil(X - N/2 - 1)), int(np.floor(X + N/2)) + 2)))
 
    remain_list = []
 
    for i in X_near:
        if i not in p_list:
            remain_list.append(i)
        
 
    remain_array = np.array(remain_list)
    min_bool = np.abs(X - remain_array).min() == np.abs(X - remain_array)
    print(remain_array[min_bool].min())
    
if N == 0:
    print(X)
array = [0 for i in range(11)]
N = int(input())
A = list(map(int, input().split()))
for a in A:
    array[a] += 1

souwa = sum(A)
Q = int(input())
for i in range(Q):
    B, C = map(int, input().split())
    souwa = souwa + (C-B)*array[B]
    array[C] += array[B]
    array[B] = 0
    print(souwa)


### FIXED CODE ###

array = [0 for i in range(100001)]
N = int(input())
A = list(map(int, input().split()))
Q = int(input())

for a in A:
    array[a] += 1
souwa = sum(A)

B = []
C = []
for i in range(Q):
    b, c = map(int, input().split())
    B.append(b)
    C.append(c)

for i in range(Q):
    souwa = souwa + (C[i]-B[i])*array[B[i]]
    array[C[i]] += array[B[i]]
    array[B[i]] = 0
    print(souwa)
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define REP(i, n)    for (ll (i) = 0;   (i) < (n); ++(i))
#define FOR(i, a, b) for (ll (i) = (a); (i) < (b); ++(i))

signed main() {

    int a, b;
    cin >> a >> b;
    if (a > b) cout << "safe" << endl;
    else cout << "unsafe" << endl;

}


### FIXED CODE ###

s,w = map(int,input().split())
if s <= w:
    print('unsafe')
else:
    print('safe')
# 期待値
def func_d(n):
    if n == 1:
        return 1
    else:
        return n + func_d(n-1)

N, K = [int(i) for i in input().split()]
P = [func_d(int(i))/int(i) for i in input().split()]

# 累積和
A = [0]
for i in range(0, N):
    A.append(P[i]+A[i])

ans = []
for i in range(K, N):
    ans.append(A[i] - A[i-K])
    
print(max(ans))

### FIXED CODE ###

def func(x):
    return (x+1)/2
N, K = [int(i) for i in input().split()]
P = [func(int(i)) for i in input().split()]

# 累積和
A = [0]
for i in P:
    A.append(A[-1]+i)

ans = []
for i in range(K, N+1):
    ans.append(A[i] - A[i-K])
    
print(max(ans))
N = int(input())
K = int(input())
x = [int(input()) for i in range(N)]
ans = 0
for i in range(N):
    ans += min(2x[i], 2*(K-x[i]))
print(ans)

### FIXED CODE ###

N = int(input())
K = int(input())
if N > 1:
  x = list(map(int, input().split()))
  ans = 0
  for i in range(N):
    ans += min(2*x[i], 2*(K-x[i]))
else: 
  x = int(input())
  ans = min(2*x, 2*(K-x))
print(ans)
N = map(int, input().split())
A = [list(map(int, input().split())) for i in range(2)]
max_candies = 0
for i in range(N):
    candies = sum(A[0][:i+1] + A[1][i:])
    if max_candies < candies:
        max_candies = candies
print(max_candies)

### FIXED CODE ###

N = int(input())
A = [list(map(int, input().split())) for i in range(2)]
max_candies = 0
for i in range(N):
    candies = sum(A[0][:i+1] + A[1][i:])
    if max_candies < candies:
        max_candies = candies
print(max_candies)
def e():
    import numpy as np

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

    dp = np.zeros(W+1, dtype=np.int64)

    for _ in range(N):
        w, v = map(int, input().split())
        # print('_, dp[:-w]+v, dp[w:], out=dp[w:] = {}, {}, {}, {}'.format(_, dp[:-w]+v, dp[w:], dp[w:]))
        np.maximum(dp[:-w]+v, dp[w:], out=dp[w:])

    print(dp[-1])


##########
if __name__ == "__main__":
    e()


### FIXED CODE ###

def b():
    def solve(N, W, V, wv):
        dp = [10**10] * (V+1)
        dp[0] = 0

        for i in range(N):
            wi = wv[i][0]
            vi = wv[i][1]

            # 降順でやらないと複数回アイテムをカウントしちゃう（j1 + wi をやった後に j2 + wi をやると、 j1 + wi == j2 のときに2カウントになってしまう）
            for v in range(V - vi, -1, -1):
                if dp[v] == -1:
                    continue
                if dp[v+vi] > dp[v] + wi:
                    dp[v+vi] = dp[v] + wi

        light = [i for i, w in enumerate(dp) if w <= W]
        return light[-1]

    N, W = map(int, input().split())
    wv = []
    V = 0

    for i in range(N):
        w, v = map(int, input().split())
        wv.append([w, v])
        V += v

    print(solve(N, W, V, wv))

if __name__ == "__main__":
    b()
S=input();print('TBD'if S[5]*4+S[6]>=4 else'Heisei')

### FIXED CODE ###

S=input();print('TBD'if int(S[5])*5+int(S[6])>=5else'Heisei')
num = input().split(" ")
[i = int(i) for i in num]

if num[0]%num[1]==0:
  print("-1")
else:
  print(num[0])

### FIXED CODE ###

num = input().split(" ")
num = [int(i) for i in num]
if num[0]%num[1]==0: print("-1")
else: print(num[0])
s = input().split()
s5 = s.count("5")
s7 = s.count("7")
    
if s5 = 2 :
    if s7 = 1 :
        print("YES")
    else :
        print("NO")
else:
    print("NO")

### FIXED CODE ###

s = input().split()
s5 = s.count("5")
s7 = s.count("7")
 
if s5 == 2:
    if s7 == 1:
  	    print("YES")
    else:
        print("NO")
else:
    print("NO")
N, M = map(int,input().split())
answer = [0]*M
ae = 0

for i in range(M):
  s,c = map(int,input().split())
  if answer[-s] == 0:
    answer[-s] = c
  elif answer[-s] != 0 and answer[-s] != c:
    print(-1)
    ae = 1
    break
if ae == 0 and answer[M-1] == 0 and M != 1:
  print(-1)
  ae = 1
elif ae == 0:
  for i in range(M):
    print(answer[-(i+1)],end='')

### FIXED CODE ###

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

for i in range(M):
  s,c = map(int,input().split())
  if answer[-s] == -1:
    answer[-s] = c
  elif answer[-s] != -1 and answer[-s] != c:
    print(-1)
    ae = 1
    break
for i in range(N):
  if answer[i] == -1:
    if i == N-1 and N > 1:
      answer[i] = 1
    else:
      answer[i] = 0
if ae == 0 and answer[N-1] == 0 and N != 1:
  print(-1)
  ae = 1
elif ae == 0:
  for i in range(N):
    print(answer[-(i+1)],end='')
n = int(input())
a = []
a.append([0,0])
for i in range(n):
    in1, in2 = map(float, input().split())
    a.append([in1,in2])

ans = 0

for i in range(n):
    if a[i][1] == 'BTC':
        ans += a[i][0]*380000
    else :
        ans += a[i][0]
print(ans)

### FIXED CODE ###

n = int(input())
a = []
a.append([0,0])
for i in range(n):
    in1, in2 = map(str, input().split())
    a.append([float(in1),in2])

ans = 0

for i in range(n):
    j = i + 1
    if a[j][1] == 'BTC':
        ans += a[j][0]*380000
    else :
        ans += a[j][0]
print(ans)
import sys
sys.setrecursionlimit(100000)

N,K=map(int,input().split())
MOD=10**9+7
branch=[[] for _ in range(N)]
for i in range(N-1):
  a,b=map(int,input().split())
  branch[a-1].append(b-1)
  branch[b-1].append(a-1)

P=[-1]*(K-1)
P[K-2]=K-2
for i in range(K-3,0,-1):
  P[i]=P[i+1]*i%MOD
  
def PPP(start,num):
  if start==K-2:
    return P[start-num+1]
  if start==K-1:
    return P[start-num+2]*(K-1)%MOD
  
ans=1

def draw(parent,nord):
  global ans
  children=len(branch[nord])-(parent !=-1)
  if children==0:
    return
  #(K-2)*(K-1)*... をchildrenの数だけ掛ける。
  ans=ans*PPP(K-1-(parent !=-1),children)%MOD
  for b in branch[nord]:
    if b != parent:
      draw(nord,b)

for i in range(len(branch[0])+1):
  ans=ans*(K-i)%MOD
for bra in branch[0]:
  draw(0,bra)
print(ans)

### FIXED CODE ###

import sys
sys.setrecursionlimit(100000)

N,K=map(int,input().split())
MOD=10**9+7
branch=[[] for _ in range(N)]
for i in range(N-1):
  a,b=map(int,input().split())
  branch[a-1].append(b-1)
  branch[b-1].append(a-1)

if K==1:
  if N==1:
    print(1)
  else:
    print(0)
  exit()
  
P=[0]*(K-1)
P[K-2]=K-2
for i in range(K-3,0,-1):
  P[i]=P[i+1]*i%MOD
  
def PPP(start,num):
  if start<num:
    return 0
  if start==K-2:
    return P[start-num+1]
  if start==K-1:
    return P[start-num+2]*(K-1)%MOD
  
ans=1

def draw(parent,nord):
  global ans
  children=len(branch[nord])-(parent !=-1)
  if children==0:
    return
  #(K-2)*(K-1)*... をchildrenの数だけ掛ける。
  ans=ans*PPP(K-1-(parent !=-1),children)%MOD
  for b in branch[nord]:
    if b != parent:
      draw(nord,b)

for i in range(len(branch[0])+1):
  ans=ans*(K-i)%MOD
for bra in branch[0]:
  draw(0,bra)
print(ans)
def gcd(a, b):
    if a < b:
        a, b = b, a
    if b == 0:
        return a
    c = a % b
    return gcd(b, c)

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

d = gcd(N, M)
n = N // d
m = M // d
f = True

for i in range(1, d):
    if S[n*i] != T[m*i]:
        f = false
        break

print(N * M // d if f else -1)

### FIXED CODE ###

from fractions import gcd
 
 
def solve(n, m, s, t):
    g = gcd(n, m)
    ng = n // g
    mg = m // g
 
    for h in range(g):
        if s[ng * h] != t[mg * h]:
            return -1
    return ng * mg * g
 
 
n, m = list(map(int, input().split()))
s = input()
t = input()
print(solve(n, m, s, t))
s=input()
k=int(input())
if len(s) <=k:
  print(s)
else:
  print(s[:k] + '...')

### FIXED CODE ###

s=int(input())
k=input()
if len(k) <=s:
  print(k)
else:
  print(k[:s] + '...')
n,x,t = map(int, input().split())
if n%x=0:
  print((n//x)*t)
else:
  print((n//x + 1)*t)

### FIXED CODE ###

n,x,t = map(int, input().split())
if n%x==0:
  print((n//x)*t)
else:
  print((n//x + 1)*t)
K= int(input())
A, B = map(int, input().split())
sum=0
for(int i = A; i <= B; i++)
    if i%K==0:
        sum+=1
if sum>=1:
    print('OK')
else:
    print('NG')

### FIXED CODE ###

K= int(input())
A, B = map(int, input().split())

sum=0
for i in range(A,B+1,1):
    
    if i%K==0:
        sum+=1
if sum>=1:
    print('OK')
else:
    print('NG')
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
a = [0]*(N+1)
b = [0]*(M+1)
X = 0
Y = 0
cnt = 0
for n in range(N):
    a[n+1] = a[n] + A[n]
for n in range(M):
    b[n+1] = b[n] + B[n]
for n in range(1, N+1):
    if a[n] <= K:
        X += 1
    else:
        break
for m in range(1, M+1):
    if b[m] <= K:
        Y += 1 
    else:
        break

if a[X] < b[Y]:
    K = K - a[X]
    cnt += X
    if K - b[Y] == 0:
        cnt += Y
    else:
        for y in range(1, Y):
            if b[y] <= K:
                cnt += 1
            else:
                break
else:
    K = K - b[Y]
    cnt += Y
    if K - b[X] == 0:
        cnt += X
    else:
        for x in range(1, X):
            if b[x] <= K:
                cnt += 1
            else:
                break
print(cnt)

### FIXED CODE ###

N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
a = [0]*(N+1)
b = [0]*(M+1)
for n in range(N):
    a[n+1] = a[n] + A[n]
for n in range(M):
    b[n+1] = b[n] + B[n]
ans, j = 0, M
for i in range(N + 1):
    if a[i] > K:
        break
    while b[j] > K - a[i]:
        j -= 1
    ans = max(ans, i + j)
print(ans)
n, k = map(int, input().split())
p = list(map(int, input().split()))
ans = []
for i in range(1, n-k+1):
    ans.append(sum(p[(i):(k+i)]))
print(print((k+max(ans))/2))

### FIXED CODE ###

n, k = map(int, input().split())
p = list(map(int, input().split()))
tmp = sum(p[:k])
ans = [tmp]
if n == k:
    ans_mae = ans[0]
    print((ans_mae+k)/2)
else:
    for i in range(1,n-k+1):
        tmp = tmp - p[i-1] + p[i+k-1]
        ans.append(tmp)
    print((max(ans)+k)/2)
N = int(input())
C = [int(input()) for _ in range(N)]
MOD = 10**9 + 7

if N <= 2:
    print(1)
    exit()

dp = [1,1]
last_idx = [-1]*(N+1)
last_idx[C[0]] = 0
last_idx[C[1]] = 1

for i in range(2, N):
    if C[i-1] == C[i] or last_idx[C[i]] == -1:
        dp.append(dp[-1] % MOD)
    else:
        dp.append((dp[-1] + dp[last_idx[C[i]]]) % MOD)
    last_idx[C[i]] = i

print(dp[-1])

### FIXED CODE ###

N = int(input())
C = [int(input()) for _ in range(N)]
MOD = 10**9 + 7

if N <= 2:
    print(1)
    exit()

dp = [1,1]
last_idx = [-1]*(N+10)
last_idx[C[0]] = 0
last_idx[C[1]] = 1

for i in range(2, N):
    if C[i-1] == C[i] or last_idx[C[i]] == -1:
        dp.append(dp[-1] % MOD)
    else:
        dp.append((dp[-1] + dp[last_idx[C[i]]]) % MOD)
    last_idx[C[i]] = i

print(dp[-1])
N,A,B=map(int,input().split())
souryou=0
for i in range(N):
  j=i+1
  j_str=str(j)
  goukei=0
  for k in range(len(j_str)):
    goukei=goukei+int(j_str[k])
  if goukei>=A&&goukei<=B:
    souryou=souryou+j
print(str(souryou))

### FIXED CODE ###

N,A,B=map(int,input().split())
souryou=0
for i in range(N):
  j=i+1
  j_str=str(j)
  goukei=0
  for k in range(len(j_str)):
    goukei=goukei+int(j_str[k])
  if goukei>=A and goukei<=B:
    souryou=souryou+j
print(str(souryou))
N = int(input())
A = input().split(" ")
A = [int(i) for i in A]
list = [min(A)]
A = sorted(A)
iii = 0
while True:
    iii += 1
    if A[N-1] % A[N-2] != 0:
        A[N-1] = A[N-1] % A[N-2]
    elif N >= 3 and A[N-1] % A[N-3] != 0:
        A[N-1] = A[N-1] % A[N-3]

    A = sorted(A)
    list.append(min(A))
    if iii > 32:
        break       
#print(list)
for i in range(N-1):
    if A[i+1]-A[i] > 0:
        list.append(A[i+1]-A[i])
#print(list)
print(min(list))

### FIXED CODE ###

A,B = map(int,input().split(" "))
if B % A == 0:
    print(A + B)
else:
    print(B - A)
N = int(input())
S = list(input().strip())

r = S.count('R')
w = S.count('W')

ans = min(r, w)

ri = S.index('R')
r2 = S[ri:].count('W')

ans = min(ans, r2)
print(ans)



### FIXED CODE ###

N = int(input())
S = list(input().strip())

l = 0
r = N-1
ans = 0
while l < r:
    if S[l] == 'W' and S[r] == 'R':
        ans += 1
        l += 1
        r -= 1
    while l < N and S[l] != 'W':
        l += 1
    while r >= 0 and S[r] != 'R':
        r -= 1

print(ans)
li = map(int, input().split())
li.sort()
if li[0] + li[1] == li[2]:
	print('Yes')
else:
	print('No')

### FIXED CODE ###

li = list(map(int, input().split()))
li.sort()
if li[0] + li[1] == li[2]:
	print('Yes')
else:
	print('No')
# encoding: utf-8

import sys
from collections import deque


class Solution:
    @staticmethod
    def doubly_linked_list():
        # write your code here
        array_length = int(input())
        _input = sys.stdin.readlines()
        task_list = list(map(lambda x: dict(action=x.split()[0], value=x.split()[-1]), _input))
        assert len(task_list) == array_length
        task_deque = deque()

        for i in range(array_length):
            task = task_list[i]
            action = task.get('action')
            value = task.get('value')
            if action == 'insert':
                task_deque.appendleft(value)
            elif action == 'deleteFirst':
                task_deque.popleft()
            elif action == 'deleteLast':
                task_deque.pop()
            elif action == 'delete':
                task_deque.remove(value)

        print(" ".join(map(str, task_deque)))
        assert len(task_deque) <= array_length


if __name__ == '__main__':
    solution = Solution()
    solution.doubly_linked_list()

### FIXED CODE ###

# encoding: utf-8

import sys
from collections import deque


class Solution:
    @staticmethod
    def doubly_linked_list():
        # write your code here
        array_length = int(input())
        task_deque = deque()
        _input = sys.stdin.readlines()

        for i in range(array_length):
            l_arg = _input[i].split()
            action = l_arg[0]
            if len(l_arg) > 1:
                value = l_arg[-1]
            # print(action, 'v', value)

            if action == 'insert':
                task_deque.appendleft(value)
            elif action == 'deleteFirst':
                task_deque.popleft()
            elif action == 'deleteLast':
                task_deque.pop()
            else:
                try:
                    task_deque.remove(value)
                except Exception as e:
                    pass
        print(" ".join(task_deque))


if __name__ == '__main__':
    solution = Solution()
    solution.doubly_linked_list()
# tenkaichi c

n = int(input())
A = [int(input()) for _ in range(n)]
A.sort()

if n%2==1:
    ans1 = 2*sum(A[n//2+2:]) + sum(A[2:n//2+2]) -2*sum(A[:2])
    ans2 = 2*sum(A[n//2+1:]) - sum(A[n//2-1:n//2+1]) -2*sum(A[:n//2-1])
    print(max(ans1,ans2))
else:
    print(-2*sum(A[:(n-2)/2])-A[(n-2)/2]+A[(n-2)/2+1]+2*sum(A[(n-2)/2+2:]))

### FIXED CODE ###

n = int(input())
A = [int(input()) for _ in range(n)]
A.sort()
if n%2==1:print(max(2*sum(A[n-n//2:])-sum(A[n-n//2-2:n-n//2])-2*sum(A[:n-n//2-2]),-2*sum(A[:n//2])+sum(A[n//2:n//2+2])+2*sum(A[n//2+2:])))
else:print(-2*sum(A[:(n-2)//2])-A[(n-2)//2]+A[(n-2)//2+1]+2*sum(A[(n-2)//2+2:]))
C:\pycharm\code_atcoder\venv\Scripts\python.exe C:/pycharm/code_atcoder/173B.py
6
AC
TLE
AC
AC
WA
TLE
AC x 3
WA x 1
TLE x 2
RE x 0

プロセスは終了コード 0 で完了しました


### FIXED CODE ###

N = int(input())
ac = 0
wa = 0
tle = 0
re = 0

for i in range(N):
    S = str(input())
    if S == 'AC':
        ac += 1
    elif S == 'WA':
        wa += 1
    elif S == 'TLE':
        tle += 1
    elif S == 'RE':
        re += 1
print('AC x '+str(ac))
print('WA x '+str(wa))
print('TLE x '+str(tle))
print('RE x '+str(re))
H, W = map(int, input().split())

S_list = []
for h in range(1, H-1):
    w = W//2
    S_A = h*W
    S_B = (H-h)*w
    S_C = (H-h)*(W-w)
    S_list.append(max(S_A,S_B,S_C) -min(S_A,S_B,S_C))
for h in range(1, H-1):
    S_A = h*W
    S_B = ((H-h)//2)*W
    S_C = -(-(H-h)//2)*W
    S_list.append(max(S_A,S_B,S_C) -min(S_A,S_B,S_C))
H, W = W, H
for h in range(1, H-1):
    w = W//2
    S_A = h*W
    S_B = (H-h)*w
    S_C = (H-h)*(W-w)
    S_list.append(max(S_A,S_B,S_C) -min(S_A,S_B,S_C))
for h in range(1, H-1):
    S_A = h*W
    S_B = ((H-h)//2)*W
    S_C = -(-(H-h)//2)*W
    S_list.append(max(S_A,S_B,S_C) -min(S_A,S_B,S_C))
print(min(S_list)) 
    

### FIXED CODE ###

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

S_list = []
for h in range(1, H):
    w = W//2
    S_A = h*W
    S_B = (H-h)*w
    S_C = (H-h)*(W-w)
    S_list.append(max(S_A,S_B,S_C) -min(S_A,S_B,S_C))
for h in range(1, H):
    S_A = h*W
    S_B = ((H-h)//2)*W
    S_C = -(-(H-h)//2)*W
    S_list.append(max(S_A,S_B,S_C) -min(S_A,S_B,S_C))
H, W = W, H
for h in range(1, H):
    w = W//2
    S_A = h*W
    S_B = (H-h)*w
    S_C = (H-h)*(W-w)
    S_list.append(max(S_A,S_B,S_C) -min(S_A,S_B,S_C))
for h in range(1, H):
    S_A = h*W
    S_B = ((H-h)//2)*W
    S_C = -(-(H-h)//2)*W
    S_list.append(max(S_A,S_B,S_C) -min(S_A,S_B,S_C))
print(min(S_list))