CodeEff/ECCO huggingface dataset:
# Original solution
import sys

input = sys.stdin.readline


def main():
    N = int(eval(input()))
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

    surplus_scores = []
    sum_lack_score = 0
    ans = 0
    for a, b in zip(A, B):
        if a > b:
            surplus_scores.append(a - b)
        elif a < b:
            sum_lack_score += b - a
            ans += 1

    if sum_lack_score > 0:
        surplus_scores.sort(reverse=True)
        pass_exam = False
        for i, score in enumerate(surplus_scores):
            sum_lack_score -= score
            if sum_lack_score <= 0:
                ans += (i + 1)
                pass_exam = True
                break
        if not pass_exam:
            ans = -1

    print(ans)


if __name__ == "__main__":
    main()


# Optimized solution
import sys

input = sys.stdin.readline


def main():
    N = int(eval(input()))
    A = tuple(map(int, input().split()))
    B = tuple(map(int, input().split()))

    surplus_scores = []
    sum_lack_score = 0
    ans = 0
    for a, b in zip(A, B):
        if a > b:
            surplus_scores.append(a - b)
        elif a < b:
            sum_lack_score += b - a
            ans += 1

    if sum_lack_score > 0:
        surplus_scores.sort(reverse=True)
        pass_exam = False
        for i, score in enumerate(surplus_scores):
            sum_lack_score -= score
            if sum_lack_score <= 0:
                ans += (i + 1)
                pass_exam = True
                break
        if not pass_exam:
            ans = -1

    print(ans)


if __name__ == "__main__":
    main()
# Original solution
import sys

read = sys.stdin.readline

N = int(read())
A = list(map(int, read().split()))
B = list(map(int, read().split()))

d = [i - j for i, j in zip(A, B)]
d.sort()
if d[0] >= 0:
    print((0))
    exit()

n = 0
below = 0
for i in d:
    if i < 0:
        below += i
        n += 1
    else:
        break

for i in d[::-1]:
    below += i
    n += 1
    if below > 0:
        print(n)
        exit()
else:
    print((-1))

# Optimized solution
import sys

read = sys.stdin.readline

N = int(read())
A = list(map(int, read().split()))
B = list(map(int, read().split()))

d = [i - j for i, j in zip(A, B)]
d.sort()
if d[0] >= 0:
    print((0))
    exit()
elif sum(d) < 0:
    print((-1))
    exit()

n = 0
below = 0
for i in d:
    if i < 0:
        below += i
        n += 1
    else:
        break

for i in d[::-1]:
    below += i
    n += 1
    if below > 0:
        print(n)
        exit()
# Original solution
def c_exam_and_wizard(N, A, B):
    ans = 0
    short_val = 0  # 合格に不足する準備度
    can_give = []
    for a, b in zip(A, B):
        if a < b:
            ans += 1
            short_val += b - a
        elif a > b:
            can_give.append(a - b)

    if short_val == 0:
        return 0
    can_give.sort()

    idx = len(can_give) - 1
    while True:
        if not can_give:
            ans = -1
            break

        if can_give[idx] >= short_val:
            ans += 1
            break

        short_val -= can_give[idx]
        can_give.pop()
        ans += 1
        idx -= 1
    return ans

N = int(eval(input()))
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
print((c_exam_and_wizard(N, A, B)))

# Optimized solution
def c_exam_and_wizard(N, A, B):
    ans = 0
    sum_of_shortage_for_passing_mark = 0
    campensation_for_shortage = []
    for a, b in zip(A, B):
        if a < b:
            ans += 1
            sum_of_shortage_for_passing_mark += b - a
        elif a > b:
            campensation_for_shortage.append(a - b)

    if sum_of_shortage_for_passing_mark == 0:
        return 0  # 準備度を変更するまでもない

    campensation_for_shortage.sort(reverse=True)  # A_i-B_iが大きなiから分けていく
    for pts in campensation_for_shortage:
        if pts >= sum_of_shortage_for_passing_mark:
            ans += 1
            break

        sum_of_shortage_for_passing_mark -= pts
        ans += 1
    else:
        ans = -1
    return ans

N = int(eval(input()))
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
print((c_exam_and_wizard(N, A, B)))
# Original solution
def c_exam_and_wizard(N, A, B):
    ans = 0
    sum_of_shortage_for_passing_mark = 0
    campensation_for_shortage = []
    for a, b in zip(A, B):
        if a < b:
            ans += 1
            sum_of_shortage_for_passing_mark += b - a
        elif a > b:
            campensation_for_shortage.append(a - b)

    if sum_of_shortage_for_passing_mark == 0:
        return 0  # 準備度を変更するまでもない

    campensation_for_shortage.sort(reverse=True)  # A_i-B_iが大きなiから分けていく
    for pts in campensation_for_shortage:
        if pts >= sum_of_shortage_for_passing_mark:
            ans += 1
            break

        sum_of_shortage_for_passing_mark -= pts
        ans += 1
    else:
        ans = -1
    return ans

N = int(eval(input()))
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
print((c_exam_and_wizard(N, A, B)))

# Optimized solution
def c_exam_and_wizard(N, A, B):
    ans = 0
    sum_of_shortage_for_passing_mark = 0
    campensation_for_shortage = []
    for a, b in zip(A, B):
        if a < b:
            ans += 1
            sum_of_shortage_for_passing_mark += b - a
        elif a > b:
            campensation_for_shortage.append(a - b)

    if sum_of_shortage_for_passing_mark == 0:
        return 0  # 準備度を変更するまでもない

    campensation_for_shortage.sort(reverse=True)  # A_i-B_iが大きなiから分けていく
    for pts in campensation_for_shortage:
        sum_of_shortage_for_passing_mark -= pts
        ans += 1
        if sum_of_shortage_for_passing_mark < 0:
            break
    else:
        ans = -1
    return ans

N = int(eval(input()))
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
print((c_exam_and_wizard(N, A, B)))
# Original solution
n = int(eval(input()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = []
cnt = 0
lack = 0

for i in range(n):
    if a[i] < b[i]:
        cnt += 1
        lack += b[i] - a[i]
    elif a[i] > b[i]:
        c.append(a[i] - b[i])

c = sorted(c)[::-1]
asum = sum(a)
bsum = sum(b)

if asum < bsum:
    print((-1))
    exit()
if cnt == 0:
    print((0))
    exit()

for i in c:
    lack -= i
    cnt += 1
    if lack <= 0:
        break

print(cnt)

# Optimized solution
n = int(eval(input()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = []
cnt = 0
lack = 0

for i in range(n):
    diff = a[i] - b[i]
    if diff < 0:
        cnt += 1
        lack -= diff
    elif diff > 0:
        c.append(diff)

c = sorted(c)[::-1]

if sum(a) < sum(b):
    print((-1))
    exit()

for i in c:
    if lack <= 0:
        break
    lack -= i
    cnt += 1

print(cnt)
# Original solution
n = int(eval(input()))
a = list(map(int,input().split()))
flag = 1
count = 0
while flag:
    flag = 0
    for i in range(n-1):
        if a[i] > a[i+1]:
            a[i], a[i+1] = a[i+1], a[i]
            count += 1
            flag = 1

print(count)

# Optimized solution
n = int(eval(input()))
s = list(map(int,input().split()))
count = 0

def merge(a, left, mid, right):
    global count
    l = a[left:mid] + [float("inf")]
    r = a[mid:right] + [float("inf")]
    i = j = 0
    for k in range(left, right):
        if l[i] <= r[j]:
            a[k] = l[i]
            i += 1
        else:
            a[k] = r[j]
            j += 1
            count += len(l) - i - 1

def mergeSort(a, left, right):
    if left + 1 < right:
        mid = right + (left - right)//2
        mergeSort(a, left, mid)
        mergeSort(a, mid, right)
        merge(a, left, mid, right)

mergeSort(s, 0, n)

print(count)
# Original solution
sentinel = 1000000001

def merge(A, left, mid, right):
    """Merge the two sorted partial sequences A[left:mid] and A[mid:right].

    To count the number of inversions,
    make a global declaration for the variable cnt.
    """
    L = A[left:mid]
    n1 = len(L)
    L.append(sentinel)
    R = A[mid:right]
    R.append(sentinel)
    i = 0
    j = 0
    global cnt
    for k in range(left, right):
        if L[i] <= R[j]:
            A[k] = L[i]
            i += 1
        else:
            A[k] = R[j]
            j += 1
            cnt += (n1 - i)

def mergeSort(A, left, right):
    if left + 1 < right:
        mid = (left + right) // 2
        mergeSort(A, left, mid)
        mergeSort(A, mid, right)
        merge(A, left, mid, right)


n = int(eval(input()))
S = list(map(int, input().split()))

cnt = 0

mergeSort(S, 0, n)

print(cnt)

# Optimized solution
sentinel = 1000000001

def merge(A, left, mid, right):
    """Merge the two sorted partial sequences A[left:mid] and A[mid:right].

    To count the number of inversions,
    make a global declaration for the variable cnt.
    """
    L = A[left:mid]
    n1 = len(L)
    L.append(sentinel)
    R = A[mid:right]
    R.append(sentinel)
    i = 0
    j = 0
    cnt = 0
    for k in range(left, right):
        if L[i] <= R[j]:
            A[k] = L[i]
            i += 1
        else:
            A[k] = R[j]
            j += 1
            cnt += n1 - i
    return cnt

def mergeSort(A, left, right):
    if left + 1 < right:
        mid = (left + right) // 2
        cnt1 = mergeSort(A, left, mid)
        cnt2 = mergeSort(A, mid, right)
        cnt3 = merge(A, left, mid, right)
        return cnt1 + cnt2 + cnt3
    else:
        return 0


n = int(eval(input()))
S = list(map(int, input().split()))

cnt = 0

n_ivs =  mergeSort(S, 0, n)

print(n_ivs)
# Original solution
n = int(eval(input()))
A = list(map(int, input().split()))

cnt = 0
for i in range(len(A) - 1):
  for j in range(i + 1, len(A)):
    if A[i] > A[j]: cnt += 1

print(cnt)

# Optimized solution
SENTINEL = 1000000001

def merge(A, l, m, r):
  L = A[l:m] + [SENTINEL]
  R = A[m:r] + [SENTINEL]
  i = 0
  j = 0
  cnt = 0
  for k in range(l, r):
    if L[i] <= R[j]:
      A[k] = L[i]
      i += 1
    else:
      A[k] = R[j]
      j += 1
      cnt += len(L) - 1 - i
  return cnt

def merge_sort(A, l, r):
  if l + 1 < r:
    m = (l + r) // 2
    cnt = 0
    cnt += merge_sort(A, l, m)
    cnt += merge_sort(A, m, r)
    cnt += merge(A, l, m, r)
    return cnt
  else:
    return 0

n = int(eval(input()))
A = list(map(int, input().split()))

print((merge_sort(A, 0, n)))
# Original solution
def g(A,l,m,r):
 global c
 L,R=A[l:m]+[1e9],A[m:r]+[1e9]
 i=j=0
 for k in range(l,r):
  if L[i]<R[j]:A[k]=L[i];i+=1
  else:A[k]=R[j];j+=1;c+=m-l-i
 return c
def s(A,l,r):
 if l+1<r:
  m=(l+r)//2
  return s(A,l,m)+s(A,m,r)+g(A,l,m,r)
 else:return 0
c=0
n=int(eval(input()))
A=list(map(int,input().split()))
s(A,0,n)
print(c)


# Optimized solution
def g(A,l,m,r):
 global c
 L,R=A[l:m]+[1e9],A[m:r]+[1e9]
 i=j=0
 for k in range(l,r):
  if L[i]<R[j]:A[k]=L[i];i+=1;c+=j
  else:A[k]=R[j];j+=1
def s(A,l,r):
 if l+1<r:m=(l+r)//2;s(A,l,m);s(A,m,r);g(A,l,m,r)
c=0
n=int(eval(input()))
A=list(map(int,input().split()))
s(A,0,n)
print(c)
# Original solution
#coding:UTF-8
def NI(A):
    cnt=0
    for i in range(len(A)):
        for j in reversed(list(range(i+1,len(A)))):
            if A[j]<A[j-1]:
                swap=A[j]
                A[j]=A[j-1]
                A[j-1]=swap
                cnt+=1
    print(cnt)
    
if __name__=="__main__":
    n=int(eval(input()))
    A=input().split(" ")
    for i in range(n):
        A[i]=int(A[i])
    NI(A)

# Optimized solution
 #coding:UTF-8
def Marge(A,left,mid,right):
    n1=mid-left
    n2=right-mid
    L=[]
    R=[]
    c=0
    for i in range(n1):
        L.append(A[left+i])
    for i in range(n2):
        R.append(A[mid+i])
    L.append(100000000000000000)
    R.append(100000000000000000)
    i=j=0
    for k in range(left,right):
        if L[i]<=R[j]:
            A[k]=L[i]
            i+=1
        else:
            A[k]=R[j]
            j+=1
            c+=n1-i
    return c
            
def MS(A,left,right,c):
    
    if left+1<right:
        mid=int((left+right)/2)
        c1=MS(A,left,mid,c)
        c2=MS(A,mid,right,c)
        c3=Marge(A,left,mid,right)
        return c1+c2+c3
    return 0
 
    
if __name__=="__main__":
    n=int(eval(input()))
    A=input().split(" ")
    cnt=0
    for i in range(n):
        A[i]=int(A[i])
    cnt=MS(A,0,n,0)
    print(cnt)
# Original solution
n = int(eval(input()))
lis = list(map(int,input().split()))
def merge_sort(arr):
    if len(arr) <= 1:
        return arr

    mid = len(arr) // 2
    # ここで分割を行う
    left = arr[:mid]
    right = arr[mid:]

    # 再帰的に分割を行う
    left = merge_sort(left)
    right = merge_sort(right)

    # returnが返ってきたら、結合を行い、結合したものを次に渡す
    return merge(left, right)


def merge(left, right):
    global ans
    merged = []
    l_i, r_i = 0, 0

    # ソート済み配列をマージするため、それぞれ左から見ていくだけで良い
    while l_i < len(left) and r_i < len(right):
        # ここで=をつけることで安定性を保っている
        if left[l_i] <= right[r_i]:
            merged.append(left[l_i])
            l_i += 1
        else:
            merged.append(right[r_i])
            r_i += 1
            ans += (len(left) - l_i)

    # 上のwhile文のどちらかがFalseになった場合終了するため、あまりをextendする
    if l_i < len(left):
        merged.extend(left[l_i:])
    if r_i < len(right):
        merged.extend(right[r_i:])
    return merged

ans = 0
merge_sort(lis)
print(ans)



# Optimized solution
#教室内の位置は右*中央
#問題は「Recursion / Divide and Conquer - The Number of Inversions」(http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_5_D&lang=jp)

#とりあえず入力部
n = int(eval(input()))
lis = list(map(int,input().split()))

#方針ですが、N=200000というところから、O(N)もしくはO(NlogN)であるという推測が立ちます。
#転倒数はバブルソートの時の入れ替えの回数というヒントが示されているので、ソートを使えばいいのではないかという予想が生えます
#というわけで計算量が一定であるところのマージソートが良さそうという気がします
#マージソートの機構を考えれば、マージの際に左側と右側を合わせていくわけですが、
#左側の数字が右の数字より大きい場合、その分だけ転倒数が生まれます(それぞれは部分的にソートされているので)
#したがって、マージする時に左が右より大きければある数だけ転倒数を増やします
#「ある数」とは、左側にあとn個数字が残っていればそのタイミングでのnになります
#以下は実装部分ですが、マージソートを描くのが面倒なのでQiitaから貼り付けました
#マージの部分が関数になっているので、ansを増やす時に関数の中で動かすためグローバル変数にします

def merge_sort(arr):
    if len(arr) <= 1:
        return arr

    mid = len(arr) // 2
    left = arr[:mid]
    right = arr[mid:]

    left = merge_sort(left)
    right = merge_sort(right)

    return merge(left, right)


def merge(left, right):
    global ans
    merged = []
    l_i, r_i = 0, 0

    while l_i < len(left) and r_i < len(right):
        if left[l_i] <= right[r_i]:
            merged.append(left[l_i])
            l_i += 1
        else:
            merged.append(right[r_i])
            r_i += 1
            ans += (len(left) - l_i)

    if l_i < len(left):
        merged.extend(left[l_i:])
    if r_i < len(right):
        merged.extend(right[r_i:])
    return merged

ans = 0
merge_sort(lis)
print(ans)
# Original solution
def merge_sort(A):
    if len(A) <= 1:
        return A, 0
    mid = len(A) // 2
    #print A[:mid]
    #print A[mid:]
    left, lcount = merge_sort(A[:mid])
    right, rcount = merge_sort(A[mid:])
    merged, count = merge(left, right)
    return merged, lcount + rcount + count

def merge(left, right):
    lpos = 0
    rpos = 0
    rcount = 0
    merged = []
    while lpos < len(left) and rpos < len(right):
        if left[lpos] < right[rpos]:
            merged.append(left[lpos])
            lpos += 1
        else:
            merged.append(right[rpos])
            rpos += 1
            rcount += len(left[lpos:])
    
    if left[lpos:]:
        merged.extend(left[lpos:])
    if right[rpos:]:
        merged.extend(right[rpos:])
    
    return merged, rcount
    
if __name__ == '__main__':
    n = int(input())
    A = list(map(int, input().split()))
    
    sorted, count = merge_sort(A)
    #print ' '.join(str(x) for x in sorted)
    print(count)

# Optimized solution
def merge_sort(A):
    if len(A) <= 1:
        return A, 0
    mid = len(A) // 2
    left, lcount = merge_sort(A[:mid])
    right, rcount = merge_sort(A[mid:])
    merged, count = merge(left, right)
    return merged, lcount + rcount + count

def merge(left, right):
    lpos = 0
    rpos = 0
    rcount = 0
    merged = []
    while lpos < len(left) and rpos < len(right):
        if left[lpos] < right[rpos]:
            merged.append(left[lpos])
            lpos += 1
        else:
            merged.append(right[rpos])
            rpos += 1
            rcount += len(left[lpos:])
    
    if left[lpos:]:
        merged.extend(left[lpos:])
    if right[rpos:]:
        merged.extend(right[rpos:])
    
    return merged, rcount
    
if __name__ == '__main__':
    n = int(input())
    A = list(map(int, input().split()))
    
    sorted, count = merge_sort(A)
    #print ' '.join(str(x) for x in sorted)
    print(count)
# Original solution
import array

def merge_sort(A):
    if len(A) <= 1:
        return A, 0
    mid = len(A) // 2
    left, lcount = merge_sort(A[:mid])
    right, rcount = merge_sort(A[mid:])
    merged, count = merge(left, right)
    return merged, lcount + rcount + count

def merge(left, right):
    lpos = 0
    rpos = 0
    rcount = 0
    merged = array.array('L', [])
    len_left = len(left)
    len_right = len(right)
    while lpos < len_left and rpos < len_right:
        if left[lpos] < right[rpos]:
            merged.append(left[lpos])
            lpos += 1
        else:
            merged.append(right[rpos])
            rpos += 1
            rcount += len(left[lpos:])
    
    if left[lpos:]:
        merged.extend(left[lpos:])
    if right[rpos:]:
        merged.extend(right[rpos:])
    
    return merged, rcount
    
if __name__ == '__main__':
    n = int(input())
    A = array.array('L', list(map(int, input().split())))
    
    sorted, count = merge_sort(A)
    #print ' '.join(str(x) for x in sorted)
    print(count)

# Optimized solution
import array

def merge_sort(A):
    if len(A) <= 1:
        return A, 0
    mid = len(A) // 2
    left, lcount = merge_sort(A[:mid])
    right, rcount = merge_sort(A[mid:])
    merged, count = merge(left, right)
    return merged, lcount + rcount + count

def merge(left, right):
    lpos = 0
    rpos = 0
    rcount = 0
    merged = array.array('i', [])  # List is too slow, so use array instead
    len_left = len(left)
    len_right = len(right)
    while lpos < len_left and rpos < len_right:
        if left[lpos] < right[rpos]:
            merged.append(left[lpos])
            lpos += 1
        else:
            merged.append(right[rpos])
            rpos += 1
            rcount += len(left[lpos:])
    
    if left[lpos:]:
        merged.extend(left[lpos:])
    if right[rpos:]:
        merged.extend(right[rpos:])
    
    return merged, rcount
    
if __name__ == '__main__':
    n = int(input())
    A = array.array('i', list(map(int, input().split())))
    
    sorted, count = merge_sort(A)
    #print ' '.join(str(x) for x in sorted)
    print(count)
# Original solution
import array

def merge_sort(A):
    if len(A) <= 1:
        return A, 0
    mid = len(A) // 2
    left, lcount = merge_sort(A[:mid])
    right, rcount = merge_sort(A[mid:])
    merged, count = merge(left, right)
    return merged, lcount + rcount + count

def merge(left, right):
    lpos = 0
    rpos = 0
    rcount = 0
    merged = array.array('I', [])  # List is too slow, so use array instead
    len_left = len(left)
    len_right = len(right)
    while lpos < len_left and rpos < len_right:
        if left[lpos] < right[rpos]:
            merged.append(left[lpos])
            lpos += 1
        else:
            merged.append(right[rpos])
            rpos += 1
            rcount += len(left[lpos:])
    
    if left[lpos:]:
        merged.extend(left[lpos:])
    if right[rpos:]:
        merged.extend(right[rpos:])
    
    return merged, rcount
    
if __name__ == '__main__':
    n = int(input())
    A = array.array('I', list(map(int, input().split())))
    
    sorted, count = merge_sort(A)
    #print ' '.join(str(x) for x in sorted)
    print(count)

# Optimized solution
from array import array

def merge_sort(A):
    if len(A) <= 1:
        return A, 0
    mid = len(A) // 2
    left, lcount = merge_sort(A[:mid])
    right, rcount = merge_sort(A[mid:])
    merged, count = merge(left, right)
    return merged, lcount + rcount + count

def merge(left, right):
    lpos = 0
    rpos = 0
    rcount = 0
    merged = array('I', [])  # List is too slow. Use an array instead.
    len_left = len(left)
    len_right = len(right)
    while lpos < len_left and rpos < len_right:
        if left[lpos] < right[rpos]:
            merged.append(left[lpos])
            lpos += 1
        else:
            merged.append(right[rpos])
            rpos += 1
            #rcount += len(left[lpos:])
            rcount += len_left - lpos
    
    if left[lpos:]:
        merged.extend(left[lpos:])
    if right[rpos:]:
        merged.extend(right[rpos:])
    
    return merged, rcount
    
if __name__ == '__main__':
    n = int(input())
    A = array('I', list(map(int, input().split())))
    
    sorted, count = merge_sort(A)
    #print ' '.join(str(x) for x in sorted)
    print(count)
# Original solution
n = int(eval(input()))
nd = list(map(int, input().split()))
c = 0
for i in range(n):
    m = min(nd)
    index = nd.index(m)
    nd.remove(m)
    c += index
print(c)


# Optimized solution
n = int(eval(input()))
nlist = list(map(int, input().split()))
W = [0for i in range(n)]
def M(l,r):      ##l~r-1 까지의 리스트.
    if (l+1 >= r):
        return 0
    elif (l+2 == r):
        if (nlist[l] <= nlist[l+1]):
            return 0
        else:
            nlist[l],nlist[l+1] = nlist[l+1],nlist[l]
            return 1
    ##위의 if문으로 (r-l)가 2이하일 경우는 해놓음
    m = (l+r)//2
    i = l
    j = m
    k = l
    cl = M(l,m)
    cr = M(m,r)
    c = 0
    while (i<m and j<r):
        if (nlist[i] <= nlist[j]):
            W[k] = nlist[i] ##l부터 r까지 W에 크기순으로 대입해나감
            k += 1
            i += 1
        else:
            W[k] = nlist[j]  ##일단 W[k]에는 더 작은쪽을 저장하는듯
            k += 1
            j += 1
            c += (m-i)  ##만약 nlist[i] > nlist[j]라면 i~m-1 까지 반전 하고있기 때문
                        ##그러고 더 작은 nlist[j]를 안에 넣는것.
    while (i<m):   ##i번째쪽에 더 큰것들이 남았을 경우 (j=r 이 되있는 상황이기에 더 반전수를 늘리지는 않음)
        W[k] = nlist[i]
        k += 1
        i += 1
    while (j<r):   ##j쪽에 큰게 남아있는경우도 동일
        W[k] = nlist[j]
        k += 1
        j += 1
    for i in range(l,r): 
        nlist[i] = W[i]
    return cl+cr+c
        
print((M(0,n)))
# Original solution
INF = float("inf")
cnt = 0
def merge(A, left, mid, right):
    global cnt
    L = A[left: mid] + [INF]
    R = A[mid: right] + [INF]
    i = j = 0
    limit = mid - left
    for k in range(left, right):
        if L[i] <= R[j]:
            A[k] = L[i]
            i += 1
            if i >= limit:
                A[k + 1: right] = R[j: -1]
                break
        else:
            cnt += limit - i
            A[k] = R[j]
            j += 1
def mergeSort(A, left, right):
    if left + 1 < right:
        mid = (left + right) // 2
        mergeSort(A, left, mid)
        mergeSort(A, mid, right)
        merge(A, left, mid, right)
n = int(eval(input()))
S = list(map(int, input().split()))
mergeSort(S, 0, n)
print(cnt)



# Optimized solution
INF = float("inf")
cnt = 0
def merge(A, left, mid, right):
    global cnt
    L = A[left: mid] + [INF]
    R = A[mid: right] + [INF]
    i = j = 0
    limit = mid - left
    for k in range(left, right):
        if L[i] <= R[j]:
            A[k] = L[i]
            i += 1
        else:
            cnt += limit - i
            A[k] = R[j]
            j += 1
def mergeSort(A, left, right):
    if left + 1 < right:
        mid = (left + right) // 2
        mergeSort(A, left, mid)
        mergeSort(A, mid, right)
        merge(A, left, mid, right)
n = int(eval(input()))
S = list(map(int, input().split()))
mergeSort(S, 0, n)
print(cnt)