Logan and Queries
InterviewTime limit2sMemory limit1024 MB
Rearrange an array so that the total of all q range-sum query answers is as large as possible, then report that maximum sum.
- Level
Medium4 of 10
- Topics
- Prefix sum, Greedy, Sorting
- Solved
- No attempts yet
Problem
Professor X has an array of numbers. He makes queries. Each query consists of two integers and , and its answer is the sum of the elements with indices from to in the original array.
Professor X's happiness level is the total of all query answers.
Logan wants to make Professor X as happy as possible. To do this, he may rearrange the elements of the array in any order.
He has not been able to manage this on his own, so he asked you for help.
Your task is to compute the maximum possible happiness level of Professor X when the elements of the array may be rearranged in any order.
Input
The first line contains two integers and ().
The second line contains integers (), the elements of the array.
Each of the next lines contains a pair of integers and (), the bounds of a segment.
Output
Print one integer: the maximum possible happiness level of Professor X when the elements may be rearranged in any order.