重新排序
差分:
s,d = [0]*100010,[0]*100010
tmp = 0
n = int(input())
a = list(map(int,input().split()))
a.insert(0,0)
for i in range(1,n+1):
s[i] = s[i-1] + a[i]
m = int(input())
for _ in range(m):
l,r = map(int,input().split())
# [l,r]的和
tmp += s[r] - s[l-1]
# 差分
d[l] += 1
d[r+1] -= 1
for i in range(1,n+1): d[i] += d[i-1] # 求每个位置出现的次数
a[1:] = sorted(a[1:],reverse=True) # 原数组排序
d[1:] = sorted(d[1:],reverse=True) # 差分数组排序
res = 0
for i in range(1,n+1):
res += a[i]*d[i]
print(res-tmp)