If the list is sorted, you can consider all subsets for size 1, then 2, and then 3, N. The algorithm is initially somewhat inefficient, but the optimized version is lower. Here is some pseudo code.
let A = {1, 2, 3} let total_sum = 0 for set_size <- 1 to N total_sum += sum(A[1:N-(set_size-1)])
First, sets with one element: {{1}, {2}, {3}}: summarize each of the elements.
Then the sets of two elements {{1, 2}, {2, 3}}: summarize each element, but the last.
Then the sets of three elements {{1, 2, 3}}: summarize each element, but the last two.
But this algorithm is inefficient. To optimize to O (n), multiply each i-th element by Ni and add (indexing from zero here). The intuition is that the first element is a minimum of N sets, the second is a minimal set of N-1, etc.
I know this is not a python question, but sometimes the code helps:
A = [1, 2, 3]
source share