description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
The only difference between easy and hard versions is the maximum value of $n$. You are given a positive integer number $n$. You really love good numbers so you want to find the smallest good number greater than or equal to $n$. The positive integer is called good if it can be represented as a sum of distinct powers of $3$ (i.e. no duplicates of powers of $3$ are allowed). For example: $30$ is a good number: $30 = 3^3 + 3^1$, $1$ is a good number: $1 = 3^0$, $12$ is a good number: $12 = 3^2 + 3^1$, but $2$ is not a good number: you can't represent it as a sum of distinct powers of $3$ ($2 = 3^0 + 3^0$), $19$ is not a good number: you can't represent it as a sum of distinct powers of $3$ (for example, the representations $19 = 3^2 + 3^2 + 3^0 = 3^2 + 3^1 + 3^1 + 3^1 + 3^0$ are invalid), $20$ is also not a good number: you can't represent it as a sum of distinct powers of $3$ (for example, the representation $20 = 3^2 + 3^2 + 3^0 + 3^0$ is invalid). Note, that there exist other representations of $19$ and $20$ as sums of powers of $3$ but none of them consists of distinct powers of $3$. For the given positive integer $n$ find such smallest $m$ ($n \le m$) that $m$ is a good number. You have to answer $q$ independent queries. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) — the number of queries. Then $q$ queries follow. The only line of the query contains one integer $n$ ($1 \le n \le 10^{18}$). -----Output----- For each query, print such smallest integer $m$ (where $n \le m$) that $m$ is a good number. -----Example----- Input 8 1 2 6 13 14 3620 10000 1000000000000000000 Output 1 3 9 13 27 6561 19683 1350851717672992089
import sys input = sys.stdin.readline I = lambda: list(map(int, input().split())) (t,) = I() for _ in range(t): (n,) = I() k = "" x = n while x: k += str(x % 3) x //= 3 k = k[::-1] ix = 0 if "2" not in k: print(n) else: p = k.find("2") if p == 0 or "0" not in k: k = "1" + "0" * len(k) else: f = n + 1 l = p - 1 while l > 0: if k[l] == "0": f = l break l -= 1 if f < p: p = f k = k[:p] + "1" + "0" * (len(k) - p - 1) else: k = "1" + "0" * len(k) k = k[::-1] an = 0 m = 1 for i in k: an += int(i) * m m *= 3 print(an)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR VAR WHILE VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF STRING VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER STRING VAR ASSIGN VAR BIN_OP STRING BIN_OP STRING FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR STRING BIN_OP STRING BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP STRING BIN_OP STRING FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
A car number in Berland consists of exactly n digits. A number is called beautiful if it has at least k equal digits. Vasya wants to change the digits in his car's number so that the number became beautiful. To replace one of n digits Vasya has to pay the sum of money, equal to the absolute difference between the old digit and the new one. Help Vasya: find the minimum sum of money he should pay to make the number of his car beautiful. You should also find the resulting beautiful number. If there are several such numbers, then print the lexicographically minimum one. Input The first line contains two space-separated integers n and k (2 ≤ n ≤ 104, 2 ≤ k ≤ n) which represent how many digits the number has and how many equal digits a beautiful number should have. The second line consists of n digits. It describes the old number of Vasya's car. It is guaranteed that the number contains no spaces and only contains digits. Output On the first line print the minimum sum of money Vasya needs to change the number. On the second line print the car's new number. If there are several solutions, print the lexicographically minimum one. Examples Input 6 5 898196 Output 4 888188 Input 3 2 533 Output 0 533 Input 10 6 0001112223 Output 3 0000002223 Note In the first sample replacing the second digit with an "8" costs |9 - 8| = 1. Replacing the fifth digit with an "8" costs the same. Replacing the sixth digit costs |6 - 8| = 2. As a result, Vasya will pay 1 + 1 + 2 = 4 for a beautiful number "888188". The lexicographical comparison of strings is performed by the < operator in modern programming languages. The string x is lexicographically smaller than the string y, if there exists such i (1 ≤ i ≤ n), that xi < yi, and for any j (1 ≤ j < i) xj = yj. The strings compared in this problem will always have the length n.
n, k = map(int, input().split()) s = input() Cnt = [0] * 10 for item in s: Cnt[int(item)] += 1 Ans = [] H = [] for i in range(0, 10): S = list(map(int, s)) e = Cnt[i] j = 1 C = [i] while e < k: if i + j < 10: e += Cnt[i + j] C.append(i + j) if i - j >= 0 and e < k: e += Cnt[i - j] C.append(i - j) j += 1 cnt = 0 cost = 0 for item in C: if cnt == k: break if item >= i: for j in range(len(s)): if S[j] == item: S[j] = i cnt += 1 cost += abs(i - item) if cnt == k: break else: for j in range(len(s) - 1, -1, -1): if S[j] == item: S[j] = i cnt += 1 cost += abs(i - item) if cnt == k: break H.append((cost, tuple(S))) H = min(H) print(H[0]) for item in H[1]: print(item, end="")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST VAR WHILE VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING
A car number in Berland consists of exactly n digits. A number is called beautiful if it has at least k equal digits. Vasya wants to change the digits in his car's number so that the number became beautiful. To replace one of n digits Vasya has to pay the sum of money, equal to the absolute difference between the old digit and the new one. Help Vasya: find the minimum sum of money he should pay to make the number of his car beautiful. You should also find the resulting beautiful number. If there are several such numbers, then print the lexicographically minimum one. Input The first line contains two space-separated integers n and k (2 ≤ n ≤ 104, 2 ≤ k ≤ n) which represent how many digits the number has and how many equal digits a beautiful number should have. The second line consists of n digits. It describes the old number of Vasya's car. It is guaranteed that the number contains no spaces and only contains digits. Output On the first line print the minimum sum of money Vasya needs to change the number. On the second line print the car's new number. If there are several solutions, print the lexicographically minimum one. Examples Input 6 5 898196 Output 4 888188 Input 3 2 533 Output 0 533 Input 10 6 0001112223 Output 3 0000002223 Note In the first sample replacing the second digit with an "8" costs |9 - 8| = 1. Replacing the fifth digit with an "8" costs the same. Replacing the sixth digit costs |6 - 8| = 2. As a result, Vasya will pay 1 + 1 + 2 = 4 for a beautiful number "888188". The lexicographical comparison of strings is performed by the < operator in modern programming languages. The string x is lexicographically smaller than the string y, if there exists such i (1 ≤ i ≤ n), that xi < yi, and for any j (1 ≤ j < i) xj = yj. The strings compared in this problem will always have the length n.
n, k = map(int, input().split()) s = list(map(int, input())) e = list(enumerate(s)) min_cost = 9 * n a = [9] * n for j in range(10): r = s[:] cost = 0 for x, y in sorted( e, key=lambda t: (abs(t[1] - j), -t[1], t[0] * (-1) ** (j > t[1])) )[:k]: r[x] = j cost += abs(y - j) if cost < min_cost: min_cost = cost a = r if min_cost == cost: a = min(a, r) print(min_cost) print("".join(map(str, a)))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
A car number in Berland consists of exactly n digits. A number is called beautiful if it has at least k equal digits. Vasya wants to change the digits in his car's number so that the number became beautiful. To replace one of n digits Vasya has to pay the sum of money, equal to the absolute difference between the old digit and the new one. Help Vasya: find the minimum sum of money he should pay to make the number of his car beautiful. You should also find the resulting beautiful number. If there are several such numbers, then print the lexicographically minimum one. Input The first line contains two space-separated integers n and k (2 ≤ n ≤ 104, 2 ≤ k ≤ n) which represent how many digits the number has and how many equal digits a beautiful number should have. The second line consists of n digits. It describes the old number of Vasya's car. It is guaranteed that the number contains no spaces and only contains digits. Output On the first line print the minimum sum of money Vasya needs to change the number. On the second line print the car's new number. If there are several solutions, print the lexicographically minimum one. Examples Input 6 5 898196 Output 4 888188 Input 3 2 533 Output 0 533 Input 10 6 0001112223 Output 3 0000002223 Note In the first sample replacing the second digit with an "8" costs |9 - 8| = 1. Replacing the fifth digit with an "8" costs the same. Replacing the sixth digit costs |6 - 8| = 2. As a result, Vasya will pay 1 + 1 + 2 = 4 for a beautiful number "888188". The lexicographical comparison of strings is performed by the < operator in modern programming languages. The string x is lexicographically smaller than the string y, if there exists such i (1 ≤ i ≤ n), that xi < yi, and for any j (1 ≤ j < i) xj = yj. The strings compared in this problem will always have the length n.
import sys n, k = map(int, input().split()) s = list(input()) anss = "" minn = sys.maxsize for i in range(10): d = [] s1 = s[:] for j in range(n): d.append([abs(int(s[j]) - i), j - n if int(s[j]) >= i else n - j]) d.sort() c = 0 for k1 in range(k): c += d[k1][0] s1[n - abs(d[k1][1])] = str(i) ans = "".join(s1) if c < minn: anss = ans minn = c if c == minn: anss = min(ans, anss) print(minn) print(anss)
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
A car number in Berland consists of exactly n digits. A number is called beautiful if it has at least k equal digits. Vasya wants to change the digits in his car's number so that the number became beautiful. To replace one of n digits Vasya has to pay the sum of money, equal to the absolute difference between the old digit and the new one. Help Vasya: find the minimum sum of money he should pay to make the number of his car beautiful. You should also find the resulting beautiful number. If there are several such numbers, then print the lexicographically minimum one. Input The first line contains two space-separated integers n and k (2 ≤ n ≤ 104, 2 ≤ k ≤ n) which represent how many digits the number has and how many equal digits a beautiful number should have. The second line consists of n digits. It describes the old number of Vasya's car. It is guaranteed that the number contains no spaces and only contains digits. Output On the first line print the minimum sum of money Vasya needs to change the number. On the second line print the car's new number. If there are several solutions, print the lexicographically minimum one. Examples Input 6 5 898196 Output 4 888188 Input 3 2 533 Output 0 533 Input 10 6 0001112223 Output 3 0000002223 Note In the first sample replacing the second digit with an "8" costs |9 - 8| = 1. Replacing the fifth digit with an "8" costs the same. Replacing the sixth digit costs |6 - 8| = 2. As a result, Vasya will pay 1 + 1 + 2 = 4 for a beautiful number "888188". The lexicographical comparison of strings is performed by the < operator in modern programming languages. The string x is lexicographically smaller than the string y, if there exists such i (1 ≤ i ≤ n), that xi < yi, and for any j (1 ≤ j < i) xj = yj. The strings compared in this problem will always have the length n.
from sys import stdin def get_freq(string): freq = [0] * 10 for i in string: freq[ord(i) - ord("0")] += 1 return freq def print_string(cstring, marr): aux = [] for mark in marr: string = cstring[:] num = str(mark) require = k - freq[mark] diff = 1 n1 = -1 n2 = -1 while require > 0: if mark - diff >= 0: n2 = mark - diff else: n2 = float("inf") if mark + diff <= 9: n1 = mark + diff else: n1 = float("inf") for i in range(len(string)): if require == 0: break if string[i] == str(n1): string[i] = num require -= 1 for i in range(len(string) - 1, -1, -1): if require == 0: break if string[i] == str(n2): string[i] = num require -= 1 diff += 1 aux.append("".join(string)) aux.sort() print(aux[0]) n, k = list(map(int, stdin.readline().split())) string = list(stdin.readline().strip()) freq = get_freq(string) ans = float("inf") mark = -1 for i in range(0, 10): if freq[i] >= k: print(0) print("".join(string)) exit() else: require = k - freq[i] diff = 1 cost = 0 while require > 0 and diff < 10: num = i + diff if num <= 9: if freq[num] >= require and require > 0: cost += require * diff require = 0 elif require > 0: cost += freq[num] * diff require -= freq[num] else: pass num = i - diff if num >= 0: if freq[num] >= require and require > 0: cost += require * diff require = 0 elif require > 0: cost += freq[num] * diff require -= freq[num] else: pass diff += 1 if ans > cost: ans = cost mark = [i] elif ans == cost: mark.append(i) print(ans) print_string(string, mark)
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR STRING IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
A car number in Berland consists of exactly n digits. A number is called beautiful if it has at least k equal digits. Vasya wants to change the digits in his car's number so that the number became beautiful. To replace one of n digits Vasya has to pay the sum of money, equal to the absolute difference between the old digit and the new one. Help Vasya: find the minimum sum of money he should pay to make the number of his car beautiful. You should also find the resulting beautiful number. If there are several such numbers, then print the lexicographically minimum one. Input The first line contains two space-separated integers n and k (2 ≤ n ≤ 104, 2 ≤ k ≤ n) which represent how many digits the number has and how many equal digits a beautiful number should have. The second line consists of n digits. It describes the old number of Vasya's car. It is guaranteed that the number contains no spaces and only contains digits. Output On the first line print the minimum sum of money Vasya needs to change the number. On the second line print the car's new number. If there are several solutions, print the lexicographically minimum one. Examples Input 6 5 898196 Output 4 888188 Input 3 2 533 Output 0 533 Input 10 6 0001112223 Output 3 0000002223 Note In the first sample replacing the second digit with an "8" costs |9 - 8| = 1. Replacing the fifth digit with an "8" costs the same. Replacing the sixth digit costs |6 - 8| = 2. As a result, Vasya will pay 1 + 1 + 2 = 4 for a beautiful number "888188". The lexicographical comparison of strings is performed by the < operator in modern programming languages. The string x is lexicographically smaller than the string y, if there exists such i (1 ≤ i ≤ n), that xi < yi, and for any j (1 ≤ j < i) xj = yj. The strings compared in this problem will always have the length n.
n, k = map(int, input().split()) p = {i: (0) for i in "0123456789"} t, s = input(), 10 * n for i in t: p[i] += 1 p = [p[i] for i in "0123456789"] for i in range(0, 10): a, d, b = p[i], 1, 0 while a < k and d < 10: j = i + d if j < 10: if a + p[j] > k: b += d * (k - a) break b += d * p[j] a += p[j] j = i - d if j >= 0: if a + p[j] > k: b += d * (k - a) break b += d * p[j] a += p[j] d += 1 if b < s: s, u = b, [i] elif b == s: u.append(i) def f(t, v): global p, k a, d, i = p[v], 1, str(v) while a < k and d < 10: j = v + d if j < 10: if a + p[j] > k: if a == k: break h, j = k - a, str(j) for x in range(n): if t[x] == j: if h == 1: t = t[: x + 1].replace(j, i) + t[x + 1 :] break h -= 1 break a += p[j] t = t.replace(str(j), i) j = v - d if j >= 0: if a + p[j] > k: if a + p[j] == k: break h, j = k - a, str(j) for x in range(n - 1, -1, -1): if t[x] == j: if h == 1: t = t[:x] + t[x:].replace(j, i) break h -= 1 break a += p[j] t = t.replace(str(j), i) d += 1 return t print(s) print(min(f(t, v) for v in u))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR STRING ASSIGN VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR LIST VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER IF BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER IF BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
You are given an undirected unweighted graph consisting of $n$ vertices and $m$ edges (which represents the map of Bertown) and the array of prices $p$ of length $m$. It is guaranteed that there is a path between each pair of vertices (districts). Mike has planned a trip from the vertex (district) $a$ to the vertex (district) $b$ and then from the vertex (district) $b$ to the vertex (district) $c$. He can visit the same district twice or more. But there is one issue: authorities of the city want to set a price for using the road so if someone goes along the road then he should pay the price corresponding to this road (he pays each time he goes along the road). The list of prices that will be used $p$ is ready and they just want to distribute it between all roads in the town in such a way that each price from the array corresponds to exactly one road. You are a good friend of Mike (and suddenly a mayor of Bertown) and want to help him to make his trip as cheap as possible. So, your task is to distribute prices between roads in such a way that if Mike chooses the optimal path then the price of the trip is the minimum possible. Note that you cannot rearrange prices after the start of the trip. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The first line of the test case contains five integers $n, m, a, b$ and $c$ ($2 \le n \le 2 \cdot 10^5$, $n-1 \le m \le min(\frac{n(n-1)}{2}, 2 \cdot 10^5)$, $1 \le a, b, c \le n$) — the number of vertices, the number of edges and districts in Mike's trip. The second line of the test case contains $m$ integers $p_1, p_2, \dots, p_m$ ($1 \le p_i \le 10^9$), where $p_i$ is the $i$-th price from the array. The following $m$ lines of the test case denote edges: edge $i$ is represented by a pair of integers $v_i$, $u_i$ ($1 \le v_i, u_i \le n$, $u_i \ne v_i$), which are the indices of vertices connected by the edge. There are no loops or multiple edges in the given graph, i. e. for each pair ($v_i, u_i$) there are no other pairs ($v_i, u_i$) or ($u_i, v_i$) in the array of edges, and for each pair $(v_i, u_i)$ the condition $v_i \ne u_i$ is satisfied. It is guaranteed that the given graph is connected. It is guaranteed that the sum of $n$ (as well as the sum of $m$) does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$, $\sum m \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the minimum possible price of Mike's trip if you distribute prices between edges optimally. -----Example----- Input 2 4 3 2 3 4 1 2 3 1 2 1 3 1 4 7 9 1 5 7 2 10 4 8 5 6 7 3 3 1 2 1 3 1 4 3 2 3 5 4 2 5 6 1 7 6 7 Output 7 12 -----Note----- One of the possible solution to the first test case of the example: [Image] One of the possible solution to the second test case of the example: [Image]
from sys import stdin input = stdin.readline def bfs(g, r): it = -1 p = [0] * len(g) q = [r] v = [0] * len(g) while it < len(q) - 1: it += 1 v[q[it]] = 1 for i in range(len(g[q[it]])): if v[g[q[it]][i]] == 0: v[g[q[it]][i]] = 1 q.append(g[q[it]][i]) p[g[q[it]][i]] = p[q[it]] + 1 return p for _ in range(int(input())): n, k, a, b, c = map(int, input().split()) ax = list(map(int, input().split())) g = [[] for i in range(n)] a -= 1 b -= 1 c -= 1 d = {} for i in range(k): l, r = map(int, input().split()) g[l - 1].append(r - 1) g[r - 1].append(l - 1) d[min(l - 1, r - 1), max(l - 1, r - 1)] = 0 aa = bfs(g, a) bb = bfs(g, b) cc = bfs(g, c) ax.sort() x = [0] y = 0 for i in range(k): y += ax[i] x.append(y) ans = 10**19 for i in range(n): if aa[i] + bb[i] + cc[i] > k: continue ans = min(ans, x[bb[i]] + x[aa[i] + bb[i] + cc[i]]) print(ans)
ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR 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 LIST VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an undirected unweighted graph consisting of $n$ vertices and $m$ edges (which represents the map of Bertown) and the array of prices $p$ of length $m$. It is guaranteed that there is a path between each pair of vertices (districts). Mike has planned a trip from the vertex (district) $a$ to the vertex (district) $b$ and then from the vertex (district) $b$ to the vertex (district) $c$. He can visit the same district twice or more. But there is one issue: authorities of the city want to set a price for using the road so if someone goes along the road then he should pay the price corresponding to this road (he pays each time he goes along the road). The list of prices that will be used $p$ is ready and they just want to distribute it between all roads in the town in such a way that each price from the array corresponds to exactly one road. You are a good friend of Mike (and suddenly a mayor of Bertown) and want to help him to make his trip as cheap as possible. So, your task is to distribute prices between roads in such a way that if Mike chooses the optimal path then the price of the trip is the minimum possible. Note that you cannot rearrange prices after the start of the trip. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The first line of the test case contains five integers $n, m, a, b$ and $c$ ($2 \le n \le 2 \cdot 10^5$, $n-1 \le m \le min(\frac{n(n-1)}{2}, 2 \cdot 10^5)$, $1 \le a, b, c \le n$) — the number of vertices, the number of edges and districts in Mike's trip. The second line of the test case contains $m$ integers $p_1, p_2, \dots, p_m$ ($1 \le p_i \le 10^9$), where $p_i$ is the $i$-th price from the array. The following $m$ lines of the test case denote edges: edge $i$ is represented by a pair of integers $v_i$, $u_i$ ($1 \le v_i, u_i \le n$, $u_i \ne v_i$), which are the indices of vertices connected by the edge. There are no loops or multiple edges in the given graph, i. e. for each pair ($v_i, u_i$) there are no other pairs ($v_i, u_i$) or ($u_i, v_i$) in the array of edges, and for each pair $(v_i, u_i)$ the condition $v_i \ne u_i$ is satisfied. It is guaranteed that the given graph is connected. It is guaranteed that the sum of $n$ (as well as the sum of $m$) does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$, $\sum m \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the minimum possible price of Mike's trip if you distribute prices between edges optimally. -----Example----- Input 2 4 3 2 3 4 1 2 3 1 2 1 3 1 4 7 9 1 5 7 2 10 4 8 5 6 7 3 3 1 2 1 3 1 4 3 2 3 5 4 2 5 6 1 7 6 7 Output 7 12 -----Note----- One of the possible solution to the first test case of the example: [Image] One of the possible solution to the second test case of the example: [Image]
import sys input = sys.stdin.buffer.readline for f in range(int(input())): n, m, x, y, z = map(int, input().split()) p = list(map(int, input().split())) p.sort() x -= 1 y -= 1 z -= 1 neig = [0] * n dx = [-1] * n dy = [-1] * n dz = [-1] * n dx[x] = 0 dy[y] = 0 dz[z] = 0 levx = [[x]] levy = [[y]] levz = [[z]] for i in range(n): neig[i] = [0] for i in range(m): a, b = map(int, input().split()) a -= 1 b -= 1 neig[a][0] += 1 neig[b][0] += 1 neig[a].append(b) neig[b].append(a) i = 0 while i < len(levx): toad = [] for v in levx[i]: for j in range(1, neig[v][0] + 1): u = neig[v][j] if dx[u] == -1: dx[u] = i + 1 toad.append(u) if len(toad) > 0: levx.append(toad) i += 1 i = 0 while i < len(levy): toad = [] for v in levy[i]: for j in range(1, neig[v][0] + 1): u = neig[v][j] if dy[u] == -1: dy[u] = i + 1 toad.append(u) if len(toad) > 0: levy.append(toad) i += 1 i = 0 while i < len(levz): toad = [] for v in levz[i]: for j in range(1, neig[v][0] + 1): u = neig[v][j] if dz[u] == -1: dz[u] = i + 1 toad.append(u) if len(toad) > 0: levz.append(toad) i += 1 pt = [0] * m for i in range(m): pt[i] = pt[i - 1] + p[i] mi = 2 * pt[m - 1] for i in range(n): m1 = dx[i] + dy[i] + dz[i] if m1 <= m: mix = 0 if m1 > 0: mix += pt[m1 - 1] if dy[i] > 0: mix += pt[dy[i] - 1] mi = min(mix, mi) print(mi)
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR 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 NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST LIST VAR ASSIGN VAR LIST LIST VAR ASSIGN VAR LIST LIST VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an undirected unweighted graph consisting of $n$ vertices and $m$ edges (which represents the map of Bertown) and the array of prices $p$ of length $m$. It is guaranteed that there is a path between each pair of vertices (districts). Mike has planned a trip from the vertex (district) $a$ to the vertex (district) $b$ and then from the vertex (district) $b$ to the vertex (district) $c$. He can visit the same district twice or more. But there is one issue: authorities of the city want to set a price for using the road so if someone goes along the road then he should pay the price corresponding to this road (he pays each time he goes along the road). The list of prices that will be used $p$ is ready and they just want to distribute it between all roads in the town in such a way that each price from the array corresponds to exactly one road. You are a good friend of Mike (and suddenly a mayor of Bertown) and want to help him to make his trip as cheap as possible. So, your task is to distribute prices between roads in such a way that if Mike chooses the optimal path then the price of the trip is the minimum possible. Note that you cannot rearrange prices after the start of the trip. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The first line of the test case contains five integers $n, m, a, b$ and $c$ ($2 \le n \le 2 \cdot 10^5$, $n-1 \le m \le min(\frac{n(n-1)}{2}, 2 \cdot 10^5)$, $1 \le a, b, c \le n$) — the number of vertices, the number of edges and districts in Mike's trip. The second line of the test case contains $m$ integers $p_1, p_2, \dots, p_m$ ($1 \le p_i \le 10^9$), where $p_i$ is the $i$-th price from the array. The following $m$ lines of the test case denote edges: edge $i$ is represented by a pair of integers $v_i$, $u_i$ ($1 \le v_i, u_i \le n$, $u_i \ne v_i$), which are the indices of vertices connected by the edge. There are no loops or multiple edges in the given graph, i. e. for each pair ($v_i, u_i$) there are no other pairs ($v_i, u_i$) or ($u_i, v_i$) in the array of edges, and for each pair $(v_i, u_i)$ the condition $v_i \ne u_i$ is satisfied. It is guaranteed that the given graph is connected. It is guaranteed that the sum of $n$ (as well as the sum of $m$) does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$, $\sum m \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the minimum possible price of Mike's trip if you distribute prices between edges optimally. -----Example----- Input 2 4 3 2 3 4 1 2 3 1 2 1 3 1 4 7 9 1 5 7 2 10 4 8 5 6 7 3 3 1 2 1 3 1 4 3 2 3 5 4 2 5 6 1 7 6 7 Output 7 12 -----Note----- One of the possible solution to the first test case of the example: [Image] One of the possible solution to the second test case of the example: [Image]
import sys input = sys.stdin.readline INF = 10**16 Q = int(input()) Query = [] for _ in range(Q): N, M, a, b, c = map(int, input().split()) P = list(map(int, input().split())) graph = [[] for _ in range(N)] for _ in range(M): u, v = map(int, input().split()) graph[u - 1].append(v - 1) graph[v - 1].append(u - 1) Query.append((N, M, a - 1, b - 1, c - 1, P, graph)) def bfs(s, N, graph): q = [s] D = [-1] * N D[s] = 0 while q: qq = [] for p in q: for np in graph[p]: if D[np] == -1: D[np] = D[p] + 1 qq.append(np) q = qq return D for N, M, a, b, c, P, graph in Query: Da = bfs(a, N, graph) Db = bfs(b, N, graph) Dc = bfs(c, N, graph) P.sort() W = [0] for p in P: W.append(W[-1] + p) ans = INF for n in range(N): da = Da[n] db = Db[n] dc = Dc[n] if da + db + dc > M: continue score = W[db] + W[da + db + dc] if score < ans: ans = score print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR 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 LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR LIST VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER WHILE VAR ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR FOR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are given an integer array $a_1, a_2, \dots, a_n$ and integer $k$. In one step you can either choose some index $i$ and decrease $a_i$ by one (make $a_i = a_i - 1$); or choose two indices $i$ and $j$ and set $a_i$ equal to $a_j$ (make $a_i = a_j$). What is the minimum number of steps you need to make the sum of array $\sum\limits_{i=1}^{n}{a_i} \le k$? (You are allowed to make values of array negative). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^{15}$) — the size of array $a$ and upper bound on its sum. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the array itself. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print one integer — the minimum number of steps to make $\sum\limits_{i=1}^{n}{a_i} \le k$. -----Examples----- Input 4 1 10 20 2 69 6 9 7 8 1 2 1 3 1 2 1 10 1 1 2 3 1 2 6 1 6 8 10 Output 10 0 2 7 -----Note----- In the first test case, you should decrease $a_1$ $10$ times to get the sum lower or equal to $k = 10$. In the second test case, the sum of array $a$ is already less or equal to $69$, so you don't need to change it. In the third test case, you can, for example: set $a_4 = a_3 = 1$; decrease $a_4$ by one, and get $a_4 = 0$. As a result, you'll get array $[1, 2, 1, 0, 1, 2, 1]$ with sum less or equal to $8$ in $1 + 1 = 2$ steps. In the fourth test case, you can, for example: choose $a_7$ and decrease in by one $3$ times; you'll get $a_7 = -2$; choose $4$ elements $a_6$, $a_8$, $a_9$ and $a_{10}$ and them equal to $a_7 = -2$. As a result, you'll get array $[1, 2, 3, 1, 2, -2, -2, -2, -2, -2]$ with sum less or equal to $1$ in $3 + 4 = 7$ steps.
def solve(): n, k = map(int, input().split()) a = list(map(int, input().split())) a.sort() summ = sum(a) if summ <= k: print(0) return ans = summ - k summ -= a[0] for i in range(1, n): summ -= a[n - i] x = max(a[0] + (summ - k + i) // (i + 1), 0) ans = min(ans, x + i) print(ans) for i in range(int(input())): solve()
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 FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are given an integer array $a_1, a_2, \dots, a_n$ and integer $k$. In one step you can either choose some index $i$ and decrease $a_i$ by one (make $a_i = a_i - 1$); or choose two indices $i$ and $j$ and set $a_i$ equal to $a_j$ (make $a_i = a_j$). What is the minimum number of steps you need to make the sum of array $\sum\limits_{i=1}^{n}{a_i} \le k$? (You are allowed to make values of array negative). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^{15}$) — the size of array $a$ and upper bound on its sum. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the array itself. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print one integer — the minimum number of steps to make $\sum\limits_{i=1}^{n}{a_i} \le k$. -----Examples----- Input 4 1 10 20 2 69 6 9 7 8 1 2 1 3 1 2 1 10 1 1 2 3 1 2 6 1 6 8 10 Output 10 0 2 7 -----Note----- In the first test case, you should decrease $a_1$ $10$ times to get the sum lower or equal to $k = 10$. In the second test case, the sum of array $a$ is already less or equal to $69$, so you don't need to change it. In the third test case, you can, for example: set $a_4 = a_3 = 1$; decrease $a_4$ by one, and get $a_4 = 0$. As a result, you'll get array $[1, 2, 1, 0, 1, 2, 1]$ with sum less or equal to $8$ in $1 + 1 = 2$ steps. In the fourth test case, you can, for example: choose $a_7$ and decrease in by one $3$ times; you'll get $a_7 = -2$; choose $4$ elements $a_6$, $a_8$, $a_9$ and $a_{10}$ and them equal to $a_7 = -2$. As a result, you'll get array $[1, 2, 3, 1, 2, -2, -2, -2, -2, -2]$ with sum less or equal to $1$ in $3 + 4 = 7$ steps.
import sys input = sys.stdin.readline def inp(): return int(input()) def st(): return input().rstrip("\n") def lis(): return list(map(int, input().split())) def ma(): return map(int, input().split()) t = inp() while t: t -= 1 n, k = ma() a = lis() a.sort() cur = sum(a) - a[0] if cur + a[0] <= k: print(0) continue z = sum(a) suff = a[0] ans = float("inf") for i in range(n): if i: suff += a[n - i] total = z - suff total = k - total ans1 = total // (i + 1) ans = min(ans, max(0, a[0] - ans1) + i) print(ans)
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
You are given an integer array $a_1, a_2, \dots, a_n$ and integer $k$. In one step you can either choose some index $i$ and decrease $a_i$ by one (make $a_i = a_i - 1$); or choose two indices $i$ and $j$ and set $a_i$ equal to $a_j$ (make $a_i = a_j$). What is the minimum number of steps you need to make the sum of array $\sum\limits_{i=1}^{n}{a_i} \le k$? (You are allowed to make values of array negative). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^{15}$) — the size of array $a$ and upper bound on its sum. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the array itself. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print one integer — the minimum number of steps to make $\sum\limits_{i=1}^{n}{a_i} \le k$. -----Examples----- Input 4 1 10 20 2 69 6 9 7 8 1 2 1 3 1 2 1 10 1 1 2 3 1 2 6 1 6 8 10 Output 10 0 2 7 -----Note----- In the first test case, you should decrease $a_1$ $10$ times to get the sum lower or equal to $k = 10$. In the second test case, the sum of array $a$ is already less or equal to $69$, so you don't need to change it. In the third test case, you can, for example: set $a_4 = a_3 = 1$; decrease $a_4$ by one, and get $a_4 = 0$. As a result, you'll get array $[1, 2, 1, 0, 1, 2, 1]$ with sum less or equal to $8$ in $1 + 1 = 2$ steps. In the fourth test case, you can, for example: choose $a_7$ and decrease in by one $3$ times; you'll get $a_7 = -2$; choose $4$ elements $a_6$, $a_8$, $a_9$ and $a_{10}$ and them equal to $a_7 = -2$. As a result, you'll get array $[1, 2, 3, 1, 2, -2, -2, -2, -2, -2]$ with sum less or equal to $1$ in $3 + 4 = 7$ steps.
from sys import stdin, stdout input = stdin.readline def output(answer): stdout.write("{}\n".format(answer)) for _ in range(int(input())): n, k = map(int, input().split()) arr = list(map(int, input().split())) arr.sort() curr_sum = sum(arr) if curr_sum <= k: output(0) continue mn = arr[0] op = curr_sum - k count = 2 for index in range(n - 1, 0, -1): curr_sum -= arr[index] - mn diff = max(0, curr_sum - k) req = -(-diff // count) op = min(op, n - index + req) count += 1 output(op)
ASSIGN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an integer array $a_1, a_2, \dots, a_n$ and integer $k$. In one step you can either choose some index $i$ and decrease $a_i$ by one (make $a_i = a_i - 1$); or choose two indices $i$ and $j$ and set $a_i$ equal to $a_j$ (make $a_i = a_j$). What is the minimum number of steps you need to make the sum of array $\sum\limits_{i=1}^{n}{a_i} \le k$? (You are allowed to make values of array negative). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^{15}$) — the size of array $a$ and upper bound on its sum. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the array itself. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print one integer — the minimum number of steps to make $\sum\limits_{i=1}^{n}{a_i} \le k$. -----Examples----- Input 4 1 10 20 2 69 6 9 7 8 1 2 1 3 1 2 1 10 1 1 2 3 1 2 6 1 6 8 10 Output 10 0 2 7 -----Note----- In the first test case, you should decrease $a_1$ $10$ times to get the sum lower or equal to $k = 10$. In the second test case, the sum of array $a$ is already less or equal to $69$, so you don't need to change it. In the third test case, you can, for example: set $a_4 = a_3 = 1$; decrease $a_4$ by one, and get $a_4 = 0$. As a result, you'll get array $[1, 2, 1, 0, 1, 2, 1]$ with sum less or equal to $8$ in $1 + 1 = 2$ steps. In the fourth test case, you can, for example: choose $a_7$ and decrease in by one $3$ times; you'll get $a_7 = -2$; choose $4$ elements $a_6$, $a_8$, $a_9$ and $a_{10}$ and them equal to $a_7 = -2$. As a result, you'll get array $[1, 2, 3, 1, 2, -2, -2, -2, -2, -2]$ with sum less or equal to $1$ in $3 + 4 = 7$ steps.
import sys input = sys.stdin.readline for _ in range(int(input())): n, k = map(int, input().split()) a = list(map(int, input().split())) a.sort() tot = sum(a) - len(a) * a[0] osum = sum(a) ans = max(0, osum - k) if n == 1: print(ans) continue for i in range(1, n): tot -= a[i - 1] - a[0] gsize = n - i rdist = osum - tot - k f = gsize if rdist > 0: f += -(-rdist // (gsize + 1)) ans = min(ans, f) print(ans)
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an integer array $a_1, a_2, \dots, a_n$ and integer $k$. In one step you can either choose some index $i$ and decrease $a_i$ by one (make $a_i = a_i - 1$); or choose two indices $i$ and $j$ and set $a_i$ equal to $a_j$ (make $a_i = a_j$). What is the minimum number of steps you need to make the sum of array $\sum\limits_{i=1}^{n}{a_i} \le k$? (You are allowed to make values of array negative). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^{15}$) — the size of array $a$ and upper bound on its sum. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the array itself. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print one integer — the minimum number of steps to make $\sum\limits_{i=1}^{n}{a_i} \le k$. -----Examples----- Input 4 1 10 20 2 69 6 9 7 8 1 2 1 3 1 2 1 10 1 1 2 3 1 2 6 1 6 8 10 Output 10 0 2 7 -----Note----- In the first test case, you should decrease $a_1$ $10$ times to get the sum lower or equal to $k = 10$. In the second test case, the sum of array $a$ is already less or equal to $69$, so you don't need to change it. In the third test case, you can, for example: set $a_4 = a_3 = 1$; decrease $a_4$ by one, and get $a_4 = 0$. As a result, you'll get array $[1, 2, 1, 0, 1, 2, 1]$ with sum less or equal to $8$ in $1 + 1 = 2$ steps. In the fourth test case, you can, for example: choose $a_7$ and decrease in by one $3$ times; you'll get $a_7 = -2$; choose $4$ elements $a_6$, $a_8$, $a_9$ and $a_{10}$ and them equal to $a_7 = -2$. As a result, you'll get array $[1, 2, 3, 1, 2, -2, -2, -2, -2, -2]$ with sum less or equal to $1$ in $3 + 4 = 7$ steps.
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n, k = map(int, input().split()) a = list(map(int, input().split())) a.sort() if n == 1: ans = max(0, a[0] - k) else: y = [] for i in range(n - 1, 0, -1): y.append(a[i] - a[0]) s = sum(a) - k c = 1 ans = max(s, 0) s0 = 0 for i in range(n - 1): c += 1 s0 += y[i] ans0 = (s - s0) // c + min((s - s0) % c, 1) + (c - 1) if s <= s0: ans = min(ans, c) elif ans0 > 0: ans = min(ans, ans0) for i in range(n - 1): s -= y[i] if s <= 0: ans = min(ans, i + 1) break print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an integer array $a_1, a_2, \dots, a_n$ and integer $k$. In one step you can either choose some index $i$ and decrease $a_i$ by one (make $a_i = a_i - 1$); or choose two indices $i$ and $j$ and set $a_i$ equal to $a_j$ (make $a_i = a_j$). What is the minimum number of steps you need to make the sum of array $\sum\limits_{i=1}^{n}{a_i} \le k$? (You are allowed to make values of array negative). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^{15}$) — the size of array $a$ and upper bound on its sum. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the array itself. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print one integer — the minimum number of steps to make $\sum\limits_{i=1}^{n}{a_i} \le k$. -----Examples----- Input 4 1 10 20 2 69 6 9 7 8 1 2 1 3 1 2 1 10 1 1 2 3 1 2 6 1 6 8 10 Output 10 0 2 7 -----Note----- In the first test case, you should decrease $a_1$ $10$ times to get the sum lower or equal to $k = 10$. In the second test case, the sum of array $a$ is already less or equal to $69$, so you don't need to change it. In the third test case, you can, for example: set $a_4 = a_3 = 1$; decrease $a_4$ by one, and get $a_4 = 0$. As a result, you'll get array $[1, 2, 1, 0, 1, 2, 1]$ with sum less or equal to $8$ in $1 + 1 = 2$ steps. In the fourth test case, you can, for example: choose $a_7$ and decrease in by one $3$ times; you'll get $a_7 = -2$; choose $4$ elements $a_6$, $a_8$, $a_9$ and $a_{10}$ and them equal to $a_7 = -2$. As a result, you'll get array $[1, 2, 3, 1, 2, -2, -2, -2, -2, -2]$ with sum less or equal to $1$ in $3 + 4 = 7$ steps.
for _ in range(int(input())): n, k = map(int, input().split()) a = [int(x) for x in input().split()] a.sort() ans = 10**18 s = sum(a) - a[0] for y in range(n): need_min = (k - s) // (y + 1) x = max(0, a[0] - need_min) ans = min(ans, x + y) s -= a[-y - 1] print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an integer array $a_1, a_2, \dots, a_n$ and integer $k$. In one step you can either choose some index $i$ and decrease $a_i$ by one (make $a_i = a_i - 1$); or choose two indices $i$ and $j$ and set $a_i$ equal to $a_j$ (make $a_i = a_j$). What is the minimum number of steps you need to make the sum of array $\sum\limits_{i=1}^{n}{a_i} \le k$? (You are allowed to make values of array negative). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^{15}$) — the size of array $a$ and upper bound on its sum. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the array itself. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print one integer — the minimum number of steps to make $\sum\limits_{i=1}^{n}{a_i} \le k$. -----Examples----- Input 4 1 10 20 2 69 6 9 7 8 1 2 1 3 1 2 1 10 1 1 2 3 1 2 6 1 6 8 10 Output 10 0 2 7 -----Note----- In the first test case, you should decrease $a_1$ $10$ times to get the sum lower or equal to $k = 10$. In the second test case, the sum of array $a$ is already less or equal to $69$, so you don't need to change it. In the third test case, you can, for example: set $a_4 = a_3 = 1$; decrease $a_4$ by one, and get $a_4 = 0$. As a result, you'll get array $[1, 2, 1, 0, 1, 2, 1]$ with sum less or equal to $8$ in $1 + 1 = 2$ steps. In the fourth test case, you can, for example: choose $a_7$ and decrease in by one $3$ times; you'll get $a_7 = -2$; choose $4$ elements $a_6$, $a_8$, $a_9$ and $a_{10}$ and them equal to $a_7 = -2$. As a result, you'll get array $[1, 2, 3, 1, 2, -2, -2, -2, -2, -2]$ with sum less or equal to $1$ in $3 + 4 = 7$ steps.
t = int(input()) for i in range(t): n, k = map(int, input().split()) arr = list(map(int, input().split())) prefix = [(0) for i in range(n)] arr.sort() prefix[0] = 0 ans = float("inf") for i in range(1, n): prefix[i] = arr[i] + prefix[i - 1] for i in range(n - 1, -1, -1): s = prefix[i] diff = k - s req = diff // (n - i) for j in range(req - 4, req + 4): cnt = 0 if j * (n - i) + s > k: continue if j <= arr[0]: cnt += arr[0] - j cnt += n - i - 1 ans = min(ans, cnt) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an integer array $a_1, a_2, \dots, a_n$ and integer $k$. In one step you can either choose some index $i$ and decrease $a_i$ by one (make $a_i = a_i - 1$); or choose two indices $i$ and $j$ and set $a_i$ equal to $a_j$ (make $a_i = a_j$). What is the minimum number of steps you need to make the sum of array $\sum\limits_{i=1}^{n}{a_i} \le k$? (You are allowed to make values of array negative). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^{15}$) — the size of array $a$ and upper bound on its sum. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the array itself. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print one integer — the minimum number of steps to make $\sum\limits_{i=1}^{n}{a_i} \le k$. -----Examples----- Input 4 1 10 20 2 69 6 9 7 8 1 2 1 3 1 2 1 10 1 1 2 3 1 2 6 1 6 8 10 Output 10 0 2 7 -----Note----- In the first test case, you should decrease $a_1$ $10$ times to get the sum lower or equal to $k = 10$. In the second test case, the sum of array $a$ is already less or equal to $69$, so you don't need to change it. In the third test case, you can, for example: set $a_4 = a_3 = 1$; decrease $a_4$ by one, and get $a_4 = 0$. As a result, you'll get array $[1, 2, 1, 0, 1, 2, 1]$ with sum less or equal to $8$ in $1 + 1 = 2$ steps. In the fourth test case, you can, for example: choose $a_7$ and decrease in by one $3$ times; you'll get $a_7 = -2$; choose $4$ elements $a_6$, $a_8$, $a_9$ and $a_{10}$ and them equal to $a_7 = -2$. As a result, you'll get array $[1, 2, 3, 1, 2, -2, -2, -2, -2, -2]$ with sum less or equal to $1$ in $3 + 4 = 7$ steps.
t = int(input()) for i in range(t): n, k = map(int, input().split()) arr = list(map(int, input().split())) arr.sort() sm = sum(arr) if sm <= k: print(0) else: mn = sm - k x = arr[-1] cnt = 2 for i in range(2, n): arr[i] += arr[i - 1] for i in range(n - 2, -1, -1): s = arr[i] if i == 0: s = 0 df = k - s df = df // cnt if df > arr[0]: df = arr[0] mn = min(mn, cnt - 1 + arr[0] - df) cnt += 1 print(mn)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an integer array $a_1, a_2, \dots, a_n$ and integer $k$. In one step you can either choose some index $i$ and decrease $a_i$ by one (make $a_i = a_i - 1$); or choose two indices $i$ and $j$ and set $a_i$ equal to $a_j$ (make $a_i = a_j$). What is the minimum number of steps you need to make the sum of array $\sum\limits_{i=1}^{n}{a_i} \le k$? (You are allowed to make values of array negative). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^{15}$) — the size of array $a$ and upper bound on its sum. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the array itself. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print one integer — the minimum number of steps to make $\sum\limits_{i=1}^{n}{a_i} \le k$. -----Examples----- Input 4 1 10 20 2 69 6 9 7 8 1 2 1 3 1 2 1 10 1 1 2 3 1 2 6 1 6 8 10 Output 10 0 2 7 -----Note----- In the first test case, you should decrease $a_1$ $10$ times to get the sum lower or equal to $k = 10$. In the second test case, the sum of array $a$ is already less or equal to $69$, so you don't need to change it. In the third test case, you can, for example: set $a_4 = a_3 = 1$; decrease $a_4$ by one, and get $a_4 = 0$. As a result, you'll get array $[1, 2, 1, 0, 1, 2, 1]$ with sum less or equal to $8$ in $1 + 1 = 2$ steps. In the fourth test case, you can, for example: choose $a_7$ and decrease in by one $3$ times; you'll get $a_7 = -2$; choose $4$ elements $a_6$, $a_8$, $a_9$ and $a_{10}$ and them equal to $a_7 = -2$. As a result, you'll get array $[1, 2, 3, 1, 2, -2, -2, -2, -2, -2]$ with sum less or equal to $1$ in $3 + 4 = 7$ steps.
def better_floor(a, b): c = a // b while c * b > a: c -= 1 return c def solve(a, n, k): result = 1e18 a.sort() pSum = [a[0]] for ii in range(1, len(a)): pSum.append(pSum[ii - 1] + a[ii]) for y in range(len(a)): x = a[0] - better_floor(k - pSum[n - 1 - y] + a[0], y + 1) result = min(result, max(0, x) + y) print(result) for test in range(int(input())): n, k = map(int, input().split()) a = list(map(int, input().split())) solve(a, n, k)
FUNC_DEF ASSIGN VAR BIN_OP VAR VAR WHILE BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 integer array $a_1, a_2, \dots, a_n$ and integer $k$. In one step you can either choose some index $i$ and decrease $a_i$ by one (make $a_i = a_i - 1$); or choose two indices $i$ and $j$ and set $a_i$ equal to $a_j$ (make $a_i = a_j$). What is the minimum number of steps you need to make the sum of array $\sum\limits_{i=1}^{n}{a_i} \le k$? (You are allowed to make values of array negative). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^{15}$) — the size of array $a$ and upper bound on its sum. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the array itself. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print one integer — the minimum number of steps to make $\sum\limits_{i=1}^{n}{a_i} \le k$. -----Examples----- Input 4 1 10 20 2 69 6 9 7 8 1 2 1 3 1 2 1 10 1 1 2 3 1 2 6 1 6 8 10 Output 10 0 2 7 -----Note----- In the first test case, you should decrease $a_1$ $10$ times to get the sum lower or equal to $k = 10$. In the second test case, the sum of array $a$ is already less or equal to $69$, so you don't need to change it. In the third test case, you can, for example: set $a_4 = a_3 = 1$; decrease $a_4$ by one, and get $a_4 = 0$. As a result, you'll get array $[1, 2, 1, 0, 1, 2, 1]$ with sum less or equal to $8$ in $1 + 1 = 2$ steps. In the fourth test case, you can, for example: choose $a_7$ and decrease in by one $3$ times; you'll get $a_7 = -2$; choose $4$ elements $a_6$, $a_8$, $a_9$ and $a_{10}$ and them equal to $a_7 = -2$. As a result, you'll get array $[1, 2, 3, 1, 2, -2, -2, -2, -2, -2]$ with sum less or equal to $1$ in $3 + 4 = 7$ steps.
t = int(input()) for _ in range(t): n, k = map(int, input().split()) arr = list(map(int, input().split())) arr.sort() tmpTarget = sum(arr) arrIfSet = [None for _ in range(n + 1)] arrIfSet[-1] = tmpTarget for i in range(n - 1, -1, -1): tmpTarget -= arr[i] - arr[0] arrIfSet[i] = tmpTarget def solve(): if sum(arr) <= k: return 0 if len(arr) == 1: return max(0, arr[0] - k) result = float("inf") for numSets, num in enumerate(reversed(arrIfSet)): i = numSets + 1 if numSets == n: break numDecreases = max((num - k + (i - 1)) // i, 0) result = min(result, numDecreases + numSets) return result result = solve() print(result)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR 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 NONE VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
You are given an integer array $a_1, a_2, \dots, a_n$ and integer $k$. In one step you can either choose some index $i$ and decrease $a_i$ by one (make $a_i = a_i - 1$); or choose two indices $i$ and $j$ and set $a_i$ equal to $a_j$ (make $a_i = a_j$). What is the minimum number of steps you need to make the sum of array $\sum\limits_{i=1}^{n}{a_i} \le k$? (You are allowed to make values of array negative). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^{15}$) — the size of array $a$ and upper bound on its sum. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the array itself. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print one integer — the minimum number of steps to make $\sum\limits_{i=1}^{n}{a_i} \le k$. -----Examples----- Input 4 1 10 20 2 69 6 9 7 8 1 2 1 3 1 2 1 10 1 1 2 3 1 2 6 1 6 8 10 Output 10 0 2 7 -----Note----- In the first test case, you should decrease $a_1$ $10$ times to get the sum lower or equal to $k = 10$. In the second test case, the sum of array $a$ is already less or equal to $69$, so you don't need to change it. In the third test case, you can, for example: set $a_4 = a_3 = 1$; decrease $a_4$ by one, and get $a_4 = 0$. As a result, you'll get array $[1, 2, 1, 0, 1, 2, 1]$ with sum less or equal to $8$ in $1 + 1 = 2$ steps. In the fourth test case, you can, for example: choose $a_7$ and decrease in by one $3$ times; you'll get $a_7 = -2$; choose $4$ elements $a_6$, $a_8$, $a_9$ and $a_{10}$ and them equal to $a_7 = -2$. As a result, you'll get array $[1, 2, 3, 1, 2, -2, -2, -2, -2, -2]$ with sum less or equal to $1$ in $3 + 4 = 7$ steps.
def answer(arr, n, k): arr.sort() sum1 = sum(arr) if sum1 <= k: return 0 minimum = sum1 - k ans = minimum total = 0 for i in range(n - 1, 0, -1): total += arr[i] - arr[0] rem = minimum - total if rem <= 0: ans = min(ans, n - i) return ans f = (rem + (n - i)) // (n - i + 1) v = f + n - i if v <= ans: ans = v return ans t = int(input()) for K in range(t): inp = input().split() n = int(inp[0]) k = int(inp[1]) arr = [] inp = input().split() for i in inp: arr.append(int(i)) print(answer(arr, n, k))
FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
You are given an integer array $a_1, a_2, \dots, a_n$ and integer $k$. In one step you can either choose some index $i$ and decrease $a_i$ by one (make $a_i = a_i - 1$); or choose two indices $i$ and $j$ and set $a_i$ equal to $a_j$ (make $a_i = a_j$). What is the minimum number of steps you need to make the sum of array $\sum\limits_{i=1}^{n}{a_i} \le k$? (You are allowed to make values of array negative). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^{15}$) — the size of array $a$ and upper bound on its sum. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the array itself. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print one integer — the minimum number of steps to make $\sum\limits_{i=1}^{n}{a_i} \le k$. -----Examples----- Input 4 1 10 20 2 69 6 9 7 8 1 2 1 3 1 2 1 10 1 1 2 3 1 2 6 1 6 8 10 Output 10 0 2 7 -----Note----- In the first test case, you should decrease $a_1$ $10$ times to get the sum lower or equal to $k = 10$. In the second test case, the sum of array $a$ is already less or equal to $69$, so you don't need to change it. In the third test case, you can, for example: set $a_4 = a_3 = 1$; decrease $a_4$ by one, and get $a_4 = 0$. As a result, you'll get array $[1, 2, 1, 0, 1, 2, 1]$ with sum less or equal to $8$ in $1 + 1 = 2$ steps. In the fourth test case, you can, for example: choose $a_7$ and decrease in by one $3$ times; you'll get $a_7 = -2$; choose $4$ elements $a_6$, $a_8$, $a_9$ and $a_{10}$ and them equal to $a_7 = -2$. As a result, you'll get array $[1, 2, 3, 1, 2, -2, -2, -2, -2, -2]$ with sum less or equal to $1$ in $3 + 4 = 7$ steps.
for _ in range(int(input())): N, K = map(int, input().split()) A = list(map(int, input().split())) A.sort() a = A[0] if len(A) == 1: print(max(0, a - K)) continue S = [a] for v in A[1:]: S.append(S[-1] + v) ans = 1 << 60 for l in range(N): x = max(0, -(-(S[-l - 1] + l * a - K) // (l + 1))) ans = min(ans, x + l) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR LIST VAR FOR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You are given an integer array $a_1, a_2, \dots, a_n$ and integer $k$. In one step you can either choose some index $i$ and decrease $a_i$ by one (make $a_i = a_i - 1$); or choose two indices $i$ and $j$ and set $a_i$ equal to $a_j$ (make $a_i = a_j$). What is the minimum number of steps you need to make the sum of array $\sum\limits_{i=1}^{n}{a_i} \le k$? (You are allowed to make values of array negative). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^{15}$) — the size of array $a$ and upper bound on its sum. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the array itself. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print one integer — the minimum number of steps to make $\sum\limits_{i=1}^{n}{a_i} \le k$. -----Examples----- Input 4 1 10 20 2 69 6 9 7 8 1 2 1 3 1 2 1 10 1 1 2 3 1 2 6 1 6 8 10 Output 10 0 2 7 -----Note----- In the first test case, you should decrease $a_1$ $10$ times to get the sum lower or equal to $k = 10$. In the second test case, the sum of array $a$ is already less or equal to $69$, so you don't need to change it. In the third test case, you can, for example: set $a_4 = a_3 = 1$; decrease $a_4$ by one, and get $a_4 = 0$. As a result, you'll get array $[1, 2, 1, 0, 1, 2, 1]$ with sum less or equal to $8$ in $1 + 1 = 2$ steps. In the fourth test case, you can, for example: choose $a_7$ and decrease in by one $3$ times; you'll get $a_7 = -2$; choose $4$ elements $a_6$, $a_8$, $a_9$ and $a_{10}$ and them equal to $a_7 = -2$. As a result, you'll get array $[1, 2, 3, 1, 2, -2, -2, -2, -2, -2]$ with sum less or equal to $1$ in $3 + 4 = 7$ steps.
from sys import stdin for _ in range(int(stdin.readline())): n, k = map(int, stdin.readline().split()) arr = list(map(int, stdin.readline().split())) arr.sort() m = arr[0] t = 0 for i in arr[1:]: t += i ans = m + t - k if ans < 0: ans = 0 sub = 0 j = n - 1 for x in range(1, n): sub += arr[j] s = k - t + sub yy = s // (x + 1) if m <= yy: y = 0 else: y = m - yy ans = min(ans, x + y) j -= 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an integer array $a_1, a_2, \dots, a_n$ and integer $k$. In one step you can either choose some index $i$ and decrease $a_i$ by one (make $a_i = a_i - 1$); or choose two indices $i$ and $j$ and set $a_i$ equal to $a_j$ (make $a_i = a_j$). What is the minimum number of steps you need to make the sum of array $\sum\limits_{i=1}^{n}{a_i} \le k$? (You are allowed to make values of array negative). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^{15}$) — the size of array $a$ and upper bound on its sum. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the array itself. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print one integer — the minimum number of steps to make $\sum\limits_{i=1}^{n}{a_i} \le k$. -----Examples----- Input 4 1 10 20 2 69 6 9 7 8 1 2 1 3 1 2 1 10 1 1 2 3 1 2 6 1 6 8 10 Output 10 0 2 7 -----Note----- In the first test case, you should decrease $a_1$ $10$ times to get the sum lower or equal to $k = 10$. In the second test case, the sum of array $a$ is already less or equal to $69$, so you don't need to change it. In the third test case, you can, for example: set $a_4 = a_3 = 1$; decrease $a_4$ by one, and get $a_4 = 0$. As a result, you'll get array $[1, 2, 1, 0, 1, 2, 1]$ with sum less or equal to $8$ in $1 + 1 = 2$ steps. In the fourth test case, you can, for example: choose $a_7$ and decrease in by one $3$ times; you'll get $a_7 = -2$; choose $4$ elements $a_6$, $a_8$, $a_9$ and $a_{10}$ and them equal to $a_7 = -2$. As a result, you'll get array $[1, 2, 3, 1, 2, -2, -2, -2, -2, -2]$ with sum less or equal to $1$ in $3 + 4 = 7$ steps.
t = int(input()) while t: n, k = map(int, input().split()) a = list(map(int, input().split())) a.sort() pre = [i for i in a] for i in range(1, n): pre[i] += pre[i - 1] if pre[-1] < k: print(0) else: ans = pre[-1] - k i = n - 1 cnt = 0 while i > 0: cnt += 1 sm = k - (pre[i - 1] - pre[0]) num = sm // (cnt + 1) if (num + 1) * (cnt + 1) + (pre[i - 1] - pre[0]) <= k: num += 1 elif num * (cnt + 1) + (pre[i - 1] - pre[0]) > k: num -= 1 tmp = cnt if num < pre[0]: tmp += pre[0] - num ans = min(ans, tmp) i -= 1 print(ans) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE 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 VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
You are given an integer array $a_1, a_2, \dots, a_n$ and integer $k$. In one step you can either choose some index $i$ and decrease $a_i$ by one (make $a_i = a_i - 1$); or choose two indices $i$ and $j$ and set $a_i$ equal to $a_j$ (make $a_i = a_j$). What is the minimum number of steps you need to make the sum of array $\sum\limits_{i=1}^{n}{a_i} \le k$? (You are allowed to make values of array negative). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^{15}$) — the size of array $a$ and upper bound on its sum. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the array itself. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print one integer — the minimum number of steps to make $\sum\limits_{i=1}^{n}{a_i} \le k$. -----Examples----- Input 4 1 10 20 2 69 6 9 7 8 1 2 1 3 1 2 1 10 1 1 2 3 1 2 6 1 6 8 10 Output 10 0 2 7 -----Note----- In the first test case, you should decrease $a_1$ $10$ times to get the sum lower or equal to $k = 10$. In the second test case, the sum of array $a$ is already less or equal to $69$, so you don't need to change it. In the third test case, you can, for example: set $a_4 = a_3 = 1$; decrease $a_4$ by one, and get $a_4 = 0$. As a result, you'll get array $[1, 2, 1, 0, 1, 2, 1]$ with sum less or equal to $8$ in $1 + 1 = 2$ steps. In the fourth test case, you can, for example: choose $a_7$ and decrease in by one $3$ times; you'll get $a_7 = -2$; choose $4$ elements $a_6$, $a_8$, $a_9$ and $a_{10}$ and them equal to $a_7 = -2$. As a result, you'll get array $[1, 2, 3, 1, 2, -2, -2, -2, -2, -2]$ with sum less or equal to $1$ in $3 + 4 = 7$ steps.
_ = int(input()) for __ in range(_): n, k = map(int, input().split()) arr = list(map(int, input().split())) arr.sort() li = arr[0] s = sum(arr) if s <= k: print(0) continue if n == 1: print(li - k) continue avg = k // n wyn = s - k tmp_wyn = 10**18 tmp_s_1 = li if li > avg: wyn = min(wyn, li - avg + n - 1) else: wyn = min(wyn, n - 1) for x in range(1, n): tmp_s_1 += arr[x] if tmp_s_1 + (n - x - 1) * li <= k: wyn = min(wyn, n - x - 1) tmp_s = 0 for x in range(1, n): tmp_s += arr[x] diff = (k - tmp_s) // (n - x) if diff >= li: tmp_wyn = n - x - 1 else: tmp_wyn = li - diff + n - x - 1 wyn = min(wyn, tmp_wyn) print(wyn)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR IF BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an integer array $a_1, a_2, \dots, a_n$ and integer $k$. In one step you can either choose some index $i$ and decrease $a_i$ by one (make $a_i = a_i - 1$); or choose two indices $i$ and $j$ and set $a_i$ equal to $a_j$ (make $a_i = a_j$). What is the minimum number of steps you need to make the sum of array $\sum\limits_{i=1}^{n}{a_i} \le k$? (You are allowed to make values of array negative). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^{15}$) — the size of array $a$ and upper bound on its sum. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the array itself. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print one integer — the minimum number of steps to make $\sum\limits_{i=1}^{n}{a_i} \le k$. -----Examples----- Input 4 1 10 20 2 69 6 9 7 8 1 2 1 3 1 2 1 10 1 1 2 3 1 2 6 1 6 8 10 Output 10 0 2 7 -----Note----- In the first test case, you should decrease $a_1$ $10$ times to get the sum lower or equal to $k = 10$. In the second test case, the sum of array $a$ is already less or equal to $69$, so you don't need to change it. In the third test case, you can, for example: set $a_4 = a_3 = 1$; decrease $a_4$ by one, and get $a_4 = 0$. As a result, you'll get array $[1, 2, 1, 0, 1, 2, 1]$ with sum less or equal to $8$ in $1 + 1 = 2$ steps. In the fourth test case, you can, for example: choose $a_7$ and decrease in by one $3$ times; you'll get $a_7 = -2$; choose $4$ elements $a_6$, $a_8$, $a_9$ and $a_{10}$ and them equal to $a_7 = -2$. As a result, you'll get array $[1, 2, 3, 1, 2, -2, -2, -2, -2, -2]$ with sum less or equal to $1$ in $3 + 4 = 7$ steps.
for _ in range(int(input())): n, k = map(int, input().split()) arr = list(map(int, input().split())) arr.sort() if sum(arr) <= k: print(0) else: ans = 10**20 pref = [0] * n pref[0] = arr[0] for i in range(1, len(arr)): pref[i] = pref[i - 1] + arr[i] for i in range(0, n): Sum = k - (pref[n - i - 1] - pref[0]) need = Sum // (i + 1) temp = i if need < arr[0]: temp += arr[0] - need ans = min(ans, temp) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an integer array $a_1, a_2, \dots, a_n$ and integer $k$. In one step you can either choose some index $i$ and decrease $a_i$ by one (make $a_i = a_i - 1$); or choose two indices $i$ and $j$ and set $a_i$ equal to $a_j$ (make $a_i = a_j$). What is the minimum number of steps you need to make the sum of array $\sum\limits_{i=1}^{n}{a_i} \le k$? (You are allowed to make values of array negative). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^{15}$) — the size of array $a$ and upper bound on its sum. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the array itself. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print one integer — the minimum number of steps to make $\sum\limits_{i=1}^{n}{a_i} \le k$. -----Examples----- Input 4 1 10 20 2 69 6 9 7 8 1 2 1 3 1 2 1 10 1 1 2 3 1 2 6 1 6 8 10 Output 10 0 2 7 -----Note----- In the first test case, you should decrease $a_1$ $10$ times to get the sum lower or equal to $k = 10$. In the second test case, the sum of array $a$ is already less or equal to $69$, so you don't need to change it. In the third test case, you can, for example: set $a_4 = a_3 = 1$; decrease $a_4$ by one, and get $a_4 = 0$. As a result, you'll get array $[1, 2, 1, 0, 1, 2, 1]$ with sum less or equal to $8$ in $1 + 1 = 2$ steps. In the fourth test case, you can, for example: choose $a_7$ and decrease in by one $3$ times; you'll get $a_7 = -2$; choose $4$ elements $a_6$, $a_8$, $a_9$ and $a_{10}$ and them equal to $a_7 = -2$. As a result, you'll get array $[1, 2, 3, 1, 2, -2, -2, -2, -2, -2]$ with sum less or equal to $1$ in $3 + 4 = 7$ steps.
R = lambda: map(int, input().split()) (t,) = R() while t: t -= 1 n, k = R() a = sorted(R()) r = 2000000000.0 for x in a: k -= x n -= 1 r = min(r, n - min(0, k - a[0] * n) // (n + 1)) print(r)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an integer array $a_1, a_2, \dots, a_n$ and integer $k$. In one step you can either choose some index $i$ and decrease $a_i$ by one (make $a_i = a_i - 1$); or choose two indices $i$ and $j$ and set $a_i$ equal to $a_j$ (make $a_i = a_j$). What is the minimum number of steps you need to make the sum of array $\sum\limits_{i=1}^{n}{a_i} \le k$? (You are allowed to make values of array negative). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^{15}$) — the size of array $a$ and upper bound on its sum. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the array itself. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print one integer — the minimum number of steps to make $\sum\limits_{i=1}^{n}{a_i} \le k$. -----Examples----- Input 4 1 10 20 2 69 6 9 7 8 1 2 1 3 1 2 1 10 1 1 2 3 1 2 6 1 6 8 10 Output 10 0 2 7 -----Note----- In the first test case, you should decrease $a_1$ $10$ times to get the sum lower or equal to $k = 10$. In the second test case, the sum of array $a$ is already less or equal to $69$, so you don't need to change it. In the third test case, you can, for example: set $a_4 = a_3 = 1$; decrease $a_4$ by one, and get $a_4 = 0$. As a result, you'll get array $[1, 2, 1, 0, 1, 2, 1]$ with sum less or equal to $8$ in $1 + 1 = 2$ steps. In the fourth test case, you can, for example: choose $a_7$ and decrease in by one $3$ times; you'll get $a_7 = -2$; choose $4$ elements $a_6$, $a_8$, $a_9$ and $a_{10}$ and them equal to $a_7 = -2$. As a result, you'll get array $[1, 2, 3, 1, 2, -2, -2, -2, -2, -2]$ with sum less or equal to $1$ in $3 + 4 = 7$ steps.
for _ in range(int(input())): n, k = list(map(int, input().split())) a = list(map(int, input().split())) a.sort() total = sum(a) l = [] for i in range(1, n): l.append(a[i] - a[0]) t = 10**18 ans = 0 while len(l) > 0 and total > k: t = min(t, ans + (total - k + ans) // (ans + 1)) total -= l.pop() ans += 1 if total > k: ans += (total - k + ans) // (ans + 1) print(min(ans, t))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given an integer array $a_1, a_2, \dots, a_n$ and integer $k$. In one step you can either choose some index $i$ and decrease $a_i$ by one (make $a_i = a_i - 1$); or choose two indices $i$ and $j$ and set $a_i$ equal to $a_j$ (make $a_i = a_j$). What is the minimum number of steps you need to make the sum of array $\sum\limits_{i=1}^{n}{a_i} \le k$? (You are allowed to make values of array negative). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^{15}$) — the size of array $a$ and upper bound on its sum. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the array itself. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print one integer — the minimum number of steps to make $\sum\limits_{i=1}^{n}{a_i} \le k$. -----Examples----- Input 4 1 10 20 2 69 6 9 7 8 1 2 1 3 1 2 1 10 1 1 2 3 1 2 6 1 6 8 10 Output 10 0 2 7 -----Note----- In the first test case, you should decrease $a_1$ $10$ times to get the sum lower or equal to $k = 10$. In the second test case, the sum of array $a$ is already less or equal to $69$, so you don't need to change it. In the third test case, you can, for example: set $a_4 = a_3 = 1$; decrease $a_4$ by one, and get $a_4 = 0$. As a result, you'll get array $[1, 2, 1, 0, 1, 2, 1]$ with sum less or equal to $8$ in $1 + 1 = 2$ steps. In the fourth test case, you can, for example: choose $a_7$ and decrease in by one $3$ times; you'll get $a_7 = -2$; choose $4$ elements $a_6$, $a_8$, $a_9$ and $a_{10}$ and them equal to $a_7 = -2$. As a result, you'll get array $[1, 2, 3, 1, 2, -2, -2, -2, -2, -2]$ with sum less or equal to $1$ in $3 + 4 = 7$ steps.
def udiv(a, b): if a % b == 0: return a // b else: return a // b + 1 for kfjdkf in range(int(input())): n, k = list(map(int, input().split())) a = list(map(int, input().split())) a.sort() sum = 0 for i in a: sum += i ans = 0 i = n - 1 mn = a[0] if sum <= k: print(0) continue if sum == k + 1: print(1) continue if n == 1: print(max(sum - k, 0)) continue b = [] s = 0 for i in range(-1, -n - 1, -1): s += a[i] b.append(s) ans = sum - k req = sum - k for i in range(n - 1): steps = i + 1 dec = b[i] - a[0] * (i + 1) if dec < req: steps += udiv(req - dec, i + 2) ans = min(ans, steps) print(ans)
FUNC_DEF IF BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR VAR RETURN BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an integer array $a_1, a_2, \dots, a_n$ and integer $k$. In one step you can either choose some index $i$ and decrease $a_i$ by one (make $a_i = a_i - 1$); or choose two indices $i$ and $j$ and set $a_i$ equal to $a_j$ (make $a_i = a_j$). What is the minimum number of steps you need to make the sum of array $\sum\limits_{i=1}^{n}{a_i} \le k$? (You are allowed to make values of array negative). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^{15}$) — the size of array $a$ and upper bound on its sum. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the array itself. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print one integer — the minimum number of steps to make $\sum\limits_{i=1}^{n}{a_i} \le k$. -----Examples----- Input 4 1 10 20 2 69 6 9 7 8 1 2 1 3 1 2 1 10 1 1 2 3 1 2 6 1 6 8 10 Output 10 0 2 7 -----Note----- In the first test case, you should decrease $a_1$ $10$ times to get the sum lower or equal to $k = 10$. In the second test case, the sum of array $a$ is already less or equal to $69$, so you don't need to change it. In the third test case, you can, for example: set $a_4 = a_3 = 1$; decrease $a_4$ by one, and get $a_4 = 0$. As a result, you'll get array $[1, 2, 1, 0, 1, 2, 1]$ with sum less or equal to $8$ in $1 + 1 = 2$ steps. In the fourth test case, you can, for example: choose $a_7$ and decrease in by one $3$ times; you'll get $a_7 = -2$; choose $4$ elements $a_6$, $a_8$, $a_9$ and $a_{10}$ and them equal to $a_7 = -2$. As a result, you'll get array $[1, 2, 3, 1, 2, -2, -2, -2, -2, -2]$ with sum less or equal to $1$ in $3 + 4 = 7$ steps.
import sys int1 = lambda x: int(x) - 1 pDB = lambda *x: print(*x, end="\n", file=sys.stderr) p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr) def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def LI1(): return list(map(int1, sys.stdin.readline().split())) def LLI1(rows_number): return [LI1() for _ in range(rows_number)] def SI(): return sys.stdin.readline().rstrip() inf = 4294967295 md = 998244353 def solve(): n, k = LI() aa = LI() if sum(aa) <= k: print(0) return aa.sort() s = 0 ans = inf for i in range(1, n): d = k - s c = n + 1 - i x = d // c cur = max(0, aa[0] - x) + c - 1 if cur < ans: ans = cur s += aa[i] x = k - s cur = max(0, aa[0] - x) if cur < ans: ans = cur print(ans) for testcase in range(II()): solve()
IMPORT ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING VAR ASSIGN VAR FUNC_CALL VAR VAR STRING STRING VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are given an integer array $a_1, a_2, \dots, a_n$ and integer $k$. In one step you can either choose some index $i$ and decrease $a_i$ by one (make $a_i = a_i - 1$); or choose two indices $i$ and $j$ and set $a_i$ equal to $a_j$ (make $a_i = a_j$). What is the minimum number of steps you need to make the sum of array $\sum\limits_{i=1}^{n}{a_i} \le k$? (You are allowed to make values of array negative). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^{15}$) — the size of array $a$ and upper bound on its sum. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the array itself. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print one integer — the minimum number of steps to make $\sum\limits_{i=1}^{n}{a_i} \le k$. -----Examples----- Input 4 1 10 20 2 69 6 9 7 8 1 2 1 3 1 2 1 10 1 1 2 3 1 2 6 1 6 8 10 Output 10 0 2 7 -----Note----- In the first test case, you should decrease $a_1$ $10$ times to get the sum lower or equal to $k = 10$. In the second test case, the sum of array $a$ is already less or equal to $69$, so you don't need to change it. In the third test case, you can, for example: set $a_4 = a_3 = 1$; decrease $a_4$ by one, and get $a_4 = 0$. As a result, you'll get array $[1, 2, 1, 0, 1, 2, 1]$ with sum less or equal to $8$ in $1 + 1 = 2$ steps. In the fourth test case, you can, for example: choose $a_7$ and decrease in by one $3$ times; you'll get $a_7 = -2$; choose $4$ elements $a_6$, $a_8$, $a_9$ and $a_{10}$ and them equal to $a_7 = -2$. As a result, you'll get array $[1, 2, 3, 1, 2, -2, -2, -2, -2, -2]$ with sum less or equal to $1$ in $3 + 4 = 7$ steps.
import sys def func(): n, k = map(int, input().split()) l = list(map(int, input().split())) sm = sum(l) l.sort() s = [] for i in range(1, n): s.append(l[i] - l[0]) count = 0 mn = sys.maxsize while len(s) and sm > k: mn = min(mn, count + (sm - k + count) // (count + 1)) sm -= s[-1] s.pop() count += 1 if sm > k: count += (sm - k + count) // (count + 1) return min(mn, count) t = int(input()) for i in range(t): print(func())
IMPORT 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 ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
You are given an integer array $a_1, a_2, \dots, a_n$ and integer $k$. In one step you can either choose some index $i$ and decrease $a_i$ by one (make $a_i = a_i - 1$); or choose two indices $i$ and $j$ and set $a_i$ equal to $a_j$ (make $a_i = a_j$). What is the minimum number of steps you need to make the sum of array $\sum\limits_{i=1}^{n}{a_i} \le k$? (You are allowed to make values of array negative). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^{15}$) — the size of array $a$ and upper bound on its sum. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the array itself. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print one integer — the minimum number of steps to make $\sum\limits_{i=1}^{n}{a_i} \le k$. -----Examples----- Input 4 1 10 20 2 69 6 9 7 8 1 2 1 3 1 2 1 10 1 1 2 3 1 2 6 1 6 8 10 Output 10 0 2 7 -----Note----- In the first test case, you should decrease $a_1$ $10$ times to get the sum lower or equal to $k = 10$. In the second test case, the sum of array $a$ is already less or equal to $69$, so you don't need to change it. In the third test case, you can, for example: set $a_4 = a_3 = 1$; decrease $a_4$ by one, and get $a_4 = 0$. As a result, you'll get array $[1, 2, 1, 0, 1, 2, 1]$ with sum less or equal to $8$ in $1 + 1 = 2$ steps. In the fourth test case, you can, for example: choose $a_7$ and decrease in by one $3$ times; you'll get $a_7 = -2$; choose $4$ elements $a_6$, $a_8$, $a_9$ and $a_{10}$ and them equal to $a_7 = -2$. As a result, you'll get array $[1, 2, 3, 1, 2, -2, -2, -2, -2, -2]$ with sum less or equal to $1$ in $3 + 4 = 7$ steps.
import sys input = sys.stdin.readline def inp(): return int(input()) def st(): return input().rstrip("\n") def lis(): return list(map(int, input().split())) def ma(): return map(int, input().split()) t = inp() while t: t -= 1 n, k = ma() a = lis() a.sort() cur = sum(a) - a[0] if cur + a[0] <= k: print(0) continue z = sum(a) suff = a[0] ans = float("inf") for i in range(n): if i: suff += a[n - i] total = z - suff total = k - total yval = 1 yval += (total - 1) // (i + 1) l = -abs(total) r = abs(total) while l <= r: mid = (l + r) // 2 val = (i + 1) * mid if val <= total: ans1 = mid l = mid + 1 else: r = mid - 1 ans = min(ans, max(0, a[0] - ans1) + i) print(ans)
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
You are given an integer array $a_1, a_2, \dots, a_n$ and integer $k$. In one step you can either choose some index $i$ and decrease $a_i$ by one (make $a_i = a_i - 1$); or choose two indices $i$ and $j$ and set $a_i$ equal to $a_j$ (make $a_i = a_j$). What is the minimum number of steps you need to make the sum of array $\sum\limits_{i=1}^{n}{a_i} \le k$? (You are allowed to make values of array negative). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^{15}$) — the size of array $a$ and upper bound on its sum. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the array itself. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print one integer — the minimum number of steps to make $\sum\limits_{i=1}^{n}{a_i} \le k$. -----Examples----- Input 4 1 10 20 2 69 6 9 7 8 1 2 1 3 1 2 1 10 1 1 2 3 1 2 6 1 6 8 10 Output 10 0 2 7 -----Note----- In the first test case, you should decrease $a_1$ $10$ times to get the sum lower or equal to $k = 10$. In the second test case, the sum of array $a$ is already less or equal to $69$, so you don't need to change it. In the third test case, you can, for example: set $a_4 = a_3 = 1$; decrease $a_4$ by one, and get $a_4 = 0$. As a result, you'll get array $[1, 2, 1, 0, 1, 2, 1]$ with sum less or equal to $8$ in $1 + 1 = 2$ steps. In the fourth test case, you can, for example: choose $a_7$ and decrease in by one $3$ times; you'll get $a_7 = -2$; choose $4$ elements $a_6$, $a_8$, $a_9$ and $a_{10}$ and them equal to $a_7 = -2$. As a result, you'll get array $[1, 2, 3, 1, 2, -2, -2, -2, -2, -2]$ with sum less or equal to $1$ in $3 + 4 = 7$ steps.
for _ in range(int(input())): n, k = map(int, input().split()) l = list(map(int, input().split())) l = sorted(l) pre = [0] * (n + 1) for i in range(1, n): pre[i] = pre[i - 1] + l[i] ans = int(1000000000000000.0 + 1) for i in range(n - 1, -1, -1): s = pre[i] cnt = n - i req = k - s req = req // cnt for x in range(req - 5, req + 6): if s + x * cnt > k: continue curr = 0 if x <= l[0]: curr += l[0] - x curr += cnt - 1 ans = min(ans, curr) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an integer array $a_1, a_2, \dots, a_n$ and integer $k$. In one step you can either choose some index $i$ and decrease $a_i$ by one (make $a_i = a_i - 1$); or choose two indices $i$ and $j$ and set $a_i$ equal to $a_j$ (make $a_i = a_j$). What is the minimum number of steps you need to make the sum of array $\sum\limits_{i=1}^{n}{a_i} \le k$? (You are allowed to make values of array negative). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^{15}$) — the size of array $a$ and upper bound on its sum. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the array itself. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print one integer — the minimum number of steps to make $\sum\limits_{i=1}^{n}{a_i} \le k$. -----Examples----- Input 4 1 10 20 2 69 6 9 7 8 1 2 1 3 1 2 1 10 1 1 2 3 1 2 6 1 6 8 10 Output 10 0 2 7 -----Note----- In the first test case, you should decrease $a_1$ $10$ times to get the sum lower or equal to $k = 10$. In the second test case, the sum of array $a$ is already less or equal to $69$, so you don't need to change it. In the third test case, you can, for example: set $a_4 = a_3 = 1$; decrease $a_4$ by one, and get $a_4 = 0$. As a result, you'll get array $[1, 2, 1, 0, 1, 2, 1]$ with sum less or equal to $8$ in $1 + 1 = 2$ steps. In the fourth test case, you can, for example: choose $a_7$ and decrease in by one $3$ times; you'll get $a_7 = -2$; choose $4$ elements $a_6$, $a_8$, $a_9$ and $a_{10}$ and them equal to $a_7 = -2$. As a result, you'll get array $[1, 2, 3, 1, 2, -2, -2, -2, -2, -2]$ with sum less or equal to $1$ in $3 + 4 = 7$ steps.
from sys import stdin input = stdin.readline def get(this): l, h = 0, n - 1 while l <= h: mid = (l + h) // 2 value = prefix[mid] + (n - mid) * this if value > k: h = mid - 1 else: ans = n - mid - 1 l = mid + 1 return ans def answer(m): this = max(0, m - k // n) m = min(m, k // n) add = 0 ans = this + n - 1 for i in range(n - 1): ans = min(ans, this + add + get(m - add)) add += 1 return ans for T in range(int(input())): n, k = map(int, input().split()) a = sorted(list(map(int, input().split()))) m = a[0] a.remove(m) prefix = [0] for i in range(n - 1): prefix.append(prefix[-1] + a[i]) print(answer(m))
ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given an integer array $a_1, a_2, \dots, a_n$ and integer $k$. In one step you can either choose some index $i$ and decrease $a_i$ by one (make $a_i = a_i - 1$); or choose two indices $i$ and $j$ and set $a_i$ equal to $a_j$ (make $a_i = a_j$). What is the minimum number of steps you need to make the sum of array $\sum\limits_{i=1}^{n}{a_i} \le k$? (You are allowed to make values of array negative). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^{15}$) — the size of array $a$ and upper bound on its sum. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the array itself. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print one integer — the minimum number of steps to make $\sum\limits_{i=1}^{n}{a_i} \le k$. -----Examples----- Input 4 1 10 20 2 69 6 9 7 8 1 2 1 3 1 2 1 10 1 1 2 3 1 2 6 1 6 8 10 Output 10 0 2 7 -----Note----- In the first test case, you should decrease $a_1$ $10$ times to get the sum lower or equal to $k = 10$. In the second test case, the sum of array $a$ is already less or equal to $69$, so you don't need to change it. In the third test case, you can, for example: set $a_4 = a_3 = 1$; decrease $a_4$ by one, and get $a_4 = 0$. As a result, you'll get array $[1, 2, 1, 0, 1, 2, 1]$ with sum less or equal to $8$ in $1 + 1 = 2$ steps. In the fourth test case, you can, for example: choose $a_7$ and decrease in by one $3$ times; you'll get $a_7 = -2$; choose $4$ elements $a_6$, $a_8$, $a_9$ and $a_{10}$ and them equal to $a_7 = -2$. As a result, you'll get array $[1, 2, 3, 1, 2, -2, -2, -2, -2, -2]$ with sum less or equal to $1$ in $3 + 4 = 7$ steps.
for _ in range(int(input())): n, k = map(int, input().split()) a_list = [int(i) for i in input().split()] a_list.sort(reverse=True) a_len = len(a_list) a_sum = sum(a_list) a_min = a_list[-1] awn = max((a_sum - k, 0)) a = a_sum - k i0 = 0 for i in range(n - 1): i0 += 1 a -= a_list[i] - a_min i1 = (a - 1) // (i + 2) + 1 if i1 < 0: i1 = 0 if i0 + i1 < awn: awn = i1 + i0 print(awn)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You are given an integer array $a_1, a_2, \dots, a_n$ and integer $k$. In one step you can either choose some index $i$ and decrease $a_i$ by one (make $a_i = a_i - 1$); or choose two indices $i$ and $j$ and set $a_i$ equal to $a_j$ (make $a_i = a_j$). What is the minimum number of steps you need to make the sum of array $\sum\limits_{i=1}^{n}{a_i} \le k$? (You are allowed to make values of array negative). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^{15}$) — the size of array $a$ and upper bound on its sum. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the array itself. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print one integer — the minimum number of steps to make $\sum\limits_{i=1}^{n}{a_i} \le k$. -----Examples----- Input 4 1 10 20 2 69 6 9 7 8 1 2 1 3 1 2 1 10 1 1 2 3 1 2 6 1 6 8 10 Output 10 0 2 7 -----Note----- In the first test case, you should decrease $a_1$ $10$ times to get the sum lower or equal to $k = 10$. In the second test case, the sum of array $a$ is already less or equal to $69$, so you don't need to change it. In the third test case, you can, for example: set $a_4 = a_3 = 1$; decrease $a_4$ by one, and get $a_4 = 0$. As a result, you'll get array $[1, 2, 1, 0, 1, 2, 1]$ with sum less or equal to $8$ in $1 + 1 = 2$ steps. In the fourth test case, you can, for example: choose $a_7$ and decrease in by one $3$ times; you'll get $a_7 = -2$; choose $4$ elements $a_6$, $a_8$, $a_9$ and $a_{10}$ and them equal to $a_7 = -2$. As a result, you'll get array $[1, 2, 3, 1, 2, -2, -2, -2, -2, -2]$ with sum less or equal to $1$ in $3 + 4 = 7$ steps.
I = lambda: [*map(int, input().split())] for _ in [0] * I()[0]: n, k = I() a = sorted(I()) k += a[0] b = 1 << 50 for i in range(n): k -= a[i] b = min(max(0, a[0] - k // (n - i)) + n - i - 1, b) print(b)
ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR BIN_OP LIST NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
You are given an integer array $a_1, a_2, \dots, a_n$ and integer $k$. In one step you can either choose some index $i$ and decrease $a_i$ by one (make $a_i = a_i - 1$); or choose two indices $i$ and $j$ and set $a_i$ equal to $a_j$ (make $a_i = a_j$). What is the minimum number of steps you need to make the sum of array $\sum\limits_{i=1}^{n}{a_i} \le k$? (You are allowed to make values of array negative). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^{15}$) — the size of array $a$ and upper bound on its sum. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the array itself. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print one integer — the minimum number of steps to make $\sum\limits_{i=1}^{n}{a_i} \le k$. -----Examples----- Input 4 1 10 20 2 69 6 9 7 8 1 2 1 3 1 2 1 10 1 1 2 3 1 2 6 1 6 8 10 Output 10 0 2 7 -----Note----- In the first test case, you should decrease $a_1$ $10$ times to get the sum lower or equal to $k = 10$. In the second test case, the sum of array $a$ is already less or equal to $69$, so you don't need to change it. In the third test case, you can, for example: set $a_4 = a_3 = 1$; decrease $a_4$ by one, and get $a_4 = 0$. As a result, you'll get array $[1, 2, 1, 0, 1, 2, 1]$ with sum less or equal to $8$ in $1 + 1 = 2$ steps. In the fourth test case, you can, for example: choose $a_7$ and decrease in by one $3$ times; you'll get $a_7 = -2$; choose $4$ elements $a_6$, $a_8$, $a_9$ and $a_{10}$ and them equal to $a_7 = -2$. As a result, you'll get array $[1, 2, 3, 1, 2, -2, -2, -2, -2, -2]$ with sum less or equal to $1$ in $3 + 4 = 7$ steps.
t = int(input()) def solve(n, k, a): a = sorted(a) s = sum(a) if s <= k: return 0 if n == 1: return s - k mn = a[0] rs = s - mn ans = s - k for i in range(n - 1, 0, -1): rs -= a[i] c = 1 + n - i nmn = (k - rs) // c ans = min(ans, c - 1 + max(mn - nmn, 0)) return ans for _ in range(t): n, k = map(int, input().split()) a = list(map(int, input().split())) print(solve(n, k, a))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER IF VAR NUMBER RETURN BIN_OP VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER RETURN VAR FOR VAR 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 FUNC_CALL VAR VAR VAR VAR
You are given an integer array $a_1, a_2, \dots, a_n$ and integer $k$. In one step you can either choose some index $i$ and decrease $a_i$ by one (make $a_i = a_i - 1$); or choose two indices $i$ and $j$ and set $a_i$ equal to $a_j$ (make $a_i = a_j$). What is the minimum number of steps you need to make the sum of array $\sum\limits_{i=1}^{n}{a_i} \le k$? (You are allowed to make values of array negative). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^{15}$) — the size of array $a$ and upper bound on its sum. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the array itself. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print one integer — the minimum number of steps to make $\sum\limits_{i=1}^{n}{a_i} \le k$. -----Examples----- Input 4 1 10 20 2 69 6 9 7 8 1 2 1 3 1 2 1 10 1 1 2 3 1 2 6 1 6 8 10 Output 10 0 2 7 -----Note----- In the first test case, you should decrease $a_1$ $10$ times to get the sum lower or equal to $k = 10$. In the second test case, the sum of array $a$ is already less or equal to $69$, so you don't need to change it. In the third test case, you can, for example: set $a_4 = a_3 = 1$; decrease $a_4$ by one, and get $a_4 = 0$. As a result, you'll get array $[1, 2, 1, 0, 1, 2, 1]$ with sum less or equal to $8$ in $1 + 1 = 2$ steps. In the fourth test case, you can, for example: choose $a_7$ and decrease in by one $3$ times; you'll get $a_7 = -2$; choose $4$ elements $a_6$, $a_8$, $a_9$ and $a_{10}$ and them equal to $a_7 = -2$. As a result, you'll get array $[1, 2, 3, 1, 2, -2, -2, -2, -2, -2]$ with sum less or equal to $1$ in $3 + 4 = 7$ steps.
for i in range(int(input())): n, m = map(int, input().split()) arr = list(map(int, input().split())) x = sum(arr) cnt = 0 arr.sort() stack = [] minn = 1000000000000000007 for i in range(1, n): stack.append(arr[i] - arr[0]) while stack and x > m: minn = min(minn, cnt + (x - m + cnt) // (cnt + 1)) x -= stack.pop() cnt += 1 if x > m: cnt += (x - m + cnt) // (cnt + 1) print(min(cnt, minn))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER WHILE VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given an integer array $a_1, a_2, \dots, a_n$ and integer $k$. In one step you can either choose some index $i$ and decrease $a_i$ by one (make $a_i = a_i - 1$); or choose two indices $i$ and $j$ and set $a_i$ equal to $a_j$ (make $a_i = a_j$). What is the minimum number of steps you need to make the sum of array $\sum\limits_{i=1}^{n}{a_i} \le k$? (You are allowed to make values of array negative). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^{15}$) — the size of array $a$ and upper bound on its sum. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the array itself. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print one integer — the minimum number of steps to make $\sum\limits_{i=1}^{n}{a_i} \le k$. -----Examples----- Input 4 1 10 20 2 69 6 9 7 8 1 2 1 3 1 2 1 10 1 1 2 3 1 2 6 1 6 8 10 Output 10 0 2 7 -----Note----- In the first test case, you should decrease $a_1$ $10$ times to get the sum lower or equal to $k = 10$. In the second test case, the sum of array $a$ is already less or equal to $69$, so you don't need to change it. In the third test case, you can, for example: set $a_4 = a_3 = 1$; decrease $a_4$ by one, and get $a_4 = 0$. As a result, you'll get array $[1, 2, 1, 0, 1, 2, 1]$ with sum less or equal to $8$ in $1 + 1 = 2$ steps. In the fourth test case, you can, for example: choose $a_7$ and decrease in by one $3$ times; you'll get $a_7 = -2$; choose $4$ elements $a_6$, $a_8$, $a_9$ and $a_{10}$ and them equal to $a_7 = -2$. As a result, you'll get array $[1, 2, 3, 1, 2, -2, -2, -2, -2, -2]$ with sum less or equal to $1$ in $3 + 4 = 7$ steps.
for _ in range(int(input())): n, maxx = [int(i) for i in input().split()] arr = [int(i) for i in input().split()] summ = sum(arr) if summ <= maxx: print(0) continue arr.sort() summ = 0 prefix = [0] for i in range(1, n): summ += arr[i] prefix.append(summ) m = arr[0] minn = float("inf") prefix[0] = 0 for i in range(n): a = (maxx - prefix[i]) // (n - i) if a >= m: minn = min(minn, n - i - 1) else: minn = min(minn, m - a + n - i - 1) print(minn)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an integer array $a_1, a_2, \dots, a_n$ and integer $k$. In one step you can either choose some index $i$ and decrease $a_i$ by one (make $a_i = a_i - 1$); or choose two indices $i$ and $j$ and set $a_i$ equal to $a_j$ (make $a_i = a_j$). What is the minimum number of steps you need to make the sum of array $\sum\limits_{i=1}^{n}{a_i} \le k$? (You are allowed to make values of array negative). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^{15}$) — the size of array $a$ and upper bound on its sum. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the array itself. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print one integer — the minimum number of steps to make $\sum\limits_{i=1}^{n}{a_i} \le k$. -----Examples----- Input 4 1 10 20 2 69 6 9 7 8 1 2 1 3 1 2 1 10 1 1 2 3 1 2 6 1 6 8 10 Output 10 0 2 7 -----Note----- In the first test case, you should decrease $a_1$ $10$ times to get the sum lower or equal to $k = 10$. In the second test case, the sum of array $a$ is already less or equal to $69$, so you don't need to change it. In the third test case, you can, for example: set $a_4 = a_3 = 1$; decrease $a_4$ by one, and get $a_4 = 0$. As a result, you'll get array $[1, 2, 1, 0, 1, 2, 1]$ with sum less or equal to $8$ in $1 + 1 = 2$ steps. In the fourth test case, you can, for example: choose $a_7$ and decrease in by one $3$ times; you'll get $a_7 = -2$; choose $4$ elements $a_6$, $a_8$, $a_9$ and $a_{10}$ and them equal to $a_7 = -2$. As a result, you'll get array $[1, 2, 3, 1, 2, -2, -2, -2, -2, -2]$ with sum less or equal to $1$ in $3 + 4 = 7$ steps.
def solve(n, k, arr): if n == 1: return 0 if arr[0] <= k else arr[0] - k arr.sort() S = sum(arr) a0 = arr[0] if S <= k: return 0 prefixSum = [0] * n curr = 0 for i in range(n): curr += arr[i] prefixSum[i] = curr ans = S - k for i in range(n - 1, 0, -1): rangeSum = prefixSum[n - 1] - prefixSum[i - 1] a = min((a0 + rangeSum - S + k) // (n - i + 1), a0) ans = min(a0 - a + n - i, ans) return ans for _ in range(int(input())): n, k = map(int, input().split()) arr = list(map(int, input().split())) print(solve(n, k, arr))
FUNC_DEF IF VAR NUMBER RETURN VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 VAR VAR
You are given an integer array $a_1, a_2, \dots, a_n$ and integer $k$. In one step you can either choose some index $i$ and decrease $a_i$ by one (make $a_i = a_i - 1$); or choose two indices $i$ and $j$ and set $a_i$ equal to $a_j$ (make $a_i = a_j$). What is the minimum number of steps you need to make the sum of array $\sum\limits_{i=1}^{n}{a_i} \le k$? (You are allowed to make values of array negative). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^{15}$) — the size of array $a$ and upper bound on its sum. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the array itself. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print one integer — the minimum number of steps to make $\sum\limits_{i=1}^{n}{a_i} \le k$. -----Examples----- Input 4 1 10 20 2 69 6 9 7 8 1 2 1 3 1 2 1 10 1 1 2 3 1 2 6 1 6 8 10 Output 10 0 2 7 -----Note----- In the first test case, you should decrease $a_1$ $10$ times to get the sum lower or equal to $k = 10$. In the second test case, the sum of array $a$ is already less or equal to $69$, so you don't need to change it. In the third test case, you can, for example: set $a_4 = a_3 = 1$; decrease $a_4$ by one, and get $a_4 = 0$. As a result, you'll get array $[1, 2, 1, 0, 1, 2, 1]$ with sum less or equal to $8$ in $1 + 1 = 2$ steps. In the fourth test case, you can, for example: choose $a_7$ and decrease in by one $3$ times; you'll get $a_7 = -2$; choose $4$ elements $a_6$, $a_8$, $a_9$ and $a_{10}$ and them equal to $a_7 = -2$. As a result, you'll get array $[1, 2, 3, 1, 2, -2, -2, -2, -2, -2]$ with sum less or equal to $1$ in $3 + 4 = 7$ steps.
import sys I = lambda: [*map(int, sys.stdin.readline().split())] (t,) = I() for _ in range(t): n, k = I() a = I() a.sort() orig = a[0] partial = [0] for i in range(1, n): partial.append(partial[-1] + a[i]) best = float("inf") for i in range(n): need = max(0, orig - (k - partial[i]) // (n - i)) + n - i - 1 best = min(need, best) print(best)
IMPORT ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an integer array $a_1, a_2, \dots, a_n$ and integer $k$. In one step you can either choose some index $i$ and decrease $a_i$ by one (make $a_i = a_i - 1$); or choose two indices $i$ and $j$ and set $a_i$ equal to $a_j$ (make $a_i = a_j$). What is the minimum number of steps you need to make the sum of array $\sum\limits_{i=1}^{n}{a_i} \le k$? (You are allowed to make values of array negative). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^{15}$) — the size of array $a$ and upper bound on its sum. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the array itself. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print one integer — the minimum number of steps to make $\sum\limits_{i=1}^{n}{a_i} \le k$. -----Examples----- Input 4 1 10 20 2 69 6 9 7 8 1 2 1 3 1 2 1 10 1 1 2 3 1 2 6 1 6 8 10 Output 10 0 2 7 -----Note----- In the first test case, you should decrease $a_1$ $10$ times to get the sum lower or equal to $k = 10$. In the second test case, the sum of array $a$ is already less or equal to $69$, so you don't need to change it. In the third test case, you can, for example: set $a_4 = a_3 = 1$; decrease $a_4$ by one, and get $a_4 = 0$. As a result, you'll get array $[1, 2, 1, 0, 1, 2, 1]$ with sum less or equal to $8$ in $1 + 1 = 2$ steps. In the fourth test case, you can, for example: choose $a_7$ and decrease in by one $3$ times; you'll get $a_7 = -2$; choose $4$ elements $a_6$, $a_8$, $a_9$ and $a_{10}$ and them equal to $a_7 = -2$. As a result, you'll get array $[1, 2, 3, 1, 2, -2, -2, -2, -2, -2]$ with sum less or equal to $1$ in $3 + 4 = 7$ steps.
for _ in range(int(input())): n, k = map(int, input().split()) arr = list(map(int, input().split())) arr.sort() minVal = arr[0] s = sum(arr) - minVal res = max(0, minVal + s - k) for i in range(n - 1, 0, -1): s -= arr[i] target = (k - s) // (1 + n - i) if target >= minVal: tmp = n - i else: tmp = n - i + minVal - target res = min(res, tmp) print(res)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an integer array $a_1, a_2, \dots, a_n$ and integer $k$. In one step you can either choose some index $i$ and decrease $a_i$ by one (make $a_i = a_i - 1$); or choose two indices $i$ and $j$ and set $a_i$ equal to $a_j$ (make $a_i = a_j$). What is the minimum number of steps you need to make the sum of array $\sum\limits_{i=1}^{n}{a_i} \le k$? (You are allowed to make values of array negative). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^{15}$) — the size of array $a$ and upper bound on its sum. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the array itself. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print one integer — the minimum number of steps to make $\sum\limits_{i=1}^{n}{a_i} \le k$. -----Examples----- Input 4 1 10 20 2 69 6 9 7 8 1 2 1 3 1 2 1 10 1 1 2 3 1 2 6 1 6 8 10 Output 10 0 2 7 -----Note----- In the first test case, you should decrease $a_1$ $10$ times to get the sum lower or equal to $k = 10$. In the second test case, the sum of array $a$ is already less or equal to $69$, so you don't need to change it. In the third test case, you can, for example: set $a_4 = a_3 = 1$; decrease $a_4$ by one, and get $a_4 = 0$. As a result, you'll get array $[1, 2, 1, 0, 1, 2, 1]$ with sum less or equal to $8$ in $1 + 1 = 2$ steps. In the fourth test case, you can, for example: choose $a_7$ and decrease in by one $3$ times; you'll get $a_7 = -2$; choose $4$ elements $a_6$, $a_8$, $a_9$ and $a_{10}$ and them equal to $a_7 = -2$. As a result, you'll get array $[1, 2, 3, 1, 2, -2, -2, -2, -2, -2]$ with sum less or equal to $1$ in $3 + 4 = 7$ steps.
T = int(input()) for t in range(T): n, k = [int(i) for i in input().split()] arr = [int(i) for i in input().split()] summ = sum(arr) arr.sort() stack = [] for i in range(1, n): stack.append(arr[i] - arr[0]) count = 0 top = n - 2 mnm = 100000000000000000000000 while len(stack) != 0 and summ > k: mnm = min(mnm, count + int((summ - k + count) / (count + 1))) summ -= stack[top] stack.pop(top) top -= 1 count += 1 if summ > k: count += int((summ - k + count) / (count + 1)) print(min(mnm, count))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL 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 ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given an integer array $a_1, a_2, \dots, a_n$ and integer $k$. In one step you can either choose some index $i$ and decrease $a_i$ by one (make $a_i = a_i - 1$); or choose two indices $i$ and $j$ and set $a_i$ equal to $a_j$ (make $a_i = a_j$). What is the minimum number of steps you need to make the sum of array $\sum\limits_{i=1}^{n}{a_i} \le k$? (You are allowed to make values of array negative). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^{15}$) — the size of array $a$ and upper bound on its sum. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the array itself. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print one integer — the minimum number of steps to make $\sum\limits_{i=1}^{n}{a_i} \le k$. -----Examples----- Input 4 1 10 20 2 69 6 9 7 8 1 2 1 3 1 2 1 10 1 1 2 3 1 2 6 1 6 8 10 Output 10 0 2 7 -----Note----- In the first test case, you should decrease $a_1$ $10$ times to get the sum lower or equal to $k = 10$. In the second test case, the sum of array $a$ is already less or equal to $69$, so you don't need to change it. In the third test case, you can, for example: set $a_4 = a_3 = 1$; decrease $a_4$ by one, and get $a_4 = 0$. As a result, you'll get array $[1, 2, 1, 0, 1, 2, 1]$ with sum less or equal to $8$ in $1 + 1 = 2$ steps. In the fourth test case, you can, for example: choose $a_7$ and decrease in by one $3$ times; you'll get $a_7 = -2$; choose $4$ elements $a_6$, $a_8$, $a_9$ and $a_{10}$ and them equal to $a_7 = -2$. As a result, you'll get array $[1, 2, 3, 1, 2, -2, -2, -2, -2, -2]$ with sum less or equal to $1$ in $3 + 4 = 7$ steps.
import sys def solve(): n, k = map(int, input().split()) arr = list(map(int, input().split())) arr.sort() su = sum(arr) if n == 1: return max(0, su - k) if su <= k: return 0 ans = 10**18 if arr[0] * n <= k: t = 0 for i in range(n - 1, -1, -1): su -= arr[i] - arr[0] t += 1 if su <= k: ans = t break su = sum(arr) - arr[0] t = 1 for i in range(n - 1, -1, -1): ans = min(ans, max(arr[0] - (k - su) // t, 0) + t - 1) su -= arr[i] t += 1 return ans input = lambda: sys.stdin.readline().rstrip() t = int(input()) for i in range(t): print(solve())
IMPORT 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 FUNC_CALL VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR NUMBER BIN_OP VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER IF BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
You are given an integer array $a_1, a_2, \dots, a_n$ and integer $k$. In one step you can either choose some index $i$ and decrease $a_i$ by one (make $a_i = a_i - 1$); or choose two indices $i$ and $j$ and set $a_i$ equal to $a_j$ (make $a_i = a_j$). What is the minimum number of steps you need to make the sum of array $\sum\limits_{i=1}^{n}{a_i} \le k$? (You are allowed to make values of array negative). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^{15}$) — the size of array $a$ and upper bound on its sum. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the array itself. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print one integer — the minimum number of steps to make $\sum\limits_{i=1}^{n}{a_i} \le k$. -----Examples----- Input 4 1 10 20 2 69 6 9 7 8 1 2 1 3 1 2 1 10 1 1 2 3 1 2 6 1 6 8 10 Output 10 0 2 7 -----Note----- In the first test case, you should decrease $a_1$ $10$ times to get the sum lower or equal to $k = 10$. In the second test case, the sum of array $a$ is already less or equal to $69$, so you don't need to change it. In the third test case, you can, for example: set $a_4 = a_3 = 1$; decrease $a_4$ by one, and get $a_4 = 0$. As a result, you'll get array $[1, 2, 1, 0, 1, 2, 1]$ with sum less or equal to $8$ in $1 + 1 = 2$ steps. In the fourth test case, you can, for example: choose $a_7$ and decrease in by one $3$ times; you'll get $a_7 = -2$; choose $4$ elements $a_6$, $a_8$, $a_9$ and $a_{10}$ and them equal to $a_7 = -2$. As a result, you'll get array $[1, 2, 3, 1, 2, -2, -2, -2, -2, -2]$ with sum less or equal to $1$ in $3 + 4 = 7$ steps.
def solve(n, k, arr): s = sum(arr) if s <= k: return 0 if n == 1: return s - k arr.sort() stack = [(arr[i] - arr[0]) for i in range(1, n)] ans = 0 m = 10**18 while stack and s > k: z = ans + (s - k + ans) // (ans + 1) m = min(m, z) s -= stack[-1] stack.pop() ans += 1 if s > k: ans += (s - k + ans) // (ans + 1) return min(ans, m) t = int(input()) for _ in range(t): n, k = map(int, input().split()) arr = list(map(int, input().split())) print(solve(n, k, arr))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER IF VAR NUMBER RETURN BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
You are given an integer array $a_1, a_2, \dots, a_n$ and integer $k$. In one step you can either choose some index $i$ and decrease $a_i$ by one (make $a_i = a_i - 1$); or choose two indices $i$ and $j$ and set $a_i$ equal to $a_j$ (make $a_i = a_j$). What is the minimum number of steps you need to make the sum of array $\sum\limits_{i=1}^{n}{a_i} \le k$? (You are allowed to make values of array negative). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^{15}$) — the size of array $a$ and upper bound on its sum. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the array itself. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print one integer — the minimum number of steps to make $\sum\limits_{i=1}^{n}{a_i} \le k$. -----Examples----- Input 4 1 10 20 2 69 6 9 7 8 1 2 1 3 1 2 1 10 1 1 2 3 1 2 6 1 6 8 10 Output 10 0 2 7 -----Note----- In the first test case, you should decrease $a_1$ $10$ times to get the sum lower or equal to $k = 10$. In the second test case, the sum of array $a$ is already less or equal to $69$, so you don't need to change it. In the third test case, you can, for example: set $a_4 = a_3 = 1$; decrease $a_4$ by one, and get $a_4 = 0$. As a result, you'll get array $[1, 2, 1, 0, 1, 2, 1]$ with sum less or equal to $8$ in $1 + 1 = 2$ steps. In the fourth test case, you can, for example: choose $a_7$ and decrease in by one $3$ times; you'll get $a_7 = -2$; choose $4$ elements $a_6$, $a_8$, $a_9$ and $a_{10}$ and them equal to $a_7 = -2$. As a result, you'll get array $[1, 2, 3, 1, 2, -2, -2, -2, -2, -2]$ with sum less or equal to $1$ in $3 + 4 = 7$ steps.
t = int(input()) while t: t -= 1 n, k = map(int, input().split()) a = list(map(int, input().split())) a.sort() a1 = [] s1 = 0 for i in range(n): s1 += a[i] a1.append(s1) s = sum(a) ans = [] for i in range(n): if (i * a[0] + a1[n - i - 1] - k) % (i + 1) == 0: ans1 = (i * a[0] + a1[n - i - 1] - k) // (i + 1) if ans1 <= 0: ans1 = 0 ans.append(ans1 + i) else: ans1 = (i * a[0] + a1[n - i - 1] - k) // (i + 1) if ans1 < 0: ans1 = 0 else: ans1 += 1 ans.append(ans1 + i) if s <= k: print(0) else: ans1 = 1e18 for i in range(len(ans)): if ans[i] >= 1: ans1 = min(ans1, ans[i]) print(int(ans1))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR 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 EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given an integer array $a_1, a_2, \dots, a_n$ and integer $k$. In one step you can either choose some index $i$ and decrease $a_i$ by one (make $a_i = a_i - 1$); or choose two indices $i$ and $j$ and set $a_i$ equal to $a_j$ (make $a_i = a_j$). What is the minimum number of steps you need to make the sum of array $\sum\limits_{i=1}^{n}{a_i} \le k$? (You are allowed to make values of array negative). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^{15}$) — the size of array $a$ and upper bound on its sum. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the array itself. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print one integer — the minimum number of steps to make $\sum\limits_{i=1}^{n}{a_i} \le k$. -----Examples----- Input 4 1 10 20 2 69 6 9 7 8 1 2 1 3 1 2 1 10 1 1 2 3 1 2 6 1 6 8 10 Output 10 0 2 7 -----Note----- In the first test case, you should decrease $a_1$ $10$ times to get the sum lower or equal to $k = 10$. In the second test case, the sum of array $a$ is already less or equal to $69$, so you don't need to change it. In the third test case, you can, for example: set $a_4 = a_3 = 1$; decrease $a_4$ by one, and get $a_4 = 0$. As a result, you'll get array $[1, 2, 1, 0, 1, 2, 1]$ with sum less or equal to $8$ in $1 + 1 = 2$ steps. In the fourth test case, you can, for example: choose $a_7$ and decrease in by one $3$ times; you'll get $a_7 = -2$; choose $4$ elements $a_6$, $a_8$, $a_9$ and $a_{10}$ and them equal to $a_7 = -2$. As a result, you'll get array $[1, 2, 3, 1, 2, -2, -2, -2, -2, -2]$ with sum less or equal to $1$ in $3 + 4 = 7$ steps.
def solve(): n, k = map(int, input().split()) a = list(map(int, input().split())) a.sort(reverse=True) smallest = a.pop() n -= 1 sums = [0] * (n + 1) for i in range(n - 1, -1, -1): sums[i] = sums[i + 1] + a[i] ans = int(1e30) for i in range(n + 1): level = (k - sums[i]) // (i + 1) ans = min(ans, max(0, smallest - level) + i) print(ans) t = int(input()) for _ in range(t): solve()
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 NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given an integer array $a_1, a_2, \dots, a_n$ and integer $k$. In one step you can either choose some index $i$ and decrease $a_i$ by one (make $a_i = a_i - 1$); or choose two indices $i$ and $j$ and set $a_i$ equal to $a_j$ (make $a_i = a_j$). What is the minimum number of steps you need to make the sum of array $\sum\limits_{i=1}^{n}{a_i} \le k$? (You are allowed to make values of array negative). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^{15}$) — the size of array $a$ and upper bound on its sum. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the array itself. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print one integer — the minimum number of steps to make $\sum\limits_{i=1}^{n}{a_i} \le k$. -----Examples----- Input 4 1 10 20 2 69 6 9 7 8 1 2 1 3 1 2 1 10 1 1 2 3 1 2 6 1 6 8 10 Output 10 0 2 7 -----Note----- In the first test case, you should decrease $a_1$ $10$ times to get the sum lower or equal to $k = 10$. In the second test case, the sum of array $a$ is already less or equal to $69$, so you don't need to change it. In the third test case, you can, for example: set $a_4 = a_3 = 1$; decrease $a_4$ by one, and get $a_4 = 0$. As a result, you'll get array $[1, 2, 1, 0, 1, 2, 1]$ with sum less or equal to $8$ in $1 + 1 = 2$ steps. In the fourth test case, you can, for example: choose $a_7$ and decrease in by one $3$ times; you'll get $a_7 = -2$; choose $4$ elements $a_6$, $a_8$, $a_9$ and $a_{10}$ and them equal to $a_7 = -2$. As a result, you'll get array $[1, 2, 3, 1, 2, -2, -2, -2, -2, -2]$ with sum less or equal to $1$ in $3 + 4 = 7$ steps.
def solve(n, k, a): a = sorted(a) s = sum(a) if s <= k: return 0 best = s - k rest = 0 for cols in range(1, n): rest += a[n - cols] - a[0] if rest >= s - k: return min(best, cols) x = (s - k - rest + cols) // (cols + 1) best = min(best, x + cols) return best tests = int(input()) for test in range(tests): n, k = map(int, input().split()) a = list(map(int, input().split())) print(solve(n, k, a))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER IF VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
You are given an integer array $a_1, a_2, \dots, a_n$ and integer $k$. In one step you can either choose some index $i$ and decrease $a_i$ by one (make $a_i = a_i - 1$); or choose two indices $i$ and $j$ and set $a_i$ equal to $a_j$ (make $a_i = a_j$). What is the minimum number of steps you need to make the sum of array $\sum\limits_{i=1}^{n}{a_i} \le k$? (You are allowed to make values of array negative). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^{15}$) — the size of array $a$ and upper bound on its sum. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the array itself. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print one integer — the minimum number of steps to make $\sum\limits_{i=1}^{n}{a_i} \le k$. -----Examples----- Input 4 1 10 20 2 69 6 9 7 8 1 2 1 3 1 2 1 10 1 1 2 3 1 2 6 1 6 8 10 Output 10 0 2 7 -----Note----- In the first test case, you should decrease $a_1$ $10$ times to get the sum lower or equal to $k = 10$. In the second test case, the sum of array $a$ is already less or equal to $69$, so you don't need to change it. In the third test case, you can, for example: set $a_4 = a_3 = 1$; decrease $a_4$ by one, and get $a_4 = 0$. As a result, you'll get array $[1, 2, 1, 0, 1, 2, 1]$ with sum less or equal to $8$ in $1 + 1 = 2$ steps. In the fourth test case, you can, for example: choose $a_7$ and decrease in by one $3$ times; you'll get $a_7 = -2$; choose $4$ elements $a_6$, $a_8$, $a_9$ and $a_{10}$ and them equal to $a_7 = -2$. As a result, you'll get array $[1, 2, 3, 1, 2, -2, -2, -2, -2, -2]$ with sum less or equal to $1$ in $3 + 4 = 7$ steps.
for _ in range(int(input())): n, k = map(int, input().split()) l = list(map(int, input().split())) l.sort() m = l[0] l[0] = 0 for i in range(1, n): l[i] += l[i - 1] ans = float("inf") for i in range(n): a = (k - l[i]) // (n - i) if a >= m: ans = min(ans, n - i - 1) else: ans = min(ans, m - a + n - i - 1) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an integer array $a_1, a_2, \dots, a_n$ and integer $k$. In one step you can either choose some index $i$ and decrease $a_i$ by one (make $a_i = a_i - 1$); or choose two indices $i$ and $j$ and set $a_i$ equal to $a_j$ (make $a_i = a_j$). What is the minimum number of steps you need to make the sum of array $\sum\limits_{i=1}^{n}{a_i} \le k$? (You are allowed to make values of array negative). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^{15}$) — the size of array $a$ and upper bound on its sum. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the array itself. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print one integer — the minimum number of steps to make $\sum\limits_{i=1}^{n}{a_i} \le k$. -----Examples----- Input 4 1 10 20 2 69 6 9 7 8 1 2 1 3 1 2 1 10 1 1 2 3 1 2 6 1 6 8 10 Output 10 0 2 7 -----Note----- In the first test case, you should decrease $a_1$ $10$ times to get the sum lower or equal to $k = 10$. In the second test case, the sum of array $a$ is already less or equal to $69$, so you don't need to change it. In the third test case, you can, for example: set $a_4 = a_3 = 1$; decrease $a_4$ by one, and get $a_4 = 0$. As a result, you'll get array $[1, 2, 1, 0, 1, 2, 1]$ with sum less or equal to $8$ in $1 + 1 = 2$ steps. In the fourth test case, you can, for example: choose $a_7$ and decrease in by one $3$ times; you'll get $a_7 = -2$; choose $4$ elements $a_6$, $a_8$, $a_9$ and $a_{10}$ and them equal to $a_7 = -2$. As a result, you'll get array $[1, 2, 3, 1, 2, -2, -2, -2, -2, -2]$ with sum less or equal to $1$ in $3 + 4 = 7$ steps.
import sys from sys import stdin tt = int(stdin.readline()) ANS = [] for loop in range(tt): n, k = map(int, stdin.readline().split()) a = list(map(int, stdin.readline().split())) a.sort() nmov = 1 nsum = sum(a) ans = max(0, nsum - k) for i in range(n - 1, 0, -1): nsum = nsum - a[i] + a[0] rem = max(0, nsum - k) dinum = (rem + nmov) // (nmov + 1) ans = min(ans, nmov + dinum) nmov += 1 ANS.append(str(ans)) print("\n".join(ANS))
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
You are given an integer array $a_1, a_2, \dots, a_n$ and integer $k$. In one step you can either choose some index $i$ and decrease $a_i$ by one (make $a_i = a_i - 1$); or choose two indices $i$ and $j$ and set $a_i$ equal to $a_j$ (make $a_i = a_j$). What is the minimum number of steps you need to make the sum of array $\sum\limits_{i=1}^{n}{a_i} \le k$? (You are allowed to make values of array negative). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^{15}$) — the size of array $a$ and upper bound on its sum. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the array itself. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print one integer — the minimum number of steps to make $\sum\limits_{i=1}^{n}{a_i} \le k$. -----Examples----- Input 4 1 10 20 2 69 6 9 7 8 1 2 1 3 1 2 1 10 1 1 2 3 1 2 6 1 6 8 10 Output 10 0 2 7 -----Note----- In the first test case, you should decrease $a_1$ $10$ times to get the sum lower or equal to $k = 10$. In the second test case, the sum of array $a$ is already less or equal to $69$, so you don't need to change it. In the third test case, you can, for example: set $a_4 = a_3 = 1$; decrease $a_4$ by one, and get $a_4 = 0$. As a result, you'll get array $[1, 2, 1, 0, 1, 2, 1]$ with sum less or equal to $8$ in $1 + 1 = 2$ steps. In the fourth test case, you can, for example: choose $a_7$ and decrease in by one $3$ times; you'll get $a_7 = -2$; choose $4$ elements $a_6$, $a_8$, $a_9$ and $a_{10}$ and them equal to $a_7 = -2$. As a result, you'll get array $[1, 2, 3, 1, 2, -2, -2, -2, -2, -2]$ with sum less or equal to $1$ in $3 + 4 = 7$ steps.
t = int(input()) for i in range(t): n, k = map(int, input().split()) a = list(map(int, input().split())) a.sort() if n == 1: print(max(0, sum(a) - k)) continue if sum(a) <= k: print(0) continue diff = sum(a) - k pref = [0] * (n + 1) for i in range(n): pref[i + 1] = pref[i] + a[n - 1 - i] - a[0] ans = 1e16 for i in range(n - 1, -1, -1): diff1 = diff - pref[i] if diff1 <= 0: ans = min(ans, i) else: ans = min(ans, i + (diff1 + i) // (i + 1)) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR 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 FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an integer array $a_1, a_2, \dots, a_n$ and integer $k$. In one step you can either choose some index $i$ and decrease $a_i$ by one (make $a_i = a_i - 1$); or choose two indices $i$ and $j$ and set $a_i$ equal to $a_j$ (make $a_i = a_j$). What is the minimum number of steps you need to make the sum of array $\sum\limits_{i=1}^{n}{a_i} \le k$? (You are allowed to make values of array negative). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^{15}$) — the size of array $a$ and upper bound on its sum. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the array itself. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print one integer — the minimum number of steps to make $\sum\limits_{i=1}^{n}{a_i} \le k$. -----Examples----- Input 4 1 10 20 2 69 6 9 7 8 1 2 1 3 1 2 1 10 1 1 2 3 1 2 6 1 6 8 10 Output 10 0 2 7 -----Note----- In the first test case, you should decrease $a_1$ $10$ times to get the sum lower or equal to $k = 10$. In the second test case, the sum of array $a$ is already less or equal to $69$, so you don't need to change it. In the third test case, you can, for example: set $a_4 = a_3 = 1$; decrease $a_4$ by one, and get $a_4 = 0$. As a result, you'll get array $[1, 2, 1, 0, 1, 2, 1]$ with sum less or equal to $8$ in $1 + 1 = 2$ steps. In the fourth test case, you can, for example: choose $a_7$ and decrease in by one $3$ times; you'll get $a_7 = -2$; choose $4$ elements $a_6$, $a_8$, $a_9$ and $a_{10}$ and them equal to $a_7 = -2$. As a result, you'll get array $[1, 2, 3, 1, 2, -2, -2, -2, -2, -2]$ with sum less or equal to $1$ in $3 + 4 = 7$ steps.
def solve(k, a): diff = -k for ai in a: diff += ai if diff <= 0: return 0 diffList = [(ai - a[0]) for ai in a] postSum = [(0) for _ in range(len(a))] postSum[len(a) - 1] = diffList[len(a) - 1] for i in reversed(range(len(a) - 1)): postSum[i] = diffList[i] + postSum[i + 1] div = [(len(a) - i) for i in range(len(a))] ret = diff for i in reversed(range(1, len(a))): a1p = (diff - postSum[i] + div[i]) // (div[i] + 1) a1p = a1p if a1p >= 0 else 0 ret = min(ret, a1p + div[i]) return ret for ___ in range(int(input())): n, k = list(map(int, input().split())) a = list(map(int, input().split())) print(solve(k, sorted(a)))
FUNC_DEF ASSIGN VAR VAR FOR VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
You are given an integer array $a_1, a_2, \dots, a_n$ and integer $k$. In one step you can either choose some index $i$ and decrease $a_i$ by one (make $a_i = a_i - 1$); or choose two indices $i$ and $j$ and set $a_i$ equal to $a_j$ (make $a_i = a_j$). What is the minimum number of steps you need to make the sum of array $\sum\limits_{i=1}^{n}{a_i} \le k$? (You are allowed to make values of array negative). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^{15}$) — the size of array $a$ and upper bound on its sum. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the array itself. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print one integer — the minimum number of steps to make $\sum\limits_{i=1}^{n}{a_i} \le k$. -----Examples----- Input 4 1 10 20 2 69 6 9 7 8 1 2 1 3 1 2 1 10 1 1 2 3 1 2 6 1 6 8 10 Output 10 0 2 7 -----Note----- In the first test case, you should decrease $a_1$ $10$ times to get the sum lower or equal to $k = 10$. In the second test case, the sum of array $a$ is already less or equal to $69$, so you don't need to change it. In the third test case, you can, for example: set $a_4 = a_3 = 1$; decrease $a_4$ by one, and get $a_4 = 0$. As a result, you'll get array $[1, 2, 1, 0, 1, 2, 1]$ with sum less or equal to $8$ in $1 + 1 = 2$ steps. In the fourth test case, you can, for example: choose $a_7$ and decrease in by one $3$ times; you'll get $a_7 = -2$; choose $4$ elements $a_6$, $a_8$, $a_9$ and $a_{10}$ and them equal to $a_7 = -2$. As a result, you'll get array $[1, 2, 3, 1, 2, -2, -2, -2, -2, -2]$ with sum less or equal to $1$ in $3 + 4 = 7$ steps.
def solve(): n, k = map(int, input().split()) a = list(map(int, input().split())) a.sort() s = sum(a) ans = 10**18 prefix = [0] for i in range(1, n): prefix.append(prefix[i - 1] + a[i]) for i in range(n): m = (k - prefix[i]) // (n - i) if m < a[0]: ans = min(ans, a[0] - m + n - i - 1) else: ans = min(ans, n - i - 1) print(ans) for _ in range(int(input())): solve()
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 FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are given an integer array $a_1, a_2, \dots, a_n$ and integer $k$. In one step you can either choose some index $i$ and decrease $a_i$ by one (make $a_i = a_i - 1$); or choose two indices $i$ and $j$ and set $a_i$ equal to $a_j$ (make $a_i = a_j$). What is the minimum number of steps you need to make the sum of array $\sum\limits_{i=1}^{n}{a_i} \le k$? (You are allowed to make values of array negative). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^{15}$) — the size of array $a$ and upper bound on its sum. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the array itself. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print one integer — the minimum number of steps to make $\sum\limits_{i=1}^{n}{a_i} \le k$. -----Examples----- Input 4 1 10 20 2 69 6 9 7 8 1 2 1 3 1 2 1 10 1 1 2 3 1 2 6 1 6 8 10 Output 10 0 2 7 -----Note----- In the first test case, you should decrease $a_1$ $10$ times to get the sum lower or equal to $k = 10$. In the second test case, the sum of array $a$ is already less or equal to $69$, so you don't need to change it. In the third test case, you can, for example: set $a_4 = a_3 = 1$; decrease $a_4$ by one, and get $a_4 = 0$. As a result, you'll get array $[1, 2, 1, 0, 1, 2, 1]$ with sum less or equal to $8$ in $1 + 1 = 2$ steps. In the fourth test case, you can, for example: choose $a_7$ and decrease in by one $3$ times; you'll get $a_7 = -2$; choose $4$ elements $a_6$, $a_8$, $a_9$ and $a_{10}$ and them equal to $a_7 = -2$. As a result, you'll get array $[1, 2, 3, 1, 2, -2, -2, -2, -2, -2]$ with sum less or equal to $1$ in $3 + 4 = 7$ steps.
from sys import stdin def read_input(): n, k = map(int, stdin.readline().split()) a = list(map(int, stdin.readline().split())) return n, k, a def solve(n, k, a): if n == 1: return max(a[0] - k, 0) answer = 10**10 a.sort(reverse=True) s = [a[0] - a[-1]] for ai in a[1:-1]: s.append(s[-1] + (ai - a[-1])) p = 0 q = n - 1 k_ = sum(a) - k while q > 0: while q >= 0 and (s[q - 1] if q > 0 else 0) + p * q >= k_ - p: answer = min(answer, p + q) q -= 1 if q >= 0: p += max(1, (k_ - (s[q - 1] if q > 0 else 0) + q) // (q + 1) - p) return answer def main(): t = int(stdin.readline()) for _ in range(t): input = read_input() answer = solve(*input) print(answer) 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 RETURN VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER VAR NUMBER FOR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR WHILE VAR NUMBER WHILE VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Read problem statement in Mandarin chinese and Vietnamese. Suzumo is the coach of the ChefLand OI team. Physical condition is very important in any olympiad, so he wants to make the children run a bit as a warmup. The team consists of $N$ children numbered $1$ through $N$ standing at some positions on the $x$-axis. For each valid $i$, the initial $x$-coordinate of the $i$-th kid is $x_{i}$, the running velocity of the $i$-th kid is constant and equal to $v_{i}$. Suzumo wants to assign a running direction (left or right, i.e. in the direction of decreasing or increasing $x$-coordinate) to each kid; the children start running at time $0$ in the assigned directions. Afterwards, Suzumo will measure the smallest time $t$ at which some kid passes another one. Help Suzumo compute the maximum time $t$ if he can assign the directions arbitrarily! Note: Child $i$ *passes* child $j$ at time $t_{ij}$ if their coordinates satisfy $x_{i} < x_{j}$ at any time $t < t_{ij}$ and $x_{i} > x_{j}$ at any time $t > t_{ij}$, or if they satisfy $x_{i} > x_{j}$ at any time $t < t_{ij}$ and $x_{i} < x_{j}$ at any time $t > t_{ij}$. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains two space-separated integers $x_{i}$ and $v_{i}$. ------ Output ------ For each test case, print a single line containing one real number — the maximum possible time $t$, or $-1$ if it is possible to assign directions in such a way that no kid ever passes another. Your answer will be considered correct if it has an absolute or relative error less than or equal to $10^{-6}$. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 50$ $1 ≤ x_{i}, v_{i} ≤ 10^{9}$ for each valid $i$ no two children have the same initial positions ----- Sample Input 1 ------ 1 3 10 10 20 30 30 10 ----- Sample Output 1 ------ 0.5 ----- explanation 1 ------ Example case 1: One optimal assignment of directions is left, right, right.
def check(mid, arr): pr = 0 n = len(arr) an = [0] for i in range(1, n): x1 = arr[i - 1][0] x2 = arr[i][0] v1 = arr[i - 1][1] v2 = arr[i][1] if pr == 0: if x2 - x1 >= mid * (v2 - v1): pr = 0 else: pr = 1 elif x2 - x1 >= mid * (v2 + v1): pr = 0 elif x2 - x1 >= mid * (v1 - v2): pr = 1 else: return False an.append(pr) return True for nitish in range(int(input())): n = int(input()) arr = [] for i in range(n): ar = list(map(int, input().strip().split(" "))) arr.append(ar) arr.sort() low = 0.0 high = 10**20 * 1.0 if check(high, arr): print(-1) continue ans = 0.0 while abs(low - high) > 1e-06: mid = (low + high) / 2 if check(mid, arr): low = mid ans = max(ans, mid) else: high = mid print(ans)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR 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 FUNC_CALL VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statement in Mandarin chinese and Vietnamese. Suzumo is the coach of the ChefLand OI team. Physical condition is very important in any olympiad, so he wants to make the children run a bit as a warmup. The team consists of $N$ children numbered $1$ through $N$ standing at some positions on the $x$-axis. For each valid $i$, the initial $x$-coordinate of the $i$-th kid is $x_{i}$, the running velocity of the $i$-th kid is constant and equal to $v_{i}$. Suzumo wants to assign a running direction (left or right, i.e. in the direction of decreasing or increasing $x$-coordinate) to each kid; the children start running at time $0$ in the assigned directions. Afterwards, Suzumo will measure the smallest time $t$ at which some kid passes another one. Help Suzumo compute the maximum time $t$ if he can assign the directions arbitrarily! Note: Child $i$ *passes* child $j$ at time $t_{ij}$ if their coordinates satisfy $x_{i} < x_{j}$ at any time $t < t_{ij}$ and $x_{i} > x_{j}$ at any time $t > t_{ij}$, or if they satisfy $x_{i} > x_{j}$ at any time $t < t_{ij}$ and $x_{i} < x_{j}$ at any time $t > t_{ij}$. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains two space-separated integers $x_{i}$ and $v_{i}$. ------ Output ------ For each test case, print a single line containing one real number — the maximum possible time $t$, or $-1$ if it is possible to assign directions in such a way that no kid ever passes another. Your answer will be considered correct if it has an absolute or relative error less than or equal to $10^{-6}$. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 50$ $1 ≤ x_{i}, v_{i} ≤ 10^{9}$ for each valid $i$ no two children have the same initial positions ----- Sample Input 1 ------ 1 3 10 10 20 30 30 10 ----- Sample Output 1 ------ 0.5 ----- explanation 1 ------ Example case 1: One optimal assignment of directions is left, right, right.
def optimal_assignment(cs): PREC = 1e-10 cs.sort() lo, hi = 0.0, 2000000000.0 if has_solution(hi, cs): return -1 for _ in range(1000): mid = lo + (hi - lo) / 2 if has_solution(mid, cs): lo = mid else: hi = mid if hi - lo < PREC: break return lo + (hi - lo) / 2 def has_solution(t, cs): prev_p = -1e50 for p, v in cs: new_p = p - v * t if new_p > prev_p: prev_p = new_p else: new_p = p + v * t if new_p > prev_p: prev_p = new_p else: return False return True t = int(input()) for _ in range(t): n = int(input()) cs = [] for _ in range(n): cs.append(tuple(map(int, input().strip().split()))) print(optimal_assignment(cs))
FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Read problem statement in Mandarin chinese and Vietnamese. Suzumo is the coach of the ChefLand OI team. Physical condition is very important in any olympiad, so he wants to make the children run a bit as a warmup. The team consists of $N$ children numbered $1$ through $N$ standing at some positions on the $x$-axis. For each valid $i$, the initial $x$-coordinate of the $i$-th kid is $x_{i}$, the running velocity of the $i$-th kid is constant and equal to $v_{i}$. Suzumo wants to assign a running direction (left or right, i.e. in the direction of decreasing or increasing $x$-coordinate) to each kid; the children start running at time $0$ in the assigned directions. Afterwards, Suzumo will measure the smallest time $t$ at which some kid passes another one. Help Suzumo compute the maximum time $t$ if he can assign the directions arbitrarily! Note: Child $i$ *passes* child $j$ at time $t_{ij}$ if their coordinates satisfy $x_{i} < x_{j}$ at any time $t < t_{ij}$ and $x_{i} > x_{j}$ at any time $t > t_{ij}$, or if they satisfy $x_{i} > x_{j}$ at any time $t < t_{ij}$ and $x_{i} < x_{j}$ at any time $t > t_{ij}$. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains two space-separated integers $x_{i}$ and $v_{i}$. ------ Output ------ For each test case, print a single line containing one real number — the maximum possible time $t$, or $-1$ if it is possible to assign directions in such a way that no kid ever passes another. Your answer will be considered correct if it has an absolute or relative error less than or equal to $10^{-6}$. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 50$ $1 ≤ x_{i}, v_{i} ≤ 10^{9}$ for each valid $i$ no two children have the same initial positions ----- Sample Input 1 ------ 1 3 10 10 20 30 30 10 ----- Sample Output 1 ------ 0.5 ----- explanation 1 ------ Example case 1: One optimal assignment of directions is left, right, right.
for query in range(int(input())): n = int(input()) child = [] for i in range(n): child.append([int(v) for v in input().split()]) child.sort() d = [float("inf"), float("inf")] for i in range(1, n): if child[i][1] <= child[i - 1][1]: left_left = d[0] else: left_left = min( d[0], (child[i][0] - child[i - 1][0]) / float(child[i][1] - child[i - 1][1]), ) left_right = d[0] right_left = min( (child[i][0] - child[i - 1][0]) / float(child[i][1] + child[i - 1][1]), d[1] ) if child[i][1] >= child[i - 1][1]: right_right = d[1] else: right_right = min( d[1], (child[i][0] - child[i - 1][0]) / float(child[i - 1][1] - child[i][1]), ) d = [max(left_left, right_left), max(left_right, right_right)] if max(d) == float("inf"): print(-1) else: print(max(d))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR STRING FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Read problem statement in Mandarin chinese and Vietnamese. Suzumo is the coach of the ChefLand OI team. Physical condition is very important in any olympiad, so he wants to make the children run a bit as a warmup. The team consists of $N$ children numbered $1$ through $N$ standing at some positions on the $x$-axis. For each valid $i$, the initial $x$-coordinate of the $i$-th kid is $x_{i}$, the running velocity of the $i$-th kid is constant and equal to $v_{i}$. Suzumo wants to assign a running direction (left or right, i.e. in the direction of decreasing or increasing $x$-coordinate) to each kid; the children start running at time $0$ in the assigned directions. Afterwards, Suzumo will measure the smallest time $t$ at which some kid passes another one. Help Suzumo compute the maximum time $t$ if he can assign the directions arbitrarily! Note: Child $i$ *passes* child $j$ at time $t_{ij}$ if their coordinates satisfy $x_{i} < x_{j}$ at any time $t < t_{ij}$ and $x_{i} > x_{j}$ at any time $t > t_{ij}$, or if they satisfy $x_{i} > x_{j}$ at any time $t < t_{ij}$ and $x_{i} < x_{j}$ at any time $t > t_{ij}$. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains two space-separated integers $x_{i}$ and $v_{i}$. ------ Output ------ For each test case, print a single line containing one real number — the maximum possible time $t$, or $-1$ if it is possible to assign directions in such a way that no kid ever passes another. Your answer will be considered correct if it has an absolute or relative error less than or equal to $10^{-6}$. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 50$ $1 ≤ x_{i}, v_{i} ≤ 10^{9}$ for each valid $i$ no two children have the same initial positions ----- Sample Input 1 ------ 1 3 10 10 20 30 30 10 ----- Sample Output 1 ------ 0.5 ----- explanation 1 ------ Example case 1: One optimal assignment of directions is left, right, right.
LEFT = 0 RIGHT = 1 T = int(input()) coordinates = None def f(): ans = [[100000000000.0, 100000000000.0]] for i in range(1, len(coordinates)): ans.append([0, 0]) distance = coordinates[i][0] - coordinates[i - 1][0] velocity = coordinates[i][1] + coordinates[i - 1][1] time_taken = distance / velocity ans[i][LEFT] = max(ans[i][LEFT], min(ans[i - 1][RIGHT], time_taken)) velocity = coordinates[i - 1][1] - coordinates[i][1] if velocity > 0: time_taken = distance / velocity ans[i][RIGHT] = max(ans[i][RIGHT], min(ans[i - 1][RIGHT], time_taken)) else: ans[i][RIGHT] = max(ans[i][RIGHT], ans[i - 1][RIGHT]) velocity = coordinates[i][1] - coordinates[i - 1][1] if velocity > 0: time_taken = distance / velocity ans[i][LEFT] = max(ans[i][LEFT], min(ans[i - 1][LEFT], time_taken)) else: ans[i][LEFT] = max(ans[i][LEFT], ans[i - 1][LEFT]) ans[i][RIGHT] = max(ans[i][RIGHT], ans[i - 1][LEFT]) return ans[-1][RIGHT] for t in range(T): N = int(input()) coordinates = [tuple(map(int, input().split())) for _ in range(N)] coordinates.sort() answer = f() if answer == 100000000000.0: answer = -1 print(answer)
ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NONE FUNC_DEF ASSIGN VAR LIST LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers. Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is w_{i}, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash. Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks. Help the party to determine minimum possible total instability! -----Input----- The first line contains one number n (2 ≤ n ≤ 50). The second line contains 2·n integer numbers w_1, w_2, ..., w_2n, where w_{i} is weight of person i (1 ≤ w_{i} ≤ 1000). -----Output----- Print minimum possible total instability. -----Examples----- Input 2 1 2 3 4 Output 1 Input 4 1 3 4 6 3 4 100 200 Output 5
n = int(input()) val = list(map(int, input().split())) val.sort() b_a = 10**9 for i in range(len(val) - 1): for j in range(i + 1, len(val)): if i == j: continue ans = 0 a, b = val[i], val[j] val.pop(j) val.pop(i) ans = 0 for k in range(len(val)): if k % 2 == 0: ans += abs(val[k + 1] - val[k]) b_a = min(b_a, ans) val.insert(i, a) val.insert(j, b) print(b_a)
ASSIGN VAR FUNC_CALL VAR 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 NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers. Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is w_{i}, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash. Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks. Help the party to determine minimum possible total instability! -----Input----- The first line contains one number n (2 ≤ n ≤ 50). The second line contains 2·n integer numbers w_1, w_2, ..., w_2n, where w_{i} is weight of person i (1 ≤ w_{i} ≤ 1000). -----Output----- Print minimum possible total instability. -----Examples----- Input 2 1 2 3 4 Output 1 Input 4 1 3 4 6 3 4 100 200 Output 5
numPeople = int(input("")) * 2 peopleWeights = input("").split(" ") peopleWeights = [int(x) for x in peopleWeights] peopleWeights.sort() minUns = 9999999999 for i in range(numPeople): for j in range(numPeople): if i < j: curUns = 0 temp = peopleWeights.copy() temp.pop(j) temp.pop(i) for k in range(numPeople // 2 - 1): curUns += temp[2 * k + 1] - temp[2 * k] if curUns < minUns: minUns = curUns print(minUns)
ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers. Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is w_{i}, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash. Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks. Help the party to determine minimum possible total instability! -----Input----- The first line contains one number n (2 ≤ n ≤ 50). The second line contains 2·n integer numbers w_1, w_2, ..., w_2n, where w_{i} is weight of person i (1 ≤ w_{i} ≤ 1000). -----Output----- Print minimum possible total instability. -----Examples----- Input 2 1 2 3 4 Output 1 Input 4 1 3 4 6 3 4 100 200 Output 5
N = int(input()) weight = list(map(int, input().split())) weight.sort() mc = -1 for i in range(0, len(weight) - 1): for x in range(i + 1, len(weight)): count = 0 tl = weight.copy() tl.pop(i) tl.pop(x - 1) for k in range(0, len(tl) - 1, 2): count += tl[k + 1] - tl[k] if mc == -1: mc = count if count < mc: mc = count print(mc)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers. Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is w_{i}, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash. Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks. Help the party to determine minimum possible total instability! -----Input----- The first line contains one number n (2 ≤ n ≤ 50). The second line contains 2·n integer numbers w_1, w_2, ..., w_2n, where w_{i} is weight of person i (1 ≤ w_{i} ≤ 1000). -----Output----- Print minimum possible total instability. -----Examples----- Input 2 1 2 3 4 Output 1 Input 4 1 3 4 6 3 4 100 200 Output 5
from itertools import combinations n = int(input()) weights = list(map(int, input().split(" "))) weights = sorted(weights) best = float("inf") for first, second in combinations(range(len(weights)), 2): result = ( weights[0:first] + weights[first + 1 : second] + weights[second + 1 : len(weights)] ) it = iter(result) total = 0 for _ in range(int(len(result) / 2)): total += sum([abs(next(it) - next(it))]) best = min(best, total) print(best)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR LIST FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers. Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is w_{i}, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash. Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks. Help the party to determine minimum possible total instability! -----Input----- The first line contains one number n (2 ≤ n ≤ 50). The second line contains 2·n integer numbers w_1, w_2, ..., w_2n, where w_{i} is weight of person i (1 ≤ w_{i} ≤ 1000). -----Output----- Print minimum possible total instability. -----Examples----- Input 2 1 2 3 4 Output 1 Input 4 1 3 4 6 3 4 100 200 Output 5
n = int(input()) w = sorted([int(x) for x in input().split()]) best = 999999999999 for ex1 in range(1, n * 2): for ex2 in range(0, ex1): prev = -1 sum = 0 for i in range(0, n * 2): if i == ex1 or i == ex2: continue if prev == -1: prev = w[i] else: sum += w[i] - prev prev = -1 best = min(best, sum) print(best)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers. Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is w_{i}, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash. Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks. Help the party to determine minimum possible total instability! -----Input----- The first line contains one number n (2 ≤ n ≤ 50). The second line contains 2·n integer numbers w_1, w_2, ..., w_2n, where w_{i} is weight of person i (1 ≤ w_{i} ≤ 1000). -----Output----- Print minimum possible total instability. -----Examples----- Input 2 1 2 3 4 Output 1 Input 4 1 3 4 6 3 4 100 200 Output 5
n = int(input()) w = input() weights = [int(x) for x in w.split(" ")] weights.sort() def get_instability(i, j): test = ( weights[: min(i, j)] + weights[min(i, j) + 1 : max(i, j)] + weights[max(i, j) + 1 :] ) instability = 0 for k in range(0, len(test) - 1, 2): instability += abs(test[k] - test[k + 1]) return instability instabilities = [] for i in range(len(weights)): for j in range(len(weights)): if i == j: continue instabilities.append(get_instability(i, j)) print(min(instabilities))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers. Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is w_{i}, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash. Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks. Help the party to determine minimum possible total instability! -----Input----- The first line contains one number n (2 ≤ n ≤ 50). The second line contains 2·n integer numbers w_1, w_2, ..., w_2n, where w_{i} is weight of person i (1 ≤ w_{i} ≤ 1000). -----Output----- Print minimum possible total instability. -----Examples----- Input 2 1 2 3 4 Output 1 Input 4 1 3 4 6 3 4 100 200 Output 5
n = int(input()) w = list(map(int, input().split())) w.sort() m = 10**20 for i in range(2 * n): for j in range(i + 1, 2 * n): v = [] s = 0 for k in range(2 * n): if k != i and k != j: v.append(w[k]) for k in range(0, len(v) - 1, 2): s += v[k + 1] - v[k] if s < m: m = s print(m)
ASSIGN VAR FUNC_CALL VAR 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 NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers. Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is w_{i}, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash. Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks. Help the party to determine minimum possible total instability! -----Input----- The first line contains one number n (2 ≤ n ≤ 50). The second line contains 2·n integer numbers w_1, w_2, ..., w_2n, where w_{i} is weight of person i (1 ≤ w_{i} ≤ 1000). -----Output----- Print minimum possible total instability. -----Examples----- Input 2 1 2 3 4 Output 1 Input 4 1 3 4 6 3 4 100 200 Output 5
n = int(input()) w = list(map(int, input().split())) w.sort() p = 2 * n num = [] for i in range(p): for j in range(i + 1, p): l = [] dif = 0 for k in range(p): if k != i and k != j: l.append(w[k]) for x in range(1, n): dif += l[2 * x - 1] - l[2 * x - 2] num.append(dif) print(min(num))
ASSIGN VAR FUNC_CALL VAR 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 NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers. Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is w_{i}, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash. Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks. Help the party to determine minimum possible total instability! -----Input----- The first line contains one number n (2 ≤ n ≤ 50). The second line contains 2·n integer numbers w_1, w_2, ..., w_2n, where w_{i} is weight of person i (1 ≤ w_{i} ≤ 1000). -----Output----- Print minimum possible total instability. -----Examples----- Input 2 1 2 3 4 Output 1 Input 4 1 3 4 6 3 4 100 200 Output 5
n = int(input()) arr = list(map(int, input().split())) arr.sort() minIns = 0 for k in range(len(arr) - 1): minIns += arr[k + 1] - arr[k] for i in range(0, 2 * n): for j in range(i + 1, 2 * n): newArr = arr[0:i] + arr[i + 1 : j] + arr[j + 1 :] minIns = min( minIns, sum(newArr[k + 1] - newArr[k] for k in range(0, len(newArr) - 1, 2)) ) print(minIns)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers. Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is w_{i}, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash. Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks. Help the party to determine minimum possible total instability! -----Input----- The first line contains one number n (2 ≤ n ≤ 50). The second line contains 2·n integer numbers w_1, w_2, ..., w_2n, where w_{i} is weight of person i (1 ≤ w_{i} ≤ 1000). -----Output----- Print minimum possible total instability. -----Examples----- Input 2 1 2 3 4 Output 1 Input 4 1 3 4 6 3 4 100 200 Output 5
n = int(input()) n *= 2 a = [int(i) for i in input().split()] a.sort() ans = 100000000 for i in range(n): for j in range(i + 1, n): b = a[:i] + a[i + 1 : j] + a[j + 1 :] cr = 0 for k in range(1, len(b), 2): cr += b[k] - b[k - 1] ans = min(ans, cr) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers. Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is w_{i}, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash. Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks. Help the party to determine minimum possible total instability! -----Input----- The first line contains one number n (2 ≤ n ≤ 50). The second line contains 2·n integer numbers w_1, w_2, ..., w_2n, where w_{i} is weight of person i (1 ≤ w_{i} ≤ 1000). -----Output----- Print minimum possible total instability. -----Examples----- Input 2 1 2 3 4 Output 1 Input 4 1 3 4 6 3 4 100 200 Output 5
n = int(input()) p = list(map(int, input().split())) p.sort() temp = 0 ans = 10**10 for i in range(2 * n): for j in range(i + 1, 2 * n): s = 0 for k in range(2 * n): if k != i and k != j: if temp == 0: temp = p[k] else: s += abs(temp - p[k]) temp = 0 ans = min(ans, s) print(ans)
ASSIGN VAR FUNC_CALL VAR 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 BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers. Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is w_{i}, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash. Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks. Help the party to determine minimum possible total instability! -----Input----- The first line contains one number n (2 ≤ n ≤ 50). The second line contains 2·n integer numbers w_1, w_2, ..., w_2n, where w_{i} is weight of person i (1 ≤ w_{i} ≤ 1000). -----Output----- Print minimum possible total instability. -----Examples----- Input 2 1 2 3 4 Output 1 Input 4 1 3 4 6 3 4 100 200 Output 5
n = int(input()) a = list(map(int, input().split())) a.sort() an = 10**9 for i in range(2 * n): for j in range(i + 1, 2 * n): if i == j: continue b = a[:] del b[j], b[i] c = 0 for k in range(0, 2 * n - 2, 2): c += abs(b[k] - b[k + 1]) an = min(an, c) print(an)
ASSIGN VAR FUNC_CALL VAR 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 NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers. Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is w_{i}, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash. Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks. Help the party to determine minimum possible total instability! -----Input----- The first line contains one number n (2 ≤ n ≤ 50). The second line contains 2·n integer numbers w_1, w_2, ..., w_2n, where w_{i} is weight of person i (1 ≤ w_{i} ≤ 1000). -----Output----- Print minimum possible total instability. -----Examples----- Input 2 1 2 3 4 Output 1 Input 4 1 3 4 6 3 4 100 200 Output 5
def strip2(nums, s1, s2): res = [0] * (len(nums) - 2) j = 0 for i in range(0, len(nums)): if i == s1 or i == s2: continue res[j] = nums[i] j += 1 return res def countPairwiseDiff(nums): total = 0 for i in range(1, len(nums), 2): total += nums[i] - nums[i - 1] return total def completeSearch(nums): nums = sorted(nums) ans = 1999999999 for skip1 in range(len(nums) - 1): for skip2 in range(skip1 + 1, len(nums)): total = countPairwiseDiff(strip2(nums, skip1, skip2)) if total < ans: ans = total print(ans) def test(): completeSearch([101, 100, 99, 5, 4, 3, 2, 1]) completeSearch([1, 3, 4, 6, 3, 4, 100, 200]) completeSearch([1, 2, 3, 40, 50, 99, 100, 101]) completeSearch([1, 9, 10, 20, 100, 1000, 1001, 10001]) def nia(): s = input() while len(s) == 0: s = input() s = s.split() iVal = [] for i in range(len(s)): iVal.append(int(s[i])) return iVal def solve(): n = input() completeSearch(nia()) solve()
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers. Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is w_{i}, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash. Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks. Help the party to determine minimum possible total instability! -----Input----- The first line contains one number n (2 ≤ n ≤ 50). The second line contains 2·n integer numbers w_1, w_2, ..., w_2n, where w_{i} is weight of person i (1 ≤ w_{i} ≤ 1000). -----Output----- Print minimum possible total instability. -----Examples----- Input 2 1 2 3 4 Output 1 Input 4 1 3 4 6 3 4 100 200 Output 5
n = int(input()) n = n * 2 arr = sorted(map(int, input().split())) x = arr[n - 1] for i in range(n): for j in range(i): a, h, q = 0, 0, 0 while h < n: if h == i or h == j: pass else: if q % 2 == 0: a -= arr[h] else: a += arr[h] q += 1 h += 1 x = min(x, a) print(x)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN 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 VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER WHILE VAR VAR IF VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers. Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is w_{i}, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash. Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks. Help the party to determine minimum possible total instability! -----Input----- The first line contains one number n (2 ≤ n ≤ 50). The second line contains 2·n integer numbers w_1, w_2, ..., w_2n, where w_{i} is weight of person i (1 ≤ w_{i} ≤ 1000). -----Output----- Print minimum possible total instability. -----Examples----- Input 2 1 2 3 4 Output 1 Input 4 1 3 4 6 3 4 100 200 Output 5
n = int(input()) nums = list(map(int, input().strip().split())) weight = sorted(nums) min_total = sum(weight) for i in range(0, 2 * n - 1): for j in range(i + 1, 2 * n): nums_tmp = [] for k in range(2 * n): if k != i and k != j: nums_tmp.append(weight[k]) total = 0 for k in range(n - 1): total += nums_tmp[2 * k + 1] - nums_tmp[2 * k] if total < min_total: min_total = total print(min_total)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers. Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is w_{i}, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash. Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks. Help the party to determine minimum possible total instability! -----Input----- The first line contains one number n (2 ≤ n ≤ 50). The second line contains 2·n integer numbers w_1, w_2, ..., w_2n, where w_{i} is weight of person i (1 ≤ w_{i} ≤ 1000). -----Output----- Print minimum possible total instability. -----Examples----- Input 2 1 2 3 4 Output 1 Input 4 1 3 4 6 3 4 100 200 Output 5
from sys import stdin, stdout n = int(stdin.readline()) values = sorted(list(map(int, stdin.readline().split()))) ans = float("inf") for i in range(2 * n): for j in range(i + 1, 2 * n): res = [] for z in range(2 * n): if z == i or z == j: continue res.append(values[z]) cnt = 0 for q in range(0, len(res), 2): cnt += res[q + 1] - res[q] ans = min(ans, cnt) stdout.write(str(ans))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers. Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is w_{i}, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash. Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks. Help the party to determine minimum possible total instability! -----Input----- The first line contains one number n (2 ≤ n ≤ 50). The second line contains 2·n integer numbers w_1, w_2, ..., w_2n, where w_{i} is weight of person i (1 ≤ w_{i} ≤ 1000). -----Output----- Print minimum possible total instability. -----Examples----- Input 2 1 2 3 4 Output 1 Input 4 1 3 4 6 3 4 100 200 Output 5
n = int(input()) w = list(map(int, input().split())) w.sort() ans = 50000 for i in range(2 * n - 1): for j in range(i + 1, 2 * n): tmp = 0 for k in range(2 * n): if k < i or j < k: if k % 2 == 0: tmp -= w[k] else: tmp += w[k] elif i < k < j: if k % 2 == 1: tmp -= w[k] else: tmp += w[k] ans = min(ans, tmp) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR VAR IF VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers. Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is w_{i}, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash. Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks. Help the party to determine minimum possible total instability! -----Input----- The first line contains one number n (2 ≤ n ≤ 50). The second line contains 2·n integer numbers w_1, w_2, ..., w_2n, where w_{i} is weight of person i (1 ≤ w_{i} ≤ 1000). -----Output----- Print minimum possible total instability. -----Examples----- Input 2 1 2 3 4 Output 1 Input 4 1 3 4 6 3 4 100 200 Output 5
n = int(input()) w = list(map(int, input().split())) w.sort() stab1 = 0 stab2 = 0 stab3 = 0 stabmin = 99999999999999999 for i in range(2 * n): for j in range(i + 1, 2 * n): temp = w.copy() del temp[i] del temp[j - 1] stab1 = 0 for k in range(n - 1): stab1 += temp[2 * k + 1] - temp[2 * k] stabmin = min(stab1, stabmin) print(stabmin)
ASSIGN VAR FUNC_CALL VAR 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 NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers. Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is w_{i}, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash. Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks. Help the party to determine minimum possible total instability! -----Input----- The first line contains one number n (2 ≤ n ≤ 50). The second line contains 2·n integer numbers w_1, w_2, ..., w_2n, where w_{i} is weight of person i (1 ≤ w_{i} ≤ 1000). -----Output----- Print minimum possible total instability. -----Examples----- Input 2 1 2 3 4 Output 1 Input 4 1 3 4 6 3 4 100 200 Output 5
n = int(input()) w = list(map(int, input().split(" "))) w.sort() best = sum(w) for i in range(0, 2 * n): for j in range(i + 1, 2 * n): k = 0 k2 = 1 s = 0 while k2 < 2 * n: while k == i or k == j: k += 1 if k >= 2 * n: break k2 = k + 1 while k2 == i or k2 == j: k2 += 1 s += w[k2] - w[k] k = k2 + 1 k2 = k + 1 if s < best: best = s print(best)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP NUMBER VAR WHILE VAR VAR VAR VAR VAR NUMBER IF VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers. Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is w_{i}, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash. Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks. Help the party to determine minimum possible total instability! -----Input----- The first line contains one number n (2 ≤ n ≤ 50). The second line contains 2·n integer numbers w_1, w_2, ..., w_2n, where w_{i} is weight of person i (1 ≤ w_{i} ≤ 1000). -----Output----- Print minimum possible total instability. -----Examples----- Input 2 1 2 3 4 Output 1 Input 4 1 3 4 6 3 4 100 200 Output 5
n = int(input().strip().split()[0]) * 2 a = [int(x) for x in input().strip().split()] a.sort() ans = 1000000000 for i in range(n): for j in range(i + 1, n): idx = 0 res = 0 for k in range(n // 2 - 1): while idx == i or idx == j: idx += 1 r = idx + 1 while r == i or r == j: r += 1 res = res + a[r] - a[idx] idx = r + 1 ans = min(ans, res) print(ans)
ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers. Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is w_{i}, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash. Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks. Help the party to determine minimum possible total instability! -----Input----- The first line contains one number n (2 ≤ n ≤ 50). The second line contains 2·n integer numbers w_1, w_2, ..., w_2n, where w_{i} is weight of person i (1 ≤ w_{i} ≤ 1000). -----Output----- Print minimum possible total instability. -----Examples----- Input 2 1 2 3 4 Output 1 Input 4 1 3 4 6 3 4 100 200 Output 5
s = int(input()) n = 2 * s a = [int(x) for x in input().split()] a.sort() m = 10**100 for i in range(n - 1): for j in range(i + 1, n): b = [] for k in range(n): if k != i and k != j: b.append(a[k]) summa = 0 for k in range(0, n - 3, 2): summa = summa + abs(b[k] - b[k + 1]) if summa < m: m = summa print(m)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers. Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is w_{i}, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash. Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks. Help the party to determine minimum possible total instability! -----Input----- The first line contains one number n (2 ≤ n ≤ 50). The second line contains 2·n integer numbers w_1, w_2, ..., w_2n, where w_{i} is weight of person i (1 ≤ w_{i} ≤ 1000). -----Output----- Print minimum possible total instability. -----Examples----- Input 2 1 2 3 4 Output 1 Input 4 1 3 4 6 3 4 100 200 Output 5
from sys import stdin, stdout def func(data, x, y): data_c = data[:] data_c.remove(x) data_c.remove(y) result = 0 for x in range(len(data_c) // 2): result += abs(data_c[x * 2] - data_c[x * 2 + 1]) return result n = int(stdin.readline().rstrip()) data = list(map(int, stdin.readline().rstrip().split())) data.sort() mnx = sum(data) for x in range(n * 2): for y in range(n * 2): if x != y: mnx = min(mnx, func(data, data[x], data[y])) stdout.write(str(mnx))
FUNC_DEF ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR 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 VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers. Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is w_{i}, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash. Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks. Help the party to determine minimum possible total instability! -----Input----- The first line contains one number n (2 ≤ n ≤ 50). The second line contains 2·n integer numbers w_1, w_2, ..., w_2n, where w_{i} is weight of person i (1 ≤ w_{i} ≤ 1000). -----Output----- Print minimum possible total instability. -----Examples----- Input 2 1 2 3 4 Output 1 Input 4 1 3 4 6 3 4 100 200 Output 5
def diff(arr): ans = [] for i in range(1, len(arr), 2): ans.append(arr[i] - arr[i - 1]) return sum(ans) n = int(input()) a = list(map(int, input().split())) a.sort() real_ans = [] for i in range(2 * n - 1): for j in range(i + 1, 2 * n): b = a.copy() b.pop(i) b.pop(j - 1) real_ans.append(diff(b)) print(min(real_ans))
FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers. Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is w_{i}, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash. Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks. Help the party to determine minimum possible total instability! -----Input----- The first line contains one number n (2 ≤ n ≤ 50). The second line contains 2·n integer numbers w_1, w_2, ..., w_2n, where w_{i} is weight of person i (1 ≤ w_{i} ≤ 1000). -----Output----- Print minimum possible total instability. -----Examples----- Input 2 1 2 3 4 Output 1 Input 4 1 3 4 6 3 4 100 200 Output 5
import itertools n = int(input()) weights = sorted([int(x) for x in input().split(" ")]) combs = itertools.combinations(weights, len(weights) - 2) def instability(c): return sum([abs(c[i + 1] - c[i]) for i in range(0, len(c), 2)]) min_instability = float("inf") for c in combs: min_instability = min(instability(c), min_instability) print(min_instability)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers. Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is w_{i}, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash. Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks. Help the party to determine minimum possible total instability! -----Input----- The first line contains one number n (2 ≤ n ≤ 50). The second line contains 2·n integer numbers w_1, w_2, ..., w_2n, where w_{i} is weight of person i (1 ≤ w_{i} ≤ 1000). -----Output----- Print minimum possible total instability. -----Examples----- Input 2 1 2 3 4 Output 1 Input 4 1 3 4 6 3 4 100 200 Output 5
n = int(input()) l = list(map(int, input().split())) nat = False for i in range(2 * n): for c in range(i + 1, 2 * n): ls = l[:] ls[i] = 0 ls[c] = 0 ls = sorted(ls) now = 0 for loc in range(1, len(ls), 2): now += ls[loc] - ls[loc - 1] if now < nat or str(nat) == "False": nat = now print(nat)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR STRING ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers. Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is w_{i}, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash. Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks. Help the party to determine minimum possible total instability! -----Input----- The first line contains one number n (2 ≤ n ≤ 50). The second line contains 2·n integer numbers w_1, w_2, ..., w_2n, where w_{i} is weight of person i (1 ≤ w_{i} ≤ 1000). -----Output----- Print minimum possible total instability. -----Examples----- Input 2 1 2 3 4 Output 1 Input 4 1 3 4 6 3 4 100 200 Output 5
N = int(input()) Kayak = {} for i in [int(i) for i in input().split()]: if i in Kayak and Kayak[i] == 1: del Kayak[i] else: Kayak[i] = 1 k = list(Kayak.keys()) k.sort() def func(li): result = 0 for i in range(int(len(li) / 2)): result += abs(li[2 * i] - li[2 * i + 1]) return result mi = 1000 if len(k) <= 2: print(0) else: for i in range(len(k) - 1): for i2 in range(i + 1, len(k)): kc = k.copy() numi = k[i] numi2 = k[i2] kc.remove(numi) kc.remove(numi2) mi = min(mi, func(kc)) print(mi)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers. Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is w_{i}, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash. Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks. Help the party to determine minimum possible total instability! -----Input----- The first line contains one number n (2 ≤ n ≤ 50). The second line contains 2·n integer numbers w_1, w_2, ..., w_2n, where w_{i} is weight of person i (1 ≤ w_{i} ≤ 1000). -----Output----- Print minimum possible total instability. -----Examples----- Input 2 1 2 3 4 Output 1 Input 4 1 3 4 6 3 4 100 200 Output 5
diffs = int(input()) weights1 = sorted(list(map(int, input().split()))) instabilities = [] for x in range(len(weights1)): for y in range(len(weights1) - 1): weights2 = weights1[:] weights2.pop(x) weights2.pop(y) instability = 0 for i in range(diffs - 1): val = 99999999 for j in range(len(weights2) - 1): if weights2[j + 1] - weights2[j] < val: val = weights2[j] - weights2[j + 1] posRem1 = j + 1 posRem2 = j weights2.pop(posRem1) weights2.pop(posRem2) instability += abs(val) instabilities.append(instability) print(min(instabilities))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers. Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is w_{i}, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash. Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks. Help the party to determine minimum possible total instability! -----Input----- The first line contains one number n (2 ≤ n ≤ 50). The second line contains 2·n integer numbers w_1, w_2, ..., w_2n, where w_{i} is weight of person i (1 ≤ w_{i} ≤ 1000). -----Output----- Print minimum possible total instability. -----Examples----- Input 2 1 2 3 4 Output 1 Input 4 1 3 4 6 3 4 100 200 Output 5
def readln(): return map(int, input().rstrip().split()) def cal(dt): rs = 0 for i in range(len(dt) // 2): rs += abs(dt[2 * i] - dt[2 * i + 1]) return rs n = int(input()) data = list(readln()) data.sort() minw = 500000 for i in range(0, 2 * n - 1): for j in range(i + 1, 2 * n): dt = list(data) dt.pop(max(i, j)) dt.pop(min(i, j)) rs = cal(dt) if rs < minw: minw = rs print(minw)
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers. Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is w_{i}, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash. Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks. Help the party to determine minimum possible total instability! -----Input----- The first line contains one number n (2 ≤ n ≤ 50). The second line contains 2·n integer numbers w_1, w_2, ..., w_2n, where w_{i} is weight of person i (1 ≤ w_{i} ≤ 1000). -----Output----- Print minimum possible total instability. -----Examples----- Input 2 1 2 3 4 Output 1 Input 4 1 3 4 6 3 4 100 200 Output 5
def get_instability(w, x, y): total = 0 last = -1 for i in range(len(w)): if i == x or i == y: continue if last == -1: last = w[i] else: total += w[i] - last last = -1 return total def main(): n = int(input()) weights = sorted(list(map(int, input().split()))) min_instability = 1 << 30 for i in range(2 * n): for j in range(i + 1, 2 * n): instability = get_instability(weights, i, j) if instability < min_instability: min_instability = instability print(min_instability) main()
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers. Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is w_{i}, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash. Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks. Help the party to determine minimum possible total instability! -----Input----- The first line contains one number n (2 ≤ n ≤ 50). The second line contains 2·n integer numbers w_1, w_2, ..., w_2n, where w_{i} is weight of person i (1 ≤ w_{i} ≤ 1000). -----Output----- Print minimum possible total instability. -----Examples----- Input 2 1 2 3 4 Output 1 Input 4 1 3 4 6 3 4 100 200 Output 5
n = int(input()) w = list(map(int, input().split())) w.sort() ans = [] for i in range(2 * n): for j in range(i + 1, 2 * n): m = w[0:i] + w[i + 1 : j] + w[j + 1 :] var = sum(m[1::2]) - sum(m[::2]) ans.append(var) print(min(ans))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers. Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is w_{i}, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash. Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks. Help the party to determine minimum possible total instability! -----Input----- The first line contains one number n (2 ≤ n ≤ 50). The second line contains 2·n integer numbers w_1, w_2, ..., w_2n, where w_{i} is weight of person i (1 ≤ w_{i} ≤ 1000). -----Output----- Print minimum possible total instability. -----Examples----- Input 2 1 2 3 4 Output 1 Input 4 1 3 4 6 3 4 100 200 Output 5
import itertools n = int(input()) A = list(map(int, input().split())) ans = 10**18 for c in itertools.combinations(range(2 * n), 2): c1, c2 = c B = [] for i in range(2 * n): if i != c1 and i != c2: B.append(A[i]) B.sort() temp = 0 for i in range(n - 1): temp += B[2 * i + 1] - B[2 * i] ans = min(ans, temp) print(ans)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers. Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is w_{i}, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash. Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks. Help the party to determine minimum possible total instability! -----Input----- The first line contains one number n (2 ≤ n ≤ 50). The second line contains 2·n integer numbers w_1, w_2, ..., w_2n, where w_{i} is weight of person i (1 ≤ w_{i} ≤ 1000). -----Output----- Print minimum possible total instability. -----Examples----- Input 2 1 2 3 4 Output 1 Input 4 1 3 4 6 3 4 100 200 Output 5
n = input() kayaks = list(map(int, input().split(" "))) kayaks.sort() def calc_sum(kayaks): curr_sum = 0 for i in range(1, len(kayaks), 2): curr_sum += kayaks[i] - kayaks[i - 1] return curr_sum min_sum = sum(kayaks) for i in range(len(kayaks)): for j in range(i + 1, len(kayaks)): new_kayaks = kayaks[0:i] + kayaks[i + 1 : j] + kayaks[j + 1 :] min_sum = min(min_sum, calc_sum(new_kayaks)) print(min_sum)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers. Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is w_{i}, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash. Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks. Help the party to determine minimum possible total instability! -----Input----- The first line contains one number n (2 ≤ n ≤ 50). The second line contains 2·n integer numbers w_1, w_2, ..., w_2n, where w_{i} is weight of person i (1 ≤ w_{i} ≤ 1000). -----Output----- Print minimum possible total instability. -----Examples----- Input 2 1 2 3 4 Output 1 Input 4 1 3 4 6 3 4 100 200 Output 5
n = int(input()) w = list(map(int, input().split())) ans = [] w.sort() ans = float("inf") n *= 2 for i in range(n - 1): for j in range(i + 1, n): cur = 0 a = list(w[:i] + w[i + 1 : j] + w[j + 1 :]) for k in range(0, len(a) - 1, 2): cur += a[k + 1] - a[k] ans = min(ans, cur) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers. Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is w_{i}, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash. Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks. Help the party to determine minimum possible total instability! -----Input----- The first line contains one number n (2 ≤ n ≤ 50). The second line contains 2·n integer numbers w_1, w_2, ..., w_2n, where w_{i} is weight of person i (1 ≤ w_{i} ≤ 1000). -----Output----- Print minimum possible total instability. -----Examples----- Input 2 1 2 3 4 Output 1 Input 4 1 3 4 6 3 4 100 200 Output 5
n = int(input()) a = list(map(int, input().split())) res = int(1000000000.0 + 7) for i in range(2 * n): for j in range(i + 1, 2 * n): b = [a[k] for k in range(2 * n) if k != i and k != j] b.sort() s = 0 for k in range(n - 1): s += b[2 * k + 1] - b[2 * k] res = min(res, s) print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers. Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is w_{i}, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash. Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks. Help the party to determine minimum possible total instability! -----Input----- The first line contains one number n (2 ≤ n ≤ 50). The second line contains 2·n integer numbers w_1, w_2, ..., w_2n, where w_{i} is weight of person i (1 ≤ w_{i} ≤ 1000). -----Output----- Print minimum possible total instability. -----Examples----- Input 2 1 2 3 4 Output 1 Input 4 1 3 4 6 3 4 100 200 Output 5
n = int(input()) list = [int(x) for x in input().split(" ")] l = sorted(list) result = max(list) for i in range(len(l)): for j in range(len(l)): l = sorted(list) if i == j: break else: l[i] = 0 l[j] = 0 l.remove(0) l.remove(0) temp = 0 k = 0 while k < len(l): term = l[k + 1] - l[k] temp += term k += 2 result = min(result, temp) print(result)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers. Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is w_{i}, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash. Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks. Help the party to determine minimum possible total instability! -----Input----- The first line contains one number n (2 ≤ n ≤ 50). The second line contains 2·n integer numbers w_1, w_2, ..., w_2n, where w_{i} is weight of person i (1 ≤ w_{i} ≤ 1000). -----Output----- Print minimum possible total instability. -----Examples----- Input 2 1 2 3 4 Output 1 Input 4 1 3 4 6 3 4 100 200 Output 5
n = int(input()) weights = input().split() weights = [int(w) for w in weights] min_ans = float("inf") perm = list(weights) perm.sort() for i in range(0, len(perm)): for j in range(i, len(perm)): if i == j: continue weights = list(perm) weights.pop(max(i, j)) weights.pop(min(i, j)) diff = [] for k in range(0, len(weights) - 1, 2): diff.append(weights[k + 1] - weights[k]) ans = sum(diff) min_ans = min(min_ans, ans) print(min_ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers. Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is w_{i}, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash. Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks. Help the party to determine minimum possible total instability! -----Input----- The first line contains one number n (2 ≤ n ≤ 50). The second line contains 2·n integer numbers w_1, w_2, ..., w_2n, where w_{i} is weight of person i (1 ≤ w_{i} ≤ 1000). -----Output----- Print minimum possible total instability. -----Examples----- Input 2 1 2 3 4 Output 1 Input 4 1 3 4 6 3 4 100 200 Output 5
def vals(i, j, L): for x in range(len(L)): if x not in (i, j): yield L[x] def pairs(i, j, L): s = [] for x in vals(i, j, L): s.append(x) if len(s) == 2: yield s s = [] def summa(i, j, L): count = 0 for a, b in pairs(i, j, L): count += abs(a - b) return count def main(): input() L = [int(i) for i in input().split()] L.sort() mini = 10**100 for i in range(len(L)): for j in range(i + 1, len(L)): mini = min(mini, summa(i, j, L)) print(mini) main()
FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR VAR ASSIGN VAR LIST FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers. Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is w_{i}, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash. Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks. Help the party to determine minimum possible total instability! -----Input----- The first line contains one number n (2 ≤ n ≤ 50). The second line contains 2·n integer numbers w_1, w_2, ..., w_2n, where w_{i} is weight of person i (1 ≤ w_{i} ≤ 1000). -----Output----- Print minimum possible total instability. -----Examples----- Input 2 1 2 3 4 Output 1 Input 4 1 3 4 6 3 4 100 200 Output 5
n = int(input()) a = sorted(list(map(int, input().split()))) x = 10**20 for i in range(2 * n): for j in range(i + 1, 2 * n): s, l, r = 0, 0, 1 while l < 2 * n - 1: while l == i or l == j: l += 1 r = l + 1 while r == i or r == j: r += 1 if r >= 2 * n: break s += abs(a[r] - a[l]) l = r + 1 x = min(s, x) print(x)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER WHILE VAR BIN_OP BIN_OP NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER IF VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers. Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is w_{i}, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash. Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks. Help the party to determine minimum possible total instability! -----Input----- The first line contains one number n (2 ≤ n ≤ 50). The second line contains 2·n integer numbers w_1, w_2, ..., w_2n, where w_{i} is weight of person i (1 ≤ w_{i} ≤ 1000). -----Output----- Print minimum possible total instability. -----Examples----- Input 2 1 2 3 4 Output 1 Input 4 1 3 4 6 3 4 100 200 Output 5
n = int(input()) v = [int(i) for i in input().split()] v = sorted(v) def dp(i, chose): if chose == 0: d = 0 while i < len(v): d += v[i + 1] - v[i] i += 2 return d if i >= len(v) - 1: return 0 return min(v[i + 1] - v[i] + dp(i + 2, chose), dp(i + 1, chose - 1)) print(dp(0, 2))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER RETURN VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers. Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is w_{i}, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash. Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks. Help the party to determine minimum possible total instability! -----Input----- The first line contains one number n (2 ≤ n ≤ 50). The second line contains 2·n integer numbers w_1, w_2, ..., w_2n, where w_{i} is weight of person i (1 ≤ w_{i} ≤ 1000). -----Output----- Print minimum possible total instability. -----Examples----- Input 2 1 2 3 4 Output 1 Input 4 1 3 4 6 3 4 100 200 Output 5
n = int(input()) weights = [int(i) for i in input().split()] weights.sort() total_instability = [] for i in range(len(weights) - 1): a = weights[i] for j in range(i + 1, len(weights)): instability = 0 try: b = weights[j] weights.remove(a) weights.remove(b) for k in range(0, len(weights), 2): instability += weights[k + 1] - weights[k] weights.append(a) weights.append(b) weights.sort() except: pass total_instability.append(instability) print(min(total_instability))
ASSIGN VAR FUNC_CALL VAR 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 BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers. Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is w_{i}, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash. Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks. Help the party to determine minimum possible total instability! -----Input----- The first line contains one number n (2 ≤ n ≤ 50). The second line contains 2·n integer numbers w_1, w_2, ..., w_2n, where w_{i} is weight of person i (1 ≤ w_{i} ≤ 1000). -----Output----- Print minimum possible total instability. -----Examples----- Input 2 1 2 3 4 Output 1 Input 4 1 3 4 6 3 4 100 200 Output 5
ct = int(input()) weights = sorted(list(map(int, input().split()))) ultidiffs = [] for j in range(2 * ct): for i in range(2 * ct): if i != j: diffs = [] new_weights = [] for k in range(2 * ct): if k != i and k != j: new_weights.append(weights[k]) for l in range(len(new_weights) // 2): diffs.append(abs(new_weights[2 * l] - new_weights[2 * l + 1])) ultidiffs.append(sum(diffs)) print(min(ultidiffs))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers. Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is w_{i}, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash. Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks. Help the party to determine minimum possible total instability! -----Input----- The first line contains one number n (2 ≤ n ≤ 50). The second line contains 2·n integer numbers w_1, w_2, ..., w_2n, where w_{i} is weight of person i (1 ≤ w_{i} ≤ 1000). -----Output----- Print minimum possible total instability. -----Examples----- Input 2 1 2 3 4 Output 1 Input 4 1 3 4 6 3 4 100 200 Output 5
n = int(input()) s = list(map(int, input().split())) s = list(sorted(s)) ansm = 100000000 for i in range(len(s)): s1 = s[:i] + s[i + 1 :] for j in range(len(s1)): s2 = s1[:j] + s1[j + 1 :] ans = 0 for i in range(1, 2 * n - 2, 2): ans += s2[i] - s2[i - 1] if ans < ansm: ansm = ans print(ansm)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers. Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is w_{i}, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash. Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks. Help the party to determine minimum possible total instability! -----Input----- The first line contains one number n (2 ≤ n ≤ 50). The second line contains 2·n integer numbers w_1, w_2, ..., w_2n, where w_{i} is weight of person i (1 ≤ w_{i} ≤ 1000). -----Output----- Print minimum possible total instability. -----Examples----- Input 2 1 2 3 4 Output 1 Input 4 1 3 4 6 3 4 100 200 Output 5
def codeforces(numbers): results = [] numbers.sort() for i in range(len(numbers)): for j in range(len(numbers)): if i == j: continue nums = [numbers[indx] for indx in range(len(numbers)) if indx not in [i, j]] results.append(calculate(nums)) return min(results) def calculate(numbers): result = 0 for indx in range(0, len(numbers), 2): result += abs(numbers[indx + 1] - numbers[indx]) return result _ = input() numbers = list(map(int, input().split())) print(codeforces(numbers))
FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR LIST VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR 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