#P15127. [ROIR 2026] 比赛结果

    ID: 17038 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 显示难度普及− 上传者: 标签>递推排序2026ROIR(俄罗斯)

[ROIR 2026] 比赛结果

Problem Description

Students from the informatics group "Kapibara Programming" took part in a contest. According to the results, the student in position ii scored aia_i points.

To encourage the participants, the group leader Alexander Igorevich decided to give the students candies. For all ii and jj, if the student in position ii has a higher score than the student in position jj, then the leader will give the student in position ii ai−aja_i - a_j candies.

Help the leader compute how many candies he needs to prepare to distribute to the students.

Input Format

The first line of the input gives an integer nn — the number of students (1≤n≤500 0001 \le n \le 500\,000).

The second line contains nn integers aia_i — the contest scores of the group members (0≤ai≤1070 \le a_i \le 10^7).

Output Format

Output one number — the total number of candies that need to be prepared for distribution to the students.

5
1 2 3 4 5
20
10
0 0 0 0 0 10000000 0 0 0 0
90000000

Hint

Note

Please note that the answer to this problem may exceed the possible value of a 32-bit integer variable, so you must use a 64-bit integer data type (int64 in Pascal, long long in C++, long in Java and C#).

Sample Explanation

In the first sample, the first student will not receive any candies, the second student will receive 1 candy, the third student will receive 1+2=31+2=3 candies, the fourth student will receive 1+2+3=61+2+3=6 candies, and the fifth student will receive 1+2+3+4=101+2+3+4=10 candies.

Scoring Rules

The score for each subtask is awarded only if all testdata in that subtask and all its required subtasks are passed.

Subtask Points Additional Constraints Required Subtasks
1 15 1≤n≤1 0001 \le n \le 1\,000
2 5 All aia_i are the same
3 For any i≠ji \ne j, ai≠aja_i \ne a_j, and 1≤ai≤n1 \le a_i \le n
4 10 0≤ai≤10 \le a_i \le 1
5 15 0≤ai≤1000 \le a_i \le 100 4
6 There are at most two distinct values among aia_i 2, 4
7 35 None 1–6

Translated by ChatGPT 5