https://www.acmicpc.net/problem/1041

So i got the mathematical pattern right. BUT actually we cant just simply sort the given list because the number on the face of a die cannot be sorted however we please. So lets observe the pattern. We can see that A pairs with F on the opposite side, B pairs with E and C with D. So we choose the min of the 3 pairs into a list and sort them. For 3 face of a die, we sum them all up. For 2 face of a die we get the first and second minimum value and so on.
The reason why we can do this is regardless of whether we choose A or F, or B or E, or C or D, we are gonna get 3 bordering faces of a die. You can imagine that die in your head and see any combination will give us 3 bordering faces.
n = int(input())
lst = list(map(int, input().split()))
if n == 1:
print(sum(lst) - max(lst))
exit()
else:
target = []
for i in range(3):
target.append(min(lst[i], lst[-1 - i]))
target.sort()
ans = 0
x, y, z = target[0], target[0] + target[1], sum(target)
ans += z * 4
ans += y * (n - 1) * 4 + y * (n - 2) * 4
ans += x * (n - 2) ** 2 * 5 + x * (n - 2) * 4
print(ans)
n space and n log n time (cuz of sort)