description
stringlengths 171
4k
| code
stringlengths 94
3.98k
| normalized_code
stringlengths 57
4.99k
|
|---|---|---|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
n = int(input())
ab = [(0, 0)] * n
for i in range(n):
ab[i] = tuple(map(int, input().split()))
ab[i] = ab[i][0], ab[i][1], ab[i][0] - ab[i][1]
ab = sorted(ab, key=lambda x: x[2], reverse=True)
sm = 0
for i in range(n):
sm += -ab[i][0] + n * ab[i][1] + (i + 1) * ab[i][2]
print(sm)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
from sys import setcheckinterval, setrecursionlimit, stdin
setcheckinterval(1000)
setrecursionlimit(10**6)
iin = lambda: int(stdin.readline())
lin = lambda: list(map(int, stdin.readline().split()))
n = iin()
a = [lin() for i in range(n)]
a1 = sorted(a, key=lambda x: x[1] - x[0])
ans = 0
for i in range(n):
ans += a1[i][0] * i + a1[i][1] * (n - i - 1)
print(ans)
|
EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
n = list(map(int, input().split()))[0]
la = list()
lb = list()
for i in range(0, n):
a, b = map(int, input().split())
la += [(abs(a - b), a, b, i)]
la.sort()
lb.sort(reverse=True)
sa = set()
left = 0
right = n - 1
sums = 0
while len(la) > 0:
g = la.pop()
if int(g[1]) > int(g[2]):
sums += int(g[1]) * left + int(g[2]) * (n - left - 1)
left += 1
else:
sums += int(g[1]) * right + int(g[2]) * (n - right - 1)
right -= 1
print(sums)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR LIST FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
def compute(s, n):
s_new = sorted(s, reverse=True)
ret = 0
for i in range(1, n + 1):
ret = ret + i * s_new[i - 1]
return ret
def main():
n = int(input())
s = []
b_sum = 0
a_sum = 0
for i in range(n):
a, b = input().split(" ")
a = int(a)
b = int(b)
a_sum = a + a_sum
b_sum = b + b_sum
s.append(a - b)
ret = compute(s, n)
print(ret + b_sum * n - a_sum)
main()
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
n = int(input())
p1 = 0
p2 = 0
l = []
s = 0
for k in range(1, n + 1):
x = str(input())
x = x.split(" ")
x = [int(y) for y in x if y != ""]
p1 = x[0] + p1
p2 = p2 + x[1]
l.append(x[0] - x[1])
l.sort(reverse=True)
for k in range(1, n + 1):
s = s + k * l[k - 1]
print(s + n * p2 - p1)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
n = int(input())
a = []
b = []
c = []
for i in range(n):
d = list(map(int, input().split()))
a.append(d[0])
b.append(d[1])
c.append(d[0] - d[1])
w = sum(a)
z = sum(b)
c.sort(reverse=True)
summ = 0
for i in range(len(c)):
summ += c[i] * (i + 1)
t = summ + n * z - w
print(t)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
n = int(input())
sb, sa = 0, 0
c = []
for i in range(n):
i, j = map(int, input().split())
sb += j
sa += i
c.append(i - j)
c.sort()
c = c[::-1]
ans = sb * n - sa
for i in range(n):
ans += c[i] * (i + 1)
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
n = int(input())
a = [0] * n
b = [0] * n
c = [0] * n
for i in range(0, n):
a[i], b[i] = map(int, input().split())
c[i] = b[i] - a[i]
c = sorted(c)
s = 0
for i in range(0, n):
s += c[i] * (n - 1 - i)
print(s + (n - 1) * sum(a))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
from sys import stdin
inp = stdin.readline
n, s = int(inp()), 0
arr = sorted(
[
[a[0] - a[1], a[0], a[1]]
for i in range(n)
for a in [[int(i) for i in inp().split()]]
]
)
for i in range(n):
s += arr[i][2] * i + arr[i][1] * (n - i - 1)
print(s)
|
ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR LIST FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
n = int(input())
ans = 0
suma = 0
sumb = 0
h = []
for i in range(1, n + 1):
a, b = map(int, input().split())
suma += a
sumb += b
h.append(a - b)
h.sort()
for i in range(n):
p = h.pop()
ans += p * (i + 1)
ans += -suma + sumb * n
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
n = int(input())
a = []
b = []
for _ in range(n):
x, y = map(int, input().split())
a.append(x)
b.append(y)
s = 0
s -= sum(a)
s += sum(b) * n
array = [(a[i] - b[i]) for i in range(n)]
array.sort(reverse=True)
for j in range(1, n + 1):
s += array[j - 1] * j
print(s)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
n = int(input())
a = []
s = 0
for _ in range(n):
t = list(map(int, input().split()))
s += t[1] * n - t[0]
a.append(t[0] - t[1])
a.sort()
for i in range(n):
s += a[i] * (n - i)
print(s)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
from sys import stdin
input = stdin.readline
n = int(input())
arr = [tuple(map(int, input().split())) for _ in range(n)]
arr = [(a, b, a - b) for a, b in arr]
arr.sort(key=lambda x: (x[2], x[0]), reverse=True)
cnt = 0
for i, t in enumerate(arr):
a, b, _ = t
cnt += i * a
cnt += (n - i - 1) * b
print(cnt)
|
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
import sys
input = sys.stdin.readline
n = int(input())
l = sorted(
[list(map(int, input().rstrip("\n").split(" "))) for i in range(n)],
key=lambda x: x[1] - x[0],
)
print(sum(l[i][0] * i + l[i][1] * (n - i - 1) for i in range(n)))
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING STRING VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
n = int(input())
l = []
a1 = 0
b1 = 0
for i in range(n):
a, b = map(int, input().split())
a1 = a1 + a
b1 = b1 + b
l.append(a - b)
l.sort(reverse=True)
s = 0
x = [i for i in range(1, n + 1)]
i = 0
while len(l) > 0:
if l[-1] <= 0:
s = s + l.pop() * x.pop()
else:
break
for i in range(len(l)):
s = s + l[i] * x[i]
print(n * b1 - a1 + s)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
from sys import stdin
input = stdin.readline
ans = 0
k = []
n = int(input())
for i in range(n):
a, b = map(int, input().split())
ans += b * n - a
k.append(a - b)
k.sort(reverse=True)
for i in range(n):
ans += k[i] * (i + 1)
print(ans)
|
ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
import sys
input = sys.stdin.readline
MOD = 1000000007
MOD2 = 998244353
ii = lambda: int(input().strip("\n"))
si = lambda: input().strip("\n")
dgl = lambda: list(map(int, input().strip("\n")))
f = lambda: map(int, input().strip("\n").split())
il = lambda: list(map(int, input().strip("\n").split()))
ls = lambda: list(input().strip("\n"))
let = "abcdefghijklmnopqrstuvwxyz"
for _ in range(1):
n = ii()
l = [il() for i in range(n)]
l.sort(reverse=True, key=lambda x: x[0] - x[1])
ans = 0
for i in range(n):
ans += l[i][0] * i + l[i][1] * (n - i - 1)
print(ans)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
def main():
n = int(input())
a = [(0) for i in range(n)]
b = [(0) for i in range(n)]
for i in range(n):
p, q = input().split()
a[i] = int(p)
b[i] = int(q)
q = []
for i in range(n):
q.append([a[i] - b[i], i])
q.sort(reverse=True)
val = 0
for i in range(n):
val += a[q[i][1]] * i + b[q[i][1]] * (n - i - 1)
print(val)
main()
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
n = int(input())
l1 = []
l2 = []
l3 = []
for i in range(n):
a, b = [int(i) for i in input().split()]
l1.append(a)
l2.append(b)
l3.append(a - b)
l3.sort()
l3.reverse()
min1 = 0
for i in range(n):
min1 += l3[i] * (i + 1) + l2[i] * n - l1[i]
print(min1)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
n = int(input())
vec = []
for i in range(n):
ab = list(map(int, input().split()))
ac = [ab[1] - ab[0]] + ab
vec.append(ac)
vec.sort()
res = 0
for i in range(n):
res += vec[i][1] * i + vec[i][2] * (n - 1 - i)
print(res)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
from sys import stdin, stdout
def sortOdd(val):
return val[0] - val[1]
n, dis = int(stdin.readline()), 0
a = [list(map(int, stdin.readline().split())) for i in range(n)]
a.sort(reverse=True, key=sortOdd)
for i in range(n):
dis += a[i][0] * i + a[i][1] * (n - 1 - i)
print(dis)
|
FUNC_DEF RETURN BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
n = int(input())
l = []
for i in range(n):
k = [int(x) for x in input().split()]
l.append([k[0], k[1], k[0] - k[1]])
l = sorted(l, key=lambda r: r[2], reverse=True)
s = 0
for i in range(n):
s += l[i][0] * i + l[i][1] * (n - 1 - i)
print(s)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
from sys import stdin
n = int(input())
ab = []
lines = stdin.readlines()
for line in lines:
a, b = map(int, line.split())
ab.append((a, b))
ab.sort(key=lambda x: max(x[0], x[1]) - min(x[0], x[1]), reverse=True)
l = 0
r = n - 1
ans = 0
for a, b in ab:
if a > b:
ans += a * l + b * (n - l - 1)
l += 1
else:
ans += a * r + b * (n - r - 1)
r -= 1
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
n = int(input())
v = [[int(x) for x in input().split()] for _ in range(n)]
ax_c = [(a - b, b * n - a) for a, b in v]
ax_c.sort(reverse=True)
s = sum(x[0] * (i + 1) + x[1] for i, x in enumerate(ax_c))
print(s)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
n = int(input())
a = []
b = []
ans = 0
for i in range(n):
t1, t2 = map(int, input().split())
a.append(t1)
b.append(t2)
ans += t2 * n - t1
c = [(a[i] - b[i]) for i in range(n)]
d = [i for i in range(n, 0, -1)]
c.sort()
for i in range(n):
ans += c[i] * d[i]
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
n = int(input())
a = []
for i in range(n):
b = list(map(int, input().split()))
a.append(b)
y = 0
x = 0
c = []
for i in a:
x += i[0]
y += i[1]
c.append(i[0] - i[1])
c.sort(reverse=True)
p = n * y - x
s = 0
for j in range(n):
s += c[j] * (j + 1)
print(s + p)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
def check(a, i, j, n):
return a[i][0] * (j - 1) + a[i][1] * (n - j)
n = int(input())
a = [0]
b = []
for i in range(1, n + 1):
a.append(list(map(int, input().split())))
b.append([a[-1][0] - a[-1][1], i])
b.sort(reverse=True)
ans = 0
for j in range(1, n + 1):
ans += check(a, b[j - 1][1], j, n)
print(ans)
|
FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
import sys
input = iter(sys.stdin.read().splitlines()).__next__
n = int(input())
A = [[int(x) for x in input().split()] for _ in range(n)]
L = []
R = []
cost = 0
for i in range(n):
mn = min(A[i])
cost += mn * (n - 1)
A[i][0] -= mn
A[i][1] -= mn
if A[i][0] == 0:
R.append(A[i][1])
elif A[i][1] == 0:
L.append(A[i][0])
cost += sum(i * e for i, e in enumerate(sorted(L, reverse=True)))
cost += sum(i * e for i, e in enumerate(sorted(R, reverse=True)))
print(cost)
|
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
n = int(input())
l = []
ans = 0
for _ in range(n):
x, y = map(int, input().split())
ans += y * n - x
l += [x - y]
l = sorted(l)
j = n
for each in l:
ans += each * j
j -= 1
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR LIST BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
from sys import stdin
input = stdin.readline
n = int(input())
l = []
for i in range(n):
a, b = map(int, input().split())
l.append([a - b, a, b])
l.sort(reverse=True)
x = 0
for i in range(n, 0, -1):
j, a, b = l[i - 1]
x += i * j - a + b * n
print(x)
|
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
n = int(input())
v = []
d = []
for _ in range(n):
v.append([int(item) for item in input().split()])
d.append(v[-1][0] - v[-1][1])
d.sort()
j = n
i = 0
ans = 0
while j >= 1:
ans += d[i] * j
i += 1
j -= 1
for i in range(n):
ans += n * v[i][1] - v[i][0]
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
import sys
sys.setrecursionlimit(10**5 + 1)
inf = int(10**20)
max_val = inf
min_val = -inf
RW = lambda: sys.stdin.readline().strip()
RI = lambda: int(RW())
RMI = lambda: [int(x) for x in sys.stdin.readline().strip().split()]
RWI = lambda: [x for x in sys.stdin.readline().strip().split()]
nb_students = RI()
line = []
for i in range(nb_students):
a, b = RMI()
line.append([b - a, a, b])
line.sort()
ans = 0
for i in range(nb_students):
ans += i * line[i][1] + (nb_students - i - 1) * line[i][2]
print(ans)
|
IMPORT EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
n = int(input())
pos = []
neg = []
res = 0
for i in range(n):
a, b = map(int, input().split())
if a > b:
pos.append([a, b])
elif a < b:
neg.append([a, b])
else:
res += (n - 1) * a
pos.sort(key=lambda l: -abs(l[0] - l[1]))
neg.sort(key=lambda l: -abs(l[0] - l[1]))
def f(i, l):
return l[0] * (i - 1) + l[1] * (n - i)
for i in range(len(pos)):
res += f(i + 1, pos[i])
for i in range(len(neg)):
res += f(n - i, neg[i])
print(res)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR IF VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF RETURN BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
n = int(input())
l = []
a = 0
b = 0
for i in range(n):
l += [list(map(int, input().split()))]
a += l[i][0]
b += l[i][1]
l1 = [(l[i][0] - l[i][1]) for i in range(n)]
l1.sort(reverse=True)
s = 0
for i in range(1, n + 1):
s += i * l1[i - 1]
s += n * b - a
print(s)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR LIST FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
n = int(input())
a = []
k = 0
for i in range(n):
h, m = list(map(int, input().split()))
a.append(h - m)
k += m
k *= n - 1
a.sort(reverse=True)
for i in range(n):
k += i * a[i]
print(k)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
n = int(input().strip())
l = []
def p(ll):
return ll[1] - ll[0]
for k in range(n):
l.append(tuple(map(int, input().strip().split())))
l.sort(key=p)
s1 = 0
s2 = n - 1
s = 0
for tt in range(n):
s = s + l[tt][0] * s1 + l[tt][1] * s2
s2 -= 1
s1 += 1
print(s)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_DEF RETURN BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
import sys
input = sys.stdin.readline
def ceil(x):
if x != int(x):
x = int(x) + 1
return x
def swaparr(arr, a, b):
temp = arr[a]
arr[a] = arr[b]
arr[b] = temp
def gcd(a, b):
if b == 0:
return a
return gcd(b, a % b)
def nCr(n, k):
if k > n - k:
k = n - k
res = 1
for i in range(k):
res = res * (n - i)
res = res / (i + 1)
return int(res)
def upper_bound(a, x, lo=0, hi=None):
if hi == None:
hi = len(a)
while lo < hi:
mid = (lo + hi) // 2
if a[mid] < x:
lo = mid + 1
else:
hi = mid
return lo
def primefs(n):
primes = {}
while n % 2 == 0 and n > 0:
primes[2] = primes.get(2, 0) + 1
n = n // 2
for i in range(3, int(n**0.5) + 2, 2):
while n % i == 0 and n > 0:
primes[i] = primes.get(i, 0) + 1
n = n // i
if n > 2:
primes[n] = primes.get(n, 0) + 1
return primes
def power(x, y, p):
res = 1
x = x % p
if x == 0:
return 0
while y > 0:
if y & 1 == 1:
res = res * x % p
y = y >> 1
x = x * x % p
return res
def swap(a, b):
temp = a
a = b
b = temp
return a, b
def find(x, link):
p = x
while p != link[p]:
p = link[p]
while x != p:
nex = link[x]
link[x] = p
x = nex
return p
def union(x, y, link, size):
x = find(x, link)
y = find(y, link)
if size[x] < size[y]:
x, y = swap(x, y)
if x != y:
size[x] += size[y]
link[y] = x
def sieve(n):
prime = [(True) for i in range(n + 1)]
p = 2
while p * p <= n:
if prime[p] == True:
for i in range(p * p, n + 1, p):
prime[i] = False
p += 1
return prime
MAXN = int(100000.0 + 5)
def spf_sieve():
spf[1] = 1
for i in range(2, MAXN):
spf[i] = i
for i in range(4, MAXN, 2):
spf[i] = 2
for i in range(3, ceil(MAXN**0.5), 2):
if spf[i] == i:
for j in range(i * i, MAXN, i):
if spf[j] == j:
spf[j] = i
def factoriazation(x):
ret = {}
while x != 1:
ret[spf[x]] = ret.get(spf[x], 0) + 1
x = x // spf[x]
return ret
def int_array():
return list(map(int, input().strip().split()))
def float_array():
return list(map(float, input().strip().split()))
def str_array():
return input().strip().split()
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float("inf")
NINF = -float("inf")
n = int(input())
a = []
ans = 0
for _ in range(n):
x, y = int_array()
ans += y * n - x
a.append(x - y)
a.sort(reverse=1)
for x, i in enumerate(a, 1):
ans += i * x
print(ans)
|
IMPORT ASSIGN VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_DEF IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_DEF NUMBER NONE IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR DICT WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER RETURN NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR WHILE VAR VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF ASSIGN VAR DICT WHILE VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
import sys
input = sys.stdin.readline
n = int(input())
a = [None] * n
b = [None] * n
res = 0
for i in range(n):
a[i] = list(map(int, input().split()))
b[i] = a[i][0] - a[i][1]
rank = [index for index, value in sorted(list(enumerate(b)), key=lambda x: -x[1])]
for i, index in enumerate(rank):
res += a[index][0] * i + a[index][1] * (n - i - 1)
print(res)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
class sol:
def __init__(self, a, b):
self.a = a
self.b = b
self.sub = a - b
n = int(input())
arr = []
for i in range(n):
a, b = map(int, input().split())
arr.append(sol(a, b))
arr = sorted(arr, key=lambda x: x.sub, reverse=True)
val = 0
for i in range(n):
dis = arr[i].a * i + arr[i].b * (n - (i + 1))
val += dis
print(val)
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
n = int(input())
l = []
m = []
su = 0
for i in range(n):
a, b = list(map(int, input().split()))
l.append((b - a, i))
m.append((a, b))
l.sort(key=lambda x: x[0])
print(sum([(m[l[i][1]][0] * i + m[l[i][1]][1] * (n - 1 - i)) for i in range(n)]))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
n = int(input())
a = [tuple(map(int, input().split())) for _ in range(n)]
a.sort(key=lambda x: x[0] - x[1], reverse=True)
ret = 0
for i in range(1, n + 1):
ret += a[i - 1][0] * (i - 1) + a[i - 1][1] * (n - i)
print(ret)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
N = int(input())
items = []
for i in range(N):
items.append(tuple(map(int, input().split())))
m = {}
for i, item in enumerate(items):
a, b = item
m[i] = a - b
m = sorted(m.items(), key=lambda x: -x[1])
ans = 0
i = 0
for k, _ in m:
a, b = items[k]
ans += a * i + b * (N - i - 1)
i += 1
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
n, arr, ans = int(input()), list(), 0
for i in range(n):
[a, b] = [int(i) for i in input().split()]
arr += [[b - a, a, b]]
arr = sorted(arr)
for i in range(n):
ans += i * arr[i][1] + (n - i - 1) * arr[i][2]
print(ans)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN LIST VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR LIST LIST BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
n = int(input())
data = [list(map(int, input().split())) for _ in range(n)]
data.sort(key=lambda x: x[0] - x[1])
print(sum(v[1] * i + v[0] * (n - i - 1) for i, v in enumerate(data)))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
n = int(input())
suma = 0
dif = []
for i in range(n):
a, b = map(int, input().split())
suma += b * (n - 1)
dif.append(a - b)
dif = sorted(dif, reverse=True)
for j in range(n):
suma += j * dif[j]
print(suma)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
def solve(a):
return a[0] - a[1]
n = int(input())
arr = []
for i in range(n):
lista = list(map(int, input().split()))
arr.append(lista)
ans = 0
for i in range(n):
ans += arr[i][1] * n - arr[i][0]
arr.sort(reverse=True, key=solve)
for i in range(n):
ans += (arr[i][0] - arr[i][1]) * (i + 1)
print(ans)
|
FUNC_DEF RETURN BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
def main():
n = int(input())
arr = []
for i in range(n):
x, y = map(int, input().split())
arr.append((x, y))
ans = 0
arr.sort(key=lambda a: a[1] - a[0])
for i in range(n):
x, y = arr[i]
ans += x * i + (n - 1 - i) * y
print(ans)
return 0
main()
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
import sys
def input():
return sys.stdin.readline()
n = int(input())
arr = []
for i in range(n):
a, b = map(int, input().split())
arr.append((a, b))
arr.sort(key=lambda x: x[1] - x[0])
answer = 0
for i in range(n):
a, b = arr[i]
answer += a * i + b * (n - i - 1)
print(answer)
|
IMPORT FUNC_DEF RETURN FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
n = int(input())
people = []
for i in range(n):
people.append(tuple(int(x) for x in input().split()))
diss = sum(n * p[1] - p[0] for p in people)
a_b = list([(p[0] - p[1]) for p in people])
a_b.sort(reverse=True)
diss += sum((idx + 1) * val for idx, val in enumerate(a_b))
print(diss)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
n = int(input())
m = []
for i in range(n):
a, b = map(int, str.split(input(), " "))
m.append((a - b, a, b))
m.sort(reverse=True)
score = 0
for i in range(n):
_, a, b = m[i]
score += (i + 1) * (a - b) + b * n - a
print(score)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
import sys
input = sys.stdin.readline
N = int(input())
X = []
for _ in range(N):
a, b = map(int, input().split())
c = b - a
X.append([a, b, c])
X = sorted(X, key=lambda x: x[2])
s = 0
for i in range(N):
s += X[i][0] * i + X[i][1] * (N - i - 1)
print(s)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
n = int(input())
x = []
y = []
d = []
for _ in range(n):
xi, yi = list(map(int, input().strip().split()))
x.append(xi)
y.append(yi)
d.append(xi - yi)
constant_sum_x = -sum(x)
constant_sum_y = n * sum(y)
d.sort()
sm = 0
for i in range(n):
sm += d[i] * (n - i)
print(constant_sum_x + constant_sum_y + sm)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
n = int(input())
li = []
li_p = []
for i in range(n):
ly = input().split()
ly.append(int(ly[0]) - int(ly[1]))
li.append(ly)
li.sort(key=lambda x: x[2], reverse=True)
cost = 0
for i in range(n):
cost += int(li[i][0]) * i + int(li[i][1]) * (n - i - 1)
print(cost)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \cdot (j-1) + b_i \cdot (n-j)$.
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of people in the queue.
Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.
-----Output-----
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
-----Examples-----
Input
3
4 2
2 3
6 1
Output
12
Input
4
2 4
3 3
7 1
2 3
Output
25
Input
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
Output
1423
-----Note-----
In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.
In the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$.
|
def mp():
return map(int, input().split())
n = int(input())
a = [0] * n
b = [0] * n
for i in range(n):
a[i], b[i] = mp()
q = [0] * n
for i in range(n):
q[i] = [b[i] - a[i], i]
q.sort()
e = 0
for i in range(n):
j = q[i][1]
e += a[j] * i + b[j] * (n - i - 1)
print(e)
|
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers.
Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$.
Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$.
Dark should choose an integer $k$ so that $m$ is minimized. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$.
-----Output-----
Print the answers for each test case in the following format:
You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$.
Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$.
If there is more than one possible $k$, you can print any of them.
-----Example-----
Input
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5
Output
1 11
5 35
3 6
0 42
0 0
1 2
3 4
-----Note-----
In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$.
In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$.
So, the maximum difference between any adjacent elements is $3$.
|
import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n = int(input())
a = [int(item) for item in input().split()]
max_val = 0
min_val = 10**12
max_diff = 0
for a1, a2 in zip(a, a[1:]):
if a1 == -1:
if a2 == -1:
continue
else:
max_val = max(max_val, a2)
min_val = min(min_val, a2)
elif a2 == -1:
max_val = max(max_val, a1)
min_val = min(min_val, a1)
else:
max_diff = max(max_diff, abs(a1 - a2))
k = (max_val + min_val) // 2
if min_val == 10**12:
k = 0
m = max(max_diff, max_val - k, k - min_val)
print(m, k)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR 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 BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers.
Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$.
Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$.
Dark should choose an integer $k$ so that $m$ is minimized. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$.
-----Output-----
Print the answers for each test case in the following format:
You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$.
Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$.
If there is more than one possible $k$, you can print any of them.
-----Example-----
Input
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5
Output
1 11
5 35
3 6
0 42
0 0
1 2
3 4
-----Note-----
In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$.
In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$.
So, the maximum difference between any adjacent elements is $3$.
|
for _ in range(int(input())):
n = int(input())
L = [int(x) for x in input().strip().split(" ")]
mini, maxi = float("inf"), 0
for i in range(n):
if L[i] == -1:
if i > 0 and L[i - 1] != -1:
mini = min(mini, L[i - 1])
maxi = max(maxi, L[i - 1])
if i < n - 1 and L[i + 1] != -1:
mini = min(mini, L[i + 1])
maxi = max(maxi, L[i + 1])
if mini == float("inf"):
mini = 0
k = (maxi + mini) // 2
L = [(k if x == -1 else x) for x in L]
m = 0
for i in range(1, n):
m = max(m, abs(L[i] - L[i - 1]))
print(m, k)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR STRING NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
|
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers.
Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$.
Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$.
Dark should choose an integer $k$ so that $m$ is minimized. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$.
-----Output-----
Print the answers for each test case in the following format:
You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$.
Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$.
If there is more than one possible $k$, you can print any of them.
-----Example-----
Input
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5
Output
1 11
5 35
3 6
0 42
0 0
1 2
3 4
-----Note-----
In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$.
In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$.
So, the maximum difference between any adjacent elements is $3$.
|
num_t = int(input())
for _ in range(num_t):
input()
arr = [int(i) for i in input().split()]
min_p = float("inf")
max_p = float("-inf")
found = False
for i in range(len(arr)):
if arr[i] == -1 and i - 1 >= 0 and arr[i - 1] != -1:
min_p = min(arr[i - 1], min_p)
max_p = max(arr[i - 1], max_p)
found = True
if arr[i] == -1 and i + 1 < len(arr) and arr[i + 1] != -1:
min_p = min(arr[i + 1], min_p)
max_p = max(arr[i + 1], max_p)
found = True
if not found:
print(0, 0)
continue
k = (max_p + min_p) // 2
m = float("-inf")
for i in range(1, len(arr)):
if arr[i] != -1 and arr[i - 1] != -1:
m = max(abs(arr[i] - arr[i - 1]), m)
print(max(m, max_p - k, k - min_p), k)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR
|
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers.
Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$.
Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$.
Dark should choose an integer $k$ so that $m$ is minimized. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$.
-----Output-----
Print the answers for each test case in the following format:
You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$.
Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$.
If there is more than one possible $k$, you can print any of them.
-----Example-----
Input
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5
Output
1 11
5 35
3 6
0 42
0 0
1 2
3 4
-----Note-----
In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$.
In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$.
So, the maximum difference between any adjacent elements is $3$.
|
t = int(input())
for i in range(t):
n = int(input())
a = list(map(int, input().split()))
mi = float("INF")
ma = -float("INF")
value = 0
for i in range(n):
if i > 0 and a[i] == -1 and a[i - 1] != -1:
mi = min(a[i - 1], mi)
ma = max(ma, a[i - 1])
if i < n - 1 and a[i] == -1 and a[i + 1] != -1:
mi = min(a[i + 1], mi)
ma = max(ma, a[i + 1])
value = (mi + ma) // 2
if a.count(-1) == len(a):
value = 0
m = 0
for i, item in enumerate(a):
if a[i] == -1:
a[i] = value
if i:
m = max(m, abs(a[i] - a[i - 1]))
print(m, value)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR 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 STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
|
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers.
Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$.
Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$.
Dark should choose an integer $k$ so that $m$ is minimized. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$.
-----Output-----
Print the answers for each test case in the following format:
You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$.
Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$.
If there is more than one possible $k$, you can print any of them.
-----Example-----
Input
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5
Output
1 11
5 35
3 6
0 42
0 0
1 2
3 4
-----Note-----
In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$.
In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$.
So, the maximum difference between any adjacent elements is $3$.
|
t = int(input())
for z in range(t):
n = int(input())
a = list(map(int, input().split()))
ma = 0
mi = 10**9
maM = 0
i = 0
j = 1
f = 0
while j < n:
if a[j] == -1 and a[i] != -1:
f = 1
if a[i] > ma:
ma = a[i]
if a[i] < mi:
mi = a[i]
if a[i] == -1 and a[j] != -1:
f = 1
if a[j] > ma:
ma = a[j]
if a[j] < mi:
mi = a[j]
if a[i] != -1 and a[j] != -1:
if abs(a[i] - a[j]) > maM:
maM = abs(a[i] - a[j])
i += 1
j += 1
k = (ma + mi) // 2
m1 = max(abs(ma - k), abs(mi - k))
m = max(m1, maM)
if f == 0 and maM == 0:
print(0, k)
else:
print(m, k)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR 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 NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR
|
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers.
Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$.
Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$.
Dark should choose an integer $k$ so that $m$ is minimized. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$.
-----Output-----
Print the answers for each test case in the following format:
You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$.
Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$.
If there is more than one possible $k$, you can print any of them.
-----Example-----
Input
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5
Output
1 11
5 35
3 6
0 42
0 0
1 2
3 4
-----Note-----
In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$.
In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$.
So, the maximum difference between any adjacent elements is $3$.
|
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
ns = []
for i in range(len(a)):
if a[i] == -1:
if i > 0 and a[i - 1] != -1:
ns.append(a[i - 1])
if i < n - 1 and a[i + 1] != -1:
ns.append(a[i + 1])
if len(ns) == 0:
mini = 1
maxi = 1
else:
mini = min(ns)
maxi = max(ns)
k = (mini + maxi) // 2
m = 0
for i in range(1, len(a)):
if a[i - 1] == -1:
a[i - 1] = k
if a[i] == -1:
a[i] = k
m = max(m, abs(a[i] - a[i - 1]))
print(m, k)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR 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 LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
|
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers.
Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$.
Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$.
Dark should choose an integer $k$ so that $m$ is minimized. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$.
-----Output-----
Print the answers for each test case in the following format:
You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$.
Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$.
If there is more than one possible $k$, you can print any of them.
-----Example-----
Input
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5
Output
1 11
5 35
3 6
0 42
0 0
1 2
3 4
-----Note-----
In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$.
In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$.
So, the maximum difference between any adjacent elements is $3$.
|
t = int(input())
for i in range(t):
n = int(input())
a = list(map(int, input().split()))
l = []
for j in range(len(a)):
if j == 0:
if a[j + 1] == -1 and a[j] != -1:
l.append(a[j])
elif j == len(a) - 1:
if a[j - 1] == -1 and a[j] != -1:
l.append(a[j])
elif (a[j - 1] == -1 or a[j + 1] == -1) and a[j] != -1:
l.append(a[j])
if len(l) == 0:
k = 0
else:
k = (max(l) + min(l)) // 2
for j in range(len(a)):
if a[j] == -1:
a[j] = k
m = 0
for j in range(len(a) - 1):
if abs(a[j] - a[j + 1]) > m:
m = abs(a[j] - a[j + 1])
print(m, k)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR 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 LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
|
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers.
Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$.
Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$.
Dark should choose an integer $k$ so that $m$ is minimized. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$.
-----Output-----
Print the answers for each test case in the following format:
You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$.
Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$.
If there is more than one possible $k$, you can print any of them.
-----Example-----
Input
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5
Output
1 11
5 35
3 6
0 42
0 0
1 2
3 4
-----Note-----
In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$.
In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$.
So, the maximum difference between any adjacent elements is $3$.
|
tc = int(input())
for T in range(tc):
n = int(input())
arr = list(map(int, input().split()))
temp = []
for i in range(n):
if arr[i] != -1:
if i == 0:
if arr[i + 1] == -1:
temp.append(arr[i])
elif i == n - 1:
if arr[i - 1] == -1:
temp.append(arr[i])
elif arr[i + 1] == -1 or arr[i - 1] == -1:
temp.append(arr[i])
siz = len(temp)
if siz >= 2:
temp.sort()
snd = (temp[0] + temp[siz - 1]) // 2
elif siz == 0:
snd = 0
else:
snd = temp[0]
for i in range(n):
if arr[i] == -1:
arr[i] = snd
fst = -1
for i in range(n - 1):
dif = abs(arr[i] - arr[i + 1])
fst = max(fst, dif)
print(fst, snd)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR 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 LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers.
Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$.
Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$.
Dark should choose an integer $k$ so that $m$ is minimized. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$.
-----Output-----
Print the answers for each test case in the following format:
You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$.
Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$.
If there is more than one possible $k$, you can print any of them.
-----Example-----
Input
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5
Output
1 11
5 35
3 6
0 42
0 0
1 2
3 4
-----Note-----
In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$.
In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$.
So, the maximum difference between any adjacent elements is $3$.
|
t = int(input())
inf, minf = 10**18, -(10**18)
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
min_n, max_n = inf, minf
ans = 0
for i in range(n):
if a[i] == -1:
if i + 1 < n and a[i + 1] > -1:
min_n = min(min_n, a[i + 1])
max_n = max(max_n, a[i + 1])
if 0 <= i - 1 and a[i - 1] > -1:
min_n = min(min_n, a[i - 1])
max_n = max(max_n, a[i - 1])
elif i + 1 < n and a[i + 1] != -1:
ans = max(ans, abs(a[i] - a[i + 1]))
if max_n == minf:
print(0, 0)
else:
x = (max_n + min_n) // 2
print(max(max_n - x, x - min_n, ans), x)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP NUMBER NUMBER BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR 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 VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR
|
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers.
Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$.
Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$.
Dark should choose an integer $k$ so that $m$ is minimized. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$.
-----Output-----
Print the answers for each test case in the following format:
You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$.
Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$.
If there is more than one possible $k$, you can print any of them.
-----Example-----
Input
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5
Output
1 11
5 35
3 6
0 42
0 0
1 2
3 4
-----Note-----
In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$.
In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$.
So, the maximum difference between any adjacent elements is $3$.
|
t = input()
t = int(t)
while t:
t -= 1
n = input()
a = input()
a = [int(str) for str in a.split()]
minn = 10000000000.0
dist = -1
maxx = -1
for i in range(len(a)):
cur = a[i]
if cur == -1:
if i == 0:
if a[i + 1] != -1:
minn = min(a[i + 1], minn)
maxx = max(a[i + 1], maxx)
elif i == len(a) - 1:
if a[i - 1] != -1:
minn = min(a[i - 1], minn)
maxx = max(a[i - 1], maxx)
else:
if a[i + 1] != -1:
minn = min(a[i + 1], minn)
maxx = max(a[i + 1], maxx)
if a[i - 1] != -1:
minn = min(a[i - 1], minn)
maxx = max(a[i - 1], maxx)
elif i != 0 and a[i - 1] != -1:
dist = max(dist, abs(a[i] - a[i - 1]))
if maxx == -1:
maxx = minn = 0
k = (maxx + minn) // 2
dist = max(dist, abs(k - maxx))
print(dist, k)
|
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers.
Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$.
Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$.
Dark should choose an integer $k$ so that $m$ is minimized. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$.
-----Output-----
Print the answers for each test case in the following format:
You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$.
Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$.
If there is more than one possible $k$, you can print any of them.
-----Example-----
Input
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5
Output
1 11
5 35
3 6
0 42
0 0
1 2
3 4
-----Note-----
In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$.
In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$.
So, the maximum difference between any adjacent elements is $3$.
|
t = int(input())
while t != 0:
n = int(input())
list1 = list(map(int, input().split()))
u = []
for i in range(len(list1)):
if list1[i] != -1:
if i > 0 and list1[i - 1] == -1 or i < n - 1 and list1[i + 1] == -1:
u.append(list1[i])
p, q = 0, 0
if len(u) > 0:
p = max(u)
q = min(u)
else:
p = 0
q = 0
ans = (p + q) // 2
maxi = 0
if list1[0] == -1:
list1[0] = ans
for i in range(1, len(list1)):
if list1[i] == -1:
list1[i] = ans
maxi = max(maxi, abs(list1[i] - list1[i - 1]))
else:
maxi = max(maxi, abs(list1[i] - list1[i - 1]))
print(maxi, ans)
t -= 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER
|
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers.
Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$.
Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$.
Dark should choose an integer $k$ so that $m$ is minimized. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$.
-----Output-----
Print the answers for each test case in the following format:
You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$.
Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$.
If there is more than one possible $k$, you can print any of them.
-----Example-----
Input
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5
Output
1 11
5 35
3 6
0 42
0 0
1 2
3 4
-----Note-----
In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$.
In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$.
So, the maximum difference between any adjacent elements is $3$.
|
t = int(input())
for s in range(t):
n = int(input())
a = list(map(int, input().split()))
i = 1
maxi, maxu, mini, d = 0, 0, 999999999, 0
while i < n:
if a[i] != -1 and a[i - 1] != -1:
d = a[i] - a[i - 1]
if d < 0:
d = d * -1
maxi = max(maxi, d)
else:
maxu = max(maxu, a[i], a[i - 1])
if a[i] != -1:
mini = min(mini, a[i])
elif a[i - 1] != -1:
mini = min(mini, a[i - 1])
i += 1
if mini == 999999999:
mini = 0
dif = maxu - mini
if dif > 2 * maxi:
print(dif - dif // 2, end=" ")
print((maxu + mini) // 2)
else:
print(maxi, end=" ")
print((maxu + mini) // 2)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR 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 NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER
|
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers.
Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$.
Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$.
Dark should choose an integer $k$ so that $m$ is minimized. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$.
-----Output-----
Print the answers for each test case in the following format:
You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$.
Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$.
If there is more than one possible $k$, you can print any of them.
-----Example-----
Input
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5
Output
1 11
5 35
3 6
0 42
0 0
1 2
3 4
-----Note-----
In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$.
In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$.
So, the maximum difference between any adjacent elements is $3$.
|
t = int(input())
for a in range(t):
menor = 1000000000
maior = 0
max_diff = 0
tam = int(input())
valores = [int(c) for c in input().split()]
for i in range(tam):
if valores[i] == -1:
if i > 0:
if valores[i - 1] != -1:
menor = min(valores[i - 1], menor)
maior = max(valores[i - 1], maior)
if i < tam - 1:
if valores[i + 1] != -1:
menor = min(valores[i + 1], menor)
maior = max(valores[i + 1], maior)
media = (maior + menor) // 2
for i in range(tam):
if valores[i] == -1:
valores[i] = media
for i in range(0, tam - 1):
max_diff = max(max_diff, abs(valores[i + 1] - valores[i]))
print(max_diff, media)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers.
Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$.
Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$.
Dark should choose an integer $k$ so that $m$ is minimized. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$.
-----Output-----
Print the answers for each test case in the following format:
You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$.
Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$.
If there is more than one possible $k$, you can print any of them.
-----Example-----
Input
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5
Output
1 11
5 35
3 6
0 42
0 0
1 2
3 4
-----Note-----
In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$.
In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$.
So, the maximum difference between any adjacent elements is $3$.
|
t = int(input())
for i in range(t):
n = int(input())
m = int(1000000000.0) + 1
M = -1
max_dif = 0
a = list(map(int, input().split()))
for j in range(len(a)):
if a[j] == -1:
if j + 1 < len(a):
if a[j + 1] < m and a[j + 1] != -1:
m = a[j + 1]
if a[j + 1] > M:
M = a[j + 1]
if j - 1 >= 0:
if a[j - 1] < m and a[j - 1] != -1:
m = a[j - 1]
if a[j - 1] > M:
M = a[j - 1]
else:
if j + 1 < len(a):
if abs(a[j] - a[j + 1]) > max_dif and a[j + 1] != -1:
max_dif = abs(a[j] - a[j + 1])
if j - 1 >= 0:
if abs(a[j] - a[j - 1]) > max_dif and a[j - 1] != -1:
max_dif = abs(a[j] - a[j - 1])
if m == int(1000000000.0) + 1 and M == -1:
print("0 0")
else:
l = max((M - m) // 2 + (M - m) % 2, max_dif)
k = m + (M - m) // 2
print(l, k)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER 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 FUNC_CALL VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
|
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers.
Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$.
Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$.
Dark should choose an integer $k$ so that $m$ is minimized. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$.
-----Output-----
Print the answers for each test case in the following format:
You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$.
Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$.
If there is more than one possible $k$, you can print any of them.
-----Example-----
Input
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5
Output
1 11
5 35
3 6
0 42
0 0
1 2
3 4
-----Note-----
In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$.
In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$.
So, the maximum difference between any adjacent elements is $3$.
|
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
c = []
for i in range(1, n - 1):
if a[i] == -1:
if a[i - 1] != -1:
c.append(a[i - 1])
if a[i + 1] != -1:
c.append(a[i + 1])
if a[-1] == -1 and a[-2] != -1:
c.append(a[-2])
if a[0] == -1 and a[1] != -1:
c.append(a[1])
if len(c) == 0:
print(0, 1)
continue
else:
d = (max(c) + min(c)) // 2
b = []
for i in range(n):
if a[i] == -1:
a[i] = d
for i in range(n - 1):
b.append(abs(a[i + 1] - a[i]))
print(max(b), d)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR 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 LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers.
Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$.
Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$.
Dark should choose an integer $k$ so that $m$ is minimized. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$.
-----Output-----
Print the answers for each test case in the following format:
You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$.
Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$.
If there is more than one possible $k$, you can print any of them.
-----Example-----
Input
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5
Output
1 11
5 35
3 6
0 42
0 0
1 2
3 4
-----Note-----
In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$.
In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$.
So, the maximum difference between any adjacent elements is $3$.
|
for t in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
if a.count(-1) == n:
print(0, 0)
else:
arr = []
for i in range(1, n):
if a[i] != -1 and a[i - 1] == -1:
arr.append(a[i])
elif a[i] == -1 and a[i - 1] != -1:
arr.append(a[i - 1])
avg = (max(arr) + min(arr)) // 2
for i in range(n):
if a[i] == -1:
a[i] = avg
maxi = abs(a[1] - a[0])
for i in range(2, n):
diff = abs(a[i] - a[i - 1])
if diff > maxi:
maxi = diff
print(maxi, avg)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers.
Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$.
Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$.
Dark should choose an integer $k$ so that $m$ is minimized. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$.
-----Output-----
Print the answers for each test case in the following format:
You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$.
Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$.
If there is more than one possible $k$, you can print any of them.
-----Example-----
Input
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5
Output
1 11
5 35
3 6
0 42
0 0
1 2
3 4
-----Note-----
In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$.
In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$.
So, the maximum difference between any adjacent elements is $3$.
|
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
deal = {}
for i in range(n):
if i < n - 1 and a[i] == -1 and a[i + 1] != -1:
deal[a[i + 1]] = True
if i > 0 and a[i] == -1 and a[i - 1] != -1:
deal[a[i - 1]] = True
dealers = deal.keys()
if len(dealers) > 0:
mine, maxe = min(dealers), max(dealers)
k = (mine + maxe) // 2
m = max(abs(mine - k), abs(maxe - k))
a = list(map(lambda x: x if x != -1 else k, a))
for i in range(n - 1):
m = max(m, abs(a[i] - a[i + 1]))
print(m, k)
else:
print(0, 0)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER
|
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers.
Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$.
Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$.
Dark should choose an integer $k$ so that $m$ is minimized. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$.
-----Output-----
Print the answers for each test case in the following format:
You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$.
Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$.
If there is more than one possible $k$, you can print any of them.
-----Example-----
Input
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5
Output
1 11
5 35
3 6
0 42
0 0
1 2
3 4
-----Note-----
In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$.
In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$.
So, the maximum difference between any adjacent elements is $3$.
|
for t in range(int(input())):
n = int(input())
l = [int(j) for j in input().split()]
ls = []
for i in range(n):
if l[i] == -1:
nb = [max(i - 1, 0), min(i + 1, n - 1)]
for j in nb:
if l[j] != -1:
ls.append(l[j])
lf = 1
k = 0
try:
mx = max(ls)
mn = min(ls)
k = (mx + mn) // 2
except:
pass
ans = 0
if l[0] == -1:
l[0] = k
for i in range(1, n):
if l[i] == -1:
l[i] = k
ans = max(ans, abs(l[i] - l[i - 1]))
print(ans, k)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
|
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers.
Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$.
Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$.
Dark should choose an integer $k$ so that $m$ is minimized. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$.
-----Output-----
Print the answers for each test case in the following format:
You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$.
Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$.
If there is more than one possible $k$, you can print any of them.
-----Example-----
Input
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5
Output
1 11
5 35
3 6
0 42
0 0
1 2
3 4
-----Note-----
In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$.
In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$.
So, the maximum difference between any adjacent elements is $3$.
|
t = int(input())
while t:
t -= 1
n = int(input())
a = list(map(int, input().split()))
num = []
for i in range(n):
if a[i] != -1:
if i:
if a[i - 1] == -1:
num.append(a[i])
if i != n - 1:
if a[i + 1] == -1:
num.append(a[i])
if num:
up = max(num)
down = min(num)
ans = (up + down) // 2
else:
ans = 0
dif = 0
for i in range(n):
if a[i] == -1:
a[i] = ans
if i:
dif = max(dif, abs(a[i] - a[i - 1]))
print(dif, ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
|
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers.
Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$.
Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$.
Dark should choose an integer $k$ so that $m$ is minimized. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$.
-----Output-----
Print the answers for each test case in the following format:
You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$.
Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$.
If there is more than one possible $k$, you can print any of them.
-----Example-----
Input
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5
Output
1 11
5 35
3 6
0 42
0 0
1 2
3 4
-----Note-----
In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$.
In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$.
So, the maximum difference between any adjacent elements is $3$.
|
a = int(input())
for i in range(a):
r = input()
z = list(map(int, input().split()))
t = []
for i in range(len(z)):
if z[i] == -1:
if i > 0:
if z[i - 1] != -1:
t.append(z[i - 1])
if i < len(z) - 1:
if z[i + 1] != -1:
t.append(z[i + 1])
if len(t) == 0:
k = 0
else:
k = (max(t) + min(t)) // 2
for i in range(len(z)):
if z[i] == -1:
z[i] = k
mini = 0
for i in range(1, len(z)):
if abs(z[i] - z[i - 1]) > mini:
mini = abs(z[i] - z[i - 1])
print(mini, k)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
|
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers.
Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$.
Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$.
Dark should choose an integer $k$ so that $m$ is minimized. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$.
-----Output-----
Print the answers for each test case in the following format:
You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$.
Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$.
If there is more than one possible $k$, you can print any of them.
-----Example-----
Input
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5
Output
1 11
5 35
3 6
0 42
0 0
1 2
3 4
-----Note-----
In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$.
In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$.
So, the maximum difference between any adjacent elements is $3$.
|
t = int(input())
for i in range(t):
n = int(input())
data = list(map(int, input().strip().split()))
arr = []
for j in range(len(data)):
if data[j] != -1 and (
j - 1 >= 0 and data[j - 1] == -1 or j + 1 < len(data) and data[j + 1] == -1
):
arr.append(data[j])
arr.sort()
if len(arr) == 0:
x = 0
else:
x = (arr[-1] + arr[0]) // 2
ans = 0
for j in range(len(data)):
if data[j] == -1:
data[j] = x
if j >= 1:
ans = max(abs(data[j] - data[j - 1]), ans)
print("%d %d" % (ans, x))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR
|
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers.
Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$.
Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$.
Dark should choose an integer $k$ so that $m$ is minimized. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$.
-----Output-----
Print the answers for each test case in the following format:
You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$.
Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$.
If there is more than one possible $k$, you can print any of them.
-----Example-----
Input
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5
Output
1 11
5 35
3 6
0 42
0 0
1 2
3 4
-----Note-----
In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$.
In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$.
So, the maximum difference between any adjacent elements is $3$.
|
I = input
for _ in [0] * int(I()):
I()
a = (*map(int, I().split()),)
b = [i for i, x in enumerate(a) if x < 0]
c = [a[i + 1 : j] for i, j in zip([-1] + b, b + [len(a)]) if j > i + 1]
b = []
for d in c:
b += d[0], d[-1]
for i in (0, -1):
if a[i] + 1:
b.pop(i)
b = b or [0]
m = min(b)
z = (max(b) - m + 1) // 2
print(max(z, z, *(abs(x - y) for d in c for x, y in zip(d, d[1:]))), m + z)
|
ASSIGN VAR VAR FOR VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP LIST NUMBER VAR BIN_OP VAR LIST FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR
|
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers.
Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$.
Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$.
Dark should choose an integer $k$ so that $m$ is minimized. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$.
-----Output-----
Print the answers for each test case in the following format:
You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$.
Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$.
If there is more than one possible $k$, you can print any of them.
-----Example-----
Input
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5
Output
1 11
5 35
3 6
0 42
0 0
1 2
3 4
-----Note-----
In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$.
In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$.
So, the maximum difference between any adjacent elements is $3$.
|
t = int(input())
for i in range(t):
n = int(input())
mx = 0
mn = 991000000000
sred = 0
m = list(map(int, input().split()))
for i in range(n):
if m[i] == -1:
continue
if i != 0:
if m[i - 1] == -1:
if mx < m[i]:
mx = m[i]
if mn > m[i]:
mn = m[i]
elif abs(m[i - 1] - m[i]) > sred:
sred = abs(m[i - 1] - m[i])
if i != n - 1:
if m[i + 1] == -1:
if mx < m[i]:
mx = m[i]
if mn > m[i]:
mn = m[i]
elif abs(m[i + 1] - m[i]) > sred:
sred = abs(m[i + 1] - m[i])
elif m[i + 1] == -1:
if mx < m[i]:
mx = m[i]
if mn > m[i]:
mn = m[i]
elif abs(m[i + 1] - m[i]) > sred:
sred = abs(m[i + 1] - m[i])
r = mx + mn
if mn > 1000000000:
print(0, 0)
continue
if sred > max(mx - r // 2, r // 2 - mn):
print(sred, r // 2)
else:
print(max(mx - r // 2, r // 2 - mn), r // 2)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER 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 NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER IF VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER
|
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers.
Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$.
Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$.
Dark should choose an integer $k$ so that $m$ is minimized. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$.
-----Output-----
Print the answers for each test case in the following format:
You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$.
Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$.
If there is more than one possible $k$, you can print any of them.
-----Example-----
Input
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5
Output
1 11
5 35
3 6
0 42
0 0
1 2
3 4
-----Note-----
In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$.
In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$.
So, the maximum difference between any adjacent elements is $3$.
|
import sys
input = sys.stdin.readline
for nt in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
c = a.count(-1)
if c == n:
print(0, 1)
continue
elif c == n - 1:
print(0, sum(a) + n - 1)
continue
minn = max(a)
maxx = 0
for i in range(1, n):
if a[i] == -1 and a[i - 1] != -1:
minn = min(minn, a[i - 1])
maxx = max(maxx, a[i - 1])
for i in range(n - 1):
if a[i] == -1 and a[i + 1] != -1:
minn = min(minn, a[i + 1])
maxx = max(maxx, a[i + 1])
ans = (minn + maxx) // 2
for i in range(n):
if a[i] == -1:
a[i] = ans
maxx = 0
for i in range(1, n):
maxx = max(maxx, abs(a[i] - a[i - 1]))
print(maxx, ans)
|
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
|
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers.
Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$.
Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$.
Dark should choose an integer $k$ so that $m$ is minimized. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$.
-----Output-----
Print the answers for each test case in the following format:
You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$.
Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$.
If there is more than one possible $k$, you can print any of them.
-----Example-----
Input
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5
Output
1 11
5 35
3 6
0 42
0 0
1 2
3 4
-----Note-----
In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$.
In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$.
So, the maximum difference between any adjacent elements is $3$.
|
t = int(input())
def gag(a, k):
ans = 0
for x in range(len(a) - 1):
a1, b1 = a[x], a[x + 1]
if a1 == -1:
a1 = k
if b1 == -1:
b1 = k
m = abs(a1 - b1)
if m > ans:
ans = m
return ans
round = lambda x: int(x + 0.5)
for x in range(t):
n = int(input())
a = list(map(int, input().split()))
l, r = 0, 10**9
while r - l >= 0.1:
m = (l + r) / 2
q, w = gag(a, (l + m) / 2), gag(a, (m + r) / 2)
if w > q:
r = m
else:
l = m
k1 = round(l)
k2 = round(r)
if gag(a, k1) > gag(a, k2):
print(gag(a, k1), k1)
else:
print(gag(a, k2), k2)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR 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 VAR NUMBER BIN_OP NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
|
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers.
Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$.
Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$.
Dark should choose an integer $k$ so that $m$ is minimized. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$.
-----Output-----
Print the answers for each test case in the following format:
You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$.
Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$.
If there is more than one possible $k$, you can print any of them.
-----Example-----
Input
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5
Output
1 11
5 35
3 6
0 42
0 0
1 2
3 4
-----Note-----
In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$.
In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$.
So, the maximum difference between any adjacent elements is $3$.
|
def solve(n, l):
l.append(-2)
i = 0
while l[i] == -1:
i += 1
if l[i] == -2:
return [0, 0]
l.insert(0, l[i])
l.pop()
j = len(l) - 1
while l[j] == -1:
j -= 1
l.append(l[j])
n = len(l)
i = 0
j = 0
mmax = -1
mmin = 10**9 + 1
while j < n:
while i < n and l[i] == -1:
i += 1
j = i + 1
while j < n and l[j] == -1:
j += 1
if j >= n:
break
if j == i + 1:
i = j
continue
mmin = min(mmin, min(l[i], l[j]))
mmax = max(mmax, max(l[i], l[j]))
i = j
res = 0
k = (mmin + mmax) // 2
for i in range(1, n):
if l[i] == -1:
l[i] = k
res = max(res, abs(l[i] - l[i - 1]))
return [res, k]
for t in range(int(input())):
n = int(input())
l = [int(i) for i in input().split()]
res = solve(n, l)
print(res[0], res[1])
|
FUNC_DEF EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER RETURN LIST NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER WHILE VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN LIST VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
|
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers.
Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$.
Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$.
Dark should choose an integer $k$ so that $m$ is minimized. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$.
-----Output-----
Print the answers for each test case in the following format:
You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$.
Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$.
If there is more than one possible $k$, you can print any of them.
-----Example-----
Input
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5
Output
1 11
5 35
3 6
0 42
0 0
1 2
3 4
-----Note-----
In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$.
In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$.
So, the maximum difference between any adjacent elements is $3$.
|
for t in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
ma = 0
b = []
for i in range(n - 1):
if a[i] != -1 and a[i + 1] != -1:
if ma < abs(a[i] - a[i + 1]):
ma = abs(a[i] - a[i + 1])
elif a[i] == -1 and a[i + 1] != -1:
b.append(a[i + 1])
elif a[i] != -1 and a[i + 1] == -1:
b.append(a[i])
if b == []:
print("0", "1")
else:
mu = int((max(b) - min(b) + 1) / 2)
k = min(b) + mu
m = max(mu, ma)
print(m, k)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR LIST EXPR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers.
Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$.
Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$.
Dark should choose an integer $k$ so that $m$ is minimized. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$.
-----Output-----
Print the answers for each test case in the following format:
You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$.
Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$.
If there is more than one possible $k$, you can print any of them.
-----Example-----
Input
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5
Output
1 11
5 35
3 6
0 42
0 0
1 2
3 4
-----Note-----
In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$.
In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$.
So, the maximum difference between any adjacent elements is $3$.
|
for _ in range(int(input(""))):
n = int(input(""))
arr = list(map(int, input().split()))
dorkar = []
if n == 2:
arr.sort()
if arr[1] == -1:
print(0, 1)
elif arr[0] == -1:
print(0, arr[1])
continue
for i in range(n):
if arr[i] != -1:
if i == 0:
if arr[i + 1] == -1:
dorkar.append(arr[i])
elif i == n - 1:
if arr[i - 1] == -1:
dorkar.append(arr[i])
elif arr[i - 1] == -1 or arr[i + 1] == -1:
dorkar.append(arr[i])
if len(dorkar) == 0:
print(0, 0)
continue
k = (min(dorkar) + max(dorkar)) // 2
for i in range(n):
if arr[i] == -1:
arr[i] = k
m = 0
for i in range(n - 1):
m = max(m, abs(arr[i] - arr[i + 1]))
print(m, k)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST IF VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
|
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers.
Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$.
Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$.
Dark should choose an integer $k$ so that $m$ is minimized. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$.
-----Output-----
Print the answers for each test case in the following format:
You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$.
Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$.
If there is more than one possible $k$, you can print any of them.
-----Example-----
Input
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5
Output
1 11
5 35
3 6
0 42
0 0
1 2
3 4
-----Note-----
In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$.
In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$.
So, the maximum difference between any adjacent elements is $3$.
|
for t in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
if a.count(-1) == n:
print(0, 0)
else:
b = []
if a[0] == -1 and a[1] != -1 or a[0] != -1 and a[1] == -1:
b.append(a[1] if a[1] != -1 else a[0])
if a[-1] == -1 and a[-2] != -1 or a[-2] == -1 and a[-1] != -1:
b.append(a[-2] if a[-2] != -1 else a[-1])
for i in range(1, n - 1):
if a[i] != -1:
if a[i + 1] == -1 or a[i - 1] == -1:
b.append(a[i])
k = (max(b) + min(b)) // 2
m = abs(k - max(b, key=lambda x: abs(x - k)))
for i in range(n - 1):
if a[i] != -1 and a[i + 1] != -1:
m = max(m, abs(a[i] - a[i + 1]))
print(m, k)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR LIST IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
|
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers.
Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$.
Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$.
Dark should choose an integer $k$ so that $m$ is minimized. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$.
-----Output-----
Print the answers for each test case in the following format:
You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$.
Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$.
If there is more than one possible $k$, you can print any of them.
-----Example-----
Input
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5
Output
1 11
5 35
3 6
0 42
0 0
1 2
3 4
-----Note-----
In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$.
In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$.
So, the maximum difference between any adjacent elements is $3$.
|
from sys import stdin, stdout
cin = stdin.readline
cout = stdout.write
mp = lambda: list(map(int, cin().split()))
def chars():
s = cin()
return list(s[: len(s) - 1])
def pl(a):
for val in a:
cout(str(val) + " ")
cout("\n")
(t,) = mp()
for _ in range(t):
(n,) = mp()
a = mp()
maxi = -(10**9)
mini = 10**9
for i in range(n):
if a[i] != -1:
if i > 0 and a[i - 1] == -1:
mini = min(mini, a[i])
maxi = max(maxi, a[i])
elif i < n - 1 and a[i + 1] == -1:
maxi = max(maxi, a[i])
mini = min(mini, a[i])
m = 0
k = (maxi + mini) // 2
for i in range(n):
if a[i] == -1:
a[i] = k
if i:
m = max(m, abs(a[i] - a[i - 1]))
pl([m, k])
|
ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR
|
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers.
Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$.
Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$.
Dark should choose an integer $k$ so that $m$ is minimized. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$.
-----Output-----
Print the answers for each test case in the following format:
You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$.
Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$.
If there is more than one possible $k$, you can print any of them.
-----Example-----
Input
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5
Output
1 11
5 35
3 6
0 42
0 0
1 2
3 4
-----Note-----
In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$.
In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$.
So, the maximum difference between any adjacent elements is $3$.
|
t = int(input())
for ti in range(t):
n = int(input())
li = [int(i) for i in input().split(" ")]
ss = 0
cc = 0
sep = 0
_min = 1919810114514
_max = 0
for idx, i in enumerate(li):
if i == -1:
if idx and li[idx - 1] != -1:
_min = min(_min, li[idx - 1])
_max = max(_max, li[idx - 1])
else:
if idx and li[idx - 1] != -1:
sep = max(sep, abs(li[idx - 1] - i))
elif idx and li[idx - 1] == -1:
_min = min(_min, i)
_max = max(_max, i)
ss += i
cc += 1
if cc:
r = (_min + _max) // 2
sep = max(sep, r - _min, _max - r)
print(sep, r)
else:
print(0, 0)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER
|
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers.
Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$.
Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$.
Dark should choose an integer $k$ so that $m$ is minimized. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$.
-----Output-----
Print the answers for each test case in the following format:
You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$.
Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$.
If there is more than one possible $k$, you can print any of them.
-----Example-----
Input
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5
Output
1 11
5 35
3 6
0 42
0 0
1 2
3 4
-----Note-----
In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$.
In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$.
So, the maximum difference between any adjacent elements is $3$.
|
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
b = []
m = 0
for i in range(n):
if a[i] == -1:
if i > 0 and a[i - 1] != -1:
b.append(a[i - 1])
if i < n - 1 and a[i + 1] != -1:
b.append(a[i + 1])
if len(b) != 0:
m = (max(b) + min(b)) // 2
c = [(m if j == -1 else j) for j in a]
k = 0
for t in range(n - 1):
k = max(k, abs(c[t + 1] - c[t]))
print(k, m)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers.
Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$.
Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$.
Dark should choose an integer $k$ so that $m$ is minimized. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$.
-----Output-----
Print the answers for each test case in the following format:
You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$.
Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$.
If there is more than one possible $k$, you can print any of them.
-----Example-----
Input
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5
Output
1 11
5 35
3 6
0 42
0 0
1 2
3 4
-----Note-----
In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$.
In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$.
So, the maximum difference between any adjacent elements is $3$.
|
def one():
return int(input())
def two():
return map(int, input().split())
def lis():
return list(map(int, input().split()))
def st():
return input()
for _ in range(one()):
input()
l = lis()
m = 10**9
M = 0
for ind, i in enumerate(l):
if i == -1:
if ind == 0:
if l[ind + 1] == -1:
continue
if m > l[ind + 1]:
m = l[ind + 1]
if M < l[ind + 1]:
M = l[ind + 1]
elif ind == len(l) - 1:
if l[ind - 1] == -1:
continue
if m > l[ind - 1]:
m = l[ind - 1]
if M < l[ind - 1]:
M = l[ind - 1]
else:
if l[ind - 1] != -1:
if m > l[ind - 1]:
m = l[ind - 1]
if M < l[ind - 1]:
M = l[ind - 1]
if l[ind + 1] != -1:
if m > l[ind + 1]:
m = l[ind + 1]
if M < l[ind + 1]:
M = l[ind + 1]
k = (M + m) // 2
if k == -1:
k = 1
maxabs = 0
for i in range(len(l) - 1):
if l[i] == -1:
l[i] = k
if l[i + 1] == -1:
l[i + 1] = k
if maxabs < abs(l[i] - l[i + 1]):
maxabs = abs(l[i] - l[i + 1])
print(maxabs, k)
|
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
|
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers.
Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$.
Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$.
Dark should choose an integer $k$ so that $m$ is minimized. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$.
-----Output-----
Print the answers for each test case in the following format:
You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$.
Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$.
If there is more than one possible $k$, you can print any of them.
-----Example-----
Input
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5
Output
1 11
5 35
3 6
0 42
0 0
1 2
3 4
-----Note-----
In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$.
In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$.
So, the maximum difference between any adjacent elements is $3$.
|
t = int(input())
while t:
n = int(input())
a = list(map(int, input().split()))
dif = 0
for i in range(n - 1):
if a[i] != -1 and a[i + 1] != -1:
if abs(a[i] - a[i + 1]) > dif:
dif = abs(a[i] - a[i + 1])
b = []
for i in range(n):
if i == 0:
if a[i] != -1 and a[i + 1] == -1:
b.append(a[i])
elif i == n - 1:
if a[i] != -1 and a[i - 1] == -1:
b.append(a[i])
elif a[i] != -1:
if a[i - 1] == -1 or a[i + 1] == -1:
b.append(a[i])
b.sort()
if len(b) != 0:
k = (b[0] + b[len(b) - 1]) // 2
ans = max(dif, k - b[0], b[len(b) - 1] - k)
print(ans, k)
else:
print(0, 0)
t -= 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE 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 NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER VAR NUMBER
|
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers.
Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$.
Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$.
Dark should choose an integer $k$ so that $m$ is minimized. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$.
-----Output-----
Print the answers for each test case in the following format:
You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$.
Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$.
If there is more than one possible $k$, you can print any of them.
-----Example-----
Input
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5
Output
1 11
5 35
3 6
0 42
0 0
1 2
3 4
-----Note-----
In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$.
In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$.
So, the maximum difference between any adjacent elements is $3$.
|
from sys import stdin, stdout
def solution():
from sys import stdin, stdout
_input, _print, _range, _int, _zip, _set, _len = (
stdin.readline,
stdout.write,
range,
int,
zip,
set,
len,
)
_min, _max, _str, _abs = min, max, str, abs
n = _int(_input())
for _ in _range(n):
l = _int(_input())
arr = [_int(i) for i in _input().split()]
maximum = -1
minimum = 1000000000
m = 0
old = arr[0]
f = True if arr[0] == -1 else False
for i in arr[1:]:
if i >= 0:
f = False
if i == -1 and old == -1:
old = i
continue
if i == -1 and old != -1:
if old > maximum:
maximum = old
if old < minimum:
minimum = old
old = i
continue
if i != -1 and old == -1:
if i > maximum:
maximum = i
if i < minimum:
minimum = i
old = i
continue
_m = _abs(old - i)
if m < _m:
m = _m
old = i
if minimum != 1000000000 and maximum != -1:
average = (maximum + minimum) // 2
_m = _max(maximum - average, average - minimum)
if m < _m:
m = _m
if f:
_print("0 42\n")
else:
_print(_str(m) + " ")
_print(_str(average) + "\n")
solution()
|
FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR 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 NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
|
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers.
Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$.
Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$.
Dark should choose an integer $k$ so that $m$ is minimized. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$.
-----Output-----
Print the answers for each test case in the following format:
You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$.
Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$.
If there is more than one possible $k$, you can print any of them.
-----Example-----
Input
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5
Output
1 11
5 35
3 6
0 42
0 0
1 2
3 4
-----Note-----
In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$.
In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$.
So, the maximum difference between any adjacent elements is $3$.
|
import sys
def run_length_compress(string):
string.append("@")
n = len(string)
begin = 0
end = 1
cnt = 1
ans = []
while True:
if end >= n:
break
if string[begin] == string[end]:
end += 1
cnt += 1
else:
ans.append(string[begin])
begin = end
end = begin + 1
cnt = 1
return ans
input = sys.stdin.readline
t = int(input())
INF = 10**18
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
max_a = 0
min_a = INF
adj = 0
b = run_length_compress(a)
for i, num in enumerate(b):
if b[i] == -1:
if i - 1 >= 0:
if b[i - 1] != -1:
min_a = min(min_a, b[i - 1])
max_a = max(max_a, b[i - 1])
if i + 1 < len(b):
if b[i + 1] != -1:
min_a = min(min_a, b[i + 1])
max_a = max(max_a, b[i + 1])
else:
if i - 1 >= 0:
if b[i - 1] != -1:
adj = max(adj, abs(b[i - 1] - b[i]))
if i + 1 < len(b):
if b[i + 1] != -1:
adj = max(adj, abs(b[i + 1] - b[i]))
if min_a == INF:
print(0, 0)
else:
print(max(adj, (max_a + 1 - min_a) // 2), min_a + (max_a + 1 - min_a) // 2)
|
IMPORT FUNC_DEF EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE NUMBER IF VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER RETURN VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR 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 NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER
|
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers.
Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$.
Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$.
Dark should choose an integer $k$ so that $m$ is minimized. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$.
-----Output-----
Print the answers for each test case in the following format:
You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$.
Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$.
If there is more than one possible $k$, you can print any of them.
-----Example-----
Input
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5
Output
1 11
5 35
3 6
0 42
0 0
1 2
3 4
-----Note-----
In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$.
In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$.
So, the maximum difference between any adjacent elements is $3$.
|
import sys
reader = (line.rstrip() for line in sys.stdin)
input = reader.__next__
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
minVal = float("inf")
maxVal = -float("inf")
for i, val in enumerate(a):
if val >= 0 and (i > 0 and a[i - 1] < 0 or i < n - 1 and a[i + 1] < 0):
minVal = min(minVal, val)
maxVal = max(maxVal, val)
if minVal == float("inf"):
print(0, 0)
continue
k = maxVal + minVal >> 1
if a[0] < 0:
a[0] = k
maxDelta = 0
for i in range(1, n):
if a[i] < 0:
a[i] = k
maxDelta = max(maxDelta, abs(a[i] - a[i - 1]))
print(maxDelta, k)
|
IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR 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 STRING ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
|
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers.
Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$.
Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$.
Dark should choose an integer $k$ so that $m$ is minimized. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$.
-----Output-----
Print the answers for each test case in the following format:
You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$.
Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$.
If there is more than one possible $k$, you can print any of them.
-----Example-----
Input
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5
Output
1 11
5 35
3 6
0 42
0 0
1 2
3 4
-----Note-----
In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$.
In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$.
So, the maximum difference between any adjacent elements is $3$.
|
n = int(input())
for v in range(n):
l = int(input())
a = list(map(int, input().split()))
t = []
for i in range(l):
if a[i] != -1:
if i > 0 and a[i - 1] == -1 or i < l - 1 and a[i + 1] == -1:
t.append(a[i])
if not t:
print(0, 0)
else:
mi = min(t)
ma = max(t)
k = (mi + ma) // 2
max_diff = 0
for e in range(l - 1):
if a[e] == -1:
a[e] = k
if a[e + 1] == -1:
a[e + 1] = k
max_diff = max(max_diff, abs(a[e] - a[e + 1]))
print(max_diff, k)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR 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 LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
|
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers.
Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$.
Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$.
Dark should choose an integer $k$ so that $m$ is minimized. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$.
-----Output-----
Print the answers for each test case in the following format:
You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$.
Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$.
If there is more than one possible $k$, you can print any of them.
-----Example-----
Input
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5
Output
1 11
5 35
3 6
0 42
0 0
1 2
3 4
-----Note-----
In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$.
In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$.
So, the maximum difference between any adjacent elements is $3$.
|
T = int(input())
for _ in range(0, T):
N = int(input())
s = [int(x) for x in input().split()]
L = []
for i in range(0, len(s)):
if s[i] == -1:
if i - 1 >= 0 and i - 1 < len(s):
if s[i - 1] != -1:
L.append(s[i - 1])
if i + 1 >= 0 and i + 1 < len(s):
if s[i + 1] != -1:
L.append(s[i + 1])
if len(L) == 0:
print(0, 0)
else:
L = list(set(L))
tot = min(L) + max(L)
ele1 = tot // 2
ele2 = tot // 2
if tot % 2 != 0:
ele2 += 1
L1 = []
L2 = []
for i in range(0, len(s)):
if s[i] == -1:
L1.append(ele1)
L2.append(ele2)
else:
L1.append(s[i])
L2.append(s[i])
mx1 = 0
mx2 = 0
for i in range(0, len(s) - 1):
mx1 = max(mx1, abs(L1[i] - L1[i + 1]))
mx2 = max(mx2, abs(L2[i] - L2[i + 1]))
if mx1 <= mx2:
print(mx1, ele1)
else:
print(mx2, ele2)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers.
Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$.
Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$.
Dark should choose an integer $k$ so that $m$ is minimized. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$.
-----Output-----
Print the answers for each test case in the following format:
You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$.
Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$.
If there is more than one possible $k$, you can print any of them.
-----Example-----
Input
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5
Output
1 11
5 35
3 6
0 42
0 0
1 2
3 4
-----Note-----
In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$.
In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$.
So, the maximum difference between any adjacent elements is $3$.
|
for i in range(int(input())):
n = int(input())
l = list(map(int, input().split()))
t1 = -1
t2 = 100000000000
m = 0
for i in range(n - 1):
if l[i] == -1 and l[i + 1] != -1:
if l[i + 1] > t1:
t1 = l[i + 1]
if l[i + 1] < t2:
t2 = l[i + 1]
elif l[i] != -1 and l[i + 1] == -1:
if l[i] > t1:
t1 = l[i]
if l[i] < t2:
t2 = l[i]
elif l[i] != -1 and l[i + 1] != -1:
if abs(l[i + 1] - l[i]) > m:
m = abs(l[i + 1] - l[i])
if t1 == -1:
if t2 == 100000000000:
print(max(m, 0), 0)
else:
print(max(m, 0), t2)
elif t2 == 100000000000:
print(max(m, 0), t1)
else:
k = (t1 + t2) // 2
print(max(m, t1 - k, k - t2), k)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR
|
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers.
Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$.
Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$.
Dark should choose an integer $k$ so that $m$ is minimized. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$.
-----Output-----
Print the answers for each test case in the following format:
You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$.
Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$.
If there is more than one possible $k$, you can print any of them.
-----Example-----
Input
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5
Output
1 11
5 35
3 6
0 42
0 0
1 2
3 4
-----Note-----
In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$.
In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$.
So, the maximum difference between any adjacent elements is $3$.
|
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
arr = []
for i in range(n):
if a[i] == -1:
if i == 0:
if a[i + 1] >= 0:
arr.append(a[i + 1])
elif i == n - 1:
if a[i - 1] >= 0:
arr.append(a[i - 1])
else:
if a[i - 1] >= 0:
arr.append(a[i - 1])
if a[i + 1] >= 0:
arr.append(a[i + 1])
if len(arr) == 0:
print("0 3")
else:
mn = min(arr)
m = max(arr)
x = (mn + m) // 2
ans = []
for i in range(0, n):
if a[i] == -1:
a[i] = x
for i in range(1, n):
ans.append(abs(a[i] - a[i - 1]))
print(max(ans), x)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers.
Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$.
Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$.
Dark should choose an integer $k$ so that $m$ is minimized. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$.
-----Output-----
Print the answers for each test case in the following format:
You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$.
Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$.
If there is more than one possible $k$, you can print any of them.
-----Example-----
Input
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5
Output
1 11
5 35
3 6
0 42
0 0
1 2
3 4
-----Note-----
In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$.
In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$.
So, the maximum difference between any adjacent elements is $3$.
|
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
ans = []
f2 = True
for i in range(n):
if a[i] != -1:
f2 = False
break
if f2:
print(0, 0)
continue
for i in range(n - 1):
if a[i] == -1 and a[i + 1] == -1:
pass
elif a[i] == -1:
ans.append(a[i + 1])
elif a[i + 1] == -1:
ans.append(a[i])
mi = min(ans)
mx = max(ans)
k = (mi + mx) // 2
m = abs(mi - k)
x = 0
for i in range(n):
if a[i] == -1:
a[i] = k
for i in range(n - 1):
x = max(x, abs(a[i] - a[i + 1]))
print(max(x, m), k)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR 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 LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
|
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers.
Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$.
Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$.
Dark should choose an integer $k$ so that $m$ is minimized. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$.
-----Output-----
Print the answers for each test case in the following format:
You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$.
Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$.
If there is more than one possible $k$, you can print any of them.
-----Example-----
Input
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5
Output
1 11
5 35
3 6
0 42
0 0
1 2
3 4
-----Note-----
In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$.
In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$.
So, the maximum difference between any adjacent elements is $3$.
|
def solve(A, N):
adj = set()
m = 0
for i in range(N - 1):
if A[i] != -1 and A[i + 1] != -1:
m = max(m, abs(A[i + 1] - A[i]))
if A[i] == -1 and A[i + 1] != -1:
adj.add(A[i + 1])
if A[i] != -1 and A[i + 1] == -1:
adj.add(A[i])
if not adj:
return "0 0"
maxVal = max(adj)
minVal = min(adj)
k = (maxVal + minVal) // 2
m = max(m, abs(maxVal - k), abs(minVal - k))
return str(m) + " " + str(k)
(T,) = map(int, input().split())
for t in range(T):
(N,) = map(int, input().split())
A = [int(x) for x in input().split()]
ans = solve(A, N)
print(ans)
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR RETURN STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers.
Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$.
Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$.
Dark should choose an integer $k$ so that $m$ is minimized. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$.
-----Output-----
Print the answers for each test case in the following format:
You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$.
Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$.
If there is more than one possible $k$, you can print any of them.
-----Example-----
Input
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5
Output
1 11
5 35
3 6
0 42
0 0
1 2
3 4
-----Note-----
In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$.
In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$.
So, the maximum difference between any adjacent elements is $3$.
|
import sys
t = int(sys.stdin.readline())
for _ in range(t):
n = int(sys.stdin.readline())
arr = list(map(int, sys.stdin.readline().split()))
a = set()
for i in range(n):
if arr[i] == -1:
if i + 1 < n and arr[i + 1] != -1:
a.add(arr[i + 1])
if i - 1 >= 0 and arr[i - 1] != -1:
a.add(arr[i - 1])
if len(a) < 1:
print(0, 0)
else:
low, high = min(a), max(a)
k = (low + high) // 2
for i in range(n):
if arr[i] == -1:
arr[i] = k
dif = 0
for i in range(n - 1):
dif = max(dif, abs(arr[i] - arr[i + 1]))
print(dif, k)
|
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR 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 FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
|
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers.
Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$.
Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$.
Dark should choose an integer $k$ so that $m$ is minimized. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$.
-----Output-----
Print the answers for each test case in the following format:
You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$.
Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$.
If there is more than one possible $k$, you can print any of them.
-----Example-----
Input
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5
Output
1 11
5 35
3 6
0 42
0 0
1 2
3 4
-----Note-----
In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$.
In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$.
So, the maximum difference between any adjacent elements is $3$.
|
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
ma = -1
k = []
for j in range(len(a) - 1):
if a[j] == -1 and a[j + 1] != -1:
k.append(a[j + 1])
elif a[j] != -1 and a[j + 1] == -1:
k.append(a[j])
else:
ma = max(ma, abs(a[j] - a[j + 1]))
if len(k) == 0:
print(0, 0)
else:
mini = min(k)
nmaximum = max(k)
print(
max(ma, max(k) - (mini + (nmaximum - mini) // 2)),
mini + (nmaximum - mini) // 2,
)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER
|
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers.
Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$.
Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$.
Dark should choose an integer $k$ so that $m$ is minimized. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$.
-----Output-----
Print the answers for each test case in the following format:
You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$.
Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$.
If there is more than one possible $k$, you can print any of them.
-----Example-----
Input
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5
Output
1 11
5 35
3 6
0 42
0 0
1 2
3 4
-----Note-----
In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$.
In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$.
So, the maximum difference between any adjacent elements is $3$.
|
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
def birth():
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
inamo = []
maxdis = 0
for i in range(n):
if i < n - 1:
if a[i] != -1 and a[i + 1] != -1:
maxdis = max(maxdis, abs(a[i + 1] - a[i]))
if a[i] == -1:
if i == 0:
if a[i + 1] != -1:
if a[i + 1] not in inamo:
inamo.append(a[i + 1])
elif i == n - 1:
if a[i - 1] != -1:
if a[i - 1] not in inamo:
inamo.append(a[i - 1])
else:
if a[i + 1] != -1:
if a[i + 1] not in inamo:
inamo.append(a[i + 1])
if a[i - 1] != -1:
if a[i - 1] not in inamo:
inamo.append(a[i - 1])
if inamo:
res = (max(inamo) + min(inamo)) // 2
maxlen = max(max(max(inamo) - res, res - min(inamo)), maxdis)
yield str(maxlen) + " " + str(res)
else:
yield "0 0"
t = int(input())
ans = birth()
print(*ans, sep="\n")
|
IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR 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 LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR EXPR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR STRING
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.