description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a_1... a_{|}t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. -----Input----- The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≀ |p| < |t| ≀ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a_1, a_2, ..., a_{|}t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≀ a_{i} ≀ |t|, all a_{i} are distinct). -----Output----- Print a single integer number, the maximum number of letters that Nastya can remove. -----Examples----- Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 -----Note----- In the first sample test sequence of removing made by Nastya looks like this: "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters.
t = list(input()) p = list(input()) s = input().split() v = [0] * len(s) for i in range(len(s)): v[i] = int(s[i]) l, r = 0, len(t) while r - l > 1: m = (l + r) // 2 t1 = t[:] for i in range(m): t1[v[i] - 1] = "*" i, j = 0, 0 while j < len(t1) and i < len(p): if p[i] == t1[j]: i += 1 j += 1 if i == len(p): l = m else: r = m print(l)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER STRING ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a_1... a_{|}t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. -----Input----- The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≀ |p| < |t| ≀ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a_1, a_2, ..., a_{|}t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≀ a_{i} ≀ |t|, all a_{i} are distinct). -----Output----- Print a single integer number, the maximum number of letters that Nastya can remove. -----Examples----- Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 -----Note----- In the first sample test sequence of removing made by Nastya looks like this: "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters.
def ok(n): mark = set() global a for i in range(n): mark.add(a[i] - 1) one = 0 two = 0 while one < len(p) and two < len(t): if two in mark: two += 1 elif p[one] == t[two]: one += 1 two += 1 else: two += 1 return one == len(p) t = input() p = input() a = list(map(int, input().split())) l = 0 r = len(t) ans = -1 while l <= r: mid = (l + r) // 2 if ok(mid): ans = mid l = mid + 1 else: r = mid - 1 print(ans)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a_1... a_{|}t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. -----Input----- The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≀ |p| < |t| ≀ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a_1, a_2, ..., a_{|}t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≀ a_{i} ≀ |t|, all a_{i} are distinct). -----Output----- Print a single integer number, the maximum number of letters that Nastya can remove. -----Examples----- Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 -----Note----- In the first sample test sequence of removing made by Nastya looks like this: "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters.
t = input() p = input() a = list(map(int, input().split())) def good(b): i = 0 l = len(b) for c in p: while i < l and b[i] != c: i += 1 if i == l: return False i += 1 return True def check(x): tt = list(t) for p in a[:x]: tt[p - 1] = "" return good(tt) le, ri = 0, len(t) - 1 while ri > le: me = (le + ri + 1) // 2 if check(me): le = me else: ri = me - 1 print(le)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER STRING RETURN FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a_1... a_{|}t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. -----Input----- The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≀ |p| < |t| ≀ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a_1, a_2, ..., a_{|}t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≀ a_{i} ≀ |t|, all a_{i} are distinct). -----Output----- Print a single integer number, the maximum number of letters that Nastya can remove. -----Examples----- Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 -----Note----- In the first sample test sequence of removing made by Nastya looks like this: "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters.
def subseq(s, b): h = iter(b) return all(any(l == ch for l in h) for ch in s) t = input() p = input() num = [int(x) for x in input().split()] st = [] l = 0 r = len(num) ans = r + 1 while l <= r: mid = (l + r) // 2 st = list(t) c = num[:mid] for i in c: st[i - 1] = "" if subseq(p, "".join(st)): ans = mid l = mid + 1 else: r = mid - 1 print(ans)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER STRING IF FUNC_CALL VAR VAR FUNC_CALL STRING VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a_1... a_{|}t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. -----Input----- The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≀ |p| < |t| ≀ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a_1, a_2, ..., a_{|}t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≀ a_{i} ≀ |t|, all a_{i} are distinct). -----Output----- Print a single integer number, the maximum number of letters that Nastya can remove. -----Examples----- Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 -----Note----- In the first sample test sequence of removing made by Nastya looks like this: "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters.
import sys inp = sys.stdin.readline def input(): return inp().strip() def iin(): return int(input()) def lin(): return list(map(int, input().split())) def main(): p, t = input(), input() a = lin() n = len(p) def check(x): rm = set(a[:x]) p1 = [p[i] for i in range(n) if i + 1 not in rm] dc = {} for i, j in enumerate(p1): try: dc[j].append(i) except: dc[j] = [i] for i in dc: dc[i] = dc[i][::-1] pv = -1 for i in t: if i in dc: while dc[i]: ch = dc[i].pop() if ch > pv: pv = ch break else: return False else: return False return True ans = 0 left, right = 0, n - 1 while left < right: md = left + right + 1 >> 1 if check(md): ans = max(md, ans) left = md else: right = md - 1 print(ans) main()
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a_1... a_{|}t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. -----Input----- The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≀ |p| < |t| ≀ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a_1, a_2, ..., a_{|}t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≀ a_{i} ≀ |t|, all a_{i} are distinct). -----Output----- Print a single integer number, the maximum number of letters that Nastya can remove. -----Examples----- Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 -----Note----- In the first sample test sequence of removing made by Nastya looks like this: "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters.
def check(x): temp = [] for i in t: temp.append(i) for i in range(x): temp[a[i] - 1] = "" l = 0 r = 0 c = 0 while r < len(s) and l < len(t): if s[r] == temp[l]: r += 1 c += 1 l += 1 return c == len(s) t = input() s = input() a = list(map(int, input().split())) lo = 0 hi = len(a) while lo < hi - 1: mid = lo + (hi - lo) // 2 if check(mid): lo = mid else: hi = mid print(lo)
FUNC_DEF ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER STRING ASSIGN VAR NUMBER 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 VAR NUMBER RETURN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a_1... a_{|}t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. -----Input----- The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≀ |p| < |t| ≀ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a_1, a_2, ..., a_{|}t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≀ a_{i} ≀ |t|, all a_{i} are distinct). -----Output----- Print a single integer number, the maximum number of letters that Nastya can remove. -----Examples----- Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 -----Note----- In the first sample test sequence of removing made by Nastya looks like this: "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters.
s = input() t = input() arr = [(x - 1) for x in map(int, input().split())] l = 0 r = len(s) - 1 def check(sa, sb): j = 0 for i in range(len(sb)): while j < len(sa) and sa[j] != sb[i]: j += 1 if j == len(sa): return False j += 1 return True while l < r: mid = l + r + 1 >> 1 d = list(s) for i in range(mid): d[arr[i]] = "" if check("".join(d), t): l = mid else: r = mid - 1 print(l)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER VAR NUMBER RETURN NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR STRING IF FUNC_CALL VAR FUNC_CALL STRING VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a_1... a_{|}t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. -----Input----- The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≀ |p| < |t| ≀ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a_1, a_2, ..., a_{|}t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≀ a_{i} ≀ |t|, all a_{i} are distinct). -----Output----- Print a single integer number, the maximum number of letters that Nastya can remove. -----Examples----- Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 -----Note----- In the first sample test sequence of removing made by Nastya looks like this: "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters.
def isSubStr(word, liveTimes, str, time): p = 0 for i in range(len(word)): if time < liveTimes[i] and word[i] == str[p]: p += 1 if p == len(str): break return p == len(str) p, t, perms = input(), input(), input().split() liveTimes = [(0) for _ in range(len(p))] for i in range(len(perms)): liveTimes[int(perms[i]) - 1] = i + 1 L, R, maxT = 0, len(p), 0 while L <= R: mid = L + (R - L) // 2 if isSubStr(p, liveTimes, t, mid): maxT = max(mid, maxT) L = mid + 1 else: R = mid - 1 print(maxT)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a_1... a_{|}t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. -----Input----- The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≀ |p| < |t| ≀ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a_1, a_2, ..., a_{|}t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≀ a_{i} ≀ |t|, all a_{i} are distinct). -----Output----- Print a single integer number, the maximum number of letters that Nastya can remove. -----Examples----- Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 -----Note----- In the first sample test sequence of removing made by Nastya looks like this: "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters.
t = list(input()) p = list(input()) lis = list(map(int, input().split())) pp = len(p) tt = len(t) l = 0 r = tt while l <= r: mid = l + (r - l) // 2 tep = t[:] for i in range(mid): tep[lis[i] - 1] = "#" j = i = 0 while i < tt and j < pp: if tep[i] == p[j]: j += 1 i += 1 if j < pp: r = mid - 1 else: l = mid + 1 print(r)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER STRING ASSIGN VAR VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a_1... a_{|}t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. -----Input----- The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≀ |p| < |t| ≀ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a_1, a_2, ..., a_{|}t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≀ a_{i} ≀ |t|, all a_{i} are distinct). -----Output----- Print a single integer number, the maximum number of letters that Nastya can remove. -----Examples----- Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 -----Note----- In the first sample test sequence of removing made by Nastya looks like this: "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters.
P = list(input()) T = list(input()) a = list(map(int, input().split())) def check(k, p, t, a): p1 = p.copy() for i in range(k): p1[a[i] - 1] = "#" n, m = len(p1), len(t) i, j = 0, 0 while i < n and j < m: if p1[i] == t[j]: i += 1 j += 1 else: i += 1 return j == m l, r = 0, len(a) while r - l > 1: mid = (l + r) // 2 if check(mid, P, T, a): l = mid else: r = mid print(l)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a_1... a_{|}t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. -----Input----- The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≀ |p| < |t| ≀ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a_1, a_2, ..., a_{|}t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≀ a_{i} ≀ |t|, all a_{i} are distinct). -----Output----- Print a single integer number, the maximum number of letters that Nastya can remove. -----Examples----- Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 -----Note----- In the first sample test sequence of removing made by Nastya looks like this: "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters.
from sys import stdin t = input().strip() p = input().strip() def possible(pos): it = 0 ip = 0 while it < len(t) and ip < len(p): if it in d: it += 1 continue if t[it] == p[ip]: it += 1 ip += 1 else: it += 1 if ip == len(p): return True else: return False a = [(int(x) - 1) for x in stdin.readline().split()] l = 0 r = len(t) - len(p) d = set() while l < r: mid = (l + r) // 2 if len(d) < mid + 1: for i in range(len(d), mid + 1): d.add(a[i]) else: for i in range(mid + 1, len(d), 1): d.discard(a[i]) if possible(mid): l = mid + 1 else: r = mid print(l)
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a_1... a_{|}t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. -----Input----- The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≀ |p| < |t| ≀ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a_1, a_2, ..., a_{|}t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≀ a_{i} ≀ |t|, all a_{i} are distinct). -----Output----- Print a single integer number, the maximum number of letters that Nastya can remove. -----Examples----- Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 -----Note----- In the first sample test sequence of removing made by Nastya looks like this: "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters.
def main(): s, w = ["", *input()], input() l = list(map(int, input().split())) lo, hi = 0, len(s) - len(w) while lo < hi: mid, t = (lo + hi) // 2, s[:] for i in l[:mid]: t[i] = "" try: i = 1 for c in w: while c != t[i]: i += 1 i += 1 except IndexError: hi = mid else: lo = mid + 1 print(lo - 1) main()
FUNC_DEF ASSIGN VAR VAR LIST STRING FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FOR VAR VAR VAR ASSIGN VAR VAR STRING ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a_1... a_{|}t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. -----Input----- The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≀ |p| < |t| ≀ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a_1, a_2, ..., a_{|}t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≀ a_{i} ≀ |t|, all a_{i} are distinct). -----Output----- Print a single integer number, the maximum number of letters that Nastya can remove. -----Examples----- Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 -----Note----- In the first sample test sequence of removing made by Nastya looks like this: "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters.
t, p = input(), input() indices = tuple(map(int, input().split())) n = len(t) np = len(p) l = 0 right = n ans = 0 while n - l > 1: m = (l + right) // 2 if l == m: break tu = list(t) for i in range(l, m): tu[indices[i] - 1] = "0" pos = 0 yes = 0 for i in tu: if i == p[pos]: pos += 1 if pos == np: yes = 1 break if yes: ans += m - l t = "".join(tu) l = m right = n else: right = m print(ans)
ASSIGN VAR 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 VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a_1... a_{|}t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. -----Input----- The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≀ |p| < |t| ≀ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a_1, a_2, ..., a_{|}t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≀ a_{i} ≀ |t|, all a_{i} are distinct). -----Output----- Print a single integer number, the maximum number of letters that Nastya can remove. -----Examples----- Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 -----Note----- In the first sample test sequence of removing made by Nastya looks like this: "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters.
s = input() p = input() a = list(map(int, input().split())) a_inds = [0] * (len(s) + 1) for i in range(len(a)): a_inds[a[i]] = i l = -1 r = len(s) def check_answer(ans): i = 0 j = 0 while i < len(s) and j < len(p): if a_inds[i + 1] >= ans and s[i] == p[j]: j += 1 i += 1 return j == len(p) while l < r - 1: m = (l + r) // 2 if check_answer(m): l = m else: r = m print(r - 1)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_CALL VAR VAR WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a_1... a_{|}t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. -----Input----- The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≀ |p| < |t| ≀ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a_1, a_2, ..., a_{|}t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≀ a_{i} ≀ |t|, all a_{i} are distinct). -----Output----- Print a single integer number, the maximum number of letters that Nastya can remove. -----Examples----- Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 -----Note----- In the first sample test sequence of removing made by Nastya looks like this: "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters.
import sys sys.setrecursionlimit(2**20) DEBUG = 0 def testRemove(string, target, remove, until): copy = string.copy() for i in remove[:until]: copy[i - 1] = 0 if DEBUG: print(copy, string) j = 0 for i in copy: if i == target[j]: j += 1 if j == len(target): return 1 return 0 def binSearch(string, target, remove, lo, hi): mid = lo + (hi - lo + 1) // 2 if lo >= hi: if testRemove(string, target, remove, mid): return mid return mid + 1 if testRemove(string, target, remove, mid): return binSearch(string, target, remove, mid, hi) return binSearch(string, target, remove, lo, mid - 1) string = list(input()) + ["\x00"] target = list(input()) + ["\x00"] remove = list(map(int, input().split())) answer = binSearch(string, target, remove, 0, len(remove) - 1) print(answer)
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR RETURN BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR LIST STRING ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR LIST STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a_1... a_{|}t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. -----Input----- The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≀ |p| < |t| ≀ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a_1, a_2, ..., a_{|}t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≀ a_{i} ≀ |t|, all a_{i} are distinct). -----Output----- Print a single integer number, the maximum number of letters that Nastya can remove. -----Examples----- Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 -----Note----- In the first sample test sequence of removing made by Nastya looks like this: "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters.
a = input() b = input() p = list(map(int, input().split())) n = len(p) h = [0] * n for i in range(n): p[i] -= 1 h[p[i]] = i def check(s1, s2, g): m1 = len(s1) m2 = len(s2) j = 0 i = 0 while j < m1 and i < m2: if s1[j] == s2[i] and h[i] > g: j = j + 1 i = i + 1 return j == m1 l = -1 r = n - 1 while l < r: md = (l + r + 1) // 2 if check(b, a, h[p[md]]): l = md else: r = md - 1 print(l + 1)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a_1... a_{|}t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. -----Input----- The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≀ |p| < |t| ≀ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a_1, a_2, ..., a_{|}t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≀ a_{i} ≀ |t|, all a_{i} are distinct). -----Output----- Print a single integer number, the maximum number of letters that Nastya can remove. -----Examples----- Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 -----Note----- In the first sample test sequence of removing made by Nastya looks like this: "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters.
s = list(input()) p = list(input()) a = list(map(int, input().strip().split(" "))) n = len(s) t = len(p) l = 0 u = n def possible(x): global n, t memeory = [True] * n for i in range(0, x): memeory[a[i] - 1] = False j = 0 for i in range(n): if memeory[i] and j < t and s[i] == p[j]: j += 1 if j == t: return True else: return False ans = u + 1 while l <= u: mid = (u + l) // 2 if possible(mid): l = mid + 1 ans = mid else: u = mid - 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a_1... a_{|}t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. -----Input----- The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≀ |p| < |t| ≀ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a_1, a_2, ..., a_{|}t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≀ a_{i} ≀ |t|, all a_{i} are distinct). -----Output----- Print a single integer number, the maximum number of letters that Nastya can remove. -----Examples----- Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 -----Note----- In the first sample test sequence of removing made by Nastya looks like this: "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters.
def check(index, string, string2, arr): string1 = list(string) for i in range(index + 1): string1[arr[i] - 1] = "-1" j = 0 for i in range(len(string1)): if string1[i] == string2[j]: j += 1 if j == len(string2): return True return False def main(): string1 = str(input()) string2 = str(input()) a = list(map(int, input().split())) start = 0 ans = -1 end = len(a) - 1 while start <= end: mid = start + (end - start) // 2 if check(mid, string1, string2, a): ans = mid start = mid + 1 else: end = mid - 1 print(ans + 1) return main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN EXPR FUNC_CALL VAR
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a_1... a_{|}t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. -----Input----- The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≀ |p| < |t| ≀ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a_1, a_2, ..., a_{|}t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≀ a_{i} ≀ |t|, all a_{i} are distinct). -----Output----- Print a single integer number, the maximum number of letters that Nastya can remove. -----Examples----- Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 -----Note----- In the first sample test sequence of removing made by Nastya looks like this: "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters.
s = list(input()) t = list(input()) arr = list(x - 1 for x in map(int, input().split())) n, m = len(arr), len(t) def binary_search(): f, e = 0, n while f <= e: mid = f + e >> 1 vis = [0] * n for i in range(mid): vis[arr[i]] = 1 idx, found = 0, 0 for i in range(n): if not vis[i] and s[i] == t[idx]: idx += 1 if idx == m: found = 1 break if found: f = mid + 1 else: e = mid - 1 return f - 1 print(binary_search())
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a_1... a_{|}t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. -----Input----- The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≀ |p| < |t| ≀ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a_1, a_2, ..., a_{|}t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≀ a_{i} ≀ |t|, all a_{i} are distinct). -----Output----- Print a single integer number, the maximum number of letters that Nastya can remove. -----Examples----- Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 -----Note----- In the first sample test sequence of removing made by Nastya looks like this: "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters.
def f(ind): global t, p, a g = t.copy() c = p.copy() b = [] for i in range(ind + 1): g[a[i] - 1] = "0" for i in range(len(g)): if g[i] != "0": b.append(g[i]) symbol = 0 for i in range(len(b)): if b[i] == c[symbol]: symbol += 1 if symbol == len(c): return True return False t = list(input()) p = list(input()) a = list(map(int, input().split())) if len(t) == len(p) or f(0) == False: print(0) else: L = 0 R = len(a) while R - L > 1: d = (L + R) // 2 if f(d): L = d else: R = d print(L + 1)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a_1... a_{|}t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. -----Input----- The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≀ |p| < |t| ≀ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a_1, a_2, ..., a_{|}t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≀ a_{i} ≀ |t|, all a_{i} are distinct). -----Output----- Print a single integer number, the maximum number of letters that Nastya can remove. -----Examples----- Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 -----Note----- In the first sample test sequence of removing made by Nastya looks like this: "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters.
s = input() t = input() a = list(map(int, input().split())) def exist(m): vis = [0] * len(s) for i in range(m): vis[a[i] - 1] = 1 j = 0 for i in range(len(s)): if vis[i] == 0: if s[i] == t[j]: j += 1 if j == len(t): return 1 return 0 l, r = 0, len(s) while l < r: mid = (l + r + 1) // 2 if exist(mid): l = mid else: r = mid - 1 print(l)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a_1... a_{|}t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. -----Input----- The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≀ |p| < |t| ≀ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a_1, a_2, ..., a_{|}t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≀ a_{i} ≀ |t|, all a_{i} are distinct). -----Output----- Print a single integer number, the maximum number of letters that Nastya can remove. -----Examples----- Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 -----Note----- In the first sample test sequence of removing made by Nastya looks like this: "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters.
s = input() t = input() a = list(map(int, input().split())) def ok(n): bad = set() for i in range(n): bad.add(a[i] - 1) pt = 0 ps = 0 while pt < len(t) and ps < len(s): if ps in bad: ps += 1 elif t[pt] == s[ps]: ps += 1 pt += 1 else: ps += 1 return pt == len(t) low = 0 high = len(s) while high >= low: mid = (high + low) // 2 if ok(mid): poss = mid low = mid + 1 else: high = mid - 1 print(poss)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a_1... a_{|}t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. -----Input----- The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≀ |p| < |t| ≀ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a_1, a_2, ..., a_{|}t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≀ a_{i} ≀ |t|, all a_{i} are distinct). -----Output----- Print a single integer number, the maximum number of letters that Nastya can remove. -----Examples----- Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 -----Note----- In the first sample test sequence of removing made by Nastya looks like this: "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters.
a = input() b = input() p = list(map(int, input().split())) for i in range(len(p)): p[i] -= 1 def good(m): pos = 0 a1 = list(a) for i in range(m): a1[p[i]] = "#" for i in range(len(a1)): if a1[i] == b[pos]: pos += 1 if pos == len(b): return True return False l = 0 r = len(a) while r - l > 1: m = (l + r) // 2 if good(m): l = m else: r = m print(l)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a_1... a_{|}t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. -----Input----- The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≀ |p| < |t| ≀ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a_1, a_2, ..., a_{|}t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≀ a_{i} ≀ |t|, all a_{i} are distinct). -----Output----- Print a single integer number, the maximum number of letters that Nastya can remove. -----Examples----- Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 -----Note----- In the first sample test sequence of removing made by Nastya looks like this: "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters.
s = list(input().strip()) t = input().strip() sq = [int(a) for a in input().strip().split()] l = len(s) lt = len(t) def check(x): tmp = s.copy() for i in range(x): tmp[sq[i] - 1] = "_" idx = 0 for i in range(l): if tmp[i] == t[idx]: idx += 1 if idx == lt: return True return False low = 0 high = l while low <= high: mid = low + high >> 1 if check(mid): low = mid + 1 else: high = mid - 1 print(high)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a_1... a_{|}t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. -----Input----- The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≀ |p| < |t| ≀ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a_1, a_2, ..., a_{|}t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≀ a_{i} ≀ |t|, all a_{i} are distinct). -----Output----- Print a single integer number, the maximum number of letters that Nastya can remove. -----Examples----- Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 -----Note----- In the first sample test sequence of removing made by Nastya looks like this: "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters.
sed = 257 mod = int(1000000000.0 + 7) def judge(t, p): j = 0 for ch in p: while j < len(t) and t[j] != ch: j += 1 if j >= len(t): return False j += 1 return True def generate(t, a, n): res = list(t) for i in range(n): res[a[i] - 1] = "#" res = "".join(res) return res.replace("#", "") t = input() p = input() a = list(map(int, input().split())) l, r = 0, len(t) - len(p) + 1 while l < r - 1: m = (l + r) // 2 tmp = generate(t, a, m) if judge(tmp, p): l = m else: r = m print(l)
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER STRING ASSIGN VAR FUNC_CALL STRING VAR RETURN FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a_1... a_{|}t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. -----Input----- The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≀ |p| < |t| ≀ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a_1, a_2, ..., a_{|}t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≀ a_{i} ≀ |t|, all a_{i} are distinct). -----Output----- Print a single integer number, the maximum number of letters that Nastya can remove. -----Examples----- Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 -----Note----- In the first sample test sequence of removing made by Nastya looks like this: "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters.
t = list(input()) p = list(input()) def has_sub(temp): idx = 0 for c in temp: if c == p[idx]: idx += 1 if idx == len(p): return True return False arr = list(map(int, input().split())) left = 0 right = len(arr) - 1 while left < right: mid = (left + right + 1) // 2 temp = t[:] for idx in range(mid): temp[arr[idx] - 1] = "0" if has_sub(temp): left = mid else: right = mid - 1 print(right)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER STRING IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a_1... a_{|}t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. -----Input----- The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≀ |p| < |t| ≀ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a_1, a_2, ..., a_{|}t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≀ a_{i} ≀ |t|, all a_{i} are distinct). -----Output----- Print a single integer number, the maximum number of letters that Nastya can remove. -----Examples----- Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 -----Note----- In the first sample test sequence of removing made by Nastya looks like this: "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters.
def good(t, p, b, k): j = 0 for i in range(len(t)): if b[i] >= k and t[i] == p[j]: j += 1 if j == len(p): return True return False def solve(t, p, a): b = [0] * len(a) for i in range(len(a)): b[a[i] - 1] = i low = 0 high = len(a) while low + 1 < high: mid = (low + high) // 2 if good(t, p, b, mid): low = mid else: high = mid return low t = input() p = input() a = list(map(int, input().split())) print(solve(t, p, a))
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a_1... a_{|}t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. -----Input----- The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≀ |p| < |t| ≀ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a_1, a_2, ..., a_{|}t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≀ a_{i} ≀ |t|, all a_{i} are distinct). -----Output----- Print a single integer number, the maximum number of letters that Nastya can remove. -----Examples----- Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 -----Note----- In the first sample test sequence of removing made by Nastya looks like this: "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters.
import sys inp = input() tar = input() seq = list(map(int, input().split())) l_inp = len(inp) l_tar = len(tar) def check_for_m(m): banned = set(seq[:m]) ip = 0 for i in range(l_inp): if not i + 1 in banned: if inp[i] == tar[ip]: ip += 1 if ip == l_tar: return True return False l = 0 r = l_inp + 1 while l + 1 < r: m = (l + r) // 2 if check_for_m(m): l = m else: r = m print(l)
IMPORT ASSIGN VAR FUNC_CALL VAR ASSIGN 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 VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a_1... a_{|}t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. -----Input----- The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≀ |p| < |t| ≀ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a_1, a_2, ..., a_{|}t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≀ a_{i} ≀ |t|, all a_{i} are distinct). -----Output----- Print a single integer number, the maximum number of letters that Nastya can remove. -----Examples----- Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 -----Note----- In the first sample test sequence of removing made by Nastya looks like this: "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters.
def checksub(tmp, t): j = 0 n = len(tmp) m = len(t) for i in range(n): if t[j] == tmp[i]: j += 1 if j == m: return True return False def solve(p, t, perms): l = 0 h = len(p) - 1 ans = 0 while l <= h: m = int((l + h) / 2) tmp = list(p) for i in range(m): tmp[perms[i] - 1] = "_" if checksub(tmp, t): ans = m l = m + 1 else: h = m - 1 return ans p = input() t = input() lst = list(map(int, input().split())) print(solve(p, t, lst))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER STRING IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a_1... a_{|}t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. -----Input----- The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≀ |p| < |t| ≀ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a_1, a_2, ..., a_{|}t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≀ a_{i} ≀ |t|, all a_{i} are distinct). -----Output----- Print a single integer number, the maximum number of letters that Nastya can remove. -----Examples----- Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 -----Note----- In the first sample test sequence of removing made by Nastya looks like this: "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters.
t = input() p = input() a = [int(v) for v in input().split()] lt = len(t) lp = len(p) def isValid(pos): flags = [0] * lt for i in range(pos + 1): flags[a[i] - 1] = 1 count = 0 for i in range(lt): if flags[i] == 0 and t[i] == p[count]: count += 1 if count == lp: return True return False left = 0 right = lt - 1 ans = -1 while left <= right: mid = (left + right) // 2 if isValid(mid): ans = mid left = mid + 1 else: right = mid - 1 print(ans + 1)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a_1... a_{|}t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. -----Input----- The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≀ |p| < |t| ≀ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a_1, a_2, ..., a_{|}t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≀ a_{i} ≀ |t|, all a_{i} are distinct). -----Output----- Print a single integer number, the maximum number of letters that Nastya can remove. -----Examples----- Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 -----Note----- In the first sample test sequence of removing made by Nastya looks like this: "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters.
def is_subsequence(a, b, idx, top): i = 0 for j, c in enumerate(a): if c == b[i] and idx[j + 1] > top: i += 1 if i == len(b): return True return False a = input() b = input() r = list(map(int, input().split())) idx = [(0) for _ in range(len(r) + 1)] for i in range(len(r)): idx[r[i]] = i lo = -1 hi = len(a) while hi - lo > 1: mi = (lo + hi) // 2 if is_subsequence(a, b, idx, mi): lo = mi else: hi = mi print(lo + 1)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a_1... a_{|}t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. -----Input----- The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≀ |p| < |t| ≀ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a_1, a_2, ..., a_{|}t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≀ a_{i} ≀ |t|, all a_{i} are distinct). -----Output----- Print a single integer number, the maximum number of letters that Nastya can remove. -----Examples----- Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 -----Note----- In the first sample test sequence of removing made by Nastya looks like this: "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters.
t = input() p = input() ai = list(map(int, input().split())) n = len(ai) ti = [[t[i], 1] for i in range(n)] for i in range(n): ai[i] -= 1 num2 = 1 def check(num): global num2 num2 -= 1 for i in range(num): ti[ai[i]][1] = num2 num3 = 0 for i in ti: if i[1] == num2: continue if i[0] == p[num3]: num3 += 1 if num3 == len(p): return True return False high = n low = 0 mid = (high + low) // 2 while high >= low: if check(mid): low = mid + 1 else: high = mid - 1 mid = (high + low) // 2 print(mid)
ASSIGN VAR FUNC_CALL VAR ASSIGN 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 LIST VAR VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR IF VAR NUMBER VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a_1... a_{|}t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. -----Input----- The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≀ |p| < |t| ≀ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a_1, a_2, ..., a_{|}t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≀ a_{i} ≀ |t|, all a_{i} are distinct). -----Output----- Print a single integer number, the maximum number of letters that Nastya can remove. -----Examples----- Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 -----Note----- In the first sample test sequence of removing made by Nastya looks like this: "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters.
def func(s1, s2): n = len(s1) m = len(s2) k = 0 for i in range(n): if s1[i] == s2[k]: k = k + 1 if k == m: return True break return False s1 = list(input()) s2 = list(input()) a = list(map(int, input().strip().split(" "))) high = len(a) low = 0 b = [] qw = 0 w = -1 for i in s1: b.append(i) while low <= high: mid = (low + high) // 2 if qw == 0: for i in range(low, mid + 1): s1[a[i] - 1] = "8" if func(s1, s2) == True: w = mid low = mid + 1 qw = 0 else: high = mid - 1 mid1 = (high + low) // 2 for i in range(mid1 + 1, mid + 1): s1[a[i] - 1] = b[a[i] - 1] qw = 1 if w != -1: print(w + 1) else: print(0)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER STRING IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a_1... a_{|}t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. -----Input----- The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≀ |p| < |t| ≀ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a_1, a_2, ..., a_{|}t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≀ a_{i} ≀ |t|, all a_{i} are distinct). -----Output----- Print a single integer number, the maximum number of letters that Nastya can remove. -----Examples----- Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 -----Note----- In the first sample test sequence of removing made by Nastya looks like this: "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters.
def sub(a, s): pa = 0 ps = 0 while pa < len(a) and ps < len(s): if a[pa] == s[ps]: ps += 1 pa += 1 else: pa += 1 return ps == len(s) def subword(t, ord_ar, n): t_copy = [] for i in range(len(ord_ar)): if ord_ar[i] >= n: t_copy.append(t[i]) return t_copy def check(t, p, ord_ar, n): s = subword(t, ord_ar, n) return sub(s, p) def bin_s(l, r, f): while r > l + 1: m = (r + l) // 2 if f(m): l = m else: r = m return l def main(): t = input().strip() p = input().strip() ord_ar = [0] * len(t) seq = list(map(int, input().strip().split())) for i, x in enumerate(seq): ord_ar[x - 1] = i ans = bin_s(0, len(t), lambda n: check(t, p, ord_ar, n)) print(ans) main()
FUNC_DEF 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 VAR NUMBER RETURN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a_1... a_{|}t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. -----Input----- The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≀ |p| < |t| ≀ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a_1, a_2, ..., a_{|}t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≀ a_{i} ≀ |t|, all a_{i} are distinct). -----Output----- Print a single integer number, the maximum number of letters that Nastya can remove. -----Examples----- Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 -----Note----- In the first sample test sequence of removing made by Nastya looks like this: "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters.
def main(): a_string = input() b_string = input() moves = [int(x) for x in input().split()] index_of_move = {} lenb = len(b_string) for index, elem in enumerate(moves): index_of_move[elem] = index + 1 l = 0 r = len(moves) - 1 while l < r: middle = int((l + r + 1) / 2) bi = 0 i = 0 for index, elem in enumerate(a_string): if index_of_move[index + 1] <= middle + 1: continue if elem is b_string[bi]: bi += 1 if bi >= lenb: break if bi >= lenb: l = middle else: r = middle - 1 if l is 0 and r is 0: bi = 0 for index, elem in enumerate(a_string): if index_of_move[index + 1] <= 1: continue if elem is b_string[bi]: bi += 1 if bi >= lenb: break if bi >= lenb: print(1) else: print(0) else: print(l + 1) def __starting_point(): main() __starting_point()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR NUMBER IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a_1... a_{|}t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. -----Input----- The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≀ |p| < |t| ≀ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a_1, a_2, ..., a_{|}t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≀ a_{i} ≀ |t|, all a_{i} are distinct). -----Output----- Print a single integer number, the maximum number of letters that Nastya can remove. -----Examples----- Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 -----Note----- In the first sample test sequence of removing made by Nastya looks like this: "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters.
s, t = input(), input() ind = list(map(int, input().split())) def is_substring(text, word, exclude): wi = 0 for i in range(len(text)): if wi < len(word) and text[i] == word[wi] and i + 1 not in exclude: wi += 1 return wi == len(word) l, r = 0, len(ind) while l < r: mid = (l + r) // 2 + 1 if is_substring(s, t, set(ind[:mid])): l = mid else: r = mid - 1 print(l)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a_1... a_{|}t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. -----Input----- The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≀ |p| < |t| ≀ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a_1, a_2, ..., a_{|}t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≀ a_{i} ≀ |t|, all a_{i} are distinct). -----Output----- Print a single integer number, the maximum number of letters that Nastya can remove. -----Examples----- Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 -----Note----- In the first sample test sequence of removing made by Nastya looks like this: "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters.
def isSubSequence(pat, text, Done): t = 0 for i in range(len(text)): if t < len(pat) and text[i] == pat[t] and i + 1 not in Done: t += 1 return t == len(pat) s = input() r = input() A = list(map(int, input().split())) n = len(A) low = 0 high = n while low < high: mid = (low + high) // 2 + 1 if isSubSequence(r, s, set(A[:mid])): low = mid else: high = mid - 1 print(low)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a_1... a_{|}t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. -----Input----- The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≀ |p| < |t| ≀ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a_1, a_2, ..., a_{|}t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≀ a_{i} ≀ |t|, all a_{i} are distinct). -----Output----- Print a single integer number, the maximum number of letters that Nastya can remove. -----Examples----- Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 -----Note----- In the first sample test sequence of removing made by Nastya looks like this: "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters.
t = input() p = input() a = list(map(int, input().strip().split())) a = [(x - 1) for x in a] n = len(t) m = len(p) lo = 0 hi = n - 1 while lo < hi: mid = lo + hi >> 1 temp = list(t) for x in a[: mid + 1]: temp[x] = "_" ptr, curr = 0, 0 while ptr < n and curr < m: while ptr < n and temp[ptr] != p[curr]: ptr += 1 if ptr < n: ptr += 1 curr += 1 if curr == m: lo = mid + 1 else: hi = mid print(lo)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a_1... a_{|}t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. -----Input----- The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≀ |p| < |t| ≀ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a_1, a_2, ..., a_{|}t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≀ a_{i} ≀ |t|, all a_{i} are distinct). -----Output----- Print a single integer number, the maximum number of letters that Nastya can remove. -----Examples----- Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 -----Note----- In the first sample test sequence of removing made by Nastya looks like this: "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters.
def is_match(string_long, string_short): idx_long = 0 for idx, value in enumerate(string_short): while idx_long < len(string_long) and string_long[idx_long] != value: idx_long += 1 if idx_long == len(string_long): return False idx_long += 1 return idx_long <= len(string_long) def solve(string_before_delete, string_after_delete, deletes): deletes_at_step = list(range(len(deletes))) for idx, value in enumerate(deletes): deletes_at_step[value - 1] = idx + 1 left = 0 right = len(deletes) while left + 1 < right: mid = (left + right + 1) // 2 string_mid = "".join( [ value for idx, value in enumerate(string_before_delete) if deletes_at_step[idx] > mid ] ) if is_match(string_mid, string_after_delete): left = mid else: right = mid return left string_before_delete = input().strip() string_after_delete = input().strip() deletes = list(map(int, input().strip().split(" "))) print(solve(string_before_delete, string_after_delete, deletes))
FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER VAR NUMBER RETURN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL STRING VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a_1... a_{|}t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya" $\rightarrow$ "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. -----Input----- The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≀ |p| < |t| ≀ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a_1, a_2, ..., a_{|}t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≀ a_{i} ≀ |t|, all a_{i} are distinct). -----Output----- Print a single integer number, the maximum number of letters that Nastya can remove. -----Examples----- Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 -----Note----- In the first sample test sequence of removing made by Nastya looks like this: "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" $\rightarrow$ "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters.
from sys import stdin def input(): return stdin.readline() s = list(input()) t = input() a = list(map(lambda x: int(x) - 1, input().split())) n = len(a) def can(s): cur = 0 for i, ch in enumerate(s): if ch == t[cur]: cur += 1 if cur == len(t): return True return False left, right = -1, n while left + 1 < right: mid = left + right >> 1 temp = s[:] for i in range(mid): temp[a[i]] = "" if can("".join(temp)): left = mid else: right = mid print(left)
FUNC_DEF RETURN FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER VAR WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR STRING IF FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is constraints. Ivan plays a computer game that contains some microtransactions to make characters look cooler. Since Ivan wants his character to be really cool, he wants to use some of these microtransactions β€” and he won't start playing until he gets all of them. Each day (during the morning) Ivan earns exactly one burle. There are $n$ types of microtransactions in the game. Each microtransaction costs $2$ burles usually and $1$ burle if it is on sale. Ivan has to order exactly $k_i$ microtransactions of the $i$-th type (he orders microtransactions during the evening). Ivan can order any (possibly zero) number of microtransactions of any types during any day (of course, if he has enough money to do it). If the microtransaction he wants to order is on sale then he can buy it for $1$ burle and otherwise he can buy it for $2$ burles. There are also $m$ special offers in the game shop. The $j$-th offer $(d_j, t_j)$ means that microtransactions of the $t_j$-th type are on sale during the $d_j$-th day. Ivan wants to order all microtransactions as soon as possible. Your task is to calculate the minimum day when he can buy all microtransactions he want and actually start playing. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the number of types of microtransactions and the number of special offers in the game shop. The second line of the input contains $n$ integers $k_1, k_2, \dots, k_n$ ($0 \le k_i \le 2 \cdot 10^5$), where $k_i$ is the number of copies of microtransaction of the $i$-th type Ivan has to order. It is guaranteed that sum of all $k_i$ is not less than $1$ and not greater than $2 \cdot 10^5$. The next $m$ lines contain special offers. The $j$-th of these lines contains the $j$-th special offer. It is given as a pair of integers $(d_j, t_j)$ ($1 \le d_j \le 2 \cdot 10^5, 1 \le t_j \le n$) and means that microtransactions of the $t_j$-th type are on sale during the $d_j$-th day. -----Output----- Print one integer β€” the minimum day when Ivan can order all microtransactions he wants and actually start playing. -----Examples----- Input 5 6 1 2 0 2 0 2 4 3 3 1 5 1 2 1 5 2 3 Output 8 Input 5 3 4 2 1 3 2 3 5 4 2 2 5 Output 20
import sys def main(): n, m = map(int, input().split()) k = [int(x) for x in input().split()] d = [[] for _ in range(4 * 10**5 + 1)] for j in range(m): dj, tj = map(int, input().split()) d[dj - 1].append(tj - 1) lo, hi = 0, 4 * 10**5 + 1 while lo < hi: mi = (hi + lo) // 2 cash = mi offset = 0 _k = k[:] for i in reversed(range(mi)): for j in d[i]: while cash and _k[j]: _k[j] -= 1 cash -= 1 if cash == i + 1: cash -= 2 offset += 1 if 2 * (sum(_k) - offset) <= cash: hi = mi else: lo = mi + 1 print(lo) input = iter(sys.stdin.read().splitlines()).__next__ main()
IMPORT FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR
The only difference between easy and hard versions is constraints. Ivan plays a computer game that contains some microtransactions to make characters look cooler. Since Ivan wants his character to be really cool, he wants to use some of these microtransactions β€” and he won't start playing until he gets all of them. Each day (during the morning) Ivan earns exactly one burle. There are $n$ types of microtransactions in the game. Each microtransaction costs $2$ burles usually and $1$ burle if it is on sale. Ivan has to order exactly $k_i$ microtransactions of the $i$-th type (he orders microtransactions during the evening). Ivan can order any (possibly zero) number of microtransactions of any types during any day (of course, if he has enough money to do it). If the microtransaction he wants to order is on sale then he can buy it for $1$ burle and otherwise he can buy it for $2$ burles. There are also $m$ special offers in the game shop. The $j$-th offer $(d_j, t_j)$ means that microtransactions of the $t_j$-th type are on sale during the $d_j$-th day. Ivan wants to order all microtransactions as soon as possible. Your task is to calculate the minimum day when he can buy all microtransactions he want and actually start playing. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the number of types of microtransactions and the number of special offers in the game shop. The second line of the input contains $n$ integers $k_1, k_2, \dots, k_n$ ($0 \le k_i \le 2 \cdot 10^5$), where $k_i$ is the number of copies of microtransaction of the $i$-th type Ivan has to order. It is guaranteed that sum of all $k_i$ is not less than $1$ and not greater than $2 \cdot 10^5$. The next $m$ lines contain special offers. The $j$-th of these lines contains the $j$-th special offer. It is given as a pair of integers $(d_j, t_j)$ ($1 \le d_j \le 2 \cdot 10^5, 1 \le t_j \le n$) and means that microtransactions of the $t_j$-th type are on sale during the $d_j$-th day. -----Output----- Print one integer β€” the minimum day when Ivan can order all microtransactions he wants and actually start playing. -----Examples----- Input 5 6 1 2 0 2 0 2 4 3 3 1 5 1 2 1 5 2 3 Output 8 Input 5 3 4 2 1 3 2 3 5 4 2 2 5 Output 20
def check(x): last = [0] * (n + 1) for i in tmp: if i[0] > x: break else: last[i[1]] = i[0] sal = [0] * (x + 1) for i in range(1, n + 1): sal[last[i]] += lis[i - 1] c = 0 for i in range(1, x + 1): c += 1 if sal[i] >= c: sal[i] -= c c = 0 else: c -= sal[i] sal[i] = 0 if sum(sal) * 2 <= c: return True else: return False n, m = map(int, input().split()) lis = list(map(int, input().split())) tmp = [] for _ in range(m): a, b = map(int, input().split()) tmp.append([a, b]) tmp.sort() l = 0 r = sum(lis) * 2 while l <= r: mid = l + (r - l) // 2 if check(mid): r = mid - 1 else: l = mid + 1 if check(r): print(r) elif check(l): print(l) else: print(l + 1)
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR VAR VAR ASSIGN VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR 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 VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
The only difference between easy and hard versions is constraints. Ivan plays a computer game that contains some microtransactions to make characters look cooler. Since Ivan wants his character to be really cool, he wants to use some of these microtransactions β€” and he won't start playing until he gets all of them. Each day (during the morning) Ivan earns exactly one burle. There are $n$ types of microtransactions in the game. Each microtransaction costs $2$ burles usually and $1$ burle if it is on sale. Ivan has to order exactly $k_i$ microtransactions of the $i$-th type (he orders microtransactions during the evening). Ivan can order any (possibly zero) number of microtransactions of any types during any day (of course, if he has enough money to do it). If the microtransaction he wants to order is on sale then he can buy it for $1$ burle and otherwise he can buy it for $2$ burles. There are also $m$ special offers in the game shop. The $j$-th offer $(d_j, t_j)$ means that microtransactions of the $t_j$-th type are on sale during the $d_j$-th day. Ivan wants to order all microtransactions as soon as possible. Your task is to calculate the minimum day when he can buy all microtransactions he want and actually start playing. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the number of types of microtransactions and the number of special offers in the game shop. The second line of the input contains $n$ integers $k_1, k_2, \dots, k_n$ ($0 \le k_i \le 2 \cdot 10^5$), where $k_i$ is the number of copies of microtransaction of the $i$-th type Ivan has to order. It is guaranteed that sum of all $k_i$ is not less than $1$ and not greater than $2 \cdot 10^5$. The next $m$ lines contain special offers. The $j$-th of these lines contains the $j$-th special offer. It is given as a pair of integers $(d_j, t_j)$ ($1 \le d_j \le 2 \cdot 10^5, 1 \le t_j \le n$) and means that microtransactions of the $t_j$-th type are on sale during the $d_j$-th day. -----Output----- Print one integer β€” the minimum day when Ivan can order all microtransactions he wants and actually start playing. -----Examples----- Input 5 6 1 2 0 2 0 2 4 3 3 1 5 1 2 1 5 2 3 Output 8 Input 5 3 4 2 1 3 2 3 5 4 2 2 5 Output 20
import sys N, M = [int(x) for x in sys.stdin.readline().rstrip().split()] K = [int(x) for x in sys.stdin.readline().rstrip().split()] D = [0] * M T = [0] * M for i in range(M): D[i], T[i] = [int(x) for x in sys.stdin.readline().rstrip().split()] D[i] -= 1 T[i] -= 1 KS = sum(K) ok = 2 * KS ng = KS - 1 while ok - ng > 1: X = (ok + ng) // 2 last = [-1] * N for i in range(M): if last[T[i]] < D[i] < X: last[T[i]] = D[i] l2i = [[] for i in range(X)] for i in range(N): if last[i] != -1: l2i[last[i]].append(i) buy = 0 now = 0 for i in range(X): now += 1 for j in l2i[i]: s = now if now < K[j] else K[j] now -= s buy += s buy += now // 2 if buy >= KS: ok = X else: ng = X print(ok)
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is constraints. Ivan plays a computer game that contains some microtransactions to make characters look cooler. Since Ivan wants his character to be really cool, he wants to use some of these microtransactions β€” and he won't start playing until he gets all of them. Each day (during the morning) Ivan earns exactly one burle. There are $n$ types of microtransactions in the game. Each microtransaction costs $2$ burles usually and $1$ burle if it is on sale. Ivan has to order exactly $k_i$ microtransactions of the $i$-th type (he orders microtransactions during the evening). Ivan can order any (possibly zero) number of microtransactions of any types during any day (of course, if he has enough money to do it). If the microtransaction he wants to order is on sale then he can buy it for $1$ burle and otherwise he can buy it for $2$ burles. There are also $m$ special offers in the game shop. The $j$-th offer $(d_j, t_j)$ means that microtransactions of the $t_j$-th type are on sale during the $d_j$-th day. Ivan wants to order all microtransactions as soon as possible. Your task is to calculate the minimum day when he can buy all microtransactions he want and actually start playing. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the number of types of microtransactions and the number of special offers in the game shop. The second line of the input contains $n$ integers $k_1, k_2, \dots, k_n$ ($0 \le k_i \le 2 \cdot 10^5$), where $k_i$ is the number of copies of microtransaction of the $i$-th type Ivan has to order. It is guaranteed that sum of all $k_i$ is not less than $1$ and not greater than $2 \cdot 10^5$. The next $m$ lines contain special offers. The $j$-th of these lines contains the $j$-th special offer. It is given as a pair of integers $(d_j, t_j)$ ($1 \le d_j \le 2 \cdot 10^5, 1 \le t_j \le n$) and means that microtransactions of the $t_j$-th type are on sale during the $d_j$-th day. -----Output----- Print one integer β€” the minimum day when Ivan can order all microtransactions he wants and actually start playing. -----Examples----- Input 5 6 1 2 0 2 0 2 4 3 3 1 5 1 2 1 5 2 3 Output 8 Input 5 3 4 2 1 3 2 3 5 4 2 2 5 Output 20
def BinarySearch(my_list, key): l = 0 r = len(my_list) - 1 while r > l: mid = (l + r) // 2 if my_list[mid + 1] <= key: l = mid + 1 else: r = mid return l def weather_can_buy(day, total, req_list, sale_days): tmp_buy = [] last_day = [] d = day my_total = total for i in range(len(req_list)): last_day.append(0) for i in range(d): tmp_buy.append(0) for i in range(len(sale_days)): if len(sale_days[i]) > 0: index = BinarySearch(sale_days[i], day) last_day[i] = sale_days[i][index] for i in range(len(last_day)): if last_day[i] - 1 > -1: tmp_buy[last_day[i] - 1] += req_list[i] money = 0 for i in range(d): money += 1 my_total -= min(money, tmp_buy[i]) money -= min(money, tmp_buy[i]) if my_total < 0: return True elif money >= my_total * 2: return True else: return False def function(): tmp = input().split(" ") n = int(tmp[0]) m = int(tmp[1]) req_list = input().split(" ") req_list = list(map(int, req_list)) total = sum(req_list) sale_days = [] for i in range(len(req_list)): sale_days.append([0]) for i in range(m): tmp = input().split(" ") dj, tj = int(tmp[0]), int(tmp[1]) sale_days[tj - 1].append(dj) for i in range(len(sale_days)): sale_days[i] = sorted(sale_days[i]) for i in range(len(sale_days)): sale_days[i] = sorted(sale_days[i]) l = 1 r = 2 * total while r > l: mid = (l + r) // 2 if weather_can_buy(mid, total, req_list, sale_days): r = mid else: l = mid + 1 print(r) function()
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
The only difference between easy and hard versions is constraints. Ivan plays a computer game that contains some microtransactions to make characters look cooler. Since Ivan wants his character to be really cool, he wants to use some of these microtransactions β€” and he won't start playing until he gets all of them. Each day (during the morning) Ivan earns exactly one burle. There are $n$ types of microtransactions in the game. Each microtransaction costs $2$ burles usually and $1$ burle if it is on sale. Ivan has to order exactly $k_i$ microtransactions of the $i$-th type (he orders microtransactions during the evening). Ivan can order any (possibly zero) number of microtransactions of any types during any day (of course, if he has enough money to do it). If the microtransaction he wants to order is on sale then he can buy it for $1$ burle and otherwise he can buy it for $2$ burles. There are also $m$ special offers in the game shop. The $j$-th offer $(d_j, t_j)$ means that microtransactions of the $t_j$-th type are on sale during the $d_j$-th day. Ivan wants to order all microtransactions as soon as possible. Your task is to calculate the minimum day when he can buy all microtransactions he want and actually start playing. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the number of types of microtransactions and the number of special offers in the game shop. The second line of the input contains $n$ integers $k_1, k_2, \dots, k_n$ ($0 \le k_i \le 2 \cdot 10^5$), where $k_i$ is the number of copies of microtransaction of the $i$-th type Ivan has to order. It is guaranteed that sum of all $k_i$ is not less than $1$ and not greater than $2 \cdot 10^5$. The next $m$ lines contain special offers. The $j$-th of these lines contains the $j$-th special offer. It is given as a pair of integers $(d_j, t_j)$ ($1 \le d_j \le 2 \cdot 10^5, 1 \le t_j \le n$) and means that microtransactions of the $t_j$-th type are on sale during the $d_j$-th day. -----Output----- Print one integer β€” the minimum day when Ivan can order all microtransactions he wants and actually start playing. -----Examples----- Input 5 6 1 2 0 2 0 2 4 3 3 1 5 1 2 1 5 2 3 Output 8 Input 5 3 4 2 1 3 2 3 5 4 2 2 5 Output 20
import sys input = sys.stdin.readline def check(num): last = [(0) for i in range(n)] for i in o: if i[0] > num: break else: last[i[1] - 1] = i[0] b = [(0) for i in range(num + 1)] for i in range(n): b[last[i]] += a[i] i = 0 c = 0 d = 0 for i in range(1, num + 1): c += 1 if c >= b[i]: c -= b[i] b[i] = 0 else: b[i] -= c c = 0 s = sum(b) if s * 2 <= c: return True return False n, m = map(int, input().split()) a = list(map(int, input().split())) o = [] for i in range(m): x, y = map(int, input().split()) o.append((x, y)) o.sort() low = 1 high = sum(a) * 2 while low < high: mid = (low + high) // 2 if check(mid): high = mid - 1 else: low = mid + 1 if check(low): print(low) else: print(low + 1)
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR 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 EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
The only difference between easy and hard versions is constraints. Ivan plays a computer game that contains some microtransactions to make characters look cooler. Since Ivan wants his character to be really cool, he wants to use some of these microtransactions β€” and he won't start playing until he gets all of them. Each day (during the morning) Ivan earns exactly one burle. There are $n$ types of microtransactions in the game. Each microtransaction costs $2$ burles usually and $1$ burle if it is on sale. Ivan has to order exactly $k_i$ microtransactions of the $i$-th type (he orders microtransactions during the evening). Ivan can order any (possibly zero) number of microtransactions of any types during any day (of course, if he has enough money to do it). If the microtransaction he wants to order is on sale then he can buy it for $1$ burle and otherwise he can buy it for $2$ burles. There are also $m$ special offers in the game shop. The $j$-th offer $(d_j, t_j)$ means that microtransactions of the $t_j$-th type are on sale during the $d_j$-th day. Ivan wants to order all microtransactions as soon as possible. Your task is to calculate the minimum day when he can buy all microtransactions he want and actually start playing. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the number of types of microtransactions and the number of special offers in the game shop. The second line of the input contains $n$ integers $k_1, k_2, \dots, k_n$ ($0 \le k_i \le 2 \cdot 10^5$), where $k_i$ is the number of copies of microtransaction of the $i$-th type Ivan has to order. It is guaranteed that sum of all $k_i$ is not less than $1$ and not greater than $2 \cdot 10^5$. The next $m$ lines contain special offers. The $j$-th of these lines contains the $j$-th special offer. It is given as a pair of integers $(d_j, t_j)$ ($1 \le d_j \le 2 \cdot 10^5, 1 \le t_j \le n$) and means that microtransactions of the $t_j$-th type are on sale during the $d_j$-th day. -----Output----- Print one integer β€” the minimum day when Ivan can order all microtransactions he wants and actually start playing. -----Examples----- Input 5 6 1 2 0 2 0 2 4 3 3 1 5 1 2 1 5 2 3 Output 8 Input 5 3 4 2 1 3 2 3 5 4 2 2 5 Output 20
def check(mid): l = [(0) for i in range(n)] for i in b: if i[0] > mid: break l[i[1] - 1] = i[0] v = [(0) for i in range(mid + 1)] for i in range(n): v[l[i]] += a[i] ct = 0 for i in range(1, mid + 1): ct += 1 if ct >= v[i]: ct -= v[i] v[i] = 0 else: v[i] -= ct ct = 0 return ct >= 2 * sum(v) def bs(): l = 0 r = 5 * 10**5 while l <= r: mid = (l + r) // 2 if check(mid): r = mid - 1 else: l = mid + 1 return r + 1 n, m = map(int, input().split()) a = list(map(int, input().split())) b = [] for i in range(m): b.append(list(map(int, input().split()))) b.sort() print(bs())
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER RETURN VAR BIN_OP NUMBER FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR 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 EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Read problems statements in Mandarin Chinese and Russian as well. Mike takes part in programming contests. His favourite topic is dynamic programming(DP). As he said, that he likes problems on DP, because "you spend a lot of time on thinking and a little time on coding". In this problem you are to solve a version of the knapsack problem(link), one of the most famous examples of DP problem. You are given N items, each has two parameters: a weight and a cost. Let's define M as the sum of the weights of all the items. Your task is to determine the most expensive cost of a knapsack, which capacity equals to 1, 2, ..., M. A cost of a knapsack equals to the sum of the costs of all the elements of the knapsack. Also, when you have a knapsack with a capacity is equal to C, then you can fill it with items, whose sum of weights is not greater than C. ------ Input ------ The first line of the input contains one integer N, denoting the number of the items. The next N lines contain two integers W and C each, denoting the weight and the cost of the corresponding item. ------ Output ------ For each capacity C (1 ≀ C ≀ M) of the knapsack, output a single integer - the most expensive cost for that capacity. ------ Constraints ------ 3 ≀ N ≀ 100000; 1 ≀ W ≀ 2, for each item; 1 ≀ C ≀ 10^{9}, for each item. ------ Example ------ Input: 5 1 1 2 2 2 3 2 4 2 5 Output: 1 5 6 9 10 12 13 14 15 ------ Explanations ------ In the test case, M equals to 9. For C = 1, it's optimal to choose {1} items; For C = 2, it's optimal to choose {5} items; For C = 3, it's optimal to choose {1, 5} items; For C = 4, it's optimal to choose {4, 5} items; For C = 5, it's optimal to choose {1, 4, 5} items; For C = 6, it's optimal to choose {3, 4, 5} items; For C = 7, it's optimal to choose {1, 3, 4, 5} items; For C = 8, it's optimal to choose {2, 3, 4, 5} items; For C = 9, it's optimal to choose {1, 2, 3, 4, 5} items.
n = int(input()) W1 = [] W2 = [] m = 0 for i in range(n): c, w = list(map(int, input().split())) if c == 1: W1.append(w) else: W2.append(w) m += c W1.sort() W2.sort() ans = [None for i in range(m + 1)] cur = 0 w1, w2 = W1[:], W2[:] for i in range(2, m + 1, 2): c1, c2 = 0, 0 flag = 1 if len(w1) >= 2: c1 = sum(w1[-2:]) elif len(w1) >= 1: c1 = w1[-1] flag = 0 if len(w2) >= 1: c2 = w2[-1] if c1 > c2: cur += c1 w1.pop() if flag: w1.pop() else: cur += c2 w2.pop() ans[i] = str(cur) w1, w2 = W1[:], W2[:] cur = 0 if len(w1) >= 1: cur = w1.pop() ans[1] = str(cur) for i in range(3, m + 1, 2): c1, c2 = 0, 0 flag = 1 if len(w1) >= 2: c1 = sum(w1[-2:]) elif len(w1) >= 1: c1 = w1[-1] flag = 0 if len(w2) >= 1: c2 = w2[-1] if c1 < c2: cur += c2 w2.pop() else: cur += c1 w1.pop() if flag: w1.pop() ans[i] = str(cur) print(" ".join(ans[1:]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NONE VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER
Read problems statements in Mandarin Chinese and Russian as well. Mike takes part in programming contests. His favourite topic is dynamic programming(DP). As he said, that he likes problems on DP, because "you spend a lot of time on thinking and a little time on coding". In this problem you are to solve a version of the knapsack problem(link), one of the most famous examples of DP problem. You are given N items, each has two parameters: a weight and a cost. Let's define M as the sum of the weights of all the items. Your task is to determine the most expensive cost of a knapsack, which capacity equals to 1, 2, ..., M. A cost of a knapsack equals to the sum of the costs of all the elements of the knapsack. Also, when you have a knapsack with a capacity is equal to C, then you can fill it with items, whose sum of weights is not greater than C. ------ Input ------ The first line of the input contains one integer N, denoting the number of the items. The next N lines contain two integers W and C each, denoting the weight and the cost of the corresponding item. ------ Output ------ For each capacity C (1 ≀ C ≀ M) of the knapsack, output a single integer - the most expensive cost for that capacity. ------ Constraints ------ 3 ≀ N ≀ 100000; 1 ≀ W ≀ 2, for each item; 1 ≀ C ≀ 10^{9}, for each item. ------ Example ------ Input: 5 1 1 2 2 2 3 2 4 2 5 Output: 1 5 6 9 10 12 13 14 15 ------ Explanations ------ In the test case, M equals to 9. For C = 1, it's optimal to choose {1} items; For C = 2, it's optimal to choose {5} items; For C = 3, it's optimal to choose {1, 5} items; For C = 4, it's optimal to choose {4, 5} items; For C = 5, it's optimal to choose {1, 4, 5} items; For C = 6, it's optimal to choose {3, 4, 5} items; For C = 7, it's optimal to choose {1, 3, 4, 5} items; For C = 8, it's optimal to choose {2, 3, 4, 5} items; For C = 9, it's optimal to choose {1, 2, 3, 4, 5} items.
def printArray(A, start, end): for i in range(start, end + 1): print(A[i], end=" ") print() n = int(input()) itemsOfWeight1 = [] itemsOfWeight2 = [] m = 0 for i in range(n): w, c = [int(x) for x in input().split()] if w == 1: itemsOfWeight1.append({"weight": w, "cost": c}) else: itemsOfWeight2.append({"weight": w, "cost": c}) m += w itemsOfWeight1.sort(key=lambda item: item["cost"], reverse=True) itemsOfWeight2.sort(key=lambda item: item["cost"], reverse=True) maxCost = [(0) for x in range(m + 1)] itemsUsed = [{"ones": -1, "twos": -1} for x in range(m + 1)] if len(itemsOfWeight1) != 0: maxCost[1] = itemsOfWeight1[0]["cost"] itemsUsed[1] = {"ones": 0, "twos": -1} for c in range(2, m + 1): nextItemOfWeight1 = itemsUsed[c - 1]["ones"] + 1 nextItemOfWeight2 = itemsUsed[c - 2]["twos"] + 1 itemOfWeight1Remaining = nextItemOfWeight1 < len(itemsOfWeight1) itemOfWeight2Remaining = nextItemOfWeight2 < len(itemsOfWeight2) if itemOfWeight2Remaining: maxCost[c] = maxCost[c - 2] + itemsOfWeight2[nextItemOfWeight2]["cost"] itemsUsed[c] = {"ones": itemsUsed[c - 2]["ones"], "twos": nextItemOfWeight2} if itemOfWeight1Remaining: possibleCost = maxCost[c - 1] + itemsOfWeight1[nextItemOfWeight1]["cost"] if possibleCost > maxCost[c]: maxCost[c] = possibleCost itemsUsed[c] = {"ones": nextItemOfWeight1, "twos": itemsUsed[c - 1]["twos"]} else: possibleCost = maxCost[c - 1] if possibleCost > maxCost[c]: maxCost[c] = possibleCost itemsUsed[c] = { "ones": itemsUsed[c - 1]["ones"], "twos": itemsUsed[c - 1]["twos"], } printArray(maxCost, 1, len(maxCost) - 1)
FUNC_DEF FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR DICT STRING STRING VAR VAR EXPR FUNC_CALL VAR DICT STRING STRING VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING NUMBER EXPR FUNC_CALL VAR VAR STRING NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR DICT STRING STRING NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER STRING ASSIGN VAR NUMBER DICT STRING STRING NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER STRING NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER STRING NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR STRING ASSIGN VAR VAR DICT STRING STRING VAR BIN_OP VAR NUMBER STRING VAR IF VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR STRING IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR DICT STRING STRING VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR DICT STRING STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER
Read problems statements in Mandarin Chinese and Russian as well. Mike takes part in programming contests. His favourite topic is dynamic programming(DP). As he said, that he likes problems on DP, because "you spend a lot of time on thinking and a little time on coding". In this problem you are to solve a version of the knapsack problem(link), one of the most famous examples of DP problem. You are given N items, each has two parameters: a weight and a cost. Let's define M as the sum of the weights of all the items. Your task is to determine the most expensive cost of a knapsack, which capacity equals to 1, 2, ..., M. A cost of a knapsack equals to the sum of the costs of all the elements of the knapsack. Also, when you have a knapsack with a capacity is equal to C, then you can fill it with items, whose sum of weights is not greater than C. ------ Input ------ The first line of the input contains one integer N, denoting the number of the items. The next N lines contain two integers W and C each, denoting the weight and the cost of the corresponding item. ------ Output ------ For each capacity C (1 ≀ C ≀ M) of the knapsack, output a single integer - the most expensive cost for that capacity. ------ Constraints ------ 3 ≀ N ≀ 100000; 1 ≀ W ≀ 2, for each item; 1 ≀ C ≀ 10^{9}, for each item. ------ Example ------ Input: 5 1 1 2 2 2 3 2 4 2 5 Output: 1 5 6 9 10 12 13 14 15 ------ Explanations ------ In the test case, M equals to 9. For C = 1, it's optimal to choose {1} items; For C = 2, it's optimal to choose {5} items; For C = 3, it's optimal to choose {1, 5} items; For C = 4, it's optimal to choose {4, 5} items; For C = 5, it's optimal to choose {1, 4, 5} items; For C = 6, it's optimal to choose {3, 4, 5} items; For C = 7, it's optimal to choose {1, 3, 4, 5} items; For C = 8, it's optimal to choose {2, 3, 4, 5} items; For C = 9, it's optimal to choose {1, 2, 3, 4, 5} items.
test = False N = int(input()) ones = [] twos = [] for i in range(N): W, C = map(int, input().strip().split(" ")) if test: print("W, C = ", W, C) if W == 1: ones.append(C) else: twos.append(C) max_weight = len(ones) + 2 * len(twos) if len(ones) >= 1: ones.append(0) ones.sort(reverse=True) even_twos = twos.copy() odd_twos = twos.copy() for i in range(1, len(ones)): if i % 2 == 1: even_twos.append(ones[i] + ones[i - 1]) else: odd_twos.append(ones[i] + ones[i - 1]) even_twos = sorted(even_twos) odd_twos = sorted(odd_twos) res = [0, ones[0]] for w in range(2, max_weight + 1): if w % 2 == 0: temp = even_twos.pop() else: temp = odd_twos.pop() res.append(res[w - 2] + temp) else: res = [0, 0] twos = sorted(twos) for w in range(2, max_weight + 1): temp = 0 if w % 2 == 0: temp = twos.pop() res.append(max(res[w - 2] + temp, res[w - 1])) res.pop(0) print(*res, sep=" ")
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING IF VAR EXPR FUNC_CALL VAR STRING VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR STRING
Read problems statements in Mandarin Chinese and Russian as well. Mike takes part in programming contests. His favourite topic is dynamic programming(DP). As he said, that he likes problems on DP, because "you spend a lot of time on thinking and a little time on coding". In this problem you are to solve a version of the knapsack problem(link), one of the most famous examples of DP problem. You are given N items, each has two parameters: a weight and a cost. Let's define M as the sum of the weights of all the items. Your task is to determine the most expensive cost of a knapsack, which capacity equals to 1, 2, ..., M. A cost of a knapsack equals to the sum of the costs of all the elements of the knapsack. Also, when you have a knapsack with a capacity is equal to C, then you can fill it with items, whose sum of weights is not greater than C. ------ Input ------ The first line of the input contains one integer N, denoting the number of the items. The next N lines contain two integers W and C each, denoting the weight and the cost of the corresponding item. ------ Output ------ For each capacity C (1 ≀ C ≀ M) of the knapsack, output a single integer - the most expensive cost for that capacity. ------ Constraints ------ 3 ≀ N ≀ 100000; 1 ≀ W ≀ 2, for each item; 1 ≀ C ≀ 10^{9}, for each item. ------ Example ------ Input: 5 1 1 2 2 2 3 2 4 2 5 Output: 1 5 6 9 10 12 13 14 15 ------ Explanations ------ In the test case, M equals to 9. For C = 1, it's optimal to choose {1} items; For C = 2, it's optimal to choose {5} items; For C = 3, it's optimal to choose {1, 5} items; For C = 4, it's optimal to choose {4, 5} items; For C = 5, it's optimal to choose {1, 4, 5} items; For C = 6, it's optimal to choose {3, 4, 5} items; For C = 7, it's optimal to choose {1, 3, 4, 5} items; For C = 8, it's optimal to choose {2, 3, 4, 5} items; For C = 9, it's optimal to choose {1, 2, 3, 4, 5} items.
n = int(input()) total = 0 w1 = [] w2 = [] for _ in range(n): w, c = map(int, input().split()) if w == 1: w1.append(c) else: w2.append(c) total += w w1.sort(reverse=True) w2.sort(reverse=True) ans = [0] * (total + 1) lw1 = len(w1) lw2 = len(w2) cur = 0 one, two = 0, 0 for i in range(2, total + 1, 2): if one + 1 < lw1 and two < lw2: if w1[one + 1] + w1[one] > w2[two]: cur += w1[one + 1] + w1[one] ans[i] = cur one += 2 else: cur += w2[two] ans[i] = cur two += 1 elif one < lw1 and two < lw2: if w1[one] > w2[two]: cur += w1[one] ans[i] = cur one += 1 else: cur += w2[two] ans[i] = cur two += 1 elif one + 1 < lw1: cur += w1[one + 1] + w1[one] ans[i] = cur one += 2 elif one < lw1: cur += w1[one] ans[i] = cur one += 1 elif two < lw2: cur += w2[two] ans[i] = cur two += 1 cur = 0 if lw1 > 0: cur = w1[0] ans[1] = cur one = 1 two = 0 for i in range(3, total + 1, 2): if one + 1 < lw1 and two < lw2: if w1[one + 1] + w1[one] > w2[two]: cur += w1[one + 1] + w1[one] ans[i] = cur one += 2 else: cur += w2[two] ans[i] = cur two += 1 elif one < lw1 and two < lw2: if w1[one] > w2[two]: cur += w1[one] ans[i] = cur one += 1 else: cur += w2[two] ans[i] = cur two += 1 elif one + 1 < lw1: cur += w1[one + 1] + w1[one] ans[i] = cur one += 2 elif one < lw1: cur += w1[one] ans[i] = cur one += 1 elif two < lw2: cur += w2[two] ans[i] = cur two += 1 print(*ans[1:])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
Read problems statements in Mandarin Chinese and Russian as well. Mike takes part in programming contests. His favourite topic is dynamic programming(DP). As he said, that he likes problems on DP, because "you spend a lot of time on thinking and a little time on coding". In this problem you are to solve a version of the knapsack problem(link), one of the most famous examples of DP problem. You are given N items, each has two parameters: a weight and a cost. Let's define M as the sum of the weights of all the items. Your task is to determine the most expensive cost of a knapsack, which capacity equals to 1, 2, ..., M. A cost of a knapsack equals to the sum of the costs of all the elements of the knapsack. Also, when you have a knapsack with a capacity is equal to C, then you can fill it with items, whose sum of weights is not greater than C. ------ Input ------ The first line of the input contains one integer N, denoting the number of the items. The next N lines contain two integers W and C each, denoting the weight and the cost of the corresponding item. ------ Output ------ For each capacity C (1 ≀ C ≀ M) of the knapsack, output a single integer - the most expensive cost for that capacity. ------ Constraints ------ 3 ≀ N ≀ 100000; 1 ≀ W ≀ 2, for each item; 1 ≀ C ≀ 10^{9}, for each item. ------ Example ------ Input: 5 1 1 2 2 2 3 2 4 2 5 Output: 1 5 6 9 10 12 13 14 15 ------ Explanations ------ In the test case, M equals to 9. For C = 1, it's optimal to choose {1} items; For C = 2, it's optimal to choose {5} items; For C = 3, it's optimal to choose {1, 5} items; For C = 4, it's optimal to choose {4, 5} items; For C = 5, it's optimal to choose {1, 4, 5} items; For C = 6, it's optimal to choose {3, 4, 5} items; For C = 7, it's optimal to choose {1, 3, 4, 5} items; For C = 8, it's optimal to choose {2, 3, 4, 5} items; For C = 9, it's optimal to choose {1, 2, 3, 4, 5} items.
n = int(input()) l = [] v = 0 o = [] t = [] for w in range(n): p, q = [int(e) for e in input().split()] l.append([q, p]) v += p if p == 1: o.append(q) else: t.append(q) o.sort(reverse=True) t.sort(reverse=True) i = 1 j = 0 k = 1 s = 0 if len(o) == 0: o.append(0) c = o[0] print(o[0], end=" ") while k < v: if j >= len(t) or i < len(o) and (len(o) == 1 or o[i] + o[i - 1] > t[j]): s = o[i] c += s i += 1 else: s = t[j] - o[i - 1] c += s j += 1 i -= 1 k += 1 print(max(c, c - s), end=" ") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER STRING WHILE VAR VAR IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR STRING EXPR FUNC_CALL VAR
Read problems statements in Mandarin Chinese and Russian as well. Mike takes part in programming contests. His favourite topic is dynamic programming(DP). As he said, that he likes problems on DP, because "you spend a lot of time on thinking and a little time on coding". In this problem you are to solve a version of the knapsack problem(link), one of the most famous examples of DP problem. You are given N items, each has two parameters: a weight and a cost. Let's define M as the sum of the weights of all the items. Your task is to determine the most expensive cost of a knapsack, which capacity equals to 1, 2, ..., M. A cost of a knapsack equals to the sum of the costs of all the elements of the knapsack. Also, when you have a knapsack with a capacity is equal to C, then you can fill it with items, whose sum of weights is not greater than C. ------ Input ------ The first line of the input contains one integer N, denoting the number of the items. The next N lines contain two integers W and C each, denoting the weight and the cost of the corresponding item. ------ Output ------ For each capacity C (1 ≀ C ≀ M) of the knapsack, output a single integer - the most expensive cost for that capacity. ------ Constraints ------ 3 ≀ N ≀ 100000; 1 ≀ W ≀ 2, for each item; 1 ≀ C ≀ 10^{9}, for each item. ------ Example ------ Input: 5 1 1 2 2 2 3 2 4 2 5 Output: 1 5 6 9 10 12 13 14 15 ------ Explanations ------ In the test case, M equals to 9. For C = 1, it's optimal to choose {1} items; For C = 2, it's optimal to choose {5} items; For C = 3, it's optimal to choose {1, 5} items; For C = 4, it's optimal to choose {4, 5} items; For C = 5, it's optimal to choose {1, 4, 5} items; For C = 6, it's optimal to choose {3, 4, 5} items; For C = 7, it's optimal to choose {1, 3, 4, 5} items; For C = 8, it's optimal to choose {2, 3, 4, 5} items; For C = 9, it's optimal to choose {1, 2, 3, 4, 5} items.
try: n = int(input()) w1 = [] w2 = [] for i in range(n): wi, ci = [int(x) for x in input().split()] if wi == 2: w2.append(ci) else: w1.append(ci) w1.sort() w2.sort() w1o = w1.copy() w2o = w2.copy() m = len(w2) * 2 + len(w1) curr = 0 ans = [(0) for i in range(m + 1)] for w in range(2, m + 1, 2): cost1 = 0 cost2 = 0 flag = 0 if len(w2) >= 1: cost1 = w2[-1] if len(w1) >= 2: cost2 = w1[-1] + w1[-2] flag = 2 elif len(w1) >= 1: cost2 = w1[-1] flag = 1 if cost1 > cost2: curr += cost1 ans[w] = curr w2.pop() else: curr += cost2 ans[w] = curr while flag > 0: w1.pop() flag -= 1 curr = 0 if len(w1o) >= 1: curr = w1o.pop() ans[1] = curr ans[1] = curr w1 = w1o w2 = w2o for w in range(3, m + 1, 2): cost1 = 0 cost2 = 0 flag = 0 if len(w2) >= 1: cost1 = w2[-1] if len(w1) >= 2: cost2 = w1[-1] + w1[-2] flag = 2 elif len(w1) >= 1: cost2 = w1[-1] flag = 1 if cost1 > cost2: curr += cost1 ans[w] = curr w2.pop() else: curr += cost2 ans[w] = curr while flag > 0: w1.pop() flag -= 1 print(" ".join([str(c) for c in ans[1:]])) except: pass
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR NUMBER
Read problems statements in Mandarin Chinese and Russian as well. Mike takes part in programming contests. His favourite topic is dynamic programming(DP). As he said, that he likes problems on DP, because "you spend a lot of time on thinking and a little time on coding". In this problem you are to solve a version of the knapsack problem(link), one of the most famous examples of DP problem. You are given N items, each has two parameters: a weight and a cost. Let's define M as the sum of the weights of all the items. Your task is to determine the most expensive cost of a knapsack, which capacity equals to 1, 2, ..., M. A cost of a knapsack equals to the sum of the costs of all the elements of the knapsack. Also, when you have a knapsack with a capacity is equal to C, then you can fill it with items, whose sum of weights is not greater than C. ------ Input ------ The first line of the input contains one integer N, denoting the number of the items. The next N lines contain two integers W and C each, denoting the weight and the cost of the corresponding item. ------ Output ------ For each capacity C (1 ≀ C ≀ M) of the knapsack, output a single integer - the most expensive cost for that capacity. ------ Constraints ------ 3 ≀ N ≀ 100000; 1 ≀ W ≀ 2, for each item; 1 ≀ C ≀ 10^{9}, for each item. ------ Example ------ Input: 5 1 1 2 2 2 3 2 4 2 5 Output: 1 5 6 9 10 12 13 14 15 ------ Explanations ------ In the test case, M equals to 9. For C = 1, it's optimal to choose {1} items; For C = 2, it's optimal to choose {5} items; For C = 3, it's optimal to choose {1, 5} items; For C = 4, it's optimal to choose {4, 5} items; For C = 5, it's optimal to choose {1, 4, 5} items; For C = 6, it's optimal to choose {3, 4, 5} items; For C = 7, it's optimal to choose {1, 3, 4, 5} items; For C = 8, it's optimal to choose {2, 3, 4, 5} items; For C = 9, it's optimal to choose {1, 2, 3, 4, 5} items.
try: n = int(input()) one = [] two = [] tw = 0 for _ in range(n): w, c = map(int, input().split()) tw += w if w == 1: one.append(c) else: two.append(c) one = sorted(one, reverse=True) two = sorted(two, reverse=True) onet = one.copy() twot = two.copy() dp = [0] * (tw + 1) cur = 0 for i in range(2, tw + 1, 2): cost = [0, 0, 0] if two: cost[0] = two[0] if len(one) > 1: cost[1] = one[0] + one[1] elif len(one) == 1: cost[2] = one[0] maxk = cost.index(max(cost)) if maxk == 0: two.pop(0) elif maxk == 1: one.pop(0) one.pop(0) else: one.pop(0) cur += cost[maxk] dp[i] = cur cur = 0 one = onet two = twot if len(one) >= 1: k = one.pop(0) dp[1] = k cur = k for i in range(3, tw + 1, 2): cost = [0, 0, 0] if two: cost[0] = two[0] if len(one) > 1: cost[1] = one[0] + one[1] elif len(one) == 1: cost[2] = one[0] maxk = cost.index(max(cost)) if maxk == 0: two.pop(0) elif maxk == 1: one.pop(0) one.pop(0) else: one.pop(0) cur += cost[maxk] dp[i] = cur print(*dp[1:]) except: pass
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER IF VAR ASSIGN VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER IF VAR ASSIGN VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
Read problems statements in Mandarin Chinese and Russian as well. Mike takes part in programming contests. His favourite topic is dynamic programming(DP). As he said, that he likes problems on DP, because "you spend a lot of time on thinking and a little time on coding". In this problem you are to solve a version of the knapsack problem(link), one of the most famous examples of DP problem. You are given N items, each has two parameters: a weight and a cost. Let's define M as the sum of the weights of all the items. Your task is to determine the most expensive cost of a knapsack, which capacity equals to 1, 2, ..., M. A cost of a knapsack equals to the sum of the costs of all the elements of the knapsack. Also, when you have a knapsack with a capacity is equal to C, then you can fill it with items, whose sum of weights is not greater than C. ------ Input ------ The first line of the input contains one integer N, denoting the number of the items. The next N lines contain two integers W and C each, denoting the weight and the cost of the corresponding item. ------ Output ------ For each capacity C (1 ≀ C ≀ M) of the knapsack, output a single integer - the most expensive cost for that capacity. ------ Constraints ------ 3 ≀ N ≀ 100000; 1 ≀ W ≀ 2, for each item; 1 ≀ C ≀ 10^{9}, for each item. ------ Example ------ Input: 5 1 1 2 2 2 3 2 4 2 5 Output: 1 5 6 9 10 12 13 14 15 ------ Explanations ------ In the test case, M equals to 9. For C = 1, it's optimal to choose {1} items; For C = 2, it's optimal to choose {5} items; For C = 3, it's optimal to choose {1, 5} items; For C = 4, it's optimal to choose {4, 5} items; For C = 5, it's optimal to choose {1, 4, 5} items; For C = 6, it's optimal to choose {3, 4, 5} items; For C = 7, it's optimal to choose {1, 3, 4, 5} items; For C = 8, it's optimal to choose {2, 3, 4, 5} items; For C = 9, it's optimal to choose {1, 2, 3, 4, 5} items.
n = int(input()) m = 0 item1 = [] item2 = [] for i in range(n): w, c = map(int, input().split()) m += w if w == 1: item1.append(c) if w == 2: item2.append(c) item1 = sorted(item1, reverse=True) item2 = sorted(item2, reverse=True) ans = [0] * (m + 1) curr = 0 l1 = len(item1) l2 = len(item2) one, two = 0, 0 for i in range(2, m + 1, 2): if one + 1 < l1 and two < l2: if item1[one + 1] + item1[one] > item2[two]: curr += item1[one] + item1[one + 1] ans[i] = curr one += 2 else: curr += item2[two] two += 1 ans[i] = curr elif one < l1 and two < l2: if item1[one] > item2[two]: curr += item1[one] one += 1 ans[i] = curr else: curr += item2[two] two += 1 ans[i] = curr elif one + 1 < l1: curr += item1[one] + item1[one + 1] one += 2 ans[i] = curr elif one < l1: curr += item1[one] one += 1 ans[i] = curr elif two < l2: curr += item2[two] two += 1 ans[i] = curr curr = 0 if l1 > 0: ans[1] = item1[0] curr = item1[0] one, two = 1, 0 for i in range(3, m + 1, 2): if one + 1 < l1 and two < l2: if item1[one + 1] + item1[one] > item2[two]: curr += item1[one] + item1[one + 1] ans[i] = curr one += 2 else: curr += item2[two] two += 1 ans[i] = curr elif one < l1 and two < l2: if item1[one] > item2[two]: curr += item1[one] one += 1 ans[i] = curr else: curr += item2[two] two += 1 ans[i] = curr elif one + 1 < l1: curr += item1[one] + item1[one + 1] one += 2 ans[i] = curr elif one < l1: curr += item1[one] one += 1 ans[i] = curr elif two < l2: curr += item2[two] two += 1 ans[i] = curr print(*ans[1:])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
Read problems statements in Mandarin Chinese and Russian as well. Mike takes part in programming contests. His favourite topic is dynamic programming(DP). As he said, that he likes problems on DP, because "you spend a lot of time on thinking and a little time on coding". In this problem you are to solve a version of the knapsack problem(link), one of the most famous examples of DP problem. You are given N items, each has two parameters: a weight and a cost. Let's define M as the sum of the weights of all the items. Your task is to determine the most expensive cost of a knapsack, which capacity equals to 1, 2, ..., M. A cost of a knapsack equals to the sum of the costs of all the elements of the knapsack. Also, when you have a knapsack with a capacity is equal to C, then you can fill it with items, whose sum of weights is not greater than C. ------ Input ------ The first line of the input contains one integer N, denoting the number of the items. The next N lines contain two integers W and C each, denoting the weight and the cost of the corresponding item. ------ Output ------ For each capacity C (1 ≀ C ≀ M) of the knapsack, output a single integer - the most expensive cost for that capacity. ------ Constraints ------ 3 ≀ N ≀ 100000; 1 ≀ W ≀ 2, for each item; 1 ≀ C ≀ 10^{9}, for each item. ------ Example ------ Input: 5 1 1 2 2 2 3 2 4 2 5 Output: 1 5 6 9 10 12 13 14 15 ------ Explanations ------ In the test case, M equals to 9. For C = 1, it's optimal to choose {1} items; For C = 2, it's optimal to choose {5} items; For C = 3, it's optimal to choose {1, 5} items; For C = 4, it's optimal to choose {4, 5} items; For C = 5, it's optimal to choose {1, 4, 5} items; For C = 6, it's optimal to choose {3, 4, 5} items; For C = 7, it's optimal to choose {1, 3, 4, 5} items; For C = 8, it's optimal to choose {2, 3, 4, 5} items; For C = 9, it's optimal to choose {1, 2, 3, 4, 5} items.
n = int(input()) cost = [[], []] for i in range(n): w, c = map(int, input().split()) cost[w - 1].append(c) cost[0].sort() cost[1].sort() l1 = len(cost[0]) l2 = len(cost[1]) m = l1 + 2 * l2 mp1 = [(0) for i in range(m + 1)] mp2 = [(0) for i in range(m + 1)] mp1[0] = l1 mp2[0] = l2 dp = [(0) for i in range(m + 1)] if l1: dp[1] = cost[0][-1] mp1[1] = l1 - 1 mp2[1] = l2 for i in range(2, m + 1): sum1, sum2 = dp[i - 1], dp[i - 2] if mp1[i - 1]: x1 = mp1[i - 1] - 1 sum1 = dp[i - 1] + cost[0][x1] if mp2[i - 2]: x2 = mp2[i - 2] - 1 sum2 = dp[i - 2] + cost[1][x2] if sum1 > sum2: mp1[i] = x1 mp2[i] = mp2[i - 1] dp[i] = sum1 if sum1 <= sum2: mp2[i] = x2 mp1[i] = mp1[i - 2] dp[i] = sum2 print(*dp[1:])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
Read problems statements in Mandarin Chinese and Russian as well. Mike takes part in programming contests. His favourite topic is dynamic programming(DP). As he said, that he likes problems on DP, because "you spend a lot of time on thinking and a little time on coding". In this problem you are to solve a version of the knapsack problem(link), one of the most famous examples of DP problem. You are given N items, each has two parameters: a weight and a cost. Let's define M as the sum of the weights of all the items. Your task is to determine the most expensive cost of a knapsack, which capacity equals to 1, 2, ..., M. A cost of a knapsack equals to the sum of the costs of all the elements of the knapsack. Also, when you have a knapsack with a capacity is equal to C, then you can fill it with items, whose sum of weights is not greater than C. ------ Input ------ The first line of the input contains one integer N, denoting the number of the items. The next N lines contain two integers W and C each, denoting the weight and the cost of the corresponding item. ------ Output ------ For each capacity C (1 ≀ C ≀ M) of the knapsack, output a single integer - the most expensive cost for that capacity. ------ Constraints ------ 3 ≀ N ≀ 100000; 1 ≀ W ≀ 2, for each item; 1 ≀ C ≀ 10^{9}, for each item. ------ Example ------ Input: 5 1 1 2 2 2 3 2 4 2 5 Output: 1 5 6 9 10 12 13 14 15 ------ Explanations ------ In the test case, M equals to 9. For C = 1, it's optimal to choose {1} items; For C = 2, it's optimal to choose {5} items; For C = 3, it's optimal to choose {1, 5} items; For C = 4, it's optimal to choose {4, 5} items; For C = 5, it's optimal to choose {1, 4, 5} items; For C = 6, it's optimal to choose {3, 4, 5} items; For C = 7, it's optimal to choose {1, 3, 4, 5} items; For C = 8, it's optimal to choose {2, 3, 4, 5} items; For C = 9, it's optimal to choose {1, 2, 3, 4, 5} items.
num_items = int(input()) costs_weight1 = [] costs_weight2 = [] total_weight = 0 for i in range(num_items): weight, cost = map(int, input().split()) total_weight += weight if weight == 1: costs_weight1.append(cost) else: costs_weight2.append(cost) costs_weight1.sort(reverse=True) costs_weight2.sort(reverse=True) max_costs = [0] * (total_weight + 1) weight1_index, weight2_index = 0, 0 for i in range(2, len(max_costs), 2): cost1 = 0 cost2 = 0 if weight1_index < len(costs_weight1): cost1 += costs_weight1[weight1_index] if weight1_index + 1 < len(costs_weight1): cost1 += costs_weight1[weight1_index + 1] if weight2_index < len(costs_weight2): cost2 += costs_weight2[weight2_index] if cost1 > cost2: max_costs[i] = max_costs[i - 2] + cost1 weight1_index += 2 else: max_costs[i] = max_costs[i - 2] + cost2 weight2_index += 1 if len(costs_weight1) != 0: max_costs[1] = costs_weight1[0] weight1_index, weight2_index = 1, 0 for i in range(3, len(max_costs), 2): cost1 = 0 cost2 = 0 if weight1_index < len(costs_weight1): cost1 += costs_weight1[weight1_index] if weight1_index + 1 < len(costs_weight1): cost1 += costs_weight1[weight1_index + 1] if weight2_index < len(costs_weight2): cost2 += costs_weight2[weight2_index] if cost1 > cost2: max_costs[i] = max_costs[i - 2] + cost1 weight1_index += 2 else: max_costs[i] = max_costs[i - 2] + cost2 weight2_index += 1 print(" ".join(map(str, max_costs[1:])))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR NUMBER
Read problems statements in Mandarin Chinese and Russian as well. Mike takes part in programming contests. His favourite topic is dynamic programming(DP). As he said, that he likes problems on DP, because "you spend a lot of time on thinking and a little time on coding". In this problem you are to solve a version of the knapsack problem(link), one of the most famous examples of DP problem. You are given N items, each has two parameters: a weight and a cost. Let's define M as the sum of the weights of all the items. Your task is to determine the most expensive cost of a knapsack, which capacity equals to 1, 2, ..., M. A cost of a knapsack equals to the sum of the costs of all the elements of the knapsack. Also, when you have a knapsack with a capacity is equal to C, then you can fill it with items, whose sum of weights is not greater than C. ------ Input ------ The first line of the input contains one integer N, denoting the number of the items. The next N lines contain two integers W and C each, denoting the weight and the cost of the corresponding item. ------ Output ------ For each capacity C (1 ≀ C ≀ M) of the knapsack, output a single integer - the most expensive cost for that capacity. ------ Constraints ------ 3 ≀ N ≀ 100000; 1 ≀ W ≀ 2, for each item; 1 ≀ C ≀ 10^{9}, for each item. ------ Example ------ Input: 5 1 1 2 2 2 3 2 4 2 5 Output: 1 5 6 9 10 12 13 14 15 ------ Explanations ------ In the test case, M equals to 9. For C = 1, it's optimal to choose {1} items; For C = 2, it's optimal to choose {5} items; For C = 3, it's optimal to choose {1, 5} items; For C = 4, it's optimal to choose {4, 5} items; For C = 5, it's optimal to choose {1, 4, 5} items; For C = 6, it's optimal to choose {3, 4, 5} items; For C = 7, it's optimal to choose {1, 3, 4, 5} items; For C = 8, it's optimal to choose {2, 3, 4, 5} items; For C = 9, it's optimal to choose {1, 2, 3, 4, 5} items.
n = int(input()) one, two = [], [] W = 0 for _ in range(n): w, c = map(int, input().split()) if w == 1: one.append(c) else: two.append(c) W += w e_one = sorted(one) e_two = sorted(two) ans = [0] * W temp = 0 for w in range(2, W + 1, 2): cost1, cost2 = 0, 0 if len(e_one) >= 2: cost1 = e_one[-1] + e_one[-2] elif len(e_one) >= 1: cost1 = e_one[-1] if len(e_two) >= 1: cost2 = e_two[-1] if cost1 >= cost2: if len(e_one) >= 2: e_one.pop() e_one.pop() elif len(e_one) >= 1: e_one.pop() temp += cost1 else: e_two.pop() temp += cost2 ans[w - 1] = temp o_one = sorted(one) o_two = sorted(two) temp = 0 if len(o_one) >= 1: ans[0] = o_one.pop() temp = ans[0] for w in range(3, W + 1, 2): cost1, cost2 = 0, 0 if len(o_one) >= 2: cost1 = o_one[-1] + o_one[-2] elif len(o_one) >= 1: cost1 = o_one[-1] if len(o_two) >= 1: cost2 = o_two[-1] if cost1 >= cost2: if len(o_one) >= 2: o_one.pop() o_one.pop() elif len(o_one) >= 1: o_one.pop() temp += cost1 else: o_two.pop() temp += cost2 ans[w - 1] = temp print(*ans, sep=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR STRING
Read problems statements in Mandarin Chinese and Russian as well. Mike takes part in programming contests. His favourite topic is dynamic programming(DP). As he said, that he likes problems on DP, because "you spend a lot of time on thinking and a little time on coding". In this problem you are to solve a version of the knapsack problem(link), one of the most famous examples of DP problem. You are given N items, each has two parameters: a weight and a cost. Let's define M as the sum of the weights of all the items. Your task is to determine the most expensive cost of a knapsack, which capacity equals to 1, 2, ..., M. A cost of a knapsack equals to the sum of the costs of all the elements of the knapsack. Also, when you have a knapsack with a capacity is equal to C, then you can fill it with items, whose sum of weights is not greater than C. ------ Input ------ The first line of the input contains one integer N, denoting the number of the items. The next N lines contain two integers W and C each, denoting the weight and the cost of the corresponding item. ------ Output ------ For each capacity C (1 ≀ C ≀ M) of the knapsack, output a single integer - the most expensive cost for that capacity. ------ Constraints ------ 3 ≀ N ≀ 100000; 1 ≀ W ≀ 2, for each item; 1 ≀ C ≀ 10^{9}, for each item. ------ Example ------ Input: 5 1 1 2 2 2 3 2 4 2 5 Output: 1 5 6 9 10 12 13 14 15 ------ Explanations ------ In the test case, M equals to 9. For C = 1, it's optimal to choose {1} items; For C = 2, it's optimal to choose {5} items; For C = 3, it's optimal to choose {1, 5} items; For C = 4, it's optimal to choose {4, 5} items; For C = 5, it's optimal to choose {1, 4, 5} items; For C = 6, it's optimal to choose {3, 4, 5} items; For C = 7, it's optimal to choose {1, 3, 4, 5} items; For C = 8, it's optimal to choose {2, 3, 4, 5} items; For C = 9, it's optimal to choose {1, 2, 3, 4, 5} items.
from sys import stdin n = int(stdin.readline()) list1 = [] weight = 0 list_one = [] list_two = [] for i in range(n): w, c = map(int, stdin.readline().split()) if w == 1: list_one.append(c) else: list_two.append(c) weight += w list1 = [0] * weight list_one.sort(reverse=True) list_two.sort(reverse=True) list_one_even = list_one[:] list_two_even = list_two[:] curr = 0 for i in range(2, weight + 1, 2): if list_two_even != [] and len(list_one_even) >= 1: if ( len(list_one_even) > 1 and list_two_even[0] >= list_one_even[0] + list_one_even[1] ): cost = list_two_even.pop(0) curr += cost list1[i - 1] = curr elif len(list_one_even) == 1 and list_two_even[0] >= list_one_even[0]: cost = list_two_even.pop(0) curr += cost list1[i - 1] = curr elif len(list_one_even) > 1: cost1 = list_one_even.pop(0) cost2 = list_one_even.pop(0) curr += cost1 + cost2 list1[i - 1] = curr else: cost = list_one_even.pop(0) curr += cost list1[i - 1] = curr elif list_two_even == []: if len(list_one_even) > 1: cost1 = list_one_even.pop(0) cost2 = list_one_even.pop(0) curr += cost1 + cost2 list1[i - 1] = curr else: cost = list_one_even.pop(0) curr += cost list1[i - 1] = curr elif list_one_even == []: if len(list_two_even) > 0: cost = list_two_even.pop(0) curr += cost list1[i - 1] = curr if len(list_one) > 0: list1[0] = list_one.pop(0) curr = list1[0] else: curr = 0 for i in range(3, weight + 1, 2): if list_two != [] and len(list_one) >= 1: if len(list_one) > 1 and list_two[0] >= list_one[0] + list_one[1]: cost = list_two.pop(0) curr += cost list1[i - 1] = curr elif len(list_one) == 1 and list_two[0] >= list_one[0]: cost = list_two.pop(0) curr += cost list1[i - 1] = curr elif len(list_one) > 1: cost1 = list_one.pop(0) cost2 = list_one.pop(0) curr += cost1 + cost2 list1[i - 1] = curr else: cost = list_one.pop(0) curr += cost list1[i - 1] = curr elif list_two == []: if len(list_one) > 1: cost1 = list_one.pop(0) cost2 = list_one.pop(0) curr += cost1 + cost2 list1[i - 1] = curr else: cost = list_one.pop(0) curr += cost list1[i - 1] = curr elif list_one == []: if len(list_two) > 0: cost = list_two.pop(0) curr += cost list1[i - 1] = curr print(" ".join(map(str, list1)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR LIST FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR LIST IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR LIST IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR LIST FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR LIST IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR LIST IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Read problems statements in Mandarin Chinese and Russian as well. Mike takes part in programming contests. His favourite topic is dynamic programming(DP). As he said, that he likes problems on DP, because "you spend a lot of time on thinking and a little time on coding". In this problem you are to solve a version of the knapsack problem(link), one of the most famous examples of DP problem. You are given N items, each has two parameters: a weight and a cost. Let's define M as the sum of the weights of all the items. Your task is to determine the most expensive cost of a knapsack, which capacity equals to 1, 2, ..., M. A cost of a knapsack equals to the sum of the costs of all the elements of the knapsack. Also, when you have a knapsack with a capacity is equal to C, then you can fill it with items, whose sum of weights is not greater than C. ------ Input ------ The first line of the input contains one integer N, denoting the number of the items. The next N lines contain two integers W and C each, denoting the weight and the cost of the corresponding item. ------ Output ------ For each capacity C (1 ≀ C ≀ M) of the knapsack, output a single integer - the most expensive cost for that capacity. ------ Constraints ------ 3 ≀ N ≀ 100000; 1 ≀ W ≀ 2, for each item; 1 ≀ C ≀ 10^{9}, for each item. ------ Example ------ Input: 5 1 1 2 2 2 3 2 4 2 5 Output: 1 5 6 9 10 12 13 14 15 ------ Explanations ------ In the test case, M equals to 9. For C = 1, it's optimal to choose {1} items; For C = 2, it's optimal to choose {5} items; For C = 3, it's optimal to choose {1, 5} items; For C = 4, it's optimal to choose {4, 5} items; For C = 5, it's optimal to choose {1, 4, 5} items; For C = 6, it's optimal to choose {3, 4, 5} items; For C = 7, it's optimal to choose {1, 3, 4, 5} items; For C = 8, it's optimal to choose {2, 3, 4, 5} items; For C = 9, it's optimal to choose {1, 2, 3, 4, 5} items.
memo = dict() n = int(input()) s, l1, l2 = 0, [], [] for i in range(n): w, p = map(int, input().split()) if w == 1: l1.append(p) else: l2.append(p) s += w l1 = sorted(l1) l2 = sorted(l2) l1 = l1[::-1] l2 = l2[::-1] dupl1 = l1[:] dupl2 = l2[:] memo[0] = 0 for i in range(2, s + 1, 2): max2, max1 = 0, 0 if len(l2) > 0: max2 = l2[0] k = True if len(l1) > 1: max1 = l1[0] + l1[1] elif len(l1) == 1: max1 = l1[0] k = False if max2 > max1: memo[i] = memo[i - 2] + max2 l2.pop(0) else: memo[i] = memo[i - 2] + max1 if k: l1.pop(0) l1.pop(0) else: l1.pop(0) if len(dupl1) > 0: memo[1] = dupl1[0] dupl1.pop(0) else: memo[1] = 0 for i in range(3, s + 1, 2): max2, max1 = 0, 0 if len(dupl2) > 0: max2 = dupl2[0] k = True if len(dupl1) > 1: max1 = dupl1[0] + dupl1[1] elif len(dupl1) == 1: max1 = dupl1[0] k = False if max2 > max1: memo[i] = memo[i - 2] + max2 dupl2.pop(0) else: memo[i] = memo[i - 2] + max1 if k: dupl1.pop(0) dupl1.pop(0) else: dupl1.pop(0) for i in range(1, s + 1): print(memo[i], end=" ")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING
Read problems statements in Mandarin Chinese and Russian as well. Mike takes part in programming contests. His favourite topic is dynamic programming(DP). As he said, that he likes problems on DP, because "you spend a lot of time on thinking and a little time on coding". In this problem you are to solve a version of the knapsack problem(link), one of the most famous examples of DP problem. You are given N items, each has two parameters: a weight and a cost. Let's define M as the sum of the weights of all the items. Your task is to determine the most expensive cost of a knapsack, which capacity equals to 1, 2, ..., M. A cost of a knapsack equals to the sum of the costs of all the elements of the knapsack. Also, when you have a knapsack with a capacity is equal to C, then you can fill it with items, whose sum of weights is not greater than C. ------ Input ------ The first line of the input contains one integer N, denoting the number of the items. The next N lines contain two integers W and C each, denoting the weight and the cost of the corresponding item. ------ Output ------ For each capacity C (1 ≀ C ≀ M) of the knapsack, output a single integer - the most expensive cost for that capacity. ------ Constraints ------ 3 ≀ N ≀ 100000; 1 ≀ W ≀ 2, for each item; 1 ≀ C ≀ 10^{9}, for each item. ------ Example ------ Input: 5 1 1 2 2 2 3 2 4 2 5 Output: 1 5 6 9 10 12 13 14 15 ------ Explanations ------ In the test case, M equals to 9. For C = 1, it's optimal to choose {1} items; For C = 2, it's optimal to choose {5} items; For C = 3, it's optimal to choose {1, 5} items; For C = 4, it's optimal to choose {4, 5} items; For C = 5, it's optimal to choose {1, 4, 5} items; For C = 6, it's optimal to choose {3, 4, 5} items; For C = 7, it's optimal to choose {1, 3, 4, 5} items; For C = 8, it's optimal to choose {2, 3, 4, 5} items; For C = 9, it's optimal to choose {1, 2, 3, 4, 5} items.
def solve(oneWP, twoWP, C): newOneWP = sorted(oneWP, reverse=True) newTwoWP = sorted(twoWP, reverse=True) dp = {} used = {} for i in range(C): dp[i] = dp.get(i, 0) used[i] = 0, 0 if len(newOneWP) > 0: dp[0] = newOneWP[0] used[0] = 1, 0 cost2 = 0 cost1 = 0 cost3 = dp[0] if len(newTwoWP) > 0: cost2 = newTwoWP[0] if len(newOneWP) >= 2: cost1 = dp[0] + newOneWP[1] maxCost = max(cost1, cost2, cost3) if maxCost == cost1: dp[1] = cost1 used[1] = 2, 0 elif maxCost == cost2: dp[1] = cost2 used[1] = 0, 1 else: dp[1] = cost3 used[1] = 1, 0 for i in range(2, C): cost1 = 0 cost2 = 0 if len(newOneWP) > used[i - 1][0]: cost1 = dp[i - 1] + newOneWP[used[i - 1][0]] if len(newTwoWP) > used[i - 2][1]: cost2 = dp[i - 2] + newTwoWP[used[i - 2][1]] cost3 = dp[i - 1] dp[i] = max(cost1, cost2, cost3) if dp[i] == cost1: used[i] = used[i - 1][0] + 1, used[i - 1][1] elif dp[i] == cost2: used[i] = used[i - 2][0], used[i - 2][1] + 1 else: used[i] = used[i - 1] for i in dp: print(dp[i], end=" ") n = int(input()) weights = [] twoWP = [] oneWP = [] for i in range(n): w, c = map(int, input().split()) weights.append(w) if w == 1: oneWP.append(c) else: twoWP.append(c) solve(oneWP, twoWP, sum(weights))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST 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 IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian as well. Mike takes part in programming contests. His favourite topic is dynamic programming(DP). As he said, that he likes problems on DP, because "you spend a lot of time on thinking and a little time on coding". In this problem you are to solve a version of the knapsack problem(link), one of the most famous examples of DP problem. You are given N items, each has two parameters: a weight and a cost. Let's define M as the sum of the weights of all the items. Your task is to determine the most expensive cost of a knapsack, which capacity equals to 1, 2, ..., M. A cost of a knapsack equals to the sum of the costs of all the elements of the knapsack. Also, when you have a knapsack with a capacity is equal to C, then you can fill it with items, whose sum of weights is not greater than C. ------ Input ------ The first line of the input contains one integer N, denoting the number of the items. The next N lines contain two integers W and C each, denoting the weight and the cost of the corresponding item. ------ Output ------ For each capacity C (1 ≀ C ≀ M) of the knapsack, output a single integer - the most expensive cost for that capacity. ------ Constraints ------ 3 ≀ N ≀ 100000; 1 ≀ W ≀ 2, for each item; 1 ≀ C ≀ 10^{9}, for each item. ------ Example ------ Input: 5 1 1 2 2 2 3 2 4 2 5 Output: 1 5 6 9 10 12 13 14 15 ------ Explanations ------ In the test case, M equals to 9. For C = 1, it's optimal to choose {1} items; For C = 2, it's optimal to choose {5} items; For C = 3, it's optimal to choose {1, 5} items; For C = 4, it's optimal to choose {4, 5} items; For C = 5, it's optimal to choose {1, 4, 5} items; For C = 6, it's optimal to choose {3, 4, 5} items; For C = 7, it's optimal to choose {1, 3, 4, 5} items; For C = 8, it's optimal to choose {2, 3, 4, 5} items; For C = 9, it's optimal to choose {1, 2, 3, 4, 5} items.
def answer(): n = len(a) + 2 * len(b) dp = [[0, 0, 0] for i in range(n + 1)] if len(a): dp[1] = [a[-1], 1, 0] for i in range(2, n + 1): x1 = dp[i - 1][0] if dp[i - 1][1] < len(a): x1 += a[-dp[i - 1][1] - 1] x2 = dp[i - 2][0] if dp[i - 2][2] < len(b): x2 += b[-dp[i - 2][2] - 1] if x1 > x2: dp[i][0] = x1 dp[i][1] = dp[i - 1][1] + 1 dp[i][2] = dp[i - 1][2] else: dp[i][0] = x2 dp[i][1] = dp[i - 2][1] dp[i][2] = dp[i - 2][2] + 1 for i in range(1, n + 1): print(dp[i][0], end=" ") n = int(input()) a, b = [], [] for i in range(n): w, c = map(int, input().split()) if w == 1: a.append(c) else: b.append(c) a.sort() b.sort() answer()
FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR NUMBER LIST VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Read problems statements in Mandarin Chinese and Russian as well. Mike takes part in programming contests. His favourite topic is dynamic programming(DP). As he said, that he likes problems on DP, because "you spend a lot of time on thinking and a little time on coding". In this problem you are to solve a version of the knapsack problem(link), one of the most famous examples of DP problem. You are given N items, each has two parameters: a weight and a cost. Let's define M as the sum of the weights of all the items. Your task is to determine the most expensive cost of a knapsack, which capacity equals to 1, 2, ..., M. A cost of a knapsack equals to the sum of the costs of all the elements of the knapsack. Also, when you have a knapsack with a capacity is equal to C, then you can fill it with items, whose sum of weights is not greater than C. ------ Input ------ The first line of the input contains one integer N, denoting the number of the items. The next N lines contain two integers W and C each, denoting the weight and the cost of the corresponding item. ------ Output ------ For each capacity C (1 ≀ C ≀ M) of the knapsack, output a single integer - the most expensive cost for that capacity. ------ Constraints ------ 3 ≀ N ≀ 100000; 1 ≀ W ≀ 2, for each item; 1 ≀ C ≀ 10^{9}, for each item. ------ Example ------ Input: 5 1 1 2 2 2 3 2 4 2 5 Output: 1 5 6 9 10 12 13 14 15 ------ Explanations ------ In the test case, M equals to 9. For C = 1, it's optimal to choose {1} items; For C = 2, it's optimal to choose {5} items; For C = 3, it's optimal to choose {1, 5} items; For C = 4, it's optimal to choose {4, 5} items; For C = 5, it's optimal to choose {1, 4, 5} items; For C = 6, it's optimal to choose {3, 4, 5} items; For C = 7, it's optimal to choose {1, 3, 4, 5} items; For C = 8, it's optimal to choose {2, 3, 4, 5} items; For C = 9, it's optimal to choose {1, 2, 3, 4, 5} items.
n, a, b, MaxW, MaxC = int(input()), list(), list(), 0, 0 for _ in range(n): w, c = map(int, input().split()) a.append(c) if w == 1 else b.append(c) MaxW, MaxC = MaxW + w, MaxC + c a, b, c, x, y = ( sorted(a), sorted(b), [0] * (MaxW + 1), [0] * (MaxW + 1), [0] * (MaxW + 1), ) c[MaxW] = MaxC for i in range(MaxW, 1, -1): if y[i] < len(b) and c[i] - b[y[i]] > c[i - 1]: c[i - 1], x[i - 1], y[i - 1] = c[i] - b[y[i]], x[i], y[i] + 1 if y[i] < len(b) and c[i] - b[y[i]] > c[i - 2]: c[i - 2], x[i - 2], y[i - 2] = c[i] - b[y[i]], x[i], y[i] + 1 if x[i] < len(a) and c[i] - a[x[i]] > c[i - 1]: c[i - 1], x[i - 1], y[i - 1] = c[i] - a[x[i]], x[i] + 1, y[i] for i in range(1, MaxW + 1): print(c[i], end=" ")
ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING
Read problems statements in Mandarin Chinese and Russian as well. Mike takes part in programming contests. His favourite topic is dynamic programming(DP). As he said, that he likes problems on DP, because "you spend a lot of time on thinking and a little time on coding". In this problem you are to solve a version of the knapsack problem(link), one of the most famous examples of DP problem. You are given N items, each has two parameters: a weight and a cost. Let's define M as the sum of the weights of all the items. Your task is to determine the most expensive cost of a knapsack, which capacity equals to 1, 2, ..., M. A cost of a knapsack equals to the sum of the costs of all the elements of the knapsack. Also, when you have a knapsack with a capacity is equal to C, then you can fill it with items, whose sum of weights is not greater than C. ------ Input ------ The first line of the input contains one integer N, denoting the number of the items. The next N lines contain two integers W and C each, denoting the weight and the cost of the corresponding item. ------ Output ------ For each capacity C (1 ≀ C ≀ M) of the knapsack, output a single integer - the most expensive cost for that capacity. ------ Constraints ------ 3 ≀ N ≀ 100000; 1 ≀ W ≀ 2, for each item; 1 ≀ C ≀ 10^{9}, for each item. ------ Example ------ Input: 5 1 1 2 2 2 3 2 4 2 5 Output: 1 5 6 9 10 12 13 14 15 ------ Explanations ------ In the test case, M equals to 9. For C = 1, it's optimal to choose {1} items; For C = 2, it's optimal to choose {5} items; For C = 3, it's optimal to choose {1, 5} items; For C = 4, it's optimal to choose {4, 5} items; For C = 5, it's optimal to choose {1, 4, 5} items; For C = 6, it's optimal to choose {3, 4, 5} items; For C = 7, it's optimal to choose {1, 3, 4, 5} items; For C = 8, it's optimal to choose {2, 3, 4, 5} items; For C = 9, it's optimal to choose {1, 2, 3, 4, 5} items.
n = int(input().strip()) values_of_weight = {(1): [], (2): []} m = 0 for i in range(n): weight, value = map(int, input().strip().split()) values_of_weight[weight].append(value) m += weight values_of_weight[1].sort() values_of_weight[2].sort() value_dp = [(0) for i in range(m + 1)] idx_1 = len(values_of_weight[1]) - 1 idx_2 = len(values_of_weight[2]) - 1 for capacity in range(2, m + 1, 2): if idx_1 >= 0 and idx_2 >= 0: if idx_1 == 0: if values_of_weight[1][idx_1] > values_of_weight[2][idx_2]: value_dp[capacity] = value_dp[capacity - 2] + values_of_weight[1][idx_1] idx_1 -= 1 else: value_dp[capacity] = value_dp[capacity - 2] + values_of_weight[2][idx_2] idx_2 -= 1 elif ( values_of_weight[1][idx_1] + values_of_weight[1][idx_1 - 1] > values_of_weight[2][idx_2] ): value_dp[capacity] = ( value_dp[capacity - 2] + values_of_weight[1][idx_1] + values_of_weight[1][idx_1 - 1] ) idx_1 -= 2 else: value_dp[capacity] = value_dp[capacity - 2] + values_of_weight[2][idx_2] idx_2 -= 1 elif idx_2 >= 0: value_dp[capacity] = value_dp[capacity - 2] + values_of_weight[2][idx_2] idx_2 -= 1 elif idx_1 >= 0: if idx_1 == 0: value_dp[capacity] = value_dp[capacity - 2] + values_of_weight[1][idx_1] idx_1 -= 1 else: value_dp[capacity] = ( value_dp[capacity - 2] + values_of_weight[1][idx_1] + values_of_weight[1][idx_1 - 1] ) idx_1 -= 2 if len(values_of_weight[1]) >= 1: value_dp[1] = values_of_weight[1].pop() idx_1 = len(values_of_weight[1]) - 1 idx_2 = len(values_of_weight[2]) - 1 for capacity in range(3, m + 1, 2): if idx_1 >= 0 and idx_2 >= 0: if idx_1 == 0: if values_of_weight[1][idx_1] > values_of_weight[2][idx_2]: value_dp[capacity] = value_dp[capacity - 2] + values_of_weight[1][idx_1] idx_1 -= 1 else: value_dp[capacity] = value_dp[capacity - 2] + values_of_weight[2][idx_2] idx_2 -= 1 elif ( values_of_weight[1][idx_1] + values_of_weight[1][idx_1 - 1] > values_of_weight[2][idx_2] ): value_dp[capacity] = ( value_dp[capacity - 2] + values_of_weight[1][idx_1] + values_of_weight[1][idx_1 - 1] ) idx_1 -= 2 else: value_dp[capacity] = value_dp[capacity - 2] + values_of_weight[2][idx_2] idx_2 -= 1 elif idx_2 >= 0: value_dp[capacity] = value_dp[capacity - 2] + values_of_weight[2][idx_2] idx_2 -= 1 elif idx_1 >= 0: if idx_1 == 0: value_dp[capacity] = value_dp[capacity - 2] + values_of_weight[1][idx_1] idx_1 -= 1 else: value_dp[capacity] = ( value_dp[capacity - 2] + values_of_weight[1][idx_1] + values_of_weight[1][idx_1 - 1] ) idx_1 -= 2 for idx in range(2, capacity + 1): if value_dp[idx] == 0: value_dp[idx] = value_dp[idx_1] print(*value_dp[1:], sep=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER LIST LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER STRING
Read problems statements in Mandarin Chinese and Russian as well. Mike takes part in programming contests. His favourite topic is dynamic programming(DP). As he said, that he likes problems on DP, because "you spend a lot of time on thinking and a little time on coding". In this problem you are to solve a version of the knapsack problem(link), one of the most famous examples of DP problem. You are given N items, each has two parameters: a weight and a cost. Let's define M as the sum of the weights of all the items. Your task is to determine the most expensive cost of a knapsack, which capacity equals to 1, 2, ..., M. A cost of a knapsack equals to the sum of the costs of all the elements of the knapsack. Also, when you have a knapsack with a capacity is equal to C, then you can fill it with items, whose sum of weights is not greater than C. ------ Input ------ The first line of the input contains one integer N, denoting the number of the items. The next N lines contain two integers W and C each, denoting the weight and the cost of the corresponding item. ------ Output ------ For each capacity C (1 ≀ C ≀ M) of the knapsack, output a single integer - the most expensive cost for that capacity. ------ Constraints ------ 3 ≀ N ≀ 100000; 1 ≀ W ≀ 2, for each item; 1 ≀ C ≀ 10^{9}, for each item. ------ Example ------ Input: 5 1 1 2 2 2 3 2 4 2 5 Output: 1 5 6 9 10 12 13 14 15 ------ Explanations ------ In the test case, M equals to 9. For C = 1, it's optimal to choose {1} items; For C = 2, it's optimal to choose {5} items; For C = 3, it's optimal to choose {1, 5} items; For C = 4, it's optimal to choose {4, 5} items; For C = 5, it's optimal to choose {1, 4, 5} items; For C = 6, it's optimal to choose {3, 4, 5} items; For C = 7, it's optimal to choose {1, 3, 4, 5} items; For C = 8, it's optimal to choose {2, 3, 4, 5} items; For C = 9, it's optimal to choose {1, 2, 3, 4, 5} items.
x = [] y = [] m = 0 for _ in range(int(input())): w, c = map(int, input().split()) m += w if w == 1: x.append(c) else: y.append(c) if not x: x.append(0) x.sort(reverse=True) y.sort(reverse=True) xn = len(x) yn = len(y) i = 1 j = 0 tKnapSack = x[0] print(tKnapSack, end=" ") for k in range(1, m): if j >= yn or i < xn and (xn == 1 or x[i] + x[i - 1] > y[j]): s = x[i] tKnapSack += s i += 1 else: s = y[j] - x[i - 1] tKnapSack += s j += 1 i -= 1 print(max(tKnapSack, tKnapSack - s), end=" ") print()
ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR STRING EXPR FUNC_CALL VAR
Read problems statements in Mandarin Chinese and Russian as well. Mike takes part in programming contests. His favourite topic is dynamic programming(DP). As he said, that he likes problems on DP, because "you spend a lot of time on thinking and a little time on coding". In this problem you are to solve a version of the knapsack problem(link), one of the most famous examples of DP problem. You are given N items, each has two parameters: a weight and a cost. Let's define M as the sum of the weights of all the items. Your task is to determine the most expensive cost of a knapsack, which capacity equals to 1, 2, ..., M. A cost of a knapsack equals to the sum of the costs of all the elements of the knapsack. Also, when you have a knapsack with a capacity is equal to C, then you can fill it with items, whose sum of weights is not greater than C. ------ Input ------ The first line of the input contains one integer N, denoting the number of the items. The next N lines contain two integers W and C each, denoting the weight and the cost of the corresponding item. ------ Output ------ For each capacity C (1 ≀ C ≀ M) of the knapsack, output a single integer - the most expensive cost for that capacity. ------ Constraints ------ 3 ≀ N ≀ 100000; 1 ≀ W ≀ 2, for each item; 1 ≀ C ≀ 10^{9}, for each item. ------ Example ------ Input: 5 1 1 2 2 2 3 2 4 2 5 Output: 1 5 6 9 10 12 13 14 15 ------ Explanations ------ In the test case, M equals to 9. For C = 1, it's optimal to choose {1} items; For C = 2, it's optimal to choose {5} items; For C = 3, it's optimal to choose {1, 5} items; For C = 4, it's optimal to choose {4, 5} items; For C = 5, it's optimal to choose {1, 4, 5} items; For C = 6, it's optimal to choose {3, 4, 5} items; For C = 7, it's optimal to choose {1, 3, 4, 5} items; For C = 8, it's optimal to choose {2, 3, 4, 5} items; For C = 9, it's optimal to choose {1, 2, 3, 4, 5} items.
from sys import stdin n = int(stdin.readline()) list1 = [] list2 = [] m = 0 sum1 = 0 pop = () for i in range(n): w, c = list(map(int, stdin.readline().split())) if w == 1: list1.append((w, c)) m += w else: list2.append((w, c)) m += w list1.sort(reverse=True, key=lambda x: x[1]) list2.sort(reverse=True, key=lambda x: x[1]) e_list1 = list1[:] e_list2 = list2[:] max1 = [0] * m if list1 != []: sum1 += list1.pop(0)[1] max1[0] = sum1 else: max1[0] = sum1 for i in range(2, m, 2): c = i + 1 if list1 != []: if list2 != [] and len(list1) >= 2: if list2[0][1] >= list1[0][1] + list1[1][1]: sum1 += list2[0][1] max1[i] = sum1 list2.pop(0) else: sum1 += list1[0][1] list1.pop(0) sum1 += list1[0][1] max1[i] = sum1 list1.pop(0) elif list2 != [] and len(list1) == 1: if list2[0][1] >= list1[0][1]: sum1 += list2[0][1] max1[i] = sum1 list2.pop(0) else: sum1 += list1[0][1] max1[i] = sum1 list1.pop(0) elif list2 == []: if len(list1) >= 2: sum1 += list1[0][1] list1.pop(0) sum1 += list1[0][1] max1[i] = sum1 list1.pop(0) else: sum1 += list1[0][1] max1[i] = sum1 list1.pop(0) elif list2 != []: sum1 += list2[0][1] max1[i] = sum1 list2.pop(0) sum1 = 0 for i in range(1, m, 2): c = i + 1 if e_list1 != []: if e_list2 != [] and len(e_list1) >= 2: if e_list2[0][1] >= e_list1[0][1] + e_list1[1][1]: sum1 += e_list2[0][1] max1[i] = sum1 e_list2.pop(0) else: sum1 += e_list1[0][1] e_list1.pop(0) sum1 += e_list1[0][1] max1[i] = sum1 e_list1.pop(0) elif e_list2 != [] and len(e_list1) == 1: if e_list2[0][1] >= e_list1[0][1]: sum1 += e_list2[0][1] max1[i] = sum1 e_list2.pop(0) else: sum1 += e_list1[0][1] max1[i] = sum1 e_list1.pop(0) elif e_list2 == []: if len(e_list1) >= 2: sum1 += e_list1[0][1] e_list1.pop(0) sum1 += e_list1[0][1] max1[i] = sum1 e_list1.pop(0) else: sum1 += e_list1[0][1] max1[i] = sum1 e_list1.pop(0) elif e_list2 != []: sum1 += e_list2[0][1] max1[i] = sum1 e_list2.pop(0) for j, val in enumerate(max1): print(val, end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR IF VAR LIST VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR LIST IF VAR LIST FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR LIST FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR LIST IF FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR LIST VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR LIST IF VAR LIST FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR LIST FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR LIST IF FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR LIST VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING
You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains n bombs, the i-th bomb is at point with coordinates (x_{i}, y_{i}). We know that no two bombs are at the same point and that no bomb is at point with coordinates (0, 0). Initially, the robot is at point with coordinates (0, 0). Also, let's mark the robot's current position as (x, y). In order to destroy all the bombs, the robot can perform three types of operations: Operation has format "1 k dir". To perform the operation robot have to move in direction dir k (k β‰₯ 1) times. There are only 4 directions the robot can move in: "R", "L", "U", "D". During one move the robot can move from the current point to one of following points: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) (corresponding to directions). It is forbidden to move from point (x, y), if at least one point on the path (besides the destination point) contains a bomb. Operation has format "2". To perform the operation robot have to pick a bomb at point (x, y) and put it in a special container. Thus, the robot can carry the bomb from any point to any other point. The operation cannot be performed if point (x, y) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in its container. Operation has format "3". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0, 0). It is forbidden to perform the operation if the container has no bomb. Help the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane. -----Input----- The first line contains a single integer n (1 ≀ n ≀ 10^5) β€” the number of bombs on the coordinate plane. Next n lines contain two integers each. The i-th line contains numbers (x_{i}, y_{i}) ( - 10^9 ≀ x_{i}, y_{i} ≀ 10^9) β€” the coordinates of the i-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0, 0). -----Output----- In a single line print a single integer k β€” the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these k operations. If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where k ≀ 10^6. -----Examples----- Input 2 1 1 -1 -1 Output 12 1 1 R 1 1 U 2 1 1 L 1 1 D 3 1 1 L 1 1 D 2 1 1 R 1 1 U 3 Input 3 5 0 0 5 1 0 Output 12 1 1 R 2 1 1 L 3 1 5 R 2 1 5 L 3 1 5 U 2 1 5 D 3
import sys def dist(p): return abs(p[0]) + abs(p[1]) n = int(input()) pts = [] for i in range(n): pts.append(tuple(map(int, input().split()))) pts.sort(key=dist) ops = [] def move(s, t, direc): if s == t: return if t > s: ops.append("1 {} {}".format(t - s, direc[0])) else: ops.append("1 {} {}".format(s - t, direc[1])) for p in pts: move(0, p[0], "RL") move(0, p[1], "UD") ops.append("2") move(p[0], 0, "RL") move(p[1], 0, "UD") ops.append("3") sys.stdout.write(str(len(ops)) + "\n") sys.stdout.write("\n".join(ops))
IMPORT FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_DEF IF VAR VAR RETURN IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER VAR NUMBER STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER NUMBER STRING EXPR FUNC_CALL VAR VAR NUMBER NUMBER STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains n bombs, the i-th bomb is at point with coordinates (x_{i}, y_{i}). We know that no two bombs are at the same point and that no bomb is at point with coordinates (0, 0). Initially, the robot is at point with coordinates (0, 0). Also, let's mark the robot's current position as (x, y). In order to destroy all the bombs, the robot can perform three types of operations: Operation has format "1 k dir". To perform the operation robot have to move in direction dir k (k β‰₯ 1) times. There are only 4 directions the robot can move in: "R", "L", "U", "D". During one move the robot can move from the current point to one of following points: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) (corresponding to directions). It is forbidden to move from point (x, y), if at least one point on the path (besides the destination point) contains a bomb. Operation has format "2". To perform the operation robot have to pick a bomb at point (x, y) and put it in a special container. Thus, the robot can carry the bomb from any point to any other point. The operation cannot be performed if point (x, y) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in its container. Operation has format "3". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0, 0). It is forbidden to perform the operation if the container has no bomb. Help the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane. -----Input----- The first line contains a single integer n (1 ≀ n ≀ 10^5) β€” the number of bombs on the coordinate plane. Next n lines contain two integers each. The i-th line contains numbers (x_{i}, y_{i}) ( - 10^9 ≀ x_{i}, y_{i} ≀ 10^9) β€” the coordinates of the i-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0, 0). -----Output----- In a single line print a single integer k β€” the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these k operations. If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where k ≀ 10^6. -----Examples----- Input 2 1 1 -1 -1 Output 12 1 1 R 1 1 U 2 1 1 L 1 1 D 3 1 1 L 1 1 D 2 1 1 R 1 1 U 3 Input 3 5 0 0 5 1 0 Output 12 1 1 R 2 1 1 L 3 1 5 R 2 1 5 L 3 1 5 U 2 1 5 D 3
from sys import stdin, stdout def inp(): return stdin.readline().strip() def iinp(): return int(inp()) def out(var, end="\n"): stdout.write(str(var) + "\n") def mp(): return map(int, inp().split()) n = iinp() ml = [] for i in range(n): x, y = mp() ml.append((x, y)) ml.sort(key=lambda x: abs(x[0]) + abs(x[1])) ansl = [] for x, y in ml: if x > 0: ansl.append("1 " + str(x) + " R") if x < 0: ansl.append("1 " + str(-x) + " L") if y > 0: ansl.append("1 " + str(y) + " U") if y < 0: ansl.append("1 " + str(-y) + " D") ansl.append("2") if x > 0: ansl.append("1 " + str(x) + " L") if x < 0: ansl.append("1 " + str(-x) + " R") if y > 0: ansl.append("1 " + str(y) + " D") if y < 0: ansl.append("1 " + str(-y) + " U") ansl.append("3") out(len(ansl)) out("\n".join(ansl))
FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING FUNC_CALL VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING FUNC_CALL VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING FUNC_CALL VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING FUNC_CALL VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING FUNC_CALL VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING FUNC_CALL VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains n bombs, the i-th bomb is at point with coordinates (x_{i}, y_{i}). We know that no two bombs are at the same point and that no bomb is at point with coordinates (0, 0). Initially, the robot is at point with coordinates (0, 0). Also, let's mark the robot's current position as (x, y). In order to destroy all the bombs, the robot can perform three types of operations: Operation has format "1 k dir". To perform the operation robot have to move in direction dir k (k β‰₯ 1) times. There are only 4 directions the robot can move in: "R", "L", "U", "D". During one move the robot can move from the current point to one of following points: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) (corresponding to directions). It is forbidden to move from point (x, y), if at least one point on the path (besides the destination point) contains a bomb. Operation has format "2". To perform the operation robot have to pick a bomb at point (x, y) and put it in a special container. Thus, the robot can carry the bomb from any point to any other point. The operation cannot be performed if point (x, y) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in its container. Operation has format "3". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0, 0). It is forbidden to perform the operation if the container has no bomb. Help the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane. -----Input----- The first line contains a single integer n (1 ≀ n ≀ 10^5) β€” the number of bombs on the coordinate plane. Next n lines contain two integers each. The i-th line contains numbers (x_{i}, y_{i}) ( - 10^9 ≀ x_{i}, y_{i} ≀ 10^9) β€” the coordinates of the i-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0, 0). -----Output----- In a single line print a single integer k β€” the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these k operations. If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where k ≀ 10^6. -----Examples----- Input 2 1 1 -1 -1 Output 12 1 1 R 1 1 U 2 1 1 L 1 1 D 3 1 1 L 1 1 D 2 1 1 R 1 1 U 3 Input 3 5 0 0 5 1 0 Output 12 1 1 R 2 1 1 L 3 1 5 R 2 1 5 L 3 1 5 U 2 1 5 D 3
n = int(input()) d = [] for i in range(n): x, y = list(map(int, input().split())) d.append([(x**2 + y**2) ** 0.5, x, y]) d.sort() ans = [] for i in range(n): if d[i][1] < 0: ans.append("1 " + str(-d[i][1]) + " L") elif d[i][1] > 0: ans.append("1 " + str(d[i][1]) + " R") if d[i][2] < 0: ans.append("1 " + str(-d[i][2]) + " D") elif d[i][2] > 0: ans.append("1 " + str(d[i][2]) + " U") ans.append("2") if d[i][2] < 0: ans.append("1 " + str(abs(d[i][2])) + " U") elif d[i][2] > 0: ans.append("1 " + str(abs(d[i][2])) + " D") if d[i][1] < 0: ans.append("1 " + str(abs(d[i][1])) + " R") elif d[i][1] > 0: ans.append("1 " + str(abs(d[i][1])) + " L") ans.append("3") print(len(ans)) print("\n".join(ans))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING FUNC_CALL VAR VAR VAR NUMBER STRING IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING FUNC_CALL VAR VAR VAR NUMBER STRING IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING FUNC_CALL VAR VAR VAR NUMBER STRING IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING FUNC_CALL VAR VAR VAR NUMBER STRING EXPR FUNC_CALL VAR STRING IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER STRING IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER STRING IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER STRING IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains n bombs, the i-th bomb is at point with coordinates (x_{i}, y_{i}). We know that no two bombs are at the same point and that no bomb is at point with coordinates (0, 0). Initially, the robot is at point with coordinates (0, 0). Also, let's mark the robot's current position as (x, y). In order to destroy all the bombs, the robot can perform three types of operations: Operation has format "1 k dir". To perform the operation robot have to move in direction dir k (k β‰₯ 1) times. There are only 4 directions the robot can move in: "R", "L", "U", "D". During one move the robot can move from the current point to one of following points: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) (corresponding to directions). It is forbidden to move from point (x, y), if at least one point on the path (besides the destination point) contains a bomb. Operation has format "2". To perform the operation robot have to pick a bomb at point (x, y) and put it in a special container. Thus, the robot can carry the bomb from any point to any other point. The operation cannot be performed if point (x, y) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in its container. Operation has format "3". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0, 0). It is forbidden to perform the operation if the container has no bomb. Help the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane. -----Input----- The first line contains a single integer n (1 ≀ n ≀ 10^5) β€” the number of bombs on the coordinate plane. Next n lines contain two integers each. The i-th line contains numbers (x_{i}, y_{i}) ( - 10^9 ≀ x_{i}, y_{i} ≀ 10^9) β€” the coordinates of the i-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0, 0). -----Output----- In a single line print a single integer k β€” the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these k operations. If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where k ≀ 10^6. -----Examples----- Input 2 1 1 -1 -1 Output 12 1 1 R 1 1 U 2 1 1 L 1 1 D 3 1 1 L 1 1 D 2 1 1 R 1 1 U 3 Input 3 5 0 0 5 1 0 Output 12 1 1 R 2 1 1 L 3 1 5 R 2 1 5 L 3 1 5 U 2 1 5 D 3
def f(i, j): x, y = str(abs(i)), str(abs(j)) l, r, u, d = " L", " R", " U", " D" if i < 0: l, r = r, l if j < 0: u, d = d, u if i: if j: return ["1 " + x + r, "1 " + y + u, "2", "1 " + x + l, "1 " + y + d, "3"] else: return ["1 " + x + r, "2", "1 " + x + l, "3"] else: return ["1 " + y + u, "2", "1 " + y + d, "3"] p, n = [], int(input()) t = [ (abs(i) + abs(j), i, j) for i, j in tuple(map(int, input().split()) for i in range(n)) ] t.sort() for r, i, j in t: p += f(i, j) print(len(p)) print("\n".join(p))
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR STRING STRING STRING STRING IF VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR IF VAR RETURN LIST BIN_OP BIN_OP STRING VAR VAR BIN_OP BIN_OP STRING VAR VAR STRING BIN_OP BIN_OP STRING VAR VAR BIN_OP BIN_OP STRING VAR VAR STRING RETURN LIST BIN_OP BIN_OP STRING VAR VAR STRING BIN_OP BIN_OP STRING VAR VAR STRING RETURN LIST BIN_OP BIN_OP STRING VAR VAR STRING BIN_OP BIN_OP STRING VAR VAR STRING ASSIGN VAR VAR LIST FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains n bombs, the i-th bomb is at point with coordinates (x_{i}, y_{i}). We know that no two bombs are at the same point and that no bomb is at point with coordinates (0, 0). Initially, the robot is at point with coordinates (0, 0). Also, let's mark the robot's current position as (x, y). In order to destroy all the bombs, the robot can perform three types of operations: Operation has format "1 k dir". To perform the operation robot have to move in direction dir k (k β‰₯ 1) times. There are only 4 directions the robot can move in: "R", "L", "U", "D". During one move the robot can move from the current point to one of following points: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) (corresponding to directions). It is forbidden to move from point (x, y), if at least one point on the path (besides the destination point) contains a bomb. Operation has format "2". To perform the operation robot have to pick a bomb at point (x, y) and put it in a special container. Thus, the robot can carry the bomb from any point to any other point. The operation cannot be performed if point (x, y) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in its container. Operation has format "3". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0, 0). It is forbidden to perform the operation if the container has no bomb. Help the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane. -----Input----- The first line contains a single integer n (1 ≀ n ≀ 10^5) β€” the number of bombs on the coordinate plane. Next n lines contain two integers each. The i-th line contains numbers (x_{i}, y_{i}) ( - 10^9 ≀ x_{i}, y_{i} ≀ 10^9) β€” the coordinates of the i-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0, 0). -----Output----- In a single line print a single integer k β€” the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these k operations. If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where k ≀ 10^6. -----Examples----- Input 2 1 1 -1 -1 Output 12 1 1 R 1 1 U 2 1 1 L 1 1 D 3 1 1 L 1 1 D 2 1 1 R 1 1 U 3 Input 3 5 0 0 5 1 0 Output 12 1 1 R 2 1 1 L 3 1 5 R 2 1 5 L 3 1 5 U 2 1 5 D 3
3 def readln(): return tuple(map(int, input().split())) (n,) = readln() ans = [] for x, y in sorted([readln() for _ in range(n)], key=lambda x: abs(x[0]) + abs(x[1])): if x > 0: ans.append("1 %d R" % x) if x < 0: ans.append("1 %d L" % -x) if y > 0: ans.append("1 %d U" % y) if y < 0: ans.append("1 %d D" % -y) ans.append("2") if x > 0: ans.append("1 %d L" % x) if x < 0: ans.append("1 %d R" % -x) if y > 0: ans.append("1 %d D" % y) if y < 0: ans.append("1 %d U" % -y) ans.append("3") print(len(ans)) print("\n".join(ans))
EXPR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains n bombs, the i-th bomb is at point with coordinates (x_{i}, y_{i}). We know that no two bombs are at the same point and that no bomb is at point with coordinates (0, 0). Initially, the robot is at point with coordinates (0, 0). Also, let's mark the robot's current position as (x, y). In order to destroy all the bombs, the robot can perform three types of operations: Operation has format "1 k dir". To perform the operation robot have to move in direction dir k (k β‰₯ 1) times. There are only 4 directions the robot can move in: "R", "L", "U", "D". During one move the robot can move from the current point to one of following points: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) (corresponding to directions). It is forbidden to move from point (x, y), if at least one point on the path (besides the destination point) contains a bomb. Operation has format "2". To perform the operation robot have to pick a bomb at point (x, y) and put it in a special container. Thus, the robot can carry the bomb from any point to any other point. The operation cannot be performed if point (x, y) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in its container. Operation has format "3". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0, 0). It is forbidden to perform the operation if the container has no bomb. Help the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane. -----Input----- The first line contains a single integer n (1 ≀ n ≀ 10^5) β€” the number of bombs on the coordinate plane. Next n lines contain two integers each. The i-th line contains numbers (x_{i}, y_{i}) ( - 10^9 ≀ x_{i}, y_{i} ≀ 10^9) β€” the coordinates of the i-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0, 0). -----Output----- In a single line print a single integer k β€” the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these k operations. If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where k ≀ 10^6. -----Examples----- Input 2 1 1 -1 -1 Output 12 1 1 R 1 1 U 2 1 1 L 1 1 D 3 1 1 L 1 1 D 2 1 1 R 1 1 U 3 Input 3 5 0 0 5 1 0 Output 12 1 1 R 2 1 1 L 3 1 5 R 2 1 5 L 3 1 5 U 2 1 5 D 3
from sys import stdin input = stdin.readline n = int(input()) seg = [list(map(int, input().split())) for _ in range(n)] seg = sorted(seg, key=lambda x: abs(x[0]) + abs(x[1])) res = [] for x, y in seg: if x > 0: res.append("1 %d %c" % (x, "R")) if x < 0: res.append("1 %d %c" % (-x, "L")) if y > 0: res.append("1 %d %c" % (y, "U")) if y < 0: res.append("1 %d %c" % (-y, "D")) res.append("2") if x > 0: res.append("1 %d %c" % (x, "L")) if x < 0: res.append("1 %d %c" % (-x, "R")) if y > 0: res.append("1 %d %c" % (y, "D")) if y < 0: res.append("1 %d %c" % (-y, "U")) res.append("3") print(len(res)) print("\n".join(res))
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 VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains n bombs, the i-th bomb is at point with coordinates (x_{i}, y_{i}). We know that no two bombs are at the same point and that no bomb is at point with coordinates (0, 0). Initially, the robot is at point with coordinates (0, 0). Also, let's mark the robot's current position as (x, y). In order to destroy all the bombs, the robot can perform three types of operations: Operation has format "1 k dir". To perform the operation robot have to move in direction dir k (k β‰₯ 1) times. There are only 4 directions the robot can move in: "R", "L", "U", "D". During one move the robot can move from the current point to one of following points: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) (corresponding to directions). It is forbidden to move from point (x, y), if at least one point on the path (besides the destination point) contains a bomb. Operation has format "2". To perform the operation robot have to pick a bomb at point (x, y) and put it in a special container. Thus, the robot can carry the bomb from any point to any other point. The operation cannot be performed if point (x, y) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in its container. Operation has format "3". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0, 0). It is forbidden to perform the operation if the container has no bomb. Help the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane. -----Input----- The first line contains a single integer n (1 ≀ n ≀ 10^5) β€” the number of bombs on the coordinate plane. Next n lines contain two integers each. The i-th line contains numbers (x_{i}, y_{i}) ( - 10^9 ≀ x_{i}, y_{i} ≀ 10^9) β€” the coordinates of the i-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0, 0). -----Output----- In a single line print a single integer k β€” the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these k operations. If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where k ≀ 10^6. -----Examples----- Input 2 1 1 -1 -1 Output 12 1 1 R 1 1 U 2 1 1 L 1 1 D 3 1 1 L 1 1 D 2 1 1 R 1 1 U 3 Input 3 5 0 0 5 1 0 Output 12 1 1 R 2 1 1 L 3 1 5 R 2 1 5 L 3 1 5 U 2 1 5 D 3
import sys input = sys.stdin.readline print = sys.stdout.write I = lambda: list(map(int, input().split())) (n,) = I() l = [] r = [] k = 0 for i in range(n): x, y = I() if x < 0: l.append([x, y]) else: r.append([x, y]) k = k + 4 if x != 0 and y != 0 else k + 2 k += 2 * n an = [] l.sort(key=lambda x: abs(x[0]) + abs(x[1])) r.sort(key=lambda x: abs(x[0]) + abs(x[1])) for i in range(len(r)): x, y = r[i] if x != 0: an.append("1 %d R" % x) if y != 0: an.append("1 %d %s" % (abs(y), "U" if y > 0 else "D")) an.append("2") if x != 0: an.append("1 %d L" % x) if y != 0: an.append("1 %d %c" % (abs(y), "D" if y > 0 else "U")) an.append("3") for i in range(len(l)): x, y = l[i] x = -x if x != 0: an.append("1 %d L" % x) if y != 0: an.append("1 %d %c" % (abs(y), "U" if y > 0 else "D")) an.append("2") if x != 0: an.append("1 %d R" % x) if y != 0: an.append("1 %d %c" % (abs(y), "D" if y > 0 else "U")) an.append("3") print(str(k) + "\n") print("\n".join(an))
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR VAR VAR NUMBER STRING STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR VAR VAR NUMBER STRING STRING EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR VAR VAR NUMBER STRING STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR VAR VAR NUMBER STRING STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains n bombs, the i-th bomb is at point with coordinates (x_{i}, y_{i}). We know that no two bombs are at the same point and that no bomb is at point with coordinates (0, 0). Initially, the robot is at point with coordinates (0, 0). Also, let's mark the robot's current position as (x, y). In order to destroy all the bombs, the robot can perform three types of operations: Operation has format "1 k dir". To perform the operation robot have to move in direction dir k (k β‰₯ 1) times. There are only 4 directions the robot can move in: "R", "L", "U", "D". During one move the robot can move from the current point to one of following points: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) (corresponding to directions). It is forbidden to move from point (x, y), if at least one point on the path (besides the destination point) contains a bomb. Operation has format "2". To perform the operation robot have to pick a bomb at point (x, y) and put it in a special container. Thus, the robot can carry the bomb from any point to any other point. The operation cannot be performed if point (x, y) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in its container. Operation has format "3". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0, 0). It is forbidden to perform the operation if the container has no bomb. Help the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane. -----Input----- The first line contains a single integer n (1 ≀ n ≀ 10^5) β€” the number of bombs on the coordinate plane. Next n lines contain two integers each. The i-th line contains numbers (x_{i}, y_{i}) ( - 10^9 ≀ x_{i}, y_{i} ≀ 10^9) β€” the coordinates of the i-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0, 0). -----Output----- In a single line print a single integer k β€” the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these k operations. If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where k ≀ 10^6. -----Examples----- Input 2 1 1 -1 -1 Output 12 1 1 R 1 1 U 2 1 1 L 1 1 D 3 1 1 L 1 1 D 2 1 1 R 1 1 U 3 Input 3 5 0 0 5 1 0 Output 12 1 1 R 2 1 1 L 3 1 5 R 2 1 5 L 3 1 5 U 2 1 5 D 3
from sys import stdin, stdout n = int(stdin.readline()) pos = [] for i in range(n): x, y = list(map(int, stdin.readline().split())) pos.append([x, y]) pos = sorted(pos, key=lambda pos: abs(pos[0]) + abs(pos[1])) moves = [] for p in pos: x, y = p xNeg, yNeg = False, False xPos, yPos = False, False if x > 0: moves.append("1 {} R".format(x)) xPos = True elif x < 0: moves.append("1 {} L".format(-x)) xNeg = True if y > 0: moves.append("1 {} U".format(y)) yPos = True elif y < 0: moves.append("1 {} D".format(-y)) yNeg = True moves.append("{}".format(2)) if xNeg: moves.append("1 {} R".format(-x)) elif xPos: moves.append("1 {} L".format(x)) if yNeg: moves.append("1 {} U".format(-y)) elif yPos: moves.append("1 {} D".format(y)) moves.append("{}".format(3)) print(len(moves)) for m in moves: stdout.write(m) stdout.write("\n")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING NUMBER IF VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR IF VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR IF VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR IF VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR FUNC_CALL STRING NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains n bombs, the i-th bomb is at point with coordinates (x_{i}, y_{i}). We know that no two bombs are at the same point and that no bomb is at point with coordinates (0, 0). Initially, the robot is at point with coordinates (0, 0). Also, let's mark the robot's current position as (x, y). In order to destroy all the bombs, the robot can perform three types of operations: Operation has format "1 k dir". To perform the operation robot have to move in direction dir k (k β‰₯ 1) times. There are only 4 directions the robot can move in: "R", "L", "U", "D". During one move the robot can move from the current point to one of following points: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) (corresponding to directions). It is forbidden to move from point (x, y), if at least one point on the path (besides the destination point) contains a bomb. Operation has format "2". To perform the operation robot have to pick a bomb at point (x, y) and put it in a special container. Thus, the robot can carry the bomb from any point to any other point. The operation cannot be performed if point (x, y) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in its container. Operation has format "3". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0, 0). It is forbidden to perform the operation if the container has no bomb. Help the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane. -----Input----- The first line contains a single integer n (1 ≀ n ≀ 10^5) β€” the number of bombs on the coordinate plane. Next n lines contain two integers each. The i-th line contains numbers (x_{i}, y_{i}) ( - 10^9 ≀ x_{i}, y_{i} ≀ 10^9) β€” the coordinates of the i-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0, 0). -----Output----- In a single line print a single integer k β€” the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these k operations. If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where k ≀ 10^6. -----Examples----- Input 2 1 1 -1 -1 Output 12 1 1 R 1 1 U 2 1 1 L 1 1 D 3 1 1 L 1 1 D 2 1 1 R 1 1 U 3 Input 3 5 0 0 5 1 0 Output 12 1 1 R 2 1 1 L 3 1 5 R 2 1 5 L 3 1 5 U 2 1 5 D 3
n = int(input()) arr = [ (abs(i) + abs(j), i, j) for i, j in tuple(map(int, input().split()) for i in range(n)) ] arr.sort() ans = [] for z, x, y in arr: X, Y = str(abs(x)), str(abs(y)) l, r, u, d = " L", " R", " U", " D" if x < 0: l, r = r, l if y < 0: u, d = d, u if x: if y: ans += ["1 " + X + r, "1 " + Y + u, "2", "1 " + X + l, "1 " + Y + d, "3"] else: ans += ["1 " + X + r, "2", "1 " + X + l, "3"] else: ans += ["1 " + Y + u, "2", "1 " + Y + d, "3"] print(len(ans)) print("\n".join(ans))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR STRING STRING STRING STRING IF VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR IF VAR VAR LIST BIN_OP BIN_OP STRING VAR VAR BIN_OP BIN_OP STRING VAR VAR STRING BIN_OP BIN_OP STRING VAR VAR BIN_OP BIN_OP STRING VAR VAR STRING VAR LIST BIN_OP BIN_OP STRING VAR VAR STRING BIN_OP BIN_OP STRING VAR VAR STRING VAR LIST BIN_OP BIN_OP STRING VAR VAR STRING BIN_OP BIN_OP STRING VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains n bombs, the i-th bomb is at point with coordinates (x_{i}, y_{i}). We know that no two bombs are at the same point and that no bomb is at point with coordinates (0, 0). Initially, the robot is at point with coordinates (0, 0). Also, let's mark the robot's current position as (x, y). In order to destroy all the bombs, the robot can perform three types of operations: Operation has format "1 k dir". To perform the operation robot have to move in direction dir k (k β‰₯ 1) times. There are only 4 directions the robot can move in: "R", "L", "U", "D". During one move the robot can move from the current point to one of following points: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) (corresponding to directions). It is forbidden to move from point (x, y), if at least one point on the path (besides the destination point) contains a bomb. Operation has format "2". To perform the operation robot have to pick a bomb at point (x, y) and put it in a special container. Thus, the robot can carry the bomb from any point to any other point. The operation cannot be performed if point (x, y) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in its container. Operation has format "3". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0, 0). It is forbidden to perform the operation if the container has no bomb. Help the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane. -----Input----- The first line contains a single integer n (1 ≀ n ≀ 10^5) β€” the number of bombs on the coordinate plane. Next n lines contain two integers each. The i-th line contains numbers (x_{i}, y_{i}) ( - 10^9 ≀ x_{i}, y_{i} ≀ 10^9) β€” the coordinates of the i-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0, 0). -----Output----- In a single line print a single integer k β€” the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these k operations. If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where k ≀ 10^6. -----Examples----- Input 2 1 1 -1 -1 Output 12 1 1 R 1 1 U 2 1 1 L 1 1 D 3 1 1 L 1 1 D 2 1 1 R 1 1 U 3 Input 3 5 0 0 5 1 0 Output 12 1 1 R 2 1 1 L 3 1 5 R 2 1 5 L 3 1 5 U 2 1 5 D 3
import sys testcases = int(input()) def get_ints(): return map(int, sys.stdin.readline().strip().split()) arr = [] count_zero = 0 for testcase in range(testcases): x, y = get_ints() arr.append((x, y)) if x == 0: count_zero += 1 if y == 0: count_zero += 1 arr.sort(key=lambda x: abs(x[0]) + abs(x[1])) print(6 * testcases - 2 * count_zero) for tup in arr: x = tup[0] y = tup[1] if x < 0: print("1 " + str(abs(x)) + " L") if x > 0: print("1 " + str(x) + " R") if y < 0: print("1 " + str(abs(y)) + " D") if y > 0: print("1 " + str(abs(y)) + " U") print("2") if x < 0: print("1 " + str(abs(x)) + " R") if x > 0: print("1 " + str(x) + " L") if y < 0: print("1 " + str(abs(y)) + " U") if y > 0: print("1 " + str(y) + " D") print("3")
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING FUNC_CALL VAR FUNC_CALL VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING FUNC_CALL VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING FUNC_CALL VAR FUNC_CALL VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING FUNC_CALL VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING FUNC_CALL VAR FUNC_CALL VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING FUNC_CALL VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING FUNC_CALL VAR FUNC_CALL VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING
You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains n bombs, the i-th bomb is at point with coordinates (x_{i}, y_{i}). We know that no two bombs are at the same point and that no bomb is at point with coordinates (0, 0). Initially, the robot is at point with coordinates (0, 0). Also, let's mark the robot's current position as (x, y). In order to destroy all the bombs, the robot can perform three types of operations: Operation has format "1 k dir". To perform the operation robot have to move in direction dir k (k β‰₯ 1) times. There are only 4 directions the robot can move in: "R", "L", "U", "D". During one move the robot can move from the current point to one of following points: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) (corresponding to directions). It is forbidden to move from point (x, y), if at least one point on the path (besides the destination point) contains a bomb. Operation has format "2". To perform the operation robot have to pick a bomb at point (x, y) and put it in a special container. Thus, the robot can carry the bomb from any point to any other point. The operation cannot be performed if point (x, y) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in its container. Operation has format "3". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0, 0). It is forbidden to perform the operation if the container has no bomb. Help the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane. -----Input----- The first line contains a single integer n (1 ≀ n ≀ 10^5) β€” the number of bombs on the coordinate plane. Next n lines contain two integers each. The i-th line contains numbers (x_{i}, y_{i}) ( - 10^9 ≀ x_{i}, y_{i} ≀ 10^9) β€” the coordinates of the i-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0, 0). -----Output----- In a single line print a single integer k β€” the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these k operations. If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where k ≀ 10^6. -----Examples----- Input 2 1 1 -1 -1 Output 12 1 1 R 1 1 U 2 1 1 L 1 1 D 3 1 1 L 1 1 D 2 1 1 R 1 1 U 3 Input 3 5 0 0 5 1 0 Output 12 1 1 R 2 1 1 L 3 1 5 R 2 1 5 L 3 1 5 U 2 1 5 D 3
n = int(input()) tot = 0 boombs = [] for _ in range(n): x, y = map(int, input().split()) tot += 2 + (2 if x == 0 or y == 0 else 4) boombs.append((abs(x) + abs(y), x, y)) boombs.sort() moves = "LR", "DU" print(tot) for boomb in boombs: ans = [] _, x, y = map(int, boomb) if x: ans.append(f"1 {abs(x)} {moves[0][x > 0]}") if y: ans.append(f"1 {abs(y)} {moves[1][y > 0]}") ans.append("2") if x: ans.append("1 " + str(abs(x)) + " " + moves[0][x < 0]) if y: ans.append("1 " + str(abs(y)) + " " + moves[1][y < 0]) ans.append("3") print("\n".join(ans))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP NUMBER VAR NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR STRING STRING EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR STRING FUNC_CALL VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING FUNC_CALL VAR VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP STRING FUNC_CALL VAR FUNC_CALL VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP STRING FUNC_CALL VAR FUNC_CALL VAR VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
After bracket sequences Arthur took up number theory. He has got a new favorite sequence of length n (a1, a2, ..., an), consisting of integers and integer k, not exceeding n. This sequence had the following property: if you write out the sums of all its segments consisting of k consecutive elements (a1 + a2 ... + ak, a2 + a3 + ... + ak + 1, ..., an - k + 1 + an - k + 2 + ... + an), then those numbers will form strictly increasing sequence. For example, for the following sample: n = 5, k = 3, a = (1, 2, 4, 5, 6) the sequence of numbers will look as follows: (1 + 2 + 4, 2 + 4 + 5, 4 + 5 + 6) = (7, 11, 15), that means that sequence a meets the described property. Obviously the sequence of sums will have n - k + 1 elements. Somebody (we won't say who) replaced some numbers in Arthur's sequence by question marks (if this number is replaced, it is replaced by exactly one question mark). We need to restore the sequence so that it meets the required property and also minimize the sum |ai|, where |ai| is the absolute value of ai. Input The first line contains two integers n and k (1 ≀ k ≀ n ≀ 105), showing how many numbers are in Arthur's sequence and the lengths of segments respectively. The next line contains n space-separated elements ai (1 ≀ i ≀ n). If ai = ?, then the i-th element of Arthur's sequence was replaced by a question mark. Otherwise, ai ( - 109 ≀ ai ≀ 109) is the i-th element of Arthur's sequence. Output If Arthur is wrong at some point and there is no sequence that could fit the given information, print a single string "Incorrect sequence" (without the quotes). Otherwise, print n integers β€” Arthur's favorite sequence. If there are multiple such sequences, print the sequence with the minimum sum |ai|, where |ai| is the absolute value of ai. If there are still several such sequences, you are allowed to print any of them. Print the elements of the sequence without leading zeroes. Examples Input 3 2 ? 1 2 Output 0 1 2 Input 5 1 -10 -9 ? -7 -6 Output -10 -9 -8 -7 -6 Input 5 3 4 6 7 2 9 Output Incorrect sequence
import sys n, k = map(int, input().split()) a = input().split() INF = 10**9 + 7 OK = True for i in range(n): if a[i] == "?": a[i] = INF else: a[i] = int(a[i]) for i in range(len(a)): if a[i] == INF: j = i + k while j < len(a) and a[j] == INF: j += k count = (j - i) // k if i - k >= 0: left = a[i - k] else: left = -INF if j < len(a): right = a[j] else: right = INF if right < INF and left > -INF and right - left <= count: print("Incorrect sequence") OK = False break if left >= -1: a[i:j:k] = [(left + g + 1) for g in range(count)] elif right <= 1: a[i:j:k] = [(right - count + g) for g in range(count)] else: if -left < right: c1 = min(-left - 1, count // 2) new = [(-c1 + g) for g in range(count)] else: c2 = min(right - 1, count // 2) new = [(c2 - count + 1 + g) for g in range(count)] a[i:j:k] = new if OK: for i in range(n - k): if a[i] >= a[i + k]: print("Incorrect sequence") OK = False break if OK: print(" ".join(map(str, a)))
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
After bracket sequences Arthur took up number theory. He has got a new favorite sequence of length n (a1, a2, ..., an), consisting of integers and integer k, not exceeding n. This sequence had the following property: if you write out the sums of all its segments consisting of k consecutive elements (a1 + a2 ... + ak, a2 + a3 + ... + ak + 1, ..., an - k + 1 + an - k + 2 + ... + an), then those numbers will form strictly increasing sequence. For example, for the following sample: n = 5, k = 3, a = (1, 2, 4, 5, 6) the sequence of numbers will look as follows: (1 + 2 + 4, 2 + 4 + 5, 4 + 5 + 6) = (7, 11, 15), that means that sequence a meets the described property. Obviously the sequence of sums will have n - k + 1 elements. Somebody (we won't say who) replaced some numbers in Arthur's sequence by question marks (if this number is replaced, it is replaced by exactly one question mark). We need to restore the sequence so that it meets the required property and also minimize the sum |ai|, where |ai| is the absolute value of ai. Input The first line contains two integers n and k (1 ≀ k ≀ n ≀ 105), showing how many numbers are in Arthur's sequence and the lengths of segments respectively. The next line contains n space-separated elements ai (1 ≀ i ≀ n). If ai = ?, then the i-th element of Arthur's sequence was replaced by a question mark. Otherwise, ai ( - 109 ≀ ai ≀ 109) is the i-th element of Arthur's sequence. Output If Arthur is wrong at some point and there is no sequence that could fit the given information, print a single string "Incorrect sequence" (without the quotes). Otherwise, print n integers β€” Arthur's favorite sequence. If there are multiple such sequences, print the sequence with the minimum sum |ai|, where |ai| is the absolute value of ai. If there are still several such sequences, you are allowed to print any of them. Print the elements of the sequence without leading zeroes. Examples Input 3 2 ? 1 2 Output 0 1 2 Input 5 1 -10 -9 ? -7 -6 Output -10 -9 -8 -7 -6 Input 5 3 4 6 7 2 9 Output Incorrect sequence
def foo(left, right, number): left += 1 right -= 1 if right - left + 1 < number: return 10**10 num = number // 2 if number % 2: if num <= right and -num >= left: return -num else: if -num >= left and num - 1 <= right: return -num if -num + 1 >= left and num <= right: return -num + 1 if abs(left) < abs(right): return left else: return right - number + 1 con = 10**10 n, k = map(int, input().split()) a = list(input().split()) for i in range(k): left = -con number = 0 place = i for j in range(i, n, k): if a[j][0] == "?": number += 1 if number == 1: place = j else: right = int(a[j]) t = foo(left, right, number) if t == con: print("Incorrect sequence") exit() for r in range(place, j, k): a[r] = t t += 1 number = 0 left = right place = j + k if number: t = foo(left, con, number) if t == con: print("Incorrect sequence") exit() for r in range(place, n, k): a[r] = t t += 1 number = 0 left = con print(" ".join(map(str, a)))
FUNC_DEF VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER IF VAR VAR VAR VAR RETURN VAR IF VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR IF BIN_OP VAR NUMBER VAR VAR VAR RETURN BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR RETURN BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR NUMBER STRING VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
After bracket sequences Arthur took up number theory. He has got a new favorite sequence of length n (a1, a2, ..., an), consisting of integers and integer k, not exceeding n. This sequence had the following property: if you write out the sums of all its segments consisting of k consecutive elements (a1 + a2 ... + ak, a2 + a3 + ... + ak + 1, ..., an - k + 1 + an - k + 2 + ... + an), then those numbers will form strictly increasing sequence. For example, for the following sample: n = 5, k = 3, a = (1, 2, 4, 5, 6) the sequence of numbers will look as follows: (1 + 2 + 4, 2 + 4 + 5, 4 + 5 + 6) = (7, 11, 15), that means that sequence a meets the described property. Obviously the sequence of sums will have n - k + 1 elements. Somebody (we won't say who) replaced some numbers in Arthur's sequence by question marks (if this number is replaced, it is replaced by exactly one question mark). We need to restore the sequence so that it meets the required property and also minimize the sum |ai|, where |ai| is the absolute value of ai. Input The first line contains two integers n and k (1 ≀ k ≀ n ≀ 105), showing how many numbers are in Arthur's sequence and the lengths of segments respectively. The next line contains n space-separated elements ai (1 ≀ i ≀ n). If ai = ?, then the i-th element of Arthur's sequence was replaced by a question mark. Otherwise, ai ( - 109 ≀ ai ≀ 109) is the i-th element of Arthur's sequence. Output If Arthur is wrong at some point and there is no sequence that could fit the given information, print a single string "Incorrect sequence" (without the quotes). Otherwise, print n integers β€” Arthur's favorite sequence. If there are multiple such sequences, print the sequence with the minimum sum |ai|, where |ai| is the absolute value of ai. If there are still several such sequences, you are allowed to print any of them. Print the elements of the sequence without leading zeroes. Examples Input 3 2 ? 1 2 Output 0 1 2 Input 5 1 -10 -9 ? -7 -6 Output -10 -9 -8 -7 -6 Input 5 3 4 6 7 2 9 Output Incorrect sequence
INF = 10000000001 def fill(s): s.insert(0, -INF) s.append(INF) i = 0 for j in filter(lambda x: s[x] != "?", range(1, len(s))): d = i - j s[j] = int(s[j]) if s[i] > s[j] + d: raise a = max(min(d // 2, s[j] + d), s[i]) for t in range(i + 1, j): s[t] = a + t - i i = j return s[1:-1] n, k = map(int, input().split()) s = input().split() try: g = [fill([s[i] for i in range(j, n, k)]) for j in range(k)] print(" ".join(str(g[i % k][i // k]) for i in range(n))) except: print("Incorrect sequence")
ASSIGN VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR STRING FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR RETURN VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
After bracket sequences Arthur took up number theory. He has got a new favorite sequence of length n (a1, a2, ..., an), consisting of integers and integer k, not exceeding n. This sequence had the following property: if you write out the sums of all its segments consisting of k consecutive elements (a1 + a2 ... + ak, a2 + a3 + ... + ak + 1, ..., an - k + 1 + an - k + 2 + ... + an), then those numbers will form strictly increasing sequence. For example, for the following sample: n = 5, k = 3, a = (1, 2, 4, 5, 6) the sequence of numbers will look as follows: (1 + 2 + 4, 2 + 4 + 5, 4 + 5 + 6) = (7, 11, 15), that means that sequence a meets the described property. Obviously the sequence of sums will have n - k + 1 elements. Somebody (we won't say who) replaced some numbers in Arthur's sequence by question marks (if this number is replaced, it is replaced by exactly one question mark). We need to restore the sequence so that it meets the required property and also minimize the sum |ai|, where |ai| is the absolute value of ai. Input The first line contains two integers n and k (1 ≀ k ≀ n ≀ 105), showing how many numbers are in Arthur's sequence and the lengths of segments respectively. The next line contains n space-separated elements ai (1 ≀ i ≀ n). If ai = ?, then the i-th element of Arthur's sequence was replaced by a question mark. Otherwise, ai ( - 109 ≀ ai ≀ 109) is the i-th element of Arthur's sequence. Output If Arthur is wrong at some point and there is no sequence that could fit the given information, print a single string "Incorrect sequence" (without the quotes). Otherwise, print n integers β€” Arthur's favorite sequence. If there are multiple such sequences, print the sequence with the minimum sum |ai|, where |ai| is the absolute value of ai. If there are still several such sequences, you are allowed to print any of them. Print the elements of the sequence without leading zeroes. Examples Input 3 2 ? 1 2 Output 0 1 2 Input 5 1 -10 -9 ? -7 -6 Output -10 -9 -8 -7 -6 Input 5 3 4 6 7 2 9 Output Incorrect sequence
n, k = map(int, input().split()) arr = [[-2 * 10**9] for i in range(k)] for i, j in enumerate(input().split()): arr[i % k].append(j) b = True for i in range(k): arr[i].append(2 * 10**9) z, c = 1, 0 while z < len(arr[i]): while arr[i][z] == "?": z += 1 arr[i][z] = int(arr[i][z]) d = z - c - 1 if arr[i][z] - arr[i][c] < d + 1: b = False break if arr[i][c] > 0: s = arr[i][c] + 1 else: s = min(-(d // 2), arr[i][z] - d) s = max(s, arr[i][c] + 1) for j in range(c + 1, z): arr[i][j] = s s += 1 c = z z += 1 if not b: print("Incorrect sequence") break else: z = 1 i = 0 j = 0 while i < n: i += 1 print(arr[j][z], end=" ") j += 1 if j == k: j = 0 z += 1
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST BIN_OP NUMBER BIN_OP NUMBER NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR STRING VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER
After bracket sequences Arthur took up number theory. He has got a new favorite sequence of length n (a1, a2, ..., an), consisting of integers and integer k, not exceeding n. This sequence had the following property: if you write out the sums of all its segments consisting of k consecutive elements (a1 + a2 ... + ak, a2 + a3 + ... + ak + 1, ..., an - k + 1 + an - k + 2 + ... + an), then those numbers will form strictly increasing sequence. For example, for the following sample: n = 5, k = 3, a = (1, 2, 4, 5, 6) the sequence of numbers will look as follows: (1 + 2 + 4, 2 + 4 + 5, 4 + 5 + 6) = (7, 11, 15), that means that sequence a meets the described property. Obviously the sequence of sums will have n - k + 1 elements. Somebody (we won't say who) replaced some numbers in Arthur's sequence by question marks (if this number is replaced, it is replaced by exactly one question mark). We need to restore the sequence so that it meets the required property and also minimize the sum |ai|, where |ai| is the absolute value of ai. Input The first line contains two integers n and k (1 ≀ k ≀ n ≀ 105), showing how many numbers are in Arthur's sequence and the lengths of segments respectively. The next line contains n space-separated elements ai (1 ≀ i ≀ n). If ai = ?, then the i-th element of Arthur's sequence was replaced by a question mark. Otherwise, ai ( - 109 ≀ ai ≀ 109) is the i-th element of Arthur's sequence. Output If Arthur is wrong at some point and there is no sequence that could fit the given information, print a single string "Incorrect sequence" (without the quotes). Otherwise, print n integers β€” Arthur's favorite sequence. If there are multiple such sequences, print the sequence with the minimum sum |ai|, where |ai| is the absolute value of ai. If there are still several such sequences, you are allowed to print any of them. Print the elements of the sequence without leading zeroes. Examples Input 3 2 ? 1 2 Output 0 1 2 Input 5 1 -10 -9 ? -7 -6 Output -10 -9 -8 -7 -6 Input 5 3 4 6 7 2 9 Output Incorrect sequence
INF = 1 << 60 Q = 1 << 58 def solve(): ans = [0] * n for i in range(k): b = [-INF] b.extend(a[i:n:k]) m = len(b) b.append(INF) lb = -INF p, q = 1, 0 while p < m: while b[p] == Q: p += 1 l = p - q - 1 lb = b[q] + 1 for j in range(q + 1, p): b[j] = lb lb += 1 if b[p] < lb: return None if lb < 0: if b[p] > 0: lb = min((l - 1) // 2, b[p] - 1) elif b[p] <= 0: lb = b[p] - 1 for j in range(p - 1, q, -1): b[j] = lb lb -= 1 q = p p = q + 1 ans[i:n:k] = b[1:m] return ans n, k = [int(x) for x in input().split()] a = [(Q if x == "?" else int(x)) for x in input().split()] ans = solve() if ans is None: print("Incorrect sequence") else: print(*ans)
ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR VAR RETURN NONE IF VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR STRING VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NONE EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
Valera's lifelong ambition was to be a photographer, so he bought a new camera. Every day he got more and more clients asking for photos, and one day Valera needed a program that would determine the maximum number of people he can serve. The camera's memory is d megabytes. Valera's camera can take photos of high and low quality. One low quality photo takes a megabytes of memory, one high quality photo take b megabytes of memory. For unknown reasons, each client asks him to make several low quality photos and several high quality photos. More formally, the i-th client asks to make xi low quality photos and yi high quality photos. Valera wants to serve as many clients per day as possible, provided that they will be pleased with his work. To please the i-th client, Valera needs to give him everything he wants, that is, to make xi low quality photos and yi high quality photos. To make one low quality photo, the camera must have at least a megabytes of free memory space. Similarly, to make one high quality photo, the camera must have at least b megabytes of free memory space. Initially the camera's memory is empty. Valera also does not delete photos from the camera so that the camera's memory gradually fills up. Calculate the maximum number of clients Valera can successfully serve and print the numbers of these clients. Input The first line contains two integers n and d (1 ≀ n ≀ 105, 1 ≀ d ≀ 109) β€” the number of clients and the camera memory size, correspondingly. The second line contains two integers a and b (1 ≀ a ≀ b ≀ 104) β€” the size of one low quality photo and of one high quality photo, correspondingly. Next n lines describe the clients. The i-th line contains two integers xi and yi (0 ≀ xi, yi ≀ 105) β€” the number of low quality photos and high quality photos the i-th client wants, correspondingly. All numbers on all lines are separated by single spaces. Output On the first line print the answer to the problem β€” the maximum number of clients that Valera can successfully serve. Print on the second line the numbers of the client in any order. All numbers must be distinct. If there are multiple answers, print any of them. The clients are numbered starting with 1 in the order in which they are defined in the input data. Examples Input 3 10 2 3 1 4 2 1 1 0 Output 2 3 2 Input 3 6 6 6 1 1 1 0 1 0 Output 1 2
def readln(): return tuple(map(int, input().split())) n, d = readln() a, b = readln() lst = [] for _ in range(n): x, y = readln() lst.append((a * x + b * y, _ + 1)) lst.sort() ans = [] s = 0 for v, i in lst: if s + v > d: break s += v ans.append(i) print(len(ans)) print(*ans)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Valera's lifelong ambition was to be a photographer, so he bought a new camera. Every day he got more and more clients asking for photos, and one day Valera needed a program that would determine the maximum number of people he can serve. The camera's memory is d megabytes. Valera's camera can take photos of high and low quality. One low quality photo takes a megabytes of memory, one high quality photo take b megabytes of memory. For unknown reasons, each client asks him to make several low quality photos and several high quality photos. More formally, the i-th client asks to make xi low quality photos and yi high quality photos. Valera wants to serve as many clients per day as possible, provided that they will be pleased with his work. To please the i-th client, Valera needs to give him everything he wants, that is, to make xi low quality photos and yi high quality photos. To make one low quality photo, the camera must have at least a megabytes of free memory space. Similarly, to make one high quality photo, the camera must have at least b megabytes of free memory space. Initially the camera's memory is empty. Valera also does not delete photos from the camera so that the camera's memory gradually fills up. Calculate the maximum number of clients Valera can successfully serve and print the numbers of these clients. Input The first line contains two integers n and d (1 ≀ n ≀ 105, 1 ≀ d ≀ 109) β€” the number of clients and the camera memory size, correspondingly. The second line contains two integers a and b (1 ≀ a ≀ b ≀ 104) β€” the size of one low quality photo and of one high quality photo, correspondingly. Next n lines describe the clients. The i-th line contains two integers xi and yi (0 ≀ xi, yi ≀ 105) β€” the number of low quality photos and high quality photos the i-th client wants, correspondingly. All numbers on all lines are separated by single spaces. Output On the first line print the answer to the problem β€” the maximum number of clients that Valera can successfully serve. Print on the second line the numbers of the client in any order. All numbers must be distinct. If there are multiple answers, print any of them. The clients are numbered starting with 1 in the order in which they are defined in the input data. Examples Input 3 10 2 3 1 4 2 1 1 0 Output 2 3 2 Input 3 6 6 6 1 1 1 0 1 0 Output 1 2
n, d = map(int, input().split()) a, b = map(int, input().split()) t = [0] * n for i in range(n): x, y = map(int, input().split()) t[i] = a * x + b * y, i t.sort() j, s = n, 0 for i in range(n): s += t[i][0] if s > d: j = i break print(j) print(" ".join(str(t[i][1] + 1) for i in range(j)))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER 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 VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR
Valera's lifelong ambition was to be a photographer, so he bought a new camera. Every day he got more and more clients asking for photos, and one day Valera needed a program that would determine the maximum number of people he can serve. The camera's memory is d megabytes. Valera's camera can take photos of high and low quality. One low quality photo takes a megabytes of memory, one high quality photo take b megabytes of memory. For unknown reasons, each client asks him to make several low quality photos and several high quality photos. More formally, the i-th client asks to make xi low quality photos and yi high quality photos. Valera wants to serve as many clients per day as possible, provided that they will be pleased with his work. To please the i-th client, Valera needs to give him everything he wants, that is, to make xi low quality photos and yi high quality photos. To make one low quality photo, the camera must have at least a megabytes of free memory space. Similarly, to make one high quality photo, the camera must have at least b megabytes of free memory space. Initially the camera's memory is empty. Valera also does not delete photos from the camera so that the camera's memory gradually fills up. Calculate the maximum number of clients Valera can successfully serve and print the numbers of these clients. Input The first line contains two integers n and d (1 ≀ n ≀ 105, 1 ≀ d ≀ 109) β€” the number of clients and the camera memory size, correspondingly. The second line contains two integers a and b (1 ≀ a ≀ b ≀ 104) β€” the size of one low quality photo and of one high quality photo, correspondingly. Next n lines describe the clients. The i-th line contains two integers xi and yi (0 ≀ xi, yi ≀ 105) β€” the number of low quality photos and high quality photos the i-th client wants, correspondingly. All numbers on all lines are separated by single spaces. Output On the first line print the answer to the problem β€” the maximum number of clients that Valera can successfully serve. Print on the second line the numbers of the client in any order. All numbers must be distinct. If there are multiple answers, print any of them. The clients are numbered starting with 1 in the order in which they are defined in the input data. Examples Input 3 10 2 3 1 4 2 1 1 0 Output 2 3 2 Input 3 6 6 6 1 1 1 0 1 0 Output 1 2
n, d = map(int, input().split()) a, b = map(int, input().split()) people = [] for i in range(1, n + 1): x, y = map(int, input().split()) people.append((a * x + b * y, i)) people.sort() pos = 0 while True: d -= people[pos][0] if d < 0: break pos += 1 if pos == n: break print(pos) if pos != 0: print(" ".join(map(lambda person: str(person[1]), people[:pos])))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE NUMBER VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR
Valera's lifelong ambition was to be a photographer, so he bought a new camera. Every day he got more and more clients asking for photos, and one day Valera needed a program that would determine the maximum number of people he can serve. The camera's memory is d megabytes. Valera's camera can take photos of high and low quality. One low quality photo takes a megabytes of memory, one high quality photo take b megabytes of memory. For unknown reasons, each client asks him to make several low quality photos and several high quality photos. More formally, the i-th client asks to make xi low quality photos and yi high quality photos. Valera wants to serve as many clients per day as possible, provided that they will be pleased with his work. To please the i-th client, Valera needs to give him everything he wants, that is, to make xi low quality photos and yi high quality photos. To make one low quality photo, the camera must have at least a megabytes of free memory space. Similarly, to make one high quality photo, the camera must have at least b megabytes of free memory space. Initially the camera's memory is empty. Valera also does not delete photos from the camera so that the camera's memory gradually fills up. Calculate the maximum number of clients Valera can successfully serve and print the numbers of these clients. Input The first line contains two integers n and d (1 ≀ n ≀ 105, 1 ≀ d ≀ 109) β€” the number of clients and the camera memory size, correspondingly. The second line contains two integers a and b (1 ≀ a ≀ b ≀ 104) β€” the size of one low quality photo and of one high quality photo, correspondingly. Next n lines describe the clients. The i-th line contains two integers xi and yi (0 ≀ xi, yi ≀ 105) β€” the number of low quality photos and high quality photos the i-th client wants, correspondingly. All numbers on all lines are separated by single spaces. Output On the first line print the answer to the problem β€” the maximum number of clients that Valera can successfully serve. Print on the second line the numbers of the client in any order. All numbers must be distinct. If there are multiple answers, print any of them. The clients are numbered starting with 1 in the order in which they are defined in the input data. Examples Input 3 10 2 3 1 4 2 1 1 0 Output 2 3 2 Input 3 6 6 6 1 1 1 0 1 0 Output 1 2
from sys import * inp = lambda: stdin.readline() def main(): n, d = map(int, inp().split()) a, b = map(int, inp().split()) l, ans = [], [] for i in range(n): x, y = map(int, inp().split()) l.append((x * a + y * b, i + 1)) l = sorted(l) for i in l: if i[0] <= d: ans.append(i[1]) d -= i[0] print(len(ans)) print(" ".join(map(str, ans))) main()
ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
Valera's lifelong ambition was to be a photographer, so he bought a new camera. Every day he got more and more clients asking for photos, and one day Valera needed a program that would determine the maximum number of people he can serve. The camera's memory is d megabytes. Valera's camera can take photos of high and low quality. One low quality photo takes a megabytes of memory, one high quality photo take b megabytes of memory. For unknown reasons, each client asks him to make several low quality photos and several high quality photos. More formally, the i-th client asks to make xi low quality photos and yi high quality photos. Valera wants to serve as many clients per day as possible, provided that they will be pleased with his work. To please the i-th client, Valera needs to give him everything he wants, that is, to make xi low quality photos and yi high quality photos. To make one low quality photo, the camera must have at least a megabytes of free memory space. Similarly, to make one high quality photo, the camera must have at least b megabytes of free memory space. Initially the camera's memory is empty. Valera also does not delete photos from the camera so that the camera's memory gradually fills up. Calculate the maximum number of clients Valera can successfully serve and print the numbers of these clients. Input The first line contains two integers n and d (1 ≀ n ≀ 105, 1 ≀ d ≀ 109) β€” the number of clients and the camera memory size, correspondingly. The second line contains two integers a and b (1 ≀ a ≀ b ≀ 104) β€” the size of one low quality photo and of one high quality photo, correspondingly. Next n lines describe the clients. The i-th line contains two integers xi and yi (0 ≀ xi, yi ≀ 105) β€” the number of low quality photos and high quality photos the i-th client wants, correspondingly. All numbers on all lines are separated by single spaces. Output On the first line print the answer to the problem β€” the maximum number of clients that Valera can successfully serve. Print on the second line the numbers of the client in any order. All numbers must be distinct. If there are multiple answers, print any of them. The clients are numbered starting with 1 in the order in which they are defined in the input data. Examples Input 3 10 2 3 1 4 2 1 1 0 Output 2 3 2 Input 3 6 6 6 1 1 1 0 1 0 Output 1 2
R = lambda: list(map(int, input().split())) n, d = R() a, b = R() lst1 = [] lst2 = [] lst3 = [] sum = 0 cnt = 1 for i in range(0, n): x, y = R() p = x * a + y * b lst1.append(p) lst2.append(i) iteams = list(zip(lst1, lst2)) iteams.sort() for i in range(0, n): sum = sum + iteams[i][0] if sum <= d: lst3.append(iteams[i][1] + 1) cnt = cnt + 1 print(len(lst3)) print(*lst3)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Valera's lifelong ambition was to be a photographer, so he bought a new camera. Every day he got more and more clients asking for photos, and one day Valera needed a program that would determine the maximum number of people he can serve. The camera's memory is d megabytes. Valera's camera can take photos of high and low quality. One low quality photo takes a megabytes of memory, one high quality photo take b megabytes of memory. For unknown reasons, each client asks him to make several low quality photos and several high quality photos. More formally, the i-th client asks to make xi low quality photos and yi high quality photos. Valera wants to serve as many clients per day as possible, provided that they will be pleased with his work. To please the i-th client, Valera needs to give him everything he wants, that is, to make xi low quality photos and yi high quality photos. To make one low quality photo, the camera must have at least a megabytes of free memory space. Similarly, to make one high quality photo, the camera must have at least b megabytes of free memory space. Initially the camera's memory is empty. Valera also does not delete photos from the camera so that the camera's memory gradually fills up. Calculate the maximum number of clients Valera can successfully serve and print the numbers of these clients. Input The first line contains two integers n and d (1 ≀ n ≀ 105, 1 ≀ d ≀ 109) β€” the number of clients and the camera memory size, correspondingly. The second line contains two integers a and b (1 ≀ a ≀ b ≀ 104) β€” the size of one low quality photo and of one high quality photo, correspondingly. Next n lines describe the clients. The i-th line contains two integers xi and yi (0 ≀ xi, yi ≀ 105) β€” the number of low quality photos and high quality photos the i-th client wants, correspondingly. All numbers on all lines are separated by single spaces. Output On the first line print the answer to the problem β€” the maximum number of clients that Valera can successfully serve. Print on the second line the numbers of the client in any order. All numbers must be distinct. If there are multiple answers, print any of them. The clients are numbered starting with 1 in the order in which they are defined in the input data. Examples Input 3 10 2 3 1 4 2 1 1 0 Output 2 3 2 Input 3 6 6 6 1 1 1 0 1 0 Output 1 2
I = lambda: map(int, input().split()) R = range n, d = I() a, b = I() m = [] v = [] for i in R(n): x, y = I() v += [x * a + y * b] m += [i] m = sorted(m, key=lambda x: v[x]) for i in R(n): if v[m[i]] > d: n = i break d -= v[m[i]] print(n) for i in R(n): print(m[i] + 1, end=" ")
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR LIST BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR LIST VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER STRING
Valera's lifelong ambition was to be a photographer, so he bought a new camera. Every day he got more and more clients asking for photos, and one day Valera needed a program that would determine the maximum number of people he can serve. The camera's memory is d megabytes. Valera's camera can take photos of high and low quality. One low quality photo takes a megabytes of memory, one high quality photo take b megabytes of memory. For unknown reasons, each client asks him to make several low quality photos and several high quality photos. More formally, the i-th client asks to make xi low quality photos and yi high quality photos. Valera wants to serve as many clients per day as possible, provided that they will be pleased with his work. To please the i-th client, Valera needs to give him everything he wants, that is, to make xi low quality photos and yi high quality photos. To make one low quality photo, the camera must have at least a megabytes of free memory space. Similarly, to make one high quality photo, the camera must have at least b megabytes of free memory space. Initially the camera's memory is empty. Valera also does not delete photos from the camera so that the camera's memory gradually fills up. Calculate the maximum number of clients Valera can successfully serve and print the numbers of these clients. Input The first line contains two integers n and d (1 ≀ n ≀ 105, 1 ≀ d ≀ 109) β€” the number of clients and the camera memory size, correspondingly. The second line contains two integers a and b (1 ≀ a ≀ b ≀ 104) β€” the size of one low quality photo and of one high quality photo, correspondingly. Next n lines describe the clients. The i-th line contains two integers xi and yi (0 ≀ xi, yi ≀ 105) β€” the number of low quality photos and high quality photos the i-th client wants, correspondingly. All numbers on all lines are separated by single spaces. Output On the first line print the answer to the problem β€” the maximum number of clients that Valera can successfully serve. Print on the second line the numbers of the client in any order. All numbers must be distinct. If there are multiple answers, print any of them. The clients are numbered starting with 1 in the order in which they are defined in the input data. Examples Input 3 10 2 3 1 4 2 1 1 0 Output 2 3 2 Input 3 6 6 6 1 1 1 0 1 0 Output 1 2
n, d = [int(x) for x in input().split()] a, b = [int(x) for x in input().split()] arr = [] for _ in range(n): arr.append([int(x) for x in input().split()] + [_ + 1]) arr.sort(key=lambda x: a * x[0] + b * x[1]) ans = [] for i in arr: d -= a * i[0] + b * i[1] if d >= 0: ans.append(i[2]) else: d += a * i[0] + b * i[1] print(len(ans)) for i in ans: print(i, end=" ")
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR LIST BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Valera's lifelong ambition was to be a photographer, so he bought a new camera. Every day he got more and more clients asking for photos, and one day Valera needed a program that would determine the maximum number of people he can serve. The camera's memory is d megabytes. Valera's camera can take photos of high and low quality. One low quality photo takes a megabytes of memory, one high quality photo take b megabytes of memory. For unknown reasons, each client asks him to make several low quality photos and several high quality photos. More formally, the i-th client asks to make xi low quality photos and yi high quality photos. Valera wants to serve as many clients per day as possible, provided that they will be pleased with his work. To please the i-th client, Valera needs to give him everything he wants, that is, to make xi low quality photos and yi high quality photos. To make one low quality photo, the camera must have at least a megabytes of free memory space. Similarly, to make one high quality photo, the camera must have at least b megabytes of free memory space. Initially the camera's memory is empty. Valera also does not delete photos from the camera so that the camera's memory gradually fills up. Calculate the maximum number of clients Valera can successfully serve and print the numbers of these clients. Input The first line contains two integers n and d (1 ≀ n ≀ 105, 1 ≀ d ≀ 109) β€” the number of clients and the camera memory size, correspondingly. The second line contains two integers a and b (1 ≀ a ≀ b ≀ 104) β€” the size of one low quality photo and of one high quality photo, correspondingly. Next n lines describe the clients. The i-th line contains two integers xi and yi (0 ≀ xi, yi ≀ 105) β€” the number of low quality photos and high quality photos the i-th client wants, correspondingly. All numbers on all lines are separated by single spaces. Output On the first line print the answer to the problem β€” the maximum number of clients that Valera can successfully serve. Print on the second line the numbers of the client in any order. All numbers must be distinct. If there are multiple answers, print any of them. The clients are numbered starting with 1 in the order in which they are defined in the input data. Examples Input 3 10 2 3 1 4 2 1 1 0 Output 2 3 2 Input 3 6 6 6 1 1 1 0 1 0 Output 1 2
from sys import stdin def arr_inp(n): if n == 1: return [int(x) for x in stdin.readline().split()] elif n == 2: return [float(x) for x in stdin.readline().split()] else: return [str(x) for x in stdin.readline().split()] def arr_inp_enu(n): return [[i, arr_inp(1)] for i in range(1, n + 1)] def add(d): for i in range(n): arr[i][1][0] *= a arr[i][1][1] *= b arr.sort(key=lambda x: x[1][0] + x[1][1]) ans = [] for i in range(n): s = sum(arr[i][1]) if s <= d: d -= s ans.append(arr[i][0]) else: break print(len(ans)) print(*ans) n, d = arr_inp(1) a, b = arr_inp(1) arr = arr_inp_enu(n) add(d)
FUNC_DEF IF VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN LIST VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Valera's lifelong ambition was to be a photographer, so he bought a new camera. Every day he got more and more clients asking for photos, and one day Valera needed a program that would determine the maximum number of people he can serve. The camera's memory is d megabytes. Valera's camera can take photos of high and low quality. One low quality photo takes a megabytes of memory, one high quality photo take b megabytes of memory. For unknown reasons, each client asks him to make several low quality photos and several high quality photos. More formally, the i-th client asks to make xi low quality photos and yi high quality photos. Valera wants to serve as many clients per day as possible, provided that they will be pleased with his work. To please the i-th client, Valera needs to give him everything he wants, that is, to make xi low quality photos and yi high quality photos. To make one low quality photo, the camera must have at least a megabytes of free memory space. Similarly, to make one high quality photo, the camera must have at least b megabytes of free memory space. Initially the camera's memory is empty. Valera also does not delete photos from the camera so that the camera's memory gradually fills up. Calculate the maximum number of clients Valera can successfully serve and print the numbers of these clients. Input The first line contains two integers n and d (1 ≀ n ≀ 105, 1 ≀ d ≀ 109) β€” the number of clients and the camera memory size, correspondingly. The second line contains two integers a and b (1 ≀ a ≀ b ≀ 104) β€” the size of one low quality photo and of one high quality photo, correspondingly. Next n lines describe the clients. The i-th line contains two integers xi and yi (0 ≀ xi, yi ≀ 105) β€” the number of low quality photos and high quality photos the i-th client wants, correspondingly. All numbers on all lines are separated by single spaces. Output On the first line print the answer to the problem β€” the maximum number of clients that Valera can successfully serve. Print on the second line the numbers of the client in any order. All numbers must be distinct. If there are multiple answers, print any of them. The clients are numbered starting with 1 in the order in which they are defined in the input data. Examples Input 3 10 2 3 1 4 2 1 1 0 Output 2 3 2 Input 3 6 6 6 1 1 1 0 1 0 Output 1 2
n, d = list(map(int, input().split())) a, b = list(map(int, input().split())) res = [] for i in range(n): x, y = list(map(int, input().split())) res.append((x, y, i + 1)) res.sort(key=lambda x: a * x[0] + b * x[1]) output = [] for x, y, idx in res: if a * x + b * y <= d: d -= a * x + b * y output.append(idx) else: break print(len(output)) for i in output: print(i, end=" ") print()
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL 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 ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Valera's lifelong ambition was to be a photographer, so he bought a new camera. Every day he got more and more clients asking for photos, and one day Valera needed a program that would determine the maximum number of people he can serve. The camera's memory is d megabytes. Valera's camera can take photos of high and low quality. One low quality photo takes a megabytes of memory, one high quality photo take b megabytes of memory. For unknown reasons, each client asks him to make several low quality photos and several high quality photos. More formally, the i-th client asks to make xi low quality photos and yi high quality photos. Valera wants to serve as many clients per day as possible, provided that they will be pleased with his work. To please the i-th client, Valera needs to give him everything he wants, that is, to make xi low quality photos and yi high quality photos. To make one low quality photo, the camera must have at least a megabytes of free memory space. Similarly, to make one high quality photo, the camera must have at least b megabytes of free memory space. Initially the camera's memory is empty. Valera also does not delete photos from the camera so that the camera's memory gradually fills up. Calculate the maximum number of clients Valera can successfully serve and print the numbers of these clients. Input The first line contains two integers n and d (1 ≀ n ≀ 105, 1 ≀ d ≀ 109) β€” the number of clients and the camera memory size, correspondingly. The second line contains two integers a and b (1 ≀ a ≀ b ≀ 104) β€” the size of one low quality photo and of one high quality photo, correspondingly. Next n lines describe the clients. The i-th line contains two integers xi and yi (0 ≀ xi, yi ≀ 105) β€” the number of low quality photos and high quality photos the i-th client wants, correspondingly. All numbers on all lines are separated by single spaces. Output On the first line print the answer to the problem β€” the maximum number of clients that Valera can successfully serve. Print on the second line the numbers of the client in any order. All numbers must be distinct. If there are multiple answers, print any of them. The clients are numbered starting with 1 in the order in which they are defined in the input data. Examples Input 3 10 2 3 1 4 2 1 1 0 Output 2 3 2 Input 3 6 6 6 1 1 1 0 1 0 Output 1 2
n, d = map(int, input().split()) a, b = map(int, input().split()) s = [None] * n for i in range(n): x, y = map(int, input().split()) s[i] = [a * x + b * y, i + 1] s.sort() res = [] for i in range(n): if d - s[i][0] >= 0: res.append(s[i][1]) d -= s[i][0] else: break print(str(len(res)) + "\n" + " ".join(map(str, res)))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Valera's lifelong ambition was to be a photographer, so he bought a new camera. Every day he got more and more clients asking for photos, and one day Valera needed a program that would determine the maximum number of people he can serve. The camera's memory is d megabytes. Valera's camera can take photos of high and low quality. One low quality photo takes a megabytes of memory, one high quality photo take b megabytes of memory. For unknown reasons, each client asks him to make several low quality photos and several high quality photos. More formally, the i-th client asks to make xi low quality photos and yi high quality photos. Valera wants to serve as many clients per day as possible, provided that they will be pleased with his work. To please the i-th client, Valera needs to give him everything he wants, that is, to make xi low quality photos and yi high quality photos. To make one low quality photo, the camera must have at least a megabytes of free memory space. Similarly, to make one high quality photo, the camera must have at least b megabytes of free memory space. Initially the camera's memory is empty. Valera also does not delete photos from the camera so that the camera's memory gradually fills up. Calculate the maximum number of clients Valera can successfully serve and print the numbers of these clients. Input The first line contains two integers n and d (1 ≀ n ≀ 105, 1 ≀ d ≀ 109) β€” the number of clients and the camera memory size, correspondingly. The second line contains two integers a and b (1 ≀ a ≀ b ≀ 104) β€” the size of one low quality photo and of one high quality photo, correspondingly. Next n lines describe the clients. The i-th line contains two integers xi and yi (0 ≀ xi, yi ≀ 105) β€” the number of low quality photos and high quality photos the i-th client wants, correspondingly. All numbers on all lines are separated by single spaces. Output On the first line print the answer to the problem β€” the maximum number of clients that Valera can successfully serve. Print on the second line the numbers of the client in any order. All numbers must be distinct. If there are multiple answers, print any of them. The clients are numbered starting with 1 in the order in which they are defined in the input data. Examples Input 3 10 2 3 1 4 2 1 1 0 Output 2 3 2 Input 3 6 6 6 1 1 1 0 1 0 Output 1 2
n, d = map(int, input().split()) lis = [] aa = [] m1, m2 = map(int, input().split()) for i in range(n): a, b = map(int, input().split()) lis.append([m1 * a + m2 * b, i + 1]) lis.sort() ans = 0 for i in range(n): if lis[i][0] + ans <= d: ans += lis[i][0] aa.append(lis[i][1]) print(len(aa)) print(*aa)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL 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 LIST BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Valera's lifelong ambition was to be a photographer, so he bought a new camera. Every day he got more and more clients asking for photos, and one day Valera needed a program that would determine the maximum number of people he can serve. The camera's memory is d megabytes. Valera's camera can take photos of high and low quality. One low quality photo takes a megabytes of memory, one high quality photo take b megabytes of memory. For unknown reasons, each client asks him to make several low quality photos and several high quality photos. More formally, the i-th client asks to make xi low quality photos and yi high quality photos. Valera wants to serve as many clients per day as possible, provided that they will be pleased with his work. To please the i-th client, Valera needs to give him everything he wants, that is, to make xi low quality photos and yi high quality photos. To make one low quality photo, the camera must have at least a megabytes of free memory space. Similarly, to make one high quality photo, the camera must have at least b megabytes of free memory space. Initially the camera's memory is empty. Valera also does not delete photos from the camera so that the camera's memory gradually fills up. Calculate the maximum number of clients Valera can successfully serve and print the numbers of these clients. Input The first line contains two integers n and d (1 ≀ n ≀ 105, 1 ≀ d ≀ 109) β€” the number of clients and the camera memory size, correspondingly. The second line contains two integers a and b (1 ≀ a ≀ b ≀ 104) β€” the size of one low quality photo and of one high quality photo, correspondingly. Next n lines describe the clients. The i-th line contains two integers xi and yi (0 ≀ xi, yi ≀ 105) β€” the number of low quality photos and high quality photos the i-th client wants, correspondingly. All numbers on all lines are separated by single spaces. Output On the first line print the answer to the problem β€” the maximum number of clients that Valera can successfully serve. Print on the second line the numbers of the client in any order. All numbers must be distinct. If there are multiple answers, print any of them. The clients are numbered starting with 1 in the order in which they are defined in the input data. Examples Input 3 10 2 3 1 4 2 1 1 0 Output 2 3 2 Input 3 6 6 6 1 1 1 0 1 0 Output 1 2
n, d = map(int, input().split()) a, b = map(int, input().split()) req = [] for i in range(n): x, y = map(int, input().split()) req.append([x, y, i]) ans = [] req.sort(key=lambda x: [x[0] * a + x[1] * b]) for i in range(n): curr = req[i][0] * a + req[i][1] * b if d >= curr: ans.append(req[i][2] + 1) d -= curr else: break print(len(ans)) print(*ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL 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 VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR LIST BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Valera's lifelong ambition was to be a photographer, so he bought a new camera. Every day he got more and more clients asking for photos, and one day Valera needed a program that would determine the maximum number of people he can serve. The camera's memory is d megabytes. Valera's camera can take photos of high and low quality. One low quality photo takes a megabytes of memory, one high quality photo take b megabytes of memory. For unknown reasons, each client asks him to make several low quality photos and several high quality photos. More formally, the i-th client asks to make xi low quality photos and yi high quality photos. Valera wants to serve as many clients per day as possible, provided that they will be pleased with his work. To please the i-th client, Valera needs to give him everything he wants, that is, to make xi low quality photos and yi high quality photos. To make one low quality photo, the camera must have at least a megabytes of free memory space. Similarly, to make one high quality photo, the camera must have at least b megabytes of free memory space. Initially the camera's memory is empty. Valera also does not delete photos from the camera so that the camera's memory gradually fills up. Calculate the maximum number of clients Valera can successfully serve and print the numbers of these clients. Input The first line contains two integers n and d (1 ≀ n ≀ 105, 1 ≀ d ≀ 109) β€” the number of clients and the camera memory size, correspondingly. The second line contains two integers a and b (1 ≀ a ≀ b ≀ 104) β€” the size of one low quality photo and of one high quality photo, correspondingly. Next n lines describe the clients. The i-th line contains two integers xi and yi (0 ≀ xi, yi ≀ 105) β€” the number of low quality photos and high quality photos the i-th client wants, correspondingly. All numbers on all lines are separated by single spaces. Output On the first line print the answer to the problem β€” the maximum number of clients that Valera can successfully serve. Print on the second line the numbers of the client in any order. All numbers must be distinct. If there are multiple answers, print any of them. The clients are numbered starting with 1 in the order in which they are defined in the input data. Examples Input 3 10 2 3 1 4 2 1 1 0 Output 2 3 2 Input 3 6 6 6 1 1 1 0 1 0 Output 1 2
n, d = map(int, input().split()) a, b = map(int, input().split()) ans = [] for i in range(n): x, y = map(int, input().split()) ans.append([x * a + y * b, i + 1]) ans.sort() nans = [] for i in range(len(ans)): if d >= ans[i][0]: nans.append(ans[i][1]) d = d - ans[i][0] else: break print(len(nans)) print(*nans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL 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 BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Valera's lifelong ambition was to be a photographer, so he bought a new camera. Every day he got more and more clients asking for photos, and one day Valera needed a program that would determine the maximum number of people he can serve. The camera's memory is d megabytes. Valera's camera can take photos of high and low quality. One low quality photo takes a megabytes of memory, one high quality photo take b megabytes of memory. For unknown reasons, each client asks him to make several low quality photos and several high quality photos. More formally, the i-th client asks to make xi low quality photos and yi high quality photos. Valera wants to serve as many clients per day as possible, provided that they will be pleased with his work. To please the i-th client, Valera needs to give him everything he wants, that is, to make xi low quality photos and yi high quality photos. To make one low quality photo, the camera must have at least a megabytes of free memory space. Similarly, to make one high quality photo, the camera must have at least b megabytes of free memory space. Initially the camera's memory is empty. Valera also does not delete photos from the camera so that the camera's memory gradually fills up. Calculate the maximum number of clients Valera can successfully serve and print the numbers of these clients. Input The first line contains two integers n and d (1 ≀ n ≀ 105, 1 ≀ d ≀ 109) β€” the number of clients and the camera memory size, correspondingly. The second line contains two integers a and b (1 ≀ a ≀ b ≀ 104) β€” the size of one low quality photo and of one high quality photo, correspondingly. Next n lines describe the clients. The i-th line contains two integers xi and yi (0 ≀ xi, yi ≀ 105) β€” the number of low quality photos and high quality photos the i-th client wants, correspondingly. All numbers on all lines are separated by single spaces. Output On the first line print the answer to the problem β€” the maximum number of clients that Valera can successfully serve. Print on the second line the numbers of the client in any order. All numbers must be distinct. If there are multiple answers, print any of them. The clients are numbered starting with 1 in the order in which they are defined in the input data. Examples Input 3 10 2 3 1 4 2 1 1 0 Output 2 3 2 Input 3 6 6 6 1 1 1 0 1 0 Output 1 2
inp = [int(x) for x in input().split()] n = inp[0] d = inp[1] size = [int(x) for x in input().split()] lowSize = size[0] highSize = size[1] sizeByCustomer = [] for i in range(n): photosCount = [int(x) for x in input().split()] photosLowCount = photosCount[0] photosHighCount = photosCount[1] totalSize = photosLowCount * lowSize + photosHighCount * highSize sizeByCustomer.append([totalSize, i + 1]) sizeByCustomer.sort(key=lambda x: x[0]) canContinue = True i = 0 result = [] while canContinue and i < n: if d - sizeByCustomer[i][0] >= 0: result.append(str(sizeByCustomer[i][1])) d -= sizeByCustomer[i][0] i += 1 else: canContinue = False print(len(result)) print(" ".join(result))
ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR VAR IF BIN_OP VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Valera's lifelong ambition was to be a photographer, so he bought a new camera. Every day he got more and more clients asking for photos, and one day Valera needed a program that would determine the maximum number of people he can serve. The camera's memory is d megabytes. Valera's camera can take photos of high and low quality. One low quality photo takes a megabytes of memory, one high quality photo take b megabytes of memory. For unknown reasons, each client asks him to make several low quality photos and several high quality photos. More formally, the i-th client asks to make xi low quality photos and yi high quality photos. Valera wants to serve as many clients per day as possible, provided that they will be pleased with his work. To please the i-th client, Valera needs to give him everything he wants, that is, to make xi low quality photos and yi high quality photos. To make one low quality photo, the camera must have at least a megabytes of free memory space. Similarly, to make one high quality photo, the camera must have at least b megabytes of free memory space. Initially the camera's memory is empty. Valera also does not delete photos from the camera so that the camera's memory gradually fills up. Calculate the maximum number of clients Valera can successfully serve and print the numbers of these clients. Input The first line contains two integers n and d (1 ≀ n ≀ 105, 1 ≀ d ≀ 109) β€” the number of clients and the camera memory size, correspondingly. The second line contains two integers a and b (1 ≀ a ≀ b ≀ 104) β€” the size of one low quality photo and of one high quality photo, correspondingly. Next n lines describe the clients. The i-th line contains two integers xi and yi (0 ≀ xi, yi ≀ 105) β€” the number of low quality photos and high quality photos the i-th client wants, correspondingly. All numbers on all lines are separated by single spaces. Output On the first line print the answer to the problem β€” the maximum number of clients that Valera can successfully serve. Print on the second line the numbers of the client in any order. All numbers must be distinct. If there are multiple answers, print any of them. The clients are numbered starting with 1 in the order in which they are defined in the input data. Examples Input 3 10 2 3 1 4 2 1 1 0 Output 2 3 2 Input 3 6 6 6 1 1 1 0 1 0 Output 1 2
lst = list(map(int, input().split())) n = lst[0] d = lst[1] lst = list(map(int, input().split())) a = lst[0] b = lst[1] ans = [] for x in range(0, n): lst = list(map(int, input().split())) ans.append([lst[0] * a + lst[1] * b, x + 1]) ans.sort() sum = 0 i = 0 for s in range(0, n): i += 1 sum += ans[s][0] if sum > d: i -= 1 sum -= ans[s][0] break print(i) for f in range(0, i): print(ans[f][1], " ", end="")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER STRING STRING
Valera's lifelong ambition was to be a photographer, so he bought a new camera. Every day he got more and more clients asking for photos, and one day Valera needed a program that would determine the maximum number of people he can serve. The camera's memory is d megabytes. Valera's camera can take photos of high and low quality. One low quality photo takes a megabytes of memory, one high quality photo take b megabytes of memory. For unknown reasons, each client asks him to make several low quality photos and several high quality photos. More formally, the i-th client asks to make xi low quality photos and yi high quality photos. Valera wants to serve as many clients per day as possible, provided that they will be pleased with his work. To please the i-th client, Valera needs to give him everything he wants, that is, to make xi low quality photos and yi high quality photos. To make one low quality photo, the camera must have at least a megabytes of free memory space. Similarly, to make one high quality photo, the camera must have at least b megabytes of free memory space. Initially the camera's memory is empty. Valera also does not delete photos from the camera so that the camera's memory gradually fills up. Calculate the maximum number of clients Valera can successfully serve and print the numbers of these clients. Input The first line contains two integers n and d (1 ≀ n ≀ 105, 1 ≀ d ≀ 109) β€” the number of clients and the camera memory size, correspondingly. The second line contains two integers a and b (1 ≀ a ≀ b ≀ 104) β€” the size of one low quality photo and of one high quality photo, correspondingly. Next n lines describe the clients. The i-th line contains two integers xi and yi (0 ≀ xi, yi ≀ 105) β€” the number of low quality photos and high quality photos the i-th client wants, correspondingly. All numbers on all lines are separated by single spaces. Output On the first line print the answer to the problem β€” the maximum number of clients that Valera can successfully serve. Print on the second line the numbers of the client in any order. All numbers must be distinct. If there are multiple answers, print any of them. The clients are numbered starting with 1 in the order in which they are defined in the input data. Examples Input 3 10 2 3 1 4 2 1 1 0 Output 2 3 2 Input 3 6 6 6 1 1 1 0 1 0 Output 1 2
n, d = [int(i) for i in input().split()] a, b = [int(i) for i in input().split()] arr, res = [], [] for j in range(n): x, y = [int(i) for i in input().split()] arr.append([x * a + y * b, j]) arr.sort() sum = 0 for i in range(n): sum = sum + arr[i][0] if sum > d: break res.append(arr[i][1] + 1) print(len(res)) for i in res: print(i, end=" ") print("")
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING
Valera's lifelong ambition was to be a photographer, so he bought a new camera. Every day he got more and more clients asking for photos, and one day Valera needed a program that would determine the maximum number of people he can serve. The camera's memory is d megabytes. Valera's camera can take photos of high and low quality. One low quality photo takes a megabytes of memory, one high quality photo take b megabytes of memory. For unknown reasons, each client asks him to make several low quality photos and several high quality photos. More formally, the i-th client asks to make xi low quality photos and yi high quality photos. Valera wants to serve as many clients per day as possible, provided that they will be pleased with his work. To please the i-th client, Valera needs to give him everything he wants, that is, to make xi low quality photos and yi high quality photos. To make one low quality photo, the camera must have at least a megabytes of free memory space. Similarly, to make one high quality photo, the camera must have at least b megabytes of free memory space. Initially the camera's memory is empty. Valera also does not delete photos from the camera so that the camera's memory gradually fills up. Calculate the maximum number of clients Valera can successfully serve and print the numbers of these clients. Input The first line contains two integers n and d (1 ≀ n ≀ 105, 1 ≀ d ≀ 109) β€” the number of clients and the camera memory size, correspondingly. The second line contains two integers a and b (1 ≀ a ≀ b ≀ 104) β€” the size of one low quality photo and of one high quality photo, correspondingly. Next n lines describe the clients. The i-th line contains two integers xi and yi (0 ≀ xi, yi ≀ 105) β€” the number of low quality photos and high quality photos the i-th client wants, correspondingly. All numbers on all lines are separated by single spaces. Output On the first line print the answer to the problem β€” the maximum number of clients that Valera can successfully serve. Print on the second line the numbers of the client in any order. All numbers must be distinct. If there are multiple answers, print any of them. The clients are numbered starting with 1 in the order in which they are defined in the input data. Examples Input 3 10 2 3 1 4 2 1 1 0 Output 2 3 2 Input 3 6 6 6 1 1 1 0 1 0 Output 1 2
n, d = map(int, input().split()) a, b = map(int, input().split()) clients = [] for i in range(n): x_i, y_i = map(int, input().split()) clients.append({"number": i + 1, "size": x_i * a + y_i * b}) clients = sorted(clients, key=lambda x: x["size"]) sum = 0 cnt = len(clients) for i in range(len(clients)): sum += clients[i]["size"] if sum > d: cnt = i break print(cnt) for i in range(cnt): print(clients[i]["number"], end=" ")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL 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 DICT STRING STRING BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR STRING IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING STRING
Valera's lifelong ambition was to be a photographer, so he bought a new camera. Every day he got more and more clients asking for photos, and one day Valera needed a program that would determine the maximum number of people he can serve. The camera's memory is d megabytes. Valera's camera can take photos of high and low quality. One low quality photo takes a megabytes of memory, one high quality photo take b megabytes of memory. For unknown reasons, each client asks him to make several low quality photos and several high quality photos. More formally, the i-th client asks to make xi low quality photos and yi high quality photos. Valera wants to serve as many clients per day as possible, provided that they will be pleased with his work. To please the i-th client, Valera needs to give him everything he wants, that is, to make xi low quality photos and yi high quality photos. To make one low quality photo, the camera must have at least a megabytes of free memory space. Similarly, to make one high quality photo, the camera must have at least b megabytes of free memory space. Initially the camera's memory is empty. Valera also does not delete photos from the camera so that the camera's memory gradually fills up. Calculate the maximum number of clients Valera can successfully serve and print the numbers of these clients. Input The first line contains two integers n and d (1 ≀ n ≀ 105, 1 ≀ d ≀ 109) β€” the number of clients and the camera memory size, correspondingly. The second line contains two integers a and b (1 ≀ a ≀ b ≀ 104) β€” the size of one low quality photo and of one high quality photo, correspondingly. Next n lines describe the clients. The i-th line contains two integers xi and yi (0 ≀ xi, yi ≀ 105) β€” the number of low quality photos and high quality photos the i-th client wants, correspondingly. All numbers on all lines are separated by single spaces. Output On the first line print the answer to the problem β€” the maximum number of clients that Valera can successfully serve. Print on the second line the numbers of the client in any order. All numbers must be distinct. If there are multiple answers, print any of them. The clients are numbered starting with 1 in the order in which they are defined in the input data. Examples Input 3 10 2 3 1 4 2 1 1 0 Output 2 3 2 Input 3 6 6 6 1 1 1 0 1 0 Output 1 2
n, d = map(int, input().split()) d1, d2 = map(int, input().split()) datos = [] for i in range(n): a, b = map(int, input().split()) datos.append((a * d1 + b * d2, i + 1)) datos = sorted(datos) posiciones = "" cont = 0 acum = 0 res = [] for x in datos: if x[0] <= d: res.append(x[1]) d -= x[0] else: break print(len(res)) print(" ".join(map(str, res)))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL 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 BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Valera's lifelong ambition was to be a photographer, so he bought a new camera. Every day he got more and more clients asking for photos, and one day Valera needed a program that would determine the maximum number of people he can serve. The camera's memory is d megabytes. Valera's camera can take photos of high and low quality. One low quality photo takes a megabytes of memory, one high quality photo take b megabytes of memory. For unknown reasons, each client asks him to make several low quality photos and several high quality photos. More formally, the i-th client asks to make xi low quality photos and yi high quality photos. Valera wants to serve as many clients per day as possible, provided that they will be pleased with his work. To please the i-th client, Valera needs to give him everything he wants, that is, to make xi low quality photos and yi high quality photos. To make one low quality photo, the camera must have at least a megabytes of free memory space. Similarly, to make one high quality photo, the camera must have at least b megabytes of free memory space. Initially the camera's memory is empty. Valera also does not delete photos from the camera so that the camera's memory gradually fills up. Calculate the maximum number of clients Valera can successfully serve and print the numbers of these clients. Input The first line contains two integers n and d (1 ≀ n ≀ 105, 1 ≀ d ≀ 109) β€” the number of clients and the camera memory size, correspondingly. The second line contains two integers a and b (1 ≀ a ≀ b ≀ 104) β€” the size of one low quality photo and of one high quality photo, correspondingly. Next n lines describe the clients. The i-th line contains two integers xi and yi (0 ≀ xi, yi ≀ 105) β€” the number of low quality photos and high quality photos the i-th client wants, correspondingly. All numbers on all lines are separated by single spaces. Output On the first line print the answer to the problem β€” the maximum number of clients that Valera can successfully serve. Print on the second line the numbers of the client in any order. All numbers must be distinct. If there are multiple answers, print any of them. The clients are numbered starting with 1 in the order in which they are defined in the input data. Examples Input 3 10 2 3 1 4 2 1 1 0 Output 2 3 2 Input 3 6 6 6 1 1 1 0 1 0 Output 1 2
master = [] n, total = map(int, input().split()) a, b = map(int, input().split()) for t in range(1, n + 1): one, two = map(int, input().split()) one = one * a two = two * b summ = one + two master.append((t, summ)) final = [] master.sort(key=lambda x: x[1]) empty = 0 for t in master: empty += t[1] if empty > total: break else: final.append(t[0]) print(len(final)) k = " ".join(map(str, final)) print(k)
ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR