#include<algorithm>
#include<iostream>
#include<sstream>
#include<cstring>
#include<cstdlib>
#include<climits>
#include<fstream>
#include<cctype>
#include<cstdio>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<map>
#include<set>
using namespace std;
#define pb push_back
#define mp make_pair
#define Y second
#define X first
#define fi freopen("input.txt","r",stdin)
#define fo freopen("output.txt","w",stdout)
const double pi = acos(-1.0);
const double eps = 1e-8;
template <class T>
inline void read(T &i)
{
bool minus=false;
char c;
for(c=getchar();(c<'0'||c>'9')&&(c!='-');c=getchar());
if(c=='-')
{minus=true;
c='0';}
for(i=0;c>='0'&&c<='9';c=getchar())
i=(i<<3)+(i<<1) + (c-48);
if(minus)
i=(~i)+1;
}
vector <string> parse(string s, string c)
{
int len = c.length(), p = -len, np;
vector <string> ans;
do
{
np = s.find(c, p+len);
ans.push_back(s.substr(p+len, np - p - len));
p = np;
}
while (p != string::npos);
return ans;
}
inline int max2(int a, int b) {
return ((a > b)? a : b);
}
inline int max3(int a, int b, int c) {
return max2(a, max2(b, c));
}
/*Solution code starts here */
#define maxn 300009
#define maxt 2060009
long long int Array[maxn];
int n;
class node
{
public:
int st,ed;
long long add,sum;
node()
{add=0,sum=0;
}
node(int l , int r)
{ st=l;
ed=r;
add=0;sum=0;
}
void merge(node L,node R)
{
if( L.st==-1)
sum=R.sum+R.add*(R.ed-R.st+1);
else if( R.st==-1)
sum= L.sum + L.add*(L.ed-L.st+1);
else
sum= L.sum + L.add*(L.ed-L.st+1) + R.sum + R.add*(R.ed-R.st+1);
}
void split( node& L , node& R )
{
sum+=add*(ed-st+1);
if(ed!=st)
{ L.add+=add;
R.add+=add;
}
add=0;
}//end spilt
};
node ident;
node Tree[maxt];
class SegmentTree
{
public:
void build(int no ,int l , int r)
{
if(r==l)
{ Tree[no].st=Tree[no].ed=r;
Tree[no].sum=Array[r];
Tree[no].add=0;
return ;
}
int lc=2*no;
int rc=2*no+1;
int mid=(l+r)/2;
build(lc,l,mid);
build(rc,mid+1,r);
Tree[no]=node(l,r);
Tree[no].merge( Tree[lc] , Tree[rc] );
}
node query(int no, int l,int r, int i, int j)
{
int lc=2*no;
int rc=2*no+1;
int mid=(l+r)/2;
if( l >= i && r<=j )//
{
if( Tree[no].add > 0 )
Tree[no].split( Tree[lc],Tree[rc] );
return Tree[no];
}
if( j < l || i > r)
return ident;
if( Tree[no].add >0 )
Tree[no].split( Tree[lc] , Tree[rc] );
node a=query(lc,l, mid,i,j);
node b=query(rc,mid+1, r, i,j);
node ans;
ans.merge(a,b);
return ans;
}
void update(int no ,int l , int r , int i , int j ,int val)
{
if( i <=l && j >=r )
{ Tree[no].add+=val;
Tree[no].split( Tree[2*no],Tree[2*no+1] );
return ;
}
if( j < l || i >r )
return ;
int mid=(l+r)/2;
update(2*no , l, mid ,i,j,val);
update(2*no+1,mid+1 ,r ,i,j,val);
Tree[no].merge( Tree[2*no] , Tree[2*no+1] );
}
};
int main()
{
ident=node(-1,-1);
int q;
cin>>n>>q;
vector<long long > in;
long long x,y;
for(int i=0;i<n;i++)
{ cin>>x;
in.pb(x);
}
memset(Array,0 ,sizeof(Array) );
SegmentTree st;
st.build(1,0,n-1);
for(int k=1;k<=q;k++)
{
cin>>x>>y;
st.update(1,0,n-1,x-1,y-1,1);
}
/* else if(op==1)
{ scanf("%d%d",&x,&y);
printf("%lld\n",(st.query(1,0,n-1,x-1,y-1) ).sum );
}
*/
vector<long long > cont;
for(int i=0;i<n;i++)
{ x=(st.query(1,0,n-1,i,i) ).sum;
cont.pb(x);
}
sort( cont.begin() , cont.end() );
sort( in.begin() , in.end() );
long long ans=0;
for(int i=0;i<n;i++)
{ //cout<<in[i]<<" "<<cont[i]<<endl;
ans=ans+(long long)( in[i]*cont[i] );
}
cout<<ans<<endl;
return 0;
}
I2luY2x1ZGU8YWxnb3JpdGhtPgojaW5jbHVkZTxpb3N0cmVhbT4KI2luY2x1ZGU8c3N0cmVhbT4KI2luY2x1ZGU8Y3N0cmluZz4KI2luY2x1ZGU8Y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ICBucCA9IHMuZmluZChjLCBwK2xlbik7CiAgICAgIGFucy5wdXNoX2JhY2socy5zdWJzdHIocCtsZW4sIG5wIC0gcCAtIGxlbikpOwogICAgICBwID0gbnA7CiAgICB9CiAgd2hpbGUgKHAgIT0gc3RyaW5nOjpucG9zKTsKCiAgcmV0dXJuIGFuczsKfQoKaW5saW5lIGludCBtYXgyKGludCBhLCBpbnQgYikgewogICAgcmV0dXJuICgoYSA+IGIpPyBhIDogYik7Cn0KCmlubGluZSBpbnQgbWF4MyhpbnQgYSwgaW50IGIsIGludCBjKSB7CiAgICByZXR1cm4gbWF4MihhLCBtYXgyKGIsIGMpKTsKfQoKLypTb2x1dGlvbiBjb2RlIHN0YXJ0cyBoZXJlICovCiAgIAojZGVmaW5lIG1heG4gMzAwMDA5CiNkZWZpbmUgbWF4dCAyMDYwMDA5Cgpsb25nIGxvbmcgaW50IEFycmF5W21heG5dOwppbnQgbjsKICAgICAKCmNsYXNzIG5vZGUKewogICAgICBwdWJsaWM6CiAgICAgICAgICAgICAKICAgaW50IHN0LGVkOwogICBsb25nIGxvbmcgYWRkLHN1bTsKICAgCiAgIG5vZGUoKQogICAge2FkZD0wLHN1bT0wOwogICAgfQogICAgICAgIAogICBub2RlKGludCBsICwgaW50IHIpCiAgIHsgc3Q9bDsKICAgICBlZD1yOwogICAgIGFkZD0wO3N1bT0wOwogICAgfSAKICAgCiAgIAogIAogICAgdm9pZCBtZXJnZShub2RlIEwsbm9kZSBSKQogICAgeyAKICAgICBpZiggTC5zdD09LTEpCiAgICAgICAgc3VtPVIuc3VtK1IuYWRkKihSLmVkLVIuc3QrMSk7ICAgICAgICAKICAgICBlbHNlIGlmKCBSLnN0PT0tMSkKICAgICAgICBzdW09IEwuc3VtICsgTC5hZGQqKEwuZWQtTC5zdCsxKTsgIAogICAgIGVsc2UKICAgICAgICBzdW09IEwuc3VtICsgTC5hZGQqKEwuZWQtTC5zdCsxKSArIFIuc3VtICsgUi5hZGQqKFIuZWQtUi5zdCsxKTsgIAogICAgICAgICAgICAgIAogICAgfSAKICAgIAogICAgdm9pZCBzcGxpdCggbm9kZSYgTCAsICBub2RlJiBSICkKICAgICB7CiAgICAgICAgc3VtKz1hZGQqKGVkLXN0KzEpOwogICAgICAgIAogICAgICAgIGlmKGVkIT1zdCkKICAgICAgICB7IEwuYWRkKz1hZGQ7CiAgICAgICAgICBSLmFkZCs9YWRkOwogICAgICAgIH0gICAgICAKICAgICAgICAKICAgICAgICAgYWRkPTA7CiAgICB9Ly9lbmQgc3BpbHQgICAgCiAKICAgCn07ICAgIAogICAKbm9kZSBpZGVudDsKbm9kZSBUcmVlW21heHRdOwogIApjbGFzcyBTZWdtZW50VHJlZQp7CiAgICAgIHB1YmxpYzoKICAgICAgICAgIAogICAgCiAgICAgICAgICAKICAgICAgCiAgICAgICAgICAgIHZvaWQgYnVpbGQoaW50ICBubyAsaW50ICBsICwgaW50ICAgcikKICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgaWYocj09bCkKICAgICAgICAgICAgICAgIHsgVHJlZVtub10uc3Q9VHJlZVtub10uZWQ9cjsKICAgICAgICAgICAgICAgICAgVHJlZVtub10uc3VtPUFycmF5W3JdOwogICAgICAgICAgICAgICAgICBUcmVlW25vXS5hZGQ9MDsgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgcmV0dXJuIDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgaW50ICBsYz0yKm5vOwogICAgICAgICAgICAgICAgaW50ICByYz0yKm5vKzE7CiAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgIGludCAgbWlkPShsK3IpLzI7CgogICAgICAgICAgICAgICAgYnVpbGQobGMsbCxtaWQpOwogICAgICAgICAgICAgICAgYnVpbGQocmMsbWlkKzEscik7IAogICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICBUcmVlW25vXT1ub2RlKGwscik7ICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgVHJlZVtub10ubWVyZ2UoIFRyZWVbbGNdICwgVHJlZVtyY10gKTsgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgIAogICAgICAgICAgICAgfSAgCiAgICAgICAgICAgICAKICAgICAgICAgICAgIAogbm9kZSAgcXVlcnkoaW50ICAgbm8sIGludCBsLGludCByLCBpbnQgIGksIGludCBqKQogICAgewogICAgICBpbnQgIGxjPTIqbm87CiAgICAgIGludCByYz0yKm5vKzE7CiAgICAgIGludCBtaWQ9KGwrcikvMjsKCiAgICAgIGlmKCBsID49IGkgJiYgcjw9aiApLy8gCiAgICAgICAgewogICAgICAgICAgaWYoIFRyZWVbbm9dLmFkZCA+IDAgKQogICAgICAgICAgICBUcmVlW25vXS5zcGxpdCggVHJlZVtsY10sVHJlZVtyY10gKTsKICAgICAgICAgICAgCiAgICAgICAgICAgIHJldHVybiBUcmVlW25vXTsKICAgICAgICB9ICAKICAgICAgICAKICAgICAgICBpZiggaiA8IGwgfHwgaSA+IHIpCiAgICAgICAgICByZXR1cm4gaWRlbnQ7CiAgICAgICAgICAKICAgICAgICAgIAogICAgICAgIGlmKCBUcmVlW25vXS5hZGQgPjAgKQogICAgICAgICAgVHJlZVtub10uc3BsaXQoIFRyZWVbbGNdICAsIFRyZWVbcmNdICk7CiAgICAgICAgICAKICAgICAgICBub2RlIGE9cXVlcnkobGMsbCwgICAgbWlkLGksaik7CiAgICAgICAgbm9kZSBiPXF1ZXJ5KHJjLG1pZCsxLCByLCBpLGopOwogICAgICAgIAogICAgICAgICBub2RlIGFuczsKICAgICAgICAgYW5zLm1lcmdlKGEsYik7CiAgICAgICAgIHJldHVybiBhbnM7ICAgIAogICAgCiAgICB9IAogICAgCiAgICB2b2lkIHVwZGF0ZShpbnQgIG5vICxpbnQgbCAsIGludCByICwgaW50IGkgLCBpbnQgaiAsaW50IHZhbCkKICAgICB7CiAgICAgICAgaWYoIGkgPD1sICYmIGogPj1yICkKICAgICAgICAgeyBUcmVlW25vXS5hZGQrPXZhbDsKICAgICAgICAgICBUcmVlW25vXS5zcGxpdCggVHJlZVsyKm5vXSxUcmVlWzIqbm8rMV0gKTsKICAgICAgICAgICByZXR1cm4gOwogICAgICAgICB9CiAgICAgICAgIAogICAgICAgICBpZiggaiA8IGwgfHwgaSA+ciApCiAgICAgICAgICAgcmV0dXJuIDsKICAgICAgICAgICAKICAgICAgICAgIGludCBtaWQ9KGwrcikvMjsKICAgICAgICAgICAKICAgICAgICAgIHVwZGF0ZSgyKm5vICwgbCwgICAgbWlkICxpLGosdmFsKTsKICAgICAgICAgIHVwZGF0ZSgyKm5vKzEsbWlkKzEgLHIgICxpLGosdmFsKTsKICAgICAgICAgIAogICAgICAgICAgVHJlZVtub10ubWVyZ2UoIFRyZWVbMipub10gLCBUcmVlWzIqbm8rMV0gKTsgICAgICAgCiAgICAgICAgICAgIAogICAgICB9ICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgCn07CgoKaW50IG1haW4oKQp7CiBpZGVudD1ub2RlKC0xLC0xKTsgICAKCgppbnQgcTsKICAgIAogY2luPj5uPj5xOwogCiAKIHZlY3Rvcjxsb25nIGxvbmcgPiBpbjsKIGxvbmcgbG9uZyB4LHk7CiAKIGZvcihpbnQgaT0wO2k8bjtpKyspCiAgeyBjaW4+Png7CiAgICBpbi5wYih4KTsKICAgfSAgIAogICAgCiBtZW1zZXQoQXJyYXksMCAsc2l6ZW9mKEFycmF5KSApOwogICAgIAogU2VnbWVudFRyZWUgc3Q7CiAgICAgCiAgc3QuYnVpbGQoMSwwLG4tMSk7CiAgICAgCgogICAgIGZvcihpbnQgaz0xO2s8PXE7aysrKQogICAgICB7ICAgCiAgICAgICAgIGNpbj4+eD4+eTsgICAgICAgICAgIAogICAgICAgIHN0LnVwZGF0ZSgxLDAsbi0xLHgtMSx5LTEsMSk7ICAgICAgICAKICAgICAgfSAgCiAgICAgIAogICAgIAogIC8qICBlbHNlIGlmKG9wPT0xKQogICAgICAgICAgICB7IHNjYW5mKCIlZCVkIiwmeCwmeSk7CiAgICAgICAgICAgICAgcHJpbnRmKCIlbGxkXG4iLChzdC5xdWVyeSgxLDAsbi0xLHgtMSx5LTEpICkuc3VtICk7IAogICAgICAgICAgICAgfSAgCiAgICAgICovCiAgICAgIAogICAgICB2ZWN0b3I8bG9uZyBsb25nID4gY29udDsKICAgICAgCiAgICAgIGZvcihpbnQgaT0wO2k8bjtpKyspCiAgICAgICAgeyAgeD0oc3QucXVlcnkoMSwwLG4tMSxpLGkpICkuc3VtOyAgIAogICAgICAgICAgY29udC5wYih4KTsKICAgICAgICB9IAogICAgICAgIAogICAgICAgIAogICAgICAgIHNvcnQoIGNvbnQuYmVnaW4oKSAsIGNvbnQuZW5kKCkgKTsKICAgICAgICBzb3J0KCBpbi5iZWdpbigpICwgaW4uZW5kKCkgKTsKICAgICAgICAKICAgICAgICBsb25nIGxvbmcgYW5zPTA7CiAgICAgICAgCiAgICAgICAgZm9yKGludCBpPTA7aTxuO2krKykKICAgICAgICAgICB7IC8vY291dDw8aW5baV08PCIgICI8PGNvbnRbaV08PGVuZGw7CiAgICAgICAgICBhbnM9YW5zKyhsb25nIGxvbmcpKCBpbltpXSpjb250W2ldICk7CiAgICAgICAgICB9IAogICAgICAgICAgCiAgICAgICAgICBjb3V0PDxhbnM8PGVuZGw7ICAgICAgICAKICAgICAKICAgICByZXR1cm4gMDsKfSAgICAgCiAgICAgCiAgICAgICAgICAgICAgIAogICAgIAo=