# Perform K of Q queries to maximize the sum of the array elements

Given an array **arr[]** of **N** integers and an integer **K**. Also given are **Q** queries which have two numbers **L** and **R**. For every query, you can increase all the elements of the array in the index range **[L, R]** by **1**. The task is to choose exactly **K** queries out of Q queries such that the sum of the array at the end is maximized. Print the **sum** after performing **K** such queries.

**Examples:**

Input:arr[] = {1, 1, 2, 2, 2, 3},

que[] = {{0, 4}, {1, 2}, {2, 5}, {2, 3}, {2, 4}},

K = 3

Output:23

We choose the first, third and the fifth query.

After performing first query -> arr[] = {2, 2, 3, 3, 3, 3}

After performing third query -> arr[] = {2, 2, 4, 4, 4, 4}

After performing fifth query -> arr[] = {2, 2, 5, 5, 5, 4}

And the array sum is 2 + 2 + 5 + 5 + 5 + 4 = 23.

Input:arr[] = {4, 5, 4, 21, 22},

que[] = {{1, 2}, {2, 2}, {2, 4}, {2, 2}},

K = 2

Output:61

**Naive approach:** A naive approach is to use Dynamic Programming and Combinatorics, in which we choose any K queries out of Q. The combination which gives the maximum sum of the array will be the answer.

**Time Complexity**: O(N*N*K)

**Efficient Approach:** Since we need to maximize the sum of the array at the end. We just need to choose those queries that affect the maximum number of elements from the array i.e. with bigger ranges. Every query contributes **(R – L + 1)** to the increase in the sum if it is chosen. The sum of the array elements after performing such queries will be **(initial sum of the array + (Contribution of K queries))**.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to perform K queries out ` `// of Q to maximize the final sum ` `int` `getFinalSum(` `int` `a[], ` `int` `n, pair<` `int` `, ` `int` `> queries[], ` ` ` `int` `q, ` `int` `k) ` `{ ` ` ` `int` `answer = 0; ` ` ` ` ` `// Get the initial sum ` ` ` `// of the array ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `answer += a[i]; ` ` ` ` ` `vector<` `int` `> contribution; ` ` ` ` ` `// Stores the contriution of every query ` ` ` `for` `(` `int` `i = 0; i < q; i++) { ` ` ` `contribution.push_back(queries[i].second ` ` ` `- queries[i].first + 1); ` ` ` `} ` ` ` ` ` `// Sort the contribution of queries ` ` ` `// in descending order ` ` ` `sort(contribution.begin(), contribution.end(), ` ` ` `greater<` `int` `>()); ` ` ` ` ` `int` `i = 0; ` ` ` ` ` `// Get the K most contributions ` ` ` `while` `(i < k) { ` ` ` `answer += contribution[i]; ` ` ` `i++; ` ` ` `} ` ` ` ` ` `return` `answer; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `a[] = { 1, 1, 2, 2, 2, 3 }; ` ` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(a[0]); ` ` ` ` ` `pair<` `int` `, ` `int` `> queries[] = { { 0, 4 }, ` ` ` `{ 1, 2 }, ` ` ` `{ 2, 5 }, ` ` ` `{ 2, 3 }, ` ` ` `{ 2, 4 } }; ` ` ` `int` `q = ` `sizeof` `(queries) / ` `sizeof` `(queries[0]); ` ` ` ` ` `int` `k = 3; ` ` ` ` ` `cout << getFinalSum(a, n, queries, q, k); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `//pair class ` `static` `class` `pair ` `{ ` ` ` `int` `first,second; ` ` ` `pair(` `int` `f,` `int` `s) ` ` ` `{ ` ` ` `first = f; ` ` ` `second = s; ` ` ` `} ` `} ` ` ` `// Function to perform K queries out ` `// of Q to maximize the final sum ` `static` `int` `getFinalSum(` `int` `a[], ` `int` `n, pair queries[], ` ` ` `int` `q, ` `int` `k) ` `{ ` ` ` `int` `answer = ` `0` `; ` ` ` ` ` `// Get the initial sum ` ` ` `// of the array ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `answer += a[i]; ` ` ` ` ` `Vector<Integer> contribution = ` `new` `Vector<Integer>(); ` ` ` ` ` `// Stores the contriution of every query ` ` ` `for` `(` `int` `i = ` `0` `; i < q; i++) ` ` ` `{ ` ` ` `contribution.add(queries[i].second ` ` ` `- queries[i].first + ` `1` `); ` ` ` `} ` ` ` ` ` `//compartor ` ` ` `Comparator<Integer> Comp = ` `new` `Comparator<Integer>() ` ` ` `{ ` ` ` `public` `int` `compare(Integer e1,Integer e2) ` ` ` `{ ` ` ` `if` `(e1 > e2) ` ` ` `return` `-` `1` `; ` ` ` `else` ` ` `return` `1` `; ` ` ` `} ` ` ` `}; ` ` ` ` ` `// Sort the contribution of queries ` ` ` `// in descending order ` ` ` `Collections.sort(contribution,Comp); ` ` ` ` ` `int` `i = ` `0` `; ` ` ` ` ` `// Get the K most contributions ` ` ` `while` `(i < k) ` ` ` `{ ` ` ` `answer += (` `int` `) contribution.get(i); ` ` ` `i++; ` ` ` `} ` ` ` ` ` `return` `answer; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `int` `a[] = { ` `1` `, ` `1` `, ` `2` `, ` `2` `, ` `2` `, ` `3` `}; ` ` ` `int` `n = a.length; ` ` ` ` ` `pair queries[] = ` `new` `pair[` `5` `]; ` ` ` `queries[` `0` `] = ` `new` `pair( ` `0` `, ` `4` `); ` ` ` `queries[` `1` `] = ` `new` `pair( ` `1` `, ` `2` `); ` ` ` `queries[` `2` `] = ` `new` `pair( ` `2` `, ` `5` `); ` ` ` `queries[` `3` `] = ` `new` `pair( ` `2` `, ` `3` `); ` ` ` `queries[` `4` `] = ` `new` `pair( ` `2` `, ` `4` `); ` ` ` `int` `q = queries.length; ` ` ` ` ` `int` `k = ` `3` `; ` ` ` `System.out.println( getFinalSum(a, n, queries, q, k)); ` `} ` `} ` ` ` `// This code is contributed by Arnab Kundu ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 implementation of the approach ` ` ` `# Function to perform K queries out ` `# of Q to maximize the final sum ` `def` `getFinalSum(a, n, queries, q, k): ` ` ` `answer ` `=` `0` ` ` ` ` `# Get the initial sum ` ` ` `# of the array ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `answer ` `+` `=` `a[i] ` ` ` ` ` `contribution ` `=` `[] ` ` ` ` ` `# Stores the contriution of every query ` ` ` `for` `i ` `in` `range` `(q): ` ` ` `contribution.append(queries[i][` `1` `]` `-` ` ` `queries[i][` `0` `] ` `+` `1` `) ` ` ` ` ` `# Sort the contribution of queries ` ` ` `# in descending order ` ` ` `contribution.sort(reverse ` `=` `True` `) ` ` ` ` ` `i ` `=` `0` ` ` ` ` `# Get the K most contributions ` ` ` `while` `(i < k): ` ` ` `answer ` `+` `=` `contribution[i] ` ` ` `i ` `+` `=` `1` ` ` ` ` `return` `answer ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `a ` `=` `[` `1` `, ` `1` `, ` `2` `, ` `2` `, ` `2` `, ` `3` `] ` ` ` `n ` `=` `len` `(a) ` ` ` ` ` `queries ` `=` `[[` `0` `, ` `4` `], [` `1` `, ` `2` `], ` ` ` `[` `2` `, ` `5` `], [` `2` `, ` `3` `], ` ` ` `[` `2` `, ` `4` `]] ` ` ` `q ` `=` `len` `(queries); ` ` ` ` ` `k ` `=` `3` ` ` ` ` `print` `(getFinalSum(a, n, queries, q, k)) ` ` ` `# This code is contributed by ` `# Surendra_Gangwar ` |

*chevron_right*

*filter_none*

**Output:**

23

## Recommended Posts:

- Maximize elements using another array
- Maximize the median of the given array after adding K elements to the same array
- Maximize the size of array by deleting exactly k sub-arrays to make array prime
- Rearrange an array to maximize i*arr[i]
- Number of elements greater than K in the range L to R using Fenwick Tree (Offline queries)
- Maximize sum of consecutive differences in a circular array
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Average of remaining elements after removing K largest and K smallest elements from array
- How to make Mergesort to perform O(n) comparisons in best case?
- Elements to be added so that all elements of a range are present in array
- Find elements larger than half of the elements in an array
- Find Kth element in an array containing odd elements first and then even elements
- Find all elements in array which have at-least two greater elements
- Check whether an array can be fit into another array rearranging the elements in the array
- Print elements of an array according to the order defined by another array | set 2

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.