Under construction

Still being built. Things may change

Fishmongers

Interview

Time limit1sMemory limit512 MB

Summary
Assign each fish to a fishmonger with a weight limit and per-kilogram price so total earnings are maximized.
Level

Medium5 of 10

Topics
Greedy, Sorting, Heap, Array
Solved
No attempts yet

Problem

You fish fish.

You hate fish.

You love monies.

Therefore sell fish.

To fishmongers.

For maximum profit.

Input

The first line of input contains two integers nn (1≤n≤100 0001 \le n \le 100\,000), the number of fish you have to sell, and mm (1≤m≤100 0001 \le m \le 100\,000), the number of fishmongers. The second line contains nn space-separated integers w1,w2,…,wnw_1, w_2, \ldots, w_n, the weight of each of your fish in kilograms (1≤wi≤100 0001 \le w_i \le 100\,000). Finally, there are mm lines, the jj'th of which consists of two integers xjx_j (1≤xj≤100 0001 \le x_j \le 100\,000) and pjp_j (1≤pj≤100 0001 \le p_j \le 100\,000), respectively indicating how many fish the jj'th fishmonger wants to buy and how many monies he will pay per kilogram.

Output

A single integer, the maximum number of monies you can obtain by selling your fish to the fishmongers.

Examples1

  1. Example 1

    Input
    4 3
    1 2 7 5
    2 4
    1 5
    3 3
    
    Expected output
    66