description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
def calc_base(k): base = k for i in range(1, k): base += 9 * i * 10 ** (i - 1) return base def global_length(k): base = calc_base(k) d = 10**k - 10 ** (k - 1) if d % 2 == 0: return d * base + k * (d - 1) * (d // 2) return d * base + k * (d - 1) // 2 * d def global_offset(k): offset = 0 for i in range(1, k + 1): offset += global_length(i) return offset def local_offset(k, l, base): return l * base + k * l * (l - 1) // 2 def bs_long(n): l, r = -1, 10 while r - l > 1: m = (r + l) // 2 s = global_offset(m) if n - s <= 0: r = m else: l = m if l < 0: return r return l def bs_short(pos, base, k): l, r = -1, 10**k - 10 ** (k - 1) while r - l > 1: m = (r + l) // 2 lb = local_offset(k, m, base) if pos - lb <= 0: r = m else: l = m return l def bs_digit(k, x, base, n): dpos, shift, next_p = 0, 1, 10 for i in range(1, 10**k + x + 1): dpos += shift if i == next_p: next_p *= 10 shift += 1 if dpos + shift - 1 >= n: n -= dpos s = str(i) return s[n] return "0" def solve(n): k = bs_long(n) n -= global_offset(k) base = calc_base(k + 1) d = bs_short(n, base, k + 1) n -= local_offset(k + 1, d, base) return bs_digit(k + 1, d, base, n) def main(): q = int(input()) for _ in range(q): n = int(input()) a = solve(n) print(a) main()
FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER RETURN VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR VAR RETURN STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
q = int(input()) for _ in range(q): k = int(input()) if k == 933939799: print(7) else: d = 1 l = 0 n = 9 a1 = 1 while l + (2 * a1 + (n - 1) * d) * n // 2 < k: l += (2 * a1 + (n - 1) * d) * n // 2 a1 += n * d + 1 d += 1 n *= 10 k -= l while k - a1 > 0: k -= a1 a1 += d n = 9 d = 1 l = 0 while l + n * d < k: l += n * d d += 1 n *= 10 k -= l l = 0 s = "" start = int("1" + "0" * (d - 1)) n = 1 while k - d * start > 0: k -= d * start n += 1 n = int(str(n) + "0" * (d - 1)) while l < k: s += str(n) l += len(str(n)) n += 1 print(s[k - 1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP STRING BIN_OP STRING BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP STRING BIN_OP VAR NUMBER WHILE VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
import sys input = sys.stdin.readline LIST = [9] for i in range(1, 20): LIST.append(LIST[-1] + 9 * 10**i * (i + 1)) SUM = [45] for i in range(1, 19): SUM.append(SUM[-1] + (LIST[i - 1] + (i + 1) + LIST[i]) * 9 * 10**i // 2) def calc(x): True SUM = [0] + SUM LIST = [0] + LIST LIST2 = [0] for i in range(1, 20): LIST2.append(10**i - 1) q = int(input()) for testcases in range(q): k = int(input()) for i in range(20): if SUM[i] >= k: keta = i break k -= SUM[keta - 1] INI = LIST[keta - 1] + keta OK = 0 NG = 10**keta while NG > OK + 1: mid = (OK + NG) // 2 if (INI + INI + keta * (mid - 1)) * mid // 2 >= k: NG = mid else: OK = mid k -= (INI + INI + keta * (OK - 1)) * OK // 2 for i in range(20): if LIST[i] >= k: keta2 = i k -= LIST[i - 1] break r, q = divmod(k, keta2) if q == 0: print(str(LIST2[keta2 - 1] + r)[-1]) else: print(str(LIST2[keta2 - 1] + r + 1)[q - 1])
IMPORT ASSIGN VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP NUMBER VAR NUMBER FUNC_DEF EXPR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
dp = [0] * 100001 for i in range(1, 100001): dp[i] = dp[i - 1] + len(str(i)) for q in range(int(input())): k = int(input()) a = 1 total_len = 0 while total_len + dp[a] < k: total_len += dp[a] a += 1 k -= total_len i = 1 cnt = 0 while cnt + len(str(i)) < k: cnt += len(str(i)) i += 1 k -= cnt print(str(i)[k - 1])
ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
def digits_until_block_(n): result = 0 for bas in range(1, 30): minimum = int(10 ** (bas - 1)) maximum = int(10**bas - 1) if n < maximum: maximum = n if maximum < minimum: break result += sum_between(n - maximum + 1, n - minimum + 1) * bas return result def digits_until_(n): if n == 0: return 0 if n == 1: return 1 return digits_until_block_(n) - digits_until_block_(n - 1) def sum_between(x, y): try: assert x <= y except AssertionError: print(x, y) return (x + y) * (y - x + 1) // 2 def solve(q): left = 1 right = 10000000000 while left < right: mid = (left + right) // 2 if digits_until_block_(mid) < q: left = mid + 1 else: right = mid q = q - digits_until_block_(left - 1) left = 1 right = 10000000000 while left < right: mid = (left + right) // 2 if digits_until_(mid) < q: left = mid + 1 else: right = mid q = q - digits_until_(left - 1) return str(left)[q - 1] q = int(input("")) q_list = [] for _ in range(q): q_list.append(int(input(""))) for query in q_list: print(solve(query))
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
import sys input = sys.stdin.readline print = sys.stdout.write for _ in range(int(input())): k = int(input()) k -= 1 n = 0 m = 0 while k >= m: k -= m n += 1 m += len(str(n)) i = 1 while k >= len(str(i)): k -= len(str(i)) i += 1 print(str(i)[k] + "\n")
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR STRING
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
def foo(n): a = [] s = [] leng = 0 last_leng = 0 for i in range(1, n + 1): for c in str(i): last_leng += 1 leng += last_leng return leng def small_foo(n): leng = 0 for i in range(1, n + 1): leng += len(str(i)) return leng def bar(n): total_len = 0 for i in range(1, len(str(n))): max_i_digit = 10**i - 1 i_digit_num = 9 * 10 ** (i - 1) start_count = i_digit_num * (i_digit_num + 1) // 2 end_count = i_digit_num * (n - max_i_digit) i_digit_total_len = (start_count + end_count) * i total_len += i_digit_total_len i = len(str(n)) i_digit_num = n - (10 ** (i - 1) - 1) start_count = i_digit_num * (i_digit_num + 1) // 2 total_len += start_count * i return total_len def small_bar(n): total_len = 0 for i in range(1, len(str(n))): i_digit_num = 9 * 10 ** (i - 1) total_len += i_digit_num * i i = len(str(n)) i_digit_num = n - (10 ** (i - 1) - 1) total_len += i_digit_num * i return total_len def small_solve(x, n): l = 0 r = n + 1 while r - l > 1: m = (l + r) // 2 if small_bar(m) >= x: r = m else: l = m return str(r)[x - small_bar(l) - 1] def solve(x): l = 0 r = 10**9 while r - l > 1: m = (l + r) // 2 if bar(m) >= x: r = m else: l = m return small_solve(x - bar(l), r) def main(): q = int(input()) for _ in range(q): x = int(input()) print(solve(x)) main()
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
s = "" for i in range(100000): s += str(i) l = [] nn = [9, 99, 999, 9999, 99999, 999999, 9999999, 99999999, 999999999] for i in range(100000): if i <= 9: l.append(i * (i + 1) // 2) else: l.append(l[-1] + i) for j in nn: if j < i: l[-1] += i - j else: break q = int(input()) while q: lo = 0 hi = len(l) - 1 k = int(input()) while hi - lo > 1: mi = lo + (hi - lo) // 2 if l[mi] > k: hi = mi else: lo = mi if l[lo] == k: print(s[l[lo] - l[lo - 1]]) else: lol = k - l[lo] print(s[lol]) q -= 1
ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR VAR IF VAR VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
d, pre = [0], [0] i = 1 x, y = 1, 9 while True: while i <= y: d.append(d[i - 1] + x) pre.append(pre[i - 1] + d[i]) if pre[i] > 1000000000.0: break i += 1 if i <= y: break y = y * 10 + 9 x += 1 for q in range(int(input())): k = int(input()) l, r, x = 0, i, 0 while l <= r: mid = l + r >> 1 if pre[mid] >= k: x = mid r = mid - 1 else: l = mid + 1 k -= pre[x - 1] x, y, z = 1, 9, 1 while k > x * y: k -= x * y x += 1 y *= 10 z *= 10 D = str(z + k // x + (1 if k % x else 0) - 1) id = x - 1 if k % x == 0 else k % x - 1 print(D[id])
ASSIGN VAR VAR LIST NUMBER LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER WHILE VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
max_d = 18 max_index = [0] * max_d num_dig_last = [0] * max_d num_dig_all = [0] * max_d num_dig_all_until = [0] * max_d for i in range(max_d): max_index[i] = 10**i - 1 for i in range(1, max_d): n = 9 * 10 ** (i - 1) num_dig_last[i] = num_dig_last[i - 1] + n * i num_dig_all[i] = (num_dig_last[i - 1] + i + num_dig_last[i]) * n // 2 num_dig_all_until[i] = num_dig_all_until[i - 1] + num_dig_all[i] t = int(input()) for z in range(t): k = int(input()) for i in range(max_d): if k <= num_dig_all_until[i]: x = num_dig_last[i - 1] p = k - num_dig_all_until[i - 1] l = 1 r = 9 * 10 ** (i - 1) res = 0 while l <= r: curr = (l + r) // 2 if p <= curr * x + i * curr * (curr + 1) / 2: res = curr r = curr - 1 else: l = curr + 1 prev = res - 1 q = p - (prev * x + i * prev * (prev + 1) // 2) for j in range(max_d): if q <= num_dig_last[j]: w = q - num_dig_last[j - 1] r = w % j x = w // j if r != 0: x += 1 r -= 1 else: r = j - 1 number = 10 ** (j - 1) + x - 1 print(str(number)[r]) break break
ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
l = [0] def count(size): nums = 10**size - 10 ** (size - 1) small = l[size - 1] + size large = l[size - 1] + nums * size if len(l) <= size: l.append(large) return nums * (small + large) // 2 def test(minSize, size, val): out = minSize * val + size * ((val + 1) * val) // 2 return out q = int(input()) for _ in range(q): want = int(input()) size = 1 while want > count(size): want -= count(size) size += 1 minSize = l[size - 1] lo = 0 hi = 10**size - 10 ** (size - 1) while hi - lo > 1: testV = (lo + hi) // 2 out = test(minSize, size, testV) if out < want: lo = testV else: hi = testV want -= test(minSize, size, lo) newS = 1 while 9 * 10 ** (newS - 1) * newS < want: want -= 9 * 10 ** (newS - 1) * newS newS += 1 want -= 1 more = want // newS dig = want % newS value = 10 ** (newS - 1) + more print(str(value)[dig])
ASSIGN VAR LIST NUMBER FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP BIN_OP NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
n = int(input()) k_list = [] for i in range(n): k_list.append(int(input())) digits = [0] w = 1 milestone = 10 str_seq = "" for i in range(1, 100000): if i == milestone: w += 1 milestone *= 10 digits.append(digits[i - 1] + w) str_seq += str(i) digits_in_sequence = [0] for i in range(1, 100000): digits_in_sequence.append(digits_in_sequence[i - 1] + digits[i]) k_list = list(enumerate(k_list)) k_list.sort(key=lambda x: x[1]) res = [""] * n k_i = 0 d_i = 0 while k_i != len(k_list): d = digits_in_sequence[d_i] ind, k = k_list[k_i] if d >= k: from_one = k - digits_in_sequence[d_i - 1] res[ind] = str_seq[from_one - 1] k_i += 1 else: d_i += 1 print("\n".join(res))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
def init(maxn): Sum = [0] * maxn Single = [0] * maxn for i in range(1, maxn): lens = 10**i - 10 ** (i - 1) pre = Single[i - 1] Single[i] = pre + lens * i for i in range(1, maxn): lens = 10**i - 10 ** (i - 1) pre = Single[i - 1] Sum[i] = (pre + i + pre + lens * i) * lens // 2 + Sum[i - 1] return Sum, Single def getAns(n, Sum, Single, maxn): ans = 0 minn = n index = 0 L, R = 1, 10**maxn while L <= R: m = (L + R) // 2 digit = len(str(m)) lens = m - 10 ** (digit - 1) + 1 pre = Single[digit - 1] cnt = (pre + digit + pre + lens * digit) * lens // 2 + Sum[digit - 1] if cnt < n: index = m minn = min(minn, n - cnt) L = m + 1 else: R = m - 1 n = minn L, R = 1, index + 11 index = 0 while L <= R: m = (L + R) // 2 digit = len(str(m)) lens = m - 10 ** (digit - 1) + 1 pre = Single[digit - 1] cnt = pre + lens * digit if cnt < n: index = m minn = min(minn, n - cnt) L = m + 1 else: R = m - 1 return str(index + 1)[minn - 1] def test(): ans = 0 Sum = 0 for i in range(1, 1000): ans += len(str(i)) Sum += ans if i % 10 == 9: print(i, ans, Sum) def main(): maxn = 10 Sum, Single = init(maxn) T = int(input()) for i in range(T): n = int(input()) print(getAns(n, Sum, Single, maxn)) main()
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
lis = [0] * 30001 s = "" for i in range(1, 30000): s += str(i) lis[i] = len(s) + lis[i - 1] for _ in range(int(input())): k = int(input()) l = 0 r = 30000 while l <= r: mid = l + (r - l) // 2 if lis[mid] >= k: r = mid - 1 else: l = mid + 1 print(s[k - lis[r] - 1])
ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
def cached(func): _cache = {} def wrapped(*args): nonlocal _cache if args not in _cache: _cache[args] = func(*args) return _cache[args] return wrapped @cached def num_count_with_len(l): return 10**l - 10 ** (l - 1) if l > 0 else 0 @cached def numbers_len_sum(l): if l < 0: return 0 return numbers_len_sum(l - 1) + l * num_count_with_len(l) def block_len(block_num): l = len(str(block_num)) return numbers_len_sum(l - 1) + (block_num - 10 ** (l - 1) + 1) * l def arith_sum(n): return n * (n + 1) // 2 @cached def block_len_sum_(l): if l <= 0: return 0 num_count = num_count_with_len(l) len_sum = numbers_len_sum(l - 1) return block_len_sum_(l - 1) + len_sum * num_count + arith_sum(num_count) * l def block_len_sum(block_num): l = len(str(block_num)) len_sum = numbers_len_sum(l - 1) num_count = block_num - 10 ** (l - 1) + 1 return block_len_sum_(l - 1) + len_sum * num_count + l * arith_sum(num_count) def binary_search(call, val): start = 0 end = 1 while call(end) <= val: end *= 2 result = start while start <= end: mid = (start + end) // 2 if call(mid) <= val: start = mid + 1 result = start else: end = mid - 1 return result cases = int(input()) for _ in range(cases): index = int(input()) - 1 block_num = binary_search(block_len_sum, index) rel_index = index - block_len_sum(block_num - 1) number = binary_search(block_len, rel_index) digit = rel_index - block_len(number - 1) print(str(number)[digit])
FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR RETURN VAR FUNC_DEF RETURN VAR NUMBER BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
def givelen(_______): tot = 0 while _______: _______ //= 10 tot += 1 return tot for _ in range(int(input())): end = int(input()) l = [] secon = sec = ans = currlen = last = 0 while end > 0: last += 1 l.append(last) currlen += givelen(last) secon = end end -= currlen for i in range(len(l)): sec = secon secon -= givelen(l[i]) if secon <= 0: ans = str(l[i])[sec - 1] break print(ans)
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR VAR VAR VAR NUMBER WHILE VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
a = int(input()) sp = [] for i in range(a): sp += [[int(input()), i]] sp.sort() w = 1 le = 0 i = 0 w2 = "" while le <= sp[-1][0]: w2 = w2 + str(w) if sp[i][0] <= le + len(w2): while i < a and sp[i][0] <= le + len(w2): sp[i] = [sp[i][1], [w2[sp[i][0] - le - 1]]] i += 1 le += len(w2) w += 1 sp.sort() for t in range(a): print(*sp[t][1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR LIST LIST FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING WHILE VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST VAR VAR NUMBER LIST VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
def num_of_digits(n): s = 0 for i in range(20): o = 10**i - 1 if o > n: break s += (n - o) * (n - o + 1) // 2 return s q = int(input("")) while q: q -= 1 digit = int(input("")) l = 0 r = int(10000000000.0) while r - l > 1: m = (r + l) // 2 if num_of_digits(m) < digit: l = m else: r = m left_soma = digit - num_of_digits(r - 1) l1 = 1 r1 = 9 prev_soma = 0 soma = r1 - l1 + 1 n_digit = 1 while left_soma > soma: l1 *= 10 r1 = r1 * 10 + 9 n_digit += 1 prev_soma = soma soma = soma + (r1 - l1 + 1) * n_digit left_soma -= prev_soma k = left_soma // n_digit left_digit = l1 - 1 + k left_digits = left_soma % n_digit if left_digits == 0: print(str(left_digit)[-1]) else: left_digit += 1 left_digits -= 1 left_digit = str(left_digit)[left_digits] print(left_digit)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
q = int(input()) a = "" for i in range(1, 100000): a += str(i) b = [] ans = 0 n = 1 i = 1 while ans < 10**9: ans += n n += len(str(i + 1)) i += 1 b.append(ans) while q: k = int(input()) for i in range(len(b)): if k > b[i]: x = 1 else: if i >= 1: k -= b[i - 1] break print(a[k - 1]) q -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP NUMBER NUMBER VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
q = int(input()) num_cnt = [[0, 0]] for i in range(1, 22000): if i < 10: num_cnt.append([num_cnt[-1][1] + 1, num_cnt[-1][1] + i]) elif i < 100: num_cnt.append([num_cnt[-1][1] + 1, num_cnt[-1][1] + 9 + (i - 9) * 2]) elif i < 1000: num_cnt.append([num_cnt[-1][1] + 1, num_cnt[-1][1] + 9 + 90 * 2 + (i - 99) * 3]) elif i < 10000: num_cnt.append( [num_cnt[-1][1] + 1, num_cnt[-1][1] + 9 + 90 * 2 + 900 * 3 + (i - 999) * 4] ) elif i < 100000: num_cnt.append( [ num_cnt[-1][1] + 1, num_cnt[-1][1] + 9 + 90 * 2 + 900 * 3 + 9000 * 4 + (i - 9999) * 5, ] ) for i in range(q): k = int(input()) left, right = 1, len(num_cnt) mid = 0 while left <= right: mid = (left + right) // 2 if k >= num_cnt[mid][0] and k <= num_cnt[mid][1]: break if k < num_cnt[mid][0]: right = mid - 1 else: left = mid + 1 now_cnt = num_cnt[mid][0] - 1 now_val = 1 succ = False while not succ: temp_val = list(str(now_val)) for j in temp_val: now_cnt += 1 if now_cnt == k: print(j) succ = True break now_val += 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP NUMBER NUMBER BIN_OP NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP NUMBER NUMBER BIN_OP NUMBER NUMBER BIN_OP NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
q = int(input()) def ar(n): return n * (n + 1) // 2 def sm(a, r, n): return a * n + (n - 1) * n // 2 * r def cale(n): if n == 0: return 0 return cale(n - 1) + 9 * 10 ** (n - 1) * n def zaj(n): poz = 1 while True: left = 1 right = 9 * 10 ** (poz - 1) + 1 while left < right: mid = (left + right) // 2 cur = cale(poz - 1) * mid cur += sm(poz, poz, mid) if cur >= n: right = mid else: left = mid + 1 if left == 9 * 10 ** (poz - 1) + 1: left = 9 * 10 ** (poz - 1) n -= cale(poz - 1) * left n -= sm(poz, poz, left) poz += 1 assert n > 0, "n == 0" else: left -= 1 n -= cale(poz - 1) * left n -= sm(poz, poz, left) ktory = 10 ** (poz - 1) + left return ktory, n for asdsjfhajfhsajf in range(q): k = int(input()) _, n = zaj(k) poz = 1 while True: left = 1 right = 9 * 10 ** (poz - 1) + 1 while left < right: mid = (left + right) // 2 cur = poz * mid if cur >= n: right = mid else: left = mid + 1 if left == 9 * 10 ** (poz - 1) + 1: left -= 1 n -= poz * left poz += 1 assert n > 0, "n == 0 down" else: left -= 1 n -= poz * left l = str(10 ** (poz - 1) + left) print(l[n - 1]) break
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER STRING VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR RETURN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER STRING VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
def solve(dp, k): for i in range(3 * 10**4): if dp[i] >= k: index = i break total = dp[index - 1] for i in range(1, index + 1): total += len(str(i)) if total >= k: total -= len(str(i)) for j in str(i): total += 1 if total == k: print(j) break break def main(): dp = [0] total = 0 for i in range(1, 3 * 10**4): total += len(str(i)) dp.append(total) for i in range(1, 3 * 10**4): dp[i] += dp[i - 1] q = int(input()) for i in range(q): k = int(input()) solve(dp, k) main()
FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER BIN_OP NUMBER NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER BIN_OP NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
def g(n): s = str(n) ans = 0 for i in range(1, len(s)): ans += i * 9 * 10 ** (i - 1) return n * len(s) - (len(s) * (10 ** (len(s) - 1) - 1) - ans) def sum(i, j): return i * (j - i + 1) + (j - i) * (j - i + 1) // 2 def f(n): ans = (n + 1) * g(n) s = str(n) for i in range(1, len(s)): ans -= i * sum(10 ** (i - 1), 10**i - 1) ans -= len(s) * sum(10 ** (len(s) - 1), n) return ans def slow_g(n): s = "" for i in range(1, n + 1): s += str(i) return len(s) def slow_f(n): ans = 0 for i in range(1, n + 1): ans += g(i) return ans def ans(n): l, r = 0, 10**18 while l + 1 < r: m = (l + r) // 2 if f(m) >= n: r = m else: l = m n -= f(r - 1) l = 0 r = r while l + 1 < r: m = (l + r) // 2 if g(m) >= n: r = m else: l = m n -= g(r - 1) return str(r)[n - 1] q = int(input()) for i in range(q): n = int(input()) print(ans(n))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR RETURN VAR FUNC_DEF ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP NUMBER NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
q = int(input()) sum = [0] * 20 cnt = [0] * 20 for i in range(1, 20): ct = 9 * 10 ** (i - 1) cnt[i] = cnt[i - 1] + i * ct sum[i] = sum[i - 1] + cnt[i - 1] * ct + i * (1 + ct) * ct // 2 def check(x, k, i): return cnt[i - 1] * x + i * (1 + x) * x // 2 < k def check2(x, k): l = 0 for i in range(10, -1, -1): if x - 10**i >= 0: x -= 10**i - 1 l = i + 1 break return cnt[l - 1] + l * x < k def cal(x): for i in range(10, -1, -1): if x - 10**i >= 0: return i + 1 return 1 def rv(x): c = [] while x: c.append(x % 10) x //= 10 c.append(0) c.reverse() return c while q: k = int(input()) i = 10 while sum[i] > k: i -= 1 k -= sum[i] i += 1 l = 1 r = 10**10 while l < r: m = (l + r) // 2 if check(m, k, i): l = m + 1 else: r = m l -= 1 k -= cnt[i - 1] * l + i * (1 + l) * l // 2 ll = 1 rr = 10**10 while ll < rr: m = (ll + rr) // 2 if check2(m, k): ll = m + 1 else: rr = m ll -= 1 k -= cnt[cal(ll) - 1] + cal(ll) * (ll - 10 ** (cal(ll) - 1) + 1) c = rv(ll + 1) print(c[k]) q -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER RETURN BIN_OP VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR LIST WHILE VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR RETURN VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
def binary_search(k): l, r = 0, 10**5 while l <= r: m = (l + r) // 2 if k <= dp[m]: r = m - 1 else: l = m + 1 return l def binary_search2(b, k): l, r = 0, len(str(b)) while l <= r: m = (l + r) // 2 if k <= dp2[m]: r = m - 1 else: l = m + 1 return l dp = [0] * 10**5 for i in range(1, 10**5): dp[i] = dp[i - 1] + len(str(i)) for i in range(1, 10**5): dp[i] += dp[i - 1] dp2 = [0] * 100 for i in range(1, 100): dp2[i] = 9 * 10 ** (i - 1) * i for i in range(2, 100): dp2[i] += dp2[i - 1] q = int(input()) for _ in range(q): k = int(input()) b = binary_search(k) k -= dp[b - 1] keta = binary_search2(b, k) k -= dp2[keta - 1] kazu = 10 ** (keta - 1) + (k - 1) // keta k -= (kazu - 10 ** (keta - 1)) * keta print(str(kazu)[k - 1])
FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
import sys input = sys.stdin.buffer.readline C = [0] X = [0] for i in range(50): A = 10**i * 9 C.append(C[i] + X[i] * A + (i + 1) * A * (A + 1) // 2) X.append(X[i] + A * (i + 1)) C2 = [0] X2 = [0] for i in range(50): A = 10**i * 9 C2.append(C2[i] + X2[i] * A + A) X2.append(i + 1) for t in range(int(input())): K = int(input()) - 1 A = 0 for i in range(50): if K < C[i + 1]: K -= C[i] A = i break L, R = 0, 10**18 while L < R: M = L + R + 1 >> 1 if X[A] * M + (A + 1) * M * (M + 1) // 2 > K: R = min(M, R - 1) else: L = M K -= X[A] * L + (A + 1) * L * (L + 1) // 2 B = 0 for i in range(50): if K < C2[i + 1]: B = i break K -= C2[B] L, R = 0, 10**18 while L < R: M = L + R + 1 >> 1 if K < X2[B] * M + M: R = min(R - 1, M) else: L = M K -= X2[B] * L + L print(str(10**B + L)[K])
IMPORT ASSIGN VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER BIN_OP NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR VAR
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
def solveQuad(gotcha, a, d): D = (2 * a - d) ** 2 + 8 * d * gotcha val = (-(2 * a - d) + D**0.5) / (2 * d) return val def isint(p): if p == int(p): return True def giveChar(index): a, an, d = 1, 9, 1 total = 0 for i in range(25): n = 9 * 10**i an = a + (n - 1) length = n * d temp = total total += length if total == index: ans = a + n - 1 return str(ans)[-1] if total > index: rem = index - temp if rem <= d: return str(a)[rem - 1] else: kk = rem // d if rem % d == 0: jj = a + kk - 1 return str(jj)[-1] else: jj = a + kk tt = rem % d return str(jj)[tt - 1] break d += 1 a = an + 1 for _ in range(int(input())): k = int(input()) a, d = 1, 1 total = 0 for i in range(25): n = 9 * 10**i an = a + (n - 1) * d sn = n * (a + an) // 2 temp = total total += sn if total > k: rem = k - temp val = solveQuad(rem, a, d) if isint(val) == True: index = a + (int(val) - 1) * d else: fval = int(val) snl = fval * (2 * a + (fval - 1) * d) // 2 index = rem - snl break d += 1 a = an + d print(giveChar(index))
FUNC_DEF ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR RETURN VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
def number(n): ans = 0 l = 0 while n // 10**l > 0: l += 1 l -= 1 while n > 0: ans += (l + 1) * (n + 1 - 10**l) n = 10**l - 1 l -= 1 return ans a = list() a.append(0) for i in range(40000): a.append(number(i + 1)) s = list() s.append(0) for i in range(1, len(a)): s.append(s[i - 1] + a[i]) OMAGAD = list() q = int(input()) for i in range(q): t = int(input()) l = 0 r = len(a) while l < r: k = (l + r) // 2 if s[k] < t: l = k + 1 else: r = k r += 3 while s[r] >= t: r -= 1 t -= s[r] L = 1 R = r + 2 while L < R: k = (L + R) // 2 if t > number(k): L = k + 1 else: R = k R -= 2 while number(R) < t: R += 1 t -= number(R - 1) OMAGAD.append(str(R)[t - 1]) for i in OMAGAD: print(i)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
def check(num): if values[num - 1] > n: return False return values[num - 1] s = "" for i in range(1, 100000): s += str(i) values = [1] prev = 1 for i in range(2, 30000): values.append(values[-1] + prev + len(str(i))) prev = prev + len(str(i)) for nt in range(int(input())): n = int(input()) low = 0 high = 3 * 10**4 while low < high: mid = (low + high) // 2 if check(mid): low = mid + 1 else: high = mid - 1 if check(low): ans = low else: ans = low - 1 value = check(ans) left = n - value if left == 0: print(str(ans)[-1]) else: print(s[left - 1])
FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR RETURN NUMBER RETURN VAR BIN_OP VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
import sys zz = 1 sys.setrecursionlimit(10**5) if zz: input = sys.stdin.readline else: sys.stdin = open("input.txt", "r") sys.stdout = open("all.txt", "w") di = [[-1, 0], [1, 0], [0, 1], [0, -1]] def fori(n): return [fi() for i in range(n)] def inc(d, c, x=1): d[c] = d[c] + x if c in d else x def ii(): return input().rstrip() def li(): return [int(xx) for xx in input().split()] def fli(): return [float(x) for x in input().split()] def comp(a, b): if a > b: return 2 return 2 if a == b else 0 def gi(): return [xx for xx in input().split()] def gtc(tc, ans): print("Case #" + str(tc) + ":", ans) def cil(n, m): return n // m + int(n % m > 0) def fi(): return int(input()) def pro(a): return reduce(lambda a, b: a * b, a) def swap(a, i, j): a[i], a[j] = a[j], a[i] def si(): return list(input().rstrip()) def mi(): return map(int, input().split()) def gh(): sys.stdout.flush() def isvalid(i, j, n, m): return 0 <= i < n and 0 <= j < m def bo(i): return ord(i) - ord("a") def graph(n, m): for i in range(m): x, y = mi() a[x].append(y) a[y].append(x) t = 1 uu = t while t > 0: t -= 1 d = [0] * 10**5 c = 0 i = 1 while c <= 10**9: d[i] = d[i - 1] + len(str(i)) i += 1 c += d[i - 1] pre = [0] i = 1 while d[i]: pre.append(pre[-1] + d[i]) i += 1 q = fi() for i in range(q): x = fi() y = x ans = -1 l = ans = 0 r = 21836 while l <= r: mid = (l + r) // 2 if pre[mid] > x: r = mid - 1 else: ans = pre[mid] cur = mid l = mid + 1 x -= ans if x == 0: print(cur % 10) continue i = 1 c = 0 while x: p = str(i) for j in p: x -= 1 if x == 0: print(j) break i += 1
IMPORT ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN NUMBER RETURN VAR VAR NUMBER NUMBER FUNC_DEF RETURN VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING FUNC_CALL VAR VAR STRING VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_DEF RETURN NUMBER VAR VAR NUMBER VAR VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
import sys def fn(f): global file k = f por = 1 spi = 0 dlc = 0 i = 1 while True: if i == por: dlc += 1 por = por * 10 spi += dlc if k > spi: k = k - spi else: j = 1 por = 1 ndl = 0 while True: if j == por: por = por * 10 ndl += 1 if k > ndl: j = j + 1 k = k - ndl else: h = str(j) print(h[k - 1]) break break i = i + 1 return 0 n = int(input()) l = [] for u in range(0, n): l.append(int(input())) for u in range(n): fn(l[u])
IMPORT FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
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**18 MOD = 10**9 + 7 for _ in range(INT()): K = INT() done = 0 cnt = 0 blkln = 0 digln = 0 i = 1 nxpow = 1 while not done: if i == nxpow: nxpow *= 10 digln += 1 blkln += digln if cnt + blkln < K: cnt += blkln else: digln = 0 j = 1 nxpow = 1 while 1: if j == nxpow: nxpow *= 10 digln += 1 if cnt + digln < K: cnt += digln else: num = str(j) ans = num[K - cnt - 1] print(ans) done = 1 break j += 1 i += 1
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 FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
def isqrt(x): if x < 0: raise ValueError("square root not defined for negative numbers") n = int(x) if n == 0: return 0 a, b = divmod(n.bit_length(), 2) x = 2 ** (a + b) while True: y = (x + n // x) // 2 if y >= x: return x x = y p = [ 0, 45, 9045, 1395495, 189414495, 23939649495, 2893942449495, 339393974949495, 38939394344949495, 1000000000000000001, ] nx = [0, 9, 189, 2889, 38889, 488889, 5888889, 68888889, 788888889, 8888888889] q = int(input()) for ut in range(q): lk = int(input()) k = lk idx = 0 for i in range(len(p) - 1): if p[i] <= k and p[i + 1] > k: idx = i idx = idx k -= 1 k -= p[idx] a = idx + 1 b = 2 * nx[idx] + idx + 1 k = -2 * k d = isqrt(b * b - 4 * a * k) x1 = (-b + d) / (2.0 * a) x2 = (-b - d) / (2.0 * a) a1 = int(x1) z = lk - p[idx] - nx[idx] * a1 - a1 * (a1 + 1) // 2 * (idx + 1) cnt = 0 ww = 1 pow = 0 pow = 1 while (cnt + pow * ww) * 9 < z: cnt += pow * ww ww += 1 pow *= 10 sym_cnt = z - cnt * 9 - 1 ok = pow + sym_cnt / ww s = str(ok) if z < 10: print(z) else: print(s[sym_cnt % ww])
FUNC_DEF IF VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR WHILE NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER IF VAR VAR RETURN VAR ASSIGN VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
class Num: cur, digits, di, stored = 0, 0, {}, [0, 0, 0, 0] def dig(n): if "1" + "0" * (len(str(n)) - 1) == str(n): Num.cur += 1 Num.digits += Num.cur else: Num.digits += Num.cur return Num.digits def work(n): j = n start = 1 + Num.stored[0] Num.cur, Num.digits = Num.stored[2], Num.stored[3] done = False n -= Num.stored[1] while not done: next = dig(start) if n > next: Num.stored[1] += next Num.stored[2] = Num.cur Num.stored[3] = Num.digits n -= next Num.stored[0] += 1 start += 1 else: done = True p = 1 while n > 9 * p * 10 ** (p - 1): n -= 9 * p * 10 ** (p - 1) p += 1 starts = 10 ** (p - 1) if n <= p: Num.di[j] = str(starts)[n - 1] else: cv = int(n / p) starts += cv n -= cv * p if n == 0: Num.di[j] = str(starts - 1)[-1] else: Num.di[j] = str(starts)[n - 1] q = int(input()) li = [] for i in range(q): li.append(int(input())) for j in sorted(li): work(j) for j in li: print(Num.di[j])
CLASS_DEF ASSIGN VAR VAR VAR VAR NUMBER NUMBER DICT LIST NUMBER NUMBER NUMBER NUMBER FUNC_DEF IF BIN_OP STRING BIN_OP STRING BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR VAR VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
dp, cnt = [0], 1 dp2 = [0] while dp[-1] <= int(1e18): ans = dp2[-1] + (10**cnt - 10 ** (cnt - 1)) * cnt dp2.append(ans) ans = ( dp[-1] + dp2[-2] * (10**cnt - 10 ** (cnt - 1)) + cnt * (10**cnt - 10 ** (cnt - 1) + 1) * (10**cnt - 10 ** (cnt - 1)) / 2 ) ans = int(ans) cnt += 1 dp.append(ans) def Cal(a, b): return dp2[b - 1] * a + b * int(a * (a + 1) / 2) q = int(input()) for _ in range(q): k = int(input()) i = 0 while k > dp[i]: i += 1 k -= dp[i - 1] l, r = 0, 10**i - 10 ** (i - 1) last = int((l + r) / 2) while not (Cal(last, i) < k and Cal(last + 1, i) >= k): if Cal(last, i) < k: l = last last = int((l + r) / 2 + 1) else: r = last last = int((l + r) / 2) k -= Cal(last, i) j = 0 while dp2[j] < k: j += 1 k -= dp2[j - 1] a = int((k - 1) / j) k -= a * j Long = str(10 ** (j - 1) + a) print(Long[k - 1])
ASSIGN VAR VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER WHILE VAR NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
def sum_first(k): return (k + 1) * k // 2 def sum_seq_len(k): res = 0 x = 1 while k >= x: res += sum_first(k - (x - 1)) x *= 10 return res def seq_len(k): res = 0 x = 1 while k >= x: res += k - (x - 1) x *= 10 return res def brut_ssl(k): res = 0 for i in range(1, k + 1): for j in range(1, i + 1): res += len(str(j)) return res def brut_sl(k): res = 0 for i in range(1, k + 1): res += len(str(i)) return res def binsrch(a, b, x, f): if a == b - 1: return a mid = (a + b) // 2 if f(mid) < x: return binsrch(mid, b, x, f) else: return binsrch(a, mid, x, f) def test(x): pref_seq_cnt = binsrch(0, 100 * x, x, sum_seq_len) seq_l = x - sum_seq_len(pref_seq_cnt) big = binsrch(0, seq_l, seq_l, seq_len) ind = seq_l - seq_len(big) return str(big + 1)[ind - 1] T = int(input()) for _ in range(T): x = int(input()) print(test(x))
FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
import sys def count_num(num): i = 0 while num: num //= 10 i += 1 return i def find1(num): for i in range(1, 21837): if num < list_psum[i]: return i - 1 list_count = [0] * 21837 list_psum = [0] * 21837 for i in range(1, 21837): list_count[i] = list_count[i - 1] + count_num(i) list_psum[i] += list_count[i] + list_psum[i - 1] T = int(sys.stdin.readline()) for _ in range(T): n = int(sys.stdin.readline()) num1 = find1(n) num2 = n - list_psum[num1] res = num1 % 10 num = 1 while num2: now_num = str(num) for d in now_num: res = d num2 -= 1 if num2 == 0: break num += 1 print(res)
IMPORT FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR RETURN BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
def l(n): s = 0 for i in range(20): o = 10**i - 1 if o > n: break s += (n - o) * (n - o + 1) // 2 return s def bs(k): n = 0 for p in range(63, -1, -1): if l(n + 2**p) < k: n += 2**p return n def num(n): if n < 10: return n for i in range(1, 19): seglen = i * 9 * 10 ** (i - 1) if n <= seglen: return str(10 ** (i - 1) + (n - 1) // i)[(n - 1) % i] else: n -= seglen q = int(input()) for _ in range(q): k = int(input()) print(num(k - l(bs(k))))
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR BIN_OP NUMBER VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER IF VAR VAR RETURN FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
t = int(input()) for _ in range(t): summ = 0 count = 0 sneg = 0 ncount = 0 x = int(input()) f = list(map(int, input().split())) for d in f: z = d if z >= 0: summ += z count += 1 else: sneg += z ncount += 1 f = sorted(f) f = f[::-1] p = count for y in range(p, x): if (summ + f[y]) * (count + 1) >= summ * count: count += 1 summ += f[y] sneg -= f[y] else: break print(count * summ + sneg)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
n = int(input().strip()) for a0 in range(n): num = int(input().strip()) a = [int(a_temp) for a_temp in input().strip().split(" ")] a = sorted(a) a = a[::-1] sec = [] s = 0 c = 0 sep = 0 for x in a: if x >= 0: s = s + x c = c + 1 else: sec.append(x) for y in sec: if (s + y) * (c + 1) >= s * c: s = s + y c = c + 1 else: sep = sep + y print(s * c + sep)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR IF BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
T = eval(input()) for i in range(T): X = eval(input()) c = 0 sum1 = 0 A = list(map(eval, input().split())) A.sort() j = X - 1 while j >= 0: if (sum1 + A[j]) * (c + 1) >= sum1 * c: sum1 = sum1 + A[j] j = j - 1 c = c + 1 else: break sum1 = sum1 * c while j >= 0: sum1 = sum1 + A[j] j = j - 1 print(sum1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
for _ in range(int(input().strip())): n = int(input().strip()) a = list(map(int, input().strip().split())) c = 0 total = 0 subtotal = 0 a.sort(reverse=True) for i in a: if i < 0: if subtotal * c > (subtotal + i) * (c + 1): total += subtotal * c + i subtotal = 0 c = 0 else: subtotal += i c += 1 else: subtotal += i c += 1 total += subtotal * c print(total)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR IF VAR NUMBER IF BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
test = int(input()) SUM = [0] for i in range(1, 100000 + 1): SUM.append(0) while test != 0: n = int(input()) ar = list(map(int, input().split())) ar.sort() for i in range(1, n + 1): SUM[i] = ar[i - 1] + SUM[i - 1] ans = SUM[n] * n for i in range(1, n + 1): if ans < SUM[i] + (n - i) * (SUM[n] - SUM[i]): ans = SUM[i] + (n - i) * (SUM[n] - SUM[i]) print(ans) test -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR 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 NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
def binary_search(arr, start, end, key): if start > end: return -1 mid = (start + end) // 2 if arr[mid] == key: return mid if arr[mid] > key: return binary_search(arr, start, mid - 1, key) else: return binary_search(arr, mid + 1, end, key) t = int(input()) for i in range(t): n = int(input()) arr = input() arr = arr.split() minpos = 10**9 for i in range(len(arr)): arr[i] = int(arr[i]) if arr[i] > 0 and minpos > arr[i]: minpos = arr[i] arr = sorted(arr) index = binary_search(arr, 0, n - 1, minpos) start = 0 if index == -1: start = n - 1 else: start = index - 1 sumpos = 0 for i in range(start + 1, n): sumpos += arr[i] npos = n - 1 - start tmp = start sumneg = 0 nneg = 0 cumarr = [arr[0]] for j in range(1, tmp + 1): cumarr.append(cumarr[j - 1] + arr[j]) maxhappiness = 0 if tmp >= 0: maxhappiness = npos * sumpos + cumarr[tmp] else: maxhappiness = npos * sumpos for j in range(tmp, -1, -1): sumneg = 0 restsum = 0 if j == 0: sumneg = cumarr[start] restsum = 0 else: sumneg = cumarr[start] - cumarr[j - 1] restsum = cumarr[j - 1] nneg = start - j + 1 if (npos + nneg) * (sumpos + sumneg) + restsum > maxhappiness: maxhappiness = (npos + nneg) * (sumpos + sumneg) + restsum print(maxhappiness)
FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN VAR IF VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
for t in range(int(input())): n = int(input()) a = map(int, input().split()) total = 0 pos = [] neg = [] for i in a: if i >= 0: pos.append(i) else: neg.append(i) neg.sort(reverse=True) s = sum(pos) l = len(pos) for i in neg: if s + i * (l + 1) >= 0: s += i l += 1 else: total += i total += s * l print(total)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
for _ in range(int(input())): input() happiness_array = list(map(int, input().split())) displeasurables = sorted(filter(lambda x: x < 0, happiness_array), reverse=True) pleasurables = [x for x in happiness_array if x >= 0] sum_displeasure = sum(displeasurables) sum_pleasure = sum(pleasurables) len_pleasure = len(pleasurables) max_pleasure = sum_pleasure * len_pleasure + sum_displeasure for item in displeasurables: len_pleasure += 1 sum_pleasure += item sum_displeasure -= item temp = sum_pleasure * len_pleasure + sum_displeasure if temp > max_pleasure: max_pleasure = temp else: break print(max_pleasure)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR 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 NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR FOR VAR VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
def solve(arr: "list[int]") -> "int": pos = 0 pos_size = 0 neg = 0 neg_li = [] for ele in arr: if ele >= 0: pos += ele pos_size += 1 else: neg_li.append(ele) neg += ele if pos == 0: return sum(neg_li) elif neg == 0: ans = pos * pos_size return ans neg_li.sort(reverse=True) mx, k, it = 0, 0, 0 for neg_el in neg_li: it += neg_el k += 1 val = (pos + it) * (pos_size + k) + (neg - it) if val > mx: mx = val return max(pos * pos_size + it, mx) for _ in range(int(input())): a = int(input()) x = [int(i) for i in input().strip().split()] print(solve(x))
FUNC_DEF STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
for z in range(int(input())): N = int(input()) inputs = list(map(int, input().split())) inputs.sort(reverse=True) positive = list(filter(lambda x: x >= 0, inputs)) negative = list(filter(lambda x: x < 0, inputs)) final_negative_list = [] total = sum(positive) counts = len(positive) for item in negative: if (total + item) * (counts + 1) > total * counts: total += item counts += 1 else: final_negative_list.append(item) print(total * counts + sum(final_negative_list))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
for t in range(int(input())): n = int(input()) a = list(map(int, input().split())) a.sort() x = s = c = 0 for ai in reversed(a): if (s + ai) * (c + 1) > s * c: s += ai c += 1 else: x += ai print(s * c + x)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
t = int(input().strip()) for a0 in range(t): n = int(input().strip()) arr = list(map(int, input().strip().split(" "))) arr.sort() ans = sum(arr) * n sumArray = [0] * (n + 1) for i in range(n): s = ans - sumArray[i] s = s // n s -= arr[i] s = s * (n - 1 if n > 2 else 1) s += arr[i] + sumArray[i] sumArray[i + 1] = arr[i] + sumArray[i] if s > ans: ans = s n -= 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
for t in range(int(input())): n = int(input()) a = list(map(int, input().split())) a.sort(reverse=True) result = 0 if a[0] <= 0: result = sum(a) else: current_max = 0 _sum = 0 i = 0 for x in a: i += 1 _sum += x if current_max > _sum * i: result = current_max + sum(a[i - 1 :]) break current_max = _sum * i else: result = current_max print(result)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
count = int(input()) for step in range(count): size = int(input()) dishes = [int(x) for x in input().split()] dishes.sort(reverse=True) happiness = 0 counter = 0 negative = 0 top = 0 for mrinal in dishes: if top <= (happiness + mrinal) * (counter + 1): top = (happiness + mrinal) * (counter + 1) happiness += mrinal counter += 1 else: negative += mrinal happiness *= counter happiness += negative print(happiness)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
varTest = int(input("\n")) for j in range(0, varTest): arraylength = int(input("\n")) array = list(map(int, input().split())) array.sort(key=None, reverse=False) length = int(len(array)) sum = int(0) startingindex = int(-1) for i in range(0, length, 1): if int(array[i]) >= 0: if startingindex == int(-1): startingindex = int(i) sum = sum + int(array[i]) answer = int(0) if startingindex != int(-1): leng = int(length - startingindex) answer = int(sum * leng) sum2 = answer if startingindex != int(0): if startingindex == -1: for i in range(0, arraylength): answer = answer + int(array[i]) else: j = startingindex for i in range(1, startingindex + 1, 1): if sum2 >= answer: answer = sum2 sum = sum + array[startingindex - i] sum2 = sum sum2 = sum2 * (length - startingindex + i) j = startingindex - i for i in range(0, j + 1): answer = answer + int(array[i]) print(answer, end="\n")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NONE NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR FUNC_CALL VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
import sys def set_input(): t = int(input()) inp = [] sum = [] for i in range(t): n = int(input()) inp = list(map(int, input().split())) sum.append(solve(n, inp)) for i in sum: print(i) def solve(n, inp): neg_sum = 0 pos_sum = 0 neg = [] pos = [] for i in inp: if i < 0: neg.append(i) elif i >= 0: pos.append(i) len_pos = len(pos) pos_sum = sum(pos) neg_sum = sum(neg) sorted1 = sorted(neg, reverse=True) old_sum = len_pos * pos_sum + neg_sum count = 0 sum1 = 0 if inp.count(0) + len(neg) == len(inp) or len(neg) == len(inp): return neg_sum elif len_pos == len(inp): return len_pos * pos_sum else: for i in sorted1: count += 1 new_sum = sum1 sum1 = (len_pos + count) * (pos_sum + i) + (neg_sum - i) if sum1 > new_sum: neg_sum = neg_sum - i pos_sum = pos_sum + i else: break return max(len_pos * pos_sum + neg_sum, new_sum) set_input()
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR IF VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR FOR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
T = int(input()) for _ in range(T): n = int(input()) Happiness = list(map(int, input().split())) Happiness.sort() Happiness.reverse() sum1 = 0 ans = 0 found = False for i in range(n): sum1 += Happiness[i] ans1 = sum1 * (i + 1) if ans1 > ans: ans = ans1 else: found = True break if found: print(ans + sum(Happiness[i:])) else: print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
t = int(input()) for i in range(t): n = int(input()) array = list(map(int, input().rstrip().split(" "))) array.sort() cum_sum = [array[0]] for j in range(1, n): cum_sum.append(cum_sum[j - 1] + array[j]) total_sum = cum_sum[n - 1] prev_sum = total_sum * n printed = False for j in range(n): new_sum = (total_sum - cum_sum[j]) * (n - (j + 1)) + cum_sum[j] if new_sum < prev_sum: print(prev_sum) printed = True break else: prev_sum = new_sum if not printed: print(new_sum)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
tc = int(input()) for t in range(tc): n = int(input()) happiness = list(map(int, input().strip().split(" "))) happiness.sort(reverse=True) totalpos = 0 poslen = 0 negative_list = [] i = 0 while i < n and happiness[i] >= 0: totalpos += happiness[i] poslen += 1 i += 1 if i < n: while i < n: if (totalpos + happiness[i]) * (poslen + 1) > totalpos * poslen: totalpos += happiness[i] poslen += 1 else: negative_list.append(happiness[i]) i += 1 print(totalpos * poslen + sum(negative_list))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR WHILE VAR VAR IF BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
t = int(input()) while t: n = int(input()) hpns = [int(x) for x in input().split()] sumt = sum(hpns) po_sum = 0 count = 0 neg_sum = 0 neg_count = 0 neg_list = [] for num in hpns: if num >= 0: po_sum += num count += 1 else: neg_sum += num neg_list.append(num) neg_list.sort(reverse=True) ans = neg_sum + count * po_sum for i in neg_list: neg_count += 1 if (po_sum + i) * (count + neg_count) + (neg_sum - i) > ans: ans = (po_sum + i) * (count + neg_count) + (neg_sum - i) po_sum += i neg_sum -= i else: break print(ans) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR FOR VAR VAR VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
t = int(input()) while t > 0: t = t - 1 n = int(input()) arr = [] neg = [] pos = [] ans = 0 copy = 0 arr = [int(x) for x in input().split(" ")] for i in range(n): if arr[i] < 0: neg.append(arr[i]) else: pos.append(arr[i]) copy = ans = sum(pos) n_terms = len(pos) ans *= n_terms neg.sort(reverse=True) if n_terms == 0: ans = sum(neg) else: for i in range(len(neg)): if (copy + neg[i]) * (n_terms + 1) >= ans: n_terms = n_terms + 1 copy = copy + neg[i] ans = copy * n_terms else: break for j in range(i, len(neg)): ans += neg[j] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
__author__ = ["vjn4006(Vaibhav Nagda)"] def main(): t = int(input().strip()) for _ in range(t): n = int(input().strip()) s = sorted(list(map(int, input().strip().split(" ")))) grt_cnt = 0 grt_sum = 0 happiness = 0 smaller = [] for i in s: if i >= 0: grt_cnt += 1 grt_sum += i else: smaller.append(i) happiness = grt_sum * grt_cnt smaller = sorted(smaller) smaller = list(reversed(smaller)) start = 0 grt_cnt += 1 for i in range(len(smaller)): if grt_cnt * (grt_sum + smaller[i]) >= happiness: grt_sum += smaller[i] happiness = grt_sum * grt_cnt grt_cnt += 1 else: start = i break for i in range(start, len(smaller)): happiness += smaller[i] print(happiness) return main()
ASSIGN VAR LIST STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
for _ in range(int(input())): N = int(input()) A = sorted(list(map(int, input().split()))) s = 0 m = 0 j = 0 z = 1 for i in range(N - 1, -1, -1): s += A[i] if z * s > m: m = z * s z += 1 else: j += A[i] print(m + j)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
t = int(input()) while t: t = t - 1 n = int(input()) a = input().split() a = [int(x) for x in a] count_negative = sum(x < 0 for x in a) count_only_neg = sum(x <= 0 for x in a) if count_only_neg == n: print(sum(a)) elif count_negative == 0: print(n * sum(a)) elif count_negative == n: print(sum(a)) else: count_positive = n - count_negative positive_list = [i for i in a if i > 0] negative_list = [i for i in a if i < 0] sum_positive = sum(positive_list) sum_negative = sum(negative_list) first_sum = sum_negative + sum_positive * count_positive second_sum = n * sum(a) negative_list.sort(reverse=True) counts = 0 new_sum = 0 pre_sum = sum_positive pre_neg = sum_negative for i in negative_list: counts = counts + 1 old_sum = new_sum new_sum = (pre_sum + i) * (count_positive + counts) + (pre_neg - i) pre_sum = pre_sum + i pre_neg = pre_neg - i if old_sum > new_sum: break print(max(first_sum, second_sum, old_sum))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
T = int(input()) while T > 0: dishes = int(input()) numbers = list(map(int, input().split())) happiness = 0 count = 0 total = 0 steps = 0 numbers.sort(reverse=True) for i in numbers: if i >= 0: count = count + i total = total + 1 elif count + (total + 1) * i > 0: count = count + i total = total + 1 else: steps = steps + i happiness = steps + count * total print(happiness) T = T - 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
for _ in range(int(input())): n = int(input()) A = list(map(int, input().split())) A = sorted(A)[::-1] left = 0 summ = sum(A) right = summ ans = -float("inf") for i in range(n): left += A[i] right -= A[i] ans = max(ans, (i + 1) * left + right) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
t = int(input()) for test in range(t): n = int(input()) a = list(map(int, input().split())) a = sorted(a, reverse=True) ans = 0 curr_sum = 0 for i in range(n): curr_sum += a[i] if curr_sum * (i + 1) > ans: ans = curr_sum * (i + 1) else: ans += a[i] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
for t in range(int(input())): n = int(input()) x = list(map(int, input().split(" "))) x.sort(reverse=True) s = x[0] mul = 1 res = 0 for i in range(1, len(x)): prev = s * mul s += x[i] mul += 1 if s * mul < prev and s * mul < prev + x[i]: res += prev s = x[i] mul = 1 res += s * mul print(res)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
t = int(input()) for i in range(t): n = int(input()) a = [] a.extend(map(int, input().split())) a.sort(reverse=True) c = 0 s = a[0] while (s + a[c + 1]) * (c + 2) >= s * (c + 1): s += a[c + 1] c += 1 if c >= n - 1: break s *= c + 1 for i in range(c + 1, n): s += a[i] print(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
t = int(input()) for i in range(t): l = int(input()) seq = list(map(int, input().split())) seq.sort(reverse=True) counter = 0 negsum = 0 posum = 0 for j in seq: if j >= 0: posum += j counter += 1 elif posum >= (counter + 1) * abs(j): posum += j counter += 1 else: negsum += j print(posum * counter + negsum)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
numTests = int(input()) for test in range(numTests): numDishes = int(input()) happy = map(int, input().split()) sumPos = 0 countPos = 0 negNums = [] for h in happy: if h > -1: sumPos += h countPos += 1 else: negNums.append(-h) negNums.sort() done = negNums == [] while not done: done = negNums[0] * (countPos + 1) > sumPos if not done: sumPos -= negNums.pop(0) countPos += 1 done = negNums == [] print(int(sumPos * countPos - sum(negNums)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR LIST WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR IF VAR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
for _ in range(int(input())): n = int(input()) arr = sorted(map(int, input().split()), reverse=True) negative_arr = tuple(i for i in arr if i < 0) positive_arr = tuple(i for i in arr if i >= 0) temp_len = len(positive_arr) positive_sum = sum(positive_arr) ans = positive_sum * temp_len if not negative_arr: print(ans) elif not positive_arr: print(sum(negative_arr)) else: neg = 0 for i in negative_arr: temp_ans = (positive_sum + i) * (temp_len + 1) if temp_ans >= ans: ans = temp_ans positive_sum += i temp_len += 1 else: neg += i print(ans + neg)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
tc = int(input()) while tc: n = int(input()) no = list(map(int, input().split())) neg = [] pos = [] ncnt = 0 pcnt = 0 psum = 0 nsum = 0 for x in range(n): temp = no[x] if temp < 0: neg.append(temp) ncnt = ncnt + 1 nsum = nsum + temp else: pos.append(temp) pcnt = pcnt + 1 psum = psum + temp neg = sorted(neg, reverse=True) ans = nsum + psum * pcnt for x in range(ncnt): nsum = nsum - neg[x] ncnt = ncnt - 1 pcnt = pcnt + 1 psum = psum + neg[x] tans = nsum + psum * pcnt if tans > ans: ans = tans else: break print(ans) tc = tc - 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
for i in range(int(input())): amd = int(input()) array = list(map(int, input().split())) array.sort() mke = len(array) - 1 for k in range(len(array)): if array[k] >= 0: mke = k break total = sum(array) * len(array) sum1 = sum(array) r = len(array) rmd = 0 for j in range(mke): sum1 = sum1 - array[j] r -= 1 rmd += array[j] newsum = sum1 * r + rmd if total > newsum: break else: total = newsum print(total)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
t = int(input()) for i in range(t): l = int(input()) seq = list(map(int, input().split())) seq.sort(reverse=True) lensubset = 0 total = 0 negtotal = 0 posum = 0 for j in range(l): if seq[j] >= 0: posum += seq[j] lensubset += 1 elif posum >= (j + 2) * abs(seq[j]): posum += seq[j] lensubset += 1 else: negtotal = sum(seq[j:]) break total = posum * lensubset + negtotal print(total)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
def max_happiness(arr): pos = [] neg = [] for y in arr: if y > 0: pos.append(y) else: neg.append(y) if len(pos) == len(arr): return sum(pos) * len(pos) elif len(neg) == len(arr): return sum(neg) else: arr.sort() arr.reverse() i = 0 s = arr[i] current = arr[i] j = 1 while j < len(arr): current += arr[j] happiness = current * (j - i + 1) if happiness >= s: s = happiness j += 1 else: return s + sum(arr[j : len(arr)]) return s test_cases = int(input()) while test_cases != 0: data = input() data2 = list(map(int, input().split())) print(max_happiness(data2)) test_cases -= 1
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN BIN_OP VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
for _ in range(int(input())): size = int(input()) dishes = sorted(list(map(int, input().split())))[::-1] positivessum, negativessum = 0, 0 positives, negatives = list(), list() for i in range(size): if dishes[i] >= 0: positives.append(dishes[i]) positivessum += dishes[i] else: negatives.append(dishes[i]) negativessum += dishes[i] lenp, lenn = len(positives), len(negatives) index = 0 posans = lenp * positivessum negans = negativessum count = lenp flag = 0 for i in range(lenn): subans = (negatives[i] + positivessum) * (count + 1) if subans > posans: posans = subans positivessum += negatives[i] count += 1 index = i elif count == lenp: flag = 1 break else: break if flag == 0: print(posans + sum(negatives[index + 1 :])) else: print(posans + negativessum)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
t = int(input()) for _ in range(t): n, sum1, sum2, cnt = int(input()), 0, 0, 0 arr = [int(i) for i in input().split()] arr.sort(reverse=True) for i in arr: if sum1 * cnt + i <= (sum1 + i) * (cnt + 1): sum1, cnt = sum1 + i, cnt + 1 else: sum2 += i print(sum2 + sum1 * cnt)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
for _ in range(int(input())): n = int(input()) (*a,) = map(int, input().split()) c = [i for i in a if i >= 0] d = [i for i in a if i < 0] s = sum(c) l = len(c) ans = s * l + sum(d) for i in reversed(sorted(d)): if i * l + s > 0: ans += i * l + s s += i l += 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
t = int(input()) for _ in range(t): n = int(input()) a = sorted(list(map(int, input().strip().split()))) pos = 0 while pos < n and a[pos] < 0: pos += 1 if pos == n: print(sum(a)) continue suma = sum(a[pos:]) lena = len(a[pos:]) sumb = sum(a[:pos]) prev = suma * lena + sumb if pos > 0: pos -= 1 sumb -= a[pos] lena += 1 suma += a[pos] next = suma * lena + sumb else: next = prev while pos > 0 and next > prev: prev = next pos -= 1 sumb -= a[pos] lena += 1 suma += a[pos] next = suma * lena + sumb print(prev)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
tc = int(input()) for _ in range(tc): n = int(input()) lst = list(map(int, input().split())) neg = [] pos = [] for elem in lst: if elem >= 0: pos.append(elem) else: neg.append(elem) pos_sum = sum(pos) pos_len = len(pos) neg_sum = sum(neg) i = 0 ans = pos_sum * pos_len + neg_sum neg.sort(reverse=True) for a in neg: pos_len += 1 pos_sum += a neg_sum -= a hap = pos_sum * pos_len + neg_sum if hap < ans: break else: ans = hap print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
T = int(input()) i = 0 while i < T: N = int(input()) negs = [] sump = 0 ns = input().split() for e in ns: e = int(e) if e < 0: negs.append(e) else: sump += e negs.sort(reverse=True) nump = N - len(negs) j = 0 for e in negs: contn = -(nump + 1) * e diff = sump if contn <= sump: sump += e nump += 1 j += 1 else: break sump = sump * nump negs = negs[j:] for e in negs: sump += e print(sump) i += 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
t = int(input()) for y in range(t): n = int(input()) k = list(input().split()) k = [int(X) for X in k] k.sort() r = 0 s = 0 for i in range(n): if k[i] < 0: e = i s = s + k[i] else: r = r + k[i] if r == 0: print(s) elif s == 0: print(r * n) else: re = r * (n - 1 - e) + s k.reverse() d = r h = n - e for j in range(n - 1 - e, n): d = d + k[j] s = s + -1 * k[j] g = d * h + s h = h + 1 if re < g: re = g print(re)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
from sys import stdin t = int(stdin.readline()) while t: n = int(stdin.readline()) l = list(map(int, stdin.readline().split())) ans = 0 s = 0 count = 0 l.sort(reverse=True) temp = 0 for i in range(0, len(l)): if (s + l[i]) * (count + 1) >= temp: count += 1 s += l[i] temp = s * count else: ans += l[i] print(s * count + ans) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
for _ in range(int(input())): n = int(input()) l = [int(i) for i in input().split()] pos_sum = 0 pos_cnt = 0 neg_sum = 0 neg_li = [] for i in l: if i >= 0: pos_sum += i pos_cnt += 1 else: neg_sum += i neg_li.append(i) if len(neg_li) == n or sum(i > 0 for i in l) == 0: print(neg_sum) elif pos_cnt == 0: print(neg_sum) elif pos_cnt == n: print(pos_sum * n) else: neg_li.sort(reverse=True) mx = 0 extra = 0 extra_sum = 0 for neg_el in neg_li: extra_sum += neg_el extra += 1 curr = (pos_sum + extra_sum) * (pos_cnt + extra) + neg_sum - extra_sum if curr > mx: mx = curr print(max(mx, pos_cnt * pos_cnt + neg_sum))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
t = int(input()) for i in range(t): n = int(input()) arr = [int(x) for x in input().split()] pos = [] neg = [] for i in arr: if i < 0: neg.append(i) else: pos.append(i) length = len(pos) max_sum = sum(pos) * length + sum(neg) if len(neg) > 0: pos_sum = sum(pos) neg_sum = sum(neg) neg = sorted(neg, reverse=True) curr_sum = 0 for i in range(len(neg)): if curr_sum >= 0: length += 1 pos_sum = pos_sum + neg[i] neg_sum = neg_sum - neg[i] curr_sum = pos_sum * length + neg_sum max_sum = max(curr_sum, max_sum) print(max_sum)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
for t in range(int(input())): n = int(input()) l = list(map(int, input().split())) sumx = 0 count = 0 sumd = 0 l.sort(reverse=True) for i in l: if (sumx + i) * (count + 1) + sumd >= sumx * count + (sumd + i): sumx += i count += 1 else: sumd += i print(sumx * count + sumd)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
t = int(input()) for i in range(t): n = int(input()) a = list(map(int, input().split())) s = 0 p = 0 l = 0 res = 0 ng = [] for j in range(len(a)): if a[j] < 0: ng.append(a[j]) else: p += a[j] l += 1 ng.sort() for k in ng[::-1]: if (p + k) * (l + 1) >= p * l: p += k l += 1 else: res += k print(res + p * l)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
t = int(input()) for i in range(t): n = int(input()) a = sorted(map(int, input().split()), reverse=True) ans = s = c = 0 for j in range(n): s += a[j] if s * (j + 1) > ans: ans = s * (j + 1) c = j + 1 else: break print(ans + sum(a[c:]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) posSum = 0 negSum = 0 no = 0 neg = [] for x in a: if x <= 0: negSum += x neg.append(x) else: posSum += x no += 1 ans = posSum * no + negSum neg = sorted(neg, reverse=True) for x in neg: no += 1 posSum += x negSum -= x curr = posSum * no + negSum if curr >= 0: ans = max(ans, curr) else: break print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
t = int(input()) for e in range(t): a, ng, ps, c = [], 0, 0, 0 x, y = [], [] n = int(input()) a = list(map(int, input().split())) for i in a: if i >= 0: ps += i c += 1 x.append(i) else: y.append(i) ng += i ans = ps * c + ng if ps == 0: print(ans) continue sumy = 0 y.sort(reverse=True) for i in y: c += 1 ps += i ng -= i ans = max(ans, ps * c + ng) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR VAR LIST LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
t = int(input()) while t > 0: n = int(input()) t -= 1 list1 = list(map(int, input().split())) c = 0 neglist = [] maxi = -1 sum1 = 0 for x in list1: if x >= 0: sum1 += x c += 1 maxi = max(maxi, sum1 * c) else: neglist.append(x) ans = maxi if maxi < 0: maxi = 0 neglist.sort() neglist.reverse() for x in neglist: sum1 += x tot = sum1 * (c + 1) if maxi < max(tot, maxi): c += 1 maxi = max(tot, maxi) else: maxi += x print(maxi)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
import itertools T = int(input()) for case in range(T): n = int(input()) xs = list(map(int, input().split())) xs.sort() sl = list(itertools.accumulate(xs)) ans = sum(xs) * len(xs) s = sum(xs) for i, x in enumerate(xs): ans = max(ans, sl[i] + (s - sl[i]) * (n - i - 1)) print(ans)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
test = int(input()) for t in range(test): n = int(input()) count = 0 res = 0 exclude = 0 for number in sorted(map(int, input().split()))[::-1]: including_cur = (res + number) * (count + 1) excluding_cur = res * count + number if including_cur >= excluding_cur: res += number count += 1 else: exclude += number print(res * count + exclude)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) pos = 0 pos_size = 0 neg = 0 neg_li = [] for it in arr: if it >= 0: pos += it pos_size += 1 else: neg_li.append(it) neg += it if pos == 0: print(sum(neg_li)) elif len(neg_li) == 0: ans = pos * pos_size print(ans) else: neg_li.sort(reverse=True) mx, k, it = 0, 0, 0 for neg_el in neg_li: it += neg_el k += 1 val = (pos + it) * (pos_size + k) + (neg - it) if val > mx: mx = val print(max(pos * pos_size + it, mx))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
for _ in range(int(input())): input() a = sorted(int(x) for x in input().split()) n = s = 0 while a: if s + n * a[-1] >= 0: n += 1 x = a.pop() s += x else: break print(s * n + sum(a))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER WHILE VAR IF BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
cases = int(input()) for case_i in range(cases): n = int(input()) arr = [int(k) for k in input().split(" ")] arr.sort(reverse=True) pos_sum = 0 pos_total = 0 total_sum = 0 i = 0 while i < n and ( arr[i] >= 0 or (pos_sum + arr[i]) * (pos_total + 1) >= pos_sum * i ): pos_sum += arr[i] pos_total += 1 i += 1 print(pos_sum * pos_total + sum(arr[i:]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
t = int(input()) while t: n = int(input()) li = [int(i) for i in input().strip().split()] res, s, m = 0, 0, 0 neg = [] for i in li: if i < 0: neg += [i] else: s += i m += 1 neg.sort() for i in neg[::-1]: if (s + i) * (m + 1) >= s * m + i: s += i m += 1 else: res += i res += s * m print(res) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER VAR LIST VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
t = int(input()) for i in range(t): n = int(input()) arr = list(map(int, input().split())) ans = 0 count = 0 negatives = [] for x in arr: if x >= 0: count += 1 ans += x else: negatives.append(x) if len(negatives) == 0: print(ans * count) continue negatives.sort() temp = sum(negatives) res = ans * count + temp z = ans for j in range(len(negatives) - 1, 0, -1): z += negatives[j] count += 1 temp -= negatives[j] res = max(res, z * count + temp) print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
for _ in range(int(input())): n = int(input()) s = sorted(list(map(int, input().split())), reverse=True) negatives, positives = s[:], s[:] negatives = list(x for x in s if x < 0) positives = list(x for x in s if x >= 0) nlen = len(negatives) plen = len(positives) finalSum = sum(positives) tn = [] for item in negatives: if (finalSum + item) * (plen + 1) > plen * finalSum: finalSum += item plen += 1 else: tn.append(item) print(sum(tn) + finalSum * plen)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR IF BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
for ti in range(int(input())): n = int(input()) m = list(map(int, input().split())) m.sort(reverse=True) r = 0 s = m[0] c = 1 for i in range(1, n): if (s + m[i]) * (c + 1) >= s * c + m[i]: s = s + m[i] c = c + 1 else: r = r + s * c s = m[i] c = 1 r = r + s * c print(r)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Russian. Chef has prepared a feast with N dishes for you. You like Chef's cooking, and so you want to eat all the dishes he has prepared for you. You are also given an array A of size N, where A_{i} represents the happiness you get by eating the i-th dish.You will eat all the dishes in a series of steps. In each step, you pick a non empty subset of the remaining dishes and eat them. The happiness you get from eating these dishes is the size of the subset multiplied by the sum of the individual happiness from the dishes in the subset. You want to maximize the happiness you get from the entire feast, which is the sum of happiness in each step. ------ Input ------ The first line contains T, the number of test cases. The first line of each test case contains a single integer N, denoting the number of dishes prepared by the Chef. The second line of each test case contains contains N space-separated integers: A_{1}, A_{2}, ..., A_{N} denoting the happiness gained by eating the dishes. ------ Output ------ Output a single number denoting the maximum happiness you can get from the feast. ------ Constraints ------ 1 ≀ T ≀ 8 1 ≀ N ≀ 10^{5} -10^{8} ≀ A_{i} ≀ 10^{8} ------ Subtasks ------ Subtask #1: A_{i} ≀ 0 (30 points) Subtask #2: Original Constraints (70 points) ----- Sample Input 1 ------ 1 3 -8 0 -2 ----- Sample Output 1 ------ -10 ----- explanation 1 ------ Example case 1. You can eat the first dish in the first step, the second dish in the second step and the third dish in the third step. total happiness = 1*(-8) + 1*0 + 1*(-2) = -10 ----- Sample Input 2 ------ 1 3 1 2 3 ----- Sample Output 2 ------ 18 ----- explanation 2 ------
t = int(input().strip()) def partition(a, p): j = -1 a[p], a[-1] = a[-1], a[p] pivot = a[-1] for i in range(len(a) - 1): if a[i] < pivot or a[i] == pivot and pivot < 0: j += 1 a[i], a[j] = a[j], a[i] a[j + 1], a[-1] = a[-1], a[j + 1] return j + 1 def closeToZero(a): ctz = 0 if a[ctz] == 0: return ctz for i in range(1, len(a)): if a[i] == 0: ctz = i break if abs(a[i]) < abs(a[ctz]): ctz = i return ctz while t: n = int(input().strip()) a = [int(x) for x in input().split()] s = 0 part = closeToZero(a) part = partition(a, part) neg = -1 if a[part] >= 0 and part > 0: neg = part - 1 elif a[part] < 0: neg = part if neg != -1: a[0 : neg + 1] = sorted(a[0 : neg + 1]) flag = False for i in a: if i > 0: flag = True break if flag == False: s = sum(a[: neg + 1]) else: ss = sum(a[neg + 1 :]) ll = len(a[neg + 1 :]) dd = sum(a[0 : neg + 1]) best = ll * ss + dd while neg >= 0: ll += 1 ss += a[neg] dd -= a[neg] b = ll * ss + dd if b > best: best = b neg -= 1 s = best print(s) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR RETURN VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR WHILE VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER