Light's Perfect Victory
View as PDFLight Yagami is pretending to work with the Japanese Task Force in order to uncover the true
identity of mass murderer 'Kira' (it's Light). His job is to investigate a given group of suspects,
, which is represented by
integers.
Every day, he must split each available group of suspects into two non-empty subgroups so that
it seems like he investigated them. However, to avoid skepticism from Ryuzaki, if a group has only
suspect, that group cannot be split further and thus cannot be included in the investigation from
the next day onwards.
The value of each suspect still in the investigation is added to the score at the end of each day.
The investigation ends when there are no more groups of suspects left. What is the maximum possible score Light can achieve?
Input
The first line contains a single integer , representing the size of the initial group
.
The second line contains integers
, representing the suspects.
Output
A line containing the maximum score achievable.
Constraints
for all
Example 1
Input
3
8 6 7
Output
36
Explanation
Light initially has the group of suspects .
- Light splits it into the two groups
and
for investigation. The score is
at the end of the day, and
is discarded.
- Light now has the group
, which he splits into
and
. The score is now
and both groups are no longer part of the investigation.
- There are no more suspects left, so the investigation is complete with a final score of
(which is also the maximum attainable score).
Example 2
Input
5
4 4 1 9 2
Output
69
Example 3
Input
1
1000
Output
1000
Comments