description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
for _ in range(int(input())): n = int(input()) s = input() s1 = input() if s == s1: print("0") continue ans = [] s2 = s + "0" i = 0 while s2[i] == "1": i += 1 if i != 0: ans.append(i) for j in range(i + 1, n + 1): if s2[j] == "1" and s2[j - 1] == "0": tmp = j elif s2[j] == "0" and s2[j - 1] == "1": ans.append(tmp) ans.append(j) s3 = "0" + s1 if s1.count("0") == n: print(len(ans), *ans) continue tmp = n for j in range(n, -1, -1): if s3[j] == "0" and s3[j - 1] == "1": tmp = j - 1 elif s3[j] == "1" and s3[j - 1] == "0": ans.append(tmp) if j != 1: ans.append(j - 1) print(len(ans), *ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR NUMBER WHILE VAR VAR STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP STRING VAR IF FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
for _ in range(int(input())): length = int(input()) a = list(input()) b = list(input()) count = length - 1 c_list = [] invert = 0 reverse = 1 le = 0 ri = length - 1 while count != -1: if reverse: if int(a[ri]) != (invert + int(b[count])) % 2: if a[ri] == a[le]: c_list.append(count + 1) else: c_list.append(1) c_list.append(count + 1) reverse = 1 - reverse invert = 1 - invert le += 1 else: ri -= 1 elif int(a[le]) != (invert + int(b[count])) % 2: if a[ri] == a[le]: c_list.append(count + 1) else: c_list.append(1) c_list.append(count + 1) reverse = 1 - reverse invert = 1 - invert ri -= 1 else: le += 1 count -= 1 print(len(c_list), *c_list)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR IF FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
for _ in range(int(input())): n = int(input()) a = input() b = input() ans = [] inv, rev = 0, 0 cnt = 0 for i in range(n - 1, -1, -1): c = cnt // 2 if cnt % 2 == 1: rev = n - c - 1 else: rev = c front = a[rev] if inv % 2 == 1: if front == "0": front = "1" else: front = "0" if front == b[i]: ans.append(1) inv += 1 ans.append(i + 1) cnt += 1 print(len(ans), *ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
for _ in range(int(input())): n = int(input()) a = list(map(int, list(input()))) b = list(map(int, list(input()))) ans = [] flg = a[0] for i in range(n - 1): if flg != a[i + 1]: flg ^= 1 ans.append(i + 1) for i in range(n - 1, -1, -1): if flg != b[i]: flg ^= 1 ans.append(i + 1) print(len(ans), end=" ") for i in ans: print(i, end=" ") print("")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
TC = int(input()) for tc in range(TC): N = int(input()) S = input() T = input() i = N - 1 j = 0 result = [] rev = False while j <= i: if rev: while j < i and S[j] != T[i - j]: j += 1 if j > i: break elif i > j: if S[i] != T[i - j]: result.append(1) result.append(i - j + 1) rev = not rev i -= 1 else: if S[j] == T[0]: result.append(1) i -= 1 else: while i >= 0 and S[i] == T[i - j]: i -= 1 if j > i: break elif i > j: if S[j] == T[i - j]: result.append(1) result.append(i - j + 1) rev = not rev j += 1 else: if S[j] != T[0]: result.append(1) j += 1 print(len(result), end=" ") for r in result: print(r, end=" ") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR IF VAR WHILE VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR IF VAR VAR IF VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR IF VAR VAR IF VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
for _ in range(int(input())): n = int(input()) ar = list(map(int, list(input()))) br = list(map(int, list(input()))) ar.append(0) br.append(0) ans = 0 x1 = [] x2 = [] for i in range(1, n + 1): if ar[i] != ar[i - 1]: x1.append(i) ans += 1 if br[i] != br[i - 1]: x2.append(i) ans += 1 print(ans, *(x1 + x2[::-1]))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
from sys import stdin, stdout def main(): t = int(stdin.readline()) for case in range(t): n = int(stdin.readline()) a = stdin.readline() b = stdin.readline() k = 0 seq = [] flips = 0 checks = n ax, ay, d = 0, n - 1, 1 by = n - 1 while checks > 0: ch = a[ax] if flips % 2 == 1: if a[ax] == "1": ch = "0" else: ch = "1" if ch == b[by]: k += 2 seq.append(1) else: k += 1 ax += d d *= -1 ax, ay = ay, ax seq.append(checks) flips += 1 checks -= 1 by -= 1 stdout.write(str(k)) stdout.write(" ") stdout.write(" ".join(map(str, seq))) stdout.write("\n") return main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
for vishal in range(int(input())): n = int(input()) a = input() b = input() ans = [] avals = [] i, j, k = 0, n - 1, 0 while k < n: if k % 2 == 0: avals.append(a[i]) i += 1 else: avals.append(str(int(not int(a[j])))) j -= 1 k += 1 k = 0 for i in range(len(b) - 1, -1, -1): if avals[k] != b[i]: ans.append(i) else: ans.append(0) ans.append(i) k += 1 print(len(ans), end=" ") for i in ans: print(i + 1, end=" ") print(end="\n")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR STRING
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
t = int(input()) for i in range(t): n = int(input()) x = input() y = input() a = [] b = [] for j in x: a.append(j) for j in y: b.append(j) p = [] j = 0 while j < n: if a[j] != b[j]: if j == n - 1: if j != 0: p.append(j + 1) p.append(1) p.append(j + 1) else: p.append(1) j += 1 elif a[j + 1] == b[j + 1]: if j != 0: p.append(j + 1) p.append(1) p.append(j + 1) else: p.append(1) j += 2 elif a[j + 1] != b[j + 1]: if a[j] == a[j + 1]: p.append(j + 2) p.append(2) p.append(j + 2) j += 2 elif j == n - 2: if j != 0: p.append(j + 2) p.append(1) p.append(2) p.append(1) p.append(j + 2) else: p.append(1) p.append(2) p.append(1) j += 2 elif a[j + 2] == b[j + 2]: p.append(j + 2) p.append(1) p.append(2) p.append(1) p.append(j + 2) j += 3 elif a[j] == a[j + 2]: p.append(j + 3) p.append(3) p.append(j + 3) j += 3 elif a[j + 1] == a[j + 2]: if j != 0: p.append(j + 1) p.append(1) p.append(j + 1) else: p.append(1) p.append(j + 3) p.append(2) p.append(j + 3) j += 3 else: j += 1 print(len(p), end=" ") print(*p)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
t = int(input()) for _ in range(t): n = int(input()) arr = list(input().rstrip("\n")) arr1 = list(input().rstrip("\n")) arr = [int(x) for x in arr] arr1 = [int(x) for x in arr1] result = [] if arr == arr1: print(0) else: if n == 1: print("1 1") continue prev = arr[0] for i in range(1, n): if prev != arr[i]: result.append(i) prev = arr[i] if arr[-1] == 1: last = 1 else: last = 0 for i in range(n - 1, -1, -1): if arr1[i] != last: result.append(i + 1) if last == 0: last = 1 else: last = 0 print(len(result), end=" ") print(*result)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR
One day, Chef prepared D brand new dishes. He named the i-th dish by a string Si. After the cooking, he decided to categorize each of these D dishes as special or not. A dish Si is called special if it's name (i.e. the string Si) can be represented in the form of a double string by removing at most one (possibly zero) character from it's name from any position. A string is called a double string if it can be represented as a concatenation of two identical, non-empty strings. e.g. "abab" is a double string as it can be represented as "ab" + "ab" where + operation denotes concatenation. Similarly, "aa", "abcabc" are double strings whereas "a", "abba", "abc" are not. -----Input----- - First line of the input contains an integer D denoting the number of dishes prepared by Chef on that day. - Each of the next D lines will contain description of a dish. - The i-th line contains the name of i-th dish Si. -----Output----- For each of the D dishes, print a single line containing "YES" or "NO" (without quotes) denoting whether the dish can be called as a special or not. -----Constraints----- - 1 ≀ D ≀ 106 - 1 ≀ |Si| ≀ 106. - Each character of string Si will be lower case English alphabet (i.e. from 'a' to 'z'). -----Subtasks----- Subtask #1 : (20 points) - Sum of |Si| in an input file doesn't exceed 2 * 103 Subtask 2 : (80 points) - Sum of |Si| in an input file doesn't exceed 2 * 106 -----Example----- Input: 3 aba abac abcd Output: YES NO NO -----Explanation----- Example case 1. We can remove the character at position 1 (0-based index) to get "aa" which is a double string. Hence, it is a special dish. Example case 2. It is not possible to remove the character at any of the position to get the double string. Hence, it is not a special dish.
t = int(input()) while t: s = input() l = len(s) if l == 1: print("NO") t -= 1 continue if l % 2 == 0: if s[: len(s) // 2] == s[len(s) // 2 :]: print("YES") else: print("NO") else: i = 0 j = l // 2 + 1 count = 0 while i <= l // 2 and j < l: if s[i] == s[j]: i += 1 j += 1 continue else: i += 1 count += 1 if i > l // 2 and j >= l and count < 2: print("YES") t -= 1 continue i = 0 j = l // 2 + 1 flag = True while i < l // 2 and j < l: if s[i] == s[j]: i += 1 j += 1 continue else: flag = False break if i == l // 2 and j == l and flag == True: print("YES") t -= 1 continue i = 0 j = l // 2 count = 0 while i < l // 2 and j < l: if s[i] == s[j]: i += 1 j += 1 continue else: j += 1 count += 1 if i >= l // 2 and j >= l - 1 and count < 2: print("YES") t -= 1 continue print("NO") t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING VAR NUMBER EXPR FUNC_CALL VAR STRING VAR NUMBER
One day, Chef prepared D brand new dishes. He named the i-th dish by a string Si. After the cooking, he decided to categorize each of these D dishes as special or not. A dish Si is called special if it's name (i.e. the string Si) can be represented in the form of a double string by removing at most one (possibly zero) character from it's name from any position. A string is called a double string if it can be represented as a concatenation of two identical, non-empty strings. e.g. "abab" is a double string as it can be represented as "ab" + "ab" where + operation denotes concatenation. Similarly, "aa", "abcabc" are double strings whereas "a", "abba", "abc" are not. -----Input----- - First line of the input contains an integer D denoting the number of dishes prepared by Chef on that day. - Each of the next D lines will contain description of a dish. - The i-th line contains the name of i-th dish Si. -----Output----- For each of the D dishes, print a single line containing "YES" or "NO" (without quotes) denoting whether the dish can be called as a special or not. -----Constraints----- - 1 ≀ D ≀ 106 - 1 ≀ |Si| ≀ 106. - Each character of string Si will be lower case English alphabet (i.e. from 'a' to 'z'). -----Subtasks----- Subtask #1 : (20 points) - Sum of |Si| in an input file doesn't exceed 2 * 103 Subtask 2 : (80 points) - Sum of |Si| in an input file doesn't exceed 2 * 106 -----Example----- Input: 3 aba abac abcd Output: YES NO NO -----Explanation----- Example case 1. We can remove the character at position 1 (0-based index) to get "aa" which is a double string. Hence, it is a special dish. Example case 2. It is not possible to remove the character at any of the position to get the double string. Hence, it is not a special dish.
def check(a, b): p = 0 a = list(a) b = list(b) for i in a: if b[p] == i: p += 1 if p == len(b): break if p == len(b): return True return False for t in range(int(input())): s = str(input()) l = len(s) if l == 1: print("NO") elif l % 2 != 0: s1a = s[0 : l // 2] s1b = s[l // 2 : l] s2a = s[0 : l // 2 + 1] s2b = s[l // 2 + 1 : l] if check(s1b, s1a) or check(s2a, s2b): print("YES") else: print("NO") else: s1 = s[0 : l // 2] s2 = s[l // 2 : l] if s1 == s2: print("YES") else: print("NO")
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
One day, Chef prepared D brand new dishes. He named the i-th dish by a string Si. After the cooking, he decided to categorize each of these D dishes as special or not. A dish Si is called special if it's name (i.e. the string Si) can be represented in the form of a double string by removing at most one (possibly zero) character from it's name from any position. A string is called a double string if it can be represented as a concatenation of two identical, non-empty strings. e.g. "abab" is a double string as it can be represented as "ab" + "ab" where + operation denotes concatenation. Similarly, "aa", "abcabc" are double strings whereas "a", "abba", "abc" are not. -----Input----- - First line of the input contains an integer D denoting the number of dishes prepared by Chef on that day. - Each of the next D lines will contain description of a dish. - The i-th line contains the name of i-th dish Si. -----Output----- For each of the D dishes, print a single line containing "YES" or "NO" (without quotes) denoting whether the dish can be called as a special or not. -----Constraints----- - 1 ≀ D ≀ 106 - 1 ≀ |Si| ≀ 106. - Each character of string Si will be lower case English alphabet (i.e. from 'a' to 'z'). -----Subtasks----- Subtask #1 : (20 points) - Sum of |Si| in an input file doesn't exceed 2 * 103 Subtask 2 : (80 points) - Sum of |Si| in an input file doesn't exceed 2 * 106 -----Example----- Input: 3 aba abac abcd Output: YES NO NO -----Explanation----- Example case 1. We can remove the character at position 1 (0-based index) to get "aa" which is a double string. Hence, it is a special dish. Example case 2. It is not possible to remove the character at any of the position to get the double string. Hence, it is not a special dish.
def is_subsequence(a, lstart, lend, rstart, rend): mismatch = 0 i = lstart j = rstart while i <= lend: if a[i] != a[j]: mismatch += 1 j += 1 else: i += 1 j += 1 if mismatch > 1: return False return True for t in range(int(input())): a = input() n = len(a) if len(a) == 1: print("NO") elif len(a) % 2 == 0: if a[0 : n // 2] == a[n // 2 : n]: print("YES") else: print("NO") elif len(a) % 2 != 0: if is_subsequence(a, 0, n // 2 - 1, n // 2, n - 1) + is_subsequence( a, n // 2 + 1, n - 1, 0, n // 2 ): print("YES") else: print("NO")
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
One day, Chef prepared D brand new dishes. He named the i-th dish by a string Si. After the cooking, he decided to categorize each of these D dishes as special or not. A dish Si is called special if it's name (i.e. the string Si) can be represented in the form of a double string by removing at most one (possibly zero) character from it's name from any position. A string is called a double string if it can be represented as a concatenation of two identical, non-empty strings. e.g. "abab" is a double string as it can be represented as "ab" + "ab" where + operation denotes concatenation. Similarly, "aa", "abcabc" are double strings whereas "a", "abba", "abc" are not. -----Input----- - First line of the input contains an integer D denoting the number of dishes prepared by Chef on that day. - Each of the next D lines will contain description of a dish. - The i-th line contains the name of i-th dish Si. -----Output----- For each of the D dishes, print a single line containing "YES" or "NO" (without quotes) denoting whether the dish can be called as a special or not. -----Constraints----- - 1 ≀ D ≀ 106 - 1 ≀ |Si| ≀ 106. - Each character of string Si will be lower case English alphabet (i.e. from 'a' to 'z'). -----Subtasks----- Subtask #1 : (20 points) - Sum of |Si| in an input file doesn't exceed 2 * 103 Subtask 2 : (80 points) - Sum of |Si| in an input file doesn't exceed 2 * 106 -----Example----- Input: 3 aba abac abcd Output: YES NO NO -----Explanation----- Example case 1. We can remove the character at position 1 (0-based index) to get "aa" which is a double string. Hence, it is a special dish. Example case 2. It is not possible to remove the character at any of the position to get the double string. Hence, it is not a special dish.
for case in range(int(input())): s = input() n = len(s) // 2 if len(s) == 1: print("NO") elif len(s) % 2 == 0: print("YES" if s[0:n] == s[n:] else "NO") else: match = True skip = 0 for i in range(n): if s[i + skip] != s[i + n + 1]: if skip == 1 or s[i + 1] != s[i + n + 1]: match = False break skip = 1 if match: print("YES") continue match = True skip = 0 for i in range(n): if s[n - 1 - i] != s[2 * n - i - skip]: if skip == 1 or s[n - 1 - i] != s[2 * n - i - 1]: match = False break skip = 1 if match: print("YES") else: print("NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR VAR STRING STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR IF VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
One day, Chef prepared D brand new dishes. He named the i-th dish by a string Si. After the cooking, he decided to categorize each of these D dishes as special or not. A dish Si is called special if it's name (i.e. the string Si) can be represented in the form of a double string by removing at most one (possibly zero) character from it's name from any position. A string is called a double string if it can be represented as a concatenation of two identical, non-empty strings. e.g. "abab" is a double string as it can be represented as "ab" + "ab" where + operation denotes concatenation. Similarly, "aa", "abcabc" are double strings whereas "a", "abba", "abc" are not. -----Input----- - First line of the input contains an integer D denoting the number of dishes prepared by Chef on that day. - Each of the next D lines will contain description of a dish. - The i-th line contains the name of i-th dish Si. -----Output----- For each of the D dishes, print a single line containing "YES" or "NO" (without quotes) denoting whether the dish can be called as a special or not. -----Constraints----- - 1 ≀ D ≀ 106 - 1 ≀ |Si| ≀ 106. - Each character of string Si will be lower case English alphabet (i.e. from 'a' to 'z'). -----Subtasks----- Subtask #1 : (20 points) - Sum of |Si| in an input file doesn't exceed 2 * 103 Subtask 2 : (80 points) - Sum of |Si| in an input file doesn't exceed 2 * 106 -----Example----- Input: 3 aba abac abcd Output: YES NO NO -----Explanation----- Example case 1. We can remove the character at position 1 (0-based index) to get "aa" which is a double string. Hence, it is a special dish. Example case 2. It is not possible to remove the character at any of the position to get the double string. Hence, it is not a special dish.
t = int(input()) for _ in range(t): s = input() l = len(s) lh = int(l / 2) if l % 2 == 0: if s[:lh] == s[lh:]: print("YES") else: print("NO") elif l == 1: print("NO") else: left = s[:lh] right = s[lh:] k = 0 i = 0 j = 0 flag1 = True while i < len(left): if j < len(right) and left[i] == right[j]: i += 1 j += 1 elif j < len(right) - 1 and left[i] == right[j + 1] and k < 2: i += 1 j += 2 k += 1 else: flag1 = False break left = s[: lh + 1] right = s[lh + 1 :] k = 0 i = 0 j = 0 flag2 = True while j < len(right): if i < len(left) and left[i] == right[j] and k < 2: i += 1 j += 1 elif i < len(left) - 1 and left[i + 1] == right[j] and k < 2: i += 2 j += 1 k += 1 else: flag2 = False break if flag1 or flag2: print("YES") else: print("NO")
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 FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
One day, Chef prepared D brand new dishes. He named the i-th dish by a string Si. After the cooking, he decided to categorize each of these D dishes as special or not. A dish Si is called special if it's name (i.e. the string Si) can be represented in the form of a double string by removing at most one (possibly zero) character from it's name from any position. A string is called a double string if it can be represented as a concatenation of two identical, non-empty strings. e.g. "abab" is a double string as it can be represented as "ab" + "ab" where + operation denotes concatenation. Similarly, "aa", "abcabc" are double strings whereas "a", "abba", "abc" are not. -----Input----- - First line of the input contains an integer D denoting the number of dishes prepared by Chef on that day. - Each of the next D lines will contain description of a dish. - The i-th line contains the name of i-th dish Si. -----Output----- For each of the D dishes, print a single line containing "YES" or "NO" (without quotes) denoting whether the dish can be called as a special or not. -----Constraints----- - 1 ≀ D ≀ 106 - 1 ≀ |Si| ≀ 106. - Each character of string Si will be lower case English alphabet (i.e. from 'a' to 'z'). -----Subtasks----- Subtask #1 : (20 points) - Sum of |Si| in an input file doesn't exceed 2 * 103 Subtask 2 : (80 points) - Sum of |Si| in an input file doesn't exceed 2 * 106 -----Example----- Input: 3 aba abac abcd Output: YES NO NO -----Explanation----- Example case 1. We can remove the character at position 1 (0-based index) to get "aa" which is a double string. Hence, it is a special dish. Example case 2. It is not possible to remove the character at any of the position to get the double string. Hence, it is not a special dish.
case = int(input()) for i in range(case): st = input() x = len(st) if x == 1: print("NO") elif x % 2 == 0: if st[: x // 2] == st[x // 2 :]: print("YES") else: print("NO") else: top = 0 to = 0 l = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ] for j in range(x): l[ord(st[j]) - 97] += 1 for j in range(26): if l[j] % 2 != 0: to = j + 97 break for j in range(x): if to == ord(st[j]): if j == 0: s0 = st[j + 1 :] elif j == x - 1: s0 = st[:j] else: s0 = st[:j] + st[j + 1 :] if s0[: (x - 1) // 2] == s0[(x - 1) // 2 :]: print("YES") top = 1 break if top != 1: print("NO")
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 IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING
One day, Chef prepared D brand new dishes. He named the i-th dish by a string Si. After the cooking, he decided to categorize each of these D dishes as special or not. A dish Si is called special if it's name (i.e. the string Si) can be represented in the form of a double string by removing at most one (possibly zero) character from it's name from any position. A string is called a double string if it can be represented as a concatenation of two identical, non-empty strings. e.g. "abab" is a double string as it can be represented as "ab" + "ab" where + operation denotes concatenation. Similarly, "aa", "abcabc" are double strings whereas "a", "abba", "abc" are not. -----Input----- - First line of the input contains an integer D denoting the number of dishes prepared by Chef on that day. - Each of the next D lines will contain description of a dish. - The i-th line contains the name of i-th dish Si. -----Output----- For each of the D dishes, print a single line containing "YES" or "NO" (without quotes) denoting whether the dish can be called as a special or not. -----Constraints----- - 1 ≀ D ≀ 106 - 1 ≀ |Si| ≀ 106. - Each character of string Si will be lower case English alphabet (i.e. from 'a' to 'z'). -----Subtasks----- Subtask #1 : (20 points) - Sum of |Si| in an input file doesn't exceed 2 * 103 Subtask 2 : (80 points) - Sum of |Si| in an input file doesn't exceed 2 * 106 -----Example----- Input: 3 aba abac abcd Output: YES NO NO -----Explanation----- Example case 1. We can remove the character at position 1 (0-based index) to get "aa" which is a double string. Hence, it is a special dish. Example case 2. It is not possible to remove the character at any of the position to get the double string. Hence, it is not a special dish.
t = int(input()) for _ in range(t): s = input() if len(s) % 2 == 0: ok = True for i in range(len(s) // 2): if s[i] != s[i + len(s) // 2]: ok = False break if ok: print("YES") else: print("NO") elif len(s) == 1: print("NO") else: idx1 = 0 idx2 = len(s) // 2 ok = False while idx1 < len(s) // 2 and idx2 < len(s): if s[idx1] != s[idx2]: idx2 += 1 else: idx1 += 1 idx2 += 1 if idx1 == len(s) // 2: ok = ok or True idx1 = len(s) // 2 + 1 idx2 = 0 while idx1 < len(s) and idx2 <= len(s) // 2: if s[idx1] != s[idx2]: idx2 += 1 else: idx1 += 1 idx2 += 1 if idx1 == len(s): ok = ok or True if ok: print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
One day, Chef prepared D brand new dishes. He named the i-th dish by a string Si. After the cooking, he decided to categorize each of these D dishes as special or not. A dish Si is called special if it's name (i.e. the string Si) can be represented in the form of a double string by removing at most one (possibly zero) character from it's name from any position. A string is called a double string if it can be represented as a concatenation of two identical, non-empty strings. e.g. "abab" is a double string as it can be represented as "ab" + "ab" where + operation denotes concatenation. Similarly, "aa", "abcabc" are double strings whereas "a", "abba", "abc" are not. -----Input----- - First line of the input contains an integer D denoting the number of dishes prepared by Chef on that day. - Each of the next D lines will contain description of a dish. - The i-th line contains the name of i-th dish Si. -----Output----- For each of the D dishes, print a single line containing "YES" or "NO" (without quotes) denoting whether the dish can be called as a special or not. -----Constraints----- - 1 ≀ D ≀ 106 - 1 ≀ |Si| ≀ 106. - Each character of string Si will be lower case English alphabet (i.e. from 'a' to 'z'). -----Subtasks----- Subtask #1 : (20 points) - Sum of |Si| in an input file doesn't exceed 2 * 103 Subtask 2 : (80 points) - Sum of |Si| in an input file doesn't exceed 2 * 106 -----Example----- Input: 3 aba abac abcd Output: YES NO NO -----Explanation----- Example case 1. We can remove the character at position 1 (0-based index) to get "aa" which is a double string. Hence, it is a special dish. Example case 2. It is not possible to remove the character at any of the position to get the double string. Hence, it is not a special dish.
def is_subsequence(A, B): i = 0 j = 0 while i < len(A) and j < len(B): if B[j] == A[i]: i += 1 j += 1 return i == len(A) def is_dish_special(S): len_over_2 = len(S) // 2 if len(S) % 2 == 0: return is_subsequence(S[0:len_over_2], S[len_over_2:]) else: return is_subsequence(S[0:len_over_2], S[len_over_2:]) or is_subsequence( S[len_over_2 + 1 :], S[0 : len_over_2 + 1] ) D = int(input()) for d in range(D): S = input().strip() if len(S) == 1: print("NO") elif is_dish_special(S): print("YES") else: print("NO")
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
One day, Chef prepared D brand new dishes. He named the i-th dish by a string Si. After the cooking, he decided to categorize each of these D dishes as special or not. A dish Si is called special if it's name (i.e. the string Si) can be represented in the form of a double string by removing at most one (possibly zero) character from it's name from any position. A string is called a double string if it can be represented as a concatenation of two identical, non-empty strings. e.g. "abab" is a double string as it can be represented as "ab" + "ab" where + operation denotes concatenation. Similarly, "aa", "abcabc" are double strings whereas "a", "abba", "abc" are not. -----Input----- - First line of the input contains an integer D denoting the number of dishes prepared by Chef on that day. - Each of the next D lines will contain description of a dish. - The i-th line contains the name of i-th dish Si. -----Output----- For each of the D dishes, print a single line containing "YES" or "NO" (without quotes) denoting whether the dish can be called as a special or not. -----Constraints----- - 1 ≀ D ≀ 106 - 1 ≀ |Si| ≀ 106. - Each character of string Si will be lower case English alphabet (i.e. from 'a' to 'z'). -----Subtasks----- Subtask #1 : (20 points) - Sum of |Si| in an input file doesn't exceed 2 * 103 Subtask 2 : (80 points) - Sum of |Si| in an input file doesn't exceed 2 * 106 -----Example----- Input: 3 aba abac abcd Output: YES NO NO -----Explanation----- Example case 1. We can remove the character at position 1 (0-based index) to get "aa" which is a double string. Hence, it is a special dish. Example case 2. It is not possible to remove the character at any of the position to get the double string. Hence, it is not a special dish.
q = int(input()) for _ in range(0, q): s = input() if len(s) == 1: print("NO") elif len(s) % 2 == 0: t = len(s) // 2 n = len(s) if s[0:t] == s[t:n]: print("YES") else: print("NO") else: n = int(len(s) // 2) l = len(s) flag = False s1 = s[0:n] s2 = s[n:l] s3 = s[0 : n + 1] s4 = s[n + 1 : l] j = 0 count = 0 i = 0 while i < n: if not s1[i] == s2[j]: j = j + 1 count += 1 if count > 1: break else: j += 1 i += 1 if count > 1: break if count >= 2: j = 0 count = 0 i = 0 while j < n: if not s3[i] == s4[j]: i = i + 1 count += 1 if count > 1: break else: i = i + 1 j = j + 1 if count > 1: break if count >= 2: print("NO") else: print("YES")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
One day, Chef prepared D brand new dishes. He named the i-th dish by a string Si. After the cooking, he decided to categorize each of these D dishes as special or not. A dish Si is called special if it's name (i.e. the string Si) can be represented in the form of a double string by removing at most one (possibly zero) character from it's name from any position. A string is called a double string if it can be represented as a concatenation of two identical, non-empty strings. e.g. "abab" is a double string as it can be represented as "ab" + "ab" where + operation denotes concatenation. Similarly, "aa", "abcabc" are double strings whereas "a", "abba", "abc" are not. -----Input----- - First line of the input contains an integer D denoting the number of dishes prepared by Chef on that day. - Each of the next D lines will contain description of a dish. - The i-th line contains the name of i-th dish Si. -----Output----- For each of the D dishes, print a single line containing "YES" or "NO" (without quotes) denoting whether the dish can be called as a special or not. -----Constraints----- - 1 ≀ D ≀ 106 - 1 ≀ |Si| ≀ 106. - Each character of string Si will be lower case English alphabet (i.e. from 'a' to 'z'). -----Subtasks----- Subtask #1 : (20 points) - Sum of |Si| in an input file doesn't exceed 2 * 103 Subtask 2 : (80 points) - Sum of |Si| in an input file doesn't exceed 2 * 106 -----Example----- Input: 3 aba abac abcd Output: YES NO NO -----Explanation----- Example case 1. We can remove the character at position 1 (0-based index) to get "aa" which is a double string. Hence, it is a special dish. Example case 2. It is not possible to remove the character at any of the position to get the double string. Hence, it is not a special dish.
def r(s, b): n, m = len(s), 0 if n == 1: return False i, j, f = 0, n // 2 + b, False while m < n // 2: if s[i] != s[j]: if f: return False else: f = True if b: i += 1 else: j += 1 else: i += 1 j += 1 m += 1 return True def sp(s): n = len(s) if n % 2 == 0: return all(s[i] == s[n // 2 + i] for i in range(n // 2)) else: return r(s, True) or r(s, False) for d in range(int(input())): print("YES" if sp(input()) else "NO")
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR STRING STRING
One day, Chef prepared D brand new dishes. He named the i-th dish by a string Si. After the cooking, he decided to categorize each of these D dishes as special or not. A dish Si is called special if it's name (i.e. the string Si) can be represented in the form of a double string by removing at most one (possibly zero) character from it's name from any position. A string is called a double string if it can be represented as a concatenation of two identical, non-empty strings. e.g. "abab" is a double string as it can be represented as "ab" + "ab" where + operation denotes concatenation. Similarly, "aa", "abcabc" are double strings whereas "a", "abba", "abc" are not. -----Input----- - First line of the input contains an integer D denoting the number of dishes prepared by Chef on that day. - Each of the next D lines will contain description of a dish. - The i-th line contains the name of i-th dish Si. -----Output----- For each of the D dishes, print a single line containing "YES" or "NO" (without quotes) denoting whether the dish can be called as a special or not. -----Constraints----- - 1 ≀ D ≀ 106 - 1 ≀ |Si| ≀ 106. - Each character of string Si will be lower case English alphabet (i.e. from 'a' to 'z'). -----Subtasks----- Subtask #1 : (20 points) - Sum of |Si| in an input file doesn't exceed 2 * 103 Subtask 2 : (80 points) - Sum of |Si| in an input file doesn't exceed 2 * 106 -----Example----- Input: 3 aba abac abcd Output: YES NO NO -----Explanation----- Example case 1. We can remove the character at position 1 (0-based index) to get "aa" which is a double string. Hence, it is a special dish. Example case 2. It is not possible to remove the character at any of the position to get the double string. Hence, it is not a special dish.
for _ in range(int(input())): s = input().strip() l = len(s) c, cc = 0, 0 lbt = len(s) // 2 s1 = s[:lbt] s2 = s[lbt:] ans = "YES" if l == 1: ans = "NO" elif l % 2 == 0: if s[:lbt] == s[lbt:]: ans = "YES" else: ans = "NO" elif ans != "NO": i, j = 0, lbt + 1 c = 0 while i < lbt + 1 and j < l: if s[i] == s[j]: c += 1 i += 1 j += 1 else: i += 1 if i < lbt + 1 and j < l and s[i] == s[j]: c += 1 i += 1 j += 1 else: break if c != lbt: c, i, j = 0, 0, lbt while i < lbt and j < l: if s[i] == s[j]: c += 1 i += 1 j += 1 else: j += 1 if i < lbt and j < l and s[i] == s[j]: c += 1 i += 1 j += 1 else: break if c == lbt: ans = "YES" else: ans = "NO" print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR STRING IF VAR NUMBER ASSIGN VAR STRING IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING IF VAR STRING ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER VAR WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR
One day, Chef prepared D brand new dishes. He named the i-th dish by a string Si. After the cooking, he decided to categorize each of these D dishes as special or not. A dish Si is called special if it's name (i.e. the string Si) can be represented in the form of a double string by removing at most one (possibly zero) character from it's name from any position. A string is called a double string if it can be represented as a concatenation of two identical, non-empty strings. e.g. "abab" is a double string as it can be represented as "ab" + "ab" where + operation denotes concatenation. Similarly, "aa", "abcabc" are double strings whereas "a", "abba", "abc" are not. -----Input----- - First line of the input contains an integer D denoting the number of dishes prepared by Chef on that day. - Each of the next D lines will contain description of a dish. - The i-th line contains the name of i-th dish Si. -----Output----- For each of the D dishes, print a single line containing "YES" or "NO" (without quotes) denoting whether the dish can be called as a special or not. -----Constraints----- - 1 ≀ D ≀ 106 - 1 ≀ |Si| ≀ 106. - Each character of string Si will be lower case English alphabet (i.e. from 'a' to 'z'). -----Subtasks----- Subtask #1 : (20 points) - Sum of |Si| in an input file doesn't exceed 2 * 103 Subtask 2 : (80 points) - Sum of |Si| in an input file doesn't exceed 2 * 106 -----Example----- Input: 3 aba abac abcd Output: YES NO NO -----Explanation----- Example case 1. We can remove the character at position 1 (0-based index) to get "aa" which is a double string. Hence, it is a special dish. Example case 2. It is not possible to remove the character at any of the position to get the double string. Hence, it is not a special dish.
t = int(input()) for _ in range(t): s = input() l = len(s) if l == 1: print("NO") elif l % 2 == 0: if s[: l // 2] == s[l // 2 :]: print("YES") else: print("NO") else: flag = 0 dica = {} pele = 0 for i in range(l): if s[i] in dica.keys(): dica[s[i]].append(i) else: dica[s[i]] = [i] for k in dica.keys(): if len(dica[k]) % 2 != 0: flag += 1 pele = k if flag != 1: print("NO") else: flag = 0 for i in dica[pele]: ns = s[:i] + s[i + 1 :] if ns[: (l - 1) // 2] == ns[(l - 1) // 2 :]: print("YES") flag = 1 break if flag == 0: print("NO")
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 IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR FOR VAR FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING
One day, Chef prepared D brand new dishes. He named the i-th dish by a string Si. After the cooking, he decided to categorize each of these D dishes as special or not. A dish Si is called special if it's name (i.e. the string Si) can be represented in the form of a double string by removing at most one (possibly zero) character from it's name from any position. A string is called a double string if it can be represented as a concatenation of two identical, non-empty strings. e.g. "abab" is a double string as it can be represented as "ab" + "ab" where + operation denotes concatenation. Similarly, "aa", "abcabc" are double strings whereas "a", "abba", "abc" are not. -----Input----- - First line of the input contains an integer D denoting the number of dishes prepared by Chef on that day. - Each of the next D lines will contain description of a dish. - The i-th line contains the name of i-th dish Si. -----Output----- For each of the D dishes, print a single line containing "YES" or "NO" (without quotes) denoting whether the dish can be called as a special or not. -----Constraints----- - 1 ≀ D ≀ 106 - 1 ≀ |Si| ≀ 106. - Each character of string Si will be lower case English alphabet (i.e. from 'a' to 'z'). -----Subtasks----- Subtask #1 : (20 points) - Sum of |Si| in an input file doesn't exceed 2 * 103 Subtask 2 : (80 points) - Sum of |Si| in an input file doesn't exceed 2 * 106 -----Example----- Input: 3 aba abac abcd Output: YES NO NO -----Explanation----- Example case 1. We can remove the character at position 1 (0-based index) to get "aa" which is a double string. Hence, it is a special dish. Example case 2. It is not possible to remove the character at any of the position to get the double string. Hence, it is not a special dish.
def isdoub(s): if len(s) == 1: return "NO" if len(s) % 2 == 0: if s[0 : len(s) // 2] == s[len(s) // 2 :]: return "YES" return "NO" for _ in range(2): u = s[0 : len(s) // 2] v = s[len(s) // 2 :] for i in range(len(v)): if i >= len(u) or u[i] != v[i]: v = v[:i] + v[i + 1 :] break if u == v: return "YES" s = "".join(reversed(s)) return "NO" for _ in range(int(input())): print(isdoub(input()))
FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN STRING IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN STRING RETURN STRING FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR RETURN STRING ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR RETURN STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR
One day, Chef prepared D brand new dishes. He named the i-th dish by a string Si. After the cooking, he decided to categorize each of these D dishes as special or not. A dish Si is called special if it's name (i.e. the string Si) can be represented in the form of a double string by removing at most one (possibly zero) character from it's name from any position. A string is called a double string if it can be represented as a concatenation of two identical, non-empty strings. e.g. "abab" is a double string as it can be represented as "ab" + "ab" where + operation denotes concatenation. Similarly, "aa", "abcabc" are double strings whereas "a", "abba", "abc" are not. -----Input----- - First line of the input contains an integer D denoting the number of dishes prepared by Chef on that day. - Each of the next D lines will contain description of a dish. - The i-th line contains the name of i-th dish Si. -----Output----- For each of the D dishes, print a single line containing "YES" or "NO" (without quotes) denoting whether the dish can be called as a special or not. -----Constraints----- - 1 ≀ D ≀ 106 - 1 ≀ |Si| ≀ 106. - Each character of string Si will be lower case English alphabet (i.e. from 'a' to 'z'). -----Subtasks----- Subtask #1 : (20 points) - Sum of |Si| in an input file doesn't exceed 2 * 103 Subtask 2 : (80 points) - Sum of |Si| in an input file doesn't exceed 2 * 106 -----Example----- Input: 3 aba abac abcd Output: YES NO NO -----Explanation----- Example case 1. We can remove the character at position 1 (0-based index) to get "aa" which is a double string. Hence, it is a special dish. Example case 2. It is not possible to remove the character at any of the position to get the double string. Hence, it is not a special dish.
d = int(input()) ans = [] n = d while d > 0: flag = 0 s = input() l = len(s) l1 = int(l / 2) a = [] for i in range(26): a.append([]) if l % 2 == 1: count = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ] for i in range(l): count[ord(s[i]) - 97] = count[ord(s[i]) - 97] + 1 a[ord(s[i]) - 97].append(i) for i in range(26): if count[i] % 2 == 1: l2 = len(a[i]) for j in range(l2): pos = a[i][j] if l1 <= pos: str1 = s[0:l1] else: str1 = s[0:pos] + s[pos + 1 : l1 + 1] if l1 > pos: str2 = s[l1 + 1 :] else: str2 = s[l1:pos] + s[pos + 1 :] if str1 == str2 and str1 != "" and str2 != "": flag = 1 break else: str1 = s[0:l1] str2 = s[l1:] if str1 == str2: flag = 1 if flag == 1: ans.append("YES") else: ans.append("NO") d = d - 1 for i in range(n): print(ans[i])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR LIST IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR STRING VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
One day, Chef prepared D brand new dishes. He named the i-th dish by a string Si. After the cooking, he decided to categorize each of these D dishes as special or not. A dish Si is called special if it's name (i.e. the string Si) can be represented in the form of a double string by removing at most one (possibly zero) character from it's name from any position. A string is called a double string if it can be represented as a concatenation of two identical, non-empty strings. e.g. "abab" is a double string as it can be represented as "ab" + "ab" where + operation denotes concatenation. Similarly, "aa", "abcabc" are double strings whereas "a", "abba", "abc" are not. -----Input----- - First line of the input contains an integer D denoting the number of dishes prepared by Chef on that day. - Each of the next D lines will contain description of a dish. - The i-th line contains the name of i-th dish Si. -----Output----- For each of the D dishes, print a single line containing "YES" or "NO" (without quotes) denoting whether the dish can be called as a special or not. -----Constraints----- - 1 ≀ D ≀ 106 - 1 ≀ |Si| ≀ 106. - Each character of string Si will be lower case English alphabet (i.e. from 'a' to 'z'). -----Subtasks----- Subtask #1 : (20 points) - Sum of |Si| in an input file doesn't exceed 2 * 103 Subtask 2 : (80 points) - Sum of |Si| in an input file doesn't exceed 2 * 106 -----Example----- Input: 3 aba abac abcd Output: YES NO NO -----Explanation----- Example case 1. We can remove the character at position 1 (0-based index) to get "aa" which is a double string. Hence, it is a special dish. Example case 2. It is not possible to remove the character at any of the position to get the double string. Hence, it is not a special dish.
def is_subseq(x, y): i = 0 for c in x: while i < len(y) and y[i] != c: i += 1 if i == len(y): return False i += 1 return True t = int(input()) while t: t -= 1 a = input() n = len(a) if n > 1 and ( is_subseq(a[: n // 2], a[n // 2 :]) or is_subseq(a[(n + 1) // 2 :], a[: (n + 1) // 2]) ): print("YES") else: print("NO")
FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
One day, Chef prepared D brand new dishes. He named the i-th dish by a string Si. After the cooking, he decided to categorize each of these D dishes as special or not. A dish Si is called special if it's name (i.e. the string Si) can be represented in the form of a double string by removing at most one (possibly zero) character from it's name from any position. A string is called a double string if it can be represented as a concatenation of two identical, non-empty strings. e.g. "abab" is a double string as it can be represented as "ab" + "ab" where + operation denotes concatenation. Similarly, "aa", "abcabc" are double strings whereas "a", "abba", "abc" are not. -----Input----- - First line of the input contains an integer D denoting the number of dishes prepared by Chef on that day. - Each of the next D lines will contain description of a dish. - The i-th line contains the name of i-th dish Si. -----Output----- For each of the D dishes, print a single line containing "YES" or "NO" (without quotes) denoting whether the dish can be called as a special or not. -----Constraints----- - 1 ≀ D ≀ 106 - 1 ≀ |Si| ≀ 106. - Each character of string Si will be lower case English alphabet (i.e. from 'a' to 'z'). -----Subtasks----- Subtask #1 : (20 points) - Sum of |Si| in an input file doesn't exceed 2 * 103 Subtask 2 : (80 points) - Sum of |Si| in an input file doesn't exceed 2 * 106 -----Example----- Input: 3 aba abac abcd Output: YES NO NO -----Explanation----- Example case 1. We can remove the character at position 1 (0-based index) to get "aa" which is a double string. Hence, it is a special dish. Example case 2. It is not possible to remove the character at any of the position to get the double string. Hence, it is not a special dish.
import sys n = int(input()) for i in range(n): s = input() m = len(s) if m == 1: sys.stdout.write("NO\n") elif m % 2 != 0: i = 0 j = int(m / 2) + 1 flag = False Flag = True while j < m: if s[i] != s[j]: if not flag: flag = True i += 1 else: Flag = False break else: i += 1 j += 1 if not Flag: i = 0 j = int(m / 2) flag = False Flag = True while j < m: if s[i] != s[j]: if not flag: flag = True j += 1 else: Flag = False break else: i += 1 j += 1 if Flag: sys.stdout.write("YES\n") else: sys.stdout.write("NO\n") else: sys.stdout.write("YES\n") elif s[: int(m / 2)] == s[int(m / 2) :]: sys.stdout.write("YES\n") else: sys.stdout.write("NO\n")
IMPORT 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 IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR IF VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR IF VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
One day, Chef prepared D brand new dishes. He named the i-th dish by a string Si. After the cooking, he decided to categorize each of these D dishes as special or not. A dish Si is called special if it's name (i.e. the string Si) can be represented in the form of a double string by removing at most one (possibly zero) character from it's name from any position. A string is called a double string if it can be represented as a concatenation of two identical, non-empty strings. e.g. "abab" is a double string as it can be represented as "ab" + "ab" where + operation denotes concatenation. Similarly, "aa", "abcabc" are double strings whereas "a", "abba", "abc" are not. -----Input----- - First line of the input contains an integer D denoting the number of dishes prepared by Chef on that day. - Each of the next D lines will contain description of a dish. - The i-th line contains the name of i-th dish Si. -----Output----- For each of the D dishes, print a single line containing "YES" or "NO" (without quotes) denoting whether the dish can be called as a special or not. -----Constraints----- - 1 ≀ D ≀ 106 - 1 ≀ |Si| ≀ 106. - Each character of string Si will be lower case English alphabet (i.e. from 'a' to 'z'). -----Subtasks----- Subtask #1 : (20 points) - Sum of |Si| in an input file doesn't exceed 2 * 103 Subtask 2 : (80 points) - Sum of |Si| in an input file doesn't exceed 2 * 106 -----Example----- Input: 3 aba abac abcd Output: YES NO NO -----Explanation----- Example case 1. We can remove the character at position 1 (0-based index) to get "aa" which is a double string. Hence, it is a special dish. Example case 2. It is not possible to remove the character at any of the position to get the double string. Hence, it is not a special dish.
t = int(input()) for i in range(t): s = input() if len(s) == 0 or len(s) == 1: print("NO") else: if len(s) % 2 == 0: flag = False half = int(len(s) / 2) if s[0:half] == s[half : len(s)]: flag = True else: flag = False else: flag = False l = int((len(s) + 1) / 2) news = s for j in range(l): if j == l - 1: news = s[0:j] + s[j + 1 : len(s)] half = int(len(news) / 2) if news[0:half] == news[half : len(news)]: flag = True break else: flag = False break if s[j] == s[l + j]: pass else: news = s[0:j] + s[j + 1 : len(s)] if ( news[0 : int(len(news) / 2)] == news[int(len(news) / 2) : len(news)] ): flag = True break else: news = s[0 : l + j] + s[l + 1 + j : len(s)] if ( news[0 : int(len(news) / 2)] == news[int(len(news) / 2) : len(news)] ): flag = True break if flag: print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
One day, Chef prepared D brand new dishes. He named the i-th dish by a string Si. After the cooking, he decided to categorize each of these D dishes as special or not. A dish Si is called special if it's name (i.e. the string Si) can be represented in the form of a double string by removing at most one (possibly zero) character from it's name from any position. A string is called a double string if it can be represented as a concatenation of two identical, non-empty strings. e.g. "abab" is a double string as it can be represented as "ab" + "ab" where + operation denotes concatenation. Similarly, "aa", "abcabc" are double strings whereas "a", "abba", "abc" are not. -----Input----- - First line of the input contains an integer D denoting the number of dishes prepared by Chef on that day. - Each of the next D lines will contain description of a dish. - The i-th line contains the name of i-th dish Si. -----Output----- For each of the D dishes, print a single line containing "YES" or "NO" (without quotes) denoting whether the dish can be called as a special or not. -----Constraints----- - 1 ≀ D ≀ 106 - 1 ≀ |Si| ≀ 106. - Each character of string Si will be lower case English alphabet (i.e. from 'a' to 'z'). -----Subtasks----- Subtask #1 : (20 points) - Sum of |Si| in an input file doesn't exceed 2 * 103 Subtask 2 : (80 points) - Sum of |Si| in an input file doesn't exceed 2 * 106 -----Example----- Input: 3 aba abac abcd Output: YES NO NO -----Explanation----- Example case 1. We can remove the character at position 1 (0-based index) to get "aa" which is a double string. Hence, it is a special dish. Example case 2. It is not possible to remove the character at any of the position to get the double string. Hence, it is not a special dish.
for _ in range(int(input())): s = input() n = len(s) if n == 1: print("NO") continue if n % 2 == 0: if s[: n // 2] == s[n // 2 :]: print("YES") else: print("NO") continue A, B = s[: n // 2], s[n // 2 :] if B[1:] == A: print("YES") continue elif B[:-1] == A: print("YES") continue elif B[-1] != B[0]: i, j = 0, 0 while i != n // 2: if A[i] == B[j]: i += 1 j += 1 elif A[i:] != B[j + 1 :]: print("NO") break else: print("YES") break else: i, j, t1 = 0, 0, False while i != n // 2: if A[i] == B[j]: i += 1 j += 1 elif A[i:] == B[j + 1 :]: t1 = True break else: break i, j, t2 = 0, 1, False while i != n // 2: if A[i] == B[j]: i += 1 j += 1 elif A[i + 1 :] == B[j:-1]: t2 = True break else: break if t1 or t2: print("YES") else: print("NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR STRING IF VAR NUMBER VAR EXPR FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
One day, Chef prepared D brand new dishes. He named the i-th dish by a string Si. After the cooking, he decided to categorize each of these D dishes as special or not. A dish Si is called special if it's name (i.e. the string Si) can be represented in the form of a double string by removing at most one (possibly zero) character from it's name from any position. A string is called a double string if it can be represented as a concatenation of two identical, non-empty strings. e.g. "abab" is a double string as it can be represented as "ab" + "ab" where + operation denotes concatenation. Similarly, "aa", "abcabc" are double strings whereas "a", "abba", "abc" are not. -----Input----- - First line of the input contains an integer D denoting the number of dishes prepared by Chef on that day. - Each of the next D lines will contain description of a dish. - The i-th line contains the name of i-th dish Si. -----Output----- For each of the D dishes, print a single line containing "YES" or "NO" (without quotes) denoting whether the dish can be called as a special or not. -----Constraints----- - 1 ≀ D ≀ 106 - 1 ≀ |Si| ≀ 106. - Each character of string Si will be lower case English alphabet (i.e. from 'a' to 'z'). -----Subtasks----- Subtask #1 : (20 points) - Sum of |Si| in an input file doesn't exceed 2 * 103 Subtask 2 : (80 points) - Sum of |Si| in an input file doesn't exceed 2 * 106 -----Example----- Input: 3 aba abac abcd Output: YES NO NO -----Explanation----- Example case 1. We can remove the character at position 1 (0-based index) to get "aa" which is a double string. Hence, it is a special dish. Example case 2. It is not possible to remove the character at any of the position to get the double string. Hence, it is not a special dish.
for _ in range(int(input())): s = input() n = len(s) ans = "NO" if len(s) == 1: ans = "NO" elif n % 2 == 0: if s[: n // 2] == s[n // 2 :]: ans = "YES" else: s1 = s[: n // 2] s2 = s[n // 2 :] index = n // 2 for i in range(n // 2): if s1[i] != s2[i]: index = i break if s1 == s2[:index] + s2[index + 1 :]: ans = "YES" else: s1 = s[: n // 2 + 1] s2 = s[n // 2 + 1 :] index = n // 2 for i in range(n // 2): if s1[i] != s2[i]: index = i break if s2 == s1[:index] + s1[index + 1 :]: ans = "YES" print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING IF BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR STRING ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR STRING ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR
One day, Chef prepared D brand new dishes. He named the i-th dish by a string Si. After the cooking, he decided to categorize each of these D dishes as special or not. A dish Si is called special if it's name (i.e. the string Si) can be represented in the form of a double string by removing at most one (possibly zero) character from it's name from any position. A string is called a double string if it can be represented as a concatenation of two identical, non-empty strings. e.g. "abab" is a double string as it can be represented as "ab" + "ab" where + operation denotes concatenation. Similarly, "aa", "abcabc" are double strings whereas "a", "abba", "abc" are not. -----Input----- - First line of the input contains an integer D denoting the number of dishes prepared by Chef on that day. - Each of the next D lines will contain description of a dish. - The i-th line contains the name of i-th dish Si. -----Output----- For each of the D dishes, print a single line containing "YES" or "NO" (without quotes) denoting whether the dish can be called as a special or not. -----Constraints----- - 1 ≀ D ≀ 106 - 1 ≀ |Si| ≀ 106. - Each character of string Si will be lower case English alphabet (i.e. from 'a' to 'z'). -----Subtasks----- Subtask #1 : (20 points) - Sum of |Si| in an input file doesn't exceed 2 * 103 Subtask 2 : (80 points) - Sum of |Si| in an input file doesn't exceed 2 * 106 -----Example----- Input: 3 aba abac abcd Output: YES NO NO -----Explanation----- Example case 1. We can remove the character at position 1 (0-based index) to get "aa" which is a double string. Hence, it is a special dish. Example case 2. It is not possible to remove the character at any of the position to get the double string. Hence, it is not a special dish.
t = int(input()) while t > 0: s = input() n = len(s) f = 0 if n == 1: f = 0 elif len(s) % 2 == 0: if s[0 : n // 2] == s[n // 2 :]: f = 1 else: l = (n + 1) // 2 for i in range(l): if i == l - 1: a = s[0:i] + s[i + 1 : len(s)] h = len(a) // 2 if a[0:h] == a[h : len(a)]: f = 1 break else: f = 0 break if s[i] == s[l + i]: pass else: a = s[0:i] + s[i + 1 : len(s)] if a[0 : len(a) // 2] == a[len(a) // 2 : len(a)]: f = 1 break else: a = s[0 : l + i] + s[l + 1 + i : len(s)] if a[0 : len(a) // 2] == a[len(a) // 2 : len(a)]: f = 1 break if f == 0: print("NO") else: print("YES") t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER
One day, Chef prepared D brand new dishes. He named the i-th dish by a string Si. After the cooking, he decided to categorize each of these D dishes as special or not. A dish Si is called special if it's name (i.e. the string Si) can be represented in the form of a double string by removing at most one (possibly zero) character from it's name from any position. A string is called a double string if it can be represented as a concatenation of two identical, non-empty strings. e.g. "abab" is a double string as it can be represented as "ab" + "ab" where + operation denotes concatenation. Similarly, "aa", "abcabc" are double strings whereas "a", "abba", "abc" are not. -----Input----- - First line of the input contains an integer D denoting the number of dishes prepared by Chef on that day. - Each of the next D lines will contain description of a dish. - The i-th line contains the name of i-th dish Si. -----Output----- For each of the D dishes, print a single line containing "YES" or "NO" (without quotes) denoting whether the dish can be called as a special or not. -----Constraints----- - 1 ≀ D ≀ 106 - 1 ≀ |Si| ≀ 106. - Each character of string Si will be lower case English alphabet (i.e. from 'a' to 'z'). -----Subtasks----- Subtask #1 : (20 points) - Sum of |Si| in an input file doesn't exceed 2 * 103 Subtask 2 : (80 points) - Sum of |Si| in an input file doesn't exceed 2 * 106 -----Example----- Input: 3 aba abac abcd Output: YES NO NO -----Explanation----- Example case 1. We can remove the character at position 1 (0-based index) to get "aa" which is a double string. Hence, it is a special dish. Example case 2. It is not possible to remove the character at any of the position to get the double string. Hence, it is not a special dish.
def ds(x, z): w = len(x) q = len(z) i = 0 j = 0 f = 0 h = 0 while i < w and j < q: if x[i] == z[j]: i += 1 j += 1 else: f += 1 i += 1 if f > 1: h = 1 break if h == 0: return True else: return False d = int(input()) while d != 0: s = str(input()) b = len(s) if b == 1: print("NO") elif b % 2 == 0: f = b // 2 r = s[0:f] p = s[f:] if r == p: print("YES") else: print("NO") else: f = b // 2 r = s[0 : f + 1] p = s[f + 1 :] y = s[0:f] t = s[f:] if ds(r, p): print("YES") elif ds(t, y): print("YES") else: print("NO") d -= 1
FUNC_DEF 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 VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER
One day, Chef prepared D brand new dishes. He named the i-th dish by a string Si. After the cooking, he decided to categorize each of these D dishes as special or not. A dish Si is called special if it's name (i.e. the string Si) can be represented in the form of a double string by removing at most one (possibly zero) character from it's name from any position. A string is called a double string if it can be represented as a concatenation of two identical, non-empty strings. e.g. "abab" is a double string as it can be represented as "ab" + "ab" where + operation denotes concatenation. Similarly, "aa", "abcabc" are double strings whereas "a", "abba", "abc" are not. -----Input----- - First line of the input contains an integer D denoting the number of dishes prepared by Chef on that day. - Each of the next D lines will contain description of a dish. - The i-th line contains the name of i-th dish Si. -----Output----- For each of the D dishes, print a single line containing "YES" or "NO" (without quotes) denoting whether the dish can be called as a special or not. -----Constraints----- - 1 ≀ D ≀ 106 - 1 ≀ |Si| ≀ 106. - Each character of string Si will be lower case English alphabet (i.e. from 'a' to 'z'). -----Subtasks----- Subtask #1 : (20 points) - Sum of |Si| in an input file doesn't exceed 2 * 103 Subtask 2 : (80 points) - Sum of |Si| in an input file doesn't exceed 2 * 106 -----Example----- Input: 3 aba abac abcd Output: YES NO NO -----Explanation----- Example case 1. We can remove the character at position 1 (0-based index) to get "aa" which is a double string. Hence, it is a special dish. Example case 2. It is not possible to remove the character at any of the position to get the double string. Hence, it is not a special dish.
def offbyone(plus, ref): lp = len(plus) if lp == 2: return ref == plus[0] or ref == plus[1] sm = lp // 2 if plus[:sm] == ref[:sm]: return offbyone(plus[sm:], ref[sm:]) elif plus[sm + 1 :] == ref[sm:]: return offbyone(plus[: sm + 1], ref[:sm]) else: return False def specialname(z): yes = "YES" no = "NO" lz = len(z) if lz == 1: return no hl = lz // 2 if lz % 2 == 0: if z[:hl] == z[hl:]: return yes else: return no elif offbyone(z[: hl + 1], z[hl + 1 :]) or offbyone(z[hl:], z[:hl]): return yes else: return no for _ in range(int(input())): print(specialname(input().strip()))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR RETURN VAR RETURN VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR
One day, Chef prepared D brand new dishes. He named the i-th dish by a string Si. After the cooking, he decided to categorize each of these D dishes as special or not. A dish Si is called special if it's name (i.e. the string Si) can be represented in the form of a double string by removing at most one (possibly zero) character from it's name from any position. A string is called a double string if it can be represented as a concatenation of two identical, non-empty strings. e.g. "abab" is a double string as it can be represented as "ab" + "ab" where + operation denotes concatenation. Similarly, "aa", "abcabc" are double strings whereas "a", "abba", "abc" are not. -----Input----- - First line of the input contains an integer D denoting the number of dishes prepared by Chef on that day. - Each of the next D lines will contain description of a dish. - The i-th line contains the name of i-th dish Si. -----Output----- For each of the D dishes, print a single line containing "YES" or "NO" (without quotes) denoting whether the dish can be called as a special or not. -----Constraints----- - 1 ≀ D ≀ 106 - 1 ≀ |Si| ≀ 106. - Each character of string Si will be lower case English alphabet (i.e. from 'a' to 'z'). -----Subtasks----- Subtask #1 : (20 points) - Sum of |Si| in an input file doesn't exceed 2 * 103 Subtask 2 : (80 points) - Sum of |Si| in an input file doesn't exceed 2 * 106 -----Example----- Input: 3 aba abac abcd Output: YES NO NO -----Explanation----- Example case 1. We can remove the character at position 1 (0-based index) to get "aa" which is a double string. Hence, it is a special dish. Example case 2. It is not possible to remove the character at any of the position to get the double string. Hence, it is not a special dish.
D = int(input()) for i in range(D): now = input() len_now = len(now) if len_now == 1: print("NO") continue elif len_now % 2 == 0: first = 0 mid = len_now // 2 second = mid while first < mid and now[first] == now[second]: first = first + 1 second = second + 1 if first == mid: print("YES") continue else: print("NO") continue else: flag = 0 error = 0 first = 0 mid = len_now // 2 second = mid + 1 while first < mid and now[first] == now[second]: first = first + 1 second = second + 1 if first == mid: print("YES") continue first = 0 second = mid + 1 error = 0 while first <= mid: if now[first] == now[second]: first = first + 1 second = second + 1 else: error = error + 1 if error > 1: break else: first = first + 1 if first == mid + 1 and error == 1: print("YES") continue first = 0 second = mid error = 0 while second < len_now: if now[first] == now[second]: first = first + 1 second = second + 1 else: error = error + 1 if error > 1: break else: second = second + 1 if second == len_now and error == 1: print("YES") continue print("NO")
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 IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
One day, Chef prepared D brand new dishes. He named the i-th dish by a string Si. After the cooking, he decided to categorize each of these D dishes as special or not. A dish Si is called special if it's name (i.e. the string Si) can be represented in the form of a double string by removing at most one (possibly zero) character from it's name from any position. A string is called a double string if it can be represented as a concatenation of two identical, non-empty strings. e.g. "abab" is a double string as it can be represented as "ab" + "ab" where + operation denotes concatenation. Similarly, "aa", "abcabc" are double strings whereas "a", "abba", "abc" are not. -----Input----- - First line of the input contains an integer D denoting the number of dishes prepared by Chef on that day. - Each of the next D lines will contain description of a dish. - The i-th line contains the name of i-th dish Si. -----Output----- For each of the D dishes, print a single line containing "YES" or "NO" (without quotes) denoting whether the dish can be called as a special or not. -----Constraints----- - 1 ≀ D ≀ 106 - 1 ≀ |Si| ≀ 106. - Each character of string Si will be lower case English alphabet (i.e. from 'a' to 'z'). -----Subtasks----- Subtask #1 : (20 points) - Sum of |Si| in an input file doesn't exceed 2 * 103 Subtask 2 : (80 points) - Sum of |Si| in an input file doesn't exceed 2 * 106 -----Example----- Input: 3 aba abac abcd Output: YES NO NO -----Explanation----- Example case 1. We can remove the character at position 1 (0-based index) to get "aa" which is a double string. Hence, it is a special dish. Example case 2. It is not possible to remove the character at any of the position to get the double string. Hence, it is not a special dish.
def isDouble(s): if len(s) % 2 == 1: return False else: return s[: len(s) // 2] == s[len(s) // 2 :] def cal(s): l = [0] * 26 i = 0 while i < len(s): k = ord(s[i]) l[k - 97] += 1 i += 1 i = 0 c = [] while i < 26: if l[i] % 2 == 1: c += [i] i += 1 if len(c) == 1: return chr(c[0] + 97) else: return False def isSpecial(s): if len(s) % 2 == 0: return s[: len(s) // 2] == s[len(s) // 2 :] elif len(s) == 1: return False else: c = cal(s) if c == False: return False else: i = 0 while i < len(s): if s[i] == c: if isDouble(s[:i] + s[i + 1 :]): return True i += 1 return False test = int(input()) while test != 0: test -= 1 s = input() if isSpecial(s): print("YES") else: print("NO")
FUNC_DEF IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN NUMBER RETURN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR LIST VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER FUNC_DEF IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
One day, Chef prepared D brand new dishes. He named the i-th dish by a string Si. After the cooking, he decided to categorize each of these D dishes as special or not. A dish Si is called special if it's name (i.e. the string Si) can be represented in the form of a double string by removing at most one (possibly zero) character from it's name from any position. A string is called a double string if it can be represented as a concatenation of two identical, non-empty strings. e.g. "abab" is a double string as it can be represented as "ab" + "ab" where + operation denotes concatenation. Similarly, "aa", "abcabc" are double strings whereas "a", "abba", "abc" are not. -----Input----- - First line of the input contains an integer D denoting the number of dishes prepared by Chef on that day. - Each of the next D lines will contain description of a dish. - The i-th line contains the name of i-th dish Si. -----Output----- For each of the D dishes, print a single line containing "YES" or "NO" (without quotes) denoting whether the dish can be called as a special or not. -----Constraints----- - 1 ≀ D ≀ 106 - 1 ≀ |Si| ≀ 106. - Each character of string Si will be lower case English alphabet (i.e. from 'a' to 'z'). -----Subtasks----- Subtask #1 : (20 points) - Sum of |Si| in an input file doesn't exceed 2 * 103 Subtask 2 : (80 points) - Sum of |Si| in an input file doesn't exceed 2 * 106 -----Example----- Input: 3 aba abac abcd Output: YES NO NO -----Explanation----- Example case 1. We can remove the character at position 1 (0-based index) to get "aa" which is a double string. Hence, it is a special dish. Example case 2. It is not possible to remove the character at any of the position to get the double string. Hence, it is not a special dish.
def specialname(z): yes = "YES" no = "NO" lz = len(z) if lz == 1: return no hl = lz // 2 if lz % 2 == 0: if z[:hl] == z[hl:]: return yes else: return no else: hist = dict() for c in z: if c in hist: hist[c] += 1 else: hist[c] = 1 if len(hist) == 1: return yes oddc = "" for c, f in hist.items(): if f % 2 == 1: if oddc != "": return no else: oddc = c w = z.replace(oddc, "") if w[: len(w) // 2] != w[len(w) // 2 :]: return no pos = -1 rc = hist[oddc] while rc > 0: pos = z.find(oddc, pos + 1) w = z[:pos] + z[pos + 1 :] if w[:hl] == w[hl:]: return yes while z[pos] == oddc: rc -= 1 if rc == 0: return no pos += 1 for _ in range(int(input())): print(specialname(input().strip()))
FUNC_DEF ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR RETURN VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR STRING FOR VAR VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER IF VAR STRING RETURN VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR STRING IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR RETURN VAR WHILE VAR VAR VAR VAR NUMBER IF VAR NUMBER RETURN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR
One day, Chef prepared D brand new dishes. He named the i-th dish by a string Si. After the cooking, he decided to categorize each of these D dishes as special or not. A dish Si is called special if it's name (i.e. the string Si) can be represented in the form of a double string by removing at most one (possibly zero) character from it's name from any position. A string is called a double string if it can be represented as a concatenation of two identical, non-empty strings. e.g. "abab" is a double string as it can be represented as "ab" + "ab" where + operation denotes concatenation. Similarly, "aa", "abcabc" are double strings whereas "a", "abba", "abc" are not. -----Input----- - First line of the input contains an integer D denoting the number of dishes prepared by Chef on that day. - Each of the next D lines will contain description of a dish. - The i-th line contains the name of i-th dish Si. -----Output----- For each of the D dishes, print a single line containing "YES" or "NO" (without quotes) denoting whether the dish can be called as a special or not. -----Constraints----- - 1 ≀ D ≀ 106 - 1 ≀ |Si| ≀ 106. - Each character of string Si will be lower case English alphabet (i.e. from 'a' to 'z'). -----Subtasks----- Subtask #1 : (20 points) - Sum of |Si| in an input file doesn't exceed 2 * 103 Subtask 2 : (80 points) - Sum of |Si| in an input file doesn't exceed 2 * 106 -----Example----- Input: 3 aba abac abcd Output: YES NO NO -----Explanation----- Example case 1. We can remove the character at position 1 (0-based index) to get "aa" which is a double string. Hence, it is a special dish. Example case 2. It is not possible to remove the character at any of the position to get the double string. Hence, it is not a special dish.
t = int(input()) while t: t -= 1 s = input() l = len(s) if l == 1: print("NO") continue flag = 0 if l % 2 == 0: for i in range(l // 2): if s[i] != s[i + l // 2]: flag = 1 break else: for i in range(l // 2): if s[i] != s[i + l // 2 + 1]: flag = 1 break if flag: flag = 0 vis = 0 i = 0 j = l // 2 + 1 while i <= l // 2: if s[i] != s[j]: if vis == 1: flag = 1 break else: i += 1 vis = 1 else: i += 1 j += 1 if flag: flag = 0 vis = 0 i = 0 j = l // 2 while i < l // 2: if s[i] != s[j]: if vis == 1: flag = 1 break else: j += 1 vis = 1 else: i += 1 j += 1 if flag == 0: print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
One day, Chef prepared D brand new dishes. He named the i-th dish by a string Si. After the cooking, he decided to categorize each of these D dishes as special or not. A dish Si is called special if it's name (i.e. the string Si) can be represented in the form of a double string by removing at most one (possibly zero) character from it's name from any position. A string is called a double string if it can be represented as a concatenation of two identical, non-empty strings. e.g. "abab" is a double string as it can be represented as "ab" + "ab" where + operation denotes concatenation. Similarly, "aa", "abcabc" are double strings whereas "a", "abba", "abc" are not. -----Input----- - First line of the input contains an integer D denoting the number of dishes prepared by Chef on that day. - Each of the next D lines will contain description of a dish. - The i-th line contains the name of i-th dish Si. -----Output----- For each of the D dishes, print a single line containing "YES" or "NO" (without quotes) denoting whether the dish can be called as a special or not. -----Constraints----- - 1 ≀ D ≀ 106 - 1 ≀ |Si| ≀ 106. - Each character of string Si will be lower case English alphabet (i.e. from 'a' to 'z'). -----Subtasks----- Subtask #1 : (20 points) - Sum of |Si| in an input file doesn't exceed 2 * 103 Subtask 2 : (80 points) - Sum of |Si| in an input file doesn't exceed 2 * 106 -----Example----- Input: 3 aba abac abcd Output: YES NO NO -----Explanation----- Example case 1. We can remove the character at position 1 (0-based index) to get "aa" which is a double string. Hence, it is a special dish. Example case 2. It is not possible to remove the character at any of the position to get the double string. Hence, it is not a special dish.
for _ in range(int(input())): s = input() s = list(s) s1 = s f = 0 l = len(s) if l >= 2: for i in range(l): if l % 2 == 0: ln = l tp1 = s1[: ln // 2] tp2 = s1[ln // 2 :] if str(tp1) == str(tp2): f = 1 break elif i == 0: tmp = s1[i + 1 :] ln = l - 1 tp1 = tmp[: ln // 2] tp2 = tmp[ln // 2 :] if str(tp1) == str(tp2): f = 1 break else: tmp = s1[:i] tmp2 = s1[i + 1 :] temp = tmp + tmp2 ln = l - 1 tp1 = temp[: ln // 2] tp2 = temp[ln // 2 :] if str(tp1) == str(tp2): f = 1 break if f == 1: print("YES") else: print("NO") elif l == 1: print("NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING
One day, Chef prepared D brand new dishes. He named the i-th dish by a string Si. After the cooking, he decided to categorize each of these D dishes as special or not. A dish Si is called special if it's name (i.e. the string Si) can be represented in the form of a double string by removing at most one (possibly zero) character from it's name from any position. A string is called a double string if it can be represented as a concatenation of two identical, non-empty strings. e.g. "abab" is a double string as it can be represented as "ab" + "ab" where + operation denotes concatenation. Similarly, "aa", "abcabc" are double strings whereas "a", "abba", "abc" are not. -----Input----- - First line of the input contains an integer D denoting the number of dishes prepared by Chef on that day. - Each of the next D lines will contain description of a dish. - The i-th line contains the name of i-th dish Si. -----Output----- For each of the D dishes, print a single line containing "YES" or "NO" (without quotes) denoting whether the dish can be called as a special or not. -----Constraints----- - 1 ≀ D ≀ 106 - 1 ≀ |Si| ≀ 106. - Each character of string Si will be lower case English alphabet (i.e. from 'a' to 'z'). -----Subtasks----- Subtask #1 : (20 points) - Sum of |Si| in an input file doesn't exceed 2 * 103 Subtask 2 : (80 points) - Sum of |Si| in an input file doesn't exceed 2 * 106 -----Example----- Input: 3 aba abac abcd Output: YES NO NO -----Explanation----- Example case 1. We can remove the character at position 1 (0-based index) to get "aa" which is a double string. Hence, it is a special dish. Example case 2. It is not possible to remove the character at any of the position to get the double string. Hence, it is not a special dish.
__author__ = "masterchief" def solve(): d = int(input()) for dish in range(d): dish_special() def dish_special(): name = input() special_name = False len_name = len(name) mid = len_name // 2 if len_name == 1: pass elif len_name % 2: if match(name[:mid], name[mid:], 1) or match( name[mid + 1 :], name[: mid + 1], 1 ): special_name = True elif match(name[:mid], name[mid:]): special_name = True print("YES") if special_name else print("NO") def match(seq_template, seq_search, fault_tolerance=0): len_template = len(seq_template) len_search = len(seq_search) faults = 0 i = 0 while i < len_template: if seq_template[i] != seq_search[i + faults]: faults += 1 else: i += 1 if faults > fault_tolerance: return False return True solve()
ASSIGN VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_DEF NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER EXPR FUNC_CALL VAR
One day, Chef prepared D brand new dishes. He named the i-th dish by a string Si. After the cooking, he decided to categorize each of these D dishes as special or not. A dish Si is called special if it's name (i.e. the string Si) can be represented in the form of a double string by removing at most one (possibly zero) character from it's name from any position. A string is called a double string if it can be represented as a concatenation of two identical, non-empty strings. e.g. "abab" is a double string as it can be represented as "ab" + "ab" where + operation denotes concatenation. Similarly, "aa", "abcabc" are double strings whereas "a", "abba", "abc" are not. -----Input----- - First line of the input contains an integer D denoting the number of dishes prepared by Chef on that day. - Each of the next D lines will contain description of a dish. - The i-th line contains the name of i-th dish Si. -----Output----- For each of the D dishes, print a single line containing "YES" or "NO" (without quotes) denoting whether the dish can be called as a special or not. -----Constraints----- - 1 ≀ D ≀ 106 - 1 ≀ |Si| ≀ 106. - Each character of string Si will be lower case English alphabet (i.e. from 'a' to 'z'). -----Subtasks----- Subtask #1 : (20 points) - Sum of |Si| in an input file doesn't exceed 2 * 103 Subtask 2 : (80 points) - Sum of |Si| in an input file doesn't exceed 2 * 106 -----Example----- Input: 3 aba abac abcd Output: YES NO NO -----Explanation----- Example case 1. We can remove the character at position 1 (0-based index) to get "aa" which is a double string. Hence, it is a special dish. Example case 2. It is not possible to remove the character at any of the position to get the double string. Hence, it is not a special dish.
def check_equal(a, b): index = 0 for i in a: while index < len(b) and i != b[index]: index += 1 if index >= len(b): return False index += 1 return True def Dob_String(n): size = len(n) midpoint = size // 2 if check_equal(n[0:midpoint], n[midpoint:size]): return "YES" elif size % 2 != 0 and check_equal(n[midpoint + 1 : size], n[0 : midpoint + 1]): return "YES" else: return "NO" T = int(input()) for _ in range(T): n = input() if len(n) > 1: print(Dob_String(n)) else: print("NO")
FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR RETURN STRING IF BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER RETURN STRING RETURN STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
One day, Chef prepared D brand new dishes. He named the i-th dish by a string Si. After the cooking, he decided to categorize each of these D dishes as special or not. A dish Si is called special if it's name (i.e. the string Si) can be represented in the form of a double string by removing at most one (possibly zero) character from it's name from any position. A string is called a double string if it can be represented as a concatenation of two identical, non-empty strings. e.g. "abab" is a double string as it can be represented as "ab" + "ab" where + operation denotes concatenation. Similarly, "aa", "abcabc" are double strings whereas "a", "abba", "abc" are not. -----Input----- - First line of the input contains an integer D denoting the number of dishes prepared by Chef on that day. - Each of the next D lines will contain description of a dish. - The i-th line contains the name of i-th dish Si. -----Output----- For each of the D dishes, print a single line containing "YES" or "NO" (without quotes) denoting whether the dish can be called as a special or not. -----Constraints----- - 1 ≀ D ≀ 106 - 1 ≀ |Si| ≀ 106. - Each character of string Si will be lower case English alphabet (i.e. from 'a' to 'z'). -----Subtasks----- Subtask #1 : (20 points) - Sum of |Si| in an input file doesn't exceed 2 * 103 Subtask 2 : (80 points) - Sum of |Si| in an input file doesn't exceed 2 * 106 -----Example----- Input: 3 aba abac abcd Output: YES NO NO -----Explanation----- Example case 1. We can remove the character at position 1 (0-based index) to get "aa" which is a double string. Hence, it is a special dish. Example case 2. It is not possible to remove the character at any of the position to get the double string. Hence, it is not a special dish.
def fnd(s, ch): a = [] for i in range(len(s)): if ch == s[i]: a += [i] return a t = int(input()) for _ in range(t): s = input() n = len(s) if len(s) == 1: print("NO") elif n % 2 == 0: if s[0 : n // 2] == s[n // 2 : n]: print("YES") else: print("NO") else: d = [0] * 26 for i in range(n): d[ord(s[i]) - 97] += 1 f = 1 p = 0 k = [0] * 2 x = 0 for i in range(26): if d[i] % 2 != 0: p += 1 k[x] = i x += 1 if p > 1: f = 0 break if f == 0: print("NO") elif s[0 : n // 2] == s[n // 2 : n]: print("YES") else: ch1 = chr(k[0] + 97) pos = fnd(s, ch1) m = 0 for i in range(len(pos)): t = s[0 : pos[i]] + s[pos[i] + 1 : n] if t[0 : n // 2] == t[n // 2 : n]: m = 1 break if m: print("YES") else: print("NO")
FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR LIST VAR RETURN VAR 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 IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER IF VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER VAR IF VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
One day, Chef prepared D brand new dishes. He named the i-th dish by a string Si. After the cooking, he decided to categorize each of these D dishes as special or not. A dish Si is called special if it's name (i.e. the string Si) can be represented in the form of a double string by removing at most one (possibly zero) character from it's name from any position. A string is called a double string if it can be represented as a concatenation of two identical, non-empty strings. e.g. "abab" is a double string as it can be represented as "ab" + "ab" where + operation denotes concatenation. Similarly, "aa", "abcabc" are double strings whereas "a", "abba", "abc" are not. -----Input----- - First line of the input contains an integer D denoting the number of dishes prepared by Chef on that day. - Each of the next D lines will contain description of a dish. - The i-th line contains the name of i-th dish Si. -----Output----- For each of the D dishes, print a single line containing "YES" or "NO" (without quotes) denoting whether the dish can be called as a special or not. -----Constraints----- - 1 ≀ D ≀ 106 - 1 ≀ |Si| ≀ 106. - Each character of string Si will be lower case English alphabet (i.e. from 'a' to 'z'). -----Subtasks----- Subtask #1 : (20 points) - Sum of |Si| in an input file doesn't exceed 2 * 103 Subtask 2 : (80 points) - Sum of |Si| in an input file doesn't exceed 2 * 106 -----Example----- Input: 3 aba abac abcd Output: YES NO NO -----Explanation----- Example case 1. We can remove the character at position 1 (0-based index) to get "aa" which is a double string. Hence, it is a special dish. Example case 2. It is not possible to remove the character at any of the position to get the double string. Hence, it is not a special dish.
t = int(input()) for ti in range(t): s = input().strip() l = len(s) if l == 1: print("NO") elif l % 2 == 0: if s[: l // 2] == s[l // 2 :]: print("YES") else: print("NO") else: special = False x = 0 for c in s: x = x ^ ord(c) c = chr(x) if c in s: k = (l - 1) // 2 n = s.count(c) i = 0 while (n > 0) & (not special): i = s.find(c, i) news = s[:i] + s[i + 1 :] if news[:k] == news[k:]: special = True break n = n - 1 i = i + 1 if special: print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
One day, Chef prepared D brand new dishes. He named the i-th dish by a string Si. After the cooking, he decided to categorize each of these D dishes as special or not. A dish Si is called special if it's name (i.e. the string Si) can be represented in the form of a double string by removing at most one (possibly zero) character from it's name from any position. A string is called a double string if it can be represented as a concatenation of two identical, non-empty strings. e.g. "abab" is a double string as it can be represented as "ab" + "ab" where + operation denotes concatenation. Similarly, "aa", "abcabc" are double strings whereas "a", "abba", "abc" are not. -----Input----- - First line of the input contains an integer D denoting the number of dishes prepared by Chef on that day. - Each of the next D lines will contain description of a dish. - The i-th line contains the name of i-th dish Si. -----Output----- For each of the D dishes, print a single line containing "YES" or "NO" (without quotes) denoting whether the dish can be called as a special or not. -----Constraints----- - 1 ≀ D ≀ 106 - 1 ≀ |Si| ≀ 106. - Each character of string Si will be lower case English alphabet (i.e. from 'a' to 'z'). -----Subtasks----- Subtask #1 : (20 points) - Sum of |Si| in an input file doesn't exceed 2 * 103 Subtask 2 : (80 points) - Sum of |Si| in an input file doesn't exceed 2 * 106 -----Example----- Input: 3 aba abac abcd Output: YES NO NO -----Explanation----- Example case 1. We can remove the character at position 1 (0-based index) to get "aa" which is a double string. Hence, it is a special dish. Example case 2. It is not possible to remove the character at any of the position to get the double string. Hence, it is not a special dish.
def isCool(s): l = len(s) if l == 1: return False if l % 2 == 0: return True if isCool2(s) else False mid = True idx = -1 for x in range(l // 2): if s[x] != s[l // 2 + x]: idx = x break if idx == -1: return True idx2 = -1 for x in range(l // 2): if s[x] != s[l // 2 + x + 1]: idx2 = x break if idx2 == -1: return True a = s[:idx] + s[idx + 1 :] idx += l // 2 b = s[:idx] + s[idx + 1 :] idx = idx2 c = s[:idx] + s[idx + 1 :] idx += l // 2 d = s[:idx] + s[idx + 1 :] if isCool2(a) or isCool2(b) or isCool2(c) or isCool2(d): return True def isCool2(s): l = len(s) idx = -1 for x in range(l // 2): if s[x] != s[l // 2 + x]: idx = x break if idx == -1: return True else: return False n = int(input()) for x in range(n): s = input() print("YES" if isCool(s) else "NO")
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING STRING
One day, Chef prepared D brand new dishes. He named the i-th dish by a string Si. After the cooking, he decided to categorize each of these D dishes as special or not. A dish Si is called special if it's name (i.e. the string Si) can be represented in the form of a double string by removing at most one (possibly zero) character from it's name from any position. A string is called a double string if it can be represented as a concatenation of two identical, non-empty strings. e.g. "abab" is a double string as it can be represented as "ab" + "ab" where + operation denotes concatenation. Similarly, "aa", "abcabc" are double strings whereas "a", "abba", "abc" are not. -----Input----- - First line of the input contains an integer D denoting the number of dishes prepared by Chef on that day. - Each of the next D lines will contain description of a dish. - The i-th line contains the name of i-th dish Si. -----Output----- For each of the D dishes, print a single line containing "YES" or "NO" (without quotes) denoting whether the dish can be called as a special or not. -----Constraints----- - 1 ≀ D ≀ 106 - 1 ≀ |Si| ≀ 106. - Each character of string Si will be lower case English alphabet (i.e. from 'a' to 'z'). -----Subtasks----- Subtask #1 : (20 points) - Sum of |Si| in an input file doesn't exceed 2 * 103 Subtask 2 : (80 points) - Sum of |Si| in an input file doesn't exceed 2 * 106 -----Example----- Input: 3 aba abac abcd Output: YES NO NO -----Explanation----- Example case 1. We can remove the character at position 1 (0-based index) to get "aa" which is a double string. Hence, it is a special dish. Example case 2. It is not possible to remove the character at any of the position to get the double string. Hence, it is not a special dish.
def check(s, l): a = s[: l >> 1] b = s[l >> 1 :] cc = 0 i, j = 0, 0 while cc < 2 and i < l >> 1 and j < l - (l >> 1): if a[i] != b[j]: cc += 1 j += 1 else: i += 1 j += 1 if cc <= 1: return True a = s[: (l >> 1) + 1] b = s[(l >> 1) + 1 :] cc = 0 i, j = 0, 0 while cc < 2 and i < (l >> 1) + 1 and j < l - (l >> 1) - 1: if a[i] != b[j]: cc += 1 i += 1 else: i += 1 j += 1 if cc <= 1: return True return False t = int(input()) for k in range(t): s = input() l = len(s) if l == 1: print("NO") continue if l & 1: if check(s, l): print("YES") else: print("NO") elif s[0 : l >> 1] == s[l >> 1 :]: print("YES") else: print("NO")
FUNC_DEF ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER 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 IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
One day, Chef prepared D brand new dishes. He named the i-th dish by a string Si. After the cooking, he decided to categorize each of these D dishes as special or not. A dish Si is called special if it's name (i.e. the string Si) can be represented in the form of a double string by removing at most one (possibly zero) character from it's name from any position. A string is called a double string if it can be represented as a concatenation of two identical, non-empty strings. e.g. "abab" is a double string as it can be represented as "ab" + "ab" where + operation denotes concatenation. Similarly, "aa", "abcabc" are double strings whereas "a", "abba", "abc" are not. -----Input----- - First line of the input contains an integer D denoting the number of dishes prepared by Chef on that day. - Each of the next D lines will contain description of a dish. - The i-th line contains the name of i-th dish Si. -----Output----- For each of the D dishes, print a single line containing "YES" or "NO" (without quotes) denoting whether the dish can be called as a special or not. -----Constraints----- - 1 ≀ D ≀ 106 - 1 ≀ |Si| ≀ 106. - Each character of string Si will be lower case English alphabet (i.e. from 'a' to 'z'). -----Subtasks----- Subtask #1 : (20 points) - Sum of |Si| in an input file doesn't exceed 2 * 103 Subtask 2 : (80 points) - Sum of |Si| in an input file doesn't exceed 2 * 106 -----Example----- Input: 3 aba abac abcd Output: YES NO NO -----Explanation----- Example case 1. We can remove the character at position 1 (0-based index) to get "aa" which is a double string. Hence, it is a special dish. Example case 2. It is not possible to remove the character at any of the position to get the double string. Hence, it is not a special dish.
sub1 = "" sub2 = "" substr = "" for x in range(int(input())): str = input().strip() m = [(0) for i in range(123)] l = len(str) if l is 1: print("NO") continue for y in range(l): m[int(ord(str[y]))] += 1 count = 0 for y in range(97, 123): if m[y] % 2 is not 0: count += 1 if count is 1: ch1 = chr(y) if count > 1: break if count > 1: print("NO") continue if count is 0: if l % 2 is not 0: print("NO") else: sub1 = str[0 : l // 2] sub2 = str[l // 2 : l] if sub1 == sub2: print("YES") else: print("NO") elif count is 1: flag = 0 for y in range(l): if str[y] is ch1: substr = str[0:y] + str[y + 1 : l] len1 = l - 1 sub1 = substr[0 : len1 // 2] sub2 = substr[len1 // 2 : len1] if sub1 == sub2: print("YES") flag = 1 break if flag is 0: print("NO")
ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING
One day, Chef prepared D brand new dishes. He named the i-th dish by a string Si. After the cooking, he decided to categorize each of these D dishes as special or not. A dish Si is called special if it's name (i.e. the string Si) can be represented in the form of a double string by removing at most one (possibly zero) character from it's name from any position. A string is called a double string if it can be represented as a concatenation of two identical, non-empty strings. e.g. "abab" is a double string as it can be represented as "ab" + "ab" where + operation denotes concatenation. Similarly, "aa", "abcabc" are double strings whereas "a", "abba", "abc" are not. -----Input----- - First line of the input contains an integer D denoting the number of dishes prepared by Chef on that day. - Each of the next D lines will contain description of a dish. - The i-th line contains the name of i-th dish Si. -----Output----- For each of the D dishes, print a single line containing "YES" or "NO" (without quotes) denoting whether the dish can be called as a special or not. -----Constraints----- - 1 ≀ D ≀ 106 - 1 ≀ |Si| ≀ 106. - Each character of string Si will be lower case English alphabet (i.e. from 'a' to 'z'). -----Subtasks----- Subtask #1 : (20 points) - Sum of |Si| in an input file doesn't exceed 2 * 103 Subtask 2 : (80 points) - Sum of |Si| in an input file doesn't exceed 2 * 106 -----Example----- Input: 3 aba abac abcd Output: YES NO NO -----Explanation----- Example case 1. We can remove the character at position 1 (0-based index) to get "aa" which is a double string. Hence, it is a special dish. Example case 2. It is not possible to remove the character at any of the position to get the double string. Hence, it is not a special dish.
for t in range(int(input())): s = list(input()) l = len(s) if l == 1: print("NO") continue if l % 2 == 0: print("YES" if s[: l // 2] == s[l // 2 :] else "NO") else: s1 = s[: l // 2] s2 = s[l // 2 : l] for v, (a, b) in enumerate(zip(s1, s2)): if not a == b: s2.pop(v) break else: s2.pop() if s1 == s2: print("YES") else: s1 = s[: l // 2 + 1] s2 = s[l // 2 + 1 :] for v, (a, b) in enumerate(zip(s1, s2)): if not a == b: s1.pop(v) break else: s1.pop() print("YES" if s1 == s2 else "NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING STRING ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR FOR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR STRING STRING
One day, Chef prepared D brand new dishes. He named the i-th dish by a string Si. After the cooking, he decided to categorize each of these D dishes as special or not. A dish Si is called special if it's name (i.e. the string Si) can be represented in the form of a double string by removing at most one (possibly zero) character from it's name from any position. A string is called a double string if it can be represented as a concatenation of two identical, non-empty strings. e.g. "abab" is a double string as it can be represented as "ab" + "ab" where + operation denotes concatenation. Similarly, "aa", "abcabc" are double strings whereas "a", "abba", "abc" are not. -----Input----- - First line of the input contains an integer D denoting the number of dishes prepared by Chef on that day. - Each of the next D lines will contain description of a dish. - The i-th line contains the name of i-th dish Si. -----Output----- For each of the D dishes, print a single line containing "YES" or "NO" (without quotes) denoting whether the dish can be called as a special or not. -----Constraints----- - 1 ≀ D ≀ 106 - 1 ≀ |Si| ≀ 106. - Each character of string Si will be lower case English alphabet (i.e. from 'a' to 'z'). -----Subtasks----- Subtask #1 : (20 points) - Sum of |Si| in an input file doesn't exceed 2 * 103 Subtask 2 : (80 points) - Sum of |Si| in an input file doesn't exceed 2 * 106 -----Example----- Input: 3 aba abac abcd Output: YES NO NO -----Explanation----- Example case 1. We can remove the character at position 1 (0-based index) to get "aa" which is a double string. Hence, it is a special dish. Example case 2. It is not possible to remove the character at any of the position to get the double string. Hence, it is not a special dish.
def check(x, y): i = 0 j = 0 if len(x) < len(y): x, y = y, x pos = len(x) - 1 while i < len(x) and j < len(y): if x[i] == y[j]: i += 1 j += 1 else: pos = i break del x[pos] if x == y: return True return False t = int(input()) while t: t -= 1 a = list(input()) n = len(a) if n % 2 == 0: if a[: n // 2] == a[n // 2 :]: print("YES") else: print("NO") elif n > 1 and ( check(a[: n // 2], a[n // 2 :]) or check(a[(n + 1) // 2 :], a[: (n + 1) // 2]) ): print("YES") else: print("NO")
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
One day, Chef prepared D brand new dishes. He named the i-th dish by a string Si. After the cooking, he decided to categorize each of these D dishes as special or not. A dish Si is called special if it's name (i.e. the string Si) can be represented in the form of a double string by removing at most one (possibly zero) character from it's name from any position. A string is called a double string if it can be represented as a concatenation of two identical, non-empty strings. e.g. "abab" is a double string as it can be represented as "ab" + "ab" where + operation denotes concatenation. Similarly, "aa", "abcabc" are double strings whereas "a", "abba", "abc" are not. -----Input----- - First line of the input contains an integer D denoting the number of dishes prepared by Chef on that day. - Each of the next D lines will contain description of a dish. - The i-th line contains the name of i-th dish Si. -----Output----- For each of the D dishes, print a single line containing "YES" or "NO" (without quotes) denoting whether the dish can be called as a special or not. -----Constraints----- - 1 ≀ D ≀ 106 - 1 ≀ |Si| ≀ 106. - Each character of string Si will be lower case English alphabet (i.e. from 'a' to 'z'). -----Subtasks----- Subtask #1 : (20 points) - Sum of |Si| in an input file doesn't exceed 2 * 103 Subtask 2 : (80 points) - Sum of |Si| in an input file doesn't exceed 2 * 106 -----Example----- Input: 3 aba abac abcd Output: YES NO NO -----Explanation----- Example case 1. We can remove the character at position 1 (0-based index) to get "aa" which is a double string. Hence, it is a special dish. Example case 2. It is not possible to remove the character at any of the position to get the double string. Hence, it is not a special dish.
f = 0 t = int(input()) while t > 0: t -= 1 o = 0 s = input() s = list(s) n = len(s) if len(s) == 1: print("NO") continue if len(s) % 2 == 1: for i in range(n): ls = s.copy() ls.pop(i) if ls[0 : int(len(ls) / 2)] == ls[int(len(ls) / 2) : int(len(ls))]: print("YES") o = 1 break if o == 0: print("NO") elif s[0 : int(len(s) / 2)] == s[int(len(s) / 2) : int(len(s))]: print("YES") else: print("NO")
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
from sys import stdin, stdout n = int(stdin.readline()) arr = list(map(int, stdin.readline().split())) ma_x = 0 c = 0 for i in arr: if i > ma_x: ma_x = i if i > 0: c += 1 if c == 1: ma_x = 0 ans = 0 for i in range(1, ma_x + 1): s = 0 end = 0 while end < n: s += arr[end] if arr[end] > i or s <= 0: s = 0 start = end + 1 ans = max(ans, s - i) end += 1 stdout.write(str(ans) + "\n")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
def maxSubArraySum(a, size): j = 30 ans = 0 while j >= 1: max1 = 0 max_so_far = 0 max_ending_here = 0 for i in range(size): if a[i] > j: max_ending_here = 0 elif a[i] <= j: max_ending_here = max_ending_here + a[i] max1 = max(max1, a[i]) if max_ending_here < 0: max_ending_here = 0 elif max_so_far < max_ending_here: max_so_far = max_ending_here j -= 1 ans = max(ans, max_so_far - max1) return ans n = int(input()) num1 = list(map(int, input().split())) print(maxSubArraySum(num1, n))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
def algo(i, j, arr, y): ans = maxi = t = 0 for x in range(i, j + 1): maxi += arr[x] if maxi < 0: t = x + 1 maxi = 0 else: ans = max(ans, maxi - y) return ans n = int(input()) arr = list(map(int, input().split())) d = [[] for _ in range(1, 31)] for i in range(n): if arr[i] > 0: d[arr[i] - 1].append(i) ans = 0 for t in range(1, 31): se = set() for rr in d[t - 1]: if rr not in se: se.add(rr) i = 0 fl = 0 for i in range(rr - 1, -1, -1): if arr[i] > t: fl = 1 break se.add(i) i += fl j = n - 1 fl = 0 for j in range(rr + 1, n): if arr[j] > t: fl = 1 break se.add(j) j -= fl ans = max(ans, algo(i, j, arr, t)) print(ans)
FUNC_DEF ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
MAX_VAL = 61 n = int(input()) lst = list(map(int, input().split(" "))) msum = [[(-10000000) for i in range(MAX_VAL)] for i in range(n)] ans = -10000000 for i in reversed(range(n)): for j in range(lst[i], 31): msum[i][j] = lst[i] if i == n - 1 else max(msum[i + 1][j] + lst[i], lst[i]) ans = max(ans, msum[i][j] - j) print(int(ans))
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
def rs(): return input().strip() def ri(): return int(input()) def ria(): return list(map(int, input().split())) def ia_to_s(a): return " ".join([str(s) for s in a]) def solve(n, a): ans = 0 for i in range(31): s = 0 for j in a: if j <= i: s = max(0, s + j) else: s = 0 ans = max(ans, s - i) return ans def main(): n = ri() a = ria() print(solve(n, a)) main()
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 RETURN FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
import sys input = sys.stdin.readline flush = sys.stdout.flush def buildM(): for i in reversed(range(1, n)): segM[i] = max(segM[2 * i], segM[2 * i + 1]) return def buildS(): for i in reversed(range(1, n + 1)): segSM[i] = max(segSM[2 * i], segSM[2 * i + 1]) segSm[i] = min(segSm[2 * i], segSm[2 * i + 1]) return def queryM(l, r): res = -31 l += n r += n while l < r: if l & 1: res = max(res, segM[l]) l += 1 if r & 1: r -= 1 res = max(res, segM[r]) l >>= 1 r >>= 1 return res def querySM(l, r): res = -30 * 10**5 - 1 l += n + 1 r += n + 1 while l < r: if l & 1: res = max(res, segSM[l]) l += 1 if r & 1: r -= 1 res = max(res, segSM[r]) l >>= 1 r >>= 1 return res def querySm(l, r): res = 30 * 10**5 + 1 l += n + 1 r += n + 1 while l < r: if l & 1: res = min(res, segSm[l]) l += 1 if r & 1: r -= 1 res = min(res, segSm[r]) l >>= 1 r >>= 1 return res n = int(input()) a = list(map(int, input().split())) S = [0] * (n + 1) for i in range(n): S[i + 1] = S[i] + a[i] segM = [0] * n + a segSM = [0] * (n + 1) + S segSm = [0] * (n + 1) + S buildM() buildS() ans = -(10**9) for i, x in enumerate(a): score = 0 l = 0 r = i while l < r: m = l + r >> 1 if queryM(m, i + 1) == x: r = m else: l = m + 1 ll = l l = i r = n - 1 while l < r: m = (l + r >> 1) + 1 if queryM(i, m + 1) == x: l = m else: r = m - 1 rr = r score += S[i] - querySm(ll, i + 1) score += querySM(i + 1, rr + 2) - S[i + 1] if score > ans: ans = score print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN FUNC_DEF ASSIGN VAR NUMBER VAR VAR 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 VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
n = int(input()) a = list(map(int, input().split())) ans = 0 for i in range(-30, 31): b = [(int(-1000000000.0) if x > i else x) for x in a] dp = b.copy() for j in range(1, n): dp[j] = max(dp[j - 1] + b[j], b[j]) ans = max(ans, max(dp) - i) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
def max_sum(arr): n = len(arr) dp = [(0) for i in range(n)] dp[0] = arr[0] for i in range(1, n): dp[i] = max(dp[i - 1] + arr[i], arr[i]) return max(dp) n = int(input()) l = input().split() li = [int(i) for i in l] maxa = 0 for mx in range(-30, 31): copy = list(li) for i in range(n): if copy[i] > mx: copy[i] = -1000000000000 maxa = max(max_sum(copy) - mx, maxa) print(maxa)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
def mcss(l, r): if l > r: return 0 s = 0 ret = 0 for i in range(l, r + 1): s += aa[i] ret = max(ret, s) s = max(s, 0) return ret n = int(input()) aa = list(map(int, input().split())) ans = 0 for maxa in range(1, 31): l = 0 for r, a in enumerate(aa): if a > maxa: ans = max(ans, mcss(l, r - 1) - maxa) l = r + 1 ans = max(ans, mcss(l, n - 1) - maxa) print(ans)
FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
from sys import stdin inp = lambda: stdin.readline().strip() n = int(inp()) a = [int(x) for x in inp().split()] ans = 0 for maximum in range(31): curr = 0 best = 0 for i in range(n): if a[i] <= maximum: val = a[i] else: val = -100000000000 curr += val best = min(best, curr) ans = max(ans, curr - best - maximum) print(ans)
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
n = int(input()) a = [int(x) for x in input().split()] a = [0] + a f = [None for i in range(n + 1)] g = [(0) for i in range(n + 1)] ans = 0 for m in range(-30, 31): for i in range(1, n + 1): g[i] = 0 if a[i] > m else max(0, g[i - 1] + a[i]) if f[i - 1] is None: f[i] = None if a[i] != m else m + g[i - 1] elif a[i] == m: f[i] = g[i - 1] + m elif a[i] < m: f[i] = f[i - 1] + a[i] else: f[i] = None for i in range(1, n + 1): if f[i] is not None: ans = max(ans, f[i] - m) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NONE VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER NONE ASSIGN VAR VAR VAR VAR VAR NONE BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NONE FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NONE ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
import sys input = sys.stdin.readline n = int(input()) a = list(map(int, input().split())) INF = 10**9 ans = -INF for mx in range(31): b = [0] * n for i in range(n): if a[i] > mx: b[i] = -INF else: b[i] = a[i] dp = [0] * n dp[0] = max(b[0], 0) for i in range(1, n): dp[i] = max(0, dp[i - 1] + b[i], b[i]) res = max(dp) - mx ans = max(ans, res) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
n = int(input()) a = list(map(int, input().split())) if max(a) <= 0: exit(print(0)) ans = [0] * 31 for i in range(1, 31): mi = su = 0 for x in a: if x > i: mi = anss = 0 continue su += x ans[i] = max(ans[i], su - mi) mi = min(mi, su) print(max(ans[i] - i for i in range(31)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR NUMBER
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
q = int(input()) w = list(map(int, input().split())) ans = 0 for i in range(-30, 31): a = [(-1000000000.0 if x > i else x) for x in w] m = 0 ma = 0 for j in a: m += j if ma < m: ma = m elif m < 0: m = 0 ans = max(ans, ma - i if i > 0 else ma) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
from sys import stdin input = stdin.readline def main(): n = int(input()) l = [int(i) for i in input().split(" ")] maxi = 0 for i in range(1, 31): if i in l: score = 0 for j in l: if j <= i: score += j maxi = max(maxi, score - i) score = max(score, 0) else: score = 0 print(maxi) main()
ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
import sys mod = 1000000007 eps = 10**-9 def main(): import sys input = sys.stdin.readline class Bit: def __init__(self, n): self.size = n self.tree = [0] * (n + 1) def sum(self, i): s = 0 while i > 0: s += self.tree[i] i -= i & -i return s def add(self, i, x): while i <= self.size: self.tree[i] += x i += i & -i def lower_bound(self, w): if w <= 0: return 0 x = 0 k = 1 << self.size.bit_length() - 1 while k: if x + k <= self.size and self.tree[x + k] < w: w -= self.tree[x + k] x += k k >>= 1 return x + 1 class SegmentTree: def __init__(self, A, initialize=True, segfunc=min, 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 = int(input()) A = list(map(int, input().split())) cs = [0] * (N + 1) B = [] for i, a in enumerate(A): cs[i + 1] = cs[i] + A[i] B.append((a, i + 1)) B.sort(key=lambda x: x[0], reverse=True) ma = SegmentTree(cs, segfunc=max, ident=-2000000000) mi = SegmentTree(cs) bit = Bit(N) ans = 0 for a, i in B: if a <= 0: break bit.add(i, 1) k = bit.sum(i) r = bit.lower_bound(k + 1) l = bit.lower_bound(k - 1) ans = max(ans, ma.query(i + 1, r + 1) - mi.query(l + 1, i + 2) - a) print(ans) main()
IMPORT ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF IMPORT ASSIGN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR NUMBER WHILE VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER RETURN BIN_OP VAR NUMBER 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 FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
import sys input = sys.stdin.buffer.readline def print(val): sys.stdout.write(str(val) + "\n") i_kart = int(input()) lista = [int(x) for x in input().split()] wynik = 0 for i in range(31): if lista[0] > i: max_local = max_total = 0 else: max_local = max_total = lista[0] for x in lista[1:]: if x > i: continue max_local = max(x, max_local + x) max_total = max(max_total, max_local) wynik = max(wynik, max_total - i) print(wynik)
IMPORT ASSIGN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
n = int(input()) l = list(map(int, input().split())) d = set(l) ans = 0 tot = 0 for j in range(31): if j not in d: continue tot = -999999999999999999999 cur = 0 for i in range(n): if l[i] > j: cur = 0 continue else: cur += l[i] if cur < 0: cur = 0 tot = max(tot, cur) ans = max(tot - j, ans) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
n = int(input()) a = list(map(int, input().split())) s = 0 ss = [0] * 31 ss_ind = [0] * 31 met = [False] * 31 sums = [a[0]] for i in range(1, n): sums.append(sums[-1] + a[i]) ans = 0 for i in range(n): if a[i] >= 0: met[a[i]] = True for j in range(31): if a[i] > j: ss[j] = 0 met[j] = False elif ss[j] + a[i] < 0: ss[j] = 0 met[j] = False else: ss[j] += a[i] if met[j]: ans = max(ans, ss[j] - j) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
def max_subarray(li, x): if len(li) == 0: return 0 li = [0] + li min_so_far = [0] index = -1 ans = 0 for p in range(1, len(li)): if li[p] == x: index = p li[p] += li[p - 1] if index != -1: ans = max(ans, li[p] - min_so_far[index - 1]) min_so_far.append(min(min_so_far[p - 1], li[p])) return max(0, ans - x) t = 1 for _ in range(t): n = int(input()) s = list(map(int, input().split())) l = [] for i in range(31): l.append([-1]) for i in range(n): for j in range(1, s[i]): l[j].append(i) for j in range(1, 31): l[j].append(n) ans = 0 for i in range(30, 0, -1): for j in range(1, len(l[i])): p = max_subarray(s[l[i][j - 1] + 1 : l[i][j]], i) ans = max(ans, p) print(ans)
FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
from sys import stdin input = stdin.readline def main(): test = 1 for _ in range(test): n = int(input()) ara = [int(x) for x in input().split()] ans = 0 for mx in range(min(ara), max(ara) + 1): sum = 0 for x in ara: sum += x if x > mx or sum < 0: sum = 0 continue ans = max(ans, sum - mx) print(ans) main()
ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
from sys import stdin input = stdin.buffer.readline n = int(input()) (*a,) = map(int, input().split()) ans = 0 for i in range(31): s = 0 for j in a: if j <= i: s = max(0, s + j) else: s = 0 ans = max(ans, s - i) print(ans)
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
from sys import stdin, stdout def yet_another_yet_another_task(a): n = len(a) preSum = [(0) for i in range(n)] postSum = [(0) for i in range(n)] preSum[0] = a[0] postSum[n - 1] = a[n - 1] for i in range(1, n): preSum[i] = preSum[i - 1] + a[i] for i in range(n - 2, -1, -1): postSum[i] = postSum[i + 1] + a[i] preSumSegmentTree = SegmentTree(0, n - 1, preSum) postSumSegmentTree = SegmentTree(0, n - 1, postSum) st = [] leftSum = [(0) for i in range(n)] for i in range(n): j = i while len(st) > 0 and st[-1][0] <= a[i]: j = st.pop()[1] st.append([a[i], j]) leftSum[i] = postSumSegmentTree.query(j, i) - postSum[i] st = [] rightSum = [(0) for i in range(n)] for i in range(n - 1, -1, -1): j = i while len(st) > 0 and st[-1][0] <= a[i]: j = st.pop()[1] st.append([a[i], j]) rightSum[i] = preSumSegmentTree.query(i, j) - preSum[i] res = 0 for i in range(n): res = max(res, leftSum[i] + rightSum[i]) return res class SegmentTree: def __init__(self, i, j, sum): self.l = i self.r = j m = (i + j) // 2 if i == j: self.maxVal = sum[i] return if i > j: return -1000000000 self.lst = SegmentTree(i, m, sum) self.rst = SegmentTree(m + 1, j, sum) if self.lst is not None and self.rst is not None: self.maxVal = max(self.lst.maxVal, self.rst.maxVal) elif self.lst is None: self.maxVal = self.rst.maxVal elif self.rst is None: self.maxVal = self.lst.maxVal def query(self, i, j): if j < self.l or self.r < i: return -1000000000 if i <= self.l and self.r <= j: return self.maxVal ql = -1000000000 qr = -1000000000 if self.lst is not None: ql = self.lst.query(i, j) if self.rst is not None: qr = self.rst.query(i, j) return max(ql, qr) def yet_another_yet_another_task2(a): res = 0 for i in range(1, 31): mxr = 0 mnr = 0 for j in a: if j > i: mxr = 0 mnr = 0 else: mxr += j mnr = min(mxr, mnr) res = max(res, mxr - mnr - i) return res n = int(stdin.readline()) a = list(map(int, stdin.readline().split())) stdout.write(str(yet_another_yet_another_task(a)) + "\n")
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR RETURN IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF VAR NONE VAR NONE ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR STRING
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
import sys from itertools import accumulate 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] class SparseTableMax: def __init__(self, aa): w = len(aa) h = w.bit_length() table = [aa] + [([-1] * w) for _ in range(h - 1)] tablei1 = table[0] for i in range(1, h): tablei = table[i] for j in range(w - (1 << i) + 1): rj = j + (1 << i - 1) tablei[j] = max(tablei1[j], tablei1[rj]) tablei1 = tablei self.table = table def max(self, l, r): i = (r - l).bit_length() - 1 tablei = self.table[i] Lmax = tablei[l] Rmax = tablei[r - (1 << i)] if Lmax > Rmax: Rmax = Lmax return Rmax class SparseTableMin: def __init__(self, aa): w = len(aa) h = w.bit_length() table = [aa] + [([-1] * w) for _ in range(h - 1)] tablei1 = table[0] for i in range(1, h): tablei = table[i] for j in range(w - (1 << i) + 1): rj = j + (1 << i - 1) tablei[j] = min(tablei1[j], tablei1[rj]) tablei1 = tablei self.table = table def min(self, l, r): i = (r - l).bit_length() - 1 tablei = self.table[i] Lmin = tablei[l] Rmin = tablei[r - (1 << i)] if Lmin < Rmin: Rmin = Lmin return Rmin def main(): inf = 10**16 n = II() aa = LI() ll = [0] * n stack = [] for i, a in enumerate(aa): while stack and stack[-1][-1] <= a: stack.pop() if stack: ll[i] = stack[-1][0] + 1 stack.append((i, a)) rr = [n] * n stack = [] for i in range(n - 1, -1, -1): a = aa[i] while stack and stack[-1][-1] <= a: stack.pop() if stack: rr[i] = stack[-1][0] stack.append((i, a)) cs = [0] + [s for s in accumulate(aa)] mn = SparseTableMin(cs) mx = SparseTableMax(cs) ans = -inf for i in range(n): l, r = ll[i], rr[i] cur = mx.max(i + 1, r + 1) - mn.min(l, i + 1) - aa[i] if cur > ans: ans = cur print(ans) main()
IMPORT 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 CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR IF VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR IF VAR ASSIGN VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
a = int(input()) z = list(map(int, input().split())) res = 0 ans = 0 for i in range(1, 31): res = 0 for j in range(len(z)): res += z[j] if z[j] > i: res = 0 res = max(res, 0) ans = max(ans, res - i) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
def solution(a): ans = 0 for mx in range(31): cum_sum = 0 cum_sum_min = 0 for val in a: cum_sum += -10000000.0 if val > mx else val cum_sum_min = min(cum_sum_min, cum_sum) ans = max(ans, cum_sum - cum_sum_min - mx) return ans n = int(input()) a = list(map(int, input().split())) print(int(solution(a)))
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
n = int(input()) l = list(map(int, input().split())) m = 0 for x in range(1, 31): s = 0 for i in range(n): if l[i] > x: s = 0 else: s += l[i] m = max(m, s - x) if s < 0: s = 0 print(m)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
import sys input = sys.stdin.readline n = int(input()) a = list(map(int, input().split())) mx = 0 cur = [0] * 30 for i in range(30): cur[i] = [0, 0] for i in a: for j in range(30): if i == j + 1 and cur[j][1] == 0: cur[j][1] = i cur[j][0] -= i cur[j][0] += i if i > j + 1: cur[j][0] = 0 cur[j][1] = 0 if cur[j][0] < 0: if cur[j][1] == 0: cur[j][0] = 0 elif cur[j][0] < -j: cur[j][0] = 0 cur[j][1] = 0 if cur[j][0] > mx and cur[j][1] > 0: mx = cur[j][0] print(mx)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR LIST NUMBER NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
n = int(input()) l = list(map(int, input().split())) ans = 0 s = set() for i in l: if i > 0: s.add(i) for i in s: now = 0 for j in l: if j > i: now -= float("INF") else: now += j if now <= 0: now = 0 else: ans = max(ans, now - i) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR FUNC_CALL VAR STRING VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
N = int(input()) A = [int(_) for _ in input().split()] dp = [([0] * 63) for _ in range(N + 1)] dp[0][A[0] + 30] = A[0] answer = 0 for i in range(1, N): el = A[i] iel = el + 30 dp[i][iel] = max(dp[i][iel], el) answer = max(answer, dp[i][iel] - max(el, 0)) for m in range(-30, 31): im = m + 30 if el > m: dp[i][iel] = max(dp[i][iel], dp[i - 1][im] + el) answer = max(answer, dp[i][iel] - max(el, 0)) elif dp[i - 1][im] > 0: dp[i][im] = max(dp[i][im], dp[i - 1][im] + el) answer = max(answer, dp[i][im] - max(m, 0)) print(answer)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
import sys input = sys.stdin.readline n = int(input()) a = list(map(int, input().split())) ans = 0 for M in range(-30, 31): acc = 0 min_acc = 0 for ai in a: acc += ai if ai <= M else -(10**18) ans = max(ans, acc - min_acc - M) min_acc = min(min_acc, acc) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
INF = int(1000000000.0) def kadane(temp): sm, t = -INF, -INF for x in temp: t = max(x, x + t) sm = max(sm, t) return sm n = int(input()) arr = [int(x) for x in input().split()] ans = 0 maxes = set([x for x in arr if x > 0]) for mx in maxes: temp = [] for ind, val in enumerate(arr): if val > mx: temp.append(-INF) else: temp.append(val) ans = max(ans, kadane(temp) - mx) print(ans)
ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
from itertools import groupby n = int(input()) a = list(map(int, input().split())) INF = 100000000000000 dp = [([-INF] * 61) for i in range(n)] ans = -INF for i in range(n): if i == 0: dp[i][a[i] + 30] = 0 continue dp[i][a[i] + 30] = 0 for j in range(61): if dp[i - 1][j] == -INF: continue if j <= a[i] + 30: dp[i][a[i] + 30] = max( dp[i][a[i] + 30], dp[i - 1][j] + a[i] - (a[i] + 30 - j) ) else: dp[i][j] = dp[i - 1][j] + a[i] ans = max(ans, max(dp[i])) print(max(ans, 0))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
from sys import gettrace, stdin if not gettrace(): def input(): return next(stdin)[:-1] def main(): n = int(input()) aa = [int(a) for a in input().split()] mlss = [0] * n dp = [[0] * 31] for a in aa[:-1]: dp.append([0] * 31) for i in range(max(0, a), 31): dp[-1][i] = max(0, dp[-2][i] + a) for i in range(1, n): mlss[i] = dp[i][max(aa[i], 0)] mrss = [0] * n dp = [[0] * 31] for a in aa[-1:0:-1]: dp.append([0] * 31) for i in range(max(0, a), 31): dp[-1][i] = max(0, dp[-2][i] + a) dp.reverse() for i in range(n - 2, -1, -1): mrss[i] = dp[i][max(aa[i], 0)] best = 0 for a, mls, mrs in zip(aa, mlss, mrss): best = max(best, mls + mrs) print(best) main()
IF FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST BIN_OP LIST NUMBER NUMBER FOR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST BIN_OP LIST NUMBER NUMBER FOR VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
import sys input = sys.stdin.readline N = int(input()) A = list(map(int, input().split())) INF = 10**12 ans = -INF dp = [-INF] * 62 for i, a in enumerate(A): ndp = [-INF] * 62 ndp[a] = max(0, dp[a] + a) for k in range(-30, a): ndp[a] = max(ndp[a], dp[k] + k) for k in range(a + 1, 31): ndp[a] = max(ndp[a], dp[k] + a) ans = max(ans, ndp[a]) for k in range(a + 1, 31): ndp[k] = dp[k] + a ans = max(ans, ndp[k]) dp = ndp print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
import sys from itertools import accumulate input = sys.stdin.readline n = int(input()) a = [0] + list(map(int, input().split())) acc = list(accumulate(a)) mxls = [0] ans = 0 l = 0 acmn = 1000000000 acmnls = [acmn] * (n + 1) for i in range(1, n + 1): if mxls[i - 1] < a[i]: t = i mxls.append(a[i]) while t >= 2 and mxls[t - 1] < a[i]: mxls[t - 1] = a[i] t -= 1 acmn = acmnls[t - 1] for j in range(t, i + 1): acmn = min(acmn, acc[j - 1] + mxls[j]) acmnls[j] = acmn else: mxls.append(a[i]) acmn = min(acmn, acc[i - 1] + mxls[i]) acmnls[i] = acmn ans = max(ans, acc[i] - acmn) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
n = int(input()) a = [int(x) for x in input().split()] mx = 1 ans = 0 while mx <= 30: loc = 0 for x in a: if x > mx: loc = 0 continue loc += x if loc < 0: loc = 0 ans = max(ans, loc - mx) mx += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
import sys from itertools import accumulate input = sys.stdin.readline class SegTree: def __init__(self, init_val, n, ide_ele, seg_func): self.segfunc = seg_func self.num = 2 ** (n - 1).bit_length() self.ide_ele = ide_ele self.seg = [self.ide_ele] * 2 * self.num for i in range(n): self.seg[i + self.num - 1] = init_val[i] for i in range(self.num - 2, -1, -1): self.seg[i] = self.segfunc(self.seg[2 * i + 1], self.seg[2 * i + 2]) def update(self, k, x): k += self.num - 1 self.seg[k] = x while k + 1: k = (k - 1) // 2 self.seg[k] = self.segfunc(self.seg[k * 2 + 1], self.seg[k * 2 + 2]) def query(self, p, q): if q <= p: return self.ide_ele p += self.num - 1 q += self.num - 2 res = self.ide_ele while q - p > 1: if p & 1 == 0: res = self.segfunc(res, self.seg[p]) if q & 1 == 1: res = self.segfunc(res, self.seg[q]) q -= 1 p = p // 2 q = (q - 1) // 2 if p == q: res = self.segfunc(res, self.seg[p]) else: res = self.segfunc(self.segfunc(res, self.seg[p]), self.seg[q]) return res n = int(input()) xs = [0] + list(map(int, input().split())) X = list(accumulate(xs)) xis = [(i, x) for i, x in enumerate(xs)] xis = sorted(xis, key=lambda x: x[1])[::-1] bx = None tmp = [] F = [(0, n + 1)] * (n + 1) sti1 = SegTree([0] * (n + 1), n + 1, -1, max) sti2 = SegTree([n + 1] * (n + 1), n + 1, 10**18, min) for i, x in xis: if bx == x: pass else: for j in tmp: sti1.update(j, j) sti2.update(j, j) tmp = [] F[i] = sti1.query(0, i), sti2.query(i, n + 1) tmp.append(i) bx = x st1 = SegTree(X, n + 1, -(10**18), max) st2 = SegTree(X, n + 1, 10**18, min) R = 0 for i in range(1, n + 1): k, l = F[i] R = max(R, st1.query(i, l) - st2.query(k, i) - xs[i]) print(R)
IMPORT ASSIGN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR WHILE BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF IF VAR VAR RETURN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NONE ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER NUMBER VAR FOR VAR VAR VAR IF VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
def solve_excluding(a, u): best_sum = 0 current_sum = 0 seen = False seen_at_some_point = False for el in a: if el > u or current_sum + el < 0: current_sum = 0 seen = False else: current_sum += el if el == u: seen = True if current_sum >= best_sum and seen: best_sum = current_sum seen_at_some_point = True return seen_at_some_point, best_sum n = int(input()) a = [int(x) for x in input().split()] best = 0 for u in range(-30, 31): seen, total = solve_excluding(a, u) if seen: total -= u if total > best: best = total print(best)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
import sys input = sys.stdin.readline n = int(input()) a = list(map(int, input().split())) ans = 0 for M in range(-30, 31): b = a[:] for i in range(n): if b[i] > M: b[i] = -(10**18) acc = 0 min_acc = 0 for bi in b: acc += bi ans = max(ans, acc - min_acc - M) min_acc = min(min_acc, acc) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
n = int(input()) a = list(map(int, input().split())) ans = 0 for m in range(1, 31): now = 0 for v in a: if v > m: now = 0 continue now = max(now + v, 0) ans = max(ans, now - m) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
def findMax(a, cand, prefix): heap = [] minPrefix = 0 result = float("-inf") maxSeen = float("-inf") for i in range(1, len(prefix)): maxSeen = max(maxSeen, a[i - 1]) if maxSeen > cand: maxSeen = float("-inf") minPrefix = i else: if maxSeen == cand: result = max(result, prefix[i] - prefix[minPrefix] - maxSeen) if prefix[i] < prefix[minPrefix]: minPrefix = i return result n = int(input()) a = [int(x) for x in input().split()] prefix = [0] for val in a: prefix.append(prefix[-1] + val) result = 0 for cand in range(-30, 31): result = max(result, findMax(a, cand, prefix)) print(result)
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
import sys input = sys.stdin.buffer.readline def I(): return list(map(int, input().split())) def sieve(n): a = [1] * n for i in range(2, n): if a[i]: for j in range(i * i, n, i): a[j] = 0 return a n = int(input()) arr = I() ans = 0 for i in range(-32, 33): maxs = -33 f = 0 currmax = -33 for j in range(n): if arr[j] == i: f = 1 if arr[j] > i: currmax = -33 continue currmax = max(arr[j] + currmax, arr[j]) maxs = max(currmax, maxs) if f: ans = max(maxs - i, ans) print(ans)
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
n = int(input()) A = list(map(int, input().split())) s = max(A) ans = 0 for val in range(s, 0, -1): mv, cv = 0, 0 for i in range(n): if A[i] <= val: cv = cv + A[i] mv = max(mv, cv) if cv < 0: cv = 0 else: cv = 0 ans = max(mv - val, ans) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
n = int(input()) cards = [int(x) for x in input().split()] possibleVals = set() for x in cards: possibleVals.add(x) ans = -999999999 for v in possibleVals: segmentHasV = True if v == cards[0] else False maxContiguousSum = cards[0] if maxContiguousSum > v: maxContiguousSum = -9999999999 if segmentHasV: ans = max(ans, maxContiguousSum - v) for i in range(1, len(cards)): e = cards[i] if e == v: segmentHasV = True if e > v: e = -9999999999 maxContiguousSum = max(maxContiguousSum + e, e) if segmentHasV == True: ans = max(ans, maxContiguousSum - v) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
n = int(input()) arr = list(map(int, input().split())) ans = 0 for i in range(1, 31): mu_now = 0 fin = 0 for j in range(n): if arr[j] > i: fin = 0 mu_now = 0 continue mu_now += arr[j] if arr[j] == i: fin = 1 mu_now = max(mu_now, 0) if fin: ans = max(ans, mu_now - i) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
import sys input = sys.stdin.buffer.readline n = int(input()) ls = list(map(int, input().split())) m = max(ls) def get_max(ls): ma = [([0] * n) for _ in range(m + 1)] for i in range(1, m + 1): mx = 0 for j, u in enumerate(ls): mx += u if mx <= 0 or u > i: mx = 0 ma[i][j] = mx return ma ml, mr = get_max(ls), get_max(list(reversed(ls))) mr = [list(reversed(u)) for u in mr] mx = 0 for i, u in enumerate(ls): if u <= 0: continue lv = ml[u][i - 1] if i > 0 else 0 rv = mr[u][i + 1] if i < n - 1 else 0 mx = max(mx, lv + rv) print(mx)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
import sys input = sys.stdin.readline n = int(input()) l = input().split() li = [int(i) for i in l] maxa = -(10**9) for i in range(-32, 32): l = [] for j in range(n): if li[j] <= i: l.append(li[j]) else: if l == []: continue dp = [(0) for ok in range(len(l))] dp[0] = l[0] for k in range(1, len(l)): dp[k] = max(dp[k - 1] + l[k], l[k]) z = max(dp) maxa = max(z - i, maxa) l = [] if l != []: dp = [(0) for ok in range(len(l))] dp[0] = l[0] for k in range(1, len(l)): dp[k] = max(dp[k - 1] + l[k], l[k]) z = max(dp) maxa = max(z - i, maxa) l = [] print(maxa)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR LIST IF VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
from sys import stdin input = stdin.buffer.readline n = int(input()) aa = [int(i) for i in input().split()] ans = -(10**9 + 7) for i in range(0, 31): msf = 0 for j in range(0, n): if aa[j] <= i: msf += aa[j] msf = max(msf, 0) ans = max(ans, msf - i) else: msf = 0 print(ans)
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
def solve(): n = int(input()) l = list(map(int, input().split())) count = 0 for i in l: if i > 0: count += 1 if count <= 1: print(0) return arr = [0] * 31 for i in range(30, 0, -1): a = 0 b = 0 sub = 0 for y in l: if y > i: a = 0 b = 0 continue b += y if sub < b - a: sub = b - a if b < a: a = b arr[i] = sub print(max([(arr[i] - i) for i in range(31)])) solve()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
def kandane(a): best = -(10**10) current = 0 for i in range(len(a)): if current >= 0: current += a[i] else: current = a[i] best = max(current, best) return best n = int(input()) arr = [int(i) for i in input().split()] ans = 0 for pot_max in range(1, 31): barr = [(a if a <= pot_max else -10000000) for a in arr] ans = max(ans, kandane(barr) - pot_max) print(ans)
FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
import sys def I(): return sys.stdin.readline().rstrip() def f(l, st, en): m = -1 for i in range(st, en): m = max(m, l[i]) if m <= 0: return 0 s = 0 ms = 0 for i in range(st, en): x = l[i] s += x ms = max(ms, s) s = max(s, 0) ans = max(ms - m, 0) le = st ri = st - 1 while le < en: le = ri + 1 ri = le while ri < en and l[ri] < m: ri += 1 if le < ri - 1: ans = max(ans, f(l, le, ri)) return ans n = int(I()) print(f(list(map(int, I().split())), 0, n))
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER VAR
Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. First, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) β€” the number of cards. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 30$) β€” the values on the cards. -----Output----- Print a single integer β€” the final score of the game. -----Examples----- Input 5 5 -2 10 -1 4 Output 6 Input 8 5 2 5 3 -30 -30 6 9 Output 10 Input 3 -10 6 -15 Output 0 -----Note----- In the first example Alice chooses a segment $[1;5]$ β€” the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$. In the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$. In the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$.
n = int(input()) ar = list(map(int, input().split())) ans = 0 for i in range(0, 31): a, b = 0, 0 for e in ar: e = e if e <= i else -float("inf") a = max(e, a + e) b = max(a, b) ans = max(ans, b - i) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR