#include<bits/stdc++.h>
using namespace std;
int long pro(int long m,int long n)
{
    int long max=min(m,n),s=0;
    if((m+n)%2!=0)
    {
        int long j=1;
        for(int i=1;i<=(m+n-1)/2;i++)
        {
            s+=j;
            if(j<max)
                j++;
        }
        s=(2*s)+1;
        return s;
    }
    else
    {
        int long j=1;
        for(int i=1;i<(m+n)/2;i++)
        {
            s+=j;
            if(j<max)
                j++;
        }
        s=(2*s)+max+1;
        return s;
    }
}
int main()
{
    int t,i=1;
    cin>>t;
    while(t--)
    {
        int long x1,x2,y1,y2;
        cin>>x1>>y1>>x2>>y2;
        int long m=x2-x1;
        int long n=y2-y1;
        if(m==0||n==0)
        {
            cout<<"1"<<endl;
            continue;
        }
        int long p=pro(m,n);
        cout<<p<<endl;
        i++;
    }
    return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main(void)
{
	long long int n,q,arr[200100],count[200100]={},a,b,ans;
	vector<pair<long long int,long long int> >v;
	cin>>n>>q;
	for(long long int i=0;i<n;i++)
		cin>>arr[i];
		for(long long int i=0;i<q;i++)
		{
			cin>>a>>b;
			v.push_back(make_pair(a,b));
		    count[a-1]++;
			if(b-1<n-1)
			count[b]--;
		}
		sort(arr,arr+n);
		for(long long int i=1;i<n;i++)
           count[i]=count[i]+count[i-1];
		sort(count,count+n);
		ans=0;
		for(long long int i=0;i<n;i++)
		ans=ans+(count[i]*arr[i]);
		cout<<ans;
			return 0;
	}


#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
int main(void)
{
	long long int n,q,arr[200],count[200]={},a,b,ans;
	cin>>n>>q;
	for(long long int i=0;i<n;i++)
	    scanf("%I64d",&arr[i]);
		for(long long int i=0;i<q;i++)
		{
			scanf("%I64d %I64d",&a,&b);
		    count[a-1]++;
			if(b-1<n-1)
			count[b]--;
		}
		sort(arr,arr+n);
		for(long long int i=1;i<n;i++)
           count[i]=count[i]+count[i-1];
		sort(count,count+n);
		ans=0;
		for(long long int i=0;i<n;i++)
		ans=ans+(count[i]*arr[i]);
		printf("%I64d",ans);
			return 0;
	}


#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main(void)
{
	long long int n,q,arr[200000],count[200000]={},k[200000],a,b,sum[200000]={},ans;
	vector<pair<long long int,long long int> >v;
	cin>>n>>q;
	for(long long int i=0;i<n;i++)
		cin>>arr[i];
		for(long long int i=0;i<q;i++)
		{
			cin>>a>>b;
			v.push_back(make_pair(a,b));
			if(a!=1||b!=n)
			{for(long long int j=a-1;j<b;j++)
			count[j]++
		}
		}
		sort(arr,arr+n);
		long long int m=*max_element(arr,arr+n);
		for(long long int i=n-1;i>=0;i--)
		{
			long long int z=distance(count,max_element(count,count+m));
			k[z]=arr[i];
			count[z]=-1;
		}
		sum[0]=k[0];
		for(long long int i=1;i<n;i++)
			sum[i]=sum[i-1]+k[i];
			ans=0;
			for(long long int i=0;i<q;i++)
			{
				a=v.at(i).first;
				b=v.at(i).second;
				if(a==1)
				ans=ans+sum[b-1];
				else
				ans=ans+sum[b-1]-sum[a-2];
			}
			cout<<ans;
			return 0;
	}


#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main(void)
{
	long long int n,q,arr[200],count[200]={},a,b,ans;
	vector<pair<long long int,long long int> >v;
	cin>>n>>q;
	for(long long int i=0;i<n;i++)
		cin>>arr[i];
		for(long long int i=0;i<q;i++)
		{
			cin>>a>>b;
			v.push_back(make_pair(a,b));
		    count[a-1]++;
			if(b-1<n-1)
			count[b]--;
		}
		sort(arr,arr+n);
		for(long long int i=1;i<n;i++)
           count[i]=count[i]+count[i-1];
		sort(count,count+n);
		ans=0;
		for(long long int i=0;i<n;i++)
		ans=ans+(count[i]*arr[i]);
		cout<<ans;
			return 0;
	}


#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main(void)
{
	long long int n,q,arr[200000],count[200000]={},a,b,ans;
	vector<pair<long long int,long long int> >v;
	cin>>n>>q;
	for(long long int i=0;i<n;i++)
		cin>>arr[i];
		for(long long int i=0;i<q;i++)
		{
			cin>>a>>b;
			v.push_back(make_pair(a,b));
		    count[a-1]++;
			if(b-1<n-1)
			count[b]--;
		}
		sort(arr,arr+n);
		for(long long int i=1;i<n;i++)
           count[i]=count[i]+count[i-1];
		sort(count,count+n);
		ans=0;
		for(long long int i=0;i<n;i++)
		ans=ans+(count[i]*arr[i]);
		cout<<ans;
			return 0;
	}


#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main(void)
{
	long long int n,q,arr[200000],count[200000]={},k[200000],a,b,sum[200000]={},ans;
	vector<pair<long long int,long long int> >v;
	cin>>n>>q;
	for(long long int i=0;i<n;i++)
		cin>>arr[i];
		for(long long int i=0;i<q;i++)
		{
			cin>>a>>b;
			v.push_back(make_pair(a,b));
			if(a!=1||b!=n)
			{count[a-1]++;
			count[b-1]++;
		}
		}
		sort(arr,arr+n);
		long long int m=*max_element(arr,arr+n);
		for(long long int i=n-1;i>=0;i--)
		{
			long long int z=distance(count,max_element(count,count+m));
			k[z]=arr[i];
			count[z]=-1;
		}
		sum[0]=k[0];
		for(long long int i=1;i<n;i++)
			sum[i]=sum[i-1]+k[i];
			ans=0;
			for(long long int i=0;i<q;i++)
			{
				a=v.at(i).first;
				b=v.at(i).second;
				if(a==1)
				ans=ans+sum[b-1];
				else
				ans=ans+sum[b-1]-sum[a-2];
			}
			cout<<ans;
			return 0;
	}


#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
int main(void)
{
	long long int n,q,arr[200100],count[200100]={},a,b,ans;
	cin>>n>>q;
	for(long long int i=0;i<n;i++)
	    scanf("%I64d",&arr[i]);
		for(long long int i=0;i<q;i++)
		{
			scanf("%I64d %I64d",&a,&b);
		    count[a-1]++;
			if(b-1<n-1)
			count[b]--;
		}
		sort(arr,arr+n);
		for(long long int i=1;i<n;i++)
           count[i]=count[i]+count[i-1];
		sort(count,count+n);
		ans=0;
		for(long long int i=0;i<n;i++)
		ans=ans+(count[i]*arr[i]);
		printf("%I64d",ans);
			return 0;
	}


#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main(void)
{
	long long int n,q,arr[200000],count[200000]={},k[200000],a,b,sum[200000]={},ans;
	vector<pair<long long int,long long int> >v;
	cin>>n>>q;
	for(long long int i=0;i<n;i++)
		cin>>arr[i];
		for(long long int i=0;i<q;i++)
		{
			cin>>a>>b;
			v.push_back(make_pair(a,b));
			if(a!=1||b!=n)
			{for(long long int j=a-1;j<b;j++)
			count[j]++;
		}
		}
		sort(arr,arr+n);
		long long int m=*max_element(arr,arr+n);
		for(long long int i=n-1;i>=0;i--)
		{
			long long int z=distance(count,max_element(count,count+m));
			k[z]=arr[i];
			count[z]=-1;
		}
		sum[0]=k[0];
		for(long long int i=1;i<n;i++)
			sum[i]=sum[i-1]+k[i];
			ans=0;
			for(long long int i=0;i<q;i++)
			{
				a=v.at(i).first;
				b=v.at(i).second;
				if(a==1)
				ans=ans+sum[b-1];
				else
				ans=ans+sum[b-1]-sum[a-2];
			}
			cout<<ans;
			return 0;
	}


#include<iostream>
#include<algorithm>
using namespace std;
int main(void)
{
	long long int n,q,arr[200100],count[200100]={},a,b,ans;
	cin>>n>>q;
	for(long long int i=0;i<n;i++)
		cin>>arr[i];
		for(long long int i=0;i<q;i++)
		{
			cin>>a>>b;
		    count[a-1]++;
			if(b-1<n-1)
			count[b]--;
		}
		sort(arr,arr+n);
		for(long long int i=1;i<n;i++)
           count[i]=count[i]+count[i-1];
		sort(count,count+n);
		ans=0;
		for(long long int i=0;i<n;i++)
		ans=ans+(count[i]*arr[i]);
		cout<<ans;
			return 0;
	}


#include <bits/stdc++.h>
using namespace std;

#define fo(i,n) for(int i=0; i<n; i++)
#define zero(a) memset(a, sizeof(a), 0)
#define all(a) a.begin(), a.end()
#define sz(a) a.size()
#define db(x) cout << #x << ": " << x << endl
#define pb push_back
#define mp make_pair

typedef long long ll;

int a[200001], c[200001], b[1<<18+1];

int put(int l, int r, int tl, int tr, int p) {
    if(l==tl && r==tr) b[p]++;
    else if (l<=(tl+tr)/2) {
        if (r>(tl+tr)/2) {
            put(l,(tl+tr)/2,tl,(tl+tr)/2,2*p);
            put((tl+tr)/2+1,r,(tl+tr)/2+1,tr,2*p+1);
        } else {
            put(l,r,tl,(tl+tr)/2,2*p);
        }
    } else {
        put(l,r,(tl+tr)/2+1,tr,2*p+1);
    }
}

int get(int v, int p, int tl, int tr) {
    if (tl==tr) {
        return b[p];
    }
    if(v<=(tl+tr)/2) {
        return b[p]+get(v,p*2,tl,(tl+tr)/2);
    }
    return b[p]+get(v,p*2+1,(tl+tr)/2+1,tr);
}

int main() {
    int n, q; cin >> n >> q;;
    fo(i,n) cin >> a[i];
    sort(a, a+n);
    fo(i,q) {
        int l, r; cin >> l >> r;
        put(l,r,1,1<<18,1);
    }
    fo(i,n) {
        c[i]=get(i+1,1,1,1<<18);
    }
    sort(c, c+n);
    ll r = 0;
    fo(i,n) r += (ll)c[i]*(ll)a[i];
    cout << r;
    return 1;
}


#include <bits/stdc++.h>
using namespace std;

#define fo(i,n) for(int i=0; i<n; i++)
#define zero(a) memset(a, sizeof(a), 0)
#define all(a) a.begin(), a.end()
#define sz(a) a.size()
#define db(x) cout << #x << ": " << x << endl
#define pb push_back
#define mp make_pair

typedef long long ll;

int a[200001], b[200001];
vector<int> lll, rr;

int main() {
    int n, q; cin >> n >> q;;
    fo(i,n) cin >> a[i];
    sort(a, a+n);
    fo(i,q) {
        int l, r; cin >> l >> r;
        lll.pb(l);
        rr.pb(r);
    }
    sort(all(lll));
    sort(all(rr));
    int i = 0, j = 0, l = 0;
    fo(k,n) {
        while(lll[i]==k+1) i++, l++;
        b[k] = l;
        while(rr[j]==k+1) j++, l--;
    }
    sort(b, b+n);
    ll r = 0;
    fo(i,n) r += (ll)b[i]*(ll)a[i];
    cout << r;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define fo(i,n) for(int i=0; i<n; i++)
#define zero(a) memset(a, sizeof(a), 0)
#define all(a) a.begin(), a.end()
#define sz(a) a.size()
#define db(x) cout << #x << ": " << x << endl
#define pb push_back
#define mp make_pair

typedef long long ll;

int a[200001], b[200001], c[200001];

int main() {
    int n, q; scanf("%d%d",&n,&q);;
    fo(i,n) scanf("%d",a+i);
    sort(a, a+n);
    fo(i,q) {
        int l, r; cin >> l >> r;
        b[l-1]++;
        if (r<n) b[r]--;
    }
    int v = 0;
    fo(i,n) {
        v+=b[i];
        c[i]=v;
    }
    sort(c, c+n);
    ll r = 0;
    fo(i,n) r += (ll)c[i]*(ll)a[i];
    cout << r;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define fo(i,n) for(int i=0; i<n; i++)
#define zero(a) memset(a, sizeof(a), 0)
#define all(a) a.begin(), a.end()
#define sz(a) a.size()
#define db(x) cout << #x << ": " << x << endl
#define pb push_back
#define mp make_pair

typedef long long ll;

int a[200001], c[200001], b[1<<19+1];

int put(int l, int r, int tl, int tr, int p) {
    if(l==tl && r==tr) b[p]++;
    else if (l<=(tl+tr)/2) {
        if (r>(tl+tr)/2) {
            put(l,(tl+tr)/2,tl,(tl+tr)/2,2*p);
            put((tl+tr)/2+1,r,(tl+tr)/2+1,tr,2*p+1);
        } else {
            put(l,r,tl,(tl+tr)/2,2*p);
        }
    } else {
        put(l,r,(tl+tr)/2+1,tr,2*p+1);
    }
}

int get(int v, int p, int tl, int tr) {
    if (tl==tr) {
        return b[p];
    }
    if(v<=(tl+tr)/2) {
        return b[p]+get(v,p*2,tl,(tl+tr)/2);
    }
    return b[p]+get(v,p*2+1,(tl+tr)/2+1,tr);
}

int main() {
    int n, q; scanf("%d%d",&n,&q);
    fo(i,n) scanf("%d",a+i);
    sort(a, a+n);
    fo(i,q) {
        int l, r; cin >> l >> r;
        put(l,r,1,1<<18,1);
    }
    fo(i,n) {
        c[i]=get(i+1,1,1,1<<18);
    }
    sort(c, c+n);
    ll r = 0;
    fo(i,n) r += (ll)c[i]*(ll)a[i];
    cout << r;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define fo(i,n) for(int i=0; i<n; i++)
#define zero(a) memset(a, sizeof(a), 0)
#define all(a) a.begin(), a.end()
#define sz(a) a.size()
#define db(x) cout << #x << ": " << x << endl
#define pb push_back
#define mp make_pair

typedef long long ll;

int a[200001], c[200001], b[1<<19+1];

int put(int l, int r, int tl, int tr, int p) {
    if(l==tl && r==tr) b[p]++;
    else if (l<=(tl+tr)/2) {
        if (r>(tl+tr)/2) {
            put(l,(tl+tr)/2,tl,(tl+tr)/2,2*p);
            put((tl+tr)/2+1,r,(tl+tr)/2+1,tr,2*p+1);
        } else {
            put(l,r,tl,(tl+tr)/2,2*p);
        }
    } else {
        put(l,r,(tl+tr)/2+1,tr,2*p+1);
    }
}

int get(int v, int p, int tl, int tr) {
    if (tl==tr) {
        return b[p];
    }
    if(v<=(tl+tr)/2) {
        return b[p]+get(v,p*2,tl,(tl+tr)/2);
    }
    return b[p]+get(v,p*2+1,(tl+tr)/2+1,tr);
}

int main() {
    int n, q; cin >> n >> q;;
    fo(i,n) cin >> a[i];
    sort(a, a+n);
    fo(i,q) {
        int l, r; cin >> l >> r;
        put(l,r,1,1<<18,1);
    }
    fo(i,n) {
        c[i]=get(i+1,1,1,1<<18);
    }
    sort(c, c+n);
    ll r = 0;
    fo(i,n) r += (ll)c[i]*(ll)a[i];
    cout << r;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define fo(i,n) for(int i=0; i<n; i++)
#define zero(a) memset(a, sizeof(a), 0)
#define all(a) a.begin(), a.end()
#define sz(a) a.size()
#define db(x) cout << #x << ": " << x << endl
#define pb push_back
#define mp make_pair

typedef long long ll;

int a[200001], b[200001];
vector<int> lll, rr;

int main() {
    int n, q; cin >> n >> q;;
    fo(i,n) cin >> a[i];
    sort(a, a+n);
    fo(i,q) {
        int l, r; cin >> l >> r;
        lll.pb(l);
        rr.pb(r);
    }
    sort(all(lll));
    sort(all(rr));
    int i = 0, j = 0, l = 0;
    fo(k,n) {
        while(lll[i]==k+1) i++, l++;
        b[k] = l;
        while(rr[j]==k+1) j++, l--;
    }
    sort(b, b+n);
    ll r = 0;
    fo(i,n) r+=b[i]*a[i];
    cout << r;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define fo(i,n) for(int i=0; i<n; i++)
#define zero(a) memset(a, sizeof(a), 0)
#define all(a) a.begin(), a.end()
#define sz(a) a.size()
#define db(x) cout << #x << ": " << x << endl
#define pb push_back
#define mp make_pair

typedef long long ll;

int a[200001], b[200001];
vector<int> lll, rr;

int main() {
    int n, q; scanf("%d%d",&n,&q);
    fo(i,n) scanf("%d",a+i);
    sort(a, a+n);
    fo(i,q) {
        int l, r; cin >> l >> r;
        lll.pb(l);
        rr.pb(r);
    }
    sort(all(lll));
    sort(all(rr));
    int i = 0, j = 0, l = 0;
    fo(k,n) {
        while(lll[i]==k+1) i++, l++;
        b[k] = l;
        while(rr[j]==k+1) j++, l--;
    }
    sort(b, b+n);
    ll r = 0;
    fo(i,n) r += (ll)b[i]*(ll)a[i];
    cout << r;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define fo(i,n) for(int i=0; i<n; i++)
#define zero(a) memset(a, sizeof(a), 0)
#define all(a) a.begin(), a.end()
#define sz(a) a.size()
#define db(x) cout << #x << ": " << x << endl
#define pb push_back
#define mp make_pair

typedef long long ll;

int a[200001], b[200001];
vector<int> lll, rr;

int main() {
    int n, q; cin >> n >> q;;
    fo(i,n) cin >> a[i];
    sort(a, a+n);
    fo(i,q) {
        int l, r; cin >> l >> r;
        lll.pb(l);
        rr.pb(r);
    }
    sort(all(lll));
    sort(all(rr));
    int i = 0, j = 0, l = 0;
    fo(k,n) {
        while(lll[i]==k+1 && i<q) i++, l++;
        b[k] = l;
        while(rr[j]==k+1 && j<q) j++, l--;
    }
    sort(b, b+n);
    ll r = 0;
    fo(i,n) r += (ll)b[i]*(ll)a[i];
    cout << r;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define fo(i,n) for(int i=0; i<n; i++)
#define zero(a) memset(a, sizeof(a), 0)
#define all(a) a.begin(), a.end()
#define sz(a) a.size()
#define db(x) cout << #x << ": " << x << endl
#define pb push_back
#define mp make_pair

typedef long long ll;

int a[200001], b[200001], c[200001];

int main() {
    int n, q; cin >> n >> q;;
    fo(i,n) cin >> a[i];
    sort(a, a+n);
    fo(i,q) {
        int l, r; cin >> l >> r;
        b[l-1]++;
        if (r<n) b[r]--;
    }
    int v = 0;
    fo(i,n) {
        v+=b[i];
        c[i]=v;
    }
    sort(c, c+n);
    ll r = 0;
    fo(i,n) r += (ll)c[i]*(ll)a[i];
    cout << r;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define fo(i,n) for(int i=0; i<n; i++)
#define zero(a) memset(a, sizeof(a), 0)
#define all(a) a.begin(), a.end()
#define sz(a) a.size()
#define db(x) cout << #x << ": " << x << endl
#define pb push_back
#define mp make_pair

typedef long long ll;

int a[200001], c[200001], b[1<<18+1];

int put(int l, int r, int tl, int tr, int p) {
    if(l==tl && r==tr) b[p]++;
    else if (l<=(tl+tr)/2) {
        if (r>(tl+tr)/2) {
            put(l,(tl+tr)/2,tl,(tl+tr)/2,2*p);
            put((tl+tr)/2+1,r,(tl+tr)/2+1,tr,2*p+1);
        } else {
            put(l,r,tl,(tl+tr)/2,2*p);
        }
    } else {
        put(l,r,(tl+tr)/2+1,tr,2*p+1);
    }
}

int get(int v, int p, int tl, int tr) {
    if (tl==tr) {
        return b[p];
    }
    if(v<=(tl+tr)/2) {
        return b[p]+get(v,p*2,tl,(tl+tr)/2);
    }
    return b[p]+get(v,p*2+1,(tl+tr)/2+1,tr);
}

int main() {
    int n, q; cin >> n >> q;;
    fo(i,n) cin >> a[i];
    sort(a, a+n);
    fo(i,q) {
        int l, r; cin >> l >> r;
        put(l,r,1,1<<18,1);
    }
    fo(i,n) {
        c[i]=get(i+1,1,1,1<<18);
    }
    sort(c, c+n);
    ll r = 0;
    fo(i,n) r += (ll)c[i]*(ll)a[i];
    cout << r;
    return 0;
}


/*
 * answer.cpp
 *
 *  Created on: Jun 12, 2013
 *      Author: Horace He
 */

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <ctime>
#include <climits>
#include <cstdlib>

using namespace std;
#define sz(x) ((int) x.size())
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;

const double pi = 3.141592653589793;
const double tau = 6.283185307179586;
const double epsilon = 1e-6;

#define Set(a, s) memset(a, s, sizeof (a))
#define Rd(r) freopen(r, "r", stdin)
#define Wt(w) freopen(w, "w", stdout)
#define parr(a,n) for (int i=0;i<(int)(n);i++) cout << a[i] << " "; cout << endl;
#define pvec(a) for (int i=0;i<a.size();i++) cout << a[i] << " "; cout << endl;

bool cmp(int a, int b)
{
    if(a>b)
        return true;
    else
        return false;
}

int main (int argc, char ** argv)
{
int n,q,l,r;
  long long a[200010],t[200010],ans;
   cin>>n>>q;
     for(int i=0;i<n;i++)
         cin>>a[i];
     sort(a,a+n);
     for(int i=0;i<q;i++){
         cin>>l>>r;
         t[l-1]++,t[r]--;
     }
     for(int i=1;i<n;i++)
         t[i]+=t[i-1];
     sort(t,t+n);
     for(int i=0;i<n;i++)
         ans+=a[i]*t[i];
     cout<<ans;
     return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <ctime>
#include <climits>
#include <cstdlib>

using namespace std;
#define sz(x) ((int) x.size())
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;

const double pi = 3.141592653589793;
const double tau = 6.283185307179586;
const double epsilon = 1e-6;

#define Set(a, s) memset(a, s, sizeof (a))
#define Rd(r) freopen(r, "r", stdin)
#define Wt(w) freopen(w, "w", stdout)
#define parr(a,n) for (int i=0;i<(int)(n);i++) cout << a[i] << " "; cout << endl;
#define pvec(a) for (int i=0;i<a.size();i++) cout << a[i] << " "; cout << endl;

int main (int argc, char ** argv)
{

    return 0;
}


/*
 * answer.cpp
 *
 *  Created on: Jun 12, 2013
 *      Author: Horace He
 */

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <ctime>
#include <climits>
#include <cstdlib>

using namespace std;
#define sz(x) ((int) x.size())
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;

const double pi = 3.141592653589793;
const double tau = 6.283185307179586;
const double epsilon = 1e-6;

#define Set(a, s) memset(a, s, sizeof (a))
#define Rd(r) freopen(r, "r", stdin)
#define Wt(w) freopen(w, "w", stdout)
#define parr(a,n) for (int i=0;i<(int)(n);i++) cout << a[i] << " "; cout << endl;
#define pvec(a) for (int i=0;i<a.size();i++) cout << a[i] << " "; cout << endl;

bool cmp(int a, int b)
{
    if(a>b)
        return true;
    else
        return false;
}

int main (int argc, char ** argv)
{
    ll max[200005]={0};
    int front[20005];
    int back[20005];
    int arr[200005];
    int n, q;
    ll ans=0;
    //Rd("input.in");
    scanf("%d %d",&n, &q);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&arr[i]);
    }
    //int a, b;
    for(int i=0;i<q;i++)
    {
        scanf("%d %d",&front[i],&back[i]);
        //front[i]--;
        //back[i]--;
        max[front[i]-1]++;
        max[back[i]]--;
    }
    ll over=0;
    //sort(front,front+q);
    //sort(back,back+q);
    ll fpos=0;
    ll bpos=0;
    /*for(int i=0;i<n;i++)
    {
        //cout<<over<<endl;
        while(front[fpos]==i)
        {
            over++;
            fpos++;
        }
        max[i]=over;
        while(back[bpos]==i)
        {
            over--;
            bpos++;
        }
    }*/
    for(int i=1;i<n;i++)
        max[i]=max[i-1];
    sort(max,max+n);
    sort(arr,arr+n);
    //parr(max,n);
    //parr(arr,n);
    for(int i=0;i<n;i++)
    {
        ans+=max[i]*arr[i];
        //cout<<max[i]<<' '<<arr[i]<<ans<<endl;
    }
    cout<<ans<<endl;
    return 0;
}


/*
 * answer.cpp
 *
 *  Created on: Jun 12, 2013
 *      Author: Horace He
 */

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <ctime>
#include <climits>
#include <cstdlib>

using namespace std;
#define sz(x) ((int) x.size())
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;

const double pi = 3.141592653589793;
const double tau = 6.283185307179586;
const double epsilon = 1e-6;

#define Set(a, s) memset(a, s, sizeof (a))
#define Rd(r) freopen(r, "r", stdin)
#define Wt(w) freopen(w, "w", stdout)
#define parr(a,n) for (int i=0;i<(int)(n);i++) cout << a[i] << " "; cout << endl;
#define pvec(a) for (int i=0;i<a.size();i++) cout << a[i] << " "; cout << endl;

bool cmp(int a, int b)
{
    if(a>b)
        return true;
    else
        return false;
}

int main (int argc, char ** argv)
{
    ll max[200005]={0};
    int front[20005];
    int back[20005];
    int arr[200005];
    int n, q;
    ll ans=0;
    //Rd("input.in");
    scanf("%d %d",&n, &q);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&arr[i]);
    }
    //int a, b;
    for(int i=0;i<q;i++)
    {
        scanf("%d %d",&front[i],&back[i]);
        //front[i]--;
        //back[i]--;
        max[front[i]-1]++;
        max[back[i]]--;
    }
    //parr(max,n+1);
    ll over=0;
    //sort(front,front+q);
    //sort(back,back+q);
    ll fpos=0;
    ll bpos=0;
    /*for(int i=0;i<n;i++)
    {
        //cout<<over<<endl;
        while(front[fpos]==i)
        {
            over++;
            fpos++;
        }
        max[i]=over;
        while(back[bpos]==i)
        {
            over--;
            bpos++;
        }
    }*/
    for(int i=1;i<n;i++)
        max[i]+=max[i-1];
    parr(max,n+1);
    sort(max,max+n);
    sort(arr,arr+n);
    //parr(max,n);
    //parr(arr,n);
    for(int i=0;i<n;i++)
    {
        ans+=max[i]*arr[i];
        //cout<<max[i]<<' '<<arr[i]<<ans<<endl;
    }
    cout<<ans<<endl;
    return 0;
}


/*
 * answer.cpp
 *
 *  Created on: Jun 12, 2013
 *      Author: Horace He
 */

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <ctime>
#include <climits>
#include <cstdlib>

using namespace std;
#define sz(x) ((int) x.size())
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;

const double pi = 3.141592653589793;
const double tau = 6.283185307179586;
const double epsilon = 1e-6;

#define Set(a, s) memset(a, s, sizeof (a))
#define Rd(r) freopen(r, "r", stdin)
#define Wt(w) freopen(w, "w", stdout)
#define parr(a,n) for (int i=0;i<(int)(n);i++) cout << a[i] << " "; cout << endl;
#define pvec(a) for (int i=0;i<a.size();i++) cout << a[i] << " "; cout << endl;

bool cmp(int a, int b)
{
    if(a>b)
        return true;
    else
        return false;
}

int main (int argc, char ** argv)
{
    ll max[200005]={0};
    ll front[20005];
    ll back[20005];
    ll arr[200005];
    ll n, q;
    ll ans=0;
    //Rd("input.in");
    scanf("%d %d",&n, &q);
    for(llint i=0;i<n;i++)
    {
        scanf("%I64",&arr[i]);
    }
    //int a, b;
    for(int i=0;i<q;i++)
    {
        scanf("%I64 %I64",&front[i],&back[i]);
        front[i]--;
        back[i]--;
    }
    sort(front,front+q);
    sort(back,back+q);
    ll over=0;
    ll fpos=0;
    ll bpos=0;
    for(ll i=0;i<n;i++)
    {
        while(front[fpos]==i)
        {
            if(front[fpos]==i)
            {
                over++;
                fpos++;
            }
        }
        max[i]=over;
        while(back[bpos]==i)
        {
            if(back[bpos]==i)
            {
                over--;
                bpos++;
            }
        }
    }
    sort(max,max+n,cmp);
    sort(arr,arr+n,cmp);
    //parr(max,n);
    //parr(arr,n);
    for(ll i=0;i<n;i++)
    {
        ans+=max[i]*arr[i];
        //cout<<max[i]<<' '<<arr[i]<<ans<<endl;
    }
    cout<<ans<<endl;
    return 0;
}


/*
 * answer.cpp
 *
 *  Created on: Jun 12, 2013
 *      Author: Horace He
 */

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <ctime>
#include <climits>
#include <cstdlib>

using namespace std;
#define sz(x) ((int) x.size())
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;

const double pi = 3.141592653589793;
const double tau = 6.283185307179586;
const double epsilon = 1e-6;

#define Set(a, s) memset(a, s, sizeof (a))
#define Rd(r) freopen(r, "r", stdin)
#define Wt(w) freopen(w, "w", stdout)
#define parr(a,n) for (int i=0;i<(int)(n);i++) cout << a[i] << " "; cout << endl;
#define pvec(a) for (int i=0;i<a.size();i++) cout << a[i] << " "; cout << endl;

bool cmp(int a, int b)
{
    if(a>b)
        return true;
    else
        return false;
}

int main (int argc, char ** argv)
{
    ll max[200005]={0};
    ll front[20005];
    ll back[20005];
    ll arr[200005];
    ll n, q;
    ll ans=0;
    //Rd("input.in");
    scanf("%I64d %I64d",&n, &q);
    for(ll i=0;i<n;i++)
    {
        scanf("%I64d",&arr[i]);
    }
    //int a, b;
    for(int i=0;i<q;i++)
    {
        scanf("%I64d %I64d",&front[i],&back[i]);
        front[i]--;
        back[i]--;
    }
    sort(front,front+q);
    sort(back,back+q);
    ll over=0;
    ll fpos=0;
    ll bpos=0;
    for(ll i=0;i<n;i++)
    {
        while(front[fpos]==i)
        {
            if(front[fpos]==i)
            {
                over++;
                fpos++;
            }
        }
        max[i]=over;
        while(back[bpos]==i)
        {
            if(back[bpos]==i)
            {
                over--;
                bpos++;
            }
        }
    }
    sort(max,max+n,cmp);
    sort(arr,arr+n,cmp);
    //parr(max,n);
    //parr(arr,n);
    for(ll i=0;i<n;i++)
    {
        ans+=max[i]*arr[i];
        //cout<<max[i]<<' '<<arr[i]<<ans<<endl;
    }
    cout<<ans<<endl;
    return 0;
}


/*
 * answer.cpp
 *
 *  Created on: Jun 12, 2013
 *      Author: Horace He
 */

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <ctime>
#include <climits>
#include <cstdlib>

using namespace std;
#define sz(x) ((int) x.size())
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;

const double pi = 3.141592653589793;
const double tau = 6.283185307179586;
const double epsilon = 1e-6;

#define Set(a, s) memset(a, s, sizeof (a))
#define Rd(r) freopen(r, "r", stdin)
#define Wt(w) freopen(w, "w", stdout)
#define parr(a,n) for (int i=0;i<(int)(n);i++) cout << a[i] << " "; cout << endl;
#define pvec(a) for (int i=0;i<a.size();i++) cout << a[i] << " "; cout << endl;

bool cmp(int a, int b)
{
    if(a>b)
        return true;
    else
        return false;
}

int main (int argc, char ** argv)
{
    ll max[200005]={0};
    int front[20005];
    int back[20005];
    int arr[200005];
    int n, q;
    ll ans=0;
    //Rd("input.in");
    scanf("%d %d",&n, &q);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&arr[i]);
    }
    //int a, b;
    for(int i=0;i<q;i++)
    {
        scanf("%d %d",&front[i],&back[i]);
        //front[i]--;
        //back[i]--;
        max[front[i]-1]++;
        max[back[i]]--;
    }
    parr(max,n);
    ll over=0;
    //sort(front,front+q);
    //sort(back,back+q);
    ll fpos=0;
    ll bpos=0;
    /*for(int i=0;i<n;i++)
    {
        //cout<<over<<endl;
        while(front[fpos]==i)
        {
            over++;
            fpos++;
        }
        max[i]=over;
        while(back[bpos]==i)
        {
            over--;
            bpos++;
        }
    }*/
    for(int i=1;i<n;i++)
        max[i]=max[i-1];
    sort(max,max+n);
    sort(arr,arr+n);
    //parr(max,n);
    //parr(arr,n);
    for(int i=0;i<n;i++)
    {
        ans+=max[i]*arr[i];
        //cout<<max[i]<<' '<<arr[i]<<ans<<endl;
    }
    cout<<ans<<endl;
    return 0;
}


/*
 * answer.cpp
 *
 *  Created on: Jun 12, 2013
 *      Author: Horace He
 */

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <ctime>
#include <climits>
#include <cstdlib>

using namespace std;
#define sz(x) ((int) x.size())
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;

const double pi = 3.141592653589793;
const double tau = 6.283185307179586;
const double epsilon = 1e-6;

#define Set(a, s) memset(a, s, sizeof (a))
#define Rd(r) freopen(r, "r", stdin)
#define Wt(w) freopen(w, "w", stdout)
#define parr(a,n) for (int i=0;i<(int)(n);i++) cout << a[i] << " "; cout << endl;
#define pvec(a) for (int i=0;i<a.size();i++) cout << a[i] << " "; cout << endl;

bool cmp(int a, int b)
{
    if(a>b)
        return true;
    else
        return false;
}

int main (int argc, char ** argv)
{
    ll max[200005]={0};
    int front[20005];
    int back[20005];
    int arr[200005];
    int n, q;
    ll ans=0;
    //Rd("input.in");
    scanf("%d %d",&n, &q);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&arr[i]);
    }
    //int a, b;
    for(int i=0;i<q;i++)
    {
        scanf("%d %d",&front[i],&back[i]);
        //front[i]--;
        //back[i]--;
        max[front[i]-1]++;
        max[back[i]]--;
    }
    ll over=0;
    //sort(front,front+q);
    //sort(back,back+q);
    ll fpos=0;
    ll bpos=0;
    /*for(int i=0;i<n;i++)
    {
        //cout<<over<<endl;
        while(front[fpos]==i)
        {
            over++;
            fpos++;
        }
        max[i]=over;
        while(back[bpos]==i)
        {
            over--;
            bpos++;
        }
    }*/
    for(int i=1;i<n;i++
        max[i]=max[i-1];
    sort(max,max+n);
    sort(arr,arr+n);
    //parr(max,n);
    //parr(arr,n);
    for(int i=0;i<n;i++)
    {
        ans+=max[i]*arr[i];
        //cout<<max[i]<<' '<<arr[i]<<ans<<endl;
    }
    cout<<ans<<endl;
    return 0;
}


/*
 * answer.cpp
 *
 *  Created on: Jun 12, 2013
 *      Author: Horace He
 */

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <ctime>
#include <climits>
#include <cstdlib>

using namespace std;
#define sz(x) ((int) x.size())
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;

const double pi = 3.141592653589793;
const double tau = 6.283185307179586;
const double epsilon = 1e-6;

#define Set(a, s) memset(a, s, sizeof (a))
#define Rd(r) freopen(r, "r", stdin)
#define Wt(w) freopen(w, "w", stdout)
#define parr(a,n) for (int i=0;i<(int)(n);i++) cout << a[i] << " "; cout << endl;
#define pvec(a) for (int i=0;i<a.size();i++) cout << a[i] << " "; cout << endl;

bool cmp(int a, int b)
{
    if(a>b)
        return true;
    else
        return false;
}

int main (int argc, char ** argv)
{
    ll max[200005]={0};
    ll front[20005];
    ll back[20005];
    ll arr[200005];
    ll n, q;
    ll ans=0;
    //Rd("input.in");
    scanf("%I64dd %I64dd",&n, &q);
    for(ll i=0;i<n;i++)
    {
        scanf("%I64d",&arr[i]);
    }
    //int a, b;
    for(int i=0;i<q;i++)
    {
        scanf("%I64d %I64d",&front[i],&back[i]);
        front[i]--;
        back[i]--;
    }
    sort(front,front+q);
    sort(back,back+q);
    ll over=0;
    ll fpos=0;
    ll bpos=0;
    for(ll i=0;i<n;i++)
    {
        while(front[fpos]==i)
        {
            if(front[fpos]==i)
            {
                over++;
                fpos++;
            }
        }
        max[i]=over;
        while(back[bpos]==i)
        {
            if(back[bpos]==i)
            {
                over--;
                bpos++;
            }
        }
    }
    sort(max,max+n,cmp);
    sort(arr,arr+n,cmp);
    //parr(max,n);
    //parr(arr,n);
    for(ll i=0;i<n;i++)
    {
        ans+=max[i]*arr[i];
        //cout<<max[i]<<' '<<arr[i]<<ans<<endl;
    }
    cout<<ans<<endl;
    return 0;
}


/*
 * answer.cpp
 *
 *  Created on: Jun 12, 2013
 *      Author: Horace He
 */

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <ctime>
#include <climits>
#include <cstdlib>

using namespace std;
#define sz(x) ((int) x.size())
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;

const double pi = 3.141592653589793;
const double tau = 6.283185307179586;
const double epsilon = 1e-6;

#define Set(a, s) memset(a, s, sizeof (a))
#define Rd(r) freopen(r, "r", stdin)
#define Wt(w) freopen(w, "w", stdout)
#define parr(a,n) for (int i=0;i<(int)(n);i++) cout << a[i] << " "; cout << endl;
#define pvec(a) for (int i=0;i<a.size();i++) cout << a[i] << " "; cout << endl;

bool cmp(int a, int b)
{
    if(a>b)
        return true;
    else
        return false;
}

int main (int argc, char ** argv)
{
    ll max[200005]={0};
    int front[20005];
    int back[20005];
    int arr[200005];
    int n, q;
    ll ans=0;
    //Rd("input.in");
    scanf("%d %d",&n, &q);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&arr[i]);
    }
    //int a, b;
    for(int i=0;i<q;i++)
    {
        scanf("%d %d",&front[i],&back[i]);
        //front[i]--;
        //back[i]--;
        max[front[i]-1]++;
        max[back[i]]--;
    }
    parr(max,n+1);
    ll over=0;
    //sort(front,front+q);
    //sort(back,back+q);
    ll fpos=0;
    ll bpos=0;
    /*for(int i=0;i<n;i++)
    {
        //cout<<over<<endl;
        while(front[fpos]==i)
        {
            over++;
            fpos++;
        }
        max[i]=over;
        while(back[bpos]==i)
        {
            over--;
            bpos++;
        }
    }*/
    for(int i=1;i<n;i++)
        max[i]=max[i-1];
    parr(max,n+1);
    sort(max,max+n);
    sort(arr,arr+n);
    //parr(max,n);
    //parr(arr,n);
    for(int i=0;i<n;i++)
    {
        ans+=max[i]*arr[i];
        //cout<<max[i]<<' '<<arr[i]<<ans<<endl;
    }
    cout<<ans<<endl;
    return 0;
}


/*
 * answer.cpp
 *
 *  Created on: Jun 12, 2013
 *      Author: Horace He
 */

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <ctime>
#include <climits>
#include <cstdlib>

using namespace std;
#define sz(x) ((int) x.size())
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;

const double pi = 3.141592653589793;
const double tau = 6.283185307179586;
const double epsilon = 1e-6;

#define Set(a, s) memset(a, s, sizeof (a))
#define Rd(r) freopen(r, "r", stdin)
#define Wt(w) freopen(w, "w", stdout)
#define parr(a,n) for (int i=0;i<(int)(n);i++) cout << a[i] << " "; cout << endl;
#define pvec(a) for (int i=0;i<a.size();i++) cout << a[i] << " "; cout << endl;

int main (int argc, char ** argv)
{
    int max[200005]={0};
    int arr[200005];
    int n, q;
    ll ans=0;
    //Rd("input.in");
    scanf("%d %d",&n, &q);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&arr[i]);
    }
    int a, b;
    for(int i=0;i<q;i++)
    {
        scanf("%d %d",&a,&b);
        for(int j=a-1;j<b;j++)
        {
            max[j]++;
        }
    }
    sort(max,max+n);
    sort(arr,arr+n);

    for(int i=0;i<n;i++)
    {
        ans+=max[i]*arr[i];
        //cout<<max[i]<<' '<<arr[i]<<ans<<endl;
    }
    cout<<ans<<endl;
    return 0;
}


/*
 * answer.cpp
 *
 *  Created on: Jun 12, 2013
 *      Author: Horace He
 */

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <ctime>
#include <climits>
#include <cstdlib>

using namespace std;
#define sz(x) ((int) x.size())
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;

const double pi = 3.141592653589793;
const double tau = 6.283185307179586;
const double epsilon = 1e-6;

#define Set(a, s) memset(a, s, sizeof (a))
#define Rd(r) freopen(r, "r", stdin)
#define Wt(w) freopen(w, "w", stdout)
#define parr(a,n) for (int i=0;i<(int)(n);i++) cout << a[i] << " "; cout << endl;
#define pvec(a) for (int i=0;i<a.size();i++) cout << a[i] << " "; cout << endl;

bool cmp(int a, int b)
{
    if(a>b)
        return true;
    else
        return false;
}

int main (int argc, char ** argv)
{
    ll max[200005]={0};
    int front[20005];
    int back[20005];
    int arr[200005];
    int n, q;
    ll ans=0;
    //Rd("input.in");
    scanf("%d %d",&n, &q);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&arr[i]);
    }
    //int a, b;
    for(int i=0;i<q;i++)
    {
        scanf("%d %d",&front[i],&back[i]);
        //front[i]--;
        //back[i]--;
        max[front[i]-1]++;
        max[back[i]]--;
    }
    //parr(max,n+1);
    ll over=0;
    //sort(front,front+q);
    //sort(back,back+q);
    ll fpos=0;
    ll bpos=0;
    /*for(int i=0;i<n;i++)
    {
        //cout<<over<<endl;
        while(front[fpos]==i)
        {
            over++;
            fpos++;
        }
        max[i]=over;
        while(back[bpos]==i)
        {
            over--;
            bpos++;
        }
    }*/
    for(int i=1;i<n;i++)
        max[i]+=max[i-1];
    //parr(max,n+1);
    sort(max,max+n);
    sort(arr,arr+n);
    //parr(max,n);
    //parr(arr,n);
    for(int i=0;i<n;i++)
    {
        ans+=max[i]*arr[i];
        //cout<<max[i]<<' '<<arr[i]<<ans<<endl;
    }
    cout<<ans<<endl;
    return 0;
}


/*
 * answer.cpp
 *
 *  Created on: Jun 12, 2013
 *      Author: Horace He
 */

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <ctime>
#include <climits>
#include <cstdlib>

using namespace std;
#define sz(x) ((int) x.size())
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;

const double pi = 3.141592653589793;
const double tau = 6.283185307179586;
const double epsilon = 1e-6;

#define Set(a, s) memset(a, s, sizeof (a))
#define Rd(r) freopen(r, "r", stdin)
#define Wt(w) freopen(w, "w", stdout)
#define parr(a,n) for (int i=0;i<(int)(n);i++) cout << a[i] << " "; cout << endl;
#define pvec(a) for (int i=0;i<a.size();i++) cout << a[i] << " "; cout << endl;

bool cmp(int a, int b)
{
    if(a>b)
        return true;
    else
        return false;
}

int main (int argc, char ** argv)
{
    ll max[200005]={0};
    int front[20005];
    int back[20005];
    int arr[200005];
    int n, q;
    ll ans=0;
    //Rd("input.in");
    scanf("%d %d",&n, &q);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&arr[i]);
    }
    //int a, b;
    for(int i=0;i<q;i++)
    {
        scanf("%d %d",&front[i],&back[i]);
        //front[i]--;
        //back[i]--;
        max[front[i]-1]++;
        max[back[i]]--;
    }
    parr(max,n+1);
    ll over=0;
    //sort(front,front+q);
    //sort(back,back+q);
    ll fpos=0;
    ll bpos=0;
    /*for(int i=0;i<n;i++)
    {
        //cout<<over<<endl;
        while(front[fpos]==i)
        {
            over++;
            fpos++;
        }
        max[i]=over;
        while(back[bpos]==i)
        {
            over--;
            bpos++;
        }
    }*/
    for(int i=1;i<n;i++)
        max[i]=max[i-1];
    sort(max,max+n);
    sort(arr,arr+n);
    //parr(max,n);
    //parr(arr,n);
    for(int i=0;i<n;i++)
    {
        ans+=max[i]*arr[i];
        //cout<<max[i]<<' '<<arr[i]<<ans<<endl;
    }
    cout<<ans<<endl;
    return 0;
}


/*
 * answer.cpp
 *
 *  Created on: Jun 12, 2013
 *      Author: Horace He
 */

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <ctime>
#include <climits>
#include <cstdlib>

using namespace std;
#define sz(x) ((int) x.size())
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;

const double pi = 3.141592653589793;
const double tau = 6.283185307179586;
const double epsilon = 1e-6;

#define Set(a, s) memset(a, s, sizeof (a))
#define Rd(r) freopen(r, "r", stdin)
#define Wt(w) freopen(w, "w", stdout)
#define parr(a,n) for (int i=0;i<(int)(n);i++) cout << a[i] << " "; cout << endl;
#define pvec(a) for (int i=0;i<a.size();i++) cout << a[i] << " "; cout << endl;

 int n,q,l,r;
  long long a[200010],t[200010],ans;

  int main(){
      cin>>n>>q;
     for(int i=0;i<n;i++)
         cin>>a[i];
     sort(a,a+n);
     for(int i=0;i<q;i++){
         cin>>l>>r;
         t[l-1]++,t[r]--;
     }
     for(int i=1;i<n;i++)
         t[i]+=t[i-1];
     sort(t,t+n);
     for(int i=0;i<n;i++)
         ans+=a[i]*t[i];
     cout<<ans;
     return 0;
 }


/*
 * answer.cpp
 *
 *  Created on: Jun 12, 2013
 *      Author: Horace He
 */

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <ctime>
#include <climits>
#include <cstdlib>

using namespace std;
#define sz(x) ((int) x.size())
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;

const double pi = 3.141592653589793;
const double tau = 6.283185307179586;
const double epsilon = 1e-6;

#define Set(a, s) memset(a, s, sizeof (a))
#define Rd(r) freopen(r, "r", stdin)
#define Wt(w) freopen(w, "w", stdout)
#define parr(a,n) for (int i=0;i<(int)(n);i++) cout << a[i] << " "; cout << endl;
#define pvec(a) for (int i=0;i<a.size();i++) cout << a[i] << " "; cout << endl;

bool cmp(int a, int b)
{
	if(a>b)
		return true;
	else
		return false;
}

int main (int argc, char ** argv)
{
	int max[200005]={0};
	int front[20005];
	int back[20005];
	int arr[200005];
	int n, q;
	ll ans=0;
	//Rd("input.in");
	scanf("%d %d",&n, &q);
	for(int i=0;i<n;i++)
	{
		scanf("%d",&arr[i]);
	}
	//int a, b;
	for(int i=0;i<q;i++)
	{
		scanf("%d %d",&front[i],&back[i]);
		front[i]--;
		back[i]--;
	}
	sort(front,front+q);
	sort(back,back+q);
	int over=0;
	int fpos=0;
	int bpos=0;
	for(int i=0;i<n;i++)
	{
		while(front[fpos]==i)
		{
			if(front[fpos]==i)
			{
				over++;
				fpos++;
			}
		}
		max[i]=over;
		while(back[bpos]==i)
		{
			if(back[bpos]==i)
			{
				over--;
				bpos++;
			}
		}
	}
	sort(max,max+n,cmp);
	sort(arr,arr+n,cmp);
	parr(max,n);
	parr(arr,n);
	for(int i=0;i<n;i++)
	{
		ans+=max[i]*arr[i];
		//cout<<max[i]<<' '<<arr[i]<<ans<<endl;
	}
	cout<<ans<<endl;
	return 0;
}


/*
 * answer.cpp
 *
 *  Created on: Jun 12, 2013
 *      Author: Horace He
 */

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <ctime>
#include <climits>
#include <cstdlib>

using namespace std;
#define sz(x) ((int) x.size())
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;

const double pi = 3.141592653589793;
const double tau = 6.283185307179586;
const double epsilon = 1e-6;

#define Set(a, s) memset(a, s, sizeof (a))
#define Rd(r) freopen(r, "r", stdin)
#define Wt(w) freopen(w, "w", stdout)
#define parr(a,n) for (int i=0;i<(int)(n);i++) cout << a[i] << " "; cout << endl;
#define pvec(a) for (int i=0;i<a.size();i++) cout << a[i] << " "; cout << endl;

bool cmp(int a, int b)
{
    if(a>b)
        return true;
    else
        return false;
}

int main (int argc, char ** argv)
{
    int max[200005]={0};
    int front[20005];
    int back[20005];
    int arr[200005];
    int n, q;
    ll ans=0;
    //Rd("input.in");
    scanf("%d %d",&n, &q);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&arr[i]);
    }
    //int a, b;
    for(int i=0;i<q;i++)
    {
        scanf("%d %d",&front[i],&back[i]);
        front[i]--;
        back[i]--;
    }
    sort(front,front+q);
    sort(back,back+q);
    int over=0;
    int fpos=0;
    int bpos=0;
    for(int i=0;i<n;i++)
    {
        while(front[fpos]==i)
        {
            if(front[fpos]==i)
            {
                over++;
                fpos++;
            }
        }
        max[i]=over;
        while(back[bpos]==i)
        {
            if(back[bpos]==i)
            {
                over--;
                bpos++;
            }
        }
    }
    sort(max,max+n,cmp);
    sort(arr,arr+n,cmp);
    //parr(max,n);
    //parr(arr,n);
    for(int i=0;i<n;i++)
    {
        ans+=max[i]*arr[i];
        //cout<<max[i]<<' '<<arr[i]<<ans<<endl;
    }
    cout<<ans<<endl;
    return 0;
}


/*
 * answer.cpp
 *
 *  Created on: Jun 12, 2013
 *      Author: Horace He
 */

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <ctime>
#include <climits>
#include <cstdlib>

using namespace std;
#define sz(x) ((int) x.size())
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;

const double pi = 3.141592653589793;
const double tau = 6.283185307179586;
const double epsilon = 1e-6;

#define Set(a, s) memset(a, s, sizeof (a))
#define Rd(r) freopen(r, "r", stdin)
#define Wt(w) freopen(w, "w", stdout)
#define parr(a,n) for (int i=0;i<(int)(n);i++) cout << a[i] << " "; cout << endl;
#define pvec(a) for (int i=0;i<a.size();i++) cout << a[i] << " "; cout << endl;

bool cmp(int a, int b)
{
    if(a>b)
        return true;
    else
        return false;
}

int main (int argc, char ** argv)
{
    ll max[200005]={0};
    int front[20005];
    int back[20005];
    ll arr[200005];
    int n, q;
    ll ans=0;
    //Rd("input.in");
    scanf("%d %d",&n, &q);
    for(ll i=0;i<n;i++)
    {
        scanf("%I64d",&arr[i]);
    }
    //int a, b;
    for(int i=0;i<q;i++)
    {
        scanf("%d %d",&front[i],&back[i]);
        front[i]--;
        back[i]--;
    }
    sort(front,front+q);
    sort(back,back+q);
    ll over=0;
    ll fpos=0;
    ll bpos=0;
    for(int i=0;i<n;i++)
    {
        //cout<<over<<endl;
        while(front[fpos]==i)
        {
            over++;
            fpos++;
        }
        max[i]=over;
        while(back[bpos]==i)
        {
            over--;
            bpos++;
        }
    }
    sort(max,max+n,cmp);
    sort(arr,arr+n,cmp);
    //parr(max,n);
    //parr(arr,n);
    for(int i=0;i<n;i++)
    {
        ans+=max[i]*arr[i];
        //cout<<max[i]<<' '<<arr[i]<<ans<<endl;
    }
    cout<<ans<<endl;
    return 0;
}


#include <iostream>
  #include <algorithm>
  using namespace std;

  int n,q,l,r;
  long long a[200010],t[200010],ans;

  int main(){
      cin>>n>>q;
     for(int i=0;i<n;i++)
         cin>>a[i];
     sort(a,a+n);
     for(int i=0;i<q;i++){
         cin>>l>>r;
         t[l-1]++,t[r]--;
     }
     for(int i=1;i<n;i++)
         t[i]+=t[i-1];
     sort(t,t+n);
     for(int i=0;i<n;i++)
         ans+=a[i]*t[i];
     cout<<ans;
     return 0;
 }


/*
 * answer.cpp
 *
 *  Created on: Jun 12, 2013
 *      Author: Horace He
 */

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <ctime>
#include <climits>
#include <cstdlib>

using namespace std;
#define sz(x) ((int) x.size())
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;

const double pi = 3.141592653589793;
const double tau = 6.283185307179586;
const double epsilon = 1e-6;

#define Set(a, s) memset(a, s, sizeof (a))
#define Rd(r) freopen(r, "r", stdin)
#define Wt(w) freopen(w, "w", stdout)
#define parr(a,n) for (int i=0;i<(int)(n);i++) cout << a[i] << " "; cout << endl;
#define pvec(a) for (int i=0;i<a.size();i++) cout << a[i] << " "; cout << endl;

bool cmp(int a, int b)
{
    if(a>b)
        return true;
    else
        return false;
}

int main (int argc, char ** argv)
{
    ll max[200005]={0};
    int front[20005];
    int back[20005];
    int arr[200005];
    int n, q;
    ll ans=0;
    //Rd("input.in");
    scanf("%d %d",&n, &q);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&arr[i]);
    }
    //int a, b;
    for(int i=0;i<q;i++)
    {
        scanf("%d %d",&front[i],&back[i]);
        //front[i]--;
        //back[i]--;
        max[front[i]-1]++;
        max[back[i]]--;
    }
    ll over=0;
    sort(front,front+q);
    sort(back,back+q);
    ll fpos=0;
    ll bpos=0;
    /*for(int i=0;i<n;i++)
    {
        //cout<<over<<endl;
        while(front[fpos]==i)
        {
            over++;
            fpos++;
        }
        max[i]=over;
        while(back[bpos]==i)
        {
            over--;
            bpos++;
        }
    }*/
    for(int i=1;i<n;i++
        max[i]=max[i-1];
    sort(max,max+n);
    sort(arr,arr+n);
    //parr(max,n);
    //parr(arr,n);
    for(int i=0;i<n;i++)
    {
        ans+=max[i]*arr[i];
        //cout<<max[i]<<' '<<arr[i]<<ans<<endl;
    }
    cout<<ans<<endl;
    return 0;
}


/*
 * answer.cpp
 *
 *  Created on: Jun 12, 2013
 *      Author: Horace He
 */

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <ctime>
#include <climits>
#include <cstdlib>

using namespace std;
#define sz(x) ((int) x.size())
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;

const double pi = 3.141592653589793;
const double tau = 6.283185307179586;
const double epsilon = 1e-6;

#define Set(a, s) memset(a, s, sizeof (a))
#define Rd(r) freopen(r, "r", stdin)
#define Wt(w) freopen(w, "w", stdout)
#define parr(a,n) for (int i=0;i<(int)(n);i++) cout << a[i] << " "; cout << endl;
#define pvec(a) for (int i=0;i<a.size();i++) cout << a[i] << " "; cout << endl;

bool cmp(int a, int b)
{
    if(a>b)
        return true;
    else
        return false;
}

int main (int argc, char ** argv)
{
    ll max[200005]={0};
    ll front[20005];
    ll back[20005];
    ll arr[200005];
    ll n, q;
    ll ans=0;
    //Rd("input.in");
    scanf("%I64dd %I64dd",&n, &q);
    for(ll int i=0;i<n;i++)
    {
        scanf("%I64d",&arr[i]);
    }
    //int a, b;
    for(int i=0;i<q;i++)
    {
        scanf("%I64d %I64d",&front[i],&back[i]);
        front[i]--;
        back[i]--;
    }
    sort(front,front+q);
    sort(back,back+q);
    ll over=0;
    ll fpos=0;
    ll bpos=0;
    for(ll i=0;i<n;i++)
    {
        while(front[fpos]==i)
        {
            if(front[fpos]==i)
            {
                over++;
                fpos++;
            }
        }
        max[i]=over;
        while(back[bpos]==i)
        {
            if(back[bpos]==i)
            {
                over--;
                bpos++;
            }
        }
    }
    sort(max,max+n,cmp);
    sort(arr,arr+n,cmp);
    //parr(max,n);
    //parr(arr,n);
    for(ll i=0;i<n;i++)
    {
        ans+=max[i]*arr[i];
        //cout<<max[i]<<' '<<arr[i]<<ans<<endl;
    }
    cout<<ans<<endl;
    return 0;
}


/*
 * answer.cpp
 *
 *  Created on: Jun 12, 2013
 *      Author: Horace He
 */

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <ctime>
#include <climits>
#include <cstdlib>

using namespace std;
#define sz(x) ((int) x.size())
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;

const double pi = 3.141592653589793;
const double tau = 6.283185307179586;
const double epsilon = 1e-6;

#define Set(a, s) memset(a, s, sizeof (a))
#define Rd(r) freopen(r, "r", stdin)
#define Wt(w) freopen(w, "w", stdout)
#define parr(a,n) for (int i=0;i<(int)(n);i++) cout << a[i] << " "; cout << endl;
#define pvec(a) for (int i=0;i<a.size();i++) cout << a[i] << " "; cout << endl;

bool cmp(int a, int b)
{
    if(a>b)
        return true;
    else
        return false;
}

int main (int argc, char ** argv)
{
    ll max[200005]={0};
    int front[20005];
    int back[20005];
    ll arr[200005];
    int n, q;
    ll ans=0;
    //Rd("input.in");
    scanf("%d %d",&n, &q);
    for(ll i=0;i<n;i++)
    {
        scanf("%I64d",&arr[i]);
    }
    //int a, b;
    for(int i=0;i<q;i++)
    {
        scanf("%d %d",&front[i],&back[i]);
        front[i]--;
        back[i]--;
    }
    sort(front,front+q);
    sort(back,back+q);
    ll over=0;
    ll fpos=0;
    ll bpos=0;
    for(int i=0;i<n;i++)
    {
        //cout<<over<<endl;
        while(front[fpos]==i)
        {
            over++;
            fpos++;
        }
        max[i]=over;
        while(back[bpos]==i)
        {
            over--;
            bpos++;
        }
    }
    sort(max,max+n);
    sort(arr,arr+n);
    //parr(max,n);
    //parr(arr,n);
    for(int i=0;i<n;i++)
    {
        ans+=max[i]*arr[i];
        //cout<<max[i]<<' '<<arr[i]<<ans<<endl;
    }
    cout<<ans<<endl;
    return 0;
}


/*
 * answer.cpp
 *
 *  Created on: Jun 12, 2013
 *      Author: Horace He
 */

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <ctime>
#include <climits>
#include <cstdlib>

using namespace std;
#define sz(x) ((int) x.size())
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;

const double pi = 3.141592653589793;
const double tau = 6.283185307179586;
const double epsilon = 1e-6;

#define Set(a, s) memset(a, s, sizeof (a))
#define Rd(r) freopen(r, "r", stdin)
#define Wt(w) freopen(w, "w", stdout)
#define parr(a,n) for (int i=0;i<(int)(n);i++) cout << a[i] << " "; cout << endl;
#define pvec(a) for (int i=0;i<a.size();i++) cout << a[i] << " "; cout << endl;

bool cmp(int a, int b)
{
    if(a>b)
        return true;
    else
        return false;
}

int main (int argc, char ** argv)
{
    ll max[200005]={0};
    ll front[20005];
    ll back[20005];
    ll arr[200005];
    ll n, q;
    ll ans=0;
    //Rd("input.in");
    scanf("%I64d %I64d",&n, &q);
    for(llint i=0;i<n;i++)
    {
        scanf("%I64d",&arr[i]);
    }
    //int a, b;
    for(int i=0;i<q;i++)
    {
        scanf("%I64d %I64d",&front[i],&back[i]);
        front[i]--;
        back[i]--;
    }
    sort(front,front+q);
    sort(back,back+q);
    ll over=0;
    ll fpos=0;
    ll bpos=0;
    for(ll i=0;i<n;i++)
    {
        while(front[fpos]==i)
        {
            if(front[fpos]==i)
            {
                over++;
                fpos++;
            }
        }
        max[i]=over;
        while(back[bpos]==i)
        {
            if(back[bpos]==i)
            {
                over--;
                bpos++;
            }
        }
    }
    sort(max,max+n,cmp);
    sort(arr,arr+n,cmp);
    //parr(max,n);
    //parr(arr,n);
    for(ll i=0;i<n;i++)
    {
        ans+=max[i]*arr[i];
        //cout<<max[i]<<' '<<arr[i]<<ans<<endl;
    }
    cout<<ans<<endl;
    return 0;
}


#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin("a.in");
int a[200010], b[200010];

int main()
{
    int n, q, i, x, y;
    cin >> n >> q;
    for(i = 1 ; i <= n ; i++)
    {
        cin >> a[i];
    }
    for(i = 1 ; i <= q ; i++)
    {
        cin >> x >> y;
        b[x]++;
        b[y + 1] --;
    }
    for(i = 1 ; i <= n ; i++)
    {
        b[i] += b[i - 1];
    }
    sort(b + 1, b + n + 1);
    sort(a + 1, a + n + 1);
    long long s = 0;
    for(i = 1 ; i <= n ; i++)
    {
        s += 1LL * a[i] * b[i];
    }
    cout << s << "\n";
}


#include <iostream>
//#include <fstream>
#include <algorithm>
using namespace std;
#define MAX 200005
//ifstream fin("a.in");
int a[MAX], b[MAX];

int main()
{
    int n, q, i, x, y;
    cin >> n >> q;
    for(i = 1 ; i <= n ; i++)
    {
        cin >> a[i];
    }
    for(i = 1 ; i <= q ; i++)
    {
        cin >> x >> y;
        b[x]++;
        b[y + 1] --;
    }
    for(i = 1 ; i <= n ; i++)
    {
        b[i] += b[i - 1];
    }
    sort(b + 1, b + n + 1);
    sort(a + 1, a + n + 1);
    long long s = 0;
    for(i = 1 ; i <= n ; i++)
    {
        s += 1LL * a[i] * b[i];
    }
    cout << s << "\n";
}


#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
#define MAX 200005

int a[MAX], b[MAX];

int main()
{
    int n, q, i, x, y;
    scanf("%d %d", &n, &q);
    for(i = 1 ; i <= n ; i++)
    {
        scanf("%d", a + i);
    }
    for(i = 1 ; i <= q ; i++)
    {
        scanf("%d %d", &x, &y);
        b[x]++;
        b[y + 1] --;
    }
    for(i = 1 ; i <= n ; i++)
    {
        b[i] += b[i - 1];
    }
    sort(b + 1, b + n + 1);
    sort(a + 1, a + n + 1);
    long long s = 0;
    for(i = 1 ; i <= n ; i++)
    {
        s += 1LL * a[i] * b[i];
    }
    cout << s << "\n";
}


#include <cstdio>
#include <cstdlib>
#include <ctime>

#include <algorithm>
#include <queue>
#include <iostream>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define pb push_back
#define FORIT(it, v) for(typeof((v).begin()) it=(v).begin(); it!=(v).end(); ++it)
#define mp make_pair
#define fi first
#define se second
#define th third

const int INF=0x3f3f3f3f, max_n = 200005;

int main() {
	int n, q;
	cin >> n >> q;
	int El[max_n];
	for (int i = 1; i <= n; ++i)
		cin >> El[i];
	int Ap[max_n];
	for (int i = 0; i < max_n; ++i)
		Ap[i] = 0;
	for (int i = 1; i <= q; ++i) {
		int l, r;
		cin >> l >> r;
		Ap[l]++;
		Ap[r+1]--;
	}
 	for (int i=1; i<=n; ++i) {
		Ap[i] += Ap[i-1];
	}
	sort (Ap+1, Ap+n+1);
	sort (El+1, El+n+1);
	long long sum = 0;
 	for (int i = 1; i <= n; ++i) {
		sum += 1ll * Ap[i] * El[i];
	}
	cout << sum << "\n";
	return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
using namespace std;
vector <long long> blokebi(431);
vector<long long> jeradoba(2*100001);
int main()
{
	long long n,q,l,r;
	vector <long long> numb;
	cin>>n>>q;
	numb.resize(n);
	for(int i=0; i<n; i++)
	{
		cin>>numb[i];
	}
long long begin, end;
	long long size=(long long)sqrt((double)n);
	for(int i=0; i<q; i++)
	{
		cin>>l>>r; l-=1; r-=1;
		if((r-l+1)<size)
		{
			for(long long j=l; j<=r; j++)
				jeradoba[j]++;
		}
		else
		{
			if(l%size==0)
				begin=l/size;
			else begin=l/size+1;
			if((r+1)%size==0) end=r/size;
			else end=r/size-1;
			for(long long j=begin; j<=end; j++)
			{
				blokebi[j]++;
			}
			for(long long j=l; j<begin*size; j++)
			{
				jeradoba[j]++;
			}
			for(long long j=(end+1)*size; j<=r; j++)
			{
				jeradoba[j]++;
			}
		}
	}
	for(int i = 0; i < n; i++)
	{
        jeradoba[i] += blokebi[i/size];
    }
	long long sum=0;
	sort(numb.begin(), numb.end());
	reverse(numb.begin(),numb.end());
	sort(jeradoba.begin(), jeradoba.end());
	reverse(jeradoba.begin(), jeradoba.end());
	for(long long i=0; i<n; i++)
	{
		sum+=(numb[i]*jeradoba[i]);
	}
	cout<<sum;
	//system("pause");
	return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
using namespace std;
vector <long long> blokebi(431);
vector<long long> jeradoba(2*100001);
int main()
{
	long long n,q,l,r;
	vector <long long> numb;
	cin>>n>>q;
	numb.resize(n);
	for(int i=0; i<n; i++)
	{
		cin>>numb[i];
	}
	int begin, end;
	long long size=(long long)sqrt((double)n);
	for(int i=0; i<q; i++)
	{
		cin>>l>>r; l-=1; r-=1;
		if((r-l+1)<size)
		{
			for(int j=l; j<=r; j++)
				jeradoba[j]++;
		}
		else
		{
			if(l%size==0)
				begin=l/size;
			else begin=l/size+1;
			if((r+1)%size==0) end=r/size;
			else end=r/size-1;
			for(int j=begin; j<=end; j++)
			{
				blokebi[j]++;
			}
			for(long long j=l; j<begin*size; j++)
			{
				jeradoba[j]++;
			}
			for(long long j=(end+1)*size; j<=r; j++)
			{
				jeradoba[j]++;
			}
		}
	}
	for(long long i=0; i<blokebi.size(); i++)
	{
		for(long long j=i*size; j<(i+1)*size; j++)
			jeradoba[j]+=blokebi[i];
	}
	long long sum=0;
	sort(numb.begin(), numb.end());
	sort(jeradoba.begin(), jeradoba.begin()+n);
	for(long long i=n-1; i>=0; i--)
	{
		sum+=(numb[i]*jeradoba[i]);
	}
	cout<<sum;
	//system("pause");
	return 0;
}


#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
vector <int> jeradoba(2*100000);
int main()
{
	vector <long long> mas;
	long long sum=0;
	int r,l, numbq,n;
	cin>>n>>numbq;
	mas.resize(n);
	for(int i=0; i<n; i++)
	{
		cin>>mas[i];
	}
	for(int i=0; i<numbq; i++)
	{
		cin>>l>>r;
		for(int j=l-1; j<r; j++)
		{
			jeradoba[j]++;
		}
	}
	sort(mas.begin(), mas.end());
	//reverse(mas.begin(),mas.end());
	sort(jeradoba.begin(),jeradoba.begin()+n);
	//reverse(jeradoba.begin(),jeradoba.end());
	for(int i=0; i<n; i++)
	{
		sum+=mas[i]*jeradoba[i];
	}
	cout<<sum;
	return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
using namespace std;
vector <long long> blokebi(448);
vector<long long> jeradoba(2*100001);
int main()
{
	long long n,q,l,r;
	vector <long long> numb;
	cin>>n>>q;
	numb.resize(n);
	for(int i=0; i<n; i++)
	{
		cin>>numb[i];
	}
long long begin, end;
	long long size=(long long)sqrt((double)n);
	for(int i=0; i<q; i++)
	{
		cin>>l>>r; l-=1; r-=1;
		if((r-l+1)<size)
		{
			for(long long j=l; j<=r; j++)
				jeradoba[j]++;
		}
		else
		{
			if(l%size==0)
				begin=l/size;
			else begin=l/size+1;
			if((r+1)%size==0) end=r/size;
			else end=r/size-1;
			for(long long j=begin; j<=end; j++)
			{
				blokebi[j]++;
			}
			for(long long j=l; j<begin*size; j++)
			{
				jeradoba[j]++;
			}
			for(long long j=(end+1)*size; j<=r; j++)
			{
				jeradoba[j]++;
			}
		}
	}
	for(int i = 0; i < n; i++)
	{
        jeradoba[i] += blokebi[i/size];
    }
	long long sum=0;
	sort(numb.begin(), numb.end());
	reverse(numb.begin(),numb.end());
	sort(jeradoba.begin(), jeradoba.end());
	reverse(jeradoba.begin(), jeradoba.end());
	for(long long i=0; i<n; i++)
	{
		sum+=(numb[i]*jeradoba[i]);
	}
	cout<<sum;
	//system("pause");
	return 0;
}


#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<math.h>
using namespace std;
vector <long long> jeradoba(2*100000);
vector <long long> blokebi(448);
int main()
{
	vector <long long> mas;
	long long sum=0;
	long long r,l, numbq,n;
	cin>>n>>numbq;
	mas.resize(n);
	for(long long i=0; i<n; i++)
	{
		cin>>mas[i];
	}
	if(n>=100)
	{
		long long size=(long long)(sqrt(double (n)));
		long long sawyblo, boloblo;
		for(long long i=0; i<numbq; i++)
		{
		  cin>>l>>r;
		  l-=1; r-=1;
		  if((r-l+1)<size)
		  {
			  for(long long j=l; j<=r; j++) jeradoba[j]++;
		  }
		  else
		  {
		  if(l%size==0)
		  {
			sawyblo=l/size;
		  }
		  else sawyblo=l/size+1;
		  if((r+1)%size==0) boloblo=r/size;
		  else boloblo=r/size-1;
		  for(long long j=sawyblo; j<=boloblo; j++)
		      blokebi[j]++;
		  for(long long j=l; j<sawyblo*size; j++)
			  jeradoba[j]++;
		  for(long long j=(boloblo+1)*size; j<=r; j++)
			  jeradoba[j]++;
		}
		}
		for(long long i=0; i<blokebi.size(); i++)
		{
			for(long long j=i*size; j<(i+1)*size; j++)
				jeradoba[j]+=blokebi[i];
		}
	}
	else
	{
	for(long long i=0; i<numbq; i++)
	{
		cin>>l>>r;
		for(long long j=l-1; j<r; j++)
		{
			jeradoba[j]++;
		}
	}
	}
	sort(mas.begin(), mas.end());
	reverse(mas.begin(),mas.end());
	sort(jeradoba.begin(),jeradoba.end());
	reverse(jeradoba.begin(), jeradoba.end());
	for(long long i=0; i<n; i++)
	{
		sum+=mas[i]*jeradoba[i];
	}
	cout<<sum;
	//system("pause");
	return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
using namespace std;
vector <long long> blokebi(431);
vector<long long> jeradoba(2*100001);
int main()
{
	long long n,q,l,r;
	vector <long long> numb;
	cin>>n>>q;
	numb.resize(n);
	for(int i=0; i<n; i++)
	{
		cin>>numb[i];
	}
	int begin, end;
	long long size=(long long)sqrt((double)n);
	for(int i=0; i<q; i++)
	{
		cin>>l>>r; l-=1; r-=1;
		if((r-l+1)<size)
		{
			for(long long j=l; j<=r; j++)
				jeradoba[j]++;
		}
		else
		{
			if(l%size==0)
				begin=l/size;
			else begin=l/size+1;
			if((r+1)%size==0) end=r/size;
			else end=r/size-1;
			for(long long j=begin; j<=end; j++)
			{
				blokebi[j]++;
			}
			for(long long j=l; j<begin*size; j++)
			{
				jeradoba[j]++;
			}
			for(long long j=(end+1)*size; j<=r; j++)
			{
				jeradoba[j]++;
			}
		}
	}
	for(long long i=0; i<blokebi.size()-1; i++)
	{
		for(long long j=i*size; j<(i+1)*size; j++)
			jeradoba[j]+=blokebi[i];
	}
	long long sum=0;
	sort(numb.begin(), numb.end());
	reverse(numb.begin(),numb.end());
	sort(jeradoba.begin(), jeradoba.end());
	reverse(jeradoba.begin(), jeradoba.end());
	for(long long i=0; i<n; i--)
	{
		sum+=(numb[i]*jeradoba[i]);
	}
	cout<<sum;
	//system("pause");
	return 0;
}


#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<math.h>
using namespace std;
vector <int> jeradoba(2*100000);
vector <int> blokebi(448);
int main()
{
	vector <long long> mas;
	long long sum=0;
	int r,l, numbq,n;
	cin>>n>>numbq;
	mas.resize(n);
	for(int i=0; i<n; i++)
	{
		cin>>mas[i];
	}
	if(n>=100)
	{
		int size=(int)(sqrt(double (n)));
		int sawyblo, boloblo;
		for(int i=0; i<numbq; i++)
		{
		  cin>>l>>r;
		  l-=1; r-=1;
		  if(l%size==0)
		  {
			sawyblo=l/size;
		  }
		  else sawyblo=l/size+1;
		  if((r+1)%size==0) boloblo=r/size;
		  else boloblo=r/size-1;
		  for(int j=sawyblo; j<=boloblo; j++)
		      blokebi[j]++;
		  for(int j=l; j<sawyblo*size; j++)
			  jeradoba[j]++;
		  for(int j=(boloblo+1)*size; j<=r; j++)
			  jeradoba[j]++;
		}
		for(int i=0; i<size-1; i++)
		{
			for(int j=i*size; j<(i+1)*size; j++)
				jeradoba[j]+=blokebi[i];
		}
	}
	else
	{
	for(int i=0; i<numbq; i++)
	{
		cin>>l>>r;
		for(int j=l-1; j<r; j++)
		{
			jeradoba[j]++;
		}
	}
	}
	sort(mas.begin(), mas.end());
	reverse(mas.begin(),mas.end());
	sort(jeradoba.begin(),jeradoba.begin()+n);
	reverse(jeradoba.begin(), jeradoba.begin()+n);
	for(int i=0; i<n; i++)
	{
		sum+=mas[i]*jeradoba[i];
	}
	cout<<sum;
	return 0;
}


#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<math.h>
using namespace std;
vector <long long> jeradoba(2*100000);
vector <long long> blokebi(448);
int main()
{
	vector <long long> mas;
	long long sum=0;
	int r,l, numbq,n;
	cin>>n>>numbq;
	mas.resize(n);
	for(int i=0; i<n; i++)
	{
		cin>>mas[i];
	}
	if(n>=100)
	{
		int size=(int)(sqrt(double (n)));
		int sawyblo, boloblo;
		for(int i=0; i<numbq; i++)
		{
		  cin>>l>>r;
		  l-=1; r-=1;
		  if((r-l+1)<size)
		  {
			  for(int j=l; j<=r; j++) jeradoba[j]++;
		  }
		  else
		  {
		  if(l%size==0)
		  {
			sawyblo=l/size;
		  }
		  else sawyblo=l/size+1;
		  if((r+1)%size==0) boloblo=r/size;
		  else boloblo=r/size-1;
		  for(int j=sawyblo; j<=boloblo; j++)
		      blokebi[j]++;
		  for(long long j=l; j<sawyblo*size; j++)
			  jeradoba[j]++;
		  for(long long j=(boloblo+1)*size; j<=r; j++)
			  jeradoba[j]++;
		}
		}
		for(int i=0; i<size; i++)
		{
			for(long long j=i*size; j<(i+1)*size; j++)
				jeradoba[j]+=blokebi[i];
		}
	}
	else
	{
	for(int i=0; i<numbq; i++)
	{
		cin>>l>>r;
		for(long long j=l-1; j<r; j++)
		{
			jeradoba[j]++;
		}
	}
	}
	sort(mas.begin(), mas.end());
	reverse(mas.begin(),mas.end());
	sort(jeradoba.begin(),jeradoba.end());
	reverse(jeradoba.begin(), jeradoba.end());
	for(int i=0; i<n; i++)
	{
		sum+=mas[i]*jeradoba[i];
	}
	cout<<sum;
	//system("pause");
	return 0;
}


#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<math.h>
using namespace std;
vector <int> jeradoba(2*100000);
vector <int> blokebi(448);
int main()
{
	vector <long long> mas;
	long long sum=0;
	int r,l, numbq,n;
	cin>>n>>numbq;
	mas.resize(n);
	for(int i=0; i<n; i++)
	{
		cin>>mas[i];
	}
	if(n>=100)
	{
		int size=(int)(sqrt(double (n)));
		int sawyblo, boloblo;
		for(int i=0; i<numbq; i++)
		{
		  cin>>l>>r;
		  l-=1; r-=1;
		  if((r-l+1)<size)
		  {
			  for(int j=l; j<=r; j++) jeradoba[j]++;
		  }
		  else
		  {
		  if(l%size==0)
		  {
			sawyblo=l/size;
		  }
		  else sawyblo=l/size+1;
		  if((r+1)%size==0) boloblo=r/size;
		  else boloblo=r/size-1;
		  for(int j=sawyblo; j<=boloblo; j++)
		      blokebi[j]++;
		  for(int j=l; j<sawyblo*size; j++)
			  jeradoba[j]++;
		  for(int j=(boloblo+1)*size; j<=r; j++)
			  jeradoba[j]++;
		}
		}
		for(int i=0; i<size-1; i++)
		{
			for(int j=i*size; j<(i+1)*size; j++)
				jeradoba[j]+=blokebi[i];
		}
	}
	else
	{
	for(int i=0; i<numbq; i++)
	{
		cin>>l>>r;
		for(int j=l-1; j<r; j++)
		{
			jeradoba[j]++;
		}
	}
	}
	sort(mas.begin(), mas.end());
	reverse(mas.begin(),mas.end());
	sort(jeradoba.begin(),jeradoba.begin()+n);
	reverse(jeradoba.begin(), jeradoba.begin()+n);
	for(int i=0; i<n; i++)
	{
		sum+=mas[i]*jeradoba[i];
	}
	cout<<sum;
	return 0;
}


#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<math.h>
using namespace std;
vector <int> jeradoba(2*100000);
vector <int> blokebi(448);
int main()
{
	vector <long long> mas;
	long long sum=0;
	int r,l, numbq,n;
	cin>>n>>numbq;
	mas.resize(n);
	for(int i=0; i<n; i++)
	{
		cin>>mas[i];
	}
	if(n>=100)
	{
		int size=(int)(sqrt(double (n)));
		int sawyblo, boloblo;
		for(int i=0; i<numbq; i++)
		{
		  cin>>l>>r;
		  l-=1; r-=1;
		  if((r-l+1)<size)
		  {
			  for(int j=l; j<=r; j++) jeradoba[j]++;
		  }
		  else
		  {
		  if(l%size==0)
		  {
			sawyblo=l/size;
		  }
		  else sawyblo=l/size+1;
		  if((r+1)%size==0) boloblo=r/size;
		  else boloblo=r/size-1;
		  for(int j=sawyblo; j<=boloblo; j++)
		      blokebi[j]++;
		  for(int j=l; j<sawyblo*size; j++)
			  jeradoba[j]++;
		  for(int j=(boloblo+1)*size; j<=r; j++)
			  jeradoba[j]++;
		}
		}
		for(int i=0; i<size; i++)
		{
			for(int j=i*size; j<(i+1)*size; j++)
				jeradoba[j]+=blokebi[i];
		}
	}
	else
	{
	for(int i=0; i<numbq; i++)
	{
		cin>>l>>r;
		for(int j=l-1; j<r; j++)
		{
			jeradoba[j]++;
		}
	}
	}
	sort(mas.begin(), mas.end());
	reverse(mas.begin(),mas.end());
	sort(jeradoba.begin(),jeradoba.end());
	reverse(jeradoba.begin(), jeradoba.end());
	for(int i=0; i<n; i++)
	{
		sum+=mas[i]*jeradoba[i];
	}
	cout<<sum;
	//system("pause");
	return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
using namespace std;
vector <long long> blokebi(431);
vector<long long> jeradoba(2*100001);
int main()
{
	long long n,q,l,r;
	vector <long long> numb;
	cin>>n>>q;
	numb.resize(n);
	for(int i=0; i<n; i++)
	{
		cin>>numb[i];
	}
	int begin, end;
	long long size=(long long)sqrt((double)n);
	for(int i=0; i<q; i++)
	{
		cin>>l>>r; l-=1; r-=1;
		if((r-l+1)<size)
		{
			for(int j=l; j<=r; j++)
				jeradoba[j]++;
		}
		else
		{
			if(l%size==0)
				begin=l/size;
			else begin=l/size+1;
			if((r+1)%size==0) end=r/size;
			else end=r/size-1;
			for(int j=begin; j<=end; j++)
			{
				blokebi[j]++;
			}
			for(long long j=l; j<begin*size; j++)
			{
				jeradoba[j]++;
			}
			for(long long j=(end+1)*size; j<=r; j++)
			{
				jeradoba[j]++;
			}
		}
	}
	for(long long i=0; i<size-1; i++)
	{
		for(long long j=i*size; j<(i+1)*size; j++)
			jeradoba[j]+=blokebi[i];
	}
	long long sum=0;
	sort(numb.begin(), numb.end());
	reverse(numb.begin(),numb.end());
	sort(jeradoba.begin(), jeradoba.begin()+n);
	reverse(jeradoba.begin(), jeradoba.end()+n);
	for(long long i=0; i<n; i--)
	{
		sum+=(numb[i]*jeradoba[i]);
	}
	cout<<sum;
	//system("pause");
	return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
using namespace std;
vector <long long> blokebi(431);
vector<long long> jeradoba(2*100001);
int main()
{
	long long n,q,l,r;
	vector <long long> numb;
	cin>>n>>q;
	numb.resize(n);
	for(int i=0; i<n; i++)
	{
		cin>>numb[i];
	}
	int begin, end;
	long long size=(long long)sqrt((double)n);
	for(int i=0; i<q; i++)
	{
		cin>>l>>r; l-=1; r-=1;
		if((r-l+1)<size)
		{
			for(int j=l; j<=r; j++)
				jeradoba[j]++;
		}
		else
		{
			if(l%size==0)
				begin=l/size;
			else begin=l/size+1;
			if((r+1)%size==0) end=r/size;
			else end=r/size-1;
			for(int j=begin; j<=end; j++)
			{
				blokebi[j]++;
			}
			for(long long j=l; j<begin*size; j++)
			{
				jeradoba[j]++;
			}
			for(long long j=(end+1)*size; j<=r; j++)
			{
				jeradoba[j]++;
			}
		}
	}
	for(long long i=0; i<blokebi.size()-1; i++)
	{
		for(long long j=i*size; j<(i+1)*size; j++)
			jeradoba[j]+=blokebi[i];
	}
	long long sum=0;
	sort(numb.begin(), numb.end());
	sort(jeradoba.begin(), jeradoba.begin()+n);
	for(long long i=n-1; i>=0; i--)
	{
		sum+=(numb[i]*jeradoba[i]);
	}
	cout<<sum;
	//system("pause");
	return 0;
}


#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<math.h>
using namespace std;
vector <long long> jeradoba(2*100000);
vector <long long> blokebi(448);
int main()
{
	vector <long long> mas;
	long long sum=0;
	long long r,l, numbq,n;
	cin>>n>>numbq;
	mas.resize(n);
	for(long long i=0; i<n; i++)
	{
		cin>>mas[i];
	}
	if(n>=100)
	{
		long long size=(long long)(sqrt(double (n)));
		long long sawyblo, boloblo;
		for(long long i=0; i<numbq; i++)
		{
		  cin>>l>>r;
		  l-=1; r-=1;
		  if((r-l+1)<size)
		  {
			  for(long long j=l; j<=r; j++) jeradoba[j]++;
		  }
		  else
		  {
		  if(l%size==0)
		  {
			sawyblo=l/size;
		  }
		  else sawyblo=l/size+1;
		  if((r+1)%size==0) boloblo=r/size;
		  else boloblo=r/size-1;
		  for(long long j=sawyblo; j<=boloblo; j++)
		      blokebi[j]++;
		  for(long long j=l; j<sawyblo*size; j++)
			  jeradoba[j]++;
		  for(long long j=(boloblo+1)*size; j<=r; j++)
			  jeradoba[j]++;
		}
		}
		for(long long i=0; i<blokebi.size()-1; i++)
		{
			for(long long j=i*size; j<(i+1)*size; j++)
				jeradoba[j]+=blokebi[i];
		}
	}
	else
	{
	for(long long i=0; i<numbq; i++)
	{
		cin>>l>>r;
		for(long long j=l-1; j<r; j++)
		{
			jeradoba[j]++;
		}
	}
	}
	sort(mas.begin(), mas.end());
	reverse(mas.begin(),mas.end());
	sort(jeradoba.begin(),jeradoba.end());
	reverse(jeradoba.begin(), jeradoba.end());
	for(long long i=0; i<n; i++)
	{
		sum+=mas[i]*jeradoba[i];
	}
	cout<<sum;
	//system("pause");
	return 0;
}


#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<math.h>
using namespace std;
vector <int> jeradoba(2*100000);
vector <int> blokebi(447);
int main()
{
	vector <long long> mas;
	long long sum=0;
	int r,l, numbq,n;
	cin>>n>>numbq;
	mas.resize(n);
	for(int i=0; i<n; i++)
	{
		cin>>mas[i];
	}
	if(n>=100)
	{
		int size=(int)(sqrt(double (n)));
		int sawyblo, boloblo;
		for(int i=0; i<numbq; i++)
		{
		  cin>>l>>r;
		  l-=1; r-=1;
		  if(l%size==0)
		  {
			sawyblo=l/size;
		  }
		  else sawyblo=l/size+1;
		  if((r+1)%size==0) boloblo=r/size;
		  else boloblo=r/size-1;
		  for(int j=sawyblo; j<=boloblo; j++)
		      blokebi[j]++;
		  for(int j=l; j<sawyblo*size; j++)
			  jeradoba[j]++;
		  for(int j=boloblo*size; j<=r; j++)
			  jeradoba[j]++;
		}
		for(int i=0; i<size-1; i++)
		{
			for(int j=i*size; j<(i+1)*size; j++)
				jeradoba[j]+=blokebi[i];
		}
	}
	else
	{
	for(int i=0; i<numbq; i++)
	{
		cin>>l>>r;
		for(int j=l-1; j<r; j++)
		{
			jeradoba[j]++;
		}
	}
	}
	sort(mas.begin(), mas.end());
	reverse(mas.begin(),mas.end());
	sort(jeradoba.begin(),jeradoba.begin()+n);
	reverse(jeradoba.begin(), jeradoba.begin()+n);
	for(int i=0; i<n; i++)
	{
		sum+=mas[i]*jeradoba[i];
	}
	cout<<sum;
	return 0;
}


#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<math.h>
using namespace std;
vector <long long> jeradoba(2*100000);
vector <long long> blokebi(448);
int main()
{
	vector <long long> mas;
	long long sum=0;
	long long r,l, numbq,n;
	cin>>n>>numbq;
	mas.resize(n);
	for(long long i=0; i<n; i++)
	{
		cin>>mas[i];
	}
	if(n>=100)
	{
		long long size=(long long)(sqrt(double (n)));
		int sawyblo, boloblo;
		for(int i=0; i<numbq; i++)
		{
		  cin>>l>>r;
		  l-=1; r-=1;
		  if((r-l+1)<size)
		  {
			  for(int j=l; j<=r; j++) jeradoba[j]++;
		  }
		  else
		  {
		  if(l%size==0)
		  {
			sawyblo=l/size;
		  }
		  else sawyblo=l/size+1;
		  if((r+1)%size==0) boloblo=r/size;
		  else boloblo=r/size-1;
		  for(int j=sawyblo; j<=boloblo; j++)
		      blokebi[j]++;
		  for(long long j=l; j<sawyblo*size; j++)
			  jeradoba[j]++;
		  for(long long j=(boloblo+1)*size; j<=r; j++)
			  jeradoba[j]++;
		}
		}
		for(long long i=0; i<size; i++)
		{
			for(long long j=i*size; j<(i+1)*size; j++)
				jeradoba[j]+=blokebi[i];
		}
	}
	else
	{
	for(int i=0; i<numbq; i++)
	{
		cin>>l>>r;
		for(long long j=l-1; j<r; j++)
		{
			jeradoba[j]++;
		}
	}
	}
	sort(mas.begin(), mas.end());
	reverse(mas.begin(),mas.end());
	sort(jeradoba.begin(),jeradoba.end());
	reverse(jeradoba.begin(), jeradoba.end());
	for(int i=0; i<n; i++)
	{
		sum+=mas[i]*jeradoba[i];
	}
	cout<<sum;
	//system("pause");
	return 0;
}


#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<math.h>
using namespace std;
vector <int> jeradoba(2*100000);
vector <int> blokebi(448);
int main()
{
	vector <long long> mas;
	long long sum=0;
	int r,l, numbq,n;
	cin>>n>>numbq;
	mas.resize(n);
	for(int i=0; i<n; i++)
	{
		cin>>mas[i];
	}
	if(n>=100)
	{
		int size=(int)(sqrt(double (n)));
		int sawyblo, boloblo;
		for(int i=0; i<numbq; i++)
		{
		  cin>>l>>r;
		  l-=1; r-=1;
		  if((l-r+1)<size)
		  {
			  for(int j=l; j<=r; j++) jeradoba[j]++;
		  }
		  else
		  {
		  if(l%size==0)
		  {
			sawyblo=l/size;
		  }
		  else sawyblo=l/size+1;
		  if((r+1)%size==0) boloblo=r/size;
		  else boloblo=r/size-1;
		  for(int j=sawyblo; j<=boloblo; j++)
		      blokebi[j]++;
		  for(int j=l; j<sawyblo*size; j++)
			  jeradoba[j]++;
		  for(int j=(boloblo+1)*size; j<=r; j++)
			  jeradoba[j]++;
		}
		}
		for(int i=0; i<size-1; i++)
		{
			for(int j=i*size; j<(i+1)*size; j++)
				jeradoba[j]+=blokebi[i];
		}
	}
	else
	{
	for(int i=0; i<numbq; i++)
	{
		cin>>l>>r;
		for(int j=l-1; j<r; j++)
		{
			jeradoba[j]++;
		}
	}
	}
	sort(mas.begin(), mas.end());
	reverse(mas.begin(),mas.end());
	sort(jeradoba.begin(),jeradoba.begin()+n);
	reverse(jeradoba.begin(), jeradoba.begin()+n);
	for(int i=0; i<n; i++)
	{
		sum+=mas[i]*jeradoba[i];
	}
	cout<<sum;
	return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
using namespace std;
vector <long long> blokebi(431);
vector<long long> jeradoba(2*100000);
int main()
{
	long long n,q,l,r;
	vector <long long> numb;
	cin>>n>>q;
	numb.resize(n);
	for(int i=0; i<n; i++)
	{
		cin>>numb[i];
	}
	int begin, end;
	long long size=(long long)sqrt((double)n);
	for(int i=0; i<q; i++)
	{
		cin>>l>>r; l-=1; r-=1;
		if((r-l+1)<size)
		{
			for(int j=l; j<=r; j++)
				jeradoba[j]++;
		}
		else
		{
			if(l%size==0)
				begin=l/size;
			else begin=l/size+1;
			if((r+1)%size==0) end=r/size;
			else end=r/size-1;
			for(int j=begin; j<=end; j++)
			{
				blokebi[j]++;
			}
			for(long long j=l; j<begin*size; j++)
			{
				jeradoba[j]++;
			}
			for(long long j=(end+1)*size; j<=r; j++)
			{
				jeradoba[j]++;
			}
		}
	}
	for(long long i=0; i<q; i++)
	{
		for(long long j=i*size; j<(i+1)*size; j++)
			jeradoba[j]+=blokebi[i];
	}
	long long sum=0;
	sort(numb.begin(), numb.end());
	sort(jeradoba.begin(), jeradoba.begin()+n);
	for(long long i=n-1; i>=0; i--)
	{
		sum+=(numb[i]*jeradoba[i]);
	}
	cout<<sum;
	//system("pause");
	return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
using namespace std;
vector <long long> blokebi(431);
vector<long long> jeradoba(2*100001);
int main()
{
	long long n,q,l,r;
	vector <long long> numb;
	cin>>n>>q;
	numb.resize(n);
	for(int i=0; i<n; i++)
	{
		cin>>numb[i];
	}
	int begin, end;
	long long size=(long long)sqrt((double)n);
	for(int i=0; i<q; i++)
	{
		cin>>l>>r; l-=1; r-=1;
		if((r-l+1)<size)
		{
			for(long long j=l; j<=r; j++)
				jeradoba[j]++;
		}
		else
		{
			if(l%size==0)
				begin=l/size;
			else begin=l/size+1;
			if((r+1)%size==0) end=r/size;
			else end=r/size-1;
			for(long long j=begin; j<=end; j++)
			{
				blokebi[j]++;
			}
			for(long long j=l; j<begin*size; j++)
			{
				jeradoba[j]++;
			}
			for(long long j=(end+1)*size; j<=r; j++)
			{
				jeradoba[j]++;
			}
		}
	}
	for(long long i=0; i<blokebi.size()-1; i++)
	{
		for(long long j=i*size; j<(i+1)*size; j++)
			jeradoba[j]+=blokebi[i];
	}
	long long sum=0;
	sort(numb.begin(), numb.end());
	reverse(numb.begin(),numb.end());
	sort(jeradoba.begin(), jeradoba.end());
	reverse(jeradoba.begin(), jeradoba.end());
	for(long long i=0; i<n; i++)
	{
		sum+=(numb[i]*jeradoba[i]);
	}
	cout<<sum;
	//system("pause");
	return 0;
}


#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
vector <int> jeradoba(2*100000);
vector <int> blokebi(447);
int main()
{
	vector <long long> mas;
	long long sum=0;
	int r,l, numbq,n;
	cin>>n>>numbq;
	mas.resize(n);
	for(int i=0; i<n; i++)
	{
		cin>>mas[i];
	}
	if(n>=100)
	{
		int size=(int)(sqrt(double (n)));
		int sawyblo, boloblo;
		for(int i=0; i<numbq; i++)
		{
		  cin>>l>>r;
		  l-=1; r-=1;
		  if(l%size==0)
		  {
			sawyblo=l/size;
		  }
		  else sawyblo=l/size+1;
		  if((r+1)%size==0) boloblo=r/size;
		  else boloblo=r/size-1;
		  for(int j=sawyblo; j<=boloblo; j++)
		      blokebi[j]++;
		  for(int j=l; j<sawyblo*size; j++)
			  jeradoba[j]++;
		  for(int j=boloblo*size; j<=r; j++)
			  jeradoba[j]++;
		}
		for(int i=0; i<size-1; i++)
		{
			for(int j=i*size; j<(i+1)*size; j++)
				jeradoba[j]+=blokebi[i];
		}
	}
	else
	{
	for(int i=0; i<numbq; i++)
	{
		cin>>l>>r;
		for(int j=l-1; j<r; j++)
		{
			jeradoba[j]++;
		}
	}
	}
	sort(mas.begin(), mas.end());
	reverse(mas.begin(),mas.end());
	sort(jeradoba.begin(),jeradoba.begin()+n);
	reverse(jeradoba.begin(), jeradoba.begin()+n);
	for(int i=0; i<n; i++)
	{
		sum+=mas[i]*jeradoba[i];
	}
	cout<<sum;
	return 0;
}


#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<math.h>
using namespace std;
vector <int> jeradoba(2*100000);
vector <int> blokebi(448);
int main()
{
	vector <long long> mas;
	long long sum=0;
	int r,l, numbq,n;
	cin>>n>>numbq;
	mas.resize(n);
	for(int i=0; i<n; i++)
	{
		cin>>mas[i];
	}
	if(n>=100)
	{
		int size=(int)(sqrt(double (n)));
		int sawyblo, boloblo;
		for(int i=0; i<numbq; i++)
		{
		  cin>>l>>r;
		  l-=1; r-=1;
		  if((r-l+1)<size)
		  {
			  for(int j=l; j<=r; j++) jeradoba[j]++;
		  }
		  else
		  {
		  if(l%size==0)
		  {
			sawyblo=l/size;
		  }
		  else sawyblo=l/size+1;
		  if((r+1)%size==0) boloblo=r/size;
		  else boloblo=r/size-1;
		  for(int j=sawyblo; j<=boloblo; j++)
		      blokebi[j]++;
		  for(int j=l; j<sawyblo*size; j++)
			  jeradoba[j]++;
		  for(int j=(boloblo+1)*size; j<=r; j++)
			  jeradoba[j]++;
		}
		}
		for(int i=0; i<size; i++)
		{
			for(int j=i*size; j<(i+1)*size; j++)
				jeradoba[j]+=blokebi[i];
		}
	}
	else
	{
	for(int i=0; i<numbq; i++)
	{
		cin>>l>>r;
		for(int j=l-1; j<r; j++)
		{
			jeradoba[j]++;
		}
	}
	}
	sort(mas.begin(), mas.end());
	reverse(mas.begin(),mas.end());
	sort(jeradoba.begin(),jeradoba.begin()+n);
	reverse(jeradoba.begin(), jeradoba.begin()+n);
	for(int i=0; i<n; i++)
	{
		sum+=mas[i]*jeradoba[i];
	}
	cout<<sum;
	//system("pause");
	return 0;
}


#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
vector <int> jeradoba(2*100000);
int main()
{
	vector <long long> mas;
	long long sum=0;
	int r,l, numbq,n;
	cin>>n>>numbq;
	mas.resize(n);
	for(int i=0; i<n; i++)
	{
		cin>>mas[i];
	}
	for(int i=0; i<numbq; i++)
	{
		cin>>l>>r;
		for(int j=l-1; j<r; j++)
		{
			jeradoba[j]++;
		}
	}
	sort(mas.begin(), mas.end());
	reverse(mas.begin(),mas.end());
	sort(jeradoba.begin(),jeradoba.end());
	reverse(jeradoba.begin(),jeradoba.end());
	for(int i=0; i<n; i++)
	{
		sum+=mas[i]*jeradoba[i];
	}
	cout<<sum;
	return 0;
}


#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<math.h>
using namespace std;
vector <long long> jeradoba(2*100000);
vector <long long> blokebi(448);
int main()
{
	vector <long long> mas;
	long long sum=0;
	long long r,l, numbq,n;
	cin>>n>>numbq;
	mas.resize(n);
	for(long long i=0; i<n; i++)
	{
		cin>>mas[i];
	}
	if(n>=100)
	{
		long long size=(long long)(sqrt(double (n)));
		long long sawyblo, boloblo;
		for(long long i=0; i<numbq; i++)
		{
		  cin>>l>>r;
		  l-=1; r-=1;
		  if((r-l+1)<size)
		  {
			  for(long long j=l; j<=r; j++) jeradoba[j]++;
		  }
		  else
		  {
		  if(l%size==0)
		  {
			sawyblo=l/size;
		  }
		  else sawyblo=l/size+1;
		  if((r+1)%size==0) boloblo=r/size;
		  else boloblo=r/size-1;
		  for(long long j=sawyblo; j<=boloblo; j++)
		      blokebi[j]++;
		  for(long long j=l; j<sawyblo*size; j++)
			  jeradoba[j]++;
		  for(long long j=(boloblo+1)*size; j<=r; j++)
			  jeradoba[j]++;
		}
		}
		for(long long i=0; i<size; i++)
		{
			for(long long j=i*size; j<(i+1)*size; j++)
				jeradoba[j]+=blokebi[i];
		}
	}
	else
	{
	for(long long i=0; i<numbq; i++)
	{
		cin>>l>>r;
		for(long long j=l-1; j<r; j++)
		{
			jeradoba[j]++;
		}
	}
	}
	sort(mas.begin(), mas.end());
	reverse(mas.begin(),mas.end());
	sort(jeradoba.begin(),jeradoba.end());
	reverse(jeradoba.begin(), jeradoba.end());
	for(long long i=0; i<n; i++)
	{
		sum+=mas[i]*jeradoba[i];
	}
	cout<<sum;
	//system("pause");
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

int n, q;
long long a[200010], x[200010], ans;
int main (int argc, char ** argv)
{
	scanf("%I64d %I64d", &n, &q);
	for (int i=0; i<n; i++)
	    scanf("%I64d", &a[i]);
	sort(a, a+n);
	int f, s;
	for (int i=0; i<q; i++)
	{
    	scanf("%I64d %I64d", &f, &s);
		x[f-1]++;
		x[s]--;
	}
	for (int i=1; i<n; i++)
	{
		x[i]+=x[i-1];
	}
	sort (x, x+n);
	for (int i=0; i<n; i++)
		ans+=x[i]*a[i];
    printf("%I64d\n", ans);
	return 0;
}


#include<bits/stdc++.h>
#define ll long long int
using namespace std;

ll n,q,i,a[200005],b[200005],l,r,ans;
int main()
{
    scanf("%I64d%I64d",&n,&q);
    for(i=0;i<n;i++)
        scanf("%I64d",&a[i]);

    while(q)
    {
        scanf("%I64d%I64d",&l,&r);
        b[l-1]++;
        b[r]--;
        q--;
    }

    for(i=1;i<n;i++)
        b[i]=b[i]+b[i-1];
    sort(b,b+n);
    sort(a,a+n);

    ans=0;
    for(i=0;i<n;i++)
        ans+=a[i]*b[i];

    cout<<ans<<endl;

    return 0;
}


#include<iostream>
#include<algorithm>

using namespace std;

int n,q,i;
long long a[200010],t[200010],f;

int main(){
  cin>>n>>q;
  for(i=0;i<n;i++)
    cin>>a[i];
  sort(a,a+n);
  int l, r;
  for(i=0;i<q;i++)
  {
    cin>>l>>r;
    t[l-1]++;
    t[r]--;
  }
  for(i=1;i<n;i++)
    t[i]+=t[i-1];
  sort(t,t+n);
  for(i=0;i<n;i++)
    f+=a[i]*t[i];
  cout<<f;
  return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

long long n, a[200010], q, x[200010];
int main (int argc, char ** argv)
{
	cin>>n>>q;
	for (int i=0; i<n; i++)
		cin>>a[i];
	sort(a, a+n);
	int f, s;
	for (int i=0; i<q; i++)
	{
		cin>>f>>s;
		for (int j=f-1; j<s; j++)
			x[j]++;
	}
	sort (x, x+n);
	long long ans=0;
	for (int i=0; i<n; i++)
		ans+=x[i]*a[i];
	cout<<ans<<'\n';
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

int n, q;
long long a[200010], x[200010], ans;
int main (int argc, char ** argv)
{
	cin>>n>>q;
	for (int i=0; i<n; i++)
		cin>>a[i];
	sort(a, a+n);
	int f, s;
	for (int i=0; i<q; i++)
	{
		cin>>f>>s;
		x[f-1]++;
		x[s]--;
	}
	for (int i=1; i<n; i++)
	{
		x[i]+=x[i-1];
	}
	sort (x, x+n);
	for (int i=0; i<n; i++)
		ans+=x[i]*a[i];
	cout<<ans<<'\n';
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

long long n, a[200010], q, x[200010];
int main (int argc, char ** argv)
{
	cin>>n>>q;
	for (int i=0; i<n; i++)
		cin>>a[i];
	sort(a, a+n);
	int f, s;
	for (int i=0; i<q; i++)
	{
		cin>>f>>s;
		x[f-1]++;
		x[s]--;
	}
	for (int i=1; i<n; i++)
	{
		x[i]+=x[i-1];
	}
	sort (x, x+n);
	long long ans=0;
	for (int i=0; i<n; i++)
		ans+=x[i]*a[i];
	cout<<ans<<'\n';
	return 0;
}


#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

long long i,n,m,x,s,y,b[200010],a[200010];
main()
{
cin>>n>>m;
for (i=1; i<=n; i++)
cin>>a[i];
for (i=1; i<=m; i++)
{
cin>>x>>y;
b[y+1]--;
b[x]++;
}

for (i=2; i<=n+1; i++)
b[i]+=b[i-1];

sort(a+1,a+n+1);
sort(b+1,b+n+2);

for (i=n; i>=1; i--)
s+=a[i]*b[i+1];

cout<<s;
return 0;
}


#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

long long n,q,i,a[200005],b[200005],l,r,ans;
int main()
{
    scanf("%I64d%I64d",&n,&q);
    for(i=0;i<n;i++)
        scanf("%I64d",&a[i]);

    while(q)
    {
        scanf("%I64d%I64d",&l,&r);
        b[l-1]++;
        b[r]--;
        q--;
    }

    for(i=1;i<n;i++)
        b[i]=b[i]+b[i-1];
    sort(b,b+n);
    sort(a,a+n);

    ans=0;
    for(i=0;i<n;i++)
        ans+=a[i]*b[i];

    cout<<ans<<endl;

    return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

int n, q;
long long a[200005], x[200005], ans;
int main (int argc, char ** argv)
{
	cin>>n>>q;
	for (int i=0; i<n; i++)
		cin>>a[i];
	sort(a, a+n);
	int f, s;
	for (int i=0; i<q; i++)
	{
		cin>>f>>s;
		x[f-1]++;
		x[s]--;
	}
	for (int i=1; i<n; i++)
	{
		x[i]+=x[i-1];
	}
	sort (x, x+n);
	for (int i=0; i<n; i++)
		ans+=x[i]*a[i];
	cout<<ans<<'\n';
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

int n, q;
long long a[200010], x[200010], ans;
int main (int argc, char ** argv)
{
	scanf("%I64d %I64d", &n, &q);
	for (int i=0; i<n; i++)
	    scanf("I64d", &a[i]);
	sort(a, a+n);
	int f, s;
	for (int i=0; i<q; i++)
	{
    	scanf("%I64d %I64d", &f, &s);
		x[f-1]++;
		x[s]--;
	}
	for (int i=1; i<n; i++)
	{
		x[i]+=x[i-1];
	}
	sort (x, x+n);
	for (int i=0; i<n; i++)
		ans+=x[i]*a[i];
    printf("%I64d\n", ans);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

long long n, a[200010], q, x[200010];
int main (int argc, char ** argv)
{
	cin>>n>>q;
	for (int i=0; i<n; i++)
		cin>>a[i];
	sort(a, a+n);
	int f, s;
	//slow
	/*for (int i=0; i<q; i++)
	{
		cin>>f>>s;
		for (int j=f-1; j<s; j++)
			x[j]++;
	}*/
	for (int i=0; i<q; i++)
	{
		cin>>f>>s;
		x[f-1]++;
		x[s]--;
	}
	for (int i=1; i<n; i++)
	{
		x[i]+=x[i-1];
	}
	sort (x, x+n);
	long long ans=0;
	for (int i=0; i<n; i++)
		ans+=x[i]*a[i];
	cout<<ans<<'\n';
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

long long n, q;
long long a[200010], x[200010], ans;
int main (int argc, char ** argv)
{
	scanf("%I64d %I64d", &n, &q);
	for (int i=0; i<n; i++)
	    scanf("%I64d", &a[i]);
	sort(a, a+n);
//	cout<<n<<' '<<q<<'\n';
	long long f, s;
	for (int i=0; i<q; i++)
	{
    	scanf("%I64d %I64d", &f, &s);
		x[f-1]++;
		x[s]--;
	}
	for (int i=1; i<n; i++)
	{
		x[i]+=x[i-1];
	}
	sort (x, x+n);
	for (int i=0; i<n; i++)
		ans+=x[i]*a[i];
    printf("%I64d\n", ans);
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>

#define out freopen("output.txt", "w", stdout);
#define in freopen("input.txt", "r", stdin);
#define pub push_back
#define pob pop_back
#define infinity 1000000010
#define LL long long
#define Pi acos(-1)
#define SZ 100010

///int col[8] = {0, 1, 1, 1, 0, -1, -1, -1};
///int row[8] = {1, 1, 0, -1, -1, -1, 0, 1};
///int col[4] = {1, 0, -1, 0};
///int row[4] = {0, 1, 0, -1};

using namespace std;

LL arr[2 * SZ];

bool comp(const LL &a, const LL &b)
{
    return a > b;
}

int main()
{
    int n, q, i, a, b;
    LL ans = 0;
    cin >> n >> q;
    for(i = 0; i < n; i++)
        cin >> arr[i];
    sort(arr, arr + n, comp);
    sort(arr, arr + (n / 2) + n % 2);
    for(i = 1; i < n; i++)
        arr[i] += arr[i - 1];
    for(i = 0; i < q; i++)
    {
        cin >> a >> b;
        ans += arr[b - 1] - arr[a - 2];
    }
    cout << ans;
    return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>

#define out freopen("output.txt", "w", stdout);
#define in freopen("input.txt", "r", stdin);
#define pub push_back
#define pob pop_back
#define infinity 1000000010
#define LL long long int
#define Pi acos(-1)
#define SZ 100010

///int col[8] = {0, 1, 1, 1, 0, -1, -1, -1};
///int row[8] = {1, 1, 0, -1, -1, -1, 0, 1};
///int col[4] = {1, 0, -1, 0};
///int row[4] = {0, 1, 0, -1};

using namespace std;

int arr[2 * SZ];
LL sum[2 * SZ];

bool comp(const int &a, const int &b)
{
    return a > b;
}

int main()
{
    int n, q, i, a, b;
    cin >> n >> q;
    LL ans = 0;
    for(i = 0; i < n; i++)
        cin >> arr[i];
    sort(arr, arr + n, comp);
    sort(arr, arr + (n / 2) + (n % 2));
    sum[0] = arr[0];
    for(i = 1; i < n; i++)
        {
            sum[i] = arr[i] + sum[i - 1];
        }
    for(i = 0; i < q; i++)
        {
            cin >> a >> b;
            ans += sum[b - 1] - sum[a - 2];
        }
    cout << ans << endl;
    return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>

#define out freopen("output.txt", "w", stdout);
#define in freopen("input.txt", "r", stdin);
#define pub push_back
#define pob pop_back
#define infinity 1000000010
#define LL long long int
#define Pi acos(-1)
#define SZ 100010

///int col[8] = {0, 1, 1, 1, 0, -1, -1, -1};
///int row[8] = {1, 1, 0, -1, -1, -1, 0, 1};
///int col[4] = {1, 0, -1, 0};
///int row[4] = {0, 1, 0, -1};

using namespace std;

LL arr[2 * SZ];
int tem[2 * SZ];

int main()
{
    int a, b, n, q, i;
    LL ans = 0;
    cin >> n >> q;
    for(i = 1; i <= n; i++)
        cin >> arr[i];
    for(i = 0; i < q; i++)
    {
        cin >> a >> b;
        tem[a]++;
        tem[b + 1]--;
    }
    for(i = 2; i <= n; i++)
        tem[i] += tem[i - 1];
    sort(tem, tem + n);
    sort(arr, arr + n);
    for(i = n; i > 0; i--)
        ans += (tem[i] * arr[i]);
    cout << ans;
    return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>

#define out freopen("output.txt", "w", stdout);
#define in freopen("input.txt", "r", stdin);
#define pub push_back
#define pob pop_back
#define infinity 1000000010
#define LL long long int
#define Pi acos(-1)
#define SZ 100010

///int col[8] = {0, 1, 1, 1, 0, -1, -1, -1};
///int row[8] = {1, 1, 0, -1, -1, -1, 0, 1};
///int col[4] = {1, 0, -1, 0};
///int row[4] = {0, 1, 0, -1};

using namespace std;

LL arr[2 * SZ];

bool comp(const LL &a, const LL &b)
{
    return a > b;
}

int main()
{
    int n, q, i, a, b;
    LL ans = 0;
    cin >> n >> q;
    for(i = 0; i < n; i++)
        cin >> arr[i];
    sort(arr, arr + n, comp);
    sort(arr, arr + (n / 2) + n % 2);
    for(i = 1; i < n; i++)
        arr[i] += arr[i - 1];
    for(i = 0; i < q; i++)
    {
        cin >> a >> b;
        ans += arr[b - 1] - arr[a - 2];
    }
    cout << ans;
    return 0;
}