#include <bits/stdc++.h>   using namespace std;   const long long int mod = 1e9 + 7;   long long int Mod(long long int n) { return ((n % mod) + mod) % mod; }   void fast() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);   }   long long int f[200005];   long long int invfact[200005];   long long int power(long long int x, long long int y) {     long long int res = 1;     while (y > 0) {       if (y & 1) {         res = (res * x) % mod;       }       x = (x * x) % mod;       y >>= 1;     }     return res;   }   void precal() {     f[0] = 1;     invfact[0] = 1;     for (int i = 1; i < 200005; i++) {       f[i] = (f[i - 1] * i) % mod;       invfact[i] = power(f[i], mod - 2);     }   }   long long int fact(long long int n, long long int k) {     if (n < k) {       return 0;     }     return ((f[n] * invfact[k]) % mod * invfact[n - k]) % mod;   }   bool prime[10000011];   void primenumbers(long long int n) {     memset(prime, true, sizeof(prime));     for (long long int p = 2; p * p <= n; p++) {       if (prime[p] == true) {         for (long long int i = p * p; i <= n; i += p) prime[i] = false;       }     }   }   long long int modI(long long int a, long long int m) {     long long int m0 = m, y = 0, x = 1;     if (m == 1) return 0;     while (a > 1) {       long long int q = a / m;       long long int t = m;       m = a % m;       a = t;       t = y;       y = x - q * y;       x = t;     }     if (x < 0) x += m0;     return x;   }   int spf[100001];   void sieve() {     spf[1] = 1;     for (int i = 2; i < 100001; i++) spf[i] = i;     for (int i = 4; i < 100001; i += 2) spf[i] = 2;     for (int i = 3; i * i < 100001; i++) {       if (spf[i] == i) {         for (int j = i * i; j < 100001; j += i)           if (spf[j] == j) spf[j] = i;       }     }   }   vector<int> getFactorization(int x) {     vector<int> ret;     while (x != 1) {       ret.push_back(spf[x]);       x = x / spf[x];     }     return ret;   }   const long long int mxn = 400010;   long long int st[mxn];   long long int a[mxn];   void build(long long int v, long long int l, long long int r) {     if (l == r) {       st[v] = a[l];     } else {       build(2 * v, l, (l + r) / 2);       build(2 * v + 1, (l + r) / 2 + 1, r);       st[v] = st[2 * v] * st[2 * v + 1];     }   }   void update(long long int v, long long int l, long long int r,               long long int pos, long long int val) {     if (pos < l || pos > r) {       return;     }     if (pos == l && pos == r) {       st[v] = val;     } else {       update(2 * v, l, (l + r) / 2, pos, val);       update(2 * v + 1, (l + r) / 2 + 1, r, pos, val);       st[v] = st[2 * v] * st[2 * v + 1];     }   }   long long int query(long long int v, long long int l, long long int r,                       long long int tl, long long int tr, long long int mo) {     if (tr < l || tl > r || tl > tr) {       return 1;     }     if (l == tl && r == tr) {       return st[v] % mo;     } else {       return (((query(2 * v, l, (l + r) / 2, tl, min(tr, (l + r) / 2), mo)) %                mo) *               ((query(2 * v + 1, (l + r) / 2 + 1, r, max(tl, (l + r) / 2 + 1), tr,                       mo)) %                mo)) %              mo;     }   }   long long int maxSubArraySum(vector<long long int> a, long long int size) {     long long int max_so_far = INT_MIN, max_ending_here = 0, start = 0, end = 0,                   s = 0;     for (long long int i = 0; i < size; i++) {       max_ending_here += a[i];       if (max_so_far < max_ending_here) {         max_so_far = max_ending_here;       }       if (max_ending_here < 0) {         max_ending_here = 0;         s = i + 1;       }     }     return max_so_far;   }   int main() {     fast();     long long int n, q;     cin >> n >> q;     vector<long long int> d(n + 1, 0);     for (long long int i = 1; i < n + 1; i++) {       cin >> d[i];     }     sort(d.begin(), d.end());     vector<long long int> pr(n + 2, 0);     while (q--) {       long long int x, y;       cin >> x >> y;       pr[x]++;       pr[y + 1]--;     }     vector<long long int> get(n + 1, 0);     for (long long int i = 1; i < n + 1; i++) {       get[i] = get[i - 1] + pr[i];     }     sort(get.begin(), get.end());     long long int ans = 0;     for (long long int i = 1; i < n + 1; i++) {       ans = ans + get[i] * d[i];     }     cout << ans <<   n ;     return 0;   }