Problem · Greedy

Maximize Total Memory Points

Learn this problem
MediumAmazonINTERNOA
See Amazon hiring insights

Problem statement

FastPrep.io Academy has recently introduced an exciting new course focused on Quantum Physics, providing learners with an opportunity to explore the fascinating world of quantum mechanics and its applications.

The course has n chapters, where the i-th chapter has memory[i] memory points, which a student gains or loses while reading that chapter. The course follows a specific requirement: before studying a chapter, the student must revisit all the previously read chapters.

This means that when reading the chapter placed at position i in the chosen reading order, the student accumulates a total of memory[0] + memory[1] + ... + memory[i] memory points, where the indices refer to the chapters in the order chosen by the student. The total memory points is the sum of the memory points gained while reading each chapter.

Students have the flexibility to read the chapters in any order and aim to maximize their total memory points. The goal is to determine the maximum possible total memory points a student can accumulate while ensuring that all chapters are read at least once.

Your task is to find and return the highest possible total memory points that a student can achieve by choosing the optimal reading order.

Function

maximizeTotalMemoryPoints(memory: int[]) → long

Examples

Example 1

memory = [3, 4, 5]return = 26
Considering all permutations of reading the chapters and their total memory points: • [3, 4, 5] → Corresponding memory points will be [3, 7, 12], thus total memory points will be 3 + 7 + 12 = 21 • [3, 5, 4] → Corresponding memory points will be [3, 8, 12], thus total memory points will be 3 + 8 + 12 = 23 • [4, 3, 5] → Corresponding memory points will be [4, 7, 12], thus total memory points will be 4 + 7 + 12 = 23 • [4, 5, 3] → Corresponding memory points will be [4, 9, 12], thus total memory points will be 4 + 9 + 12 = 25 • [5, 3, 4] → Corresponding memory points will be [5, 8, 12], thus total memory points will be 5 + 8 + 12 = 25 • [5, 4, 3] → Corresponding memory points will be [5, 9, 12], thus total memory points will be 5 + 9 + 12 = 26

Constraints

  • 1 ≤ n ≤ 10^5
  • -10^3 ≤ memory[i] ≤ 10^3

More Amazon problems

drafts saved locally
public long maximizeTotalMemoryPoints(int[] memory) {
  // write your code here
}
memory[3, 4, 5]
expected26
checking account