Codeforces Round #169 (Div. 2)---C. Little Girl and Maximum Sum (simple greedy)

Source: Internet
Author: User

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 is Inde Xed starting from 1); Also, there is Q-queries, each of the defined by a pair of integers li, ri (1?≤?li?≤?ri?≤?n). You need to find for each query the sum of elements of the array with indexes from Li to RI, inclusive.

The little girl found the problem rather boring. She decided to reorder the array elements before replying to the queries in a-a-the-makes the sum of query replies Maxi Mum possible. Your task is to find the value of this maximum sum.
Input

The first line contains, space-separated integers n (1?≤?n?≤?2 105) and Q (1?≤?q?≤?2 105)-the number of elements in t He array and the number of queries, correspondingly.

The next line contains n space-separated integers ai (1?≤?ai?≤?2 105)-the array elements.

Each of the following Q lines contains, space-separated integers li and ri (1?≤?li?≤?ri?≤?n)-the i-th query.
Output

In a single, line print a, integer-the maximum sum of query replies after the array elements is reordered.

%lld specifier to read or write 64-bit integers inс++. It is preferred to use the CIN, cout streams or the%i64d specifier.
Sample Test (s)
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

Greedy, to count the number of occurrences of each position, and then of course, the largest number of places where the most, and then multiply the line

/************************************************************************* > File name:cf-169-c.cpp > Aut Hor:alex > Mail: [email protected] > Created time:2015 April 02 Thursday 15:11 23 Seconds ******************************* *****************************************/#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <cstring>#include <cstdio>#include <cmath>#include <cstdlib>#include <queue>#include <stack>#include <map>#include <bitset>#include <set>#include <vector>using namespace STD;Const DoublePI =ACOs(-1.0);Const intINF =0x3f3f3f3f;Const DoubleEPS =1e-15;typedef Long LongLL;typedefPair <int,int> PLL;Static Const intN =200100;intSum[n];intArr[n];intMain () {intN, Q; while(~scanf("%d%d", &n, &q)) {memset(Sum,0,sizeof(sum)); for(inti =1; I <= N; ++i) {scanf("%d", &arr[i]); }intL, R; while(q--) {scanf("%d%d", &l, &r);            ++SUM[L]; --sum[r +1]; } for(inti =1; I <= N; ++i) {Sum[i] + = sum[i-1]; } sort (sum +1, Sum + n +1); Sort (arr +1, arr + n +1); LL ans =0; for(inti = n; I >=1;        -i) {ans + = (LL) arr[i] * Sum[i]; }printf("%i64d\n", ans); }return 0;}

Codeforces Round #169 (Div. 2)---C. Little Girl and Maximum Sum (simple greedy)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.