description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β€” "PriceFixed". Here are some rules of that store: The store has an infinite number of items of every product. All products have the same price: $2$ rubles per item. For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!). Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β€” the number of products. Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β€” the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product. The sum of all $a_i$ does not exceed $10^{14}$. -----Output----- Output the minimum sum that Lena needs to make all purchases. -----Examples----- Input 3 3 4 1 3 1 5 Output 8 Input 5 2 7 2 8 1 2 2 4 1 8 Output 12 -----Note----- In the first example, Lena can purchase the products in the following way: one item of product $3$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased), one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased). In total, she spends $8$ rubles. It can be proved that it is impossible to spend less. In the second example Lena can purchase the products in the following way: one item of product $1$ for $2$ rubles, two items of product $2$ for $2$ rubles for each, one item of product $5$ for $2$ rubles, one item of product $3$ for $1$ ruble, two items of product $4$ for $1$ ruble for each, one item of product $1$ for $1$ ruble. In total, she spends $12$ rubles.
R = lambda: map(int, input().split()) n = int(input()) arr = [list(R()) for i in range(n)] arr.sort(key=lambda x: (-x[1], x[0])) toto = tot = sum(x[0] for x in arr) acc = 0 for req, thr in arr: if tot > thr: taken = min(req, tot - thr) tot -= taken acc += taken print(toto * 2 - acc)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β€” "PriceFixed". Here are some rules of that store: The store has an infinite number of items of every product. All products have the same price: $2$ rubles per item. For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!). Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β€” the number of products. Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β€” the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product. The sum of all $a_i$ does not exceed $10^{14}$. -----Output----- Output the minimum sum that Lena needs to make all purchases. -----Examples----- Input 3 3 4 1 3 1 5 Output 8 Input 5 2 7 2 8 1 2 2 4 1 8 Output 12 -----Note----- In the first example, Lena can purchase the products in the following way: one item of product $3$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased), one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased). In total, she spends $8$ rubles. It can be proved that it is impossible to spend less. In the second example Lena can purchase the products in the following way: one item of product $1$ for $2$ rubles, two items of product $2$ for $2$ rubles for each, one item of product $5$ for $2$ rubles, one item of product $3$ for $1$ ruble, two items of product $4$ for $1$ ruble for each, one item of product $1$ for $1$ ruble. In total, she spends $12$ rubles.
n = int(input()) A = [] for i in range(n): a, b = map(int, input().split()) A.append((b, a)) A.sort() l = 0 r = n - 1 c = 0 res = 0 while l <= r: if c < A[l][0]: x = A[l][0] - c if x < A[r][1]: res += x * 2 c += x A[r] = A[r][0], A[r][1] - x else: res += A[r][1] * 2 c += A[r][1] r -= 1 else: res += A[l][1] c += A[l][1] l += 1 print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR IF VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β€” "PriceFixed". Here are some rules of that store: The store has an infinite number of items of every product. All products have the same price: $2$ rubles per item. For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!). Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β€” the number of products. Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β€” the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product. The sum of all $a_i$ does not exceed $10^{14}$. -----Output----- Output the minimum sum that Lena needs to make all purchases. -----Examples----- Input 3 3 4 1 3 1 5 Output 8 Input 5 2 7 2 8 1 2 2 4 1 8 Output 12 -----Note----- In the first example, Lena can purchase the products in the following way: one item of product $3$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased), one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased). In total, she spends $8$ rubles. It can be proved that it is impossible to spend less. In the second example Lena can purchase the products in the following way: one item of product $1$ for $2$ rubles, two items of product $2$ for $2$ rubles for each, one item of product $5$ for $2$ rubles, one item of product $3$ for $1$ ruble, two items of product $4$ for $1$ ruble for each, one item of product $1$ for $1$ ruble. In total, she spends $12$ rubles.
price = 2 discounted_price = 1 class sortable: def __init__(self, necessary, qfdp) -> None: self.necessary = necessary self.quantity_for_discounted_price = qfdp def __lt__(self, other): return self.quantity_for_discounted_price < other.quantity_for_discounted_price def buy(products, quantity, i, rightmost): while quantity > 0: if rightmost == i: break if quantity > products[rightmost].necessary: quantity -= products[rightmost].necessary products[rightmost].necessary = 0 rightmost -= 1 else: products[rightmost].necessary -= quantity quantity = 0 assert rightmost >= i return rightmost, quantity def solve(): n = int(input()) products = [0] * n for i in range(n): necessary_quantity, discounted_quantity = [int(x) for x in input().split()] products[i] = sortable(necessary_quantity, discounted_quantity) products.sort() bought, paid, r = 0, 0, n - 1 for i in range(n): assert paid >= bought if products[i].necessary == 0: continue if bought >= products[i].quantity_for_discounted_price: paid += discounted_price * products[i].necessary else: r, left = buy( products, products[i].quantity_for_discounted_price - bought, i, r ) paid += price * (products[i].quantity_for_discounted_price - bought - left) bought += products[i].quantity_for_discounted_price - bought - left if r > i: assert left == 0 paid += discounted_price * products[i].necessary if r == i: if ( products[i].necessary + bought < products[i].quantity_for_discounted_price ): paid += price * products[i].necessary else: paid += price * (products[i].quantity_for_discounted_price - bought) paid += discounted_price * ( products[i].necessary + bought - products[i].quantity_for_discounted_price ) bought += products[i].necessary products[i].necessary = 0 return paid print(solve())
ASSIGN VAR NUMBER ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR NONE FUNC_DEF RETURN VAR VAR FUNC_DEF WHILE VAR NUMBER IF VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR IF VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β€” "PriceFixed". Here are some rules of that store: The store has an infinite number of items of every product. All products have the same price: $2$ rubles per item. For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!). Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β€” the number of products. Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β€” the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product. The sum of all $a_i$ does not exceed $10^{14}$. -----Output----- Output the minimum sum that Lena needs to make all purchases. -----Examples----- Input 3 3 4 1 3 1 5 Output 8 Input 5 2 7 2 8 1 2 2 4 1 8 Output 12 -----Note----- In the first example, Lena can purchase the products in the following way: one item of product $3$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased), one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased). In total, she spends $8$ rubles. It can be proved that it is impossible to spend less. In the second example Lena can purchase the products in the following way: one item of product $1$ for $2$ rubles, two items of product $2$ for $2$ rubles for each, one item of product $5$ for $2$ rubles, one item of product $3$ for $1$ ruble, two items of product $4$ for $1$ ruble for each, one item of product $1$ for $1$ ruble. In total, she spends $12$ rubles.
n = int(input()) l = [] needed = 0 for _ in range(n): a, b = map(int, input().split()) needed += a l.append([b, a]) l.sort() items, total = 0, 0 i, j = 0, n - 1 aa = [] bb = [] for a, b in l: aa.append(a) bb.append(b) while items < needed: if aa[i] <= items: buy = bb[i] bb[i] = 0 items += buy total += buy i += 1 elif bb[j] >= aa[i] - items: buy = aa[i] - items bb[j] -= buy items += buy total += buy * 2 if bb[j] == 0: j -= 1 else: buy = bb[j] bb[j] = 0 items += buy total += buy * 2 j -= 1 print(total)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR 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 EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β€” "PriceFixed". Here are some rules of that store: The store has an infinite number of items of every product. All products have the same price: $2$ rubles per item. For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!). Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β€” the number of products. Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β€” the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product. The sum of all $a_i$ does not exceed $10^{14}$. -----Output----- Output the minimum sum that Lena needs to make all purchases. -----Examples----- Input 3 3 4 1 3 1 5 Output 8 Input 5 2 7 2 8 1 2 2 4 1 8 Output 12 -----Note----- In the first example, Lena can purchase the products in the following way: one item of product $3$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased), one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased). In total, she spends $8$ rubles. It can be proved that it is impossible to spend less. In the second example Lena can purchase the products in the following way: one item of product $1$ for $2$ rubles, two items of product $2$ for $2$ rubles for each, one item of product $5$ for $2$ rubles, one item of product $3$ for $1$ ruble, two items of product $4$ for $1$ ruble for each, one item of product $1$ for $1$ ruble. In total, she spends $12$ rubles.
n = int(input()) item = [] for i in range(n): a, b = map(int, input().split()) item.append([a, b]) item.sort(key=lambda x: x[1]) i = 0 j = n - 1 count = 0 ans = 0 while i <= j: if item[i][1] <= count: temp = item[i][0] item[i][0] = 0 ans += temp count += temp i += 1 elif item[i][1] - count <= item[j][0]: temp = item[i][1] - count item[j][0] -= temp count += temp ans += temp * 2 temp = item[i][0] item[i][0] = 0 ans += temp count += temp i += 1 else: temp = item[j][0] item[j][0] = 0 count += temp ans += temp * 2 j -= 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β€” "PriceFixed". Here are some rules of that store: The store has an infinite number of items of every product. All products have the same price: $2$ rubles per item. For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!). Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β€” the number of products. Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β€” the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product. The sum of all $a_i$ does not exceed $10^{14}$. -----Output----- Output the minimum sum that Lena needs to make all purchases. -----Examples----- Input 3 3 4 1 3 1 5 Output 8 Input 5 2 7 2 8 1 2 2 4 1 8 Output 12 -----Note----- In the first example, Lena can purchase the products in the following way: one item of product $3$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased), one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased). In total, she spends $8$ rubles. It can be proved that it is impossible to spend less. In the second example Lena can purchase the products in the following way: one item of product $1$ for $2$ rubles, two items of product $2$ for $2$ rubles for each, one item of product $5$ for $2$ rubles, one item of product $3$ for $1$ ruble, two items of product $4$ for $1$ ruble for each, one item of product $1$ for $1$ ruble. In total, she spends $12$ rubles.
n = int(input()) lists = [] for i in range(n): a, req = list(map(int, input().split())) lists.append([a, req]) def last(n): return n[1] qua = sorted(lists, key=last) low = 0 high = n - 1 ans = 0 bought = 0 while low <= high: if qua[low][-1] <= bought: ans += qua[low][0] bought += qua[low][0] low += 1 else: req = min(qua[low][-1] - bought, qua[high][0]) bought += req ans += 2 * req qua[high][0] -= req if qua[high][0] == 0: high -= 1 print(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 VAR VAR FUNC_DEF RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR BIN_OP NUMBER VAR VAR VAR NUMBER VAR IF VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β€” "PriceFixed". Here are some rules of that store: The store has an infinite number of items of every product. All products have the same price: $2$ rubles per item. For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!). Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β€” the number of products. Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β€” the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product. The sum of all $a_i$ does not exceed $10^{14}$. -----Output----- Output the minimum sum that Lena needs to make all purchases. -----Examples----- Input 3 3 4 1 3 1 5 Output 8 Input 5 2 7 2 8 1 2 2 4 1 8 Output 12 -----Note----- In the first example, Lena can purchase the products in the following way: one item of product $3$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased), one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased). In total, she spends $8$ rubles. It can be proved that it is impossible to spend less. In the second example Lena can purchase the products in the following way: one item of product $1$ for $2$ rubles, two items of product $2$ for $2$ rubles for each, one item of product $5$ for $2$ rubles, one item of product $3$ for $1$ ruble, two items of product $4$ for $1$ ruble for each, one item of product $1$ for $1$ ruble. In total, she spends $12$ rubles.
n = int(input()) li = [] for i in range(n): a, b = map(int, input().split()) li.append((b, a)) li.sort() cost = 0 nitems = 0 while len(li) > 0: if li[0][0] <= nitems: cost += li[0][1] nitems += li[0][1] del li[0] elif li[0][0] >= nitems + li[-1][1]: cost += 2 * li[-1][1] nitems += li[-1][1] del li[-1] else: cost += 2 * (li[0][0] - nitems) li[-1] = li[-1][0], li[-1][1] - li[0][0] + nitems nitems += li[0][0] - nitems print(cost)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP NUMBER VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β€” "PriceFixed". Here are some rules of that store: The store has an infinite number of items of every product. All products have the same price: $2$ rubles per item. For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!). Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β€” the number of products. Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β€” the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product. The sum of all $a_i$ does not exceed $10^{14}$. -----Output----- Output the minimum sum that Lena needs to make all purchases. -----Examples----- Input 3 3 4 1 3 1 5 Output 8 Input 5 2 7 2 8 1 2 2 4 1 8 Output 12 -----Note----- In the first example, Lena can purchase the products in the following way: one item of product $3$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased), one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased). In total, she spends $8$ rubles. It can be proved that it is impossible to spend less. In the second example Lena can purchase the products in the following way: one item of product $1$ for $2$ rubles, two items of product $2$ for $2$ rubles for each, one item of product $5$ for $2$ rubles, one item of product $3$ for $1$ ruble, two items of product $4$ for $1$ ruble for each, one item of product $1$ for $1$ ruble. In total, she spends $12$ rubles.
n = int(input()) a = [0] * n b = [0] * n bb = [] for i in range(n): a[i], b[i] = map(int, input().split()) bb.append((i, a[i], b[i])) bb.sort(key=lambda x: x[2]) tot = sum(a) lg = n - 1 rm = bb[lg][1] bought = 0 while rm == 0: lg -= 1 rm = bb[lg][1] res = 0 fg = 0 while bb[fg][1] == 0: fg += 1 while bought < tot: lim = bb[fg][2] while bought < lim and bought < tot: q = min(rm, lim - bought) rm -= q bought += q res += 2 * q while rm == 0 and bought < tot: lg -= 1 rm = bb[lg][1] if lim <= bought < tot: if fg == lg: q = rm else: q = bb[fg][1] fg += 1 bought += q res += q print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP NUMBER VAR WHILE VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β€” "PriceFixed". Here are some rules of that store: The store has an infinite number of items of every product. All products have the same price: $2$ rubles per item. For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!). Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β€” the number of products. Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β€” the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product. The sum of all $a_i$ does not exceed $10^{14}$. -----Output----- Output the minimum sum that Lena needs to make all purchases. -----Examples----- Input 3 3 4 1 3 1 5 Output 8 Input 5 2 7 2 8 1 2 2 4 1 8 Output 12 -----Note----- In the first example, Lena can purchase the products in the following way: one item of product $3$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased), one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased). In total, she spends $8$ rubles. It can be proved that it is impossible to spend less. In the second example Lena can purchase the products in the following way: one item of product $1$ for $2$ rubles, two items of product $2$ for $2$ rubles for each, one item of product $5$ for $2$ rubles, one item of product $3$ for $1$ ruble, two items of product $4$ for $1$ ruble for each, one item of product $1$ for $1$ ruble. In total, she spends $12$ rubles.
def func(): n = int(input()) arr = [] for _ in range(n): a, b = map(int, input().split()) arr.append([a, b]) arr.sort(key=lambda x: x[1]) cur_prod = 0 ans = 0 i = 0 j = n - 1 while i <= j: while arr[i][1] > cur_prod and arr[i][0] > 0: diff = arr[i][1] - cur_prod d = max(arr[j][0] - diff, 0) ans += (arr[j][0] - d) * 2 cur_prod += arr[j][0] - d arr[j][0] = d if d == 0: j -= 1 cur_prod += arr[i][0] ans += arr[i][0] i += 1 print(ans) for _ in range(1): func()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR WHILE VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR IF VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β€” "PriceFixed". Here are some rules of that store: The store has an infinite number of items of every product. All products have the same price: $2$ rubles per item. For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!). Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β€” the number of products. Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β€” the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product. The sum of all $a_i$ does not exceed $10^{14}$. -----Output----- Output the minimum sum that Lena needs to make all purchases. -----Examples----- Input 3 3 4 1 3 1 5 Output 8 Input 5 2 7 2 8 1 2 2 4 1 8 Output 12 -----Note----- In the first example, Lena can purchase the products in the following way: one item of product $3$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased), one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased). In total, she spends $8$ rubles. It can be proved that it is impossible to spend less. In the second example Lena can purchase the products in the following way: one item of product $1$ for $2$ rubles, two items of product $2$ for $2$ rubles for each, one item of product $5$ for $2$ rubles, one item of product $3$ for $1$ ruble, two items of product $4$ for $1$ ruble for each, one item of product $1$ for $1$ ruble. In total, she spends $12$ rubles.
n = int(input()) arr = [] for i in range(n): a, b = map(int, input().split()) arr.append([b, a]) arr.sort() last = -1 ans = 0 prod = 0 for i in range(n): if arr[i][0] > prod: while last >= -n and prod < arr[i][0]: if prod + arr[last][1] <= arr[i][0]: prod += arr[last][1] ans += arr[last][1] * 2 arr[last][1] = 0 last -= 1 else: needed = arr[i][0] - prod arr[last][1] -= needed prod += needed ans += needed * 2 if prod >= arr[i][0]: prod += arr[i][1] ans += arr[i][1] arr[i][1] = 0 else: prod += arr[i][1] * 2 ans += arr[i][1] * 2 arr[i][1] = 0 else: prod += arr[i][1] ans += arr[i][1] arr[i][1] = 0 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR WHILE VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β€” "PriceFixed". Here are some rules of that store: The store has an infinite number of items of every product. All products have the same price: $2$ rubles per item. For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!). Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β€” the number of products. Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β€” the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product. The sum of all $a_i$ does not exceed $10^{14}$. -----Output----- Output the minimum sum that Lena needs to make all purchases. -----Examples----- Input 3 3 4 1 3 1 5 Output 8 Input 5 2 7 2 8 1 2 2 4 1 8 Output 12 -----Note----- In the first example, Lena can purchase the products in the following way: one item of product $3$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased), one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased). In total, she spends $8$ rubles. It can be proved that it is impossible to spend less. In the second example Lena can purchase the products in the following way: one item of product $1$ for $2$ rubles, two items of product $2$ for $2$ rubles for each, one item of product $5$ for $2$ rubles, one item of product $3$ for $1$ ruble, two items of product $4$ for $1$ ruble for each, one item of product $1$ for $1$ ruble. In total, she spends $12$ rubles.
prices = [] for _ in range(int(input())): ai, bi = map(int, input().split()) prices.append([ai, bi]) prices.sort(key=lambda x: x[1]) p, q = 0, len(prices) - 1 count = 0 bought = 0 while p <= q: if bought < prices[p][1]: tmp = min(prices[q][0], prices[p][1] - bought) count += 2 * tmp bought += tmp prices[q][0] -= tmp if prices[q][0] == 0: q -= 1 else: bought += prices[p][0] count += prices[p][0] p += 1 print(count)
ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR BIN_OP NUMBER VAR VAR VAR VAR VAR NUMBER VAR IF VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β€” "PriceFixed". Here are some rules of that store: The store has an infinite number of items of every product. All products have the same price: $2$ rubles per item. For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!). Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β€” the number of products. Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β€” the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product. The sum of all $a_i$ does not exceed $10^{14}$. -----Output----- Output the minimum sum that Lena needs to make all purchases. -----Examples----- Input 3 3 4 1 3 1 5 Output 8 Input 5 2 7 2 8 1 2 2 4 1 8 Output 12 -----Note----- In the first example, Lena can purchase the products in the following way: one item of product $3$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased), one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased). In total, she spends $8$ rubles. It can be proved that it is impossible to spend less. In the second example Lena can purchase the products in the following way: one item of product $1$ for $2$ rubles, two items of product $2$ for $2$ rubles for each, one item of product $5$ for $2$ rubles, one item of product $3$ for $1$ ruble, two items of product $4$ for $1$ ruble for each, one item of product $1$ for $1$ ruble. In total, she spends $12$ rubles.
import sys input = sys.stdin.readline n = int(input()) a = [] b = 0 for _ in range(n): c, d = map(int, input().split()) a.append((c, d)) b += c a.sort(key=lambda x: x[1]) aa = [a[0]] for i in range(1, n): if a[i][1] == a[i - 1][1]: aa[-1] = aa[-1][0] + a[i][0], a[i][1] else: aa.append(a[i]) e = 0 f = 0 for i in range(n): if a[i][1] <= e: e += a[i][0] f += a[i][0] if e > b: f -= e - b break elif a[i][1] < b: f += (a[i][1] - e) * 2 e += a[i][1] - e if e > b: f -= (e - b) * 2 break e += a[i][0] f += a[i][0] if e > b: f -= e - b break else: f += a[i][0] * 2 e += a[i][0] if e > b: f -= (e - b) * 2 break print(f)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR IF VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β€” "PriceFixed". Here are some rules of that store: The store has an infinite number of items of every product. All products have the same price: $2$ rubles per item. For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!). Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β€” the number of products. Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β€” the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product. The sum of all $a_i$ does not exceed $10^{14}$. -----Output----- Output the minimum sum that Lena needs to make all purchases. -----Examples----- Input 3 3 4 1 3 1 5 Output 8 Input 5 2 7 2 8 1 2 2 4 1 8 Output 12 -----Note----- In the first example, Lena can purchase the products in the following way: one item of product $3$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased), one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased). In total, she spends $8$ rubles. It can be proved that it is impossible to spend less. In the second example Lena can purchase the products in the following way: one item of product $1$ for $2$ rubles, two items of product $2$ for $2$ rubles for each, one item of product $5$ for $2$ rubles, one item of product $3$ for $1$ ruble, two items of product $4$ for $1$ ruble for each, one item of product $1$ for $1$ ruble. In total, she spends $12$ rubles.
a = sorted([*map(int, s.split())][::-1] for s in [*open(0)][1:]) i = c = r = 0 j = len(a) - 1 while i <= j: x = _, y = a[j] d = min(a[i][0] - c, y) if d > 0: x[1] -= d r += d j -= d == y else: d = a[i][1] i += 1 c += d print(c + r)
ASSIGN VAR FUNC_CALL VAR LIST FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER VAR LIST FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β€” "PriceFixed". Here are some rules of that store: The store has an infinite number of items of every product. All products have the same price: $2$ rubles per item. For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!). Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β€” the number of products. Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β€” the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product. The sum of all $a_i$ does not exceed $10^{14}$. -----Output----- Output the minimum sum that Lena needs to make all purchases. -----Examples----- Input 3 3 4 1 3 1 5 Output 8 Input 5 2 7 2 8 1 2 2 4 1 8 Output 12 -----Note----- In the first example, Lena can purchase the products in the following way: one item of product $3$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased), one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased). In total, she spends $8$ rubles. It can be proved that it is impossible to spend less. In the second example Lena can purchase the products in the following way: one item of product $1$ for $2$ rubles, two items of product $2$ for $2$ rubles for each, one item of product $5$ for $2$ rubles, one item of product $3$ for $1$ ruble, two items of product $4$ for $1$ ruble for each, one item of product $1$ for $1$ ruble. In total, she spends $12$ rubles.
n = int(input()) a = [] for i in range(n): temp = [int(x) for x in input().split()] a.append(temp) a = sorted(a, key=lambda x: x[1]) rubels = 0 xp = 0 i = 0 j = n - 1 while i < j: if a[j][1] <= xp or a[j][0] == 0: xp += a[j][0] rubels += a[j][0] j -= 1 elif a[i][1] <= xp: xp += a[i][0] rubels += a[i][0] i += 1 else: required = a[i][1] - xp purchase = min(required, a[j][0]) xp += purchase rubels += 2 * purchase a[j][0] -= purchase if a[i][1] <= xp: xp += a[i][0] rubels += a[i][0] else: required = a[i][1] - xp purchase = min(required, a[j][0]) xp += purchase rubels += 2 * purchase a[i][0] -= purchase if a[i][0] > 0: rubels += a[i][0] xp += a[i][0] print(rubels)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR BIN_OP NUMBER VAR VAR VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR BIN_OP NUMBER VAR VAR VAR NUMBER VAR IF VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β€” "PriceFixed". Here are some rules of that store: The store has an infinite number of items of every product. All products have the same price: $2$ rubles per item. For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!). Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β€” the number of products. Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β€” the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product. The sum of all $a_i$ does not exceed $10^{14}$. -----Output----- Output the minimum sum that Lena needs to make all purchases. -----Examples----- Input 3 3 4 1 3 1 5 Output 8 Input 5 2 7 2 8 1 2 2 4 1 8 Output 12 -----Note----- In the first example, Lena can purchase the products in the following way: one item of product $3$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased), one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased). In total, she spends $8$ rubles. It can be proved that it is impossible to spend less. In the second example Lena can purchase the products in the following way: one item of product $1$ for $2$ rubles, two items of product $2$ for $2$ rubles for each, one item of product $5$ for $2$ rubles, one item of product $3$ for $1$ ruble, two items of product $4$ for $1$ ruble for each, one item of product $1$ for $1$ ruble. In total, she spends $12$ rubles.
from sys import stdin, stdout n = int(stdin.readline()) products = [] for _ in range(n): products.append([int(x) for x in stdin.readline().split()]) products.sort(key=lambda x: x[1], reverse=True) answer = 0 volume = 0 front_pointer = 0 back_pointer = n - 1 while front_pointer < back_pointer: if volume + products[front_pointer][0] >= products[back_pointer][1]: gap = max(0, products[back_pointer][1] - volume) products[front_pointer][0] -= gap answer += gap * 2 + products[back_pointer][0] volume += gap + products[back_pointer][0] back_pointer -= 1 else: answer += products[front_pointer][0] * 2 volume += products[front_pointer][0] front_pointer += 1 if volume + products[front_pointer][0] >= products[back_pointer][1]: gap = max(0, products[back_pointer][1] - volume) answer += gap * 2 + products[front_pointer][0] - gap else: answer += products[front_pointer][0] * 2 stdout.write(str(answer) + "\n")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β€” "PriceFixed". Here are some rules of that store: The store has an infinite number of items of every product. All products have the same price: $2$ rubles per item. For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!). Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β€” the number of products. Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β€” the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product. The sum of all $a_i$ does not exceed $10^{14}$. -----Output----- Output the minimum sum that Lena needs to make all purchases. -----Examples----- Input 3 3 4 1 3 1 5 Output 8 Input 5 2 7 2 8 1 2 2 4 1 8 Output 12 -----Note----- In the first example, Lena can purchase the products in the following way: one item of product $3$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased), one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased). In total, she spends $8$ rubles. It can be proved that it is impossible to spend less. In the second example Lena can purchase the products in the following way: one item of product $1$ for $2$ rubles, two items of product $2$ for $2$ rubles for each, one item of product $5$ for $2$ rubles, one item of product $3$ for $1$ ruble, two items of product $4$ for $1$ ruble for each, one item of product $1$ for $1$ ruble. In total, she spends $12$ rubles.
n = int(input()) arr = [] for _ in range(n): arr.append(list(map(int, input().split()))) arr.sort(key=lambda x: x[1]) lo, hi = 0, n - 1 ans, counter = 0, 0 while lo <= hi: to_buy, req = arr[lo] if counter >= req or to_buy == 0: counter += to_buy ans += to_buy lo += 1 else: a, b = arr[hi] temp = min(a, req - counter) counter += temp ans += temp * 2 arr[hi][0] -= temp if arr[hi][0] == 0: hi -= 1 print(ans)
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 NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR IF VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β€” "PriceFixed". Here are some rules of that store: The store has an infinite number of items of every product. All products have the same price: $2$ rubles per item. For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!). Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β€” the number of products. Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β€” the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product. The sum of all $a_i$ does not exceed $10^{14}$. -----Output----- Output the minimum sum that Lena needs to make all purchases. -----Examples----- Input 3 3 4 1 3 1 5 Output 8 Input 5 2 7 2 8 1 2 2 4 1 8 Output 12 -----Note----- In the first example, Lena can purchase the products in the following way: one item of product $3$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased), one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased). In total, she spends $8$ rubles. It can be proved that it is impossible to spend less. In the second example Lena can purchase the products in the following way: one item of product $1$ for $2$ rubles, two items of product $2$ for $2$ rubles for each, one item of product $5$ for $2$ rubles, one item of product $3$ for $1$ ruble, two items of product $4$ for $1$ ruble for each, one item of product $1$ for $1$ ruble. In total, she spends $12$ rubles.
def answer(): s, ans = 0, 0 a.sort(key=lambda x: x[1], reverse=True) i, j = 0, n - 1 while i <= j: if a[j][1] >= s + a[i][0]: ans += 2 * a[i][0] s += a[i][0] i += 1 else: a[i][0] -= max(0, a[j][1] - s) ans += 2 * max(0, a[j][1] - s) s += max(0, a[j][1] - s) ans += a[j][0] s += a[j][0] j -= 1 return ans n = int(input()) a = [] for i in range(n): p, q = map(int, input().split()) a.append([p, q]) print(answer())
FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR BIN_OP NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β€” "PriceFixed". Here are some rules of that store: The store has an infinite number of items of every product. All products have the same price: $2$ rubles per item. For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!). Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β€” the number of products. Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β€” the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product. The sum of all $a_i$ does not exceed $10^{14}$. -----Output----- Output the minimum sum that Lena needs to make all purchases. -----Examples----- Input 3 3 4 1 3 1 5 Output 8 Input 5 2 7 2 8 1 2 2 4 1 8 Output 12 -----Note----- In the first example, Lena can purchase the products in the following way: one item of product $3$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased), one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased). In total, she spends $8$ rubles. It can be proved that it is impossible to spend less. In the second example Lena can purchase the products in the following way: one item of product $1$ for $2$ rubles, two items of product $2$ for $2$ rubles for each, one item of product $5$ for $2$ rubles, one item of product $3$ for $1$ ruble, two items of product $4$ for $1$ ruble for each, one item of product $1$ for $1$ ruble. In total, she spends $12$ rubles.
try: n = int(input()) arr = [] total = 0 for _ in range(n): tuples = list(map(int, input().split())) total += tuples[0] arr.append(tuples) arr = list(reversed(sorted(arr, key=lambda item: (item[1], -item[0])))) buy = 0 cost = 0 left = 0 right = n - 1 while left <= right: if buy + arr[left][0] < arr[right][1]: buy += arr[left][0] cost += 2 * arr[left][0] arr[left][0] = 0 left += 1 else: temp = max(arr[right][1] - buy, 0) another_temp = arr[left][0] arr[left][0] -= temp buy += temp cost += 2 * temp buy += arr[right][0] cost += arr[right][0] arr[right][0] = 0 right -= 1 if temp == another_temp: left += 1 print(cost) except e: pass
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR VAR BIN_OP NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β€” "PriceFixed". Here are some rules of that store: The store has an infinite number of items of every product. All products have the same price: $2$ rubles per item. For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!). Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β€” the number of products. Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β€” the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product. The sum of all $a_i$ does not exceed $10^{14}$. -----Output----- Output the minimum sum that Lena needs to make all purchases. -----Examples----- Input 3 3 4 1 3 1 5 Output 8 Input 5 2 7 2 8 1 2 2 4 1 8 Output 12 -----Note----- In the first example, Lena can purchase the products in the following way: one item of product $3$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased), one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased). In total, she spends $8$ rubles. It can be proved that it is impossible to spend less. In the second example Lena can purchase the products in the following way: one item of product $1$ for $2$ rubles, two items of product $2$ for $2$ rubles for each, one item of product $5$ for $2$ rubles, one item of product $3$ for $1$ ruble, two items of product $4$ for $1$ ruble for each, one item of product $1$ for $1$ ruble. In total, she spends $12$ rubles.
import sys n = int(sys.stdin.readline()) data = [] for i in range(n): temp = list(map(int, sys.stdin.readline().split())) data.append(temp) data.sort(key=lambda x: x[1]) left, right = 0, n - 1 ans, cnt = 0, 0 while left <= right: if cnt >= data[left][1]: ans += data[left][0] cnt += data[left][0] left += 1 elif data[right][0] > data[left][1] - cnt: data[right][0] -= data[left][1] - cnt ans += (data[left][1] - cnt) * 2 cnt += data[left][1] - cnt else: cnt += data[right][0] ans += data[right][0] * 2 right -= 1 print(ans)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β€” "PriceFixed". Here are some rules of that store: The store has an infinite number of items of every product. All products have the same price: $2$ rubles per item. For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!). Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β€” the number of products. Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β€” the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product. The sum of all $a_i$ does not exceed $10^{14}$. -----Output----- Output the minimum sum that Lena needs to make all purchases. -----Examples----- Input 3 3 4 1 3 1 5 Output 8 Input 5 2 7 2 8 1 2 2 4 1 8 Output 12 -----Note----- In the first example, Lena can purchase the products in the following way: one item of product $3$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased), one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased). In total, she spends $8$ rubles. It can be proved that it is impossible to spend less. In the second example Lena can purchase the products in the following way: one item of product $1$ for $2$ rubles, two items of product $2$ for $2$ rubles for each, one item of product $5$ for $2$ rubles, one item of product $3$ for $1$ ruble, two items of product $4$ for $1$ ruble for each, one item of product $1$ for $1$ ruble. In total, she spends $12$ rubles.
n = int(input()) arr = [] for w in range(n): inp = input().split() arr.append([int(inp[0]), int(inp[1])]) arr.sort(key=lambda x: x[1]) answer = 0 count = 0 i = 0 j = len(arr) - 1 while i <= j: if arr[i][1] <= count: count += arr[i][0] i += 1 elif arr[j][0] + count <= arr[i][1]: answer += arr[j][0] count += arr[j][0] j -= 1 else: arr[j][0] -= arr[i][1] - count answer += arr[i][1] - count count = arr[i][1] print(answer + count)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β€” "PriceFixed". Here are some rules of that store: The store has an infinite number of items of every product. All products have the same price: $2$ rubles per item. For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!). Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β€” the number of products. Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β€” the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product. The sum of all $a_i$ does not exceed $10^{14}$. -----Output----- Output the minimum sum that Lena needs to make all purchases. -----Examples----- Input 3 3 4 1 3 1 5 Output 8 Input 5 2 7 2 8 1 2 2 4 1 8 Output 12 -----Note----- In the first example, Lena can purchase the products in the following way: one item of product $3$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased), one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased). In total, she spends $8$ rubles. It can be proved that it is impossible to spend less. In the second example Lena can purchase the products in the following way: one item of product $1$ for $2$ rubles, two items of product $2$ for $2$ rubles for each, one item of product $5$ for $2$ rubles, one item of product $3$ for $1$ ruble, two items of product $4$ for $1$ ruble for each, one item of product $1$ for $1$ ruble. In total, she spends $12$ rubles.
n = int(input()) al = [] for _ in range(n): ai, bi = map(int, input().split()) al.append([ai, bi]) al = sorted(al, key=lambda x: x[1]) p1 = 0 p2 = len(al) - 1 quan = 0 cost = 0 while p2 >= p1: diff = al[p1][1] - quan while diff > 0: tmp = min(diff, al[p2][0]) diff -= tmp al[p2][0] -= tmp quan += tmp cost += 2 * tmp if al[p2][0] == 0: p2 -= 1 if p2 < p1: break if p2 < p1: break cost += al[p1][0] quan += al[p1][0] al[p1][0] = 0 p1 += 1 print(cost)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR VAR BIN_OP NUMBER VAR IF VAR VAR NUMBER NUMBER VAR NUMBER IF VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β€” "PriceFixed". Here are some rules of that store: The store has an infinite number of items of every product. All products have the same price: $2$ rubles per item. For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!). Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β€” the number of products. Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β€” the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product. The sum of all $a_i$ does not exceed $10^{14}$. -----Output----- Output the minimum sum that Lena needs to make all purchases. -----Examples----- Input 3 3 4 1 3 1 5 Output 8 Input 5 2 7 2 8 1 2 2 4 1 8 Output 12 -----Note----- In the first example, Lena can purchase the products in the following way: one item of product $3$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased), one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased). In total, she spends $8$ rubles. It can be proved that it is impossible to spend less. In the second example Lena can purchase the products in the following way: one item of product $1$ for $2$ rubles, two items of product $2$ for $2$ rubles for each, one item of product $5$ for $2$ rubles, one item of product $3$ for $1$ ruble, two items of product $4$ for $1$ ruble for each, one item of product $1$ for $1$ ruble. In total, she spends $12$ rubles.
n = int(input()) l = [] for i in range(n): x, y = map(int, input().split()) l.append([x, y]) l.sort(key=lambda l: l[1]) i = 0 j = n - 1 ans = 0 x = 0 while i <= j: if x >= l[i][1]: ans += l[i][0] x += l[i][0] i += 1 elif x + l[j][0] <= l[i][1]: ans += 2 * l[j][0] x += l[j][0] j -= 1 else: l[j][0] -= l[i][1] - x ans += 2 * (l[i][1] - x) x += l[i][1] - x print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR BIN_OP NUMBER BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β€” "PriceFixed". Here are some rules of that store: The store has an infinite number of items of every product. All products have the same price: $2$ rubles per item. For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!). Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β€” the number of products. Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β€” the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product. The sum of all $a_i$ does not exceed $10^{14}$. -----Output----- Output the minimum sum that Lena needs to make all purchases. -----Examples----- Input 3 3 4 1 3 1 5 Output 8 Input 5 2 7 2 8 1 2 2 4 1 8 Output 12 -----Note----- In the first example, Lena can purchase the products in the following way: one item of product $3$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased), one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased). In total, she spends $8$ rubles. It can be proved that it is impossible to spend less. In the second example Lena can purchase the products in the following way: one item of product $1$ for $2$ rubles, two items of product $2$ for $2$ rubles for each, one item of product $5$ for $2$ rubles, one item of product $3$ for $1$ ruble, two items of product $4$ for $1$ ruble for each, one item of product $1$ for $1$ ruble. In total, she spends $12$ rubles.
import sys input = sys.stdin.readline def solve(): n = int(input()) a = [tuple(reversed(tuple(map(int, input().split())))) for i in range(n)] a.sort() b = [0] * n have = 0 j = len(a) - 1 r = 0 for i in range(len(a)): c, d = a[i] while c > have and j > i: if b[j] < a[j][1]: w = min(c - have, a[j][1] - b[j]) r += w * 2 have += w b[j] += w else: j -= 1 if c <= have: w = d - b[i] b[i] += w have += w r += w else: w = c - have if w + b[i] <= d: b[i] += w r += w * 2 have += w w = d - b[i] b[i] += w r += w have += w else: w = d - b[i] b[i] += w r += w * 2 have += w print(r) solve()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β€” "PriceFixed". Here are some rules of that store: The store has an infinite number of items of every product. All products have the same price: $2$ rubles per item. For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!). Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β€” the number of products. Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β€” the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product. The sum of all $a_i$ does not exceed $10^{14}$. -----Output----- Output the minimum sum that Lena needs to make all purchases. -----Examples----- Input 3 3 4 1 3 1 5 Output 8 Input 5 2 7 2 8 1 2 2 4 1 8 Output 12 -----Note----- In the first example, Lena can purchase the products in the following way: one item of product $3$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased), one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased). In total, she spends $8$ rubles. It can be proved that it is impossible to spend less. In the second example Lena can purchase the products in the following way: one item of product $1$ for $2$ rubles, two items of product $2$ for $2$ rubles for each, one item of product $5$ for $2$ rubles, one item of product $3$ for $1$ ruble, two items of product $4$ for $1$ ruble for each, one item of product $1$ for $1$ ruble. In total, she spends $12$ rubles.
n = int(input()) a = [] for _ in range(n): x, y = map(int, input().split()) a.append([x, y]) a.sort(key=lambda k: k[1]) needed = a[0][1] items = 0 i = 0 j = n - 1 ans = 0 while True: if i > j: break elif i == j: if items >= a[i][1]: ans += a[i][0] else: need = a[i][1] - items if need > a[i][0]: ans += 2 * a[i][0] else: ans += 2 * need ans += a[i][0] - need break if a[j][0] + items < needed: ans += 2 * a[j][0] items += a[j][0] j -= 1 elif a[j][0] + items == needed: ans += 2 * a[j][0] items += a[j][0] a[j][0] = 0 while i <= j: if a[i][1] <= items: ans += a[i][0] items += a[i][0] else: needed = a[i][1] break i += 1 j -= 1 else: need = needed - items ans += 2 * need a[j][0] -= need items += need while i <= j: if a[i][1] <= items: ans += a[i][0] items += a[i][0] else: needed = a[i][1] break i += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR IF VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR IF VAR VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP NUMBER VAR VAR VAR NUMBER VAR VAR VAR WHILE VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β€” "PriceFixed". Here are some rules of that store: The store has an infinite number of items of every product. All products have the same price: $2$ rubles per item. For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!). Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β€” the number of products. Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β€” the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product. The sum of all $a_i$ does not exceed $10^{14}$. -----Output----- Output the minimum sum that Lena needs to make all purchases. -----Examples----- Input 3 3 4 1 3 1 5 Output 8 Input 5 2 7 2 8 1 2 2 4 1 8 Output 12 -----Note----- In the first example, Lena can purchase the products in the following way: one item of product $3$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased), one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased). In total, she spends $8$ rubles. It can be proved that it is impossible to spend less. In the second example Lena can purchase the products in the following way: one item of product $1$ for $2$ rubles, two items of product $2$ for $2$ rubles for each, one item of product $5$ for $2$ rubles, one item of product $3$ for $1$ ruble, two items of product $4$ for $1$ ruble for each, one item of product $1$ for $1$ ruble. In total, she spends $12$ rubles.
events = [] def solve(): events.sort(key=lambda x: x[1]) l = 0 r = len(events) - 1 moneyNeeded = 0 productsBought = 0 while l <= r: if productsBought < events[l][1]: delta = events[l][1] - productsBought cost = min(events[r][0], delta) * 2 productsBought += min(events[r][0], delta) events[r][0] -= min(events[r][0], delta) moneyNeeded += cost if events[r][0] == 0: r -= 1 else: productsBought += events[l][0] moneyNeeded += events[l][0] l += 1 return moneyNeeded t = int(input()) for test in range(1, t + 1): required, discount = list(map(int, input().split())) events.append([required, discount]) print(f"{solve()}")
ASSIGN VAR LIST FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR IF VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β€” "PriceFixed". Here are some rules of that store: The store has an infinite number of items of every product. All products have the same price: $2$ rubles per item. For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!). Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β€” the number of products. Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β€” the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product. The sum of all $a_i$ does not exceed $10^{14}$. -----Output----- Output the minimum sum that Lena needs to make all purchases. -----Examples----- Input 3 3 4 1 3 1 5 Output 8 Input 5 2 7 2 8 1 2 2 4 1 8 Output 12 -----Note----- In the first example, Lena can purchase the products in the following way: one item of product $3$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased), one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased). In total, she spends $8$ rubles. It can be proved that it is impossible to spend less. In the second example Lena can purchase the products in the following way: one item of product $1$ for $2$ rubles, two items of product $2$ for $2$ rubles for each, one item of product $5$ for $2$ rubles, one item of product $3$ for $1$ ruble, two items of product $4$ for $1$ ruble for each, one item of product $1$ for $1$ ruble. In total, she spends $12$ rubles.
n = int(input()) m = [] for i in range(n): a = list(map(int, input().split())) m.append([a[1], a[0]]) m.sort() x = 0 for i in range(n): x += m[i][1] ans = x * 2 for i in range(n - 1, -1, -1): y = x - m[i][0] if y >= m[i][1]: ans -= m[i][1] x -= m[i][1] elif y > 0: ans -= y x -= y print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β€” "PriceFixed". Here are some rules of that store: The store has an infinite number of items of every product. All products have the same price: $2$ rubles per item. For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!). Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β€” the number of products. Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β€” the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product. The sum of all $a_i$ does not exceed $10^{14}$. -----Output----- Output the minimum sum that Lena needs to make all purchases. -----Examples----- Input 3 3 4 1 3 1 5 Output 8 Input 5 2 7 2 8 1 2 2 4 1 8 Output 12 -----Note----- In the first example, Lena can purchase the products in the following way: one item of product $3$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased), one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased). In total, she spends $8$ rubles. It can be proved that it is impossible to spend less. In the second example Lena can purchase the products in the following way: one item of product $1$ for $2$ rubles, two items of product $2$ for $2$ rubles for each, one item of product $5$ for $2$ rubles, one item of product $3$ for $1$ ruble, two items of product $4$ for $1$ ruble for each, one item of product $1$ for $1$ ruble. In total, she spends $12$ rubles.
prices = [] count = 0 n = int(input()) for _ in range(n): ai, bi = map(int, input().split()) prices.append([bi, ai]) count += ai prices.sort() discount = 0 for p in range(n - 1, -1, -1): x = prices[p] if x[0] >= count: continue else: discount += min(count - x[0] - discount, x[1]) print(count * 2 - discount)
ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β€” "PriceFixed". Here are some rules of that store: The store has an infinite number of items of every product. All products have the same price: $2$ rubles per item. For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!). Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β€” the number of products. Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β€” the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product. The sum of all $a_i$ does not exceed $10^{14}$. -----Output----- Output the minimum sum that Lena needs to make all purchases. -----Examples----- Input 3 3 4 1 3 1 5 Output 8 Input 5 2 7 2 8 1 2 2 4 1 8 Output 12 -----Note----- In the first example, Lena can purchase the products in the following way: one item of product $3$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased), one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased). In total, she spends $8$ rubles. It can be proved that it is impossible to spend less. In the second example Lena can purchase the products in the following way: one item of product $1$ for $2$ rubles, two items of product $2$ for $2$ rubles for each, one item of product $5$ for $2$ rubles, one item of product $3$ for $1$ ruble, two items of product $4$ for $1$ ruble for each, one item of product $1$ for $1$ ruble. In total, she spends $12$ rubles.
import sys n = int(input()) items = [] total_items = 0 for _ in range(n): c, l = list(map(int, sys.stdin.readline().strip().split())) total_items += c items.append((c, l)) items.sort(key=lambda x: x[1], reverse=True) total_cost = 2 * total_items pos = 0 while pos < total_items: count, start = items.pop() if start < total_items: if start > pos: pos = start pos += count total_cost -= count if pos > total_items: total_cost += pos - total_items else: break print(total_cost)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR 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 FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β€” "PriceFixed". Here are some rules of that store: The store has an infinite number of items of every product. All products have the same price: $2$ rubles per item. For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!). Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β€” the number of products. Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β€” the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product. The sum of all $a_i$ does not exceed $10^{14}$. -----Output----- Output the minimum sum that Lena needs to make all purchases. -----Examples----- Input 3 3 4 1 3 1 5 Output 8 Input 5 2 7 2 8 1 2 2 4 1 8 Output 12 -----Note----- In the first example, Lena can purchase the products in the following way: one item of product $3$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased), one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased). In total, she spends $8$ rubles. It can be proved that it is impossible to spend less. In the second example Lena can purchase the products in the following way: one item of product $1$ for $2$ rubles, two items of product $2$ for $2$ rubles for each, one item of product $5$ for $2$ rubles, one item of product $3$ for $1$ ruble, two items of product $4$ for $1$ ruble for each, one item of product $1$ for $1$ ruble. In total, she spends $12$ rubles.
n = int(input()) a = [] b = [] for i in range(n): x, y = map(int, input().split()) a.append(x) b.append(y) ba = zip(b, a) ba = sorted(ba) temp = [] for i in ba: temp.append(list(i)) ba = temp ans = 0 i = 0 j = n - 1 c = 0 while i <= j: if c >= ba[i][0]: c += ba[i][1] ans += ba[i][1] i += 1 else: if ba[j][1] == 0: j -= 1 else: tmp = min(ba[i][0] - c, ba[j][1]) c += tmp ans += 2 * tmp ba[j][1] -= tmp if ba[j][1] == 0: j -= 1 print(ans)
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 VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR BIN_OP NUMBER VAR VAR VAR NUMBER VAR IF VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β€” "PriceFixed". Here are some rules of that store: The store has an infinite number of items of every product. All products have the same price: $2$ rubles per item. For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!). Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β€” the number of products. Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β€” the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product. The sum of all $a_i$ does not exceed $10^{14}$. -----Output----- Output the minimum sum that Lena needs to make all purchases. -----Examples----- Input 3 3 4 1 3 1 5 Output 8 Input 5 2 7 2 8 1 2 2 4 1 8 Output 12 -----Note----- In the first example, Lena can purchase the products in the following way: one item of product $3$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased), one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased). In total, she spends $8$ rubles. It can be proved that it is impossible to spend less. In the second example Lena can purchase the products in the following way: one item of product $1$ for $2$ rubles, two items of product $2$ for $2$ rubles for each, one item of product $5$ for $2$ rubles, one item of product $3$ for $1$ ruble, two items of product $4$ for $1$ ruble for each, one item of product $1$ for $1$ ruble. In total, she spends $12$ rubles.
import sys input = sys.stdin.readline n = int(input()) ar = [] for i in range(int(n)): ar.append(list(map(int, input().split()))) ar.sort(key=lambda x: x[1]) j = n - 1 tot = 0 ans = 0 for i in range(n): req = max(ar[i][1] - tot, 0) while req != 0: if j < 0: break a = ar[j][0] if a <= req: req -= a tot += a ar[j][0] = 0 j -= 1 ans += 2 * a else: ar[j][0] -= req tot += req ans += 2 * req req = 0 break if req == 0: ans += ar[i][0] else: ans += ar[i][0] * 2 tot += ar[i][0] ar[i][0] = 0 print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL 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 NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR NUMBER WHILE VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β€” "PriceFixed". Here are some rules of that store: The store has an infinite number of items of every product. All products have the same price: $2$ rubles per item. For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!). Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β€” the number of products. Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β€” the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product. The sum of all $a_i$ does not exceed $10^{14}$. -----Output----- Output the minimum sum that Lena needs to make all purchases. -----Examples----- Input 3 3 4 1 3 1 5 Output 8 Input 5 2 7 2 8 1 2 2 4 1 8 Output 12 -----Note----- In the first example, Lena can purchase the products in the following way: one item of product $3$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased), one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased). In total, she spends $8$ rubles. It can be proved that it is impossible to spend less. In the second example Lena can purchase the products in the following way: one item of product $1$ for $2$ rubles, two items of product $2$ for $2$ rubles for each, one item of product $5$ for $2$ rubles, one item of product $3$ for $1$ ruble, two items of product $4$ for $1$ ruble for each, one item of product $1$ for $1$ ruble. In total, she spends $12$ rubles.
import sys input = sys.stdin.readline n = int(input()) v = [] for i in range(n): a, b = map(int, input().split()) v.append([a, b]) v.sort(key=lambda x: x[1]) ans = 0 l = 0 r = n - 1 now = 0 while True: if v[l][1] <= now: ans += v[l][0] now += v[l][0] l += 1 else: a, b = v[r] if now + a <= v[l][1]: ans += a * 2 now += a r -= 1 else: x = v[l][1] - now v[r][0] -= x ans += x * 2 now += x if l > r: break print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β€” "PriceFixed". Here are some rules of that store: The store has an infinite number of items of every product. All products have the same price: $2$ rubles per item. For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!). Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β€” the number of products. Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β€” the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product. The sum of all $a_i$ does not exceed $10^{14}$. -----Output----- Output the minimum sum that Lena needs to make all purchases. -----Examples----- Input 3 3 4 1 3 1 5 Output 8 Input 5 2 7 2 8 1 2 2 4 1 8 Output 12 -----Note----- In the first example, Lena can purchase the products in the following way: one item of product $3$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased), one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased). In total, she spends $8$ rubles. It can be proved that it is impossible to spend less. In the second example Lena can purchase the products in the following way: one item of product $1$ for $2$ rubles, two items of product $2$ for $2$ rubles for each, one item of product $5$ for $2$ rubles, one item of product $3$ for $1$ ruble, two items of product $4$ for $1$ ruble for each, one item of product $1$ for $1$ ruble. In total, she spends $12$ rubles.
n = int(input()) li = [] for case in range(n): tp = list(map(int, input().split())) li.append(tp) li.sort(key=lambda x: (x[1], x[0])) l, r = 0, len(li) - 1 totalBought = 0 totalCost = 0 while l <= r: minNeedForDisc = max(li[l][1] - totalBought, 0) if not minNeedForDisc: totalBought += li[l][0] totalCost += li[l][0] li[l][0] = 0 l += 1 else: buy = min(minNeedForDisc, li[r][0]) totalBought += buy totalCost += 2 * buy li[r][0] -= buy if not li[r][0]: r -= 1 print(totalCost)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR BIN_OP NUMBER VAR VAR VAR NUMBER VAR IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β€” "PriceFixed". Here are some rules of that store: The store has an infinite number of items of every product. All products have the same price: $2$ rubles per item. For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!). Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β€” the number of products. Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β€” the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product. The sum of all $a_i$ does not exceed $10^{14}$. -----Output----- Output the minimum sum that Lena needs to make all purchases. -----Examples----- Input 3 3 4 1 3 1 5 Output 8 Input 5 2 7 2 8 1 2 2 4 1 8 Output 12 -----Note----- In the first example, Lena can purchase the products in the following way: one item of product $3$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased), one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased). In total, she spends $8$ rubles. It can be proved that it is impossible to spend less. In the second example Lena can purchase the products in the following way: one item of product $1$ for $2$ rubles, two items of product $2$ for $2$ rubles for each, one item of product $5$ for $2$ rubles, one item of product $3$ for $1$ ruble, two items of product $4$ for $1$ ruble for each, one item of product $1$ for $1$ ruble. In total, she spends $12$ rubles.
n = int(input()) u = [] for i in range(n): a, b = map(int, input().split()) u.append([a, b]) u.sort(key=lambda x: x[1]) i1 = 0 i2 = -1 ans = 0 k = 0 for _ in range(n): while i2 < len(u) - 1 and u[i2 + 1][1] <= k: i2 += 1 if i1 > i2: if u[-1][0] < u[i2 + 1][1] - k: ans += 2 * u[-1][0] k += u[-1][0] u.pop() else: ans += 2 * (u[i2 + 1][1] - k) u[-1][0] -= u[i2 + 1][1] - k k = u[i2 + 1][1] i2 += 1 ans += 1 * u[i1][0] k += u[i1][0] i1 += 1 else: ans += 1 * u[i1][0] k += u[i1][0] i1 += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER IF VAR VAR IF VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP NUMBER VAR NUMBER NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β€” "PriceFixed". Here are some rules of that store: The store has an infinite number of items of every product. All products have the same price: $2$ rubles per item. For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!). Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β€” the number of products. Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β€” the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product. The sum of all $a_i$ does not exceed $10^{14}$. -----Output----- Output the minimum sum that Lena needs to make all purchases. -----Examples----- Input 3 3 4 1 3 1 5 Output 8 Input 5 2 7 2 8 1 2 2 4 1 8 Output 12 -----Note----- In the first example, Lena can purchase the products in the following way: one item of product $3$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased), one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased). In total, she spends $8$ rubles. It can be proved that it is impossible to spend less. In the second example Lena can purchase the products in the following way: one item of product $1$ for $2$ rubles, two items of product $2$ for $2$ rubles for each, one item of product $5$ for $2$ rubles, one item of product $3$ for $1$ ruble, two items of product $4$ for $1$ ruble for each, one item of product $1$ for $1$ ruble. In total, she spends $12$ rubles.
import sys input = sys.stdin.readline n = int(input()) A = [list(map(int, input().split())) for _ in range(n)] A.sort(key=lambda x: (x[1], x[0])) ans = 0 j = n - 1 cur = 0 for i, (a, b) in enumerate(A): if a == 0: break while j > i and cur < b: d = min(b - cur, A[j][0]) A[j][0] -= d cur += d ans += d * 2 if A[j][0] == 0: j -= 1 if cur >= b: ans += a cur += a else: x = b - cur if a >= x: ans += x * 2 + (a - x) cur += a else: ans += a * 2 cur += a print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β€” "PriceFixed". Here are some rules of that store: The store has an infinite number of items of every product. All products have the same price: $2$ rubles per item. For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!). Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β€” the number of products. Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β€” the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product. The sum of all $a_i$ does not exceed $10^{14}$. -----Output----- Output the minimum sum that Lena needs to make all purchases. -----Examples----- Input 3 3 4 1 3 1 5 Output 8 Input 5 2 7 2 8 1 2 2 4 1 8 Output 12 -----Note----- In the first example, Lena can purchase the products in the following way: one item of product $3$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased), one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased). In total, she spends $8$ rubles. It can be proved that it is impossible to spend less. In the second example Lena can purchase the products in the following way: one item of product $1$ for $2$ rubles, two items of product $2$ for $2$ rubles for each, one item of product $5$ for $2$ rubles, one item of product $3$ for $1$ ruble, two items of product $4$ for $1$ ruble for each, one item of product $1$ for $1$ ruble. In total, she spends $12$ rubles.
n = int(input()) ab = [] for i in range(n): ab.append([int(x) for x in input().split()]) ab.sort(key=lambda x: x[1]) i = 0 j = n - 1 cnt = 0 ans = 0 while i <= j: if ab[i][1] <= cnt: ans += ab[i][0] cnt += ab[i][0] ab[i][0] = 0 i += 1 else: k = min(ab[j][0], ab[i][1] - cnt) ab[j][0] -= k cnt += k ans += k * 2 if ab[j][0] == 0: j -= 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β€” "PriceFixed". Here are some rules of that store: The store has an infinite number of items of every product. All products have the same price: $2$ rubles per item. For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!). Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β€” the number of products. Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β€” the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product. The sum of all $a_i$ does not exceed $10^{14}$. -----Output----- Output the minimum sum that Lena needs to make all purchases. -----Examples----- Input 3 3 4 1 3 1 5 Output 8 Input 5 2 7 2 8 1 2 2 4 1 8 Output 12 -----Note----- In the first example, Lena can purchase the products in the following way: one item of product $3$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased), one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased). In total, she spends $8$ rubles. It can be proved that it is impossible to spend less. In the second example Lena can purchase the products in the following way: one item of product $1$ for $2$ rubles, two items of product $2$ for $2$ rubles for each, one item of product $5$ for $2$ rubles, one item of product $3$ for $1$ ruble, two items of product $4$ for $1$ ruble for each, one item of product $1$ for $1$ ruble. In total, she spends $12$ rubles.
from sys import stdin input = stdin.readline rn = lambda: int(input()) rns = lambda: map(int, input().split()) rl = lambda: list(map(int, input().split())) rs = lambda: input() YN = lambda x: print("YES") if x else print("NO") mod = 10**9 + 7 n = rn() items = [] t = 0 for i in range(n): items.append(rl()) t += items[-1][0] items.sort(key=lambda x: (x[1], x[0])) ans = 0 acc = 0 start = 0 while acc < t: if items[start][1] <= acc: ans += items[start][0] acc += items[start][0] start += 1 else: sub = min(items[start][1] - acc, items[-1][0]) ans += sub * 2 acc += sub items[-1][0] -= sub if items[-1][0] == 0: items.pop() print(ans)
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER NUMBER VAR IF VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β€” "PriceFixed". Here are some rules of that store: The store has an infinite number of items of every product. All products have the same price: $2$ rubles per item. For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!). Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β€” the number of products. Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β€” the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product. The sum of all $a_i$ does not exceed $10^{14}$. -----Output----- Output the minimum sum that Lena needs to make all purchases. -----Examples----- Input 3 3 4 1 3 1 5 Output 8 Input 5 2 7 2 8 1 2 2 4 1 8 Output 12 -----Note----- In the first example, Lena can purchase the products in the following way: one item of product $3$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased), one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased). In total, she spends $8$ rubles. It can be proved that it is impossible to spend less. In the second example Lena can purchase the products in the following way: one item of product $1$ for $2$ rubles, two items of product $2$ for $2$ rubles for each, one item of product $5$ for $2$ rubles, one item of product $3$ for $1$ ruble, two items of product $4$ for $1$ ruble for each, one item of product $1$ for $1$ ruble. In total, she spends $12$ rubles.
b, a = zip(*sorted([*map(int, s.split())][::-1] for s in [*open(0)][1:])) (*a,) = a i = c = r = 0 j = len(a) - 1 while i <= j: if (d := min(b[i] - c, a[j])) > 0: a[j] -= d c += d r += d j -= a[j] < 1 else: c += a[i] i += 1 print(c + r)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR LIST FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER VAR LIST FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β€” "PriceFixed". Here are some rules of that store: The store has an infinite number of items of every product. All products have the same price: $2$ rubles per item. For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!). Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β€” the number of products. Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β€” the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product. The sum of all $a_i$ does not exceed $10^{14}$. -----Output----- Output the minimum sum that Lena needs to make all purchases. -----Examples----- Input 3 3 4 1 3 1 5 Output 8 Input 5 2 7 2 8 1 2 2 4 1 8 Output 12 -----Note----- In the first example, Lena can purchase the products in the following way: one item of product $3$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased), one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased). In total, she spends $8$ rubles. It can be proved that it is impossible to spend less. In the second example Lena can purchase the products in the following way: one item of product $1$ for $2$ rubles, two items of product $2$ for $2$ rubles for each, one item of product $5$ for $2$ rubles, one item of product $3$ for $1$ ruble, two items of product $4$ for $1$ ruble for each, one item of product $1$ for $1$ ruble. In total, she spends $12$ rubles.
n = int(input()) a = [""] * n ans = 0 sm = 0 for i in range(n): g, h = [int(x) for x in input().split()] a[i] = [h, g] sm += g a.sort() for i in range(n - 1, -1, -1): x = a[i] k = min(x[0], sm) ans += min(sm - ans - k, x[1]) print(sm * 2 - ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST VAR VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β€” "PriceFixed". Here are some rules of that store: The store has an infinite number of items of every product. All products have the same price: $2$ rubles per item. For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!). Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β€” the number of products. Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β€” the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product. The sum of all $a_i$ does not exceed $10^{14}$. -----Output----- Output the minimum sum that Lena needs to make all purchases. -----Examples----- Input 3 3 4 1 3 1 5 Output 8 Input 5 2 7 2 8 1 2 2 4 1 8 Output 12 -----Note----- In the first example, Lena can purchase the products in the following way: one item of product $3$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased), one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased). In total, she spends $8$ rubles. It can be proved that it is impossible to spend less. In the second example Lena can purchase the products in the following way: one item of product $1$ for $2$ rubles, two items of product $2$ for $2$ rubles for each, one item of product $5$ for $2$ rubles, one item of product $3$ for $1$ ruble, two items of product $4$ for $1$ ruble for each, one item of product $1$ for $1$ ruble. In total, she spends $12$ rubles.
arr = [] for _ in range(int(input())): arr.append(list(map(int, input().split()))) arr = sorted(arr, key=lambda x: x[1], reverse=True) p = 0 c = 0 i = 0 j = len(arr) - 1 while i <= j: need = arr[j][1] if need <= c: p += arr[j][0] c += arr[j][0] j -= 1 continue diff = need - c ext = arr[i][0] if ext <= diff: p += ext * 2 c += ext i += 1 elif ext > diff: arr[i][0] -= diff p += diff * 2 c += diff print(p)
ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β€” "PriceFixed". Here are some rules of that store: The store has an infinite number of items of every product. All products have the same price: $2$ rubles per item. For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!). Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β€” the number of products. Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β€” the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product. The sum of all $a_i$ does not exceed $10^{14}$. -----Output----- Output the minimum sum that Lena needs to make all purchases. -----Examples----- Input 3 3 4 1 3 1 5 Output 8 Input 5 2 7 2 8 1 2 2 4 1 8 Output 12 -----Note----- In the first example, Lena can purchase the products in the following way: one item of product $3$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased), one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased). In total, she spends $8$ rubles. It can be proved that it is impossible to spend less. In the second example Lena can purchase the products in the following way: one item of product $1$ for $2$ rubles, two items of product $2$ for $2$ rubles for each, one item of product $5$ for $2$ rubles, one item of product $3$ for $1$ ruble, two items of product $4$ for $1$ ruble for each, one item of product $1$ for $1$ ruble. In total, she spends $12$ rubles.
import sys input = sys.stdin.readline n = int(input()) ab = [list(map(int, input().split())) for i in range(n)] ab.sort(key=lambda x: x[1]) sa = 0 for i in range(n): sa += ab[i][0] c = 0 for i in range(n)[::-1]: r = sa - ab[i][1] c += max(min(r, ab[i][0]), 0) sa -= max(min(r, ab[i][0]), 0) print(sa * 2 + c)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β€” "PriceFixed". Here are some rules of that store: The store has an infinite number of items of every product. All products have the same price: $2$ rubles per item. For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!). Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β€” the number of products. Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β€” the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product. The sum of all $a_i$ does not exceed $10^{14}$. -----Output----- Output the minimum sum that Lena needs to make all purchases. -----Examples----- Input 3 3 4 1 3 1 5 Output 8 Input 5 2 7 2 8 1 2 2 4 1 8 Output 12 -----Note----- In the first example, Lena can purchase the products in the following way: one item of product $3$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased), one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased). In total, she spends $8$ rubles. It can be proved that it is impossible to spend less. In the second example Lena can purchase the products in the following way: one item of product $1$ for $2$ rubles, two items of product $2$ for $2$ rubles for each, one item of product $5$ for $2$ rubles, one item of product $3$ for $1$ ruble, two items of product $4$ for $1$ ruble for each, one item of product $1$ for $1$ ruble. In total, she spends $12$ rubles.
n = int(input()) a = [] b = [] d = 0 bl = [] for i in range(n): q = input().split() a.append(int(q[0])) bl.append([int(q[1]), int(q[0])]) sa = sum(a) lb = len(bl) for i in range(lb): if bl[i][0] < sa: b.append(bl[i]) b.sort(reverse=True) sat = sa for i in range(len(b)): x = min(sat - b[i][0], b[i][1]) sat -= x d += x print(2 * sa - d)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β€” "PriceFixed". Here are some rules of that store: The store has an infinite number of items of every product. All products have the same price: $2$ rubles per item. For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!). Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β€” the number of products. Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β€” the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product. The sum of all $a_i$ does not exceed $10^{14}$. -----Output----- Output the minimum sum that Lena needs to make all purchases. -----Examples----- Input 3 3 4 1 3 1 5 Output 8 Input 5 2 7 2 8 1 2 2 4 1 8 Output 12 -----Note----- In the first example, Lena can purchase the products in the following way: one item of product $3$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased), one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased). In total, she spends $8$ rubles. It can be proved that it is impossible to spend less. In the second example Lena can purchase the products in the following way: one item of product $1$ for $2$ rubles, two items of product $2$ for $2$ rubles for each, one item of product $5$ for $2$ rubles, one item of product $3$ for $1$ ruble, two items of product $4$ for $1$ ruble for each, one item of product $1$ for $1$ ruble. In total, she spends $12$ rubles.
import sys input = sys.stdin.buffer.readline n = int(input()) a = [] for i in range(n): b, c = map(int, input().split()) a.append([c, b]) a.sort(reverse=True) pre_cup = 0 cost = 0 i = 0 j = n - 1 add = 0 while i <= j: if pre_cup >= a[j][0]: cost += a[j][1] pre_cup += a[j][1] j -= 1 elif pre_cup + a[i][1] > a[j][0] and i < j: cost += (a[j][0] - pre_cup) * 2 cost += a[j][1] a[i][1] -= a[j][0] - pre_cup pre_cup += a[j][1] + a[j][0] - pre_cup j -= 1 else: rec_cup = a[i][1] limit = a[i][0] if pre_cup + rec_cup >= limit: cost += (limit - pre_cup) * 2 cost += rec_cup - (limit - pre_cup) pre_cup += rec_cup else: cost += rec_cup * 2 pre_cup += rec_cup i += 1 add += 1 print(cost)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β€” "PriceFixed". Here are some rules of that store: The store has an infinite number of items of every product. All products have the same price: $2$ rubles per item. For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!). Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β€” the number of products. Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β€” the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product. The sum of all $a_i$ does not exceed $10^{14}$. -----Output----- Output the minimum sum that Lena needs to make all purchases. -----Examples----- Input 3 3 4 1 3 1 5 Output 8 Input 5 2 7 2 8 1 2 2 4 1 8 Output 12 -----Note----- In the first example, Lena can purchase the products in the following way: one item of product $3$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased), one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased). In total, she spends $8$ rubles. It can be proved that it is impossible to spend less. In the second example Lena can purchase the products in the following way: one item of product $1$ for $2$ rubles, two items of product $2$ for $2$ rubles for each, one item of product $5$ for $2$ rubles, one item of product $3$ for $1$ ruble, two items of product $4$ for $1$ ruble for each, one item of product $1$ for $1$ ruble. In total, she spends $12$ rubles.
n, p, k, sm, nn = int(input()), [], 0, 0, 0 for i in range(n): a, b = map(int, input().split()) p.append([b, a]) nn += a p.sort() u = 0 d = n - 1 while k < nn: if p[u][0] > k: a = min(p[u][0] - k, p[d][1]) if k >= p[d][0]: sm += a else: sm += 2 * a k += a p[d][1] -= a if p[d][1] == 0: d -= 1 if k >= p[u][0]: sm += p[u][1] k += p[u][1] p[u][1] = 0 u += 1 else: sm += p[u][1] k += p[u][1] u += 1 print(sm)
ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR LIST 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 LIST VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR BIN_OP NUMBER VAR VAR VAR VAR VAR NUMBER VAR IF VAR VAR NUMBER NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β€” "PriceFixed". Here are some rules of that store: The store has an infinite number of items of every product. All products have the same price: $2$ rubles per item. For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!). Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β€” the number of products. Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β€” the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product. The sum of all $a_i$ does not exceed $10^{14}$. -----Output----- Output the minimum sum that Lena needs to make all purchases. -----Examples----- Input 3 3 4 1 3 1 5 Output 8 Input 5 2 7 2 8 1 2 2 4 1 8 Output 12 -----Note----- In the first example, Lena can purchase the products in the following way: one item of product $3$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased), one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased). In total, she spends $8$ rubles. It can be proved that it is impossible to spend less. In the second example Lena can purchase the products in the following way: one item of product $1$ for $2$ rubles, two items of product $2$ for $2$ rubles for each, one item of product $5$ for $2$ rubles, one item of product $3$ for $1$ ruble, two items of product $4$ for $1$ ruble for each, one item of product $1$ for $1$ ruble. In total, she spends $12$ rubles.
def solve(n: int, a) -> int: a.sort(key=lambda x: x[1]) left = 0 right = n - 1 bought = 0 cost = 0 while left <= right: if bought < a[left][1]: buying = min(a[right][0], a[left][1] - bought) a[right][0] -= buying bought += buying cost += 2 * buying if not a[right][0]: right -= 1 else: buying = a[left][0] bought += buying cost += buying left += 1 return cost n = int(input()) a = [] for _ in range(n): a.append(list(map(int, input().split()))) print(solve(n, a))
FUNC_DEF VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR BIN_OP NUMBER VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
This is the harder version of the problem. In this version, $1 \le n \le 10^6$ and $0 \leq a_i \leq 10^6$. You can hack this problem if you locked it. But you can hack the previous problem only if you locked both problems Christmas is coming, and our protagonist, Bob, is preparing a spectacular present for his long-time best friend Alice. This year, he decides to prepare $n$ boxes of chocolate, numbered from $1$ to $n$. Initially, the $i$-th box contains $a_i$ chocolate pieces. Since Bob is a typical nice guy, he will not send Alice $n$ empty boxes. In other words, at least one of $a_1, a_2, \ldots, a_n$ is positive. Since Alice dislikes coprime sets, she will be happy only if there exists some integer $k > 1$ such that the number of pieces in each box is divisible by $k$. Note that Alice won't mind if there exists some empty boxes. Charlie, Alice's boyfriend, also is Bob's second best friend, so he decides to help Bob by rearranging the chocolate pieces. In one second, Charlie can pick up a piece in box $i$ and put it into either box $i-1$ or box $i+1$ (if such boxes exist). Of course, he wants to help his friend as quickly as possible. Therefore, he asks you to calculate the minimum number of seconds he would need to make Alice happy. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^6$)Β β€” the number of chocolate boxes. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^6$)Β β€” the number of chocolate pieces in the $i$-th box. It is guaranteed that at least one of $a_1, a_2, \ldots, a_n$ is positive. -----Output----- If there is no way for Charlie to make Alice happy, print $-1$. Otherwise, print a single integer $x$Β β€” the minimum number of seconds for Charlie to help Bob make Alice happy. -----Examples----- Input 3 4 8 5 Output 9 Input 5 3 10 2 1 5 Output 2 Input 4 0 5 15 10 Output 0 Input 1 1 Output -1 -----Note----- In the first example, Charlie can move all chocolate pieces to the second box. Each box will be divisible by $17$. In the second example, Charlie can move a piece from box $2$ to box $3$ and a piece from box $4$ to box $5$. Each box will be divisible by $3$. In the third example, each box is already divisible by $5$. In the fourth example, since Charlie has no available move, he cannot help Bob make Alice happy.
import sys input = sys.stdin.buffer.readline n = int(input()) a = list(map(int, input().split())) if sum(a) == 1: print(-1) exit() sm = sum(a) nmax = 10**6 + 10 eratos = [(0) for i in range(nmax + 1)] prime = [] cnt = 2 while True: while cnt <= nmax and eratos[cnt]: cnt += 1 if cnt > nmax: break eratos[cnt] = 1 prime.append(cnt) for i in range(cnt**2, nmax + 1, cnt): eratos[i] = 1 dvls = set() for i in prime: while sm % i == 0: dvls.add(i) sm //= i if sm != 1: dvls.add(sm) ansls = [] ls = [] for dv in dvls: cnti = 0 ans = 0 if dv == 2: for i in range(n): if a[i] % 2: cnti += 1 if cnti % 2: pivot = i else: ans += i - pivot else: flg = 0 for i, ai in enumerate(a): rai = ai % dv if rai: cnti += rai if 1 <= cnti <= dv // 2: ls.append((i, rai)) elif not flg: pivot = i while ls: j, num = ls.pop() ans += (pivot - j) * num if cnti < dv: flg = 1 need = dv - cnti else: cnti -= dv ls.append((pivot, cnti)) elif cnti >= dv: ans += need * (i - pivot) cnti -= dv if cnti <= dv // 2: flg = 0 if rai - need: ls.append((i, rai - need)) else: pivot = i need = dv - cnti else: ans += rai * (i - pivot) need -= rai ansls.append(ans) print(min(ansls))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE NUMBER WHILE VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR WHILE BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR IF NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
This is the harder version of the problem. In this version, $1 \le n \le 10^6$ and $0 \leq a_i \leq 10^6$. You can hack this problem if you locked it. But you can hack the previous problem only if you locked both problems Christmas is coming, and our protagonist, Bob, is preparing a spectacular present for his long-time best friend Alice. This year, he decides to prepare $n$ boxes of chocolate, numbered from $1$ to $n$. Initially, the $i$-th box contains $a_i$ chocolate pieces. Since Bob is a typical nice guy, he will not send Alice $n$ empty boxes. In other words, at least one of $a_1, a_2, \ldots, a_n$ is positive. Since Alice dislikes coprime sets, she will be happy only if there exists some integer $k > 1$ such that the number of pieces in each box is divisible by $k$. Note that Alice won't mind if there exists some empty boxes. Charlie, Alice's boyfriend, also is Bob's second best friend, so he decides to help Bob by rearranging the chocolate pieces. In one second, Charlie can pick up a piece in box $i$ and put it into either box $i-1$ or box $i+1$ (if such boxes exist). Of course, he wants to help his friend as quickly as possible. Therefore, he asks you to calculate the minimum number of seconds he would need to make Alice happy. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^6$)Β β€” the number of chocolate boxes. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^6$)Β β€” the number of chocolate pieces in the $i$-th box. It is guaranteed that at least one of $a_1, a_2, \ldots, a_n$ is positive. -----Output----- If there is no way for Charlie to make Alice happy, print $-1$. Otherwise, print a single integer $x$Β β€” the minimum number of seconds for Charlie to help Bob make Alice happy. -----Examples----- Input 3 4 8 5 Output 9 Input 5 3 10 2 1 5 Output 2 Input 4 0 5 15 10 Output 0 Input 1 1 Output -1 -----Note----- In the first example, Charlie can move all chocolate pieces to the second box. Each box will be divisible by $17$. In the second example, Charlie can move a piece from box $2$ to box $3$ and a piece from box $4$ to box $5$. Each box will be divisible by $3$. In the third example, each box is already divisible by $5$. In the fourth example, since Charlie has no available move, he cannot help Bob make Alice happy.
def simple_div(x): if not x & 1: yield 2 while not x & 1: x >>= 1 i = 3 while i * i <= x: if x % i == 0: yield i while x % i == 0: x //= i i += 2 if x != 1: yield x def __main__(): n = int(input()) a = list(map(int, input().split())) sa = sum(a) a.pop() if sa == 1: print(-1) return res = 1 << 64 for d in simple_div(sa): tmp = 0 m = 0 half = d >> 1 for x in a: m = (x + m) % d tmp += m if m <= half else d - m if res <= tmp: break else: res = tmp print(res) __main__()
FUNC_DEF IF BIN_OP VAR NUMBER EXPR NUMBER WHILE BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER EXPR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
This is the harder version of the problem. In this version, $1 \le n \le 10^6$ and $0 \leq a_i \leq 10^6$. You can hack this problem if you locked it. But you can hack the previous problem only if you locked both problems Christmas is coming, and our protagonist, Bob, is preparing a spectacular present for his long-time best friend Alice. This year, he decides to prepare $n$ boxes of chocolate, numbered from $1$ to $n$. Initially, the $i$-th box contains $a_i$ chocolate pieces. Since Bob is a typical nice guy, he will not send Alice $n$ empty boxes. In other words, at least one of $a_1, a_2, \ldots, a_n$ is positive. Since Alice dislikes coprime sets, she will be happy only if there exists some integer $k > 1$ such that the number of pieces in each box is divisible by $k$. Note that Alice won't mind if there exists some empty boxes. Charlie, Alice's boyfriend, also is Bob's second best friend, so he decides to help Bob by rearranging the chocolate pieces. In one second, Charlie can pick up a piece in box $i$ and put it into either box $i-1$ or box $i+1$ (if such boxes exist). Of course, he wants to help his friend as quickly as possible. Therefore, he asks you to calculate the minimum number of seconds he would need to make Alice happy. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^6$)Β β€” the number of chocolate boxes. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^6$)Β β€” the number of chocolate pieces in the $i$-th box. It is guaranteed that at least one of $a_1, a_2, \ldots, a_n$ is positive. -----Output----- If there is no way for Charlie to make Alice happy, print $-1$. Otherwise, print a single integer $x$Β β€” the minimum number of seconds for Charlie to help Bob make Alice happy. -----Examples----- Input 3 4 8 5 Output 9 Input 5 3 10 2 1 5 Output 2 Input 4 0 5 15 10 Output 0 Input 1 1 Output -1 -----Note----- In the first example, Charlie can move all chocolate pieces to the second box. Each box will be divisible by $17$. In the second example, Charlie can move a piece from box $2$ to box $3$ and a piece from box $4$ to box $5$. Each box will be divisible by $3$. In the third example, each box is already divisible by $5$. In the fourth example, since Charlie has no available move, he cannot help Bob make Alice happy.
import sys def prime_decomposition(n): i = 2 table = [] while i * i <= n: while n % i == 0: n //= i table.append(i) i += 1 if n > 1: table.append(n) return table input = sys.stdin.readline N = int(input()) A = list(map(int, input().split())) su = sum(A) if su == 1: print(-1) exit() primes = sorted(set(prime_decomposition(su))) ans = 10**18 for p in primes: an = 0 half = p >> 1 cnt = 0 for a in A: a %= p cnt += a if cnt <= half: an += cnt elif cnt < p: an += p - cnt else: cnt -= p if cnt <= half: an += cnt else: an += p - cnt if ans <= an: break else: ans = min(ans, an) print(ans)
IMPORT FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE BIN_OP VAR VAR VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
a = input().strip() b = input().strip() n, m = len(a), len(b) p = m - 1 en = [-1] * m for j in range(n - 1, -1, -1): if a[j] == b[p]: en[p] = j p -= 1 if p == -1: break st = [-1] * m p = 0 for j in range(n): if a[j] == b[p]: st[p] = j p += 1 if p == m: break an = max(en[0], n - 1 - st[-1]) for i in range(m - 1): an = max(an, en[i + 1] - st[i] - 1) print(an)
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
import sys def input(): return sys.stdin.readline().rstrip() def slv(): S = input() T = input() n = len(T) needcntl = [0] * (n + 1) needcntr = [0] * (n + 1) idx = 0 i = 0 while i < n: if T[i] == S[idx]: needcntl[i + 1] = idx + 1 i += 1 idx += 1 S = S[::-1] T = T[::-1] idx = 0 i = 0 while i < n: if T[i] == S[idx]: needcntr[i + 1] = idx + 1 i += 1 idx += 1 ans = 0 for j in range(n + 1): remove_cnt = needcntl[j] + needcntr[n - j] ans = max(ans, len(S) - remove_cnt) print(ans) return def main(): t = 1 for i in range(t): slv() return main()
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN EXPR FUNC_CALL VAR
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
from sys import stdin s = stdin.readline().strip() s1 = stdin.readline().strip() dp = [(-1) for i in range(len(s1) + 1)] dp1 = [(-1) for i in range(len(s1) + 1)] x = 0 for i in range(len(s)): if s[i] == s1[x]: dp[x + 1] = i x += 1 if x >= len(s1): break x = len(s1) - 1 y = 1 dp[0] = -1 dp1[0] = len(s) for i in range(len(s) - 1, -1, -1): if s[i] == s1[x]: dp1[y] = i x -= 1 y += 1 if x < 0: break ans = 0 for i in range(len(s1) + 1): ans = max(dp1[len(s1) - i] - dp[i] - 1, ans) ans = max(dp[len(s1) - i] - dp1[i] - 1, ans) print(ans)
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
s = " " + input() + " " t = " " + input() + " " f = [0] cnt = 0 for i in range(1, len(s)): f.append(f[i - 1]) if s[i] == t[cnt + 1]: cnt += 1 f[i] += 1 s = s[::-1] t = t[::-1] g = [0] cnt = 0 for i in range(1, len(s)): g.append(g[i - 1]) if s[i] == t[cnt + 1]: cnt += 1 g[i] += 1 g = g[::-1] ans = l = r = 0 while l < len(s): while r < len(s) and f[l] + g[r] >= len(t) - 2: r += 1 ans = max(ans, r - l - 2) l += 1 print(ans)
ASSIGN VAR BIN_OP BIN_OP STRING FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP STRING FUNC_CALL VAR STRING ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
from sys import stdin, stdout input = stdin.readline s, t = list(map(str, stdin.read().split())) n, m = len(s), len(t) a = [0] * m b = [0] * m pos = 0 for i in range(n): if s[i] == t[pos]: a[pos] = i pos += 1 if pos == m: break pos = m - 1 for i in range(n - 1, -1, -1): if s[i] == t[pos]: b[pos] = i pos -= 1 if pos == -1: break res = max(b[0], n - (a[-1] + 1)) for i in range(m - 1): res = max(res, b[i + 1] - 1 - (a[i] + 1) + 1) print(res)
ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
s = input() t = input() def compute_prefix(s, t): prefix = [(0) for i in range(len(t) + 1)] i = 0 j = 0 while i < len(t): while s[j] != t[i]: j += 1 prefix[i + 1] = j + 1 i += 1 j += 1 return prefix prefix = compute_prefix(s, t) suffix = compute_prefix(s[::-1], t[::-1]) res = 0 for i in range(len(t) + 1): res = max(res, len(s) - (prefix[i] + suffix[len(t) - i])) print(res)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
from sys import stdin input = stdin.readline s = input() t = input() n = len(t) low = [(0) for i in range(len(t))] high = [(0) for i in range(len(t))] acc = 0 for i in range(n): acc_t = t[i] while acc_t != s[acc]: acc += 1 low[i] = acc acc += 1 acc = len(s) - 1 for i in range(len(t)): acc_t = t[n - i - 1] while acc_t != s[acc]: acc -= 1 high[n - i - 1] = acc acc -= 1 res = max(high[0], len(s) - low[n - 1] - 1) for i in range(n - 1): res = max(res, high[i + 1] - low[i] - 1) print(res)
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
s = input() t = input() i = 0 l = [] for c in t: while s[i] != c: i += 1 l.append(i) i += 1 i = len(s) - 1 r = [] for c in t[::-1]: while s[i] != c: i -= 1 r.append(i) i -= 1 r.reverse() mx = r[0] for i in range(len(t) - 1): mx = max(mx, r[i + 1] - l[i] - 1) mx = max(mx, len(s) - l[-1] - 1) print(mx)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR WHILE VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
from sys import setrecursionlimit as SRL from sys import stdin SRL(10**7) rd = stdin.readline rrd = lambda: map(int, rd().strip().split()) s = str(rd().strip()) t = str(rd().strip()) s = "0" + s canl = [0] * (len(s) + 10) canr = [0] * (len(s) + 10) j = 0 for i, v in enumerate(s): if j < len(t) and v == t[j]: j += 1 canl[i] = j j = len(t) - 1 for i in range(len(s) - 1, -1, -1): if j >= 0 and s[i] == t[j]: j -= 1 canr[i] = len(t) - j - 1 def check(x): if x > len(s): return False for i in range(1, len(s) - x + 1): l = i - 1 r = i + x if canl[l] + canr[r] >= len(t): return True return False l = 0 r = len(s) while l < r: mid = (l + r) // 2 if check(mid): l = mid + 1 else: r = mid print(r - 1)
EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER 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 BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
s = input() t = input() s2 = s[::-1] t2 = t[::-1] n = len(s) m = len(t) pos = [0] * m j = 0 for i in range(n): if s[i] == t[j]: pos[j] = i j += 1 if j == m: break pos2 = [0] * m j = 0 for i in range(n): if s2[i] == t2[j]: pos2[j] = i j += 1 if j == m: break ans = max(pos[0], pos2[0], n - pos[-1] - 1, n - pos2[-1] - 1) e = -(m - 1) for j in range(m - 1): ans = max(ans, abs(n - pos2[-(j + 2)] - 1 - pos[j]) - 1) print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER 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 VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
import sys input = sys.stdin.readline s = input() t = input() l, r = [0] * len(t), [0] * len(t) li, ri = 0, len(s) - 1 for i in range(len(t)): while s[li] != t[i]: li += 1 while s[ri] != t[-i - 1]: ri -= 1 l[i] = li r[-i - 1] = ri li += 1 ri -= 1 print( max( [r[0], len(s) - l[-1] - 1] + [(max(0, r[i] - l[i - 1]) - 1) for i in range(1, len(t))] ) )
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP LIST VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
s = input() t = input() n, m = len(s), len(t) i, j = 0, 0 first = [] last = [] while j < m: if s[i] == t[j]: first.append(i) i += 1 j += 1 else: i += 1 i, j = n - 1, m - 1 while j >= 0: if s[i] == t[j]: last.append(i) j -= 1 i -= 1 last = last[::-1] ans = max(last[0], n - first[-1] - 1) for i in range(m - 1): ans = max(ans, last[i + 1] - first[i] - 1) print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR LIST WHILE VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
from sys import setcheckinterval, stdin setcheckinterval(1000) def iin(): return int(stdin.readline()) def lin(): return list(map(int, stdin.readline().split())) s = input() t = input() n = len(s) d1 = {} d1[0] = -1 d2 = {} d2[0] = n l = len(t) ch = 0 for i in range(n): if ch == l: break if s[i] == t[ch]: ch += 1 d1[ch] = i s = list(s) s = s[::-1] t = list(t) t = t[::-1] ch = 0 for i in range(n): if ch == l: break if s[i] == t[ch]: ch += 1 d2[ch] = n - i - 1 ans = [] for i in range(l + 1): ans.append(d2[l - i] - d1[i]) print(max(ans) - 1)
EXPR FUNC_CALL VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
def main(): string = input() substring = input() size = len(substring) string_size = len(string) first = [] last = [] first.append(-1) index = 0 for i in range(string_size): c = string[i] if c == substring[index]: first.append(i) index = index + 1 if index >= size: break last.append(len(string)) index = size - 1 for i in range(string_size): c = string[string_size - i - 1] if c == substring[index]: last.append(string_size - i - 1) index = index - 1 if index < 0: break last = last[::-1] m = 0 for i in range(size + 1): length = last[i] - first[i] if length > m: m = length print(m - 1) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
s = input() t = input() r = [0] * len(t) for i in range(len(t) - 1, -1, -1): p = len(s) - 1 if i + 1 < len(t): p = r[i + 1] - 1 while t[i] != s[p]: p -= 1 r[i] = p ans = p = 0 for i in range(len(s)): rp = len(s) - 1 if p < len(t): rp = r[p] - 1 ans = max(rp - i + 1, ans) if p < len(t) and t[p] == s[i]: p += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
def find_first(s, t): idx = 0 l = [-1] for i in range(len(s)): if t[idx] == s[i]: l.append(i) idx += 1 if idx == len(t): return l return None def find_last(s, t): idx = len(t) - 1 l = [len(s)] for i in range(len(s) - 1, -1, -1): if t[idx] == s[i]: l.append(i) idx -= 1 if idx == -1: return l[::-1] return None bigstr = input() substr = input() first = find_first(bigstr, substr) last = find_last(bigstr, substr) mx = 0 for idx in range(len(first)): mx = max(mx, last[idx] - first[idx] - 1) print(mx)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN VAR RETURN NONE FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER RETURN VAR NUMBER RETURN NONE ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
s = input() t = input() j = len(t) - 1 last = [None] * len(t) for _i in range(len(s)): i = len(s) - _i - 1 if j >= 0 and s[i] == t[j]: last[j] = i j -= 1 answer = -1 j = 0 first = [None] * len(t) for i in range(len(s)): if j < len(t): if s[i] == t[j]: first[j] = i if j > 0: answer = max(answer, last[j] - first[j - 1] - 1) else: answer = last[j] j += 1 else: break answer = max(answer, len(s) - 1 - first[-1]) print(answer)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NONE FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NONE FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
s = input() t = input() n = len(t) b = [0] * (len(s) + 1) a = [0] * (len(s) + 1) c = 0 for i in range(len(s)): if c == n: b[i + 1] = c elif s[i] == t[c]: c += 1 b[i + 1] = c else: b[i + 1] = c c = n - 1 for i in range(len(s) - 1, -1, -1): if c == -1: a[i] = n elif s[i] == t[c]: c -= 1 a[i] = n - c - 1 else: a[i] = n - c - 1 st = 1 e = 1 m = 0 a = [0] + a b.append(0) while e < len(s): if b[st - 1] + a[e + 1] >= len(t): m = max(m, e - st + 1) e += 1 else: st += 1 if st > e: e += 1 if b[st - 1] + a[e + 1] >= len(t): m = max(m, e - st + 1) else: while st <= len(s): if b[st - 1] + a[e + 1] >= len(t): m = max(m, e - st + 1) break st += 1 print(m)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
big = input() small = input() pre = [] post = [] i = 0 j = 0 while 1: if j == len(small): break if big[i] == small[j]: pre.append(i) i += 1 j += 1 else: i += 1 big = big[::-1] small = small[::-1] i = 0 j = 0 while 1: if j == len(small): break if big[i] == small[j]: post.append(i) i += 1 j += 1 else: i += 1 for i in range(len(small)): post[i] = len(big) - 1 - post[i] post.reverse() ans = max(post[0], len(big) - (pre[-1] + 1)) for i in range(len(small) - 1): ans = max(ans, post[i + 1] - (pre[i] + 1)) print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
s = input() t = input() n, m = len(s), len(t) ans = 0 max_pos = [(-1) for i in range(m)] max_pos.append(n) j = m - 1 for i in range(n - 1, -1, -1): if j < 0: ans = max(ans, i + 1) break if j >= 0 and s[i] == t[j]: max_pos[j] = i j -= 1 curr = 0 for i in range(n): if curr < m and t[curr] == s[i]: ans = max(ans, max_pos[curr + 1] - i - 1) curr += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
def solver(): s = input().strip() t = input().strip() left = [len(s)] * len(t) right = [-1] * len(t) cnt = 0 for i in range(len(t)): while s[cnt] != t[i]: cnt += 1 left[i] = cnt cnt += 1 cnt = len(s) - 1 for i in range(len(t) - 1, -1, -1): while s[cnt] != t[i]: cnt -= 1 right[i] = cnt cnt -= 1 ans = max(len(s) - left[-1] - 1, right[0]) for i in range(len(t) - 1): ans = max(ans, right[i + 1] - left[i] - 1) print(ans) def main(): while 1: try: solver() except EOFError: break main()
FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF WHILE NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
s = input() t = input() s_pos = [len(s) for i in range(len(t))] i, j = len(s) - 1, len(t) - 1 while i > -1 and j > -1: if s[i] == t[j]: s_pos[j] = i j -= 1 i -= 1 p, q = 0, 0 ans = 0 while p < len(s): if q == len(t): ans = max(ans, len(s) - p) break remove = s_pos[q] - p ans = max(ans, remove) if s[p] == t[q]: q += 1 p += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
import sys in_file = sys.stdin s = in_file.readline().strip() t = in_file.readline().strip() L = [0] l = 0 R = [0] r = len(t) - 1 for i in range(len(s)): if l < len(t) and s[i] == t[l]: l += 1 L.append(l) if r >= 0 and s[len(s) - i - 1] == t[r]: r -= 1 R.append(len(t) - r - 1) Lc = [(-1) for i in range(len(t) + 1)] Rc = [(-1) for i in range(len(t) + 1)] for i in range(len(L)): if Lc[L[i]] == -1: Lc[L[i]] = i if Rc[R[i]] == -1: Rc[R[i]] = i res = len(s) for i in range(len(t) + 1): j = len(t) - i if Rc[j] + Lc[i] < res: res = Rc[j] + Lc[i] sys.stdout.write(str(len(s) - res)) sys.stdout.flush()
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
s = str(input()) t = str(input()) i = 0 j = 0 arr1 = [] while i < len(s): if s[i] == t[j]: arr1.append(i + 1) j += 1 i += 1 if j == len(t): break i = len(s) - 1 j = len(t) - 1 arr2 = [0] * len(t) while i >= 0: if s[i] == t[j]: arr2[j] = i + 1 j -= 1 i -= 1 if j == -1: break ans = max(arr2[0] - 1, len(s) - arr1[-1]) for i in range(len(t) - 1): ans = max(ans, arr2[i + 1] - arr1[i] - 1) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR WHILE VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
s = input() t = input() n = len(s) m = len(t) dp1 = [0] * (n + 1) for i in range(n): dp1[i + 1] = dp1[i] if dp1[i] < m and s[i] == t[dp1[i]]: dp1[i + 1] += 1 dp2 = [0] * (n + 1) for i in range(n - 1, -1, -1): dp2[i] = dp2[i + 1] if dp2[i + 1] < m and s[i] == t[-1 - dp2[i + 1]]: dp2[i] += 1 def check(ln): for i in range(n - ln + 1): if dp1[i] + dp2[i + ln] >= m: return True return False l, r = 0, n while r - l > 1: mid = (l + r) // 2 if check(mid): l = mid else: r = mid print(l)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER 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
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
s = input() n = len(s) t = input() m = len(t) rg = [0] * m j = m - 1 for i in range(n): if s[n - 1 - i] == t[j]: rg[j] = n - 1 - i if j == 0: break j -= 1 lg = [0] * m j = 0 for i in range(n): if s[i] == t[j]: lg[j] = i if j == m - 1: break j += 1 mx = max(rg[0], n - 1 - lg[-1]) for j in range(1, m): mx = max(mx, rg[j] - lg[j - 1] - 1) print(mx)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
s = input() t = input() n, m = len(s), len(t) l, r, j = [0] * m, [0] * m, 0 for i in range(m): while s[j] != t[i]: j += 1 l[i] = j j += 1 j = n - 1 for i in range(m - 1, -1, -1): while s[j] != t[i]: j -= 1 r[i] = j j -= 1 ans = max(0, r[0], n - 1 - l[-1]) for i in range(m - 1): ans = max(ans, r[i + 1] - l[i] - 1) print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
s = input() t = input() dfirst = {} dlast = {} mx = -99 x = 0 for i in range(len(t)): while s[x] != t[i]: x += 1 dfirst[i] = x if i == len(t) - 1: mx = max(mx, len(s) - x - 1) x += 1 x = len(s) - 1 trev = t[::-1] for i in range(len(trev)): while s[x] != trev[i]: x -= 1 dlast[len(t) - 1 - i] = x if i == len(trev) - 1: mx = max(mx, x) x -= 1 for i in range(1, len(t)): mx = max(mx, dlast[i] - dfirst[i - 1] - 1) print(mx)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
def f(n): for i in range(-1, lt): if right[i + 1] - left[i] > n: return True return False s = input() t = input() ls = len(s) lt = len(t) right = dict() left = dict() i = 0 j = 0 ls = len(s) left[-1] = -1 while i < lt: if s[j] == t[i]: left[i] = j i += 1 j += 1 j = ls - 1 i = lt - 1 right[lt] = ls while i >= 0: if s[j] == t[i]: right[i] = j i -= 1 j -= 1 low = 0 high = ls - 1 while low <= high: mid = (high + low) // 2 if f(mid): low = mid + 1 else: high = mid - 1 print(high)
FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
s = list(input()) t = list(input()) fo = [] ti = 0 for i, c in enumerate(s): if c == t[ti]: ti += 1 fo.append(i) if ti == len(t): break lo = [] ti = len(t) - 1 for i in reversed(range(len(s))): c = s[i] if c == t[ti]: ti -= 1 lo.append(i) if ti == -1: break mx = max(len(s) - fo[-1] - 1, lo[-1]) lo.reverse() for i in range(1, len(t)): mx = max(mx, lo[i] - fo[i - 1] - 1) print(mx)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
s = input() t = input() ar1 = [] i = 0 for j in range(len(s)): if i < len(t) and s[j] == t[i]: ar1.append(j) i += 1 i = len(t) - 1 ar2 = [0] * len(t) j = len(s) - 1 while j >= 0: if i >= 0 and s[j] == t[i]: ar2[i] = j i -= 1 j -= 1 ans = ar1[0] ans = max(ans, ar2[0]) ans = max(ans, len(s) - ar1[-1] - 1) ans = max(ans, len(s) - ar2[-1] - 1) for i in range(len(t) - 1): ans = max(ans, ar2[i + 1] - ar1[i] - 1) print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
s1 = list(input()) r_s1 = s1[::-1] s2 = list(input()) r_s2 = s2[::-1] l1 = len(s1) l2 = len(s2) arr1 = [] arr2 = [] i = 0 j = 0 while i < l1 and j < l2: if s1[i] == s2[j]: arr1.append(i) j += 1 i += 1 i = 0 j = 0 while i < l1 and j < l2: if r_s1[i] == r_s2[j]: arr2.append(l1 - 1 - i) j += 1 i += 1 ans = 0 for i in range(1, l2): ans = max(ans, arr1[i] - arr1[i - 1] - 1) ans = max(ans, arr2[i - 1] - arr2[i] - 1) for i in range(l2 - 1): ans = max(ans, arr2[l2 - 2 - i] - arr1[i] - 1) ans = max(ans, l1 - arr1[-1] - 1, arr1[0]) ans = max(ans, arr2[-1], l1 - arr2[0] - 1) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
s = input() t = input() n = len(s) m = len(t) le = [] ri = [] ans = 0 p = 0 for i in range(n): if p < m: if s[i] == t[p]: le.append(i) if p == m - 1: ans = max(ans, n - 1 - i) p += 1 p = m - 1 for j in range(n - 1, -1, -1): if p >= 0: if s[j] == t[p]: ri.append(j) if p == 0: ans = max(ans, j - 0) p -= 1 ri.reverse() for k in range(m - 1): ans = max(ans, ri[k + 1] - le[k] - 1) print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
s = input() t = input() pr = [] pospr = [] j = 0 for i in range(len(s)): if j != len(t) and s[i] == t[j]: j += 1 pospr.append(i) pr.append(j) suf = [0] * len(s) j = len(t) - 1 possuf = [0] * len(t) for i in range(len(s) - 1, -1, -1): if j != -1 and s[i] == t[j]: possuf[j] = i j -= 1 suf[i] = len(t) - j - 1 ans = 0 for i in range(len(s)): sym = pr[i - 1] if i != 0 else 0 last = possuf[sym] if sym != len(t) else len(s) if last - i > ans: ans = last - i print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
str1 = input() str2 = input() n, m = len(str1), len(str2) pos, i = [], 0 for p in range(n): if i == m: break if str1[p] == str2[i]: pos.append(p) i += 1 pos = [-1] + pos + [n] ans = 0 for i in range(1, m + 2): ans = max(ans, pos[i] - pos[i - 1] - 1) i = 0 for p in range(n - 1, -1, -1): if i == m: break if str1[p] == str2[m - i - 1]: ans = max(ans, p - pos[m - i - 1] - 1) i += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR LIST VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
def solve(): s1 = input() s2 = input() i = 0 left = [] for c in s2: while c != s1[i]: i += 1 left.append(i) i += 1 i = -1 right = [] for c in s2[::-1]: while c != s1[i]: i -= 1 right.append(len(s1) + i) i -= 1 right.reverse() mx = 0 for i in range(len(s2) - 1): mx = max(mx, right[i + 1] - left[i] - 1) mx = max(mx, len(s1) - left[-1] - 1) mx = max(mx, right[0]) print(mx) for case in range(1): solve()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR WHILE VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
s = input() t = input() n = len(t) first = [0] * n last = [0] * n p = 0 i = 0 while p < n: if s[i] == t[p]: first[p] = i p += 1 i += 1 p = n - 1 i = len(s) - 1 while p >= 0: if s[i] == t[p]: last[p] = i p -= 1 i -= 1 ans = max(last[0], len(s) - 1 - first[-1]) for i in range(n - 1): ans = max(ans, last[i + 1] - first[i] - 1) print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
s = input() t = input() fp = [-1] tp = 0 for i in range(len(s)): if s[i] == t[tp]: fp.append(i) tp += 1 if tp == len(t): break lp = [len(s)] tp = len(t) - 1 for i in range(len(s) - 1, -1, -1): if s[i] == t[tp]: lp.append(i) tp -= 1 if tp == -1: break lp.reverse() print(max([(l - f) for l, f in zip(lp, fp)]) - 1)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
s = input() t = input() left = [-1] rightReversed = [len(s)] i = 0 for x in range(len(s)): if t[i] == s[x]: left.append(x) i += 1 if i >= len(t): break i = len(t) - 1 for x in range(len(s) - 1, -1, -1): if t[i] == s[x]: rightReversed.append(x) i -= 1 if i < 0: break right = rightReversed[::-1] maxN = 0 for x in range(len(t) + 1): maxN = max(right[x] - left[x], maxN) print(maxN - 1)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
s = input() t = input() pos = [[-1, -1] for i in range(len(t))] ptr = 0 for i, c in enumerate(t): while s[ptr] != c: ptr += 1 pos[i][0] = ptr ptr += 1 ptr = len(s) - 1 for i in range(len(t) - 1, -1, -1): c = t[i] while s[ptr] != c: ptr -= 1 pos[i][1] = ptr ptr -= 1 best = max(pos[0][1], len(s) - pos[-1][0] - 1) for i in range(1, len(pos)): best = max(best, pos[i][1] - pos[i - 1][0] - 1) print(best)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
from sys import stdin, stdout s = stdin.readline().strip() t = stdin.readline().strip() leftmost = [-1] ptr1 = ptr2 = 0 while ptr2 < len(t): if s[ptr1] == t[ptr2]: leftmost.append(ptr1) ptr1 += 1 ptr2 += 1 else: ptr1 += 1 rightmost = [] ptr1 = len(s) - 1 ptr2 = len(t) - 1 while ptr2 >= 0: if s[ptr1] == t[ptr2]: rightmost.append(ptr1) ptr1 -= 1 ptr2 -= 1 else: ptr1 -= 1 rightmost.reverse() rightmost.append(len(s)) ans = -1 for i in range(len(leftmost)): ans = max(ans, rightmost[i] - leftmost[i] - 1) stdout.write(str(ans) + "\n")
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
big = input() small = input() left = [0] * (len(small) + 1) left[0] = -1 wsk = -1 for i in range(1, len(small) + 1): while True: wsk += 1 if big[wsk] == small[i - 1]: left[i] = wsk break big = big[::-1] small = small[::-1] right = [0] * (len(small) + 1) right[0] = -1 wsk = -1 for i in range(1, len(small) + 1): while True: wsk += 1 if big[wsk] == small[i - 1]: right[i] = wsk break wyn = 100000000 for i in range(len(left)): wyn = min(wyn, right[i] + left[len(left) - 1 - i] + 2) print(len(big) - wyn)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
l1 = [i for i in input()] l2 = [i for i in input()] length1 = len(l1) length2 = len(l2) ij = 0 ji = len(l2) - 1 count1 = [] count2 = [] for i in range(length1): if ij < length2: if l1[i] == l2[ij]: ij += 1 count1.append(i) if ji >= 0: if l1[-i - 1] == l2[ji]: ji -= 1 count2.append(length1 - i - 1) if ij == length2 and ji == -1: break final = [count2[len(count2) - 1 - 0]] for i in range(length2 - 1): x = count2[-i - 2] - count1[i] - 1 if x > final[0]: final[0] = x print(max(final[0], length1 - 1 - count1[len(count1) - 1]))
ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR LIST VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
def process(s, t): l = [] r = [0] * len(t) j = 0 for i in range(len(t)): while s[j] != t[i]: j += 1 l.append(j) j += 1 j = len(s) - 1 for i in reversed(range(len(t))): while s[j] != t[i]: j -= 1 r[i] = j j -= 1 res = max(r[0], len(s) - 1 - l[-1]) for i in range(len(t) - 1): res = max(res, r[i + 1] - l[i] - 1) return res s = input() t = input() print(process(s, t))
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
s = input() t = input() f, b = [], [] cur = len(t) - 1 for i in range(len(s) - 1, -1, -1): if s[i] == t[cur]: b.append(i) cur -= 1 if cur == -1: break b.sort() cur = 0 for i in range(len(s)): if s[i] == t[cur]: f.append(i) cur += 1 if cur == len(t): break ans = max(b[0], len(s) - f[-1] - 1) bbb = 0 bbb ^= 1 bbb += 1 for i in range(len(f) - 1): ans = max(ans, abs(b[i + 1] - 1 - f[i])) print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
s = input() t = input() n = len(s) m = len(t) l = [-1] r = [n] c = 0 for i in range(n): if s[i] == t[c]: l.append(i) c += 1 if c >= m: break c = 0 for i in range(n): if s[-1 - i] == t[-1 - c]: r.append(n - 1 - i) c += 1 if c >= m: break mx = 0 for i in range(m + 1): mx = max(mx, r[m - i] - l[i] - 1) print(mx)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
s = input() t = input() lastL = [0] * len(t) lastR = [0] * len(t) j = 0 for i in range(len(s)): if s[i] == t[j]: lastL[j] = i j += 1 if j >= len(t): break j = len(t) - 1 for i in range(len(s) - 1, -1, -1): if s[i] == t[j]: lastR[j] = i j -= 1 if j < 0: break max_gap = max(lastR[0], len(s) - 1 - lastL[-1]) for i in range(len(t) - 1): max_gap = max(max_gap, lastR[i + 1] - 1 - lastL[i]) print(max_gap)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
s = input() t = input() forward = [] backward = [] rptr = 0 for i in range(len(s)): if rptr == len(t): break if s[i] == t[rptr]: forward.append(i) rptr += 1 rptr = len(t) - 1 for i in range(len(s) - 1, -1, -1): if rptr == -1: break if s[i] == t[rptr]: backward.append(i) rptr -= 1 backward = backward[::-1] backward.append(forward[-1]) mx = max(forward[0], len(s) - 1 - backward[-2], len(s) - 1 - forward[-1], backward[0]) for i in range(len(t)): mx = max(mx, abs(forward[i] - backward[i + 1]) - 1) print(mx)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
import sys inf = float("inf") mod, MOD = 1000000007, 998244353 def is_subsq(a, b): i = 0 j = 0 while i < len(b) and j < len(a): if a[j] == b[i]: j += 1 i += 1 return j == len(a) def get_array(): return list(map(int, sys.stdin.readline().strip().split())) def get_ints(): return map(int, sys.stdin.readline().strip().split()) def input(): return sys.stdin.readline().strip() s = input() t = input() n = len(s) m = len(t) l = [-1] r = [n] c = 0 for i in range(n): if s[i] == t[c]: l.append(i) c += 1 if c >= m: break c = 0 for i in range(n): if s[-1 - i] == t[-1 - c]: r.append(n - 1 - i) c += 1 if c >= m: break mx = 0 for i in range(m + 1): mx = max(mx, r[m - i] - l[i] - 1) print(mx)
IMPORT ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER NUMBER 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 RETURN VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
a = input() b = input() n = len(a) m = len(b) i = 0 j = 0 f, bb = [0] * m, [0] * m while j != m: if a[i] == b[j]: f[j] = i i += 1 j += 1 else: i += 1 i = 0 j = 0 while j != m: if a[n - 1 - i] == b[m - 1 - j]: bb[m - 1 - j] = n - 1 - i i += 1 j += 1 else: i += 1 ans = max(n - f[-1] - 1, bb[0]) for i in range(m - 1): ans = max(ans, bb[i + 1] - f[i] - 1) print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
s = input() t = input() sl, tl = len(s), len(t) left = [] right = [] x = 0 for i in range(sl): if x < tl and s[i] == t[x]: left.append(i) x += 1 x = tl - 1 for i in range(sl - 1, -1, -1): if x >= 0 and s[i] == t[x]: right.append(i) x -= 1 right.reverse() if tl == 1: print(max(max(left[0], sl - left[0] - 1), max(right[0], sl - right[0] - 1))) else: ans = max(sl - left[-1] - 1, right[0]) for i in range(1, tl): ans = max(ans, right[i] - left[i - 1] - 1) print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
s = input() t = input() l = 0 pre = [] suf = [] for i in range(len(s)): if s[i] == t[l]: pre.append(i) l += 1 if l == len(t): break l = len(t) - 1 for i in range(len(s) - 1, -1, -1): if s[i] == t[l]: suf.append(i) l -= 1 if l == -1: break pre.sort() suf.sort() ans = 0 ans = max(suf[0], len(s) - pre[-1] - 1) for i in range(len(t) - 1): ans = max(ans, abs(suf[i + 1] - pre[i] - 1)) print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer β€” the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
import sys s = sys.stdin.readline() t = sys.stdin.readline() sn, tn = len(s), len(t) left, right = [0] * tn, [0] * tn prev, last = -1, sn for i in range(tn): left[i] = prev = s.find(t[i], prev + 1) right[tn - i - 1] = last = s.rfind(t[tn - i - 1], 0, last) ans = max(right[i + 1] - left[i] - 1 for i in range(tn - 1)) print(max(right[0], sn - left[-1] - 1, ans))
IMPORT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR