Slow Sums Algorithm
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
The Slow Sums problem is a greedy algorithm puzzle: given an array of positive integers, you repeatedly pick two numbers, replace them with their sum, and accumulate a penalty equal to that sum. The goal is to maximize the total penalty. The key insight is that numbers added earlier contribute to more penalties — so you should always sum the two largest numbers first.
Problem Statement
Given an array of positive integers, repeatedly perform the following until one number remains:
- Pick any two numbers from the array
- Replace them with their sum
- Add the sum to a running penalty total
Maximize the total penalty.
Example
Array: [4, 2, 1, 3]
Greedy (optimal) — sum largest first:
Sort descending: [4, 3, 2, 1]
- Sum 4 + 3 = 7, penalty = 7, array =
[7, 2, 1] - Sum 7 + 2 = 9, penalty = 7 + 9 = 16, array =
[9, 1] - Sum 9 + 1 = 10, penalty = 16 + 10 = 26, array =
[10]
Total penalty: 26
Non-optimal — sum smallest first:
- Sum 1 + 2 = 3, penalty = 3, array =
[4, 3, 3] - Sum 3 + 3 = 6, penalty = 3 + 6 = 9, array =
[4, 6] - Sum 4 + 6 = 10, penalty = 9 + 10 = 19, array =
[10]
Total penalty: 19 (less than 26)
Why Greedy Works
When you sum two numbers, the result carries forward into future sums. A number that is summed in step 1 participates in steps 2, 3, and so on — its value is effectively counted multiple times. By summing the largest numbers first, they accumulate the most repeated contributions to the penalty.
Formally: if you sort the array in descending order as a[0] >= a[1] >= ... >= a[n-1], the maximum penalty is:
Wait — that is the formula for a different formulation. The actual implementation is simpler: just always combine the two largest elements.
Implementation
Python
Alternative: Sort-Based (Simpler)
Since after each combination the result is always >= any remaining element (we combined the two largest), we can use a simple sorted approach:
This works because after sorting, we always add the next-largest number to the running sum. Each addition produces a penalty equal to the running sum.
C++
JavaScript
Time Complexity
| Approach | Time | Space |
| Max-heap | O(n log n) | O(n) |
| Sort-based | O(n log n) sort + O(n) scan | O(1) extra |
Both approaches are O(n log n) overall. The sort-based approach is simpler and has better constants.
Proof of Correctness
Claim: Always combining the two largest numbers maximizes the total penalty.
Intuition: Each number's contribution to the total penalty equals its value multiplied by the number of times it participates in a combination. Numbers combined earlier participate in more subsequent combinations. By prioritizing larger numbers, their multiplicative effect is maximized.
Exchange argument: Suppose an optimal solution combines two non-largest numbers a and b (where a larger number c exists). Swapping to combine c instead of the smaller number always increases or maintains the penalty, because c > a (or c > b) and the larger value contributes to more future sums.
Variations
Minimize Penalty (Fast Sums)
The opposite problem — minimize the total penalty by summing the smallest first:
This is equivalent to the optimal merge pattern (Huffman coding without the tree).
Common Pitfalls
- Integer overflow: For large arrays with large values, the penalty can exceed 32-bit integer range. Use
long long(C++),BigInt(JavaScript), or Python's arbitrary-precision integers. - Using a min-heap instead of max-heap: The problem asks to maximize penalty, so you need a max-heap. In Python, negate values to simulate a max-heap with
heapq. - Not recognizing the sort optimization: The sort-based approach is simpler and faster than the heap approach for this specific problem. After sorting, the running sum is always the largest element, so no re-heaping is needed.
- Confusing with Huffman coding: The minimum penalty version (sum smallest first) is equivalent to Huffman coding. The maximum penalty version (this problem) is the opposite — sum largest first.
- Empty or single-element arrays: Handle edge cases: an empty array has 0 penalty, and a single-element array also has 0 penalty (no combinations possible).
Summary
- Always combine the two largest numbers to maximize the total penalty
- Sort the array descending and accumulate a running sum — O(n log n) time, O(1) extra space
- Alternatively, use a max-heap for a more general approach
- The opposite problem (minimize penalty) sums the two smallest — equivalent to Huffman coding
- Total penalty can be very large — use 64-bit integers or arbitrary-precision arithmetic
Related reading
- Smallest n-bit integer c that has k 1-bits and is the sum of two n-bit integers that have g, h bits set to 1dynamic programming
- Smallest window substring that has both uppercase and corresponding lowercase characters
- Smart pagination algorithm that works with local data cache
- Smart progress bar ETA computation
- Slow wordpress in eks cluster
- SNIReadSyncOverAsync Performance issue
- Smallest number that cannot be formed from sum of numbers from array
- Smallest number that is evenly divisible by all of the numbers from 1 to 20?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.