[Community Puzzle] Add'em Up

my solution suggests to take the array say A[0,n-1] and store the first two minimum in another array which is B, and try to do the following :slight_smile:
if min(A[j,n/2]) + first_min(B) < first_min(B) + second_min(B)
then push the min(A[j,n/2]) + first_min(B) in B (add this amount to the cost) otherwise push first_min(B) + second_min(B)(add this amount to the cost) (j iterate over the array A). then in the second iteration assign B to A → A[0,n/2] (let’s say n is even.) store the first two min of A in B (after deallocating it and assign a new instance ) and repeate the above process …
by using this approach the size of A will be decreasing by the half ,untile you get A with one entry . then stop the algorithm . this approach works fine, but it cannot pass the validator 6 after submitting , i would to know what this validator tests ???

min(A[j,n/2]) + first_min(B) < first_min(B) + second_min(B)
is just the same as:
min(A[j,n/2]) < second_min(B).

Also, what is j?

If the initial card values are [3, 6, 6, 4, 2, 3, 5, 2, 5], can you show what each step of your card-adding is like?

j is an iterator that iterate over A

You mentioned that in your earlier post, but from which index? From 0? Are the two values in B included in the iteration?

Also, if the initial card values are [3, 6, 6, 4, 2, 3, 5, 2, 5], can you show what each step of your card-adding is like?

Are you asking me to get The Solution or to solve my issue??,

I’m asking to you to understand how your algorithm actually works, so that I may try to suggest a fix for you.

Ok im going to add a Picture

i cannot upload an image, but i get the ansere as 1253

Can you show me every step? What do the arrays look like after each step?

the result is 112 excuse me

Can you also show like:
Step 1: _ and _ are merged, the cards are now: _______
Step 2: _ and _ are merged, the cards are now: _______
Step 3: _ and _ are merged, the cards are now: _______
etc?

9
3 6 6 4 2 3 5 2 5
[3]-[6]-[6]-[4]-[2]-[3]-[5]-[2]-[5]-
====================== iteration 1 ==============
get first min: 2, the A array becomes: [2]-[6]-[6]-[4]-[3]-[3]-[5]-[2]-[5]-
get second min: 2, the A array becomes: [2]-[2]-[6]-[4]-[3]-[3]-[5]-[6]-[5]-
after iterating array A array B is : [8]-[12]-[10]-[6]-
====================== iteration 2 ==============
get first min: 6, the A array becomes: [6]-[12]-[10]-[8]-
get second min: 8, the A array becomes: [6]-[8]-[10]-[12]-
after iterating array A array B is : [14]-[22]-
====================== iteration 3 ==============
get first min: 14, the A array becomes: [14]-[22]-
get second min: 22, the A array becomes: [14]-[22]-
after iterating array A array B is : [36]-
function result: 112

Can you explain a bit more how you get [8]-[12]-[10]-[6] in iteration 1?

first of all let’s agree with some things:
first min => take the min of an array A[1,j] and put it in A[1], then return its value.
second min => take the min of the array A[2,j] and put it in A[2] , then return its value.

the algorithm consists of the following code .
let sum an array.
let A the array of the cards values

[
[
take the first and second min of A and place their sum in sum .
now iterate over A with the variable j that start from 3 (the arrays in programming start from 0 in this case j should start from 2).
now sum is {4}
test if the first min of sum is less than the second min of A[j,…]
if so , add the first min of A[j,…] to sum[1] because get min places the min at first entry .
otherwise push the value first min + second min in sum array.
]each time you get out from this loop assign sum to A] repeat untile A contains only one entry

the idea is to merge only the small numbers, these can be either an merge result cards or a new card. sum contains cards that are already merged ,so if the min of sum is less than merging the two first mins of the rest cards we take only the min of sum + the first min of rest otherwise we merge new card and push it in sum

This is useful for your debugging:

Input

10
20 40 60 80 100 2 4 6 8 10

Output

866

I just want to thank you
I read the whole conversation and it helped a lot :grin:
thank you