description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
n = int(input()) l = list(map(int, input().split())) l.sort() s = sum(l) c = 1 o = float("inf") a = t = 0 while t < s: t = sum(abs(c**i - l[i]) for i in range(n)) if t < o: a = c o = t c += 1 print(o)
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 FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
n, s = open(0) arr = sorted(map(int, s.split())) n = int(n) def mys(x): s = 0 for i in range(n): s += abs(arr[i] - x**i) return s mmax = mys(1) mmin = mys(1) t = 2 while t ** (n - 1) <= mmax + arr[n - 1]: mmin = min(mmin, mys(t)) t += 1 print(mmin)
ASSIGN VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
n = int(input()) lis = sorted(map(int, input().split())) if n == 1: print(0) else: root = int(lis[-1] ** (1 / float(n - 1))) + 1 ans = 100000000000000000000000000 for i in range(1, root + 2): if i ** (n - 1) > 20**9: break c = 1 tmp = 0 for j in range(n): tmp += abs(lis[j] - c) c *= i ans = min(ans, tmp) 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 IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
n = int(input()) arr = [int(d) for d in input().split()] arr.sort() t = int(pow(arr[n - 1], 1 / (n - 1))) s1 = 0 s2 = 0 for i in range(n): k1 = arr[i] - t**i k2 = arr[i] - (t + 1) ** i if k1 >= 0: s1 += k1 else: s1 -= k1 if k2 >= 0: s2 += k2 else: s2 -= k2 print(int(s1) if s1 <= s2 else int(s2))
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 FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
n = int(input()) arr = list(map(int, input().split())) cost = 10**100 arr.sort() for c in range(99999): ans = 0 for i in range(len(arr)): ans += abs(c**i - arr[i]) if ans < cost: cost = ans else: break print(cost)
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 EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
a = int(input()) arr = list(map(int, input().split())) arr.sort() n = 0 k = 0 prevans = float("inf") while True: ans = 0 for i in range(0, len(arr)): ans = ans + abs(pow(n, i) - arr[i]) if ans > prevans: k = 1 break n = n + 1 if k == 1: break prevans = ans print(prevans)
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 FUNC_CALL VAR STRING WHILE NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
n = int(input()) nums = list(map(int, input().split())) if len(nums) > 34: result = 0 for num in nums: result += num - 1 print(result) else: nums.sort() sum_of_elements = 0 for element in nums: sum_of_elements += element right_bound = 35000 best_c, min_loss = None, float("inf") for current_c in range(1, right_bound + 1): this_c_loss = 0 current_c_in_pow = 1 for power in range(len(nums)): current_loss = abs(current_c_in_pow - nums[power]) current_c_in_pow *= current_c this_c_loss += current_loss if this_c_loss < min_loss: min_loss = this_c_loss best_c = current_c print(min_loss)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NONE FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
import sys input = sys.stdin.readline N = int(input()) arr = list(map(int, input().split())) arr.sort() p = int(arr[-1] ** (1 / (N - 1))) - 1 seq = 1 while True: diff = 0 for i in range(N): diff += abs(arr[i] - p**i) if seq < 2: ans = diff elif ans > diff: ans = diff else: break p += 1 seq += 1 print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
n = int(input()) a = list(map(int, input().split())) a.sort() ans = 0 for i in range(n): ans += a[i] - 1 for i in range(1, 100000): b = [1] curr = 1 flag = 0 for j in range(n - 1): curr = curr * i if curr > 10**10: flag = 1 break b.append(curr) if flag == 1: continue tmp = 0 for j in range(n): tmp += abs(a[j] - b[j]) 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 VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
def getCost(a, c): cur = 1 result = 0 for i in range(len(a)): result += abs(a[i] - cur) cur *= c return result n = int(input()) a = [int(x) for x in input().split()] a.sort() result = float("inf") baseline = getCost(a, 1) for cand in range(10**7): if cand ** (n - 1) >= 5 * baseline: break result = min(result, getCost(a, cand)) print(result if result != float("inf") else 0)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR NUMBER
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
N = int(input()) a = sorted(map(int, input().split())) ans = 0 if N >= 100: for i in range(N): ans += a[i] - 1 print(ans) exit() i = 0 while True: if pow(i, N - 1) > a[-1]: t = i break i += 1 ans = 0 for i in range(N): ans += abs(pow(t, i) - a[i]) ans2 = 0 for i in range(N): ans2 += abs(pow(t - 1, i) - a[i]) print(min(ans, ans2))
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 IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
import sys input = sys.stdin.readline n = int(input()) a = list(map(int, input().split())) if n == 1 or n == 2: print(abs(a[0] - 1)) sys.exit(0) a.sort() bound = 1 while pow(bound, n - 1) <= 10000000000.0: bound += 1 if pow(bound, n - 1) > 19237982374980273849: bound = 1 break ans = 999999999999999 for b in range(1, bound + 1): cur = 0 mul = 1 for v in a: cur += abs(mul - v) mul *= b ans = min(ans, cur) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
import sys def f(a, m1, n): res = 0 pw = 1 for i in range(0, n): res += abs(a[i] - pw) pw *= m1 if res >= 10**14: break return res n = int(input()) a = list(map(int, input().split())) a = sorted(a) l = 0 r = 10**5 + 1 while r - l > 1: m = (l + r) // 2 if f(a, m, n) >= f(a, m + 1, n): l = m else: r = m print(min(f(a, l, n), f(a, r, n)))
IMPORT FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR IF VAR BIN_OP NUMBER NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
def pow(a, b): res = 1 while b != 0: if b % 2: b -= 1 res *= a else: b //= 2 a *= a return res n = int(input()) a = list(map(int, input().split())) a.sort() big = 100000000000 out1 = 1000000000000000 out2 = 0 i = 1 while 1: out2 = 0 flag = 0 for j in range(n): zz = pow(i, j) if zz > big: flag = 1 break out2 += abs(zz - a[j]) if flag: break i = i + 1 out1 = min(out1, out2) print(out1)
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
n = int(input()) a = [int(i) for i in input().split(" ")] a.sort() ans = 10**18 upper_bound = int(10 ** (10 / (n - 1))) for c in range(1, upper_bound + 1): m = 0 for i in range(0, n): m += abs(a[i] - c**i) if c**i > 10**20: m = 10**18 break ans = min(ans, m) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
n = int(input()) A = list(map(int, input().split())) A.sort() if n >= 100: print(sum(A) - n) else: ans = sum(A) - n M = 10**12 for i in range(2, max(A)): x = 0 y = 1 for a in A: if y > 10**11: y = -1 break x += abs(a - y) y *= i if y == -1: break ans = min(ans, x) 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 IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
def cost(a, x): ans = 0 cr = 1 for i in a: ans += abs(i - cr) cr *= x return ans n = int(input()) a = list(map(int, input().split())) a.sort() ans = cost(a, 1) if n > 33: print(ans) else: for i in range(2, 1000000000): cr = cost(a, i) ans = min(ans, cr) if cr >= 4 * ans: break print(ans)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
n = int(input()) arr = [int(num) for num in input().split(" ")] arr.sort() ans = arr[0] - 1 ans1 = arr[0] - 1 b = arr[-1] x = int(b ** (1 / (n - 1))) for i in range(1, n): ans = ans + abs(arr[i] - x**i) ans1 = ans1 + abs(arr[i] - (x + 1) ** i) print(min(ans, ans1))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
n = int(input()) l = list(map(int, input().split())) l.sort() s = [(0) for i in range(100001)] for j in range(100001): su = 0 for k in range(n): if j**k > 10**18: break su += abs(l[k] - j**k) s[j] = su print(min(s))
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 VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
n = int(input()) mas = [int(i) for i in input().split()] mas.sort() q = 1000 * 1000 * 1000 * 1000 * 1000 best = 1000 * 1000 * 1000 * 1000 * 1000 for a in range(100000): c = 1 now = 0 for i in range(n): now = now + abs(c - mas[i]) c *= a if now > q or c > q: now = q break best = min(now, best) print(best)
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 BIN_OP BIN_OP BIN_OP BIN_OP NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
def findNthRoot(x, n): x = float(x) n = int(n) if x >= 0 and x <= 1: low = x high = 1 else: low = 1 high = x epsilon = 0.1 guess = (low + high) / 2 while abs(guess**n - x) >= epsilon: if guess**n > x: high = guess else: low = guess guess = (low + high) / 2 return guess n = int(input()) arr = sorted(list(map(int, input().split()))) cost1 = 0 cost2 = 0 if 1 < n < 35: let = findNthRoot(arr[-1], n - 1) // 1 for i in range(n): cost1 += abs(arr[i] - let**i) cost2 += abs(arr[i] - (let + 1) ** i) print(int(min(cost1, cost2))) elif n <= 1: print(int(abs(arr[0] - 1))) else: for i in range(n): cost1 += abs(arr[i] - 1) print(cost1)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
n = int(input()) a = list(map(int, input().split())) ans = 10**15 a.sort() c = 1 while True: if pow(c, n - 1) > pow(10, 10): break temp = 0 for i in range(n): temp += abs(pow(c, i) - a[i]) ans = min(ans, temp) c += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
import sys input = sys.stdin.readline n = int(input()) a = list(map(int, input().split())) a.sort() c = 1 b = [] e = 0 for i in range(1, 10**9 + 2): s = 0 for j in range(len(a)): if i**j > 10**10: e = 1 break s += abs(i**j - a[j]) if e == 0: b.append(s) if e == 1: break print(min(b))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
n = int(input()) a = sorted(list(map(int, input().split()))) x = 1 c = 10**18 f = 0 while x ** (n - 1) <= f + a[-1]: m = 0 for i in range(n): m += abs(a[i] - x**i) if m < c: c = m if x == 1: f = m x += 1 print(c)
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 NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
from sys import stdin, stdout input = stdin.readline n = int(input()) a = [int(x) for x in input().split()] a.sort() ans = sum(a) - n if n < 100: c = 2 while c ** (n - 1) < 2 * sum(a): cur = 0 for i in range(n): cur += abs(a[i] - c**i) ans = min(cur, ans) c += 1 stdout.write(str(ans) + "\n")
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR BIN_OP VAR NUMBER BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
def all_same(a): return all([(x == 1) for x in a]) N = int(input()) array = sorted([int(x) for x in input().split()]) total = sum(array) previous = 10**20 for i in range(10**5 + 1): counter = 0 for j in range(N): counter += abs(array[j] - i**j) if counter > previous: print(previous) exit() else: previous = counter
FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER VAR VAR 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 FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
n = int(input()) l = list(map(int, input().split())) l.sort() mini = float("inf") if n < 34: for i in range(1, 32000): ans = 0 for j in range(n): ans += abs(i**j - l[j]) if mini > ans: mini = ans ch = i print(mini) else: ans = 0 for i in range(n): ans += l[i] - 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
from sys import stdin, stdout def fn(near): power = 1 ans = 0 for i in range(n): ans += abs(a[i] - power) if ans > aa: return float("inf") power *= near return ans for _ in range(1): n = int(stdin.readline()) a = list(map(int, stdin.readline().split())) a.sort() near = 1 aa = float("inf") end = 10**6 // n for c in range(1, min(55555, end)): aa = min(aa, fn(c)) print(aa)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR RETURN FUNC_CALL VAR STRING VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR NUMBER 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 FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
def fastPow(a, n): if n == 0: return 1 ans = fastPow(a, n // 2) ans = ans * ans if n % 2 == 1: ans = ans * a return ans n = int(input()) arr = [int(x) for x in input().split()] arr.sort() limit = fastPow(10, 24) i = 1 answer = fastPow(10, 24) limit2 = 1000000 while fastPow(i, n) <= limit and i <= limit2: b = 0 val = 1 for j in range(0, n): a1 = arr[j] a2 = val if a1 > a2: b += a1 - a2 else: b += a2 - a1 val = val * i if answer > b: answer = b i = i + 1 print(answer)
FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
n = int(input()) l = list(map(int, input().split())) l.sort() x = l[n - 1] y = 1 p = [] s = 0 for i in range(2, 100000): if i ** (n - 1) <= x: y = i else: break for i in range(n): s = s + l[i] - 1 p.append(s) s = 0 for i in range(0, n): s = s + abs(y**i - l[i]) p.append(s) s = 0 for i in range(0, n): s = s + abs((y + 1) ** i - l[i]) p.append(s) p.sort() print(p[0])
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 VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
n = int(input()) a = sorted(list(map(int, input().split()))) inf = 10**18 if n < 3: print(a[0] - 1) exit() ans = sum(a) - n for j in range(10**9): c_p = 1 c_c = 0 for i in range(n): c_c += abs(a[i] - c_p) c_p *= j + 1 if c_p > inf: break if c_p > inf: break if c_p / (j + 1) > ans + a[n - 1]: break ans = min(ans, c_c) print(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 BIN_OP NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
thres = 1000000000000000000 def power(a, b): result = 1 while b > 0: if b % 2 == 1: result = result * a if result > thres: return -1 a = a * a b = b // 2 return result def bin_search(): low = 2 high = last while low < high: mid = (low + high) // 2 temp = power(mid, n - 1) if temp > last or temp == -1: high = mid - 1 else: low = mid + 1 return low, high def calculate(num): res = 0 for i in range(n): temp = abs(a[i] - power(num, i)) res += temp return res n = int(input()) a = list(map(int, input().split())) a.sort() last = a[-1] first, second = bin_search() result = thres for i in range(second - 1, first + 2): temp = calculate(i) result = min(result, temp) print(result)
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
n = int(input()) input_array = input().split() array = [] for i in input_array: array.append(int(i)) array.sort() sum_var = sum(array) temp = 1 result_var = sum_var - n while pow(temp, n - 1) - array[n - 1] <= sum_var - n: position = 0 for i in range(n): temp_var = pow(temp, i) - array[i] if temp_var < 0: temp_var *= -1 position += temp_var result_var = min(result_var, position) temp += 1 print(result_var)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
n = int(input()) sequence = sorted(map(int, input().split())) sol = sum(sequence) - n if n >= 50: print(sol) exit() max_c = 33000 if n == 3 else 2000 for c in range(2, max_c): c_power_sequence = [(c**i) for i in range(n)] curr = 0 for i in range(n): curr += abs(sequence[i] - c_power_sequence[i]) if curr < sol: sol = curr print(sol)
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 FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
n = int(input()) a = sorted([int(r) for r in input().split()]) ans = 10**14 + 9 lim = 10**11 c = 1 while c ** (n - 1) <= lim: sum = 0 for i in range(0, n): sum += abs(c**i - a[i]) if sum <= ans: ans = sum c = c + 1 print(ans)
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 BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
import sys input = sys.stdin.readline def nth_element(c, k): return c**k for _ in range(1): n = int(input()) arr = [int(j) for j in input().split()] res = [] if n <= 50: arr.sort() cmax = (10**9) ** (1 / (n - 1)) for i in range(1, int(cmax) + 3): temp = 0 for j in range(n): temp += abs(arr[j] - nth_element(i, j)) res.append(temp) print(min(res)) else: print(abs(sum(arr) - n))
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST IF VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
def scanInt(): return int(input()) def scanList(): return [i for i in input().split(" ")] def scanIntList(): return [int(i) for i in scanList()] n = scanInt() a = scanIntList() def count(l, a): cost = 0 c = 1 for i in l: cost += abs(c - i) if c > 1e18: return 1e18 c *= a return cost a.sort() ans = count(a, 1) for i in range(1, int(pow(1000000000000000.0, 1 / n)) + 10): ans = min(ans, count(a, i)) print(ans)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN VAR VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER RETURN NUMBER VAR VAR RETURN VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
import sys input = sys.stdin.readline n = int(input()) A = list(map(int, input().split())) A.sort() k = 10**9 if n > 3: k = 10**5 m = 10**18 a = m for i in range(1, k): b = 0 for j in range(n): b += abs(A[j] - i**j) if b > a: break else: m = min(b, m) a = b print(m)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
n = int(input()) a = map(int, input().split()) a = list(sorted(a)) if n >= 60: print(sum(a) - n) else: min_dif = -1 for c in range(1, 10**6): dif = 0 for i in range(len(a)): dif += abs(c**i - a[i]) if dif < min_dif or min_dif == -1: min_dif = dif else: break print(min_dif)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
import sys def get_ints(): return list(map(int, sys.stdin.readline().strip().split())) n = int(input()) A = get_ints() A.sort() ans = 0 i = 0 while i < n: ans += A[i] - 1 i += 1 if n <= 60: base = 1 while base <= 10**5: flag = True while flag: i = 0 cur = 0 while i < n: cur += abs(A[i] - base**i) if cur >= ans: flag = False break i += 1 ans = min(ans, cur) base += 1 print(ans)
IMPORT FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
n = int(input()) inp = list(map(int, input().split())) inp.sort() i = 0 j = 0 while i ** (n - 1) <= inp[-1]: i += 1 while (j + 1) ** (n - 1) <= inp[-1]: j += 1 sm = 0 sm2 = 0 for z in range(n): sm += abs(inp[z] - i**z) sm2 += abs(inp[z] - j**z) print(min(sm, sm2))
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 WHILE BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER WHILE BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
import sys for _ in range(1): n = int(input()) a = [int(x) for x in input().split()] a.sort() best = 1e18 for c in range(100000): good = True x = 1 for i in range(n - 1): x *= c if x > 1000000000000.0: good = False break if good: cur = 0 x = 1 for i in range(0, n): cur += abs(a[i] - x) x *= c best = min(best, cur) print(best)
IMPORT FOR VAR FUNC_CALL VAR NUMBER 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 NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
import sys input = lambda: sys.stdin.readline().rstrip("\r\n") def solve(): n = int(input()) a = list(map(int, input().split())) a.sort() ans = float("inf") max_limit, maxx = 1, a[-1] while True: if pow(max_limit, n - 1) > maxx: break max_limit += 1 first, second = 0, 0 for i in range(n): first += abs(a[i] - pow(max_limit, i)) max_limit -= 1 for i in range(n): second += abs(a[i] - pow(max_limit, i)) print(min(first, second)) solve()
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF 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 FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER VAR NUMBER WHILE NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
n = int(input()) a = list(map(int, input().split())) a.sort() if n >= 35: sum0 = 0 for j in range(n): sum0 = sum0 + a[j] - 1 print(sum0) else: c = 1 while c ** (n - 1) < a[n - 1]: c = c + 1 if c == 1: sum0 = 0 b = 1 for j in range(n): if a[j] - b > 0: k = a[j] - b else: k = b - a[j] sum0 = sum0 + k print(sum0) else: sum1 = 0 b = 1 for j in range(n): if a[j] - b > 0: k = a[j] - b else: k = b - a[j] sum1 = sum1 + k b = b * c sum2 = 0 b = 1 for j in range(n): if a[j] - b > 0: k = a[j] - b else: k = b - a[j] sum2 = sum2 + k b = b * (c - 1) if sum1 > sum2: print(sum2) else: print(sum1)
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 IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
n = int(input()) a = list(map(int, input().split())) a = sorted(a) c = int(a[-1] ** (1 / (n - 1))) res1 = sum(abs(c**i - a[i]) for i in range(n)) res2 = sum(abs((c + 1) ** i - a[i]) for i in range(n)) print(res1 if res1 < res2 else res2)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
n = int(input()) arr = [int(i) for i in input().split()] arr.sort() max1 = (10**18) ** (1 / (n - 1)) ans = 10**18 c = 1 j = 1 while j <= max1: val = 0 ind = 1 for i in range(n): val += abs(ind - arr[i]) ind *= j if min(ans, val) == ans: break else: ans = val j += 1 print(ans)
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 BIN_OP BIN_OP NUMBER NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
n = int(input()) xs = [int(x) for x in input().split()] if n > 100: print(sum(x - 1 for x in xs)) elif n == 2: print(xs[0] - 1) else: xs.sort() ans = 123456123467819234 i = 0 while True: i += 1 cur = 0 for j in range(len(xs)): d = pow(i, j) cur += abs(d - xs[j]) if cur >= ans: break else: ans = min(ans, cur) continue break print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
def chk(arr, x): ml = 1 ans = 0 for i in arr: ans += abs(i - ml) ml *= x if ml > 10000000000000000000: return ml return ans n = int(input()) arr = list(map(int, input().split())) arr.sort() ans = 100000000000000000000 for i in range(1, 100010): x = chk(arr, i) ans = min(ans, x) print(ans)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER RETURN VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
n = int(input()) a = list(map(int, input().split())) a.sort() ans = 100000000000000.0 c = 1 while True: cost = 0 k = 1 for i in range(len(a)): cost += abs(a[i] - k) k = k * c if k > 100000000000000.0: cost = 100000000000000.0 break if cost > 100000000000000.0: break if cost == 100000000000000.0: break ans = min(cost, ans) c += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
import sys input = sys.stdin.readline def calc(r, n): return pow(r, n - 1) n = int(input()) a = [int(i) for i in input().split()] if n > 200: sys.stdout.write(str(sum(a) - n)) else: a.sort() maxa = a[-1] lo, hi = 1, 1000000000 while lo < hi: mid = (lo + (hi - 1)) // 2 mx1 = calc(mid, n) mx2 = calc(mid + 1, n) if abs(maxa - mx1) < abs(maxa - mx2): hi = mid else: lo = mid + 1 pq, pq1 = [], [] for i in range(n): pq.append(mid**i) for i in range(n): pq1.append((mid + 1) ** i) ans, ans1 = 0, 0 for i in range(n): ans += abs(a[i] - pq[i]) ans1 += abs(a[i] - pq1[i]) sys.stdout.write(str(min(ans1, ans)) + "\n")
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR STRING
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
from sys import stdin input = stdin.readline n = int(input()) a = list(map(int, input().rstrip().split())) a = sorted(a) answer = 10**18 c = 0 flag = True while flag: c += 1 newAns = 0 for i in range(n): newAns += abs(a[i] - c**i) if newAns > answer: flag = False break answer = min(answer, newAns) print(answer)
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
n = int(input()) a = list(map(int, input().split())) a.sort() def c(x): now = 1 res = 0 for i in range(n): if now > 10**18: return 10**18 res += abs(now - a[i]) now *= x return res l = 0 r = 10**9 while r - l > 10: llr = (l + l + r) // 3 lrr = (l + r + r) // 3 v1 = c(llr) v2 = c(lrr) if v1 > v2: l = llr else: r = lrr ans = 10**18 for i in range(l, l + 100): ans = min(ans, c(i)) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP NUMBER NUMBER RETURN BIN_OP NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
n = int(input()) (*a,) = map(int, input().split()) a.sort() ans = 10**40 if n >= 60: ans = sum(a) ans -= n print(ans) exit() mx = int(a[-1] ** (1 / (n - 1))) * n ee = 0 for i in range(1, mx): tmp = 0 for j in range(n): tmp += abs(pow(i, j) - a[j]) if ans > tmp: ee = i ans = min(ans, tmp) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
n = int(input()) a = [int(item) for item in input().split()] if n == 1: print(0) exit() ans = -1 a.sort() for p in range(1, int(pow(1000000000, 1.0 / (n - 1))) + 2): cur = 0 for i in range(n): cur += abs(a[i] - pow(p, i)) if ans == -1: ans = cur else: ans = min(ans, cur) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
n = int(input()) a = list(map(int, input().split())) a.sort() def solve(cur): ans = 0 tot = cur cur = 1 for aa in a: ans += abs(aa - cur) cur *= tot return ans ans = solve(1) tmp = int(pow(a[-1], 1 / (n - 1))) if n <= 32: for i in range(tmp - 1, tmp + 2): ans = min(ans, solve(i)) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
t = int(input()) x = list(map(int, input().split())) x.sort() n = len(x) c = 1 big = 10**10 while c ** (n - 1) <= big: c += 1 c -= 1 ans = float("inf") for i in range(1, c + 1): tmp = 0 for j in range(n): tmp += abs(x[j] - i**j) 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 FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
def checker(mid, arr, n): ansm = 0 ansf = 0 for i in range(n): am = mid**i af = (mid - 1) ** i ansm += abs(arr[i] - am) ansf += abs(arr[i] - af) return ansm, ansf n = int(input()) arr = list(map(int, input().split())) arr.sort() ans = 0 l = 0 r = int(arr[-1] ** (1 / (n - 2))) + 1 ans = 2**31 a = 0 while l <= r: mid = (l + r) // 2 ansm, ansf = checker(mid, arr, n) if ansm < ansf: l = mid + 1 ans = ansm else: ans = ansf r = mid - 1 a = mid print(ans)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN 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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
import sys input = sys.stdin.readline n = int(input()) cost, ans = 0, 1000000000000000000000000 a = [int(x) for x in input().split()] a.sort() l, r = -1, -1 if n > 32: l, r = 1, 1 elif 32 >= n > 20: l, r = 1, 2 elif 20 >= n >= 15: l, r = 1, 10 elif 15 > n >= 10: l, r = 1, 14 elif 10 > n >= 7: l, r = 1, 25 elif 7 > n >= 5: l, r = 1, 1000 elif n == 4: l, r = 1, 1200 elif n == 3: l, r = 1, 100000 elif n == 2: l, r = 1, 10000000 elif n == 1: l, r = 1, 10000000 for q in range(l, r + 1): cost = 0 for i in range(n): cost += abs(a[i] - q**i) ans = min(ans, cost) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
def check(m): global a, n ans = 0 cm = 1 for i in range(n): ans += abs(a[i] - cm) cm *= m return ans n = int(input()) a = list(map(int, input().split())) a.sort() if n > 200: print(check(1)) exit(0) l = 1 r = 10000000 while r - l != 1: s = (l + r) // 2 if check(s) - check(s + 1) > 0: l = s else: r = s print(min(check(r), check(l)))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
import sys def main(): import sys input = sys.stdin.buffer.readline n = int(input()) alst = list(map(int, input().split())) alst.sort() one = sum(alst) - n ans = one num = 1 while 1: cnt = 0 num2 = 1 for i in range(n): tmp = abs(num2 - alst[i]) if tmp > ans: break cnt += tmp num2 *= num else: num += 1 ans = min(ans, cnt) continue break print(ans) main()
IMPORT FUNC_DEF IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
mod = 10**9 + 7 def f(x): n = len(a) s = 0 p = 1 for i in range(n): s += abs(a[i] - p) p *= x if s > 10**30: break return s def solve(): n = int(input()) global a a = list(map(int, input().split())) a.sort() ans = 10**30 for i in range(1, 1000): ans = min(ans, f(i)) if n < 1000: for i in range(1000, 100000): ans = min(ans, f(i)) print(ans) t = 1 while t > 0: solve() t -= 1
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR IF VAR BIN_OP NUMBER NUMBER RETURN VAR FUNC_DEF 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 NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
import sys input = lambda: sys.stdin.readline().rstrip("\r\n") n = int(input()) a = [int(x) for x in input().split()] a.sort() ans = float("inf") for i in range(1, 31624): ans1 = 0 for j in range(n): ans1 += abs(i**j - a[j]) if ans1 > ans: break ans = min(ans, ans1) print(ans)
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING 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 FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
n = int(input()) numbers = input() spin = numbers.split() new = [] for i in range(n): new.append(int(spin[i])) new = sorted(new) sum = sum(new) k = 1 while pow(k, n - 1) < pow(10, 10): k += 1 counta = sum - n for x in range(2, k): count = 0 z = 0 for i in range(n): z = int(x) ** i - int(new[i]) count = count + abs(z) counta = min(count, counta) print(counta)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
n = int(input()) l = list(map(int, input().split())) l.sort() benchmark = sum([abs(x - 1) for x in l]) def f(num): err = 0 for i, x in enumerate(l): err += abs(x - num**i) if err > benchmark: return float("inf") return err x = 10000000000.0 + 1 ans = 0 while x > 0: while f(ans + x) > f(ans + x + 1): ans += x x //= 2 ans += 1 total = sum([int(abs(x - ans**i)) for i, x in enumerate(l)]) print(total)
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 FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR RETURN FUNC_CALL VAR STRING RETURN VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER WHILE FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
n = int(input()) nums = list(map(int, input().split())) nums.sort() minval = abs(sum(nums) - n) for c in range(2, int(100000.0)): diff = nums[0] - 1 val = 1 for i in range(1, n): val = val * c diff += abs(nums[i] - val) if diff > minval: break minval = min(minval, diff) print(minval)
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 FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
import sys reader = (s.rstrip() for s in sys.stdin) input = reader.__next__ n = int(input()) arry = list(map(int, input().split())) maxNum = max(arry) maxRecur = maxNum ** (1 / (n - 1)) currMin = float("inf") arry.sort() for i in range(int(maxRecur) + 2): curr = 0 for j in range(n): curr += abs(i**j - arry[j]) currMin = min(currMin, curr) print(currMin)
IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
import sys sys.setrecursionlimit(10**5) readline = sys.stdin.readline def process(): _ = int(readline()) arr = list(map(int, readline().strip().split())) arr.sort() ans, mx = 10**20, 10**20 overflow = False for i in range(1, 10**5): val, diff = 1, 0 for num in arr: if val * i > mx: overflow = True break else: diff += abs(val - num) val *= i ans = min(ans, diff) if overflow: break print(ans) process()
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP NUMBER NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
import sys def main(): n = int(input()) arr = get_array() arr.sort() ans = 10**18 r = 10**5 // n for i in range(1, r + 1): curr = 0 x = 0 for j in arr: curr += abs(j - i**x) x += 1 ans = min(ans, curr) print(ans) get_array = lambda: list(map(int, sys.stdin.readline().split())) get_ints = lambda: map(int, sys.stdin.readline().split()) input = lambda: sys.stdin.readline().strip() main()
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
n = int(input()) arr = list(map(int, input().strip().split()))[:n] arr.sort() ans = float("inf") c = 1 while True: power = 1 val = 0 for i in range(n): if power > 10**10: val = -1 break val += abs(power - arr[i]) power *= c if val == -1: break ans = min(ans, val) c += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
import sys input = sys.stdin.readline def inp(): return int(input()) def inlt(): return list(map(int, input().split())) def insr(): s = input() return list(s[: len(s) - 1]) def invr(): return map(int, input().split()) n = inp() l = list(invr()) l.sort() min_cost = None i = 0 while True: i += 1 cost = 0 for j, num in enumerate(l): power = i**j cost += abs(power - num) if min_cost is not None and cost > min_cost: print(min_cost) break min_cost = cost
IMPORT ASSIGN VAR 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 ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NONE ASSIGN VAR NUMBER WHILE NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NONE VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
def solve(): limit = 10**10 ans = 10**15 j = 1 while True: val = 0 power = 1 for i in range(N): if power > limit: val = -1 break val += abs(power - A[i]) power *= j if val == -1: break ans = min(ans, val) j += 1 return ans N = int(input()) A = [int(s) for s in input().split()] A.sort() print(solve())
FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
from sys import stdin, stdout input = stdin.readline def geti(): return map(int, input().strip().split()) def getl(): return list(map(int, input().strip().split())) def gets(): return input("") def geta(): return int(input()) def print_s(s): stdout.write(s + "\n") def solve(): n = geta() a = getl() ans = float("inf") a.sort() if n > 32: print(sum(a) - n) return r = 1 while True: temp = 0 prefix = [1] for i in range(n): temp += abs(prefix[-1] - a[i]) prefix.append(prefix[-1] * r) ans = min(ans, temp) if r ** (n - 1) > a[-1]: break r += 1 print(ans) solve()
ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
n = int(input()) arr = list(map(int, input().split())) sol = -1 arr.sort() bnd = int(1000000.0 - n) c = 1 while c ** (n - 1) < arr[-1] * (n + 1): curr = 0 for i in range(len(arr)): curr += abs(arr[i] - c**i) if sol == -1 or curr < sol: sol = curr c += 1 print(sol)
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 EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
import sys def input(): return sys.stdin.readline().rstrip() def input_split(): return [int(i) for i in input().split()] def cost(c, arr): if c == 0: return sum(arr) else: return sum([int(abs(c**i - arr[i])) for i in range(len(arr))]) n = int(input()) arr = input_split() if n > 35 or n == 1: ans = sum(arr) - n else: arr.sort() if n == 2: ans = min(arr) - 1 else: ans = 10**12 for c in range(1, 4 * 10**4): pot = cost(c, arr) ans = min(ans, pot) print(ans)
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \le i \le n-1$ then $a_i = c^i$. Given a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\{0,1,...,n - 1\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. Find the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Input----- The first line contains an integer $n$ ($3 \le n \le 10^5$). The second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence. -----Examples----- Input 3 1 3 2 Output 1 Input 3 1000000000 1000000000 1000000000 Output 1999982505 -----Note----- In the first example, we first reorder $\{1, 3, 2\}$ into $\{1, 2, 3\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\{1, 2, 4\}$.
n = int(input()) l = list(map(int, input().split())) l = sorted(l) c = 2 ans = sum(l) - n while True: currPw = 1 currCost = 0 done = 0 for i in l: if currPw - l[-1] > ans: done = 1 break currCost += abs(currPw - i) currPw *= c if done: break ans = min(ans, currCost) c += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Naruto has recently joined the Ninja Academy for higher education, while filling the admission form he is required to choose minimum $X$ subjects out of a list of $N$ subjects. He is not very interested in studies but wants to get a good overall CPI. However, every subject has a different weightage/credit. The credit (denoted as $C$_{i}) of a particular course is equal to the position in which it appears in the subject list, i.e, the first course has a credit=1, the second course has a credit=2, and so on. Naruto knows his capabilities, and so he has come up with the scores ($S$_{i}) he can achieve in every course. He wants to maximise his CPI, which can be calculated as the weighted mean of all the subjects taken, denoted as __Sum(C_{i}*S_{i})/Sum(C_{i})__ for all i s.t Naruto has taken up course i ------ Input: ------ First line will contain $T$, number of testcases. Then the testcases follow. First line of every test case contains two integers, $N$ and $X$ Next line of the test case contains $N$ space separated integers S_{i}, denoting the score he can get in every subject ------ Output: ------ For each testcase, output in a single line the maximum cpi he can get after choosing the subjects. Your output will be considered correct if it has an absolute error of less than 10^{-6}. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 1000$ $1 ≤ X ≤ N$ $1 ≤ S$_{i}$ ≤ 10000$ ------ Sample Input: ------ 1 5 2 50 40 30 20 10 ------ Sample Output: ------ 43.3333333333 ------ Explanation: ------ He can achieve the max credit by picking the first two subjects, which produces a CPI of (50 * 1+40 * 2)/(1+2) = 43.333333333
t = int(input()) for r in range(t): [n, x] = [int(y) for y in input().split()] e1 = [int(u) for u in input().split()] e = [[e1[x], x + 1] for x in range(n)] p = [] x1 = 0 c1 = 0 for x2 in range(n): if x2 < x: y2 = 0 for x3 in e: y1 = (x1 * c1 + x3[1] * x3[0]) / (c1 + x3[1]) if y1 > y2: temp = x3 temp1 = y1 y2 = y1 e.remove(temp) x1 = temp1 c1 += temp[1] print(x1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN LIST 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 LIST VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Naruto has recently joined the Ninja Academy for higher education, while filling the admission form he is required to choose minimum $X$ subjects out of a list of $N$ subjects. He is not very interested in studies but wants to get a good overall CPI. However, every subject has a different weightage/credit. The credit (denoted as $C$_{i}) of a particular course is equal to the position in which it appears in the subject list, i.e, the first course has a credit=1, the second course has a credit=2, and so on. Naruto knows his capabilities, and so he has come up with the scores ($S$_{i}) he can achieve in every course. He wants to maximise his CPI, which can be calculated as the weighted mean of all the subjects taken, denoted as __Sum(C_{i}*S_{i})/Sum(C_{i})__ for all i s.t Naruto has taken up course i ------ Input: ------ First line will contain $T$, number of testcases. Then the testcases follow. First line of every test case contains two integers, $N$ and $X$ Next line of the test case contains $N$ space separated integers S_{i}, denoting the score he can get in every subject ------ Output: ------ For each testcase, output in a single line the maximum cpi he can get after choosing the subjects. Your output will be considered correct if it has an absolute error of less than 10^{-6}. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 1000$ $1 ≤ X ≤ N$ $1 ≤ S$_{i}$ ≤ 10000$ ------ Sample Input: ------ 1 5 2 50 40 30 20 10 ------ Sample Output: ------ 43.3333333333 ------ Explanation: ------ He can achieve the max credit by picking the first two subjects, which produces a CPI of (50 * 1+40 * 2)/(1+2) = 43.333333333
for _ in range(int(input())): n, x = [int(i) for i in input().split()] s = [int(i) for i in input().split()] cg = s[0] credits = 1 for i in range(1, n): if s[i] > cg: cg = s[i] credits = i + 1 s[credits - 1] = 0 while x > 1: best = 0 for i in range(n): if s[i] != 0: temp = (cg * credits + (i + 1) * s[i]) / (credits + 1 + i) if temp > best: best = temp val = i credits += val + 1 cg = best s[val] = 0 x -= 1 print(cg)
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 VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Naruto has recently joined the Ninja Academy for higher education, while filling the admission form he is required to choose minimum $X$ subjects out of a list of $N$ subjects. He is not very interested in studies but wants to get a good overall CPI. However, every subject has a different weightage/credit. The credit (denoted as $C$_{i}) of a particular course is equal to the position in which it appears in the subject list, i.e, the first course has a credit=1, the second course has a credit=2, and so on. Naruto knows his capabilities, and so he has come up with the scores ($S$_{i}) he can achieve in every course. He wants to maximise his CPI, which can be calculated as the weighted mean of all the subjects taken, denoted as __Sum(C_{i}*S_{i})/Sum(C_{i})__ for all i s.t Naruto has taken up course i ------ Input: ------ First line will contain $T$, number of testcases. Then the testcases follow. First line of every test case contains two integers, $N$ and $X$ Next line of the test case contains $N$ space separated integers S_{i}, denoting the score he can get in every subject ------ Output: ------ For each testcase, output in a single line the maximum cpi he can get after choosing the subjects. Your output will be considered correct if it has an absolute error of less than 10^{-6}. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 1000$ $1 ≤ X ≤ N$ $1 ≤ S$_{i}$ ≤ 10000$ ------ Sample Input: ------ 1 5 2 50 40 30 20 10 ------ Sample Output: ------ 43.3333333333 ------ Explanation: ------ He can achieve the max credit by picking the first two subjects, which produces a CPI of (50 * 1+40 * 2)/(1+2) = 43.333333333
t = int(input()) for _ in range(t): n, x = [int(i) for i in input().split()] a = [int(i) for i in input().split()] s = 0 d = 0 for i in range(x): max = 0 dt = 0 for j in range(n): if a[j] != 0 and (s + a[j] * (j + 1)) / (d + j + 1) > max: max = (s + a[j] * (j + 1)) / (d + j + 1) dt = j s += a[dt] * (dt + 1) a[dt] = 0 d += dt + 1 print("{:.10f}".format(s / d))
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 NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR VAR
Naruto has recently joined the Ninja Academy for higher education, while filling the admission form he is required to choose minimum $X$ subjects out of a list of $N$ subjects. He is not very interested in studies but wants to get a good overall CPI. However, every subject has a different weightage/credit. The credit (denoted as $C$_{i}) of a particular course is equal to the position in which it appears in the subject list, i.e, the first course has a credit=1, the second course has a credit=2, and so on. Naruto knows his capabilities, and so he has come up with the scores ($S$_{i}) he can achieve in every course. He wants to maximise his CPI, which can be calculated as the weighted mean of all the subjects taken, denoted as __Sum(C_{i}*S_{i})/Sum(C_{i})__ for all i s.t Naruto has taken up course i ------ Input: ------ First line will contain $T$, number of testcases. Then the testcases follow. First line of every test case contains two integers, $N$ and $X$ Next line of the test case contains $N$ space separated integers S_{i}, denoting the score he can get in every subject ------ Output: ------ For each testcase, output in a single line the maximum cpi he can get after choosing the subjects. Your output will be considered correct if it has an absolute error of less than 10^{-6}. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 1000$ $1 ≤ X ≤ N$ $1 ≤ S$_{i}$ ≤ 10000$ ------ Sample Input: ------ 1 5 2 50 40 30 20 10 ------ Sample Output: ------ 43.3333333333 ------ Explanation: ------ He can achieve the max credit by picking the first two subjects, which produces a CPI of (50 * 1+40 * 2)/(1+2) = 43.333333333
def ii(): return int(input()) def mi(): return map(int, input().split()) def li(): return list(mi()) def check(A, n, k): num = 0 dem = 0 mx = 0 AA = [0] * n a, b = 0, 0 for i in range(n): for j in range(n): if mx < (A[i] + A[j]) / (i + j + 2) and i != j: mx = (A[i] + A[j]) / (i + j + 2) a = i b = j num += A[a] + A[b] dem += a + b + 2 AA[a] = 1 AA[b] = 1 x = 2 while x < k: a, b = 0, 0 mx = 0 for i in range(n): if AA[i] == 0: temp = (num + A[i]) / (dem + i + 1) if mx < temp: mx = temp a = A[i] b = i + 1 x += 1 num += a dem += b AA[b - 1] = 1 return mx for _ in range(ii()): n, k = mi() A = li() if k == 1: print(max(A)) else: for i in range(n): A[i] = A[i] * (i + 1) mx = check(A, n, k) print("{0:.10f}".format(mx))
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
As usual, Sereja has array a, its elements are integers: a[1], a[2], ..., a[n]. Let's introduce notation: $f(a, l, r) = \sum_{i = l}^{r} a [ i ] ; m(a) = \operatorname{max}_{1 \leq l \leq r \leq n} f(a, l, r)$ A swap operation is the following sequence of actions: choose two indexes i, j (i ≠ j); perform assignments tmp = a[i], a[i] = a[j], a[j] = tmp. What maximum value of function m(a) can Sereja get if he is allowed to perform at most k swap operations? -----Input----- The first line contains two integers n and k (1 ≤ n ≤ 200; 1 ≤ k ≤ 10). The next line contains n integers a[1], a[2], ..., a[n] ( - 1000 ≤ a[i] ≤ 1000). -----Output----- In a single line print the maximum value of m(a) that Sereja can get if he is allowed to perform at most k swap operations. -----Examples----- Input 10 2 10 -1 2 2 2 2 2 2 -1 10 Output 32 Input 5 10 -1 -1 -1 -1 -1 Output -1
n, k = [int(c) for c in input().split()] a = [int(c) for c in input().split()] best = -1000001 seq = [] other = [] for l in range(n): for r in range(l + 1, n + 1): seq = sorted(a[l:r]) other = a[:l] + a[r:] other.sort() other.reverse() seq_sum = sum(seq) best = max(best, seq_sum) for sw in range(0, min(k, len(seq), len(other))): seq_sum = seq_sum - seq[sw] + other[sw] best = max(best, seq_sum) print(best)
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 NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
As usual, Sereja has array a, its elements are integers: a[1], a[2], ..., a[n]. Let's introduce notation: $f(a, l, r) = \sum_{i = l}^{r} a [ i ] ; m(a) = \operatorname{max}_{1 \leq l \leq r \leq n} f(a, l, r)$ A swap operation is the following sequence of actions: choose two indexes i, j (i ≠ j); perform assignments tmp = a[i], a[i] = a[j], a[j] = tmp. What maximum value of function m(a) can Sereja get if he is allowed to perform at most k swap operations? -----Input----- The first line contains two integers n and k (1 ≤ n ≤ 200; 1 ≤ k ≤ 10). The next line contains n integers a[1], a[2], ..., a[n] ( - 1000 ≤ a[i] ≤ 1000). -----Output----- In a single line print the maximum value of m(a) that Sereja can get if he is allowed to perform at most k swap operations. -----Examples----- Input 10 2 10 -1 2 2 2 2 2 2 -1 10 Output 32 Input 5 10 -1 -1 -1 -1 -1 Output -1
def readln(): return tuple(map(int, input().split())) n, k = readln() a = list(readln()) ans = -(10**9) for i in range(n): for j in range(i, n): x = a[:i] + a[j + 1 :] y = a[i : j + 1] x.sort() y.sort() x.reverse() for p in range(min(k, min(len(x), len(y)))): if x[p] > y[p]: x[p], y[p] = y[p], x[p] ans = max(ans, sum(y)) print(ans)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
As usual, Sereja has array a, its elements are integers: a[1], a[2], ..., a[n]. Let's introduce notation: $f(a, l, r) = \sum_{i = l}^{r} a [ i ] ; m(a) = \operatorname{max}_{1 \leq l \leq r \leq n} f(a, l, r)$ A swap operation is the following sequence of actions: choose two indexes i, j (i ≠ j); perform assignments tmp = a[i], a[i] = a[j], a[j] = tmp. What maximum value of function m(a) can Sereja get if he is allowed to perform at most k swap operations? -----Input----- The first line contains two integers n and k (1 ≤ n ≤ 200; 1 ≤ k ≤ 10). The next line contains n integers a[1], a[2], ..., a[n] ( - 1000 ≤ a[i] ≤ 1000). -----Output----- In a single line print the maximum value of m(a) that Sereja can get if he is allowed to perform at most k swap operations. -----Examples----- Input 10 2 10 -1 2 2 2 2 2 2 -1 10 Output 32 Input 5 10 -1 -1 -1 -1 -1 Output -1
n, k = map(int, input().split()) a = list(map(int, input().split())) def solve(i, j): cur_res = sum(a[i : j + 1]) a1 = sorted(a[i : j + 1]) a2 = sorted(a[:i] + a[j + 1 :], reverse=True) for t in range(min(k, len(a1), len(a2))): m = min(a1) if a2[t] > m: cur_res += a2[t] - m a1[a1.index(m)] = a2[t] return cur_res print(max(solve(i, j) for i in range(n) for j in range(i, n)))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR
As usual, Sereja has array a, its elements are integers: a[1], a[2], ..., a[n]. Let's introduce notation: $f(a, l, r) = \sum_{i = l}^{r} a [ i ] ; m(a) = \operatorname{max}_{1 \leq l \leq r \leq n} f(a, l, r)$ A swap operation is the following sequence of actions: choose two indexes i, j (i ≠ j); perform assignments tmp = a[i], a[i] = a[j], a[j] = tmp. What maximum value of function m(a) can Sereja get if he is allowed to perform at most k swap operations? -----Input----- The first line contains two integers n and k (1 ≤ n ≤ 200; 1 ≤ k ≤ 10). The next line contains n integers a[1], a[2], ..., a[n] ( - 1000 ≤ a[i] ≤ 1000). -----Output----- In a single line print the maximum value of m(a) that Sereja can get if he is allowed to perform at most k swap operations. -----Examples----- Input 10 2 10 -1 2 2 2 2 2 2 -1 10 Output 32 Input 5 10 -1 -1 -1 -1 -1 Output -1
bot = True read_line = lambda: [int(i) for i in input().split()] n, k = read_line() x = read_line() print( max( sum(sorted(x[l:r] + sorted(x[:l] + x[r:])[-k:])[l - r :]) for l in range(n) for r in range(l + 1, n + 1) ) )
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER
As usual, Sereja has array a, its elements are integers: a[1], a[2], ..., a[n]. Let's introduce notation: $f(a, l, r) = \sum_{i = l}^{r} a [ i ] ; m(a) = \operatorname{max}_{1 \leq l \leq r \leq n} f(a, l, r)$ A swap operation is the following sequence of actions: choose two indexes i, j (i ≠ j); perform assignments tmp = a[i], a[i] = a[j], a[j] = tmp. What maximum value of function m(a) can Sereja get if he is allowed to perform at most k swap operations? -----Input----- The first line contains two integers n and k (1 ≤ n ≤ 200; 1 ≤ k ≤ 10). The next line contains n integers a[1], a[2], ..., a[n] ( - 1000 ≤ a[i] ≤ 1000). -----Output----- In a single line print the maximum value of m(a) that Sereja can get if he is allowed to perform at most k swap operations. -----Examples----- Input 10 2 10 -1 2 2 2 2 2 2 -1 10 Output 32 Input 5 10 -1 -1 -1 -1 -1 Output -1
def f(a, k): ans = -float("inf") for i in range(len(a) + 1): for j in range(i): mid = a[j:i] l = a[:j] + a[i:] l = sorted(l, reverse=True) mid = sorted(mid) s = sum(mid) ans = max(ans, s) kk = k while kk: if l and mid and s - mid[0] + l[0] > s: s = s - mid[0] + l[0] l.pop(0) mid.pop(0) kk -= 1 ans = max(ans, s) else: break return ans n, k = map(int, input().strip().split()) a = list(map(int, input().strip().split())) print(f(a, k))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR WHILE VAR IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
As usual, Sereja has array a, its elements are integers: a[1], a[2], ..., a[n]. Let's introduce notation: $f(a, l, r) = \sum_{i = l}^{r} a [ i ] ; m(a) = \operatorname{max}_{1 \leq l \leq r \leq n} f(a, l, r)$ A swap operation is the following sequence of actions: choose two indexes i, j (i ≠ j); perform assignments tmp = a[i], a[i] = a[j], a[j] = tmp. What maximum value of function m(a) can Sereja get if he is allowed to perform at most k swap operations? -----Input----- The first line contains two integers n and k (1 ≤ n ≤ 200; 1 ≤ k ≤ 10). The next line contains n integers a[1], a[2], ..., a[n] ( - 1000 ≤ a[i] ≤ 1000). -----Output----- In a single line print the maximum value of m(a) that Sereja can get if he is allowed to perform at most k swap operations. -----Examples----- Input 10 2 10 -1 2 2 2 2 2 2 -1 10 Output 32 Input 5 10 -1 -1 -1 -1 -1 Output -1
n, kr = map(int, input().split()) l = list(map(int, input().split())) ans = -2e18 for i in range(n): for j in range(i, n): k = kr sub = l[i : j + 1] sub_sum = sum(sub) sub.sort() chk = l[0:i] + l[j + 1 :] chk.sort() chk.reverse() ans = max(ans, sub_sum) for t in range(len(sub)): if t < len(chk) and k > 0: if sub[t] < chk[t]: sub_sum -= sub[t] sub_sum += chk[t] ans = max(ans, sub_sum) k -= 1 else: break print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
As usual, Sereja has array a, its elements are integers: a[1], a[2], ..., a[n]. Let's introduce notation: $f(a, l, r) = \sum_{i = l}^{r} a [ i ] ; m(a) = \operatorname{max}_{1 \leq l \leq r \leq n} f(a, l, r)$ A swap operation is the following sequence of actions: choose two indexes i, j (i ≠ j); perform assignments tmp = a[i], a[i] = a[j], a[j] = tmp. What maximum value of function m(a) can Sereja get if he is allowed to perform at most k swap operations? -----Input----- The first line contains two integers n and k (1 ≤ n ≤ 200; 1 ≤ k ≤ 10). The next line contains n integers a[1], a[2], ..., a[n] ( - 1000 ≤ a[i] ≤ 1000). -----Output----- In a single line print the maximum value of m(a) that Sereja can get if he is allowed to perform at most k swap operations. -----Examples----- Input 10 2 10 -1 2 2 2 2 2 2 -1 10 Output 32 Input 5 10 -1 -1 -1 -1 -1 Output -1
n, k = map(int, input().split(" ")) A = list(map(int, input().split(" "))) best = max(A) for left in range(n): for right in range(left, n): inner = A[left : right + 1] outer = A[:left] + A[right + 1 :] inner.sort() outer.sort(reverse=True) for i in range(k): if i >= len(inner) or i >= len(outer): break if inner[i] < outer[i]: inner[i] = outer[i] else: break best = max(best, sum(inner)) print(best)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
As usual, Sereja has array a, its elements are integers: a[1], a[2], ..., a[n]. Let's introduce notation: $f(a, l, r) = \sum_{i = l}^{r} a [ i ] ; m(a) = \operatorname{max}_{1 \leq l \leq r \leq n} f(a, l, r)$ A swap operation is the following sequence of actions: choose two indexes i, j (i ≠ j); perform assignments tmp = a[i], a[i] = a[j], a[j] = tmp. What maximum value of function m(a) can Sereja get if he is allowed to perform at most k swap operations? -----Input----- The first line contains two integers n and k (1 ≤ n ≤ 200; 1 ≤ k ≤ 10). The next line contains n integers a[1], a[2], ..., a[n] ( - 1000 ≤ a[i] ≤ 1000). -----Output----- In a single line print the maximum value of m(a) that Sereja can get if he is allowed to perform at most k swap operations. -----Examples----- Input 10 2 10 -1 2 2 2 2 2 2 -1 10 Output 32 Input 5 10 -1 -1 -1 -1 -1 Output -1
R = lambda: map(int, input().split()) n, k = R() a = list(R()) def f(l, r): x = sorted(a[:l] + a[r + 1 :], reverse=True) y = sorted(a[l : r + 1]) return sum(y + [max(0, x[i] - y[i]) for i in range(min(k, len(x), len(y)))]) print(max(f(l, r) for l in range(n) for r in range(l, n)))
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR
As usual, Sereja has array a, its elements are integers: a[1], a[2], ..., a[n]. Let's introduce notation: $f(a, l, r) = \sum_{i = l}^{r} a [ i ] ; m(a) = \operatorname{max}_{1 \leq l \leq r \leq n} f(a, l, r)$ A swap operation is the following sequence of actions: choose two indexes i, j (i ≠ j); perform assignments tmp = a[i], a[i] = a[j], a[j] = tmp. What maximum value of function m(a) can Sereja get if he is allowed to perform at most k swap operations? -----Input----- The first line contains two integers n and k (1 ≤ n ≤ 200; 1 ≤ k ≤ 10). The next line contains n integers a[1], a[2], ..., a[n] ( - 1000 ≤ a[i] ≤ 1000). -----Output----- In a single line print the maximum value of m(a) that Sereja can get if he is allowed to perform at most k swap operations. -----Examples----- Input 10 2 10 -1 2 2 2 2 2 2 -1 10 Output 32 Input 5 10 -1 -1 -1 -1 -1 Output -1
n, m = map(int, input().split()) lis = list(map(int, input().split())) k = -100000000 for l in range(n): for r in range(l + 1, n + 1): k = max(k, sum(sorted(lis[l:r] + sorted(lis[:l] + lis[r:])[-m:])[l - r :])) print(k)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
As usual, Sereja has array a, its elements are integers: a[1], a[2], ..., a[n]. Let's introduce notation: $f(a, l, r) = \sum_{i = l}^{r} a [ i ] ; m(a) = \operatorname{max}_{1 \leq l \leq r \leq n} f(a, l, r)$ A swap operation is the following sequence of actions: choose two indexes i, j (i ≠ j); perform assignments tmp = a[i], a[i] = a[j], a[j] = tmp. What maximum value of function m(a) can Sereja get if he is allowed to perform at most k swap operations? -----Input----- The first line contains two integers n and k (1 ≤ n ≤ 200; 1 ≤ k ≤ 10). The next line contains n integers a[1], a[2], ..., a[n] ( - 1000 ≤ a[i] ≤ 1000). -----Output----- In a single line print the maximum value of m(a) that Sereja can get if he is allowed to perform at most k swap operations. -----Examples----- Input 10 2 10 -1 2 2 2 2 2 2 -1 10 Output 32 Input 5 10 -1 -1 -1 -1 -1 Output -1
def solve(a, l, r, k): out = sorted(a[:l] + a[r:], reverse=True) inside = sorted(a[l:r]) cur = sum(a[l:r]) for i in range(min(k, len(inside), len(out))): if out[i] > inside[i]: cur += out[i] - inside[i] else: break return cur n, k = [int(x) for x in input().split()] a = [int(x) for x in input().split()] assert len(a) == n best = a[0] for l in range(n): for r in range(l + 1, n + 1): cur = solve(a, l, r, k) if cur > best: best = cur print(best)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR RETURN 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 FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
As usual, Sereja has array a, its elements are integers: a[1], a[2], ..., a[n]. Let's introduce notation: $f(a, l, r) = \sum_{i = l}^{r} a [ i ] ; m(a) = \operatorname{max}_{1 \leq l \leq r \leq n} f(a, l, r)$ A swap operation is the following sequence of actions: choose two indexes i, j (i ≠ j); perform assignments tmp = a[i], a[i] = a[j], a[j] = tmp. What maximum value of function m(a) can Sereja get if he is allowed to perform at most k swap operations? -----Input----- The first line contains two integers n and k (1 ≤ n ≤ 200; 1 ≤ k ≤ 10). The next line contains n integers a[1], a[2], ..., a[n] ( - 1000 ≤ a[i] ≤ 1000). -----Output----- In a single line print the maximum value of m(a) that Sereja can get if he is allowed to perform at most k swap operations. -----Examples----- Input 10 2 10 -1 2 2 2 2 2 2 -1 10 Output 32 Input 5 10 -1 -1 -1 -1 -1 Output -1
n, k = map(int, input().split()) a = list(map(int, input().split())) res = a[0] for l in range(n): for r in range(l, n): inside = sorted(a[l : r + 1]) outside = sorted(a[:l] + a[r + 1 :], reverse=True) new_res = sum(inside) for i in range(min(k, len(inside), len(outside))): if outside[i] > inside[i]: new_res += outside[i] - inside[i] else: break if new_res > res: res = new_res print(res)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
As usual, Sereja has array a, its elements are integers: a[1], a[2], ..., a[n]. Let's introduce notation: $f(a, l, r) = \sum_{i = l}^{r} a [ i ] ; m(a) = \operatorname{max}_{1 \leq l \leq r \leq n} f(a, l, r)$ A swap operation is the following sequence of actions: choose two indexes i, j (i ≠ j); perform assignments tmp = a[i], a[i] = a[j], a[j] = tmp. What maximum value of function m(a) can Sereja get if he is allowed to perform at most k swap operations? -----Input----- The first line contains two integers n and k (1 ≤ n ≤ 200; 1 ≤ k ≤ 10). The next line contains n integers a[1], a[2], ..., a[n] ( - 1000 ≤ a[i] ≤ 1000). -----Output----- In a single line print the maximum value of m(a) that Sereja can get if he is allowed to perform at most k swap operations. -----Examples----- Input 10 2 10 -1 2 2 2 2 2 2 -1 10 Output 32 Input 5 10 -1 -1 -1 -1 -1 Output -1
def solve(curr, other, k): t = 0 while t < k and t < len(curr) and t < len(other) and other[t] > curr[t]: t += 1 return t n, k = map(int, input().split()) arr = list(map(int, input().split())) maxx = -(10**100) for i in range(n): curr = [] other = sorted(arr[:i] + arr[i + 1 :], reverse=True) for j in range(i, n): curr.append(arr[j]) curr.sort() if j < n - 1: del other[other.index(arr[j + 1])] t = min(len(curr), len(other), k) t = solve(curr, other, k) maxx = max(maxx, sum(curr) - sum(curr[:t]) + sum(other[:t])) print(maxx)
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Thanos wants to destroy the avengers base, but he needs to destroy the avengers along with their base. Let we represent their base with an array, where each position can be occupied by many avengers, but one avenger can occupy only one position. Length of their base is a perfect power of $2$. Thanos wants to destroy the base using minimum power. He starts with the whole base and in one step he can do either of following: if the current length is at least $2$, divide the base into $2$ equal halves and destroy them separately, or burn the current base. If it contains no avenger in it, it takes $A$ amount of power, otherwise it takes his $B \cdot n_a \cdot l$ amount of power, where $n_a$ is the number of avengers and $l$ is the length of the current base. Output the minimum power needed by Thanos to destroy the avengers' base. -----Input----- The first line contains four integers $n$, $k$, $A$ and $B$ ($1 \leq n \leq 30$, $1 \leq k \leq 10^5$, $1 \leq A,B \leq 10^4$), where $2^n$ is the length of the base, $k$ is the number of avengers and $A$ and $B$ are the constants explained in the question. The second line contains $k$ integers $a_{1}, a_{2}, a_{3}, \ldots, a_{k}$ ($1 \leq a_{i} \leq 2^n$), where $a_{i}$ represents the position of avenger in the base. -----Output----- Output one integer — the minimum power needed to destroy the avengers base. -----Examples----- Input 2 2 1 2 1 3 Output 6 Input 3 2 1 2 1 7 Output 8 -----Note----- Consider the first example. One option for Thanos is to burn the whole base $1-4$ with power $2 \cdot 2 \cdot 4 = 16$. Otherwise he can divide the base into two parts $1-2$ and $3-4$. For base $1-2$, he can either burn it with power $2 \cdot 1 \cdot 2 = 4$ or divide it into $2$ parts $1-1$ and $2-2$. For base $1-1$, he can burn it with power $2 \cdot 1 \cdot 1 = 2$. For $2-2$, he can destroy it with power $1$, as there are no avengers. So, the total power for destroying $1-2$ is $2 + 1 = 3$, which is less than $4$. Similarly, he needs $3$ power to destroy $3-4$. The total minimum power needed is $6$.
def explosion(a, x, y, q, l, r): if r - l == 1: q1 = q while q1 < len(a) and a[q1] < r: q1 += 1 if q1 == q: return x else: return (q1 - q) * y q1 = q while q1 < len(a) and a[q1] < (l + r) // 2: q1 += 1 q2 = q1 while q2 < len(a) and a[q2] < r: q2 += 1 if q1 - q == 0 and q2 - q1 == 0: return x if q2 - q1 != 0 and q1 - q != 0: d1, d2, d = ( explosion(a, x, y, q, l, (l + r) // 2), explosion(a, x, y, q1, (l + r) // 2, r), (q2 - q) * (r - l) * y, ) if d1 + d2 < d: return d1 + d2 else: return d if q1 != q: d, d1 = explosion(a, x, y, q, l, (l + r) // 2), (r - l) * (q1 - q) * y if x + d < d1: return x + d else: return d1 else: d, d1 = explosion(a, x, y, q1, (l + r) // 2, r), (r - l) * (q2 - q1) * y if x + d < d1: return x + d else: return d1 n, k, x, y = map(int, input().split()) a = sorted(list(map(lambda x: int(x) - 1, input().split()))) print(explosion(a, x, y, 0, 0, 2**n))
FUNC_DEF IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN VAR RETURN BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER RETURN VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR RETURN BIN_OP VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR RETURN BIN_OP VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR RETURN BIN_OP VAR VAR RETURN VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER BIN_OP NUMBER VAR
Thanos wants to destroy the avengers base, but he needs to destroy the avengers along with their base. Let we represent their base with an array, where each position can be occupied by many avengers, but one avenger can occupy only one position. Length of their base is a perfect power of $2$. Thanos wants to destroy the base using minimum power. He starts with the whole base and in one step he can do either of following: if the current length is at least $2$, divide the base into $2$ equal halves and destroy them separately, or burn the current base. If it contains no avenger in it, it takes $A$ amount of power, otherwise it takes his $B \cdot n_a \cdot l$ amount of power, where $n_a$ is the number of avengers and $l$ is the length of the current base. Output the minimum power needed by Thanos to destroy the avengers' base. -----Input----- The first line contains four integers $n$, $k$, $A$ and $B$ ($1 \leq n \leq 30$, $1 \leq k \leq 10^5$, $1 \leq A,B \leq 10^4$), where $2^n$ is the length of the base, $k$ is the number of avengers and $A$ and $B$ are the constants explained in the question. The second line contains $k$ integers $a_{1}, a_{2}, a_{3}, \ldots, a_{k}$ ($1 \leq a_{i} \leq 2^n$), where $a_{i}$ represents the position of avenger in the base. -----Output----- Output one integer — the minimum power needed to destroy the avengers base. -----Examples----- Input 2 2 1 2 1 3 Output 6 Input 3 2 1 2 1 7 Output 8 -----Note----- Consider the first example. One option for Thanos is to burn the whole base $1-4$ with power $2 \cdot 2 \cdot 4 = 16$. Otherwise he can divide the base into two parts $1-2$ and $3-4$. For base $1-2$, he can either burn it with power $2 \cdot 1 \cdot 2 = 4$ or divide it into $2$ parts $1-1$ and $2-2$. For base $1-1$, he can burn it with power $2 \cdot 1 \cdot 1 = 2$. For $2-2$, he can destroy it with power $1$, as there are no avengers. So, the total power for destroying $1-2$ is $2 + 1 = 3$, which is less than $4$. Similarly, he needs $3$ power to destroy $3-4$. The total minimum power needed is $6$.
def min_cost(l, r, arr): if len(arr) == 0: return a elif l == r: return b * len(arr) else: m = (l + r) // 2 back = [] forw = [] for i in arr: if i <= m: back.append(i) else: forw.append(i) return min( b * len(arr) * (r - l + 1), min_cost(l, m, back) + min_cost(m + 1, r, forw) ) n, k, a, b = map(int, input().split()) arr = list(map(int, input().split())) arr.sort() print(min_cost(1, 2**n, arr))
FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR IF VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN 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 EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR VAR
Thanos wants to destroy the avengers base, but he needs to destroy the avengers along with their base. Let we represent their base with an array, where each position can be occupied by many avengers, but one avenger can occupy only one position. Length of their base is a perfect power of $2$. Thanos wants to destroy the base using minimum power. He starts with the whole base and in one step he can do either of following: if the current length is at least $2$, divide the base into $2$ equal halves and destroy them separately, or burn the current base. If it contains no avenger in it, it takes $A$ amount of power, otherwise it takes his $B \cdot n_a \cdot l$ amount of power, where $n_a$ is the number of avengers and $l$ is the length of the current base. Output the minimum power needed by Thanos to destroy the avengers' base. -----Input----- The first line contains four integers $n$, $k$, $A$ and $B$ ($1 \leq n \leq 30$, $1 \leq k \leq 10^5$, $1 \leq A,B \leq 10^4$), where $2^n$ is the length of the base, $k$ is the number of avengers and $A$ and $B$ are the constants explained in the question. The second line contains $k$ integers $a_{1}, a_{2}, a_{3}, \ldots, a_{k}$ ($1 \leq a_{i} \leq 2^n$), where $a_{i}$ represents the position of avenger in the base. -----Output----- Output one integer — the minimum power needed to destroy the avengers base. -----Examples----- Input 2 2 1 2 1 3 Output 6 Input 3 2 1 2 1 7 Output 8 -----Note----- Consider the first example. One option for Thanos is to burn the whole base $1-4$ with power $2 \cdot 2 \cdot 4 = 16$. Otherwise he can divide the base into two parts $1-2$ and $3-4$. For base $1-2$, he can either burn it with power $2 \cdot 1 \cdot 2 = 4$ or divide it into $2$ parts $1-1$ and $2-2$. For base $1-1$, he can burn it with power $2 \cdot 1 \cdot 1 = 2$. For $2-2$, he can destroy it with power $1$, as there are no avengers. So, the total power for destroying $1-2$ is $2 + 1 = 3$, which is less than $4$. Similarly, he needs $3$ power to destroy $3-4$. The total minimum power needed is $6$.
N, K, A, B = map(int, input().split()) DATA = sorted(list(map(int, input().split()))) idx = 1 next_data = [(DATA[0], 1)] while idx < len(DATA): if DATA[idx] == next_data[-1][0]: next_data[-1] = DATA[idx], next_data[-1][1] + 1 else: next_data.append((DATA[idx], 1)) idx += 1 DATA = next_data DATA = [(i - 1, i - 1, n, B * n) for i, n in DATA] size = 1 for _ in range(N): next_data = [] size *= 2 idx = 0 while idx < len(DATA): i, j, n, val = DATA[idx] ni = i // size * size nj = (i // size + 1) * size - 1 if idx < len(DATA) - 1 and DATA[idx + 1][1] == nj: i2, j2, n2, val2 = DATA[idx + 1] next_data.append((ni, nj, n + n2, min(val + val2, B * size * (n + n2)))) idx += 1 else: next_data.append((ni, nj, n, min(val + A, B * size * n))) idx += 1 DATA = next_data print(DATA[0][3])
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER
Thanos wants to destroy the avengers base, but he needs to destroy the avengers along with their base. Let we represent their base with an array, where each position can be occupied by many avengers, but one avenger can occupy only one position. Length of their base is a perfect power of $2$. Thanos wants to destroy the base using minimum power. He starts with the whole base and in one step he can do either of following: if the current length is at least $2$, divide the base into $2$ equal halves and destroy them separately, or burn the current base. If it contains no avenger in it, it takes $A$ amount of power, otherwise it takes his $B \cdot n_a \cdot l$ amount of power, where $n_a$ is the number of avengers and $l$ is the length of the current base. Output the minimum power needed by Thanos to destroy the avengers' base. -----Input----- The first line contains four integers $n$, $k$, $A$ and $B$ ($1 \leq n \leq 30$, $1 \leq k \leq 10^5$, $1 \leq A,B \leq 10^4$), where $2^n$ is the length of the base, $k$ is the number of avengers and $A$ and $B$ are the constants explained in the question. The second line contains $k$ integers $a_{1}, a_{2}, a_{3}, \ldots, a_{k}$ ($1 \leq a_{i} \leq 2^n$), where $a_{i}$ represents the position of avenger in the base. -----Output----- Output one integer — the minimum power needed to destroy the avengers base. -----Examples----- Input 2 2 1 2 1 3 Output 6 Input 3 2 1 2 1 7 Output 8 -----Note----- Consider the first example. One option for Thanos is to burn the whole base $1-4$ with power $2 \cdot 2 \cdot 4 = 16$. Otherwise he can divide the base into two parts $1-2$ and $3-4$. For base $1-2$, he can either burn it with power $2 \cdot 1 \cdot 2 = 4$ or divide it into $2$ parts $1-1$ and $2-2$. For base $1-1$, he can burn it with power $2 \cdot 1 \cdot 1 = 2$. For $2-2$, he can destroy it with power $1$, as there are no avengers. So, the total power for destroying $1-2$ is $2 + 1 = 3$, which is less than $4$. Similarly, he needs $3$ power to destroy $3-4$. The total minimum power needed is $6$.
def rec(l, r, a, A, B): if len(a) == 0: return A cur = len(a) * B * (r - l + 1) if l == r: return cur b = [] c = [] mid = (l + r) // 2 for i in a: if i <= mid: b.append(i) else: c.append(i - mid - 1) return min(rec(l, mid, b, A, B) + rec(l, mid, c, A, B), cur) n, k, A, B = map(int, input().split()) a = list(map(int, input().split())) for i in range(k): a[i] -= 1 print(rec(0, 2**n - 1, a, A, B))
FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR RETURN VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN 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 FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR
Thanos wants to destroy the avengers base, but he needs to destroy the avengers along with their base. Let we represent their base with an array, where each position can be occupied by many avengers, but one avenger can occupy only one position. Length of their base is a perfect power of $2$. Thanos wants to destroy the base using minimum power. He starts with the whole base and in one step he can do either of following: if the current length is at least $2$, divide the base into $2$ equal halves and destroy them separately, or burn the current base. If it contains no avenger in it, it takes $A$ amount of power, otherwise it takes his $B \cdot n_a \cdot l$ amount of power, where $n_a$ is the number of avengers and $l$ is the length of the current base. Output the minimum power needed by Thanos to destroy the avengers' base. -----Input----- The first line contains four integers $n$, $k$, $A$ and $B$ ($1 \leq n \leq 30$, $1 \leq k \leq 10^5$, $1 \leq A,B \leq 10^4$), where $2^n$ is the length of the base, $k$ is the number of avengers and $A$ and $B$ are the constants explained in the question. The second line contains $k$ integers $a_{1}, a_{2}, a_{3}, \ldots, a_{k}$ ($1 \leq a_{i} \leq 2^n$), where $a_{i}$ represents the position of avenger in the base. -----Output----- Output one integer — the minimum power needed to destroy the avengers base. -----Examples----- Input 2 2 1 2 1 3 Output 6 Input 3 2 1 2 1 7 Output 8 -----Note----- Consider the first example. One option for Thanos is to burn the whole base $1-4$ with power $2 \cdot 2 \cdot 4 = 16$. Otherwise he can divide the base into two parts $1-2$ and $3-4$. For base $1-2$, he can either burn it with power $2 \cdot 1 \cdot 2 = 4$ or divide it into $2$ parts $1-1$ and $2-2$. For base $1-1$, he can burn it with power $2 \cdot 1 \cdot 1 = 2$. For $2-2$, he can destroy it with power $1$, as there are no avengers. So, the total power for destroying $1-2$ is $2 + 1 = 3$, which is less than $4$. Similarly, he needs $3$ power to destroy $3-4$. The total minimum power needed is $6$.
n, k, A, B = map(int, input().split()) a = list(map(int, input().split())) def bs(l, r, val, a): l1 = l r1 = r res = -1 while l1 <= r1: mid = (l1 + r1) // 2 if a[mid] <= val: res = mid l1 = mid + 1 else: r1 = mid - 1 return res def bsfl(l, r, val, a): l1 = l r1 = r res = -1 while l1 <= r1: mid = (l1 + r1) // 2 if a[mid] < val: l1 = mid + 1 elif a[mid] >= val: res = mid r1 = mid - 1 return res def split(left, right, a): l1 = bsfl(0, k - 1, left, a) if a[l1] > right: l1 = -1 r1 = bs(0, k - 1, right, a) if a[r1] < left: r1 = -1 if r1 == -1 or l1 == -1: return A if left == right: res = B * (right - left + 1) * (r1 - l1 + 1) return res res = min( B * (right - left + 1) * (r1 - l1 + 1), split(left, (right + left) // 2, a) + split((right + left) // 2 + 1, right, a), ) return res a.sort() print(split(1, 2**n, a))
ASSIGN 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 FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR RETURN VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR VAR
Thanos wants to destroy the avengers base, but he needs to destroy the avengers along with their base. Let we represent their base with an array, where each position can be occupied by many avengers, but one avenger can occupy only one position. Length of their base is a perfect power of $2$. Thanos wants to destroy the base using minimum power. He starts with the whole base and in one step he can do either of following: if the current length is at least $2$, divide the base into $2$ equal halves and destroy them separately, or burn the current base. If it contains no avenger in it, it takes $A$ amount of power, otherwise it takes his $B \cdot n_a \cdot l$ amount of power, where $n_a$ is the number of avengers and $l$ is the length of the current base. Output the minimum power needed by Thanos to destroy the avengers' base. -----Input----- The first line contains four integers $n$, $k$, $A$ and $B$ ($1 \leq n \leq 30$, $1 \leq k \leq 10^5$, $1 \leq A,B \leq 10^4$), where $2^n$ is the length of the base, $k$ is the number of avengers and $A$ and $B$ are the constants explained in the question. The second line contains $k$ integers $a_{1}, a_{2}, a_{3}, \ldots, a_{k}$ ($1 \leq a_{i} \leq 2^n$), where $a_{i}$ represents the position of avenger in the base. -----Output----- Output one integer — the minimum power needed to destroy the avengers base. -----Examples----- Input 2 2 1 2 1 3 Output 6 Input 3 2 1 2 1 7 Output 8 -----Note----- Consider the first example. One option for Thanos is to burn the whole base $1-4$ with power $2 \cdot 2 \cdot 4 = 16$. Otherwise he can divide the base into two parts $1-2$ and $3-4$. For base $1-2$, he can either burn it with power $2 \cdot 1 \cdot 2 = 4$ or divide it into $2$ parts $1-1$ and $2-2$. For base $1-1$, he can burn it with power $2 \cdot 1 \cdot 1 = 2$. For $2-2$, he can destroy it with power $1$, as there are no avengers. So, the total power for destroying $1-2$ is $2 + 1 = 3$, which is less than $4$. Similarly, he needs $3$ power to destroy $3-4$. The total minimum power needed is $6$.
n, k, A, B = [int(e) for e in input().split()] power = tuple([int(e) for e in input().split()]) memo = {} def solve(n, k): if n == 0 and len(k) == 0: return A elif n == 0: return B * len(k) if len(k) != 0: opt1 = B * len(k) * (1 << n) else: opt1 = A ref = 1 << n - 1 kl = tuple([e for e in k if e <= ref]) kr = tuple([(e - ref) for e in k if e > ref]) if (n, kl) in memo: l = memo[n, kl] else: tmp = solve(n - 1, kl) memo[n, kl] = tmp l = tmp if (n, kr) in memo: r = memo[n, kr] else: tmp = solve(n - 1, kr) memo[n, kr] = tmp r = tmp opt2 = l + r return min(opt1, opt2) print(solve(n, power))
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FUNC_DEF IF VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN VAR IF VAR NUMBER RETURN BIN_OP VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Thanos wants to destroy the avengers base, but he needs to destroy the avengers along with their base. Let we represent their base with an array, where each position can be occupied by many avengers, but one avenger can occupy only one position. Length of their base is a perfect power of $2$. Thanos wants to destroy the base using minimum power. He starts with the whole base and in one step he can do either of following: if the current length is at least $2$, divide the base into $2$ equal halves and destroy them separately, or burn the current base. If it contains no avenger in it, it takes $A$ amount of power, otherwise it takes his $B \cdot n_a \cdot l$ amount of power, where $n_a$ is the number of avengers and $l$ is the length of the current base. Output the minimum power needed by Thanos to destroy the avengers' base. -----Input----- The first line contains four integers $n$, $k$, $A$ and $B$ ($1 \leq n \leq 30$, $1 \leq k \leq 10^5$, $1 \leq A,B \leq 10^4$), where $2^n$ is the length of the base, $k$ is the number of avengers and $A$ and $B$ are the constants explained in the question. The second line contains $k$ integers $a_{1}, a_{2}, a_{3}, \ldots, a_{k}$ ($1 \leq a_{i} \leq 2^n$), where $a_{i}$ represents the position of avenger in the base. -----Output----- Output one integer — the minimum power needed to destroy the avengers base. -----Examples----- Input 2 2 1 2 1 3 Output 6 Input 3 2 1 2 1 7 Output 8 -----Note----- Consider the first example. One option for Thanos is to burn the whole base $1-4$ with power $2 \cdot 2 \cdot 4 = 16$. Otherwise he can divide the base into two parts $1-2$ and $3-4$. For base $1-2$, he can either burn it with power $2 \cdot 1 \cdot 2 = 4$ or divide it into $2$ parts $1-1$ and $2-2$. For base $1-1$, he can burn it with power $2 \cdot 1 \cdot 1 = 2$. For $2-2$, he can destroy it with power $1$, as there are no avengers. So, the total power for destroying $1-2$ is $2 + 1 = 3$, which is less than $4$. Similarly, he needs $3$ power to destroy $3-4$. The total minimum power needed is $6$.
import sys LI = lambda: list(map(int, sys.stdin.readline().split())) MI = lambda: map(int, sys.stdin.readline().split()) SI = lambda: sys.stdin.readline().strip("\n") II = lambda: int(sys.stdin.readline()) def fnd1(s, e): global arr, k l, r = 0, k - 1 ret = -1 while l <= r: m = (l + r) // 2 if arr[m] >= s: r = m - 1 ret = m else: l = m + 1 return ret if arr[ret] <= e else -1 def fnd2(s, e): global arr, k l, r = 0, k - 1 ret = -1 while l <= r: m = (l + r) // 2 if arr[m] <= e: l = m + 1 ret = m else: r = m - 1 return ret if arr[ret] >= s else -1 def val(s, e): global arr, k, a, b if s == 0 or e == 0: return 0 if s == e: i = fnd1(s, e) if i == -1: return a else: j = fnd2(s, e) return b * (j - i + 1) else: m = (s + e) // 2 i = fnd1(s, e) if i == -1: return a else: j = fnd2(s, e) return min(b * (j - i + 1) * (e - s + 1), val(s, m) + val(m + 1, e)) n, k, a, b = MI() arr = sorted(LI()) print(val(1, 2**n))
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR VAR NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR
Thanos wants to destroy the avengers base, but he needs to destroy the avengers along with their base. Let we represent their base with an array, where each position can be occupied by many avengers, but one avenger can occupy only one position. Length of their base is a perfect power of $2$. Thanos wants to destroy the base using minimum power. He starts with the whole base and in one step he can do either of following: if the current length is at least $2$, divide the base into $2$ equal halves and destroy them separately, or burn the current base. If it contains no avenger in it, it takes $A$ amount of power, otherwise it takes his $B \cdot n_a \cdot l$ amount of power, where $n_a$ is the number of avengers and $l$ is the length of the current base. Output the minimum power needed by Thanos to destroy the avengers' base. -----Input----- The first line contains four integers $n$, $k$, $A$ and $B$ ($1 \leq n \leq 30$, $1 \leq k \leq 10^5$, $1 \leq A,B \leq 10^4$), where $2^n$ is the length of the base, $k$ is the number of avengers and $A$ and $B$ are the constants explained in the question. The second line contains $k$ integers $a_{1}, a_{2}, a_{3}, \ldots, a_{k}$ ($1 \leq a_{i} \leq 2^n$), where $a_{i}$ represents the position of avenger in the base. -----Output----- Output one integer — the minimum power needed to destroy the avengers base. -----Examples----- Input 2 2 1 2 1 3 Output 6 Input 3 2 1 2 1 7 Output 8 -----Note----- Consider the first example. One option for Thanos is to burn the whole base $1-4$ with power $2 \cdot 2 \cdot 4 = 16$. Otherwise he can divide the base into two parts $1-2$ and $3-4$. For base $1-2$, he can either burn it with power $2 \cdot 1 \cdot 2 = 4$ or divide it into $2$ parts $1-1$ and $2-2$. For base $1-1$, he can burn it with power $2 \cdot 1 \cdot 1 = 2$. For $2-2$, he can destroy it with power $1$, as there are no avengers. So, the total power for destroying $1-2$ is $2 + 1 = 3$, which is less than $4$. Similarly, he needs $3$ power to destroy $3-4$. The total minimum power needed is $6$.
n, k, A, B = map(int, input().split()) f = list(sorted(list(map(int, input().split())))) def min_cost(l, r, full): if len(full) == 0: return A elif l == r: return B * len(full) else: mid = (l + r) // 2 left = [] right = [] for i in full: if i <= mid: left.append(i) else: right.append(i) return min( len(full) * (r - l + 1) * B, min_cost(l, mid, left) + min_cost(mid + 1, r, right), ) print(min_cost(1, 2**n, f))
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR IF VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR VAR
Thanos wants to destroy the avengers base, but he needs to destroy the avengers along with their base. Let we represent their base with an array, where each position can be occupied by many avengers, but one avenger can occupy only one position. Length of their base is a perfect power of $2$. Thanos wants to destroy the base using minimum power. He starts with the whole base and in one step he can do either of following: if the current length is at least $2$, divide the base into $2$ equal halves and destroy them separately, or burn the current base. If it contains no avenger in it, it takes $A$ amount of power, otherwise it takes his $B \cdot n_a \cdot l$ amount of power, where $n_a$ is the number of avengers and $l$ is the length of the current base. Output the minimum power needed by Thanos to destroy the avengers' base. -----Input----- The first line contains four integers $n$, $k$, $A$ and $B$ ($1 \leq n \leq 30$, $1 \leq k \leq 10^5$, $1 \leq A,B \leq 10^4$), where $2^n$ is the length of the base, $k$ is the number of avengers and $A$ and $B$ are the constants explained in the question. The second line contains $k$ integers $a_{1}, a_{2}, a_{3}, \ldots, a_{k}$ ($1 \leq a_{i} \leq 2^n$), where $a_{i}$ represents the position of avenger in the base. -----Output----- Output one integer — the minimum power needed to destroy the avengers base. -----Examples----- Input 2 2 1 2 1 3 Output 6 Input 3 2 1 2 1 7 Output 8 -----Note----- Consider the first example. One option for Thanos is to burn the whole base $1-4$ with power $2 \cdot 2 \cdot 4 = 16$. Otherwise he can divide the base into two parts $1-2$ and $3-4$. For base $1-2$, he can either burn it with power $2 \cdot 1 \cdot 2 = 4$ or divide it into $2$ parts $1-1$ and $2-2$. For base $1-1$, he can burn it with power $2 \cdot 1 \cdot 1 = 2$. For $2-2$, he can destroy it with power $1$, as there are no avengers. So, the total power for destroying $1-2$ is $2 + 1 = 3$, which is less than $4$. Similarly, he needs $3$ power to destroy $3-4$. The total minimum power needed is $6$.
def main(): n, k, a, b = map(int, input().split()) pos = list(map(int, input().split())) pos.sort() def search(interval_left, interval_right, index_left, index_right): if interval_left == interval_right: if index_left > index_right: return a else: return b * (index_right - index_left + 1) if index_left > index_right: return a interval_mid = interval_left + (interval_right - interval_left) // 2 left, right = index_left, index_right index_mid = left - 1 while left <= right: mid = (left + right) // 2 if pos[mid] <= interval_mid: index_mid = mid left = left + 1 else: right = right - 1 t0 = b * (index_right - index_left + 1) * (interval_right - interval_left + 1) t1 = search(interval_left, interval_mid, index_left, index_mid) t2 = search(interval_mid + 1, interval_right, index_mid + 1, index_right) ans = min(t0, t1 + t2) return min(t0, t1 + t2) ret = search(1, 1 << n, 0, k - 1) print(ret) main()
FUNC_DEF ASSIGN 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 FUNC_DEF IF VAR VAR IF VAR VAR RETURN VAR RETURN BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR RETURN VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR