description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
def bi(a, x): l = -1 u = len(a) while u - l > 1: md = int((l + u) / 2) if x >= a[md]: l = md else: u = md return u - 1 n, k = map(int, input().split()) a = list(map(int, input().split())) a = sorted(a) diff = [0] * (n // 2 + 1) for i in range(1, n // 2 + 1): diff[i] = (a[i + n // 2] - a[i + n // 2 - 1]) * i + diff[i - 1] index = bi(diff, k) print(a[n // 2 + index] + (k - diff[index]) // (index + 1))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
y = lambda: map(int, input().split()) n, k = y() a = sorted(y()) + [(1 << 31) - 1] d = 1 i = n // 2 v = a[i] while k >= d: while d < n - i and v == a[i + d]: d += 1 r = min(k // d, a[i + d] - v) v += r k -= d * r print(v)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR LIST BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR WHILE VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
def read_ints(): return map(int, input().split(" ")) n, k = read_ints() a = list(read_ints()) a.sort() idx = n // 2 length = 1 ans = a[idx] while idx < n - 1 and k >= length: nxt = a[idx + 1] delta = min(nxt - a[idx], k // length) k -= delta * length ans = a[idx] + delta idx += 1 length += 1 if idx == n - 1 and k >= length: ans = a[idx] + k // length print(ans)
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
line = input() tmp1 = line.split(" ") n, m = int(tmp1[0]), int(tmp1[1]) line1 = input() num = [int(i) for i in line1.split(" ")] num = sorted(num) j = n // 2 r = n // 2 + 1 while m and r < n: if num[j] == num[r]: r += 1 else: t = min(m, (num[r] - num[j]) * (r - j)) num[j] += t // (r - j) m -= t if num[j] == num[n - 1]: print(num[j] + m // (n - j)) else: print(num[j] + min(m, (num[r] - num[j]) * (r - j)) // (r - j))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER WHILE VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = map(int, input().split()) a = list(map(int, input().split())) a.sort() a.reverse() def ok(center): global a, k, n kneed = 0 for I in a[0 : n // 2 + 1]: kneed += max(0, center - I) if kneed <= k: return True else: return False L = 0 R = a[0] + k + 1 while R - L > 1: M = (L + R) // 2 if ok(M): L = M else: R = M print(L)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER 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
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = [int(i) for i in input().split()] l = [int(i) for i in input().split()] l.sort() def actually_ok(l, m, kk): k = kk for i in range(len(l) - 1, int(n / 2) - 1, -1): if l[i] > m: return False k -= m - l[i] if k < 0: return False else: return True def ok(l, m, kk): k = kk for i in range(len(l) - 1, int(n / 2) - 1, -1): k -= max(m - l[i], 0) if k < 0: return False else: return True left = l[int(n / 2)] right = l[int(n / 2)] + k mid = (left + right) / 2 while right - left > 1: mid = int((left + right) / 2) if ok(l, mid, k): left = mid else: right = mid if ok(l, right, k): print(right) else: print(left)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR RETURN NUMBER VAR BIN_OP VAR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = map(int, input().split()) assert n % 2 == 1 arr = sorted(map(int, input().split())) arr = arr[n // 2 :] med = arr[0] for i in range(1, len(arr)): if k == 0: break if arr[i] == med: continue else: diff = arr[i] - med kdiff = min(diff * i, k) med += kdiff // i k -= kdiff print(med + k // len(arr))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = map(int, input().split()) a = sorted(list(map(int, input().split()))) mid = n // 2 if n == 1: print(a[0] + k) exit() t = k k -= min(a[mid + 1] - a[mid], k) if k == 0: print(a[mid] + t) exit() a[mid] += a[mid + 1] - a[mid] c = 1 for i in range(mid + 1, n): if a[i] == a[mid]: c += 1 else: r = a[i] - a[mid] yy = r * c t = k k -= min(yy, k) if k == 0: a[mid] += t // c else: c += 1 a[mid] = a[i] if k != 0: a[mid] += k // c print(a[mid])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = map(int, input().split(" ")) arr = [int(i) for i in input().split(" ")] arr.sort() l, r = arr[n // 2], int(2000000000.0 + 5) while l < r: mid = (l + r + 1) // 2 reqd = 0 for i in range(n // 2, n): reqd += max(mid - arr[i], 0) if reqd > k: r = mid - 1 else: l = mid print(l)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = map(int, input().strip().split()) a = list(map(int, input().strip().split())) a.sort() m = int(n // 2) flag = 0 v = a[m] r = 0 for i in range(m + 1, n): p = r + (a[i] - a[i - 1]) * (i - m) if p < k: r = p else: v = a[i - 1] + int((k - r) // (i - m)) flag = 1 break if flag == 0: v = a[n - 1] + int((k - r) // (n - m)) print(v)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, m = map(int, input().split()) A = list(map(int, input().split())) t = n // 2 s = m i = 1 k = 0 A.sort() while t < n: s += A[t] k = s // i if t != n - 1 and k <= A[t + 1]: break t += 1 i += 1 print(k)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR WHILE VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = list(map(int, input().split())) arr = list(map(int, input().split())) arr.sort() curr = arr[n // 2] count = 1 for i in range(n // 2 + 1, n): diff = arr[i] - curr if diff and diff * count <= k: k -= diff * count curr += diff elif diff and diff * count > k: curr += k // count k = 0 break count += 1 curr = curr + k // ((n + 1) // 2) print(curr)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = map(int, input().split()) data = list(map(int, input().split())) data.sort() mid = n // 2 arr = data[mid:] mid = arr[0] for i in range(1, len(arr)): count = arr[i] - arr[i - 1] if k >= count * i: k -= count * i mid += count else: mid += k // i k = 0 if k: mid += k // len(arr) print(mid)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = map(int, input().split()) l = list(map(int, input().split())) def median(arr, k): arr.sort() n = (len(arr) - 1) // 2 arr = arr[n:] while k > 0: m = arr.count(arr[0]) if m == len(arr): return arr[0] + k // m x = arr[m] ecart = x - arr[0] if m * ecart <= k: arr = [x] * m + arr[m:] k -= m * ecart else: return arr[0] + k // m return arr[0] print(median(l, k))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST VAR VAR VAR VAR VAR BIN_OP VAR VAR RETURN BIN_OP VAR NUMBER BIN_OP VAR VAR RETURN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = map(int, input().split()) a = list(map(int, input().split())) a.sort() mid = n // 2 for i in range(mid, n - 1): if k - (a[i + 1] - a[i]) * (i - mid + 1) >= 0: k -= (a[i + 1] - a[i]) * (i - mid + 1) else: exit(print(k // (i - mid + 1) + a[i])) print(k // (n - mid) + a[-1])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR NUMBER
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
I = lambda: map(int, input().split()) n, k = I() a = sorted(I()) q, w, z = n // 2, 1, a[n // 2] for i in range(q, n - 1): t = (a[i + 1] - a[i]) * w if k >= t: k -= t z = a[i + 1] w += 1 else: break print(z + k // w)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = map(int, input().split()) numbers = [int(i) for i in input().split()] numbers.sort() l = numbers[0] r = numbers[-1] + k m = n // 2 while l <= r: mid = (l + r) // 2 tmp = 0 for i in numbers[m:]: if i < mid: tmp += mid - i else: break if tmp <= k: l = mid + 1 else: r = mid - 1 print(l - 1)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
def valid(a, mid, n, k): tot = 0 x = int((n - 1) / 2) for i in range(x, n): tot += max(0, mid - a[i]) if tot <= k: return True return False n, k = [int(x) for x in input().split()] a = [int(x) for x in input().split()] a.sort() s = 0 e = 10000000000 while s <= e: mid = int((s + e) / 2) fb = valid(a, mid, n, k) sb = valid(a, mid + 1, n, k) if fb == True and sb == False: print(mid) break elif sb == True: s = mid + 1 else: e = mid - 1
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = input().split() intn = int(n) intk = int(k) a = list(map(int, input().strip().split()))[:intn] a.sort() l = 2000000000 s = 0 ans = 0 while s <= l: mid = (s + l) // 2 su = 0 for i in range(intn // 2, intn): su = su + max(0, mid - a[i]) if su > intk: l = mid - 1 else: ans = mid s = mid + 1 print(ans)
ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP 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
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, m = [int(i) for i in input().split()] arr = [int(i) for i in input().split()] arr.sort() mid = n // 2 dp = [0] * n half = 0 for i in range(mid, n): if i == mid: dp[i] = arr[i] + m half += dp[i] else: dp[i] = arr[i] if dp[i] >= dp[i - 1]: break else: div = i - mid + 1 half += dp[i] dp[i] = half // div dp[mid] = half // div rem = half % div dp[i] += 1 print(dp[mid])
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
def check(mid, k, n): cnt = 0 for i in range(n // 2, n): cnt += mid - ar[i] if cnt > k: return False if cnt <= k: return True else: return False def solve(n, k, ar): ar.sort() l = 1 h = 2000000000 while l != h: mid = (l + h + 1) // 2 if check(mid, k, n) is False: h = mid - 1 else: l = mid print(l) n, k = map(int, input().split()) ar = list(map(int, input().split())) solve(n, k, ar)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n_k = input() n_k = n_k.split() n = int(n_k[0]) k = int(n_k[1]) A = input() A = A.split() i = 0 while i < n: A[i] = int(A[i]) i = i + 1 A.sort() p = int(n / 2) i = 0 lim = int(n / 2) + 1 result = A[p] path = 0 while p < n: m = 1 while p + 1 < n and A[p + 1] == A[p]: p = p + 1 m = m + 1 path = path + m p = p + 1 z = int(k / path) if p < n: d = A[p] - A[p - 1] if z >= d: k = k - d * path result = result + d elif z == 0: break else: k = k - z * path result = result + z elif p == n: result = result + z print(result)
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 ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n = list(map(int, input().split())) in_arr = [int(x) for x in input().split()] in_arr.sort() arr = [] for i in range(int(n[0] / 2), n[0]): arr.append(in_arr[i]) l = 0 r = 10000000001 while r - l > 1: mid = int((r + l) / 2) total = 0 for i in arr: total += max(0, mid - i) if total <= n[1]: l = mid else: r = mid print(l)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = map(int, input().split()) def find(m, k, a): for i in range(n): if k == 0: break if a[i] < m: temp = min(k, m - a[i]) a[i] += temp k -= temp else: break if a[0] == m: for i in range(1, n): if not a[i] >= a[i - 1]: return False return True return False a = sorted([int(x) for x in input().split()])[int(n / 2) :] n = len(a) l = a[0] h = a[n - 1] + k // n ans = 0 while l <= h: m = (l + h) // 2 if find(m, k, a.copy()): ans = max(ans, m) l = m + 1 else: h = m - 1 print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL 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
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = map(int, input().split()) a = sorted(map(int, input().split())) if n == 1: print(a[0] + k) exit() y = n // 2 ans = a[y] while True: for y in range(y + 1, n): if a[y] != ans: break else: r = n - n // 2 ans += k // r print(ans) break r = y - n // 2 if ans + k // r >= a[y]: k -= (a[y] - ans) * r ans = a[y] else: ans += k // r print(ans) break
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR WHILE NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = input().split() n = int(n) k = int(k) w = input().split() w = [int(i) for i in w] if n == 1: print(w[0] + k) else: w = sorted(w) idx = len(w) // 2 + 1 mul = 1 gain = 0 for i in range(idx - 1, len(w) - 1): d = w[i + 1] - w[i] if k > d * mul: k = k - d * mul gain += d else: gain += k // mul k = k - d * mul break mul += 1 if k > 0: gain += k // mul print(w[idx - 1] + gain)
ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
N, k = map(int, input().split()) a = list(map(int, input().split())) a = sorted(a) def calc(x): num = 0 for i in range(N // 2, N): num += max(x - a[i], 0) if num > k: return False return num <= k l, r = 0, 10**9 * 3 while r - l > 1: mid = (l + r) // 2 if calc(mid): l = mid else: r = mid print(l)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN VAR VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP NUMBER 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
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = map(int, input().split()) s = list(map(int, input().split())) s.sort() for i in range(n // 2, n - 1): if k >= (s[i + 1] - s[i]) * (i + 1 - n // 2): k -= (s[i + 1] - s[i]) * (i + 1 - n // 2) else: print(s[i] + k // (i + 1 - n // 2)) break else: print(s[-1] + k // (n - n // 2))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = map(int, input().split()) A = list(map(int, input().split())) A.sort() lena = len(A) medInd = lena // 2 res = A[medInd] count = 1 while k > 0: if count <= medInd: fin = A[medInd + count] if k < (fin - res) * count: res += k // count break else: k -= (fin - res) * count res = fin count += 1 else: res += k // count break print(res)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR IF VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = map(int, input().split()) a = sorted(list(map(int, input().split()))) low = a[n // 2] high = 2 * 10**9 + 5 def ok(x): r = 0 for i in range(n // 2, n): if a[i] < x: r += x - a[i] return r <= k while low + 1 < high: mid = (low + high) // 2 if ok(mid): low = mid else: high = mid print(low)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR RETURN VAR VAR 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
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
def cond(a, n, k, v): s = 0 for i in range(n // 2, n): s += max(0, v - a[i]) return s <= k def bin_search(a, n, k): left, right = -1, 10**18 while right - left > 1: mid = (right + left) // 2 if cond(a, n, k, mid): left = mid else: right = mid if left < 0: return right return left def main(): n, k = map(int, input().split()) numbers = list(map(int, input().split())) numbers.sort() print(bin_search(numbers, n, k)) main()
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP NUMBER NUMBER 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 IF VAR NUMBER RETURN VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = map(int, input().split()) ar = list(map(int, input().split())) ar.sort() med = ar[(n - 1) // 2] flag = True for i, j in enumerate(range((n + 1) // 2, n), 1): pr = ar[j] - ar[j - 1] if k >= pr * i: med = ar[j] k -= pr * i elif pr != 0: med = ar[j - 1] + k // i flag = False break if flag: med = ar[-1] + k // ((n + 1) // 2) print(med)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
ans = 0 n, k = [int(x) for x in input().split()] a = [int(x) for x in input().split()] def binarysearch(l: int, r: int): global ans while l <= r: mid = (l + r) // 2 sum = 0 for i in range(n // 2, n): sum += max(0, mid - a[i]) if sum <= k: l = mid + 1 ans = mid else: r = mid - 1 a.sort() binarysearch(0, a[n // 2] + k) print(ans)
ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER BIN_OP 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 EXPR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = map(int, input().split()) a = list(map(int, input().split())) a.sort() m = a[n // 2] p = 1 q = n // 2 + 1 e = True t = True if n == 1: m += k k = 0 q = 0 while a[q] == m: p += 1 q += 1 if q == n: q -= 1 e = False break if e == False: m += k // p k = 0 while k > 0: if k - (a[q] - m) * p < 0: t = False break k -= (a[q] - m) * p m = a[q] while a[q] == m: p += 1 q += 1 if q == n: q -= 1 e = False break if e == False: if k > 0: m += k // p k = 0 break if t == False: m += k // p print(m)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = tuple(map(int, input().split())) a = list(map(int, input().split())) a.sort() def ok(x): mv = int(0) for i in range(n // 2, n): if x - a[i] > 0: mv += x - a[i] if mv > k: return False if mv <= k: return True else: return False mx = 2000000000 mn = 1 while mn < mx: md = (mn + mx + 1) // 2 if ok(md): mn = md else: mx = md - 1 print(mn)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER 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
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
import sys input = sys.stdin.readline N, K = map(int, sys.stdin.readline().split()) A = list(map(int, sys.stdin.readline().split())) A.sort() med = A[N // 2] n = K key = N // 2 num = 1 if N > 1: while key < N - 1 and n >= (A[key + 1] - A[key]) * num: med += A[key + 1] - A[key] n -= (A[key + 1] - A[key]) * num key += 1 num += 1 if key == N - 1: med += n // (N // 2 + 1) else: med += n // num else: med += K print(med)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, m = map(int, input().split()) lis = sorted(map(int, input().split())) ind = n // 2 mid = lis[ind] i = ind + 1 c = 1 while m > 0 and i < n: k = lis[i] - mid if k == 0: c += 1 else: aa = min(m // c, k) m -= aa * c mid += aa c += 1 i += 1 mid += m // (ind + 1) print(mid)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = map(int, input().split()) a = sorted(list(map(int, input().split()))) res = a[n // 2] for i in range(n // 2 + 1, n): d = min(a[i] - res, k // (i - n // 2)) res += d k -= d * (i - n // 2) res += k // (n // 2 + 1) print(res)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = (int(i) for i in input().split()) a = sorted([int(i) for i in input().split()])[n // 2 :] m = 0 for i in range(n - n // 2): if i * (a[i] - m) <= k: k -= i * (a[i] - m) m = a[i] else: i -= 1 break if k // (i + 1) > 0: m += k // (i + 1) print(m)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = map(int, input().split()) arr = list(map(int, input().split())) arr.sort() sum = 0 cnt = 0 ans = arr[n // 2] for i in range(n // 2, n): sum += arr[i] cnt += 1 check = (sum + k) // cnt if check >= arr[i] and i == n - 1: ans = max(ans, check) break if check >= arr[i] and check <= arr[i + 1]: ans = max(ans, check) print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = map(int, input().split()) arr = list(map(int, input().split())) if n == 1: print(arr[0] + k) else: res = 0 arr.sort() a = arr[n // 2 :] diff = [] for i in range(1, len(a)): diff.append(a[i] - a[i - 1]) l = 0 while k > 0 and l < len(diff): if k > diff[l] * (l + 1): k -= diff[l] * (l + 1) l += 1 else: res = a[l] + k // (l + 1) k = 0 if k > 0: res = a[~0] + k // len(a) print(res)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = map(int, input().split()) array = list(map(int, input().split())) array.sort() med = n // 2 def key(m): s = 10**10 ind = None for i in range(n): if 0 <= m - array[i] <= s: s = m - array[i] ind = i if ind == None: return False if ind < med: return False else: for i in range(ind - 1, med - 1, -1): s += m - array[i] return s <= k left = array[med] right = 10**10 while right - left != 1: mid = (right + left) // 2 if key(mid): left = mid else: right = mid print(left)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR IF NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR IF VAR NONE RETURN NUMBER IF VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR RETURN VAR VAR ASSIGN VAR VAR VAR 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
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = list(map(int, input().split())) a = list(map(int, input().split())) if n == 1: print(a[0] + k) exit(0) a = sorted(a) a = a[int(n / 2) :] + [10**10] n = int(n / 2) + 2 m, next = a[0], a[1] ans, nans = m, 1 while k > 0: dx = k / nans dy = next - ans if dx <= dy: ans = ans + dx break k -= dy * nans ans = next nans += 1 next = a[nans] print(int(ans))
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER LIST BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
def num_operations(daList, targetNum, numOpp): num = 0 for i in range(len(daList) // 2, len(daList)): num += (targetNum - daList[i]) * (daList[i] < targetNum) return num <= numOpp def binary_search(low, high, givenList, numOpp): res = low - 1 while low <= high: mid = (low + high) // 2 if num_operations(givenList, mid, numOpp): res = mid low = mid + 1 else: high = mid - 1 return res numEle, numOpp = map(int, input().split()) numbers = sorted(map(int, input().split())) print( binary_search(numbers[numEle // 2], numbers[numEle // 2] + numOpp, numbers, numOpp) )
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
import sys n, k = [int(x) for x in input().split()] lis = [int(x) for x in input().split()] lis = sorted(lis) middle_ind = n // 2 middle_value = lis[middle_ind] for i in range(middle_ind, n - 1): if k - (lis[i + 1] - lis[i]) * (i - middle_ind + 1) < 0: print(lis[i] + k // (i - middle_ind + 1)) sys.exit() k = k - (lis[i + 1] - lis[i]) * (i - middle_ind + 1) print(lis[n - 1] + k // (middle_ind + 1))
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
def find(A, K): A = sorted(A) def findout(target, K): want = 0 for x in A: want += max(0, target - x) if want <= K: return True return False A = A[len(A) // 2 :] left = min(A) right = left + K + 1 while right - left > 1: mid = (left + right) // 2 if findout(mid, K): left = mid else: right = mid if findout(left + 1, K): return left + 1 else: return left n, k = list(map(int, input().strip().split(" "))) A = list(map(int, input().strip().split(" "))) print(find(A, k))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
[n, k] = list(map(int, input().split())) a = list(map(int, input().split())) a.sort() ps = [0] for i in range(n): ps.append(ps[-1] + a[i]) ans = 0 for i in range(n // 2, n): ts = ps[i + 1] - ps[n // 2] ts = ts + k avg = ts // (i - n // 2 + 1) try: if avg > a[i + 1]: continue else: ans = avg break except: ans = avg continue print(ans)
ASSIGN LIST VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = map(int, input().split()) A = [int(x) for x in input().split()] A.sort() Fixed = n // 2 i = n // 2 + 1 if n == 1: print(A[0] + k) exit() if k - (i - Fixed) * (A[i] - A[Fixed]) < 0: print(A[Fixed] + k) exit() x = 0 while 1: if k - (i - Fixed) * (A[i] - A[Fixed]) < 0: x = k // (i - Fixed) break k -= (i - Fixed) * (A[i] - A[Fixed]) A[Fixed] = A[i] i += 1 if i == n: x = k // (n // 2 + 1) break print(A[Fixed] + x)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR IF BIN_OP VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE NUMBER IF BIN_OP VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
valuenum, operationnum = [int(x) for x in input().split(" ")] values = [int(x) for x in input().split(" ")] values.sort() req = 0 lastmax = values[(valuenum - 1) // 2] canfill = True inclusions = 0 for i in range(1, (valuenum - 1) // 2 + 1): inclusions = i if values[(valuenum - 1) // 2 + i] > lastmax: if req + (values[(valuenum - 1) // 2 + i] - lastmax) * i <= operationnum: req += (values[(valuenum - 1) // 2 + i] - lastmax) * i lastmax = values[(valuenum - 1) // 2 + i] else: canfill = False break if canfill: print(values[-1] + (operationnum - req) // ((valuenum - 1) // 2 + 1)) else: print(lastmax + (operationnum - req) // inclusions)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR IF VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR IF BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
import sys def binary_search(start, end, array, n, k): solution = 0 while start <= end: med = (start + end) // 2 count = 0 for idx in range((n + 1) // 2 - 1, n): count += max(med - array[idx], 0) if count <= k: start = med + 1 solution = max(solution, med) else: end = med - 1 return solution def main(): sys.setrecursionlimit(10**6) N, K = list(map(int, input().split())) ARRAY = list(map(int, input().split())) ARRAY = sorted(ARRAY) sol = binary_search(min(ARRAY), max(ARRAY) + K, ARRAY, N, K) print(sol) main()
IMPORT FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = map(int, input().split()) a = list(map(int, input().split())) a.sort() med = a[n // 2 :] i = 0 mn = med[0] for i in range(len(med) - 1): if k >= (i + 1) * (med[i + 1] - med[i]): k -= (i + 1) * (med[i + 1] - med[i]) mn = med[i + 1] else: mn += k // (i + 1) k = 0 break mn += k // len(med) print(mn)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = map(int, input().split()) arr = [int(i) for i in input().split()] arr.sort() arr_size = len(arr) arr_start = int(arr_size / 2) if n == 1: flag = True else: flag = False m = 1 sum_k = 0 max_num = arr[arr_start] while arr_start < arr_size - 1: temp = arr[arr_start + 1] - arr[arr_start] sum_k = sum_k + temp * m max_num = max_num + temp if sum_k > k: if (sum_k - k) % m: max_num -= int((sum_k - k) / m) + 1 else: max_num -= int((sum_k - k) / m) break if sum_k == k: break m += 1 arr_start += 1 if arr_start == arr_size - 1: flag = True if flag: max_num += int((k - sum_k) / m) print(max_num)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
class Read: @staticmethod def int(): return int(input()) @staticmethod def list(sep=" "): return input().split(sep) @staticmethod def list_int(sep=" "): return list(map(int, input().split(sep))) def solve(): n, k = Read.list_int() a = Read.list_int() a.sort() m_index = n // 2 a = a[m_index:] current_m = a[0] index = 0 while True: next_el = current_m while next_el <= current_m: index += 1 if index == len(a): print(k // len(a) + current_m) return next_el = a[index] k_m = (next_el - current_m) * index if k_m <= k: k -= k_m current_m = next_el else: print(k // index + current_m) return query_count = 1 for j in range(query_count): solve()
CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF STRING RETURN FUNC_CALL FUNC_CALL VAR VAR VAR FUNC_DEF STRING RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR RETURN ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR RETURN ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = map(int, input().split()) a = sorted(list(map(int, input().split()))) s = [] med = a[n // 2] for i in range(n // 2): s.append((a[n // 2 + 1 + i] - a[n // 2 + i]) * (i + 1)) for i in range(n // 2): if k <= s[i]: med += k // (i + 1) k = 0 else: med = a[n // 2 + i + 1] k -= s[i] print(med + k // (n // 2 + 1))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = map(int, input().split()) l = [int(x) for x in input().split()] l.sort() def bs(m): moves = abs(l[n // 2] - m) ma = m for i in range(n // 2 + 1, n): ma = max(l[i], ma) if l[i] < ma: moves += abs(l[i] - ma) if moves <= k: return True else: return False if n == 1: print(l[0] + k) else: lo, hi = l[n // 2], max(l) + k while lo <= hi: m = lo + (hi - lo) // 2 if l[n // 2] >= m: lo = m + 1 elif bs(m): ans = m lo = m + 1 else: hi = m - 1 print(lo - 1)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = map(int, input().split()) a = sorted(map(int, input().split())) if n < 2: print(a[0] + k) exit() i = n // 2 while k >= (i - n // 2 + 1) * (a[i + 1] - a[i]): diff = (i - n // 2 + 1) * (a[i + 1] - a[i]) if diff: k -= diff a[n // 2 : i + 1] = [a[i + 1]] * (i - n // 2 + 1) i += 1 if i > n - 2: break print(a[n // 2] + k // (i - n // 2 + 1))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP LIST VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
def do(arr, k): n = len(arr) arr = arr[n // 2 :] n -= n // 2 i = 0 while k and i < n - 1: while i < n - 1 and arr[i + 1] == arr[i]: i += 1 if i == n - 1: break if (arr[i + 1] - arr[i]) * (i + 1) <= k: k -= (arr[i + 1] - arr[i]) * (i + 1) else: return arr[i] + k // (i + 1) i += 1 return arr[i] + k // (i + 1) k = int(input().split()[1]) arr = list(map(int, input().split())) print(do(sorted(arr), k))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER RETURN BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = map(int, input().split()) x = sorted(map(int, input().split()))[n // 2 :] r = x[0] for i in range(1, len(x)): if i * (x[i] - r) <= k: k -= i * (x[i] - r) r = x[i] else: r += k // i break else: r += k // len(x) print(r)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = list(map(int, input().split())) arr = list(map(int, input().split())) arr.sort() n //= 2 arr = arr[n:] n += 1 med = arr[0] flag = 1 while flag < n: if arr[flag] > med: kk = (arr[flag] - med) * flag if kk < k: k -= kk med = arr[flag] else: med += k // flag k = 0 break flag += 1 if flag == n: med += k // flag print(med)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = map(int, input().split()) l = [float("inf")] + sorted(map(int, input().split())) + [float("inf")] i = n // 2 + 2 I = (n + 1) // 2 cnt = 1 while k: while l[I] == l[i]: i += 1 cnt += 1 d = min((l[i] - l[I]) * cnt, k) l[I] += d // cnt k -= d print(l[I])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP LIST FUNC_CALL VAR STRING FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR LIST FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR WHILE VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
l = [int(i) for i in input().split(" ")] n = l[0] k = l[1] l = [int(i) for i in input().split(" ")] l = sorted(l) med = (n - 1) // 2 val = l[med] end = False vl = 0 s = 1 vals = [] for i in range(med + 1, len(l)): if l[i] == l[i - 1]: s += 1 continue knum = s if k >= (l[i] - l[i - 1]) * s: k -= (l[i] - l[i - 1]) * s vl += l[i] - l[i - 1] else: vl += k // s k = k % s break s += 1 vl += k // s print(vl + val)
ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
def rint(): return int(input()) def rints(): return list(map(int, input().split())) n, k = rints() a = rints() a.sort() c = 1 m = a[n // 2] while c <= n // 2 and k > 0: ne = a[n // 2 + c] if (ne - m) * c > k: break k -= (ne - m) * c m = ne c += 1 print(m + k // c)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
input1 = input() n, k = input1.split(" ") n = int(n) k = int(k) input2 = input() a = input2.split(" ") for t in range(n): a[t] = int(a[t]) if len(a) == 1: print(a[0] + k) else: a.sort() mid = int((n - 1) / 2) c = mid diff = [] if a[mid + 1] - a[mid] >= k: print(a[mid] + k) else: len = mid copyk = k flag = 1 copyt = 0 for t in range(len): copykprev = copyk copyk = copyk - (a[t + mid + 1] - a[t + mid]) * (t + 1) if copyk <= 0: flag = 0 copyt = t + mid break if flag == 1: ans = a[n - 1] + int(copyk / (n - mid)) print(ans) else: print(a[copyt] + int(copykprev / (t + 1)))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR LIST IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
from itertools import accumulate n, k = map(int, input().split()) a = list(map(int, input().split())) if len(a) == 1: print(a[0] + k) else: a.sort() a = a[len(a) // 2 :] b = list(accumulate(a)) mx = 0 if a[0] + k <= a[1]: mx = a[0] + k for i in range(1, len(a) - 1): lmax = a[i + 1] if (b[i] + k) // (i + 1) + 1 < a[i]: break elif lmax * (i + 1) - b[i] - k > i + 1: mx = max(mx, (b[i] + k) // (i + 1)) break elif b[i] + k < lmax * (i + 1): mx = max(mx, (b[i] + k) // (i + 1)) else: mx = max(mx, lmax) else: if b[-1] + k >= a[-1] * len(a): mx = max(mx, (b[-1] + k) // len(a)) print(mx)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR IF BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = map(int, input().strip().split()) l = list(map(int, input().strip().split())) l.sort() mid = int((n - 1) / 2) s = 0 mx = l[n - 1] ans = -1 def check(start, end, l): mx = l[end] ret = 0 for i in range(start, end): ret += mx - l[i] return ret for i in range(mid, n - 1): s += mx - l[i] if k >= s: k -= s ans = mx + k // (mid + 1) else: r = n - 1 le = mid while r - le > 1: m = (r + le) // 2 if check(mid, m, l) <= k: le = int(m) else: r = int(m) last = -1 if check(mid, r, l) <= k: last = r else: last = le k -= check(mid, last, l) num = last - mid + 1 ans = l[last] + k // num print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP 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 FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = [int(i) for i in input().split(" ")] a = [int(i) for i in input().split(" ")] a.sort() b = a[n // 2 :] def p(m): ans = 0 for s in b: if m <= s: break else: ans += m - s return ans <= k low = b[0] high = b[0] + k while low < high: mid = low + (high - low + 1) // 2 if p(mid): low = mid else: high = mid - 1 print(low)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER 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
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = map(int, input().split()) mass = [int(i) for i in input().split()] mass.sort() mass = mass[(n - 1) // 2 :] lent = (n - 1) // 2 + 1 i = 0 num = 1 myx = mass[0] while k > 0: i += 1 if i == lent: myx += k // num break maxx = mass[i] k -= num * (maxx - myx) if k <= 0: k += num * (maxx - myx) myx = myx + k // num break else: myx = maxx num += 1 print(myx)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
def main(): n, k = map(int, input().split()) a = list(map(int, input().split())) a.sort() mid = n // 2 i = mid + 1 final_val = a[mid] while i < n and k > 0: if a[mid] != a[i]: if k >= (a[i] - a[mid]) * (i - mid): k -= (a[i] - a[mid]) * (i - mid) final_val = a[i] a[mid] = a[i] else: final_val += k // (i - mid) k = 0 break i += 1 if k > 0 and i == n: final_val += k // (i - mid) print(final_val) main()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR VAR NUMBER IF VAR VAR VAR VAR IF VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = [int(s) for s in input().split()] arr = sorted([int(s) for s in input().split()]) arr = arr[::-1] arr = [pow(10, 11)] + arr[0 : n // 2 + 1] n = len(arr) point = n - 1 while k != 0: t = min(k, (n - point) * (arr[point - 1] - arr[point])) k -= t arr[point] += t // (n - point) if k != 0: point -= 1 print(arr[point])
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST FUNC_CALL VAR NUMBER NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
R = lambda: map(int, input().split()) n, k = R() L = sorted(R()) if n == 1: print(L[0] + k) quit() i = n // 2 m = L[i] s = 0 T = [0] i += 1 l = 0 while i < n: p = s t = L[i] - L[i - 1] s += t s += t * l T.append(s) i += 1 l += 1 j = 0 while j < len(T): if k < T[j]: break j += 1 k -= T[j - 1] m = L[n // 2 + j - 1] if k > 0: m += k // j print(m)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = map(int, input().split()) a = list(map(int, input().split())) a.sort() a.insert(0, 0) def F(m): s = 0 for i in range(n // 2 + 1, n + 1): s += max(0, m - a[i]) return s <= k l = a[n // 2 + 1] r = 100000000000 while l <= r: m = (l + r) // 2 if F(m): res = m l = m + 1 else: r = m - 1 print(res)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR RETURN VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
def bs(l, h): while l < h: m = (l + h + 1) // 2 if gf(m): l = m else: h = m - 1 return l def gf(x): s = 0 for i in a[m:]: s += max(0, x - i) return s <= k n, k = map(int, input().split()) a = sorted(list(map(int, input().split()))) m = n // 2 print(bs(a[m], a[m] + k))
FUNC_DEF 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 RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR RETURN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
from sys import stdin n, k = (int(i) for i in stdin.readline().split()) arr = sorted(int(i) for i in stdin.readline().split()) bot = arr[n // 2] top = arr[-1] def double(alpha, beta): if beta == alpha + 1: if limit(beta) > k: return alpha else: return beta middle = (alpha + beta) // 2 A = limit(middle) if A > k: return double(alpha, middle) if A < k: return double(middle, beta) if A == k: return middle def limit(mid): B = 0 for i in arr[n // 2 :]: if i >= mid: break B += mid - i return B print(double(bot, top + k))
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_DEF IF VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN VAR RETURN VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR IF VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
from sys import stdin, stdout input = stdin.readline t = 1 for _ in range(t): n, k = map(int, input().split()) a = [int(x) for x in input().split()] a = sorted(a) val = 1 for i in range(n // 2 + 1, n): diff = (a[i] - a[i - 1]) * val if diff <= k: k -= diff else: m = k // val ans = a[i - 1] + m break val += 1 else: m = k // val ans = a[-1] + m print(ans)
ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
from sys import stdin, stdout def INI(): return int(stdin.readline()) def INL(): return [int(_) for _ in stdin.readline().split()] def INS(): return stdin.readline() def MOD(): return pow(10, 9) + 7 def OPS(ans): stdout.write(str(ans) + "\n") def OPL(ans): [stdout.write(str(_) + " ") for _ in ans] stdout.write("\n") n, k = INL() A = sorted(INL()) ans = A[n // 2] for _ in range(n // 2 + 1, n): if A[_] != A[_ - 1]: if k >= (A[_] - A[_ - 1]) * (_ - n // 2): k -= (A[_] - A[_ - 1]) * (_ - n // 2) ans += A[_] - A[_ - 1] else: ans += k // (_ - n // 2) break else: ans = A[-1] + k // (n - n // 2) OPS(ans)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = map(int, input().split()) a = list(map(int, input().split())) a.sort() a = a + [a[n - 1]] c = 0 m = (n - 1) // 2 i = m if i + 1 < n: add = (a[i + 1] - a[i]) * (i - m + 1) while i + 1 < n and c + add <= k: c += add i += 1 add = (a[i + 1] - a[i]) * (i - m + 1) a[i] += (k - c) // (i - m + 1) print(a[i])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
from sys import stdin n, m = map(int, stdin.readline().strip().split()) s = list(map(int, stdin.readline().strip().split())) s.sort() s = s[n // 2 :] low = s[0] high = 2 * 10**9 def can(x): ans = 0 for i in range(len(s)): if s[i] < x: ans += x - s[i] if ans <= m: return True else: return False while high - low > 1: mid = (high + low) // 2 if can(mid): low = mid else: high = mid if can(high): print(high) else: print(low)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN 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 IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = map(int, input().split()) a = list(map(int, input().split())) a.sort() index = n >> 1 cur = a[index] i = index + 1 index = 1 while k > 0 and i < len(a): p = (a[i] - a[i - 1]) * index if k < index: break elif p <= k: cur = a[i] k = k - p index += 1 i += 1 else: cur = cur + k // index break if i == len(a): cur = cur + k // index print(cur)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = map(int, input().split()) a = sorted(list(map(int, input().split()))) start_index = n // 2 cur_index = start_index m = a[start_index] numbers_to_inc_count = 1 while k != 0: if cur_index == n - 1: m += k // numbers_to_inc_count k = 0 elif (a[cur_index + 1] - m) * numbers_to_inc_count <= k: k -= (a[cur_index + 1] - m) * numbers_to_inc_count m = a[cur_index + 1] cur_index += 1 numbers_to_inc_count += 1 else: m += k // numbers_to_inc_count k = 0 print(m)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = map(int, input().split()) a = [int(o) for o in input().split()] d = [] a = sorted(a) lol = n // 2 i = n // 2 cv = a[lol] while i < n: try: d.append([d[-1][0] + (a[i] - cv) * (i - lol), a[i], i]) except: d.append([(a[i] - cv) * (i - lol), a[i], i]) cv = a[i] if d[-1][0] > k: ans = d[-2][1] + (k - d[-2][0]) // (d[-2][2] - lol + 1) break if i == n - 1: ans = d[-1][1] + (k - d[-1][0]) // (d[-1][2] - lol + 1) break i += 1 print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = list(map(int, input().split())) a = list(map(int, input().split())) a.sort() def check(m): c = 0 for i in range(n // 2, n): if a[i] > m: break else: c += m - a[i] return c <= k l = a[n // 2] r = a[n // 2] + k + 1 while r - l > 1: m = (r + l) // 2 if check(m): l = m else: r = m print(l)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR RETURN VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER 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
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = map(int, input().split()) a = list(map(int, input().split())) a.sort() j = int(n / 2) x = 1 ans = a[j] while k > 0 and j < n - 1: y = a[j + 1] - a[j] if x * y <= k: k -= x * y x += 1 ans = a[j + 1] j += 1 else: ans += int(k / x) k = 0 break if k: ans += int(k / (int(n / 2) + 1)) print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
line1 = input().split() n, m = int(line1[0]), int(line1[1]) lst = input().split() lst = [int(x) for x in lst] lst.sort() median = n // 2 lst = lst[median:] total = [] for i in range(len(lst)): if i == 0: total.append(lst[i]) else: total.append(total[-1] + lst[i]) def eval(lst, total, index): target = lst[index] target_total = target * (index + 1) actual_total = total[index] return target_total - actual_total start = 0 end = len(total) - 1 largest = 0 while start <= end: mid = (start + end) // 2 amount_needed = eval(lst, total, mid) if amount_needed == m: largest = mid break elif amount_needed < m: largest = max(largest, mid) start = mid + 1 else: end = mid - 1 leftover = m - eval(lst, total, largest) print(lst[largest] + leftover // (largest + 1))
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
def check(x): ans1 = 0 for i in range(n // 2, n): ans1 += max(0, x - arr[i]) return ans1 n, k = map(int, input().split()) arr = list(map(int, input().split())) arr = sorted(arr) l = 1 r = 2000000000 ans = 0 while l <= r: mid = (l + r) // 2 if check(mid) <= k: ans = max(ans, mid) l = mid + 1 else: r = mid - 1 print(ans)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL 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
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = map(int, input().split()) num = list(sorted(map(int, input().split()))) x1 = 1 x2 = 2000000000.0 + 1 while x1 <= x2: m = int((x1 + x2) / 2) s = 0 for i in range(int(n / 2), n): s += max(0, m - num[i]) if s <= k: ans = m x1 = m + 1 else: x2 = m - 1 print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
def check(m): dif = lista[n // 2] + m soma = 0 for x in lista[n // 2 :]: soma += max(0, dif - x) return soma <= k n, k = [int(i) for i in input().split()] lista = [int(i) for i in input().split()] lista.sort() l = 0 r = 1000000001 while l < r: mid = (l + r + 1) // 2 if check(mid): l = mid else: r = mid - 1 print(l + lista[n // 2])
FUNC_DEF ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR RETURN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER 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 BIN_OP VAR VAR BIN_OP VAR NUMBER
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = (*map(int, input().split()),) a = [int(x) for x in input().split()] a.sort() md = n // 2 wnl = 1 ub = a[md + wnl] if md + wnl < n else 10**10 v = a[md] while k: while md + wnl < n: if a[md + wnl] != v: break wnl += 1 ub = a[md + wnl] if md + wnl < n else 10**10 gp = ub - v tms = min(k // wnl, gp) if not tms: break k -= wnl * tms v += tms print(v)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR VAR WHILE VAR WHILE BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
size, k = map(int, input().split()) array = sorted(list(map(int, input().split()))) median_pos = size // 2 increase_pos = list() dif = list() for x in range(median_pos + 1, size): cur = array[x] prev = array[x - 1] if cur != prev: increase_pos.append(x) dif.append(cur - prev) increased = 0 stop = size for x in range(len(increase_pos)): dif_range = increase_pos[x] - median_pos k_waste = dif_range * dif[x] if k_waste > k: stop = increase_pos[x] break increased += dif[x] k -= k_waste stop = increase_pos[x] + 1 num = stop - size // 2 times = k // num result = increased + array[median_pos] + times print(result)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
import sys input = sys.stdin.readline n, k = list(map(int, input().split())) l = list(map(int, input().split())) l = sorted(l) p = n // 2 ans = l[p] tgt = 1 while p < n - 1: diff = l[p + 1] - l[p] if k >= diff * tgt: k -= diff * tgt p += 1 ans = l[p] tgt += 1 else: print(ans + k // tgt) return print(ans + k // tgt)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR RETURN EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = [int(i) for i in input().split()] a = sorted([int(i) for i in input().split()]) c = 1 ans = a[n // 2] for i in range(n // 2, n - 1): if a[i + 1] != a[i]: r = a[i + 1] - a[i] if k - c * r < 0: ans += k // c break k -= c * r ans += r c += 1 else: ans += k // (n // 2 + 1) print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
mod = 1000000007 MOD = 998244353 ii = lambda: int(input()) si = lambda: input() dgl = lambda: list(map(int, input())) f = lambda: map(int, input().split()) il = lambda: list(map(int, input().split())) it = lambda: tuple(map(int, input().split())) ls = lambda: list(input()) n, k = f() diff = [] sm = 0 l = sorted(il()) if n == 1: print(l[0] + k) exit() for i in range(n // 2 + 1, n): sm += (i - n // 2) * (l[i] - l[i - 1]) diff.append([sm, l[i]]) mx = l[n // 2] if k > diff[-1][0]: print(diff[-1][1] + (k - diff[-1][0]) // ((n + 1) // 2)) exit() diff = [[0, l[n // 2]]] + diff for i in range(1, len(diff)): if k <= diff[i][0]: print(diff[i - 1][1] + (k - diff[i - 1][0]) // i) exit() print(mx)
ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST LIST NUMBER VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
def can(x): cnt = 0 for i in range(n // 2, n): cnt += max(0, x - arr[i]) if cnt <= k: return True else: return False n, k = map(int, input().split()) arr = list(map(int, input().split())) arr.sort() if n == 1: print(arr[0] + k) else: l = arr[n // 2] r = arr[n // 2] + k ans = arr[n // 2] while l <= r: mid = (l + r) // 2 if can(mid): ans = mid l = mid + 1 else: r = mid - 1 print(ans)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = map(int, input().split()) arr = sorted(list(map(int, input().split()))) mid = n // 2 q = 1 i = 0 ans = arr[mid] while mid + 1 < n: for i in arr[mid + 1 :]: if i != ans: break q += 1 mid += 1 if k - q * (i - ans) < 0: break k = k - q * (i - ans) ans = i if k > 0 and k // q: ans = ans + k // q print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR WHILE BIN_OP VAR NUMBER VAR FOR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = map(int, input().split()) lis = list(map(int, input().split())) lis.sort() mid = len(lis) // 2 c = 0 i = 1 ans = lis[mid] while i < n - mid: l = lis[mid + i] - ans if k - c >= l * i: c += l * i ans += l else: ans += (k - c) // i c = k break i += 1 if c < k: ans += (k - c) // (mid + 1) print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = map(int, input().split()) a = list(map(int, input().split())) a.sort() def f(m, k, n, a): j = n // 2 g = a[j] if m - g > 0: k -= m - g for i in range(j + 1, n): if a[i] <= m: k -= m - a[i] return k >= 0 l = a[0] r = a[-1] + k + 1 while r - l > 1: m = (l + r) // 2 if f(m, k, n, a): l = m else: r = m print(l)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR RETURN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER 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 EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = map(int, input().split()) l = list(map(int, input().split())) l.sort() lo, hi = 1, 999999999999 while lo < hi: mid = (lo + hi) // 2 tmp = 0 for i in range(n // 2, n): tmp += max(mid - l[i], 0) if tmp <= k: lo = mid else: hi = mid - 1 if hi - lo == 1: tmp = 0 for i in range(n // 2, n): tmp += max(hi - l[i], 0) if tmp <= k: lo = hi else: hi = lo print(lo)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
def main(): n, k = input().split() n = int(n) k = int(k) a = [int(i) for i in input().split()] a.sort() mid = int(n / 2) for i in range(mid + 1, n): if a[mid] < a[i]: diff = a[i] - a[mid] if (i - mid) * diff >= k: print(a[mid] + int(k / (i - mid))) return else: a[mid] += diff k -= (i - mid) * diff print(a[mid] + int(k / (mid + 1))) main()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR RETURN VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
x = input().split() n = input().split() for i in range(0, len(n)): n[i] = int(n[i]) moves = int(x[1]) n.sort() medianIndex = int(len(n) / 2) median = n[medianIndex] maxi = int(n[-1]) + moves medmid = 0 tots = 0 while median <= maxi: medmid = int((median + maxi) / 2) total = 0 for i in range(medianIndex, len(n)): if medmid > n[i]: total += medmid - n[i] if total <= moves: tots = medmid median = medmid + 1 else: maxi = medmid - 1 print(tots)
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
def isPossible(x, k): temp = a.copy() for i in range(int(n / 2), n): if x > temp[i]: diff = x - temp[i] k -= diff temp[i] = x if temp[i] < temp[i - 1] or k < 0: return False else: break return True n, k = map(int, input().split()) a = list(map(int, input().split())) a = sorted(a) mid = int(n / 2) l = a[mid] h = a[mid] + k res = l while l <= h: mid = int(l + (h - l) / 2) if isPossible(mid, k): res = mid l = mid + 1 else: h = mid - 1 print(res)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
N, K = map(int, input().split()) A = list(map(int, input().split())) piv = N // 2 A.sort() ans = A[piv] k = 0 for i in range(piv + 1, N): if ans < A[i]: a = (A[i] - ans) * (i - piv) if k + a > K: ans += (K - k) // (i - piv) k += a break else: ans = A[i] k += a if k < K: ans += (K - k) // (N - piv) print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR