description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >.
The following definition of a regular bracket sequence is well-known, so you can be familiar with it.
Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS.
For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not.
Determine the least number of replaces to make the string s RBS.
-----Input-----
The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6.
-----Output-----
If it's impossible to get RBS from s print Impossible.
Otherwise print the least number of replaces needed to get RBS from s.
-----Examples-----
Input
[<}){}
Output
2
Input
{()}[]
Output
0
Input
]]
Output
Impossible | l = []
cnt = 0
for i in input():
if i in "[<{(":
l.append(i)
elif len(l) > 0:
if l.pop() != {"}": "{", ")": "(", "]": "[", ">": "<"}[i]:
cnt += 1
else:
print("Impossible")
break
else:
if len(l) > 0:
print("Impossible")
else:
print(cnt) | ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR |
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >.
The following definition of a regular bracket sequence is well-known, so you can be familiar with it.
Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS.
For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not.
Determine the least number of replaces to make the string s RBS.
-----Input-----
The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6.
-----Output-----
If it's impossible to get RBS from s print Impossible.
Otherwise print the least number of replaces needed to get RBS from s.
-----Examples-----
Input
[<}){}
Output
2
Input
{()}[]
Output
0
Input
]]
Output
Impossible | import sys
INF = 10**18 + 3
def main():
opening = "<{[("
closing = ">}])"
s = input()
stack = []
res = 0
for i, sym in enumerate(s):
if sym in opening:
kind = opening.find(sym)
stack.append(kind)
else:
kind = closing.find(sym)
if not stack:
res = -1
break
if stack[-1] != kind:
res += 1
del stack[-1]
print("Impossible" if res == -1 or stack else res)
def set_input(file):
global input
input = lambda: file.readline().rstrip()
def set_output(file):
global print
lprint = print
def print(*args, **kwargs):
kwargs["file"] = kwargs.get("file", file)
return lprint(*args, **kwargs)
is_mine = "MINE" in sys.argv
set_input(open("input.txt", "r") if is_mine else sys.stdin)
set_output(sys.stdout if is_mine else sys.stdout)
main() | IMPORT ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR STRING VAR FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_DEF ASSIGN VAR STRING FUNC_CALL VAR STRING VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR STRING VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR STRING STRING VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR |
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >.
The following definition of a regular bracket sequence is well-known, so you can be familiar with it.
Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS.
For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not.
Determine the least number of replaces to make the string s RBS.
-----Input-----
The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6.
-----Output-----
If it's impossible to get RBS from s print Impossible.
Otherwise print the least number of replaces needed to get RBS from s.
-----Examples-----
Input
[<}){}
Output
2
Input
{()}[]
Output
0
Input
]]
Output
Impossible | s = input()
stack = []
answer = 0
corrects = "[]", "{}", "<>", "()"
for i in s:
if i == "[" or i == "{" or i == "<" or i == "(":
stack.append(i)
elif len(stack) == 0:
print("Impossible")
exit()
else:
top = stack.pop()
if top + i not in corrects:
answer += 1
print(answer if len(stack) == 0 else "Impossible") | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR STRING STRING STRING STRING FOR VAR VAR IF VAR STRING VAR STRING VAR STRING VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR STRING |
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >.
The following definition of a regular bracket sequence is well-known, so you can be familiar with it.
Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS.
For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not.
Determine the least number of replaces to make the string s RBS.
-----Input-----
The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6.
-----Output-----
If it's impossible to get RBS from s print Impossible.
Otherwise print the least number of replaces needed to get RBS from s.
-----Examples-----
Input
[<}){}
Output
2
Input
{()}[]
Output
0
Input
]]
Output
Impossible | s = input()
cnt = 0
st = []
for elem in s:
if elem in "([{<":
st.append(elem)
else:
if len(st) == 0:
print("Impossible")
break
elem2 = st.pop()
if elem2 + elem not in "()[]{}<>":
cnt += 1
else:
if len(st) == 0:
print(cnt)
else:
print("Impossible") | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR IF BIN_OP VAR VAR STRING VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING |
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >.
The following definition of a regular bracket sequence is well-known, so you can be familiar with it.
Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS.
For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not.
Determine the least number of replaces to make the string s RBS.
-----Input-----
The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6.
-----Output-----
If it's impossible to get RBS from s print Impossible.
Otherwise print the least number of replaces needed to get RBS from s.
-----Examples-----
Input
[<}){}
Output
2
Input
{()}[]
Output
0
Input
]]
Output
Impossible | opens = ["(", "{", "<", "["]
closes = [")", "}", ">", "]"]
def solve(inp):
stack = []
res = 0
for i, x in enumerate(inp):
if x in opens:
stack.append((i, x))
if x in closes:
if not stack:
return "Impossible"
ii, xx = stack.pop()
if closes.index(x) != opens.index(xx):
res += 1
if stack:
return "Impossible"
return res
print(solve(input())) | ASSIGN VAR LIST STRING STRING STRING STRING ASSIGN VAR LIST STRING STRING STRING STRING FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR IF VAR RETURN STRING ASSIGN VAR VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR RETURN STRING RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR |
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >.
The following definition of a regular bracket sequence is well-known, so you can be familiar with it.
Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS.
For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not.
Determine the least number of replaces to make the string s RBS.
-----Input-----
The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6.
-----Output-----
If it's impossible to get RBS from s print Impossible.
Otherwise print the least number of replaces needed to get RBS from s.
-----Examples-----
Input
[<}){}
Output
2
Input
{()}[]
Output
0
Input
]]
Output
Impossible | def main(s: str):
if len(s) == 0:
return "Impossible"
leftchr = ["(", "{", "<", "["]
rightchr = [")", "}", ">", "]"]
stack = list()
opdict = {"(": ")", "{": "}", "<": ">", "[": "]"}
mix = 0
for oper in s:
if oper in leftchr:
stack.append(oper)
elif oper in rightchr:
if len(stack) == 0:
return "Impossible"
else:
top = stack.pop()
if opdict.get(top) != oper:
mix += 1
if len(stack) != 0:
return "Impossible"
return mix
s = str(input())
print(main(s)) | FUNC_DEF VAR IF FUNC_CALL VAR VAR NUMBER RETURN STRING ASSIGN VAR LIST STRING STRING STRING STRING ASSIGN VAR LIST STRING STRING STRING STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN STRING ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN STRING RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >.
The following definition of a regular bracket sequence is well-known, so you can be familiar with it.
Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS.
For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not.
Determine the least number of replaces to make the string s RBS.
-----Input-----
The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6.
-----Output-----
If it's impossible to get RBS from s print Impossible.
Otherwise print the least number of replaces needed to get RBS from s.
-----Examples-----
Input
[<}){}
Output
2
Input
{()}[]
Output
0
Input
]]
Output
Impossible | def main(s: str):
if len(s) == 0 or len(s) % 2 != 0:
return "Impossible"
leftchr = ["(", "{", "<", "["]
stack = list()
opdict = {"(": ")", "{": "}", "<": ">", "[": "]"}
mix = 0
for oper in s:
if oper in leftchr:
stack.append(oper)
else:
try:
top = stack.pop()
except:
return "Impossible"
if opdict.get(top) != oper:
mix += 1
if len(stack) != 0:
return "Impossible"
return mix
s = str(input())
print(main(s)) | FUNC_DEF VAR IF FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN STRING ASSIGN VAR LIST STRING STRING STRING STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR RETURN STRING IF FUNC_CALL VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN STRING RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >.
The following definition of a regular bracket sequence is well-known, so you can be familiar with it.
Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS.
For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not.
Determine the least number of replaces to make the string s RBS.
-----Input-----
The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6.
-----Output-----
If it's impossible to get RBS from s print Impossible.
Otherwise print the least number of replaces needed to get RBS from s.
-----Examples-----
Input
[<}){}
Output
2
Input
{()}[]
Output
0
Input
]]
Output
Impossible | def check_open(char):
if char in "<{[(":
return 1
return 0
def find_type(char):
if char in "()":
return 0
elif char in "[]":
return 1
elif char in "{}":
return 2
else:
return 3
s = input()
stack = []
impossible = 0
count = 0
for char in s:
if check_open(char):
stack.append(char)
else:
if len(stack) == 0:
impossible = True
break
if check_open(stack[-1]):
if find_type(char) != find_type(stack[-1]):
count += 1
stack.pop()
else:
impossible = True
break
if len(stack) != 0:
impossible = True
if impossible:
print("Impossible")
else:
print(count) | FUNC_DEF IF VAR STRING RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR |
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >.
The following definition of a regular bracket sequence is well-known, so you can be familiar with it.
Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS.
For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not.
Determine the least number of replaces to make the string s RBS.
-----Input-----
The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6.
-----Output-----
If it's impossible to get RBS from s print Impossible.
Otherwise print the least number of replaces needed to get RBS from s.
-----Examples-----
Input
[<}){}
Output
2
Input
{()}[]
Output
0
Input
]]
Output
Impossible | s = input()
stack = []
open = ["<", "{", "(", "["]
close = [">", "}", ")", "]"]
ans = 0
for i in range(len(s)):
if s[i] in open:
stack.append(s[i])
else:
if len(stack) == 0:
print("Impossible")
exit()
head = stack.pop()
k = close.index(s[i])
if head != open[k]:
ans += 1
if len(stack) > 0:
print("Impossible")
exit()
print(ans) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST STRING STRING STRING STRING ASSIGN VAR LIST STRING STRING STRING STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR |
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >.
The following definition of a regular bracket sequence is well-known, so you can be familiar with it.
Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS.
For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not.
Determine the least number of replaces to make the string s RBS.
-----Input-----
The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6.
-----Output-----
If it's impossible to get RBS from s print Impossible.
Otherwise print the least number of replaces needed to get RBS from s.
-----Examples-----
Input
[<}){}
Output
2
Input
{()}[]
Output
0
Input
]]
Output
Impossible | IMPOSSIBLE = "Impossible"
scopes = {"{": "}", "(": ")", "[": "]", "<": ">"}
stack = []
swap = 0
for i in input():
if i in scopes:
stack.append(i)
elif stack:
if scopes[stack[-1]] != i:
swap += 1
stack.pop()
else:
print(IMPOSSIBLE)
break
else:
print(swap if not stack else IMPOSSIBLE) | ASSIGN VAR STRING ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR |
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >.
The following definition of a regular bracket sequence is well-known, so you can be familiar with it.
Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS.
For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not.
Determine the least number of replaces to make the string s RBS.
-----Input-----
The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6.
-----Output-----
If it's impossible to get RBS from s print Impossible.
Otherwise print the least number of replaces needed to get RBS from s.
-----Examples-----
Input
[<}){}
Output
2
Input
{()}[]
Output
0
Input
]]
Output
Impossible | s = input()
def check_str(s):
brackets = {"<": ">", "{": "}", "[": "]", "(": ")"}
res = 0
stack = []
for c in s:
if c in brackets:
stack.append(c)
else:
if stack:
top = stack.pop()
if top in brackets:
if brackets[top] != c:
res += 1
continue
return "Impossible"
if stack:
return "Impossible"
return res
print(check_str(s)) | ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR IF VAR VAR VAR VAR NUMBER RETURN STRING IF VAR RETURN STRING RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >.
The following definition of a regular bracket sequence is well-known, so you can be familiar with it.
Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS.
For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not.
Determine the least number of replaces to make the string s RBS.
-----Input-----
The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6.
-----Output-----
If it's impossible to get RBS from s print Impossible.
Otherwise print the least number of replaces needed to get RBS from s.
-----Examples-----
Input
[<}){}
Output
2
Input
{()}[]
Output
0
Input
]]
Output
Impossible | s = input()
open = "[<({"
close = "]>)}"
clopen = {"]": "[", "}": "{", ">": "<", ")": "("}
popen = []
c = 0
for i in range(len(s)):
if s[i] in open:
popen.append(s[i])
else:
if len(popen) == 0:
print("Impossible")
return
p = popen.pop()
if clopen[s[i]] != p:
c += 1
print(c if len(popen) == 0 else "Impossible") | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR FUNC_CALL VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR STRING |
Given a string of characters, find the length of the longest proper prefix which is also a proper suffix.
NOTE: Prefix and suffix can be overlapping but they should not be equal to the entire string.
Example 1:
Input: s = "abab"
Output: 2
Explanation: "ab" is the longest proper
prefix and suffix.
Example 2:
Input: s = "aaaa"
Output: 3
Explanation: "aaa" is the longest proper
prefix and suffix.
Your task:
You do not need to read any input or print anything. The task is to complete the function lps(), which takes a string as input and returns an integer.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 β€ |s| β€ 10^{5}
s contains lower case English alphabets | class Solution:
def lps(self, string):
first = 0
second = 2
Lps_arr = [0] * (len(string) + 1)
string = " " + string
while second < len(string):
if string[first + 1] == string[second]:
Lps_arr[second] = first + 1
first += 1
second += 1
elif first != 0:
first = Lps_arr[first]
else:
second += 1
return Lps_arr[-1] | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP STRING VAR WHILE VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR NUMBER |
Given a string of characters, find the length of the longest proper prefix which is also a proper suffix.
NOTE: Prefix and suffix can be overlapping but they should not be equal to the entire string.
Example 1:
Input: s = "abab"
Output: 2
Explanation: "ab" is the longest proper
prefix and suffix.
Example 2:
Input: s = "aaaa"
Output: 3
Explanation: "aaa" is the longest proper
prefix and suffix.
Your task:
You do not need to read any input or print anything. The task is to complete the function lps(), which takes a string as input and returns an integer.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 β€ |s| β€ 10^{5}
s contains lower case English alphabets | class Solution:
def lps(self, s):
l = len(s)
l1 = [i for i in range(l) if s[i] == s[0]]
n = len(l1)
if n > 1:
for i in range(1, n):
d = l - l1[i]
a = s[:d]
b = s[l1[i] :]
if a == b:
return d
return 0
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR RETURN VAR RETURN NUMBER RETURN NUMBER |
Given a string of characters, find the length of the longest proper prefix which is also a proper suffix.
NOTE: Prefix and suffix can be overlapping but they should not be equal to the entire string.
Example 1:
Input: s = "abab"
Output: 2
Explanation: "ab" is the longest proper
prefix and suffix.
Example 2:
Input: s = "aaaa"
Output: 3
Explanation: "aaa" is the longest proper
prefix and suffix.
Your task:
You do not need to read any input or print anything. The task is to complete the function lps(), which takes a string as input and returns an integer.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 β€ |s| β€ 10^{5}
s contains lower case English alphabets | class Solution:
def lps(self, s):
j = -1
p = [j]
for c in s:
while j >= 0 and s[j] != c:
j = p[j]
j += 1
p.append(j)
return len(s[:j]) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST VAR FOR VAR VAR WHILE VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR |
Given a string of characters, find the length of the longest proper prefix which is also a proper suffix.
NOTE: Prefix and suffix can be overlapping but they should not be equal to the entire string.
Example 1:
Input: s = "abab"
Output: 2
Explanation: "ab" is the longest proper
prefix and suffix.
Example 2:
Input: s = "aaaa"
Output: 3
Explanation: "aaa" is the longest proper
prefix and suffix.
Your task:
You do not need to read any input or print anything. The task is to complete the function lps(), which takes a string as input and returns an integer.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 β€ |s| β€ 10^{5}
s contains lower case English alphabets | class Solution:
def lps(self, s):
lps = [0] * len(s)
prevLPS, i = 0, 1
while i < len(s):
if s[prevLPS] == s[i]:
lps[i] = prevLPS + 1
prevLPS += 1
i += 1
elif prevLPS == 0:
lps[i] = 0
i += 1
else:
prevLPS = lps[prevLPS - 1]
return lps[-1] | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER |
Given a string of characters, find the length of the longest proper prefix which is also a proper suffix.
NOTE: Prefix and suffix can be overlapping but they should not be equal to the entire string.
Example 1:
Input: s = "abab"
Output: 2
Explanation: "ab" is the longest proper
prefix and suffix.
Example 2:
Input: s = "aaaa"
Output: 3
Explanation: "aaa" is the longest proper
prefix and suffix.
Your task:
You do not need to read any input or print anything. The task is to complete the function lps(), which takes a string as input and returns an integer.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 β€ |s| β€ 10^{5}
s contains lower case English alphabets | class Solution:
def lps(self, s):
for substrlen in range(len(s) - 1, 0, -1):
index = 0
while True:
if index == substrlen:
return substrlen
if s[index] != s[len(s) - substrlen + index]:
break
index += 1
return 0 | CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR RETURN VAR IF VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN NUMBER |
Given a string of characters, find the length of the longest proper prefix which is also a proper suffix.
NOTE: Prefix and suffix can be overlapping but they should not be equal to the entire string.
Example 1:
Input: s = "abab"
Output: 2
Explanation: "ab" is the longest proper
prefix and suffix.
Example 2:
Input: s = "aaaa"
Output: 3
Explanation: "aaa" is the longest proper
prefix and suffix.
Your task:
You do not need to read any input or print anything. The task is to complete the function lps(), which takes a string as input and returns an integer.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 β€ |s| β€ 10^{5}
s contains lower case English alphabets | class Solution:
def lps(self, s):
arr = []
n = len(s)
for i in range(len(s) - 1):
if s[i] == s[-1]:
arr.append(i)
out = float("-inf")
for i in arr[::-1]:
if s[: i + 1] == s[n - i - 1 :]:
return i + 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR NUMBER RETURN NUMBER |
Given a string of characters, find the length of the longest proper prefix which is also a proper suffix.
NOTE: Prefix and suffix can be overlapping but they should not be equal to the entire string.
Example 1:
Input: s = "abab"
Output: 2
Explanation: "ab" is the longest proper
prefix and suffix.
Example 2:
Input: s = "aaaa"
Output: 3
Explanation: "aaa" is the longest proper
prefix and suffix.
Your task:
You do not need to read any input or print anything. The task is to complete the function lps(), which takes a string as input and returns an integer.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 β€ |s| β€ 10^{5}
s contains lower case English alphabets | class Solution:
def lps(self, s):
n = len(s)
lps = [0] * n
i, j = 1, 0
while i < n:
if s[i] == s[j]:
lps[i] = j + 1
i += 1
j += 1
elif j != 0:
j = lps[j - 1]
else:
lps[i] = 0
i += 1
return lps[-1] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER RETURN VAR NUMBER |
Given a string of characters, find the length of the longest proper prefix which is also a proper suffix.
NOTE: Prefix and suffix can be overlapping but they should not be equal to the entire string.
Example 1:
Input: s = "abab"
Output: 2
Explanation: "ab" is the longest proper
prefix and suffix.
Example 2:
Input: s = "aaaa"
Output: 3
Explanation: "aaa" is the longest proper
prefix and suffix.
Your task:
You do not need to read any input or print anything. The task is to complete the function lps(), which takes a string as input and returns an integer.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 β€ |s| β€ 10^{5}
s contains lower case English alphabets | class Solution:
def lps(self, str):
A = [(0) for i in range(len(str))]
j = 0
i = 1
while i < len(str):
if str[i] == str[j]:
A[i] = j + 1
j += 1
i += 1
elif j == 0:
i += 1
else:
j = A[j - 1]
return A[-1] | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER |
Given a string of characters, find the length of the longest proper prefix which is also a proper suffix.
NOTE: Prefix and suffix can be overlapping but they should not be equal to the entire string.
Example 1:
Input: s = "abab"
Output: 2
Explanation: "ab" is the longest proper
prefix and suffix.
Example 2:
Input: s = "aaaa"
Output: 3
Explanation: "aaa" is the longest proper
prefix and suffix.
Your task:
You do not need to read any input or print anything. The task is to complete the function lps(), which takes a string as input and returns an integer.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 β€ |s| β€ 10^{5}
s contains lower case English alphabets | class Solution:
def lps(self, s):
if s[0] * len(s) == s:
return len(s) - 1
i = 0
if len(s) % 2 == 0:
mid = len(s) // 2
else:
mid = len(s) // 2 + 1
j = 1
count = 0
k = 1
while i < len(s) - 1 and j < len(s):
if s[i] == s[j]:
count += 1
i += 1
j += 1
else:
i = 0
k += 1
j = k
count = 0
return count | CLASS_DEF FUNC_DEF IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER RETURN VAR |
Given a string of characters, find the length of the longest proper prefix which is also a proper suffix.
NOTE: Prefix and suffix can be overlapping but they should not be equal to the entire string.
Example 1:
Input: s = "abab"
Output: 2
Explanation: "ab" is the longest proper
prefix and suffix.
Example 2:
Input: s = "aaaa"
Output: 3
Explanation: "aaa" is the longest proper
prefix and suffix.
Your task:
You do not need to read any input or print anything. The task is to complete the function lps(), which takes a string as input and returns an integer.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 β€ |s| β€ 10^{5}
s contains lower case English alphabets | class Solution:
def lps(self, S):
lenpf = 0
i = 1
lps = [(0) for i in range(len(S))]
while i < len(S):
if S[i] == S[lenpf]:
lps[i] = lenpf + 1
lenpf += 1
i += 1
elif lenpf != 0:
lenpf = lps[lenpf - 1]
else:
lps[i] = 0
i += 1
return lps[len(S) - 1] | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER RETURN VAR BIN_OP FUNC_CALL VAR VAR NUMBER |
Given a string of characters, find the length of the longest proper prefix which is also a proper suffix.
NOTE: Prefix and suffix can be overlapping but they should not be equal to the entire string.
Example 1:
Input: s = "abab"
Output: 2
Explanation: "ab" is the longest proper
prefix and suffix.
Example 2:
Input: s = "aaaa"
Output: 3
Explanation: "aaa" is the longest proper
prefix and suffix.
Your task:
You do not need to read any input or print anything. The task is to complete the function lps(), which takes a string as input and returns an integer.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 β€ |s| β€ 10^{5}
s contains lower case English alphabets | class Solution:
def lps(self, s):
n = len(s)
if n == 1:
return 0
ele = s[0]
gle = s[-1]
i = 1
j = n - 2
ans = 0
str = ""
while i < n and j > 0:
if s[j] == gle:
str = s[0 : j + 1]
j -= 1
if s[i] == ele and s[i:] == str:
ans = max(ans, n - i)
return ans
i += 1
if n > 1 and ele == gle and ans == 0:
return 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING WHILE VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER RETURN VAR |
Given a string of characters, find the length of the longest proper prefix which is also a proper suffix.
NOTE: Prefix and suffix can be overlapping but they should not be equal to the entire string.
Example 1:
Input: s = "abab"
Output: 2
Explanation: "ab" is the longest proper
prefix and suffix.
Example 2:
Input: s = "aaaa"
Output: 3
Explanation: "aaa" is the longest proper
prefix and suffix.
Your task:
You do not need to read any input or print anything. The task is to complete the function lps(), which takes a string as input and returns an integer.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 β€ |s| β€ 10^{5}
s contains lower case English alphabets | class Solution:
def lps(self, s):
n = len(s)
k = 0
j = 1
while j + k < n:
if s[k] == s[j + k]:
k += 1
else:
j += 1
k = 0
return k | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER RETURN VAR |
Given a string of characters, find the length of the longest proper prefix which is also a proper suffix.
NOTE: Prefix and suffix can be overlapping but they should not be equal to the entire string.
Example 1:
Input: s = "abab"
Output: 2
Explanation: "ab" is the longest proper
prefix and suffix.
Example 2:
Input: s = "aaaa"
Output: 3
Explanation: "aaa" is the longest proper
prefix and suffix.
Your task:
You do not need to read any input or print anything. The task is to complete the function lps(), which takes a string as input and returns an integer.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 β€ |s| β€ 10^{5}
s contains lower case English alphabets | class Solution:
def lps(self, s):
i, j, k, cnt = 0, 1, 1, 0
n = len(s)
while i < n - 1 and j < n:
if s[i] == s[j]:
cnt = cnt + 1
i = i + 1
j = j + 1
elif s[i] != s[j]:
i = 0
k = k + 1
cnt = 0
j = k
return cnt | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR RETURN VAR |
Given a string of characters, find the length of the longest proper prefix which is also a proper suffix.
NOTE: Prefix and suffix can be overlapping but they should not be equal to the entire string.
Example 1:
Input: s = "abab"
Output: 2
Explanation: "ab" is the longest proper
prefix and suffix.
Example 2:
Input: s = "aaaa"
Output: 3
Explanation: "aaa" is the longest proper
prefix and suffix.
Your task:
You do not need to read any input or print anything. The task is to complete the function lps(), which takes a string as input and returns an integer.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 β€ |s| β€ 10^{5}
s contains lower case English alphabets | class Solution:
def lps(self, s):
n = len(s)
table = [0] * n
i, j = 1, 0
while i < n:
if s[i] == s[j]:
table[i] = j + 1
i += 1
j += 1
elif j > 0:
j = table[j - 1]
else:
i += 1
return table[-1]
def longest_prefix_suffix(self, s):
n = len(self.s)
if n < 2:
return 0
length = lps(self.s)
if length == 0:
return 0
if length >= n // 2:
return n // 2
return length | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER RETURN VAR |
Given a string of characters, find the length of the longest proper prefix which is also a proper suffix.
NOTE: Prefix and suffix can be overlapping but they should not be equal to the entire string.
Example 1:
Input: s = "abab"
Output: 2
Explanation: "ab" is the longest proper
prefix and suffix.
Example 2:
Input: s = "aaaa"
Output: 3
Explanation: "aaa" is the longest proper
prefix and suffix.
Your task:
You do not need to read any input or print anything. The task is to complete the function lps(), which takes a string as input and returns an integer.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 β€ |s| β€ 10^{5}
s contains lower case English alphabets | class Solution:
def lps(self, pat):
n = len(pat)
lps = [0] * n
le = 0
i = 1
lps[0] = 0
while i < n:
if pat[i] == pat[le]:
le += 1
lps[i] = le
i += 1
elif le != 0:
le = lps[le - 1]
else:
lps[i] = 0
i += 1
return lps[n - 1] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER RETURN VAR BIN_OP VAR NUMBER |
Given a string of characters, find the length of the longest proper prefix which is also a proper suffix.
NOTE: Prefix and suffix can be overlapping but they should not be equal to the entire string.
Example 1:
Input: s = "abab"
Output: 2
Explanation: "ab" is the longest proper
prefix and suffix.
Example 2:
Input: s = "aaaa"
Output: 3
Explanation: "aaa" is the longest proper
prefix and suffix.
Your task:
You do not need to read any input or print anything. The task is to complete the function lps(), which takes a string as input and returns an integer.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 β€ |s| β€ 10^{5}
s contains lower case English alphabets | class Solution:
def lps(self, s):
for index in range(1, len(s)):
prefixindex = 0
while (
index + prefixindex < len(s)
and s[prefixindex] == s[index + prefixindex]
):
prefixindex += 1
if index + prefixindex == len(s):
return len(s) - index
return 0 | CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR RETURN NUMBER |
Given a string of characters, find the length of the longest proper prefix which is also a proper suffix.
NOTE: Prefix and suffix can be overlapping but they should not be equal to the entire string.
Example 1:
Input: s = "abab"
Output: 2
Explanation: "ab" is the longest proper
prefix and suffix.
Example 2:
Input: s = "aaaa"
Output: 3
Explanation: "aaa" is the longest proper
prefix and suffix.
Your task:
You do not need to read any input or print anything. The task is to complete the function lps(), which takes a string as input and returns an integer.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 β€ |s| β€ 10^{5}
s contains lower case English alphabets | class Solution:
def lps(self, s):
n = len(s)
pi = [0] * n
j = 0
for i in range(1, n):
while j > 0 and s[i] != s[j]:
j = pi[j - 1]
if s[i] == s[j]:
j += 1
pi[i] = j
return pi[-1] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR WHILE VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR NUMBER |
Given a string of characters, find the length of the longest proper prefix which is also a proper suffix.
NOTE: Prefix and suffix can be overlapping but they should not be equal to the entire string.
Example 1:
Input: s = "abab"
Output: 2
Explanation: "ab" is the longest proper
prefix and suffix.
Example 2:
Input: s = "aaaa"
Output: 3
Explanation: "aaa" is the longest proper
prefix and suffix.
Your task:
You do not need to read any input or print anything. The task is to complete the function lps(), which takes a string as input and returns an integer.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 β€ |s| β€ 10^{5}
s contains lower case English alphabets | class Solution:
def lps(self, s):
arr = [0] * len(s)
j = 0
if len(s) == 1:
return 0
i = 1
while i < len(s):
if s[i] == s[j]:
arr[i] = j + 1
j += 1
i += 1
else:
if j != 0:
j = arr[j - 1]
if j == 0 and s[i] != s[j]:
arr[i] = 0
i += 1
return arr[-1] | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER RETURN VAR NUMBER |
Given a string of characters, find the length of the longest proper prefix which is also a proper suffix.
NOTE: Prefix and suffix can be overlapping but they should not be equal to the entire string.
Example 1:
Input: s = "abab"
Output: 2
Explanation: "ab" is the longest proper
prefix and suffix.
Example 2:
Input: s = "aaaa"
Output: 3
Explanation: "aaa" is the longest proper
prefix and suffix.
Your task:
You do not need to read any input or print anything. The task is to complete the function lps(), which takes a string as input and returns an integer.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 β€ |s| β€ 10^{5}
s contains lower case English alphabets | class Solution:
def lps(self, s):
n = len(s)
for i in range(n - 1, 0, -1):
for j in range(i):
if s[j] != s[n - i + j]:
break
else:
return i
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR RETURN NUMBER |
Given a string of characters, find the length of the longest proper prefix which is also a proper suffix.
NOTE: Prefix and suffix can be overlapping but they should not be equal to the entire string.
Example 1:
Input: s = "abab"
Output: 2
Explanation: "ab" is the longest proper
prefix and suffix.
Example 2:
Input: s = "aaaa"
Output: 3
Explanation: "aaa" is the longest proper
prefix and suffix.
Your task:
You do not need to read any input or print anything. The task is to complete the function lps(), which takes a string as input and returns an integer.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 β€ |s| β€ 10^{5}
s contains lower case English alphabets | class Solution:
def lps(self, s):
if len(s) == 0:
return 0
if len(s) == 1:
return 1
b = "".join(sorted(s))
if b[0] == b[len(b) - 1]:
return len(b) - 1
i = 0
j = 1
n = len(s)
count = 0
while j < n:
count = 0
if s[j] == s[i] and i + (j - n - 1) < j:
ii = i
jj = j
while jj < n:
if s[ii] != s[jj]:
break
ii += 1
jj += 1
count += 1
if jj == n:
return count
j += 1
return count | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR RETURN VAR VAR NUMBER RETURN VAR |
Given a string of characters, find the length of the longest proper prefix which is also a proper suffix.
NOTE: Prefix and suffix can be overlapping but they should not be equal to the entire string.
Example 1:
Input: s = "abab"
Output: 2
Explanation: "ab" is the longest proper
prefix and suffix.
Example 2:
Input: s = "aaaa"
Output: 3
Explanation: "aaa" is the longest proper
prefix and suffix.
Your task:
You do not need to read any input or print anything. The task is to complete the function lps(), which takes a string as input and returns an integer.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 β€ |s| β€ 10^{5}
s contains lower case English alphabets | class Solution:
def lps(self, s):
n = len(s)
ans = 0
fli = []
for i in range(1, n):
if s[i] == s[0]:
fli.append(i)
for i in fli:
if s[i:] == s[: n - i]:
ans = n - i
break
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN 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 almost_difference():
n = int(input())
if n == 1:
return 0
dict_equal = dict()
array = [int(el) for el in input().split()]
ad_sum = 0
prev_sum = 0
for i in range(n):
if not array[i] in dict_equal.keys():
dict_equal[array[i]] = 0
if not array[i] - 1 in dict_equal.keys():
dict_equal[array[i] - 1] = 0
if not array[i] + 1 in dict_equal.keys():
dict_equal[array[i] + 1] = 0
ad_sum = (
ad_sum
+ i * array[i]
- prev_sum
+ dict_equal[array[i] + 1]
- dict_equal[array[i] - 1]
)
dict_equal[array[i]] += 1
prev_sum += array[i]
return ad_sum
print(almost_difference()) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER RETURN NUMBER ASSIGN 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 VAR IF VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR NUMBER 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 VAR VAR VAR NUMBER VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR 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. | import sys
n = int(input())
a = list(map(int, input().split()))
s = 0
ans = 0
cnt = {}
for i in range(n):
m = i
tmp = s
for j in range(a[i] - 1, a[i] + 2):
if j in cnt:
m -= cnt[j]
tmp -= cnt[j] * j
ans += m * a[i] - tmp
s += a[i]
if a[i] in cnt:
cnt[a[i]] += 1
else:
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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR 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())
nums = list(map(int, input().split()))
cnt = {}
answer = 0
summ = 0
for i, item in enumerate(nums):
cnt[item] = cnt.get(item, 0) + 1
summ += item
answer += (i + 1) * item - summ
if item + 1 in cnt:
answer += cnt[item + 1]
if item - 1 in cnt:
answer -= cnt[item - 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 DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR 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 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(k) for k in input().split()]
res = 0
tot = 0
um = {(-100): -100}
for i in range(0, n):
if arr[i] in um:
um[arr[i]] += 1
else:
um[arr[i]] = 1
res += i * arr[i] - tot
if arr[i] + 1 in um:
res += um[arr[i] + 1]
if arr[i] - 1 in um:
res -= um[arr[i] - 1]
tot += arr[i]
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 ASSIGN VAR DICT NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR 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 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 = [0] + list(map(int, input().split()))
s = [0] * len(a)
mp = {}
for i in range(1, n + 1):
s[i] = s[i - 1] + a[i]
if mp.get(a[i]):
mp[a[i]] += 1
else:
mp[a[i]] = 1
ans = 0
for i in range(1, n + 1):
ans += s[n] - s[i - 1]
l = mp.get(a[i] - 1, 0)
m = mp.get(a[i], 0)
r = mp.get(a[i] + 1, 0)
ans -= l * (a[i] - 1) + m * a[i] + r * (a[i] + 1)
ans -= a[i] * (n - i + 1 - l - m - r)
mp[a[i]] -= 1
if mp[a[i]] == 0:
mp.pop(a[i])
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER 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 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 VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER 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. | d = {}
n = int(input())
arr = list(map(int, input().split()))
prefix = [(0) for i in range(n)]
prefix[0] = arr[0]
for i in range(1, n):
prefix[i] = prefix[i - 1] + arr[i]
for i in range(1, n):
try:
d[arr[i]] += 1
except:
d[arr[i]] = 1
s = 0
for i in range(n - 1):
cnt = 0
minus = 0
if arr[i] in d:
cnt += d[arr[i]]
minus = minus + d[arr[i]] * arr[i]
if arr[i] + 1 in d:
cnt += d[arr[i] + 1]
minus = minus + d[arr[i] + 1] * (arr[i] + 1)
if arr[i] - 1 in d:
minus = minus + d[arr[i] - 1] * (arr[i] - 1)
cnt += d[arr[i] - 1]
s = s + prefix[n - 1] - prefix[i] - minus - (n - i - 1 - cnt) * arr[i]
if arr[i + 1] in d:
if d[arr[i + 1]] == 1:
d.pop(arr[i + 1])
else:
d[arr[i + 1]] -= 1
print(s) | 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 VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR 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())
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):
ans -= d.get(t[i] - 1, 0)
ans += d.get(t[i] + 1, 0)
d[t[i]] = d.get(t[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 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 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. | def main():
n = int(input())
arr = list(map(int, input().split()))
cnt = dict()
ss = dict()
for i in range(n):
cnt[arr[i]] = 0
cnt[arr[i] - 1] = 0
cnt[arr[i] + 1] = 0
ss[arr[i] - 1] = 0
ss[arr[i] + 1] = 0
ss[arr[i]] = 0
sm = 0
ans = 0
for i in range(n):
ans -= sm
ans += i * arr[i]
ans -= cnt[arr[i] - 1] * arr[i]
ans -= cnt[arr[i] + 1] * arr[i]
ans += ss[arr[i] + 1]
ans += ss[arr[i] - 1]
sm += arr[i]
cnt[arr[i]] += 1
ss[arr[i]] += arr[i]
print(ans)
main() | FUNC_DEF 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 FUNC_CALL VAR 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 BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR 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. | summ = 0
glob = 0
N = int(input())
a = {}
cnt = 0
huh = list(map(int, input().split()))
for i in huh:
if i - 1 not in a:
a[i - 1] = 0
if i + 1 not in a:
a[i + 1] = 0
if i not in a:
a[i] = 0
glob += (cnt - a[i - 1] - a[i + 1]) * i - (
summ - a[i - 1] * (i - 1) - a[i + 1] * (i + 1)
)
a[i] += 1
summ += i
cnt += 1
print(glob) | ASSIGN VAR NUMBER ASSIGN VAR NUMBER 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 FOR VAR VAR 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 IF VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER 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())
l = list(map(int, input().split()))
s = 0
for i in range(n):
s += l[i] * i - l[i] * (n - i - 1)
d = {}
c = 0
for i in range(n):
if l[i] not in d:
d[l[i]] = 1
else:
d[l[i]] += 1
if l[i] + 1 in d:
c -= d[l[i] + 1]
if l[i] - 1 in d:
c += d[l[i] - 1]
print(s - c) | 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 VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR 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 BIN_OP 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())
xs = [int(_) for _ in input().split()]
cnt = {}
sm = 0
ans = 0
for i in range(n):
if xs[i] - 1 not in cnt:
cnt[xs[i] - 1] = 0
if xs[i] + 1 not in cnt:
cnt[xs[i] + 1] = 0
if xs[i] not in cnt:
cnt[xs[i]] = 0
ans = ans + i * xs[i] - sm - cnt[xs[i] - 1] + cnt[xs[i] + 1]
sm = sm + xs[i]
cnt[xs[i]] = cnt[xs[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 FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN 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 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())
lst = input().split()
lst = [int(x) for x in lst]
ans = 0
cnt = {}
for i, e in enumerate(lst):
ans += (2 * i + 1 - n) * e
cnt[e] = 0
for e in lst:
if e - 1 in cnt.keys():
ans -= cnt[e - 1]
if e + 1 in cnt.keys():
ans += cnt[e + 1]
cnt[e] += 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 ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER FOR 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 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())
ssum = 0
fans = 0
mp = {(0): 0}
a = list(map(int, input().strip().split()))[:n]
def find(x):
if mp.get(x):
return mp[x]
else:
return 0
for i in range(n):
if not mp.get(a[i]):
mp[a[i]] = 0
mp[a[i]] += 1
fans += i * a[i] - ssum + find(a[i] + 1) - find(a[i] - 1)
ssum += a[i]
print(fans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR RETURN VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER 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 |
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
mx = a[0]
dic = dict()
for i in range(n):
dic[a[i]] = 0
for i in range(n):
s = s - a[i] * (n - i - 1) + a[i] * i
if a[i] - 1 in dic:
s = s + dic[a[i] - 1] * (a[i] - 1 - a[i])
if a[i] + 1 in dic:
s = s + dic[a[i] + 1] * (a[i] + 1 - a[i])
d = dic[a[i]] + 1
t = {a[i]: d}
dic.update(t)
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 NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR DICT VAR VAR VAR EXPR FUNC_CALL 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())
v = [0] + [int(x) for x in input().split(" ")]
ma = {}
st = 0
ris = 0
for p in range(n, 0, -1):
val = v[p]
v2 = val + 1
c2 = ma.get(v2, 0)
v1 = val
c1 = ma.get(v1, 0)
v0 = val - 1
c0 = ma.get(v0, 0)
cnt = n - p - c2 - c1 - c0
sp = st - v2 * c2 - v1 * c1 - v0 * c0
ris -= cnt * val
ris += sp
st += val
ma[val] = ma.get(val, 0) + 1
print(ris) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR 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())
a = [int(x) for x in input().split()]
cnt = {}
for i in a:
cnt[i] = 0
cnt[i - 1] = 0
cnt[i + 1] = 0
c = -(n - 1)
ans = 0
for i in range(n):
ans += c * a[i]
c += 2
cnt[a[i]] += 1
ans += cnt[a[i] + 1] - cnt[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 DICT FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR NUMBER 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(m) for m in input().split()]
mp = dict()
pre = 0
ans = 0
for i in range(0, n):
ans += a[i] * i - pre + mp.get(a[i] + 1, 0) - mp.get(a[i] - 1, 0)
pre += a[i]
mp[a[i]] = mp.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 FUNC_CALL VAR ASSIGN 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 FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR 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())
x = input().split()
s = 0
ans = 0
mp = {}
for i in range(0, n):
s = s + int(x[n - i - 1])
ans += s - (i + 1) * int(x[n - i - 1])
if int(x[n - i - 1]) + 1 in mp:
ans -= mp[int(x[n - i - 1]) + 1]
if int(x[n - i - 1]) - 1 in mp:
ans += mp[int(x[n - i - 1]) - 1]
if int(x[n - i - 1]) in mp:
mp[int(x[n - i - 1])] += 1
else:
mp[int(x[n - i - 1])] = 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP 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())
arr = list(map(int, input().split()))
sum = 0
sum2 = 0
for i in range(n - 1):
sum = sum - arr[i] * (n - i - 1)
for i in range(n - 1, 0, -1):
sum2 += arr[i]
sum += sum2
dict = {arr[n - 1]: 1}
for i in range(n - 2, -1, -1):
if arr[i] + 1 in dict:
sum -= dict[arr[i] + 1]
if arr[i] - 1 in dict:
sum += dict[arr[i] - 1]
if arr[i] in dict:
dict[arr[i]] += 1
else:
dict[arr[i]] = 1
print(sum) | 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 BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR VAR VAR ASSIGN VAR DICT VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER 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 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. | numCnt = int(input())
nums = [int(i) for i in input().split(" ")]
cnt = dict()
curSum = 0
curCnt = 0
ans = 0
for a in nums:
ans += curCnt * a - curSum
ans += cnt.get(a + 1, 0)
ans -= cnt.get(a - 1, 0)
curSum += a
curCnt += 1
if not a in cnt:
cnt[a] = 0
cnt[a] += 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR 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. | class BIT:
def __init__(self, n):
self.n = n
self.bit = [0] * (n + 1)
def _sum(self, i):
s = 0
while i > 0:
s += self.bit[i]
i -= i & -i
return s
def add(self, i, val):
i = i + 1
while i <= self.n:
self.bit[i] += val
i += i & -i
def get_sum(self, l, r):
return self._sum(r) - self._sum(l)
def add1(ind_li, val):
for i in ind_li:
st.add(i, val)
def cnt1(ind_li):
for i in ind_li:
cnt.add(i, 1)
def calc(ind_li, val):
res = 0
for i in ind_li:
res1 = st.get_sum(0, i) - val * cnt.get_sum(0, i)
res2 = st.get_sum(i + 1, n) - val * cnt.get_sum(i + 1, n)
res += res2 - res1
return res
n = int(input())
a = list(map(int, input().split()))
st = BIT(n)
cnt = BIT(n)
b = sorted(zip(a, range(len(a))))
b = b + [(b[-1][0] + 1, b[-1][1])]
prev = 0
now = b[0][0]
tmp_now = []
tmp_prev = []
ans = 0
for i in range(n + 1):
if now == b[i][0]:
tmp_now.append(b[i][1])
continue
if prev + 1 == now:
ans += calc(tmp_now, now)
add1(tmp_prev, prev)
cnt1(tmp_prev)
else:
add1(tmp_prev, prev)
cnt1(tmp_prev)
ans += calc(tmp_now, now)
prev = now
tmp_prev = tmp_now[0:]
tmp_now = [b[i][1]]
now = b[i][0]
print(ans) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR 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 FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR LIST BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST 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 = [int(x) for x in input().split()]
count = {}
cum = 0
tot = 0
for i in range(0, n):
tot = tot + i * a[i] - cum
if a[i] - 1 in count:
tot = tot - count[a[i] - 1]
if a[i] + 1 in count:
tot = tot + count[a[i] + 1]
if a[i] in count:
count[a[i]] = count[a[i]] + 1
else:
count[a[i]] = 1
cum = cum + a[i]
print(tot) | 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 NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR 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 IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR 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 = input().split()
a = [int(x) for x in a]
cnt = {}
for i in range(n):
cnt[a[i]] = 0
cnt[a[i] + 1] = 0
cnt[a[i] - 1] = 0
ans = 0
res = 0
for i in range(n):
ans = ans + (i - cnt[a[i]] - cnt[a[i] + 1] - cnt[a[i] - 1]) * a[i]
ans = ans - (
res - cnt[a[i] - 1] * (a[i] - 1) - cnt[a[i]] * a[i] - cnt[a[i] + 1] * (a[i] + 1)
)
cnt[a[i]] = cnt[a[i]] + 1
res = res + a[i]
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 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 NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR 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(x) for x in input().split()]
m = {}
total = 0
ans = 0
for i in range(n):
ans += a[i] * i - total
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
total += a[i]
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 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 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 = input().split()
dic = {}
ans = 0
sum = 0
for i in range(0, n):
x = int(a[i])
sum += x
ans += (i + 1) * x - sum
if x in dic:
dic[x] += 1
else:
dic[x] = 1
if x + 1 in dic:
ans += dic[x + 1]
if x - 1 in dic:
ans -= dic[x - 1]
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN 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())
a = list(map(int, input().split(" ")))
sum1 = a[n - 1]
ans = 0
map1 = {}
map1[a[n - 1]] = 1
for i in range(n - 2, -1, -1):
c1 = map1.get(a[i], 0)
c2 = map1.get(a[i] + 1, 0)
c3 = map1.get(a[i] - 1, 0)
temp = sum1 - c1 * a[i] - c2 * (a[i] + 1) - c3 * (a[i] - 1)
cnt2 = (n - 1 - i - c1 - c2 - c3) * a[i]
ans += temp
ans -= cnt2
sum1 += a[i]
if a[i] in map1:
map1[a[i]] += 1
else:
map1[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 STRING ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR 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())
t = list(map(int, input().split()))
c = {}
u = {}
a = [(0) for _ in range(n)]
for i in range(n):
a[i] = t[i] * i - t[i] * (n - 1 - i)
if t[i] not in u:
u[t[i]] = [i]
else:
u[t[i]].append(i)
l = []
for x in u:
l.append(x)
l.sort()
p = len(l)
res = 0
for i in range(p - 1):
if l[i] + 1 in u:
l1 = len(u[l[i]])
l2 = len(u[l[i] + 1])
j1 = 0
j2 = 0
t1 = 0
t2 = 0
for j in range(l1 + l2):
if j1 == l1:
t2 += 1
j2 += 1
res -= t1
elif j2 == l2:
t1 += 1
j1 += 1
res += t2
elif u[l[i]][j1] < u[l[i] + 1][j2]:
t1 += 1
j1 += 1
res += t2
else:
t2 += 1
j2 += 1
res -= t1
for i in range(n):
res += a[i]
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 DICT ASSIGN VAR DICT ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR VAR FOR VAR FUNC_CALL 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()))
sm = 0
ans = 0
mp = {}
for i in range(n):
ans += i * a[i] - sm
ans -= mp.get(a[i] - 1, 0)
ans += mp.get(a[i] + 1, 0)
mp[a[i]] = mp.get(a[i], 0) + 1
sm += 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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR 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 ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER 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())
f = dict()
s = 0
ret = 0
a = list(map(int, input().split()))
for i in range(0, n):
ret += a[i] * i - s
ret -= f.get(a[i] - 1, 0)
ret += f.get(a[i] + 1, 0)
s += a[i]
f[a[i]] = f.get(a[i], 0) + 1
print(ret) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR 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 NUMBER VAR 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 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. | dic = {}
n = int(input())
arr = [int(x) for x in input().split()]
for x in arr:
if x not in dic:
dic[x] = 1
else:
dic[x] += 1
total = 0
su = 0
for x in arr:
su += x
for x in range(len(arr)):
su -= arr[x]
hold = 0
cnt = n - x - 1
for y in range(-1, 2):
num = arr[x] + y
if num in dic:
hold += dic[num] * num
cnt -= dic[num]
dic[arr[x]] -= 1
hold -= arr[x]
cnt += 1
su -= hold
total += su - arr[x] * cnt
su += hold
print(total) | 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 ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR BIN_OP VAR 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. | N = int(input())
A = list(map(int, input().split()))
dic = {}
ans = 0
for i, j in enumerate(A):
ans += (2 * i - N + 1) * j - dic.get(j - 1, 0) + dic.get(j + 1, 0)
dic[j] = dic.get(j, 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 FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER 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. | n = int(input())
a = list(map(int, input().split()))
occ = {}
for i in a:
occ[i] = occ.get(i, 0) + 1
s = sum(a)
ans = 0
j = n
for i in a:
val = s
nb_cnt = j
if i - 1 > 0:
val -= (i - 1) * occ.get(i - 1, 0)
nb_cnt -= occ.get(i - 1, 0)
val -= i * occ.get(i, 0)
nb_cnt -= occ.get(i, 0)
val -= (i + 1) * occ.get(i + 1, 0)
nb_cnt -= occ.get(i + 1, 0)
ans += val - i * nb_cnt
occ[i] = occ[i] - 1
s -= i
j -= 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 VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN 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())
v = []
mp = {}
for i in input().split():
v.append(int(i))
mp[int(i)] = 0
mp[int(i) - 1] = 0
mp[int(i) + 1] = 0
sum = 0
ele = 0
ans = 0
for i in v[::-1]:
ans = ans + (sum - ele * i)
a = (i + 1) * mp[i + 1] - mp[i + 1] * i
b = (i - 1) * mp[i - 1] - mp[i - 1] * i
ans = ans - (a + b)
mp[i] = mp[i] + 1
sum = sum + i
ele = ele + 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP 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. | vcnt = {}
n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(0, n):
ans += a[i] * (i * 2 + 1 - n)
for i in range(0, n):
v = a[i]
if v - 1 in vcnt:
ans -= vcnt[v - 1]
if v + 1 in vcnt:
ans += vcnt[v + 1]
if not v in vcnt:
vcnt[v] = 1
else:
vcnt[v] += 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 FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN 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 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(" ")))
b = {}
ans = 0
for i in range(0, n):
if a[i] - 1 in b:
ans -= b[a[i] - 1]
if a[i] + 1 in b:
ans += b[a[i] + 1]
if a[i] in b.keys():
b[a[i]] += 1
else:
b[a[i]] = 1
k = n - 1
for i in range(0, int(n / 2)):
ans += k * (a[n - 1 - i] - a[i])
k -= 2
print(ans) | 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 DICT ASSIGN VAR NUMBER 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 FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR BIN_OP BIN_OP 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. | from sys import *
n = int(stdin.readline())
a = list(map(int, stdin.read().split()))
freq = {}
ans = 0
psum = 0
for i in range(len(a)):
psum += a[i]
ans += (i + 1) * a[i] - psum
if a[i] in freq:
freq[a[i]] += 1
else:
freq[a[i]] = 1
if a[i] + 1 in freq:
ans -= (a[i] - (a[i] + 1)) * freq[a[i] + 1]
if a[i] - 1 in freq:
ans -= (a[i] - (a[i] - 1)) * freq[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 ASSIGN VAR NUMBER ASSIGN VAR 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 BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER 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()]
sum = list()
sum.append(0)
cnt = dict()
for i in range(0, n):
cnt[a[i]] = 0
cnt[a[i] - 1] = 0
cnt[a[i] + 1] = 0
sum.append(a[i] + sum[i])
ans = 0
for i in range(0, n):
cur = 0
cur += i * a[i] - sum[i]
cur -= cnt[a[i] - 1]
cur += cnt[a[i] + 1]
cnt[a[i]] += 1
ans += cur
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 EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR 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 EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER 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. | cnt = {}
n = int(input())
tot_sum = 0
tot_cnt = 0
res = 0
for y in input().split():
y = int(y)
tot_sum += y
tot_cnt += 1
if y in cnt:
cnt[y] += 1
else:
cnt[y] = 1
tmp_sum = tot_sum
tmp_cnt = tot_cnt
for d in range(-1, 2):
x = y + d
if x in cnt:
tmp_sum -= x * cnt[x]
tmp_cnt -= cnt[x]
res += tmp_cnt * y - tmp_sum
print(res) | ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR VAR 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. | import sys
def main():
N = int(input())
A = list(map(int, input().split()))
cnt = dict()
pref = 0
ans = 0
for i in range(N):
v = A[i]
cnt[v - 1] = cnt.get(v - 1, 0)
cnt[v] = cnt.get(v, 0)
cnt[v + 1] = cnt.get(v + 1, 0)
add = pref - (v - 1) * cnt[v - 1] - v * cnt[v] - (v + 1) * cnt[v + 1]
num_pairs = i - cnt[v - 1] - cnt[v] - cnt[v + 1]
add -= v * num_pairs
ans -= add
pref += v
cnt[v] += 1
print(ans)
main() | IMPORT FUNC_DEF 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 FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR 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. | import sys
mm = {}
sum = 0
ans = 0
line = sys.stdin.readline()
n = int(line)
line = sys.stdin.readline()
ls = line.split()
i = 0
for t in ls:
t = int(ls[i])
ans += t * i + sum
sum -= t
for j in range(-1, 2):
if t + j in mm:
ans += j * mm[t + j]
if t in mm:
mm[t] += 1
else:
mm[t] = 1
i += 1
print(ans) | IMPORT ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN 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())
v = list(map(int, input().split()))
mt = {}
sum = 0
ans = 0
for i in range(n - 1, -1, -1):
if not v[i] in mt:
mt[v[i]] = 0
if not v[i] + 1 in mt:
mt[v[i] + 1] = 0
if not v[i] - 1 in mt:
mt[v[i] - 1] = 0
cnt = n - i - 1 - mt[v[i]] - mt[v[i] + 1] - mt[v[i] - 1]
ans += (
sum
- mt[v[i]] * v[i]
- mt[v[i] + 1] * (v[i] + 1)
- mt[v[i] - 1] * (v[i] - 1)
- cnt * v[i]
)
mt[v[i]] += 1
sum += v[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 DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR 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. | n = int(input())
a = list(map(int, input().split()))
ans = 0
s = 0
mp = {}
for x in a:
for i in range(-1, 2):
mp[x + i] = 0
for i in range(1, n + 1):
x = a[i - 1]
s += x
mp[x] += 1
ans += i * x - s - (mp[x - 1] - 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 NUMBER ASSIGN VAR DICT FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR 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. | n = input()
arr = list(map(int, input().split()))
mp = {}
ans = 0
sums = 0
count = 0
for i in range(int(n) - 1, -1, -1):
sums += arr[i]
if arr[i] in mp:
mp[arr[i]] += 1
else:
mp[arr[i]] = 1
count = count + 1
ans1 = 0
ans2 = 0
if arr[i] + 1 in mp:
ans1 = mp[arr[i] + 1]
if arr[i] - 1 in mp:
ans2 = mp[arr[i] - 1]
ans += sums + ans2 - ans1 - arr[i] * count
print(ans) | ASSIGN 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 NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER 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 VAR BIN_OP BIN_OP BIN_OP VAR VAR 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())
dic = {}
sl = list(map(int, input().split()))
summ = [(0) for i in range(n)]
summ[n - 1] = sl[n - 1]
for i in range(n - 2, -1, -1):
summ[i] = summ[i + 1] + sl[i]
for i in sl:
if i in dic:
dic[i] = dic[i] + 1
else:
dic[i] = 1
fuckdiff = 0
for i in range(0, n - 1):
fuckdiff += summ[i] - (n - i) * sl[i]
if sl[i] + 1 in dic:
fuckdiff -= dic[sl[i] + 1]
if sl[i] - 1 in dic:
fuckdiff += dic[sl[i] - 1]
dic[sl[i]] -= 1
print(fuckdiff) | 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 NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR 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 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 = input()
arr = arr.split()
for i in range(n):
arr[i] = int(arr[i])
ans = 0
sm = arr[0]
m = {}
m[sm] = 1
for i in range(1, n):
a = 0
b = 0
c = 0
d = 0
x = arr[i]
if not x in m:
m[x] = 0
m[x] += 1
if x - 1 in m:
a = m[x - 1]
if x + 1 in m:
b = m[x + 1]
ans = ans + x * (i - a - b) - (sm - a * (x - 1) - b * (x + 1))
sm += x
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR 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 ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR 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())
a = [int(i) for i in input().split()]
b = []
c = {}
s = 0
for i in range(n):
if i == 0:
b.append(a[i])
else:
b.append(b[-1] + a[i])
try:
lower = c[a[i] - 1]
except KeyError:
lower = 0
try:
this = c[a[i]]
except KeyError:
this = 0
try:
upper = c[a[i] + 1]
except KeyError:
upper = 0
tokeep = i - lower - this - upper
s += (
tokeep * a[i]
- b[i - 1]
+ lower * (a[i] - 1)
+ this * a[i]
+ upper * (a[i] + 1)
)
try:
c[a[i]] += 1
except KeyError:
c[a[i]] = 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 LIST ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR 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())
nums = list(map(int, input().split()))
pref_sums = []
helper = []
all_numbers = {i: (0) for i in nums}
for i in nums:
all_numbers[i] += 1
cur = {}
for i in range(n):
c = nums[i]
if c not in cur.keys():
cur[c] = 1
else:
cur[c] += 1
lesser_all = 0
lesser_cur = 0
bigger_all = 0
bigger_cur = 0
if c - 1 in all_numbers.keys():
lesser_all = all_numbers[c - 1]
if c - 1 in cur.keys():
lesser_cur = cur[c - 1]
if c + 1 in all_numbers.keys():
bigger_all = all_numbers[c + 1]
if c + 1 in cur.keys():
bigger_cur = cur[c + 1]
helper.append((lesser_all - lesser_cur, bigger_all - bigger_cur))
if len(pref_sums) == 0:
pref_sums.append(c)
else:
pref_sums.append(c + pref_sums[-1])
answer = 0
for i in range(n):
k = helper[i][1]
l = helper[i][0]
mul = n - i - 1
s = pref_sums[n - 1] - pref_sums[i]
answer += s - mul * nums[i] - k + l
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 LIST ASSIGN VAR LIST ASSIGN VAR VAR NUMBER VAR VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR 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. | n = int(input())
a = list(map(int, input().split(" ")))
s = 0
ss = 0
m = n
mp = {}
while n > 0:
n -= 1
ss += a[n]
s += ss - (m - n) * a[n]
mp.setdefault(a[n] - 1, 0)
mp.setdefault(a[n] + 1, 0)
mp.setdefault(a[n], 0)
s += mp[a[n] - 1] - mp[a[n] + 1]
mp[a[n]] += 1
print(s) | 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 VAR ASSIGN VAR DICT WHILE VAR NUMBER VAR NUMBER VAR VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER 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())
vs = list(map(int, input().split()))
dc = {}
for x in vs:
dc[x] = 0
for x in vs:
dc[x] += 1
ans = 0
for i in range(n):
ans += i * vs[i]
ans -= (n - 1 - i) * vs[i]
for i in range(n):
dc[vs[i]] -= 1
try:
ans -= dc[vs[i] + 1]
except:
pass
try:
ans += dc[vs[i] - 1]
except:
pass
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 VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER 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 FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER 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 = input().split(" ")
mp = {}
for i in range(len(a)):
a[i] = int(a[i])
mp[a[i]] = 0
for i in a:
mp[i] += 1
sum = []
sum.append(a[0])
for i in range(1, len(a)):
sum.append(sum[i - 1] + a[i])
ans = 0
for i in range(0, n - 1):
cur = sum[n - 1] - sum[i]
rang = n - 1 - i
mp[a[i]] -= 1
cur -= rang * a[i]
cur -= mp.get(a[i] + 1, 0)
cur += mp.get(a[i] - 1, 0)
ans += cur
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER 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. | import sys
input = sys.stdin.readline
n = int(input())
a = map(int, input().split())
d = {}
ans = sum = num = 0
for x in a:
t = x * num - sum
if x - 1 not in d:
d[x - 1] = 0
if x + 1 not in d:
d[x + 1] = 0
if x not in d:
d[x] = 0
t -= d[x - 1]
t += d[x + 1]
d[x] += 1
num += 1
sum += x
ans += t
print(ans) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR 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 IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER 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_int_list(string):
ls = []
sum = ""
for s in string:
if s == " ":
ls.append(int(sum))
sum = ""
else:
sum += s
ls.append(int(sum))
return ls
def extras(d, num):
plus = minus = 0
if num + 1 in d:
plus = d[num + 1]
if num - 1 in d:
minus = d[num - 1]
return plus - minus
def get_ans(ls, n):
d = {}
sum = 0
k = 0
for i in ls:
k += 1
if i in d:
d[i] += 1
else:
d[i] = 1
sum += k * i - (n + 1 - k) * i + extras(d, i)
return sum
n = int(input())
x = input()
ls = get_int_list(x)
print(get_ans(ls, n)) | FUNC_DEF ASSIGN VAR LIST ASSIGN VAR STRING FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR STRING VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR 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 RETURN BIN_OP VAR VAR FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL 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. | n = int(input())
a = list(map(int, input().split()))
cnt = {}
pref = 0
ans = 0
for i in range(n):
ans += i * a[i] - pref
t = 0
if a[i] - 1 in cnt:
ans += cnt[a[i] - 1] * (a[i] - 1)
t += cnt[a[i] - 1]
if a[i] + 1 in cnt:
ans += cnt[a[i] + 1] * (a[i] + 1)
t += cnt[a[i] + 1]
if a[i] in cnt:
ans += cnt[a[i]] * a[i]
t += cnt[a[i]]
ans -= a[i] * t
if a[i] in cnt:
cnt[a[i]] += 1
else:
cnt[a[i]] = 1
pref += 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 DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR IF 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. | n = int(input())
a = list(map(int, input().split()))
res = 0
cur = 0
d = {}
for j, i in enumerate(a):
if i - 1 in d:
res -= d[i - 1]
if i + 1 in d:
res += d[i + 1]
if i in d:
d[i] += 1
else:
d[i] = 1
res += i * j - cur
cur += i
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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR FUNC_CALL 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 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. | n = int(input())
nums = [int(i) for i in input().split()]
map = {nums[0]: 1}
result = 0
partial = nums[0]
for a in range(1, n):
if nums[a] in map.keys():
map[nums[a]] += 1
else:
map.update({nums[a]: 1})
result += a * nums[a] - partial
partial += nums[a]
new = 0
if nums[a] - 1 in map.keys():
new -= map[nums[a] - 1]
if nums[a] + 1 in map.keys():
new += map[nums[a] + 1]
result += new
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 VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR DICT VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER 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 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
mp = {}
n = int(sys.stdin.readline())
A = list(map(int, sys.stdin.readline().split()))
ans = 0
ele = 0
sel = 0
for x in A:
ans = ans + ele * x - sel
if x - 1 in mp:
ans = ans - mp[x - 1]
if x + 1 in mp:
ans = ans + mp[x + 1]
if x in mp:
mp[x] += 1
else:
mp[x] = 1
sel += x
ele += 1
print(ans) | IMPORT 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 ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR 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 IF VAR VAR VAR VAR NUMBER ASSIGN 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. | maxN = 2 * 10**5
n = int(input())
bad = 0
ghabl = 0
a = list(map(int, input().split()))
for i in range(n):
if i:
bad += a[i] - a[0]
Sum = bad
for i in range(1, n):
bad -= a[i] - a[i - 1]
bad -= (n - 1 - i) * (a[i] - a[i - 1])
Sum += bad
maP = dict()
maP["pedram"] = 1
for i in range(n):
if a[i] in maP:
maP[a[i]] += 1
else:
maP[a[i]] = 1
if a[i] - 1 in maP:
Sum -= maP[a[i] - 1]
if a[i] + 1 in maP:
Sum += maP[a[i] + 1]
print(Sum) | ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR 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 IF VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING NUMBER 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. | def list_input():
return list(map(int, input().split()))
def map_input():
return map(int, input().split())
def map_string():
return input().split()
ans = 0
d = {}
cnt = 0
n = int(input())
a = list_input()
for i in range(n):
tot = i
cur = cnt
if a[i] in d:
tot -= d[a[i]]
cur -= a[i] * d[a[i]]
if a[i] - 1 in d:
tot -= d[a[i] - 1]
cur -= d[a[i] - 1] * (a[i] - 1)
if a[i] + 1 in d:
tot -= d[a[i] + 1]
cur -= d[a[i] + 1] * (a[i] + 1)
ans += tot * a[i] - cur
cnt += a[i]
if a[i] not in d:
d[a[i]] = 1
else:
d[a[i]] += 1
print(ans) | FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR 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 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 VAR BIN_OP BIN_OP VAR VAR 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())
cnt = {}
ans = 0
allsum = 0
v = list(map(int, input().split()))
for i in range(n):
x = v[i]
ans += (i - cnt.get(x, 0) - cnt.get(x - 1, 0) - cnt.get(x + 1, 0)) * x - (
allsum
- cnt.get(x, 0) * x
- cnt.get(x - 1, 0) * (x - 1)
- cnt.get(x + 1, 0) * (x + 1)
)
cnt[x] = cnt.get(x, 0) + 1
allsum += x
print(ans) | 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 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 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 ASSIGN VAR VAR BIN_OP FUNC_CALL 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 = list(map(int, input().split()))
res, sum = 0, a[0]
cnt = dict()
cnt[a[0]] = 1
for i in range(1, n):
res += a[i] * i - sum
sum += a[i]
if a[i] - 1 in cnt:
res -= cnt[a[i] - 1]
if a[i] + 1 in cnt:
res += cnt[a[i] + 1]
if a[i] not in cnt:
cnt[a[i]] = 1
else:
cnt[a[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 VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR 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 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())
dat = input().split()
cnt = n
sums = 0
mep = {}
for num in dat:
num = int(num)
sums += num
try:
mep[num] += 1
except KeyError:
mep[num] = 1
ans = 0
for num in dat:
num = int(num)
ans += sums - cnt * num
if num + 1 in mep:
ans -= mep[num + 1]
if num - 1 in mep:
ans += mep[num - 1]
cnt -= 1
sums -= num
mep[num] -= 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP 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 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())
s = input().split()
a = [int(i) for i in s]
m1 = {}
m2 = {}
sum = 0
def get(m, a):
if a in m:
return m[a]
else:
return 0
ans = 0
for i in range(n):
ans += i * a[i] - sum + get(m1, a[i]) - get(m2, a[i])
if not a[i] - 1 in m1:
m1[a[i] - 1] = 0
if not a[i] + 1 in m2:
m2[a[i] + 1] = 0
m1[a[i] - 1] += 1
m2[a[i] + 1] += 1
sum += a[i]
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 DICT ASSIGN VAR DICT ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR RETURN VAR VAR RETURN 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 VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER 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(x) for x in input().split()]
s = a[n - 1]
ans = 0
D = dict()
D[a[n - 1]] = 1
for i in range(n - 2, -1, -1):
x = a[i]
c = 0
d = 0
if x - 1 in D:
c = D[x - 1]
if x + 1 in D:
d = D[x + 1]
ans += s - ((n - 1 - i) * x - c + d)
s += x
if x in D:
D[x] += 1
else:
D[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 VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER 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 VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR 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())
l = list(map(int, input().split()))
d = dict()
su = 0
ans = 0
for i in range(n):
if l[i] in d:
d[l[i]] += 1
else:
d.update({l[i]: 1})
c = 0
c1 = 0
j = l[i]
if j + 1 in d:
c = d[j + 1]
if j - 1 in d:
c1 = d[j - 1]
ans += i * l[i] - su + c - c1
su += l[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 ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR DICT VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR 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 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 almostDifference(nums):
ht = {}
result = 0
prefix_sum = 0
for i, n in enumerate(nums):
ht.setdefault(n, 0)
ht.setdefault(n - 1, 0)
ht.setdefault(n + 1, 0)
result -= prefix_sum
result += n * i
result -= ht[n - 1] * 1
result -= ht[n + 1] * -1
ht[n] += 1
prefix_sum += n
return result
_ = int(input())
nums = [int(x) for x in input().split()]
print(almostDifference(nums)) | FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR 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. | mp = dict()
n = int(input())
ans = 0
sum1 = 0
l = list(map(int, input().split()))
for i in range(0, n):
t = l[i]
if t - 1 not in mp:
mp[t - 1] = 0
if t + 1 not in mp:
mp[t + 1] = 0
if t not in mp:
mp[t] = 0
ans += i * t - sum1
ans -= mp[t - 1]
ans += mp[t + 1]
sum1 += t
mp[t] += 1
print(ans) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR 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 NUMBER VAR ASSIGN VAR VAR VAR 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 IF VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP 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())
a = list(map(int, input().split()))
bad = 0
for i in range(1, n):
bad += a[i] - a[0]
Sum = bad
for i in range(1, n):
bad -= (a[i] - a[i - 1]) * (n - i)
Sum += bad
maP = dict()
for i in range(n):
if a[i] in maP:
maP[a[i]] += 1
else:
maP[a[i]] = 1
for j in range(-1, 2, 2):
if a[i] + j in maP:
Sum += j * maP[a[i] + j]
print(Sum) | 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 NUMBER VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL 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 NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.