description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
This is the easier version of the problem. In this version $1 \le n, m \le 100$. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 100$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
import sys def main(): n = int(input()) al = [[int(x), n - i] for i, x in enumerate(input().split())] al.sort() al.reverse() arr = [[[] for i in range(n)] for i in range(n)] for i in range(1, n + 1): for j in range(i - 1, n): arr[j][n - al[i - 1][1]] = al[i - 1][0] for i in range(n): arr[i] = list(filter(None, arr[i])) q = int(input()) for _ in range(q): k, ind = map(int, input().split()) print(arr[k - 1][ind - 1]) main()
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR NONE VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR
This is the easier version of the problem. In this version $1 \le n, m \le 100$. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 100$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
n = int(input()) s = list(map(int, input().split())) for i in range(n): s[i] = s[i], i mx = sorted(s, key=lambda y: y[0], reverse=True) m = int(input()) for i in range(m): k, pos = map(int, input().split()) pos -= 1 a = 0 last = mx[0][0] res = [] d = dict() d2 = dict() for x in range(k): if mx[x][0] not in d: d[mx[x][0]] = 1 else: d[mx[x][0]] += 1 for x in range(n): if mx[x][0] != last: a += 1 last = mx[x][0] if a == k: break if mx[x][0] not in d2: d2[mx[x][0]] = [mx[x][1]] else: d2[mx[x][0]].append(mx[x][1]) for a in list(d.keys()): d2[a].sort() for j in range(d[a]): res.append((a, d2[a][j])) res = sorted(res, key=lambda y: y[1]) print(res[pos][0])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER LIST VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
This is the easier version of the problem. In this version $1 \le n, m \le 100$. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 100$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
n = int(input()) line = list(map(int, input().split())) m = int(input()) for _ in range(m): k, pos = map(int, input().split()) lf = line[:] while len(lf) > k: j = -1 x = min(lf) while j > -len(lf) - 1: if lf[j] == x: del lf[j] if len(lf) == k: break else: j -= 1 print(lf[pos - 1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
This is the easier version of the problem. In this version $1 \le n, m \le 100$. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 100$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
n = int(input()) data = list(map(int, input().split())) m = int(input()) for _ in range(m): k, pos = map(int, input().split()) s = data[:] ans = [] for i in range(k): x = s.index(max(s)) ans.append(x) s[x] = -1 ans.sort() print(data[ans[pos - 1]])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER
This is the easier version of the problem. In this version $1 \le n, m \le 100$. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 100$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
n = int(input()) a = list(map(int, input().split())) b = [] for i in range(n): b.append([a[i], -i]) b.sort(reverse=True) for i in range(int(input())): k, pos = map(int, input().split()) ans = 0 z = [] for i in range(k): ans += b[i][0] z.append([-b[i][1], b[i][0]]) z.sort() print(z[pos - 1][1])
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 FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER
This is the easier version of the problem. In this version $1 \le n, m \le 100$. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 100$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
n = int(input()) arr = list(map(int, input().split())) q = int(input()) qs = [] for i in range(q): a, b = map(int, input().split()) qs.append([a, b - 1, i]) qs.sort() ret = [] used = [(0) for i in range(n)] ans = [(-1) for i in range(q)] for z in range(q): l, pos, o = qs[z] while len(ret) < l: mx = 0 ind = -1 for i in range(n): if not used[i]: if mx < arr[i]: mx = arr[i] ind = i used[ind] = 1 ret.append(mx) c = [] for i in range(n): if used[i]: c.append(arr[i]) ans[qs[z][2]] = c[pos] for i in range(q): print(ans[i])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
This is the easier version of the problem. In this version $1 \le n, m \le 100$. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 100$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
n = int(input()) a = [int(x) for x in input().split()] b = [[] for i in range(n + 3)] for g in range(1, n + 1): k = g cnt = 0 mm = 10**10 m = 0 for i in range(n): if len(b[g]) != 0: mm = min(b[g]) if cnt == k: if a[i] > mm: for j in range(k - 1, -1, -1): if b[g][j] == mm: b[g].pop(j) break b[g].append(a[i]) else: b[g].append(a[i]) cnt += 1 m = max(b[g]) m = int(input()) for i in range(m): k, pos = [int(x) for x in input().split()] pos -= 1 print(b[k][pos])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR IF VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR
This is the easier version of the problem. In this version $1 \le n, m \le 100$. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 100$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
n = int(input()) arr = list(map(int, input().split())) arr_sorted = [(arr[i], i) for i in range(n)] arr_sorted.sort(key=lambda el: (el[0], -el[1]), reverse=True) m = int(input()) for req_i in range(m): k, pos = map(int, input().split()) pos -= 1 cur_arr = arr_sorted[:k] cur_arr.sort(key=lambda el: el[1]) print(cur_arr[pos][0])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
This is the easier version of the problem. In this version $1 \le n, m \le 100$. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 100$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
n = int(input()) a = [int(i) for i in input().split()] b = sorted(a) b.reverse() m = int(input()) for _ in range(m): k, pos = map(int, input().split()) x = 0 j = 0 d = dict() ans = 0 for i in b[:k]: d[i] = d.get(i, 0) + 1 while x != pos: u = a[j] if u in d: d[u] -= 1 x += 1 if d[u] == 0: del d[u] j += 1 ans = u print(ans)
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 EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
This is the easier version of the problem. In this version $1 \le n, m \le 100$. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 100$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
n = int(input()) a = [int(i) for i in input().split()] b = sorted(a) c = [] c.append(a) for i in range(1, n + 1): k = len(c[i - 1]) - 1 - c[i - 1][::-1].index(b[i - 1]) c.append(c[i - 1][0:k] + c[i - 1][k + 1 :]) m = int(input()) for i in range(m): k, pos = map(int, input().split()) print(c[len(c) - k - 1][pos - 1])
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 LIST EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR 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 VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER
This is the easier version of the problem. In this version $1 \le n, m \le 100$. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 100$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
n = int(input()) aa = [int(i) for i in input().split()] dic = {} for i in range(0, n): if aa[i] not in dic: dic[aa[i]] = [i] else: dic[aa[i]].append(i) ll = sorted(dic)[::-1] m = int(input()) for _ in range(0, m): k, pos = map(int, input().split()) ans = [] for i in range(0, len(ll)): if len(dic[ll[i]]) < k: ans += dic[ll[i]] k -= len(dic[ll[i]]) else: ans += dic[ll[i]][:k] break print(aa[sorted(ans)[pos - 1]])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
This is the easier version of the problem. In this version $1 \le n, m \le 100$. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 100$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
import sys as _sys def main(): t = 1 for i_t in range(t): (n,) = _read_ints() a = tuple(_read_ints()) (m,) = _read_ints() queries = (tuple(_read_ints()) for i_query in range(m)) result = process_queries(a, queries) print(*result, sep="\n") def _read_line(): result = _sys.stdin.readline() assert result[-1] == "\n" return result[:-1] def _read_ints(): return map(int, _read_line().split()) def process_queries(sequence, queries): sequence = tuple(sequence) sorted_sequence = sorted(sequence, reverse=True) for k, pos in queries: pos -= 1 k_max_elems = sorted_sequence[:k][::-1] seq = sequence subseq = [] while len(subseq) < k: for i_next_elem in range(len(k_max_elems)): next_elem = k_max_elems[i_next_elem] seq_after = seq[seq.index(next_elem) + 1 :] elems_remain = ( k_max_elems[:i_next_elem] + k_max_elems[i_next_elem + 1 :] ) if _contains_elems(seq_after, elems_remain): seq = seq_after k_max_elems = elems_remain subseq.append(next_elem) break yield subseq[pos] def _contains_elems(seq, elems): seq = sorted(seq) subseq = sorted(elems) i_seq = 0 i_subseq = 0 while i_seq < len(seq) and i_subseq < len(subseq): if seq[i_seq] == subseq[i_subseq]: i_subseq += 1 i_seq += 1 return i_subseq == len(subseq) main()
IMPORT FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER STRING RETURN VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
This is the easier version of the problem. In this version $1 \le n, m \le 100$. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 100$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
n = int(input()) arr = list(map(int, input().split())) arr = [(i, num) for num, i in enumerate(arr)] arr.sort(key=lambda x: (-x[0], x[1])) m = int(input()) for q in range(m): k, pos = tuple(map(int, input().split())) now = [] for i in arr: if len(now) == k: break now.append(i) now.sort(key=lambda x: x[1]) print(now[pos - 1][0])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER
This is the easier version of the problem. In this version $1 \le n, m \le 100$. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 100$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
n = int(input()) s = sorted([[v, -i] for i, v in enumerate(map(int, input().split()))]) for _ in range(int(input())): k, i = map(int, input().split()) ans = sorted(s[-k:], key=lambda x: -x[1]) print(ans[i - 1][0])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR LIST VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER
This is the easier version of the problem. In this version $1 \le n, m \le 100$. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 100$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
n = int(input()) c = list(map(lambda x: (int(x[1]), -x[0]), enumerate(input().split()))) so = sorted(c) for i in range(int(input())): k, r = map(int, input().split()) now = so[-k:] now.sort(key=lambda x: -x[1]) print(now[r - 1][0])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER
This is the easier version of the problem. In this version $1 \le n, m \le 100$. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 100$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
from sys import stdin n = int(stdin.readline()) alist = list(map(int, stdin.readline().split())) aindlist = sorted( [(elem, i) for i, elem in enumerate(alist)], key=lambda x: (-x[0], x[1]) ) m = int(stdin.readline()) ans = [] for _ in range(m): k, pos = map(int, stdin.readline().split()) temp = [x[1] for x in aindlist[:k]] temp.sort() ans.append(alist[temp[pos - 1]]) print(*ans, sep="\n")
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 VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING
This is the easier version of the problem. In this version $1 \le n, m \le 100$. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 100$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
n = int(input()) A = list(map(int, input().split())) L = A.copy() L2 = L.copy() A.sort() c = int(input()) r = [] def S(x, L): idx = L.index(x) L[idx] = -1 return [idx, x] for i in range(c): k, pos = map(int, input().split()) B = A[: 0 - k - 1 : -1] H = list(map(lambda x: S(x, L), B)) H.sort() N = list(map(lambda x: x[1], H)) r += [N[pos - 1]] L = L2.copy() print("\n".join(map(str, r)))
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 ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER RETURN LIST VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
This is the easier version of the problem. In this version $1 \le n, m \le 100$. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 100$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
n = int(input()) p = list(map(int, input().split())) p = sorted(list(enumerate(p)), key=lambda x: -x[1]) a = [[]] for i in range(n): a.append(sorted(a[-1] + [p[i]], key=lambda x: x[0])) m = int(input()) for _ in range(m): x, y = map(int, input().split()) print(a[x][y - 1][1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER LIST VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER
This is the easier version of the problem. In this version $1 \le n, m \le 100$. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 100$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
n = int(input()) a = [int(x) for x in input().split()] z = a[:] z.sort(reverse=True) an = [[] for i in range(n + 1)] an[0] = a[:] for i in range(n): for j in range(len(a) - 1, -1, -1): if a[j] == z[-1]: del a[j] z.pop() break an[i + 1] = a[:] an = an[::-1] for j in range(int(input())): x, y = map(int, input().split()) print(an[x][y - 1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER
This is the easier version of the problem. In this version $1 \le n, m \le 100$. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 100$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
n = int(input()) nar = list(map(int, input().split())) m = int(input()) for q in range(m): k, p = list(map(int, input().split())) mar = [] for i in range(k): mar.append(nar[i]) for i in range(k, n): minItem = 0 for j in range(1, k): if mar[j] <= mar[minItem]: minItem = j if nar[i] > mar[minItem]: mar.pop(minItem) mar.append(nar[i]) print(mar[p - 1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
This is the easier version of the problem. In this version $1 \le n, m \le 100$. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 100$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
n = int(input()) a = list(map(int, input().split())) m = int(input()) for i in range(m): k, pos = map(int, input().split()) save = [] for j in range(len(a)): save.append(a[j]) ans = [] for x in range(k): maximum = 0 pr = -1 for j in range(len(save)): if save[j] > maximum: maximum = save[j] pr = j ans.append(maximum) save.pop(pr) ans.sort() answer = [] for i in range(len(a)): if a[i] in ans: answer.append(a[i]) ans.pop(ans.index(a[i])) print(answer[pos - 1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
This is the easier version of the problem. In this version $1 \le n, m \le 100$. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 100$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
def main(): n = int(input()) a = list(enumerate(map(int, input().split()))) a.sort(key=lambda item: (item[1], -item[0])) m = int(input()) for i in range(m): k, pos = map(int, input().split()) s = a[-k:] s = sorted(s) print(s[pos - 1][1]) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR
This is the easier version of the problem. In this version $1 \le n, m \le 100$. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 100$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
n = int(input()) a = [int(i) for i in input().split()] b = [(a[i], n - i) for i in range(n)] b.sort(reverse=True) b = [(b[i][0], n - b[i][1]) for i in range(n)] m = int(input()) for qu in range(m): k, p = list(map(int, input().split())) c = b[:k] c.sort(key=lambda x: x[1]) print(c[p - 1][0])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER
This is the easier version of the problem. In this version $1 \le n, m \le 100$. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 100$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
n = int(input()) a = sorted( zip(list(map(int, input().split())), list(range(n))), key=lambda x: (x[0], -x[1]), reverse=True, ) m = int(input()) for i in range(m): k, pos = list(map(int, input().split())) b = sorted(a[:k], key=lambda x: x[1]) print(b[pos - 1][0])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER
This is the easier version of the problem. In this version $1 \le n, m \le 100$. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 100$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
a = int(input()) Ans = [] A = list(map(int, input().split())) for i in range(a): A[i] = [A[i], -i] A.sort() for i in range(a): A[i][1] = -A[i][1] for i in range(int(input())): n, m = map(int, input().split()) B = list(A[a - n :]) B.sort(key=lambda n: n[1]) Ans.append(B[m - 1][0]) for an in Ans: print(an)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR
This is the easier version of the problem. In this version $1 \le n, m \le 100$. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 100$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
n = int(input()) b = list(map(int, input().split())) a = [] for i in range(n): a.append([b[i], n - i]) a.sort() a.reverse() p = [] t = [] for i in range(n): t.append(a[i]) d = t.copy() d.sort(key=lambda x: -x[1]) p.append(d) m = int(input()) for i in range(m): k, pos = map(int, input().split()) d = p[k - 1].copy() print(d[pos - 1][0])
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 FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER
This is the easier version of the problem. In this version $1 \le n, m \le 100$. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 100$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
n = int(input()) data = input().split() data1 = [] for i in range(n): data1.append((int(data[i]), i)) data1.sort(key=lambda x: (x[0], -x[1])) for i in range(int(input())): k, pos = map(int, input().split()) temp = sorted(data1[len(data1) - k :], key=lambda x: x[1]) print(temp[pos - 1][0])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER
This is the easier version of the problem. In this version $1 \le n, m \le 100$. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 100$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
n = int(input()) a = list(map(int, input().split())) u = sorted(a) m = int(input()) ans = [] for _ in range(m): k, pos = map(int, input().split()) d = [] p = [] s = u[n - k :] for i in range(n): if len(d) == k: break for j in range(len(s)): if s[j] == a[i]: s[j] = -1 d.append(i) break d.sort() ans.append(a[d[pos - 1]]) print("\n".join(map(str, ans)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
This is the easier version of the problem. In this version $1 \le n, m \le 100$. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 100$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
n = int(input()) arr = list(map(int, input().split())) m = int(input()) q = [] for i in range(m): a, b = map(int, input().split()) q.append((a, b)) def f(arr, m): vis = [False] * len(arr) arr2 = [] for i in range(len(arr)): arr2.append([arr[i], n - i]) arr2.sort(reverse=True) arr2 = arr2[:m] for i in range(len(arr2)): arr2[i][1] = n - arr2[i][1] arr2.sort(key=lambda x: x[1]) res = [] for i in range(len(arr2)): res.append(arr2[i][0]) return res for i in q: x = f(arr, i[0]) print(x[i[1] - 1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER
This is the easier version of the problem. In this version $1 \le n, m \le 100$. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 100$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
n = int(input()) arr = [int(i) for i in input().split()] sor = [[arr[i], n - i] for i in range(n)] sor.sort() m = int(input()) for i in range(m): op = [] [q, index] = [int(i) for i in input().split()] for j in range(q): op.append(n - sor[-1 - j][1]) op.sort() print(arr[op[index - 1]])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN LIST VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER
This is the easier version of the problem. In this version $1 \le n, m \le 100$. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 100$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
n = int(input()) a = list(map(int, input().split())) a1 = sorted(a, key=lambda x: -x) m = int(input()) for i in range(m): k, p = list(map(int, input().split())) c = {} c1 = {} s = [] for j in range(n + 1): s.append({}) pos = {} for j in range(n): c[a1[j]] = 0 c1[a1[j]] = 0 s[0][a[j]] = 0 pos[a1[j]] = [] for j in range(n): s[0][a[j]] += 1 for j in range(n): for t in range(n): s[j + 1][a[t]] = s[j][a[t]] s[j + 1][a[j]] -= 1 b = [] for j in range(k): c[a1[j]] += 1 for j in range(k, n): c1[a1[j]] += 1 ns = [] us = [0] * n ans = [] for j in range(n - 1, -1, -1): if c1[a[j]] ^ 0: c1[a[j]] -= 1 else: ans.append(a[j]) for j in range(len(ans) // 2): ans[j], ans[len(ans) - 1 - j] = ans[len(ans) - 1 - j], ans[j] print(ans[p - 1])
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 VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR LIST FOR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
This is the easier version of the problem. In this version $1 \le n, m \le 100$. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 100$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
n = int(input()) b = list(map(int, input().split())) a = [([0] * 2) for i in range(n)] for i in range(n): a[i][0] = b[i] a[i][1] = i for i in range(n - 1): for j in range(n - i - 1): if a[j][0] > a[j + 1][0]: a[j], a[j + 1] = a[j + 1], a[j] elif a[j][0] == a[j + 1][0] and a[j][1] < a[j + 1][1]: a[j], a[j + 1] = a[j + 1], a[j] m = int(input()) for k in range(m): k, ind = map(int, input().split()) ans = [([0] * 2) for i in range(k)] for i in range(k): ans[i][0] = a[n - i - 1][1] ans[i][1] = a[n - i - 1][0] ans.sort() print(ans[ind - 1][1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER
This is the easier version of the problem. In this version $1 \le n, m \le 100$. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 100$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
def get_index(lt, vec): max_el = 0 pos = None for i in range(len(vec)): if i not in lt: if max_el < vec[i]: max_el = vec[i] pos = i return pos n = int(input()) vec = [int(x) for x in input().split()] lst = [[vec.index(max(vec))]] for i in range(n - 1): lt = list(lst[i]) ind = get_index(lt, vec) lt.append(ind) lst.append(lt) for l in lst: l.sort() m = int(input()) for i in range(m): k, pos = [int(x) for x in input().split()] print(vec[lst[k - 1][pos - 1]])
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NONE FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST LIST FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER
This is the easier version of the problem. In this version $1 \le n, m \le 100$. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 100$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
import sys def f(a): return -a[1] fin = sys.stdin n = int(input()) arr = list(map(int, fin.readline().split())) arr_ind = list() for i in range(n): arr_ind.append([arr[i], -i]) arr_ind.sort() arr_ind.reverse() ans_array = list() for k in range(1, n + 1): ans = arr_ind[:k] ans.sort(key=f) ans_array.append(ans) tests = int(input()) for test in range(tests): k, pos = map(int, input().split()) print(ans_array[k - 1][pos - 1][0])
IMPORT FUNC_DEF RETURN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER
This is the easier version of the problem. In this version $1 \le n, m \le 100$. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 100$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
n = int(input()) a = list(map(int, input().split())) s_a = sorted(a) m = int(input()) req = [] for _ in range(m): req.append(list(map(int, input().split()))) d = dict() for r in req: if r[0] in d.keys(): print(d[r[0]][r[1] - 1]) else: del_indx = [] for e in s_a[: n - r[0]]: for i in reversed(range(n)): if a[i] == e: if i not in del_indx: del_indx.append(i) break new_a = [] for i in range(n): if i not in del_indx: new_a.append(a[i]) d[r[0]] = new_a print(d[r[0]][r[1] - 1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR NUMBER FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER
This is the harder version of the problem. In this version, $1 \le n, m \le 2\cdot10^5$. You can hack this problem if you locked it. But you can hack the previous problem only if you locked both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 2\cdot10^5$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 2\cdot10^5$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
import sys as _sys def main(): (n,) = _read_ints() a = tuple(_read_ints()) (m,) = _read_ints() queries = (tuple(_read_ints()) for i_query in range(m)) result = process_queries(a, queries) print(*result, sep="\n") def _read_line(): result = _sys.stdin.readline() assert result[-1] == "\n" return result[:-1] def _read_ints(): return map(int, _read_line().split()) def process_queries(sequence, queries): sequence = tuple(sequence) indices_to_select = sorted( range(len(sequence)), key=lambda index: (-sequence[index], index) ) enumerated_queries = sorted(enumerate(queries), key=lambda iv: iv[1][0])[::-1] queries_responses = [None] * len(enumerated_queries) selections_tree = [0] * (len(sequence) + 1) selected_n = 0 for index_to_select in indices_to_select: _fenwick_tree_add(selections_tree, index_to_select, 1) selected_n += 1 while enumerated_queries and enumerated_queries[-1][1][0] == selected_n: query_index, (_k, subseq_index) = enumerated_queries.pop() seq_index = _find_seq_index_by_subseq_index(selections_tree, subseq_index) queries_responses[query_index] = sequence[seq_index] return queries_responses def _find_seq_index_by_subseq_index(tree, subseq_i): seq_length = len(tree) - 1 min_i = 0 max_i = seq_length - 1 while min_i != max_i: mid_i = (min_i + max_i) // 2 if _fenwick_tree_prefix_sum(tree, mid_i) < subseq_i: min_i = mid_i + 1 else: max_i = mid_i return min_i def _fenwick_tree_prefix_sum(tree, i): i += 1 result = 0 while i != 0: result += tree[i] i -= i & -i return result def _fenwick_tree_add(tree, i, x): i += 1 while i < len(tree): tree[i] += x i += i & -i main()
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER STRING RETURN VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NONE FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR NUMBER NUMBER NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR FUNC_DEF VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR
This is the harder version of the problem. In this version, $1 \le n, m \le 2\cdot10^5$. You can hack this problem if you locked it. But you can hack the previous problem only if you locked both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 2\cdot10^5$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 2\cdot10^5$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
import sys WIDTH = 10 def index_tree(n): levels = [[1] * n] size = WIDTH while size < n: m, r = n // size, n % size levels.append([size] * m + ([r] if r > 0 else [])) size *= WIDTH return levels def dec_index(levels, i): for level in levels: level[i] -= 1 i //= WIDTH def find_pos(levels, pos): i, l = 0, len(levels) - 1 total = 0 while True: level = levels[l] while total + level[i] < pos: total += level[i] i += 1 if l == 0: return i i *= WIDTH l -= 1 def main(): numbers = [int(x) for x in sys.stdin.read().split()] n = numbers[0] sequence = numbers[1 : n + 1] m = numbers[n + 1] queries = {} for i in range(n + 2, n + 2 + 2 * m, 2): k, pos = numbers[i], numbers[i + 1] if k in queries: queries[k][pos] = None else: queries[k] = {pos: None} sequence1 = sorted([(s, -i) for i, s in enumerate(sequence)]) tree = index_tree(n) size = n for _, neg_i in sequence1: if size in queries: for pos in queries[size]: queries[size][pos] = find_pos(tree, pos) dec_index(tree, -neg_i) size -= 1 for i in range(n + 2, n + 2 + 2 * m, 2): k, pos = numbers[i], numbers[i + 1] print(sequence[queries[k][pos]]) main()
IMPORT ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR LIST BIN_OP LIST NUMBER VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP LIST VAR VAR VAR NUMBER LIST VAR LIST VAR VAR RETURN VAR FUNC_DEF FOR VAR VAR VAR VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR VAR VAR WHILE BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER RETURN VAR VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NONE ASSIGN VAR VAR DICT VAR NONE ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR VAR VAR IF VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR
In an embassy of a well-known kingdom an electronic queue is organised. Every person who comes to the embassy, needs to make the following three actions: show the ID, pay money to the cashier and be fingerprinted. Besides, the actions should be performed in the given order. For each action several separate windows are singled out: k1 separate windows for the first action (the first type windows), k2 windows for the second one (the second type windows), and k3 for the third one (the third type windows). The service time for one person in any of the first type window equals to t1. Similarly, it takes t2 time to serve a person in any of the second type windows. And it takes t3 to serve one person in any of the third type windows. Thus, the service time depends only on the window type and is independent from the person who is applying for visa. At some moment n people come to the embassy, the i-th person comes at the moment of time ci. The person is registered under some number. After that he sits in the hall and waits for his number to be shown on a special board. Besides the person's number the board shows the number of the window where one should go and the person goes there immediately. Let's consider that the time needed to approach the window is negligible. The table can show information for no more than one person at a time. The electronic queue works so as to immediately start working with the person who has approached the window, as there are no other people in front of the window. The Client Service Quality inspectors noticed that several people spend too much time in the embassy (this is particularly tiresome as the embassy has no mobile phone reception and 3G). It was decided to organise the system so that the largest time a person spends in the embassy were minimum. Help the inspectors organise the queue. Consider that all actions except for being served in at the window, happen instantly. Input The first line contains three space-separated integers k1, k2, k3 (1 ≀ ki ≀ 109), they are the number of windows of the first, second and third type correspondingly. The second line contains three space-separated integers t1, t2, t3 (1 ≀ ti ≀ 105), they are the periods of time needed to serve one person in the window of the first, second and third type correspondingly. The third line contains an integer n (1 ≀ n ≀ 105), it is the number of people. The fourth line contains n space-separated integers ci (1 ≀ ci ≀ 109) in the non-decreasing order; ci is the time when the person number i comes to the embassy. Output Print the single number, the maximum time a person will spend in the embassy if the queue is organized optimally. Please, do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams (also you may use the %I64d specificator). Examples Input 1 1 1 1 1 1 5 1 1 1 1 1 Output 7 Input 2 1 1 5 1 1 5 1 2 3 3 5 Output 13 Note In the first test 5 people come simultaneously at the moment of time equal to 1. There is one window of every type, it takes 1 unit of time to be served at each window. That's why the maximal time a person spends in the embassy is the time needed to be served at the windows (3 units of time) plus the time the last person who comes to the first window waits (4 units of time). Windows in the second test work like this: The first window of the first type: [1, 6) β€” the first person, [6, 11) β€” third person, [11, 16) β€” fifth person The second window of the first type: [2, 7) β€” the second person, [7, 12) β€” the fourth person The only second type window: [6, 7) β€” first, [7, 8) β€” second, [11, 12) β€” third, [12, 13) β€” fourth, [16, 17) β€” fifth The only third type window: [7, 8) β€” first, [8, 9) β€” second, [12, 13) β€” third, [13, 14) β€” fourth, [17, 18) β€” fifth We can see that it takes most time to serve the fifth person.
q = zip(map(int, input().split()), map(int, input().split())) n = int(input()) c = list(map(int, input().split())) d = c[:] for k, t in q: for i in range(min(k, n)): c[i] += t for i in range(k, n): c[i] = max(c[i], c[i - k]) + t print(max(c[i] - d[i] for i in range(n)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL 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 VAR FOR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
s = input() m = ["z" for i in range(len(s))] m[-1] = s[-1] c = s[-1] for i in range(len(s) - 2, -1, -1): if s[i] < c: c = s[i] m[i] = c ind = m.index(min(m)) l = [] res = "" for i in range(len(s)): while l and l[-1] <= m[i]: res += l.pop() l.append(s[i]) print(res + "".join(map(str, l[::-1])))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR NUMBER
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
from itertools import takewhile def f(s): t = [] u = [] chars = "abcdefghijklmnopqrstuvwxyz" for c in chars: stack = list(takewhile(lambda x: x <= c, reversed(t))) count = len(stack) if count > 0: u += stack t = t[:-count] count = s.count(c) if count > 0: rindex = s.rindex(c) u += c * count t += [x for x in s[:rindex] if x != c] s = s[rindex + 1 :] u += reversed(t) return "".join(u) print(f(input()))
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR STRING FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR RETURN FUNC_CALL STRING VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
s = input() resultBase = "" resultRest = "" best = len(s) - 1 mini = [0] * len(s) for i in range(len(s) - 1, -1, -1): mini[i] = best if s[best] >= s[i]: best = i for i in range(len(s)): resultRest += s[i] while len(resultRest) > 0 and resultRest[-1] <= s[mini[i]]: resultBase += resultRest[-1] resultRest = resultRest[:-1] print(resultBase + resultRest[::-1])
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
s = input() c = ["z"] * (len(s) + 1) for i in range(len(s) - 1, -1, -1): c[i] = min(s[i], c[i + 1]) l = [] j = 0 while len(l) > 0 or j < len(s): if len(l) > 0 and l[len(l) - 1] <= c[j]: print(l[len(l) - 1], end="") l.pop() else: l.append(s[j]) j += 1
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
s = input() leng = len(s) minarray = [0] * leng mins = "z" for i in range(leng - 1, -1, -1): if s[i] < mins: mins = s[i] minarray[i] = mins st = [] fist = [] for i in range(leng): while st != [] and st[-1] <= minarray[i]: fist.append(st[-1]) st.pop() if s[i] == minarray[i]: fist.append(s[i]) else: st.append(s[i]) while st != []: fist.append(st[-1]) st.pop() print("".join(fist))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR WHILE VAR LIST VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR WHILE VAR LIST EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
s = input() t = list() ans = "" index = 0 for character in "abcdefghijklmnopqrstuvwxyz": while len(t) > 0 and t[-1] <= character: ans += t[-1] t.pop() pos = s.find(character, index) while pos >= 0: while index < pos: t.append(s[index]) index += 1 ans += character index += 1 pos = s.find(character, index) while len(t) > 0: ans += t[-1] t.pop() print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR STRING WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
s, t, u = input(), [], "" j, n = 0, len(s) p = ["|"] * (n + 1) for i in range(n, 0, -1): p[i - 1] = min(s[i - 1], p[i]) while j < n: if t and t[-1] <= p[j]: u += t.pop() else: k = p[j] while p[j] == k: if s[j] == p[j]: u += s[j] else: t += [s[j]] j += 1 print(u + "".join(t[::-1]))
ASSIGN VAR VAR VAR FUNC_CALL VAR LIST STRING ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST STRING BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR WHILE VAR VAR IF VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR WHILE VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR LIST VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL STRING VAR NUMBER
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
def main(): s = list(input()) suffix = [] for x in reversed(s): if suffix: suffix.append(min(suffix[-1], x)) else: suffix.append(x) suffix = suffix[::-1] u = [] t = [] i = 0 while True: m = suffix[i] while t and t[-1] <= m: u.append(t[-1]) t.pop() while s[i] != m: t.append(s[i]) i += 1 u.append(s[i]) i += 1 if i == len(s): break u += t[::-1] print("".join(u)) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR WHILE VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
s = input() stack, res, k = [], [], [0] * 26 for c in s: k[ord(c) - 97] += 1 for c in s: stack.append(c) k[ord(c) - 97] -= 1 while stack and sum(k[: ord(stack[-1]) - 97]) == 0: res.append(stack.pop()) print("".join(res))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR LIST LIST BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
ls = [0] * 26 def getFirst(): for i in range(0, 26): if ls[i] > 0: return i def remover(i): ls[i] -= 1 s = input() c = {} for i in s: n = ord(i) - 97 if n in c: c[n] += 1 else: c[n] = 1 ks = [*c] for i in ks: ls[i] = c[i] stack = [] ans = "" i = int(0) while i < len(s): f = getFirst() n = ord(s[i]) - 97 if len(stack) != 0: if ord(stack[len(stack) - 1]) - 97 <= f: ans += stack.pop() i -= 1 else: stack.append(s[i]) remover(n) else: stack.append(s[i]) remover(n) i += 1 while len(stack) > 0: a = stack.pop() ans += a print(ans)
ASSIGN VAR BIN_OP LIST NUMBER NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR NUMBER RETURN VAR FUNC_DEF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
s = input() l = len(s) mn = [0] * l mnn = s[-1] for i in range(l - 1, -1, -1): mnn = min(mnn, s[i]) mn[i] = mnn st = [] for i in range(0, l): while len(st) != 0 and st[-1] <= mn[i]: print(st.pop(), end="") st.append(s[i]) while len(st) != 0: print(st.pop(), end="")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR STRING
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
s = list(input()) s.reverse() n = len(s) min_char = [s[0]] for i in range(1, n): min_char.append(min(s[i], min_char[i - 1])) t = [] u = [] while len(u) < n: if len(t) == 0: t.append(s.pop()) elif len(s) == 0: u.append(t.pop()) elif min_char[len(s) - 1] < t[-1]: t.append(s.pop()) else: u.append(t.pop()) print("".join(u))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
def find_min(count): for i in range(26): if count[i] > 0: return i return -1 def calc_pos(letter): return ord(letter) - ord("a") s = input() count = [(0) for i in range(26)] for c in s: count[calc_pos(c)] += 1 t = [] u = "" for i in range(len(s)): t.append(s[i]) count[calc_pos(s[i])] -= 1 while t and find_min(count) != -1 and calc_pos(t[-1]) <= find_min(count): u += t.pop() while len(t) > 0: u += t.pop() print(u)
FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER RETURN VAR RETURN NUMBER FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
s = list(input()) t = [] u = [] letters = [[] for _ in range(ord("z") - ord("a") + 1)] for index, letter in enumerate(s): letters[ord(letter) - ord("a")] += [index] for indexes in letters: indexes.reverse() current = 0 while current < len(s): if len(t) == 0: t += s[current] letters[ord(s[current]) - ord("a")].pop() current += 1 continue best_in_word = chr(ord("z") + 1) best_index = -1 for i in range(ord("z") - ord("a") + 1): if len(letters[i]) != 0: best_in_word = chr(ord("a") + i) best_index = letters[i][-1] break if t[-1] <= best_in_word: u += t[-1] t.pop() continue while current <= best_index: t += s[current] letters[ord(s[current]) - ord("a")].pop() current += 1 while len(t) > 0: u += t[-1] t.pop() print("".join(u))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR STRING FUNC_CALL VAR STRING NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING LIST VAR FOR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR STRING FUNC_CALL VAR STRING NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR WHILE VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
def solve(s): suffix_min = [] t = [] u = [] m = "z" for c in reversed(s): m = min(m, c) suffix_min.append(m) for c, m in zip(s, reversed(suffix_min)): while len(t) > 0: top = t.pop() if top <= m: u.append(top) else: t.append(top) break if c > m: t.append(c) else: u.append(c) return "".join(u) + "".join(reversed(t)) def main(): print(solve(input())) main()
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL STRING VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
def place(c): return ord(c) - ord("a") s, stk, res, freq = input(), [], [], [(0) for i in range(26)] for c in s: freq[place(c)] += 1 for c in s: freq[place(c)], isLowAvailable, _ = freq[place(c)] - 1, False, stk.append(c) while stk and not isLowAvailable: for i in range(place(stk[-1]) - 1, -1, -1): if freq[i]: isLowAvailable = True if not isLowAvailable: res.append(stk.pop()) while stk: res.append(stk.pop()) print("".join(res))
FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR FUNC_CALL VAR LIST LIST NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR WHILE VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
s = input() min_s = [chr(123)] * len(s) min_s[-1] = s[-1] for i in range(len(s) - 2, -1, -1): min_s[i] = min(s[i], min_s[i + 1]) t = [] u = [] for i in range(len(s)): if len(t) and min_s[i] >= t[-1]: while len(t) and t[-1] <= min_s[i]: u.append(t.pop()) if s[i] == min_s[i]: u.append(s[i]) else: t.append(s[i]) u = u + t[::-1] assert len(u) == len(s) print("".join(u))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
s = input() n = len(s) cur = "z", n mi = [cur for _ in range(n)] ns = ["z"] * n for i in range(n - 1, -1, -1): if (s[i], i) < cur: cur = s[i], i mi[i] = cur ns[i] = s[i] pos = 0 cache = list() res = "" while len(res) < n: c, i = mi[pos] res += c cache += ns[pos:i] pos = i if cache: val = cache[-1] mi[pos] = val, pos ns[pos] = val if pos < n - 1: mi[pos] = min(mi[pos], mi[pos + 1]) cache.pop() else: pos += 1 print(res)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST STRING VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
s = [char for char in input().strip()] alpha_dict = {} for alpha in "qwertyuiopasdfghjklzxcvbnm": alpha_dict[alpha] = 0 for char in s: alpha_dict[char] += 1 t = [] u = [] for char in s: t.append(char) alpha_dict[char] -= 1 while t: top = t[-1] smaller_found = False for alpha in alpha_dict: if alpha < top and alpha_dict[alpha] > 0: smaller_found = True break if smaller_found: break else: u.append(t.pop()) while t: u.append(t.pop()) print(*u, sep="")
ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR STRING ASSIGN VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR STRING
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
s = input() u = "" t = [] d1 = [0] * 26 d2 = [0] * 26 n = len(s) a = ord("a") for i in range(n): d1[ord(s[i]) - a] += 1 i = 0 for j in range(26): while i < n and d2[j] < d1[j]: if s[i] == chr(j + a): u += s[i] d2[j] += 1 else: t.append(s[i]) d2[ord(s[i]) - a] += 1 i += 1 for p in range(26): if d2[p] < d1[p]: q = p break else: q = 26 while len(t) > 0 and t[-1] <= chr(q + a): k = t.pop(-1) u += k print(u)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR IF VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
from sys import stderr, stdin def readInts(): return map(int, stdin.readline().strip().split()) def print_err(*args, **kwargs): print(*args, file=stderr, **kwargs) def solve(s): s = list(s) sn = len(s) pq = sorted(zip(list(s), range(sn))) ix_left = 0 u, v = [], [] for c, ix in pq: if ix < ix_left: continue while u and c >= u[-1]: v.append(u.pop()) for cix in range(ix_left, ix + 1): u.append(s[cix]) ix_left = ix + 1 while u: v.append(u.pop()) return v def run(): s = input().strip() print("".join(solve(s))) run()
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR LIST LIST FOR VAR VAR VAR IF VAR VAR WHILE VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
RI = lambda: [int(x) for x in input().split()] rw = lambda: [input().strip()] s = list(input().strip()) stack = [] index_stack = -1 ans = "" d = {} ch = "abcdefghijklmnopqrstuvwxyz" for i in ch: d[i] = 0 for i in range(len(s)): d[s[i]] += 1 previous = -1 index = 0 for i in d: while index_stack >= 0 and stack[index_stack] <= i: ans += stack[index_stack] stack.pop() index_stack -= 1 while index != len(s) and d[i] != 0: if s[index] == i: ans += i d[i] -= 1 else: stack.append(s[index]) index_stack += 1 d[s[index]] -= 1 index += 1 while len(stack) != 0: ans += stack[index_stack] index_stack -= 1 stack.pop() print(ans)
ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR DICT ASSIGN VAR STRING FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
import sys s = input() n = len(s) smallest = [""] * (n - 1) + [s[-1]] for i in range(n - 2, -1, -1): smallest[i] = min(smallest[i + 1], s[i]) t, u = [], [] for i in range(n): while t and t[-1] <= smallest[i]: u.append(t.pop()) if s[i] == smallest[i]: u.append(s[i]) else: t.append(s[i]) u += t[::-1] print(*u, sep="")
IMPORT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST STRING BIN_OP VAR NUMBER LIST VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
def go(cnt, n): for i in range(0, n): if cnt[i] > 0: return 1 return 0 s = input() n = len(s) t = [] u = [] cnt = [0] * 26 for i in s: cnt[ord(i) - 97] += 1 for i in range(n): if len(t) == 0 or t[-1] >= s[i]: t.append(s[i]) else: for j in reversed(t): if go(cnt, ord(j) - 97) == 0: u.append(j) t.pop() else: break t.append(s[i]) cnt[ord(s[i]) - 97] -= 1 print("".join(u + t[::-1]))
FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR VAR NUMBER
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
s = input() n = len(s) freq = [0] * 26 if len(s) == 0: print("") for i in s: freq[ord(i) - ord("a")] += 1 stack = [] ans = "" for i in range(n): stack.append(s[i]) freq[ord(s[i]) - ord("a")] -= 1 flag = True while len(stack) and flag: for j in reversed(range(ord(stack[-1]) - ord("a"))): if freq[j] > 0: flag = False if flag: ans += stack[-1] stack.pop() print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR LIST ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
def fnz(l): for i in range(len(l)): if l[i] != 0: return i return i alphabets = [ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", ] count_alph = [0] * 26 s = input() t = [] u = [] for i in s: count_alph[alphabets.index(i)] += 1 for i in s: t.append(i) count_alph[alphabets.index(i)] -= 1 while t[-1] <= alphabets[fnz(count_alph)]: u.append(t.pop()) if len(t) == 0: break u = u + t[::-1] ans = "" for i in u: ans += i print(ans)
FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER RETURN VAR RETURN VAR ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR STRING FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
def get_min_char(freq): for c in "abcdefghijklmnopqrstuvwxyz": if freq[c] != 0: return c def solve(s): suffix_min = [] t = [] u = [] letters = "abcdefghijklmnopqrstuvwxyz" freq = {c: s.count(c) for c in letters} for c in s: m = get_min_char(freq) while len(t) > 0: top = t.pop() if top <= m: u.append(top) else: t.append(top) break if c > m: t.append(c) else: u.append(c) freq[c] -= 1 return "".join(u) + "".join(reversed(t)) def main(): print(solve(input())) main()
FUNC_DEF FOR VAR STRING IF VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN BIN_OP FUNC_CALL STRING VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
a = input() b = list(a) c = b[:] c.sort() t = [] answer = [] for i in range(len(c)): while len(t) != 0 and c[i] >= t[len(t) - 1]: answer.append(t[len(t) - 1]) t.pop(len(t) - 1) if c[i] in b: if b[0] == c[i]: answer.append(b[0]) b.pop(0) else: while b[0] != c[i]: t.append(b[0]) b.pop(0) answer.append(b[0]) b.pop(0) if len(t) != 0: for i in range(1, len(t) + 1): answer.append(t[len(t) - i]) print("".join(answer))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
s = input() m = ["z"] * len(s) m[-1] = s[-1] c = s[-1] for x in range(len(s) - 2, -1, -1): c = min(c, s[x]) m[x] = c i = m.index(min(m)) t = [] y = "" for x in range(len(s)): while t and t[-1] <= m[x]: y += t.pop() t.append(s[x]) print(y, end="") for x in t[::-1]: print(x, end="")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING FOR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
s = input() iterate = 0 e = [(True) for i in range(len(s))] ans = ["" for i in range(len(s))] idx = 0 lastOccur = [(-1) for i in range(26)] for i in range(len(s)): lastOccur[ord(s[i]) - ord("a")] = i i = 0 while i < 26 and iterate < len(s): j = iterate - 1 while j >= 0 and ord(s[j]) - ord("a") <= i: if e[j]: ans[idx] = s[j] e[j] = False idx += 1 j -= 1 j = iterate while j < lastOccur[i] + 1: if e[j] and ord(s[j]) - ord("a") == i: ans[idx] = s[j] e[j] = False idx += 1 j += 1 iterate = j i += 1 if iterate >= len(s): for j in range(len(s) - 1, -1, -1): if e[j]: ans[idx] = s[j] idx += 1 str1 = "".join(ans) print(str1)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR STRING VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR WHILE VAR BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
restantes = {} def possui_menor(l): n = ord(l) - ord("a") for k in sorted(restantes.keys()): if k >= l: break if restantes[k] > 0: return True return False s = list(input()) pilha = [] resposta = "" for l in s: if l not in restantes: restantes[l] = 0 restantes[l] += 1 for l in s: pilha.append(l) restantes[l] = restantes[l] - 1 prox = pilha[-1] while len(pilha) > 0 and not possui_menor(prox): resposta += pilha.pop(-1) if len(pilha) > 0: prox = pilha[-1] while len(pilha) > 0: resposta += pilha.pop(-1) print(resposta)
ASSIGN VAR DICT FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR IF VAR VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR STRING FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
q_string = str(input()) l = len(q_string) queue_list = [[0, 0] for i in range(l)] for i in range(l): index = l - i - 1 if index == l - 1: queue_list[index][0] = index queue_list[index][1] = q_string[index] continue elif queue_list[index + 1][1] < q_string[index]: queue_list[index][1] = queue_list[index + 1][1] queue_list[index][0] = queue_list[index + 1][0] else: queue_list[index][1] = q_string[index] queue_list[index][0] = index s_stack = [] curr_index = 0 output_string = [] while len(output_string) != l: min_q = queue_list[curr_index][1] min_index_q = queue_list[curr_index][0] len_stack = len(s_stack) min_s = "zz" if len_stack == 0 else s_stack[len_stack - 1] if min_q < min_s: for j in range(curr_index, min_index_q): s_stack.append(q_string[j]) output_string.append(q_string[min_index_q]) curr_index = min_index_q + 1 else: output_string.append(s_stack[len_stack - 1]) s_stack.pop() if curr_index >= l: while len(s_stack) != 0: output_string.append(s_stack.pop()) break print("".join(output_string))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER STRING VAR BIN_OP VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR WHILE FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
s = input() dic = {} t = [] u = "" for i in s: if i in dic: dic[i] = dic[i] + 1 else: dic[i] = 1 j = "a" for i in s: while j not in dic or dic[j] == 0: j = chr(ord(j) + 1) if len(t) > 0: last_t = t[-1] while ord(j) >= ord(last_t): u = u + last_t t.pop() if len(t) == 0: break last_t = t[-1] t.append(i) dic[i] = dic[i] - 1 len_t = len(t) for i in range(len_t): u = u + t.pop() print(u)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR STRING FOR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR STRING FOR VAR VAR WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
s = list(map(ord, list(input()))) n = len(s) t = [] u = "" arr = [0] * 26 for i in s: arr[i - 97] += 1 for i in s: arr[i - 97] -= 1 t.append(chr(i)) while t and sum(arr[: ord(t[-1]) - 97]) == 0: u += t.pop() while t: u += t.pop() print(u)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR FUNC_CALL VAR WHILE VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
s = input() al = [0] * 27 for e in s: al[ord(e) - 96] += 1 t = [] ans = "" for e in range(len(s)): t.append(s[e]) al[ord(s[e]) - 96] -= 1 while e < len(s) - 1 and t and sum(al[: ord(t[-1]) - 96]) == 0: ans += t[-1] t.pop() while t: ans += t[-1] t.pop() print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR WHILE VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
s = input() s += "{" ans = "" tmp = [] m = ["z" for i in range(len(s) + 1)] for i in range(len(s) - 1, -1, -1): m[i] = min(m[i + 1], s[i]) for i in range(len(s) - 1): tmp.append(s[i]) while len(tmp) and tmp[-1] <= m[i + 1]: ans += tmp.pop() print(ans)
ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR STRING VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
input_str = input() s = [] t = [] u = [] for x in input_str: s.append(x) s.reverse() var = 97 while len(s) != 0 or len(t) != 0: track = len(s) for z in range(len(s) - 1, -1, -1): if s[z] == chr(var): track = z for y in range(len(s) - 1, z, -1): t.append(s[y]) s.pop() u.append(s[z]) s.pop() var += 1 while True: if len(t) != 0 and ord(t[len(t) - 1]) <= var: u.append(t[len(t) - 1]) t.pop() else: break output_str = "" for z in u: output_str += z print(output_str)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER WHILE NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
base = ord("a") char_list = [(ord(char) - base) for char in list(input())] min_string = "" char_count = [0] * 26 stack = [] for x in char_list: char_count[x] += 1 cur_min_char = 0 for i in range(26): if char_count[i]: cur_min_char = i break for x in char_list: if x == cur_min_char: min_string += chr(x + base) char_count[x] -= 1 if char_count[x] == 0: cur_min_char = 0 for i in range(26): if char_count[i]: cur_min_char = i break while stack and stack[-1] <= cur_min_char: min_string += chr(stack.pop() + base) else: stack.append(x) char_count[x] -= 1 while stack: min_string += chr(stack.pop() + base) print(min_string)
ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR FOR VAR VAR IF VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR WHILE VAR VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
s = input() t = [] u = [] n = len(s) a = [0] * 26 for i in range(len(s)): z = ord(s[i]) - 97 a[z] += 1 FirstLetterInS = 0 LengthT = 0 while len(u) != n: if len(t) == 0: a[ord(s[FirstLetterInS]) - 97] -= 1 z = s[FirstLetterInS] t.append(z) FirstLetterInS += 1 else: flag = False x = -1 for i in range(ord(t[-1]) - 97): if a[i] > 0: flag = True break if flag: z = s[FirstLetterInS] t.append(z) FirstLetterInS += 1 a[ord(z) - 97] -= 1 else: u += t[-1] t.pop() print("".join(u))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
s = list(input()) t = [] u = [] a = [[(0) for i in range(26)]] for i in range(len(s) - 1, -1, -1): b = a[-1][:] a.append(b) a[-1][ord(s[i]) - ord("a")] = a[-2][ord(s[i]) - ord("a")] + 1 b = a[::-1] j = 0 i = 0 while i < len(s): if b[i][j] == 0: j += 1 continue while len(t) > 0 and ord(t[-1]) <= j + ord("a"): u.append(t.pop()) if b[i][j] != 0: if s[i] == chr(j + ord("a")): u.append(s[i]) else: t.append(s[i]) else: j += 1 i += 1 while len(t) > 0: u.append(t.pop()) print("".join(u))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR VAR NUMBER IF VAR VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
s = input() def f(l=0): c = min(s[l:]) cnt = s[l:].count(c) return c, cnt c, cnt = f() res, t = [], [] for i, e in enumerate(s): if e == c: res.append(e) if cnt > 1: cnt -= 1 else: if i == len(s) - 1: break c, cnt = f(i + 1) while len(t) and t[-1] <= c: res.append(t[-1]) t.pop() else: t.append(e) print("".join(res + t[::-1]))
ASSIGN VAR FUNC_CALL VAR FUNC_DEF NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE FUNC_CALL VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR VAR NUMBER
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
al = [0] * 26 bs = ord("a") s = [(ord(x) - bs) for x in list(input())] st = [] for x in s: al[x] += 1 cm = -1 for i in range(26): if al[i]: cm = i break anso = [] for x in s: if x == cm: anso.append(x) al[cm] -= 1 if not al[cm]: for i in range(cm, 26): if al[i]: cm = i break while st and st[-1] <= cm: anso.append(st.pop()) else: al[x] -= 1 st.append(x) ans = "" for x in anso: ans += chr(bs + x) for x in st[::-1]: ans += chr(bs + x) print(ans)
ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR WHILE VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
def compare(a, vis): for i in range(0, ord(a) - ord("a")): if vis[i] > 0: return False return True n = input() vis = [0] * 26 for letter in n: vis[ord(letter) - ord("a")] = vis[ord(letter) - ord("a")] + 1 st = [] s = [] for i in range(0, len(n)): st.append(n[i]) vis[ord(n[i]) - ord("a")] = vis[ord(n[i]) - ord("a")] - 1 while len(st) > 0 and compare(st[len(st) - 1], vis): s.append(st[len(st) - 1]) st.pop() while len(st) != 0: s.append(st[len(st) - 1]) st.pop() for letter in s: print(letter, end="")
FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING IF VAR VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING BIN_OP VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
from sys import stdin lines, line_index = stdin.readlines(), -1 def get_line(): global lines, line_index line_index += 1 return lines[line_index] def next_min_char(info): for k, v in enumerate(info): if v != 0: return k return -1 def main(): data = get_line().strip() stack = [] output = [] a = ord("a") z = a + 26 n = len(data) best = [-1] * (n + 1) best[n] = z for i in range(n - 1, -1, -1): best[i] = min(best[i + 1], ord(data[i])) cur = 0 while stack or cur < n: if stack and ord(stack[-1]) <= best[cur]: output.append(stack.pop()) else: stack.append(data[cur]) cur += 1 print("".join(output)) main()
ASSIGN VAR VAR FUNC_CALL VAR NUMBER FUNC_DEF VAR NUMBER RETURN VAR VAR FUNC_DEF FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR IF VAR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
def is_smallest(lst, pr, s): j = ord(s) - ord("a") for i in range(0, j): if lst[i] > 0: return False return True inp_str = list(input()) res = "" alpha = [0] * 26 qu = [] for i in range(0, len(inp_str)): j = ord(inp_str[i]) - ord("a") alpha[j] += 1 for x in inp_str: qu.append(x) k = ord(x) - ord("a") alpha[k] -= 1 while is_smallest(alpha, qu, x): res += qu.pop() if not qu: break x = qu[-1] while qu: res += qu.pop() print(res)
FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR IF VAR ASSIGN VAR VAR NUMBER WHILE VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
ct = [0] * 26 string = input() for s in string: ct[ord(s) - ord("a")] += 1 t = [] u = [] for s in string: t.append(s) ct[ord(s) - ord("a")] -= 1 while t and sum(ct[: ord(t[-1]) - ord("a")]) == 0: u.append(t.pop()) print(*u, sep="")
ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER WHILE VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR STRING
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
s = input() prefmin = ["{"] * (len(s) + 1) st = [] for i in range(len(s) - 1, -1, -1): prefmin[i] = min(s[i], prefmin[i + 1]) for i in range(len(s)): while len(st) and st[-1] <= prefmin[i]: print(st.pop(), end="") if prefmin[i] == s[i]: print(s[i], end="") else: st.append(s[i]) for i in range(len(st) - 1, -1, -1): print(st[i], end="")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR STRING IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR STRING
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
sequence, stack, letter_count, ans = input(), [], [0] * 26, "" for x in sequence: letter_count[ord(x) - 97] += 1 for x in sequence: stack.append(x) letter_count[ord(x) - 97] -= 1 while stack and sum(letter_count[: ord(stack[-1]) - 97]) == 0: ans += stack.pop() print(ans)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR LIST BIN_OP LIST NUMBER NUMBER STRING FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
s = input() ls = len(s) s1 = s[::-1] d = dict() t = list() u = list() sets = sorted(set(s)) setsl = len(sets) for i in range(setsl): try: idx = s1.index(sets[i]) if i == 0: d[sets[i]] = 0, ls - idx - 1 prev = sets[i] elif ls - idx - 1 > d[prev][1]: d[sets[i]] = d[prev][1] + 1, ls - idx - 1 prev = sets[i] except: pass for i in d: while t and t[-1] <= i: u.append(t.pop()) for j in range(d[i][0], d[i][1] + 1): if s[j] == i: u.append(s[j]) else: t.append(s[j]) u = "".join(u + t[::-1]) print(u)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR VAR WHILE VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL STRING BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
s = input().strip() t = [] u = [] a = "abcdefghijklmnopqrstuvwxyz" z = dict() for i in a: z[i] = 0 for i in s: z[i] += 1 for i in s: t.append(i) z[i] -= 1 while t: f = 0 top = t[-1] for j in z: if j < top and z[j] > 0: f = 1 break if f == 1: break else: u.append(t.pop()) while t: u.append(t.pop()) print("".join(u))
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
s = list(input()) t = [] u = "" alphabets = list("abcdefghijklmnopqrstuvwxyz") idx = 0 for c in alphabets: while t and t[-1] <= c: u = u + t.pop() try: nxt = s.index(c, idx) except ValueError: nxt = -1 while nxt >= 0: while idx < nxt: t.append(s[idx]) idx += 1 u = u + c idx += 1 try: nxt = s.index(c, idx) except ValueError: nxt = -1 while t: u = u + t.pop() print(u)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
s = list(input()) t = list() u = list() d = dict() var = 97 for i in range(26): d[chr(var + i)] = 0 for i in s: d[i] += 1 i = 0 while i < len(s): if d[chr(var)] > 0: while i < len(s): d[s[i]] -= 1 t.append(s[i]) i += 1 if t[-1] <= chr(var): u.append(t.pop()) break else: var += 1 while t and t[-1] <= chr(var): u.append(t.pop()) while t != []: u.append(t.pop()) print("".join(u))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER WHILE VAR VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR LIST EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
def have_less(x, ch): for k in range(ord(ch) - ord("a") - 1, -1, -1): if x[k] > 0: return True return False s = input() t, u = [], [] stat = [0] * 26 for ch in s: stat[ord(ch) - ord("a")] += 1 pos = 0 while pos < len(s): if len(t) == 0: stat[ord(s[pos]) - ord("a")] -= 1 t.append(s[pos]) pos += 1 while pos < len(s) and have_less(stat, t[-1]): t.append(s[pos]) stat[ord(s[pos]) - ord("a")] -= 1 pos += 1 u.append(t.pop()) print("".join(u) + "".join(t[::-1]))
FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER NUMBER NUMBER IF VAR VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL STRING VAR FUNC_CALL STRING VAR NUMBER
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
f = lambda q: ord(q) - 97 s, t, u = input(), [], "" k = [0] * 26 for i in s: k[f(i)] += 1 for i in s: t.append(i) k[f(i)] -= 1 while t and sum(k[: f(t[-1])]) == 0: u += t.pop() print(u)
ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR LIST STRING ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
s = input() s = s[-1::-1] l = [s[0]] for i in range(1, len(s)): l.append(min(s[i], l[-1])) s = s[-1::-1] l = l[-1::-1] st = [] p1 = 0 ans = [] while len(ans) != len(s): if p1 + 1 < len(l) and l[p1 + 1] < s[p1]: if len(st) != 0 and l[p1 + 1] >= st[-1]: ans.append(st[-1]) st.__delitem__(len(st) - 1) else: st.append(s[p1]) p1 += 1 elif len(st) == 0: ans.append(s[p1]) p1 += 1 elif p1 == len(l): ans.append(st[-1]) st.__delitem__(len(st) - 1) elif s[p1] < st[-1]: ans.append(s[p1]) p1 += 1 else: ans.append(st[-1]) st.__delitem__(len(st) - 1) print(*ans, sep="")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
def letters(): return (chr(i) for i in range(ord("a"), ord("z") + 1)) s = input() ls = {lt: (0) for lt in letters()} for lt in s: ls[lt] += 1 s = [ch for ch in reversed(s)] stack = [] res = [] for curr in letters(): while stack and stack[-1] <= curr: res.append(stack.pop(-1)) while ls[curr] > 0: if s[-1] != curr: c = s.pop(-1) ls[c] -= 1 stack.append(c) else: ls[curr] -= 1 res.append(s.pop(-1)) res += reversed(stack) print("".join(res))
FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR WHILE VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER WHILE VAR VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
ptr = ord("a") def s_has_smaller(s_cnt_local, c): global ptr for i in range(ptr, ord(c)): ptr = i if s_cnt_local[i] > 0: return True return False s = list(input()) s.reverse() t = [] u = [] s_cnt = [0] * (ord("z") + 1) for x in s: s_cnt[ord(x)] += 1 while s or t: if not s: while t: u.append(t.pop()) elif not t: x = s.pop() s_cnt[ord(x)] -= 1 t.append(x) elif s_has_smaller(s_cnt, t[-1]): x = s.pop() s_cnt[ord(x)] -= 1 t.append(x) else: x = t.pop() u.append(x) print("".join(u))
ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR STRING NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF VAR WHILE VAR EXPR FUNC_CALL VAR FUNC_CALL VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
str = list(input().strip()) alphabet = [0] * 26 t = [] u = [] def findFirst(): for index, val in enumerate(alphabet): if val > 0: return index else: return False i = 0 size = len(str) while i < size: alphabet[ord(str[i]) - 97] += 1 i += 1 i = 0 while i < size: t.append(str[i]) count = 0 while len(t): index = findFirst() alphabet[ord(t[-1]) - 97] -= 1 val = chr(index + 97) if t[-1] == val: u.append(t.pop()) if len(t): count += 1 alphabet[ord(t[-1]) - 97] += 1 else: break i += 1 while len(t): u.append(t.pop()) for item in u: print(item, end="") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR NUMBER WHILE FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
s = input() n = len(s) mins = [ord("z") + 1] * n mins[-1] = s[-1] for i in range(1, n): mins[-i - 1] = min(mins[-i], s[-i - 1]) a = [] r = "" i = 0 j = 0 while i < n: while len(a) > 0 and a[-1] <= mins[i]: r += a.pop() mn = mins[i] while s[i] > mn: a.append(s[i]) i += 1 a.append(s[i]) i += 1 while len(a) > 0: r += a.pop() print(r)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST BIN_OP FUNC_CALL VAR STRING NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR WHILE VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. -----Input----- First line contains non-empty string s (1 ≀ |s| ≀ 10^5), consisting of lowercase English letters. -----Output----- Print resulting string u. -----Examples----- Input cab Output abc Input acdb Output abdc
def getindex(a): return ord(a) - 97 s = input() cs = [(0) for i in range(26)] t = [] u = [] for i in s: cs[getindex(i)] += 1 ls = 0 for i in range(len(s)): si = getindex(s[i]) while cs[ls] == 0: ls += 1 while t and getindex(t[-1]) <= ls: u.append(t.pop()) if ls != si: t.append(s[i]) else: u.append(s[i]) cs[si] -= 1 while t: u.append(t.pop()) print("".join(u))
FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER WHILE VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Read problem statements in [Russian] and [Mandarin Chinese]. Chef goes to a candy store that sells N different candies. For each candy, we know its price and sweetness. Chef has D dollars and can take a maximum of 2 candies, one in each hand. Find the maximum total sweetness he can buy under the given constraints. NOTE: Since the input-output is large, prefer using fast input-output methods. ------ Input Format ------ - The first line contains an integer T, the number of test cases. Then the test cases follow. - Each test case contains three lines of input. - First line will contain 2 space separated integers N and D, the number of different candies and the amount of money Chef has. - Second line contains N space separated integers P_{1}, P_{2}, \ldots, P_{N}, where P_{i} is the price of the i^{th} candy. - Third line contains N space separated integers S_{1}, S_{2}, \ldots, S_{N}, where S_{i} is the sweetness of the i^{th} candy. ------ Output Format ------ For each testcase, output in a single line, the maximum total sweetness Chef can buy with the money he has, and with at most two candies. ------ Constraints ------ $1 ≀ T ≀ 2.5*10^{5}$ $1 ≀ N ≀ 10^{5}$ $1 ≀ D ≀ 10^{9}$ $1 ≀ P_{i}, S_{i} ≀ 10^{9}$ - Sum $N$ over all testcases is atmost $10^{6}$. ----- Sample Input 1 ------ 3 2 10 1 2 2 1 5 7 1 2 3 4 5 1 2 3 4 5 5 7 6 6 6 6 6 5 4 3 2 1 ----- Sample Output 1 ------ 3 7 5 ----- explanation 1 ------ TestCase $1$: Chef can collect both the candies available with the money he has. TestCase $2$: Chef can collect candies at index $[2, 5]$ or $[3, 4]$. In both cases, he will get the total sweetness of $7$. TestCase $3$: Since the price of all candies is the same, it's always optimal to choose the candies with maximum sweetness. Also, in this case, no more than one candy can be chosen.
import sys input = sys.stdin.readline t = int(input().strip()) for __ in range(t): n, d = map(int, input().split()) p = list(map(int, input().split())) s = list(map(int, input().split())) arr = [(0, 0)] + [ (candy_price, candy_sweetness) for candy_price, candy_sweetness in zip(p, s) ] arr.sort() smax = [0] for i in range(1, len(arr)): smax.append(max(smax[-1], arr[i][1])) left = 0 right = len(arr) - 1 final_result = 0 while right >= 0 and arr[right][0] > d: right -= 1 while right >= 0 and left < right: while left + 1 < right and arr[left + 1][0] + arr[right][0] <= d: left += 1 final_result = max(final_result, smax[left] + arr[right][1]) while right > 0 and arr[right - 1][0] == arr[right][0]: right -= 1 right -= 1 for i in range(right, 0, -1): final_result = max(final_result, smax[i - 1] + arr[i][1]) print(final_result)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER WHILE VAR NUMBER VAR VAR WHILE BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Russian] and [Mandarin Chinese]. Chef goes to a candy store that sells N different candies. For each candy, we know its price and sweetness. Chef has D dollars and can take a maximum of 2 candies, one in each hand. Find the maximum total sweetness he can buy under the given constraints. NOTE: Since the input-output is large, prefer using fast input-output methods. ------ Input Format ------ - The first line contains an integer T, the number of test cases. Then the test cases follow. - Each test case contains three lines of input. - First line will contain 2 space separated integers N and D, the number of different candies and the amount of money Chef has. - Second line contains N space separated integers P_{1}, P_{2}, \ldots, P_{N}, where P_{i} is the price of the i^{th} candy. - Third line contains N space separated integers S_{1}, S_{2}, \ldots, S_{N}, where S_{i} is the sweetness of the i^{th} candy. ------ Output Format ------ For each testcase, output in a single line, the maximum total sweetness Chef can buy with the money he has, and with at most two candies. ------ Constraints ------ $1 ≀ T ≀ 2.5*10^{5}$ $1 ≀ N ≀ 10^{5}$ $1 ≀ D ≀ 10^{9}$ $1 ≀ P_{i}, S_{i} ≀ 10^{9}$ - Sum $N$ over all testcases is atmost $10^{6}$. ----- Sample Input 1 ------ 3 2 10 1 2 2 1 5 7 1 2 3 4 5 1 2 3 4 5 5 7 6 6 6 6 6 5 4 3 2 1 ----- Sample Output 1 ------ 3 7 5 ----- explanation 1 ------ TestCase $1$: Chef can collect both the candies available with the money he has. TestCase $2$: Chef can collect candies at index $[2, 5]$ or $[3, 4]$. In both cases, he will get the total sweetness of $7$. TestCase $3$: Since the price of all candies is the same, it's always optimal to choose the candies with maximum sweetness. Also, in this case, no more than one candy can be chosen.
import sys input = sys.stdin.readline for _ in range(int(input())): n, d = map(int, input().split()) p = list(map(int, input().split())) s = list(map(int, input().split())) s_p = [[p[i], i] for i in range(n)] s_s = [[s[i], i] for i in range(n)] s_p = sorted(s_p) s_s = sorted(s_s) j = n - 1 ans = 0 for i in range(n): if p[i] <= d: ans = max(ans, s[i]) while j >= 0 and (s_p[i][0] + p[s_s[j][1]] > d or s_p[i][1] == s_s[j][1]): j -= 1 if j != -1: ans = max(ans, s[s_p[i][1]] + s_s[j][0]) print(ans)
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR WHILE VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR