代码如下
def mer(a,l):
global ans
if l<=1:
return a
mid=l//2
ll=l-mid
left=mer(a[:mid],mid)
right=mer(a[mid:],ll)
p,q,tel=0,0,list()
k=mid-p
while mid>p and ll>q:
if left[p]>right[q]:
tel.append(right[q])
ans+=(mid-p)
q+=1
else:
tel.append(left[p])
p+=1
tel.extend(right[q:])
tel.extend(left[p:])
return tel
n=int(input())
a=list(map(int,input().split()))
ans=0
mer(a,n)
print(ans)