#P9143. [THUPC 2023 初赛] 众数

[THUPC 2023 初赛] 众数

Problem Description

You have several positive integers in [1,n][1,n]: for 1in1 \le i \le n, you have aia_i copies of the integer ii. Let S=i=1naiS = \sum_{i=1}^n a_i.

For a sequence p1,p2,,plp_1,p_2,\cdots,p_l, define its mode maj(p1,p2,,pl)\text{maj}(p_1,p_2,\cdots,p_l) as the number that appears the most times. If multiple numbers are tied for the highest frequency, the largest of them is the mode.

Now you need to arrange these SS numbers into a sequence b1,b2,,bSb_1,b_2,\cdots,b_S such that i=1Smaj(b1,b2,,bi)\sum_{i=1}^S \text{maj}(b_1,b_2,\cdots,b_i) is maximized. Output this maximum value.

Input Format

The first line contains an integer nn, representing the value range.

The next line contains nn positive integers a1,a2,,ana_1,a_2,\cdots,a_n, representing the count of each number.

Output Format

Output one positive integer, the maximum value of i=1Smaj(b1,b2,,bi)\sum_{i=1}^S \text{maj}(b_1,b_2,\cdots,b_i).

3
1 3 2
17

Hint

Sample Explanation 1

One sequence that achieves the maximum value is (3,2,3,1,2,2)(3,2,3,1,2,2).

Constraints

For all testdata, 1n1051 \le n \leq 10^5 and 1a1,a2,,an1051 \le a_1,a_2,\cdots,a_n \le 10^5.

Source

From the 2023 Tsinghua University Student Algorithm Contest and Intercollegiate Invitational (THUPC2023) Preliminary Round.

Solutions and other resources can be found at https://github.com/THUSAAC/THUPC2023-Pre.

Translated by ChatGPT 5