description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Phoenix is picking berries in his backyard. There are $n$ shrubs, and each shrub has $a_i$ red berries and $b_i$ blue berries. Each basket can contain $k$ berries. But, Phoenix has decided that each basket may only contain berries from the same shrub or berries of the same color (red or blue). In other words, all berries in a basket must be from the same shrub or/and have the same color. For example, if there are two shrubs with $5$ red and $2$ blue berries in the first shrub and $2$ red and $1$ blue berries in the second shrub then Phoenix can fill $2$ baskets of capacity $4$ completely: the first basket will contain $3$ red and $1$ blue berries from the first shrub; the second basket will contain the $2$ remaining red berries from the first shrub and $2$ red berries from the second shrub. Help Phoenix determine the maximum number of baskets he can fill completely! -----Input----- The first line contains two integers $n$ and $k$ ($ 1\le n, k \le 500$)Β β€” the number of shrubs and the basket capacity, respectively. The $i$-th of the next $n$ lines contain two integers $a_i$ and $b_i$ ($0 \le a_i, b_i \le 10^9$)Β β€” the number of red and blue berries in the $i$-th shrub, respectively. -----Output----- Output one integerΒ β€” the maximum number of baskets that Phoenix can fill completely. -----Examples----- Input 2 4 5 2 2 1 Output 2 Input 1 5 2 3 Output 1 Input 2 5 2 1 1 3 Output 0 Input 1 2 1000000000 1 Output 500000000 -----Note----- The first example is described above. In the second example, Phoenix can fill one basket fully using all the berries from the first (and only) shrub. In the third example, Phoenix cannot fill any basket completely because there are less than $5$ berries in each shrub, less than $5$ total red berries, and less than $5$ total blue berries. In the fourth example, Phoenix can put all the red berries into baskets, leaving an extra blue berry behind.
import sys input = sys.stdin.readline n, k = map(int, input().split()) a = [None] b = [None] for _ in range(n): x, y = map(int, input().split()) a.append(x) b.append(y) dp = [([None] * 505) for _ in range(505)] totA = sum(a[1:]) totB = sum(b[1:]) dp[0][0] = True for i in range(1, n + 1): for j in range(k): dp[i][j] = dp[i - 1][(j - a[i] % k + k) % k] for l in range(min(k - 1, a[i]) + 1): if (a[i] - l) % k + b[i] >= k: dp[i][j] = dp[i][j] or dp[i - 1][(j - l + k) % k] ans = 0 for i in range(k): if dp[n][i]: ans = max(ans, (totA + totB - i) // k) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NONE ASSIGN VAR LIST NONE FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NONE NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Phoenix is picking berries in his backyard. There are $n$ shrubs, and each shrub has $a_i$ red berries and $b_i$ blue berries. Each basket can contain $k$ berries. But, Phoenix has decided that each basket may only contain berries from the same shrub or berries of the same color (red or blue). In other words, all berries in a basket must be from the same shrub or/and have the same color. For example, if there are two shrubs with $5$ red and $2$ blue berries in the first shrub and $2$ red and $1$ blue berries in the second shrub then Phoenix can fill $2$ baskets of capacity $4$ completely: the first basket will contain $3$ red and $1$ blue berries from the first shrub; the second basket will contain the $2$ remaining red berries from the first shrub and $2$ red berries from the second shrub. Help Phoenix determine the maximum number of baskets he can fill completely! -----Input----- The first line contains two integers $n$ and $k$ ($ 1\le n, k \le 500$)Β β€” the number of shrubs and the basket capacity, respectively. The $i$-th of the next $n$ lines contain two integers $a_i$ and $b_i$ ($0 \le a_i, b_i \le 10^9$)Β β€” the number of red and blue berries in the $i$-th shrub, respectively. -----Output----- Output one integerΒ β€” the maximum number of baskets that Phoenix can fill completely. -----Examples----- Input 2 4 5 2 2 1 Output 2 Input 1 5 2 3 Output 1 Input 2 5 2 1 1 3 Output 0 Input 1 2 1000000000 1 Output 500000000 -----Note----- The first example is described above. In the second example, Phoenix can fill one basket fully using all the berries from the first (and only) shrub. In the third example, Phoenix cannot fill any basket completely because there are less than $5$ berries in each shrub, less than $5$ total red berries, and less than $5$ total blue berries. In the fourth example, Phoenix can put all the red berries into baskets, leaving an extra blue berry behind.
n, k = map(int, input().split()) sumr = 0 sumb = 0 sumtot = 0 possr = [0] * k possr[0] = 1 for i in range(n): a, b = map(int, input().split()) sumr += a sumb += b sumtot += a sumtot += b tot = a + b poss2 = [0] * k for j in range(k): rest = a - j rest %= k if (rest + b >= k or rest == 0) and j <= a: for l in range(k): if possr[l] == 1: poss2[(l + j) % k] = 1 for j in range(k): possr[j] = poss2[j] sol1 = sumtot // k sol2 = sumr // k + sumb // k if sol1 == sol2: print(sol1) else: i = 0 while possr[i] == 0: i += 1 sumr %= k sumb %= k sumr -= i sumb += sumr if sumb >= k: print(sol1) else: print(sol2)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
import sys def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [([c] * b) for i in range(a)] def list3d(a, b, c, d): return [[([d] * c) for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[([e] * d) for j in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)] def Yes(): print("Yes") def No(): print("No") def YES(): print("YES") def NO(): print("NO") INF = 10**19 MOD = 10**9 + 7 def digit_sum(S): res = 0 for s in S: res += int(s) return res MAX = 150 ans = list2d(MAX + 1, 10, -1) last = list2d(10, 10, 0) for i in range(10): for j in range(i, i + 10): cnt = 0 for k in range(i, j + 1): k %= 10 cnt += k last[i][j % 10] = cnt for i in range(MAX + 1): s = "" tmp = i while tmp - 9 >= 0: s += "9" tmp -= 9 if tmp: s += str(tmp) s = s[::-1] for j in range(10): cnt = i * (j + 1) if cnt > MAX: break for k in range(10): if k + j >= 10: break res = cnt + last[k][k + j] if res <= MAX: if ans[res][j] == -1 or ans[res][j] > int(s + str(k)): ans[res][j] = int(s + str(k)) for i in range(MAX + 1): x = 0 while x * 9 <= i: tmp = i s1 = "9" * x tmp -= x * 9 if tmp - 8 >= 0: s1 += "8" tmp -= 8 while tmp - 9 >= 0: s1 += "9" tmp -= 9 if tmp: s1 += str(tmp) if not s1: s1 = "0" s1 = s1[::-1] s2 = str(int(s1) + 1) dsm1 = digit_sum(s1) dsm2 = digit_sum(s2) for j in range(1, 10): for a in range(1, j + 1): b = j + 1 - a cnt = dsm1 * a + dsm2 * b if cnt > MAX: break k = 10 - a if k + j < 10: continue res = cnt + last[k][(k + j) % 10] if res <= MAX: if ans[res][j] == -1 or ans[res][j] > int(s1 + str(k)): ans[res][j] = int(s1 + str(k)) x += 1 for _ in range(INT()): N, K = MAP() print(ans[N][K])
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF NONE RETURN VAR NONE FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR STRING ASSIGN VAR VAR WHILE BIN_OP VAR NUMBER NUMBER VAR STRING VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP STRING VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR STRING VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR STRING VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
for ss in range(int(input())): n, k = input().strip().split() n = int(n) k = int(k) list = [] ilist = [] klaar = True for i in range(10): m = 0 for t in range(k + 1): s = i + t if s >= 10: s = s - 9 m = m + s if i == 0 and m > n: print(-1) klaar = False break if (n - m) % (k + 1) == 0 and n > m: list.append(m) ilist.append(i) if n == m: print(i) klaar = False break if not klaar: break if klaar: if not list: print(-1) continue else: i = ilist[list.index(max(list))] if i + k < 10 or i + k >= 10 and int((n - max(list)) / (k + 1)) < 9: lom = int((n - max(list)) / (k + 1)) print(int(str(lom % 9) + lom // 9 * "9" + str(i))) else: lom = int((n - max(list)) / (k + 1) - 8) print(int(str(lom % 9) + lom // 9 * "9" + "8" + str(i)))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR IF VAR IF VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER STRING FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER STRING STRING FUNC_CALL VAR VAR
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
def sum_digit(x): s = 0 while x > 0: s += x % 10 x //= 10 return s def get_min(x): num9 = x // 9 r = x % 9 return (r + 1) * 10**num9 - 1 S = [[(0) for _ in range(10)] for _ in range(100)] dp = [[float("inf") for _ in range(10)] for _ in range(2000)] mid = [(10**i - 1) for i in range(17)] for l, x in enumerate(mid): for i in range(10): for j in range(10): if x == 0: base = i * 10 + j else: base = (i * 10**l + x) * 10 + j s = 0 for num in range(10): s += sum_digit(base + num) if base < dp[s][num]: dp[s][num] = base if x == 0: S[base][num] = s for s in range(151): for s2 in range(90): for num in range(10): remain = s - S[s2][num] if remain < 0: continue if remain % (num + 1) == 0: min_ = get_min(remain // (num + 1)) if min_ * 100 + s2 < dp[s][num]: dp[s][num] = min_ * 100 + s2 for _ in range(int(input())): n, k = map(int, input().split()) if dp[n][k] < float("inf"): print(dp[n][k]) else: print(-1)
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
def generate_prefix(su): pre = "" if su >= 8: pre += "8" su -= 8 if len(pre) > 0: nu = 9 else: nu = 8 while su > 0: while su >= nu: su -= nu pre += str(nu) nu -= 1 pre = pre[::-1] return pre def get_number(su, nines, d, k): last_digit_sum = 0 with_zeros = 0 with_nines = 0 for i in range(k + 1): if d + i < 10: last_digit_sum += d + i with_nines += 1 else: last_digit_sum += (d + i) % 10 with_zeros += 1 su -= with_zeros + last_digit_sum + nines * 9 * with_nines if su < 0 or su % (k + 1) != 0: return "" su //= k + 1 prefix = generate_prefix(su) if prefix == "0": prefix = "" ret = prefix + "9" * nines + str(d) return ret for _ in range(int(input())): su, k = map(int, input().split()) fans = 1e20 for d in range(10): for nines in range(0, 64): this_ans = get_number(su, nines, d, k) if this_ans == "": continue elif int(this_ans) < int(fans): fans = this_ans if fans == 1e20: print(-1) else: print(fans)
FUNC_DEF ASSIGN VAR STRING IF VAR NUMBER VAR STRING VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER WHILE VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN STRING VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR STRING ASSIGN VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP STRING VAR FUNC_CALL VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR STRING IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
t = int(input()) buf = [] INF = 10**18 for _ in range(t): n, k = list(map(int, input().split())) base = n - k * (k + 1) // 2 ans = INF l = k + 1 if base >= 0 and base % l == 0: ini = base // l if ini <= 9 - k: ans = min(ans, ini) else: tmp = ini - (9 - k) tmps = str(9 - k) while tmp: d = min(9, tmp) tmp -= d tmps += str(d) ans = min(ans, int(tmps[::-1])) for d in range(1, 16): for i in range(1, l): new_base = base + d * 9 * i if new_base < 0 or new_base % l != 0: continue ini = new_base // l tmp = ini - 9 * (d - 1) - (10 - (l - i)) tmps = str(10 - (l - i)) + "9" * (d - 1) if tmp < 0: continue if tmp <= 8: tmps += str(tmp) ans = min(ans, int(tmps[::-1])) continue tmp -= 8 tmps += "8" while tmp: d = min(9, tmp) tmp -= d tmps += str(d) ans = min(ans, int(tmps[::-1])) if ans == INF: ans = -1 buf.append(ans) print("\n".join(map(str, buf)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR VAR BIN_OP STRING BIN_OP VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR STRING WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
def get(sum): if sum < 9: return sum sum -= 8 ret = sum % 9 sum = sum - sum % 9 while sum > 0: ret = ret * 10 + 9 sum -= 9 return ret * 10 + 8 t = int(input()) while t > 0: t -= 1 n, k = [int(i) for i in input().split()] ok = 0 ans = 0 for dig in range(10): for i in range(20): d = dig already = 0 cur = d + 9 * i for j in range(k + 1): already += cur d += 1 if d <= 9: cur += 1 else: d = 0 cur = cur - 9 * (i + 1) + 1 rem = n - already d = dig if rem >= 0 and rem % (k + 1) == 0: req = rem // (k + 1) temp = get(req) for z in range(i): temp = temp * 10 + 9 temp = temp * 10 + d if ok == 0: ok = 1 ans = temp else: ans = min(ans, temp) if ok == 1: print(ans) else: print(-1)
FUNC_DEF IF VAR NUMBER RETURN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
def f(x): ret = 0 while x: ret += x % 10 x //= 10 return ret def d(x, k): ret = 0 for i in range(k + 1): ret += f(x + i) return ret t = int(input()) while t: t -= 1 n, k = map(int, input().split()) fl = False s = 0 ans = "-1" for j in range(30): s = 0 for f3 in range(j): if d(s, k) > n: continue s *= 10 s += 9 if d(s, k) > n: continue for i in range(10): h = s s *= 10 s += i if d(s, k) > n: s = h continue jj = d(s, k) if jj == n or jj <= n and not (n - jj) % (k + 1): ans1 = str(s) if jj != n: yy = (n - jj) // (k + 1) ggg = "" x = 0 while yy: if yy > 9: if not x: ggg = "8" + ggg yy -= 8 else: ggg = "9" + ggg yy -= 9 else: uu = yy if not x: uu = min(uu, 8) ggg = chr(uu + ord("0")) + ggg yy -= uu x += 1 if d(int(ggg + ans1), k) == n: ans1 = ggg + ans1 else: ans1 = ggg + "0" + ans1 if ans == "-1" or int(ans) > int(ans1): ans = ans1 s = h print(ans)
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR IF VAR NUMBER IF VAR ASSIGN VAR BIN_OP STRING VAR VAR NUMBER ASSIGN VAR BIN_OP STRING VAR VAR NUMBER ASSIGN VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR STRING VAR IF VAR STRING FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
def sum_digits(number): return sum(int(digit) for digit in str(number)) nb_cases = int(input()) for case in range(nb_cases): n, k = [int(x) for x in input().split()] dp = ["-1"] * 151 dp[int(k * (k + 1) / 2)] = "0" for n1 in range(int(dp[1])): dp[n1] = "-1" for x in range(1, 10): n1 = 0 for y in range(k + 1): n1 += sum_digits(x + y) if n1 > n or int(dp[n1]) >= 0: continue else: dp[n1] = str(x) if n == 1 and k >= 2: dp[1] = -1 for n1 in range(10, n + 1): if dp[n1] != "-1": continue if n1 - (k + 1) < 0 or dp[n1 - (k + 1)] == "-1": continue nb = dp[n1 - (k + 1)] if int(nb[len(nb) - 1]) == 9 - k: if int(nb[0]) < 9 and len(nb) > 1: dp[n1] = str(int(nb[0]) + 1) + dp[n1 - (k + 1)][1:] else: dp[n1] = "1" + nb elif int(nb[0]) < 9: if len(nb) == 2 and nb[0] == "8" or len(nb) == 1 and int(nb[0]) > 9 - k: dp[n1] = "1" + nb else: dp[n1] = str(int(nb[0]) + 1) + nb[1:] else: dp[n1] = "1" + nb print(dp[n]) n = 24 k = 3
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR 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 BIN_OP LIST STRING NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR STRING IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP NUMBER VAR IF FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP STRING VAR IF FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP STRING VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
import sys input = sys.stdin.readline t = int(input()) best = [[(-1) for x in range(10)] for y in range(151)] for pp in range(100000): total = 0 for kk in range(10): l = list(str(pp + kk)) l = [int(v) for v in l] total += sum(l) if total < 151 and best[total][kk] == -1: best[total][kk] = pp my_list = [] num = 99900 for abc in range(5, 20): for pr in range(9): num += 10**abc my_list.append(num) for rrr in my_list: for hh in range(200): pp = rrr + hh total = 0 for kk in range(10): l = list(str(pp + kk)) l = [int(v) for v in l] total += sum(l) if total < 151 and best[total][kk] == -1: best[total][kk] = pp for _ in range(t): a, b = list(map(int, input().split())) print(best[a][b])
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
def digitsum(n): ans = 0 while n > 0: ans += n % 10 n //= 10 return ans def construct_min_prefix(x): s = "" if x % 9 != 0: s += str(x % 9) s += "9" * (x // 9) return s def find_sum(x, k): done = 0 for i in range(k + 1): done += digitsum(int(x) + i) return done def find(pref, s, e, n, k): ans = 10**40 st = str(pref) + "9" * s + str(e) possible = find_sum(st, k) if possible > n: return ans if possible == n: ans = min(ans, int(st)) if possible < n: remaining = n - possible if remaining % (k + 1) == 0: num = construct_min_prefix(remaining // (k + 1)) + st ans = min(ans, int(num)) return ans for vishal in range(int(input())): ans = 10**40 n, k = list(map(int, input().split(" "))) for nines in range(100): for lastone in range(10): for aurek in range(10): if aurek + 9 * nines + lastone > n: continue ans = min(ans, find(aurek, nines, lastone, n, k)) if ans >= 10**40: print(-1) else: print(ans)
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR STRING IF BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP STRING BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP STRING VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
def digitsum(n): s = 0 while n: s += n % 10 n //= 10 return s for nitish in range(int(input())): ans = 10**30 str_arr = input().split(" ") arr = [int(num) for num in str_arr] n = arr[0] k = arr[1] for lastdigit in range(10): for numberofnine in range(100): if numberofnine * 9 + lastdigit > n: break s = "" for x in range(numberofnine): s += "9" s += str(lastdigit) xx = int(s) done = 0 for j in range(k + 1): done += digitsum(xx + j) done -= max(0, k + 1 - (10 - lastdigit)) if done > n: continue for aurek in range(9): p = min(k + 1, 10 - lastdigit) ddone = done + aurek * p + (aurek + 1) * (k + 1 - p) if ddone > n: continue if ddone == n: if aurek > 0: s = str(aurek) + s ans = min(ans, int(s)) else: ans = min(ans, int(s)) continue ddone = n - ddone if ddone % (k + 1) != 0: continue ddone //= k + 1 t = ddone % 9 ss = "" if t: ss += str(t) for jj in range(ddone // 9): ss += "9" if len(ss) > 0: ss += str(aurek) if len(ss) == 0: ans = min(ans, xx) else: ss += s ans = min(ans, int(ss)) if ans == 10**30: ans = -1 print(ans)
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR STRING VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER VAR IF VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR IF VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR STRING IF VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR STRING IF FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
from sys import stdin input = stdin.readline INF = 10**9 + 7 MAX = 10**7 + 7 MOD = 10**9 + 7 def buildDigit(crs): s = "9" * (crs // 9) s += str(crs % 9) s = list(s) s.sort() return "".join(s) for Ti in range(int(input().strip())): n, k = [int(x) for x in input().strip().split()] d = list(range(0, 10)) + list(range(1, 10)) ans = [float("inf"), float("inf")] for di in range(10): cds = 0 for ki in range(k + 1): cds += d[di + ki] crs = n - cds if crs >= 0 and crs % (k + 1) == 0: cr = buildDigit(crs // (k + 1)) cn = cr + str(di) if cr[-1] == "9" and di + k > 9: cn = cr[:-1] + "1" cn = [int(x) for x in cn] cn.sort() ncn = [cn[0]] for i in range(1, len(cn)): if ncn[-1] + cn[i] <= 9: ncn[-1] += cn[i] else: ncn.append(cn[i]) cn = "".join([str(x) for x in ncn]) + "8" + str(di) ans = min(ans, [len(cn), cn]) ans = ans[1] if ans[1] != float("inf") else -1 print(str(int(ans)))
ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP STRING BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL STRING VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR LIST FUNC_CALL VAR STRING FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR NUMBER STRING BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR STRING FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR LIST FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR STRING VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
from sys import stdin, stdout def cont(n): res = 0 if n < 8: return n n -= 8 res += n % 9 n -= n % 9 while n: res *= 10 res += min(n, 9) n -= min(n, 9) res = res * 10 + 8 return res t = 1 t = int(stdin.readline()) for _ in range(t): n, k = list(map(int, stdin.readline().split())) ans = 10**17 + 1 po = 1 for n9 in range(0, 17, 1): po *= 10 for d in range(0, 10, 1): a = min(10 - d, k + 1) s = n - n9 * 9 * a - (k + 1 - a) for i in range(k + 1): s -= (i + d) % 10 if s >= 0 and s % (k + 1) == 0: ans = min(ans, po * cont(s // (k + 1)) + (po - 10 + d)) if ans == 10**17 + 1: print(-1) else: print(ans)
FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER RETURN VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER WHILE VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR IF VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
def fun(x): if x < 9: return x ws = x // 9 res = x - 9 * ws for i in range(0, ws): res = res * 10 + 9 return res t = int(input()) for it in range(0, t): res = 0 sum = 0 ans = 4e18 n, k = map(int, input().split(" ")) for c in range(0, 10): if c + k < 10: sum = n - (2 * c + k) * (k + 1) // 2 if sum % (k + 1) == 0 and sum >= 0: res = fun(sum // (k + 1)) * 10 + c ans = min(res, ans) else: num = 10 - c sum = (c + 9) * num // 2 + (0 + k - num) * (k - num + 1) // 2 res = 0 for b in range(0, 18): sum += b * 9 * num if sum > n: continue for w in range(0, b): res = res * 10 + 9 res = res * 10 + c all = n - sum if all < k + 1: if all == k - num + 1: ans = min(ans, res) else: all -= k - num + 1 if all % (k + 1) == 0: x = all // (k + 1) if x < 9: a = x res = a * 10 ** (b + 1) + res else: a = fun(all // (k + 1) - 8) res = (a * 10 + 8) * 10 ** (b + 1) + res ans = min(res, ans) if ans != 4e18: print(ans) else: print("-1")
FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
tests = int(input()) INF = 10**20 for test in range(tests): n, w = map(int, input().split()) res = INF for k in range(17): for d in range(10): tmp = 0 for i in range(w + 1): if d + i <= 9: tmp += 9 * k + d + i else: tmp += 1 + (d + i) - 10 if n >= tmp and (n - tmp) % (w + 1) == 0: s = (n - tmp) // (w + 1) if s <= 8: prefix = str(s) else: prefix = str((s - 8) % 9) + "9" * ((s - 8) // 9) + "8" prefix += "9" * k prefix += str(d) x = int(prefix) if sum(sum(int(c) for c in str(x + i)) for i in range(w + 1)) == n: res = min(res, x) if res == INF: res = -1 print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP STRING BIN_OP BIN_OP VAR NUMBER NUMBER STRING VAR BIN_OP STRING VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
T = int(input()) while T > 0: n, k = map(int, input().split()) res = float("inf") for last in range(0, 10): if last + k <= 9: al = (last + last + k) * (k + 1) // 2 left = n - al if left >= 0 and left % (k + 1) == 0: x = left // (k + 1) temp = last index = 1 while x >= 10: temp += 9 * 10**index index += 1 x -= 9 temp += x * 10**index res = min(res, temp) else: number = 0 al = (last + 9) * (9 - last + 1) // 2 + (last + k - 8) * (k + last - 9) // 2 while True: already = 9 * number * (9 - last + 1) + al left = n - already if left < 0: break if left % (k + 1) == 0: index = 1 y = last for i in range(number): y += 9 * 10**index index += 1 x = left // (k + 1) if x > 8: x -= 8 y += 8 * 10**index index += 1 while x >= 10: y += 9 * 10**index index += 1 x -= 9 y += x * 10**index res = min(res, y) number += 1 if res == float("inf"): res = -1 print(res) T -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP NUMBER BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER WHILE NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR BIN_OP NUMBER BIN_OP NUMBER VAR VAR NUMBER WHILE VAR NUMBER VAR BIN_OP NUMBER BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
for _ in range(int(input())): n, k = tuple(map(int, input().split())) ans = float("inf") for count_9 in range(100): for last_number in range(10): first_sum = count_9 * 9 + last_number have_sum = first_sum * (k + 1) + k * (k + 1) // 2 if last_number + k >= 10: have_sum -= (count_9 + 1) * 9 * (last_number + k - 9) if have_sum > n: continue need = n - have_sum if need % (k + 1) != 0: continue if need == 0: ans = min(ans, int("9" * count_9 + str(last_number))) continue once = need // (k + 1) if once <= 8: first = str(once) else: need_9 = (once - 8) // 9 first = str(once - need_9 * 9 - 8) + "9" * need_9 + "8" ans = min(ans, int(first + "9" * count_9 + str(last_number))) if ans == float("inf"): ans = -1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP STRING VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP STRING VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP STRING VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
for _ in range(int(input())): n, k = map(int, input().split()) ans = float("inf") for i in range(10): SUM = 0 for j in range(i, i + k + 1): j %= 10 SUM += j a = min(9, i + k) - i + 1 b = k + 1 - a SUM += b nine = 0 while nine * 9 * a <= n - SUM: remain = n - SUM - nine * 9 * a if remain % (k + 1) == 0: remain //= k + 1 if remain <= 8: s = str(remain) else: remain -= 8 s = str(remain % 9) + "9" * (remain // 9) + "8" s += "9" * nine + str(i) s = int(s) ans = min(ans, s) nine += 1 if ans == float("inf"): ans = -1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP STRING BIN_OP VAR NUMBER STRING VAR BIN_OP BIN_OP STRING VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
def cal(a, b, k): base = 9 * a su = (k - b) * (k - b + 1) // 2 + (k - b + 1) while b: su += base base -= 1 b -= 1 return su for _ in range(int(input())): n, k = map(int, input().split()) st = "" if n < k * (k + 1) // 2: print(-1) continue if (n - k * (k + 1) // 2) % (k + 1) == 0: x = (n - k * (k + 1) // 2) // (k + 1) st += str(min(x, 9 - k)) x -= min(x, 9 - k) while x: st += str(min(9, x)) x -= min(9, x) st = st[::-1] for i in range(1, 19): for j in range(1, k + 1): ca = cal(i, j, k) if n < ca: continue if (n - ca) % (k + 1): continue t = str(10 - j) for ii in range(i - 1): t += "9" m = (n - ca) // (k + 1) if m: t += str(min(8, m)) m -= min(8, m) while m: t += str(min(9, m)) m -= min(9, m) t = t[::-1] if st: if int(t) < int(st): st = t else: st = t if st: print(st) else: print(-1)
FUNC_DEF ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING IF VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR WHILE VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR IF BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER VAR WHILE VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER IF VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
def num(l): sumi = 0 for i in l: sumi = sumi * 10 + i return sumi t = int(input()) for you in range(t): l = input().split() n = int(l[0]) k = int(l[1]) poss = 0 mina = [] for d in range(10): num1 = min(10 - d, k + 1) num2 = k + 1 - num1 for l in range(18): z = ( n - num1 * 9 * l - num2 * (num2 + 1) // 2 - num1 * (2 * d + num1 - 1) // 2 ) if z >= 0 and z % (k + 1) == 0: pref = z // (k + 1) lo = [] while pref > 0: if lo == []: if pref <= 8: lo.append(pref) pref = 0 else: lo.append(8) pref -= 8 elif pref <= 9: lo.append(pref) pref = 0 else: lo.append(9) pref -= 9 lo.reverse() for i in range(l): lo.append(9) lo.append(d) mina.append(num(lo)) if mina == []: print(-1) else: print(min(mina))
FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER IF VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR LIST WHILE VAR NUMBER IF VAR LIST IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR LIST EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
from sys import stdin input = stdin.buffer.readline inf = 10**100 def f(x): return sum(int(i) for i in str(x)) for _ in range(int(input())): n, k = map(int, input().split()) ans = inf for cnt in range(20): for lst in range(10): x = int("9" * cnt + str(lst)) s = 0 for i in range(k + 1): s += f(x + i) if n < s or (n - s) % (k + 1): continue d = (n - s) // (k + 1) if d < 8: y = str(d) else: y = "8" + "9" * ((d - 8) // 9) if (d - 8) % 9: y += str((d - 8) % 9) ans = min(ans, int(y[::-1] + str(x))) if ans == inf: print(-1) else: print(ans)
ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP STRING VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP STRING BIN_OP STRING BIN_OP BIN_OP VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
mat = [([0] * 10) for i in range(10)] def val(n): r = 0 while n: r, n = r + n % 10, n // 10 return r for k in range(10): for s in range(10): mat[s % 10][k] = sum([val(n) for n in range(s, s + k + 1)]) def get_k(k): return [m[k] for m in mat] def get_value(n, k, acc=0): n_n = n + 1 r = 0 while n: r, n = r + n % 10, n // 10 if k <= 0: return acc + r return get_value(n_n, k - 1, acc + r) t = int(input()) for _ in range(t): data = input().split(" ") n = int(data[0]) k = int(data[1]) results = [] for index, i in enumerate(get_k(k)): if i > n: continue rest = n - i rest = rest / (k + 1) nn = [] if rest - 8 >= 0 and k > 9 - index: nn.append(8) rest -= 8 while rest - 9 >= 0: nn.append(9) rest -= 9 x = int(str(int(rest)) + "".join([str(n_) for n_ in nn[::-1]]) + str(index)) val = get_value(int(x), k) if val == n: results.append(x) if len(results) == 0: print(-1) else: print(min(results))
ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_DEF RETURN VAR VAR VAR VAR FUNC_DEF NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER RETURN BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR LIST IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
m = [] for k in range(10): t = [] for i in range(10): a = 0 for j in range(k + 1): a += (i + j) % 10 + (i + j) // 10 t.append(a) m.append(t) for _ in range(int(input())): n, k = [int(x) for x in input().split()] l = m[k] ans = [] for i in range(10): if n - l[i] < 0 or (n - l[i]) % (k + 1): continue p = (n - l[i]) // (k + 1) if p // 9 > 0 and i + k > 9: a = int(str(p % 9 + 1) + "9" * (p // 9 - 1) + "8") elif p // 9 > 0: a = int(str(p % 9) + "9" * (p // 9)) else: a = p % 9 ans.append(10 * a + i) if len(ans) == 0: print(-1) continue print(min(ans))
ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP STRING BIN_OP BIN_OP VAR NUMBER NUMBER STRING IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP STRING BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
import sys def input(): return sys.stdin.readline().rstrip() def input_split(): return [int(i) for i in input().split()] def k_one(n): if n == 0: return 0 ans = "" lis = list(range(1, 10)) lis.reverse() for i in lis: if n == 0: return int(ans) div = n // i n = n % i ans = str(i) * div + ans return int(ans) testCases = int(input()) answers = [] def sum_digs(a): ans = 0 while a > 0: ans += a % 10 a = a // 10 return ans store = [ [[(-1) for case_k in range(11)] for last_dig in range(10)] for num_nines in range(17) ] for num_nines in range(17): for last_dig in range(10): for case_k in range(1, 11): s = 0 current = int("9" * num_nines + str(last_dig)) for i in range(case_k): s += sum_digs(current + i) store[num_nines][last_dig][case_k] = s - case_k * sum_digs(current) for _ in range(testCases): n, k = input_split() k = k + 1 if k == 1: ans = k_one(n) else: possibilities = [] for case_num_nines in range(17): for case_end_digit in range(10): temp = n - store[case_num_nines][case_end_digit][k] ending = int("9" * case_num_nines + str(case_end_digit)) if temp < 0: continue if temp % k != 0: continue else: fx = temp // k if fx < sum_digs(ending): continue else: pending = fx - sum_digs(ending) if pending < 9: ans = int( str(pending) + "9" * case_num_nines + str(case_end_digit) ) else: ans = int( str(k_one(pending - 8)) + str(8) + "9" * case_num_nines + str(case_end_digit) ) possibilities.append(ans) if len(possibilities) == 0: ans = -1 else: ans = min(possibilities) answers.append(ans) print(*answers, sep="\n")
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 NUMBER ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR FOR VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP STRING VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP STRING VAR FUNC_CALL VAR VAR IF VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP STRING VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP STRING VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
MAX = 150 INF = float("inf") def compute_suffix(digit_sum): digit_sum += 1 digit_count = digit_sum // 9 return 10**digit_count * (digit_sum % 9) + (10**digit_count - 1) - 1 def compute_prefix_sum(first_digit, prefix_len, num_count): pre_count = min(num_count, 10 - first_digit) result = ( 9 * prefix_len * pre_count + (2 * first_digit + pre_count - 1) * pre_count // 2 ) result += (num_count - pre_count) * (num_count - pre_count + 1) // 2 return result def solve(suffix_table, digit_sum, num_count): result = INF for first_digit in range(min(10, digit_sum + 1)): for prefix_len in range((digit_sum - first_digit) // 9 + 1): prefix_sum = compute_prefix_sum(first_digit, prefix_len, num_count) if prefix_sum > digit_sum or (digit_sum - prefix_sum) % num_count != 0: continue suffix = suffix_table[(digit_sum - prefix_sum) // num_count] candidate = ( suffix * 10 ** (prefix_len + 1) + (10**prefix_len - 1) * 10 + first_digit ) result = min(result, candidate) return result if result != INF else -1 suffix_table = [compute_suffix(n) for n in range(MAX + 1)] test_count = int(input()) for _ in range(test_count): digit_sum, num_count = map(int, input().split()) print(solve(suffix_table, digit_sum, num_count + 1))
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
for _ in range(int(input())): n, k = map(int, input().split()) ans = float("inf") for m in range(20): for l in range(10): if k + l <= 9: dsum = n - k * (k + 1) // 2 if dsum % (k + 1) == 0: f = dsum // (k + 1) f -= l if f >= 0: if 9 > f: test = str(f) * (f != 0) + str(l) ans = min(ans, int(test)) else: q = f // 9 r = f % 9 test = str(r) * (r != 0) + "9" * q + str(l) test = int(test) ans = min(test, ans) else: dsum = ( n - (k + l - 9) * (1 - 9 * m - l) - (9 - l) * (10 - l) // 2 - (k + l - 9) * (k + l - 10) // 2 ) if dsum % (k + 1) == 0: f = dsum // (k + 1) f -= 9 * m + l if f >= 0: if 9 > f: test = str(f) * (f != 0) + "9" * m + str(l) ans = min(ans, int(test)) else: f -= 8 q = f // 9 r = f % 9 test = str(r) * (r != 0) + "9" * q + "8" + "9" * m + str(l) test = int(test) ans = min(test, ans) if ans != float("inf"): print(ans) else: print(-1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER IF NUMBER VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP STRING VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP NUMBER BIN_OP NUMBER VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR VAR IF VAR NUMBER IF NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP STRING VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP STRING VAR STRING BIN_OP STRING VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
def find_smallest(fx, allow_nine_end): if fx < 0: return None if fx == 0: return "" a = [] if not allow_nine_end: d = min(8, fx) a.append(d) fx -= d while fx: d = min(9, fx) a.append(d) fx -= d return "".join(map(str, a[::-1])) t = int(input()) for _ in range(t): n, k = map(int, input().split()) m = 10**20 for end in range(0, 9 - k + 1): a = n - k * (k + 1) // 2 if a % (k + 1) != 0: continue fx = a // (k + 1) f = find_smallest(fx - end, True) if f is not None: m = min(m, int(str(f) + str(end))) for flips in range(1, 20): for count_after_flip in range(1, k + 1): a = n + 9 * flips * count_after_flip - k * (k + 1) // 2 if a % (k + 1) != 0: continue fx = a // (k + 1) end = count_after_flip - k - 1 + 10 nines_count = flips - 1 f = find_smallest(fx - end - 9 * nines_count, False) if f is not None: m = min(m, int(str(f) + "9" * nines_count + str(end))) if m != 10**20: print(m) else: print(-1)
FUNC_DEF IF VAR NUMBER RETURN NONE IF VAR NUMBER RETURN STRING ASSIGN VAR LIST IF VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL STRING FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP STRING VAR FUNC_CALL VAR VAR IF VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
for _ in range(int(input())): N, K = map(int, input().split()) ans = float("inf") for i in range(100 - K): val = 0 for j in range(i, i + K + 1): val += sum(list(map(int, list(str(j))))) if (N - val) % (K + 1) == 0 and N >= val: x = int((N - val) // (K + 1)) tail = str(x % 9) + str("9") * int(x // 9) if i < 10: ans = min(ans, int(tail + "0" + str(i))) else: ans = min(ans, int(tail + str(i))) print(-1) if ans == float("inf") else print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR STRING FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR STRING FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR VAR FUNC_CALL VAR STRING FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
from sys import exit, stderr, stdin def rl(): return [int(w) for w in stdin.readline().split()] def sumdigits(x): r = 0 while x > 0: r += x % 10 x //= 10 return r def f(x, k): return sum(sumdigits(y) for y in range(x, x + k + 1)) def search(n, k): for x0 in range(10): g = f(90 + x0, k) - f(x0, k) for x2 in range(10): f2 = f(x2 * 10 + x0, k) if n < f2: continue for n9 in range((n - f2) // g + 1): rn = n - f2 - n9 * g if rn % (k + 1) == 0: rn //= k + 1 x = x0 + (x2 + 1) * 10 ** (n9 + 1) - 10 xh = 10 ** (n9 + 2) while rn > 0: rr = min(rn, 9) x += xh * rr rn -= rr xh *= 10 yield x (T,) = rl() for _ in range(T): r = list(search(*rl())) if r: print(min(r)) else: print(-1)
FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
def func(n): res = 0 if n < 8: return n n -= 8 res += n % 9 n -= n % 9 while n > 0: res = res * 10 res += min(n, 9) n -= min(n, 9) res = res * 10 + 8 return res for _ in range(int(input())): n, k = list(map(int, input().split())) ans = 10**20 p = 1 for nine in range(0, 17): p = p * 10 for d in range(0, 10): a = min(10 - d, k + 1) f = n - 9 * nine * a - (k + 1 - a) for i in range(k + 1): f -= (d + i) % 10 if f >= 0 and f % (k + 1) == 0: f = f // (k + 1) f = func(f) f = p * f + p - 10 + d ans = min(ans, f) if ans == 10**20: print(-1) else: print(ans)
FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER RETURN VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
t = int(input()) for _ in range(t): n, k = map(int, input().split()) ans = float("INF") for w in range(17): for d in range(10): count = 0 for i in range(k + 1): if d + i <= 9: count += 9 * w + d + i else: count += 1 + (d + i) - 10 if n >= count and (n - count) % (k + 1) == 0: s = (n - count) // (k + 1) if s <= 8: c = str(s) else: c = str((s - 8) % 9) + "9" * ((s - 8) // 9) + "8" c += "9" * w c += str(d) x = int(c) ans = min(ans, x) print(-1 if ans == float("INF") else ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP STRING BIN_OP BIN_OP VAR NUMBER NUMBER STRING VAR BIN_OP STRING VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER VAR
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
def f(n): ans = 0 while n: ans += n % 10 n //= 10 return ans def get(n, k): ans = 0 for i in range(k + 1): ans += f(n + i) return ans t = int(input()) for tmp in range(t): w, k = map(int, input().split()) ans = -1 for last in range(10): c9 = 0 while last + c9 * 9 <= w: st = "9" * c9 + str(last) stx = int(st) h = get(stx, k) if h <= w and (w - h) % (k + 1) == 0: ost = (w - h) // (k + 1) pref = "" while ost > 0: if ost >= 9: if len(pref) == 0: pref += "8" ost -= 8 else: pref += "9" ost -= 9 else: pref += str(ost) break pref = pref[::-1] s = pref + "9" * c9 + str(last) x = int(s) if get(x, k) == w: if ans == -1 or x < ans: ans = x c9 += 1 print(ans)
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP STRING VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR STRING WHILE VAR NUMBER IF VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR STRING VAR NUMBER VAR STRING VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP STRING VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
def solve(s): ans, ten = 0, 1 while s > 0: val = min(9, s) ans += ten * val ten *= 10 s -= val return ans def digit_sum(val): s = 0 while val > 0: s += val % 10 val = int(val / 10) return s def f(k): return int(k * (k - 1) / 2) def xsolve(s, p): ans, ten = 0, 1 while s > p: ans += ten * 9 ten *= 10 s -= 9 if s < 0 or s + 1 != p: return None d = min(s, 8) s -= d p -= d + 1 ans += ten * d ten *= 10 while s > 0: d = min(s, 9) ans += ten * d ten *= 10 s -= d return ans def main(n, k): ans = None if n - f(k + 1) >= 0 and (n - f(k + 1)) % (k + 1) == 0: ex = 10 - (k + 1) val = (n - f(k + 1)) / (k + 1) used = False if ex < val: val -= ex used = True pos = solve(int(val)) if used: pos = 10 * pos + ex ans = pos for l in range(1, k + 1): for s in range(1, 151): left = n - s * l - f(l) - f(k + 1 - l) if left < 0: break elif left % (k + 1 - l) != 0 or s - (10 - l) < 0: continue p = int(left / (k + 1 - l)) pos = xsolve(s - (10 - l), p) if pos != None: pos = pos * 10 + (10 - l) if ans == None: ans = pos else: ans = min(ans, pos) if ans == None: ans = -1 print(int(ans)) t = int(input()) for _ in range(t): n, k = input().split() main(int(n), int(k))
FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR VAR VAR NUMBER VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NONE IF BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER IF BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR VAR IF VAR NONE ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
def sumDigits(x): if x == 0: return 0 return x % 10 + sumDigits(x // 10) def create(x): ans = "" while x >= 9: ans = "9" + ans x -= 9 ans = str(x) + ans return int(ans) def create_ex(x, y): track = -1 for i in range(9, x + 1, 9): r = x - i if r + 1 == y: track = i if track == -1: return -1 r = x - track ans = 0 if r < 9: ans = r else: ans = create(r - 8) * 10 + 8 ans *= 10 ** (track // 9) ans += create(track) return ans preResult = {} for i in range(100): s = 0 for k in range(10): s += sumDigits(i + k) if s <= 150 and (s, k) not in preResult: preResult[s, k] = i pre = [[(-1) for i in range(152)] for j in range(152)] for x in range(1, 151): pre[x][x] = create(x) if x != 150: if x < 9: pre[x][x + 1] = x else: pre[x][x + 1] = create(x - 8) * 10 + 8 for y in range(1, x): pre[x][y] = create_ex(x, y) def update(x, maybe): if x == -1: return maybe if x > maybe: return maybe return x def solve(): n, k = input().strip().split(" ") n = int(n) k = int(k) if (n, k) in preResult: return preResult[n, k] ans = -1 for st in range(10): rem = n flag = False p1 = 0 p2 = 0 for i in range(k + 1): flag = flag | (st + i > 9) rem -= (st + i) % 10 p1 += st + i <= 9 p2 += st + i > 9 if rem <= 0: continue if flag: for x in range(1, 151): if rem - x * p1 <= 0: break r = rem - x * p1 if r % p2 != 0: continue r //= p2 if pre[x][r] == -1: continue if x == r: continue ans = update(ans, pre[x][r] * 10 + st) else: if rem % (k + 1) != 0: continue r = rem // (k + 1) if pre[r][r] == -1: continue ans = update(ans, pre[r][r] * 10 + st) return ans t = int(input()) for iter in range(t): ans = solve() print(ans)
FUNC_DEF IF VAR NUMBER RETURN NUMBER RETURN BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR STRING WHILE VAR NUMBER ASSIGN VAR BIN_OP STRING VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN VAR IF VAR VAR RETURN VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
def get(summ): return str(summ % 9) + "9" * int(summ // 9) for t in range(0, int(input())): n, k = map(int, input().split()) result = -1 for last_digit in range(10): last_digit_sum = 0 for i in range(k + 1): last_digit_sum += (last_digit + i) % 10 if last_digit_sum > n: continue if last_digit + k >= 10: for cnt9 in range(15): sum9 = 9 * cnt9 * (10 - last_digit) if sum9 > n - last_digit_sum: break for first_digt in range(9): first_digit_sum = (10 - last_digit) * first_digt + ( k - 9 + last_digit ) * (first_digt + 1) f = first_digit_sum + sum9 + last_digit_sum if n >= f: remain = n - f if remain % (k + 1) == 0: pres = int( get(remain // (k + 1)) + str(first_digt) + "9" * int(cnt9) + str(last_digit) ) if result == -1: result = pres else: result = min(result, pres) elif (n - last_digit_sum) % (k + 1) == 0: pres = int(get((n - last_digit_sum) // (k + 1)) + str(last_digit)) if result == -1: result = pres else: result = min(result, pres) print(result)
FUNC_DEF RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP STRING FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR IF BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR IF VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP STRING FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
from sys import stdin, stdout def get(s): return str(s % 9) + "9" * (s // 9) def sum_of_digits(n, k): k += 1 res = 10**100 for d in range(10): end = 0 for i in range(k): end += (d + i) % 10 if end > n: continue if d + k > 10: for cnt in range(12): s = 9 * cnt * (10 - d) if s > n - end: break for nd in range(9): ns = s + (10 - d) * nd + (k - (10 - d)) * (nd + 1) if ns > n - end: break if (n - end - ns) % k == 0: res = min( res, int( get((n - end - ns) // k) + str(nd) + "9" * cnt + str(d) ), ) elif (n - end) % k == 0: res = min(res, int(get((n - end) // k) + str(d))) if res == 10**100: return -1 else: return res t = int(stdin.readline()) for i in range(t): n, k = map(int, stdin.readline().split()) stdout.write(str(sum_of_digits(n, k)) + "\n")
FUNC_DEF RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP STRING BIN_OP VAR NUMBER FUNC_DEF VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR IF BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR IF VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP STRING VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP NUMBER NUMBER RETURN NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR STRING
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
import sys def gen1(n): base = 10**n - 1 yield base for i in range(0, n): yield base - 10**i def gen2(n): for i in range(n): for x in gen1(i): for j in range(0, 10): yield j * 10**i + x def dsum(n): s = 0 while n > 0: s += n % 10 n //= 10 return s def dsuminv(n): h = n // 9 r = n % 9 + 1 return r * 10**h - 1 h = {} for x in gen2(15): for j in range(19): h[10 * x + j] = dsum(10 * x + j) kk = sorted(h.keys()) def solve(n, k): if n < k * (k + 1) // 2: return -1 if k == 0: return dsuminv(n) rs = sum(h[kk[i]] for i in range(k + 1)) for j in range(len(kk) - k - 1): if n < 20 and j > 200: break if rs == n and kk[j + k] == kk[j] + k: return kk[j] rs -= h[kk[j]] rs += h[kk[j + k + 1]] return -1 t = int(input().strip()) for _ in range(t): n, k = list(map(int, input().strip().split())) print(solve(n, k))
IMPORT FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR BIN_OP VAR BIN_OP NUMBER VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR RETURN VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
def read_list(): return list(map(int, input().strip().split(" "))) def print_list(l): print(" ".join(map(str, l))) INF = 10**18 N = int(input()) for _ in range(N): n, k = read_list() base = k * (k + 1) // 2 dlt = n - base res = INF if dlt >= 0 and dlt % (k + 1) == 0: tmp = [] now = dlt // (k + 1) tt = min(now, 9 - k) tmp.append(tt) now -= tt while now >= 9: now -= 9 tmp.append(9) tmp.append(now) tmp = int("".join(map(str, tmp[::-1]))) res = min(tmp, res) for n9 in range(1, n // 9 + 1): for t in range(k, 0, -1): if (dlt + n9 * t * 9) % (k + 1) == 0: now = (dlt + n9 * t * 9) // (k + 1) tmp = [] tt = 9 - k + t if now < (n9 - 1) * 9 + tt: continue tmp.append(tt) now -= tt for _ in range(max(0, n9 - 1)): tmp.append(9) now -= 9 if now > 8: tmp.append(8) now -= 8 while now >= 9: tmp.append(9) now -= 9 tmp.append(now) tmp = int("".join(map(str, tmp[::-1]))) res = min(tmp, res) if res == INF: print(-1) else: print(res)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR WHILE VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR IF VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
t = int(input()) while t > 0: n, k = map(int, input().split()) best = -1 for pos in range(0, 10): for kol in range(0, 20): sm = 0 for i in range(0, k + 1): if i + pos <= 9: sm += 9 * kol + pos + i else: sm += i + pos - 9 x = int(n - sm) if x % (k + 1) == 0 and x >= 0: s = x / (k + 1) s = int(s) if s <= 8: p = kol ans = s while p > 0: ans *= 10 ans += 9 p -= 1 ans *= 10 ans += pos if best == -1: best = ans else: best = min(best, ans) else: s -= 8 col = s // 9 s -= col * 9 ans = s p = kol while col > 0: ans *= 10 ans += 9 col -= 1 ans *= 10 ans += 8 while p > 0: ans *= 10 ans += 9 p -= 1 ans *= 10 ans += pos if best == -1: best = ans else: best = min(best, ans) print(best) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
for _ in range(int(input())): n, k = map(int, input().split()) ans = [] if n <= (18 - k) * (k + 1) // 2: for i in range(10): check = n for j in range(k + 1): if i + j > 9: break else: check -= i + j else: if check == 0: ans.append(i) else: check = n - (18 - k) * (k + 1) // 2 if check % (k + 1) == 0: check //= k + 1 i, j = check % 9, check // 9 if i == 0: ans.append(int("9" * j + f"{9 - k}")) else: ans.append(int(f"{i}" + "9" * j + f"{9 - k}")) for num9 in range(20): for j in range(k): check = n - (18 - j) * (j + 1) // 2 - (k - j) * (k - j - 1) // 2 check -= num9 * 9 * (j + 1) check -= 1 * (k - j) if check >= 0: if check % (k + 1) == 0: check //= k + 1 h, i = check % 9, check // 9 if i == 0: if h == 0: ans.append(int("9" * num9 + f"{9 - j}")) else: ans.append(int(f"{h}" + "9" * num9 + f"{9 - j}")) else: check -= 8 h, i = check % 9, check // 9 if h == 0: ans.append(int("9" * i + "8" + "9" * num9 + f"{9 - j}")) else: ans.append( int(f"{h}" + "9" * i + "8" + "9" * num9 + f"{9 - j}") ) if len(ans): print(min(ans)) else: print(-1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST IF VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP STRING VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP STRING VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR VAR IF VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP STRING VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP STRING VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP STRING VAR STRING BIN_OP STRING VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP STRING VAR STRING BIN_OP STRING VAR BIN_OP NUMBER VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
for i in range(int(input())): n, k = map(int, input().split()) ans = "a" * 100 for x in range(10): for d in range(n // 9 + 2): left = n for i in range(k + 1): left -= d * 9 + i + x if x + i > 9: left += (d + 1) * 9 if left < 0: break if left % (k + 1) != 0: continue left //= k + 1 temp = "9" * d + str(x) if left: add = min(left, 8) temp = str(add) + temp left -= add while left > 0: add = min(left, 9) temp = str(add) + temp left -= add if len(temp) < len(ans): ans = str(temp) elif len(temp) == len(ans) and temp < ans: ans = str(temp) if ans[0] == "a": ans = -1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP STRING NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
import sys input = sys.stdin.readline def suming(x, k): s = 0 for i in range(k + 1): xi = x + i while xi > 0: s += xi % 10 xi //= 10 return s for f in range(int(input())): n, k = map(int, input().split()) if k == 0: sol = 0 toad = 1 while n > 0: if n < 10: sol += toad * n n = 0 else: sol += toad * 9 n -= 9 toad *= 10 print(sol) else: mink = -1 mx = -1 mxk = -1 mn = 2 * n for i in range(10): foo = suming(i, k) if foo % (k + 1) == n % (k + 1) and mink == -1: mink = i if foo % (k + 1) == n % (k + 1): if foo > mx: mx = foo mxk = i mn = min(foo, mn) if mink == -1: print(-1) elif mn > n: print(-1) elif mx >= n: cont = True i = 0 while cont: foo = suming(i, k) if foo == n: print(i) cont = False i += 1 else: s = mxk rem = n - mx rem //= k + 1 if rem < 9: s += 10 * rem print(s) else: s += 80 toad = 100 rem -= 8 if mink + k < 10: s += 10 rem -= 1 while rem > 0: if rem < 10: s += toad * rem rem = 0 else: s += toad * 9 rem -= 9 toad *= 10 print(s)
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
def make_ans(count_9, last_num, start): if start <= 8: ans_str = str(start) + "9" * count_9 + str(last_num) else: start -= 8 count_first_9 = 0 while start > 9: count_first_9 += 1 start -= 9 ans_str = ( (str(start) if start != 0 else "") + "9" * count_first_9 + "8" + "9" * count_9 + str(last_num) ) return int(ans_str) def main(): n, k = map(int, input().split()) flag = False ans = 0 for count_9 in range(100): for last_num in range(10): s = 9 * count_9 * min(k + 1, 10 - last_num) if k + 1 > 10 - last_num: s += 1 * (k + 1 - (10 - last_num)) d_last_num = last_num for _ in range(k + 1): s += d_last_num d_last_num = (d_last_num + 1) % 10 ost = n - s if ost < 0: continue if ost % (k + 1) == 0: if flag: ans = min(make_ans(count_9, last_num, ost // (k + 1)), ans) else: ans = make_ans(count_9, last_num, ost // (k + 1)) flag = True if flag: print(ans) else: print("-1") t = int(input()) for _ in range(t): main()
FUNC_DEF IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP STRING VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR STRING BIN_OP STRING VAR STRING BIN_OP STRING VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR IF BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
def get(s): return str(s % 9) + "9" * (s // 9) for tc in range(int(input())): n, k = map(int, input().split()) k += 1 bst = 10**100 for d in range(10): ends = 0 for i in range(k): ends += (d + i) % 10 if ends > n: continue if d + k > 10: for cnt in range(12): s = 9 * cnt * (10 - d) if s > n - ends: break for nd in range(9): ns = s + (10 - d) * nd + (k - (10 - d)) * (nd + 1) if ns > n - ends: break if (n - ends - ns) % k == 0: bst = min( bst, int( get((n - ends - ns) // k) + str(nd) + "9" * cnt + str(d) ), ) elif (n - ends) % k == 0: bst = min(bst, int(get((n - ends) // k) + str(d))) print(-1 if bst == 10**100 else bst)
FUNC_DEF RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP STRING BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR IF BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR IF VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP STRING VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP NUMBER NUMBER NUMBER VAR
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
def digit_sum(n): tot = 0 while n: tot += n % 10 n //= 10 return tot def sum_f(n, k): tot = 0 for i in range(k + 1): tot += digit_sum(n + i) return tot t = int(input().split()[0]) for case in range(t): n, k = map(int, input().split()) ans = -1 for i in range(1000 - k): diff = n - sum_f(i, k) if diff == 0: ans = i break if diff > 0 and diff % (k + 1) == 0: rem = diff // (k + 1) prefix = 0 while rem >= 10: prefix = prefix * 10 + 9 rem -= 9 if rem > 0 and prefix == 0: prefix = rem elif rem > 0: prefix = int(str(rem) + str(prefix)) curr = prefix * 1000 + i if ans == -1: ans = curr elif curr < ans: ans = curr print(ans)
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
def func(x, d, n, k): ans = str(x) for i in range(d): ans = "9" + ans for i in range(k + 1): y = (x + i) % 10 if y >= x: n -= d * 9 + y else: n -= 1 + y if n < 0: return -1 if n % (k + 1) != 0: return -1 p = int(n / (k + 1)) if p >= 8: ans = "8" + ans n -= (k + 1) * 8 n = int(n / (k + 1)) v = int(n / 9) for i in range(v): ans = "9" + ans if n - 9 * v != 0: ans = str(n - 9 * v) + ans else: ans = str(p) + ans return int(ans) t = int(input()) while t: t -= 1 n, k = list(map(int, input().split())) ans = -1 for i in range(10): for j in range(18): res = func(i, j, n, k) if res != -1 and ans == -1: ans = res elif res != -1: ans = min(ans, res) print(ans)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP STRING VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP NUMBER VAR IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP STRING VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP STRING VAR IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
import sys def rs(): return sys.stdin.readline().rstrip() def ri(): return int(sys.stdin.readline()) def ria(): return list(map(int, sys.stdin.readline().split())) def ws(s): sys.stdout.write(s + "\n") def wi(n): sys.stdout.write(str(n) + "\n") def wia(a): sys.stdout.write(" ".join([str(x) for x in a]) + "\n") def solve(n, k): ans = 10**18 for last_digit in range(10): for nines_count in range(20): s = 0 for ki in range(k + 1): overflow = ki + last_digit >= 10 if overflow: s += 1 + (ki + last_digit) % 10 else: s += 9 * nines_count + last_digit + ki if s > n: continue left = n - s pre = [] if left >= 8 * (k + 1): pre.append(8) left -= 8 * (k + 1) for d in range(9, 0, -1): while left >= d * (k + 1): pre.append(d) left -= d * (k + 1) if left == 0: pre.reverse() x = int( "".join([str(d) for d in pre]) + "9" * nines_count + str(last_digit) ) ans = min(ans, x) return -1 if ans == 10**18 else ans def main(): for _ in range(ri()): n, k = ria() wi(solve(n, k)) main()
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL 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 EXPR FUNC_CALL VAR BIN_OP VAR STRING FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR STRING FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST IF VAR BIN_OP NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER WHILE VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR BIN_OP STRING VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR BIN_OP NUMBER NUMBER NUMBER VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
import sys input = sys.stdin.readline I = lambda: list(map(int, input().split())) (t,) = I() for _ in range(t): n, k = I() if k == 0: a = "9" * (n // 9) + (str(n % 9) if n % 9 else "") print(a[::-1]) continue mi = (k + 1) * k // 2 if n < mi: print(-1) else: an = -1 for i in range(10): mi = 0 for j in range(k + 1): mi += sum(int(p) for p in str(i + j)) if n < mi or (n - mi) % (k + 1): continue a = str(i) r = (n - mi) // (k + 1) if i + k >= 10: a += str(min(8, r)) r -= min(8, r) while r: a += str(min(9, r)) r -= min(9, r) if an == -1: an = int(a[::-1]) else: an = min(an, int(a[::-1])) print(an)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER VAR WHILE VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
for _ in range(int(input())): n, k = map(int, input().split()) ans = float("inf") for i in range(10000): Sum = 0 for j in range(i, i + k + 1): tmp = j while tmp: Sum += tmp % 10 tmp //= 10 if (n - Sum) % (k + 1) == 0 and n >= Sum: x = (n - Sum) // (k + 1) tmp = str(x % 9) + str("9") * (x // 9) tmp += "0" + str(i) if i < 10 else str(i) ans = min(ans, int(tmp)) print(-1) if ans == float("inf") else print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR STRING BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP STRING FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR VAR FUNC_CALL VAR STRING FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
def num(pref): if pref == 0: return str() if pref < 9: return str(pref) else: ans = [8] pref -= 8 while pref > 9: ans.append(9) pref -= 9 if pref > 0: ans.append(pref) ans.reverse() return "".join([str(i) for i in ans]) def comp(a, b): if len(a) != len(b): return len(a) < len(b) else: for i in range(len(a)): if int(a[i]) < int(b[i]): return True elif int(a[i]) > int(b[i]): return False for _ in range(int(input())): n, k = map(int, input().split()) b = True ar = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, ] ans = "1" * n for dig in range(10): for n_9 in range(17): t1 = min(10 - dig, k + 1) t = n - sum(ar[dig : dig + k + 1]) - 9 * n_9 * t1 - k - 1 + t1 if t >= 0 and t % (k + 1) == 0: pref = t // (k + 1) t_ans = num(pref) + "9" * n_9 + str(dig) if comp(t_ans, ans): ans = t_ans b = False if b: print(-1) else: print(ans)
FUNC_DEF IF VAR NUMBER RETURN FUNC_CALL VAR IF VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP STRING VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER VAR IF VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP STRING VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
t = int(input()) best = [[(-1) for x in range(10)] for y in range(151)] m = [0] n = 0 for a in range(2, 17): for p in range(9): n += 10**a m.append(n) for r in m: for h in range(105): p = r + h total = 0 for k in range(10): l = list(str(p + k)) l = [int(v) for v in l] total += sum(l) if total < 151 and best[total][k] == -1: best[total][k] = p for _ in range(t): a, b = list(map(int, input().split())) print(best[a][b])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
import sys sys.setrecursionlimit(10**6) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def SI(): return sys.stdin.readline()[:-1] def tox(s, k, c9): if c9: back = 9 - k + c9 s -= back res = str(back) if s < 0: return -1 if s == 0: return int(res) back = min(8, s) s -= back res += str(back) else: back = min(9 - k, s) s -= back res = str(back) if s == 0: return int(res[::-1]) c9, s = divmod(s, 9) res += "9" * c9 if s: res += str(s) return int(res[::-1]) def main(): for _ in range(II()): n, k = MI() s = k * (k + 1) // 2 if n < s: print(-1) continue n -= s + 9 x = -1 for i in range(k + 1): n += 9 if n % (k + 1): continue d = n // (k + 1) x = tox(d, k, i) if x != -1: break print(x) main()
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR NUMBER FUNC_DEF IF VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP STRING VAR IF VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
def pre(n): if n <= 8: return n n -= 8 count = n // 9 n = n % 9 if n > 0: return int(str(n) + "9" * count + str(8)) else: return int("9" * count + str(8)) def answer(n, k): ans = 10**20 for i in range(17): for j in range(0, 10): t = min(10 - j, k + 1) p = n - (k + 1 - t) - 9 * i * t for h in range(k + 1): p -= (h + j) % 10 if p >= 0 and p % (k + 1) == 0: ans = min( ans, 10 ** (i + 1) * pre(p // (k + 1)) + (10 ** (i + 1) - 10) + j ) if ans == 10**20: return -1 else: return ans t = int(input()) for i in range(t): n, k = map(int, input().split()) print(answer(n, k))
FUNC_DEF IF VAR NUMBER RETURN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP STRING VAR FUNC_CALL VAR NUMBER RETURN FUNC_CALL VAR BIN_OP BIN_OP STRING VAR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER VAR IF VAR BIN_OP NUMBER NUMBER RETURN NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
def f(x): return sum(map(int, str(x))) def subsolve(n, k, x): x = max(0, x - 20) ans = -1 for _ in range(41): s = 0 for i in range(k + 1): s += f(x + i) if s == n: ans = x break x += 1 return ans def solve(): n, k = map(int, input().split()) u = k * (k + 1) // 2 if n < u: print(-1) return m = (n - u) // (k + 1) x = [] while m: w = min(m, 9) x.append(str(w)) m -= w x.reverse() if not x: print(subsolve(n, k, 0)) return xl = len(x) tl = 10 ** (xl - 1) first = int(x[0]) x = int("".join(x)) ans = -1 for j in range(3): tj = 10**j for i in range(10): w = subsolve(n, k, (x + (i - first) * tl) * tj) if ans == -1 or w != -1 and w < ans: ans = w print(ans) t = int(input()) for _ in range(t): solve()
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER RETURN ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
def read_int(): return int(input()) def read_ints(): return list(map(int, input().split(" "))) d = [(i % 10) for i in range(20)] def one(n): digits = ["9" for i in range(n // 9)] if n % 9 != 0: digits.append(str(n % 9)) digits.reverse() return "".join(digits) def two(n): if n % 2 == 1: if n <= 17: return n // 2 return one((n - 17) // 2) + "8" else: if n < 10: return -1 if n < 28: return (n - 9) // 2 * 10 + 9 return one((n - 28) // 2) + "89" def three(n): if n % 3 != 0: return -1 if n <= 24: return n // 3 - 1 return one((n - 24) // 3) + "7" def more(n, k): ans = int(1e50) for i in range(10): last = 0 for j in range(i, i + k + 1): last += d[j] if last > n: continue if i + k < 10: if last == n: return i else: rem = n - last if rem % (k + 1) == 0: up = int(one(rem // (k + 1))) * 10 + d[i] ans = min(ans, up) else: right = i + k - 9 left = k + 1 - right rem = n - last + left if rem % (k + 1) == 0: up = (int(one(rem // (k + 1))) - 1) * 10 + d[i] ans = min(ans, up) return -1 if ans == int(1e50) else ans t = read_int() for case_num in range(t): n, k = read_ints() print(more(n, k))
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR STRING VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR RETURN FUNC_CALL STRING VAR FUNC_DEF IF BIN_OP VAR NUMBER NUMBER IF VAR NUMBER RETURN BIN_OP VAR NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER STRING IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER STRING FUNC_DEF IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER IF VAR NUMBER RETURN BIN_OP BIN_OP VAR NUMBER NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR IF VAR VAR IF BIN_OP VAR VAR NUMBER IF VAR VAR RETURN VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_CALL VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
def digsum(x): ret = 0 while x > 0: ret += x % 10 x //= 10 return ret tt = int(input()) for loop in range(tt): n, k = map(int, input().split()) ans = float("inf") if (n - k * (k + 1) // 2) % (k + 1) == 0: X = (n - k * (k + 1) // 2) // (k + 1) now = [] while X > 0: if len(now) == 0: now.append(min(X, 9 - k)) else: now.append(min(9, X)) X -= now[-1] tmp = 0 for i in range(len(now)): tmp += 10**i * now[i] ans = min(tmp, ans) for t in range(1, k + 1): for m in range(1, 50): if (n - k * (k + 1) // 2 + 9 * m * (k - t + 1)) % (k + 1) == 0: X = (n - k * (k + 1) // 2 + 9 * m * (k - t + 1)) // (k + 1) now = [] Xdown = 10**m - t while Xdown > 0: now.append(Xdown % 10) Xdown //= 10 X -= now[-1] if X < 0: continue fl = True while X > 0: if fl: now.append(min(8, X)) fl = False else: now.append(min(9, X)) X -= now[-1] tmp = 0 for i in range(len(now)): tmp += 10**i * now[i] ans = min(tmp, ans) if ans == float("inf"): ans = -1 if ans == 0: ntmp = 0 for j in range(ans, ans + k + 1): ntmp += digsum(j) if ntmp != n: ans = -1 print(ans)
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING IF BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST WHILE VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
def f(sm, nine): if sm < 0: return -1 if nine * 9 > sm: return -1 sm -= nine * 9 ret = nine * "9" if sm >= 8: sm -= 8 ret = "8" + ret ret = str(sm % 9) + sm // 9 * "9" + ret return int(ret) def g(sm, lstmax): mn = min(sm, lstmax) sm -= lstmax ret = str(sm % 9) + sm // 9 * "9" + str(mn) return int(ret) def digit_sum(n): s = str(n) return sum(int(i) for i in s) def nc(i): return i * (i + 1) // 2 def valid(n, k): n %= 10 if n + k >= 10: return False return True def naive(n, k): c = 0 sm = 0 for i in range(k + 1): sm += digit_sum(c + i) for _ in range(10**6): if sm == n: return c sm -= digit_sum(c) c += 1 sm += digit_sum(c + k) else: return -1 for _ in range(int(input())): n, k = list(map(int, input().split())) mp = k * (k + 1) // 2 res = 10**100 if n - mp > 0 and (n - mp) % (k + 1) == 0: fx = (n - mp) // (k + 1) res = min(res, g(fx, 9 - k)) for i in range(k + 1): for j in range(1, 30): mp = nc(k - i) - nc(i) if ( n + 9 * j * (k - i) - mp > 0 and (n + 9 * j * (k - i) - mp) % (k + 1) == 0 ): fx = (n + 9 * j * (k - i) - mp) // (k + 1) if f(fx, j) - i < 0: continue res = min(res, f(fx, j) - i) for i in range(400): tmp = 0 for j in range(k + 1): tmp += digit_sum(i + j) if tmp == n: res = min(res, i) break if res == 10**100: res = -1 print(res)
FUNC_DEF IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER VAR RETURN NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER STRING VAR RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER STRING FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF VAR NUMBER IF BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER IF VAR VAR RETURN VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
t = int(input()) def solve(n, k): ans = -1 for i in range(10): m = n // 9 if i + k < 10: m = 0 for j in range(m + 1): if i + k >= 10 and j == 0: continue cur_sum = (i + (i + k)) * (k + 1) // 2 if j != 0: before_turn = 10 - i after_turn = k + 1 - before_turn cur_sum = ( (i + 9) * before_turn // 2 + (1 + after_turn) * after_turn // 2 + before_turn * 9 * (j - 1) ) rem = n - cur_sum if rem < 0 or rem % (k + 1) != 0: continue num = [i] for x in range(j - 1): num.append(9) mx = 9 if i + k >= 10: mx = 8 rem = rem // (k + 1) while rem != 0: d = min(rem, mx) mx = 9 rem -= d num.append(d) cur_best = 0 for d in range(len(num)): cur_best += 10**d * num[d] if ans == -1: ans = cur_best else: ans = min(ans, cur_best) print(ans) for i in range(t): n, k = input().split(" ") n = int(n) k = int(k) solve(n, k)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
def findPref(targetDigitSum): assert targetDigitSum >= 0 if targetDigitSum == 0: return "" if targetDigitSum <= 8: return str(targetDigitSum) rem = targetDigitSum - 8 if rem % 9 == 0: return "9" * (rem // 9) + "8" return str(rem % 9) + "9" * (rem // 9) + "8" def findPref9(target): assert target >= 0 if target == 0: return "" if target % 9 == 0: return "9" * (target // 9) return str(target % 9) + "9" * (target // 9) def compare(a, b): if a == None and b == None: return 0 if a == None: return 1 if b == None: return -1 if len(a) > len(b): return 1 if len(b) > len(a): return -1 if a > b: return 1 if a < b: return -1 return 0 def solve(n, k): ans = None for d0 in range(10): sumLowDigitColumns = sum((d0 + d) % 10 for d in range(k + 1)) HON = 0 if d0 + k <= 9 else 10 - d0 if HON == 0: rem = n - sumLowDigitColumns if rem >= 0 and rem % (k + 1) == 0: pref = findPref9(rem // (k + 1)) cur = pref + str(d0) if compare(cur, ans) < 0: ans = cur else: WON = 0 while True: rem = n - sumLowDigitColumns + HON - WON * HON * 9 - k - 1 if rem < 0: break if rem % (k + 1) == 0: pref = findPref(rem // (k + 1)) cur = pref + "9" * WON + str(d0) if compare(cur, ans) < 0: ans = cur WON += 1 return -1 if ans == None else ans TN = int(input()) for _ in range(TN): items = input().split() n = int(items[0]) k = int(items[1]) print(solve(n, k))
FUNC_DEF VAR NUMBER IF VAR NUMBER RETURN STRING IF VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP STRING BIN_OP VAR NUMBER STRING RETURN BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP STRING BIN_OP VAR NUMBER STRING FUNC_DEF VAR NUMBER IF VAR NUMBER RETURN STRING IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP STRING BIN_OP VAR NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP STRING BIN_OP VAR NUMBER FUNC_DEF IF VAR NONE VAR NONE RETURN NUMBER IF VAR NONE RETURN NUMBER IF VAR NONE RETURN NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NONE FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER BIN_OP NUMBER VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP STRING VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR NONE NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
def digitsum(n): ans = 0 while n > 0: ans += n % 10 n //= 10 return ans for vishal in range(int(input())): ans = 10**40 n, k = list(map(int, input().split(" "))) for lastdigit in range(10): for numberofnine in range(100): if lastdigit + 9 * numberofnine > n: continue candi = "9" * numberofnine + str(lastdigit) mandi = candi done = 0 for ii in range(k + 1): done += digitsum(int(candi) + ii) if k + 1 >= 10 - lastdigit: done -= k + 1 - 10 + lastdigit if done > n: continue for aurek in range(9): ddone = 0 if k + 1 >= 10 - lastdigit: ddone = ( done + aurek * (10 - lastdigit) + (aurek + 1) * (k + 1 - 10 + lastdigit) ) else: ddone = done + aurek * (k + 1) if ddone > n: continue if ddone == n: if aurek > 0: candi = str(aurek) + candi ans = min(ans, int(candi)) else: ans = min(ans, int(candi)) continue ddone = n - ddone if ddone % (k + 1) != 0: continue ddone //= k + 1 t = ddone % 9 ss = "" if t: ss += str(t) for iii in range(ddone // 9): ss += "9" if len(ss) > 0: ss += str(aurek) if len(ss) == 0: ans = min(ans, int(mandi)) else: ss += candi ans = min(ans, int(ss)) if ans >= 10**40: ans = -1 print(ans)
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR IF VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR STRING IF VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR STRING IF FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
line = input() t = int(line) def helper(n): res = "" if n % 9 == 0 else str(n % 9) res += "9" * (n // 9) return res for _ in range(t): line = input() n, k = [int(i) for i in line.split(" ")] res = "" if k == 0: print(helper(n)) else: k += 1 a = sum(range(k)) for i in range(10): r = n - a a = a - i + (i + k) % 10 if r < 0: continue elif i + k - 1 < 10: if r % k != 0: continue cur = helper(r // k) + str(i) if not res or len(res) > len(cur) or len(res) == len(cur) and res > cur: res = cur else: ten = (i + k - 1) % 10 + 1 last = str(i) while r >= ten: if (r - ten) % k == 0: if r - k * 9 + (k - ten) <= 0: cur = helper((r - ten) // k) + last else: cur = helper((r - k * 9 + (k - ten)) // k) + "8" + last if ( not res or len(res) > len(cur) or len(res) == len(cur) and res > cur ): res = cur break else: last = "9" + last r -= (k - ten) * 9 if res: print(res) else: print(-1)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER NUMBER STRING FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP STRING BIN_OP VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR IF BIN_OP BIN_OP VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR STRING VAR IF VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP STRING VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) β€” the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
def construct(fx, d, i): s = "9" * (i - 1) + str(d) fx -= d + 9 * (i - 1) if fx < 0: return 2**63 - 1 if fx >= 8: s = "8" + s fx -= 8 else: s = str(fx) + s fx = 0 while fx: if fx >= 9: s = "9" + s fx -= 9 else: s = str(fx) + s fx = 0 return int(s) for T in range(int(input())): n, k = (int(x) for x in input().split()) ans = 2**63 - 1 for d in range(10): for i in range(1, 21): temp = n + 9 * i * max(0, k - 9 + d) - k * (k + 1) // 2 if temp < 0 or temp % (k + 1): continue temp //= k + 1 s = construct(temp, d, i) ans = min(ans, s) print(ans if ans != 2**63 - 1 else -1)
FUNC_DEF ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER IF VAR NUMBER RETURN BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP STRING VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR IF VAR NUMBER ASSIGN VAR BIN_OP STRING VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER RETURN FUNC_CALL VAR VAR 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 BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR NUMBER
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers. Consider a number axis. The capital of your empire is initially at $0$. There are $n$ unconquered kingdoms at positions $0<x_1<x_2<\ldots<x_n$. You want to conquer all other kingdoms. There are two actions available to you: You can change the location of your capital (let its current position be $c_1$) to any other conquered kingdom (let its position be $c_2$) at a cost of $a\cdot |c_1-c_2|$. From the current capital (let its current position be $c_1$) you can conquer an unconquered kingdom (let its position be $c_2$) at a cost of $b\cdot |c_1-c_2|$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $0$ or one of $x_1,x_2,\ldots,x_n$. Also note that conquering a kingdom does not change the position of your capital. Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The description of each test case follows. The first line of each test case contains $3$ integers $n$, $a$, and $b$ ($1 \leq n \leq 2 \cdot 10^5$; $1 \leq a,b \leq 10^5$). The second line of each test case contains $n$ integers $x_1, x_2, \ldots, x_n$ ($1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$). The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output a single integer β€” the minimum cost to conquer all kingdoms. -----Examples----- Input 4 5 2 7 3 5 12 13 21 5 6 3 1 5 6 21 30 2 9 3 10 15 11 27182 31415 16 18 33 98 874 989 4848 20458 34365 38117 72030 Output 173 171 75 3298918744 -----Note----- Here is an optimal sequence of moves for the second test case: Conquer the kingdom at position $1$ with cost $3\cdot(1-0)=3$. Move the capital to the kingdom at position $1$ with cost $6\cdot(1-0)=6$. Conquer the kingdom at position $5$ with cost $3\cdot(5-1)=12$. Move the capital to the kingdom at position $5$ with cost $6\cdot(5-1)=24$. Conquer the kingdom at position $6$ with cost $3\cdot(6-5)=3$. Conquer the kingdom at position $21$ with cost $3\cdot(21-5)=48$. Conquer the kingdom at position $30$ with cost $3\cdot(30-5)=75$. The total cost is $3+6+12+24+3+48+75=171$. You cannot get a lower cost than this.
import sys def solve(): inp = sys.stdin.readline n, a, b = map(int, inp().split()) x = list(map(int, inp().split())) s = sum(x) r = 0 now = 0 for i in range(n): s -= x[i] r += (x[i] - now) * b if (s - now * (n - i - 1)) * b >= (s - x[i] * (n - i - 1)) * b + ( x[i] - now ) * a: r += (x[i] - now) * a now = x[i] print(r) def main(): for i in range(int(sys.stdin.readline())): solve() main()
IMPORT FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR IF BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers. Consider a number axis. The capital of your empire is initially at $0$. There are $n$ unconquered kingdoms at positions $0<x_1<x_2<\ldots<x_n$. You want to conquer all other kingdoms. There are two actions available to you: You can change the location of your capital (let its current position be $c_1$) to any other conquered kingdom (let its position be $c_2$) at a cost of $a\cdot |c_1-c_2|$. From the current capital (let its current position be $c_1$) you can conquer an unconquered kingdom (let its position be $c_2$) at a cost of $b\cdot |c_1-c_2|$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $0$ or one of $x_1,x_2,\ldots,x_n$. Also note that conquering a kingdom does not change the position of your capital. Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The description of each test case follows. The first line of each test case contains $3$ integers $n$, $a$, and $b$ ($1 \leq n \leq 2 \cdot 10^5$; $1 \leq a,b \leq 10^5$). The second line of each test case contains $n$ integers $x_1, x_2, \ldots, x_n$ ($1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$). The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output a single integer β€” the minimum cost to conquer all kingdoms. -----Examples----- Input 4 5 2 7 3 5 12 13 21 5 6 3 1 5 6 21 30 2 9 3 10 15 11 27182 31415 16 18 33 98 874 989 4848 20458 34365 38117 72030 Output 173 171 75 3298918744 -----Note----- Here is an optimal sequence of moves for the second test case: Conquer the kingdom at position $1$ with cost $3\cdot(1-0)=3$. Move the capital to the kingdom at position $1$ with cost $6\cdot(1-0)=6$. Conquer the kingdom at position $5$ with cost $3\cdot(5-1)=12$. Move the capital to the kingdom at position $5$ with cost $6\cdot(5-1)=24$. Conquer the kingdom at position $6$ with cost $3\cdot(6-5)=3$. Conquer the kingdom at position $21$ with cost $3\cdot(21-5)=48$. Conquer the kingdom at position $30$ with cost $3\cdot(30-5)=75$. The total cost is $3+6+12+24+3+48+75=171$. You cannot get a lower cost than this.
for iii in range(int(input())): n, a, b = map(int, input().split()) q = list(map(int, input().split())) q1 = [0] + q for i in range(1, n + 1): q1[i] += q1[i - 1] bwd_sum = 0 shift_cap = 0 fwd = 0 summ = 123456789876543212345678 for i in range(n + 1): if i == 0: bwd_sum = 0 elif i == 1: bwd_sum = q[i - 1] * b else: bwd_sum = bwd_sum + (q[i - 1] - q[i - 2]) * b if i == 0: shift_cap = 0 else: shift_cap = q[i - 1] * a if i == 0: fwd = q1[n] * b else: fwd = (q1[n] - q1[i] - q[i - 1] * (n - i)) * b summ = min(summ, fwd + bwd_sum + shift_cap) print(summ)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers. Consider a number axis. The capital of your empire is initially at $0$. There are $n$ unconquered kingdoms at positions $0<x_1<x_2<\ldots<x_n$. You want to conquer all other kingdoms. There are two actions available to you: You can change the location of your capital (let its current position be $c_1$) to any other conquered kingdom (let its position be $c_2$) at a cost of $a\cdot |c_1-c_2|$. From the current capital (let its current position be $c_1$) you can conquer an unconquered kingdom (let its position be $c_2$) at a cost of $b\cdot |c_1-c_2|$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $0$ or one of $x_1,x_2,\ldots,x_n$. Also note that conquering a kingdom does not change the position of your capital. Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The description of each test case follows. The first line of each test case contains $3$ integers $n$, $a$, and $b$ ($1 \leq n \leq 2 \cdot 10^5$; $1 \leq a,b \leq 10^5$). The second line of each test case contains $n$ integers $x_1, x_2, \ldots, x_n$ ($1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$). The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output a single integer β€” the minimum cost to conquer all kingdoms. -----Examples----- Input 4 5 2 7 3 5 12 13 21 5 6 3 1 5 6 21 30 2 9 3 10 15 11 27182 31415 16 18 33 98 874 989 4848 20458 34365 38117 72030 Output 173 171 75 3298918744 -----Note----- Here is an optimal sequence of moves for the second test case: Conquer the kingdom at position $1$ with cost $3\cdot(1-0)=3$. Move the capital to the kingdom at position $1$ with cost $6\cdot(1-0)=6$. Conquer the kingdom at position $5$ with cost $3\cdot(5-1)=12$. Move the capital to the kingdom at position $5$ with cost $6\cdot(5-1)=24$. Conquer the kingdom at position $6$ with cost $3\cdot(6-5)=3$. Conquer the kingdom at position $21$ with cost $3\cdot(21-5)=48$. Conquer the kingdom at position $30$ with cost $3\cdot(30-5)=75$. The total cost is $3+6+12+24+3+48+75=171$. You cannot get a lower cost than this.
for test in range(int(input())): n, a, b = map(int, input().split()) x = list(map(int, input().split())) sm = sum(x) ans = sm * b s = 0 for i in range(n): s += x[i] ans = min(ans, (a + b) * x[i] + (sm - s - (n - i - 1) * x[i]) * b) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers. Consider a number axis. The capital of your empire is initially at $0$. There are $n$ unconquered kingdoms at positions $0<x_1<x_2<\ldots<x_n$. You want to conquer all other kingdoms. There are two actions available to you: You can change the location of your capital (let its current position be $c_1$) to any other conquered kingdom (let its position be $c_2$) at a cost of $a\cdot |c_1-c_2|$. From the current capital (let its current position be $c_1$) you can conquer an unconquered kingdom (let its position be $c_2$) at a cost of $b\cdot |c_1-c_2|$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $0$ or one of $x_1,x_2,\ldots,x_n$. Also note that conquering a kingdom does not change the position of your capital. Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The description of each test case follows. The first line of each test case contains $3$ integers $n$, $a$, and $b$ ($1 \leq n \leq 2 \cdot 10^5$; $1 \leq a,b \leq 10^5$). The second line of each test case contains $n$ integers $x_1, x_2, \ldots, x_n$ ($1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$). The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output a single integer β€” the minimum cost to conquer all kingdoms. -----Examples----- Input 4 5 2 7 3 5 12 13 21 5 6 3 1 5 6 21 30 2 9 3 10 15 11 27182 31415 16 18 33 98 874 989 4848 20458 34365 38117 72030 Output 173 171 75 3298918744 -----Note----- Here is an optimal sequence of moves for the second test case: Conquer the kingdom at position $1$ with cost $3\cdot(1-0)=3$. Move the capital to the kingdom at position $1$ with cost $6\cdot(1-0)=6$. Conquer the kingdom at position $5$ with cost $3\cdot(5-1)=12$. Move the capital to the kingdom at position $5$ with cost $6\cdot(5-1)=24$. Conquer the kingdom at position $6$ with cost $3\cdot(6-5)=3$. Conquer the kingdom at position $21$ with cost $3\cdot(21-5)=48$. Conquer the kingdom at position $30$ with cost $3\cdot(30-5)=75$. The total cost is $3+6+12+24+3+48+75=171$. You cannot get a lower cost than this.
for _ in range(int(input())): n, a, b = map(int, input().split()) lst = list(map(int, input().split())) pos = 0 llstr = [0] s2 = sum(lst) mini = sum(lst) * b for i in range(n): llstr.append((a + b) * (lst[i] - pos) + llstr[-1]) pos = lst[i] s2 -= lst[i] s3 = s2 - pos * (n - 1 - i) mini = min(s3 * b + llstr[-1], mini) pos = lst[i] print(mini)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers. Consider a number axis. The capital of your empire is initially at $0$. There are $n$ unconquered kingdoms at positions $0<x_1<x_2<\ldots<x_n$. You want to conquer all other kingdoms. There are two actions available to you: You can change the location of your capital (let its current position be $c_1$) to any other conquered kingdom (let its position be $c_2$) at a cost of $a\cdot |c_1-c_2|$. From the current capital (let its current position be $c_1$) you can conquer an unconquered kingdom (let its position be $c_2$) at a cost of $b\cdot |c_1-c_2|$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $0$ or one of $x_1,x_2,\ldots,x_n$. Also note that conquering a kingdom does not change the position of your capital. Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The description of each test case follows. The first line of each test case contains $3$ integers $n$, $a$, and $b$ ($1 \leq n \leq 2 \cdot 10^5$; $1 \leq a,b \leq 10^5$). The second line of each test case contains $n$ integers $x_1, x_2, \ldots, x_n$ ($1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$). The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output a single integer β€” the minimum cost to conquer all kingdoms. -----Examples----- Input 4 5 2 7 3 5 12 13 21 5 6 3 1 5 6 21 30 2 9 3 10 15 11 27182 31415 16 18 33 98 874 989 4848 20458 34365 38117 72030 Output 173 171 75 3298918744 -----Note----- Here is an optimal sequence of moves for the second test case: Conquer the kingdom at position $1$ with cost $3\cdot(1-0)=3$. Move the capital to the kingdom at position $1$ with cost $6\cdot(1-0)=6$. Conquer the kingdom at position $5$ with cost $3\cdot(5-1)=12$. Move the capital to the kingdom at position $5$ with cost $6\cdot(5-1)=24$. Conquer the kingdom at position $6$ with cost $3\cdot(6-5)=3$. Conquer the kingdom at position $21$ with cost $3\cdot(21-5)=48$. Conquer the kingdom at position $30$ with cost $3\cdot(30-5)=75$. The total cost is $3+6+12+24+3+48+75=171$. You cannot get a lower cost than this.
cases = int(input()) for case in range(cases): n, b, a = map(int, input().split()) kingdoms = list(map(int, input().split())) total = a * sum(kingdoms) index = 0 cur = 0 for item in kingdoms: difference = (item - cur) * b - a * (item - cur) * (n - 1 - index) if difference < 0: total += difference cur = item index += 1 else: break print(total)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers. Consider a number axis. The capital of your empire is initially at $0$. There are $n$ unconquered kingdoms at positions $0<x_1<x_2<\ldots<x_n$. You want to conquer all other kingdoms. There are two actions available to you: You can change the location of your capital (let its current position be $c_1$) to any other conquered kingdom (let its position be $c_2$) at a cost of $a\cdot |c_1-c_2|$. From the current capital (let its current position be $c_1$) you can conquer an unconquered kingdom (let its position be $c_2$) at a cost of $b\cdot |c_1-c_2|$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $0$ or one of $x_1,x_2,\ldots,x_n$. Also note that conquering a kingdom does not change the position of your capital. Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The description of each test case follows. The first line of each test case contains $3$ integers $n$, $a$, and $b$ ($1 \leq n \leq 2 \cdot 10^5$; $1 \leq a,b \leq 10^5$). The second line of each test case contains $n$ integers $x_1, x_2, \ldots, x_n$ ($1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$). The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output a single integer β€” the minimum cost to conquer all kingdoms. -----Examples----- Input 4 5 2 7 3 5 12 13 21 5 6 3 1 5 6 21 30 2 9 3 10 15 11 27182 31415 16 18 33 98 874 989 4848 20458 34365 38117 72030 Output 173 171 75 3298918744 -----Note----- Here is an optimal sequence of moves for the second test case: Conquer the kingdom at position $1$ with cost $3\cdot(1-0)=3$. Move the capital to the kingdom at position $1$ with cost $6\cdot(1-0)=6$. Conquer the kingdom at position $5$ with cost $3\cdot(5-1)=12$. Move the capital to the kingdom at position $5$ with cost $6\cdot(5-1)=24$. Conquer the kingdom at position $6$ with cost $3\cdot(6-5)=3$. Conquer the kingdom at position $21$ with cost $3\cdot(21-5)=48$. Conquer the kingdom at position $30$ with cost $3\cdot(30-5)=75$. The total cost is $3+6+12+24+3+48+75=171$. You cannot get a lower cost than this.
t = int(input()) def solve(n, a, b, X): dists = [X[0]] for x in X[1:]: dists.append(x + dists[-1]) lowest = dists[-1] * b for i in range(n - 1): current = a * X[i] current += b * X[i + 1] current += (dists[-1] - dists[i + 1] - (n - i - 2) * X[i]) * b lowest = min(current, lowest) return lowest for _ in range(t): n, a, b = map(int, input().split()) X = list(map(int, input().split())) print(solve(n, a, b, X))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST VAR NUMBER FOR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN 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_CALL VAR VAR VAR VAR VAR
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers. Consider a number axis. The capital of your empire is initially at $0$. There are $n$ unconquered kingdoms at positions $0<x_1<x_2<\ldots<x_n$. You want to conquer all other kingdoms. There are two actions available to you: You can change the location of your capital (let its current position be $c_1$) to any other conquered kingdom (let its position be $c_2$) at a cost of $a\cdot |c_1-c_2|$. From the current capital (let its current position be $c_1$) you can conquer an unconquered kingdom (let its position be $c_2$) at a cost of $b\cdot |c_1-c_2|$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $0$ or one of $x_1,x_2,\ldots,x_n$. Also note that conquering a kingdom does not change the position of your capital. Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The description of each test case follows. The first line of each test case contains $3$ integers $n$, $a$, and $b$ ($1 \leq n \leq 2 \cdot 10^5$; $1 \leq a,b \leq 10^5$). The second line of each test case contains $n$ integers $x_1, x_2, \ldots, x_n$ ($1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$). The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output a single integer β€” the minimum cost to conquer all kingdoms. -----Examples----- Input 4 5 2 7 3 5 12 13 21 5 6 3 1 5 6 21 30 2 9 3 10 15 11 27182 31415 16 18 33 98 874 989 4848 20458 34365 38117 72030 Output 173 171 75 3298918744 -----Note----- Here is an optimal sequence of moves for the second test case: Conquer the kingdom at position $1$ with cost $3\cdot(1-0)=3$. Move the capital to the kingdom at position $1$ with cost $6\cdot(1-0)=6$. Conquer the kingdom at position $5$ with cost $3\cdot(5-1)=12$. Move the capital to the kingdom at position $5$ with cost $6\cdot(5-1)=24$. Conquer the kingdom at position $6$ with cost $3\cdot(6-5)=3$. Conquer the kingdom at position $21$ with cost $3\cdot(21-5)=48$. Conquer the kingdom at position $30$ with cost $3\cdot(30-5)=75$. The total cost is $3+6+12+24+3+48+75=171$. You cannot get a lower cost than this.
def solve(): n, a, b = map(int, input().split()) x = [0] + list(map(int, input().split())) n += 1 s = sum(x[1:]) mx = float("inf") for i in range(n): v = x[i] cr = (a + b) * v s -= v cr += (s - (n - i - 1) * v) * b mx = min(mx, cr) print(mx) tc = int(input()) for _ in range(tc): solve()
FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers. Consider a number axis. The capital of your empire is initially at $0$. There are $n$ unconquered kingdoms at positions $0<x_1<x_2<\ldots<x_n$. You want to conquer all other kingdoms. There are two actions available to you: You can change the location of your capital (let its current position be $c_1$) to any other conquered kingdom (let its position be $c_2$) at a cost of $a\cdot |c_1-c_2|$. From the current capital (let its current position be $c_1$) you can conquer an unconquered kingdom (let its position be $c_2$) at a cost of $b\cdot |c_1-c_2|$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $0$ or one of $x_1,x_2,\ldots,x_n$. Also note that conquering a kingdom does not change the position of your capital. Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The description of each test case follows. The first line of each test case contains $3$ integers $n$, $a$, and $b$ ($1 \leq n \leq 2 \cdot 10^5$; $1 \leq a,b \leq 10^5$). The second line of each test case contains $n$ integers $x_1, x_2, \ldots, x_n$ ($1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$). The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output a single integer β€” the minimum cost to conquer all kingdoms. -----Examples----- Input 4 5 2 7 3 5 12 13 21 5 6 3 1 5 6 21 30 2 9 3 10 15 11 27182 31415 16 18 33 98 874 989 4848 20458 34365 38117 72030 Output 173 171 75 3298918744 -----Note----- Here is an optimal sequence of moves for the second test case: Conquer the kingdom at position $1$ with cost $3\cdot(1-0)=3$. Move the capital to the kingdom at position $1$ with cost $6\cdot(1-0)=6$. Conquer the kingdom at position $5$ with cost $3\cdot(5-1)=12$. Move the capital to the kingdom at position $5$ with cost $6\cdot(5-1)=24$. Conquer the kingdom at position $6$ with cost $3\cdot(6-5)=3$. Conquer the kingdom at position $21$ with cost $3\cdot(21-5)=48$. Conquer the kingdom at position $30$ with cost $3\cdot(30-5)=75$. The total cost is $3+6+12+24+3+48+75=171$. You cannot get a lower cost than this.
def mincost(nums): presum = [0] * (n + 1) for i in range(1, n + 1): presum[i] = presum[i - 1] + nums[i - 1] ans = b * presum[n] for i in range(n): tmp = ( b * nums[i] + a * nums[i] + b * (presum[n] - presum[i + 1]) - (n - i - 1) * b * nums[i] ) ans = min(ans, tmp) return ans t = int(input()) for _ in range(t): n, a, b = [int(x) for x in input().split()] nums = [int(x) for x in input().split()] print(mincost(nums))
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers. Consider a number axis. The capital of your empire is initially at $0$. There are $n$ unconquered kingdoms at positions $0<x_1<x_2<\ldots<x_n$. You want to conquer all other kingdoms. There are two actions available to you: You can change the location of your capital (let its current position be $c_1$) to any other conquered kingdom (let its position be $c_2$) at a cost of $a\cdot |c_1-c_2|$. From the current capital (let its current position be $c_1$) you can conquer an unconquered kingdom (let its position be $c_2$) at a cost of $b\cdot |c_1-c_2|$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $0$ or one of $x_1,x_2,\ldots,x_n$. Also note that conquering a kingdom does not change the position of your capital. Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The description of each test case follows. The first line of each test case contains $3$ integers $n$, $a$, and $b$ ($1 \leq n \leq 2 \cdot 10^5$; $1 \leq a,b \leq 10^5$). The second line of each test case contains $n$ integers $x_1, x_2, \ldots, x_n$ ($1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$). The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output a single integer β€” the minimum cost to conquer all kingdoms. -----Examples----- Input 4 5 2 7 3 5 12 13 21 5 6 3 1 5 6 21 30 2 9 3 10 15 11 27182 31415 16 18 33 98 874 989 4848 20458 34365 38117 72030 Output 173 171 75 3298918744 -----Note----- Here is an optimal sequence of moves for the second test case: Conquer the kingdom at position $1$ with cost $3\cdot(1-0)=3$. Move the capital to the kingdom at position $1$ with cost $6\cdot(1-0)=6$. Conquer the kingdom at position $5$ with cost $3\cdot(5-1)=12$. Move the capital to the kingdom at position $5$ with cost $6\cdot(5-1)=24$. Conquer the kingdom at position $6$ with cost $3\cdot(6-5)=3$. Conquer the kingdom at position $21$ with cost $3\cdot(21-5)=48$. Conquer the kingdom at position $30$ with cost $3\cdot(30-5)=75$. The total cost is $3+6+12+24+3+48+75=171$. You cannot get a lower cost than this.
t = int(input()) for _ in range(0, t): inputs = [int(num) for num in input().split()] n = inputs[0] a = inputs[1] b = inputs[2] list1 = [int(num) for num in input().split()] c = 0 cost_now = 0 now = 0 cost = 0 for i in range(0, len(list1)): point = list1[i] cost += (point - now) * b c += 1 if (point - now) * a <= (point - now) * b * (n - c): cost += (point - now) * a now = point print(cost)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers. Consider a number axis. The capital of your empire is initially at $0$. There are $n$ unconquered kingdoms at positions $0<x_1<x_2<\ldots<x_n$. You want to conquer all other kingdoms. There are two actions available to you: You can change the location of your capital (let its current position be $c_1$) to any other conquered kingdom (let its position be $c_2$) at a cost of $a\cdot |c_1-c_2|$. From the current capital (let its current position be $c_1$) you can conquer an unconquered kingdom (let its position be $c_2$) at a cost of $b\cdot |c_1-c_2|$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $0$ or one of $x_1,x_2,\ldots,x_n$. Also note that conquering a kingdom does not change the position of your capital. Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The description of each test case follows. The first line of each test case contains $3$ integers $n$, $a$, and $b$ ($1 \leq n \leq 2 \cdot 10^5$; $1 \leq a,b \leq 10^5$). The second line of each test case contains $n$ integers $x_1, x_2, \ldots, x_n$ ($1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$). The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output a single integer β€” the minimum cost to conquer all kingdoms. -----Examples----- Input 4 5 2 7 3 5 12 13 21 5 6 3 1 5 6 21 30 2 9 3 10 15 11 27182 31415 16 18 33 98 874 989 4848 20458 34365 38117 72030 Output 173 171 75 3298918744 -----Note----- Here is an optimal sequence of moves for the second test case: Conquer the kingdom at position $1$ with cost $3\cdot(1-0)=3$. Move the capital to the kingdom at position $1$ with cost $6\cdot(1-0)=6$. Conquer the kingdom at position $5$ with cost $3\cdot(5-1)=12$. Move the capital to the kingdom at position $5$ with cost $6\cdot(5-1)=24$. Conquer the kingdom at position $6$ with cost $3\cdot(6-5)=3$. Conquer the kingdom at position $21$ with cost $3\cdot(21-5)=48$. Conquer the kingdom at position $30$ with cost $3\cdot(30-5)=75$. The total cost is $3+6+12+24+3+48+75=171$. You cannot get a lower cost than this.
def solve(): n, a, b = map(int, input().split()) x = list(map(int, input().split())) ans = 10**18 suffix = 0 for i in range(n - 1, -1, -1): prefix = x[i] ans = min(ans, (a + b) * prefix + b * suffix) if i == 0: suffix += x[i] * n else: suffix += (x[i] - x[i - 1]) * (n - i) ans = min(ans, b * suffix) print(ans) t = int(input()) for _ in range(t): solve()
FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers. Consider a number axis. The capital of your empire is initially at $0$. There are $n$ unconquered kingdoms at positions $0<x_1<x_2<\ldots<x_n$. You want to conquer all other kingdoms. There are two actions available to you: You can change the location of your capital (let its current position be $c_1$) to any other conquered kingdom (let its position be $c_2$) at a cost of $a\cdot |c_1-c_2|$. From the current capital (let its current position be $c_1$) you can conquer an unconquered kingdom (let its position be $c_2$) at a cost of $b\cdot |c_1-c_2|$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $0$ or one of $x_1,x_2,\ldots,x_n$. Also note that conquering a kingdom does not change the position of your capital. Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The description of each test case follows. The first line of each test case contains $3$ integers $n$, $a$, and $b$ ($1 \leq n \leq 2 \cdot 10^5$; $1 \leq a,b \leq 10^5$). The second line of each test case contains $n$ integers $x_1, x_2, \ldots, x_n$ ($1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$). The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output a single integer β€” the minimum cost to conquer all kingdoms. -----Examples----- Input 4 5 2 7 3 5 12 13 21 5 6 3 1 5 6 21 30 2 9 3 10 15 11 27182 31415 16 18 33 98 874 989 4848 20458 34365 38117 72030 Output 173 171 75 3298918744 -----Note----- Here is an optimal sequence of moves for the second test case: Conquer the kingdom at position $1$ with cost $3\cdot(1-0)=3$. Move the capital to the kingdom at position $1$ with cost $6\cdot(1-0)=6$. Conquer the kingdom at position $5$ with cost $3\cdot(5-1)=12$. Move the capital to the kingdom at position $5$ with cost $6\cdot(5-1)=24$. Conquer the kingdom at position $6$ with cost $3\cdot(6-5)=3$. Conquer the kingdom at position $21$ with cost $3\cdot(21-5)=48$. Conquer the kingdom at position $30$ with cost $3\cdot(30-5)=75$. The total cost is $3+6+12+24+3+48+75=171$. You cannot get a lower cost than this.
t = int(input()) for _ in range(t): n, a, b = map(int, input().split()) arr = list(map(int, input().split())) help = [0] * n for i in reversed(range(n - 1)): help[i] = help[i + 1] + arr[i + 1] cost = b * arr[0] index = 0 capital = 0 while index < n - 1: if b * (help[index] - capital * (n - (index + 1))) > a * ( arr[index] - capital ) + b * (help[index] - arr[index] * (n - (index + 1))): cost += a * (arr[index] - capital) capital = arr[index] cost += b * (arr[index + 1] - capital) index += 1 print(cost)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers. Consider a number axis. The capital of your empire is initially at $0$. There are $n$ unconquered kingdoms at positions $0<x_1<x_2<\ldots<x_n$. You want to conquer all other kingdoms. There are two actions available to you: You can change the location of your capital (let its current position be $c_1$) to any other conquered kingdom (let its position be $c_2$) at a cost of $a\cdot |c_1-c_2|$. From the current capital (let its current position be $c_1$) you can conquer an unconquered kingdom (let its position be $c_2$) at a cost of $b\cdot |c_1-c_2|$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $0$ or one of $x_1,x_2,\ldots,x_n$. Also note that conquering a kingdom does not change the position of your capital. Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The description of each test case follows. The first line of each test case contains $3$ integers $n$, $a$, and $b$ ($1 \leq n \leq 2 \cdot 10^5$; $1 \leq a,b \leq 10^5$). The second line of each test case contains $n$ integers $x_1, x_2, \ldots, x_n$ ($1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$). The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output a single integer β€” the minimum cost to conquer all kingdoms. -----Examples----- Input 4 5 2 7 3 5 12 13 21 5 6 3 1 5 6 21 30 2 9 3 10 15 11 27182 31415 16 18 33 98 874 989 4848 20458 34365 38117 72030 Output 173 171 75 3298918744 -----Note----- Here is an optimal sequence of moves for the second test case: Conquer the kingdom at position $1$ with cost $3\cdot(1-0)=3$. Move the capital to the kingdom at position $1$ with cost $6\cdot(1-0)=6$. Conquer the kingdom at position $5$ with cost $3\cdot(5-1)=12$. Move the capital to the kingdom at position $5$ with cost $6\cdot(5-1)=24$. Conquer the kingdom at position $6$ with cost $3\cdot(6-5)=3$. Conquer the kingdom at position $21$ with cost $3\cdot(21-5)=48$. Conquer the kingdom at position $30$ with cost $3\cdot(30-5)=75$. The total cost is $3+6+12+24+3+48+75=171$. You cannot get a lower cost than this.
def solve(): n, a, b = list(map(int, input().split(" "))) x = [0] + list(map(int, input().split(" "))) mn = float("inf") sum = x[n] for i in range(n - 1, -1, -1): res = b * x[i] + a * x[i] + b * (sum - (n - i) * x[i]) sum += x[i] mn = min(res, mn) print(mn) for t in range(int(input())): solve()
FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers. Consider a number axis. The capital of your empire is initially at $0$. There are $n$ unconquered kingdoms at positions $0<x_1<x_2<\ldots<x_n$. You want to conquer all other kingdoms. There are two actions available to you: You can change the location of your capital (let its current position be $c_1$) to any other conquered kingdom (let its position be $c_2$) at a cost of $a\cdot |c_1-c_2|$. From the current capital (let its current position be $c_1$) you can conquer an unconquered kingdom (let its position be $c_2$) at a cost of $b\cdot |c_1-c_2|$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $0$ or one of $x_1,x_2,\ldots,x_n$. Also note that conquering a kingdom does not change the position of your capital. Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The description of each test case follows. The first line of each test case contains $3$ integers $n$, $a$, and $b$ ($1 \leq n \leq 2 \cdot 10^5$; $1 \leq a,b \leq 10^5$). The second line of each test case contains $n$ integers $x_1, x_2, \ldots, x_n$ ($1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$). The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output a single integer β€” the minimum cost to conquer all kingdoms. -----Examples----- Input 4 5 2 7 3 5 12 13 21 5 6 3 1 5 6 21 30 2 9 3 10 15 11 27182 31415 16 18 33 98 874 989 4848 20458 34365 38117 72030 Output 173 171 75 3298918744 -----Note----- Here is an optimal sequence of moves for the second test case: Conquer the kingdom at position $1$ with cost $3\cdot(1-0)=3$. Move the capital to the kingdom at position $1$ with cost $6\cdot(1-0)=6$. Conquer the kingdom at position $5$ with cost $3\cdot(5-1)=12$. Move the capital to the kingdom at position $5$ with cost $6\cdot(5-1)=24$. Conquer the kingdom at position $6$ with cost $3\cdot(6-5)=3$. Conquer the kingdom at position $21$ with cost $3\cdot(21-5)=48$. Conquer the kingdom at position $30$ with cost $3\cdot(30-5)=75$. The total cost is $3+6+12+24+3+48+75=171$. You cannot get a lower cost than this.
for _ in range(int(input())): n, a, b = map(int, input().split()) positions = [0] positions.extend(map(int, input().split())) cost = 0 capital = 0 for i in range(1, n + 1): if b * (n - i - 1) > a - b: cost += (a + b) * (positions[i] - positions[i - 1]) capital = positions[i] else: cost += b * (positions[i] - capital) print(cost)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers. Consider a number axis. The capital of your empire is initially at $0$. There are $n$ unconquered kingdoms at positions $0<x_1<x_2<\ldots<x_n$. You want to conquer all other kingdoms. There are two actions available to you: You can change the location of your capital (let its current position be $c_1$) to any other conquered kingdom (let its position be $c_2$) at a cost of $a\cdot |c_1-c_2|$. From the current capital (let its current position be $c_1$) you can conquer an unconquered kingdom (let its position be $c_2$) at a cost of $b\cdot |c_1-c_2|$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $0$ or one of $x_1,x_2,\ldots,x_n$. Also note that conquering a kingdom does not change the position of your capital. Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The description of each test case follows. The first line of each test case contains $3$ integers $n$, $a$, and $b$ ($1 \leq n \leq 2 \cdot 10^5$; $1 \leq a,b \leq 10^5$). The second line of each test case contains $n$ integers $x_1, x_2, \ldots, x_n$ ($1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$). The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output a single integer β€” the minimum cost to conquer all kingdoms. -----Examples----- Input 4 5 2 7 3 5 12 13 21 5 6 3 1 5 6 21 30 2 9 3 10 15 11 27182 31415 16 18 33 98 874 989 4848 20458 34365 38117 72030 Output 173 171 75 3298918744 -----Note----- Here is an optimal sequence of moves for the second test case: Conquer the kingdom at position $1$ with cost $3\cdot(1-0)=3$. Move the capital to the kingdom at position $1$ with cost $6\cdot(1-0)=6$. Conquer the kingdom at position $5$ with cost $3\cdot(5-1)=12$. Move the capital to the kingdom at position $5$ with cost $6\cdot(5-1)=24$. Conquer the kingdom at position $6$ with cost $3\cdot(6-5)=3$. Conquer the kingdom at position $21$ with cost $3\cdot(21-5)=48$. Conquer the kingdom at position $30$ with cost $3\cdot(30-5)=75$. The total cost is $3+6+12+24+3+48+75=171$. You cannot get a lower cost than this.
t = int(input()) for qwer in range(t): n, a, b = map(int, input().split()) l = ["0"] + input().split() sp = list(map(int, l)) ind = 0 colvo = n for i in range(n - 1): c = sp[i + 1] - sp[ind] if c * (colvo - 1) * b > c * a: ind = i + 1 colvo -= 1 if a <= b: s = 0 for i in range(n): s += (sp[i + 1] - sp[i]) * (a + b) s -= (sp[-1] - sp[-2]) * a else: s = 0 for i in range(n): if i < ind: s += (sp[i + 1] - sp[i]) * (b + a) else: s += (sp[i + 1] - sp[ind]) * b print(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers. Consider a number axis. The capital of your empire is initially at $0$. There are $n$ unconquered kingdoms at positions $0<x_1<x_2<\ldots<x_n$. You want to conquer all other kingdoms. There are two actions available to you: You can change the location of your capital (let its current position be $c_1$) to any other conquered kingdom (let its position be $c_2$) at a cost of $a\cdot |c_1-c_2|$. From the current capital (let its current position be $c_1$) you can conquer an unconquered kingdom (let its position be $c_2$) at a cost of $b\cdot |c_1-c_2|$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $0$ or one of $x_1,x_2,\ldots,x_n$. Also note that conquering a kingdom does not change the position of your capital. Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The description of each test case follows. The first line of each test case contains $3$ integers $n$, $a$, and $b$ ($1 \leq n \leq 2 \cdot 10^5$; $1 \leq a,b \leq 10^5$). The second line of each test case contains $n$ integers $x_1, x_2, \ldots, x_n$ ($1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$). The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output a single integer β€” the minimum cost to conquer all kingdoms. -----Examples----- Input 4 5 2 7 3 5 12 13 21 5 6 3 1 5 6 21 30 2 9 3 10 15 11 27182 31415 16 18 33 98 874 989 4848 20458 34365 38117 72030 Output 173 171 75 3298918744 -----Note----- Here is an optimal sequence of moves for the second test case: Conquer the kingdom at position $1$ with cost $3\cdot(1-0)=3$. Move the capital to the kingdom at position $1$ with cost $6\cdot(1-0)=6$. Conquer the kingdom at position $5$ with cost $3\cdot(5-1)=12$. Move the capital to the kingdom at position $5$ with cost $6\cdot(5-1)=24$. Conquer the kingdom at position $6$ with cost $3\cdot(6-5)=3$. Conquer the kingdom at position $21$ with cost $3\cdot(21-5)=48$. Conquer the kingdom at position $30$ with cost $3\cdot(30-5)=75$. The total cost is $3+6+12+24+3+48+75=171$. You cannot get a lower cost than this.
t = int(input()) for _ in range(t): n, a, b = map(int, input().split()) x = list(map(int, input().split())) sumX = 0 for num in x: sumX += num sumPrev = 0 minimum = 2 * 10 ^ 13 + 1 for i in range(n - 1): if i != 0: sumPrev += x[i - 1] additional = a * x[i] - b * (sumPrev + x[i] * (n - i - 1)) if additional < minimum: minimum = additional print(min(b * sumX, b * sumX + minimum))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers. Consider a number axis. The capital of your empire is initially at $0$. There are $n$ unconquered kingdoms at positions $0<x_1<x_2<\ldots<x_n$. You want to conquer all other kingdoms. There are two actions available to you: You can change the location of your capital (let its current position be $c_1$) to any other conquered kingdom (let its position be $c_2$) at a cost of $a\cdot |c_1-c_2|$. From the current capital (let its current position be $c_1$) you can conquer an unconquered kingdom (let its position be $c_2$) at a cost of $b\cdot |c_1-c_2|$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $0$ or one of $x_1,x_2,\ldots,x_n$. Also note that conquering a kingdom does not change the position of your capital. Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The description of each test case follows. The first line of each test case contains $3$ integers $n$, $a$, and $b$ ($1 \leq n \leq 2 \cdot 10^5$; $1 \leq a,b \leq 10^5$). The second line of each test case contains $n$ integers $x_1, x_2, \ldots, x_n$ ($1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$). The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output a single integer β€” the minimum cost to conquer all kingdoms. -----Examples----- Input 4 5 2 7 3 5 12 13 21 5 6 3 1 5 6 21 30 2 9 3 10 15 11 27182 31415 16 18 33 98 874 989 4848 20458 34365 38117 72030 Output 173 171 75 3298918744 -----Note----- Here is an optimal sequence of moves for the second test case: Conquer the kingdom at position $1$ with cost $3\cdot(1-0)=3$. Move the capital to the kingdom at position $1$ with cost $6\cdot(1-0)=6$. Conquer the kingdom at position $5$ with cost $3\cdot(5-1)=12$. Move the capital to the kingdom at position $5$ with cost $6\cdot(5-1)=24$. Conquer the kingdom at position $6$ with cost $3\cdot(6-5)=3$. Conquer the kingdom at position $21$ with cost $3\cdot(21-5)=48$. Conquer the kingdom at position $30$ with cost $3\cdot(30-5)=75$. The total cost is $3+6+12+24+3+48+75=171$. You cannot get a lower cost than this.
t = int(input()) for i in range(t): n, a, b = map(int, input().split()) lst = list(map(int, input().split())) pre = [lst[0]] for j in range(1, n): pre.append(pre[-1] + lst[j]) ans = [] dif = [lst[0]] for j in range(1, n): dif.append(lst[j] - lst[j - 1]) ans.append(pre[-1] * b) predif = [dif[0]] for j in range(1, len(dif)): predif.append(predif[-1] + dif[j]) for j in range(n): q = (a + b) * predif[j] w = (pre[-1] - pre[j] - (n - j - 1) * lst[j]) * b ans.append(q + w) print(min(ans))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers. Consider a number axis. The capital of your empire is initially at $0$. There are $n$ unconquered kingdoms at positions $0<x_1<x_2<\ldots<x_n$. You want to conquer all other kingdoms. There are two actions available to you: You can change the location of your capital (let its current position be $c_1$) to any other conquered kingdom (let its position be $c_2$) at a cost of $a\cdot |c_1-c_2|$. From the current capital (let its current position be $c_1$) you can conquer an unconquered kingdom (let its position be $c_2$) at a cost of $b\cdot |c_1-c_2|$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $0$ or one of $x_1,x_2,\ldots,x_n$. Also note that conquering a kingdom does not change the position of your capital. Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The description of each test case follows. The first line of each test case contains $3$ integers $n$, $a$, and $b$ ($1 \leq n \leq 2 \cdot 10^5$; $1 \leq a,b \leq 10^5$). The second line of each test case contains $n$ integers $x_1, x_2, \ldots, x_n$ ($1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$). The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output a single integer β€” the minimum cost to conquer all kingdoms. -----Examples----- Input 4 5 2 7 3 5 12 13 21 5 6 3 1 5 6 21 30 2 9 3 10 15 11 27182 31415 16 18 33 98 874 989 4848 20458 34365 38117 72030 Output 173 171 75 3298918744 -----Note----- Here is an optimal sequence of moves for the second test case: Conquer the kingdom at position $1$ with cost $3\cdot(1-0)=3$. Move the capital to the kingdom at position $1$ with cost $6\cdot(1-0)=6$. Conquer the kingdom at position $5$ with cost $3\cdot(5-1)=12$. Move the capital to the kingdom at position $5$ with cost $6\cdot(5-1)=24$. Conquer the kingdom at position $6$ with cost $3\cdot(6-5)=3$. Conquer the kingdom at position $21$ with cost $3\cdot(21-5)=48$. Conquer the kingdom at position $30$ with cost $3\cdot(30-5)=75$. The total cost is $3+6+12+24+3+48+75=171$. You cannot get a lower cost than this.
for _ in range(int(input())): n, a, b = map(int, input().split(" ")) c = list(map(int, input().split(" "))) sc = sum(c) sci = 0 ans = b * sc for i in range(n): sc -= c[i] val = (a + b) * c[i] + b * (sc - c[i] * (len(c) - i - 1)) if val <= ans: ans = val print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR 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 ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers. Consider a number axis. The capital of your empire is initially at $0$. There are $n$ unconquered kingdoms at positions $0<x_1<x_2<\ldots<x_n$. You want to conquer all other kingdoms. There are two actions available to you: You can change the location of your capital (let its current position be $c_1$) to any other conquered kingdom (let its position be $c_2$) at a cost of $a\cdot |c_1-c_2|$. From the current capital (let its current position be $c_1$) you can conquer an unconquered kingdom (let its position be $c_2$) at a cost of $b\cdot |c_1-c_2|$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $0$ or one of $x_1,x_2,\ldots,x_n$. Also note that conquering a kingdom does not change the position of your capital. Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The description of each test case follows. The first line of each test case contains $3$ integers $n$, $a$, and $b$ ($1 \leq n \leq 2 \cdot 10^5$; $1 \leq a,b \leq 10^5$). The second line of each test case contains $n$ integers $x_1, x_2, \ldots, x_n$ ($1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$). The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output a single integer β€” the minimum cost to conquer all kingdoms. -----Examples----- Input 4 5 2 7 3 5 12 13 21 5 6 3 1 5 6 21 30 2 9 3 10 15 11 27182 31415 16 18 33 98 874 989 4848 20458 34365 38117 72030 Output 173 171 75 3298918744 -----Note----- Here is an optimal sequence of moves for the second test case: Conquer the kingdom at position $1$ with cost $3\cdot(1-0)=3$. Move the capital to the kingdom at position $1$ with cost $6\cdot(1-0)=6$. Conquer the kingdom at position $5$ with cost $3\cdot(5-1)=12$. Move the capital to the kingdom at position $5$ with cost $6\cdot(5-1)=24$. Conquer the kingdom at position $6$ with cost $3\cdot(6-5)=3$. Conquer the kingdom at position $21$ with cost $3\cdot(21-5)=48$. Conquer the kingdom at position $30$ with cost $3\cdot(30-5)=75$. The total cost is $3+6+12+24+3+48+75=171$. You cannot get a lower cost than this.
for _ in range(int(input())): n, a, b = map(int, input().split()) arr = [0] my = [int(i) for i in input().split()] arr.extend(my) tot = sum(arr) * b ans = 0 pre = 0 post = 1 for i in range(1, len(arr)): while post < i: save = (arr[post] - arr[pre]) * b * (len(arr) - i) ext = (arr[post] - arr[pre]) * a if save >= ext: tot -= save - ext pre = post post += 1 else: break print(tot)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers. Consider a number axis. The capital of your empire is initially at $0$. There are $n$ unconquered kingdoms at positions $0<x_1<x_2<\ldots<x_n$. You want to conquer all other kingdoms. There are two actions available to you: You can change the location of your capital (let its current position be $c_1$) to any other conquered kingdom (let its position be $c_2$) at a cost of $a\cdot |c_1-c_2|$. From the current capital (let its current position be $c_1$) you can conquer an unconquered kingdom (let its position be $c_2$) at a cost of $b\cdot |c_1-c_2|$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $0$ or one of $x_1,x_2,\ldots,x_n$. Also note that conquering a kingdom does not change the position of your capital. Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The description of each test case follows. The first line of each test case contains $3$ integers $n$, $a$, and $b$ ($1 \leq n \leq 2 \cdot 10^5$; $1 \leq a,b \leq 10^5$). The second line of each test case contains $n$ integers $x_1, x_2, \ldots, x_n$ ($1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$). The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output a single integer β€” the minimum cost to conquer all kingdoms. -----Examples----- Input 4 5 2 7 3 5 12 13 21 5 6 3 1 5 6 21 30 2 9 3 10 15 11 27182 31415 16 18 33 98 874 989 4848 20458 34365 38117 72030 Output 173 171 75 3298918744 -----Note----- Here is an optimal sequence of moves for the second test case: Conquer the kingdom at position $1$ with cost $3\cdot(1-0)=3$. Move the capital to the kingdom at position $1$ with cost $6\cdot(1-0)=6$. Conquer the kingdom at position $5$ with cost $3\cdot(5-1)=12$. Move the capital to the kingdom at position $5$ with cost $6\cdot(5-1)=24$. Conquer the kingdom at position $6$ with cost $3\cdot(6-5)=3$. Conquer the kingdom at position $21$ with cost $3\cdot(21-5)=48$. Conquer the kingdom at position $30$ with cost $3\cdot(30-5)=75$. The total cost is $3+6+12+24+3+48+75=171$. You cannot get a lower cost than this.
for i in range(int(input())): l = list(map(int, input().split())) n = l[0] a = l[1] b = l[2] l = [0] l.extend(list(map(int, input().split()))) l1 = [b * sum(l)] a1 = 0 b1 = 1 for j in range(n): l1.append(l1[j] + a * (l[j + 1] - l[j]) - (n - j - 1) * b * (l[j + 1] - l[j])) print(min(l1))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers. Consider a number axis. The capital of your empire is initially at $0$. There are $n$ unconquered kingdoms at positions $0<x_1<x_2<\ldots<x_n$. You want to conquer all other kingdoms. There are two actions available to you: You can change the location of your capital (let its current position be $c_1$) to any other conquered kingdom (let its position be $c_2$) at a cost of $a\cdot |c_1-c_2|$. From the current capital (let its current position be $c_1$) you can conquer an unconquered kingdom (let its position be $c_2$) at a cost of $b\cdot |c_1-c_2|$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $0$ or one of $x_1,x_2,\ldots,x_n$. Also note that conquering a kingdom does not change the position of your capital. Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The description of each test case follows. The first line of each test case contains $3$ integers $n$, $a$, and $b$ ($1 \leq n \leq 2 \cdot 10^5$; $1 \leq a,b \leq 10^5$). The second line of each test case contains $n$ integers $x_1, x_2, \ldots, x_n$ ($1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$). The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output a single integer β€” the minimum cost to conquer all kingdoms. -----Examples----- Input 4 5 2 7 3 5 12 13 21 5 6 3 1 5 6 21 30 2 9 3 10 15 11 27182 31415 16 18 33 98 874 989 4848 20458 34365 38117 72030 Output 173 171 75 3298918744 -----Note----- Here is an optimal sequence of moves for the second test case: Conquer the kingdom at position $1$ with cost $3\cdot(1-0)=3$. Move the capital to the kingdom at position $1$ with cost $6\cdot(1-0)=6$. Conquer the kingdom at position $5$ with cost $3\cdot(5-1)=12$. Move the capital to the kingdom at position $5$ with cost $6\cdot(5-1)=24$. Conquer the kingdom at position $6$ with cost $3\cdot(6-5)=3$. Conquer the kingdom at position $21$ with cost $3\cdot(21-5)=48$. Conquer the kingdom at position $30$ with cost $3\cdot(30-5)=75$. The total cost is $3+6+12+24+3+48+75=171$. You cannot get a lower cost than this.
def main(): T = int(input()) for _ in range(T): n, a, b = list(map(int, input().strip().split(" "))) v = list(map(int, input().strip().split(" "))) sum = 0 for i in range(len(v)): sum += v[i] res = 1e18 v.insert(0, 0) cur = 0 for i in range(0, n): cur = (a + b) * v[i] sum -= v[i] cur += (sum - (n - i) * v[i]) * b res = min(res, cur) print(res) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers. Consider a number axis. The capital of your empire is initially at $0$. There are $n$ unconquered kingdoms at positions $0<x_1<x_2<\ldots<x_n$. You want to conquer all other kingdoms. There are two actions available to you: You can change the location of your capital (let its current position be $c_1$) to any other conquered kingdom (let its position be $c_2$) at a cost of $a\cdot |c_1-c_2|$. From the current capital (let its current position be $c_1$) you can conquer an unconquered kingdom (let its position be $c_2$) at a cost of $b\cdot |c_1-c_2|$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $0$ or one of $x_1,x_2,\ldots,x_n$. Also note that conquering a kingdom does not change the position of your capital. Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The description of each test case follows. The first line of each test case contains $3$ integers $n$, $a$, and $b$ ($1 \leq n \leq 2 \cdot 10^5$; $1 \leq a,b \leq 10^5$). The second line of each test case contains $n$ integers $x_1, x_2, \ldots, x_n$ ($1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$). The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output a single integer β€” the minimum cost to conquer all kingdoms. -----Examples----- Input 4 5 2 7 3 5 12 13 21 5 6 3 1 5 6 21 30 2 9 3 10 15 11 27182 31415 16 18 33 98 874 989 4848 20458 34365 38117 72030 Output 173 171 75 3298918744 -----Note----- Here is an optimal sequence of moves for the second test case: Conquer the kingdom at position $1$ with cost $3\cdot(1-0)=3$. Move the capital to the kingdom at position $1$ with cost $6\cdot(1-0)=6$. Conquer the kingdom at position $5$ with cost $3\cdot(5-1)=12$. Move the capital to the kingdom at position $5$ with cost $6\cdot(5-1)=24$. Conquer the kingdom at position $6$ with cost $3\cdot(6-5)=3$. Conquer the kingdom at position $21$ with cost $3\cdot(21-5)=48$. Conquer the kingdom at position $30$ with cost $3\cdot(30-5)=75$. The total cost is $3+6+12+24+3+48+75=171$. You cannot get a lower cost than this.
for _ in range(int(input())): n, a, b = map(int, input().split()) l = list(map(int, input().split())) l = [0] + l ans = [(0) for i in range(n)] ans[0] = sum(l) * b for i in range(1, len(ans)): ans[i] = ans[i - 1] + (l[i] - l[i - 1]) * a - (n - i) * b * (l[i] - l[i - 1]) print(min(ans))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers. Consider a number axis. The capital of your empire is initially at $0$. There are $n$ unconquered kingdoms at positions $0<x_1<x_2<\ldots<x_n$. You want to conquer all other kingdoms. There are two actions available to you: You can change the location of your capital (let its current position be $c_1$) to any other conquered kingdom (let its position be $c_2$) at a cost of $a\cdot |c_1-c_2|$. From the current capital (let its current position be $c_1$) you can conquer an unconquered kingdom (let its position be $c_2$) at a cost of $b\cdot |c_1-c_2|$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $0$ or one of $x_1,x_2,\ldots,x_n$. Also note that conquering a kingdom does not change the position of your capital. Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The description of each test case follows. The first line of each test case contains $3$ integers $n$, $a$, and $b$ ($1 \leq n \leq 2 \cdot 10^5$; $1 \leq a,b \leq 10^5$). The second line of each test case contains $n$ integers $x_1, x_2, \ldots, x_n$ ($1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$). The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output a single integer β€” the minimum cost to conquer all kingdoms. -----Examples----- Input 4 5 2 7 3 5 12 13 21 5 6 3 1 5 6 21 30 2 9 3 10 15 11 27182 31415 16 18 33 98 874 989 4848 20458 34365 38117 72030 Output 173 171 75 3298918744 -----Note----- Here is an optimal sequence of moves for the second test case: Conquer the kingdom at position $1$ with cost $3\cdot(1-0)=3$. Move the capital to the kingdom at position $1$ with cost $6\cdot(1-0)=6$. Conquer the kingdom at position $5$ with cost $3\cdot(5-1)=12$. Move the capital to the kingdom at position $5$ with cost $6\cdot(5-1)=24$. Conquer the kingdom at position $6$ with cost $3\cdot(6-5)=3$. Conquer the kingdom at position $21$ with cost $3\cdot(21-5)=48$. Conquer the kingdom at position $30$ with cost $3\cdot(30-5)=75$. The total cost is $3+6+12+24+3+48+75=171$. You cannot get a lower cost than this.
def solve(): N, A, B = map(int, input().split()) x = list(map(int, input().split())) x.append(0) x.insert(0, 0) N += 1 COSTA = 0 COSTB = 0 COSTC = 0 ANS = 1 << 60 for i in range(N): COSTB += x[i] * B for i in range(0, N): COSTA = x[i] * A COSTB -= (N - i) * (x[i] - x[i - 1]) * B COSTC = x[i] * B ANS = min(ANS, COSTA + COSTB + COSTC) print(ANS) t = int(input()) while t > 0: solve() t -= 1
FUNC_DEF ASSIGN 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 NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers. Consider a number axis. The capital of your empire is initially at $0$. There are $n$ unconquered kingdoms at positions $0<x_1<x_2<\ldots<x_n$. You want to conquer all other kingdoms. There are two actions available to you: You can change the location of your capital (let its current position be $c_1$) to any other conquered kingdom (let its position be $c_2$) at a cost of $a\cdot |c_1-c_2|$. From the current capital (let its current position be $c_1$) you can conquer an unconquered kingdom (let its position be $c_2$) at a cost of $b\cdot |c_1-c_2|$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $0$ or one of $x_1,x_2,\ldots,x_n$. Also note that conquering a kingdom does not change the position of your capital. Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The description of each test case follows. The first line of each test case contains $3$ integers $n$, $a$, and $b$ ($1 \leq n \leq 2 \cdot 10^5$; $1 \leq a,b \leq 10^5$). The second line of each test case contains $n$ integers $x_1, x_2, \ldots, x_n$ ($1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$). The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output a single integer β€” the minimum cost to conquer all kingdoms. -----Examples----- Input 4 5 2 7 3 5 12 13 21 5 6 3 1 5 6 21 30 2 9 3 10 15 11 27182 31415 16 18 33 98 874 989 4848 20458 34365 38117 72030 Output 173 171 75 3298918744 -----Note----- Here is an optimal sequence of moves for the second test case: Conquer the kingdom at position $1$ with cost $3\cdot(1-0)=3$. Move the capital to the kingdom at position $1$ with cost $6\cdot(1-0)=6$. Conquer the kingdom at position $5$ with cost $3\cdot(5-1)=12$. Move the capital to the kingdom at position $5$ with cost $6\cdot(5-1)=24$. Conquer the kingdom at position $6$ with cost $3\cdot(6-5)=3$. Conquer the kingdom at position $21$ with cost $3\cdot(21-5)=48$. Conquer the kingdom at position $30$ with cost $3\cdot(30-5)=75$. The total cost is $3+6+12+24+3+48+75=171$. You cannot get a lower cost than this.
t = int(input()) for i in range(t): n, a, b = [int(i) for i in input().split()] positions = [int(i) for i in input().split()] s = sum(positions) finals = [] neg = 0 for i in range(len(positions)): neg += positions[i] cost = 0 cost += positions[i] * (a + b) cost += (s - neg - (n - i - 1) * positions[i]) * b finals.append(cost) finals.append(sum(positions) * b) print(min(finals))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers. Consider a number axis. The capital of your empire is initially at $0$. There are $n$ unconquered kingdoms at positions $0<x_1<x_2<\ldots<x_n$. You want to conquer all other kingdoms. There are two actions available to you: You can change the location of your capital (let its current position be $c_1$) to any other conquered kingdom (let its position be $c_2$) at a cost of $a\cdot |c_1-c_2|$. From the current capital (let its current position be $c_1$) you can conquer an unconquered kingdom (let its position be $c_2$) at a cost of $b\cdot |c_1-c_2|$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $0$ or one of $x_1,x_2,\ldots,x_n$. Also note that conquering a kingdom does not change the position of your capital. Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The description of each test case follows. The first line of each test case contains $3$ integers $n$, $a$, and $b$ ($1 \leq n \leq 2 \cdot 10^5$; $1 \leq a,b \leq 10^5$). The second line of each test case contains $n$ integers $x_1, x_2, \ldots, x_n$ ($1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$). The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output a single integer β€” the minimum cost to conquer all kingdoms. -----Examples----- Input 4 5 2 7 3 5 12 13 21 5 6 3 1 5 6 21 30 2 9 3 10 15 11 27182 31415 16 18 33 98 874 989 4848 20458 34365 38117 72030 Output 173 171 75 3298918744 -----Note----- Here is an optimal sequence of moves for the second test case: Conquer the kingdom at position $1$ with cost $3\cdot(1-0)=3$. Move the capital to the kingdom at position $1$ with cost $6\cdot(1-0)=6$. Conquer the kingdom at position $5$ with cost $3\cdot(5-1)=12$. Move the capital to the kingdom at position $5$ with cost $6\cdot(5-1)=24$. Conquer the kingdom at position $6$ with cost $3\cdot(6-5)=3$. Conquer the kingdom at position $21$ with cost $3\cdot(21-5)=48$. Conquer the kingdom at position $30$ with cost $3\cdot(30-5)=75$. The total cost is $3+6+12+24+3+48+75=171$. You cannot get a lower cost than this.
for _ in " " * int(input()): n, a, b = map(int, input().split()) x = list(map(int, input().split())) sf = [0] * n pr = [0] * n sf[-1] = x[-1] pr[0] = x[0] for i in range(1, n): pr[i] = pr[i - 1] + x[i] for i in range(n - 2, -1, -1): sf[i] = sf[i + 1] + x[i] mnc = b * sum(x) prc = 0 xc = 0 xi = -1 for i in range(n - 1): if xi == -1: temp = b * pr[i] else: temp = b * (pr[i] - pr[xi] - (i - xi) * xc) crc = prc + a * (x[i] - xc) + temp if crc + b * (sf[i + 1] - (n - i - 1) * x[i]) <= mnc: mnc = crc + b * (sf[i + 1] - (n - i - 1) * x[i]) prc = crc xc = x[i] xi = i print(mnc)
FOR VAR BIN_OP STRING FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers. Consider a number axis. The capital of your empire is initially at $0$. There are $n$ unconquered kingdoms at positions $0<x_1<x_2<\ldots<x_n$. You want to conquer all other kingdoms. There are two actions available to you: You can change the location of your capital (let its current position be $c_1$) to any other conquered kingdom (let its position be $c_2$) at a cost of $a\cdot |c_1-c_2|$. From the current capital (let its current position be $c_1$) you can conquer an unconquered kingdom (let its position be $c_2$) at a cost of $b\cdot |c_1-c_2|$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $0$ or one of $x_1,x_2,\ldots,x_n$. Also note that conquering a kingdom does not change the position of your capital. Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The description of each test case follows. The first line of each test case contains $3$ integers $n$, $a$, and $b$ ($1 \leq n \leq 2 \cdot 10^5$; $1 \leq a,b \leq 10^5$). The second line of each test case contains $n$ integers $x_1, x_2, \ldots, x_n$ ($1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$). The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output a single integer β€” the minimum cost to conquer all kingdoms. -----Examples----- Input 4 5 2 7 3 5 12 13 21 5 6 3 1 5 6 21 30 2 9 3 10 15 11 27182 31415 16 18 33 98 874 989 4848 20458 34365 38117 72030 Output 173 171 75 3298918744 -----Note----- Here is an optimal sequence of moves for the second test case: Conquer the kingdom at position $1$ with cost $3\cdot(1-0)=3$. Move the capital to the kingdom at position $1$ with cost $6\cdot(1-0)=6$. Conquer the kingdom at position $5$ with cost $3\cdot(5-1)=12$. Move the capital to the kingdom at position $5$ with cost $6\cdot(5-1)=24$. Conquer the kingdom at position $6$ with cost $3\cdot(6-5)=3$. Conquer the kingdom at position $21$ with cost $3\cdot(21-5)=48$. Conquer the kingdom at position $30$ with cost $3\cdot(30-5)=75$. The total cost is $3+6+12+24+3+48+75=171$. You cannot get a lower cost than this.
t = int(input()) while t: t -= 1 n, a, b = map(int, input().split()) x = [0] + [*map(int, input().split())] l = [sum(x) * b] for i in range(1, n): c = l[-1] + a * (x[i] - x[i - 1]) - (x[i] - x[i - 1]) * (n - i) * b if c > l[-1]: break l.append(c) print(l[-1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST BIN_OP FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers. Consider a number axis. The capital of your empire is initially at $0$. There are $n$ unconquered kingdoms at positions $0<x_1<x_2<\ldots<x_n$. You want to conquer all other kingdoms. There are two actions available to you: You can change the location of your capital (let its current position be $c_1$) to any other conquered kingdom (let its position be $c_2$) at a cost of $a\cdot |c_1-c_2|$. From the current capital (let its current position be $c_1$) you can conquer an unconquered kingdom (let its position be $c_2$) at a cost of $b\cdot |c_1-c_2|$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $0$ or one of $x_1,x_2,\ldots,x_n$. Also note that conquering a kingdom does not change the position of your capital. Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The description of each test case follows. The first line of each test case contains $3$ integers $n$, $a$, and $b$ ($1 \leq n \leq 2 \cdot 10^5$; $1 \leq a,b \leq 10^5$). The second line of each test case contains $n$ integers $x_1, x_2, \ldots, x_n$ ($1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$). The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output a single integer β€” the minimum cost to conquer all kingdoms. -----Examples----- Input 4 5 2 7 3 5 12 13 21 5 6 3 1 5 6 21 30 2 9 3 10 15 11 27182 31415 16 18 33 98 874 989 4848 20458 34365 38117 72030 Output 173 171 75 3298918744 -----Note----- Here is an optimal sequence of moves for the second test case: Conquer the kingdom at position $1$ with cost $3\cdot(1-0)=3$. Move the capital to the kingdom at position $1$ with cost $6\cdot(1-0)=6$. Conquer the kingdom at position $5$ with cost $3\cdot(5-1)=12$. Move the capital to the kingdom at position $5$ with cost $6\cdot(5-1)=24$. Conquer the kingdom at position $6$ with cost $3\cdot(6-5)=3$. Conquer the kingdom at position $21$ with cost $3\cdot(21-5)=48$. Conquer the kingdom at position $30$ with cost $3\cdot(30-5)=75$. The total cost is $3+6+12+24+3+48+75=171$. You cannot get a lower cost than this.
for _ in range(int(input())): n, a, b = map(int, input().split()) v = list(map(int, input().split())) v = [0] + v s = sum(v) res = int(1e18) n += 1 for i in range(n): curr = (a + b) * v[i] s -= v[i] curr += (s - (n - i - 1) * v[i]) * b res = min(res, curr) print(res)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers. Consider a number axis. The capital of your empire is initially at $0$. There are $n$ unconquered kingdoms at positions $0<x_1<x_2<\ldots<x_n$. You want to conquer all other kingdoms. There are two actions available to you: You can change the location of your capital (let its current position be $c_1$) to any other conquered kingdom (let its position be $c_2$) at a cost of $a\cdot |c_1-c_2|$. From the current capital (let its current position be $c_1$) you can conquer an unconquered kingdom (let its position be $c_2$) at a cost of $b\cdot |c_1-c_2|$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $0$ or one of $x_1,x_2,\ldots,x_n$. Also note that conquering a kingdom does not change the position of your capital. Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The description of each test case follows. The first line of each test case contains $3$ integers $n$, $a$, and $b$ ($1 \leq n \leq 2 \cdot 10^5$; $1 \leq a,b \leq 10^5$). The second line of each test case contains $n$ integers $x_1, x_2, \ldots, x_n$ ($1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$). The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output a single integer β€” the minimum cost to conquer all kingdoms. -----Examples----- Input 4 5 2 7 3 5 12 13 21 5 6 3 1 5 6 21 30 2 9 3 10 15 11 27182 31415 16 18 33 98 874 989 4848 20458 34365 38117 72030 Output 173 171 75 3298918744 -----Note----- Here is an optimal sequence of moves for the second test case: Conquer the kingdom at position $1$ with cost $3\cdot(1-0)=3$. Move the capital to the kingdom at position $1$ with cost $6\cdot(1-0)=6$. Conquer the kingdom at position $5$ with cost $3\cdot(5-1)=12$. Move the capital to the kingdom at position $5$ with cost $6\cdot(5-1)=24$. Conquer the kingdom at position $6$ with cost $3\cdot(6-5)=3$. Conquer the kingdom at position $21$ with cost $3\cdot(21-5)=48$. Conquer the kingdom at position $30$ with cost $3\cdot(30-5)=75$. The total cost is $3+6+12+24+3+48+75=171$. You cannot get a lower cost than this.
t = int(input()) for i in range(t): n, a, b = map(int, input().split()) ls = list(map(int, input().split())) ls.insert(0, 0) n += 1 sum = 0 x = ls[n - 1] lastCost = x * (a + b) for j in range(n - 1, -1, -1): x = ls[j] cost = x * (a + b) + b * (sum - (n - j - 1) * x) if cost > lastCost: break else: lastCost = cost sum += x print(str(lastCost))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN 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 NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers. Consider a number axis. The capital of your empire is initially at $0$. There are $n$ unconquered kingdoms at positions $0<x_1<x_2<\ldots<x_n$. You want to conquer all other kingdoms. There are two actions available to you: You can change the location of your capital (let its current position be $c_1$) to any other conquered kingdom (let its position be $c_2$) at a cost of $a\cdot |c_1-c_2|$. From the current capital (let its current position be $c_1$) you can conquer an unconquered kingdom (let its position be $c_2$) at a cost of $b\cdot |c_1-c_2|$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $0$ or one of $x_1,x_2,\ldots,x_n$. Also note that conquering a kingdom does not change the position of your capital. Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The description of each test case follows. The first line of each test case contains $3$ integers $n$, $a$, and $b$ ($1 \leq n \leq 2 \cdot 10^5$; $1 \leq a,b \leq 10^5$). The second line of each test case contains $n$ integers $x_1, x_2, \ldots, x_n$ ($1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$). The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output a single integer β€” the minimum cost to conquer all kingdoms. -----Examples----- Input 4 5 2 7 3 5 12 13 21 5 6 3 1 5 6 21 30 2 9 3 10 15 11 27182 31415 16 18 33 98 874 989 4848 20458 34365 38117 72030 Output 173 171 75 3298918744 -----Note----- Here is an optimal sequence of moves for the second test case: Conquer the kingdom at position $1$ with cost $3\cdot(1-0)=3$. Move the capital to the kingdom at position $1$ with cost $6\cdot(1-0)=6$. Conquer the kingdom at position $5$ with cost $3\cdot(5-1)=12$. Move the capital to the kingdom at position $5$ with cost $6\cdot(5-1)=24$. Conquer the kingdom at position $6$ with cost $3\cdot(6-5)=3$. Conquer the kingdom at position $21$ with cost $3\cdot(21-5)=48$. Conquer the kingdom at position $30$ with cost $3\cdot(30-5)=75$. The total cost is $3+6+12+24+3+48+75=171$. You cannot get a lower cost than this.
for _ in range(int(input())): n, a, b = [int(x) for x in input().split()] v = [int(x) for x in input().split()] v = [0] + v con = [0] * (n + 1) for i in range(1, n + 1): con[i] = b * (v[i] - 0) for i in range(n - 1, -1, -1): con[i] += con[i + 1] cost = 0 val = 0 ans = 10**18 for i in range(n): cost = v[i] * a if i != 0: val += (v[i] - v[i - 1]) * b cost += val rem = n - i rem *= v[i] * b cost += con[i + 1] - rem ans = min(ans, cost) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR 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 BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers. Consider a number axis. The capital of your empire is initially at $0$. There are $n$ unconquered kingdoms at positions $0<x_1<x_2<\ldots<x_n$. You want to conquer all other kingdoms. There are two actions available to you: You can change the location of your capital (let its current position be $c_1$) to any other conquered kingdom (let its position be $c_2$) at a cost of $a\cdot |c_1-c_2|$. From the current capital (let its current position be $c_1$) you can conquer an unconquered kingdom (let its position be $c_2$) at a cost of $b\cdot |c_1-c_2|$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $0$ or one of $x_1,x_2,\ldots,x_n$. Also note that conquering a kingdom does not change the position of your capital. Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The description of each test case follows. The first line of each test case contains $3$ integers $n$, $a$, and $b$ ($1 \leq n \leq 2 \cdot 10^5$; $1 \leq a,b \leq 10^5$). The second line of each test case contains $n$ integers $x_1, x_2, \ldots, x_n$ ($1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$). The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output a single integer β€” the minimum cost to conquer all kingdoms. -----Examples----- Input 4 5 2 7 3 5 12 13 21 5 6 3 1 5 6 21 30 2 9 3 10 15 11 27182 31415 16 18 33 98 874 989 4848 20458 34365 38117 72030 Output 173 171 75 3298918744 -----Note----- Here is an optimal sequence of moves for the second test case: Conquer the kingdom at position $1$ with cost $3\cdot(1-0)=3$. Move the capital to the kingdom at position $1$ with cost $6\cdot(1-0)=6$. Conquer the kingdom at position $5$ with cost $3\cdot(5-1)=12$. Move the capital to the kingdom at position $5$ with cost $6\cdot(5-1)=24$. Conquer the kingdom at position $6$ with cost $3\cdot(6-5)=3$. Conquer the kingdom at position $21$ with cost $3\cdot(21-5)=48$. Conquer the kingdom at position $30$ with cost $3\cdot(30-5)=75$. The total cost is $3+6+12+24+3+48+75=171$. You cannot get a lower cost than this.
def solve(): n, a, b = list(map(int, input().split())) arr = [0] + list(map(int, input().split())) second_arr = [0] + [abs(arr[i] - arr[i + 1]) for i in range(n)] s = sum(arr) iter_sum = 0 min_ = float("inf") n = len(arr) length = n for i in range(length): s -= arr[i] n -= 1 value = abs(s - n * arr[i]) iter_sum += second_arr[i] min_ = min(min_, (a + b) * iter_sum + b * value) return min_ for _ in range(int(input())): print(solve())
FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers. Consider a number axis. The capital of your empire is initially at $0$. There are $n$ unconquered kingdoms at positions $0<x_1<x_2<\ldots<x_n$. You want to conquer all other kingdoms. There are two actions available to you: You can change the location of your capital (let its current position be $c_1$) to any other conquered kingdom (let its position be $c_2$) at a cost of $a\cdot |c_1-c_2|$. From the current capital (let its current position be $c_1$) you can conquer an unconquered kingdom (let its position be $c_2$) at a cost of $b\cdot |c_1-c_2|$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $0$ or one of $x_1,x_2,\ldots,x_n$. Also note that conquering a kingdom does not change the position of your capital. Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The description of each test case follows. The first line of each test case contains $3$ integers $n$, $a$, and $b$ ($1 \leq n \leq 2 \cdot 10^5$; $1 \leq a,b \leq 10^5$). The second line of each test case contains $n$ integers $x_1, x_2, \ldots, x_n$ ($1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$). The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output a single integer β€” the minimum cost to conquer all kingdoms. -----Examples----- Input 4 5 2 7 3 5 12 13 21 5 6 3 1 5 6 21 30 2 9 3 10 15 11 27182 31415 16 18 33 98 874 989 4848 20458 34365 38117 72030 Output 173 171 75 3298918744 -----Note----- Here is an optimal sequence of moves for the second test case: Conquer the kingdom at position $1$ with cost $3\cdot(1-0)=3$. Move the capital to the kingdom at position $1$ with cost $6\cdot(1-0)=6$. Conquer the kingdom at position $5$ with cost $3\cdot(5-1)=12$. Move the capital to the kingdom at position $5$ with cost $6\cdot(5-1)=24$. Conquer the kingdom at position $6$ with cost $3\cdot(6-5)=3$. Conquer the kingdom at position $21$ with cost $3\cdot(21-5)=48$. Conquer the kingdom at position $30$ with cost $3\cdot(30-5)=75$. The total cost is $3+6+12+24+3+48+75=171$. You cannot get a lower cost than this.
for _ in range(int(input())): n, a, b = map(int, input().split()) array = list(map(int, input().split())) sm = sum(array) sums = [sm] for i in array: sm -= i sums.append(sm) mn = sums[0] * b for i in range(n): tp = array[i] * a tp += (sums[i + 1] - (n - i - 2) * array[i]) * b mn = min(mn, tp) print(mn)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers. Consider a number axis. The capital of your empire is initially at $0$. There are $n$ unconquered kingdoms at positions $0<x_1<x_2<\ldots<x_n$. You want to conquer all other kingdoms. There are two actions available to you: You can change the location of your capital (let its current position be $c_1$) to any other conquered kingdom (let its position be $c_2$) at a cost of $a\cdot |c_1-c_2|$. From the current capital (let its current position be $c_1$) you can conquer an unconquered kingdom (let its position be $c_2$) at a cost of $b\cdot |c_1-c_2|$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $0$ or one of $x_1,x_2,\ldots,x_n$. Also note that conquering a kingdom does not change the position of your capital. Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The description of each test case follows. The first line of each test case contains $3$ integers $n$, $a$, and $b$ ($1 \leq n \leq 2 \cdot 10^5$; $1 \leq a,b \leq 10^5$). The second line of each test case contains $n$ integers $x_1, x_2, \ldots, x_n$ ($1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$). The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output a single integer β€” the minimum cost to conquer all kingdoms. -----Examples----- Input 4 5 2 7 3 5 12 13 21 5 6 3 1 5 6 21 30 2 9 3 10 15 11 27182 31415 16 18 33 98 874 989 4848 20458 34365 38117 72030 Output 173 171 75 3298918744 -----Note----- Here is an optimal sequence of moves for the second test case: Conquer the kingdom at position $1$ with cost $3\cdot(1-0)=3$. Move the capital to the kingdom at position $1$ with cost $6\cdot(1-0)=6$. Conquer the kingdom at position $5$ with cost $3\cdot(5-1)=12$. Move the capital to the kingdom at position $5$ with cost $6\cdot(5-1)=24$. Conquer the kingdom at position $6$ with cost $3\cdot(6-5)=3$. Conquer the kingdom at position $21$ with cost $3\cdot(21-5)=48$. Conquer the kingdom at position $30$ with cost $3\cdot(30-5)=75$. The total cost is $3+6+12+24+3+48+75=171$. You cannot get a lower cost than this.
iter = int(input()) for _ in range(iter): lst = list(map(int, input().split())) kingdoms = list(map(int, input().split())) min_cost = 0 prefix = [0] * lst[0] prefix[0] = kingdoms[0] for i in range(1, lst[0]): prefix[i] += kingdoms[i] + prefix[i - 1] min_cost = lst[2] * prefix[-1] for i in range(lst[0] - 1): curr_cost = kingdoms[0] * (lst[1] + lst[2]) curr_cost += (kingdoms[i] - kingdoms[0]) * (lst[2] + lst[1]) curr_cost += -kingdoms[i] * lst[2] * (lst[0] - 1 - i) curr_cost += (prefix[-1] - prefix[i]) * lst[2] min_cost = min(curr_cost, min_cost) print(min_cost)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers. Consider a number axis. The capital of your empire is initially at $0$. There are $n$ unconquered kingdoms at positions $0<x_1<x_2<\ldots<x_n$. You want to conquer all other kingdoms. There are two actions available to you: You can change the location of your capital (let its current position be $c_1$) to any other conquered kingdom (let its position be $c_2$) at a cost of $a\cdot |c_1-c_2|$. From the current capital (let its current position be $c_1$) you can conquer an unconquered kingdom (let its position be $c_2$) at a cost of $b\cdot |c_1-c_2|$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $0$ or one of $x_1,x_2,\ldots,x_n$. Also note that conquering a kingdom does not change the position of your capital. Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The description of each test case follows. The first line of each test case contains $3$ integers $n$, $a$, and $b$ ($1 \leq n \leq 2 \cdot 10^5$; $1 \leq a,b \leq 10^5$). The second line of each test case contains $n$ integers $x_1, x_2, \ldots, x_n$ ($1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$). The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output a single integer β€” the minimum cost to conquer all kingdoms. -----Examples----- Input 4 5 2 7 3 5 12 13 21 5 6 3 1 5 6 21 30 2 9 3 10 15 11 27182 31415 16 18 33 98 874 989 4848 20458 34365 38117 72030 Output 173 171 75 3298918744 -----Note----- Here is an optimal sequence of moves for the second test case: Conquer the kingdom at position $1$ with cost $3\cdot(1-0)=3$. Move the capital to the kingdom at position $1$ with cost $6\cdot(1-0)=6$. Conquer the kingdom at position $5$ with cost $3\cdot(5-1)=12$. Move the capital to the kingdom at position $5$ with cost $6\cdot(5-1)=24$. Conquer the kingdom at position $6$ with cost $3\cdot(6-5)=3$. Conquer the kingdom at position $21$ with cost $3\cdot(21-5)=48$. Conquer the kingdom at position $30$ with cost $3\cdot(30-5)=75$. The total cost is $3+6+12+24+3+48+75=171$. You cannot get a lower cost than this.
def solve(n, a, b, xs): S = sum(xs) res = b * S for i in range(n): S -= xs[i] tmp = a * xs[i] + b * (S - (n - i - 2) * xs[i]) res = min(res, tmp) return res for _ in range(int(input())): n, a, b = map(int, input().split()) xs = [*map(int, input().split())] res = solve(n, a, b, xs) print(res)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers. Consider a number axis. The capital of your empire is initially at $0$. There are $n$ unconquered kingdoms at positions $0<x_1<x_2<\ldots<x_n$. You want to conquer all other kingdoms. There are two actions available to you: You can change the location of your capital (let its current position be $c_1$) to any other conquered kingdom (let its position be $c_2$) at a cost of $a\cdot |c_1-c_2|$. From the current capital (let its current position be $c_1$) you can conquer an unconquered kingdom (let its position be $c_2$) at a cost of $b\cdot |c_1-c_2|$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $0$ or one of $x_1,x_2,\ldots,x_n$. Also note that conquering a kingdom does not change the position of your capital. Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The description of each test case follows. The first line of each test case contains $3$ integers $n$, $a$, and $b$ ($1 \leq n \leq 2 \cdot 10^5$; $1 \leq a,b \leq 10^5$). The second line of each test case contains $n$ integers $x_1, x_2, \ldots, x_n$ ($1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$). The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output a single integer β€” the minimum cost to conquer all kingdoms. -----Examples----- Input 4 5 2 7 3 5 12 13 21 5 6 3 1 5 6 21 30 2 9 3 10 15 11 27182 31415 16 18 33 98 874 989 4848 20458 34365 38117 72030 Output 173 171 75 3298918744 -----Note----- Here is an optimal sequence of moves for the second test case: Conquer the kingdom at position $1$ with cost $3\cdot(1-0)=3$. Move the capital to the kingdom at position $1$ with cost $6\cdot(1-0)=6$. Conquer the kingdom at position $5$ with cost $3\cdot(5-1)=12$. Move the capital to the kingdom at position $5$ with cost $6\cdot(5-1)=24$. Conquer the kingdom at position $6$ with cost $3\cdot(6-5)=3$. Conquer the kingdom at position $21$ with cost $3\cdot(21-5)=48$. Conquer the kingdom at position $30$ with cost $3\cdot(30-5)=75$. The total cost is $3+6+12+24+3+48+75=171$. You cannot get a lower cost than this.
t = int(input()) while t > 0: c = input().split() n, a, b = int(c[0]), int(c[1]), int(c[2]) x = input().split() for i in range(n): x[i] = int(x[i]) x = [0] + x ans = 0 cur = 0 ind = 0 for i in range(1, n + 1): while ind < i - 1: if b * (x[ind + 1] - x[ind]) * (n + 1 - i) >= a * (x[ind + 1] - x[ind]): ans += a * (x[ind + 1] - x[ind]) ind += 1 else: break ans += b * (x[i] - x[ind]) print(ans) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
There are n people and k keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key and then go to the office. Once a key is taken by somebody, it couldn't be taken by anybody else. You are to determine the minimum time needed for all n people to get to the office with keys. Assume that people move a unit distance per 1 second. If two people reach a key at the same time, only one of them can take the key. A person can pass through a point with a key without taking it. Input The first line contains three integers n, k and p (1 ≀ n ≀ 1 000, n ≀ k ≀ 2 000, 1 ≀ p ≀ 109) β€” the number of people, the number of keys and the office location. The second line contains n distinct integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” positions in which people are located initially. The positions are given in arbitrary order. The third line contains k distinct integers b1, b2, ..., bk (1 ≀ bj ≀ 109) β€” positions of the keys. The positions are given in arbitrary order. Note that there can't be more than one person or more than one key in the same point. A person and a key can be located in the same point. Output Print the minimum time (in seconds) needed for all n to reach the office with keys. Examples Input 2 4 50 20 100 60 10 40 80 Output 50 Input 1 2 10 11 15 7 Output 7 Note In the first example the person located at point 20 should take the key located at point 40 and go with it to the office located at point 50. He spends 30 seconds. The person located at point 100 can take the key located at point 80 and go to the office with it. He spends 50 seconds. Thus, after 50 seconds everybody is in office with keys.
n, k, p = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) a.sort() b.sort() def cost(j): if j < 0 or j + n - 1 >= k: return 1000000000000 maximum = 0 for i in range(n): t = abs(a[i] - b[i + j]) + abs(b[i + j] - p) maximum = max(maximum, t) return maximum l = 0 r = k - n while l < r: mid = (l + r) // 2 cost1 = cost(mid) cost1r = cost(mid + 1) if cost1 < cost1r: r = mid else: l = mid + 1 print(cost(l))
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_DEF IF VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
There are n people and k keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key and then go to the office. Once a key is taken by somebody, it couldn't be taken by anybody else. You are to determine the minimum time needed for all n people to get to the office with keys. Assume that people move a unit distance per 1 second. If two people reach a key at the same time, only one of them can take the key. A person can pass through a point with a key without taking it. Input The first line contains three integers n, k and p (1 ≀ n ≀ 1 000, n ≀ k ≀ 2 000, 1 ≀ p ≀ 109) β€” the number of people, the number of keys and the office location. The second line contains n distinct integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” positions in which people are located initially. The positions are given in arbitrary order. The third line contains k distinct integers b1, b2, ..., bk (1 ≀ bj ≀ 109) β€” positions of the keys. The positions are given in arbitrary order. Note that there can't be more than one person or more than one key in the same point. A person and a key can be located in the same point. Output Print the minimum time (in seconds) needed for all n to reach the office with keys. Examples Input 2 4 50 20 100 60 10 40 80 Output 50 Input 1 2 10 11 15 7 Output 7 Note In the first example the person located at point 20 should take the key located at point 40 and go with it to the office located at point 50. He spends 30 seconds. The person located at point 100 can take the key located at point 80 and go to the office with it. He spends 50 seconds. Thus, after 50 seconds everybody is in office with keys.
read = lambda: map(int, input().split()) n, k, p = read() a, b = sorted(read()), sorted(read()) print( min( max(abs(b[i + d] - a[i]) + abs(b[i + d] - p) for i in range(n)) for d in range(k - n + 1) ) )
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER
There are n people and k keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key and then go to the office. Once a key is taken by somebody, it couldn't be taken by anybody else. You are to determine the minimum time needed for all n people to get to the office with keys. Assume that people move a unit distance per 1 second. If two people reach a key at the same time, only one of them can take the key. A person can pass through a point with a key without taking it. Input The first line contains three integers n, k and p (1 ≀ n ≀ 1 000, n ≀ k ≀ 2 000, 1 ≀ p ≀ 109) β€” the number of people, the number of keys and the office location. The second line contains n distinct integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” positions in which people are located initially. The positions are given in arbitrary order. The third line contains k distinct integers b1, b2, ..., bk (1 ≀ bj ≀ 109) β€” positions of the keys. The positions are given in arbitrary order. Note that there can't be more than one person or more than one key in the same point. A person and a key can be located in the same point. Output Print the minimum time (in seconds) needed for all n to reach the office with keys. Examples Input 2 4 50 20 100 60 10 40 80 Output 50 Input 1 2 10 11 15 7 Output 7 Note In the first example the person located at point 20 should take the key located at point 40 and go with it to the office located at point 50. He spends 30 seconds. The person located at point 100 can take the key located at point 80 and go to the office with it. He spends 50 seconds. Thus, after 50 seconds everybody is in office with keys.
n, k, p = [int(i) for i in input().split()] A = [int(i) for i in input().split()] B = [int(i) for i in input().split()] A.sort() B.sort() ans = 10**18 for start in range(k): curmax = 0 curans = 10**18 if start + n > k: continue for i in range(n): curmax = max(curmax, abs(A[i] - B[start + i]) + abs(B[start + i] - p)) ans = min(ans, curmax) print(ans)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER IF BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR