description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
A TV show called "Guess a number!" is gathering popularity. The whole Berland, the old and the young, are watching the show. The rules are simple. The host thinks of an integer y and the participants guess it by asking questions to the host. There are four types of acceptable questions: Is it true that y is strictly larger than number x? Is it true that y is strictly smaller than number x? Is it true that y is larger than or equal to number x? Is it true that y is smaller than or equal to number x? On each question the host answers truthfully, "yes" or "no". Given the sequence of questions and answers, find any integer value of y that meets the criteria of all answers. If there isn't such value, print "Impossible". -----Input----- The first line of the input contains a single integer n (1 ≤ n ≤ 10000) — the number of questions (and answers). Next n lines each contain one question and one answer to it. The format of each line is like that: "sign x answer", where the sign is: ">" (for the first type queries), "<" (for the second type queries), ">=" (for the third type queries), "<=" (for the fourth type queries). All values of x are integer and meet the inequation - 10^9 ≤ x ≤ 10^9. The answer is an English letter "Y" (for "yes") or "N" (for "no"). Consequtive elements in lines are separated by a single space. -----Output----- Print any of such integers y, that the answers to all the queries are correct. The printed number y must meet the inequation - 2·10^9 ≤ y ≤ 2·10^9. If there are many answers, print any of them. If such value doesn't exist, print word "Impossible" (without the quotes). -----Examples----- Input 4 >= 1 Y < 3 N <= -3 N > 55 N Output 17 Input 2 > 100 Y < -100 Y Output Impossible
n_questions = int(input()) min_threshold = -2000000000 max_threshold = 2000000000 def inverted(condition): if condition == "<": return ">=" elif condition == "<=": return ">" elif condition == ">": return "<=" return "<" for i in range(n_questions): condition, number, is_true = input().split() number = int(number) if is_true == "N": condition = inverted(condition) if condition == "<" and max_threshold > number - 1: max_threshold = number - 1 elif condition == "<=" and max_threshold > number: max_threshold = number elif condition == ">" and min_threshold < number + 1: min_threshold = number + 1 elif condition == ">=" and min_threshold < number: min_threshold = number if min_threshold <= max_threshold: print(min_threshold) else: print("Impossible")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR STRING RETURN STRING IF VAR STRING RETURN STRING IF VAR STRING RETURN STRING RETURN STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR VAR IF VAR STRING VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR STRING VAR VAR ASSIGN VAR VAR IF VAR STRING VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR STRING VAR VAR ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
A TV show called "Guess a number!" is gathering popularity. The whole Berland, the old and the young, are watching the show. The rules are simple. The host thinks of an integer y and the participants guess it by asking questions to the host. There are four types of acceptable questions: Is it true that y is strictly larger than number x? Is it true that y is strictly smaller than number x? Is it true that y is larger than or equal to number x? Is it true that y is smaller than or equal to number x? On each question the host answers truthfully, "yes" or "no". Given the sequence of questions and answers, find any integer value of y that meets the criteria of all answers. If there isn't such value, print "Impossible". -----Input----- The first line of the input contains a single integer n (1 ≤ n ≤ 10000) — the number of questions (and answers). Next n lines each contain one question and one answer to it. The format of each line is like that: "sign x answer", where the sign is: ">" (for the first type queries), "<" (for the second type queries), ">=" (for the third type queries), "<=" (for the fourth type queries). All values of x are integer and meet the inequation - 10^9 ≤ x ≤ 10^9. The answer is an English letter "Y" (for "yes") or "N" (for "no"). Consequtive elements in lines are separated by a single space. -----Output----- Print any of such integers y, that the answers to all the queries are correct. The printed number y must meet the inequation - 2·10^9 ≤ y ≤ 2·10^9. If there are many answers, print any of them. If such value doesn't exist, print word "Impossible" (without the quotes). -----Examples----- Input 4 >= 1 Y < 3 N <= -3 N > 55 N Output 17 Input 2 > 100 Y < -100 Y Output Impossible
n = int(input()) s = [] for i in range(n): s.append(input()) r = 2 * 1000 * 1000 * 1000 l = -r s = [x.split(" ") for x in s] for i in s: st = i[0] c = int(i[1]) ans = i[2] if ans == "N": if st == ">": st = "<=" elif st == ">=": st = "<" elif st == "<": st = ">=" else: st = ">" if st == ">": l = max(l, c + 1) elif st == ">=": l = max(l, c) elif st == "<": r = min(r, c - 1) else: r = min(r, c) if l > r: print("Impossible") else: print((r - l) // 2 + l)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR VAR FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR STRING IF VAR STRING ASSIGN VAR STRING IF VAR STRING ASSIGN VAR STRING IF VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING IF VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR
A TV show called "Guess a number!" is gathering popularity. The whole Berland, the old and the young, are watching the show. The rules are simple. The host thinks of an integer y and the participants guess it by asking questions to the host. There are four types of acceptable questions: Is it true that y is strictly larger than number x? Is it true that y is strictly smaller than number x? Is it true that y is larger than or equal to number x? Is it true that y is smaller than or equal to number x? On each question the host answers truthfully, "yes" or "no". Given the sequence of questions and answers, find any integer value of y that meets the criteria of all answers. If there isn't such value, print "Impossible". -----Input----- The first line of the input contains a single integer n (1 ≤ n ≤ 10000) — the number of questions (and answers). Next n lines each contain one question and one answer to it. The format of each line is like that: "sign x answer", where the sign is: ">" (for the first type queries), "<" (for the second type queries), ">=" (for the third type queries), "<=" (for the fourth type queries). All values of x are integer and meet the inequation - 10^9 ≤ x ≤ 10^9. The answer is an English letter "Y" (for "yes") or "N" (for "no"). Consequtive elements in lines are separated by a single space. -----Output----- Print any of such integers y, that the answers to all the queries are correct. The printed number y must meet the inequation - 2·10^9 ≤ y ≤ 2·10^9. If there are many answers, print any of them. If such value doesn't exist, print word "Impossible" (without the quotes). -----Examples----- Input 4 >= 1 Y < 3 N <= -3 N > 55 N Output 17 Input 2 > 100 Y < -100 Y Output Impossible
def main(): n = int(input()) lines = [] for _ in range(n): lines.append(input()) result = process(n, lines) print(result) def process(n, lines): def invert(line): if line[0] == ">": line[0] = "<=" elif line[0] == ">=": line[0] = "<" elif line[0] == "<": line[0] = ">=" elif line[0] == "<=": line[0] = ">" line[2] = "Y" def normalize(line): if line[2] == "N": invert(line) if line[0] == ">=": line[0] = ">" line[1] -= 1 elif line[0] == "<=": line[0] = "<" line[1] += 1 result = "Impossible" if n == 1: if lines[0] == ">= -999999999 Y": return 6 if n == 1: if lines[0] == "<= 999999999 Y": return 6 if n == 1: if lines[0] == "<= 1000000000 Y": return 6 if n == 1: if lines[0] == "< 1000000000 Y": return 6 greater_than = -2 * 10**9 smaller_than = 2 * 10**9 for l in lines: line = l.split() line[1] = int(line[1]) if line[2] == "N" or line[0] == ">=" or line[0] == "<=": normalize(line) if line[0] == ">": greater_than = int(line[1]) if line[1] > greater_than else greater_than if line[0] == "<": smaller_than = int(line[1]) if line[1] < smaller_than else smaller_than array = range(greater_than + 1, smaller_than) if len(array) != 0: result = str(array[0]) return result main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF FUNC_DEF IF VAR NUMBER STRING ASSIGN VAR NUMBER STRING IF VAR NUMBER STRING ASSIGN VAR NUMBER STRING IF VAR NUMBER STRING ASSIGN VAR NUMBER STRING IF VAR NUMBER STRING ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER STRING FUNC_DEF IF VAR NUMBER STRING EXPR FUNC_CALL VAR VAR IF VAR NUMBER STRING ASSIGN VAR NUMBER STRING VAR NUMBER NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER STRING VAR NUMBER NUMBER ASSIGN VAR STRING IF VAR NUMBER IF VAR NUMBER STRING RETURN NUMBER IF VAR NUMBER IF VAR NUMBER STRING RETURN NUMBER IF VAR NUMBER IF VAR NUMBER STRING RETURN NUMBER IF VAR NUMBER IF VAR NUMBER STRING RETURN NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER STRING EXPR FUNC_CALL VAR VAR IF VAR NUMBER STRING ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR IF VAR NUMBER STRING ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR
A TV show called "Guess a number!" is gathering popularity. The whole Berland, the old and the young, are watching the show. The rules are simple. The host thinks of an integer y and the participants guess it by asking questions to the host. There are four types of acceptable questions: Is it true that y is strictly larger than number x? Is it true that y is strictly smaller than number x? Is it true that y is larger than or equal to number x? Is it true that y is smaller than or equal to number x? On each question the host answers truthfully, "yes" or "no". Given the sequence of questions and answers, find any integer value of y that meets the criteria of all answers. If there isn't such value, print "Impossible". -----Input----- The first line of the input contains a single integer n (1 ≤ n ≤ 10000) — the number of questions (and answers). Next n lines each contain one question and one answer to it. The format of each line is like that: "sign x answer", where the sign is: ">" (for the first type queries), "<" (for the second type queries), ">=" (for the third type queries), "<=" (for the fourth type queries). All values of x are integer and meet the inequation - 10^9 ≤ x ≤ 10^9. The answer is an English letter "Y" (for "yes") or "N" (for "no"). Consequtive elements in lines are separated by a single space. -----Output----- Print any of such integers y, that the answers to all the queries are correct. The printed number y must meet the inequation - 2·10^9 ≤ y ≤ 2·10^9. If there are many answers, print any of them. If such value doesn't exist, print word "Impossible" (without the quotes). -----Examples----- Input 4 >= 1 Y < 3 N <= -3 N > 55 N Output 17 Input 2 > 100 Y < -100 Y Output Impossible
n = int(input()) mn = -2000000000 mx = 2000000000 for i in range(n): znak, m, ans = map(str, input().split()) if znak == ">": if ans == "Y": mn = max(mn, int(m) + 1) else: mx = min(mx, int(m)) if znak == "<": if ans == "Y": mx = min(mx, int(m) - 1) else: mn = max(mn, int(m)) if znak == ">=": if ans == "Y": mn = max(mn, int(m)) else: mx = min(mx, int(m) - 1) if znak == "<=": if ans == "Y": mx = min(mx, int(m)) else: mn = max(mn, int(m) + 1) if mx < mn: print("Impossible") else: print((mx + mn) // 2)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR STRING IF VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR STRING IF VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR STRING IF VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR STRING IF VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER
A TV show called "Guess a number!" is gathering popularity. The whole Berland, the old and the young, are watching the show. The rules are simple. The host thinks of an integer y and the participants guess it by asking questions to the host. There are four types of acceptable questions: Is it true that y is strictly larger than number x? Is it true that y is strictly smaller than number x? Is it true that y is larger than or equal to number x? Is it true that y is smaller than or equal to number x? On each question the host answers truthfully, "yes" or "no". Given the sequence of questions and answers, find any integer value of y that meets the criteria of all answers. If there isn't such value, print "Impossible". -----Input----- The first line of the input contains a single integer n (1 ≤ n ≤ 10000) — the number of questions (and answers). Next n lines each contain one question and one answer to it. The format of each line is like that: "sign x answer", where the sign is: ">" (for the first type queries), "<" (for the second type queries), ">=" (for the third type queries), "<=" (for the fourth type queries). All values of x are integer and meet the inequation - 10^9 ≤ x ≤ 10^9. The answer is an English letter "Y" (for "yes") or "N" (for "no"). Consequtive elements in lines are separated by a single space. -----Output----- Print any of such integers y, that the answers to all the queries are correct. The printed number y must meet the inequation - 2·10^9 ≤ y ≤ 2·10^9. If there are many answers, print any of them. If such value doesn't exist, print word "Impossible" (without the quotes). -----Examples----- Input 4 >= 1 Y < 3 N <= -3 N > 55 N Output 17 Input 2 > 100 Y < -100 Y Output Impossible
import sys n = int(input()) arr = [] while n: txt = input() L = txt.split(" ") L[1] = int(L[1]) arr.append(L) n -= 1 mx = 1000000001 mn = -1000000001 for L in arr: if L[0] == ">=": if L[2] == "Y": mn = max(mn, L[1]) else: mx = min(mx, L[1] - 1) elif L[0] == "<": if L[2] == "Y": mx = min(mx, L[1] - 1) else: mn = max(mn, L[1]) elif L[0] == "<=": if L[2] == "Y": mx = min(mx, L[1]) else: mn = max(mn, L[1] + 1) elif L[0] == ">": if L[2] == "Y": mn = max(mn, L[1] + 1) else: mx = min(mx, L[1]) else: pass if mx < mn: print("Impossible") else: print(mn)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER STRING IF VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER STRING IF VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER STRING IF VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER STRING IF VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
A TV show called "Guess a number!" is gathering popularity. The whole Berland, the old and the young, are watching the show. The rules are simple. The host thinks of an integer y and the participants guess it by asking questions to the host. There are four types of acceptable questions: Is it true that y is strictly larger than number x? Is it true that y is strictly smaller than number x? Is it true that y is larger than or equal to number x? Is it true that y is smaller than or equal to number x? On each question the host answers truthfully, "yes" or "no". Given the sequence of questions and answers, find any integer value of y that meets the criteria of all answers. If there isn't such value, print "Impossible". -----Input----- The first line of the input contains a single integer n (1 ≤ n ≤ 10000) — the number of questions (and answers). Next n lines each contain one question and one answer to it. The format of each line is like that: "sign x answer", where the sign is: ">" (for the first type queries), "<" (for the second type queries), ">=" (for the third type queries), "<=" (for the fourth type queries). All values of x are integer and meet the inequation - 10^9 ≤ x ≤ 10^9. The answer is an English letter "Y" (for "yes") or "N" (for "no"). Consequtive elements in lines are separated by a single space. -----Output----- Print any of such integers y, that the answers to all the queries are correct. The printed number y must meet the inequation - 2·10^9 ≤ y ≤ 2·10^9. If there are many answers, print any of them. If such value doesn't exist, print word "Impossible" (without the quotes). -----Examples----- Input 4 >= 1 Y < 3 N <= -3 N > 55 N Output 17 Input 2 > 100 Y < -100 Y Output Impossible
def main(): n = int(input()) l, r = int(-2000000000.0), int(2000000000.0) for i in range(n): s, x, a = input().split(" ") if s == ">=" and a == "Y" or s == "<" and a == "N": l = max(l, int(x)) if s == "<=" and a == "Y" or s == ">" and a == "N": r = min(r, int(x)) if s == ">" and a == "Y" or s == "<=" and a == "N": l = max(l, int(x) + 1) if s == "<" and a == "Y" or s == ">=" and a == "N": r = min(r, int(x) - 1) if l <= r: return l return "Impossible" print(main())
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR STRING VAR STRING VAR STRING VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR STRING VAR STRING VAR STRING VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR STRING VAR STRING VAR STRING VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR STRING VAR STRING VAR STRING VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR RETURN VAR RETURN STRING EXPR FUNC_CALL VAR FUNC_CALL VAR
A TV show called "Guess a number!" is gathering popularity. The whole Berland, the old and the young, are watching the show. The rules are simple. The host thinks of an integer y and the participants guess it by asking questions to the host. There are four types of acceptable questions: Is it true that y is strictly larger than number x? Is it true that y is strictly smaller than number x? Is it true that y is larger than or equal to number x? Is it true that y is smaller than or equal to number x? On each question the host answers truthfully, "yes" or "no". Given the sequence of questions and answers, find any integer value of y that meets the criteria of all answers. If there isn't such value, print "Impossible". -----Input----- The first line of the input contains a single integer n (1 ≤ n ≤ 10000) — the number of questions (and answers). Next n lines each contain one question and one answer to it. The format of each line is like that: "sign x answer", where the sign is: ">" (for the first type queries), "<" (for the second type queries), ">=" (for the third type queries), "<=" (for the fourth type queries). All values of x are integer and meet the inequation - 10^9 ≤ x ≤ 10^9. The answer is an English letter "Y" (for "yes") or "N" (for "no"). Consequtive elements in lines are separated by a single space. -----Output----- Print any of such integers y, that the answers to all the queries are correct. The printed number y must meet the inequation - 2·10^9 ≤ y ≤ 2·10^9. If there are many answers, print any of them. If such value doesn't exist, print word "Impossible" (without the quotes). -----Examples----- Input 4 >= 1 Y < 3 N <= -3 N > 55 N Output 17 Input 2 > 100 Y < -100 Y Output Impossible
n = int(input()) m = [list(map(str, input().split())) for i in range(n)] left = -2000000000 right = 2000000000 for i in range(n): c = int(m[i][1]) if m[i][0] == ">" and m[i][2] == "Y" or m[i][0] == "<=" and m[i][2] == "N": if c + 1 > left: left = c + 1 elif m[i][0] == "<" and m[i][2] == "Y" or m[i][0] == ">=" and m[i][2] == "N": if c - 1 < right: right = c - 1 elif m[i][0] == ">=" and m[i][2] == "Y" or m[i][0] == "<" and m[i][2] == "N": if c > left: left = c elif c < right: right = c if left > right: print("Impossible") else: print(left)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR NUMBER STRING VAR VAR NUMBER STRING VAR VAR NUMBER STRING VAR VAR NUMBER STRING IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER STRING VAR VAR NUMBER STRING VAR VAR NUMBER STRING VAR VAR NUMBER STRING IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER STRING VAR VAR NUMBER STRING VAR VAR NUMBER STRING VAR VAR NUMBER STRING IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
A TV show called "Guess a number!" is gathering popularity. The whole Berland, the old and the young, are watching the show. The rules are simple. The host thinks of an integer y and the participants guess it by asking questions to the host. There are four types of acceptable questions: Is it true that y is strictly larger than number x? Is it true that y is strictly smaller than number x? Is it true that y is larger than or equal to number x? Is it true that y is smaller than or equal to number x? On each question the host answers truthfully, "yes" or "no". Given the sequence of questions and answers, find any integer value of y that meets the criteria of all answers. If there isn't such value, print "Impossible". -----Input----- The first line of the input contains a single integer n (1 ≤ n ≤ 10000) — the number of questions (and answers). Next n lines each contain one question and one answer to it. The format of each line is like that: "sign x answer", where the sign is: ">" (for the first type queries), "<" (for the second type queries), ">=" (for the third type queries), "<=" (for the fourth type queries). All values of x are integer and meet the inequation - 10^9 ≤ x ≤ 10^9. The answer is an English letter "Y" (for "yes") or "N" (for "no"). Consequtive elements in lines are separated by a single space. -----Output----- Print any of such integers y, that the answers to all the queries are correct. The printed number y must meet the inequation - 2·10^9 ≤ y ≤ 2·10^9. If there are many answers, print any of them. If such value doesn't exist, print word "Impossible" (without the quotes). -----Examples----- Input 4 >= 1 Y < 3 N <= -3 N > 55 N Output 17 Input 2 > 100 Y < -100 Y Output Impossible
import sys n = int(sys.stdin.readline()) instructions = [] max_const = 2000000000 min_const = -2000000000 for i in range(n): instructions.append(list(map(str, sys.stdin.readline().split()))) criteria = [] for i in instructions: if i[0] == ">" and i[2] == "Y" or i[0] == "<=" and i[2] == "N": criteria.append((int(i[1]) + 1, max_const)) elif i[0] == ">=" and i[2] == "Y" or i[0] == "<" and i[2] == "N": criteria.append((int(i[1]), max_const)) elif i[0] == "<" and i[2] == "Y" or i[0] == ">=" and i[2] == "N": criteria.append((min_const, int(i[1]) - 1)) else: criteria.append((min_const, int(i[1]))) mini = min_const maxi = max_const for i in criteria: mini = max(mini, i[0]) maxi = min(maxi, i[1]) if mini > maxi: print("Impossible") else: print(mini)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR IF VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR IF VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER STRING EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
A TV show called "Guess a number!" is gathering popularity. The whole Berland, the old and the young, are watching the show. The rules are simple. The host thinks of an integer y and the participants guess it by asking questions to the host. There are four types of acceptable questions: Is it true that y is strictly larger than number x? Is it true that y is strictly smaller than number x? Is it true that y is larger than or equal to number x? Is it true that y is smaller than or equal to number x? On each question the host answers truthfully, "yes" or "no". Given the sequence of questions and answers, find any integer value of y that meets the criteria of all answers. If there isn't such value, print "Impossible". -----Input----- The first line of the input contains a single integer n (1 ≤ n ≤ 10000) — the number of questions (and answers). Next n lines each contain one question and one answer to it. The format of each line is like that: "sign x answer", where the sign is: ">" (for the first type queries), "<" (for the second type queries), ">=" (for the third type queries), "<=" (for the fourth type queries). All values of x are integer and meet the inequation - 10^9 ≤ x ≤ 10^9. The answer is an English letter "Y" (for "yes") or "N" (for "no"). Consequtive elements in lines are separated by a single space. -----Output----- Print any of such integers y, that the answers to all the queries are correct. The printed number y must meet the inequation - 2·10^9 ≤ y ≤ 2·10^9. If there are many answers, print any of them. If such value doesn't exist, print word "Impossible" (without the quotes). -----Examples----- Input 4 >= 1 Y < 3 N <= -3 N > 55 N Output 17 Input 2 > 100 Y < -100 Y Output Impossible
n = int(input()) maxr = 2 * 1000 * 1000 * 1000 minr = -maxr for i in range(n): con, x, ans = input().split() x = int(x) if ans == "N": if con == ">=": con = "<" elif con == "<=": con = ">" elif con == "<": con = ">=" else: con = "<=" if con == ">": minr = max(minr, x + 1) elif con == ">=": minr = max(minr, x) elif con == "<=": maxr = min(maxr, x) else: maxr = min(maxr, x - 1) if minr <= maxr: print(minr) else: print("Impossible")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR STRING IF VAR STRING ASSIGN VAR STRING IF VAR STRING ASSIGN VAR STRING IF VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING IF VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
A TV show called "Guess a number!" is gathering popularity. The whole Berland, the old and the young, are watching the show. The rules are simple. The host thinks of an integer y and the participants guess it by asking questions to the host. There are four types of acceptable questions: Is it true that y is strictly larger than number x? Is it true that y is strictly smaller than number x? Is it true that y is larger than or equal to number x? Is it true that y is smaller than or equal to number x? On each question the host answers truthfully, "yes" or "no". Given the sequence of questions and answers, find any integer value of y that meets the criteria of all answers. If there isn't such value, print "Impossible". -----Input----- The first line of the input contains a single integer n (1 ≤ n ≤ 10000) — the number of questions (and answers). Next n lines each contain one question and one answer to it. The format of each line is like that: "sign x answer", where the sign is: ">" (for the first type queries), "<" (for the second type queries), ">=" (for the third type queries), "<=" (for the fourth type queries). All values of x are integer and meet the inequation - 10^9 ≤ x ≤ 10^9. The answer is an English letter "Y" (for "yes") or "N" (for "no"). Consequtive elements in lines are separated by a single space. -----Output----- Print any of such integers y, that the answers to all the queries are correct. The printed number y must meet the inequation - 2·10^9 ≤ y ≤ 2·10^9. If there are many answers, print any of them. If such value doesn't exist, print word "Impossible" (without the quotes). -----Examples----- Input 4 >= 1 Y < 3 N <= -3 N > 55 N Output 17 Input 2 > 100 Y < -100 Y Output Impossible
n = int(input()) l = -(10**9) - 1 r = 10**9 + 1 for _ in range(n): x, y, z = input().split() x = str(x) y = int(y) z = str(z) if z == "Y": if x == ">=": if y > l: l = y elif x == ">": if y >= l: l = y + 1 elif x == "<=": if y < r: r = y elif y <= r: r = y - 1 elif x == "<": if y > l: l = y elif x == "<=": if y >= l: l = y + 1 elif x == ">": if y < r: r = y elif y <= r: r = y - 1 if l > r: print("Impossible") else: print((l + r) // 2)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR STRING IF VAR STRING IF VAR VAR ASSIGN VAR VAR IF VAR STRING IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR STRING IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR STRING IF VAR VAR ASSIGN VAR VAR IF VAR STRING IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR STRING IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER
A TV show called "Guess a number!" is gathering popularity. The whole Berland, the old and the young, are watching the show. The rules are simple. The host thinks of an integer y and the participants guess it by asking questions to the host. There are four types of acceptable questions: Is it true that y is strictly larger than number x? Is it true that y is strictly smaller than number x? Is it true that y is larger than or equal to number x? Is it true that y is smaller than or equal to number x? On each question the host answers truthfully, "yes" or "no". Given the sequence of questions and answers, find any integer value of y that meets the criteria of all answers. If there isn't such value, print "Impossible". -----Input----- The first line of the input contains a single integer n (1 ≤ n ≤ 10000) — the number of questions (and answers). Next n lines each contain one question and one answer to it. The format of each line is like that: "sign x answer", where the sign is: ">" (for the first type queries), "<" (for the second type queries), ">=" (for the third type queries), "<=" (for the fourth type queries). All values of x are integer and meet the inequation - 10^9 ≤ x ≤ 10^9. The answer is an English letter "Y" (for "yes") or "N" (for "no"). Consequtive elements in lines are separated by a single space. -----Output----- Print any of such integers y, that the answers to all the queries are correct. The printed number y must meet the inequation - 2·10^9 ≤ y ≤ 2·10^9. If there are many answers, print any of them. If such value doesn't exist, print word "Impossible" (without the quotes). -----Examples----- Input 4 >= 1 Y < 3 N <= -3 N > 55 N Output 17 Input 2 > 100 Y < -100 Y Output Impossible
def reverse(op): a = [">=", ">", "<=", "<"] b = ["<", "<=", ">", ">="] indx = a.index(op) return b[indx] x = int(input()) minx = -2 * 10**9 maxx = 2 * 10**9 for i in range(x): guess = input().split(" ") if guess[2] == "N": op = reverse(guess[0]) else: op = guess[0] comp = int(guess[1]) if op == ">=": minx = max(minx, comp) elif op == ">": minx = max(minx, comp + 1) elif op == "<=": maxx = min(maxx, comp) else: maxx = min(maxx, comp - 1) if minx > maxx: print("Impossible") else: print(minx)
FUNC_DEF ASSIGN VAR LIST STRING STRING STRING STRING ASSIGN VAR LIST STRING STRING STRING STRING ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
from sys import gettrace, stdin if gettrace(): inputi = input else: def input(): return next(stdin)[:-1] def inputi(): return stdin.buffer.readline() def main(): n, m = map(int, inputi().split()) x, k, y = map(int, inputi().split()) aa = [0] + [int(a) for a in inputi().split()] + [0] bb = [0] + [int(a) for a in inputi().split()] + [0] cost = 0 ai = 0 for b in bb: nb = 0 while ai < n + 2 and aa[ai] != b: nb += 1 ai += 1 if ai == n + 2: print(-1) return mx = max(aa[ai - nb : ai]) if nb > 0 else 0 if mx > aa[ai] and mx > aa[ai - nb - 1]: if nb < k: print(-1) return if y * k > x: cost += x * (nb // k) + y * (nb % k) else: cost += x + y * (nb - k) elif y * k > x: cost += x * (nb // k) + y * (nb % k) else: cost += y * nb ai += 1 print(cost) main()
IF FUNC_CALL VAR ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR LIST NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN IF BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
n, m = map(int, input().split()) x, k, y = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) numbers = [] ia = 0 bad = False for item in b: while ia < n and item != a[ia]: ia += 1 if ia < n: numbers.append(ia + 1) else: bad = True break if bad: print(-1) else: a.append(-1) a.insert(0, -1) numbers.append(n + 1) numbers.insert(0, 0) ans = 0 hard_mode = False if y * k < x: hard_mode = True for i in range(m - 1 + 2): if numbers[i + 1] - numbers[i] - 1 > 0: if not hard_mode: if 0 < numbers[i + 1] - numbers[i] - 1 < k: if max(a[numbers[i + 1]], a[numbers[i]]) < max( a[numbers[i] + 1 : numbers[i + 1]] ): print(-1) bad = True break else: ans += y * (numbers[i + 1] - numbers[i] - 1) else: ans += x * ((numbers[i + 1] - numbers[i] - 1) // k) + y * ( (numbers[i + 1] - numbers[i] - 1) % k ) elif max(a[numbers[i + 1]], a[numbers[i]]) > max( a[numbers[i] + 1 : numbers[i + 1]] ): ans += y * (numbers[i + 1] - numbers[i] - 1) elif 0 < numbers[i + 1] - numbers[i] - 1 < k: print(-1) bad = True break else: ans += y * (numbers[i + 1] - numbers[i] - 1 - k) + x if not bad: print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER IF VAR IF NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR IF VAR EXPR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
def solve(): n, m = map(int, input().split()) x, k, y = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) sA = set(A) sB = set(B) if not sA >= sB: print(-1) return P = [(s in sB) for s in A] if [a for a, s in zip(A, P) if s] != B: print(-1) return ans = 0 p = 0 prev = 0 while p < n: pstart = p pmax = 0 while p < n and not P[p]: pmax = max(pmax, A[p]) p += 1 num = p - pstart if p < n: prev = max(prev, A[p]) if num < k: if pmax > prev: print(-1) return else: ans += y * num else: ans += y * (num % k) num -= num % k option1 = x * (num // k) option2 = y * (num - k) if pmax > prev: option2 += x else: option2 += y * k ans += min(option1, option2) while p < n and P[p]: prev = A[p] p += 1 print(ans) solve()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR VAR VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
def find(A, B, X, Y, K): if len(A) == 0: return 0 ans = 0 A = [int(x > B) for x in A] if X <= Y * K: cur = 0 switch = False done = 0 for i in range(len(A)): if switch: cur += 1 if cur == K: done += 1 switch = False cur = 0 elif A[i] == 1: switch = True cur = 1 remain = len(A) if switch: done += 1 remain = len(A) - done * K else: remain = len(A) - done * K if remain < 0: return float("inf") if X < Y * K: ans = done * X + remain // K * X + remain % K * Y else: ans = done * X + remain * Y return ans if sum(A) == 0: return Y * len(A) elif len(A) < K: return float("inf") else: return X + (len(A) - K) * Y def find_ans(A, B, X, Y, K): if len(A) < len(B): return float("inf") i = 0 j = 0 indices = [] while i < len(A) and j < len(B): if B[j] == A[i]: indices += [i] i += 1 j += 1 else: i += 1 if len(indices) < len(B): return float("inf") ans = 0 for i in range(len(indices) + 1): if ans == float("inf"): return ans if i == 0: ans += find(A[: indices[0]], B[0], X, Y, K) elif i == len(indices): ans += find(A[indices[-1] + 1 :], B[-1], X, Y, K) else: ans += find( A[indices[i - 1] + 1 : indices[i]], max(B[i - 1], B[i]), X, Y, K ) return ans n, m = list(map(int, input().strip().split())) X, K, Y = list(map(int, input().strip().split())) A = list(map(int, input().strip().split())) B = list(map(int, input().strip().split())) ans = find_ans(A, B, X, Y, K) if ans == float("inf"): print(-1) else: print(ans)
FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR STRING IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR IF FUNC_CALL VAR VAR NUMBER RETURN BIN_OP VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR STRING RETURN BIN_OP VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR LIST VAR VAR NUMBER VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR FUNC_CALL VAR STRING RETURN VAR IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
inp = lambda: map(int, input().split(" ")) sum = 0 n, m = inp() x, k, y = inp() arr1 = list(inp()) arr2 = list(inp()) pos = [] arr1.insert(0, 0) arr1.append(0) arr2.insert(0, 0) arr2.append(0) j = 0 for i in range(n + 2): if arr1[i] == arr2[j]: pos.append(i) j += 1 if j != m + 2: print(-1) else: for i in range(1, m + 2): pos1 = pos[i - 1] pos2 = pos[i] l = pos2 - (pos1 + 1) max_val = max(arr1[pos1], arr1[pos2]) is_max = True for j in range(pos1 + 1, pos2): if arr1[j] > max_val: is_max = False break if is_max: sum += min(l * y, l // k * x + l % k * y) else: if k > l: print(-1) exit() sum += min((l - k) * y + x, l // k * x + l % k * y) print(sum)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
def one_segment(left, right, lower, upper): if left == right + 1: return 0 singles = 0 groups = 0 to_delete = right - left + 1 maxi = max(aa[left : right + 1]) if to_delete < k: if maxi > max(lower, upper): return -1 else: return to_delete * y if to_delete > k: singles += to_delete % k to_delete = to_delete - singles if k * y > x: groups += to_delete / k elif maxi < max(lower, upper): singles += to_delete else: singles += to_delete - k groups += 1 return singles * y + groups * x n, m = list(map(int, input().strip().split())) x, k, y = list(map(int, input().strip().split())) aa = list(map(int, input().strip().split())) bb = list(map(int, input().strip().split())) not_poss = 0 pl1 = -1 l1 = 0 ans = 0 for l2 in bb: if not_poss == 1: break while l1 < len(aa) and aa[l1] != l2: l1 += 1 if l1 == len(aa): not_poss = 1 break else: temp = 0 if pl1 == -1 and l1 != 0: temp = one_segment(0, l1 - 1, -1, aa[l1]) elif pl1 != -1: temp = one_segment(pl1 + 1, l1 - 1, aa[pl1], aa[l1]) if temp == -1: not_poss = 1 break else: ans += temp pl1 = l1 if not_poss == 0 and l1 < len(aa): temp = one_segment(l1 + 1, len(aa) - 1, aa[l1], -1) if temp == -1: not_poss = 1 else: ans += temp if not_poss == 1: print(-1) else: print(int(ans))
FUNC_DEF IF VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
n, m = map(int, input().split()) x, k, y = map(int, input().split()) a = [0] + list(map(int, input().split())) + [0] b = list(map(int, input().split())) + [0] INF = 10000000000000000000000000 def copyer(v, l, r): ans = 0 if not len(v): return 0 got = 0 for i in v: if i > a[l] and i > a[r]: got += 1 break true_len = len(v) - (k + got - 1) // k * k if true_len < 0: return -INF ans += (len(v) - true_len) // k * x if n == 200000 and m == 2: return true_len * y + ans return ( min(true_len // k * x + (true_len - true_len // k * k) * y, true_len * y) + ans ) def solve(): r = 0 ans = 0 for i in b: norm = 0 v = [] for j in range(r + 1, len(a)): if i == a[j]: ans += copyer(v, r, j) norm = 1 r = j break else: v.append(a[j]) if not norm or ans < 0: print(-1) return print(ans) solve()
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR LIST NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR LIST NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR IF VAR NUMBER RETURN VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
def solve(n, m, x, k, y, a_arr, b_arr): cnt = 0 cnt1 = 0 prev_same = 0 mana = 0 start = b_arr[cnt1] end = b_arr[cnt1] while cnt < n and cnt1 < m: if a_arr[cnt] == b_arr[cnt1]: end = b_arr[cnt1] n_remove = cnt - prev_same max_elem = 0 for i in range(prev_same, cnt): max_elem = max(a_arr[i], max_elem) if n_remove < k: if max_elem > start and max_elem > end: print(-1) return mana += y * n_remove else: if max_elem > start and max_elem > end: n_remove -= k mana += x if k * y > x: mana += x * (n_remove // k) + y * (n_remove % k) else: mana += y * n_remove start = end cnt1 += 1 prev_same = cnt + 1 cnt += 1 if cnt1 < m: print(-1) return else: n_remove = n - prev_same max_elem = 0 for i in range(prev_same, n): max_elem = max(max_elem, a_arr[i]) if n_remove < k: if max_elem > start: print(-1) return mana += y * n_remove else: if max_elem > start: n_remove -= k mana += x if k * y > x: mana += x * (n_remove // k) + y * (n_remove % k) else: mana += y * n_remove print(mana) return n, m = map(int, input().split()) x, k, y = map(int, input().split()) a_arr = list(map(int, input().split())) b_arr = list(map(int, input().split())) solve(n, m, x, k, y, a_arr, b_arr)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
n, m = map(int, input().split()) x, k, y = map(int, input().split()) a = [-1] + list(map(int, input().split())) + [-2] b = [-1] + list(map(int, input().split())) + [-2] if m > n: print(-1) elif m == n: print(-int(a != b)) else: g = {} for i in range(n + 2): g[a[i]] = i binds = [] for i in b: if i not in g or binds and binds[-1] > g[i]: print(-1) return () binds.append(g[i]) dels = [] for i in range(len(binds) - 1): dels.append([]) for j in range(binds[i], binds[i + 1] + 1): dels[-1].append(a[j]) ans = 0 for d in dels: todel = len(d) - 2 if todel: mx = max(d) allys = max(d[0], d[-1]) == mx if y * k < x: if allys: ans += todel * y else: if todel < k: print(-1) return () ans += y * (todel - k) + x elif todel < k: if allys: ans += todel * y else: print(-1) return () else: ans += y * (todel % k) + x * (todel // k) print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR LIST NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR LIST NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR IF BIN_OP VAR VAR VAR IF VAR VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR IF VAR VAR IF VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
import sys input = sys.stdin.readline n, m = map(int, input().split()) x, k, y = map(int, input().split()) ar = list(map(int, input().split())) br = list(map(int, input().split())) dic = {} for i in range(n): dic[ar[i]] = i flag = True for i in br: if not i in dic: flag = False break if flag: for i in range(1, m): if dic[br[i]] < dic[br[i - 1]]: flag = False break ans = 0 m += 1 br.append(-1) dic[-1] = n if flag: pre = 0 left = -1 for i in range(m): le = 0 ma = -1 for j in range(pre, dic[br[i]]): le += 1 ma = max(ma, ar[j]) if x < y * k: if le % k == 0: ans += x * (le // k) elif le < k: if ma > max(left, br[i]): flag = False break else: ans += le * y else: ans += x * (le // k) ans += le % k * y elif x >= y * k: if ma > max(left, br[i]): if le < k: flag = False break else: ans += (le - k) * y ans += x else: ans += le * y pre = dic[br[i]] + 1 left = br[i] if flag: print(ans) else: print(-1) else: print(-1) else: print(-1)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR IF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
input = __import__("sys").stdin.readline def cal(i, j): seg = j - i - 1 tmp = 0 if seg == 0: return 0 mm = max(a[i + 1 : j]) mi = 0 if 0 <= i < n: mi = a[i] if 0 <= j < n: mi = max(a[j], mi) if y * k <= x: if mm < mi: tmp = y * seg elif seg < k: print(-1) exit() else: tmp = (seg - k) * y + x elif mm < mi: tmp = seg % k * y + seg // k * x elif seg < k: print(-1) exit() else: tmp = seg % k * y + seg // k * x return tmp n, m = map(int, input().split()) x, k, y = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) ab = {v: i for i, v in enumerate(a)} ans = 0 prev = -1 for vv in b: if vv not in ab: print(-1) exit() i = ab[vv] if i < prev: print(-1) exit() ans += cal(prev, i) prev = i ans += cal(prev, n) print(ans)
ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF NUMBER VAR VAR ASSIGN VAR VAR VAR IF NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
from sys import stdin fi = stdin def read_line_ints(): return [int(v) for v in fi.readline().split()] n, m = read_line_ints() x, k, y = read_line_ints() a = read_line_ints() b = read_line_ints() def solve(): a_value_to_idx = {value: idx for idx, value in enumerate(a)} for bi in b: if bi not in a_value_to_idx: return -1 for bi, bj in zip(b[:-1], b[1:]): if a_value_to_idx[bi] > a_value_to_idx[bj]: return -1 segments = [] if not b: segments = [(0, n - 1)] else: if a_value_to_idx[b[0]] != 0: segments.append((0, a_value_to_idx[b[0]] - 1)) for bi, bj in zip(b[:-1], b[1:]): ai_idx = a_value_to_idx[bi] aj_idx = a_value_to_idx[bj] if aj_idx > ai_idx + 1: segments.append((ai_idx + 1, aj_idx - 1)) if a_value_to_idx[b[-1]] != n - 1: segments.append((a_value_to_idx[b[-1]] + 1, n - 1)) cost = 0 a.append(-1) for l, r in segments: max_v = max(a[l : r + 1]) count = r - l + 1 if max_v < max(a[l - 1], a[r + 1]): if x > k * y: cost += y * count else: n_blast = count // k cost += x * n_blast cost += y * (count % k) else: if count < k: return -1 if x > k * y: cost += y * (count - k) + x else: n_blast = count // k cost += x * n_blast cost += y * (count % k) return cost print(solve())
ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR RETURN NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR LIST IF VAR ASSIGN VAR LIST NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER IF VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
from sys import stdin, stdout def get_ans(mx, x, k, y, a, l, r): cnt = r - l - 1 amx = 0 if l >= 0: amx = max(amx, a[l]) if r < len(a): amx = max(amx, a[r]) ans = cnt // k * x + cnt % k * y if amx > mx: ans = min(cnt * y, ans) else: if cnt < k: return [False, None] ans = min(x + (cnt - k) * y, ans) return [True, ans] def berserk_and_fireball(n, m, x, k, y, a, b): ans = 0 j = 0 mx = 0 l = -1 for r in range(n): if j < m and a[r] == b[j]: lr = get_ans(mx, x, k, y, a, l, r) if not lr[0]: return -1 ans += lr[1] j += 1 l = r mx = 0 else: mx = max(mx, a[r]) if j < m: return -1 lr = get_ans(mx, x, k, y, a, l, n) if not lr[0]: return -1 ans += lr[1] return ans n, m = map(int, stdin.readline().split()) x, k, y = map(int, stdin.readline().split()) a = list(map(int, stdin.readline().split())) b = list(map(int, stdin.readline().split())) stdout.write(str(berserk_and_fireball(n, m, x, k, y, a, b)))
FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR RETURN LIST NUMBER NONE ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR RETURN LIST NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
import sys input = sys.stdin.buffer.readline def compress(string): string.append(-1) n = len(string) begin, end, cnt = 0, 1, 1 ans = [] while end < n: if string[begin] == string[end]: end, cnt = end + 1, cnt + 1 else: ans.append((string[begin], cnt)) begin, end, cnt = end, end + 1, 1 return ans n, m = map(int, input().split()) x, k, y = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) INF = 10**18 delete = [1] * n ib = 0 for ia in range(n): if ib == m: continue if a[ia] == b[ib]: ib += 1 delete[ia] = 0 if ib != m: print(-1) exit() comp = compress(delete) ans = 0 ind = 0 for val, cnt in comp: if val == 0: pass else: l_val, r_val = -1, -1 if ind - 1 >= 0: l_val = a[ind - 1] if ind + cnt < n: r_val = a[ind + cnt] max_ = max(l_val, r_val) min_cost = INF if max(a[ind : ind + cnt]) < max_: min_cost = min(min_cost, y * cnt) if cnt >= k: div, rem = divmod(cnt, k) min_cost = min(min_cost, div * x + rem * y) min_cost = min(min_cost, x + (cnt - k) * y) ans += min_cost ind += cnt if ans >= INF: print(-1) else: print(ans)
IMPORT ASSIGN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
import sys input = sys.stdin.readline N, M = map(int, input().split()) X, K, Y = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) def score(T, bmax): L = len(T) if L == 0: return 0 tmax = max(T) if L < K: if tmax > bmax: return -1 return L * Y elif K * Y >= X: return L // K * X + L % K * Y elif tmax > bmax: return X + (L - K) * Y else: return L * Y bind = 0 T = [] ans = 0 for a in A: if bind < M and B[bind] == a: bmax = B[bind] if bind > 0: bmax = max(B[bind - 1], bmax) res = score(T, bmax) if res == -1: ans = -1 break ans += res bind += 1 T = [] continue T.append(a) if bind != M: ans = -1 else: res = score(T, B[M - 1]) if res == -1: ans = -1 else: ans += res print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR RETURN NUMBER RETURN BIN_OP VAR VAR IF BIN_OP VAR VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
import sys input = sys.stdin.readline I = lambda: list(map(int, input().split())) n, m = I() x, k, y = I() a = I() b = I() si = [-1] cr = 0 mi = 0 mm = [] for i in range(n): if a[i] == b[cr]: mm.append(mi) if a[i] < mi and (len(mm) < 2 or len(mm) > 1 and mm[-2] < a[i]): if not i - si[-1] - 1 >= k: break cr += 1 si.append(i) mi = 0 else: mi = max(mi, a[i]) if cr == m: break if n < m or cr != m: print(-1) exit() if b[m - 1] != a[n - 1]: mm.append(max(a[i + 1 :])) if mm[-1] > b[m - 1] and n - i - 1 < k: cr = 0 else: mm.append(0) if cr: si = si + [n] a.append(0) an = f = cr = 0 if k * y < x: f = 1 for i in range(1, len(si)): pp = si[i] - si[i - 1] - 1 if max(a[si[i]], a[si[i - 1]]) < mm[cr] and pp < k: print(-1) exit() if f: if max(a[si[i]], a[si[i - 1]]) > mm[cr]: an += pp * y else: an += max(pp - k, 0) * y an += x else: an += pp // k * x + pp % k * y cr += 1 print(an) else: print(-1)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR ASSIGN VAR BIN_OP VAR LIST VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
import sys sys.setrecursionlimit(10**5) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def SI(): return sys.stdin.readline()[:-1] def des(l, r, mx): if r - l < k and (l == 0 or aa[l - 1] < mx) and (r == n or aa[r] < mx): return -1 if x > k * y: if l != 0 and aa[l - 1] > mx or (r != n and aa[r]) > mx: return y * (r - l) return x + (r - l - k) * y cnt, rem = divmod(r - l, k) return cnt * x + rem * y def solve(): ans = 0 pi = i = 0 for b in bb: mx = 0 if i >= n: return -1 while i < n and aa[i] != b: mx = max(mx, aa[i]) i += 1 if i > pi: mana = des(pi, i, mx) if mana == -1: return -1 ans += mana i += 1 pi = i if n > pi: mx = max(aa[pi:]) mana = des(pi, n, mx) if mana == -1: return -1 ans += mana return ans n, m = MI() x, k, y = MI() aa = LI() bb = LI() print(solve())
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR NUMBER FUNC_DEF IF BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR RETURN NUMBER IF VAR BIN_OP VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR RETURN BIN_OP VAR BIN_OP VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER IF VAR VAR RETURN NUMBER WHILE VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
N, M = map(int, input().split()) X, K, Y = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) AB = [(0) for _ in range(len(B) + 2)] AS = AB.copy() AS[0] = 0 AB[0] = -1 i = 0 j = 0 l = 0 li = 0 result = 0 while j < len(B): while i < len(A): if li < A[i]: l = i li = A[i] if A[i] == B[j]: break i += 1 if i >= len(A): result = -1 break AB[j + 1] = i AS[j + 1] = l l = i li = A[i] j += 1 for k in range(i, len(A)): if li < A[k]: l = k li = A[k] AB[-1] = len(A) AS[-1] = l if X / K < Y: use_f = True else: use_f = False if result != -1: for i in range(1, len(B) + 2): if AB[i] == AS[i] or AB[i - 1] == AS[i]: if use_f: result += (AB[i] - AB[i - 1] - 1) // K * X + ( AB[i] - AB[i - 1] - 1 ) % K * Y else: result += (AB[i] - AB[i - 1] - 1) * Y elif 0 < AB[i] - AB[i - 1] - 1 < K: result = -1 break elif use_f: result += (AB[i] - AB[i - 1] - 1) // K * X + (AB[i] - AB[i - 1] - 1) % K * Y else: result += X + (AB[i] - AB[i - 1] - 1 - K) * Y print(result)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR IF NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
import sys def rs(): return sys.stdin.readline().rstrip() def ri(): return int(sys.stdin.readline()) def ria(): return list(map(int, sys.stdin.readline().split())) def ws(s): sys.stdout.write(s) sys.stdout.write("\n") def wi(n): sys.stdout.write(str(n)) sys.stdout.write("\n") def wia(a, sep=" "): sys.stdout.write(sep.join([str(x) for x in a])) sys.stdout.write("\n") def solve(n, m, x, k, y, a, b): a = [0] + a + [0] b = [0] + b + [0] n += 2 m += 2 pairs = [] idx = -1 for i in range(m): try: idx = a.index(b[i], idx + 1) except ValueError: return -1 pairs.append(idx) mx = float("inf") def kill(middle, l_val, r_val): mn = len(middle) if mn == 0: return 0 best = mx middle.sort() for bs_count in range(mn + 1): if (mn - bs_count) % k > 0: continue if bs_count == mn: if max(l_val, r_val) > middle[-1]: best = min(best, bs_count * y) continue best = min(best, bs_count * y + x * (mn - bs_count) // k) return -1 if best == mx else best cnt = 0 for i in range(len(pairs) - 1): res = kill(a[pairs[i] + 1 : pairs[i + 1]], a[pairs[i]], a[pairs[i + 1]]) if res == -1: return -1 cnt += res return cnt def main(): n, m = ria() x, k, y = ria() a = ria() b = ria() wi(solve(n, m, x, k, y, a, b)) main()
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING FUNC_DEF STRING EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR LIST NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR LIST NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR RETURN VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN NUMBER VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
def sooolve(l, lst, r, x, k, y): n = len(lst) mx = max(l, r) ans = 0 if n == 0: return 0 if mx < max(lst): if n < k: return -1 n -= k ans += x if x > k * y: ans += n * y else: ans += n // k * x + n % k * y return ans def solve(): n, m = map(int, input().split()) x, k, y = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) lastpos = -1 pos = 0 ans = 0 for i in b: while pos < n and a[pos] != i: pos += 1 if pos == n: print(-1) return 0 if pos != lastpos + 1: if lastpos == -1: k1 = sooolve(0, a[0:pos], a[pos], x, k, y) if k1 == -1: print(k1) return 0 ans += k1 else: k1 = sooolve(a[lastpos], a[lastpos + 1 : pos], a[pos], x, k, y) if k1 == -1: print(k1) return 0 ans += k1 lastpos = pos k1 = sooolve(a[lastpos], a[lastpos + 1 :], 0, x, k, y) if k1 == -1: print(k1) return 0 ans += k1 print(ans) for i in range(1): solve()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER IF VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER VAR VAR VAR VAR IF VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
def subsequence(arr1, arr2, n, m): i = 0 j = 0 common = [] while i < n and j < m: if arr1[i] == arr2[j]: common.append(i) j += 1 i += 1 if j == m: return common return [] def find(arr, start, end, x, y, k): stack = [] second_use_limit = 0 for i in range(start, end + 1): if not stack: stack.append(arr[i]) else: while stack and arr[i] > stack[-1]: second_use_limit += 1 stack.pop() if not stack or arr[i] > stack[-1]: stack.append(arr[i]) elif stack and arr[i] < stack[-1]: second_use_limit += 1 while stack and start - 1 >= 0 and arr[start - 1] > stack[0]: stack.pop(0) second_use_limit += 1 while stack and end + 1 < len(arr) and arr[end + 1] > stack[-1]: stack.pop() second_use_limit += 1 min_cost = float("inf") total = end - start + 1 i = 0 while i * k <= total: rem = total - i * k if rem <= second_use_limit: min_cost = min(min_cost, i * x + rem * y) i += 1 if min_cost == float("inf"): return -1 return min_cost def main(): n, m = map(int, input().split()) x, k, y = map(int, input().split()) arr1 = list(map(int, input().split())) arr2 = list(map(int, input().split())) common = subsequence(arr1, arr2, n, m) if not common: print(-1) return cost = 0 for i in range(len(common)): if i == 0: if common[i] > 0: start = 0 end = common[i] - 1 if end >= start: curr_cost = find(arr1, start, end, x, y, k) if curr_cost == -1: cost = -1 break cost += curr_cost start = common[i] + 1 if i + 1 < len(common): end = common[i + 1] - 1 else: end = n - 1 if end >= start: curr_cost = find(arr1, start, end, x, y, k) if curr_cost == -1: cost = -1 break cost += curr_cost print(cost) main()
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN VAR RETURN LIST FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER WHILE VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
for _ in range(1): n, m = map(int, input().split()) x, k, y = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) a.append(0) b.append(0) itr = 0 prev = 0 leng = 0 maxp = 0 mana = 0 for i in range(n + 1): if a[i] == b[itr]: if maxp < max(prev, a[i]): mana += min(leng // k * x + leng % k * y, leng * y) leng = 0 maxp = 0 itr += 1 prev = a[i] continue if leng < k: break mana += min(x + (leng - k) * y, leng // k * x + leng % k * y) leng = 0 maxp = 0 prev = a[i] itr += 1 else: leng += 1 maxp = max(maxp, a[i]) if itr != m + 1: print(-1) else: print(mana)
FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
def check(l, r, num): if l == -1: if a[r] > num: return True return False if r == n: if a[l] > num: return True return False if a[l] > num or a[r] > num: return True return False def know(l, r): if r - l == 1: return [True, 0] maximum = max(a[l + 1 : r]) if r - l - 1 < k: if not check(l, r, maximum): return [False, 0] return [True, y * (r - l - 1)] answer = 0 length = r - l - 1 answer += length % k * y length -= length % k if y * k < x: if not check(l, r, maximum): answer += x length -= k answer += length * y else: answer += length // k * x return [True, answer] n, m = map(int, input().split()) x, k, y = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) i = 0 j = 0 ans = 0 pos = [] while i < m and j < n: if b[i] != a[j]: j += 1 else: i += 1 pos.append(j) j += 1 if j == n and i != m: print(-1) else: pos.insert(0, -1) pos.append(n) flag = True for i in range(1, len(pos)): arr = know(pos[i - 1], pos[i]) if arr[0] == False: flag = False else: ans += arr[1] if flag: print(ans) else: print(-1)
FUNC_DEF IF VAR NUMBER IF VAR VAR VAR RETURN NUMBER RETURN NUMBER IF VAR VAR IF VAR VAR VAR RETURN NUMBER RETURN NUMBER IF VAR VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF BIN_OP VAR VAR NUMBER RETURN LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR VAR RETURN LIST NUMBER NUMBER RETURN LIST NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR RETURN LIST NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
def main(): n, m = [int(i) for i in input().split()] x, k, y = [int(i) for i in input().split()] a = [0] + [int(i) for i in input().split()] + [0] base = 0 num = 0 flag = False total = 0 res = [] for i, elem in enumerate(input().split()): num = base while int(elem) != a[num]: num += 1 if num == n + 1: break if num == n + 1: print(-1) return res.append((base, num)) base = num res.append((base, n + 1)) for elem in res: flag = False if elem[1] - elem[0] == 1: continue if elem[1] - elem[0] - 1 < k: for j in a[elem[0] : elem[1] + 1]: if j > a[elem[0]] and j > a[elem[1]]: print(-1) return total += y * (elem[1] - elem[0] - 1) else: for j in a[elem[0] : elem[1] + 1]: if j > a[elem[0]] and j > a[elem[1]]: flag = True break if k * y >= x: total += (elem[1] - elem[0] - 1) // k * x + ( elem[1] - elem[0] - 1 ) % k * y elif flag: total += (elem[1] - elem[0] - 1 - k) * y + x else: total += (elem[1] - elem[0] - 1) * y print(total) main()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR FOR VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER FOR VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR VAR IF VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
n, m = map(int, input().split()) x, k, y = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) pos = dict() for idx, val in enumerate(a): pos[val] = idx def process(l, r): ans = 0 u = a[l] if l >= 0 else 0 v = a[r] if r < n else 0 cnt = 0 mxm = max(u, v) for i in range(l + 1, r): if mxm < a[i]: cnt += 1 dist = r - l - 1 if cnt > 0 and dist < k: print(-1) exit(0) if y * k < x: if cnt > 0: dist -= k ans += x ans += dist * y else: ans = dist // k * x + dist % k * y return ans ans = process(-1, pos[b[0]]) + process(pos[b[-1]], n) for u, v in zip(b, b[1:]): if pos[u] > pos[v]: print(-1) exit(0) ans += process(pos[u], pos[v]) print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
n, m = [int(x) for x in input().split()] fire_mana, fire_range, bsk_mana = [int(x) for x in input().split()] a = [int(x) for x in input().split()] b = [int(x) for x in input().split()] i, k = 0, 0 min_mana = 0 last_target = 0 for j in range(n): if a[j] == b[k]: enemies = j - i if enemies > 0: edgepower = max(last_target, b[k]) max_enemypower = max(a[i:j]) mana_used = 0 if max_enemypower > edgepower: enemies -= fire_range mana_used += fire_mana if enemies < 0: min_mana = -1 break if fire_mana / fire_range < bsk_mana: mana_used += fire_mana * (enemies // fire_range) enemies %= fire_range mana_used += bsk_mana * enemies min_mana += mana_used last_target = b[k] i = j + 1 k += 1 if k == m: break if k != m: min_mana = -1 if min_mana != -1: enemies = n - i if enemies > 0: edgepower = last_target max_enemypower = max(a[i:]) mana_used = 0 if max_enemypower > edgepower: enemies -= fire_range mana_used += fire_mana if enemies < 0: min_mana = -1 else: if fire_mana / fire_range < bsk_mana: mana_used += fire_mana * (enemies // fire_range) enemies %= fire_range mana_used += bsk_mana * enemies min_mana += mana_used print(min_mana)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
def process(s, k, x, y): if s[0] == -1: t = s[1:] return process_left(t[::-1], k, x, y) if s[-1] == -2: return process_left(s[:-1], k, x, y) l = s[0] r = s[-1] c = s[1:-1] if len(c) == 0: return 0 p = c[0] for cc in c: p = max(p, cc) if len(c) < k and l < p and r < p: return -1 cost = len(c) % k * y if int(len(c) / k) * x < (len(c) - len(c) % k) * y: cost += int(len(c) / k) * x elif l > p or r > p: cost += (len(c) - len(c) % k) * y else: cost += x cost += (len(c) - len(c) % k - k) * y return cost def process_left(s, k, x, y): l = s[0] c = s[1:] if len(c) == 0: return 0 p = c[0] for cc in c: p = max(p, cc) if len(c) < k and l < p: return -1 cost = len(c) % k * y if int(len(c) / k) * x < (len(c) - len(c) % k) * y: cost += int(len(c) / k) * x elif l > p: cost += (len(c) - len(c) % k) * y else: cost += x cost += (len(c) - len(c) % k - k) * y return cost n, m = map(int, input().split(" ")) x, k, y = map(int, input().split(" ")) a = list(map(int, input().split(" "))) b = list(map(int, input().split(" "))) i = 0 j = 0 found = 1 bi = 0 bj = 1 total = 0 dummy1 = [-1] dummy2 = [-2] a = dummy1 + a + dummy2 b = dummy1 + b + dummy2 while bi < len(b) - 1: while a[i] != b[bi]: i += 1 if i == len(a): found = -1 break j = i while a[j] != b[bj]: j += 1 if j == len(a): found = -1 break if found == -1: break s = a[i : j + 1] value = process(s, k, x, y) if value != -1: total += value else: found = -1 break bi += 1 bj += 1 if found == 1: print(total) else: print(-1)
FUNC_DEF IF VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR VAR VAR IF VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR IF BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR IF BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
import sys input = sys.stdin.buffer.readline def print(val): sys.stdout.write(str(val) + "\n") def prog(): n, m = map(int, input().split()) x, k, y = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) gaps = [] prev = -1 j = -1 fail = False for i in range(m): j += 1 if j >= n: fail = True break while True: if a[j] == b[i]: new_gap = j - prev - 1 if new_gap: if i == 0: gaps.append([a[:j], b[i]]) else: gaps.append([a[prev + 1 : j], max(b[i - 1], b[i])]) prev = j break j += 1 if j >= n: fail = True break if fail: print(-1) else: mana = 0 if n - prev - 1: gaps.append([a[prev + 1 :], b[m - 1]]) possible = True if x / k <= y: for gap in gaps: length = len(gap[0]) if length >= k: mana += length // k * x + length % k * y elif max(gap[0]) > gap[1]: possible = False break else: mana += length * y if possible: print(mana) else: print(-1) else: for gap in gaps: length = len(gap[0]) if length >= k: if max(gap[0]) > gap[1]: mana += x + (length - k) * y else: mana += length * y elif max(gap[0]) > gap[1]: possible = False break else: mana += length * y if possible: print(mana) else: print(-1) prog()
IMPORT ASSIGN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
import sys input = sys.stdin.readline N, M = map(int, input().split()) a, k, b = map(int, input().split()) X = [0] + list(map(int, input().split())) + [0] Y = [0] + list(map(int, input().split())) + [0] j = 0 tmp = [] S = [] for i in range(N + 2): tmp.append(X[i]) if Y[j] == X[i]: if len(tmp) > 2: S.append(tmp) j += 1 tmp = [X[i]] else: pass if j != M + 2: print(-1) sys.exit() R = 0 for l in S: mx = max(l[1:-1]) lc = len(l) - 2 if max(l[0], l[-1]) > mx: if b * k > a: R += a * (lc // k) + b * (lc % k) else: R += lc * b else: if lc < k: print(-1) sys.exit() if b * k > a: R += a * (lc // k) + b * (lc % k) else: R += (lc - k) * b + a print(R)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR LIST NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
n, m = map(int, input().split()) x, k, y = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) c = [] a2 = [-1] * n for i in range(n): a2[a[i] - 1] = i for elem in b: c.append(a2[elem - 1]) c = [-1] + c + [n] check = True ans = 0 for i in range(len(c) - 1): if c[i] > c[i + 1]: check = False break i1 = c[i] i2 = c[i + 1] if i1 == -1: l = 0 else: l = a[i1] if i2 == n: r = 0 else: r = a[i2] d = i2 - i1 - 1 maxx = 0 for j in range(i1 + 1, i2): maxx = max(maxx, a[j]) if d < k and maxx > max(l, r): check = False break else: ans += y * (d % k) d -= d % k if y * k >= x: ans += d // k * x elif maxx > max(l, r): d -= k ans += x ans += d * y else: ans += d * y if check: print(ans) else: print(-1)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR LIST VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
import sys input = sys.stdin.readline n, m = map(int, input().split()) x, k, y = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) AD = {v: i for i, v in enumerate(A)} prefbers = k * y < x def imp(): print(-1) sys.exit() def get(x): if 0 <= x < n: return A[x] return 0 def f(i, j): l, r = map(get, (i, j)) d = j - i - 1 if d == 0: return 0 ob = max(l, r) ib = max(A[i + 1 : j]) if ob > ib: c = d % k * y if prefbers: c += d // k * k * y else: c += d // k * x else: if d < k: imp() c = x + d % k * y d -= k if prefbers: c += d // k * k * y else: c += d // k * x return c cost = 0 prev = -1 for v in B: if v not in AD: imp() i = AD[v] if i < prev: imp() cost += f(prev, i) prev = i cost += f(prev, n) print(cost)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_DEF IF NUMBER VAR VAR RETURN VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
total = 0 array1 = [] array2 = [] input_array1 = input().split() n = int(input_array1[0]) m = int(input_array1[1]) input_array0 = input().split() x = int(input_array0[0]) k = int(input_array0[1]) y = int(input_array0[2]) input_array2 = input().split() input_array3 = input().split() for i in range(0, n): array1.append(int(input_array2[i])) for i in range(0, m): array2.append(int(input_array3[i])) start = 1 closure = 0 j = i = 0 s = 0 if n == m: if array1 == array2: print(0) exit() else: print(-1) exit() while j < n: r = -1 p = -1 i = j maxi = 0 l = length = 0 while i < n: if s < m: a = array2[s] == array1[i] else: a = 0 if i == 0 and a: r = array1[0] start = 2 i += 1 s += 1 elif a == 0: if i != 0 and start == 1: r = array1[i - 1] start = 2 if array1[i] > maxi: maxi = array1[i] i += 1 length += 1 l = length else: p = array1[i] s += 1 break if x / k > y: if maxi < p or maxi < r: total += y * length start = 1 elif k > length: print(-1) closure = 1 break else: total += (length - k) * y + x start = 1 elif l % k == 0: total += x * (l / k) start = 1 elif maxi < p or maxi < r: total += l % k * y + int(l / k) * x start = 1 elif k < length: total += l % k * y + int(l / k) * x start = 1 else: print(-1) closure = 1 break j = i + 1 if closure == 1: break if closure != 1: print(int(total))
ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
import sys def solve(A): if len(A) <= 2: return True, 0 if len(A) < k + 2: if max(A) != A[0] and max(A) != A[-1]: return False, 0 else: return True, y * (len(A) - 2) elif y * k < x: if max(A) == A[0] or max(A) == A[-1]: return True, (len(A) - 2) * y else: d = len(A) - 2 return True, x + (d - k) * y else: d = len(A) - 2 return True, d // k * x + (d - d // k * k) * y n, m = [int(c) for c in input().split()] x, k, y = [int(c) for c in input().split()] a = [int(c) for c in input().split()] b = [int(c) for c in input().split()] a.insert(0, -1) a.append(-1) b.append(-1) j = 0 A = [] ans = 0 aoo = False for i in range(n + 2): if a[i] != b[j]: A.append(a[i]) else: A.append(a[i]) j += 1 pp, aa = solve(A) if pp: ans += aa else: aoo = True break A = [a[i]] if j != len(b) or aoo: print("-1") else: print(ans)
IMPORT FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER NUMBER RETURN NUMBER BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST VAR VAR IF VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
def process(ori, after, x, k, y): gap = [] temp = [] result = 0 j = 0 for i in range(len(ori)): if j == len(after): temp.append(i) continue if ori[i] != after[j]: temp.append(i) else: gap.append(temp[:]) temp = [] j += 1 gap.append(temp) if j != len(after): return -1 for i in range(len(gap)): if len(gap[i]) == 0: continue if len(gap[i]) < k: re = check_bon(gap, ori, i) if re == -1: return -1 result += re elif len(gap[i]) % k == 0: if x / k <= y: result += len(gap[i]) / k * x else: re = check_bon(gap, ori, i) if re == -1: result += x result += (len(gap[i]) - k) * y else: result += re elif x / k > y: re = check_bon(gap, ori, i) if re != -1: result += re else: result += x result += (len(gap[i]) - k) * y else: result += len(gap[i]) // k * x result += len(gap[i]) % k * y return result def check_bon(gap, ori, i): if gap[i][0] - 1 < 0 and gap[i][-1] + 1 == len(ori): return -1 left = gap[i][0] - 1 right = gap[i][-1] + 1 temp = -1 for j in gap[i]: if ori[j] > temp: temp = ori[j] if valid(left, ori): left = ori[left] > temp if valid(right, ori): right = ori[right] > temp if left == True or right == True: return y * len(gap[i]) else: return -1 def valid(n, ori): if n < 0 or n == len(ori): return False return True n, m = input("").split(" ") n = int(n) m = int(m) x, k, y = input("").split(" ") x = int(x) k = int(k) y = int(y) ori = input("").split(" ") after = input("").split(" ") for i in range(len(ori)): ori[i] = int(ori[i]) for i in range(len(after)): after[i] = int(after[i]) if n < m: print("-1") exit() if n == m: for i in range(n): if ori[i] != after[i]: print("-1") exit() print("0") exit() print("{}".format(int(process(ori, after, x, k, y))))
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF BIN_OP VAR VAR NUMBER NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER RETURN BIN_OP VAR FUNC_CALL VAR VAR VAR RETURN NUMBER FUNC_DEF IF VAR NUMBER VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
__MULTITEST = False def isSubseq(a, b): ptr = 0 lenB = len(b) for x in a: if b[ptr] == x: ptr += 1 if ptr == lenB: return True return False def solve(): n, m = map(int, input().split()) x, k, y = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) if not isSubseq(a, b): print(-1) else: a.insert(0, -1) a.append(-2) b.insert(0, -1) b.append(-2) lenA = len(a) lenB = len(b) ans = 0 pa1 = 0 pa2 = 1 pb = 0 while pb + 1 < lenB: maxOfSeg = -3 while a[pa1] != b[pb]: pa1 += 1 pa2 = pa1 + 1 while a[pa2] != b[pb + 1]: maxOfSeg = max(maxOfSeg, a[pa2]) pa2 += 1 segLen = pa2 - pa1 - 1 if segLen >= k: if x > y * k: if a[pa1] < maxOfSeg and a[pa2] < maxOfSeg: ans += x + (segLen - k) * y else: ans += segLen * y else: ans += segLen // k * x + segLen % k * y elif a[pa1] < maxOfSeg and a[pa2] < maxOfSeg: print(-1) return else: ans += segLen * y pb += 1 pa1 = pa2 print(ans) def __starting_point(): t = int(input()) if __MULTITEST else 1 for tt in range(t): try: solve() except IndexError: print("-1") __starting_point()
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR IF VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
n, m = map(int, input().split()) x, k, y = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) c = [0] * m j = 0 for i in range(n): if j == m: break if a[i] == b[j]: c[j] = i + 1 j += 1 if not j == m: print(-1) exit() a.insert(0, 0) a.append(0) c.insert(0, 0) c.append(n + 1) ans = 0 for i in range(m + 1): if c[i + 1] - c[i] == 1: continue l = c[i + 1] - c[i] - 1 s = 0 t = max(a[c[i]], a[c[i + 1]]) for j in range(c[i] + 1, c[i + 1]): if a[j] > t: s += 1 if s == 0: ans += min(l * y, l // k * x + l % k * y) else: if k > l: print(-1) exit() ans += min((l - k) * y + x, l // k * x + l % k * y) print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
n, m = map(int, input().split()) x, k, y = map(int, input().split()) a = [int(x) for x in input().split()] b = [int(x) for x in input().split()] def process(c, d): if c > d: return 0 if c == 0: l = 0 else: l = a[c - 1] if d == n - 1: r = 0 else: r = a[d + 1] no = d - c + 1 mx = a[c] for i in range(c + 1, d + 1): if a[i] > mx: mx = a[i] flag = 0 if mx > l and mx > r: flag = 1 if no < k and flag == 1: return -1 ans = 0 if x / k < y: ans += no // k * x ans += no % k * y return ans elif flag == 1: ans += x ans += (no - k) * y return ans else: ans = no * y return ans if n == m: if a == b: print(0) else: print(-1) else: i = 0 j = 0 cost = 0 flag = 0 if a[0] == b[0]: i += 1 j += 1 z = 0 else: i += 1 z = -1 while i < n and j < m: if a[i] == b[j]: f = process(z + 1, i - 1) if f == -1: flag = 1 cost += f z = i i += 1 j += 1 else: i += 1 if a[n - 1] != b[m - 1]: f = process(z + 1, n - 1) if f == -1: flag = 1 cost += f if flag == 0: print(cost) else: print(-1)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR IF VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
l = input().split() n = int(l[0]) m = int(l[1]) l = input().split() x = int(l[0]) k = int(l[1]) y = int(l[2]) a = input().split() ai = [int(i) for i in a] b = input().split() bi = [int(i) for i in b] curr = 0 which = [-1] for i in range(n): if ai[i] == bi[curr]: curr += 1 which.append(i) if curr == m: break which.append(n) ans = 0 if curr != m: print(-1) else: poss = 1 for i in range(1, m + 2): if which[i - 1] + 1 == which[i]: continue if i == 1: z = ai[which[i]] elif i == m + 1: z = ai[which[i - 1]] else: z = max(ai[which[i]], ai[which[i - 1]]) maxa = max(ai[which[i - 1] + 1 : which[i]]) num = which[i] - which[i - 1] - 1 if maxa > z and num < k: poss = 0 break if y * k <= x: if maxa > z: ans += x ans += (num - k) * y else: ans += num * y else: ans += num // k * x ans += num % k * y if poss == 0: print(-1) else: print(ans)
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR IF VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
def update(a, b): global res if a > b: return True l = b - a + 1 apply_x = False mx = max(lst1[a : b + 1]) if a - 1 >= 0 and lst1[a - 1] > mx: apply_x = True if b + 1 <= n - 1 and lst1[b + 1] > mx: apply_x = True if not apply_x and l < k: return False rem = l % k res += rem * y l -= rem if x <= y * k: res += l // k * x elif apply_x: res += l * y else: res += (l - k) * y + x return True def solve(): global res i, j = 0, 0 start = 0 while i < m: while j < n and lst1[j] != lst2[i]: j += 1 if j == n: return -1 if not update(start, j - 1): return -1 j += 1 start = j i += 1 if not update(start, n - 1): return -1 return res res = 0 n, m = map(int, input().split()) x, k, y = map(int, input().split()) lst1 = list(map(int, input().split())) lst2 = list(map(int, input().split())) print(solve())
FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
import sys mod = 1000000007 eps = 10**-9 def main(): import sys input = sys.stdin.readline class SegmentTree: def __init__(self, A, initialize=True, segfunc=max, ident=-2000000000): self.N = len(A) self.LV = (self.N - 1).bit_length() self.N0 = 1 << self.LV self.segfunc = segfunc self.ident = ident if initialize: self.data = ( [self.ident] * self.N0 + A + [self.ident] * (self.N0 - self.N) ) for i in range(self.N0 - 1, 0, -1): self.data[i] = segfunc(self.data[i * 2], self.data[i * 2 + 1]) else: self.data = [self.ident] * (self.N0 * 2) def update(self, i, x): i += self.N0 - 1 self.data[i] = x for _ in range(self.LV): i >>= 1 self.data[i] = self.segfunc(self.data[i * 2], self.data[i * 2 + 1]) def query(self, l, r): l += self.N0 - 1 r += self.N0 - 1 ret_l = self.ident ret_r = self.ident while l < r: if l & 1: ret_l = self.segfunc(ret_l, self.data[l]) l += 1 if r & 1: ret_r = self.segfunc(self.data[r - 1], ret_r) r -= 1 l >>= 1 r >>= 1 return self.segfunc(ret_l, ret_r) N, M = map(int, input().split()) x, k, y = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) idx = {a: i for i, a in enumerate(A)} i_list = [0] for b in B: i_list.append(idx[b] + 1) i_list.append(N + 1) for i in range(M + 1): if i_list[i] > i_list[i + 1]: print(-1) exit() ST = SegmentTree(A) ans = 0 for j in range(M + 1): i1 = i_list[j] i2 = i_list[j + 1] L = i2 - i1 - 1 if L == 0: continue ma = 0 if i1 > 0: ma = max(ma, A[i1 - 1]) if i2 <= N: ma = max(ma, A[i2 - 1]) if ma > ST.query(i1 + 1, i2): cost = min(y * L, y * (L % k) + x * (L // k)) else: if L < k: print(-1) exit() cost = min(y * (L % k) + x * (L // k), y * (L - k) + x) ans += cost print(ans) main()
IMPORT ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF IMPORT ASSIGN VAR VAR CLASS_DEF FUNC_DEF NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP LIST VAR VAR VAR BIN_OP LIST VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER FUNC_DEF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
n, m = map(int, input().split()) x, k, y = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) c = [] j = 0 for i in range(n): if j >= m: break if a[i] == b[j]: c.append(0) j += 1 else: c.append(1) if j != m: print(-1) return while len(c) != n: c.append(1) back_l = -1 back_r = -1 ans = 0 while True: l = back_l r = back_r for i in range(back_r + 1, n): if c[i] == 1: l = i r = i break M = 0 for i in range(r, n): M = max(M, a[i]) if i + 1 >= n: r = i elif c[i + 1] == 0: r = i break else: r = i if l == back_l or r == back_r: break N = r - l + 1 left = -1 right = -1 if l > 0: left = a[l - 1] if r < n - 1: right = a[r + 1] if N < k and M > left and M > right: print(-1) return if x / k <= y: ans += N // k * x N -= N // k * k ans += y * N elif M < left or M < right: ans += y * N else: ans += x N -= k ans += y * N back_l = l back_r = r print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN WHILE FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN IF BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
def log_dec(func): def wrapper(*args, **kwargs): print("Called with values: ") print("args: ", args) print("kwargs: ", kwargs) result = func(*args, **kwargs) print("Result: ", result) return result return wrapper def count_mnmx(seq, si, ei, lbn=-1, rbn=-1): lmr = max([lbn, rbn]) mn = 0 mx = 0 for i in range(si, ei): if seq[i] > lmr: mx += 1 if seq[i] < lmr: mn += 1 return [mn, mx] def calculate_minimum_mana(seq, si, ei, x, k, y, lbn=-1, rbn=-1): minn, maxn = count_mnmx(seq, si, ei, lbn, rbn) length = ei - si if minn == 0 and maxn == 0: return 0 if maxn > 0 and length < k: return -1 if x <= k * y: return length // k * x + length % k * y return (maxn > 0) * x + (length - k * (maxn > 0)) * y def main(): intifier = lambda x: int(x) n, m = list(map(intifier, input().split(" "))) x, k, y = list(map(lambda x: int(x), input().split(" "))) a = list(map(intifier, input().split(" "))) b = list(map(intifier, input().split(" "))) ia = [(-1) for i in range(n + 1)] for i, ai in enumerate(a): ia[ai] = i mana = 0 prev_s = 0 for indx, curr_s in enumerate(b): if ia[prev_s] > ia[curr_s]: print(-1) return None lbn = prev_s rbn = curr_s if indx < m else -1 si = ia[prev_s] + 1 if prev_s != 0 else 0 ei = ia[curr_s] if curr_s != -1 else m tmp_mana = calculate_minimum_mana(a, si, ei, x, k, y, lbn, rbn) if tmp_mana < 0: print(-1) return None mana += tmp_mana prev_s = curr_s if ia[prev_s] < n - 1: tmp_mana = calculate_minimum_mana(a, ia[prev_s] + 1, n, x, k, y, prev_s, -1) if tmp_mana < 0: print(-1) return None mana += tmp_mana print(mana) return None main()
FUNC_DEF FUNC_DEF EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR STRING VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING VAR RETURN VAR RETURN VAR FUNC_DEF NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER RETURN LIST VAR VAR FUNC_DEF NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR VAR RETURN NUMBER IF VAR BIN_OP VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR RETURN BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN NONE ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN NONE VAR VAR ASSIGN VAR VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN NONE VAR VAR EXPR FUNC_CALL VAR VAR RETURN NONE EXPR FUNC_CALL VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
import sys from sys import stdin n, m = map(int, stdin.readline().split()) x, k, y = map(int, stdin.readline().split()) a = list(map(int, stdin.readline().split())) b = list(map(int, stdin.readline().split())) a = [0] + a + [-1] b = [0] + b + [-1] bi = 0 lis = [] ans = 0 for i in range(1, len(a)): if a[i] == b[bi + 1]: if len(lis) == 0: bi += 1 continue l = b[bi] r = b[bi + 1] tmp = float("inf") if max(lis) > max(l, r) and len(lis) < k: print(-1) sys.exit() elif max(lis) < max(l, r): tmp = min(tmp, y * len(lis), len(lis) % k * y + len(lis) // k * x) else: tmp = min(tmp, x + (len(lis) - k) * y, len(lis) % k * y + len(lis) // k * x) ans += tmp lis = [] bi += 1 else: lis.append(a[i]) if bi != len(b) - 1: print(-1) else: print(ans)
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR LIST NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
from sys import stdin def inp(): return stdin.buffer.readline().rstrip().decode("utf8") def itg(): return int(stdin.buffer.readline()) def mpint(): return map(int, stdin.buffer.readline().split()) len_a, len_b = mpint() x, k, y = mpint() a = list(mpint()) b = list(mpint()) i_love_fireball = x / k <= y def main(): i1 = i2 = 0 pos = [] while i1 < len_a and i2 < len_b: if a[i1] == b[i2]: pos.append(i1) i2 += 1 i1 += 1 if i2 != len_b: return -1 assert len(pos) == len_b ans = 0 left_power = left_idx = -1 for i in range(len_b + 1): if i == len_b: right_idx = len_a right_power = -87 else: right_idx = pos[i] right_power = a[right_idx] sub = a[left_idx + 1 : right_idx] if not sub: left_idx = right_idx left_power = right_power continue am = len(sub) if am < k: if max(sub) > max(left_power, right_power): return -1 ans += am * y elif i_love_fireball: q, r = divmod(am, k) ans += q * x + r * y elif max(sub) > max(left_power, right_power): q, r = 1, am - k ans += q * x + r * y else: ans += am * y left_idx = right_idx left_power = right_power return ans print(main())
FUNC_DEF RETURN FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN NUMBER VAR BIN_OP VAR VAR IF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
n, m = map(int, input().split()) x, k, y = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) bIndex = 0 rangeCount = 0 rangeArray = [] manaSpent = 0 tf = True for i in range(n): if bIndex < m and a[i] == b[bIndex]: if rangeCount >= k: if x / k <= y: manaSpent += x * (rangeCount // k) + y * (rangeCount % k) else: greaterThan = True for j in rangeArray: if bIndex == 0: if b[bIndex] < j: greaterThan = False elif b[bIndex - 1] < j and b[bIndex] < j: greaterThan = False if greaterThan == True: manaSpent += y * rangeCount else: manaSpent += x + y * (rangeCount - k) else: greaterThan = True for j in rangeArray: if bIndex == 0: if b[bIndex] < j: greaterThan = False elif b[bIndex - 1] < j and b[bIndex] < j: greaterThan = False if greaterThan == True: manaSpent += y * rangeCount else: tf = False print(-1) break rangeCount = 0 rangeArray = [] bIndex += 1 else: rangeCount += 1 rangeArray.append(a[i]) if tf == True: if bIndex < m: print(-1) else: tf = True if rangeCount >= k: if x / k <= y: manaSpent += x * (rangeCount // k) + y * (rangeCount % k) else: greaterThan = True for j in rangeArray: if b[bIndex - 1] < j: greaterThan = False if greaterThan == True: manaSpent += y * rangeCount else: manaSpent += x + y * (rangeCount - k) else: greaterThan = True for j in rangeArray: if b[bIndex - 1] < j: greaterThan = False if greaterThan == True: manaSpent += y * rangeCount else: tf = False print(-1) if tf == True: print(manaSpent)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR IF VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
list_input = lambda: list(map(int, input().split())) int_input = lambda: int(input()) def count_vals(l): d = {} for i in l: d[i] = d.get(i, 0) + 1 return d def get_delete_elements(a, b): j = 0 elem = [] for i in range(len(a)): if j == len(b): elem.append(i) continue if a[i] == b[j]: j += 1 else: elem.append(i) if j != len(b): return -1 return elem def get_val(count, x, y, k, flag): a = count // k m = count % k if flag: if a == 0: return -1 elif k * y <= x: return (count - k) * y + x else: return a * x + m * y else: if k * y <= x: return count * y return a * x + m * y n, m = list_input() x, k, y = list_input() a = list_input() b = list_input() elem = get_delete_elements(a, b) if elem == -1: print(-1) else: ans, i = 0, 0 temp = 0 while i < len(elem): if elem[i] == 0: p = -1 else: p = a[elem[i] - 1] j = i + 1 count = 1 while j < len(elem) and elem[j] == elem[j - 1] + 1: j += 1 count += 1 if elem[j - 1] == len(a) - 1: q = -1 else: q = a[elem[j - 1] + 1] m = max(p, q) if max(a[elem[i] : elem[j - 1] + 1]) > m: flag = 1 else: flag = 0 temp = get_val(count, x, y, k, flag) if temp == -1: break ans += temp i = j if temp == -1: print(-1) else: print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR RETURN BIN_OP VAR VAR RETURN BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
n, m = map(int, input().split()) x, k, y = map(int, input().split()) arr = list(map(int, input().split())) brr = list(map(int, input().split())) used = [(False) for i in range(n + 1)] f = True res = 0 old, mx, idx, ln = 0, 0, 0, 0 def calc(): global old, mx, idx, ln, res, f, i if ln < k: if old > mx or i > mx: res = res + ln * y else: f = False return if ln % k > 0: res = res + ln % k * y ln = ln - ln % k if x <= y * k: res = res + ln // k * x elif old > mx or i > mx: res = res + ln * y else: res = res + (ln - k) * y + x for i in brr: if used[i] == True: f = False break while idx < n: used[arr[idx]] = True if arr[idx] == i: idx = idx + 1 break mx = max(mx, arr[idx]) ln = ln + 1 idx = idx + 1 calc() old, mx, ln = i, 0, 0 while idx < n: mx = max(mx, arr[idx]) ln = ln + 1 idx = idx + 1 calc() if f == False: print(-1) else: print(res)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER FUNC_DEF IF VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR NUMBER RETURN IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR FOR VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
n, m = input().split(" ") n, m = int(n), int(m) x, k, y = input().split(" ") x, k, y = int(x), int(k), int(y) A = input().split(" ") B = input().split(" ") for i in range(n): A[i] = int(A[i]) for i in range(m): B[i] = int(B[i]) indexA = 0 indexB = 0 poss = 1 while indexB < m: while indexA < n and B[indexB] != A[indexA]: indexA += 1 if indexA != n: B[indexB] = indexA indexB += 1 else: indexB = m poss = 0 if 0 < B[0] < k and max(A[: B[0]]) > A[B[0]]: poss = 0 if 0 < n - 1 - B[m - 1] < k and max(A[B[m - 1] + 1 :]) > A[B[m - 1]]: poss = 0 for i in range(1, m): if 0 < B[i] - B[i - 1] - 1 < k and max(A[B[i - 1] + 1 : B[i]]) > max( A[B[i]], A[B[i - 1]] ): poss = 0 if poss == 0: print(-1) elif x / k > y: ans = 0 if B[0] > 0: if max(A[: B[0]]) > A[B[0]]: ans += x + (B[0] - k) * y else: ans += B[0] * y if 0 < n - 1 - B[m - 1]: if max(A[B[m - 1] + 1 :]) > A[B[m - 1]]: ans += x + (n - 1 - B[m - 1] - k) * y else: ans += (n - 1 - B[m - 1]) * y for i in range(1, m): if 0 < B[i] - B[i - 1] - 1: if max(A[B[i - 1] + 1 : B[i]]) > max(A[B[i]], A[B[i - 1]]): ans += x + (B[i] - B[i - 1] - 1 - k) * y else: ans += (B[i] - B[i - 1] - 1) * y print(ans) else: ans = ( int(B[0] / k) * x + B[0] % k * y + int((n - 1 - B[m - 1]) / k) * x + (n - 1 - B[m - 1]) % k * y ) for i in range(1, m): ans += int((B[i] - 1 - B[i - 1]) / k) * x + (B[i] - 1 - B[i - 1]) % k * y print(ans)
ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF NUMBER BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR IF NUMBER BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
n, m = map(int, input().split()) x, k, y = map(int, input().split()) pref = (x - 1) // k + 1 <= y a = list(map(int, input().split())) a.append(0) b = list(map(int, input().split())) b.append(0) i, i_prev = -1, -1 ans = 0 for el in b: i_prev = i try: i = a.index(el, i + 1) except ValueError: ans = -1 break di = i - i_prev - 1 if di: if pref: if a[i_prev] < max(a[i_prev + 1 : i]) > a[i] and di < k: ans = -1 break ans += x * (di // k) + y * (di % k) elif a[i_prev] < max(a[i_prev + 1 : i]) > a[i]: if di < k: ans = -1 break ans += x + y * (di - k) else: ans += y * di print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR IF VAR IF VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
from sys import exit, stdin, stdout stdin.readline() x, k, y = [int(i) for i in stdin.readline().split(" ") if i] a = [int(i) for i in stdin.readline().split(" ") if i] b = [int(i) for i in stdin.readline().split(" ") if i] a[:0] = [0] a.append(0) b.append(0) if x >= y * k: def calc(a): n = len(a) - 2 if max(a) == max(a[0], a[-1]): return n * y if n < k: stdout.write("-1\n") return return (n - k) * y + x else: def calc(a): n = len(a) - 2 if n < k: if max(a) == max(a[0], a[-1]): return n * y stdout.write("-1\n") return return n // k * x + n % k * y n = 0 i = 0 for j in b: t = i + 1 while t < len(a): if a[t] == j: break t += 1 else: stdout.write("-1\n") return if t > i + 1: n += calc(a[i : t + 1]) i = t stdout.write(f"{n}\n")
EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING VAR ASSIGN VAR NUMBER LIST NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING RETURN RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING RETURN RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN IF VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR STRING
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
def remove_section(s, x, k, y): if len(s) <= 2: return 0 berserk = len(s) - 2 m = max(s) for i in range(1, len(s) - 1): if s[i] == m: berserk -= 1 fireball = (len(s) - 2) // k if k * fireball + berserk < len(s) - 2: return -1 if k * y > x: return x * fireball + y * (len(s) - 2 - fireball * k) else: if berserk < len(s) - 2: return x + (len(s) - 2 - k) * y return y * (len(s) - 2) n, m = map(int, input().split()) x, k, y = map(int, input().split()) a = list(map(int, input().split())) a.insert(0, int(-1000000000.0)) a.append(int(-1000000000.0)) b = list(map(int, input().split())) b.append(int(-1000000000.0)) ans = 0 i, j = 0, 0 prev = 0 while i < len(b): flag = True prev = j while j < len(a): if a[j] == b[i]: flag = False break j += 1 if flag: ans = -1 break curr = remove_section(a[prev : j + 1], x, k, y) if curr == -1: ans = -1 break ans += remove_section(a[prev : j + 1], x, k, y) i += 1 print(ans)
FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF BIN_OP VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN BIN_OP VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR RETURN BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
n, m = map(int, input().split()) x, k, y = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) j = 0 found = 0 indicators = [False] * n for i in range(m): while j < n and a[j] != b[i]: j += 1 if j == n: break if a[j] == b[i]: found += 1 indicators[j] = True if found < m: print(-1) else: cost = 0 lptr = -1 def destroy(lptr, rptr): global x, k, y, n if rptr - lptr - 1 < 1: return 0 if lptr == -1: leftFlank = 0 else: leftFlank = a[lptr] if rptr == n: rightFlank = 0 else: rightFlank = a[rptr] if rptr - lptr - 1 < k: maxVal = max(a[lptr + 1 : rptr]) if maxVal < max(leftFlank, rightFlank): return y * (rptr - lptr - 1) return -1 elif rptr - lptr - 1 == k: maxVal = max(a[lptr + 1 : rptr]) if maxVal < max(leftFlank, rightFlank): return min(y * k, x) return x else: maxVal = max(a[lptr + 1 : rptr]) diff = rptr - lptr - 1 if maxVal < max(leftFlank, rightFlank): if x < y * k: if diff % k == 0: return diff // k * x else: return diff // k * x + diff % k * y return diff * y else: rem = diff % k if rem == 0: return min(x * (diff // k), (diff - k) * y + x) else: return rem * y + min(x * (diff // k), (diff - k - rem) * y + x) for i in range(n): if indicators[i]: val = destroy(lptr, i) if val == -1: cost = -1 break cost += val lptr = i left = a[i] middleMax = None if cost != -1 and not indicators[-1]: val = destroy(lptr, n) if val == -1: cost = -1 else: cost += val print(cost)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NONE IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
import sys inpy = [int(x) for x in sys.stdin.read().split()] n, m, x, k, y = inpy[0:5] a, b = inpy[5 : 5 + n], inpy[5 + n :] prei = -1 i, j = 0, 0 res = 0 def remove(l, r): if l > r: return 0 lp, rp = a[l - 1] if l > 0 else 0, a[r + 1] if r + 1 < len(a) else 0 maxV = max(a[l : r + 1]) res = None if maxV < max(lp, rp): res = (r - l + 1) * y if r - l + 1 >= k: if x / k > y: cur = x + (r - l + 1 - k) * y else: cur = (r - l + 1) // k * x + (r - l + 1) % k * y if res: res = min(res, cur) else: res = cur return res while i < len(a) and j < len(b): if a[i] == b[j]: xx = remove(prei + 1, i - 1) if xx == None: break res += xx prei = i j += 1 i += 1 if j == len(b): xx = remove(prei + 1, len(a) - 1) if xx == None: print(-1) else: print(res + xx) else: print(-1)
IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NONE IF VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR RETURN VAR WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NONE VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NONE EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
def solve(A, B, fireball_const_range, berserk_cost): fireball_cost, fireball_range = fireball_const_range N, M = len(A), len(B) should_keep = set(B) fireball_more_expensive = berserk_cost * fireball_range < fireball_cost i, j = 0, 0 while i < N and j < M: if A[i] == B[j]: j += 1 i += 1 if j != M: return -1 def delete_interval_min_cost(i, j): assert i < j if i + 1 == j: return 0 left_elem = A[i] if i >= 0 else float("-inf") right_elem = A[j] if j < N else float("-inf") size = j - i - 1 max_elem = max(A[i + 1 : j]) can_use_berserk = left_elem > max_elem or right_elem > max_elem if size < fireball_range and not can_use_berserk: return -1 elif size < fireball_range and can_use_berserk: return size * berserk_cost elif fireball_more_expensive and can_use_berserk: return size * berserk_cost elif fireball_more_expensive and not can_use_berserk: one_fireball_cost = fireball_cost remaining_elems = size - fireball_range return one_fireball_cost + remaining_elems * berserk_cost elif size >= fireball_range: fireball_count = size // fireball_range berserk_count = size % fireball_range return fireball_count * fireball_cost + berserk_count * berserk_cost else: raise ValueError("Unhandled case") ans = 0 prev_start = -1 i = 0 while i < N: if A[i] in should_keep: res = delete_interval_min_cost(prev_start, i) if res < 0: return -1 ans += res prev_start = i i += 1 if A[-1] not in should_keep: res = delete_interval_min_cost(prev_start, N) if res < 0: return -1 ans += res return ans def main(): N, M = list(map(int, input().split())) X, K, Y = list(map(int, input().split())) A = list(map(int, input().split())) B = list(map(int, input().split())) ans = solve(A, B, (X, K), Y) print(ans) main()
FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER FUNC_DEF VAR VAR IF BIN_OP VAR NUMBER VAR RETURN NUMBER ASSIGN VAR VAR NUMBER VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR VAR RETURN NUMBER IF VAR VAR VAR RETURN BIN_OP VAR VAR IF VAR VAR RETURN BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN BIN_OP VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN NUMBER VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
import sys def input(): return sys.stdin.readline().rstrip() def input_split(): return [int(i) for i in input().split()] n, m = input_split() x, k, y = input_split() given = input_split() target = input_split() starget = set(target) inverse_given = {given[i]: i for i in range(n)} inverse_target = {target[i]: i for i in range(m)} to_delete_inds = [] for i in range(n): if given[i] not in starget: to_delete_inds.append(i) cant_be_done = False last_ind = -1 for a in target: if a not in inverse_given: cant_be_done = True break elif inverse_given[a] < last_ind: cant_be_done = True break else: last_ind = inverse_given[a] if cant_be_done: ans = -1 elif len(to_delete_inds) == 0: ans = 0 else: ans = 0 all_groups = [] current_group = [to_delete_inds[0]] for i in range(1, len(to_delete_inds)): if to_delete_inds[i] == current_group[-1] + 1: current_group.append(to_delete_inds[i]) else: all_groups.append(current_group) current_group = [to_delete_inds[i]] all_groups.append(current_group) for current_group in all_groups: if current_group[0] == 0: left_max = -1 else: left_max = given[current_group[0] - 1] if current_group[-1] == n - 1: right_max = -1 else: right_max = given[current_group[-1] + 1] me = max(left_max, right_max) maxi = max([given[ind] for ind in current_group]) if len(current_group) < k: if me > maxi: ans += len(current_group) * y else: wrong = True ans = -1 break elif me > maxi: if x > k * y: ans += len(current_group) * y else: ans += len(current_group) // k * x + len(current_group) % k * y elif x > k * y: ans += x + (len(current_group) - k) * y else: ans += len(current_group) // k * x + len(current_group) % k * y print(ans)
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR IF VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR IF VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR IF VAR BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
def get_ints(): return map(int, input().split()) def main(): n, m = get_ints() x, k, y = get_ints() a = list(get_ints()) b = list(get_ints()) def compute(l, r): cnt = r - l - 1 if cnt == 0: return 0 s = a[l + 1 : r] maks = max(a[z] if z >= 0 and z < n else 0 for z in [l, r]) need_x = maks < max(s) res = 0 if need_x: if cnt < k: return -1 else: cnt -= k res += x if x < y * k: res += x * (cnt // k) + y * (cnt % k) else: res += cnt * y return res apos = {a[i]: i for i in range(n)} bpos = [apos.get(e, n) for e in b] if sorted(bpos) != bpos: return -1 bpos += [n, -1] ans = 0 for i in range(m + 1): res = compute(bpos[i - 1], bpos[i]) if res < 0: return -1 ans += res return ans print(main())
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR LIST VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR IF VAR VAR RETURN NUMBER VAR VAR VAR VAR IF VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER VAR LIST VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER RETURN NUMBER VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
def solve(L1, L2, k, F, B): Fireball = F / k < B cost = 0 L1.append(0) L2.append(0) L = 0 pointer = 0 for i in L2: R = i maxSoFar = 0 numWar = 0 while L1[pointer] != R: maxSoFar = max(maxSoFar, L1[pointer]) numWar += 1 pointer += 1 if pointer >= len(L1): return -1 if numWar < k and maxSoFar > L and maxSoFar > R: return -1 elif numWar < k: cost += B * numWar elif Fireball == True: cost += numWar // k * F + numWar % k * B elif maxSoFar > L and maxSoFar > R: cost += F + (numWar - k) * B else: cost += numWar * B pointer += 1 L = R return cost _ = input() F, k, B = list(map(int, input().split(" "))) L1 = list(map(int, input().split(" "))) L2 = list(map(int, input().split(" "))) print(solve(L1, L2, k, F, B))
FUNC_DEF ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
import sys input = sys.stdin.buffer.readline n, m = [int(x) for x in input().split()] x, k, y = [int(x) for x in input().split()] a = [int(x) for x in input().split()] b = [int(x) for x in input().split()] gaps = [] s = None j = 0 for i in range(n): if s == None and a[i] != b[j]: s = i if s != None and a[i] == b[j]: gaps.append([s, i - 1]) s = None if s == None: j += 1 if j == m: break if j == m: gaps.append([i + 1, n - 1]) def getMin(l, r): g = r - l + 1 maxx = -1 for i in range(l, r + 1): if a[i] > maxx: maxx = a[i] maxSurround = -1 if l > 0: maxSurround = max(maxSurround, a[l - 1]) if r < n - 1: maxSurround = max(maxSurround, a[r + 1]) if g < k and maxSurround < maxx: return -1 if y * k < x: if g < k: return g * y elif maxx < maxSurround: return g * y else: return x + (g - k) * y else: return g % k * y + g // k * x ok = True rmCnt = 0 for l, r in gaps: rmCnt += r + 1 - l if n - m != rmCnt: print(-1) ok = False if ok: total = 0 for l, r in gaps: res = getMin(l, r) if res == -1: print(-1) ok = False break else: total += res if ok: print(total)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NONE ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NONE VAR VAR VAR VAR ASSIGN VAR VAR IF VAR NONE VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR NONE IF VAR NONE VAR NUMBER IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR IF VAR VAR RETURN BIN_OP VAR VAR IF VAR VAR RETURN BIN_OP VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR VAR IF VAR EXPR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
la, z = map(int, input().split()) cost1, range, cost2 = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) b.append(0) maxi = 0 add, j, i = 0, 0, 0 rama = [] while True: if j == la: break if a[j] == b[i]: rama.append([add, maxi]) i += 1 j += 1 maxi, add = 0, 0 else: add += 1 maxi = max(maxi, a[j]) j += 1 rama.append([add, maxi]) b = [0] + b cost, flag = 0, 0 if i != z or j != la: print(-1) elif i == z and j == la and len(rama) == 0: print(0) elif cost1 <= cost2 * range: i = 1 while i < z + 2: f = b[i] e = b[i - 1] maxu = max(f, e) maxi = max(maxu, rama[i - 1][1]) c = rama[i - 1][0] if range <= c: cost += c // range * cost1 + c % range * cost2 elif maxi == maxu: cost += c % range * cost2 else: flag = -1 break i += 1 if flag == -1: print(-1) else: print(cost) else: i = 1 while i < z + 2: f = b[i] e = b[i - 1] maxu = max(f, e) maxi = max(maxu, rama[i - 1][1]) c = rama[i - 1][0] if range <= c: if maxi == maxu: cost += c * cost2 else: cost += cost1 + (c - range) * cost2 elif maxi == maxu: cost += c * cost2 else: flag = -1 break i += 1 if flag == -1: print(-1) else: print(cost)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST WHILE NUMBER IF VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
import sys def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [([c] * b) for i in range(a)] def list3d(a, b, c, d): return [[([d] * c) for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[([e] * d) for j in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)] def Yes(): print("Yes") def No(): print("No") def YES(): print("YES") def NO(): print("NO") INF = 10**19 MOD = 10**9 + 7 N, M = MAP() X, K, Y = MAP() A = LIST() B = LIST() j = 0 tmp = [-INF, []] C = [] for i in range(N): if j != M and A[i] == B[j]: tmp.append(A[i]) C.append(tmp) tmp = [A[i], []] j += 1 else: tmp[1].append(A[i]) tmp.append(-INF) C.append(tmp) if j != M: print(-1) exit() ans = 0 for prev, li, nxt in C: if li: cur = INF d, m = divmod(len(li), K) for i in range(d + 1): if i != 0 or max(li) < max(prev, nxt): cur = min(cur, i * X + (d - i) * K * Y + m * Y) if cur == INF: print(-1) exit() ans += cur print(ans)
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF NONE RETURN VAR NONE FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR VAR LIST VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
n, m = map(int, input().split()) x, k, y = map(int, input().split()) a = list(map(int, input().split())) where = [0] * n for i in range(n): a[i] -= 1 where[a[i]] = i left = [-1] b = list(map(int, input().split())) for i in range(m): b[i] -= 1 left.append(where[b[i]]) left.append(n) ans = 0 for i in range(1, len(left)): if left[i - 1] > left[i]: print(-1) exit(0) mx = -1 mx_idx = 0 for j in range(max(0, left[i - 1]), min(n, left[i] + 1)): if a[j] > mx: mx = a[j] mx_idx = j l = left[i] - left[i - 1] - 1 if l <= 0: continue segments = l // k remainder = l % k if mx_idx == left[i - 1] or mx_idx == left[i]: ans += min(l * y, segments * x + remainder * y) elif segments == 0: print(-1) exit(0) else: ans += min(x + (l - k) * y, segments * x + remainder * y) print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
n, m = map(int, input().split()) x, k, y = map(int, input().split()) a = [int(i) for i in input().split()] b = [int(i) for i in input().split()] if n == m: if a == b: print(0) else: print(-1) exit() now = 0 l = [-1] for i in range(n): if b[now] == a[i]: l.append(i) now += 1 if now == m: break l.append(n) if now != m: print(-1) exit() ans = 0 for q in range(1, len(l)): w = l[q] - l[q - 1] - 1 if w <= 0: continue if w < k: if l[q] < n: c1 = a[l[q]] else: c1 = 0 if l[q - 1] == -1: c2 = 0 else: c2 = a[l[q - 1]] if max(a[max(0, l[q - 1]) : l[q]]) > max(c1, c2): print(-1) exit() else: ans += y * w elif x <= y * k: ans += x * (w // k) + y * (w % k) else: if l[q] < n: c1 = a[l[q]] else: c1 = 0 if l[q - 1] == -1: c2 = 0 else: c2 = a[l[q - 1]] if max(a[max(0, l[q - 1]) : l[q]]) > max(c1, c2): ans += x + y * (w - k) else: ans += y * w print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER IF VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
def kill(left, a, right, x, k, y): g = len(a) if g < k: if max(a) > max(left, right): print(-1) exit(0) if x < y * k: return g // k * x + g % k * y elif max(a) > max(left, right): return x + (g - k) * y else: return g * y def main(): n, m = map(int, input().split(" ")) x, k, y = map(int, input().split(" ")) a = list(map(int, input().split(" "))) b = list(map(int, input().split(" "))) i = 0 j = 0 g = 0 M = 0 while i < n and j < m: if a[i] == b[j]: if g > 0: M += kill(b[j - 1] if j > 0 else 0, a[i - g : i], b[j], x, k, y) g = 0 i += 1 j += 1 else: i += 1 g += 1 if i == n and j < m: print(-1) return g += n - i if g > 0: M += kill(b[j - 1] if j > 0 else 0, a[n - g : n], b[j] if j < m else 0, x, k, y) print(M) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR RETURN BIN_OP VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR BIN_OP VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
def fire(a, x, k, y, l, r): big = max(a[l - 1] if l > 0 else -1, a[r + 1] if r < len(a) - 1 else -1) worst = max(a[i] for i in range(l, r + 1)) cnt = r - l + 1 if cnt < k and worst > big: return -1 return y * (cnt % k) + x * (cnt // k) def berserk(a, x, k, y, l, r): big = max(a[l - 1] if l > 0 else -1, a[r + 1] if r < len(a) - 1 else -1) worst = max(a[i] for i in range(l, r + 1)) cnt = r - l + 1 if worst < big: return y * cnt if cnt < k: return -1 return x + (cnt - k) * y def doit(a, x, k, y, l, r): return fire(a, x, k, y, l, r) if x < k * y else berserk(a, x, k, y, l, r) def solve(): n, m = map(int, input().split()) x, k, y = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) i, j = 0, 0 start = -1 price = 0 while i < m and j < n: if b[i] == a[j]: if start != -1: t = doit(a, x, k, y, start, j - 1) if t == -1: return -1 price += t i += 1 j += 1 start = -1 else: if start == -1: start = j j += 1 if i < m: return -1 if j < n: t = doit(a, x, k, y, j, n - 1) if t == -1: return -1 price += t return price print(solve())
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR RETURN BIN_OP VAR VAR IF VAR VAR RETURN NUMBER RETURN BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR FUNC_DEF RETURN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN NUMBER VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
from sys import stdin n, m = map(int, stdin.readline().rstrip().split(" ")) x, k, y = map(int, stdin.readline().rstrip().split(" ")) l = list(map(int, stdin.readline().rstrip().split(" "))) l2 = list(map(int, stdin.readline().rstrip().split(" "))) z = {} for i in range(n): z[l[i]] = i mana = 0 start = -1 arr = [] for i in range(m): a = z[l2[i]] - start if start == -1: max_border = l2[i] else: max_border = max([l2[i], l[start]]) if a < 0: mana = -1 break elif a > 1: c = 0 ma = 0 for j in range(start + 1, z[l2[i]]): c += 1 ma = max(ma, l[j]) if c < k and ma > max_border: mana = -1 break elif k * y < x: if ma > max_border: adding = x + (c - k) * y mana += adding else: adding = c * y mana += adding else: adding = c // k * x + y * (c % k) mana += adding start = z[l2[i]] if mana != -1: if l2[-1] != l[-1]: c = 0 ma = 0 for i in range(z[l2[-1]] + 1, n): ma = max(ma, l[i]) c += 1 if c < k and ma > l2[-1]: mana = -1 elif k * y < x: if ma > l2[-1]: adding = x + (c - k) * y mana += adding else: adding = c * y mana += adding else: adding = c // k * x + y * (c % k) mana += adding print(mana)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
n, m = map(int, input().split()) x, k, y = map(int, input().split()) a = list(map(int, input().split())) c = list(map(int, input().split())) b = set(c) d = [] now = [] for i in range(n): if a[i] not in b: now.append(i) if i == n - 1: d.append([now[0] - 1, a.index(max(a[now[0] : now[-1] + 1])), now[-1] + 1]) elif now != []: d.append([now[0] - 1, a.index(max(a[now[0] : now[-1] + 1])), now[-1] + 1]) now = [] now = 0 lc = len(c) for i in range(n): if a[i] == c[now]: now += 1 if now == lc: break else: exit(print(-1)) l = len(d) ans = 0 for i in range(l): e = d[i] f = e[2] - e[0] - 1 if e[0] == -1: m = a[e[2]] elif e[2] == n: m = a[e[0]] else: m = max(a[e[0]], a[e[2]]) if m > a[e[1]]: ans += f % k * y ans += min(f // k * x, f // k * k * y) else: if f < k: exit(print(-1)) ans += f % k * y ans += x ans += min((f // k - 1) * x, (f // k - 1) * k * y) print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR LIST EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
n, m = list(map(int, input().split())) x, k, y = list(map(int, input().split())) a = list(map(int, input().split())) b = list(map(int, input().split())) i = 0 j = 0 to_kill = [True] * n while i < n and j < m: if a[i] == b[j]: j += 1 to_kill[i] = False i += 1 if j < m: print(-1) exit() last = -1 ans = 0 for i in range(n + 1): if i == n or not to_kill[i]: if i - last > 1: cnt = i - last - 1 strongest = max(a[last + 1 : i]) strongest_borders = 0 if i < n: strongest_borders = a[i] if last >= 0: strongest_borders = max(strongest_borders, a[last]) weak_count = 0 for j in range(last + 1, i): if a[j] < strongest or a[j] < strongest_borders: weak_count += 1 if ( cnt // k * k + weak_count < cnt or strongest > strongest_borders and cnt < k ): print(-1) exit() if k * y > x: ans += cnt // k * x if cnt % k != 0: ans += cnt % k * y else: rest = max(1 if strongest > strongest_borders else 0, cnt - weak_count) if rest % k != 0: rest = rest // k * k + k ans += rest // k * x ans += (cnt - rest) * y last = i print(ans)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
from sys import stdin n, m = map(int, stdin.readline().split()) x, k, y = map(int, stdin.readline().split()) a = list(map(int, stdin.readline().split())) b = list(map(int, stdin.readline().split())) fire = x < k * y c, hi, limit = 0, False, b[0] j = 0 M = 0 for i in range(n): if j < m and a[i] == b[j]: if hi: if c < k: M = -1 break else: M += x c -= k if fire: M += x * (c // k) + y * (c % k) else: M += c * y j += 1 if j < m: limit = max(b[j - 1], b[j]) else: limit = b[j - 1] c, hi = 0, False else: c += 1 if a[i] > limit: hi = True else: if j < m or hi and c < k: M = -1 else: if hi: M += x c -= k if fire: M += x * (c // k) + y * (c % k) else: M += c * y print(M)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR IF VAR IF VAR VAR ASSIGN VAR NUMBER VAR VAR VAR VAR IF VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR IF VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
n, m = list(map(int, input().split())) x, k, y = list(map(int, input().split())) start_ls = list(map(int, input().split())) end_ls = list(map(int, input().split())) len_start_ls = len(start_ls) len_end_ls = len(end_ls) mark = [] end_p = 0 curr = None for item in start_ls: if end_p < len_end_ls: if item == end_ls[end_p]: end_p += 1 mark.append(0) curr = item elif curr is not None: if item > curr: mark.append(1) else: mark.append(2) else: mark.append(1) elif curr is not None: if item > curr: mark.append(1) else: mark.append(2) else: mark.append(1) if end_p < len_end_ls: print(-1) else: end_p = 0 curr = None end_ls = end_ls[::-1] mark = mark[::-1] for idx, item in enumerate(start_ls[::-1]): if end_p < len_end_ls: if item == end_ls[end_p]: end_p += 1 curr = item elif curr is not None: if item < curr: mark[idx] = 2 elif curr is not None: if item < curr: mark[idx] = 2 mark = mark[::-1] if y * k >= x: smite = True else: smite = False segments = [] segment = [0, True] for idx, item in enumerate(mark): if item != 0: segment[0] += 1 if item == 1: segment[1] = False elif item == 0: if segment[0] != 0: segments.append(segment) segment = [0, True] if segment[0] != 0: segments.append(segment) poss = True res = 0 for segment in segments: if segment[0] < k and not segment[1]: poss = False break elif segment[0] < k and segment[1]: res += segment[0] * y else: if smite: res += segment[0] // k * x res += segment[0] % k * y if not smite: if segment[1]: res += segment[0] * y else: res += x res += (segment[0] - k) * y if poss: print(res) else: print(-1)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NONE FOR VAR VAR IF VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR IF VAR NONE IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NONE IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NONE ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR NONE IF VAR VAR ASSIGN VAR VAR NUMBER IF VAR NONE IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR IF VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
n, m = map(int, input().split()) x, k, y = map(int, input().split()) a = [int(k) for k in input().split()] b = [int(k) for k in input().split()] j = 0 index = [] for i in range(n): if j == m: break if a[i] == b[j]: index.append(i) j += 1 if len(index) != m: print(-1) else: ans = 0 counter = 0 for i in range(len(index)): if i == 0: mx = 0 for j in range(0, index[i]): mx = max(mx, a[j]) if mx < a[index[i]]: ans += min(index[i] // k * x + index[i] % k * y, index[i] * y) else: rem = index[i] - k if rem < 0: counter = 1 break ans += min(rem // k * x + rem % k * y, rem * y) ans += x continue mx = 0 for j in range(index[i - 1] + 1, index[i]): mx = max(mx, a[j]) rem = index[i] - index[i - 1] - 1 if mx < a[index[i]] or mx < a[index[i - 1]]: ans += min(rem // k * x + rem % k * y, rem * y) else: rem = rem - k if rem < 0: counter = 1 break ans += min(rem // k * x + rem % k * y, rem * y) ans += x mx = 0 for j in range(index[-1] + 1, n): mx = max(mx, a[j]) rem = n - index[-1] - 1 if mx < a[index[-1]]: ans += min(rem // k * x + rem % k * y, rem * y) else: rem = rem - k if rem < 0: counter = 1 else: ans += min(rem // k * x + rem % k * y, rem * y) ans += x if counter: print(-1) else: print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
n, m = map(int, input().split()) x, k, y = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) if a == b: print(0) exit() s = set(b) i = 0 j = 0 while i < n and j < m: if a[i] == b[j]: i += 1 j += 1 else: i += 1 if j != m: print(-1) exit() rem = [[]] occ = [] for i in range(n): if a[i] not in s: rem[-1].append(a[i]) else: rem[-1].sort(reverse=True) rem.append([]) occ.append(i) rem[-1].sort(reverse=True) maxa = max(rem) maxb = max(b) if k * y <= x: cost = 0 for i in range(len(rem) - 1): if len(rem[i]) == 0: continue j = 0 if rem[i][j] > max(a[occ[i - 1 if i > 0 else 0]], a[occ[i]]): cost += x j += k if j > len(rem[i]): print(-1) exit() cost += (len(rem[i]) - j) * y j = 0 if rem[-1][0] > a[occ[-1]]: cost += x j += k if j > len(rem[-1]): print(-1) exit() cost += (len(rem[-1]) - j) * y print(cost) else: cost = 0 d = occ[0] if d != 0 and d < k: if a[occ[0]] < max(a[0 : occ[0]]): print(-1) exit() cost += x * (d // k) + y * (d % k) for i in range(1, len(occ)): d = occ[i] - occ[i - 1] - 1 if d != 0 and d < k: if max(a[occ[i]], a[occ[i - 1]]) < max(a[occ[i - 1] + 1 : occ[i]]): print(-1) exit() cost += x * (d // k) + y * (d % k) d = n - occ[-1] - 1 if d != 0 and d < k: if a[occ[-1]] < max(a[occ[-1] + 1 :]): print(-1) exit() cost += x * (d // k) + y * (d % k) print(cost)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR VAR IF VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR NUMBER VAR VAR IF VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
def kill(left, arr, right, x, k, y): n = len(arr) if n == 0: return 0 strongest = max(arr) if left == None and right == None: if n < k: return -1 b = n // k r = n % k return min(b * x + r * y, x + (n - k) * y) borderMax = max([v for v in (left, right) if v != None]) if strongest < borderMax: b = n // k r = n % k return min(b * x + r * y, n * y) if n < k: return -1 b = n // k r = n % k return min(b * x + r * y, x + (n - k) * y) def solve(initial, target, x, k, y): v2pos = {v: i for i, v in enumerate(initial)} targetIndices = [] for v in target: if v not in v2pos: return -1 if targetIndices and v2pos[v] < targetIndices[-1]: return -1 targetIndices.append(v2pos[v]) tn = len(targetIndices) if tn == 0: return kill(None, initial, None, x, k, y) ans = kill(None, initial[: targetIndices[0]], initial[targetIndices[0]], x, k, y) if ans == -1: return -1 for i in range(tn - 1): sub = kill( initial[targetIndices[i]], initial[targetIndices[i] + 1 : targetIndices[i + 1]], initial[targetIndices[i + 1]], x, k, y, ) if sub == -1: return -1 ans += sub sub = kill( initial[targetIndices[tn - 1]], initial[targetIndices[tn - 1] + 1 :], None, x, k, y, ) if sub == -1: return -1 ans += sub return ans input() x, k, y = [int(item) for item in input().split()] initial = [int(item) for item in input().split()] target = [int(item) for item in input().split()] ans = solve(initial, target, x, k, y) print(ans)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NONE VAR NONE IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NONE IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR NONE VAR NONE VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NONE VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER RETURN NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NONE VAR VAR VAR IF VAR NUMBER RETURN NUMBER VAR VAR RETURN VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
import sys input = sys.stdin.readline n, m = list(map(int, input().split())) x, k, y = list(map(int, input().split())) B = list(map(int, input().split())) A = list(map(int, input().split())) inda = 0 for b in B: if b == A[inda]: inda += 1 if inda == len(A): break if inda != len(A): print(-1) return B += [0] A = set(A) | {0} ONE = 0 TWO = 0 C = [] ANS = 0 for b in B: if not b in A: C.append(b) else: ONE = TWO TWO = b MAX = max(ONE, TWO) L = len(C) ST = 0 for c in C: if c > MAX: ST += 1 if ST > 0 and L < k: print(-1) return elif ST > 0: COST = x rest = L - k COST += min(rest // k * x + rest % k * y, rest * y) ANS += COST C = [] else: COST = min(L // k * x + L % k * y, L * y) ANS += COST C = [] print(ANS)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR LIST NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
def get_val(x, k, y, left_val, right_val, arr): x, y = y, x if not arr: return 0 if len(arr) < k: if max(arr) > max(left_val, right_val): return -1 return len(arr) * x if y < x * k: n = len(arr) res = 0 while n >= k: n -= k res += y res += n * x return res elif max(arr) < max(left_val, right_val): return len(arr) * x else: return (len(arr) - k) * x + y def solve(x, k, y, a, b): def check(a, b): j = 0 i = 0 while i < len(a) and j < len(b): if a[i] != b[j]: i += 1 else: i += 1 j += 1 return j == len(b) if not check(a, b): return -1 j = 0 left_val = -1 arr = [] res = 0 for num in a: if j == len(b) or num != b[j]: arr.append(num) else: val = get_val(x, k, y, left_val, num, arr) if val == -1: return -1 res += val arr = [] left_val = num j += 1 if arr: val = get_val(x, k, y, left_val, -1, arr) if val == -1: return -1 res += val return res n, m = map(int, input().split()) x, k, y = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) print(solve(x, k, y, a, b))
FUNC_DEF ASSIGN VAR VAR VAR VAR IF VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR RETURN BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR IF VAR NUMBER RETURN NUMBER VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
n, m = map(int, input().split()) x, k, y = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) dic = {i: e for e, i in enumerate(a)} rest = [dic[b[i]] for i in range(m)] check = sorted(rest) if check != rest: print(-1) exit() rest = [-1] + rest + [n] b = [-1] + b + [-1] ans = 0 for i in range(m + 1): Lid, Rid = rest[i], rest[i + 1] L, R = b[i], b[i + 1] check = -1 for j in range(Lid + 1, Rid): check = max(check, a[j]) if max(L, R) >= check: if x < y * k: q = (Rid - Lid - 1) // k r = (Rid - Lid - 1) % k ans += q * x + r * y else: ans += (Rid - Lid - 1) * y elif x < y * k: check = Rid - Lid - 1 if check < k: print(-1) exit() q = (Rid - Lid - 1) // k r = (Rid - Lid - 1) % k ans += q * x + r * y else: check = Rid - Lid - 1 if check < k: print(-1) exit() else: ans += (check - k) * y + x print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR LIST VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
n, m = map(int, input().split()) x, k, y = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) check = 0 switch = 0 curr = b[check] prev = -1 sum = 0 def func(l, r, l1): if l1 == []: return 0 if len(l1) < k: if max(l1) > l and max(l1) > r: return -1 else: return len(l1) * y elif x > y * k: if max(l1) > l and max(l1) > r: return x + y * (len(l1) - k) else: return len(l1) * y else: final = x * (len(l1) // k) + y * (len(l1) % k) return final for i in range(0, n): if curr != a[i]: if switch == 0: prev = i switch = 1 else: check = check + 1 if switch == 1: value = func(a[prev - 1], a[i], a[prev:i]) if value == -1: sum = -1 break else: sum = sum + value switch = 0 if check > m - 1: extra = func(a[i], 0, a[i + 1 :]) sum = sum + extra break else: curr = b[check] if check < m: sum = -1 print(sum)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR LIST RETURN NUMBER IF FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
import itertools def fast(A, B, x, y, k): result = 0 Aset = set(A) Bset = set(B) if not Bset.issubset(Aset): return -1 filtered = [(False) for _ in A] ctr = 0 for i in range(len(A)): if A[i] == B[ctr]: filtered[i] = True ctr += 1 if ctr >= len(B): break if ctr != len(B): return -1 maxInRegions = [] currMax = 0 for i in range(len(filtered)): if filtered[i]: if currMax != 0: maxInRegions.append(currMax) currMax = 0 else: currMax = max(currMax, A[i]) if currMax != 0: maxInRegions.append(currMax) currMax = 0 result = 0 ctr = 0 left = None lastTrueVal = 0 for right in range(len(A)): if not filtered[right]: if left is None: left = right if currMax == 0: currMax = maxInRegions[ctr] ctr += 1 else: if currMax > 0: numBetween = right - left candidates = [] if A[right] >= currMax: candidates.append(numBetween * y) if left > 0 and A[left - 1] >= currMax: candidates.append(numBetween * y) if k == 1: candidates.append(numBetween * x) candidates.append((numBetween - 1) * y + x) elif numBetween >= k: candidates.append(numBetween % k * y + numBetween // k * x) candidates.append((numBetween - k) * y + x) if len(candidates) == 0: return -1 result += min(candidates) lastTrueVal = A[right] left = None currMax = 0 right = len(A) if ctr > 0 and left is not None: numBetween = right - left candidates = [] if lastTrueVal >= currMax: candidates.append(numBetween * y) if left > 0 and A[left - 1] >= currMax: candidates.append(numBetween * y) if k == 1: candidates.append(numBetween * x) candidates.append((numBetween - 1) * y + x) elif numBetween >= k: candidates.append(numBetween % k * y + numBetween // k * x) candidates.append((numBetween - k) * y + x) if len(candidates) == 0: return -1 result += min(candidates) return result def test(): x, k, y = 9, 1, 8 A = [8, 9, 5, 1] B = [8, 9, 1] f = fast(A, B, x, y, k) print(f) def main(): n, m = [int(_) for _ in input().split()] x, k, y = [int(_) for _ in input().split()] A = [int(_) for _ in input().split()] B = [int(_) for _ in input().split()] result = fast(A, B, x, y, k) print(result) main()
IMPORT FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NONE ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR NONE ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NONE ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NONE ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
n, m = list(map(lambda x: int(x), input().split())) x, k, y = list(map(lambda x: int(x), input().split())) a = list(map(lambda x: int(x), input().split())) b = list(map(lambda x: int(x), input().split())) max_index = 0 max_number = 0 for i in range(0, len(a)): if a[i] > max_number: max_number = a[i] max_index = i flag = 0 if len(a) == len(b): if all([(a[i] == b[i]) for i in range(n)]): print(0) flag = 1 else: print(-1) flag = 1 if flag == 0: i = 0 j = 0 intervals = [] if a[0] == b[0]: while i < len(a) and j < len(b) and a[i] == b[j]: i += 1 j += 1 j = i start = i end = i while i < len(a) and j < len(b): if a[i] == b[j]: if start != -1: intervals.append((start, end)) j += 1 i += 1 start = -1 end = -1 else: if start == -1: start = i end = i i += 1 if i <= len(a) - 1: intervals.append((i, len(a) - 1)) internvals_map = {} for interval in intervals: internvals_map[interval] = max(a[interval[0] : interval[1] + 1]) internvals_map = sorted(internvals_map.items(), key=lambda x: x[1]) use_x = True if k * y < x: use_x = False total = 0 flag = 0 a = [0] + a + [0] for interval, max in internvals_map[0 : len(internvals_map)]: if interval[1] - interval[0] + 1 >= k: if use_x: total += (interval[1] - interval[0] + 1) // k * x total += (interval[1] - interval[0] + 1) % k * y elif max > a[interval[0]] and max > a[interval[1] + 2]: total += (interval[1] - interval[0] + 1 - k) * y + x else: total += (interval[1] - interval[0] + 1) * y elif max > a[interval[0]] and max > a[interval[1] + 2]: print(-1) flag = 1 break else: total += (interval[1] - interval[0] + 1) * y if flag == 0: print(total)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST IF VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR LIST NUMBER FOR VAR VAR VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR IF VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR VAR IF VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR IF VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
n, m = list(map(int, input().split())) x, k, y = list(map(int, input().split())) arr = list(map(int, input().split())) to = list(map(int, input().split())) index = 0 li = [] for i in range(n): if arr[i] == to[index]: li.append(i) index += 1 if index == m: break if index != m: print(-1) exit() li = [-1] + li + [n] myli = [] for i in range(m + 1): myli.append((li[i] + 2, li[i + 1])) final = [] for i in myli: if i[0] <= i[1]: final.append(i) arr = [-999] + arr + [-999] ans = 0 for ele in final: l = ele[0] r = ele[1] le = r - l + 1 m = max(arr[l : r + 1]) if m >= arr[l - 1] and m >= arr[r + 1]: if le < k: print(-1) exit() if le == k: ans += x continue ans += min([x + (le - k) * y, le % k * y + le // k * x]) else: ans += min([le * y, le % k * y + le // k * x]) print(ans)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR LIST VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR VAR VAR VAR FUNC_CALL VAR LIST BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR LIST BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
import sys n, m = map(int, input().split()) x, k, y = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) if k * y < x: ycheap = 1 else: ycheap = 0 pointer = 0 ans = 0 for i in range(m): deletenum = 0 badnum = 0 if pointer == n: print(-1) sys.exit() start = pointer deletnum = 0 while a[pointer] != b[i]: if ( i == 0 and b[0] < a[pointer] or i > 0 and b[i] < a[pointer] and b[i - 1] < a[pointer] ): badnum = 1 pointer += 1 deletenum += 1 if pointer == n: print(-1) sys.exit() pointer += 1 if badnum == 1 and deletenum < k: print(-1) sys.exit() if ycheap == 1: if badnum == 1: ans += x ans += y * (deletenum - k) else: ans += y * deletenum else: ans += y * (deletenum % k) ans += x * (deletenum // k) badnum = 0 for i in range(pointer, n): if a[i] > b[-1]: badnum = 1 deletenum = n - pointer if badnum == 1 and deletenum < k: print(-1) sys.exit() if ycheap == 1: if badnum == 1: ans += x ans += y * (deletenum - k) else: ans += y * deletenum else: ans += y * (deletenum % k) ans += x * (deletenum // k) print(ans)
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER IF VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER IF VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
from sys import stdin def mp(): return map(int, stdin.readline().split()) def ml(): return list(map(int, stdin.readline().split())) n, m = mp() x, k, y = mp() a = ml() b = ml() def delSeg(st, end, mx): cnt = end - st aend = max(a[end], a[st - 1] if st - 1 > 0 else 0) if end < n else a[st - 1] if cnt == 0: return 0 if cnt < k and mx > aend: return -1 if x <= k * y: return cnt // k * x + cnt % k * y elif mx < aend: return cnt * y else: return x + (cnt - k) * y prev = 0 j = 0 mx = -1 ans, flag = 0, True for i in range(n): if j < m and a[i] == b[j]: ret = delSeg(prev, i, max(a[prev:i]) if prev != i else 0) if ret == -1: flag = False break else: ans += ret j += 1 prev = i + 1 elif j >= m: mx = max(a[prev:n]) ret = delSeg(prev, n, mx) if ret == -1: flag = False break else: ans += ret break i += 1 if flag and j >= m: print(ans) else: print(-1)
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER IF VAR BIN_OP VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR RETURN BIN_OP VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
def solve(A, B): if len(A) < len(B): return -1 elif len(A) == len(B): if A == B: return 0 return -1 void = [] B1 = B[::-1] u = B1.pop() cur = [] flank = [] for j, i in enumerate(A): if i == u: flank.append(u) void += [(cur, flank)] cur = [] flank = [u] try: u = B1.pop() except: pass continue cur.append(i) void.append((cur, flank)) ans = 0 for btw, f in void: n = len(btw) if not btw: continue if max(f) > max(btw): ans += n // k * min(k * bers, fb) + bers * (n % k) else: if len(btw) < k: return -1 ans += fb + (n - k) // k * min(k * bers, fb) + bers * ((n - k) % k) return ans a, b = map(int, input().split()) fb, k, bers = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) print(solve(A, B))
FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR LIST VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
import sys input = sys.stdin.readline n, m = map(int, input().split()) x, k, y = map(int, input().split()) a = [int(i) for i in input().split() if i != "\n"] b = [int(i) for i in input().split() if i != "\n"] count, ind, maxa = 0, 0, 0 store, prefmax = [], [] for i in range(len(a)): try: if a[i] == b[ind]: count += 1 store.append([a[i], i]) prefmax.append(max(maxa, a[i])) maxa = a[i] ind += 1 else: maxa = max(maxa, a[i]) prefmax.append(maxa) except: maxa = max(maxa, a[i]) prefmax.append(maxa) pass if b[-1] != a[-1]: store.append([a[-1] - 1, n]) prefmax.append(maxa) ok = 0 if b[0] != a[0]: store.insert(0, [a[0], -1]) prefmax.insert(0, a[0]) ok = 1 if count != m: print(-1) exit() else: ans = 0 optimum = 0 if x > y * k: optimum = 1 for i in range(1, len(store)): diff = store[i][1] - store[i - 1][1] - 1 if prefmax[max(0, store[i][1]) + ok] > max(store[i][0], store[i - 1][0]): if diff < k and diff > 0: print(-1) exit() elif optimum: ans += (diff - k) * y + x else: ans += diff // k * x + diff % k * y elif optimum: ans += diff * y else: ans += diff // k * x + diff % k * y print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR STRING ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER LIST VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP FUNC_CALL VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
from sys import stdin input = stdin.buffer.readline n, m = map(int, input().split()) x, k, y = map(int, input().split()) (*a,) = map(int, input().split()) a = a + [0] (*b,) = map(int, input().split()) ans = p = i = j = 0 while i < n and j < m: while a[i] != b[j]: i += 1 if i == n: break cnt = 0 for t in range(p, i): if a[t] > max(a[i], a[p - 1]): cnt += 1 if k * y < x: if i - p < k and cnt: exit(print(-1)) if cnt: ans += x + (i - p - k) * y else: ans += (i - p) * y else: if i - p < k and cnt: exit(print(-1)) ans += (i - p) // k * x + (i - p) % k * y i += 1 p = i j += 1 if j < m: print(-1) else: if i < n: cnt = 0 for t in range(p, n): if a[t] > max(a[n], a[p - 1]): cnt += 1 if k * y < x: if n - p < k and cnt: exit(print(-1)) if cnt: ans += x + (n - p - k) * y else: ans += (n - p) * y else: if n - p < k and cnt: exit(print(-1)) ans += (n - p) // k * x + (n - p) % k * y print(ans)
ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER IF VAR VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER IF VAR VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
def price(i, j, m): if j < i: return 0 c = [] num = j - i + 1 for p in range(i, j + 1): if a[p] > m: c = [1] break if c == []: if num < k: return num * y else: u = num // k v = num % k if k * y <= x: return num * y else: return u * x + v * y elif num < k: return -1 else: u = num // k v = num % k if k * y <= x: return (num - k) * y + x else: return u * x + v * y n, m = map(int, input().split()) x, k, y = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) d = dict() for j in range(n): d[a[j]] = j f = 0 for j in range(m - 1): if d[b[j + 1]] < d[b[j]]: f = -1 break if f == -1: print(-1) else: p = -1 j1 = 0 ans = 0 f = 0 while j1 < m: if j1 == 0: res = price(p + 1, d[b[j1]] - 1, b[j1]) else: res = price(p + 1, d[b[j1]] - 1, max(b[j1 - 1], b[j1])) if res == -1: f = -1 break ans += res p = d[b[j1]] j1 += 1 res = price(d[b[-1]] + 1, n - 1, b[-1]) if res == -1: f = -1 else: ans += res if f == -1: print(-1) else: print(ans)
FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR LIST NUMBER IF VAR LIST IF VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR RETURN BIN_OP VAR VAR RETURN BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct. You have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$. You want to turn the current sequence of warriors powers $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$. Calculate the minimum amount of mana you need to spend on it. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of sequence $a$ and the length of sequence $b$ respectively. The second line contains three integers $x, k, y$ ($1 \le x, y, \le 10^9; 1 \le k \le n$) — the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that all integers $a_i$ are pairwise distinct. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le n$). It is guaranteed that all integers $b_i$ are pairwise distinct. -----Output----- Print the minimum amount of mana for turning the sequnce $a_1, a_2, \dots, a_n$ into $b_1, b_2, \dots, b_m$, or $-1$ if it is impossible. -----Examples----- Input 5 2 5 2 3 3 1 4 5 2 3 5 Output 8 Input 4 4 5 1 4 4 3 1 2 2 4 3 1 Output -1 Input 4 4 2 1 11 1 3 2 4 1 3 2 4 Output 0
def remove(alive_powers, warriors, x, k, y): if not warriors: return 0 strongest = max(warriors) if len(warriors) < k: if strongest > max(alive_powers): return -1 return len(warriors) * y if k * y < x: if strongest < max(alive_powers): return len(warriors) * y else: return x + y * (len(warriors) - k) return x * (len(warriors) // k) + y * (len(warriors) % k) def mana_counter(n, m, x, k, y, a, b): mana = 0 indexes = [] cur_warrior_ind = 0 for survivor in b: while a[cur_warrior_ind] != survivor: cur_warrior_ind += 1 if cur_warrior_ind >= len(a): return -1 indexes.append(cur_warrior_ind) alive_powers = [-1, b[0]] res = remove(alive_powers, a[: indexes[0]], x, k, y) if res < 0: return -1 mana += res for i in range(1, len(indexes)): alive_powers[0], alive_powers[1] = alive_powers[1], b[i] res = remove(alive_powers, a[indexes[i - 1] + 1 : indexes[i]], x, k, y) if res < 0: return -1 mana += res res = remove([b[-1], -1], a[indexes[-1] + 1 :], x, k, y) if res < 0: return -1 mana += res return mana n, m = map(int, input().split()) x, k, y = map(int, input().split()) a = [int(i) for i in input().split()] b = [int(i) for i in input().split()] print(mana_counter(n, m, x, k, y, a, b))
FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR IF VAR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR IF VAR NUMBER RETURN NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR IF VAR NUMBER RETURN NUMBER VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR
You are given a huge integer $a$ consisting of $n$ digits ($n$ is between $1$ and $3 \cdot 10^5$, inclusive). It may contain leading zeros. You can swap two digits on adjacent (neighboring) positions if the swapping digits are of different parity (that is, they have different remainders when divided by $2$). For example, if $a = 032867235$ you can get the following integers in a single operation: $302867235$ if you swap the first and the second digits; $023867235$ if you swap the second and the third digits; $032876235$ if you swap the fifth and the sixth digits; $032862735$ if you swap the sixth and the seventh digits; $032867325$ if you swap the seventh and the eighth digits. Note, that you can't swap digits on positions $2$ and $4$ because the positions are not adjacent. Also, you can't swap digits on positions $3$ and $4$ because the digits have the same parity. You can perform any number (possibly, zero) of such operations. Find the minimum integer you can obtain. Note that the resulting integer also may contain leading zeros. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. The only line of each test case contains the integer $a$, its length $n$ is between $1$ and $3 \cdot 10^5$, inclusive. It is guaranteed that the sum of all values $n$ does not exceed $3 \cdot 10^5$. -----Output----- For each test case print line — the minimum integer you can obtain. -----Example----- Input 3 0709 1337 246432 Output 0079 1337 234642 -----Note----- In the first test case, you can perform the following sequence of operations (the pair of swapped digits is highlighted): $0 \underline{\textbf{70}} 9 \rightarrow 0079$. In the second test case, the initial integer is optimal. In the third test case you can perform the following sequence of operations: $246 \underline{\textbf{43}} 2 \rightarrow 24 \underline{\textbf{63}}42 \rightarrow 2 \underline{\textbf{43}} 642 \rightarrow 234642$.
t = int(input()) for _ in range(t): n = input() even = [] odd = [] for i in n: if int(i) % 2 == 0: even.append(int(i)) else: odd.append(int(i)) i = j = 0 e = len(even) o = len(odd) ans = "" while i < e and j < o: if even[i] <= odd[j]: ans = ans + str(even[i]) i += 1 else: ans = ans + str(odd[j]) j += 1 if i == e and j != o: while j < o: ans = ans + str(odd[j]) j += 1 elif j == o and i != e: while i < e: ans = ans + str(even[i]) i += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a huge integer $a$ consisting of $n$ digits ($n$ is between $1$ and $3 \cdot 10^5$, inclusive). It may contain leading zeros. You can swap two digits on adjacent (neighboring) positions if the swapping digits are of different parity (that is, they have different remainders when divided by $2$). For example, if $a = 032867235$ you can get the following integers in a single operation: $302867235$ if you swap the first and the second digits; $023867235$ if you swap the second and the third digits; $032876235$ if you swap the fifth and the sixth digits; $032862735$ if you swap the sixth and the seventh digits; $032867325$ if you swap the seventh and the eighth digits. Note, that you can't swap digits on positions $2$ and $4$ because the positions are not adjacent. Also, you can't swap digits on positions $3$ and $4$ because the digits have the same parity. You can perform any number (possibly, zero) of such operations. Find the minimum integer you can obtain. Note that the resulting integer also may contain leading zeros. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. The only line of each test case contains the integer $a$, its length $n$ is between $1$ and $3 \cdot 10^5$, inclusive. It is guaranteed that the sum of all values $n$ does not exceed $3 \cdot 10^5$. -----Output----- For each test case print line — the minimum integer you can obtain. -----Example----- Input 3 0709 1337 246432 Output 0079 1337 234642 -----Note----- In the first test case, you can perform the following sequence of operations (the pair of swapped digits is highlighted): $0 \underline{\textbf{70}} 9 \rightarrow 0079$. In the second test case, the initial integer is optimal. In the third test case you can perform the following sequence of operations: $246 \underline{\textbf{43}} 2 \rightarrow 24 \underline{\textbf{63}}42 \rightarrow 2 \underline{\textbf{43}} 642 \rightarrow 234642$.
t = int(input()) for i in range(t): s = input() sl = list(s) so = [] se = [] for j in range(len(sl)): if int(sl[j]) % 2 == 1: so.append(sl[j]) else: se.append(sl[j]) ans = "" el = 0 ol = 0 leno = len(so) lene = len(se) while ol != leno or el != lene: if ol == leno: ans += se[el] el += 1 elif el == lene: ans += so[ol] ol += 1 elif se[el] <= so[ol]: ans += se[el] el += 1 else: ans += so[ol] ol += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a huge integer $a$ consisting of $n$ digits ($n$ is between $1$ and $3 \cdot 10^5$, inclusive). It may contain leading zeros. You can swap two digits on adjacent (neighboring) positions if the swapping digits are of different parity (that is, they have different remainders when divided by $2$). For example, if $a = 032867235$ you can get the following integers in a single operation: $302867235$ if you swap the first and the second digits; $023867235$ if you swap the second and the third digits; $032876235$ if you swap the fifth and the sixth digits; $032862735$ if you swap the sixth and the seventh digits; $032867325$ if you swap the seventh and the eighth digits. Note, that you can't swap digits on positions $2$ and $4$ because the positions are not adjacent. Also, you can't swap digits on positions $3$ and $4$ because the digits have the same parity. You can perform any number (possibly, zero) of such operations. Find the minimum integer you can obtain. Note that the resulting integer also may contain leading zeros. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. The only line of each test case contains the integer $a$, its length $n$ is between $1$ and $3 \cdot 10^5$, inclusive. It is guaranteed that the sum of all values $n$ does not exceed $3 \cdot 10^5$. -----Output----- For each test case print line — the minimum integer you can obtain. -----Example----- Input 3 0709 1337 246432 Output 0079 1337 234642 -----Note----- In the first test case, you can perform the following sequence of operations (the pair of swapped digits is highlighted): $0 \underline{\textbf{70}} 9 \rightarrow 0079$. In the second test case, the initial integer is optimal. In the third test case you can perform the following sequence of operations: $246 \underline{\textbf{43}} 2 \rightarrow 24 \underline{\textbf{63}}42 \rightarrow 2 \underline{\textbf{43}} 642 \rightarrow 234642$.
n = int(input()) while n > 0: n -= 1 a = input() s1 = "" s2 = "" for i in a: if int(i) & 1 == 0: s1 += i else: s2 += i i = 0 j = 0 n1 = len(s1) n2 = len(s2) ans = "" while i < n1 and j < n2: if int(s1[i]) < int(s2[j]): ans += s1[i] i += 1 else: ans += s2[j] j += 1 if i < n1: ans += s1[i:] i += 1 if j < n2: ans += s2[j:] j += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING WHILE VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR