Given an array of N integers, you have to select all of these integers in any order. For every integer you select, you get points equal to the value of: **the selected integer * number of integers selected before the current integer.** Your task is to maximize these points.

**Note: **You can select every integer exactly 1 time.

**Examples:**

Input: a = {1, 4, 2, 3, 9}

Output: 56

The optimal solution for this array would be select the numbers in the order 1, 2, 3, 4, 9 that gives points 0, 2, 6, 12, 36 respectively and giving a total maximum points of 56.

Input: a = {1, 2, 2, 4, 9}

Output: 54

The optimal solution for this array would be select the numbers in the order 1, 2, 2, 4, 9 that gives points 0, 2, 4, 12, 36 respectively and giving a total maximum points of 54.

The idea is to use Greedy Approach, i.e., maximize multiplier for the largest element.

We sort the given array and start picking elements in this sorted manner, starting with the first element.

Below is the implementation of the above approach:

## C++

`// C++ program for the Optimal Solution ` `#include <bits/stdc++.h> ` `#include <iostream> ` `using` `namespace` `std; ` ` ` ` ` `// Function to calculate the maximum points ` ` ` `// earned by making an optimal selection on ` ` ` `// the given array ` ` ` `static` `int` `findOptimalSolution(` `int` `a[], ` `int` `N) ` ` ` `{ ` ` ` `// Sorting the array ` ` ` `sort(a, a+N); ` ` ` `// Variable to store the total points earned ` ` ` `int` `points = 0; ` ` ` ` ` `for` `(` `int` `i = 0; i < N; i++) { ` ` ` `points += a[i] * i; ` ` ` `} ` ` ` `return` `points; ` ` ` `} ` ` ` ` ` `// Driver code ` `int` `main() { ` ` ` `int` `a[] = { 1, 4, 2, 3, 9 }; ` ` ` `int` `N = ` `sizeof` `(a)/` `sizeof` `(a[0]); ` ` ` `cout<<(findOptimalSolution(a, N)); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program for the Optimal Solution ` `import` `java.io.*; ` `import` `java.util.*; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to calculate the maximum points ` ` ` `// earned by making an optimal selection on ` ` ` `// the given array ` ` ` `static` `int` `findOptimalSolution(` `int` `[] a, ` `int` `N) ` ` ` `{ ` ` ` `// Sorting the array ` ` ` `Arrays.sort(a); ` ` ` ` ` `// Variable to store the total points earned ` ` ` `int` `points = ` `0` `; ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < N; i++) { ` ` ` `points += a[i] * i; ` ` ` `} ` ` ` `return` `points; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `[] a = { ` `1` `, ` `4` `, ` `2` `, ` `3` `, ` `9` `}; ` ` ` `int` `N = a.length; ` ` ` `System.out.println(findOptimalSolution(a, N)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the Optimal Solution ` ` ` `# Function to calculate the maximum points ` `# earned by making an optimal selection on ` `# the given array ` `def` `findOptimalSolution(a, N) : ` ` ` ` ` `# Sorting the array ` ` ` `a.sort() ` ` ` ` ` `# Variable to store the total points earned ` ` ` `points ` `=` `0` ` ` ` ` `for` `i ` `in` `range` `(` `0` `, N): ` ` ` `points ` `+` `=` `a[i] ` `*` `i ` ` ` ` ` `return` `points ` ` ` ` ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `a ` `=` `[` `1` `, ` `4` `, ` `2` `, ` `3` `, ` `9` `] ` ` ` `N ` `=` `len` `(a) ` ` ` `print` `(findOptimalSolution(a, N)) ` |

*chevron_right*

*filter_none*

## C#

`//C# program for the Optimal Solution ` `using` `System; ` ` ` `public` `class` `GFG{ ` ` ` ` ` `// Function to calculate the maximum points ` ` ` `// earned by making an optimal selection on ` ` ` `// the given array ` ` ` `static` `int` `findOptimalSolution(` `int` `[]a, ` `int` `N) ` ` ` `{ ` ` ` `// Sorting the array ` ` ` `Array.Sort(a); ` ` ` ` ` `// Variable to store the total points earned ` ` ` `int` `points = 0; ` ` ` ` ` `for` `(` `int` `i = 0; i < N; i++) { ` ` ` `points += a[i] * i; ` ` ` `} ` ` ` `return` `points; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `static` `public` `void` `Main (){ ` ` ` `int` `[] a = { 1, 4, 2, 3, 9 }; ` ` ` `int` `N = a.Length; ` ` ` `Console.WriteLine(findOptimalSolution(a, N)); ` ` ` `} ` `//This code is contributed by ajit ` `} ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program for the Optimal Solution ` ` ` `// Function to calculate the maximum ` `// points earned by making an optimal ` `// selection on the given array ` `function` `findOptimalSolution(` `$a` `, ` `$N` `) ` `{ ` ` ` `// Sorting the array ` ` ` `sort(` `$a` `); ` ` ` ` ` `// Variable to store the ` ` ` `// total points earned ` ` ` `$points` `= 0; ` ` ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$N` `; ` `$i` `++) ` ` ` `{ ` ` ` `$points` `+= ` `$a` `[` `$i` `] * ` `$i` `; ` ` ` `} ` ` ` `return` `$points` `; ` `} ` ` ` `// Driver code ` `$a` `= ` `array` `( 1, 4, 2, 3, 9 ); ` `$N` `= sizeof(` `$a` `); ` ` ` `echo` `(findOptimalSolution(` `$a` `, ` `$N` `)); ` ` ` `// This code is contributed by ajit ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

56

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.

## Recommended Posts:

- Rearrange the array to maximize the number of primes in prefix sum of the array
- Rearrange Array to maximize number having Array elements as digits based on given conditions
- Rearrange an Array such that Sum of same-indexed subsets differ from their Sum in the original Array
- Rearrange array such that all even-indexed elements in the Array is even
- Rearrange an array to make similar indexed elements different from that of another array
- Rearrange array to make it non-decreasing by swapping pairs having GCD equal to minimum array element
- Rearrange an array so that arr[i] becomes arr[arr[i]] with O(1) extra space
- Rearrange array in alternating positive & negative items with O(1) extra space | Set 1
- Rearrange an array such that 'arr[j]' becomes 'i' if 'arr[i]' is 'j' | Set 1
- Rearrange an array in maximum minimum form | Set 1
- Rearrange array in alternating positive & negative items with O(1) extra space | Set 2
- Rearrange an array to minimize sum of product of consecutive pair elements
- Rearrange an array in maximum minimum form | Set 2 (O(1) extra space)
- Rearrange an array such that every odd indexed element is greater than it previous
- Rearrange an array in order - smallest, largest, 2nd smallest, 2nd largest, ..
- Rearrange array such that even index elements are smaller and odd index elements are greater
- Rearrange array such that even positioned are greater than odd
- Rearrange array such that arr[i] >= arr[j] if i is even and arr[i]<=arr[j] if i is odd and j < i
- Rearrange all elements of array which are multiples of x in increasing order
- Rearrange all elements of array which are multiples of x in decreasing order

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.