description
stringlengths 171
4k
| code
stringlengths 94
3.98k
| normalized_code
stringlengths 57
4.99k
|
|---|---|---|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
hobs, pillows, frodo = input().split()
hobs, pillows, frodo = int(hobs), int(pillows), int(frodo)
turn = 0
left = frodo
right = frodo
pilCount = 1
pillows -= hobs
while True:
if left < 1 and right > hobs:
pilCount += pillows // hobs
break
elif left < 1:
pillows -= right
elif right > hobs:
pillows -= hobs - left + 1
else:
pillows -= turn * 2 + 1
if pillows < 0:
break
left -= 1
right += 1
pilCount += 1
turn += 1
print(pilCount)
|
ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR VAR WHILE NUMBER IF VAR NUMBER VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
n, m, k = list(map(int, input().split()))
s = 0
m -= n
s += 1
if m == 0:
print(1)
return
while m >= 0:
if k > s and n - k + 1 > s and m >= 2 * (s - 1) + 1:
m -= 2 * (s - 1) + 1
elif k > s and m >= n - k + s:
m -= n - k + s
elif n - k + 1 > s and m >= k + s - 1:
m -= k + s - 1
elif m >= n:
s += m // n
m -= n * (m // n)
print(s)
return
else:
break
s += 1
print(s)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN WHILE VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR NUMBER EXPR FUNC_CALL VAR VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
n, m, k = [int(f) for f in input().split()]
l, r = 0, m
t = True
for x in range(60):
i = (l + r) // 2
c = k - 1
b = n - k
s = (
i
- 1
+ n
+ [(i - 2 - b) * b if i - 2 - b > 0 else 0][0]
+ [(i - 2 - c) * c if i - 2 - c > 0 else 0][0]
+ min(i - 2, b) * (1 + min(b, i - 2)) / 2
+ min(i - 2, c) * (1 + min(c, i - 2)) / 2
)
if n == 1:
print(m)
t = False
break
if s <= m:
l = i
elif s >= m:
r = i
if t:
print(l)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR LIST BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER LIST BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
def v(length, start):
W = start * (start + 1) // 2
t = max(0, start - length)
T = t * (t + 1) // 2
return W - T + max(0, length - start)
def check(p):
return p + v(k - 1, p - 1) + v(n - k, p - 1) <= m
n, m, k = map(int, input().split())
l = 0
r = 10**100
while r - l > 1:
mid = (l + r) // 2
if not check(mid):
r = mid
else:
l = mid
print(l)
|
FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
def __starting_point():
n, m, k = map(int, input().split())
m -= n
res = 1
lvl = 0
while m > 0:
x = min(k, lvl + 1) + min(n - k, lvl)
if x == n:
res += m // n
break
elif m >= x:
m -= x
lvl += 1
res += 1
else:
break
print(res)
__starting_point()
|
FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
hobbits_nr, pillows_nr, frodos_place = (int(x) for x in input().split())
pillows_nr = pillows_nr - hobbits_nr
frodos_pillows = 1
left = frodos_place
right = frodos_place + 1
while pillows_nr >= right - left:
pillows_nr -= right - left
frodos_pillows += 1
right += 1
left += -1
if right > hobbits_nr + 1:
right = hobbits_nr + 1
if left < 1:
left = 1
if left == 1 and right == hobbits_nr + 1:
frodos_pillows += pillows_nr // hobbits_nr
pillows_nr = pillows_nr % hobbits_nr
print(frodos_pillows)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
n, m, kk = map(int, input().split())
def getleft(x, k):
res = 0
if x < k:
res = (1 + x) * x / 2
res += k - x
else:
res = (x - k + 1 + x) * k / 2
return res
def getright(x, k):
return getleft(x, n - k + 1)
lt = 1
rt = m
ans = 1
while lt <= rt:
mid = (lt + rt) // 2
ta = getleft(mid, kk) + getright(mid, kk) - mid
if ta <= m:
lt = mid + 1
ans = mid
else:
rt = mid - 1
print(ans)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
def main():
n, hi, k = map(int, input().split())
m, l, lo = (hi - n) * 2, n - k + 1, 0
while lo < hi - 1:
mid = (lo + hi) // 2
x = mid * mid * 2
if mid > k:
x -= (mid - k) * (mid - k + 1)
if mid > l:
x -= (mid - l) * (mid - l + 1)
if x > m:
hi = mid
else:
lo = mid
print(lo + 1)
main()
|
FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
n, m, k = input().split(" ")
n, m, k = int(n), int(m), int(k)
ans = 1
m -= n
a = 0
b = 0
c = a + b + 1
while m >= c:
ans += 1
m -= c
if a < k - 1:
a += 1
if b < n - k:
b += 1
if a == k - 1 and b == n - k:
break
c = a + b + 1
ans += m // n
print(ans)
|
ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
def pillows_needed(height, width):
if height > width:
return height * (height + 1) // 2 - (height - width) * (height - width + 1) // 2
else:
return height * (height + 1) // 2 + (width - height)
n, m, k = list(map(int, input().split()))
a, b, c = 0, m, 0
while a < b:
c = (a + b + 1) // 2
if m >= c + pillows_needed(c - 1, n - k) + pillows_needed(c - 1, k - 1):
a = c
else:
b = c - 1
print(a)
|
FUNC_DEF IF VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
q, w, e = list(map(int, input().split()))
w -= q
z = e - 1
x = q - e
z, x = min(z, x), max(z, x)
ans = 1
t = 1
while w - t >= 0:
w -= t
ans += 1
if z == x == 0:
ans += w // t
break
if z > 0:
z -= 1
t += 1
if x > 0:
x -= 1
t += 1
print(ans)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
def main():
n, m, k = map(int, input().split())
l = 1
r = 10**10
while l + 1 < r:
med = (l + r) // 2
cnt = n
left = max(0, med - 1 - (k - 1))
right = max(0, med - 1 - (n - k))
left_cord = max(1, k - med + 1)
right_cord = min(k + med - 1, n)
cnt += (k - left_cord + 1) * (left + med - 1) / 2
cnt += (right_cord - k + 1) * (right + med - 1) / 2
cnt -= med - 1
if cnt <= m:
l = med
else:
r = med
print(l)
main()
|
FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
n, m, k = [int(i) for i in input().split()]
def get_min_pillow(bed, x):
res = x * (x + 1) / 2
if bed >= x:
res += bed - x
else:
temp = x - bed
res -= temp * (temp + 1) / 2
return res
def get_min(s, x):
return x * (x + 1) / 2 + (s - x) if s >= x else s * (x + x - s + 1) / 2
def check(x):
return get_min(k, x) + get_min(n - k + 1, x) - x <= m
l = (m + n - 1) // n
r = m + 1
while r - l > 1:
mid = l + r >> 1
if check(mid):
l = mid
else:
r = mid
print(l)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN VAR FUNC_DEF RETURN VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER FUNC_DEF RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
n, m, k = map(int, input().split())
i, j = 1, m
def check(mid):
ans = mid * (mid + 1) // 2
if mid > k:
ans -= (mid - k) * (mid - k + 1) // 2
else:
ans += k - mid
ans += mid * (mid - 1) // 2
if n - k > mid - 1:
ans += 1 * (n - k - mid + 1)
else:
t = mid - 1 - n + k
ans -= t * (t + 1) // 2
return ans <= m
while i < j:
mid = (i + j + 1) // 2
if check(mid):
i = mid
else:
j = mid - 1
print(j)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
n, m, k = map(int, input().split())
a = 0
b = 0
a_lim = k - 1
b_lim = n - k
m = m - n
x = 1
while m - a - b - 1 >= 0:
m = m - a - b - 1
x += 1
if a == a_lim:
pass
else:
a += 1
if b == b_lim:
pass
else:
b += 1
if a == a_lim and b == b_lim:
x = x + m // n
break
print(x)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
n, m, k = map(int, input().split())
m -= n
d = 0
k -= 1
ans = 1
while m > 1 and d != max(k, n - k - 1):
ans += 1
m -= 1
l = min(d, k)
r = min(d, n - k - 1)
d += 1
m -= l
m -= r
ans += m // n
print(ans)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
from sys import stdin, stdout
n, m, k = map(int, stdin.readline().split())
left = k - 1
right = n - k
l = 1
r = m + 1
while r - l > 1:
mid = (r + l) // 2
cnt = mid
if mid > left:
cnt += (2 * mid - 1 - left) * left // 2
else:
cnt += mid * (mid - 1) // 2 + left - (mid - 1)
if mid > right:
cnt += (2 * mid - 1 - right) * right // 2
else:
cnt += mid * (mid - 1) // 2 + right - (mid - 1)
if cnt <= m:
l = mid
else:
r = mid
stdout.write(str(l))
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
n, m, k = map(int, input().split())
f = lambda k, s: (2 * s - k + 1) * k - s if k < s else (s - 2) * s + 2 * k
s, m = m // n + 1, 2 * m + 1
while f(k, s) + f(n - k + 1, s) < m:
s += 1
print(s - 1)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER WHILE BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
def just_sum(n):
return n * (n + 1) // 2
def get_sum(a, b):
return just_sum(b) - just_sum(a - 1)
def check(middle):
left = k - 1
right = n - k
if left < middle:
left_sum = get_sum(middle - left, middle - 1)
else:
left_sum = just_sum(middle - 1) + (left - (middle - 1))
if right < middle:
right_sum = get_sum(middle - right, middle - 1)
else:
right_sum = just_sum(middle - 1) + (right - (middle - 1))
return left_sum + right_sum + middle <= m
n, m, k = map(int, input().split())
l = 1
r = m + 1
while l < r - 1:
middle = (l + r) // 2
if check(middle):
l = middle
else:
r = middle
print(l)
|
FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
I = lambda: list(map(int, input().split()))
n, m, k = I()
l, r = 1, m + 1
onLeft, onRight = k - 1, n - k
while l < r - 1:
amount = l + r >> 1
s = amount
for neighbors in [onLeft, onRight]:
if neighbors > amount - 1:
s += (amount - 1) * amount // 2 + neighbors - amount + 1
else:
s += (amount - neighbors + amount - 1) * neighbors // 2
if s <= m:
l = amount
else:
r = amount
print(l)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FOR VAR LIST VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
def check(n, m, k, r):
a = r - k
u = 0
if a > 0:
u += (a + r) * (k + 1) // 2
else:
u += r * (r + 1) // 2
b = r - (n - 1 - k)
v = 0
if b > 0:
v += (b + r) * (n - k) // 2
else:
v += r * (r + 1) // 2
t = u + v - r
return t <= m
n, m, k = map(int, input().split())
k -= 1
m -= n
INF = 10**9 + 10
a = 0
b = INF
while b - a > 1:
mid = (a + b) // 2
if check(n, m, k, mid):
a = mid
else:
b = mid
res = 1 + a
print(res)
|
FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
n, m, k = list(map(int, input().split()))
def g(a, b):
return (a + a - b + 1) * b >> 1
def f(x):
return g(x, min(x, k)) + g(x - 1, min(x - 1, n - k))
l, r, res = 1, m - n, 0
while l <= r:
mid = l + r >> 1
if f(mid) <= m - n:
res, l = mid, mid + 1
else:
r = mid - 1
print(res + 1)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
n, m, k = list(map(int, input().split()))
min_delta = min(k - 1, n - k)
max_delta = max(k - 1, n - k)
top = (
((k - 1) * k + (n - k) * (n - k + 1)) // 2
+ (max_delta - min_delta) * min_delta
+ max_delta
+ 1
)
def get_level(n, k, level):
return 1 + min(level, k - 1) + min(level, n - k)
if top <= m:
print(max_delta + 1 + (m - top) // n)
else:
add = m - n
curr_level = 0
while add >= get_level(n, k, curr_level):
add -= get_level(n, k, curr_level)
curr_level += 1
print(curr_level + 1)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR VAR VAR NUMBER FUNC_DEF RETURN BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
def summ(t):
ans = t * (t + 1) // 2
return ans
n, m, k = map(int, input().split())
def res(p):
global n
tmp = n * p
l = k - 1
t = min(p - 1, l)
tmp -= summ(t)
tmp -= (p - 1) * (l - t)
r = n - k
t = min(p - 1, r)
tmp -= summ(t)
tmp -= (p - 1) * (r - t)
return tmp
l = 0
r = m + 1
while r - l > 1:
mid = l + (r - l) // 2
if res(mid) <= m:
l = mid
else:
r = mid
print(l)
|
FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
n, m, k = map(int, input().split())
m -= n
def Sum(cnt, x):
return cnt * (x - 1 + (x - cnt)) // 2
def check(x):
l = min(k - 1, x - 1)
r = min(n - k, x - 1)
cur = Sum(l, x) + Sum(r, x) + x
return cur <= m
L = -1
R = 10**20
while R - L > 1:
M = L + R >> 1
if check(M):
L = M
else:
R = M
print(L + 1)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
import sys
def sum_(a):
return max(0, a * (a + 1) // 2)
def check(a):
if a * n <= m:
return 1
a -= 1
ans = n
t1 = a - k + 1
if t1 >= 0:
ans += sum_(a) - sum_(t1 - 1)
else:
ans += sum_(a)
z = n - k + 1
t2 = a - z + 1
if t2 >= 0:
ans += sum_(a - 1) - sum_(t2 - 1)
else:
ans += sum_(a - 1)
return ans <= m
def bins():
l = 1
r = m + 1
while l + 1 != r:
m1 = (l + r) // 2
if check(m1):
l = m1
else:
r = m1
print(l)
n, m, k = map(int, input().split())
bins()
|
IMPORT FUNC_DEF RETURN FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF IF BIN_OP VAR VAR VAR RETURN NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
n, m, k = map(int, input().split())
l = 1
r = m + 1
left = k - 1
right = n - k
rs = right * (right - 1) // 2
ls = left * (left - 1) // 2
while r > l + 1:
md = (l + r) // 2
if md > right:
s = md - right
tr = s * right + rs
else:
tr = right - md + 1 + md * (md - 1) // 2
if md > left:
s = md - left
lr = s * left + ls
else:
lr = left - md + 1 + md * (md - 1) // 2
if m >= lr + tr + md:
l = md
else:
r = md
print(l)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
n, m, k = map(int, input().split(" "))
m -= n
ans = 1
side1 = k - 1
side2 = n - k
prevadd = 1
while m >= prevadd:
m -= prevadd
ans += 1
if side1 + side2 == 0:
ans += m // n
break
if side1 > 0:
side1 -= 1
prevadd += 1
if side2 > 0:
side2 -= 1
prevadd += 1
print(ans)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
n, m, k = map(int, input().split())
done = 0
for A in range(m // n, m + 1):
up = A
if k - 1 >= A - 1:
up = up + A * (A - 1) / 2
up = up + k - A
else:
s = A - k + 1
up = up + (s + (A - 1)) * (A - s) / 2
kk = n - k
if kk >= A - 1:
up = up + A * (A - 1) / 2
up = up + kk - (A - 1)
else:
s = A - kk
up = up + (s + (A - 1)) * (A - s) / 2
if up > m:
done = 1
print(A - 1)
break
if done == 0:
print(m)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
hob, pil, fro = map(int, input().split())
pil -= hob
ans = 1
rad = 0
x, y = fro - 1, hob - fro
while pil > 0:
if rad >= max(x, y):
ans += pil // hob
break
pil -= min(rad, x)
pil -= min(rad, y)
if pil < 1:
break
pil -= 1
ans += 1
rad += 1
print(ans)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR WHILE VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
def tri(x):
return x * (x + 1) // 2
def C(x, k, n):
ret = 0
if x == 0:
return x
if x <= k:
ret = ret + tri(x - 1) + (k - x)
else:
rem = x - k
ret = ret + tri(x - 1) - tri(rem)
r = n - k + 1
if x <= r:
ret = ret + tri(x) + (r - x)
else:
rem = x - r
ret = ret + tri(x) - tri(rem)
return ret
n, m, k = map(int, input().split())
ans = 0
hi = m
lo = 0
while lo <= hi:
mid = (hi + lo) // 2
if C(mid, k, n) <= m:
ans = mid
lo = mid + 1
else:
hi = mid - 1
print(ans)
|
FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER RETURN VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
def calc(y, x):
if y > x - 1:
return int(x * (x - 1) / 2) + y - x + 1
return int((x - 1 + x - y) * y / 2)
def main():
n, m, k = map(int, input().split())
low = 1
hi = m
beds_left = k - 1
beds_right = n - k
while low <= hi:
mid = low + hi >> 1
cant = calc(beds_left, mid) + calc(beds_right, mid)
comp = m - mid - cant
if comp < 0:
hi = mid - 1
else:
low = mid + 1
print(low - 1)
main()
|
FUNC_DEF IF VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
n, m, k = map(int, input().split())
kk1 = min(k - 1, n - k)
kk2 = n - kk1 - 1
mm = m - n
s1 = (kk1 + 1) ** 2
if s1 >= mm:
res = 1 + int(mm**0.5)
else:
res = 1 + (kk1 + 1)
mmm = mm - s1
a = 2 * (kk1 + 1)
b = kk2 - kk1 - 1
s2 = a * b + b * (b - 1) // 2
if s2 >= mmm:
res += int(-(2 * a - 1) / 2 + (((2 * a - 1) / 2) ** 2 + 2 * mmm) ** 0.5)
else:
mmmm = mmm - s2
res += b + mmmm // n
print(res)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER NUMBER BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
beds, pillows, frodo = map(int, input().split())
flag = 0
if beds == pillows:
flag = 1
pillows = pillows - beds - 1
layer = 2
while pillows > 0:
if beds == 1:
layer = pillows + 2
pillows = 0
break
if frodo - 1 < layer - 1 and beds - frodo < layer - 1:
layer += int(pillows / beds) + 1
break
else:
pillows -= min(frodo - 1, layer - 1) + min(layer - 1, beds - frodo)
pillows -= 1
if pillows < 0:
layer -= 1
layer += 1
if flag == 1:
print("1")
elif pillows < 0:
print(layer)
elif pillows == 0:
print(layer)
else:
print(layer - 1)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
def f(med):
left = k - 1
right = n - k
ans = 0
if med > left + 1:
d = med - left
ans += (med + d) * (med - d + 1) // 2
else:
ans += med * (med + 1) // 2
left -= med - 1
ans += left
if med > right + 1:
d = med - right
ans += (med + d) * (med - d + 1) // 2
else:
ans += med * (med + 1) // 2
right -= med - 1
ans += right
if ans - med <= m:
return True
else:
return False
n, m, k = list(map(int, input().split()))
l = 1
r = m + 1
while r - l > 1:
med = (r + l) // 2
if f(med):
l = med
else:
r = med
print(l)
|
FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
def pil_add(height, div):
add = height
if height > div:
add = div
return add
hobb, pillow, number = (int(i) for i in input().split())
div_first = number
div_last = hobb - number + 1
other_pillow = pillow - hobb
height = 0
heap_pil = 0
right_flag = False
left_flag = False
while heap_pil <= other_pillow:
height += 1
if not (right_flag and left_flag):
right = pil_add(height, div_first)
if right == div_first:
right_flag = True
left = pil_add(height, div_last)
if left == div_last:
left_flag = True
heap_pil += right + left - 1
else:
other_pillow -= heap_pil
height += int(other_pillow / hobb)
break
print(height)
|
FUNC_DEF ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
def partSum(a, b):
if a > b:
return 0
supp = 0
if a <= 0:
supp = -a + 1
a = 1
return supp + b * (b + 1) // 2 - (a - 1) * (a - 1 + 1) // 2
def f(v, l, r):
return v + partSum(v - l, v - 1) + partSum(v - r, v - 1)
def valid(v, l, r):
return m - v - partSum(v - l, v - 1) - partSum(v - r, v - 1) >= 0
n, m, k = [int(item) for item in input().split()]
l = k - 1
r = n - k
a, b = 1, m
while a <= b:
bound = (a + b) // 2
if valid(bound, l, r):
if not valid(bound + 1, l, r):
break
a = bound + 1
else:
if valid(bound - 1, l, r):
bound -= 1
break
b = bound - 1
a, b = 1, bound
while a <= b:
mi = (a + b) // 2
if f(mi, l, r) > m:
b = mi - 1
elif f(mi, l, r) < m:
a = mi + 1
else:
print(mi)
break
else:
if f(mi, l, r) <= m:
print(mi)
else:
print(mi - 1)
|
FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER RETURN BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER FUNC_DEF RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
def total_pillows(x, y):
if y > x - 1:
return y - (x - 1) + (x - 1) * x / 2
else:
return (x - 1 + x - y) * y / 2
n, m, k = list(map(int, input().split()))
y1 = n - k
y2 = k - 1
l, h = 0, m
ans = 1 if m == n else 0
if n == 1:
ans = m
if ans != 1 and ans != m:
while l <= h:
mid = (h + l) // 2
if y1 != y2:
r1 = total_pillows(mid, y2)
r2 = total_pillows(mid, y1)
else:
r1 = total_pillows(mid, y2)
r2 = r1
if r1 + r2 + mid <= m:
ans = mid
l = mid + 1
else:
h = mid - 1
print(ans)
|
FUNC_DEF IF VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
import sys
input = sys.stdin.readline
def f(x):
z = m - n
if x > k:
sumi = k * (2 * x - k + 1) // 2
else:
sumi = x * (x + 1) // 2
y = n - k
if x > y:
sumi += y * (2 * x - y - 1) // 2
else:
sumi += x * (x - 1) // 2
if sumi <= z:
return 1
return 0
def bsearch(l, r):
m = (l + r) // 2
if f(m):
if f(m + 1) == 0:
return m
return bsearch(m + 1, r)
return bsearch(l, m - 1)
l = input().split()
n = int(l[0])
m = int(l[1])
k = int(l[2])
lol = bsearch(0, 10**18)
print(lol + 1)
|
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER RETURN VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
n, m, k = list(map(int, input().split()))
left = 0
right = 10000000000
while right - left > 1:
mid = (left + right) // 2
counter = mid
lh = k - 1
if lh >= mid - 1:
counter += (mid - 1) * mid // 2
counter += lh - (mid - 1)
else:
last_hobbit = mid - lh - 1
counter += (mid - 1) * mid // 2 - last_hobbit * (last_hobbit + 1) // 2
rh = n - k
if rh >= mid - 1:
counter += (mid - 1) * mid // 2
counter += rh - (mid - 1)
else:
last_hobbit = mid - rh - 1
counter += (mid - 1) * mid // 2 - last_hobbit * (last_hobbit + 1) // 2
if counter > m:
right = mid
else:
left = mid
print(left)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
def ri():
return map(int, input().split())
n, m, k = ri()
def area(h, n, k):
s = n
if k >= h - 1:
s += h * (h - 1) // 2
else:
s += k * (k + 1) // 2 + k * (h - k - 1)
if n - k >= h - 1:
s += (h - 1) * (h - 2) // 2
else:
s += (n - k - 1) * (n - k) // 2 + (n - k) * (h - n + k - 1)
return s
h1 = 1
h2 = m
hmax = 0
hprev = 0
while True:
hh = (h1 + h2) // 2
if hh == hprev:
break
hprev = hh
mm = area(hh, n, k)
if mm > m:
h2 = hh
else:
h1 = hh + 1
hmax = max(hmax, hh)
print(hmax)
|
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
n, m, k = list(map(int, input().split()))
def __sum(n):
return n * (n + 1) // 2
def _sum(l, r):
if l > r:
return 0
delta = 0
if l <= 0:
delta = abs(l) + 1
l = 1
return __sum(r) - __sum(l - 1) + delta
left = 1
right = int(1000000000.0) + 2
while right - left > 1:
mid = (left + right) // 2
sub = _sum(mid - k + 1, mid) + _sum(mid - (n - k), mid - 1)
if sub <= m:
left = mid
else:
right = mid
print(left)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
def check(n, m, k, x):
s = x
if x <= k:
s += (x - 1) * x // 2
else:
s += (x - 1) * x // 2 - (x - k) * (x - k + 1) // 2
if x - 1 <= n - k:
s += (x - 1) * x // 2
else:
s += (x - 1) * x // 2 - (x - 1 - n + k) * (x - n + k) // 2
if s <= m:
return True
return False
def main():
n, m, k = map(int, input().split())
m -= n
l = 0
h = 1000 * 1000 * 1000
while l <= h:
x = (l + h) // 2
if check(n, m, k, x):
l = x + 1
else:
h = x - 1
print(l)
main()
|
FUNC_DEF ASSIGN VAR VAR IF VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
def distribute(target, n, k, m):
right = n - k
prev = target - 1
total = m - target
if right >= prev:
total -= prev * (prev + 1) // 2
total -= right - prev
else:
temp = prev - right
total -= prev * (prev + 1) // 2 - temp * (temp + 1) // 2
right = k - 1
prev = target - 1
if right >= prev:
total -= prev * (prev + 1) // 2
total -= right - prev
else:
temp = prev - right
total -= prev * (prev + 1) // 2 - temp * (temp + 1) // 2
if total < 0:
return -1
else:
return 1
n, m, k = map(int, input().split())
low = 1
high = m - n + 1
while low <= high:
mid = low + (high - low) // 2
val = distribute(mid, n, k, m)
if val == -1:
high = mid - 1
else:
low = mid + 1
print(high)
|
FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
n, m, k = list(map(int, input().split()))
p = m - n
min_d, max_d = min(abs(n - k + 1), k), max(abs(n - k + 1), k)
i = 1
r = m + 1
l = 0
while r - l > 1:
i = (r + l) // 2
min_dist = min_d - i
max_dist = max_d - i
min_summ = 0
max_summ = 0
if min_dist < 0:
min_summ = abs(min_dist) * (abs(min_dist) + 1)
if max_dist < 0:
max_summ = abs(max_dist) * (abs(max_dist) + 1)
summ = (i * (i + 1) - i) * 2 - min_summ - max_summ
if summ > p * 2:
r = i
else:
l = i
print(r)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
n, m, k = map(int, input().split())
l = 1
r = m + 1
s = 0
f = 0
while r - l > 1:
x = (l + r) // 2
if x - 1 < k - 1:
f = x * (x - 1) // 2 + (k - 1 - (x - 1))
else:
f = (x - 1 - (k - 2) + (x - 1)) * (k - 1) // 2
if f + x > m:
s = 0
elif x - 1 >= n - k:
s = (n - k) * (x - 1 + (x - 1) - (n - k - 1)) // 2
else:
s = x * (x - 1) // 2 + (n - k) - (x - 1)
if f + s + x <= m:
l = x
else:
r = x
print(l)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
def I():
return list(map(int, input().split()))
def f(k):
s = 0
if k - idx > 1:
s -= (k - idx - 1) * (k - idx) // 2
if k - (n - idx - 1) > 1:
s -= (k - (n - idx - 1) - 1) * (k - (n - idx - 1)) // 2
s += k**2
return s
n, m, idx = I()
idx -= 1
m = m - n
l = 0
r = m
while l <= r:
mid = (r - l) // 2 + l
if f(mid) <= m:
l = mid + 1
else:
r = mid - 1
print(r + 1)
|
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER IF BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
places, pillow_num, frodo_place = map(int, input().split())
def calculate_sum(fp, p, length):
if fp == p:
left_sum_min = (p - 1) * p / 2
else:
if p < fp:
left_sum_min = (p - 1) * p / 2 + (fp - 1 - (p - 1))
if p > fp:
left_sum_min = (p - (fp - 1) + p - 1) * (fp - 1) / 2
left_sum_max = (p + 1 + p + fp - 1) * (fp - 1) / 2
fp = length - fp + 1
if fp == p:
right_sum_min = (p - 1) * p / 2
else:
if p < fp:
right_sum_min = (p - 1) * p / 2 + (fp - 1 - (p - 1))
if p > fp:
right_sum_min = (p - (fp - 1) + p - 1) * (fp - 1) / 2
right_sum_max = (p + 1 + p + fp - 1) * (fp - 1) / 2
return left_sum_min + right_sum_min + p, left_sum_max + right_sum_max + p
left = 0
right = pillow_num
while right - left > 1:
middle = left + abs(left - right) // 2
pillows = calculate_sum(frodo_place, middle, places)
if pillows[0] > pillow_num:
right = middle
continue
if pillows[1] < pillow_num:
left = middle
continue
left = middle
def print_pillow(left, right):
if (
calculate_sum(frodo_place, max(left, right), places)[0] <= pillow_num
and calculate_sum(frodo_place, max(left, right), places)[1] >= pillow_num
):
print(max(left, right))
return 0
print(min(left, right))
return 0
print_pillow(left, right)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
n, m, k = map(int, input().split())
a = m - n
if n == 1:
print(m)
elif n == 2:
print((m + 1) // 2)
elif a == 0:
print(1)
else:
l = 0
h = a
ans = 1
while l <= h:
m = (l + h) // 2
if m <= k:
x = m * (m + 1) // 2
else:
z = m - k
x = m * (m + 1) // 2 - z * (z + 1) // 2
if m <= n - k + 1:
y = m * (m + 1) // 2
else:
z = m - (n - k + 1)
y = m * (m + 1) // 2 - z * (z + 1) // 2
if x + y - m <= a:
l = m + 1
ans = m
continue
else:
h = m - 1
continue
if l == h:
break
print(ans + 1)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
inn = input()
data = inn.split()
n = int(data[0])
m = int(data[1])
k = int(data[2])
frodo = 1
pillows = m - n
left = k - 1
right = n - k
R, L = 0, 0
x = 0
while pillows > x:
pillows = pillows - (1 + L + R)
frodo += 1
if left > 0:
L += 1
left -= 1
x += 1
if right > 0:
R += 1
right -= 1
x += 1
if left == 0 and right == 0:
frodo += pillows // (1 + L + R)
break
print(frodo)
|
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
num_hobbits, num_pillows, frodo_pos = map(int, input().split())
num_pillows -= num_hobbits
if num_pillows == 0:
print(1)
elif num_pillows <= 2:
print(2)
else:
left, right = frodo_pos - 1, num_hobbits - frodo_pos
frodo_amt = 1
def f(x):
return 1 + min(x - 1, left) + min(x - 1, right)
last_amt = -1
while True:
this_round_amt = f(frodo_amt)
if this_round_amt == last_amt:
print(frodo_amt + num_pillows // this_round_amt)
break
if this_round_amt > num_pillows:
print(frodo_amt)
break
last_amt = this_round_amt
num_pillows -= this_round_amt
frodo_amt += 1
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER FUNC_DEF RETURN BIN_OP BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
def check(x):
k1 = k - 1
k2 = n - k
p1 = min(k1, x - 1) * (2 * (x - 1) - (min(k1, x - 1) - 1)) // 2
p2 = min(k2, x - 1) * (2 * (x - 1) - (min(k2, x - 1) - 1)) // 2
f = p1 + x + p2
return f <= m - n
n, m, k = map(int, input().split())
l = 0
r = 10**9
while r - l > 1:
M = (l + r) // 2
if check(M):
l = M
else:
r = M
print(l + 1)
|
FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
n, m, k = [int(i) for i in input().strip().split(" ")]
def check(t):
a = t - k + 1
b = t - n + k
pillows = t
if a > 1:
pillows += (t - a) * (a + t - 1) // 2
else:
pillows += 1 - a + t * (t - 1) // 2
if b > 1:
pillows += (t - b) * (b + t - 1) // 2
else:
pillows += 1 - b + t * (t - 1) // 2
return pillows <= m
low = 1
high = m
while low < high:
mid = low + (high - low + 1) // 2
if check(mid):
low = mid
else:
high = mid - 1
print(low)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
n, m, k = [int(i) for i in input().split()]
res = 1
m = m - n
if m > 0:
m -= 1
res += 1
l = k - 1
r = k + 1
t1 = l
t2 = r
while m > 0:
if l <= 0 and r > n:
break
else:
if l == 0:
l += 1
if r > n:
r -= 1
temp = r - l + 1
m -= temp
if m >= 0:
res += 1
if l > 0:
l -= 1
if r <= n:
r += 1
if m > 0:
print(res + m // n)
else:
print(res)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER IF VAR NUMBER VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
import sys
n, m, k = map(int, input().split())
ans = 1
m -= n
l = min(k - 1, n - k)
step = 1
for i in range(l):
if m - step < 0:
print(ans)
sys.exit(0)
m -= step
step += 2
ans += 1
while step < n:
if m - step < 0:
print(ans)
sys.exit(0)
m -= step
step += 1
ans += 1
ans += m // n
print(ans)
|
IMPORT ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
n, m, k = [int(i) for i in input().split()]
def one_sided(slots, x):
if slots >= x - 1:
return slots - x + 1 + (x - 1) * x // 2
else:
return slots * (2 * x - slots - 1) // 2
def pillows_used(x):
left_slot = k - 1
right_slot = n - k
return x + one_sided(left_slot, x) + one_sided(right_slot, x) <= m
def main():
left = 1
right = m
if pillows_used(m):
return m
mid = (left + right) // 2
while left != right:
if pillows_used(mid):
left = mid + 1
else:
right = mid
mid = (left + right) // 2
return mid - 1
print(main())
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER RETURN BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
def f(n, m, k, x):
if x - n + k > 0 and x - k + 1 > 0:
ans = (
(x - 1 + (x - (n - k))) * (n - k) // 2
+ (x - 1 + (x - (k - 1))) * (k - 1) // 2
+ x
)
elif x - n + k <= 0 and x - k + 1 > 0:
ans = (
(x - 1 + 1) * (n - k - (-(x - n + k) + 1)) // 2
+ (x - 1 + (x - (k - 1))) * (k - 1) // 2
+ x
+ (-(x - n + k) + 1)
)
elif x - k + 1 <= 0 and x - n + k > 0:
ans = (
(x - 1 + (x - (n - k))) * (n - k) // 2
+ (x - 1 + 1) * (k - 1 - (-(x - k + 1) + 1)) // 2
+ x
+ (-(x - k + 1) + 1)
)
elif x - k + 1 <= 0 and x - n + k <= 0:
ans = (
(x - 1 + 1) * (n - k - (-(x - n + k) + 1)) // 2
+ (x - 1 + 1) * (k - 1 - (-(x - k + 1) + 1)) // 2
+ x
+ (-(x - n + k) + 1)
+ (-(x - k + 1) + 1)
)
if ans <= m:
return True
else:
return False
n, m, k = map(int, input().split())
l = 1
r = 1000000001
while l + 1 < r:
merg = (l + r) // 2
if f(n, m, k, merg):
l = merg
else:
r = merg
print(l)
|
FUNC_DEF IF BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR IF BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
n, m, k = map(int, input().split())
l = 1
r = m
ans = 0
while l <= r:
mid = (l + r) // 2
maxa = min(mid * n, m)
mini = 0
print
if mid < k:
mini += mid * (mid + 1) // 2 + (k - mid)
else:
mini += mid * (mid + 1) // 2
t = mid - k
mini -= t * (t + 1) // 2
spots = n - k
mid -= 1
if mid < spots:
mini += mid * (mid + 1) // 2 + (spots - mid)
else:
mini += mid * (mid + 1) // 2
t = mid - spots
mini -= t * (t + 1) // 2
mid += 1
if maxa < m:
l = mid + 1
elif mini <= m <= maxa:
ans = max(mid, ans)
l = mid + 1
else:
r = mid - 1
print(ans)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER EXPR VAR IF VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
def f(a):
sums1 = 0
sums2 = 0
if k >= a:
sums1 = a * (a + 1) // 2
sums1 += k - a
else:
sums1 = k * (a + a - k + 1) // 2
sums3 = 0
sums4 = 0
k1 = n - k + 1
if k1 >= a:
sums2 = a * (a + 1) // 2
sums2 += k1 - a
else:
sums2 = k1 * (a + a - k1 + 1) // 2
if sums1 + sums2 - a <= m:
return True
else:
return False
n, m, k = map(int, input().split())
left = 1
right = 10**9 + 5
while left + 1 != right:
mid = (left + right) // 2
if f(mid):
left = mid
else:
right = mid
if f(right):
print(right)
else:
print(left)
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
n, m, k = map(int, input().split())
low = 1
high = m
ans = -1
while low <= high:
mid = low + (high - low) // 2
numReq = 0
if mid > k:
numReq += mid * (mid + 1) // 2 - (mid - k) * (mid - k + 1) // 2
elif mid <= k:
numReq += mid * (mid + 1) // 2 + (k - mid)
if mid == 1:
numReq += n - k
elif mid - 1 > n - k:
numReq += mid * (mid - 1) // 2 - (mid - n + k) * (mid - n + k - 1) // 2
elif mid - 1 <= n - k:
numReq += mid * (mid - 1) // 2 + (n - k - mid + 1)
if numReq > m:
high = mid - 1
else:
low = mid + 1
ans = mid
print(ans)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR IF VAR NUMBER VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
def read(type=int):
return type(input())
def read_arr(type=int):
return [type(token) for token in input().split()]
def runB():
n, m, k = read_arr()
ans = 1
m -= n
l, r = k, k
while (r < n or l > 1) and m > 0:
ans += 1
m -= r - l + 1
if l > 1:
l -= 1
if r < n:
r += 1
ans += m // n
print(ans)
runB()
|
FUNC_DEF VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF VAR RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR WHILE VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.
Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?
-----Input-----
The only line contain three integers n, m and k (1 ≤ n ≤ m ≤ 10^9, 1 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.
-----Output-----
Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.
-----Examples-----
Input
4 6 2
Output
2
Input
3 10 3
Output
4
Input
3 6 1
Output
3
-----Note-----
In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.
In the second example Frodo can take at most four pillows, giving three pillows to each of the others.
In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.
|
def myf(n, m, k):
res = 1
m2 = m - n
left = k
right = k
i = 1
while (left != 1 or right != n) and m2 > 0:
res = res + 1
m2 = m2 - (right - left + 1)
if right < n:
if left > 1:
left -= 1
right += 1
else:
right += 1
elif left > 1:
left -= 1
res = res + m2 // n
return res
n, m, k = map(int, input().split())
res = myf(n, m, k)
print(res)
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL 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())
maps = []
for i in range(N):
a, b = map(int, input().split())
diff = b - a
maps += [[diff, a, b]]
maps.sort()
ans = 0
for i, v in enumerate(maps):
a = v[1]
b = v[2]
j = i + 1
ans += a * (j - 1) + b * (N - j)
print(ans)
|
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 VAR LIST LIST VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER 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())
nums = []
for i in range(n):
nums.append(list(map(int, input().split())))
nums.sort(key=lambda x: x[1] - x[0])
ans = 0
for idx, ele in enumerate(nums):
ans += idx * ele[0] + (n - idx - 1) * ele[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 EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP 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$.
|
t = int(input())
ss = []
for _ in range(0, t):
a, b = map(int, input().split())
ss.append([b - a, a, b])
ss = sorted(ss)
s = 0
for i in range(1, t + 1):
s += ss[i - 1][1] * (i - 1) + ss[i - 1][2] * (t - i)
print(s)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR 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())
people = list()
for i in range(n):
people.append([int(el) for el in input().split()])
disbal = [(i, el[1] - el[0]) for i, el in enumerate(people)]
disbal = sorted(disbal, key=lambda x: x[1])
power = 0
for cur, el in enumerate(disbal):
power += people[el[0]][0] * cur + people[el[0]][1] * (n - cur - 1)
print(power)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER 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 argsort(seq):
return sorted(range(len(seq)), key=seq.__getitem__)
n = int(input())
sta = []
geb = []
for i in range(n):
a = list(map(int, input().split()))
sta.append(a[1] - a[0])
geb.append(a)
sor = argsort(sta)
left = 0
right = n - 1
ans = 0
for i in range(n):
g = sor[i]
ans += geb[g][0] * left + geb[g][1] * right
left += 1
right -= 1
print(ans)
|
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR 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 BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP 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$.
|
n = int(input())
students = []
total = 0
for _ in range(n):
a, b = map(int, input().split())
total += b * n - a
students.append((a, b))
students.sort(key=lambda x: x[0] - x[1])
students.reverse()
for i in range(n):
total += (students[i][0] - students[i][1]) * (i + 1)
print(total)
|
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 EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL 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$.
|
from sys import stdin
input = stdin.readline
n = int(input())
ab = [list(map(int, input().split())) for i in range(n)]
ab.sort(key=lambda x: x[0] - x[1], reverse=True)
ans = 0
for i in range(n):
ans += ab[i][0] * i + ab[i][1] * (n - 1 - i)
print(ans)
|
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 EXPR FUNC_CALL VAR BIN_OP VAR NUMBER 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$.
|
import sys
__version__ = "0.2"
__date__ = "2019-04-19"
def solve(n, ab):
ordered_students = sorted(ab, key=lambda x: x[1] - x[0])
return sum(a * j + b * (n - j - 1) for j, (a, b) in enumerate(ordered_students))
def main(argv=None):
n = int(input())
ab = [list(map(int, input().split())) for student in range(n)]
print(solve(n, ab))
return 0
STATUS = main()
sys.exit(STATUS)
|
IMPORT ASSIGN VAR STRING ASSIGN VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF NONE 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 FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL 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())
values = []
for _ in range(n):
a, b = list(map(int, input().split()))
values.append((a, b))
values.sort(key=lambda x: x[0] - x[1], reverse=True)
diss = sum([(values[i][0] * i + values[i][1] * (n - i - 1)) for i in range(n)])
print(diss)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST 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 VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN 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 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())
que = []
for i in range(n):
a, b = [int(x) for x in input().split()]
que.append((a - b, a, b))
que.sort(reverse=True)
counter = 0
for i in range(n):
counter += que[i][1] * i + que[i][2] * (n - i - 1)
print(counter)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR 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 BIN_OP 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 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())
weights = []
nums = []
for i in range(n):
nums.append([int(x) for x in input().split()])
weights.append(nums[i][0] - nums[i][1])
nums = [x[0] for x in sorted(zip(nums, weights), reverse=True, key=lambda x: x[1])]
cost = 0
for j in range(n):
cost += nums[j][0] * j + nums[j][1] * (n - j - 1)
print(cost)
|
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 VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR 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$.
|
intin = lambda: map(int, input().split())
n = int(input())
ans = 0
x = [0] * n
for i in range(n):
a, b = intin()
x[i] = a - b
ans += n * b - a
x.sort(reverse=True)
for i in range(n):
ans += (i + 1) * x[i]
print(ans)
|
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP 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$.
|
def key(ab):
a, b = ab
return a - b
def main():
n = int(input())
nm = 0
m1 = n - 1
res = 0
for a, b in sorted((tuple(map(int, input().split())) for _ in range(n)), key=key):
res += a * m1 + b * nm
nm += 1
m1 -= 1
print(res)
main()
|
FUNC_DEF ASSIGN VAR VAR VAR RETURN BIN_OP VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER 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())
iparray = []
sumab = 0
for i in range(n):
ip = list(map(int, input().split()))
iparray.append(ip[0] - ip[1])
sumab += ip[1] * n - ip[0]
iparray.sort(reverse=True)
result = 0
for i in range(1, n + 1):
result = result + i * iparray[i - 1]
final = result + sumab
print(final)
|
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 EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL 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 ASSIGN 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
class Main:
def __init__(self):
self.buff = None
self.index = 0
def next(self):
if self.buff is None or self.index == len(self.buff):
self.buff = sys.stdin.readline().split()
self.index = 0
val = self.buff[self.index]
self.index += 1
return val
def next_int(self):
return int(self.next())
def solve(self):
n = self.next_int()
x = sorted(
[(self.next_int(), self.next_int()) for _ in range(0, n)],
key=lambda o: o[1] - o[0],
)
ans = 0
for i in range(0, n):
ans += x[i][0] * i + x[i][1] * (n - i - 1)
print(ans)
Main().solve()
|
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR NUMBER FUNC_DEF IF VAR NONE VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER 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 EXPR FUNC_CALL 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
n = int(sys.stdin.readline().strip())
Q = [0] * n
for i in range(0, n):
line = sys.stdin.readline().strip().split()
a = int(line[0])
b = int(line[1])
Q[i] = [a, b, b - a]
Q.sort(key=lambda x: x[2])
s = 0
for i in range(0, n):
s = s + Q[i][0] * i + Q[i][1] * (n - 1 - i)
print(s)
|
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR LIST VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR 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$.
|
row = int(input())
data = [([0] * 2) for _ in range(row)]
for i in range(row):
arr = input()
arr = [int(x) for x in arr.split(" ")]
for j in range(2):
data[i][j] = arr[j]
data = sorted(data, key=lambda e: e[1] - e[0])
res = 0
for i in range(row):
A = i
B = row - i - 1
res += data[i][0] * A + data[i][1] * B
print(res)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR 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$.
|
n = int(input())
arr = []
ans = 0
for i in range(n):
a, b = map(int, input().split())
arr.append(a - b)
ans += b * n - a
arr = sorted(arr)[::-1]
for i in range(n):
ans += arr[i] * (i + 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 EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR 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$.
|
n = int(input())
arr = [([0] + [int(i) for i in input().split()]) for i in range(n)]
for i in range(n):
arr[i][0] = arr[i][1] - arr[i][2]
arr = sorted(arr)[::-1]
ans = 0
for i in range(n):
ans += i * arr[i][1] + (n - i - 1) * arr[i][2]
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER 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())
ab = [tuple(map(int, input().split())) for i in range(n)]
a = [(k[0] - k[1]) for k in ab]
a.sort()
var = sum([(a[i] * (n - i)) for i in range(n)])
const = sum([(k[1] * n - k[0]) for k in ab])
print(var + const)
|
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 BIN_OP VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR 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$.
|
n = int(input())
q1 = []
for i in range(n):
a1, b1 = map(int, input().split())
q1.append((10**8 - (a1 - b1), a1, b1))
q1.sort()
c = 0
for j in range(n):
a1, b1 = q1[j][1], q1[j][2]
c += a1 * j + b1 * (n - 1 - j)
print(c)
|
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 BIN_OP BIN_OP NUMBER NUMBER BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR 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$.
|
import sys
def main():
count = int(sys.stdin.readline().rstrip("\r\n"))
items = []
summ = 0
i = 0
while i < count:
[l, r] = sys.stdin.readline().rstrip("\r\n").split()
l = int(l)
r = int(r)
items.append([r - l, l, r])
i += 1
items.sort()
i = 0
while i < count:
summ += items[i][1] * i + items[i][2] * (count - i - 1)
i += 1
print(summ)
main()
|
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN LIST VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER 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())
chars = []
for i in range(n):
a, b = map(int, input().strip().split())
chars.append((i, a, b))
new_order = sorted(chars, key=lambda x: x[2] - x[1])
total_dis = 0
for i, e in enumerate(new_order):
total_dis += chars[e[0]][1] * i + chars[e[0]][2] * (n - i - 1)
print(total_dis)
def total_order(p1, p2):
pass
|
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 FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF
|
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):
a, b = [int(x) for x in input().split()]
l += [(a, b, i)]
def keyf(ab):
a, b, i = ab
return -(a - b)
l.sort(key=keyf)
ans = 0
for i in range(n):
ans += (n - 1) * l[i][1] + i * (l[i][0] - l[i][1])
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR LIST VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR RETURN BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR 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$.
|
def main():
n = int(input())
ans = 0
vals = []
for i in range(n):
a, b = map(int, input().split())
ans += b * n
ans -= a
vals.append(a - b)
vals.sort()
k = n
for i in vals:
ans += i * k
k -= 1
print(ans)
main()
|
FUNC_DEF 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 VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FOR VAR VAR VAR BIN_OP VAR 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())
AB = [list(map(int, input().split())) for _ in range(N)]
AB.sort(key=lambda x: x[1] - x[0])
ans = 0
for i in range(N):
a, b = AB[i]
ans += a * i + b * (N - i - 1)
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL 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$.
|
import sys
N = int(input())
alu = 0
A = []
B = []
for _ in range(N):
a, b = map(int, sys.stdin.readline().split())
alu += min(a, b)
if a < b:
B.append(b - a)
else:
A.append(a - b)
alu *= N - 1
A.sort(reverse=True)
B.sort(reverse=True)
alu += sum([(i * j) for i, j in enumerate(A)]) + sum([(i * j) for i, j in enumerate(B)])
print(alu)
|
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR 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$.
|
def cmp(a):
return a[0] - a[1]
n = int(input())
v = []
for i in range(n):
v.append(list(map(int, input().split())))
v = list(sorted(v, key=cmp, reverse=True))
s = 0
for i in range(n):
s = s + v[i][0] * i + v[i][1] * (n - i - 1)
print(s)
|
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 EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR 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 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 in_int():
return int(input())
def in_list():
return [int(x) for x in input().split()]
n = int(input())
ns = []
ans = 0
for i in range(n):
x, y = in_list()
ns.append(x - y)
ans += -x + y * n
ns.sort(reverse=True)
for i in range(n):
c = ns[i]
ans += c * (i + 1)
print(ans)
|
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR 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 EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP 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())
total = 0
arr = []
for i in range(n):
a, b = input().split()
a, b = int(a), int(b)
arr.append(a - b)
total += n * b - a
arr.sort(key=lambda P: -1 * P)
for i in range(n):
total += (i + 1) * arr[i]
print(total)
|
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 FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP 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())
a, b = zip(*[map(int, input().split()) for _ in range(n)])
s = (n - 1) * sum(b)
d = sorted(a[i] - b[i] for i in range(n))[::-1]
for i in range(n):
s += d[i] * i
print(s)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR 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():
n = int(input())
a = list()
b = list()
a_append = a.append
b_append = b.append
split = str.split
for i in range(n):
new_a, new_b = map(int, split(input()))
a_append(new_a)
b_append(new_b)
c = [(a[i] - b[i], i) for i in range(n)]
c = sorted(c, key=lambda item: item[0], reverse=True)
sum = 0
for i in range(n):
index = c[i][1]
sum += a[index] * i + b[index] * (n - i - 1)
print(sum)
solve()
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER 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 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())
a = []
b = []
c = 0
d = 0
for i in range(n):
a.append(list(map(int, input().split())))
b.append(a[i][1] - a[i][0])
c = c + a[i][0]
d = d + a[i][1]
b.sort()
s = 0
for i in range(n):
s = s + b[i] * (i + 1)
s = -1 * s
print(s - c + d * n)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR 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$.
|
n = int(input())
ans = 0
c = []
for i in range(n):
[a, b] = input().strip().split(" ")
a = int(a)
b = int(b)
ans = ans + b * n - a
c.append(b - a)
c = sorted(c)
tem = 0
for i in range(n):
tem = tem + c[i] * (i + 1)
ans = ans - tem
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN LIST VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN 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())
s = []
bs = []
k = 0
for i in range(n):
a, b = map(int, input().split())
bs.append(b)
s.append(a - b)
z = sorted(zip(s, bs))
s = [x for x, y in z][::-1]
bs = [y for x, y in z][::-1]
for i in range(n):
k += s[i] * i + bs[i] * (n - 1)
print(k)
|
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 BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP 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$.
|
y = []
a = int(input())
for i in range(a):
temp = list(map(int, input().split()))
temp.append(temp[1] - temp[0])
y.append(temp)
final = sorted(y, key=lambda x: x[2])
ans = 0
for i in range(a):
ans += final[i][0] * i + final[i][1] * (a - 1 - i)
print(ans)
|
ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL 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 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
sys.setrecursionlimit(20000000)
input = sys.stdin.readline
n = int(input())
a = [list(map(int, input().split())) for i in range(n)]
for i in range(n):
a[i].append(a[i][0] - a[i][1])
a.sort(key=lambda x: -x[2])
ans = 0
for i in range(n):
ans += a[i][0] * i + a[i][1] * (n - i - 1)
print(ans)
|
IMPORT EXPR FUNC_CALL VAR NUMBER 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 FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL 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_ = 100010
n = int(input())
a = []
b = []
d = []
sum = 0
for i in range(n):
ai, bi = list(map(int, input().split()))
a.append(ai)
b.append(bi)
d.append(ai - bi)
sum += bi * n - ai
d.sort()
d = d[::-1]
for i in range(n):
sum += d[i] * (i + 1)
print(sum)
|
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST 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 VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.