Calculate the sum of the initial array and store the frequency of each element in map
for ( var i = 0 ; i < N ; i ++ ) { sum += A [ i ] ; if ( count . has ( A [ i ] ) ) count . set ( A [ i ] , count . get ( A [ i ] ) + 1 ) else count . set ( A [ i ] , 1 ) }