func_code_string: #include <bits/stdc++.h>   using namespace std;   const long long int MOD = 1000000007;   const long double PI = 2 * acos(0.0);   const vector<long long int> dx = {1, -1, 0, 0};   const vector<long long int> dy = {0, 0, 1, -1};   vector<long long int> ga(long long int n, bool oneIndexed = false) {     vector<long long int> a;     if (oneIndexed) a.push_back(0ll);     for (long long int i = 0; i < n; i++) {       long long int p;       cin >> p;       a.push_back(p);     }     return move(a);   }   vector<unsigned long long int> gau(unsigned long long int n,                                      bool oneIndexed = false) {     vector<unsigned long long int> a;     if (oneIndexed) a.push_back(0llu);     for (unsigned long long int i = 0; i < n; i++) {       unsigned long long int p;       cin >> p;       a.push_back(p);     }     return move(a);   }   vector<string> gas(unsigned long long int n, bool oneIndexed = false) {     vector<string> a;     if (oneIndexed) a.push_back(  );     for (unsigned long long int i = 0; i < n; i++) {       string p;       cin >> p;       a.push_back(p);     }     return move(a);   }   template <typename T, typename A>   void pa(vector<T, A> const &a, long long int begin = 0,           long long int end = -1) {     if (end == -1) end = (long long int)a.size() - 1;     for (long long int i = begin; i <= end; i++) {       cout << a[i] <<    ;     }     cout <<   n ;   }   template <typename T, typename A>   void papair(vector<T, A> const &a, long long int begin = 0,               long long int end = -1) {     if (end == -1) end = (long long int)a.size() - 1;     for (long long int i = begin; i <= end; i++) {       cout << a[i].first <<     << a[i].second <<   n ;     }     cout <<   n ;   }   void yes() { cout <<  YES n ; }   void no() { cout <<  NO n ; }   void yesno(int f) {     if (f)       yes();     else       no();   }   void solve() {     vector<long long int> infreq, outfreq;     long long int n, q;     cin >> n >> q;     vector<long long int> a = ga(n, 1);     infreq = vector<long long int>(n + 1, 0);     outfreq = vector<long long int>(n + 1, 0);     while (q--) {       long long int l, r;       cin >> l >> r;       infreq[l] += 1;       outfreq[r] += 1;     }     vector<long long int> freq(n + 1, 0);     for (long long int i = 1; i <= n; i++) {       freq[i] += freq[i - 1];       freq[i] += infreq[i];       freq[i] -= outfreq[i - 1];     }     vector<pair<long long int, long long int> > data;     for (long long int i = 1; i <= n; i++) data.push_back({freq[i], i});     sort(data.begin(), data.end());     sort(a.begin(), a.end());     long long int s = 0;     while (!data.empty()) {       s += data.back().first * a.back();       a.pop_back();       data.pop_back();     }     cout << s;   }   int main(void) {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     solve();     return 0;   }   