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

import bisect
import sys
input=sys.stdin.readline
n=int(input())
lists=list(map(int,input().split()))
listss=sorted(lists)
count=0
for i in range(n):
    for j in range(n):
        if i!=j:
            index1=bisect.bisect_left(listss, abs(listss[j]-listss[i])+1)
            index2=bisect.bisect_right(listss,listss[i]+listss[j]-1)
            count+=(index2-index1)
            x=abs(listss[j]-listss[i])
            if  x+1<=listss[i]:
                count-=1
            if x+1<=listss[j]:
                count-=1
print(count//6)


### FIXED CODE ###

import bisect
n=int(input())
lists=list(map(int,input().split()))
lists=sorted(lists)

cnt=0
import math
for i in range(n):
    for j in range(i+1,n):
        #i<j 
        x=lists[i]
        y=lists[j]
        left=i 
        right=j 
        index=bisect.bisect_right(lists,y-x)
        cnt+=min(j-i-1,j-index)

print(cnt)
### BUGGY CODE ###

import sys


sys.setrecursionlimit(10 ** 9)

n,m = [int(_) for _ in input().split()]
l = [[] for _ in range(n+1)]
a = [0]*m
b = [0]*m
for i in range(m):
    a[i],b[i] = [int(_) for _ in input().split()]
    l[a[i]].append(b[i])
    l[b[i]].append(a[i])
seen = {}
for i in range(n+1):
    seen[i] = False

def dfs(l, todo):
    global seen
    global c
    if len(todo) != 0:
        s = todo.pop()
        for i in l[s]:
            if not seen[i]:
                todo.append(i)
                seen[i] = True
                c += 1
            else:
                continue
        return dfs(l, todo)
    else:
        return 0


for j in range(m):
    l[a[j]].remove(b[j])
    l[b[j]].remove(a[j])
    ans = 0
    for i in range(n+1):
        seen[i] = False
    for i in range(1, n+1):
        if not seen[i]:
            s = i
            seen[s] = True
            c = 1
            todo = [s]
            dfs(l, todo)
            ans += c * (n-c)
    print(ans//2)

### FIXED CODE ###

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

def check(a, b):
    global uf
    global ans_list
    if uf.same(a,b):
        ans_list.append(0)
    else:
        ans_list.append(uf.size(a)*uf.size(b))
        uf.union(a,b)
    return 0




n,m = [int(_) for _ in input().split()]
l = []
for i in range(m):
    l.append([int(_) for _ in input().split()])
l.reverse()

ans_list = []
uf = UnionFind(n+1)
for i in range(m):
    a,b = l[i]
    check(a, b)
ans_list.reverse()
s = [0] * m
s[0] = ans_list[0]
for i in range(1, len(ans_list)):
    s[i] = s[i-1] + ans_list[i]
for i in range(len(s)):
    print(s[i])
### BUGGY CODE ###

# from sys import stdin
import math


def combinations_count(n, r):
    if n < 2:
        return 0
    return n*(n-1)//2


N = int(input())
A = list(map(int, input().split()))
# print(A)
d = {}
cnt = 0
for l in range(N):
    for i in range(N):
        if i != cnt:
            d[A[i]] = d.get(A[i], 0)+1
    # print(d)
    # print(len(d))
    sum = 0
    for key, value in enumerate(d):
        # print(key,value)
        sum = sum+combinations_count(d.get(value, 0), 2)
    print(sum)
    d = {}
    cnt += 1


### FIXED CODE ###

# from sys import stdin
# import math
from functools import lru_cache


@lru_cache(maxsize=1000)
def combinations_count(n, r):
    if n < 2:
        return 0
    return n*(n-1)//2


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

d = {}
cnt = 0
for i in range(N):
    d[A[i]] = d.get(A[i], 0)+1
# print(d)
sum1 = 0
for key, value in enumerate(d):
    # print(key,value)
    sum1 = sum1+combinations_count(d.get(value, 0), 2)

for l in range(N):

    # print(d)
    # print(len(d))
    sum = 0
    sum = sum1-combinations_count(d.get(A[cnt], 0), 2)
    d[A[cnt]] = d.get(A[cnt], 0)-1
    sum = sum+combinations_count(d.get(A[cnt], 0), 2)
    # for key, value in enumerate(d):
    #     # print(key,value)
    #     sum = sum+combinations_count(d.get(value, 0), 2)
    print(sum)
    d[A[cnt]] = d.get(A[cnt], 0)+1
    # d = {}
    cnt += 1
### BUGGY CODE ###

N=int(input())
List=list(map(int,input().split()))

break_count=0
break_flag=0
for i in range(N):
  for j in range(i):
    if List[i]>List[j]:
      break_count+=1
      break_flag=1
      break
  if break_flag==1:
    break_flag=0
    continue
print(N-break_count)

### FIXED CODE ###

N=int(input())
List=list(map(int,input().split()))

counter=0
mj=N
for i in range(N):
  if mj>List[i]:
    mj=List[i]
  if List[i]==mj:
    counter+=1
print(counter)
### BUGGY CODE ###

#! /usr/bin/env python3

N, x = map(int, input().split())
C = list(map(int, input().split()))
cnt = 0
for i in range(N-1):
    while C[i]+C[i+1] > x:
        cnt += 1
        C[i+1] -= 1
print(cnt)

### FIXED CODE ###

#! /usr/bin/env python3

N, x = map(int, input().split())
C = list(map(int, input().split()))
cnt = 0
for i in range(N-1):
    if C[i]+C[i+1] > x:
        a = C[i]+C[i+1] - x
        cnt += a
        C[i] = min(C[i+1]-a, 0)
        C[i+1] = max(C[i+1]-a, 0)
print(cnt)
### BUGGY CODE ###

import math, sys
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque
from copy import deepcopy
from functools import lru_cache
from heapq import heapify, heappop, heappush
from itertools import accumulate, combinations, permutations
input = sys.stdin.readline
mod = 10**9 + 7
ns = lambda: input().strip()
ni = lambda: int(input().strip())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

def main():
    a, b, k = nm()

    for i in range(a, b+1):
        if i < a + k or i > b - k:
            print(i)


if __name__ == '__main__':
    main()

### FIXED CODE ###

import math, sys
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque
from copy import deepcopy
from functools import lru_cache
from heapq import heapify, heappop, heappush
from itertools import accumulate, combinations, permutations
input = sys.stdin.readline
mod = 10**9 + 7
ns = lambda: input().strip()
ni = lambda: int(input().strip())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

def main():
    a, b, k = nm()

    if 2*k >= b - a + 1:
        for i in range(a, b+1):
            print(i) 
        exit()

    for i in range(a, a+k):
        print(i)

    for i in range(b-k+1, b+1):
        print(i)


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

from bisect import bisect_left, bisect_right
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

def main():
    N,M,Q,*lr = map(int, read().split())

    LR = []
    query = []
    for i, (l, r) in enumerate(zip(*[iter(lr)]*2)):
        if i < M:
            LR.append((l, r))
        else:
            query.append((l, r))
    
    LR.sort()
    L = [int(i[0]) for i in LR]
    R = [int(i[1]) for i in LR]
    for p, q in query:
        p_in_L = bisect_left(L, p)
        q_in_L = bisect_right(L, q)
        q_in_R = bisect_right(sorted(R[p_in_L:q_in_L]), q)
        print(q_in_R)


if __name__ == "__main__":
    main()


### FIXED CODE ###

# 累積和(1次元)を用いた解法
from itertools import accumulate
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

def main():
    N,M,Q,*lr = map(int, read().split())

    LR = [[0]*(N+1) for _ in range(N+1)]
    query = []
    for i, (l, r) in enumerate(zip(*[iter(lr)]*2)):
        if i < M:
            LR[l][r] += 1
        else:
            query.append((l, r))
    
    LR_acc = []
    for L in LR:
        LR_acc.append(list(accumulate(L)))
    
    for p, q in query:
        ans = 0
        for i in range(p,q+1):
            ans += LR_acc[i][q] - LR_acc[i][p-1]
        print(ans)


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

s = input()
ans = 0

while True:
    ans += s.count("01")*2
    s = s.replace("01", "")
    ans += s.count("10")*2
    s = s.replace("10", "")
    if not (s.count("01") or s.count("10")):
        break

print(ans)

### FIXED CODE ###

import collections
s = list(input())
c = collections.Counter(s)
print(min(c["0"], c["1"])*2)
### BUGGY CODE ###

n = int(input())
A = [int(input()) for _ in range(n)]
print(len(list(filter(lambda x: A.count(x)%2==1, set(A)))))

### FIXED CODE ###

from collections import Counter

n = int(input())
A = [int(input()) for _ in range(n)]
ans = 0
for k, v in Counter(A).items():
    if v % 2 == 0:
        continue
    ans += 1
print(ans)
### BUGGY CODE ###

N=int(input())

string=list()
for i in range(N):
  c=list(input())
  c.sort()
 # print(''.join(c))
  string.append(''.join(c))
  
#print(string)

count=0
M=N
for j in range(N):
  s=string[0]
  c=string.count(s)
  M=M-c
  count=count +int(c*(c-1)/2)
  for k in range(c):
   string.remove(s)
  if j+1>M-1:
    break
  
print(count)
  

### FIXED CODE ###

import collections
N=int(input())

string=list()
for i in range(N):
  c=list(input())
  c.sort()
 # print(''.join(c))
  string.append(''.join(c))
  
#print(string)

a=collections.Counter(string)
b=list(a.values())

count=0
for j in range(len(b)):
  count=count +int(b[j]*(b[j]-1)/2)
  
print(count)
### BUGGY CODE ###

h,w,d=map(int,input().split())
box=[list(map(int,input().split())) for i in range(h)]
adress={}
for i in range(h):
    for j in range(w):
        adress[box[i][j]]=[i,j]
q=int(input())
order=[list(map(int,input().split())) for i in range(q)]
for x,y in order:
    ans=0
    try:
        for i in range(x,y-1,d):
            ans +=abs(adress[i+d][0]-adress[i][0])+abs(adress[i+d][1]-adress[i][1])
        print(ans)
    except:print(0)

### FIXED CODE ###

H, W, D = map(int, input().split())
m = [None] * (H * W + 1)
for i in range(H):
    l = map(int, input().split())
    for j, a in enumerate(l):
        m[a] = (i, j)
Q = int(input())
LR = []
for _ in range(Q):
    l, r = map(int, input().split())
    LR.append((l, r))
 
S = [0] * (H * W + 1)
for k in range(D + 1, H * W + 1):
    x, y = m[k]
    i, j = m[k - D]
    S[k] = S[k - D] + abs(x - i) + abs(y - j)
 
for l, r in LR:
    print(S[r] - S[l])
### BUGGY CODE ###

from collections import defaultdict


def prepare(n, MOD):
    f = 1
    factorials = [1]
    for m in range(1, n + 1):
        f *= m
        f %= MOD
        factorials.append(f)
    inv = pow(f, MOD - 2, MOD)
    invs = [1] * (n + 1)
    invs[n] = inv
    for m in range(n, 1, -1):
        inv *= m
        inv %= MOD
        invs[m - 1] = inv

    return factorials, invs


def prime_factorize(n):
    f = defaultdict(lambda: 0)
    f[2] = len(bin(n & -n)) - 3
    i = 3
    k = n >> f[2]
    while k > 1:
        while True:
            d, m = divmod(k, i)
            if m == 0:
                f[i] += 1
                k = d
            else:
                break
        i += 2
    return f


MOD = 1000000007
n, m = map(int, input().split())
pf = prime_factorize(m)
mx = max(pf.values())
f, r = prepare(n + mx, MOD)
ans = 1
for p, e in pf.items():
    ans *= f[n + e - 1] * r[n - 1]
    ans %= MOD
    ans *= r[e]
    ans %= MOD
print(ans)


### FIXED CODE ###

from collections import defaultdict


def prepare(n, MOD):
    f = 1
    factorials = [1]
    for m in range(1, n + 1):
        f *= m
        f %= MOD
        factorials.append(f)
    inv = pow(f, MOD - 2, MOD)
    invs = [1] * (n + 1)
    invs[n] = inv
    for m in range(n, 1, -1):
        inv *= m
        inv %= MOD
        invs[m - 1] = inv

    return factorials, invs


def prime_factorize(n):
    f = defaultdict(lambda: 0)
    f[2] = len(bin(n & -n)) - 3
    i = 3
    k = n >> f[2]
    while k > 1:
        while True:
            d, m = divmod(k, i)
            if m == 0:
                f[i] += 1
                k = d
            else:
                break
        i += 2
    return f


MOD = 1000000007
n, m = map(int, input().split())
pf = prime_factorize(m)
mx = max(pf.values())
f, r = prepare(n + mx, MOD)
ans = 1
for p, e in pf.items():
    ans *= f[n + e - 1] * r[n - 1]
    ans %= MOD
    ans *= r[e]
    ans %= MOD
print(ans)
### BUGGY CODE ###

N = int(input())

Xs = list(map(int,input().split()))
sort_xs = sorted(Xs)

for i in range(N):
    index= sort_xs.index(Xs[i])
    mid = 0
    if index < N // 2:
        mid = N // 2
    else:
        mid = N // 2 - 1

    print(sort_xs[mid])


### FIXED CODE ###

N = int(input())

Xs = list(map(int,input().split()))
sort_xs = sorted(Xs)
l = sort_xs[N // 2 - 1]
r = sort_xs[N // 2]

for i in range(N):
    print(r if Xs[i] <= l else l)
### BUGGY CODE ###


s = input()
number = 0
for i in range(0,len(s)- 3):
    for j in range(i+3,len(s)):
        sum = 0
        for k in range(j-i+1):
            sum = sum + int(s[j- k]) * (10 ** (k))
            #print(sum)
        if sum % 2019 == 0:
            number = number + 1
print(number)

### FIXED CODE ###

s = input()
t = 0
number = 0
#m = [0 for i in range(2019)]
m = [0]*2019
m[0] = 1
a = 1
for i in range(len(s)):
    t = t  +  int(s[len(s) - 1 - i])* a 
    #print(t)
    m[t % 2019] += 1
    a = a * 10
    a = a % 2019
    #print(t%2019)

for j in range(2019):
    #if m[j]>= 2:
    number += (m[j]*(m[j] - 1))//2

print(number)
### BUGGY CODE ###

n = int(input())
s = list(str(input()))

R = []
G = []
B = []

for i in range(n):
    if s[i] == 'R':
        R.append(i)
    elif s[i] == 'G':
        G.append(i)
    elif s[i] == 'B':
        B.append(i)
c = 0
for r in R:
    for g in G:
        for b in B:
            [i,j,k] = sorted([r,g,b])
            if j-i != k-j:
                c = c+1
print(c)

### FIXED CODE ###

n = int(input())
s = list(str(input()))


c = 0
for i in range(n):
    for j in range(i+1, n):
        k = 2*j-i
        if k<n and s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
            c = c+1
            
        

R = 0
G = 0
B = 0

for i in range(n):
    if s[i] == 'R':
        R = R+1
    elif s[i] == 'G':
        G = G+1
    elif s[i] == 'B':
        B = B+1

print(R*G*B-c)
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()

from collections import Counter

def main():
    N = II()
    A = LI()
    idxs = [0] * (N + 1)
    for i, a in enumerate(A):
        idxs[a] = i
    def func(l, r):
        if l == r:
            return 0
        a = min(A[l:r])
        idx = idxs[a]
        area = (idx + 1 - l) * (r - idx) * a
        return area + func(l, idx) + func(idx + 1, r)
    ans = func(0, N)

    return ans

print(main())


### FIXED CODE ###

import sys
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()

from collections import Counter

def main():
    N = II()
    A = LI()
    idxs = [0] * (N + 1)
    for i, a in enumerate(A):
        idxs[a] = i
    ll = list(range(N + 1))
    rr = list(range(N + 1))
    ans = 0
    for a in range(N, 0, -1):
        idx = idxs[a]
        l, r = ll[idx], rr[idx]
        ans += (idx - l + 1) * (r - idx + 1) * a
        ll[r + 1] = l
        rr[l - 1] = r

    return ans

print(main())
### BUGGY CODE ###

import sys
INF = 10 ** 15
MOD = 10 ** 9 + 7
sys.setrecursionlimit(100000000)
input = sys.stdin.readline
from math import pi
from functools import lru_cache

def main():
    n = int(input())
    num = list(map(int,input().split()))
    
    @lru_cache(maxsize = None)
    def dfs(a,b,c):
        if a + b + c == n:
            return 1
        ans = 0
        if num[a + b + c] == a:
            ans += dfs(a + 1,b,c)
            ans %= MOD
        if num[a + b + c] == b:
            ans += dfs(a,b + 1,c)
            ans %= MOD
        if num[a + b + c] == c:
            ans += dfs(a,b,c + 1)
            ans %= MOD
        return ans
    
    print(dfs(0,0,0))

if __name__ =='__main__':
    main()    

### FIXED CODE ###

MOD = 10 ** 9 + 7
INF = 10 ** 10
import sys
sys.setrecursionlimit(100000000)
dy = (-1,0,1,0)
dx = (0,1,0,-1)

ans = 1
def dfs(i,x,y,z,a,n):
    global ans
    if i == n:
        return
    if x == y == z == a[i]:
        ans *= 3
        ans %= MOD
        dfs(i + 1,x + 1,y,z,a,n)
    elif x == y == a[i]:
        ans *= 2
        ans %= MOD
        dfs(i + 1,x + 1,y,z,a,n)
    elif y == z == a[i]:
        ans *= 2
        ans %= MOD
        dfs(i + 1,x,y + 1,z,a,n)
    elif x == a[i]:
        dfs(i + 1,x + 1,y,z,a,n)
    elif y == a[i]:
        dfs(i + 1,x,y + 1,z,a,n)
    elif z == a[i]:
        dfs(i + 1,x,y,z + 1,a,n)
    else:
        ans = 0
        return

def main():
    n = int(input())
    a = list(map(int,input().split()))

    dfs(0,0,0,0,a,n)
    print(ans)

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

n=int(input())
ans=0

if n==1:
    print(0)
    exit()
    
for i in range(n):
    ans+=i
print(ans)

### FIXED CODE ###

n=int(input())

if n==1:
    print(0)
    exit()
    

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

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

dp = [10**9]*n
dp[0] = 0
dp[1] = dp[0] + abs(h[1]-h[0])

for i in range(2, n): 
    #dp[i] = min([dp[i-j]+abs(h[i]-h[i-j]) for j in range(1, min(k, i)+1)])
    for j in range(1, min(k, i)+1):  
        dp[i] = min([dp[i], dp[i-j]+abs(h[i]-h[i-j])])
        #print(i, dp[i])

print(dp[n-1])

### FIXED CODE ###

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

dp = [10**9+1]*n
dp[0] = 0
dp[1] = dp[0] + abs(h[1]-h[0])

for i in range(2, n): 
    dp[i] = min([dp[i-j]+abs(h[i]-h[i-j]) for j in range(1, min(k, i)+1)])

print(dp[n-1])
### BUGGY CODE ###

#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#

import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
H,n = readInts()
A = []
B = []
for i in range(n):
    a,b = readInts()
    A.append(a)
    B.append(b)
# dp[h][i] := 体力 h の時の、魔法i番目を使った時の魔力の最小値
# dp[h] でいいかもこれ
INF = float('inf')
dp = [INF] * (H+1)
dp[0] = 0
for h in range(0,H+1):
    for i in range(n):
        dp[min(h+A[i],H)] = min(dp[min(h+A[i],H)], dp[h] + B[i])

#print(dp)
print(dp[H])


### FIXED CODE ###

#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#

import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
H,n = readInts()
A = []
B = []
for i in range(n):
    a,b = readInts()
    A.append(a)
    B.append(b)
# dp[h][i] := 体力 h の時の、魔法i番目を使った時の魔力の最小値
# dp[h] でいいかもこれ
INF = float('inf')
dp = [INF] * (H+1)
dp[0] = 0
for h in range(0,H+1):
    for i in range(n):
        dp[min(h+A[i],H)] = min(dp[min(h+A[i],H)], dp[h] + B[i])

#print(dp)
print(dp[H])
### BUGGY CODE ###

(N,M),B,P = [map(int,raw_input().split()) for i in xrange(3)]
C1 = []
for i in xrange(M): C1 += [1]*P[i] if i%2 else [0]*P[i]
C2 = [1-c for c in C1]

ans = 1e10
for C in [C1,C2]:
    if sum(B) == sum(C):
        tmp = 0
        i = j = 0
        while not i == j == N-1:
            if B[i] and C[j]:
                tmp += abs(i-j)
                i += 1; j += 1
            else:
                i += 1-B[i]; j += 1-C[i]
            i = min(N-1,i); j = min(N-1,j)
        ans = min(ans, tmp)
print ans

### FIXED CODE ###

N,M = map(int,raw_input().split())
B = map(int,raw_input().split())
P = map(int,raw_input().split())
C1,C2 = [],[]
for i in xrange(M):
    C1 += [1]*P[i] if i%2 else [0]*P[i]
    C2 += [0]*P[i] if i%2 else [1]*P[i]

ans = 1e10
for C in [C1,C2]:
    if sum(B) == sum(C):
        tmp = 0
        i = j = 0
        while not i == j == N-1:
            if not B[i]: i = min(N-1, i+1)
            if not C[j]: j = min(N-1, j+1)
            if B[i] and C[j]:
                tmp += abs(i-j)
                i = min(N-1, i+1)
                j = min(N-1, j+1)
        ans = min(ans, tmp)
print ans
### BUGGY CODE ###

from collections import defaultdict

H, N = map(int, input().split())
AB = [tuple(map(int, input().split())) for _ in range(N)]
AB.sort()
dp = [float("inf")] * (H + AB[-1][0])
dp[0] = 0
for i in range(1, len(dp)):
    for a, b in AB:
        if i - a >= 0:
            dp[i] = min(dp[i], dp[i - a] + b)
        else:
            break

ans = min(dp[H:])
print(ans)



### FIXED CODE ###

from functools import lru_cache
import sys

sys.setrecursionlimit(10 ** 8)

H, N = map(int, input().split())
AB = [tuple(map(int, input().split())) for _ in range(N)]
# 効率順にソート
# 効率が同じなら、小規模な魔法を優先
AB.sort(key=lambda ab: (ab[1] / ab[0], ab[0]))


@lru_cache(maxsize=None)
def dp(h):
    if h <= 0:
        return 0
    else:
        best = float("inf")
        for a, b in AB:
            val = b + dp(h - a)
            if val < best:
                best = val
            else:
                # 更新されなかった場合は、以降の魔法で更新されることはない
                break
        return best


print(dp(H))