Harryxun/stf_run huggingface dataset:
def solve():
    n = int(input())
    a = list(map(int, input().split()))
    ruiseki = [0,a[0]]
    ans = 0
    for i in range(n-1):
        ruiseki.append(ruiseki[i]+a[i+1])
        if ruiseki[i]+a[i+1] == 0:
            ans += 1
    c = collections.Counter(ruiseki).values()
    for i in c:
        ans += i*(i-1)//2
    print(ans)
    return 0

if __name__ == "__main__":
    solve()

### FIXED CODE ###

import numpy as np
import math
import collections
import fractions
import itertools

def solve():
    n = int(input())
    a = list(map(int, input().split()))
    ruiseki = [0]
    ans = 0
    for i in range(n):
        ruiseki.append(ruiseki[i]+a[i])
    c = collections.Counter(ruiseki).values()
    for i in c:
        ans += i*(i-1)//2
    print(ans)
    return 0

if __name__ == "__main__":
    solve()
X, N = map(int, input().split())
p = list(map(int, input().split()))
for d in range(X + 1):
  for s in [-1, +1]: a=X+s*d
    if p.count(a) == 0:
      print(a)
      exit(0)

### FIXED CODE ###

X, N = map(int, input().split())
p = list(map(int, input().split()))
for d in range(X + 1):
  for s in [-1, +1]: 
    a=X+s*d
    if p.count(a) == 0:
      print(a)
      exit(0)
n,k=map(int,input().split())
hlist=list(map(int,input().split()))

mins=[0]

if n == 2:
	print(abs(hlist[1]-hlist[0]))

for i in range(1,n):	
	smallest=10001
	tmp=hlist[i]
	for j in range(min(k,i)):
		smallest=min(smallest,mins[i-j-1]+abs(tmp-hlist[i-j-1]))
	mins.append(min(tmplist))

print(mins[n-1])


### FIXED CODE ###

n,k=map(int,input().split())
hlist=list(map(int,input().split()))
 
mins=[0]
 
for i in range(1,n):
	if i == 1:
		mins.append(abs(hlist[1]-hlist[0]))
		continue
	
	tmplist=[]
	tmp=hlist[i]
	for j in range(min(k,i)):
		tmplist.append(mins[i-j-1]+abs(tmp-hlist[i-j-1]))
	mins.append(min(tmplist))
 
print(mins[n-1])
x = int().split()
a = int(nums[0])
b = int(nums[1])
print(a*b,(a+b)*2)

### FIXED CODE ###

a,b=map(int,input().split())
print(a*b,(a+b)*2)
N, K=map(int, input().split())
H=list(map(int, input().split()))

count=0
for i in H:
  if H>=K:
    count+=1
print(count)

### FIXED CODE ###

N, K=map(int, input().split())
H=list(map(int, input().split()))

count=0
for i in H:
  if i>=K:
    count+=1
print(count)
import math
print(math.factorial(int(input()))%(10**9+7)

### FIXED CODE ###

import math
print(math.factorial(int(input()))%(10**9+7))
import sys
sys.setrecursionlimit(10*5)

N=int(input())
edges=[[] for _ in range(N)]
for _ in range(N-1):
    u,v,w=map(int,input().split())
    edges[u-1].append((v-1,w))
    edges[v-1].append((u-1,w))

dist=[-1 for _ in range(N)]
def measure(src):
    for v,w in edges[src]:
        if dist[v]==-1:
            dist[v]=dist[src]+w
            measure(v)

dist[0]=0
measure(0)

for i in range(N):
    if dist[i]%2:
        print(1)
    else:
        print(0)   

### FIXED CODE ###

import sys
sys.setrecursionlimit(100000)

N=int(input())
edges=[[] for _ in range(N)]
for _ in range(N-1):
    u,v,w=map(int,input().split())
    edges[u-1].append((v-1,w))
    edges[v-1].append((u-1,w))

dist=[-1 for _ in range(N)]
def measure(src):
    for v,w in edges[src]:
        if dist[v]==-1:
            dist[v]=dist[src]+w
            measure(v)

dist[0]=0
measure(0)

for i in range(N):
    if dist[i]%2:
        print(1)
    else:
        print(0)
(h,n),*t=[list(map(int,o.split()))for o in open(0)]
d=[0]*(h+10100)
for i in range(1,h+1):d[i]=min(d[i-a]+b for a,b in t)
print(d[h])

### FIXED CODE ###

h, a = map(int, input().split())
print(-(-h//a))
import sys
sys.setrecursionlimit(1000000000000)

def check(x, y):
	array[x][y] = "0"
	if array[x + 1][y] == "1" : check(x + 1, y)
	if array[x - 1][y] == "1" : check(x - 1, y)
	if array[x][y + 1] == "1" : check(x, y + 1)
	if array[x][y - 1] == "1" : check(x, y - 1)
	if array[x + 1][y + 1] == "1" : check(x + 1, y + 1)
	if array[x + 1][y - 1] == "1" : check(x + 1, y - 1)
	if array[x - 1][y + 1] == "1" : check(x - 1, y + 1)
	if array[x - 1][y - 1] == "1" : check(x - 1, y - 1)

while True:
	h,w = map(int, raw_input().split())
	if h == 0 and w == 0 : break

	array = []
	array.append(list("$" * (2 + h)))
	for i in range(0, w) : array.append(list("$" + raw_input().replace(" ", "") + "$"))
	array.append(list("$" * (2 + h)))
	
	count = 0
	for i in range(1, h + 1):
		for j in range(1, w + 1):
			if array[i][j] == "1":
				check(i, j)
				count += 1
				
	print count

### FIXED CODE ###

import sys
sys.setrecursionlimit(100000)

def check(x, y):
	array[x][y] = "0"
	if array[x + 1][y] == "1" : check(x + 1, y)
	if array[x - 1][y] == "1" : check(x - 1, y)
	if array[x][y + 1] == "1" : check(x, y + 1)
	if array[x][y - 1] == "1" : check(x, y - 1)
	if array[x + 1][y + 1] == "1" : check(x + 1, y + 1)
	if array[x + 1][y - 1] == "1" : check(x + 1, y - 1)
	if array[x - 1][y + 1] == "1" : check(x - 1, y + 1)
	if array[x - 1][y - 1] == "1" : check(x - 1, y - 1)

while True:
	h,w = map(int, raw_input().split())
	if h == 0 and w == 0 : break

	array = []
	array.append(list("0" * (2 + h)))
	for i in range(0, w) : array.append(list("0" + raw_input().replace(" ", "") + "0"))
	array.append(list("0" * (2 + h)))
	
	count = 0
	for i in range(1, w + 1):
		for j in range(1, h + 1):
			if array[i][j] == "1":
				check(i, j)
				count += 1
				
	print count
def main():
    N = int(input())
    cnt = [[0] * 18 for _ in range(41)]
    ans = 0
    for _ in range(N):
        a = input()
        if "." in a:
            b = int(a.replace(".", "") + "0" * (10 - (len(a) - a.index("."))))
        else:
            b = int(a) * 10 ** 9
        d2 = 0
        d5 = 0
        while b % 2 == 0:
            d2 += 1
            b //= 2
        while b % 5 == 0:
            d5 += 1
            b //= 5
        for i in range(max(0, 18-d2), 41):
            for j in range(max(0, 18-d5), 18):
                ans += cnt[i][j]
        cnt[d2][d5] += 1
    print(ans)


if __name__ == "__main__":
    main()


### FIXED CODE ###

def main():
    N = int(input())
    cnt = [[0] * 19 for _ in range(44)]
    ans = 0
    for _ in range(N):
        a = input()
        if "." in a:
            b = int(a.replace(".", "")) * 10 ** (10 - (len(a) - a.index(".")))
        else:
            b = int(a) * 10 ** 9
        d2 = 0
        d5 = 0
        while b % 2 == 0:
            d2 += 1
            b //= 2
        while b % 5 == 0:
            d5 += 1
            b //= 5
        for i in range(max(0, 18-d2), 44):
            for j in range(max(0, 18-d5), 19):
                ans += cnt[i][j]
        cnt[d2][d5] += 1
    print(ans)


if __name__ == "__main__":
    main()
import math
N = int(input())
current_lcm = None
for _ in range(N):
    t = int(input())
    if current_lcm is None:
        current_lcm = t
        continue
    current_lcm = current_lcm // math.gcd(current_lcm, t)
    current_lcm *= t
print(current_lcm)

### FIXED CODE ###

def gcd(a, b):
    a, b = max([a, b]), min([a, b])
    if b == 0:
        return a
    return gcd(b, a % b)
N = int(input())
current_lcm = None
for _ in range(N):
    t = int(input())
    if current_lcm is None:
        current_lcm = t
        continue
    current_lcm = current_lcm * t // gcd(current_lcm, t)
print(current_lcm)
first_input = input().split()
x = int(first_input[0])
n = int(first_input[1])
if n != 0:
    second_input = map(int, input().split())
    p_a = list(map(int, second_input))
else:
    second_input = input()

def create_full_array(minimum, maximum):
    full_list = []
    for i in range(minimum,maximum+1):
        full_list.append(int(i))
    return full_list

if n == 0:
    print(x)
elif x > max(p_a) + 1 or x < min(p_a):
    ans = x - 1
    print(ans)
elif x == max(p_a) + 1:
    ans = x + 1
    print(ans)
else:
    full_array = create_full_array(min(p_a), max(p_a))
    if x in p_a:
        target_list = list(set(p_a)^set(full_array))
        abs_key = {}
        for i in target_list:
            abs_key[i] = abs(x-i)
        minimum = min(abs_key.values())
        t_k = []
        t_v = []
        [t_k.append(k) for k, v in abs_key.items() if v == minimum]
        print(min(t_k))

    else:
        print(x)



### FIXED CODE ###

first_input = input().split()
x = int(first_input[0])
n = int(first_input[1])
if n != 0:
    second_input = map(int, input().split())
    p_a = list(map(int, second_input))
else:
    second_input = input()

def create_full_array(minimum, maximum):
    full_list = []
    for i in range(minimum,maximum+1):
        full_list.append(int(i))
    return full_list

if n == 0:
    print(x)
elif x > max(p_a) + 1 or x < min(p_a):
    ans = x - 1
    print(ans)
elif x == max(p_a) + 1:
    ans = x + 1
    print(ans)
else:
    full_array = create_full_array(min(p_a), max(p_a))
    if x in p_a:
        target_list = list(set(p_a)^set(full_array))
        abs_key = {}
        if len(target_list) == 0:
            if abs(x - min(p_a)) <= abs(x - max(p_a)):
                print(min(p_a) - 1)
            else:
                print(max(p_a) + 1)
        else:
            for i in target_list:
                abs_key[i] = abs(x-i)
            minimum = min(abs_key.values())
            t_k = []
            t_v = []
            [t_k.append(k) for k, v in abs_key.items() if v == minimum]
            print(min(t_k))

    else:
        print(x)
n = input()
p = [int(input()) for i in range(3)]

dif = []
for i in range(n):
  if p[i]!= i:
    dif.append(1)

if len(dif) == 0 or len(dif) == 2:
  print('YES')
else:
  print('NO')
    

### FIXED CODE ###

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

dif = []
for i in range(1,n+1):
  if p[i-1] != i:
    dif.append(i)

if len(dif) == 0 or len(dif) == 2:
  print('YES')
else:
  print('NO')
h, w = list(map(int, input().split()))
si, sj = list(map(lambda x: int(x)-1, input().split()))
ti, tj = list(map(lambda x: int(x)-1, input().split()))
s = [None] * h
for i in range(h):
    s[i] = input()

# 探索時に移動する方向
di = [-1, 0, 1, 0]
dj = [0, -1, 0, 1]

# 距離情報を一番遠い値で初期化
import numpy as np
INF = int(1e9)
dist = np.full((h, w), INF)

from collections import deque
q = deque()

# 開始位置
dist[si][sj] = 0
q.append((si,sj))

while (len(q)>0):
    (i, j) = q[0]
    d = dist[i][j]
    q.popleft()
    # 移動Aによるコスト
    for v in range(len(di)):
        ni, nj = i+di[v], j+dj[v]
        if (ni<0 or ni>=h or nj<0 or nj>=w):
            continue # マップの境界
        if (s[ni][nj] == '#'):
            continue # 壁
        if (dist[ni][nj]<=d):
            continue # 探索済みコスト以上
        dist[ni][nj] = d
        q.append((ni, nj))
    # 移動Bによるコスト
    for ei in range(-2, 3):
        for ej in range(-2, 3):
            ni, nj = i+ei, j+ej
            if (ni<0 or ni>=h or nj<0 or nj>=w):
                continue # マップの境界
            if (s[ni][nj] == '#'):
                continue # 壁
            if (dist[ni][nj]<=d):
                continue # 探索済みコスト以上
            dist[ni][nj] = d+1
            q.append((ni, nj))

ans = dist[ti][tj]
if ans == INF:
    ans = -1
print(ans)


### FIXED CODE ###

h, w = list(map(int, input().split()))
si, sj = list(map(int, input().split()))
ti, tj = list(map(int, input().split()))
s = [None] * h
for i in range(h):
    s[i] = input()

# 移動Aで探索する方向
d_a = [(-1,0), (0,-1),(1,0),(0,1)]
# 移動Bで探索する方向
d_b = [ (i, j) for j in range(-2, 3) for i in range(-2, 3)]

# 距離情報を一番遠い値で初期化
#import numpy as np
INF = int(1e9)
#dist = np.full((h+2, w+2), INF)
dist = [[INF for j in range(w+2)] for i in range(h+2)]

# マップ境界と壁を距離0にして探索させない
for i in range(h+2):
    dist[i][0] = 0
    dist[i][-1] = 0
for j in range(w+2):
    dist[0][j] = 0
    dist[-1][j] = 0
for i in range(h):
    for j in range(w):
        if s[i][j]=='#': dist[i+1][j+1] = 0
#dist = [[INF for j in range(w)] for i in range(h)]

# 01-BFS探索用キュー
from collections import deque
q = deque()

# 開始位置
dist[si][sj] = 0
q.append((si,sj))

while (len(q)>0):
    i, j = q[0]
    d = dist[i][j]
    q.popleft()
    # 移動Aによるコスト
    for v in range(len(d_a)):
        di, dj = d_a[v][0]+i, d_a[v][1]+j
        if (di, dj) == (ti, tj):
            print(d)
            exit(0)
        if (dist[di][dj]<=d):
            continue # 探索済みコスト以上
        dist[di][dj] = d
        q.appendleft((di, dj))
    # 移動Bによるコスト
    for v in range(len(d_b)):
        # マップ境界で2以上移動したときに、反対の境界に向かうように剰余を取る
        di, dj = (d_b[v][0]+i)%(h+2), (d_b[v][1]+j)%(w+2)
        if (dist[di][dj]<=d):
            continue # 探索済みコスト以上
        dist[di][dj] = d+1
        q.append((di, dj))

ans = dist[ti][tj]
if ans == INF:
    ans = -1
print(ans)
n = int(input())
w = [map(int, input().split())]
ans = 0
for i in range(n):
    s1 = sum(w[:i])
    s2 = sum(w[i:])
    z = int(abs(s1 - s2))
    ans = min(ans, z)
print(ans)


### FIXED CODE ###

n = int(input())
w = list(map(int, input().split()))
ans = float('inf')
for i in range(n):
    s1 = sum(w[:i])
    s2 = sum(w[i:])
    z = abs(s1 - s2)
    ans = min(ans, z)
print(ans)
import re
import sys
from bisect import bisect, bisect_left, insort, insort_left
from collections import Counter, defaultdict, deque
from copy import deepcopy
from decimal import Decimal
from itertools import (
    accumulate, combinations, combinations_with_replacement, groupby,
    permutations, product)
from math import (acos, asin, atan, ceil, cos, degrees, factorial, gcd, hypot,
                  log2, pi, radians, sin, sqrt, tan)
from operator import itemgetter, mul
from string import ascii_lowercase, ascii_uppercase, digits


def inp():
    return(int(input()))


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


def instr():
    s = input()
    return(list(s[:len(s)]))


def invr():
    return(map(int, input().split()))


def def_value():
    return 0


def ncr(n, r):
    if r > n:
        return 0
    return n*(n-1) // 2


n, m = invr()
res = []
if n == 1:
    res = [0]
elif n == 2:
    res = [1, 0]
elif n == 3:
    res = [1, 0, 0]

f = [0, 0, 0]
out = 1
for _ in range(m):
    si, ci = invr()
    if si == 1 and n > 1 and ci == 0:
        out = -1
        break
    if f[si-1] == 0 or res[si-1] == ci:
        res[si-1] = ci
        f[si-1] = 1
    else:
        out = -1
        break
if out == -1:
    print(-1)

elif res[0] == 0 and len(res) > 1:
    print(-1)
else:
    for i in res:
        print(i, end="")


### FIXED CODE ###

import re
import sys
from bisect import bisect, bisect_left, insort, insort_left
from collections import Counter, defaultdict, deque
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from itertools import (
    accumulate, combinations, combinations_with_replacement, groupby,
    permutations, product)
from math import (
    acos, asin, atan, ceil, cos, degrees, factorial, hypot, log2, pi, radians,
    sin, sqrt, tan)
from operator import itemgetter, mul
from string import ascii_lowercase, ascii_uppercase, digits


def inp():
    return(int(input()))


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


def instr():
    s = input()
    return(list(s[:len(s)]))


def invr():
    return(map(int, input().split()))


n, m = invr()
res = []
if n == 1:
    res = [0]
elif n == 2:
    res = [1, 0]
elif n == 3:
    res = [1, 0, 0]

f = [0, 0, 0]
out = 1
for _ in range(m):
    si, ci = invr()
    if si == 1 and n > 1 and ci == 0:
        out = -1
        break
    if f[si-1] == 0 or res[si-1] == ci:
        res[si-1] = ci
        f[si-1] = 1
    else:
        out = -1
        break
if out == -1:
    print(-1)

elif res[0] == 0 and len(res) > 1:
    print(-1)
else:
    for i in res:
        print(i, end="")
import re
s1 = input()
t = input()
pre = ""
count = 0
ans = ""
start = 0
end = 0
it = ""

for w in s1:
  if w == t[count]:
    count += 1
    it += w
  elif pre == "?" and w == t[0]:
    count += 1
    it += w
  elif w != "?" and pre == "?":
    pre = w
    count = 0
    it = ""
  elif w == "?" and pre == "?":
    count += 1
    it += w
  else:
    if w == "?":
      pre = w
      count += 1
      it += w
  # if count >= len(t):
  #   print("きたよ")
  #   print(w)
  #   end = s1.index(w)
  #   start = end - len(t)
#print(f"count is {count}")
#print("endは" + end)
#replace = "?" * len(t)
#print("replace is" + it)
if count >= len(t):
  it = it.replace("?","\?")
  # 合致した最も後方のものを置換
  ans = re.sub(it,t,s1)
  ans = ans.replace("?","a")
  #print(ans)
  # if end < len(s1) - 1:
  #   ans = s1[:start] + t + s1[end+1:]
  #   # 全てaに置換
  #   ans = ans.replace("?","a")
  #   print(ans)
  # else:
  #   ans = s1[:start] + t
  #   ans = ans.replace("?","a")
  print(ans)
else:
  print("UNRESTORABLE")

### FIXED CODE ###

# pythonはindexでstringの文字を変更できない
s = list(input())
t = list(input())
x = 0
ans = []
gotk = False

# pythonて関数にするとスコープの外の変数参照できないんだっけ？

# 0 <= k <= s - t
def match(k):
  for i in range(len(t)):
    if s[i + k] != t[i] and s[i + k] != "?":
      return False
  return True

def construct(k,s):
  for i in range(len(t)):
    s[i + k] = t[i]
  for i in range(len(s)):
    if s[i] == "?":
      s[i] = "a"
  return s

# 大きいkから探せば良い(今回逆順にしてるためrangeの第二引数が直前まで処理されることに注意！)
for i in range(len(s) - len(t),-1,-1):
  if match(i):
    x = i
    gotk = True
    break

if gotk == True:
  ans = construct(x,s)
  print("".join(ans))
else:
  print("UNRESTORABLE")
import math
abc = input()
abc = abc.split(' ')
a = int(abc[0])
b = int(abc[1])
c = int(abc[2])

if 4*a*b<(c-a-b)(c-a-b):
    print('Yes')
else:
    print('No')



### FIXED CODE ###

import math
abc = input()
abc = abc.split(' ')
a = int(abc[0])
b = int(abc[1])
c = int(abc[2])

if (c-a-b>0) and 4*a*b<(c-a-b)*(c-a-b):
    print('Yes')
else:
    print('No')
def P(N, M):
  if N > M;
  	return No
  return Yes

### FIXED CODE ###

import sys
import socket

hostname = socket.gethostname()

if hostname == 'F451C':
    sys.stdin = open('a1.in')


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


def read_str_list():
    return input().split()


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


def read_str():
    return input()


def main():
    n, m = read_int_list()
    if n > m:
      res = 'No'
    else:
      res = 'Yes'
    print(res)


main()
import numpy as np
S=input()
Q=int(input())
q=[[0 for i in range(3)] for j in range(Q)]
onecntr=0
for i in range(Q):
    tmp=input().split()
    if(tmp[0]=="1"):
        q[i][0]=1
    else:
        q[i][0]=2
        q[i][1]=int(tmp[1])
        q[i][2]=tmp[2]
    

for i in range(Q):
    if(q[i][0]==1):
        onecntr+=1
    else:
        
        if(onecntr%2==0):
            if(q[i][1]==1):
                S=q[i][2]+S
            else:
                S=S+q[i][2]
        else:
            if(q[i][1]==2):
                S=q[i][2]+S
            else:
                S=S+q[i][2]
    
if(onecntr%2==1):
    S=S[::-1]

print(S)
            

### FIXED CODE ###

S=input()
cntr=0
if(S=="AAA" or S=="BBB"):
    print("No")
else:
    print("Yes")
from collections import defaultdict
from re import finditer
from bisect import bisect_left

def main():
    s = input()
    t = input()


    str_dict = defaultdict(list)



    for char in set(t):
        m = finditer(char, s)
        if not m:
            return '-1'
        for j in m:
            str_dict[char] += [j.start() + 1]



    ans = 0
    for i in t:
        remain = ans % len(s)
        array = str_dict[i]
        str_ind = bisect_left(array, remain)
        if str_ind != len(array):
            ans += array[str_ind] - remain
        else:
            ans += len(s) - remain + array[0]
    return ans


print(main())

### FIXED CODE ###

from collections import defaultdict
from re import finditer
from bisect import bisect_right

def main():
    s = input()
    t = input()


    str_dict = defaultdict(list)



    for char in set(t):
        c_iter = finditer(char, s)
        flag = True
        for j in c_iter:
            flag = False
            str_dict[char] += [j.start() + 1]
        if flag:
            return '-1'


    ans = 0
    for i in t:
        remain = ans % len(s)
        array = str_dict[i]
        str_ind = bisect_right(array, remain)
        if str_ind != len(array):
            ans += array[str_ind] - remain
        else:
            ans += len(s) - remain + array[0]
    return ans


print(main())
def solve():
  n,l = map(int,input().split())
  
  length = [int(input()) for i in range(n)]
  pare = [(length[i], i) for i in range(n)]
  pare.sort(reverse=True)
  dp = [0] * (n + 1)
  for p in pare:
    i = p[0]
    if i == 0:
      dp[i] = dp[i + 1] + (l - length[i])
    else:
      dp[i] = max(dp[i - 1], dp[i + 1]) + (l - length[i])
  print(max(dp))
solve()


### FIXED CODE ###

def solve():
  n, l = map(int,input().split())
  ans = 0
  pre = 0
  trend = 1
  acc = 0
  for i in range(n):
    length = int(input())
    time = l - length
    if length > pre and trend >= 0:
      acc += time
    elif length < pre and trend <= 0:
      acc += time
    else:
      if ans < acc:
        ans = acc
      acc = time + (l - pre)
      trend = length - pre
    pre = length
  else:
    ans = max(ans, acc)
  print(ans)
solve()
n=input()
print(["Yes","No"][int(n)%sum(list(map(int,list(n))))])

### FIXED CODE ###

n=input()
print(["Yes","No"][int(n)%sum(list(map(int,list(n))))>0])
def pushBack(lists, x):
	return lists.insert(-1, x)

def randomAccess(lists, p):
	print(lists[p])

def popBack(lists):
	return lists.pop(-1)

if __name__ == '__main__':
	l = list()
	n = int(input())
	for i in range(n):
		j= input().split()

		if int(j[0]) == 0:
			pushBack(l, x)
		elif int(j[0]) == 1:
			randomAccess(l, x)
		else:
			popBack(l)



### FIXED CODE ###

def pushBack(lists, x):
    return lists.insert(len(lists), x)

def randomAccess(lists, p):
    print(lists[p])

def popBack(lists):
    return lists.pop(len(lists) - 1)

if __name__ == '__main__':
    l = list()
    n = int(input())
    for i in range(n):
        j= input().split()

        if int(j[0]) == 0:
            pushBack(l, int(j[1]))
        elif int(j[0]) == 1:
            randomAccess(l, int(j[1]))
        else:
            popBack(l)
n, m = map(int, input().split())
x = sorted(list(map(int, input().split())))
dif = []
pos = []
ans = 0

if n >= m:
    print(0)
else:
    for i in range(m - 1):
        dif.append([x[i + 1] - x[i], i])

    dif.sort(reverse = True)

    for i in range(n - 1):
        pos.append(dif[i][1])

    pos.sort()

    ans += x[pos[0]] - x[0]
    for i in range(n - 2):
        ans += x[pos[i + 1]] - x[pos[i] + 1]
    ans += x[m - 1] - x[pos[-1] + 1]

    print(ans)


### FIXED CODE ###

n, m = map(int, input().split())
x = sorted(list(map(int, input().split())))
dif = []
pos = []
ans = 0

if n >= m:
    print(0)
elif n == 1:
    print(x[-1] - x[0])
else:
    for i in range(m - 1):
        dif.append([x[i + 1] - x[i], i])

    dif.sort(reverse = True)

    for i in range(n - 1):
        pos.append(dif[i][1])

    pos.sort()

    ans += x[pos[0]] - x[0]
    for i in range(n - 2):
        ans += x[pos[i + 1]] - x[pos[i] + 1]
    ans += x[m - 1] - x[pos[-1] + 1]

    print(ans)
# C - Typical Stairs

MOD = (10 ** 9) + 7
N, M = map(int, input().split())
A = [False] * (N + 1)
for i in range(N):
    A[int(input())] = True
dp = [0] * (N + 1)
dp[0] = 1
if 1 not in A:
    dp[1] = 1

for i in range(2, N + 1):
    if A[i]:
        dp[i] = 0
    else:
        dp[i] = (dp[i - 1] + dp[i - 2]) % MOD

print(dp[N])


### FIXED CODE ###

# C - Typical Stairs

MOD = (10 ** 9) + 7
N, M = map(int, input().split())
A = [False] * (N + 1)
for _ in range(M):
    A[int(input())] = True
dp = [0] * (N + 1)
dp[0] = 1
if A[1] == False:
    dp[1] = 1

for i in range(2, N + 1):
    if A[i] == True:
        dp[i] = 0
    else:
        dp[i] = (dp[i - 1] + dp[i - 2]) % MOD

print(dp[N])
x,n = map(int,input().split())
p = list(map(int,input.split()))
i = 0
ans = 0
while True:
  if(x-1 in p):
    if(x+1 in p):
      None
    else:
      ans = x+1
      break
  else:
    ans = x-1
    break
  i += 1
print(ans)
    
      

### FIXED CODE ###

x,n = map(int,input().split())
p = list(map(int,input().split()))
i = 0
ans = 0
while True:
  if(x-i in p):
    if(x+i in p):
      None
    else:
      ans = x+i
      break
  else:
    ans = x-i
    break
  i += 1
print(ans)