# from itertools import permutations # n = 6 # kumiawase = list(permutations(range(1, n + 1), n)) # ans = 0 # goukei = n * (n + 1) // 2 # for kumi1 in kumiawase: # expected = n * (n + 1) // 2 # for i in range(n): # if kumi1[i] == n: # expected -= 2 * (i + 1) # for kumi2 in kumiawase: # res = 0 # for i in range(n): # p = kumi1[i] # k = kumi2[i] # if k > p: # res += i + 1 # elif p > k: # res -= i + 1 # if res > expected: # print(kumi1, kumi2, res, expected) # ans = max(ans, res) # print(ans) n = int(input()) P = list(map(int, input().split())) PP = [(p, i) for i, p in enumerate(P)] PP.sort(reverse=True) ans = 0 for idx, (p, i) in enumerate(PP): temp = (n - idx) * (n - idx + 1) // 2 # print(idx, temp) ans = max(ans, temp - (2 * (i + 1))) print(max(0, ans))