We need to parse the problem: We have an array of n elements (values a_i). There are q queries, each query is sum over a subarray [l_i, r_i]. We can reorder the array arbitrarily before answering the queries (i.e., we can permute the given values arbitrarily across positions 1..n). Then we answer each query with the sum over its positions. The total sum of replies is sum over i=1..q of sum_{j=l_i}^{r_i} a_{perm(j)}. We want to maximize this total sum by choosing the permutation of values.
We need to compute the maximum possible total sum.
Observations: The total sum of query replies can be expressed as: For each position j (1-indexed), the value at position j contributes to the total sum multiplied by the number of queries that cover that position. Let's denote freq[j] = number of queries with l ≤ j ≤ r. Then the total sum = sum_{j=1}^n (freq[j] * a_{perm(j)}). So we need to assign the largest a_i to the positions with the highest frequencies to maximize the sum. This is a straightforward greedy: sort frequencies descending, sort a_i descending, multiply pairwise and sum.
Thus the problem reduces to: given n and q queries (intervals), compute for each position the coverage count freq[j] (number of intervals covering j). Then sort these frequencies, sort the array values, compute dot product.
Constraints: n, q up to 2e5, values up to 2e5, but that's okay. Need to compute freq efficiently. Use difference array: initialize array diff of size n+2 zeros. For each query [l, r], diff[l] += 1, diff[r+1] -= 1. Then prefix sum to get freq. O(n+q). Then sort freq (O(n log n)) and sort a (O(n log n)). Then compute sum as 64-bit.
Edge cases: queries can be many, n large. Sum can be large: each a_i up to 2e5, freq up to q (2e5), product up to 4e10, sum up to n * 4e10 = 8e15, fits in 64-bit? 2e5 * 2e5 * 2e5? Actually max sum: n=2e5, each freq up to 2e5, each a_i up to 2e5, product = 4e10, sum of n such = 8e15 < 9e18 (2^63-1 ~9.22e18), so 64-bit is safe.
Example 1: n=3, a=[5,3,2], queries: [1,2],[2,3],[1,3]. Frequencies: position1: queries 1 and 3 => 2, position2: all three =>3, position3: queries2 and3 =>2. Sorted freqs: 3,2,2. Sorted a descending: 5,3,2 => dot = 53 + 32 + 2*2 = 15+6+4=25. Matches.
Example 2: n=5, a=[5,2,4,1,3]; queries: [1,5],[2,3],[2,3]. Compute frequencies: diff: query1: [1,5] -> +1 at1, -1 at6; query2: [2,3] -> +1 at2, -1 at4; query3: same -> +1 at2, -1 at4. Prefix: pos1:1; pos2:1+2=3? Actually let's compute: diff all zero. After q1: diff[1]=1,diff[6]=-1. After q2: diff[2]=+1,diff[4]=-1. After q3: diff[2]=+1 again -> diff[2]=2, diff[4]=-1 again -> diff[4]=-2. Now prefix: pos1:1; pos2:1+2=3; pos3:3+0? diff[3]=0 =>3; pos4:3 + diff[4]=3-2=1; pos5:1+diff[5]=0? Actually diff[5]=0 so 1; pos6: irrelevant. So frequencies: [1,3,3,1,1]? Wait check pos5: after pos4 we have 1, then pos5 diff[5]=0 so stays 1. Yes. Sorted freqs descending: 3,3,1,1,1. Sorted a descending: 5,4,3,2,1. Dot = 53 +43 +31 +21 +1*1 = 15+12+3+2+1=33. Matches.
Thus solution is straightforward.
But careful: input size large, need fast I/O in C++.
Implementation steps:
- Read n, q.
- Read array a of size n.
- Initialize diff array of size n+2 to 0 (or use vector diff(n+2,0)).
- For each query: read l, r; diff[l]++; diff[r+1]--.
- Compute freq array of size n: for i=1..n: cur += diff[i]; freq[i-1] = cur.
- Sort freq (in non-increasing order). (Can sort ascending both but we need descending dot; simplest: sort both descending.)
- Sort a descending.
- Compute sum = 0; for i=0..n-1: sum += (long long)freq[i] * a[i]; // need long long.
- Print sum.
Complexities: O((n+q) log n) due to sorts.
We can also compute frequencies with Fenwick or segment tree? But difference array is O(n+q).
Potential pitfalls: diff size n+2, ensure r+1 ≤ n+1. Use 1-indexing.
Memory: diff array up to 2e5+2, fine.
Edge cases: n=1, q=1, etc.
Thus answer.
But wait: The problem statement says "Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier." That's typical for Codeforces. We'll just use long long and cout.
Double-check constraints: a_i up to 2e5, but can be sorted.
Implementation in C++:
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
vector a(n);
for (int i=0; i<n; ++i) cin >> a[i];
vector diff(n+2, 0);
for (int i=0; i<q; ++i) {
int l, r;
cin >> l >> r;
diff[l]++;
diff[r+1]--;
}
vector freq(n);
int cur = 0;
for (int i=1; i<=n; ++i) {
cur += diff[i];
freq[i-1] = cur;
}
sort(freq.begin(), freq.end(), greater());
sort(a.begin(), a.end(), greater());
long long sum = 0;
for (int i=0; i<n; ++i) {
sum += (long long)freq[i] * a[i];
}
cout << sum << '\n';
return 0;
}
That's it. But the problem might have extra nuance? The problem says "reorder the array elements before replying to the queries in a way that makes the sum of query replies maximum possible." That is exactly the greedy assignment. So solution is correct.
We might also consider using counting sort because values are up to 2e5 and frequencies up to 2e5, but sort O(n log n) is fine for 2e5.
One more nuance: The frequencies may be computed more directly: For each query [l,r], we add 1 to all positions in that range. We can also use a sweep line with events. But diff array is efficient.
Thus final answer.
But note: the problem description says "the little girl loves the problems on array queries very much." It's from Codeforces (maybe problem 276C? Actually it's similar to "Little Girl and Maximum Sum" problem). Yes, known as "Little Girl and Maximum Sum" (CF 276C). So solution exactly as above.
But let's ensure we didn't miss anything: The array values are given, we can reorder arbitrarily. That's it.
Now proceed to produce final answer. ```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
cin >> n >> q;
vector<int> a(n);
for (int i = 0; i < n; ++i) cin >> a[i];
vector<int> diff(n + 2, 0); // 1‑based, with an extra cell for r+1
for (int i = 0; i < q; ++i) {
int l, r;
cin >> l >> r;
diff[l]++;
diff[r + 1]--;
}
vector<int> freq(n);
int cur = 0;
for (int i = 1; i <= n; ++i) {
cur += diff[i];
freq[i - 1] = cur;
}
sort(freq.begin(), freq.end(), greater<int>());
sort(a.begin(), a.end(), greater<int>());
long long total = 0;
for (int i = 0; i < n; ++i) {
total += (long long)freq[i] * a[i];
}
cout << total << '\n';
return 0;
}