# Maximum sum of pair values such that value of pairs of same group should be a multiple of i in range [1, N]

Given an array of pairs **arr[] **of size **N,** where the first element of the pair is the value of the pair and the second element is the group at which this pair belongs, the task is to find the maximum sum of pair values, such that the number of pairs of the same group should be a multiple of** i** for all** i **in the range** [1, N]**.

**Examples:**

Input:arr[] = {{5, 3}, {9, 3}, {6, 3}, {7, 3}, {9, 3}, {7, 3}}, N = 6Output :{43, 43, 43, 32, 38, 43}Explanation:

There are 6 pairs of groups 3.

For i = 1, select all the pairs of group 3, since 6 % 1 = 0, then the sum will be 5 + 9 + 6 + 7 + 9 + 7 = 43.

For i = 2, select all the pairs of group 3 since 6 % 2 = 0, then the sum will be 5 + 9 + 6 + 7 + 9 + 7 = 43.

For i = 3, select all the pairs of group 3 since 6 % 3 = 0, then the sum will be 5 + 9 + 6 + 7 + 9 + 7 = 43.

For i = 4, select 4 pairs of group 3 whose sum of value is largest, then the sum will be 9 + 9 + 7 + 7 = 32.

For i = 5, select 5 pairs of group 3 whose sum of value is largest, then the sum will be 9 + 9 + 7 + 7 + 6 = 38.

For i = 6, select all the pairs of group 3 since 6 % 6 = 0, then the sum will be 5 + 9 + 6 + 7 + 9 + 7 = 43.

Input:arr[] = {{6, 1}, {8, 2}, {3, 1}, {1, 2}, {5, 1}, {1, 2}, {5, 1}}, N = 7Output :{29, 28, 26, 19, 0, 0, 0}

**Approach: **The simplest idea to solve the problem is to segregate the elements of the array on the basis of the group and then use the prefix sum array to find the sum of elements from each group in **O(1)**. Follow the steps below to solve the problem:

- Initialize a hash map
**m**with the first element as integer and the second element as vector. - Iterate in the range
**[1, N]**using the variable**i**and insert all the pairs of the vector in their respective groups. - Sort all the vectors in the hash map in ascending order and store them in a vector say
**v**. - Initialize vectors say,
**ans**of size**N**and**it**to store answers for all**i**in the range**[1, N]**and - Iterate in the range
**[0, v.size()-1]**using the variable**i**and perform the following steps:- Initialize a vector
**c**and Iterate in the range**[0, v[i].size()-1]**using the variable**j**and perform the following steps:- If
**j**is equal to**0**, then append**v[i][j]**to the vector**c,**otherwise append**c.back() + v[i][j].**

- If
- Insert the vector
**c**in the vector**it**.

- Initialize a vector
- Now, iterate through the vector
**it**using the variable**i**and perform the following steps:- Iterate in the range
**[1, it[i].size()]**using the variable**j**and perform the following steps:- Store the number of elements that cannot be taken if
**j**elements are chosen from the vector**it[i]**in the variable**left**. - Add
**it[i].back() – it[i][left-1]**(Subtracting the left smallest number) in the**ans[j-1]**.

- Store the number of elements that cannot be taken if

- Iterate in the range
- After performing the above steps, print the vector
**ans**.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` ` ` `// Function to find the maximum sum of` `// pair values, such that the number of` `// pairs of the same group should be a` `// multiple of i for all i in the` `// range [1, N]` `void` `findMaximumSumValue(vector<pair<` `int` `, ` `int` `> > arr, ` `int` `n)` `{` ` ` ` ` `// Map for storing elements of same group` ` ` `// together` ` ` `map<` `int` `, vector<` `int` `> > mp;` ` ` ` ` `// Segregate students on the basis of group` ` ` `for` `(` `int` `i = 0; i < n; i++) {` ` ` `mp[arr[i].second - 1].push_back(arr[i].first);` ` ` `}` ` ` ` ` `vector<vector<` `int` `> > v;` ` ` ` ` `// Pushing all the vectors in the map to v` ` ` `for` `(` `auto` `i : mp) {` ` ` `v.push_back(i.second);` ` ` ` ` `// Sort all the groups in ascending order` ` ` `sort(v.back().begin(), v.back().end());` ` ` `}` ` ` ` ` `// Vector to store answer` ` ` `vector<` `int` `> ans(n, 0);` ` ` ` ` `// Vector to store prefix sum array` ` ` `// for each group` ` ` `vector<vector<` `int` `> > it;` ` ` ` ` `// Traverse through the groups` ` ` `for` `(` `auto` `i : v) {` ` ` `vector<` `int` `> c;` ` ` ` ` `// Save prefix sum array in vector C` ` ` `for` `(` `int` `j = 0; j < (` `int` `)i.size(); j++) {` ` ` `if` `(j == 0) {` ` ` `c.push_back(i[j]);` ` ` `}` ` ` `else` `{` ` ` `c.push_back(c.back() + i[j]);` ` ` `}` ` ` `}` ` ` ` ` `// Insert the prefix sum c in it` ` ` `it.push_back(c);` ` ` `}` ` ` ` ` `// Traverse through the prefix function of each group` ` ` `for` `(` `auto` `i : it) {` ` ` ` ` `// Traverse for all number of elements in i` ` ` `for` `(` `int` `j = 1; j <= (` `int` `)i.size(); j++) {` ` ` ` ` `// Check the number students to be left.` ` ` `int` `left = (` `int` `)i.size() % j;` ` ` `int` `del = 0;` ` ` ` ` `// If left is greater than 0 subtract the` ` ` `// value of left smallest elements` ` ` `if` `(left > 0)` ` ` `del = i[left - 1];` ` ` `ans[j - 1] += (i.back() - del);` ` ` `}` ` ` `}` ` ` ` ` `// Print the answer list for every` ` ` `// possible size` ` ` `for` `(` `int` `i = 0; i < n; i++) {` ` ` `cout << ans[i] << ` `" "` `;` ` ` `}` ` ` ` ` `cout << endl;` `}` ` ` `// Driver Code` `int` `main()` `{` ` ` `// Given Input` ` ` `vector<pair<` `int` `, ` `int` `> > arr` ` ` `= { { 5, 3 }, { 9, 3 }, { 6, 3 },` ` ` `{ 7, 3 }, { 9, 3 }, { 7, 3 } };` ` ` ` ` `int` `N = arr.size();` ` ` ` ` `// Function Call` ` ` `findMaximumSumValue(arr, N);` ` ` ` ` `return` `0;` `}` |

**Output:**

43 43 43 32 38 43

**Time Complexity: **O(N^{2})**Auxiliary Space: **O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend **live classes **with experts, please refer **DSA Live Classes for Working Professionals **and **Competitive Programming Live for Students**.