Skip to content

Commit

Permalink
Merge pull request #5042 from Sosuke23/a
Browse files Browse the repository at this point in the history
Little Girl and Maximum Sum Editorial
  • Loading branch information
SansPapyrus683 authored Jan 15, 2025
2 parents 9efa56b + 31e584b commit 0be11df
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
2 changes: 1 addition & 1 deletion content/3_Silver/More_Prefix_Sums.problems.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"isStarred": false,
"tags": ["Difference Array"],
"solutionMetadata": {
"kind": "none"
"kind": "internal"
}
}
],
Expand Down
86 changes: 86 additions & 0 deletions solutions/silver/cf-276C.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
---
id: cf-276C
source: CF
title: Little Girl and Maximum Sum
author: Rameez Parwez
---

## Explanation

We can calculate the contribution of each index in the range query using a difference array technique.

To maximize the sum, sort both the array and frequency array in descending order
and pair the elements of the array with their respective frequencies.

## Implementation

**Time Complexity:** $ \mathcal{O} (N \log N)$

<LanguageSection>
<CPPSection>

```cpp
#include <algorithm>
#include <iostream>
#include <vector>

using std::cout;
using std::endl;
using std::vector;

int main() {
int n, q;
std::cin >> n >> q;
vector<int> arr(n);

for (int &x : arr) { std::cin >> x; }

vector<int> freq(n + 1);
for (int i = 0; i < q; i++) {
int l, r;
std::cin >> l >> r;
l--;
freq[l] += 1;
freq[r] -= 1;
}

for (int i = 1; i <= n; i++) { freq[i] += freq[i - 1]; }

std::sort(std::rbegin(freq), std::rend(freq));
std::sort(std::rbegin(arr), std::rend(arr));

long long res = 0;
for (int i = 0; i < n; i++) { res += 1LL * freq[i] * arr[i]; }

cout << res << endl;
}
```

</CPPSection>
<PySection>

```py
n, q = map(int, input().split())
arr = list(map(int, input().split()))

freq = [0] * (n + 1)
for _ in range(q):
l, r = map(int, input().split())
freq[l - 1] += 1
freq[r] -= 1

for i in range(1, n + 1):
freq[i] += freq[i - 1]

freq.sort(reverse=True)
arr.sort(reverse=True)

res = 0
for i in range(n):
res += freq[i] * arr[i]

print(res)
```

</PySection>
</LanguageSection>

0 comments on commit 0be11df

Please sign in to comment.