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

n,m  =map(int,input().split())
lm = []
for _ in range(n):
    lm.append(list(map(int,input().split())))
s = 0
mon = 0
lm.sort(key = lambda x:x[0])

while s + lm[0][1] < m:
    s += lm[0][1]
    mon += lm[0][1]*lm[0][0]
    lm = lm[1::]
while s < m:
    s += 1
    mon += lm[0][0]
    lm[0][1] -= 1
    
print(mon)

### FIXED CODE ###

N, M = list(map(int, input().split()))
AB = [list(map(int, input().split())) for i in range(N)]
 
AB.sort()
Ans = 0
for i in AB:
  if i[1] >= M:
    Ans += i[0] * M
    break
  else:
    Ans += i[0] * i[1]
    M -= i[1]
 
print(Ans)
### BUGGY CODE ###


N = int(input())
S = input()
# ans = 0
R_index = [n for n, v in enumerate(S) if v == 'R']
G_index = [n for n, v in enumerate(S) if v == 'G']
B_index = [n for n, v in enumerate(S) if v == 'B']
ans = len(R_index) *len(G_index) *len(B_index)
for i in range(N):
    for j in range(i+1, N):
        if S[i] == 'R' and S[j] =='G':
            # ans += sum(x > j for x in B_index)
            if j+j-i in B_index:
                ans -= 1
            continue
        elif S[i] == 'R' and S[j] =='B':
            # ans += sum(x > j for x in G_index)
            if j+j-i in G_index:
                ans -= 1
            continue
        elif S[i] == 'G' and S[j] =='R':
            # ans += sum(x > j for x in B_index)
            if j+j-i in B_index:
                ans -= 1
            continue
        elif S[i] == 'G' and S[j] =='B':
            # ans += sum(x > j for x in R_index)
            if j+j-i in R_index:
                ans -= 1
            continue
        elif S[i] == 'B' and S[j] =='R':
            # ans += sum(x > j for x in G_index)
            if j+j-i in G_index:
                ans -= 1
            continue
        elif S[i] == 'B' and S[j] =='G':
            # ans += sum(x > j for x in R_index)
            if j+j-i in R_index:
                ans -= 1
            continue
        # for k in range(j+1, N):
        #     if j-i == k-j:
        #         continue
        #     if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
        #         ans += 1

print(ans)

### FIXED CODE ###

from collections import Counter

N = int(input())
S = input()
index = Counter(S)
ans = index["R"]*index["G"]*index["B"]

for diff in range(1, N//2 +1):
    for i in range(N):
        j = i +diff
        k = j +diff
        if k >= N:
            break
        if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
            ans -= 1
print(ans)
### BUGGY CODE ###

n, m, q = map(int, input().split())
M = [list(map(int, input().split())) for _ in range(m)]
Q = [list(map(int, input().split())) for _ in range(q)]

for q in Q:
    x, y = q
    cnt = 0
    for m in M:
        l, r = m
        if x <= l and r <= y:
            cnt += 1
    print(cnt)


### FIXED CODE ###

n, m, q = map(int, input().split())
cities = [[0 for _ in range(n+1)] for _ in range(n+2)]
trains = [list(map(int, input().split())) for _ in range(m)]
quests = [list(map(int, input().split())) for _ in range(q)]

for train in trains:
    x, y = train[0], train[1]
    cities[x][y] += 1

for i in range(n):
    for j in range(1, n-i+1):
        x = j + i
        cities[j][x] += cities[j][x-1] + cities[j+1][x] - cities[j+1][x-1]

for quest in quests:
    p, q = quest[0], quest[1]
    print(cities[p][q])
### BUGGY CODE ###

n = int(input())
ans = 0
for i in range(1,n):
  ans += i%(i+1)
print(ans)

### FIXED CODE ###

n = int(input())
print(int((n)*(n-1)//2))
### BUGGY CODE ###

K, S = [int(i) for i in input().split()]
l = []

for x in range(K + 1):
    if S - x >= 0:
        for y in range(K + 1):
            if K >= S - x - y >= 0:
                z = S - x - y
                l.append((x, y, z))


print(len(l))

### FIXED CODE ###

K, S = [int(i) for i in input().split()]
ans = 0

for x in range(K + 1):
    if S - x >= 0:
        for y in range(K + 1):
            if K >= S - x - y >= 0:
                ans += 1

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

import collections
def main(N,S):
    S.sort()
    setS = set(S)
    counterS = collections.Counter(S)
    maxcount = 0
    for i in setS:
        maxcount = max(maxcount, S.count(i))
    keys = [k for k, v in counterS.items() if v == maxcount]
    keys.sort()
    for j in keys:
        print(j)

N = int(input())
S = [input() for _ in range(N)]
main(N,S)

### FIXED CODE ###

import collections
def main(N,S):
    counterS = collections.Counter(S)
    maxcount = max(counterS.values())
    keys = [k for k, v in counterS.items() if v == maxcount]
    keys.sort()
    for j in keys:
        print(j)

N = int(input())
S = [input() for _ in range(N)]
main(N,S)
### BUGGY CODE ###

n=int(input())
arr=list(map(int,input().strip().split()))
arr.append(0)
arr.sort()
arr.append(0)
count=0
s=set()
for i in range(1,n+1):
    if(arr[i]==arr[i-1] or arr[i]==arr[i+1]):
        s.add(arr[i])
    else:
        if(len(s)):
            crr=list(s)
            flag=0
            for j in range(len(s)):
                if(arr[i]%crr[j]==0):
                    flag=1
                    break
            if(flag==0):
                count=count+1
            s.add(arr[i])
        else:
            s.add(arr[i])
            count=count+1
print(count)            
                
            
            
        

            
        
    


### FIXED CODE ###

n=int(input())
arr=list(map(int,input().strip().split()))
arr.sort()
maxi=arr[-1]
booll=[True]*(maxi+1)
r=0
for i in range(n-1):
    a=arr[i]
    if(booll[a]==True):
        for j in range(a,maxi+1,a):
            booll[j]=False
        if(a<arr[i+1]):
            r=r+1
if(booll[arr[n-1]]==True):
    r=r+1
print(r)
### BUGGY CODE ###

import sys
import copy
from collections import deque

n = int(input())
a = [deque(map(int, sys.stdin.readline().rstrip().split())) for _ in range(n)]

day = 0
setn = set(range(n))
while True:
    popped = 0
    plist = set()
    zeros = 0
    tmpns = copy.copy(setn)
    for i in setn:
        if len(a[i]) == 0:
            tmpns.remove(i)
            continue
        if i in plist:
            continue
        num = a[i][0] - 1
        if num < i:
            continue
        if num in plist:
            continue
        if a[num][0] - 1 == i:
            plist.add(num)
            a[i].popleft()
            a[num].popleft()
            popped += 1
    if popped == 0:
        if len(tmpns) == 0:
            break
        else:
            day = -1
            break
    day += 1

print(day)


### FIXED CODE ###

import sys
from collections import deque

n = int(input())
a = [deque(map(int, sys.stdin.readline().rstrip().split())) for _ in range(n)]

current = []

for i in range(n):
    target = a[i][0] -1
    if a[target][0] -1 == i and i < target:
        current.append([i, target])

day = 0
zc = 0
while True:
    popped = 0
    plist = set()
    zeros = 0
    tc = []
    for i in current:
        a[i[0]].popleft()
        a[i[1]].popleft()
        if len(a[i[0]]) > 0:
            plist.add(i[0])
        else:
            zc += 1
        if len(a[i[1]]) > 0:
            plist.add(i[1])
        else:
            zc += 1
    for i in plist:
        target = a[i][0] -1
        if a[target][0] -1 == i:
            if not target in plist or i < target:
                tc.append([i, target])
    day += 1
    if len(tc) == 0:
        break
    current = tc

if zc != n:
    day = -1

print(day)
### BUGGY CODE ###

A,B = list(map(int,input().split()))
for i in range(min(A,B),A*B+1):
    if i % A == 0 and i % B == 0:
        print(i)
        break


### FIXED CODE ###

import fractions
A,B = list(map(int,input().split()))
print(int(A*B/fractions.gcd(A,B)))
### BUGGY CODE ###

# input
N = int(input())
As = list(map(int, input().split()))


# def print_list(lis):
#   for l in lis:
#     print(l)


# score[i][j][p] : Asのi番目を左端、j番目を右端とした時の最終スコア
#                  p = 0 が太郎君目線、p = 1 が次郎くん目線
score = [[[None for _ in range(2)] for _ in range(N)] for _ in range(N)]

for i in range(N):
  # 初期化
  # 区間長さ0であれば1通りしかない
  score[i][i][0] = As[i]
  score[i][i][1] = -As[i]

# print_list(score)

for k in range(1, N):
  # 配列の幅を広げながら計算を進める
  for i in range(N):
    # if k == 0:
    #   continue
    j = i + k
    if j >= N:
      # 幅をとりすぎたら中断
      continue
    if i == N - 1:
      score[i][j][0] = score[i][j-1][1] + As[j]
      score[i][j][1] = score[i][j-1][0] - As[j]
    else:
      # print(i,j)
      score[i][j][0] = max(score[i+1][j][1] + As[i], score[i][j-1][1] + As[j])
      score[i][j][1] = min(score[i+1][j][0] - As[i], score[i][j-1][0] - As[j])
    # print_list(score)

print(score[0][N-1][0])


### FIXED CODE ###

# input
N = int(input())
As = list(map(int, input().split()))

# Nの偶奇。区間幅がNの偶奇と一致すれば太郎君目線
oe = N % 2

# def print_list(lis):
#   for l in lis:
#     print(l)


# score[i][j][p] : Asのi番目を左端、j番目を右端とした時の最終スコア
#                  p = 0 が太郎君目線、p = 1 が次郎くん目線
score = [[None for _ in range(N)] for _ in range(N)]

for i in range(N):
  # 初期化
  # 区間長さ1であれば1通りしかない
  if oe == 1:
    score[i][i] = As[i]
  else:
    score[i][i] = -As[i]

# print_list(score)

for k in range(1, N):
  # 配列の幅を広げながら計算を進める
  for i in range(0, N-k):
    # if k == 0:
    #   continue
    j = i + k
    if i == N - 1:
      if (k + 1) % 2 == oe:
        score[i][j] = score[i][j-1] + As[j]
      else:
        score[i][j] = score[i][j-1] - As[j]
    else:
      # print(i,j)
      if (k + 1) % 2 == oe:
        score[i][j] = max(score[i+1][j] + As[i], score[i][j-1] + As[j])
      else:
        score[i][j] = min(score[i+1][j] - As[i], score[i][j-1] - As[j])
    # print_list(score)

print(score[0][N-1])
### BUGGY CODE ###

import sys
readline = sys.stdin.readline

N,K = map(int,readline().split())
A = sorted(list(map(int,readline().split())))
F = sorted(list(map(int,readline().split())),reverse = True)

ok = A[-1] * F[0]
ng = -1

def isOk(x):
  # 時間がxになるためには、A_iをx/F_i以下まで減らす必要がある
  diff = 0
  for i in range(len(A)):
    diff += max(A[i] - x // F[i], 0)
  return diff <= K
  
while (ok - ng)>1:
  mid = (ok + ng) // 2
  if isOk(mid):
    ok = mid
  else:
    ng = mid
print(ok)

### FIXED CODE ###

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

a=sorted(a)[::-1]
f=sorted(f)

ok=10**18
ng=-1

import math
def isOK(val,arr,farr,k):
  s=0
  for i in range(len(arr)):
    s+=max(math.ceil(a[i]-val/f[i]),0)
  return s<=k
  
while abs(ok-ng)>1:
  mid=abs(ok+ng)//2
  if isOK(mid,a,f,k):
    ok=mid
  else:
    ng=mid
    
print(ok)
### BUGGY CODE ###

a, b, c, d = map(int, input().split())
ans = 0

if a == d:
    print(a)
else:
    for _ in range(a):
        ans += 1
    
    if d-a != 0:
        for _ in range(d-a):
            ans += 0

    if d-a-b != 0:
        for _ in range(d-a-b):
            ans += -1
    
    print(ans)

### FIXED CODE ###

a, b, c, d = map(int, input().split())

if a >= d:
    print(d*1)
elif a < d:
    if d-(a+b) <= 0:
        print(a)
    elif d-(a+b+c) <= 0:
        print(a + (d-a-b)*(-1))
### BUGGY CODE ###

def main():
    n = int(input())
    itr = map(int, input().split())
    dic = {}
    ls = []
    for num in itr:
        z = dic.setdefault(num, 0)
        dic[num] = z + 1
        ls.append(num)

    for i in range(n):
        # ls[i]を除く
        a = ls[i]
        dic[a] -= 1
        res = 0
        for k, v in dic.items():
            if v < 2:
                continue
            elif v == 2:
                res += 1
            else:
                res += int(v*(v-1)/2)
        print(res)
        dic[a] += 1


main()


### FIXED CODE ###

def z(z_dic, v: int):
    if v in z_dic:
        return z_dic[v]
    z = 0
    if v == 2:
        z = 1
    elif v > 2:
        z = int(v*(v-1)/2)
    z_dic.setdefault(v, z)
    return z


def main():
    n = int(input())

    ar = [0] * (n+1)
    ls = []

    for x in map(int, input().split()):
        ls.append(x)
        ar[x] += 1

    zls = []
    zls2 = []

    z_dic = {}

    for i in range(1, n+1):
        v = ar[i]
        zls.append(z(z_dic, v))
        zls2.append(z(z_dic, v-1))
    ss = sum(zls)

    for i in range(1, n+1):
        aa = ls[i-1]
        print(ss - zls[aa-1] + zls2[aa-1])
    #
    # itr = map(int, input().split())
    # dic = {}
    # for num in itr:
    #     dic.setdefault(num, 0)
    #     dic[num] += 1
    #     ls.append(num)
    #
    # for i in range(n):
    #     # ls[i]を除く
    #     a = ls[i]
    #     dic[a] -= 1
    #     res = 0
    #     for k, v in dic.items():
    #         if v < 2:
    #             continue
    #         elif v == 2:
    #             res += 1
    #         else:
    #             res += int(v*(v-1)/2)
    #     print(res)
    #     dic[a] += 1


main()
### BUGGY CODE ###

import numpy as np
def Expected_Value(x):
    EV = 0
    for i in range(1,x+1):
        EV += i/x
    return EV
N, K = map(int, input().split())
pi = np.array(list(map(int, input().split())))
ma = 0
for i in range(N-K+1):
    x = sum(pi[i:i+K])
    if x > ma:
        ma = x
        max_index = i
array_max = (np.array(pi[max_index:max_index+K])-1) *0.5 + 1
print(array_max.sum())

### FIXED CODE ###

N, K = map(int, input().split())
pi = list(map(int,input().split()))
def EV(x):
    return 1+(x-1)*0.5
for i in range(N):
    pi[i] = EV(pi[i])

ruisekiwa = sum(pi[0:K])
max_EV = ruisekiwa

for i in range(N-K):
    ruisekiwa = ruisekiwa + pi[i+K]-pi[i]
    if ruisekiwa > max_EV:
        max_EV = ruisekiwa
print(max_EV)
### BUGGY CODE ###

N = int(input())
S = str(input())


S_R = []
S_G = []
S_B = []


for i in range(N):
    s = S[i]
    if (s == 'R'):
        S_R.append(i)
    if (s == 'G'):
        S_G.append(i)
    if (s == 'B'):
        S_B.append(i)

count = 0

for i in S_R[::1]:
    for j in S_G[::1]:
        abs_ij =abs(i-j)
        for k in S_B[::1]:
            abs_jk = abs(k-j)
            abs_ki = abs(k-i)
            if((abs_ij!=abs_jk) & (abs_jk!=abs_ki)& (abs_ki!=abs_ij)):
                count+= 1

print(count)

### FIXED CODE ###

n = int(input())
s = input()
 
r, b, g = 0, 0, 0
for i in range(n):
    if s[i] == 'R':
        r += 1
    if s[i] == 'G':
        g += 1
    if s[i] == 'B':
        b += 1
ans = r * g * b
 
for i in range(n):
    for j in range(1, n // 2 + 1):
        if i + 2 * j >= n:
            break
        if s[i] != s[i+j] and s[i] != s[i+2*j] and s[i+j] != s[i+2*j]:
            ans -= 1
 
print(ans)
### BUGGY CODE ###

import heapq
L, R = map(int, input().split())
R = min(R, L+4038)
ans = []
heapq.heapify(ans)
for i in range(L, R):
    for j in range(L+1, R+1):
        heapq.heappush(ans, (i*j) % 2019)

print(heapq.heappop(ans))


### FIXED CODE ###

L, R = map(int, input().split())
R = min(R, L+4038)
ans = 2018
for i in range(L, R):
    for j in range(L+1, R+1):
        ans = min((i*j) % 2019, ans)
print(ans)
### BUGGY CODE ###

#!/usr/bin/env python3
N = int(input())
A = list(map(int, input().split()))

mod = 10 ** 9 + 7
ans = 0
for i in range(N - 1):
    ans += A[i] * sum(A[i + 1:]) % mod
    ans = ans % mod
print(ans)


### FIXED CODE ###

#!/usr/bin/env python3
N = int(input())
A = list(map(int, input().split()))

mod = 10 ** 9 + 7

s = sum(A)** 2

a = 0
for i in A:
    a += (i ** 2)

ans = (s - a) // 2
ans = ans % mod

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

import collections
import itertools

n = int(input())
a = []
ans = 0
for i in range(n):
    s = list(input())
    s.sort()
    S = ''.join(s)
    a.append(S)

c = collections.Counter(a)
for i in c.values():
    ans += len(list(itertools.combinations(range(i), 2)))
print(ans)


### FIXED CODE ###

n = int(input())
a = {}
ans = 0
for i in range(n):
    s = list(input())
    s.sort()
    S = ''.join(s)
    if S in a:
        a[S] += 1
    else:
        a[S] = 1

for j in a.values():
    ans += j * (j-1) // 2
print(ans)
### BUGGY CODE ###

while 1:
    try:
        s1 = raw_input()
        s2 = raw_input()
        if len(s1) < len(s2): s1,s2 = s2,s1
        l = sum(min(s1.count(i), s2.count(i)) for i in set(list(s1)) & set(list(s2)))
        f = False
        for i in range(l,0,-1):
            for j in range(0,l-i+1):
                x = s2.find(s1[j:j+i])
                if x != -1:
                    f = True
                    print i
                    break
            if f: break
        if i == 1 and not f: print 0
    except:
        break

### FIXED CODE ###

while 1:
    try:
        s1 = raw_input()
        s2 = raw_input()
        if len(s1) < len(s2): s1,s2 = s2,s1
        l = sum(min(s1.count(i), s2.count(i)) for i in set(list(s1)) & set(list(s2)))
        if l == 0: print 0; continue
        ans = 0
        for i in range(len(s2)):
            if len(s2) - i <= ans: break
            for j in range(ans+1, min(l + 1,len(s2)-i+1)):
                if s2[i:i+j] in s1:
                    ans = j
                else: break
        print ans
    except:
        break
### BUGGY CODE ###

import numpy as np

N, Q = (int(x) for x in input().split())

dic = {}
#for i in range(N) :
#    dic[i+1] = []

for i in range(N-1) :
    a, b = (int(x) for x in input().split())
    #dic[a].append(b)
    #dic[b].append(a)
    dic[b] = a
    #for n in dic.keys() :
    #    if a in dic[n] :
    #        dic[n].append(b)

SUM = np.zeros(N, dtype=np.int)

for i in range(Q) :
    p, x = (int(x) for x in input().split())
    SUM[p-1] = SUM[p-1] + x
    #for n in dic[p] :
    #    SUM[n-1] = SUM[n-1] + x

for i in range(N-1) :
    SUM[i+1] += SUM[dic.get(i+2)-1]

print(' '.join(map(str, SUM)))

### FIXED CODE ###


import numpy as np

N, Q = (int(x) for x in input().split())

dic = {}

for i in range(N-1) :
    #a, b = (int(x) for x in input().split())
    a, b = map(int,input().split())
    dic[b] = a

#SUM = np.zeros(N, dtype=np.int)
SUM = [0]*N

for i in range(Q) :
    #p, x = (int(x) for x in input().split())
    p, x = map(int,input().split())
    SUM[p-1] = SUM[p-1] + x

for i in range(N-1) :
    SUM[i+1] += SUM[dic.get(i+2)-1]

print(' '.join(map(str, SUM)))
### BUGGY CODE ###

n,m,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
a_sum = [0]
b_sum = [0]
for i in range(n):
    a_sum.append(a[i]+a_sum[i])
    if a_sum[i]>k:
        break
for i in range(m):
    b_sum.append(b[i]+b_sum[i])
    if b_sum[i]>k:
        break
        
ans = 0
for i in range(len(a_sum)):
    if a_sum[i]>k:
        break
    time = k - a_sum[i]
    for j in range(len(b_sum)-1,-1,-1):
        if time>=b_sum[j]:
            break
    if time>=b_sum[len(b_sum)-1]:
        j = len(b_sum)-1
    ans = max(ans,i+j)
    
print(ans)

### FIXED CODE ###

n,m,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
a_sum = [0]
b_sum = [0]
for i in range(n):
    a_sum.append(a[i]+a_sum[i])
    if a_sum[i]>k:
        break
for i in range(m):
    b_sum.append(b[i]+b_sum[i])
    if b_sum[i]>k:
        break
        
ans = 0
y = len(b_sum)-1
for i in range(len(a_sum)):
    if a_sum[i]>k:
        break
    time = k - a_sum[i]
    for j in range(y,-1,-1):
        if time>=b_sum[j]:
            y = j
            break
    if time>=b_sum[len(b_sum)-1]:
        j = len(b_sum)-1
    ans = max(ans,i+j)
    
print(ans)
### BUGGY CODE ###

import itertools


N, M = map(int, input().split())
edges = [list(map(lambda x: int(x) - 1, input().split())) for _ in range(M)]

res = 0
if M < N - 1:
    print(res)
else:
    for path in itertools.permutations(edges, N - 1):
        vertex = [True] * N
        v = 0
        vertex[v] = False
        for edge in path:
            if v in edge:
                nv = edge[0] if v == edge[1] else edge[1]
                if vertex[nv]:
                    vertex[nv] = False
                    v = nv
                    continue
            break
        else:
            res += 1
    print(res)


### FIXED CODE ###

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

edges = [[] for _ in range(N)]
for _ in range(M):
    a, b = map(int, input().split())
    edges[a - 1].append(b - 1)
    edges[b - 1].append(a - 1)


def dfs(node, track):
    if len(track) == N:
        global res
        res += 1
    else:
        for dst in [x for x in edges[node] if x not in track]:
            dfs(dst, track + tuple([dst]))


res = 0
dfs(0, tuple([0]))
print(res)
### BUGGY CODE ###

N=int(input())
p=1+N
for i in range(1,N):
    if N%i==0:
        if (N/i)+i<=p:
            p=(N//i)+i
        else:
            p=p
    else:
        pass

print(p-2)

### FIXED CODE ###

N=int(input())
p=0
for i in range(1,N):
  if i**2<=N:
    p=p+1
  else:
    break
    
q=p   
for i in reversed(range(p+1)):
  if N%i!=0:
    q=q-1
  else:
    break
    
print((q+(N//q))-2)
### BUGGY CODE ###

def main():
    n = int(input())
    a = list(map(int, input().split()))
    a = [((i-1)//n, (i-1) % n) for i in a]
    b = [[min(i, n-i-1, j, n-j-1) for j in range(n)] for i in range(n)]
    sit = [[True for j in range(n)] for i in range(n)]
    ans = 0
    for i, j in a:
        sit[i][j] = False
        ans += b[i][j]
        q = [(i, j, b[i][j])]
        while q:
            x, y, z = q.pop()
            if x != 0:
                if b[x-1][y] > z:
                    b[x-1][y] = z
                    q.append((x-1, y, z+sit[x-1][y]))
            if x != n-1:
                if b[x+1][y] > z:
                    b[x+1][y] = z
                    q.append((x+1, y, z+sit[x+1][y]))
            if y != 0:
                if b[x][y-1] > z:
                    b[x][y-1] = z
                    q.append((x, y-1, z+sit[x][y-1]))
            if y != n-1:
                if b[x][y+1] > z:
                    b[x][y+1] = z
                    q.append((x, y+1, z+sit[x][y+1]))
    print(ans)


main()


### FIXED CODE ###

def main():
    n = int(input())
    a = list(map(int, input().split()))
    a = [((i-1)//n, (i-1) % n) for i in a]
    b = [[min(i, n-i-1, j, n-j-1) for j in range(n)] for i in range(n)]
    sit = [[True for j in range(n)] for i in range(n)]
    ans = 0
    for i, j in a:
        sit[i][j] = False
        ans += b[i][j]
        q = [(i, j, b[i][j])]
        while q:
            x, y, z = q.pop()
            if x != 0:
                if b[x-1][y] > z:
                    b[x-1][y] = z
                    q.append((x-1, y, z+sit[x-1][y]))
            if x != n-1:
                if b[x+1][y] > z:
                    b[x+1][y] = z
                    q.append((x+1, y, z+sit[x+1][y]))
            if y != 0:
                if b[x][y-1] > z:
                    b[x][y-1] = z
                    q.append((x, y-1, z+sit[x][y-1]))
            if y != n-1:
                if b[x][y+1] > z:
                    b[x][y+1] = z
                    q.append((x, y+1, z+sit[x][y+1]))
    print(ans)


main()
### BUGGY CODE ###

S=input()
k=[0]*2019
t=0
k[0]=1
for i,s in enumerate(S[::-1]):
  t+=int(s)*(10**i)
  l=t%2019
  k[l]+=1
  
ans=0
for l in k:
  ans+=l*(l-1)//2
print(ans)

### FIXED CODE ###

S=input()
k=[0]*2019
t=0
k[0]=1
for i,s in enumerate(S[::-1]):
  t+=int(s)*pow(10,i,2019)
  t%=2019
  k[t]+=1
  
ans=0
for l in k:
  ans+=l*(l-1)//2
print(ans)
### BUGGY CODE ###

N = int(input())

X = list(map(int,input().split()))

for i in range(1,N):
	if max(X[:i])-min(X[i:]) > 1:
		print("No")
		exit()
print("Yes")

### FIXED CODE ###

N = int(input())

X = list(map(int,input().split()))

maxlst = []
minlst = []
R = 10**10
L = 0
for i in range(N):
	L = max(X[i],L) 
	maxlst.append(L)
	R = min(X[-i-1],R)
	minlst.append(R)

minlst.reverse()
for i in range(N-1):
	if maxlst[i]-minlst[i+1] > 1:
		print("No")
		exit()
print("Yes")