-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLittleGirlandMaximumSum.cpp
More file actions
60 lines (50 loc) · 1.18 KB
/
LittleGirlandMaximumSum.cpp
File metadata and controls
60 lines (50 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
AUTHOR: Antarikshya Mitra
TIME:
PROBLEM NO:- Little Girl and Maximum Sum 276C
*/
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
const int MOD = 1e9 + 7;
void solve(vector<int> &arr, vector<pair<int,int>> &queries)
{
int n = arr.size();
vector<long long> freq(n + 1, 0);
// Difference array
for (auto &q : queries)
{
freq[q.first-1]++;
freq[q.second]--;
}
// Prefix sum → actual frequency
for (int i = 1; i < n; i++)
freq[i] += freq[i - 1];
freq.pop_back(); // remove extra element
// Sort both descending
sort(arr.begin(), arr.end(), greater<int>());
sort(freq.begin(), freq.end(), greater<long long>());
long long ans = 0;
for (int i = 0; i < n; i++)
ans += arr[i] * freq[i];
cout << ans << endl;
}
int32_t main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n,q;
cin>>n>>q;
vector<int> arr(n);
vector<pair<int,int>> queries(q);
for (auto &a:arr)
cin>>a;
for(int i=0;i<q;i++)
{
cin>>queries[i].first>>queries[i].second;
}
solve(arr,queries);
return 0;
}