The little girl loves the problems on array queries very much.

One day she came across a rather well-known problem: you've got an array of $n$ elements (the elements of the array are indexed starting from 1); also, there are $q$ queries, each one is defined by a pair of integers $l_i$, $r_i$ $(1 \le l_i \le r_i \le n)$. You need to find for each query the sum of elements of the array with indexes from $l_i$ to $r_i$, inclusive.

The little girl found the problem rather boring. She decided to reorder the array elements before replying to the queries in a way that makes the sum of query replies maximum possible. Your task is to find the value of this maximum sum.
The first line contains two space-separated integers $n$ ($1 \le n \le 2\cdot10^5$) and $q$ ($1 \le q \le 2\cdot10^5$) — the number of elements in the array and the number of queries, correspondingly.

The next line contains $n$ space-separated integers $a_i$ ($1 \le a_i \le 2\cdot10^5$) — the array elements.

Each of the following $q$ lines contains two space-separated integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) — the $i$-th query.
"""
## NOTE
- one solution is with binary indexed tree -> give NLogN time
    - we don't use it
- interesting question in finding max number of overlaps between multiple ranges
    - kinda similar to line sweeping
- hard

## INFO
276_C_LittleGirlAndMaximumSum
https://codeforces.com/problemset/problem/276/C

## TAGS
- data structures
- greedy
- implementation
- sortings
- *1500

## TESTS
-in:
3 3
5 3 2
1 2
2 3
1 3
-out:
25

we re-arrenge to [3, 5, 2]
3+5=8
5+2=7
3+5+2=10
sum:25


-in:
5 3
5 2 4 1 3
1 5
2 3
2 3
-out:
33

"""

#%%

import sys
import time

start_time = time.time()
FILE = False

# ---------------------- HELPER INPUT FUNCTIONS ----------------------#


def get_int():
    return int(sys.stdin.readline())


def get_str():
    return sys.stdin.readline().strip()


def get_list_int():
    return list(map(int, sys.stdin.readline().split()))


def get_list_str():
    return list(sys.stdin.readline().split())


# -------------------------- SOLVE FUNCTION --------------------------#


def solve():
    """
    the range with max overlap should have the larger numbers
    https://codeforces.com/contest/276/submission/117326267

    implementation is simple. find the max range and sort based on the max occurance to lowest

    1------3
        2------5

     0  1  2  3  4  5  6
    [1  1  0 -1  0 -1  0]   <- line sweep
    [1  2  2  1  1  0  0]   <- pre-sum


    """
    n, g = get_list_int()
    arr = get_list_int()

    cnt = [0] * n
    for _ in range(g):
        s, e = get_list_int()
        cnt[s - 1] += 1
        if e < n:
            cnt[e] -= 1

    for i in range(1, n):
        cnt[i] += cnt[i - 1]

    cnt.sort()
    arr.sort()
    print(sum(a * b for a, b in zip(cnt, arr)))


# -------------------------- MAIN FUNCTION --------------------------#


def main():
    if FILE:
        sys.stdin = open("./src/codeforces/input.txt", "r")

    solve()

    if FILE:
        print("\033[95m" + "> time elapsed: ", (time.time() - start_time) * 1000.0, "ms")


main()
