Maximum query sum
Time: 1 s · Memory: 256 MB
- Prefix sums
Statement
You are given an array of numbers and queries . The answer to a query is . Before the queries the elements may be rearranged in any way. Find the maximum total of the answers to all queries.
Input format
The first line contains and (). The second line contains integers (). Each of the next lines contains a query: two integers and ().
Output format
A single number — the maximum total.
Examples
Input
3 3 5 3 2 1 2 2 3 1 3
Output
25
Input
5 3 5 2 4 1 3 1 5 2 3 2 3
Output
33
Note
Let be the number of queries that cover position ; a difference array gives it in . The total is , so the largest number goes to the most used position: sort and separately and multiply matching elements.
