description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
N = int(input()) arr = input() arr = list(map(int, arr.split(" "))) dict1 = {} dict2 = {} ans = 0 for i in range(0, N): ans += i * arr[i] ans -= (N - i - 1) * arr[i] for i in range(0, N): op1 = 0 if arr[i] - 1 in dict1: op1 = dict1[arr[i] - 1] op2 = 0 if arr[i] + 1 in dict1: op2 = dict1[arr[i] + 1] op1 += op2 ans -= op1 * arr[i] if arr[i] in dict1: dict1[arr[i]] += 1 else: dict1[arr[i]] = 1 for i in range(N - 1, -1, -1): op1 = 0 if arr[i] - 1 in dict2: op1 = dict2[arr[i] - 1] op2 = 0 if arr[i] + 1 in dict2: op2 = dict2[arr[i] + 1] op1 += op2 ans += op1 * arr[i] if arr[i] in dict2: dict2[arr[i]] += 1 else: dict2[arr[i]] = 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) arr = [int(x) for x in input().split()] sm = sum(arr) d = {} for i in arr: d[i] = d.get(i, 0) + 1 ans = 0 cnt = n for i in range(n): temp = sm temp -= d.get(arr[i], 0) * arr[i] temp -= d.get(arr[i] - 1, 0) * (arr[i] - 1) temp -= d.get(arr[i] + 1, 0) * (arr[i] + 1) val = ( temp - (cnt - d.get(arr[i], 0) - d.get(arr[i] + 1, 0) - d.get(arr[i] - 1, 0)) * arr[i] ) ans += val d[arr[i]] -= 1 sm -= arr[i] cnt -= 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) numbers = list(map(int, input().split())) count = {} pre_sum = 0 ans = 0 for i in range(len(numbers)): cnt = 0 sum = 0 for j in range(-1, 2): cnt += count[numbers[i] + j] if numbers[i] + j in count else 0 sum += ( (numbers[i] + j) * count[numbers[i] + j] if numbers[i] + j in count else 0 ) ans += numbers[i] * (i - cnt) - (pre_sum - sum) pre_sum += numbers[i] if numbers[i] not in count: count[numbers[i]] = 0 count[numbers[i]] += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) ans = 0 a = [int(i) for i in input().split(" ")] c = dict() for i in a: c[i] = c[i - 1] = c[i + 1] = 0 for i in range(n): x = a[i] c[x] = c[x] + 1 ans = ans + c[x + 1] - c[x - 1] + x * (i + 1 - n + i) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) dic = {} ans = s = 0 k = input().split() for i in range(n): a = int(k[i]) if a - 1 in dic: b = dic[a - 1] else: b = 0 if a in dic: c = dic[a] else: c = 0 dic.update({a: 0}) if a + 1 in dic: d = dic[a + 1] else: d = 0 ans += a * (i - b - c - d) - s + b * (a - 1) + c * a + d * (a + 1) dic[a] += 1 s += a print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR DICT VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) array = list(map(int, input().split())) d = dict() t, c = 0, 0 for i in range(len(array)): c += array[i] t += (i + 1) * array[i] - c if array[i] in d: d[array[i]] += 1 else: d[array[i]] = 1 if array[i] + 1 in d: t += d[array[i] + 1] if array[i] - 1 in d: t -= d[array[i] - 1] print(t)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) a = list(map(int, input().split())) m = {} s = 0 for i in range(0, len(a)): if a[i] in m: m[a[i]] = m[a[i]] + 1 else: m[a[i]] = 1 if a[i] + 1 in m: s += m[a[i] + 1] * 1 if a[i] - 1 in m: s += m[a[i] - 1] * -1 t = 2 * i - n + 1 s += t * a[i] print(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) arr = input().split() b = [] for x in arr: b.append(int(x)) cnt = {} ans = 0 for i in range(n): ans += b[i] * i + -b[i] * (n - i - 1) for i in range(n): if b[i] - 1 in cnt.keys(): ans -= cnt[b[i] - 1] if b[i] + 1 in cnt.keys(): ans += cnt[b[i] + 1] if b[i] in cnt.keys(): cnt[b[i]] += 1 else: cnt[b[i]] = 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) lis = input().split() a = list() sum = [0] dic = dict() for i in range(n): a.append(int(lis[i])) sum.append(sum[i] + a[i]) dic[a[i]] = 0 ans = 0 for i in range(n): ans = ans + (i * a[i] - sum[i]) if a[i] - 1 in dic.keys(): ans = ans - dic[a[i] - 1] if a[i] + 1 in dic.keys(): ans = ans + dic[a[i] + 1] dic[a[i]] = dic[a[i]] + 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) arr = list(map(int, input().split())) ans = 0 mp = {} dict = {} for i in range(0, n): mp[arr[i]] = 0 for i in range(0, n): x = arr[i] ans = ans + i * x - (n - i - 1) * x if x + 1 in mp: ans = ans + mp[x + 1] if x - 1 in mp: ans = ans - mp[x - 1] mp[x] = mp[x] + 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) a = [int(i) for i in input().split()] ans = 0 map = dict() for i in range(0, n): ans += (i - map.get(a[i], 0) - map.get(a[i] - 1, 0) - map.get(a[i] + 1, 0)) * a[i] map[a[i]] = map.get(a[i], 0) + 1 map = dict() for i in range(n - 1, -1, -1): ans -= ( n - i - 1 - map.get(a[i], 0) - map.get(a[i] - 1, 0) - map.get(a[i] + 1, 0) ) * a[i] map[a[i]] = map.get(a[i], 0) + 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) a = list(map(int, input().split())) cnt = dict() def get(x): global cnt if x in cnt: return cnt[x] return 0 def inc(x): global cnt if x in cnt: cnt[x] += 1 return cnt[x] = 1 s = 0 ans = 0 for i in range(n): ans += i * a[i] - s - get(a[i] - 1) + get(a[i] + 1) s += a[i] inc(a[i]) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN VAR VAR RETURN NUMBER FUNC_DEF IF VAR VAR VAR VAR NUMBER RETURN ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) x = n L = [int(i) for i in input().strip().split(" ")] d = {} for u in L: d[str(u)] = 0 d[str(u + 1)] = 0 d[str(u - 1)] = 0 CS = 0 Al = 0 i = 0 for j in L: d[str(j)] = d[str(j)] + 1 x, y = j - 1, j + 1 L = d[str(x)] A = d[str(y)] CS += j * i - Al - L + A Al += j i += 1 print(CS)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) a = list(map(int, input().split())) p = [0] * (n + 1) for i in range(n): p[i + 1] = p[i] + a[i] mp = dict() cnt = 0 for i in range(n): f1 = mp.setdefault(a[i]) f2 = mp.setdefault(a[i] - 1) f3 = mp.setdefault(a[i] + 1) if f1 == None: f1 = 0 if f2 == None: f2 = 0 if f3 == None: f3 = 0 mp[a[i]] = f1 + 1 cnt += i * a[i] - p[i] + f3 - f2 print(cnt)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF VAR NONE ASSIGN VAR NUMBER IF VAR NONE ASSIGN VAR NUMBER IF VAR NONE ASSIGN VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) nums = list(map(int, input().split())) pref = [0] * (n + 1) for i in range(n): pref[i + 1] = pref[i] + nums[i] counter = dict() answer = 0 counter[nums[0]] = 1 for i in range(1, n): answer += nums[i] * i - pref[i] answer -= counter.get(nums[i] - 1, 0) answer += counter.get(nums[i] + 1, 0) counter[nums[i]] = counter.get(nums[i], 0) + 1 print(answer)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) a = list(map(int, input().split(" "))) summ = 0 diff = 0 d = {} for i in range(0, n): diff += i * a[i] - summ summ += a[i] d[a[i]] = 0 d[a[i] - 1] = 0 d[a[i] + 1] = 0 for i in range(0, n): diff -= d[a[i] - 1] - d[a[i] + 1] d[a[i]] += 1 print(diff)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
cnt = {} n = int(input()) a = list(map(int, input().split())) sum = 0 ans = 0 for i in range(0, n): x = a[i] sum += x if not x in cnt: cnt[x] = 0 if not x + 1 in cnt: cnt[x + 1] = 0 if not x - 1 in cnt: cnt[x - 1] = 0 cnt[x] += 1 ans += (i + 1 - cnt[x] - cnt[x - 1] - cnt[x + 1]) * x - ( sum - cnt[x] * x - cnt[x + 1] * (x + 1) - cnt[x - 1] * (x - 1) ) print(ans)
ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) a = list(map(int, input().split())) M = dict() ans = 0 for i in range(n): M[a[i]] = M[a[i] - 1] = M[a[i] + 1] = 0 S = 0 for i in range(n - 1, -1, -1): y = a[i] c = n - i - 1 - M[y - 1] - M[y] - M[y + 1] ans += S - (y + 1) * M[y + 1] - (y - 1) * M[y - 1] - y * M[y] - c * y M[y] += 1 S += y print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
def main(): n = int(input()) ans = 0 s = 0 k = {} i = 0 for y in input().split(): x = int(y) ans += i * x - k.get(x - 1, 0) + k.get(x + 1, 0) - s k[x] = k.get(x, 0) + 1 s += x i += 1 print(ans) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) L = {} sum = 0 for i, v in enumerate(input().split(" ")): a = int(v) if a in L: L[a] += 1 else: L[a] = 1 sum += (2 * i - n + 1) * a if a - 1 in L: sum -= L[a - 1] if a + 1 in L: sum += L[a + 1] print(sum)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) v = [int(x) for x in input().split()] mp = {} s = sum(v) ans = 0 cnt = n for i in range(n): if v[i] in mp: mp[v[i]] += 1 else: mp[v[i]] = 1 for i in range(n): c = v[i] s -= c cnt -= 1 ans += s - cnt * c ans -= mp.get(c + 1, 0) ans += mp.get(c - 1, 0) mp[c] -= 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
import sys d = {} n = int(input()) a = [int(i) for i in input().split()] for i in a: if i in d: d[i] += 1 else: d[i] = 1 ss = sum(a) ans = 0 for i in range(n): ss -= a[i] s = ss d[a[i]] -= 1 k = n - i - 1 if a[i] - 1 in d: k -= d[a[i] - 1] s -= d[a[i] - 1] * (a[i] - 1) if a[i] + 1 in d: k -= d[a[i] + 1] s -= d[a[i] + 1] * (a[i] + 1) if a[i] in d: k -= d[a[i]] s -= d[a[i]] * a[i] ans += s - k * a[i] print(ans)
IMPORT ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) a = [int(x) for x in input().split()] ans = 0 summ = 0 mp = {} for i in range(0, n): l = 0 if a[i] - 1 in mp: l = mp[a[i] - 1] r = 0 if a[i] + 1 in mp: r = mp[a[i] + 1] ans = ans + i * a[i] - summ - (l - r) summ = summ + a[i] x = 0 if a[i] in mp: x = mp[a[i]] del mp[a[i]] mp[a[i]] = x + 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) a = list(map(int, input().strip().split())) mapp = {} for i in a: mapp[i] = 0 mapp[i + 1] = 0 mapp[i - 1] = 0 s = sum(a) ans = 0 for i in range(n): mapp[a[i]] += 1 ans += s - a[i] * (n - i) s -= a[i] ans += mapp[a[i] + 1] ans -= mapp[a[i] - 1] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) sum = 0 res = 0 v = [int(x) for x in input().split()] m = {} for i in range(n - 1, -1, -1): sum += v[i] cur = n - i if m.get(v[i]): m[v[i]] += 1 else: m[v[i]] = 1 cursum = sum - m[v[i]] * v[i] cur -= m[v[i]] if m.get(v[i] - 1): cursum -= m[v[i] - 1] * (v[i] - 1) cur -= m[v[i] - 1] if m.get(v[i] + 1): cursum -= m[v[i] + 1] * (v[i] + 1) cur -= m[v[i] + 1] res += cursum - v[i] * cur print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) d = {} ans = 0 a = list(map(int, input().split())) sumsofar = a[0] for i in range(1, n): ans += i * a[i] - sumsofar sumsofar += a[i] d[a[0]] = 1 for i in range(1, n): try: ans -= d[a[i] - 1] except: pass try: ans += d[a[i] + 1] except: pass try: d[a[i]] += 1 except: d[a[i]] = 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) t = input().split() x = {int(i): (0) for i in t} result = 0 for i in range(1, n + 1): a = int(t[i - 1]) x[a] += 1 result += a * (2 * i - n - 1) if a + 1 in x: result += x[a + 1] if a - 1 in x: result -= x[a - 1] print(result)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) line = list(map(int, input().split())) m = dict() ans = 0 for i in range(n): if line[i] not in m: m[line[i]] = 1 else: m[line[i]] += 1 var = m[line[i]] if line[i] - 1 in m: var += m[line[i] - 1] if line[i] + 1 in m: var += m[line[i] + 1] ans += (i + 1 - var) * line[i] x = dict() for j in range(n): i = n - j - 1 if line[i] not in x: x[line[i]] = 1 else: x[line[i]] += 1 var = x[line[i]] if line[i] - 1 in x: var += x[line[i] - 1] if line[i] + 1 in x: var += x[line[i] + 1] ans -= (j + 1 - var) * line[i] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) a = [int(i) for i in input().split()] finish = {} for i in range(n): finish[a[i]] = 0 result = 0 for i in range(n - 1, -1, -1): result += (i * 2 + 1 - n) * a[i] if a[i] - 1 in finish.keys(): result += finish[a[i] - 1] if a[i] + 1 in finish.keys(): result -= finish[a[i] + 1] finish[a[i]] += 1 print(result)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR VAR IF BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) arr = list(map(int, input().split())) freq = dict() res = 0 for i in range(n - 1, -1, -1): freq[arr[i]] = freq.get(arr[i], 0) + 1 tp = -1 * (n - i - 1) * arr[i] tp += i * arr[i] tp -= freq.get(arr[i] + 1, 0) tp += freq.get(arr[i] - 1, 0) res += tp print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) keys = list(map(int, input().split())) dictionary = {} ans = 0 sum = 0 for i in range(0, n): key = keys[i] a = dictionary.get(key - 1) b = dictionary.get(key) c = dictionary.get(key + 1) if a == None: a = 0 if b == None: b = 0 if c == None: c = 0 ans += key * (i - a - b - c) - (sum - a * (key - 1) - b * key - c * (key + 1)) sum += key if dictionary.get(key) == None: dictionary[key] = 1 else: dictionary[key] += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NONE ASSIGN VAR NUMBER IF VAR NONE ASSIGN VAR NUMBER IF VAR NONE ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR NONE ASSIGN VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
def f(a): if a not in ma: ma[a] = 0 mb[a] = 0 n = int(input()) x = list(map(int, input().split())) ma, mb = dict(), dict() ans = 0 for i in range(n): ans += x[i] * i ans -= x[i] * (n - 1 - i) f(x[i]) f(x[i] - 1) ma[x[i]] += 1 for i in range(n): ans += ma[x[i] - 1] - mb[x[i] - 1] ans -= mb[x[i] - 1] mb[x[i]] += 1 print(ans)
FUNC_DEF IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
def update(fenwick, idx, val, n): while idx < n: fenwick[idx] += val idx += idx & -idx def get_sum(fenwick, idx): sum = 0 while idx > 0: sum += fenwick[idx] idx -= idx & -idx return sum def count_smaller_elements(arr): temp = arr.copy() temp.sort() curr_size = 1 Hashed_Value = {} for item in temp: if item not in Hashed_Value: Hashed_Value[item] = curr_size curr_size += 1 fenwick = [0] * curr_size for i in range(n): temp[i] = Hashed_Value[arr[i]] temp = temp[::-1] count_smaller = [0] * n for i in range(n): count_smaller[i] = get_sum(fenwick, temp[i] - 1) update(fenwick, temp[i], 1, curr_size) return count_smaller[::-1] n = int(input()) l1 = list(map(int, input().split())) d1 = {} ans = 0 for i in range(-1, -n - 1, -1): if l1[i] not in d1: ans -= l1[i] * (abs(i) - 1) else: ans -= l1[i] * (abs(i) - 1 - d1[l1[i]]) if l1[i] not in d1: d1[l1[i]] = 1 else: d1[l1[i]] += 1 d1 = {} for i in range(n): if l1[i] not in d1: ans += l1[i] * i else: ans += l1[i] * (i - d1[l1[i]]) if l1[i] not in d1: d1[l1[i]] = 1 else: d1[l1[i]] += 1 for item in l1: if item - 1 in d1: ans -= d1[item - 1] if item + 1 in d1: ans += d1[item + 1] if item not in d1: d1[item] = 1 else: d1[item] += 1 print(ans)
FUNC_DEF WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) m = {} almost_sum = 0 s = list(map(int, input().split())) sum_all = 0 for i in range(n): a = s[i] if a not in m: m[a] = 1 else: m[a] += 1 u = 0 if a + 1 in m: u = m[a + 1] l = 0 if a - 1 in m: l = m[a - 1] almost_sum += a * i - sum_all - l + u sum_all += a print(almost_sum)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) a = list(map(int, input().split())) sum = ans = 0 d = {} for i in range(n)[::-1]: sum += a[i] if a[i] in d.keys(): d[a[i]] += 1 else: d[a[i]] = 1 for j in range(a[i] - 1, a[i] + 2): if j in d.keys(): ans += (a[i] - j) * d[j] ans -= a[i] * (n - i) ans += sum print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
N = int(input()) A = [int(s) for s in input().split()] D = {} ans = 0 sm = 0 for i in range(N): a = A[i] x = i y = sm for b in (a - 1, a, a + 1): if b in D: x -= D[b] y -= b * D[b] ans += a * x ans -= y if a in D: D[a] += 1 else: D[a] = 1 sm += a print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) a = [0] + input().split() s_i = 0 global mp mp = {} for i in range(1, n + 1): a[i] = int(a[i]) if a[i] not in mp: mp[a[i]] = 1 else: mp[a[i]] += 1 s_i += a[i] ans = 0 for i in range(n, 0, -1): s_in = ( mp.get(a[i] - 1, 0) * (a[i] - 1) + mp.get(a[i], 0) * a[i] + mp.get(a[i] + 1, 0) * (a[i] + 1) ) cnt_in = mp.get(a[i] - 1, 0) + mp.get(a[i], 0) + mp.get(a[i] + 1, 0) ans += a[i] * (i - cnt_in) - (s_i - s_in) mp[a[i]] -= 1 s_i -= a[i] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
a = int(input()) arr = list(map(int, input().split())) d = dict() summ = [0] brr = arr nd = dict() mimpp = dict() mimpn = dict() for i in arr: summ.append(i + summ[len(summ) - 1]) if i in d: d[i] = d[i] + 1 else: d[i] = 1 for i in range(0, len(brr)): if brr[i] in nd: nd[brr[i]] = nd[brr[i]] + 1 else: nd[brr[i]] = 1 mimpn[i] = 0 mimpp[i] = 0 if brr[i] - 1 in d: mimpn[i] = mimpn[i] + d[brr[i] - 1] if brr[i] + 1 in d: mimpp[i] = mimpp[i] + d[brr[i] + 1] if brr[i] - 1 in nd: mimpn[i] = mimpn[i] - nd[brr[i] - 1] if brr[i] + 1 in nd: mimpp[i] = mimpp[i] - nd[brr[i] + 1] ans = 0 ind = 0 su = sum(arr) for i in range(0, len(arr)): ans = ans + su - summ[ind] - (a - ind) * arr[i] ans = ans + mimpn[i] ans = ans - mimpp[i] ind = ind + 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) a = [int(x) for x in input().split()] m1 = {(-1): -1} m2 = {(-1): -1} ans = 0 for i in range(n): x, y, z = 0, 0, 0 if a[i] in m1: x = m1[a[i]] if a[i] - 1 in m1: y = m1[a[i] - 1] if a[i] + 1 in m1: z = m1[a[i] + 1] ans = ans + a[i] * (i - x - y - z) m1[a[i]] = x + 1 for i in range(n): x, y, z = 0, 0, 0 if a[n - 1 - i] in m2: x = m2[a[n - 1 - i]] if a[n - 1 - i] - 1 in m2: y = m2[a[n - 1 - i] - 1] if a[n - 1 - i] + 1 in m2: z = m2[a[n - 1 - i] + 1] ans = ans - a[n - 1 - i] * (i - x - y - z) m2[a[n - 1 - i]] = x + 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
dic = {} n = int(input()) sum1 = 0 a = list(map(int, input().split())) for i in range(n - 1, -1, -1): try: dic[a[i]] += 1 except: dic[a[i]] = 1 sum1 += a[i] * (-n + 1 + 2 * i) try: sum1 -= dic[a[i] + 1] except: pass try: sum1 += dic[a[i] - 1] except: pass print(sum1)
ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) a = list(map(int, input().split())) ans = 0 memo = dict((a[i], 0) for i in range(n)) for i in range(n): ans += a[i] * (2 * i - n + 1) memo[a[i]] += 1 ans -= memo.get(a[i] - 1, 0) ans += memo.get(a[i] + 1, 0) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) a = [int(x) for x in input().split(" ")] def init_map(): mp = {} for x in a: mp[x] = mp[x + 1] = mp[x - 1] = 0 return mp mp = init_map() result = 0 for m, v in enumerate(a): result += v * (m - mp[v] - mp[v - 1] - mp[v + 1]) mp[v] += 1 mp = init_map() for m, v in enumerate(a[::-1]): result -= v * (m - mp[v] - mp[v - 1] - mp[v + 1]) mp[v] += 1 print(result)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) l = list(map(int, input().split())) f = dict() s, val, ans = 0, 0, 0 for i in l: ans += i * (val - f.get(i - 1, 0) - f.get(i + 1, 0) - f.get(i, 0)) - ( s - f.get(i - 1, 0) * (i - 1) - f.get(i + 1, 0) * (i + 1) - f.get(i, 0) * i ) s += i val += 1 f[i] = f.get(i, 0) + 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) m = {} ans = 0 a = input().split() for i in range(n): a[i] = int(a[i]) m[a[i]], m[a[i] - 1], m[a[i] + 1] = 0, 0, 0 ans += a[i] * (2 * i - n + 1) for i in range(n): ans += m[a[i] + 1] - m[a[i] - 1] m[a[i]] += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER NUMBER VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) ls = [int(i) for i in input().split()] fullsum = sum(ls[i] * (-n + 1 + 2 * i) for i in range(n)) numdict = dict() for num in ls: if num - 1 in numdict: fullsum -= numdict[num - 1] if num + 1 in numdict: fullsum += numdict[num + 1] if num in numdict: numdict[num] += 1 else: numdict[num] = 1 print(fullsum)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
line = input() n = int(line) line = input().rstrip().split(" ") a = [] for i in range(0, n): a.append(int(line[i])) mpa = {} ans = 0 sum_a = 0 for i in range(0, n): ans = ans + (a[i] * i - sum_a + (mpa.get(a[i] + 1, 0) - mpa.get(a[i] - 1, 0))) sum_a = sum_a + a[i] mpa[a[i]] = mpa.get(a[i], 0) + 1 print(str(ans))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) d = {} a = list(map(int, input().split())) tot = ans = 0 for i in range(n): ans += i * a[i] - tot tot += a[i] ans += d.get(a[i] + 1, 0) - d.get(a[i] - 1, 0) d.setdefault(a[i], 0) d[a[i]] += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
N = 2 * 10**5 + 3 n = int(input()) A, cnt = list(map(int, input().split(" "))), {} s, a = 0, 0 for i in range(n - 1, -1, -1): a += s if A[i] - 1 in cnt: a += cnt[A[i] - 1] if A[i] + 1 in cnt: a -= cnt[A[i] + 1] a -= (n - (i + 1)) * A[i] s += A[i] if A[i] not in cnt: cnt[A[i]] = 0 cnt[A[i]] += 1 print(a)
ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING DICT ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) arr = [int(lol) for lol in input().split()] count = {} ans = 0 s = 0 i = 0 for lol in arr: if lol not in count.keys(): count[lol] = 1 else: count[lol] += 1 ans += i * lol ans -= s if lol - 1 in count.keys(): ans -= count[lol - 1] if lol + 1 in count.keys(): ans += count[lol + 1] s += lol i += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
pre = 0 ans = 0 n = int(input()) ain = input() arr = ain.split(" ") m = {} for i in range(n): a = int(arr[i]) ans += i * a - pre if a - 1 in m and m[a - 1] > 0: ans -= m[a - 1] if a + 1 in m and m[a + 1] > 0: ans -= m[a + 1] * -1 pre += a if a in m: m[a] += 1 else: m[a] = 1 print(ans)
ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) s = 0 ans = 0 l = input().split(" ") mp = {} for i in range(0, n): l[i] = int(l[i]) f = i * l[i] f += mp.get(l[i] + 1, 0) f -= mp.get(l[i] - 1, 0) ans += f - s s += l[i] temp = mp.get(l[i], 0) mp[l[i]] = temp + 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) l = list(map(int, input().split())) total = 0 for i in range(n - 1, -1, -1): total = total + i * l[i] for i in range(0, n): total = total - (n - i - 1) * l[i] d = {} for i in range(len(l)): d[l[i]] = 0 d[l[i] + 1] = 0 d[l[i] - 1] = 0 for i in range(0, n): if i == 0: d[l[i]] = d[l[i]] + 1 else: temp1 = l[i] - 1 temp2 = l[i] + 1 total = total - d[temp1] * l[i] + d[temp1] * temp1 total = total - d[temp2] * l[i] + d[temp2] * temp2 d[l[i]] = d[l[i]] + 1 print(total)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = input() mp = list(map(int, input().split())) sum = 0 ans = 0 cnt = 0 tt = dict() for i in mp: sum += i cnt = cnt + 1 ans += cnt * i - sum if i - 1 in tt: ans -= tt[i - 1] if i + 1 in tt: ans += tt[i + 1] if i not in tt: tt[i] = 0 tt[i] = tt[i] + 1 print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) a = {} ans = 0 sum = 0 i = 0 for t in map(int, input().split()): sum += t a[t] = a.get(t, 0) + 1 ans += (i - a.get(t, 0) - a.get(t - 1, 0) - a.get(t + 1, 0) + 1) * t - ( sum - a.get(t, 0) * t - a.get(t - 1, 0) * (t - 1) - a.get(t + 1, 0) * (t + 1) ) i += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) ans = 0 sum = 0 mp = {} c = [] c = list(map(int, input().split())) for i in range(0, n): tmp = c[i] z1 = 0 z2 = 0 if tmp - 1 in mp: z1 = mp[tmp - 1] if tmp + 1 in mp: z2 = mp[tmp + 1] ans += tmp * i - sum - z1 + z2 if tmp in mp: mp[tmp] += 1 else: mp[tmp] = 1 sum += tmp print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
cnt = {} ans = 0 n = int(input()) inp = [int(i) for i in input().split()] sum = 0 for i in range(n): x = inp[i] if x not in cnt.keys(): cnt[x] = 0 if x - 1 not in cnt.keys(): cnt[x - 1] = 0 if x + 1 not in cnt.keys(): cnt[x + 1] = 0 minus = cnt[x - 1] * (x - 1) + cnt[x + 1] * (x + 1) + cnt[x] * x cnt_x = i - cnt[x - 1] - cnt[x + 1] - cnt[x] ans += cnt_x * x - (sum - minus) cnt[x] += 1 sum += x print(ans)
ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
N = int(input()) s = list(map(int, input().split())) M = {} ans = 0 j = 1 - N for i in s: M[i] = M.get(i, 0) + 1 ans += j * i j += 2 for i in s: ans += M.get(i - 1, 0) ans -= M.get(i + 1, 0) M[i] = M.get(i, 0) - 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) soujanya = {} sum = 0 k = input().split() a = {} for j in range(n): i = j + 1 a[i] = int(k[j]) sum += a[i] if a[i] in soujanya.keys(): soujanya[a[i]] += 1 else: soujanya[a[i]] = 1 counter = n ans = 0 for j in range(n): i = j + 1 temp = counter if a[i] - 1 in soujanya.keys(): k1 = soujanya[a[i] - 1] else: k1 = 0 if a[i] in soujanya.keys(): k2 = soujanya[a[i]] else: k2 = 0 if a[i] + 1 in soujanya.keys(): k3 = soujanya[a[i] + 1] else: k3 = 0 temp -= k1 + k2 + k3 k = sum - (k1 * (a[i] - 1) + k2 * a[i] + k3 * (a[i] + 1)) ans += k - temp * a[i] counter = counter - 1 sum -= a[i] soujanya[a[i]] -= 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR VAR NUMBER FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) arr = list(map(int, input().split())) n -= 1 ans = 0 for num in arr: ans += n * num n -= 2 ans = -ans dic = {num: (0) for num in arr} for i in range(len(arr)): dic[arr[i]] += 1 if arr[i] - 1 in dic: ans -= dic[arr[i] - 1] if arr[i] + 1 in dic: ans += dic[arr[i] + 1] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) d = dict() a = list(map(int, input().split())) s = 0 ans = 0 for i in range(n): ans = ans + i * a[i] - s if d.get(a[i] - 1): ans -= d[a[i] - 1] if d.get(a[i] + 1): ans += d[a[i] + 1] if d.get(a[i]): d[a[i]] += 1 else: d[a[i]] = 1 s += a[i] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
def query(l, r): return s[r] - s[l - 1] n = int(input()) a = [0] + [int(i) for i in input().split()] s = [(0) for i in range(n + 1)] for i in range(1, n + 1): s[i] = s[i - 1] + a[i] ans = 0 m = {} for i in range(n, 0, -1): ans += query(i + 1, n) - (n - i) * a[i] if a[i] + 1 in m: ans -= m[a[i] + 1] if a[i] - 1 in m: ans += m[a[i] - 1] if a[i] in m: m[a[i]] += 1 else: m[a[i]] = 1 print(ans)
FUNC_DEF RETURN BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) a = input().split(" ") ans = 0 kk = 0 kol = dict() for i in range(n - 1, -1, -1): a[i] = int(a[i]) kk += a[i] kol[a[i]] = 1 + kol.get(a[i], 0) plus = kk - (n - i) * a[i] plus += kol.get(a[i] - 1, 0) plus -= kol.get(a[i] + 1, 0) ans += plus print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) a = [int(x) for x in input().split()] ans = 0 d = {} for i in range(n): ans += a[i] * i ans -= a[i] * (n - i - 1) if a[i] not in d: d[a[i]] = 1 else: d[a[i]] += 1 if a[i] - 1 in d: ans -= d[a[i] - 1] if a[i] + 1 in d: ans += d[a[i] + 1] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
input() a = 0 b = 0 c = 0 d = dict() e = dict() for i in list(map(int, input().split())): x, y = 0, 0 for j in range(i - 1, i + 2): if j in d: x += d[j] y += e[j] a += (c - y) * i - b + x b += i c += 1 d[i] = i if i not in d else d[i] + i e[i] = 1 if i not in e else e[i] + 1 print(a)
EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) a = map(int, input().split()) mp = {} s = 0 ans = 0 i = 0 for x in a: i += 1 s += x if x not in mp: mp[x] = 0 if x + 1 not in mp: mp[x + 1] = 0 if x - 1 not in mp: mp[x - 1] = 0 mp[x] += 1 adj = mp[x] + mp[x + 1] + mp[x - 1] c = s c -= mp[x] * x c -= mp[x + 1] * (x + 1) c -= mp[x - 1] * (x - 1) valid = i - adj ans += valid * x - c print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
input() ans = 0 sum = 0 i = 0 d = dict() arr = list(map(int, input().split())) for cur in arr: d[cur] = 0 d[cur - 1] = 0 d[cur + 1] = 0 for cur in arr: ans += i * cur - sum + d[cur + 1] - d[cur - 1] d[cur] += 1 i += 1 sum += cur print(ans)
EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) a = [int(x) for x in input().split()] l = {} s = 0 for i in range(n): x = a[i] if l.get(x, "none") == "none": l[x] = 1 else: l[x] = l[x] + 1 s += i * x - (n - 1 - i) * x if l.get(x + 1, "none") != "none": s += l[x + 1] if l.get(x - 1, "none") != "none": s -= l[x - 1] print(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR STRING STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER STRING STRING VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER STRING STRING VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) arr = list(map(int, input().split())) arr_sum = sum(arr) cnt = {} for val in arr: try: cnt[val] += 1 except: cnt[val] = 1 res = 0 for i in range(n - 1, -1, -1): arr_sum -= arr[i] cnt[arr[i]] -= 1 res += i * arr[i] - arr_sum if arr[i] + 1 in cnt.keys(): res += cnt[arr[i] + 1] if arr[i] - 1 in cnt.keys(): res -= cnt[arr[i] - 1] print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n, a, ft = 0, [(0, 0)], [0] def query(k): res = 0 while k > 0: res += ft[k] k -= k & -k return res def adjust(k, v): while k <= n: ft[k] += v k += k & -k def rquery(l, r): if l > r: return 0 return query(r) - (0 if l == 1 else query(l - 1)) n, ans = int(input()), 0 tmp = [int(x) for x in input().split()] for i in range(1, n + 1): a.append((tmp[i - 1], i)) ft = [(0) for i in range(n + 1)] ans = sum([(x * (2 * i - n - 1)) for x, i in a]) a.sort(key=lambda x: x[0]) L, R, cur, i = [], [], 0, 1 while i <= n: v, k = a[i] if v == cur: ans -= rquery(1, k - 1) - rquery(k + 1, n) R.append(k) i += 1 else: for j in L: adjust(j, -1) L = [] if v - cur == 1: for j in R: L.append(j) adjust(j, 1) R, cur = [], v print(ans)
ASSIGN VAR VAR VAR NUMBER LIST NUMBER NUMBER LIST NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR LIST LIST NUMBER NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST IF BIN_OP VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) xd = input().split() a = [0] for s in xd: a.append(int(s)) b = [0] * (n + 1) for i in range(1, n + 1): b[i] = b[i - 1] + a[i] sol = 0 mp = {} def mpg(x): if x in mp: return mp[x] else: return 0 for i in range(1, n + 1): x = a[i] mp[x] = mpg(x) + 1 sol += i * x - b[i] sol -= mpg(x - 1) sol += mpg(x + 1) print(sol)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) a = input().split() an = 0 sum = int(a[n - 1]) cnt = {} for x in range(n): cnt[int(a[x])] = 0 cnt[int(a[x]) + 1] = 0 cnt[int(a[x]) - 1] = 0 cnt[int(a[n - 1])] = 1 x = n - 2 while x >= 0: rsum = sum - ( cnt[int(a[x])] * int(a[x]) + cnt[int(a[x]) + 1] * (int(a[x]) + 1) + cnt[int(a[x]) - 1] * (int(a[x]) - 1) ) an += rsum - int(a[x]) * ( n - (x + 1) - cnt[int(a[x])] - cnt[int(a[x]) + 1] - cnt[int(a[x]) - 1] ) cnt[int(a[x])] += 1 sum += int(a[x]) x = x - 1 print(an)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) a = list(map(int, input().split(" "))) x = 0 for i in range(int(n / 2)): j = n - 1 - i x += (a[j] - a[i]) * (j - i) m = {} for i in range(n): if a[i] in m: m[a[i]] += 1 else: m[a[i]] = 1 if a[i] + 1 in m: x += m[a[i] + 1] if a[i] - 1 in m: x -= m[a[i] - 1] print(x)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) a = list(map(int, input().split())) cnt = 0 m = dict() s = 0 ans = 0 for i in a: m[i] = 0 m[i - 1] = 0 m[i + 1] = 0 for i in range(n - 1, -1, -1): ans += s - cnt * a[i] for j in range(-1, 2): ans -= m[a[i] + j] * (a[i] + j - a[i]) s += a[i] m[a[i]] += 1 cnt += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input().strip()) arr = [int(i) for i in input().strip().split(" ")] dic = {arr[0]: 1} ic = [(0) for i in range(n)] isu = [(0) for i in range(n)] pre = [arr[0]] for i in range(1, n): pre.append(pre[i - 1] + arr[i]) for i in range(1, n): ici, isi = 0, 0 if arr[i] in dic: ici += dic[arr[i]] isi += arr[i] * dic[arr[i]] if arr[i] - 1 in dic: ici += dic[arr[i] - 1] isi += (arr[i] - 1) * dic[arr[i] - 1] if arr[i] + 1 in dic: ici += dic[arr[i] + 1] isi += (arr[i] + 1) * dic[arr[i] + 1] ic[i], isu[i] = ici, isi if arr[i] in dic: dic[arr[i]] += 1 else: dic[arr[i]] = 1 ans = 0 for i in range(1, n): ans += arr[i] * i - pre[i - 1] - ic[i] * arr[i] + isu[i] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR DICT VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) a = [int(x) for x in input().split()] d = 0 for i in range(n): d += -1 * a[i] * (n - i - 1) d += a[i] * i di = {} for i in range(n): if a[i] not in di: di[a[i]] = 0 if a[i] - 1 in di: d -= di[a[i] - 1] if a[i] + 1 in di: d += di[a[i] + 1] di[a[i]] += 1 print(d)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
map = dict() sum = 0 n = int(input()) a = [int(x) for x in input().split()] ans = 0 for i, a in enumerate(a): ans += i * a ans -= sum if a - 1 in map: ans -= map[a - 1] if a + 1 in map: ans += map[a + 1] sum += a if a in map: map[a] += 1 else: map[a] = 1 print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) a = [int(x) for x in input().split()] s = sum(a) t = sum((i + 1) * ai for i, ai in enumerate(a)) p = 2 * t - (n + 1) * s q = 0 r = 0 freqs = {} for y in a: q += freqs.get(y - 1, 0) r += freqs.get(y + 1, 0) freqs[y] = 1 + freqs.get(y, 0) print(p - q + r)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
sequence_dict = {} sum = 0 number_of_elements = int(input()) sequence = list(map(int, input().split())) for index, element in enumerate(sequence): sum += ( index * element - (number_of_elements - 1 - index) * element - sequence_dict.get(element - 1, 0) + sequence_dict.get(element + 1, 0) ) sequence_dict[element] = sequence_dict.get(element, 0) + 1 print(sum)
ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
m = {} A = [0] * 200001 B = [] sum1 = 0 ans = 0 n = int(input()) B = input().split(" ") for i in range(n): A[i] = int(B[i]) sum1 += A[i] if A[i] in m: m[A[i]] += 1 else: m[A[i]] = 1 np = n for i in range(n): if A[i] in m: a1 = m[A[i]] else: a1 = 0 if A[i] + 1 in m: a2 = m[A[i] + 1] else: a2 = 0 if A[i] - 1 in m: a3 = m[A[i] - 1] else: a3 = 0 val1 = sum1 - A[i] * a1 - (A[i] - 1) * a3 - (A[i] + 1) * a2 val2 = (np - a1 - a3 - a2) * A[i] ans += val1 - val2 m[A[i]] -= 1 sum1 -= A[i] np -= 1 print(ans)
ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
def bnyk(x): if x in freq.keys(): return freq[x] else: return 0 freq = {} n = int(input()) list = input().split() ans = 0 ttl = 0 for i in range(n): x = int(list[i]) a = bnyk(x - 1) b = bnyk(x) c = bnyk(x + 1) ans += x * (i - a - b - c) - (ttl - (x - 1) * a - x * b - (x + 1) * c) ttl += x if x in freq.keys(): freq[x] += 1 else: freq[x] = 1 print("%d\n" % ans)
FUNC_DEF IF VAR FUNC_CALL VAR RETURN VAR VAR RETURN NUMBER ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) tot = 0 ans = 0 cnt = {} a = list(map(int, input().split())) for i in range(1, n + 1): x = a[i - 1] ans += (i - 1) * x - tot if x - 1 in cnt: ans -= cnt[x - 1] if x + 1 in cnt: ans += cnt[x + 1] if x in cnt: cnt[x] += 1 else: cnt[x] = 1 tot += x print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) a = list(map(int, input().split())) d = dict() s = 0 ans = 0 for elem in a: if d.get(elem) == None: d[elem] = 0 d[elem] += 1 s += elem for i in range(n): cur = a[i] d[cur] -= 1 s -= cur cnt = n - i - 1 if d.get(cur - 1) == None: cnt1 = 0 else: cnt1 = d[cur - 1] if d.get(cur) == None: cnt2 = 0 else: cnt2 = d[cur] if d.get(cur + 1) == None: cnt3 = 0 else: cnt3 = d[cur + 1] ans += ( s - cnt1 * (cur - 1) - cnt2 * cur - cnt3 * (cur + 1) - (cnt - cnt1 - cnt2 - cnt3) * cur ) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR NONE ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER NONE ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NONE ASSIGN VAR NUMBER ASSIGN VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER NONE ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
def get_s_in(i): global mp res = 0 if a[i] - 1 in mp: res += mp[a[i] - 1] * (a[i] - 1) if a[i] in mp: res += mp[a[i]] * a[i] if a[i] + 1 in mp: res += mp[a[i] + 1] * (a[i] + 1) return res def get_cnt_in(i): global mp res = 0 if a[i] - 1 in mp: res += mp[a[i] - 1] if a[i] in mp: res += mp[a[i]] if a[i] + 1 in mp: res += mp[a[i] + 1] return res n = int(input()) a = [0] + input().split() s = [0] * (n + 1) global mp mp = {} for i in range(1, n + 1): a[i] = int(a[i]) if a[i] not in mp: mp[a[i]] = 1 else: mp[a[i]] += 1 s[i] = s[i - 1] + a[i] ans = 0 for i in range(n, 0, -1): s_in = get_s_in(i) cnt_in = get_cnt_in(i) ans += a[i] * (i - cnt_in) - (s[i] - s_in) mp[a[i]] -= 1 print(ans)
FUNC_DEF ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
tmp = input() ans = 0 arr = [int(x) for x in input().split(" ")] n = len(arr) m = {} sum = 0 for i in range(0, n): m[arr[i]] = m.get(arr[i], 0) + 1 sum += arr[i] for i in range(0, n): m[arr[i]] = m.get(arr[i], 0) - 1 sum -= arr[i] c1 = m.get(arr[i] - 1, 0) c2 = m.get(arr[i], 0) c3 = m.get(arr[i] + 1, 0) cur_sum = sum - (c1 * (arr[i] - 1) + c2 * arr[i] + c3 * (arr[i] + 1)) cur_cnt = n - i - 1 - c1 - c2 - c3 ans += cur_sum - cur_cnt * arr[i] print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) a = list(map(int, input().split())) mm = dict() S = [0] * 200001 for i in range(n): if i > 0: S[i] = S[i - 1] + a[i] if a[i] not in mm: mm[a[i]] = 1 else: mm[a[i]] += 1 ans = 0 for i in range(n): if i > 0: ans += -a[i] * (n - 1 - i) + S[n - 1] - S[i] else: ans += -a[i] * (n - 1 - i) + S[n - 1] for i in range(n): if a[i] + 1 in mm: ans -= mm[a[i] + 1] if a[i] - 1 in mm: ans += mm[a[i] - 1] mm[a[i]] -= 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
trash = int(input()) numbers = input().split(" ", trash) for i in range(trash): numbers[i] = int(numbers[i]) dict = {} for i in range(trash): dict[numbers[i]] = 0 dict[numbers[i] - 1] = 0 dict[numbers[i] + 1] = 0 res = 0 fncount = numbers[0] dict[numbers[0]] = 1 for i in range(1, trash): dict[numbers[i]] = dict[numbers[i]] + 1 res = res + numbers[i] * i - fncount - dict[numbers[i] - 1] + dict[numbers[i] + 1] fncount = fncount + numbers[i] print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) A = [int(i) for i in input().split()] acc = 0 res = 0 for i in range(len(A)): acc -= A[i] res += acc + A[i] * (i + 1) mp = dict() for i in range(len(A)): mp[A[i]] = mp.get(A[i], 0) + 1 res -= mp.get(A[i] - 1, 0) - mp.get(A[i] + 1, 0) print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) s = input().split() a = [int(x) for x in s] p = [(0) for i in range(len(a) + 1)] m = {} def get(ai): if ai in m: return m[ai] return 0 i = 0 for ai in a: p[i + 1] = p[i] + ai if ai in m: m[ai] += 1 else: m[ai] = 1 i += 1 i = n - 1 ans = 0 for ai in reversed(a): ans += i * ai - p[i] - get(ai - 1) + get(ai + 1) m[ai] -= 1 i -= 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) list1 = list(map(int, input().split())) x = 0 ans = 0 dic = {} list2 = [] for i in range(n): list2.append(x) x += list1[n - 1 - i] dic[list1[i]] = dic.get(list1[i], 0) + 1 list2.reverse() for i in range(n): ans += list2[i] - (n - 1 - i) * list1[i] ans -= dic.get(list1[i] + 1, 0) ans += dic.get(list1[i] - 1, 0) dic[list1[i]] -= 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) a = list(map(int, input().split())) m = dict() s = 0 ans = 0 cnt = 0 for i in range(n): mp = m.get(a[i] + 1, 0) mm = m.get(a[i] - 1, 0) ans += cnt * a[i] - s + mp - mm m.update({a[i]: m.get(a[i], 0) + 1}) s += a[i] cnt += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR DICT VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) a = list(map(int, input().split())) s = 0 ans = 0 for i in range(0, n): ans += a[i] * i - s s += a[i] d = {} for i in range(0, n): if a[i] + 1 in d: ans += d[a[i] + 1] if a[i] - 1 in d: ans -= d[a[i] - 1] if not a[i] in d: d[a[i]] = 0 d[a[i]] += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) t = [int(x) for x in input().split()] ans = 0 d = {} for i in range(n): ans += (-(n - 1) + 2 * i) * t[i] for i in range(n): if t[i] - 1 in d: ans -= d[t[i] - 1] if t[i] + 1 in d: ans += d[t[i] + 1] if t[i] in d: d[t[i]] += 1 else: d[t[i]] = 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) cnt = {} res = 0 s = 0 a = list(map(int, input().split())) for i in range(n): x = a[i] s += x cnt[x] = cnt.get(x, 0) + 1 res += x * (i + 1 - (cnt.get(x, 0) + cnt.get(x - 1, 0) + cnt.get(x + 1, 0))) - ( s - ( x * cnt.get(x, 0) + (x - 1) * cnt.get(x - 1, 0) + (x + 1) * cnt.get(x + 1, 0) ) ) print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) a = list(map(int, input().split())) ps = [a[0]] * n for i in range(1, n): ps[i] = a[i] + ps[i - 1] cnt = dict() ans = 0 cnt[a[0]] = 1 for i in range(1, n): ans += a[i] * i ans -= ps[i - 1] if a[i] - 1 not in cnt: cnt[a[i] - 1] = 0 ans -= cnt[a[i] - 1] if a[i] + 1 not in cnt: cnt[a[i] + 1] = 0 ans += cnt[a[i] + 1] if a[i] not in cnt: cnt[a[i]] = 0 cnt[a[i]] = cnt[a[i]] + 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) a = list(map(int, input().split())) dic = {} for i in range(0, n): dic[a[i]] = 0 dic[a[i] - 1] = 0 dic[a[i] + 1] = 0 ans = 0 summ = 0 for i in range(0, n): ans += i * a[i] - summ ans -= dic[a[i] - 1] ans += dic[a[i] + 1] summ += a[i] dic[a[i]] += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
def set(x): if x not in m: m[x] = 0 n = int(input()) a = list(map(int, input().split())) m = {} ans, total = 0, 0 for i in range(0, n): x = a[i] set(x), set(x - 1), set(x + 1) ans = ans + (i - m[x] - m[x - 1] - m[x + 1]) * x ans = ans - (total - m[x] * x - m[x - 1] * (x - 1) - m[x + 1] * (x + 1)) m[x], total = m[x] + 1, total + x print(ans)
FUNC_DEF IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) a = list(map(int, input().split())) cnt = {} ans = 0 sm = 0 i = n - 1 while i >= 0: if i != n - 1: ans = ans + sm - a[i] * (n - i - 1) if a[i] - 1 in cnt: ans = ans + cnt[a[i] - 1] if a[i] + 1 in cnt: ans = ans - cnt[a[i] + 1] sm = sm + a[i] if a[i] in cnt: cnt[a[i]] = cnt[a[i]] + 1 else: cnt[a[i]] = 1 i = i - 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
from itertools import accumulate def cald(left, right): i = 0 j = 0 ans = 0 l1 = len(left) l2 = len(right) while j < len(right): while i < l1 and left[i] < right[j]: i += 1 ans += l1 - 2 * i j += 1 return ans n = int(input()) a = list(map(int, input().split())) dict = {} for i in range(n): if a[i] not in dict: dict[a[i]] = [] dict[a[i]].append(i) ks = list(dict.keys()) ans = 0 for x in ks: if x - 1 in dict: ans += cald(dict[x - 1], dict[x]) pre = list(accumulate(a)) for i in range(1, n): ans += i * a[i] - pre[i - 1] print(ans)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
import sys n = int(input()) a = list(map(int, input().split())) cnt = dict() ans = 0 for i in range(n): ans += i * a[i] ans -= (n - 1 - i) * a[i] cnt[a[i]] = 0 for i in range(n): if a[i] - 1 in cnt: ans -= cnt[a[i] - 1] if a[i] + 1 in cnt: ans += cnt[a[i] + 1] cnt[a[i]] += 1 print(ans)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's denote a function $d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$ You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Input----- The first line contains one integer n (1 ≀ n ≀ 200000) β€” the number of elements in a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” elements of the array. -----Output----- Print one integer β€” the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≀ i ≀ j ≀ n. -----Examples----- Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 -----Note----- In the first example: d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2.
n = int(input()) sum = 0 res = 0 cnt = dict() a = list(map(int, input().split())) for i in range(n): sub = 0 num = 0 for j in range(-1, 2, 1): if a[i] + j in cnt: sub += (a[i] + j) * cnt[a[i] + j] num += cnt[a[i] + j] res += (i - num) * a[i] - (sum - sub) sum += a[i] cnt[a[i]] = cnt.get(a[i], 0) + 1 print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR