#include<iostream>#include<cstring>#include<algorithm>usingnamespace std;constint N =100010;typedeflonglong LL;int w[N], s[N];intmain(){int n;scanf("%d",&n);for(int i =1; i <= n; i ++)scanf("%d",&w[i]);int m;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 s1 =0, s2 =0;for(int i =1; i <=n ; i++)
s1 +=(LL)w[i]* s[i];sort(w +1, w +1+ n);sort(s +1, s +1+ n);for(int i =1; i <= n; i ++)
s2 +=(LL)s[i]* w[i];printf("%lld", s2 - s1);}