Created
February 18, 2021 05:21
This is my code of Problem D of Panasonic Programming Contest (AtCoder Beginner Contest 186).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 入力 | |
N = int(input()) | |
A = sorted([int(x) for x in input().split()]) | |
# 答えを格納する変数 | |
ans = 0 | |
# Aの累積和を格納する配列 | |
Aruiseki = [[] for _ in range(N)] | |
# Aの累積和を計算しておく | |
Aruiseki[N-1] = A[N-1] | |
for i in range(N-2,-1,-1): | |
Aruiseki[i] = Aruiseki[i+1] + A[i] | |
# Aの累積和を利用して計算する | |
# 配列のindexに注意 | |
for i in range(N-1): | |
ans += Aruiseki[i+1] - (N - i - 1) * A[i] | |
# 答えを出力 | |
print(ans) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment