![](https://img-blog.csdnimg.cn/img_convert/c9da323b58a468ea231a84912704ccd5.png)
![](https://img-blog.csdnimg.cn/img_convert/57fa7d1545d371fcb49c029e9ebf9988.png)
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 100010;
int w[N], s[N];
int n, m;
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i ++) scanf("%d", &w[i]);
scanf("%d", &m);
while (m --)
{
int l, r;
scanf("%d%d", &l, &r);
s[l] ++, s[r + 1] --;
}
for (int i = 1; i <= n; i ++)
s[i] += s[i - 1];
LL res1 = 0;
for (int i = 1; i <= n; i ++)
res1 += (LL)w[i] * s[i];
sort(w + 1, w + n + 1);
sort(s + 1, s + n + 1);
LL res2 = 0;
for (int i = 1; i <= n; i ++)
res2 += (LL)w[i] * s[i];
printf("%lld\n", res2 - res1);
return 0;
}