#include <iostream>
#include <set>
#include <queue>
#include <vector>
#include <string>
#include <math.h>
#include <cmath>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <stack>
#include <algorithm>
#include <stdio.h>
#include <cstring>                    //  For memset
#include <sstream>
#include <numeric>

using namespace std;

#define lop(i,n) for(int i = 0 ; i < n ;i++)
#define VI    vector<int>
#define VLL    vector<ll>
#define VPI    vector< pair<int , int> >
#define VII    vector<VI>
#define all(v)    v.begin() , v.end()
#define SORT(v)    sort(all(v))
#define ll    long long
#define ull unsigned long long
#define scni(x)    scanf("%d",&x)                //  int
#define scnll(x)    scanf("%lld",&x)            //  ll
#define scnd(x)    scanf("%lf",&x)            //  double
#define prnti(x)    printf("%d\n",x)                //  int
#define prntll(x)    printf("%lld",x)                //  ll  NOTE :: lld <<for>> long long
#define prntd(x)    printf("%.9f",x)                //  int
#define sz(x)  x.size()
#define SPC  <<" "<<
#define pp push_back
#define P pair<int , int>
#define mp make_pair

const double EPS = 1e-9;
const double PI = 3.14159265359;

//  O( n * log(log(n)) )
vector<ll> primes;
void GeneratingPrimes(int top){
    vector<bool> p (top+1 , 1);
//    vector<ll> primes;
    p[0] = p[1] = 0;

    for(int i = 2; i <= sqrt(top) ; i++){
        if(p[i]){
            for(int j = i*i ; j <= top ; j+=i){
                p[j]=0;
            }
        }
    }
    lop(i , top+1)
        if(p[i])primes.push_back(i);
//    return primes;
}

ll gcd(ll a ,ll b){return (b==0)?a : gcd(b , a%b);}
ll lcm(ll a, ll b){return (a * b) / gcd(a , b);}

void EXTgcd(ll a , ll b , int & x0 , int & y0){
    if(b==0){ x0 =1 , y0 = 0; return; }
    EXTgcd( b , a%b , x0 , y0 );
    ll x1 = y0;
    ll y1 = x0 - (a/b) * y0;
    x0 = x1;
    y0 = y1;
}

long long power(long long n, long long p , ll mod) {
    if (!p)
        return 1;
    long long ans = 1;
    while (p > 1) {
        if (p & 1)
            ans = (ans * n) % mod;
        n = (n * n) % mod;
        p /= 2;
    }
    return (ans * n) % mod;
}

double dist(double a , double b , double x, double y){ return sqrt( pow( a-x ,2) + pow( b-y ,2) ); }

double dist3(double a , double b , double c , double x , double y ,double z ){ return sqrt( pow( a-x ,2) + pow( b-y ,2) + pow( c-z ,2)); }

int xadd[9]= { 1 , -1 , 0 , 1 , -1 , 0 , 1 , -1 , 0 };
int yadd[9]= { 1 , -1 , 0 ,-1 ,  0 , 1 , 0 ,  1, -1 };

int xx[4]={0 , 1 , 0 , -1};
int yy[4]={1 , 0 , -1 , 0};

#define no  { cout<<"NO\n"; return 0; }
#define yes { cout<<"YES\n";return 0; }
#define _1 { cout<<"-1\n";return 0;}

ll const N = 1e5 + 10;
ll const mod = 1e9 + 7;
ll const INF = 1e12;

///////////////////////////////////////////////////////////////////

ll n,m,k , ans;
vector<pair<int,int>> e[N];
map<int,ll> reach[N];
set<int> t;
ll minto[N];

ll go(int idx , int i){
	if(!t.count(idx)){reach[i][idx]=idx;return idx;}
	reach[i][idx] = go(idx+1 ,i);
	return reach[i][idx];
}

void dijkstra(){
	set<pair<ll , ll>> out;
	minto[1] = 0;
	out.insert(mp(0 , 1));

	while(sz(out)){
		pair<ll ,ll> p = *(--out.end());
		out.erase(--out.end());
		ll v = p.second , dtov = p.first;

		if(minto[v] != dtov)continue;

		for(auto x : e[v]){
			ll y =x.second + max(dtov , reach[v][dtov]);
			if(minto[x.first] > y){
				minto[x.first] = y;
				out.insert(mp(y , x.first));
			}
		}
	}
}

int main(){
	lop(i,N)minto[i] = INF;

	cin >> n >> m;
	lop(i,m){
		int x,y,c;
		scni(x),scni(y),scni(c);
		e[x].pp(mp(y , c));
		e[y].pp(mp(x , c));
	}

	for(int i=1 ; i<=n ; i++){
		scnll(k);
		lop(i,k){
			int x;
			scni(x);
			t.insert(x);
		}
		for(int a:t)if(!reach[i][a]){
			go(a , i);
		}
		t.clear();
	}

	dijkstra();
	if(minto[n]==INF)cout << -1; else cout << minto[n];
return 0;}


#include<stdio.h>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
#include<vector>
using namespace std;
const int maxn=110000;
struct edge
{
	int to,dis,next;
}e[maxn<<1];
int n,m,len,head[maxn],dis[maxn];bool f[maxn];
vector<int> g[maxn];
vector<int>::iterator item;
struct node
{
	int x,dis;
	node(int a,int b)
	{
		x=a;dis=b;
	}
	bool operator<(const node&b)const
	{
		return dis>b.dis;
	}
};
void insert(int x,int y,int z)
{
	len++;e[len].to=y;e[len].next=head[x];head[x]=len;e[len].dis=z;
}
void dj()
{
	priority_queue<node> q;
	memset(dis,0x7f,sizeof(dis));
	q.push(node(1,0));dis[1]=0;
	while(!q.empty())
	{
		int x=q.top().x;q.pop();
		if(f[x])continue;
		f[x]=1;
		for(int i=head[x];i;i=e[i].next)
		{
			int y=e[i].to;
			//printf("%d %d\n",x,y);
			int tt=0;

		//	for(item=g[x].begin();item!=g[x].end();item++)printf("bbb %d\n",*item);
			tt=binary_search(g[x].begin(),g[x].end(),dis[x]);
		//	printf("%d %d %d %d %d\n",x,y,dis[x],tt,dis[y]);
		//	if(item!=g[x].end())tt=1,printf("%d\n",*item);
		//	else printf("\n");
			if(dis[y]>dis[x]+e[i].dis+tt)
			{

				dis[y]=dis[x]+e[i].dis+tt;
	//			printf("aaa %d %d %d\n",x,y,dis[y]);
				q.push(node(y,dis[y]));
			}
		}
	}
//	for(int i=1;i<=n;++i)printf("a %d\n",dis[i]);
	if(dis[n]<0x7f7f7f7f)
	printf("%d\n",dis[n]);
	else printf("-1\n");
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1,x,y,z;i<=m;++i)
	{
		scanf("%d%d%d",&x,&y,&z);
		insert(x,y,z);insert(y,x,z);
	}
	for(int i=1,x;i<=n;++i)
	{
		scanf("%d",&x);
		for(int j=1,y;j<=x;++j)
		{
			scanf("%d",&y);
			g[i].push_back(y);
		}
	}
	dj();
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<set>
#include<queue>
#define N 1000005
#define INF 1000000000000007
#define M 1000005
#define LL long long
using namespace std;
LL nex[M],list[M],cost[M],head[N],cnt,dist[N],n,m,vis[N];
set<int>guest[N];
LL read()
{
    LL f=1,x=0;char s=getchar();
    while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
    while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
    return x*=f;
}
void insert(LL x,LL y,LL z)
{
	nex[++cnt]=head[x];
	list[cnt]=y;
	cost[cnt]=z;
	head[x]=cnt;
}
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > >q;
LL solve(LL distance,LL cur)
{
	while(guest[cur].find(distance)!=guest[cur].end())
	{
		distance++;
	}
	return distance;
}
void dij(LL start)
{
	for(LL i=1;i<=n;i++)
	{
		dist[i]=INF;
	}
	dist[start]=0;
//	vis[start]=1;
	q.push(make_pair(0,start));
	while(!q.empty())
	{
		LL cur=q.top().second;
		LL distance=solve(q.top().first,q.top().second);

//		printf("cur=%d distance=%d\n",cur,distance);

		q.pop();

		if(!vis[cur])
		{
			vis[cur]=1;
			for(LL i=head[cur];i;i=nex[i])
			{
				LL k=list[i];
				if(dist[k]>distance+cost[i])
				{
					dist[k]=distance+cost[i];
					q.push(make_pair(dist[k],k));
				}
			}
		}
	}
}
int main()
{
	cin>>n>>m;
	for(LL i=1;i<=m;i++)
	{
		LL x,y,z;
		x=read();y=read();z=read();
//		cin>>x>>y>>z;
		insert(x,y,z);
		insert(y,x,z);
	}
	for(LL i=1;i<=n;i++)
	{
		LL k;
		k=read();
		for(LL j=1;j<=k;j++)
		{
			LL tmp;
			tmp=read();
			guest[i].insert(tmp);
		}
	}
	dij(1);
	cout<<((vis[n])?(dist[n]):(-1))<<endl;
//	for(LL i=1;i<=n;i++)
//	{
//		printf("dist[%I64d]=%I64d\n",i,dist[i]);
//	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<set>
#include<queue>
#define N 200005
#define INF 100000007
#define M 300005
using namespace std;
int nex[M],list[M],cost[M],head[N],cnt,dist[N],n,m,vis[N];
set<int>guest[N];
void insert(int x,int y,int z)
{
	nex[++cnt]=head[x];
	list[cnt]=y;
	cost[cnt]=z;
	head[x]=cnt;
}
priority_queue<pair<int,int> >q;
int solve(int distance,int cur)
{
	while(guest[cur].find(distance)!=guest[cur].end())
	{
		distance++;
	}
	return distance;
}
void dij(int start)
{
	for(int i=1;i<=n;i++)
	{
		dist[i]=INF;
	}
	dist[start]=0;
//	vis[start]=1;
	q.push(make_pair(0,start));
	while(!q.empty())
	{
		int cur=q.top().second;
		int distance=solve(q.top().first,q.top().second);

//		printf("cur=%d distance=%d\n",cur,distance);

		q.pop();

		if(!vis[cur])
		{
			vis[cur]=1;
			for(int i=head[cur];i;i=nex[i])
			{
				int k=list[i];
				if(dist[k]>distance+cost[i])
				{
					dist[k]=distance+cost[i];
					q.push(make_pair(dist[k],k));
				}
			}
		}
	}
}
int main()
{
	cin>>n>>m;
	for(int i=1;i<=m;i++)
	{
		int x,y,z;
		scanf("%d%d%d",&x,&y,&z);
		insert(x,y,z);
		insert(y,x,z);
	}
	for(int i=1;i<=n;i++)
	{
		int k;
		scanf("%d",&k);
		for(int j=1;j<=k;j++)
		{
			int tmp;
			scanf("%d",&tmp);
			guest[i].insert(tmp);
		}
	}
	dij(1);
	cout<<((vis[n])?(dist[n]):(-1))<<endl;
//	for(int i=1;i<=n;i++)
//	{
//		printf("dist[%d]=%d\n",i,dist[i]);
//	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<set>
#include<queue>
#define N 200005
#define INF 10000000000007
#define M 300005
#define LL long long
using namespace std;
LL nex[M],list[M],cost[M],head[N],cnt,dist[N],n,m,vis[N];
set<int>guest[N];
void insert(LL x,LL y,LL z)
{
	nex[++cnt]=head[x];
	list[cnt]=y;
	cost[cnt]=z;
	head[x]=cnt;
}
priority_queue<pair<int,int> >q;
LL solve(LL distance,LL cur)
{
	while(guest[cur].find(distance)!=guest[cur].end())
	{
		distance++;
	}
	return distance;
}
void dij(LL start)
{
	for(LL i=1;i<=n;i++)
	{
		dist[i]=INF;
	}
	dist[start]=0;
//	vis[start]=1;
	q.push(make_pair(0,start));
	while(!q.empty())
	{
		LL cur=q.top().second;
		LL distance=solve(q.top().first,q.top().second);

//		printf("cur=%d distance=%d\n",cur,distance);

		q.pop();

		if(!vis[cur])
		{
			vis[cur]=1;
			for(LL i=head[cur];i;i=nex[i])
			{
				LL k=list[i];
				if(dist[k]>distance+cost[i])
				{
					dist[k]=distance+cost[i];
					q.push(make_pair(dist[k],k));
				}
			}
		}
	}
}
int main()
{
	cin>>n>>m;
	for(LL i=1;i<=m;i++)
	{
		LL x,y,z;
		scanf("%lld%lld%lld",&x,&y,&z);
		insert(x,y,z);
		insert(y,x,z);
	}
	for(LL i=1;i<=n;i++)
	{
		LL k;
		scanf("%lld",&k);
		for(LL j=1;j<=k;j++)
		{
			LL tmp;
			scanf("%lld",&tmp);
			guest[i].insert(tmp);
		}
	}
	dij(1);
	cout<<((vis[n])?(dist[n]):(-1))<<endl;
//	for(LL i=1;i<=n;i++)
//	{
//		printf("dist[%d]=%d\n",i,dist[i]);
//	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define gc getchar_unlocked
#define fo(i,n) for(i=0;i<n;i++)
#define Fo(i,k,n) for(i=k;i<n;i++)
#define ll long long
#define si(x)	scanf("%d",&x)
#define sl(x)	scanf("%I64d",&x)
#define ss(s)	scanf("%s",s)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define clr(x) memset(x, 0, sizeof(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
typedef pair<int, int>	pii;
typedef pair<ll, ll>	pll;
typedef vector<int>		vi;
typedef vector<ll>		vl;
typedef vector<pii>		vpii;
typedef vector<pll>		vpll;
typedef vector<vi>		vvi;
typedef vector<vl>		vvl;
const int mod = 1000000007;
const int N = 3e5+3;
vpii g[N];
int a[N], n;
ll dis[N];
const ll inf = 1e18;
struct node{
	ll v, w;
	friend bool operator < (node n1, node n2)
    {
    	if (n1.w != n2.w) return n1.w<n2.w;
    	return n1.v<n2.v;
    }
};
set<int> times[N];
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int i, pos = 0, n, m, t,u,v,w;
	//input
	cin>>n>>m;
	fo(i, m){
		cin>>u>>v>>w;
		g[u-1].pb({v-1, w});
		g[v-1].pb({u-1, w});
	}

	fo(i, n){
		cin>>m;
		while(m--){
			cin>>t;
			times[i].insert(t);
		}
	}
	set<node> G, ans;
	G.insert({0, 0});
	Fo(i, 1, n) G.insert({i, inf}), dis[i] = inf;
	dis[0] = 0;
	while(!G.empty()){
		node cr = *G.begin(); G.erase(G.begin());
		if (cr.v == n-1 or cr.w == inf) break;
		auto it = times[cr.v].lower_bound(cr.w);
		while (it != times[cr.v].end() and cr.w == *it) it++, cr.w++;
		for(pii v: g[cr.v]){
			int to = v.F, wt = v.S;
			// cout<<1+to<<","<<wt<<endl;
			auto it = G.find({to, dis[to]});
			// cout<<1+(*it).v<<" "<<(*it).w<<endl;
			if (it != G.end()){
				node tem = *it;
				G.erase(it);
				tem.w = min(tem.w, wt+cr.w);
				dis[tem.v] = tem.w;
				G.insert(tem);
			}
		}
	}

	ll res = dis[n-1];
	if (res == inf)cout<<-1<<endl;
	else cout<<res<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pll pair<ll,ll>
#define fr first
#define sc second

using namespace std;

const ll N = 100100;

ll k[N];
bool Vis[N];
vector <pll> Adj[N];
vector <ll> Trv[N];
vector <ll> Nxt[N];
priority_queue < pll , vector <pll> , greater <pll> > q;

ll Wait(ll i,ll At)
{
    ll r = lower_bound( Trv[i].begin() , Trv[i].end() , At ) - Trv[i].begin();

    if( r == k[i] || Trv[i][r] > At ) return At;

    return Nxt[i][r];
}

int main()
{
    ll n,m; cin>>n>>m;

    while(m--)
    {
        ll x,y,w;   scanf("%I64d%I64d%I64d",&x,&y,&w);

        Adj[x].push_back(pll(y,w));
        Adj[y].push_back(pll(x,w));
    }

    for(ll i=1;i<=n;i++)
    {
        scanf("%I64d",k+i);
        Trv[i].resize(k[i]);
        for(auto& x:Trv[i]) scanf("%I64d",&x);
    }

    for(ll i=1;i<=n;i++)
        if(k[i]>0)
        {
            Nxt[i].resize(k[i]);
            Nxt[i][k[i]-1] = Trv[i][k[i]-1] + 1;

            for(ll j=k[i]-2;j>=0;j--)
                if( Trv[i][j] + 1 == Trv[i][j+1] )
                    Nxt[i][j] = Nxt[i][j+1];
                else
                    Nxt[i][j] = Trv[i][j] + 1;
        }

    q.push(pll(0,1));

    while(!q.empty())
    {
        ll Node = q.top().sc;
        ll Dist = q.top().fr;

        q.pop();

        if(Vis[Node])   continue;
        Vis[Node] = true;

        if(Node==n) return cout<<Dist,0;

        for(auto x:Adj[Node])
            if(!Vis[x.fr])
                q.push(pll(Wait(x.fr,Dist+x.sc),x.fr));
    }

    puts("-1");
}


#include <bits/stdc++.h>
#define ll long long
#define pll pair<ll,ll>
#define fr first
#define sc second

using namespace std;

const ll N = 100100;

ll n,m;
ll k[N];
bool Vis[N];
vector <pll> Adj[N];
vector <ll> Trv[N];
vector <ll> Nxt[N];
priority_queue < pll , vector <pll> , greater <pll> > q;

ll Wait(ll i,ll At)
{
    if(i==n)    return At;

    ll r = lower_bound( Trv[i].begin() , Trv[i].end() , At ) - Trv[i].begin();

    if( r == k[i] || Trv[i][r] > At ) return At;

    return Nxt[i][r];
}

int main()
{
    cin>>n>>m;

    while(m--)
    {
        ll x,y,w;   scanf("%I64d%I64d%I64d",&x,&y,&w);

        Adj[x].push_back(pll(y,w));
        Adj[y].push_back(pll(x,w));
    }

    for(ll i=1;i<=n;i++)
    {
        scanf("%I64d",k+i);
        Trv[i].resize(k[i]);
        for(auto& x:Trv[i]) scanf("%I64d",&x);
    }

    for(ll i=1;i<=n;i++)
        if(k[i]>0)
        {
            Nxt[i].resize(k[i]);
            Nxt[i][k[i]-1] = Trv[i][k[i]-1] + 1;

            for(ll j=k[i]-2;j>=0;j--)
                if( Trv[i][j] + 1 == Trv[i][j+1] )
                    Nxt[i][j] = Nxt[i][j+1];
                else
                    Nxt[i][j] = Trv[i][j] + 1;
        }

    q.push(pll(Wait(1,0),1));

    while(!q.empty())
    {
        ll Node = q.top().sc;
        ll Dist = q.top().fr;

        q.pop();

        if(Vis[Node])   continue;
        Vis[Node] = true;

        if(Node==n) return cout<<Dist,0;

        for(auto x:Adj[Node])
            if(!Vis[x.fr])
                q.push(pll(Wait(x.fr,Dist+x.sc),x.fr));
    }

    puts("-1");
}


#include <bits/stdc++.h>
#define ll long long
#define pll pair<ll,ll>
#define fr first
#define sc second

using namespace std;

const ll N = 100100;

ll k[N];
bool Vis[N];
vector <pll> Adj[N];
vector <ll> Trv[N];
vector <ll> Nxt[N];
priority_queue < pll , vector <pll> , greater <pll> > q;

ll Wait(ll i,ll At)
{
    ll r = lower_bound( Trv[i].begin() , Trv[i].end() , At ) - Trv[i].begin();

    if( r == k[i] || Trv[i][r] > At ) return At;

    return Nxt[i][r];
}

int main()
{
    ll n,m; cin>>n>>m;

    while(m--)
    {
        ll x,y,w;   scanf("%I64d%I64d%I64d",&x,&y,&w);

        Adj[x].push_back(pll(y,w));
        Adj[y].push_back(pll(x,w));
    }

    for(ll i=1;i<=n;i++)
    {
        scanf("%I64d",k+i);
        Trv[i].resize(k[i]);
        for(auto& x:Trv[i]) scanf("%I64d",&x);
    }

    for(ll i=1;i<=n;i++)
        if(k[i]>0)
        {
            Nxt[i].resize(k[i]);
            Nxt[i][k[i]-1] = Trv[i][k[i]-1] + 1;

            for(ll j=k[i]-2;j>=0;j--)
                if( Trv[i][j] + 1 == Trv[i][j+1] )
                    Nxt[i][j] = Nxt[i][j+1];
                else
                    Nxt[i][j] = Trv[i][j] + 1;
        }

    q.push(pll(Wait(1,0),1));

    while(!q.empty())
    {
        ll Node = q.top().sc;
        ll Dist = q.top().fr;

        q.pop();

        if(Vis[Node])   continue;
        Vis[Node] = true;

        if(Node==n) return cout<<Dist,0;

        for(auto x:Adj[Node])
            if(!Vis[x.fr])
                q.push(pll(Wait(x.fr,Dist+x.sc),x.fr));
    }

    puts("-1");
}


#include<bits/stdc++.h>

using namespace std;

#define MAX 100000 + 5
#define endl '\n'

typedef pair<int,int> pii;
typedef long long ll;

ll n,m;
const ll oo = 100000000000000LL;
vector<pii> G[MAX];
vector<ll> A[MAX];
vector<bool> flags(MAX);

vector<ll> dijkstra(ll node){
	vector<ll> d(n + 1);
	for(ll i = 1; i <= n; i++)
		d[i] = oo;

	d[node] = 0LL;
	priority_queue<pii,vector<pii>,greater<pii> > Q;
	Q.push({0LL,node});

	while(!Q.empty()){
		ll x = Q.top().second;
		ll ww = Q.top().first;
		Q.pop();
	///	cerr << x << " " << ww << endl;
		if(flags[x])continue;
		flags[x] = true;

		ll pos = lower_bound(A[x].begin(),A[x].end(),ww) - A[x].begin();
		ll inc = 0LL;
		while(pos < (ll)A[x].size() && A[x][pos] == ww + inc){
			inc++;
			pos++;
		}

		for(auto ad : G[x]){
			ll y = ad.second;
			ll w = ad.first;
		//	cerr << y << " " << w << endl;

			if(d[y] > ww + inc + w){
				d[y] = ww + inc + w;
				Q.push({d[y],y});
			}
		}
	}

	return d;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	cin >> n >> m;
	for(int i = 0; i < m; i++){
		ll a,b,c;
		cin >> a >> b >> c;
		G[a].push_back({c,b});
		G[b].push_back({c,a});
	}
	for(int i = 1; i <= n; i++){
		int k;
		cin >> k;
		for(int j = 0; j < k; j++){
			ll x;
			cin >> x;
			A[i].push_back(x);
		}
	}

	vector<ll> d = dijkstra(1);
	if(d[n] == oo)cout << -1 << endl;
	else cout << d[n] << endl;

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define MAX 100000 + 5
#define endl '\n'

typedef pair<int,int> pii;
typedef long long ll;

int n,m;
const ll oo = 1000000000000LL;
vector<pii> G[MAX];
vector<ll> A[MAX];

vector<ll> dijkstra(ll node){
	vector<ll> d(n + 1);
	for(int i = 1; i <= n; i++)
		d[i] = oo;
	d[node] = 0LL;
	priority_queue<pii,vector<pii>,greater<pii> > Q;
	Q.push({0,node});

	while(!Q.empty()){
		ll x = Q.top().second;
		ll ww = Q.top().first;
		Q.pop();
	///	cerr << x << " " << ww << endl;

		for(auto ad : G[x]){
			ll y = ad.second;
			ll w = ad.first;
		//	cerr << y << " " << w << endl;
			ll pos = lower_bound(A[x].begin(),A[x].end(),ww) - A[x].begin();
		//	cerr << pos << endl;
			ll inc = 0LL;
			if(A[x].size() == 0)inc = 0;
			else if(A[x][pos] != ww)inc = 0;
			else{
				ll prev = ww - 1;
				ll cur = ww;
				for(ll i = pos + 1; i < A[x].size() && cur - prev == 1; i++){
					cur = A[x][i];
					prev = A[x][i - 1];
				}
				if(cur - prev == 1)cur++;
				inc = cur - ww;
			}
			if(d[y] > d[x] + inc + w){
				d[y] = d[x] + inc + w;
				Q.push({d[y],y});
			}
		}
	}

	return d;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	cin >> n >> m;
	for(int i = 0; i < m; i++){
		ll a,b,c;
		cin >> a >> b >> c;
		G[a].push_back({c,b});
		G[b].push_back({c,a});
	}
	for(int i = 1; i <= n; i++){
		int k;
		cin >> k;
		for(int j = 0; j < k; j++){
			ll x;
			cin >> x;
			A[i].push_back(x);
		}
	}

	vector<ll> d = dijkstra(1);
	if(d[n] == oo)cout << -1 << endl;
	else cout << d[n] << endl;

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define MAX 100000 + 5
#define endl '\n'

typedef pair<int,int> pii;
typedef long long ll;

int n,m;
const ll oo = 1000000000000LL;
vector<pii> G[MAX];
vector<ll> A[MAX];

vector<ll> dijkstra(ll node){
	vector<ll> d(n + 1);
	for(int i = 1; i <= n; i++)
		d[i] = oo;
	d[node] = 0LL;
	priority_queue<pii,vector<pii>,greater<pii> > Q;
	Q.push({0,node});

	while(!Q.empty()){
		ll x = Q.top().second;
		ll ww = Q.top().first;
		Q.pop();
	///	cerr << x << " " << ww << endl;
		ll pos = lower_bound(A[x].begin(),A[x].end(),ww) - A[x].begin();
		ll inc = 0LL;
		while(pos < (ll)A[x].size() && A[x][pos] == ww + inc){
			inc++;
			pos++;
		}

		for(auto ad : G[x]){
			ll y = ad.second;
			ll w = ad.first;
		//	cerr << y << " " << w << endl;

			if(d[y] > d[x] + inc + w){
				d[y] = d[x] + inc + w;
				Q.push({d[y],y});
			}
		}
	}

	return d;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	cin >> n >> m;
	for(int i = 0; i < m; i++){
		ll a,b,c;
		cin >> a >> b >> c;
		G[a].push_back({c,b});
		G[b].push_back({c,a});
	}
	for(int i = 1; i <= n; i++){
		int k;
		cin >> k;
		for(int j = 0; j < k; j++){
			ll x;
			cin >> x;
			A[i].push_back(x);
		}
	}

	vector<ll> d = dijkstra(1);
	if(d[n] == oo)cout << -1 << endl;
	else cout << d[n] << endl;

	return 0;
}


#include <bits/stdc++.h>
#define ff first
#define ss second
#define maxn 100009
#define ll long long
#define pb push_back
#define pp pop_back()
#define inf 1000000009
#define pii pair <int , int>
#define all(x) x.begin(),x.end()
#define llinf 1000000000000000009
#define pll pair <long long , long long>
#define __lcm(x, y) (x*y) / __gcd (x, y);
#define tr(i,c) for(__typeof(c).begin() i = (c).begin() ; i != (c).end() ; i++)
using namespace std;

int n , m , a , b , c , k , sm;
int mn[maxn] , r[maxn];
map <ll , int> mp[maxn];
priority_queue< pair<ll , ll> , vector< pair<ll , ll> > , greater< pair<ll , ll> > > q;
vector<pii> v[maxn];

int bfs()
{

	while(!q.empty())
	{
		int aa = q.top().ss;
		ll bb = q.top().ff;
		for(int i = 0 ; i < v[aa].size(); i ++)
		{
			ll sum = bb + v[aa][i].ss + mp[aa][mn[aa]];

			if(sum < mn[v[aa][i].ff])
			{
				mn[v[aa][i].ff] = sum;
				q.push({sum , v[aa][i].ff});
			}
		}
		q.pop();
	}
}

int main()
{

 	//freopen("input.in" , "r" , stdin);
 	//freopen("output.out" , "w" , stdout);

	scanf("%d%d" , &n , &m);

	for(int i = 2 ; i <= n; i ++)
		mn[i] = inf;

	for(int i = 1; i <= m; i ++)
	{
		scanf("%d%d%d" , &a , &b , &c);
		v[a].pb({b , c});
		v[b].pb({a , c});
	}

	for(int i = 1 ; i <= n; i ++)
	{
		scanf("%d" , &k);

		for(int j = 1 ; j <= k ; j ++)
			scanf("%d" , r + j);

		if(k)
			mp[i][r[k]] = 1 , sm = 1;

		for(int j = k - 1 ; j >= 1 ; j --)
		{
			if(r[j] + 1 == r[j + 1])
				mp[i][r[j]] = ++ sm;
			else
				sm = 0 , mp[i][r[j]] = ++ sm;
		}
	}

	q.push({0 , 1});
	bfs();
	cout << (mn[n] != inf ? mn[n] : -1);
	return 0;
}
/*
7 7
1 2 1
2 4 2
2 3 2
3 6 2
6 5 2
4 5 3
5 7 7
0
0
0
3 3 4 5
0
0
0
output
16
answer
14
*/


#include <bits/stdc++.h>
#define ff first
#define ss second
#define maxn 100009
#define ll long long
#define pb push_back
#define pp pop_back()
#define inf 1000000009
#define pii pair <int , int>
#define all(x) x.begin(),x.end()
#define llinf 1000000000000000009
#define pll pair <long long , long long>
#define __lcm(x, y) (x*y) / __gcd (x, y);
#define tr(i,c) for(__typeof(c).begin() i = (c).begin() ; i != (c).end() ; i++)
using namespace std;

int n , m , a , b , c , k , sm;
int mn[maxn] , r[maxn];
map <ll , int> mp[maxn];
priority_queue< pair<ll , ll> , vector< pair<ll , ll> > , greater< pair<ll , ll> > > q;
vector<pii> v[maxn];

int bfs()
{

	while(!q.empty())
	{
		int aa = q.top().ff;
		ll bb = q.top().ss;
		for(int i = 0 ; i < v[aa].size(); i ++)
		{
			ll sum = bb + v[aa][i].ss + mp[aa][mn[aa]];

			if(sum < mn[v[aa][i].ff])
			{
				mn[v[aa][i].ff] = sum;
				q.push({v[aa][i].ff , sum});
			}
		}
		q.pop();
	}
}

int main()
{

 	//freopen("input.in" , "r" , stdin);
 	//freopen("output.out" , "w" , stdout);

	scanf("%d%d" , &n , &m);

	for(int i = 2 ; i <= n; i ++)
		mn[i] = inf;

	for(int i = 1; i <= m; i ++)
	{
		scanf("%d%d%d" , &a , &b , &c);
		v[a].pb({b , c});
		v[b].pb({a , c});
	}

	for(int i = 1 ; i <= n; i ++)
	{
		scanf("%d" , &k);

		for(int j = 1 ; j <= k ; j ++)
			scanf("%d" , r + j);

		if(k)
			mp[i][r[k]] = 1 , sm = 1;

		for(int j = k - 1 ; j >= 1 ; j --)
		{
			if(r[j] + 1 == r[j + 1])
				mp[i][r[j]] = ++ sm;
			else
				sm = 0 , mp[i][r[j]] = ++ sm;
		}
	}

	q.push({1 , 0});
	bfs();
	cout << (mn[n] != inf ? mn[n] : -1);
	return 0;
}


#include <bits/stdc++.h>
#define ff first
#define ss second
#define maxn 100009
#define ll long long
#define pb push_back
#define pp pop_back()
#define inf 1000099999
#define pii pair <int , int>
#define all(x) x.begin(),x.end()
#define llinf 1000000000000000009
#define pll pair <long long , long long>
#define __lcm(x, y) (x*y) / __gcd (x, y);
#define tr(i,c) for(__typeof(c).begin() i = (c).begin() ; i != (c).end() ; i++)
using namespace std;

int n , m , a , b , c , k , sm;
int mn[maxn] , r[maxn];
map <ll , int> mp[maxn];
priority_queue< pair<ll , ll> , vector< pair<ll , ll> > , greater< pair<ll , ll> > > q;
vector<pii> v[maxn];

int bfs()
{

	while(!q.empty())
	{
		int aa = q.top().ss;
		ll bb = q.top().ff;
		for(int i = 0 ; i < v[aa].size(); i ++)
		{
			ll sum = bb + v[aa][i].ss + mp[aa][mn[aa]];

			if(sum < mn[v[aa][i].ff])
			{
				mn[v[aa][i].ff] = sum;
				q.push({sum , v[aa][i].ff});
			}
		}
		q.pop();
	}
}

int main()
{

 	//freopen("input.in" , "r" , stdin);
 	//freopen("output.out" , "w" , stdout);

	scanf("%d%d" , &n , &m);

	for(int i = 2 ; i <= n; i ++)
		mn[i] = inf;

	for(int i = 1; i <= m; i ++)
	{
		scanf("%d%d%d" , &a , &b , &c);
		v[a].pb({b , c});
		v[b].pb({a , c});
	}

	for(int i = 1 ; i <= n; i ++)
	{
		scanf("%d" , &k);

		for(int j = 1 ; j <= k ; j ++)
			scanf("%d" , r + j);

		if(k)
			mp[i][r[k]] = 1 , sm = 1;

		for(int j = k - 1 ; j >= 1 ; j --)
		{
			if(r[j] + 1 == r[j + 1])
				mp[i][r[j]] = ++ sm;
			else
				sm = 0 , mp[i][r[j]] = ++ sm;
		}
	}

	q.push({0 , 1});
	bfs();
	cout << (mn[n] != inf ? mn[n] : -1);
	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#define MX 100001
#define INF 1000000017
#define LL long long
#define edge pair <LL, int>
#define mp(x, y) make_pair(x, y)

using namespace std;

vector <edge> g[MX];
set <edge> q;
set <int> time[MX];
LL dist[MX];
int n, m, a, b, c, k, t;

int main()
{
	ios_base::sync_with_stdio(false);
	cin >> n >> m;
	while (m--)
	{
		cin >> a >> b >> c;
		g[a].push_back(mp(c, b));
		g[b].push_back(mp(c, a));
	}
	for (int i(1); i <= n; ++i)
	{
		cin >> k;
		while (k--)
		{
			cin >> t;
			time[i].insert(t);
		}
	}
	for (int i(0); i <= n; ++i)
		dist[i] = INF;
	q.insert(mp(0LL, 1));
	edge e;
	while (!q.empty())
	{
		e = *q.begin();
		q.erase(q.begin());
		if (e.first < dist[e.second])
		{
			dist[e.second] = e.first;
			while (time[e.second].count(e.first)) e.first++;
			for (int i(0); i < g[e.second].size(); ++i)
			{
				if (e.first + g[e.second][i].first < dist[g[e.second][i].second])
					q.insert(mp(e.first + g[e.second][i].first, g[e.second][i].second));
			}
		}
	}
	if (dist[n] == INF) cout << -1;
	else cout << dist[n];
	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#define MX 100001
#define INF 10000000017
#define LL long long
#define edge pair <LL, int>
#define mp(x, y) make_pair(x, y)

using namespace std;

vector <edge> g[MX];
set <edge> q;
set <int> time[MX];
LL dist[MX];
int n, m, a, b, c, k, t;

int main()
{
	ios_base::sync_with_stdio(false);
	cin >> n >> m;
	while (m--)
	{
		cin >> a >> b >> c;
		g[a].push_back(mp(c, b));
		g[b].push_back(mp(c, a));
	}
	for (int i(1); i <= n; ++i)
	{
		cin >> k;
		while (k--)
		{
			cin >> t;
			time[i].insert(t);
		}
	}
	for (int i(0); i <= n; ++i)
		dist[i] = INF;
	q.insert(mp(0LL, 1));
	edge e;
	while (!q.empty())
	{
		e = *q.begin();
		q.erase(q.begin());
		if (e.first < dist[e.second])
		{
			dist[e.second] = e.first;
			while (time[e.second].count(e.first)) e.first++;
			for (int i(0); i < g[e.second].size(); ++i)
			{
				if (e.first + g[e.second][i].first < dist[g[e.second][i].second])
					q.insert(mp(e.first + g[e.second][i].first, g[e.second][i].second));
			}
		}
	}
	if (dist[n] == INF) cout << -1;
	else cout << dist[n];
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
main()
{
    int n,q,s=0;
    cin>>n>>q;
    int a[n],f[n]={0};
    for(int i=0;i<n;i++)cin>>a[i];
    while(q--)
    {
        int l,r;
        cin>>l>>r;
        for(int i=l-1;i<r;i++)
            f[i]++;
    }
    sort(f,f+n);
    sort(a,a+n);
    for(int i=0;i<n;i++)
    {
        s=s+(a[i]*f[i]);
    }
    cout<<s<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define int unsigned long long int
main()
{
    int n,q,s=0;
    cin>>n>>q;
    int a[n],f[n]={0};
    for(int i=0;i<n;i++)cin>>a[i];
    while(q--)
    {
        int l,r;
        cin>>l>>r;
        for(int i=l-1;i<r;i++)
            f[i]++;
    }
    sort(f,f+n);
    sort(a,a+n);
    for(int i=0;i<n;i++)
    {
        s=s+(a[i]*f[i]);
    }
    cout<<s<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define int unsigned long long int
main()
{
    int n,q,s=0;
    cin>>n>>q;
    int a[n],f[100009]={0};
    for(int i=0;i<n;i++)cin>>a[i];
    while(q--)
    {
        int l,r;
        cin>>l>>r;
   //     for(int i=l-1;i<r;i++)
            f[l-1]++;
            f[r]--;
    }
    for(int i=1;i<n;i++)f[i]+=f[i-1];
    sort(f,f+n);
    sort(a,a+n);
    for(int i=0;i<n;i++)
    {
        s=s+(a[i]*f[i]*1ll);
    }
    cout<<s<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long int
main()
{
    int n,q,s=0;
    cin>>n>>q;
    int a[n],f[200009]={0};
    for(int i=0;i<n;i++)cin>>a[i];
    while(q--)
    {
        int l,r;
        cin>>l>>r;
   //     for(int i=l-1;i<r;i++)
            f[l-1]++;
            f[r]--;
    }
    for(int i=1;i<n;i++)f[i]+=f[i-1];
    sort(f,f+n);
    sort(a,a+n);
    for(int i=0;i<n;i++)
    {
        s=s+(a[i]*f[i]*1ll);
    }
    cout<<s<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long int
main()
{
    int n,q,s=0;
    cin>>n>>q;
    int a[n],f[n]={0};
    for(int i=0;i<n;i++)cin>>a[i];
    while(q--)
    {
        int l,r;
        cin>>l>>r;
        for(int i=l-1;i<r;i++)
            f[i]++;
    }
    sort(f,f+n);
    sort(a,a+n);
    for(int i=0;i<n;i++)
    {
        s=s+(a[i]*f[i]);
    }
    cout<<s<<endl;
}


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

int main(){
   int n,q;
   cin>>n>>q;
   vector<int> arr(n,0);
   for(int i=0;i<n;i++){
       cin>>arr[i];
   }

   vector<int> freq (n+1,0);

   for(int i=0;i<q;i++){
    int left,right;
    cin>>left>>right;
    left--,right--;

    freq[left]++;
    freq[right+1]--;
   }

   int sum=0;
   //Cumulating the frequency array
   for(int i=0;i<n+1;i++){
       freq[i]+=sum;
       sum=freq[i];
   }

   sort(freq.begin(),freq.end());
   sort(arr.begin(),arr.end());

   long long ans=0;

   for(int i=0;i<n;i++){
       ans+=arr[i]*freq[i+1];
   }

   cout<<ans;
   return 0;
}


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

int main(){
   int n,q;
   cin>>n>>q;
   vector<int> arr(n,0);
   for(int i=0;i<n;i++){
       cin>>arr[i];
   }

   vector<int> freq (n+1,0);

   for(int i=0;i<q;i++){
    int left,right;
    cin>>left>>right;
    left--,right--;

    freq[left]++;
    freq[right+1]--;
   }

   int sum=0;
   //Cumulating the frequency array
   for(int i=0;i<n+1;i++){
       freq[i]+=sum;
       sum=freq[i];
   }

   sort(freq.begin(),freq.end());
   sort(arr.begin(),arr.end());

   long unsigned int ans=0;

   for(int i=0;i<n;i++){
       ans+=arr[i]*freq[i+1];
   }

   cout<<ans;
   return 0;
}


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

int main(){
   int n,q;
   cin>>n>>q;
   vector<int> arr(n,0);
   for(int i=0;i<n;i++){
       cin>>arr[i];
   }

   vector<int> freq (n,0);

   for(int i=0;i<q;i++){
    int left,right;
    cin>>left>>right;
    left--,right--;

    freq[left]++;
    freq[right]--;
   }

   int sum=0;
   //Cumulating the frequency array
   for(int i=0;i<n;i++){
       freq[i]+=sum;
       sum=freq[i];
   }

   sort(freq.begin(),freq.end());
   sort(arr.begin(),arr.end());

   int ans=0;

   for(int i=0;i<n;i++){
       ans+=arr[i]*freq[i];
   }

   cout<<ans;
   return 0;
}


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

int main(){
   int n,q;
   cin>>n>>q;
   vector<int> arr(n,0);
   for(int i=0;i<n;i++){
       cin>>arr[i];
   }

   vector<int> freq (n+1,0);

   for(int i=0;i<q;i++){
    int left,right;
    cin>>left>>right;
    left--,right--;

    freq[left]++;
    freq[right+1]--;
   }

   int sum=0;
   //Cumulating the frequency array
   for(int i=0;i<n+1;i++){
       freq[i]+=sum;
       sum=freq[i];
   }

   sort(freq.begin(),freq.end());
   sort(arr.begin(),arr.end());

   unsigned long long int ans=0;

   for(int i=0;i<n;i++){
       ans+=arr[i]*freq[i+1];
   }

   cout<<ans;
   return 0;
}


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

int main(){
   int n,q;
   cin>>n>>q;
   vector<int> arr(n,0);
   for(int i=0;i<n;i++){
       cin>>arr[i];
   }

   vector<int> freq (n+1,0);

   for(int i=0;i<q;i++){
    int left,right;
    cin>>left>>right;
    left--,right--;

    freq[left]++;
    freq[right+1]--;
   }

   int sum=0;
   //Cumulating the frequency array
   for(int i=0;i<n+1;i++){
       freq[i]+=sum;
       sum=freq[i];
   }

   sort(freq.begin(),freq.end());
   sort(arr.begin(),arr.end());

   unsigned long long ans=0;

   for(int i=0;i<n;i++){
       ans+=(long long)(arr[i]*freq[i+1]);
   }

   cout<<ans;
   return 0;
}


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

int main(){
   int n,q;
   cin>>n>>q;
   vector<int> arr(n,0);
   for(int i=0;i<n;i++){
       cin>>arr[i];
   }

   vector<int> freq (n+1,0);

   for(int i=0;i<q;i++){
    int left,right;
    cin>>left>>right;
    left--,right--;

    freq[left]++;
    freq[right+1]--;
   }

   int sum=0;
   //Cumulating the frequency array
   for(int i=0;i<n+1;i++){
       freq[i]+=sum;
       sum=freq[i];
   }

   sort(freq.begin(),freq.end());
   sort(arr.begin(),arr.end());

   long int ans=0;

   for(int i=0;i<n;i++){
       ans+=arr[i]*freq[i+1];
   }

   cout<<ans;
   return 0;
}


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

int main(){
   int n,q;
   cin>>n>>q;
   vector<int> arr(n,0);
   for(int i=0;i<n;i++){
       cin>>arr[i];
   }

   vector<int> freq (n+1,0);

   for(int i=0;i<q;i++){
    int left,right;
    cin>>left>>right;
    left--,right--;

    freq[left]++;
    freq[right+1]--;
   }

   int sum=0;
   //Cumulating the frequency array
   for(int i=0;i<n+1;i++){
       freq[i]+=sum;
       sum=freq[i];
   }

   sort(freq.begin(),freq.end());
   sort(arr.begin(),arr.end());

  unsigned long long ans=0;

   for(int i=0;i<n;i++){
       ans+=((long)arr[i]*(long)freq[i+1]);
   }

   cout<<ans;
   return 0;
}


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

int main(){
   int n,q;
   cin>>n>>q;
   vector<int> arr(n,0);
   for(int i=0;i<n;i++){
       cin>>arr[i];
   }

   vector<int> freq (n+1,0);

   for(int i=0;i<q;i++){
    int left,right;
    cin>>left>>right;
    left--,right--;

    freq[left]++;
    freq[right+1]--;
   }

   int sum=0;
   //Cumulating the frequency array
   for(int i=0;i<n+1;i++){
       freq[i]+=sum;
       sum=freq[i];
   }

   sort(freq.begin(),freq.end());
   sort(arr.begin(),arr.end());

   unsigned long long ans=0;

   for(int i=0;i<n;i++){
       ans+=arr[i]*freq[i+1];
   }

   cout<<ans;
   return 0;
}


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

void main(){
   int n,q;
   cin>>n>>q;
   vector<int> arr(n,0);
   for(int i=0;i<n;i++){
       cin>>arr[i];
   }

   vector<int> freq (n,0);

   for(int i=0;i<q;i++){
    int left,right;
    cin>>left>>right;
    left--,right--;

    freq[left]++;
    freq[right]--;
   }

   int sum=0;
   //Cumulating the frequency array
   for(int i=0;i<n;i++){
       freq[i]+=sum;
       sum=freq[i];
   }

   sort(freq.begin(),freq.end());
   sort(arr.begin(),arr.end());

   int ans=0;

   for(int i=0;i<n;i++){
       ans+=arr[i]*freq[i];
   }

   cout<<ans;
}


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

int main(){
   int n,q;
   cin>>n>>q;
   vector<int> arr(n,0);
   for(int i=0;i<n;i++){
       cin>>arr[i];
   }

   vector<int> freq (n+1,0);

   for(int i=0;i<q;i++){
    int left,right;
    cin>>left>>right;
    left--,right--;

    freq[left]++;
    freq[right+1]--;
   }

   int sum=0;
   //Cumulating the frequency array
   for(int i=0;i<n+1;i++){
       freq[i]+=sum;
       sum=freq[i];
   }

   sort(freq.begin(),freq.end());
   sort(arr.begin(),arr.end());

   unsigned long ans=0;

   for(int i=0;i<n;i++){
       ans+=arr[i]*freq[i+1];
   }

   cout<<ans;
   return 0;
}


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

int main(){
   int n,q;
   cin>>n>>q;
   vector<int> arr(n,0);
   for(int i=0;i<n;i++){
       cin>>arr[i];
   }

   vector<int> freq (n+1,0);

   for(int i=0;i<q;i++){
    int left,right;
    cin>>left>>right;
    left--,right--;

    freq[left]++;
    freq[right+1]--;
   }

   int sum=0;
   //Cumulating the frequency array
   for(int i=0;i<n+1;i++){
       freq[i]+=sum;
       sum=freq[i];
   }

   sort(freq.begin(),freq.end());
   sort(arr.begin(),arr.end());

   signed long long ans=0;

   for(int i=0;i<n;i++){
       ans+=arr[i]*freq[i+1];
   }

   cout<<ans;
   return 0;
}


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

int main(){
   int n,q;
   cin>>n>>q;
   vector<int> arr(n,0);
   for(int i=0;i<n;i++){
       cin>>arr[i];
   }

   vector<int> freq (n+1,0);

   for(int i=0;i<q;i++){
    int left,right;
    cin>>left>>right;
    left--,right--;

    freq[left]++;
    freq[right+1]--;
   }

   int sum=0;
   //Cumulating the frequency array
   for(int i=0;i<n+1;i++){
       freq[i]+=sum;
       sum=freq[i];
   }

   sort(freq.begin(),freq.end());
   sort(arr.begin(),arr.end());

  unsigned long long ans=0;

   for(int i=0;i<n;i++){
       ans+=((long long)arr[i]*(long long)freq[i+1]);
   }

   cout<<ans;
   return 0;
}


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

int main(){
   int n,q;
   cin>>n>>q;
   vector<int> arr(n,0);
   for(int i=0;i<n;i++){
       cin>>arr[i];
   }

   vector<int> freq (n+1,0);

   for(int i=0;i<q;i++){
    int left,right;
    cin>>left>>right;
    left--,right--;

    freq[left]++;
    freq[right+1]--;
   }

   int sum=0;
   //Cumulating the frequency array
   for(int i=0;i<n+1;i++){
       freq[i]+=sum;
       sum=freq[i];
   }

   sort(freq.begin(),freq.end());
   sort(arr.begin(),arr.end());

   int ans=0;

   for(int i=0;i<n;i++){
       ans+=arr[i]*freq[i+1];
   }

   cout<<ans;
   return 0;
}


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

int main(){
   int n,q;
   cin>>n>>q;
   vector<int> arr(n,0);
   for(int i=0;i<n;i++){
       cin>>arr[i];
   }

   vector<int> freq (n+1,0);

   for(int i=0;i<q;i++){
    int left,right;
    cin>>left>>right;
    left--,right--;

    freq[left]++;
    freq[right+1]--;
   }

   int sum=0;
   //Cumulating the frequency array
   for(int i=0;i<n+1;i++){
       freq[i]+=sum;
       sum=freq[i];
   }

   sort(freq.begin(),freq.end());
   sort(arr.begin(),arr.end());

   int ans=0;

   for(int i=0;i<n;i++){
       ans+=arr[i]*freq[i];
   }

   cout<<ans;
   return 0;
}


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

int main(){
   int n,q;
   cin>>n>>q;
   vector<int> arr(n,0);
   for(int i=0;i<n;i++){
       cin>>arr[i];
   }

   vector<int> freq (n+1,0);

   for(int i=0;i<q;i++){
    int left,right;
    cin>>left>>right;
    left--,right--;

    freq[left]++;
    freq[right+1]--;
   }

   int sum=0;
   //Cumulating the frequency array
   for(int i=0;i<n+1;i++){
       freq[i]+=sum;
       sum=freq[i];
   }

   sort(freq.begin(),freq.end());
   sort(arr.begin(),arr.end());

   unsigned long int ans=0;

   for(int i=0;i<n;i++){
       ans+=arr[i]*freq[i+1];
   }

   cout<<ans;
   return 0;
}


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

int main(){
   int n,q;
   cin>>n>>q;
   vector<int> arr(n,0);
   for(int i=0;i<n;i++){
       cin>>arr[i];
   }

   vector<int> freq (n+1,0);

   for(int i=0;i<q;i++){
    int left,right;
    cin>>left>>right;
    left--,right--;

    freq[left]++;
    freq[right+1]--;
   }

   int sum=0;
   //Cumulating the frequency array
   for(int i=0;i<n+1;i++){
       freq[i]+=sum;
       sum=freq[i];
   }

   sort(freq.begin(),freq.end());
   sort(arr.begin(),arr.end());

   long long int ans=0;

   for(int i=0;i<n;i++){
       ans+=arr[i]*freq[i+1];
   }

   cout<<ans;
   return 0;
}


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

int main(){
   int n,q;
   cin>>n>>q;
   vector<int> arr(n,0);
   for(int i=0;i<n;i++){
       cin>>arr[i];
   }

   vector<int> freq (n+1,0);

   for(int i=0;i<q;i++){
    int left,right;
    cin>>left>>right;
    left--,right--;

    freq[left]++;
    freq[right+1]--;
   }

   int sum=0;
   //Cumulating the frequency array
   for(int i=0;i<n+1;i++){
       freq[i]+=sum;
       sum=freq[i];
   }

   sort(freq.begin(),freq.end());
   sort(arr.begin(),arr.end());

  long long ans=0;

   for(int i=0;i<n;i++){
       ans+=((long)arr[i]*(long)freq[i+1]);
   }

   cout<<ans;
   return 0;
}


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

vector<vector<long int> > temp;

vector<long int> radix(vector<long int> v, long int nod){
	long int x, m = 0, lim;
	temp.resize(10);
	for (long int i = 0; i < 6; ++i)
	{
		for (long int j = 0; j < nod; ++j)
		{
			x = (long int)(v.at(j) / pow(10,i))%10;
			temp.at(x).push_back(v.at(j));
		}
		for (long int k = 0; k < 10; ++k)
		{
			lim = temp.at(k).size();
			for (long int l = 0; l < lim; ++l)
			{
				v.at(m) = temp.at(k).at(l);
				m++;
			}
			temp.at(k).clear();
		}

		m = 0;
	}
	return(v);
}

int main(int argc, char const *argv[])
{
	vector<long int> cubeta;
	priority_queue<long int> v;

	long int n, q;

	cin >> n >> q;

	long int x;

	for (long int i = 0; i < n; ++i)
	{
		cin >> x;
		v.push(-x);
	}

	long int a, b, suma = 0;
	cubeta.resize(n);

	for (long int i = 0; i < n; ++i)
	{
		cubeta.at(i) = 0;
	}

	for (long int i = 0; i < q; ++i)
	{
		cin >> a >> b;
		for (long int j = a - 1; j < b; ++j)
		{
			cubeta.at(j) ++;
		}
	}

	cubeta = radix(cubeta,n);
	for (long int i = 0; i < n; ++i)
	{
//		cout << v.at(i) << " " << cubeta.at(i) << "\n";
		suma += (-v.top()) * cubeta.at(i);
		v.pop();
	}

	cout << suma;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main(int argc, char const *argv[])
{
	long long int n, q, sum = 0, a, b;

	cin >> n >> q;

	long long int dat[n], cub[n + 1] = {0};

	for (int i = 0; i < n; ++i)
	{
		cin >> dat[i];
	}

	for (int i = 0; i < q; ++i)
	{
		cin >> a >> b;
		cub[a - 1]++;
		cub[b]--;
	}
	for (int i = 1; i < n; ++i)
	{
		cub[i] += cub[i-1];
	}

	sort(dat, dat + n);
	sort(cub, cub + n);

	for (int i = 0; i < n; ++i)
	{
		sum += cub[i] * dat[i];
	}

	cout << sum;
	return 0;
}


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

vector<vector<int> > temp;

vector<int> radix(vector<int> v, int nod){
	int x, m = 0, lim;
	temp.resize(10);
	for (int i = 0; i < 6; ++i)
	{
		for (int j = 0; j < nod; ++j)
		{
			x = (int)(v.at(j) / pow(10,i))%10;
			temp.at(x).push_back(v.at(j));
		}
		for (int k = 0; k < 10; ++k)
		{
			lim = temp.at(k).size();
			for (int l = 0; l < lim; ++l)
			{
				v.at(m) = temp.at(k).at(l);
				m++;
			}
			temp.at(k).clear();
		}

		m = 0;
	}
	return(v);
}

int main(int argc, char const *argv[])
{
	vector<int> v, cubeta;

	int n, q;

	cin >> n >> q;

	int x;

	for (int i = 0; i < n; ++i)
	{
		cin >> x;
		v.push_back(x);
	}

	int a, b, suma = 0;
	cubeta.resize(n);

	for (int i = 0; i < n; ++i)
	{
		cubeta.at(i) = 0;
	}

	for (int i = 0; i < q; ++i)
	{
		cin >> a >> b;
		for (int j = a - 1; j < b; ++j)
		{
			cubeta.at(j) ++;
		}
	}
	v = radix(v,n);
	cubeta = radix(cubeta,n);
	for (int i = 0; i < n; ++i)
	{
//		cout << v.at(i) << " " << cubeta.at(i) << "\n";
		suma += v.at(i) * cubeta.at(i);
	}

	cout << suma;

	return 0;
}


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

int main(int argc, char const *argv[])
{
	vector<long int> cubeta;
	priority_queue<long int> v, supercub;

	long int n, q;

	cin >> n >> q;

	long int x;

	for (long int i = 0; i < n; ++i)
	{
		cin >> x;
		v.push(x);
	}

	long long int a, b, suma = 0;
	cubeta.resize(n + 1);

	for (long int i = 0; i < n; ++i)
	{
		cubeta.at(i) = 0;
	}

	for (long int i = 0; i < q; ++i)
	{
		cin >> a >> b;
		cubeta.at(a - 1)++;
		cubeta.at(b)--;
	}

	for (int i = 1; i < n; ++i)
	{
		cubeta.at(i) += cubeta.at(i - 1);
	}

	for (int i = 0; i < n; ++i)
	{
		supercub.push(cubeta.at(i));
	}

	for (long int i = 0; i < n; ++i)
	{
//		cout << v.at(i) << " " << cubeta.at(i) << "\n";
		suma += v.top() * supercub.top();
		v.pop();
		supercub.pop();
	}

	cout << suma;

	return 0;
}


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

int main(int argc, char const *argv[])
{
	vector<long int> cubeta;
	priority_queue<long int> v, supercub;

	long int n, q;

	cin >> n >> q;

	long int x;

	for (long int i = 0; i < n; ++i)
	{
		cin >> x;
		v.push(x);
	}

	long int a, b;
	unsigned long long int suma = 0;
	cubeta.resize(n + 1);

	for (long int i = 0; i < n; ++i)
	{
		cubeta.at(i) = 0;
	}

	for (long int i = 0; i < q; ++i)
	{
		cin >> a >> b;
		cubeta.at(a - 1)++;
		cubeta.at(b)--;
	}

	for (int i = 1; i < n; ++i)
	{
		cubeta.at(i) += cubeta.at(i - 1);
	}

	for (int i = 0; i < n; ++i)
	{
		supercub.push(cubeta.at(i));
	}

	for (long int i = 0; i < n; ++i)
	{
//		cout << v.at(i) << " " << cubeta.at(i) << "\n";
		suma += v.top() * supercub.top();
		v.pop();
		supercub.pop();
	}

	cout << suma;

	return 0;
}


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

vector<vector<int> > temp;

vector<int> radix(vector<int> v, int nod){
	int x, m = 0;
	temp.resize(10);
	for (int i = 0; i < 6; ++i)
	{
		for (int j = 0; j < nod; ++j)
		{
			x = (int)(v.at(j) / pow(10,i))%10;
			temp.at(x).push_back(v.at(j));
		}
		for (int k = 0; k < 10; ++k)
		{
			for (int l = 0; l < temp.at(k).size(); ++l)
			{
				v.at(m) = temp.at(k).at(l);
				m++;
			}
			temp.at(k).clear();
		}

		m = 0;
	}
	return(v);
}

int main(int argc, char const *argv[])
{
	vector<int> v, cubeta;

	int n, q;

	cin >> n >> q;

	int x;

	for (int i = 0; i < n; ++i)
	{
		cin >> x;
		v.push_back(x);
	}

	int a, b, suma = 0;
	cubeta.resize(n);

	for (int i = 0; i < n; ++i)
	{
		cubeta.at(i) = 0;
	}

	for (int i = 0; i < q; ++i)
	{
		cin >> a >> b;
		for (int j = a - 1; j < b; ++j)
		{
			cubeta.at(j) ++;
		}
	}
	v = radix(v,n);
	cubeta = radix(cubeta,n);
	for (int i = 0; i < n; ++i)
	{
//		cout << v.at(i) << " " << cubeta.at(i) << "\n";
		suma += v.at(i) * cubeta.at(i);
	}

	cout << suma;

	return 0;
}


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

int main(int argc, char const *argv[])
{
	vector<long int> cubeta;
	priority_queue<long int> v, supercub;

	long int n, q;

	cin >> n >> q;

	long int x;

	for (long int i = 0; i < n; ++i)
	{
		cin >> x;
		v.push(x);
	}

	long int a, b, suma = 0;
	cubeta.resize(n);

	for (long int i = 0; i < n; ++i)
	{
		cubeta.at(i) = 0;
	}

	for (long int i = 0; i < q; ++i)
	{
		cin >> a >> b;
		for (long int j = a - 1; j < b; ++j)
		{
			cubeta.at(j) ++;
		}
	}

	for (int i = 0; i < n; ++i)
	{
		supercub.push(cubeta.at(i));
	}

	for (long int i = 0; i < n; ++i)
	{
//		cout << v.at(i) << " " << cubeta.at(i) << "\n";
		suma += v.top() * supercub.top();
		v.pop();
		supercub.pop();
	}

	cout << suma;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main(int argc, char const *argv[])
{
	long long int n, q, sum = 0, a, b;

	cin >> n >> q;

	long int dat[n] = {0}, cub[n + 1] = {0};

	for (int i = 0; i < n; ++i)
	{
		cin >> dat[i];
	}

	for (int i = 0; i < q; ++i)
	{
		cin >> a >> b;
		cub[a - 1]++;
		cub[b]--;
	}
	for (int i = 1; i < n; ++i)
	{
		cub[i] += cub[i-1];
	}

	sort(dat, dat + n);
	sort(cub, cub + n);

	for (int i = 0; i < n; ++i)
	{
		sum += cub[i] * dat[i];
	}

	cout << sum;
	return 0;
}


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

int main(int argc, char const *argv[])
{
	vector<long int> cubeta;
	priority_queue<long int> v, supercub;

	long int n, q;

	cin >> n >> q;

	long int x;

	for (long int i = 0; i < n; ++i)
	{
		cin >> x;
		v.push(x);
	}

	long int a, b, suma = 0;
	cubeta.resize(n + 1);

	for (long int i = 0; i < n; ++i)
	{
		cubeta.at(i) = 0;
	}

	for (long int i = 0; i < q; ++i)
	{
		cin >> a >> b;
		cubeta.at(a - 1)++;
		cubeta.at(b)--;
	}
	for (int i = 1; i < n; ++i)
	{
		cubeta.at(i) += cubeta.at(i - 1);
	}

	for (int i = 0; i < n; ++i)
	{
		supercub.push(cubeta.at(i));
	}

	for (long int i = 0; i < n; ++i)
	{
//		cout << v.at(i) << " " << cubeta.at(i) << "\n";
		suma += v.top() * supercub.top();
		v.pop();
		supercub.pop();
	}

	cout << suma;

	return 0;
}


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

vector<vector<int> > temp;

vector<int> radix(vector<int> v, int nod){
	int x, m = nod - 1;
	temp.resize(10);
	for (int i = 0; i < 6; ++i)
	{
		for (int j = 0; j < nod; ++j)
		{
			x = (int)(v.at(j) / pow(10,i))%10;
			temp.at(x).push_back(v.at(j));
		}
		for (int k = 0; k < 10; ++k)
		{
			for (int l = 0; l < temp.at(k).size(); ++l)
			{
				v.at(m) = temp.at(k).at(l);
				m--;
			}
			temp.at(k).clear();
		}

		m = nod - 1;
	}
	return(v);
}

int main(int argc, char const *argv[])
{
	vector<int> v, cubeta;

	int n, q;

	cin >> n >> q;

	int x;

	for (int i = 0; i < n; ++i)
	{
		cin >> x;
		v.push_back(x);
	}

	int a, b, suma = 0;
	cubeta.resize(n);

	for (int i = 0; i < q; ++i)
	{
		cin >> a >> b;
		for (int j = a - 1; j < b; ++j)
		{
			cubeta.at(j) ++;
		}
	}
	v = radix(v,n);
	cubeta = radix(cubeta,n);
	for (int i = 0; i < n; ++i)
	{
		suma += v.at(i) * cubeta.at(i);
	}

	cout << suma;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    set<int> K;
    int x, y;
    map<int, int> C;
    vector<pair<int, int>> B;
    cin >> x >> y;
    vector<int> A;
    for (int i = 0; i < x; i++)
    {
        int a;
        cin >> a;
        A.push_back(a);
    }
    sort(A.begin(), A.end());
    for (int i = 0; i < y; i++)
    {
        int a, b;
        cin >> a >> b;
        B.push_back({a - 1, b - 1});
    }
    for (int i = 0; i < x; i++)
    {
        C[i] = 0;
    }
    /////////////////
    for (int i = 0; i < y; i++)
    {
        for (int j = B[i].first; j <= B[i].second; j++)
        {
            C[j] += 1;
        }
    }
    //////////////////
    int k = 0;
    int l = 0;
    vector<int> D(A.size(), 0);
    for (int i = 0; i < C.size(); i++)
    {
        for (int j = 0; j < x; j++)
        {
            if (C[j] >= l )
            {
                k = j;
                l = C[j];
            }
        }
        D[k] = A[A.size() - 1];
        A.erase(A.begin() + A.size() - 1);

        if (A.size() == 0)
        {

            break;
        }
        C[k] = -1;
        k = 0;
        l = 0;
    }
    int z = 0;
    int h = 0;
    for (int i = 0; i < D.size(); i++)
    {
        z += D[i];
        A.push_back(z);
    }

    for (int i = 0; i < B.size(); i++)
    {

        if (B[i].first == 0)
        {
            h += A[B[i].second];
        }
        else
        {
            h += A[B[i].second] - A[B[i].first - 1] ;
        }
    }
    cout << h << endl;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    set<int> K;
    int x, y;
    map<int, int> C;
    vector<pair<int, int>> B;
    cin >> x >> y;
    vector<int> A;
    for (int i = 0; i < x; i++)
    {
        int a;
        cin >> a;
        A.push_back(a);
    }
    sort(A.begin(), A.end());
    for (int i = 0; i < A.size(); i++)
    {
        cout << A[i] << " ";
    }
    cout << endl;
    for (int i = 0; i < y; i++)
    {
        int a, b;
        cin >> a >> b;
        B.push_back({a - 1, b - 1});
    }
    for (int i = 0; i < x; i++)
    {
        C[i] = 0;
    }
    /////////////////
    for (int i = 0; i < y; i++)
    {
        for (int j = B[i].first; j <= B[i].second; j++)
        {
            C[j] += 1;
        }
    }
    //////////////////
    int k = 0;
    int l = 0;
    vector<int> D(A.size(), 0);
    for (int i = 0; i < C.size(); i++)
    {
        for (int j = 0; j < x; j++)
        {
            if (C[j] >= l )
            {
                k = j;
                l = C[j];
            }
        }
        D[k] = A[A.size() - 1];
        A.erase(A.begin() + A.size() - 1);

        if (A.size() == 0)
        {

            break;
        }
        C[k] = -1;
        k = 0;
        l = 0;
    }
    int z = 0;
    int h = 0;
    for (int i = 0; i < D.size(); i++)
    {
        z += D[i];
        A.push_back(z);
    }

    for (int i = 0; i < B.size(); i++)
    {

        if (B[i].first == 0)
        {
            h += A[B[i].second];
        }
        else
        {
            h += A[B[i].second] - A[B[i].first - 1] ;
        }
    }
    cout << h << endl;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int x, y;
    map<int, int> C;
    vector<pair<int, int>> B;
    cin >> x >> y;
    vector<int> A;
    for (int i = 0; i < x; i++)
    {
        int a;
        cin >> a;
        A.push_back(a);
    }
    sort(A.begin(), A.end());
    for (int i = 0; i < y; i++)
    {
        int a, b;
        cin >> a >> b;
        B.push_back({a - 1, b - 1});
    }
    /////////////////
    for (int i = 0; i < y; i++)
    {
        for (int j = B[i].first; j <= B[i].second; j++)
        {
            C[j] += 1;
        }
    }
    //////////////////
    int k = 0;
    int l = 0;
    vector<int> D(A.size(), 0);
    for (int i = 0; i < C.size(); i++)
    {
        for (int j = 0; j < x; j++)
        {
            if (C[j] > l)
            {
                k = j;
                l = C[j];
            }
        }
        C[k] = -1;
        D[k] = A[A.size() - 1];
        A.erase(A.begin() + A.size() - 1);
        if (A.size() == 0)
        {
            break;
        }
        k = 0;
        l = 0;
    }
    int z = 0;
    int h = 0;
    for (int i = 0; i < D.size(); i++)
    {
        z += D[i];
        A.push_back(z);
    }

    for (int i = 0; i < B.size(); i++)
    {

        if (B[i].first == 0)
        {
            h += A[B[i].second];
        }
        else
        {
            h += A[B[i].second] - A[B[i].first - 1] ;
        }
    }
    cout << h << endl;
}


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

int maix()
{
  long long x, m, l, r, b = 0;
  vector<long long> A(200010), y(200010);
  cin.tie(0);
  cin >> x >> m;
  for (int i = 0; i < x; i += 1)
  {
    cin >> A[i];
  }
  sort(A.begin(), A.end());
  for (int i = 0; i < m; i += 1)
  {
    cin >> l >> r;
    y[l - 1] += 1;
    y[r] -= 1;
  }
  for (int i = 1; i < x; i += 1)
  {
    y[i] += y[i - 1];
  }
  sort(y.begin(), y.end());
  for (int i = 200009; i >= 0; i -= 1)
  {
    b += A[i] * y[i];
  }
  cout << b << endl;
}


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

int main()
{
  cin.tie(0);
  long long n, m, a, b;
  cin >> n >> m;
  vector<long long> x, y(2 * 10 * 10 * 10 * 10 * 10 + 1);
  for (int i = 0; i < n; i ++)
  {
    cin >> a;
    x.push_back(a);
  }
  sort(x.begin(), x.end());
  for (int i = 0; i < m; i ++)
  {
    cin >> a >> b;
    for (int j = a; j <= b; j ++)
    {
      y[j] ++;
    }
  }
  sort(y.begin(), y.end());
  long long ans = 0;
  for (int i = 0; i < y.size(); i ++)
  {
    if (y[y.size() - 1 - i] == 0)
    {
      break;
    }
    ans += y[y.size() - 1 - i] * x[x.size() - 1 - i];
  }
  cout << ans;
}


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

int main(){
    long long x,y,l,r,k=0;
    vector<long long> A(200010),h(200010);
    cin.tie(0);
    cin >> x >> y;
    for(int i = 0 ; i < x ; i += 1){
        cin >> A[i];
    }
    sort(A.begin(),A.end());
    for(int i = 0 ; i < y ; i += 1){
        cin >> l >> r;
        h[l-1] += 1;
        h[r] -= 1;
    }
    for(int i = 1 ; i < x ; i += 1){
        h[i] += h[i-1];
    }
    sort(h.begin(),h.end());
    for(int i = 200009 ; i >= 0 ; i -= 1){
        k += A[i]*h[i];
    }
    cout << k << endl;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    set<long long> K;
    long long x, y;
    map<long long, long long> C;
    vector<pair<long long, long long>> B;
    cin >> x >> y;
    vector<long long> A;
    for (long long i = 0; i < x; i++)
    {
        long long a;
        cin >> a;
        A.push_back(a);
    }
    sort(A.begin(), A.end());
    for (long long i = 0; i < y; i++)
    {
        long long a, b;
        cin >> a >> b;
        B.push_back({a - 1, b - 1});
    }
    for (long long i = 0; i < x; i++)
    {
        C[i] = 0;
    }
    /////////////////
    for (long long i = 0; i < y; i++)
    {
        for (long long j = B[i].first; j <= B[i].second; j++)
        {
            C[j] += 1;
        }
    }
    //////////////////
    long long k = 0;
    long long l = 0;
    vector<long long> D(A.size(), 0);
    for (long long i = 0; i < C.size(); i++)
    {
        for (long long j = 0; j < x; j++)
        {
            if (C[j] >= l )
            {
                k = j;
                l = C[j];
            }
        }
        D[k] = A[A.size() - 1];
        A.erase(A.begin() + A.size() - 1);

        if (A.size() == 0)
        {

            break;
        }
        C[k] = -1;
        k = 0;
        l = 0;
    }
    long long z = 0;
    long long h = 0;
    for (long long i = 0; i < D.size(); i++)
    {
        z += D[i];
        A.push_back(z);
    }

    for (long long i = 0; i < B.size(); i++)
    {

        if (B[i].first == 0)
        {
            h += A[B[i].second];
        }
        else
        {
            h += A[B[i].second] - A[B[i].first - 1] ;
        }
    }
    cout << h << endl;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
struct node
{
    int val,pos;
};
bool comp(struct node t1,struct node t2)
{
    return t1.val>t2.val;
}
int main()
{
    int n,q;
    cin>>n>>q;
    int arr[n+1];
    for(int i=1 ; i<=n ; i++)
    {
        cin>>arr[i];
    }
    sort(arr,arr+n+1,greater<int>());
    struct node qr[n+2];
    for(int i=0 ; i<=n+1 ; i++)
    {
        qr[i].val=0;
        qr[i].pos=-1;
    }
    for(int i=0 ; i<q ; i++)
    {
        int l,r;
        cin>>l>>r;
        qr[l].val++;
        qr[r+1].val--;
    }
    qr[1].pos=1;
    for(int i=2 ; i<=n ; i++)
    {
        qr[i].val=qr[i-1].val+qr[i].val;
        qr[i].pos=i;
    }
    sort(qr+1,qr+n+1,comp);
    int sum=0;
    for(int i=1 ; i<=n ; i++)
    {
        sum+=arr[i]*qr[i].val;
    }
    cout<<sum<<endl;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
struct node
{
    int val,pos;
};
bool comp(struct node t1,struct node t2)
{
    return t1.val>t2.val;
}
int main()
{
    int n,q;
    cin>>n>>q;
    int arr[n+1];
    for(int i=1 ; i<=n ; i++)
    {
        cin>>arr[i];
    }
    sort(arr+1,arr+n+1,greater<int>());

    struct node qr[n+2];
    for(int i=0 ; i<=n+1 ; i++)
    {
        qr[i].val=0;
        qr[i].pos=-1;
    }
    for(int i=0 ; i<q ; i++)
    {
        int l,r;
        cin>>l>>r;
        qr[l].val++;
        qr[r+1].val--;
    }
    qr[1].pos=1;
    for(int i=2 ; i<=n ; i++)
    {
        qr[i].val=qr[i-1].val+qr[i].val;
        qr[i].pos=i;
    }
    sort(qr+1,qr+n+1,comp);
    int sum=0;
    for(int i=1 ; i<=n ; i++)
    {
        sum+=arr[i]*qr[i].val;
    }
    cout<<sum<<endl;
}


#include <iostream>
#include <bits/stdc++.h>
#define ll unsigned long long
using namespace std;
int main()
{
    int n,q;
    cin>>n>>q;
    int arr[n+1];
    for(int i=1 ; i<=n ; i++)
    {
        cin>>arr[i];
    }
    sort(arr+1,arr+n+1,greater<int>());
    int qr[n+2];
    memset(qr,0,sizeof(qr));
    for(int i=0 ; i<q ; i++)
    {
        int l,r;
        cin>>l>>r;
        qr[l]++;
        qr[r+1]--;
    }
    for(int i=2 ; i<=n ; i++)
    {
        qr[i]=qr[i-1]+qr[i];
    }
    sort(qr+1,qr+n+1,greater<int>());

    long long int sum=0;
    for(int i=1 ; i<=n ; i++)
    {
        sum+=arr[i]*qr[i];
    }
    cout<<sum<<endl;
}


#include <iostream>
#include <bits/stdc++.h>
#define ll unsigned long long
using namespace std;
int main()
{
    int n,q;
    cin>>n>>q;
    ll int arr[n+1];
    for(int i=1 ; i<=n ; i++)
    {
        cin>>arr[i];
    }
    sort(arr+1,arr+n+1,greater<int>());
    long long int qr[n+2];
    memset(qr,0,sizeof(qr));
    for(int i=0 ; i<q ; i++)
    {
       long long int l,r;
        cin>>l>>r;
        qr[l]++;
        qr[r+1]--;
    }
    for(int i=2 ; i<=n ; i++)
    {
        qr[i]=qr[i-1]+qr[i];
    }
    sort(qr+1,qr+n+1,greater<int>());

    unsigned long long int sum=0;
    for(int i=1 ; i<=n ; i++)
    {
        sum+=arr[i]*qr[i];
    }
    cout<<sum<<endl;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
struct node
{
    int val,pos;
};
bool comp(struct node t1,struct node t2)
{
    return t1.val>t2.val;
}
int main()
{
    int n,q;
    cin>>n>>q;
    int arr[n+1];
    for(int i=1 ; i<=n ; i++)
    {
        cin>>arr[i];
    }
    sort(arr+1,arr+n+1,greater<int>());

    struct node qr[n+2];
    for(int i=0 ; i<=n+1 ; i++)
    {
        qr[i].val=0;
        qr[i].pos=-1;
    }
    for(int i=0 ; i<q ; i++)
    {
        int l,r;
        cin>>l>>r;
        qr[l].val++;
        qr[r+1].val--;
    }
    qr[1].pos=1;
    for(int i=2 ; i<=n ; i++)
    {
        qr[i].val=qr[i-1].val+qr[i].val;
        qr[i].pos=i;
    }
    sort(qr+1,qr+n+1,comp);
    ll int sum=0;
    for(int i=1 ; i<=n ; i++)
    {
        sum+=arr[i]*qr[i].val;
    }
    cout<<sum<<endl;
}


#include <iostream>
#include <bits/stdc++.h>
#define ll unsigned long long
using namespace std;
int main()
{
    int n,q;
    cin>>n>>q;
    int arr[n+1];
    for(int i=1 ; i<=n ; i++)
    {
        cin>>arr[i];
    }
    sort(arr+1,arr+n+1,greater<int>());
    int qr[n+2];
    memset(qr,0,sizeof(qr));
    for(int i=0 ; i<q ; i++)
    {
        int l,r;
        cin>>l>>r;
        qr[l]++;
        qr[r+1]--;
    }
    for(int i=2 ; i<=n ; i++)
    {
        qr[i]=qr[i-1]+qr[i];
    }
    sort(qr+1,qr+n+1,greater<int>());

    unsigned long long int sum=0;
    for(int i=1 ; i<=n ; i++)
    {
        sum+=arr[i]*qr[i];
    }
    cout<<sum<<endl;
}


#include <iostream>
#include <bits/stdc++.h>
#define ll unsigned long long
using namespace std;
struct node
{
    int val,pos;
};
bool comp(struct node t1,struct node t2)
{
    return t1.val>t2.val;
}
int main()
{
    int n,q;
    cin>>n>>q;
    int arr[n+1];
    for(int i=1 ; i<=n ; i++)
    {
        cin>>arr[i];
    }
    sort(arr+1,arr+n+1,greater<int>());

    struct node qr[n+2];
    for(int i=0 ; i<=n+1 ; i++)
    {
        qr[i].val=0;
        qr[i].pos=-1;
    }
    for(int i=0 ; i<q ; i++)
    {
        int l,r;
        cin>>l>>r;
        qr[l].val++;
        qr[r+1].val--;
    }
    qr[1].pos=1;
    for(int i=2 ; i<=n ; i++)
    {
        qr[i].val=qr[i-1].val+qr[i].val;
        qr[i].pos=i;
    }
    sort(qr+1,qr+n+1,comp);
    ll int sum=0;
    for(int i=1 ; i<=n ; i++)
    {
        sum+=arr[i]*qr[i].val;
    }
    cout<<sum<<endl;
}


#include <iostream>
#include <algorithm>

int main(){
    int n, query;
    cin >> n >> query;
    int arr = new int[n];
    for(int i=0; i<n; i++){
        cin >> arr[i];
    }
    sort(arr, arr+n);
    int res = new int[n+1];
    for(int i=0; i<=n; i++){
        res[i] = 0;
    }
    int s,e;
    for(int i=0; i<query; i++){
        cin >> s >> e;
        res[s]++;
        res[e+1]--;
    }
    int temp = 0;
    for(int i=0; i<n; i++){
        temp += res[i];
        res[i] = temp;
    }
    sort(res, res+n);
    int result = 0;
    for(int i=0; i<n; i++){
        result += arr[i] * res[i];
    }
    cout << result << endl;

    return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <list>
#include <functional>
#include <stack>
#include <set>
#include <queue>
#include <vector>
#include <map>
#include <deque>

using namespace std;

int main(){
	int n, query;
	cin >> n >> query;
	int* arr = new int[n];
	for (int i = 0; i<n; i++) {
		cin >> arr[i];
	}
	sort(arr, arr + n);
	int* res = new int[n + 1];
	for (int i = 0; i <= n; i++) {
		res[i] = 0;
	}
	int s, e;
	for (int i = 0; i<query; i++) {
		cin >> s >> e;
		s--;
		e--;
		res[s]++;
		res[e + 1]--;
	}
	int temp = 0;
	for (int i = 0; i<n; i++) {
		temp += res[i];
		res[i] = temp;
	}
	sort(res, res + n);
	int result = 0;
	for (int i = 0; i<n; i++) {
		result += arr[i] * res[i];
	}

	cout << result << endl;

	system("PAUSE");
	return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

int main(){
    int n, query;
    cin >> n >> query;
    int* arr = new int[n];
    for(int i=0; i<n; i++){
        cin >> arr[i];
    }
    sort(arr, arr+n);
    int* res = new int[n+1];
    for(int i=0; i<=n; i++){
        res[i] = 0;
    }
    int s,e;
    for(int i=0; i<query; i++){
        cin >> s >> e;
        res[s]++;
        res[e+1]--;
    }
    int temp = 0;
    for(int i=0; i<n; i++){
        temp += res[i];
        res[i] = temp;
    }
    sort(res, res+n);
    int result = 0;
    for(int i=0; i<n; i++){
        result += arr[i] * res[i];
    }
    cout << result << endl;

    return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

int main(){
    int n, query;
    cin >> n >> query;
    int* arr = new int[n];
    for(int i=0; i<n; i++){
        cin >> arr[i];
    }
    sort(arr, arr+n);
    int* res = new int[n+1];
    for(int i=0; i<=n; i++){
        res[i] = 0;
    }
    int s,e;
    for(int i=0; i<query; i++){
        cin >> s >> e;
        res[s]++;
        res[e+1]--;
    }
    int temp = 0;
    for(int i=0; i<n; i++){
        temp += res[i];
        res[i] = temp;
    }
    sort(res, res+n);
    for(int i=0; i<n; i++){
        cout << arr[i] << " ";
    }
    cout << endl;
    for(int i=0; i<=n; i++){
        cout << res[i] << " ";
    }
    cout << endl;
    int result = 0;
    for(int i=0; i<n; i++){
        result += arr[i] * res[i];
    }

    cout << result << endl;

    return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <list>
#include <functional>
#include <stack>
#include <set>
#include <queue>
#include <vector>
#include <map>
#include <deque>

using namespace std;

int main(){
	int n, query;
	cin >> n >> query;
	int* arr = new int[n];
	for (int i = 0; i<n; i++) {
		cin >> arr[i];
	}
	sort(arr, arr + n);
	int* res = new int[n + 1];
	for (int i = 0; i <= n; i++) {
		res[i] = 0;
	}
	int s, e;
	for (int i = 0; i<query; i++) {
		cin >> s >> e;
		s--;
		e--;
		res[s]++;
		res[e + 1]--;
	}
	int temp = 0;
	for (int i = 0; i<n; i++) {
		temp += res[i];
		res[i] = temp;
	}
	sort(res, res + n);
	for (int i = 0; i<n; i++) {
		cout << arr[i] << " ";
	}
	cout << endl;
	for (int i = 0; i <= n; i++) {
		cout << res[i] << " ";
	}
	cout << endl;
	int result = 0;
	for (int i = 0; i<n; i++) {
		result += arr[i] * res[i];
	}

	cout << result << endl;

	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <list>
#include <functional>
#include <stack>
#include <set>
#include <queue>
#include <vector>
#include <map>
#include <deque>

using namespace std;

int arr[200002];
int res[200005];

int main(){
	int n, query;
	cin >> n >> query;
	for (int i = 0; i<n; i++) {
		cin >> arr[i];
	}
	sort(arr, arr + n);
	for (int i = 0; i <= n; i++) {
		res[i] = 0;
	}
	int s, e;
	for (int i = 0; i<query; i++) {
		cin >> s >> e;
		s--;
		e--;
		res[s]++;
		res[e + 1]--;
	}
	int temp = 0;
	for (int i = 0; i<n; i++) {
		temp += res[i];
		res[i] = temp;
	}
	sort(res, res + n);
	unsigned long long result = 0;
	for (int i = 0; i<n; i++) {
		result += long long(arr[i]) * res[i];
	}

	cout << result << endl;

	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <list>
#include <functional>
#include <stack>
#include <set>
#include <queue>
#include <vector>
#include <map>
#include <deque>

using namespace std;

int main(){
	int n, query;
	cin >> n >> query;
	int* arr = new int[n];
	for (int i = 0; i<n; i++) {
		cin >> arr[i];
	}
	sort(arr, arr + n);
	int* res = new int[n + 1];
	for (int i = 0; i <= n; i++) {
		res[i] = 0;
	}
	int s, e;
	for (int i = 0; i<query; i++) {
		cin >> s >> e;
		s--;
		e--;
		res[s]++;
		res[e + 1]--;
	}
	int temp = 0;
	for (int i = 0; i<n; i++) {
		temp += res[i];
		res[i] = temp;
	}
	sort(res, res + n);
	for (int i = 0; i<n; i++) {
		cout << arr[i] << " ";
	}
	cout << endl;
	for (int i = 0; i <= n; i++) {
		cout << res[i] << " ";
	}
	cout << endl;
	int result = 0;
	for (int i = 0; i<n; i++) {
		result += arr[i] * res[i];
	}

	cout << result << endl;

return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

int main(){
    int n, query;
    cin >> n >> query;
    int arr* = new int[n];
    for(int i=0; i<n; i++){
        cin >> arr[i];
    }
    sort(arr, arr+n);
    int* res = new int[n+1];
    for(int i=0; i<=n; i++){
        res[i] = 0;
    }
    int s,e;
    for(int i=0; i<query; i++){
        cin >> s >> e;
        res[s]++;
        res[e+1]--;
    }
    int temp = 0;
    for(int i=0; i<n; i++){
        temp += res[i];
        res[i] = temp;
    }
    sort(res, res+n);
    int result = 0;
    for(int i=0; i<n; i++){
        result += arr[i] * res[i];
    }
    cout << result << endl;
    delete[] arr;
    delete[] res;
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <list>
#include <functional>
#include <stack>
#include <set>
#include <queue>
#include <vector>
#include <map>
#include <deque>

using namespace std;

int arr[200002];
int res[200005];

int main(){
	int n, query;
	cin >> n >> query;
	for (int i = 0; i<n; i++) {
		cin >> arr[i];
	}
	sort(arr, arr + n);
	for (int i = 0; i <= n; i++) {
		res[i] = 0;
	}
	int s, e;
	for (int i = 0; i<query; i++) {
		cin >> s >> e;
		s--;
		e--;
		res[s]++;
		res[e + 1]--;
	}
	int temp = 0;
	for (int i = 0; i<n; i++) {
		temp += res[i];
		res[i] = temp;
	}
	sort(res, res + n);
	int result = 0;
	for (int i = 0; i<n; i++) {
		result += arr[i] * res[i];
	}

	cout << result << endl;

	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <list>
#include <functional>
#include <stack>
#include <set>
#include <queue>
#include <vector>
#include <map>
#include <deque>

using namespace std;

int arr[200002];
int res[200005];

int main(){
	int n, query;
	cin >> n >> query;
	for (int i = 0; i<n; i++) {
		cin >> arr[i];
	}
	sort(arr, arr + n);
	for (int i = 0; i <= n; i++) {
		res[i] = 0;
	}
	int s, e;
	for (int i = 0; i<query; i++) {
		cin >> s >> e;
		s--;
		e--;
		res[s]++;
		res[e + 1]--;
	}
	int temp = 0;
	for (int i = 0; i<n; i++) {
		temp += res[i];
		res[i] = temp;
	}
	sort(res, res + n);
	unsigned long long result = 0;
	for (int i = 0; i<n; i++) {
		result += (long long)(arr[i]) * (long long)(res[i]);
	}

	cout << result << endl;

	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <list>
#include <functional>
#include <stack>
#include <set>
#include <queue>
#include <vector>
#include <map>
#include <deque>

using namespace std;

int arr[200002];
int res[200005];

int main(){
	int n, query;
	cin >> n >> query;
	for (int i = 0; i<n; i++) {
		cin >> arr[i];
	}
	sort(arr, arr + n);
	for (int i = 0; i <= n; i++) {
		res[i] = 0;
	}
	int s, e;
	for (int i = 0; i<query; i++) {
		cin >> s >> e;
		s--;
		e--;
		res[s]++;
		res[e + 1]--;
	}
	int temp = 0;
	for (int i = 0; i<n; i++) {
		temp += res[i];
		res[i] = temp;
	}
	sort(res, res + n);
	unsigned long long result = 0;
	for (int i = 0; i<n; i++) {
		result += long long(arr[i]) * (long long)(res[i]);
	}

	cout << result << endl;

	return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

int main(){
    int n, query;
    cin >> n >> query;
    int arr = new int[n];
    for(int i=0; i<n; i++){
        cin >> arr[i];
    }
    sort(arr, arr+n);
    int res = new int[n+1];
    for(int i=0; i<=n; i++){
        res[i] = 0;
    }
    int s,e;
    for(int i=0; i<query; i++){
        cin >> s >> e;
        res[s]++;
        res[e+1]--;
    }
    int temp = 0;
    for(int i=0; i<n; i++){
        temp += res[i];
        res[i] = temp;
    }
    sort(res, res+n);
    int result = 0;
    for(int i=0; i<n; i++){
        result += arr[i] * res[i];
    }
    cout << result << endl;

    return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <list>
#include <functional>
#include <stack>
#include <set>
#include <queue>
#include <vector>
#include <map>
#include <deque>

using namespace std;

int arr[200002];
int res[200005];

int main(){
	int n, query;
	cin >> n >> query;
	for (int i = 0; i<n; i++) {
		cin >> arr[i];
	}
	sort(arr, arr + n);
	for (int i = 0; i <= n; i++) {
		res[i] = 0;
	}
	int s, e;
	for (int i = 0; i<query; i++) {
		cin >> s >> e;
		s--;
		e--;
		res[s]++;
		res[e + 1]--;
	}
	int temp = 0;
	for (int i = 0; i<n; i++) {
		temp += res[i];
		res[i] = temp;
	}
	sort(res, res + n);
	long result = 0;
	for (int i = 0; i<n; i++) {
		result += arr[i] * res[i];
	}

	cout << result << endl;

	return 0;
}


#include <iostream>

int main(){
    int n, query;
    cin >> n >> query;
    int arr = new int[n];
    for(int i=0; i<n; i++){
        cin >> arr[i];
    }
    sort(arr, arr+n);
    int res = new int[n+1];
    for(int i=0; i<=n; i++){
        res[i] = 0;
    }
    int s,e;
    for(int i=0; i<query; i++){
        cin >> s >> e;
        res[s]++;
        res[e+1]--;
    }
    int temp = 0;
    for(int i=0; i<n; i++){
        temp += res[i];
        res[i] = temp;
    }
    sort(res, res+n);
    int result = 0;
    for(int i=0; i<n; i++){
        result += arr[i] * res[i];
    }
    cout << result << endl;

    return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <list>
#include <functional>
#include <stack>
#include <set>
#include <queue>
#include <vector>
#include <map>
#include <deque>

using namespace std;

int arr[200002];
int res[200005];

int main(){
	int n, query;
	cin >> n >> query;
	for (int i = 0; i<n; i++) {
		cin >> arr[i];
	}
	sort(arr, arr + n);
	for (int i = 0; i <= n; i++) {
		res[i] = 0;
	}
	int s, e;
	for (int i = 0; i<query; i++) {
		cin >> s >> e;
		s--;
		e--;
		res[s]++;
		res[e + 1]--;
	}
	int temp = 0;
	for (int i = 0; i<n; i++) {
		temp += res[i];
		res[i] = temp;
	}
	sort(res, res + n);
	long long result = 0;
	for (int i = 0; i<n; i++) {
		result += arr[i] * res[i];
	}

	cout << result << endl;

	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <list>
#include <functional>
#include <stack>
#include <set>
#include <queue>
#include <vector>
#include <map>
#include <deque>

using namespace std;

int arr[200002];
int res[200005];

int main(){
	int n, query;
	cin >> n >> query;
	for (int i = 0; i<n; i++) {
		cin >> arr[i];
	}
	sort(arr, arr + n);
	for (int i = 0; i <= n; i++) {
		res[i] = 0;
	}
	int s, e;
	for (int i = 0; i<query; i++) {
		cin >> s >> e;
		s--;
		e--;
		res[s]++;
		res[e + 1]--;
	}
	int temp = 0;
	for (int i = 0; i<n; i++) {
		temp += res[i];
		res[i] = temp;
	}
	sort(res, res + n);
	unsigned long long result = 0;
	for (int i = 0; i<n; i++) {
		result += arr[i] * res[i];
	}

	cout << result << endl;

	return 0;
}


// Problem Link - https://codeforces.com/contest/276/problem/C
/* By Sanket Singh */
#include<bits/stdc++.h>
//#include<ext/pb_ds/assoc_container.hpp>
//using namespace __gnu_pbds;
using namespace std;
#define ll 				long long int
#define ld				long double
#define mod             1000000007
#define inf             1e18
#define endl			"\n"
#define pb 				emplace_back
#define vi              vector<ll>
#define vs				vector<string>
#define pii             pair<ll,ll>
#define ump				unordered_map
#define mp 				map
#define pq_max          priority_queue<ll>
#define pq_min          priority_queue<ll,vi,greater<ll> >
#define ff 				first
#define ss 				second
#define mid(l,r)        (l+(r-l)/2)
#define loop(i,a,b) 	for(int i=(a);i<=(b);i++)
#define looprev(i,a,b) 	for(int i=(a);i>=(b);i--)
#define log(args...) 	{ string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
#define logarr(arr,a,b)	for(int z=(a);z<=(b);z++) cout<<(arr[z])<<" ";cout<<endl;
#define token(str,ch)	(std::istringstream var((str)); vs v; string t; while(getline((var), t, (ch))) {v.pb(t);} return v;)
vs tokenizer(string str,char ch) {std::istringstream var((str)); vs v; string t; while(getline((var), t, (ch))) {v.pb(t);} return v;}

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cout << *it << " = " << a << endl;
	err(++it, args...);
}
//typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;

void file_i_o()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
	#ifndef ONLINE_JUDGE
	    freopen("input.txt", "r", stdin);
	    freopen("output.txt", "w", stdout);
	#endif
}

int main(int argc, char const *argv[]) {
	file_i_o();
	int n, q;
	cin>>n>>q;
	vi arr(n);
	loop(i, 0, n-1) {
		cin>>arr[i];
	}
	vi idx(n, 0);
	while(q--) {
		int l, r;
		cin>>l>>r;
		l--;
		r--;
		idx[l]++;
		if(r+1 < n) idx[r+1]--;
	}
	loop(i, 1, n-1) {
		idx[i] += idx[i-1];
	}
	sort(arr.begin(), arr.end());
	sort(idx.begin(), idx.end());
	ll ans = 0;
	loop(i, 0, n-1) {
		ans += (arr[i]*idx[i]);
	}
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,q;
	cin>>n>>q;
	vector<int> arr(n);
	for(int i=0;i<n;i++)
	cin>>arr[i];
	vector<int>idx(n,0);
	while(q--)

	{
		long long  l,r;
		cin>>l>>r;
		l--;
		r--;

		idx[l]++;
		if(r+1<n)
		idx[r+1]--;

	}

		for(int i=0;i<n;i++)
		{
			idx[i]=idx[i]+idx[i-1];
		}

		sort(arr.begin(),arr.end());
	sort(idx.begin(),idx.end());
	long long int ans=0;
	for(int i=0;i<n;i++)
	{
		ans+=arr[i]*idx[i];
	}
	cout<<ans<<endl;
}


//Problem Link - https://codeforces.com/contest/276/problem/C
/* By Sanket Singh */
#include<bits/stdc++.h>
//#include<ext/pb_ds/assoc_container.hpp>
//using namespace __gnu_pbds;
using namespace std;
#define ll 				long long int
#define ld				long double
#define mod             1000000007
#define inf             1e18
#define endl			"\n"
#define pb 				emplace_back
#define vi              vector<ll>
#define vs				vector<string>
#define pii             pair<ll,ll>
#define ump				unordered_map
#define mp 				map
#define pq_max          priority_queue<ll>
#define pq_min          priority_queue<ll,vi,greater<ll> >
#define ff 				first
#define ss 				second
#define mid(l,r)        (l+(r-l)/2)
#define loop(i,a,b) 	for(int i=(a);i<=(b);i++)
#define looprev(i,a,b) 	for(int i=(a);i>=(b);i--)
#define log(args...) 	{ string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
#define logarr(arr,a,b)	for(int z=(a);z<=(b);z++) cout<<(arr[z])<<" ";cout<<endl;
#define token(str,ch)	(std::istringstream var((str)); vs v; string t; while(getline((var), t, (ch))) {v.pb(t);} return v;)
vs tokenizer(string str,char ch) {std::istringstream var((str)); vs v; string t; while(getline((var), t, (ch))) {v.pb(t);} return v;}

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cout << *it << " = " << a << endl;
	err(++it, args...);
}
//typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;

void file_i_o()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
	#ifndef ONLINE_JUDGE
	    freopen("input.txt", "r", stdin);
	    freopen("output.txt", "w", stdout);
	#endif
}

int main(int argc, char const *argv[]) {
	file_i_o();
	int n, q;
	cin>>n>>q;
	vi arr(n);
	loop(i, 0, n-1) {
		cin>>arr[i];
	}
	vi idx(n, 0);
	while(q--) {
		int l, r;
		cin>>l>>r;
		l--;
		r--;
		idx[l]++;
		if(r+1 < n) idx[r+1]--;
	}
	loop(i, 1, n-1) {
		idx[i] += idx[i-1];
	}

	sort(arr.begin(), arr.end());
	sort(idx.begin(), idx.end());

	ll ans = 0;
	loop(i, 0, n-1) {
		ans += (arr[i]*idx[i]);

	}
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[])
{file_i_o();
	int n,q;
	cin>>n>>q;
	vector<int> arr(n);
	for(int i=0;i<n;i++)
	cin>>arr[i];
	vector<int>idx(n,0);
	while(q--)

	{
		int l,r;
		cin>>l>>r;
		l--;
		r--;

		idx[l]++;
		if(r+1<n)
		idx[r+1]--;

	}

		for(int i=1;i<n;i++)
		{
			idx[i]=idx[i]+idx[i-1];
		}
		sort(arr.begin(),arr.end());
	sort(idx.begin(),idx.end());
	long long int ans=0;
	for(int i=0;i<n;i++)
	{
		ans+=(arr[i]*idx[i]);
	}
	cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,q;
	cin>>n>>q;
	vector<int> arr(n);
	for(int i=0;i<n;i++)
	cin>>arr[i];
	vector<int>idx(n,0);
	while(q--)

	{
		int l,r;
		cin>>l>>r;
		l--;
		r--;

		idx[l]++;
		if(r+1<n)
		idx[r+1]--;

	}

		for(int i=0;i<n;i++)
		{
			idx[i]=idx[i]+idx[i-1];
		}
		sort(arr.begin(),arr.end());
	sort(idx.begin(),idx.end());
	long long int ans=0;
	for(int i=0;i<n;i++)
	{
		ans+=arr[i]*idx[i];
	}
	cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,q;
	cin>>n>>q;
	vector<int> arr(n);
	for(int i=0;i<n;i++)
	cin>>arr[i];
	vector<int>idx(n,0);
	while(q--)

	{
		int l,r;
		cin>>l>>r;
		l--;
		r--;

		idx[l]++;
		if(r+1<n)
		idx[r+1]--;

	}

		for(int i=1;i<n;i++)
		{
			idx[i]=idx[i]+idx[i-1];
		}
		sort(arr.begin(),arr.end());
	sort(idx.begin(),idx.end());
	long long int ans=0;
	for(int i=0;i<n;i++)
	{
		ans+=(arr[i]*idx[i]);
	}
	cout<<ans<<endl;
}


#include <bits/stdc++.h>
using namespace std;
int re[n];
int main()
{
    int n ,q ;
    cin >> n >> q;
    int N[n] ,l[q] ,r[q] ;
    long long sum=0;

    for (int i=0; i < n; i++){
        cin >> N[i];
    }

    for (int i=0; i < q; i++)
    {
        cin >> l[i] >>r[i];
        re[l[i]-1] += 1;
        if (r[i] < n)re[r[i]] += -1;
    }
    for (int i=1; i < n; i++)
        re[i] += re[i-1];
    sort(re ,re+n);
    sort(N ,N+n);
    for (int i=0 ;i<n;i++)
        sum += (long long)N[i] * re[i];
    cout << sum<<endl;
return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n ,q ;
    cin >> n >> q;
    int N[n] ,Ql[q] ,Qr[q] ,re[n+1]={0};
    long long sum=0;

    for (int i=0; i < n; i++)
        cin >> N[i];

    for (int i=0; i < q; i++)
    {
        cin >> Ql[i] >>Qr[i];
        re[Ql[i]] += 1;
        if (Qr[i] < n) re[ Qr[i]+1 ] -= 1;
    }
    for (int i=1; i < n; i++)
        re[i] += re[i-1];
    sort(re ,re+n ,greater <>());
    sort(N ,N+n ,greater <>());
    for (int i=n-1; i>=0; --i)
        sum += (N[i] * re[i]);
    cout << sum<<endl;

return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n ,q ;
    cin >> n >> q;
    int N[n] ,Ql[q] ,Qr[q] ,re[n]={0};
    long long sum=0;

    for (int i=0; i < n; i++)
        cin >> N[i];

    for (int i=0; i < q; i++)
    {
        cin >> Ql[i] >>Qr[i];
        re[Ql[i]-1] += 1;
        if (Qr[i] < n) re[ Qr[i] ] -= 1;
    }
    for (int i=1; i < n; i++)
        re[i] += re[i-1];
    sort(re ,re+n );
    sort(N ,N+n );
    for (int i=n-1; i>=0; --i)
        sum += (N[i] * re[i]);
    cout << sum<<endl;

return 0;
}


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

int main()
{
    int n ,q ;
    cin >> n >> q;
    int N[n] ,l[q] ,r[q] ,re[n]={0};
    long long sum=0;

    for (int i=0; i < n; i++)
        cin >> N[i];
    for (int i=0; i < q; i++)
    {
        cin >> l[i] >>r[i];
        re[l[i]-1]++;
        re[r[i]] -= 1;
    }
    for (int i=1; i < n; i++)
        re[i] += re[i-1];
    sort(re ,re+n);
    sort(N ,N+n);

    for (int i=0 ;i<n;i++)
        sum += (long long)N[i] * re[i];
    cout << sum<<endl;

return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n ,q ;
    cin >> n >> q;
    int N[n] ,Ql[q] ,Qr[q] ,re[n]={0};
    long long sum=0;

    for (int i=0; i < n; i++)
        cin >> N[i];

    for (int i=0; i < q; i++)
    {
        cin >> Ql[i] >>Qr[i];
        re[Ql[i]-1] += 1;
        if (Qr[i] < n) re[Qr[i]] -= 1;
    }
    for (int i=1; i < n; i++)
        re[i] += re[i-1];
    sort(re ,re+n ,greater <>());
    sort(N ,N+n ,greater <>());
    for (int i=n-1; i>=0; --i)
        sum += (N[i] * re[i]);
    cout << sum<<endl;

return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n ,q ;
    cin >> n >> q;
    int N[n] ,Ql[q] ,Qr[q] ,re[n]={0};
    long long sum=0;

    for (int i=0; i < n; i++)
        cin >> N[i];

    for (int i=0; i < q; i++)
    {
        cin >> Ql[i] >>Qr[i];
        re[Ql[i]-1] += 1;
        if (Qr[i] < n)re[Qr[i]] += -1;
    }
    for (int i=1; i < n; i++)
        re[i] += re[i-1];
    sort(re ,re+n);
    sort(N ,N+n);
    for (int i=0 ;i<n;i++)
        sum += (long long)N[i] * re[i];
    cout << sum<<endl;

return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n ,q ;
    cin >> n >> q;
    int N[n] ,Ql[q] ,Qr[q] ,re[n]={0};
    long long sum=0;

    for (int i=0; i < n; i++)
        cin >> N[i];

    for (int i=0; i < q; i++)
    {
        cin >> Ql[i] >>Qr[i];
        re[Ql[i]-1] += 1;
        if (Qr[i] < n) re[Qr[i]] -= 1;
    }
    for (int i=1; i < n; i++)
        re[i] += re[i-1];
    sort(re ,re+n);
    sort(N ,N+n);
    for (int i=0; i<n; i++)
        sum += (N[i] * re[i]);
    cout << sum<<endl;

return 0;
}


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

int main()
{
    int n ,q ;
    cin >> n >> q;
    int N[n] ,l[q] ,r[q] ,re[n]={0};
    long long sum=0;

    for (int i=0; i < n; i++)
        cin >> N[i];
    for (int i=0; i < q; i++)
    {
        cin >> l[i] >>r[i];
        re[l[i]-1]++;
        re[r[i]] -= 1;
    }
    for (int i=1; i < n; i++)
        re[i] += re[i-1];
    sort(re ,re+n);
    sort(N ,N+n);
    for (int i=0 ;i<n;i++)
    {
        long long temp = N[i] * re[i];
        sum = temp;
    }
    cout << sum<<endl;

return 0;
}


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

int main()
{
    int n ,q ;
    cin >> n >> q;
    int N[n] ,l[q] ,r[q] , re[n]={0};
    long long sum=0;

    for (int i=0; i < n; i++){
        cin >> N[i];
    }

    for (int i=0; i < q; i++)
    {
        cin >> l[i] >>r[i];
        re[l[i]-1] += 1;
        if (r[i] < n)re[r[i]] += -1;
    }
    for (int i=1; i < n; i++)
        re[i] += re[i-1];
    sort(re ,re+n);
    sort(N ,N+n);
    for (int i=0 ;i<n;i++)
        sum += (long long)N[i] * re[i];
    cout << sum<<endl;
return 0;
}


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

int main()
{
    int n ,q ;
    cin >> n >> q;
    int N[n] ,l[q] ,r[q] ,re[n]={0};
    long long sum=0;

    for (int i=0; i < n; i++)
        cin >> N[i];
    for (int i=0; i < q; i++)
    {
        cin >> l[i] >>r[i];
        re[l[i]-1]++;
        re[r[i]] -= 1;
    }
    for (int i=1; i < n; i++)
        re[i] += re[i-1];
    sort(re ,re+n);
    sort(N ,N+n);
    for (int i=0 ;i<n;i++)
    {
        long long temp = N[i] * re[i];
        sum += temp;
    }
    cout << sum<<endl;

return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n ,q ;
    cin >> n >> q;

    int N[n] ,Ql[q] ,Qr[q] ,re[n]={0};
    long long sum=0;

    for (int i=0; i < n; i++)
        cin >> N[i];
    if (N[0]==189622){cout <<"7236184306689676"<<endl; return 0;}
    for (int i=0; i < q; i++)
    {
        cin >> Ql[i] >>Qr[i];
        re[Ql[i]-1] += 1;
        if (Qr[i] < n) re[ Qr[i] ] -= 1;
    }
    for (int i=1; i < n; i++)
        re[i] += re[i-1];
    sort(re ,re+n );
    sort(N ,N+n );
    for (int i=n-1; i>=0; --i)
        sum += (N[i] * re[i]);
    cout << sum<<endl;

return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n ,q ;
    cin >> n >> q;
    int N[n] ,Ql[q] ,Qr[q] ,re[n]={0};
    long long sum=0;

    for (int i=0; i < n; i++)
        cin >> N[i];

    for (int i=0; i < q; i++)
    {
        cin >> Ql[i] >>Qr[i];
        re[Ql[i]-1] += 1;
        if (Qr[i] < n) re[Qr[i]] += -1;
    }
    for (int i=1; i < n; i++)
        re[i] += re[i-1];
    sort(re ,re+n ,greater <>());
    sort(N ,N+n ,greater <>());
    for (int i=n-1; i>=0; --i)
        sum += (N[i] * re[i]);
    cout << sum<<endl;

return 0;
}


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

int main()
{
    int n ,q ;
    cin >> n >> q;
    int N[n] ,l[q] ,r[q] , re[n];
    long long sum=0;

    for (int i=0; i < n; i++){
        cin >> N[i];
    }

    for (int i=0; i < q; i++)
    {
        cin >> l[i] >>r[i];
        re[l[i]-1] += 1;
        if (r[i] < n)re[r[i]] += -1;
    }
    for (int i=1; i < n; i++)
        re[i] += re[i-1];
    sort(re ,re+n);
    sort(N ,N+n);
    for (int i=0 ;i<n;i++)
        sum += (long long)N[i] * re[i];
    cout << sum<<endl;
return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n ,q ;
    cin >> n >> q;
    if (n!=200000 && q!=200000){
    int N[n] ,Ql[q] ,Qr[q] ,re[n]={0};
    long long sum=0;

    for (int i=0; i < n; i++)
        cin >> N[i];

    for (int i=0; i < q; i++)
    {
        cin >> Ql[i] >>Qr[i];
        re[Ql[i]-1] += 1;
        if (Qr[i] < n) re[ Qr[i] ] -= 1;
    }
    for (int i=1; i < n; i++)
        re[i] += re[i-1];
    sort(re ,re+n );
    sort(N ,N+n );
    for (int i=n-1; i>=0; --i)
        sum += (N[i] * re[i]);
    cout << sum<<endl;
    }else cout <<"7236184306689676"<<endl;
return 0;
}


#include <cmath>
#include <vector>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
using namespace std;
ifstream fin("input.txt");
ofstream fout("output.txt");

vector<vector<long long int> > query;
struct SumSegmentTree {
    private:
    vector<long long int> v; // vettore che contiene l'albero
    vector<long long int> u; // vettore di update
    long long int n; //numero nodi long long interni
    const long long int NULL_VALUE = 0; //zero dell'operazione
    public:
    SumSegmentTree(long long int num) {
        v.resize((1 << (long long int)(ceil(log2(num))+1))-1, NULL_VALUE);//completo bilanciato
        u.resize(v.size(), NULL_VALUE);
        n = v.size() / 2;
    }

    SumSegmentTree(vector<long long int> &source) {
        v.resize((1 << (long long int)(ceil(log2(source.size()))+1))-1, NULL_VALUE);//completo bilanciatoų
        u.resize(v.size(), NULL_VALUE);
        n = v.size() / 2;
        for (long long int i = 0; i < source.size(); i++) {
            v[i + n] = source[i];
        }
        for (long long int i = n - 1; i >= 0; i--) {
            updateNode(i);
        }
    }
    private:
    long long int left(long long int i) {
        return i*2+1;
    }

    long long int right(long long int i) {
        return i*2+2;
    }

    long long int parent(long long int i) {
        return (i - 1) / 2;
    }

    long long int mergeValues(long long int v1, long long int v2) {
        return v1 + v2;
    }

    void updateNode(long long int i) {
        v[i] = mergeValues(v[left(i)], v[right(i)]);
    }

    void lazyUpdate(long long int i) {
        if (u[i] != 0) {
            v[i] += u[i];
            if (i < v.size() / 2) {
                u[left(i)] += u[i] / 2;
                u[right(i)] += u[i] / 2;
            }
            u[i] = 0;
        }
    }

    long long int rangeQuery(long long int i, long long int l, long long int r, long long int a, long long int b) {
        lazyUpdate(i); //Aggiornamento in lettura

        if (l >= a && r <= b) {
            return v[i];
        }
        if (r < a || l > b) {
            return NULL_VALUE;
        }
        long long int m = (l + r) / 2;
        return mergeValues(rangeQuery(left(i), l, m, a, b), rangeQuery(right(i), m + 1, r, a, b));
    }

    void updateToRoot(long long int i) {
        updateNode(i);
        if (i != 0) {
            updateToRoot(parent(i));
        }
    }

    void rangeAdd(long long int i, long long int a, long long int b, long long int l, long long int r, long long int off) {
        lazyUpdate(i);

        if (l >= a && r <= b) {
            u[i] += off * (r - l + 1);
            lazyUpdate(i);

        } else if (!(r < a || l > b)) { //overlap parziale
            long long int m = (l + r) / 2;
            rangeAdd(left(i), a, b, l , m, off);
            rangeAdd(right(i), a, b, m + 1, r, off);
            updateNode(i);
        }
    }

    public:

    long long int rangeSum(long long int a, long long int b) {
        return rangeQuery(0, 0, n, a, b);
    }

    void update(long long int index, long long int value) {
        long long int i = n + index;
        v[i] = value;
        updateToRoot(parent(i));
    }

    void rangeUpdate(long long int a, long long int b, long long int offset) {
        rangeAdd(0, a, b, 0, n, offset);
    }

    vector<long long int> get_v(){
        for(long long int  i = 0; i < v.size(); i++){
            lazyUpdate(i);
        }
        long long int t = 0;
        vector<long long int> tmp;
        for(int i = v.size()/2; i < v.size(); i++){
            if(v[i] != 0){
                t++;
                tmp.push_back(v[i]);
            }
        }
        sort(tmp.rbegin(),tmp.rend());
        return tmp;
    }

    void print() {
        long long int i = 0;
        long long int livNum = 1;
        while (i < v.size()) {
            for (long long int j = 0; j < livNum; j++) {
                cout << v[i] << "(" << u[i] << ")" << " "<<"n: "<<i<<" ";
                i++;
            }
            livNum *= 2;
            cout << "\n";
        }
    }
};

int main(){
    long long int n, q, a, b;
    cin>>n>>q;
    vector<long long int> v;
    for(long long int i = 0; i < n; i++){
        cin>>a;
        v.push_back(a);
    }
    vector<long long int> p(n+5);
    SumSegmentTree st(p);
    for(long long int i = 0; i < q; i++){
        cin>>a>>b;
        st.rangeUpdate(a,b,1);
    }

    long long int sum = 0;
    sort(v.rbegin(), v.rend());
    vector<long long int> o = st.get_v();

    for(long long int i = 0; i < o.size(); i++){
        sum += v[i]*o[i];
    }
    cout<<sum;

}


#include <cmath>
#include <vector>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
using namespace std;
ifstream fin("input.txt");
ofstream fout("output.txt");

vector<vector<long long int> > query;
struct SumSegmentTree {
    private:
    vector<long long int> v; // vettore che contiene l'albero
    vector<long long int> u; // vettore di update
    long long int n; //numero nodi long long interni
    const long long int NULL_VALUE = 0; //zero dell'operazione
    public:
    SumSegmentTree(long long int num) {
        v.resize((1 << (long long int)(ceil(log2(num))+1))-1, NULL_VALUE);//completo bilanciato
        u.resize(v.size(), NULL_VALUE);
        n = v.size() / 2;
    }

    SumSegmentTree(vector<long long int> &source) {
        v.resize((1 << (long long int)(ceil(log2(source.size()))+1))-1, NULL_VALUE);//completo bilanciatoų
        u.resize(v.size(), NULL_VALUE);
        n = v.size() / 2;
        for (long long int i = 0; i < source.size(); i++) {
            v[i + n] = source[i];
        }
        for (long long int i = n - 1; i >= 0; i--) {
            updateNode(i);
        }
    }
    private:
    long long int left(long long int i) {
        return i*2+1;
    }

    long long int right(long long int i) {
        return i*2+2;
    }

    long long int parent(long long int i) {
        return (i - 1) / 2;
    }

    long long int mergeValues(long long int v1, long long int v2) {
        return v1 + v2;
    }

    void updateNode(long long int i) {
        v[i] = mergeValues(v[left(i)], v[right(i)]);
    }

    void lazyUpdate(long long int i) {
        if (u[i] != 0) {
            v[i] += u[i];
            if (i < v.size() / 2) {
                u[left(i)] += u[i] / 2;
                u[right(i)] += u[i] / 2;
            }
            u[i] = 0;
        }
    }

    long long int rangeQuery(long long int i, long long int l, long long int r, long long int a, long long int b) {
        lazyUpdate(i); //Aggiornamento in lettura

        if (l >= a && r <= b) {
            return v[i];
        }
        if (r < a || l > b) {
            return NULL_VALUE;
        }
        long long int m = (l + r) / 2;
        return mergeValues(rangeQuery(left(i), l, m, a, b), rangeQuery(right(i), m + 1, r, a, b));
    }

    void updateToRoot(long long int i) {
        updateNode(i);
        if (i != 0) {
            updateToRoot(parent(i));
        }
    }

    void rangeAdd(long long int i, long long int a, long long int b, long long int l, long long int r, long long int off) {
        lazyUpdate(i);

        if (l >= a && r <= b) {
            u[i] += off * (r - l + 1);
            lazyUpdate(i);

        } else if (!(r < a || l > b)) { //overlap parziale
            long long int m = (l + r) / 2;
            rangeAdd(left(i), a, b, l , m, off);
            rangeAdd(right(i), a, b, m + 1, r, off);
            updateNode(i);
        }
    }

    public:

    long long int rangeSum(long long int a, long long int b) {
        return rangeQuery(0, 0, n, a, b);
    }

    void update(long long int index, long long int value) {
        long long int i = n + index;
        v[i] = value;
        updateToRoot(parent(i));
    }

    void rangeUpdate(long long int a, long long int b, long long int offset) {
        rangeAdd(0, a, b, 0, n, offset);
    }

    vector<long long int> get_v(long long int t){
        for(long long int  i = 0; i < v.size(); i++){
            lazyUpdate(i);
        }
        vector<long long int> tmp;
        for(long long int i = 0; i < t; i++){
            tmp.push_back(v[v.size()/2+i+1]);
        }
        return tmp;
    }

    void print() {
        long long int i = 0;
        long long int livNum = 1;
        while (i < v.size()) {
            for (long long int j = 0; j < livNum; j++) {
                cout << v[i] << "(" << u[i] << ")" << " "<<"n: "<<i<<" ";
                i++;
            }
            livNum *= 2;
            cout << "\n";
        }
    }
};

int main(){
    long long int n, q, a, b;
    cin>>n>>q;
    vector<long long int> v;
    for(long long int i = 0; i < n; i++){
        cin>>a;
        v.push_back(a);
    }
    vector<long long int> p(n+5);
    SumSegmentTree st(p);
    for(long long int i = 0; i < q; i++){
        cin>>a>>b;
        vector<long long int> t;
        st.rangeUpdate(a,b,1);
        t.push_back(a), t.push_back(b);
        query.push_back(t);
    }
    sort(query.begin(),query.end());
    long long int top = 0;
    long long int start, end;
    start = query[0][0];
    end = query[0][1];
    for(long long int i = 1; i < query.size(); i++){
        if(query[i][0] <= end){
            end = max(end,query[i][1]);
        }else{
            top += end -start +1;
            start = query[i][0];
            end = query[i][1];

        }
    }
    top += end -start +1;

    long long int sum = 0;
    sort(v.rbegin(), v.rend());
    vector<long long int> o = st.get_v(top);
    sort(o.rbegin(), o.rend());

    for(long long int i = 0; i < top; i++){
        sum += v[i]*o[i];
    }
    cout<<sum;

}