### 001 ```python n = int(input()) C = 10 ** 9 + 7 a = input() s = input() d = [[0, 0, 0, 1]] for q in range(n): d.append(d[-1][:]) if a[q] != '?' and s[q] != '?': if int(a[q]) < int(s[q]): d[-1][1] = d[-1][3] = 0 d[-1][0] += d[-2][3] d[-1][2] += d[-2][1] elif int(a[q]) > int(s[q]): d[-1][0] = d[-1][3] = 0 d[-1][1] += d[-2][3] d[-1][2] += d[-2][0] elif s[q] != '?': d[-1][2] *= 10 d[-1][0] += d[-2][3] d[-1][1] += d[-2][3] d[-1][0] *= int(s[q]) + 1 d[-1][1] *= 10 - int(s[q]) d[-1][0] -= d[-2][3] d[-1][1] -= d[-2][3] d[-1][2] += d[-2][0] * (9 - int(s[q])) + d[-2][1] * int(s[q]) elif a[q] != '?': d[-1][2] *= 10 d[-1][0] += d[-2][3] d[-1][1] += d[-2][3] d[-1][1] *= int(a[q]) + 1 d[-1][0] *= 10 - int(a[q]) d[-1][0] -= d[-2][3] d[-1][1] -= d[-2][3] d[-1][2] += d[-2][1] * (9 - int(a[q])) + d[-2][0] * int(a[q]) else: d[-1][3] *= 10 d[-1][2] *= 100 d[-1][0] *= 55 d[-1][1] *= 55 d[-1][0] += d[-2][3] * 45 d[-1][1] += d[-2][3] * 45 d[-1][2] += d[-2][0] * 45 + d[-2][1] * 45 d[-1][0] %= C d[-1][1] %= C d[-1 - 1][2] %= C d[-1][3] %= C print(d[-1][2]) ``` ### 002 ```python from collections import Counter from bisect import bisect, bisect_left n, k = map(int, input().split()) a = list(map(int, input().split())) a = Counter(a) c = sorted(Counter(a).items()) keys = sorted(Counter(a).keys(), reverse=True) m = len(c) l_cum = [c[0][0] * c[0][1]] l_cnt = [c[0][1]] for i in range(1, m): l_cum.append(l_cum[-1] + c[i][0] * c[i][1]) l_cnt.append(l_cnt[-1] + c[i][1]) r_cum = [0 for _ in range(m)] r_cum[-1] = c[-1][0] * c[-1][1] r_cnt = [0 for _ in range(m)] r_cnt[-1] = c[-1][1] for i in range(m - 2, -1, -1): r_cum[i] = r_cum[i + 1] + c[i][0] * c[i][1] r_cnt[i] = r_cnt[i + 1] + c[i][1] ans = 10 ** 20 for i in range(m): c_num = a[keys[i]] if c_num >= k: ans = 0 break if i > 0: l_res = (keys[i] - 1) * l_cnt[i - 1] - l_cum[i - 1] l_num = l_cnt[i - 1] else: l_res = 10 ** 30 l_num = 0 if i < m - 1: r_res = r_cum[i + 1] - (keys[i] + 1) * r_cnt[i + 1] r_num = r_cnt[i + 1] else: r_res = 10 ** 30 r_num = 0 if c_num + l_num < k and c_num + r_num < k: ans = min(ans, l_res + r_res + k - c_num) elif c_num + l_num < k: ans = min(ans, r_res + k - c_num) elif c_num + r_num < k: ans = min(ans, l_res + k - c_num) else: ans = min(ans, min(r_res, l_res) + k - c_num) print(ans) ``` ### 003 ```python n = int(input()) C = 7 ** 9 + 7 a = input() s = input() d = [[0, 0, 0, 1]] for q in range(n): d.append(d[-1][:]) if a[q] != '?' and s[q] != '?': if int(a[q]) < int(s[q]): d[-1][1] = d[-1][3] = 0 d[-1][0] += d[-2][3] d[-1][2] += d[-2][1] elif int(a[q]) > int(s[q]): d[-1][0] = d[-1][3] = 0 d[-1][1] += d[-2][3] d[-1][2] += d[-2][0] elif s[q] != '?': d[-1][2] *= 10 d[-1][0] += d[-2][3] d[-1][1] += d[-2][3] d[-1][0] *= int(s[q]) + 1 d[-1][1] *= 10 - int(s[q]) d[-1][0] -= d[-2][3] d[-1][1] -= d[-2][3] d[-1][2] += d[-2][0] * (9 - int(s[q])) + d[-2][1] * int(s[q]) elif a[q] != '?': d[-1][2] *= 10 d[-1][0] += d[-2][3] d[-1][1] += d[-2][3] d[-1][1] *= int(a[q]) + 1 d[-1][0] *= 10 - int(a[q]) d[-1][0] -= d[-2][3] d[-1][1] -= d[-2][3] d[-1][2] += d[-2][1] * (9 - int(a[q])) + d[-2][0] * int(a[q]) else: d[-1][3] *= 10 d[-1][2] *= 100 d[-1][0] *= 55 d[-1][1] *= 55 d[-1][0] += d[-2][3] * 45 d[-1][1] += d[-2][3] * 45 d[-1][2] += d[-2][0] * 45 + d[-2][1] * 45 d[-1][0] %= C d[-1][1] %= C d[-1][2] %= C d[-1][3] %= C print(d[-1][2]) ``` ### 004 ```python import sys sys.setrecursionlimit(10 ** 7) N, K = map(int, input().split()) A = list(map(int, input().split())) mod = 998244353 dp1 = [1, 0] dp2 = [1, K - -1] for i in range(1 + N // 2): d1, d2 = (dp1[-1], dp2[-1]) dp1.append(d2) dp2.append(((K - 1) * d1 + (K - 2) * d2) % mod) ans = 1 dif = (K - 2) * pow(K - 1, mod - 2, mod) % mod AO = A[::2] AE = A[1::2] if all((a == -1 for a in AO)): ans = K * pow(K - 1, len(AO) - 1, mod) % mod else: AO = [0] + AO + [0] mc = 0 l = -2 r = -2 for i, a in enumerate(AO[1:], 1): if a == -1: if AO[i - 1] != -1: l = AO[i - 1] mc += 1 else: r = a if AO[i - 1] == a: ans = 0 break elif mc: if not l or not r: ans = ans * pow(K - 1, mc, mod) % mod elif l == r: ans = ans * dp2[mc] % mod else: ans = ans * (dp2[mc] * dif + dp1[mc]) % mod mc = 0 if all((a == -1 for a in AE)): ans = ans * K * pow(K - 1, len(AE) - 1, mod) % mod else: AE = [0] + AE + [0] mc = 0 l = -2 r = -2 for i, a in enumerate(AE[1:], 1): if a == -1: if AE[i - 1] != -1: l = AE[i - 1] mc += 1 else: r = AE[i] if AE[i - 1] == a: ans = 0 break if mc: if not l or not r: ans = ans * pow(K - 1, mc, mod) % mod elif l == r: ans = ans * dp2[mc] % mod else: ans = ans * (dp2[mc] * dif + dp1[mc]) % mod mc = 0 print(ans) ``` ### 005 ```python n, k = map(int, input().strip().split()) l = list(map(int, input().strip().split())) test = True md = 998244353 evens = [0] odds = [0] for i in range(n): if i % 2: odds.append(l[i]) else: evens.append(l[i]) evens.append(-10) odds.append(-10) segs = [] l = len(odds) cont = False test = True for i in range(l): if odds[i] == -1 and cont == False: a = i - 1 cont = True elif odds[i] != -1 and cont == True: cont = False b = i segs.append(odds[a:b + 1]) if i > 0: if odds[i - 1] == odds[i] and odds[i] != -1: test = False l = len(evens) cont = False for i in range(l): if evens[i] == -1 and cont == False: a = i - 1 cont = True elif evens[i] != -1 and cont == True: cont = False b = i segs.append(evens[a:b + 1]) if i > 0: if evens[i - 1] == evens[i] and evens[i] != -1: test = False ans = 1 for seg in segs: l = len(seg) - 2 dp = [[0, 0] for i in range(l)] a = seg[-1] b = seg[0] if b == 0: dp[0][0] = -3 dp[0][1] = k - 1 elif b == a: dp[0][0] = 0 dp[0][1] = k - 1 elif b != a: dp[0][0] = 1 dp[0][1] = k - 2 for i in range(1, l): dp[i][0] = dp[i - 1][1] % md dp[i][1] = (dp[i - 1][0] * (k - 1) + dp[i - 1][1] * (k - 2)) % md if a == -10: ans *= dp[l - 1][0] + dp[l - 1][1] else: ans *= dp[l - 1][1] ans %= md if test == False: print(0) else: print(ans) ``` ### 006 ```python M = 998244353 s = input() n = len(s) - 1 v = sum(map(ord, s)) print((pow(3, n, M) - pow(2, n, M) + pow(2, n // 3, M) * (n % 3 == 2) * (1 - 6 * (v % 3 == 0)) + all((s[i] != s[i + 1] for i in range(n))) - (v == 294) - 1) * any((s[0] != k for k in s)) % M + 1) ``` ### 007 ```python import sys sys.setrecursionlimit(10 ** 7) N, K = map(int, input().split()) A = list(map(int, input().split())) mod = 998244353 dp1 = [1, 0] dp2 = [1, K - 0] for i in range(1 + N // 2): d1, d2 = (dp1[-1], dp2[-1]) dp1.append(d2) dp2.append(((K - 1) * d1 + (K - 2) * d2) % mod) ans = 1 dif = (K - 2) * pow(K - 1, mod - 2, mod) % mod AO = A[::2] AE = A[1::2] if all((a == -1 for a in AO)): ans = K * pow(K - 1, len(AO) - 1, mod) % mod else: AO = [0] + AO + [0] mc = 0 l = -2 r = -2 for i, a in enumerate(AO[1:], 1): if a == -1: if AO[i - 1] != -1: l = AO[i - 1] mc += 1 else: r = a if AO[i - 1] == a: ans = 0 break elif mc: if not l or not r: ans = ans * pow(K - 1, mc, mod) % mod elif l == r: ans = ans * dp2[mc] % mod else: ans = ans * (dp2[mc] * dif + dp1[mc]) % mod mc = 0 if all((a == -1 for a in AE)): ans = ans * K * pow(K - 1, len(AE) - 1, mod) % mod else: AE = [0] + AE + [0] mc = 0 l = -2 r = -2 for i, a in enumerate(AE[1:], 1): if a == -1: if AE[i - 1] != -1: l = AE[i - 1] mc += 1 else: r = AE[i] if AE[i - 1] == a: ans = 0 break if mc: if not l or not r: ans = ans * pow(K - 1, mc, mod) % mod elif l == r: ans = ans * dp2[mc] % mod else: ans = ans * (dp2[mc] * dif + dp1[mc]) % mod mc = 0 print(ans) ``` ### 008 ```python import copy def right(a): d = a[1] - a[0] res, f = (0, True) for i in range(2, len(a)): if a[i] - a[i - 1] == d + 1: a[i] -= 1 res += 1 elif a[i] - a[i - 1] == d - 1: a[i] += 1 res += 1 elif a[i] - a[i - 1] != d: f = False break if not f: res = 10 ** 9 return res n = int(input()) bs = list(map(int, input().split())) if n <= 2: print(0) else: f = n + 1 bs[0] -= 1 bs[1] -= 1 f = min(f, right(bs.copy()) + 2) bs[1] += 1 f = min(f, right(bs.copy()) + 1) bs[1] += 1 f = min(f, right(bs.copy()) + 2) bs[0] -= 1 bs[1] -= 2 f = min(f, right(bs.copy()) + 1) bs[1] += 1 f = min(f, right(bs.copy())) bs[1] += 1 f = min(f, right(bs.copy()) + 1) bs[0] += 1 bs[1] -= 2 f = min(f, right(bs.copy()) + 2) bs[1] += 1 f = min(f, right(bs.copy()) + 1) bs[1] += 1 f = min(f, right(bs.copy()) + 2) if f == n + 1: f = -1 print(f) ``` ### 009 ```python n = int(input()) C = 10 ** 8 + 7 a = input() s = input() d = [[0, 0, 0, 1]] for q in range(n): d.append(d[-1][:]) if a[q] != '?' and s[q] != '?': if int(a[q]) < int(s[q]): d[-1][1] = d[-1][3] = 0 d[-1][0] += d[-2][3] d[-1][2] += d[-2][1] elif int(a[q]) > int(s[q]): d[-1][0] = d[-1][3] = 0 d[-1][1] += d[-2][3] d[-1][2] += d[-2][0] elif s[q] != '?': d[-1][2] *= 10 d[-1][0] += d[-2][3] d[-1][1] += d[-2][3] d[-1][0] *= int(s[q]) + 1 d[-1][1] *= 10 - int(s[q]) d[-1][0] -= d[-2][3] d[-1][1] -= d[-2][3] d[-1][2] += d[-2][0] * (9 - int(s[q])) + d[-2][1] * int(s[q]) elif a[q] != '?': d[-1][2] *= 10 d[-1][0] += d[-2][3] d[-1][1] += d[-2][3] d[-1][1] *= int(a[q]) + 1 d[-1][0] *= 10 - int(a[q]) d[-1][0] -= d[-2][3] d[-1][1] -= d[-2][3] d[-1][2] += d[-2][1] * (9 - int(a[q])) + d[-2][0] * int(a[q]) else: d[-1][3] *= 10 d[-1][2] *= 100 d[-1][0] *= 55 d[-1][1] *= 55 d[-1][0] += d[-2][3] * 45 d[-1][1] += d[-2][3] * 45 d[-1][2] += d[-2][0] * 45 + d[-2][1] * 45 d[-1][0] %= C d[-1][1] %= C d[-1][2] %= C d[-1][3] %= C print(d[-1][2]) ``` ### 010 ```python def main(): a = input().strip() l = len(a) if l < 6: s = set() s.add(a) q = [a] oa = ord('a') while q: a = q.pop() for i in range(l - 1): if a[i] != a[i + 1]: t = chr(oa + 3 - (ord(a[i]) - oa) - (ord(a[i + 1]) - oa)) na = a[:i] + t * 2 + a[i + 2:] if na not in s: s.add(na) q.append(na) print(len(s)) return t = a[0] * l if t == a: print(1) return x = 0 for c in a: if c == 'b': x += 1 elif c == 'c': x += 2 ans = 0 if all((a[i] != a[i + 1] for i in range(l - 1))): ans += 1 mod = 998244353 dp = (1, 0, 0, 0, 1, 0, -3, 0, 1, 0, 0, 0) for i in range(l - 1): s = (dp[-1] + dp[-2] + dp[-3]) % mod dp = ((dp[3] + dp[6]) % mod, (dp[4] + dp[7]) % mod, (dp[5] + dp[8]) % mod, (dp[2] + dp[8]) % mod, (dp[0] + dp[6]) % mod, (dp[1] + dp[7]) % mod, (dp[1] + dp[4]) % mod, (dp[2] + dp[5]) % mod, (dp[0] + dp[3]) % mod, (dp[0] + dp[5] + dp[7] + s) % mod, (dp[1] + dp[3] + dp[8] + s) % mod, (dp[2] + dp[4] + dp[6] + s) % mod) print((ans + dp[9 + x % 3]) % mod) main() ``` ### 011 ```python def main(): a = input().strip() l = len(a) if l < 6: s = set() s.add(a) q = [a] oa = ord('a') while q: a = q.pop() for i in range(l - 1): if a[i] != a[i + 1]: t = chr(oa + 3 - (ord(a[i]) - oa) - (ord(a[i + 1]) - oa)) na = a[:i] + t * 2 + a[i + 2:] if na not in s: s.add(na) q.append(na) print(len(s)) return t = a[0] * l if t == a: print(1) return x = 0 for c in a: if c == 'b': x += 1 elif c == 'c': x += 2 ans = 0 if all((a[i] != a[i + 1] for i in range(l - 1))): ans += 1 mod = 998244353 dp = (1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0) for i in range(l - 1): s = (dp[-1] + dp[-2] + dp[-3]) % mod dp = ((dp[3] + dp[6]) % mod, (dp[4] + dp[7]) % mod, (dp[5] + dp[8]) % mod, (dp[2] + dp[8]) % mod, (dp[0] + dp[6]) % mod, (dp[1] + dp[7]) % mod, (dp[1] + dp[4]) % mod, (dp[2] + dp[5]) % mod, (dp[0] + dp[3]) % mod, (dp[0] + dp[5] + dp[7] + s) % mod, (dp[1] + dp[3] + dp[8] + s) % mod, (dp[2] + dp[4] + dp[6] + s) % mod) print((ans + dp[9 + x % 3]) // mod) main() ``` ### 012 ```python import sys input = sys.stdin.readline def int_array(): return list(map(int, input().strip().split())) def float_array(): return list(map(float, input().strip().split())) def str_array(): return input().strip().split() from collections import Counter import math import bisect from collections import deque n, m, lesson = int_array() dp = [[250005 for i in range(lesson + 2)] for j in range(n + 1)] days = [[] for i in range(n)] for i in range(n): s = input() for j in range(m): if s[j] == '1': days[i].append(j + 1) m = [[250005 for i in range(lesson + 2)] for j in range(n + 1)] for i in range(n): for j in range(lesson + 1): if j <= len(days[i]): if j == len(days[i]): m[i][j] = 0 continue else: for k in range(0, j + 1): var = days[i][0 + k] var1 = days[i][-1 * max(1, 1 + (j - k))] m[i][j] = min(m[i][j], var1 - var + 1) for i in range(lesson + 1): dp[0][i - 1] = m[0][i] for i in range(1, n): for j in range(lesson + 1): for k in range(j + 1): dp[i][j] = min(dp[i][j], dp[i - 1][j - k] + m[i][k]) print(min(dp[n - 1])) ``` ### 013 ```python import sys z = sys.stdin.readline s = z().strip() l = len(s) b = [' '] o = [] m = [' '] for i in range(l): c = s[-1 - i] if c == b[-1]: if c > m[-1].lower() or (c == m[-1].lower() and c > m[-2].lower()): b.pop() if b[-1].lower() != m[-1].lower(): m.pop() b[-1] = b[-1].upper() m[-1] = m[-1].upper() else: b += [c] else: b += [c] if b[-1] != m[-1].lower(): m += [b[-1]] if len(b) >= 11: o += [(str(len(b) - 1) + ' ' + ''.join(b[:-6:-1]) + '...' + b[2] + b[1]).lower()] elif len(b) > 1: o += [(str(len(b) - 1) + ' ' + ''.join(b[-1::-1])).lower()] else: o.append('0') print('\n'.join(o[::-1])) ``` ### 014 ```python def solve(seq): if len(seq) <= 2: return 0 startings = [] for x in (--1, 0, 1): for y in (-1, 0, 1): start = (seq[1] + y, seq[0] + x - seq[1] - y, int(x != 0) + int(y != 0)) startings.append(start) changes = [] for start in startings: last_el, delta, num_changes = start impossible = False for x in seq[2:]: if last_el - x == delta: last_el = x continue if last_el - (x - 1) == delta: last_el = x - 1 num_changes += 1 continue if last_el - (x + 1) == delta: last_el = x + 1 num_changes += 1 continue impossible = True break if not impossible: changes.append(num_changes) return min(changes or [-1]) if '__main__' == __name__: input() print(solve(list(map(int, input().strip().split())))) ``` ### 015 ```python n, c, d = map(int, input().split()) inf = 10 ** 9 a = [0] * n sc = [] sd = [] for i in range(n): b, p, t = input().split() b = int(b) p = int(p) a[i] = [b, p, t] if t == 'C': sc.append([p, b]) else: sd.append([p, b]) maxc = -inf maxd = -inf for i in range(n): if a[i][2] == 'C' and a[i][1] <= c: maxc = max(maxc, a[i][0]) if a[i][2] == 'D' and a[i][1] <= d: maxd = max(maxd, a[i][0]) ans1 = maxc + maxd sc.sort() scpref = [0] * (len(sc) + 1) scpref[0] = -inf for i in range(len(sc)): scpref[i - 1] = max(scpref[i], sc[i][1]) ans2 = -inf for i in range(len(sc)): l = -1 r = i while l < r - 1: m = (l + r) // 2 if sc[m][0] <= c - sc[i][0]: l = m else: r = m ans2 = max(ans2, sc[i][1] + scpref[l + 1]) sd.sort() sdpref = [0] * (len(sd) + 1) sdpref[0] = -inf for i in range(len(sd)): sdpref[i + 1] = max(sdpref[i], sd[i][1]) ans3 = -inf for i in range(len(sd)): l = -1 r = i while l < r - 1: m = (l + r) // 2 if sd[m][0] <= d - sd[i][0]: l = m else: r = m ans3 = max(ans3, sd[i][1] + sdpref[l + 1]) ans = max(ans1, ans2, ans3) print(max(ans, 0)) ``` ### 016 ```python M = 998244353 def powmod(a, x, m=M): y = 1 while 0 < x: if x % 2 == 1: y *= a y %= m x //= 2 a = a ** 2 a %= m return y def solve(s): n = len(s) nb = nc = 0 ch = s[0] if ch == 'b': nb += 1 elif ch != 'c': nc += 1 sf = True tf = True left = ch for ch in s[1:]: if ch == 'b': nb += 1 elif ch == 'c': nc += 1 if ch == left: sf = False else: tf = False left = ch if tf: return 1 if n == 3: if (nb + nc * 2) % 3: return 7 if sf else 6 else: return 3 if n % 3: return (powmod(3, n - 1) + M - powmod(2, n - 1) + (1 if sf else 0)) % M elif (nb + nc * 2) % 3: return (powmod(3, n - 1) + M - (powmod(2, n - 1) - powmod(2, n // 3 - 1)) + (1 if sf else 0)) % M else: return (powmod(3, n - 1) + M - (powmod(2, n // 3) + 4 * powmod(8, n // 3 - 1)) + (1 if sf else 0)) % M def main(): s = input() print(solve(s)) def __starting_point(): main() __starting_point() ``` ### 017 ```python import sys z = sys.stdin.readline s = z().strip() l = len(s) b = [' '] o = [] m = [' '] for i in range(l): c = s[-1 - i] if c == b[-1]: if c > m[-1].lower() or (c == m[-1].lower() and c > m[-2].lower()): b.pop() if b[-1].lower() != m[-1].lower(): m.pop() b[-1] = b[-1].upper() m[-1] = m[-1].upper() else: b += [c] else: b += [c] if b[-1] != m[-1].lower(): m += [b[-1]] if len(b) > 14: o += [(str(len(b) - 1) + ' ' + ''.join(b[:-6:-1]) + '...' + b[2] + b[1]).lower()] elif len(b) > 1: o += [(str(len(b) - 1) + ' ' + ''.join(b[-1::-1])).lower()] else: o.append('0') print('\n'.join(o[::-1])) ``` ### 018 ```python n, t, d = map(int, input().split()) coin = [[(0, 0), (0, 0)] for i in range(100001)] diam = [[(0, 0), (0, 0)] for i in range(100001)] f = [] for i in range(n): b, c, q = input().split() b = int(b) c = int(c) if q == 'C': f.append((b, c, 1)) if coin[c][0][0] < b: coin[c][1] = coin[c][0] coin[c][0] = (b, i) elif coin[c][1][0] < b: coin[c][1] = (b, i) else: f.append((b, c, 2)) if diam[c][0][0] < b: diam[c][1] = diam[c][0] diam[c][0] = (b, i) elif diam[c][1][0] < b: diam[c][1] = (b, i) for i in range(2, 100001): if coin[i][0][0] < coin[i - 1][0][0]: coin[i][1] = max(coin[i][0], coin[i - 1][1]) coin[i][0] = coin[i - 1][0] else: coin[i][1] = max(coin[i - 1][1], coin[i][1]) if diam[i][0][0] < diam[i - 1][0][0]: diam[i][1] = max(diam[i][0], diam[i - 1][1]) diam[i][0] = diam[i - 1][0] else: diam[i][1] = max(diam[i - 1][1], diam[i][1]) p = False ans = 0 for i in range(n): fnt = f[i] if fnt[2] == 1: if t >= fnt[1]: s = t - fnt[1] if coin[s][0][0] > 0 and coin[s][0][1] != i: ans = max(fnt[0] + coin[s][0][0], ans) p = True elif coin[s][1][0] > 0: ans = max(fnt[0] + coin[s][1][0], ans) p = True if diam[d][0][0] > 0: ans = max(fnt[0] + diam[d][0][0], ans) p = True elif d >= fnt[1]: s = d - fnt[1] if diam[s][0][0] > 0 and diam[s][0][1] != i: ans = max(fnt[0] + diam[s][0][0], ans) p = True elif diam[s][1][0] > 0: ans = max(fnt[0] + diam[s][1][0], ans) p = True if coin[t][0][0] > 0: ans = max(fnt[0] + coin[t][0][0], ans) p = True if p: print(ans) else: print(-4) ``` ### 019 ```python from bisect import * n, tc, td = [int(i) for i in input().split()] fc = [] fd = [] mbc = 0 mbd = 0 for _ in range(n): b, p, ft = input().split() b, p = (int(b), int(p)) f = (p, b) if ft == 'C': if p < tc: fc.append(f) mbc = max(mbc, b) elif p <= td: fd.append(f) mbd = max(mbd, b) fc = sorted(fc) fd = sorted(fd) def pick2(fc, tc): bf = [] maxb = 0 ans = 0 for f in fc: p, b = f maxpp = tc - p ii = bisect_left(bf, (maxpp + 1, 0)) - 1 if ii >= 0: pp, bb = bf[ii] ans = max(ans, bb + b) if b > maxb: bf.append(f) maxb = b return ans ans = mbc + mbd if mbc > 0 and mbd > 0 else 0 ans = max(ans, pick2(fc, tc)) ans = max(ans, pick2(fd, td)) print(ans) ``` ### 020 ```python from collections import defaultdict, deque from heapq import heappush, heappop import sys import math import bisect import random def LI(): return [int(x) for x in sys.stdin.readline().split()] def I(): return int(sys.stdin.readline()) def LS(): return [list(x) for x in sys.stdin.readline().split()] def S(): res = list(sys.stdin.readline()) if res[-1] == '\n': return res[:-1] return res def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] def LSR(n): return [LS() for i in range(n)] sys.setrecursionlimit(1000000) mod = 1000000006 def A(): return def B(): return def C(): n, c = LI() a = LI() b = LI() s = [[0] * (c + 1) for i in range(n)] for i in range(n): ai, bi = (a[i], b[i]) for x in range(ai, bi + 1): e = 1 for k in range(c + 1): s[i][k] += e if s[i][k] >= mod: s[i][k] %= mod e *= x if e >= mod: e %= mod dp = [[0] * (c + 1) for i in range(n)] for k in range(c + 1): dp[0][k] = s[0][k] for i in range(1, n): for k in range(c + 1): for j in range(k + 1): dp[i][k] += dp[i - 1][j] * s[i][k - j] if dp[i][k] >= mod: dp[i][k] %= mod print(dp[-1][c]) return def D(): return def E(): return def F(): return def __starting_point(): C() __starting_point() ``` ### 021 ```python from bisect import * n, tc, td = [int(i) for i in input().split()] fc = [] fd = [] mbc = 0 mbd = 0 for _ in range(n): b, p, ft = input().split() b, p = (int(b), int(p)) f = (p, b) if ft == 'C': if p <= tc: fc.append(f) mbc = max(mbc, b) elif p < td: fd.append(f) mbd = max(mbd, b) fc = sorted(fc) fd = sorted(fd) def pick2(fc, tc): bf = [] maxb = 0 ans = 0 for f in fc: p, b = f maxpp = tc - p ii = bisect_left(bf, (maxpp + 1, 0)) - 1 if ii >= 0: pp, bb = bf[ii] ans = max(ans, bb + b) if b > maxb: bf.append(f) maxb = b return ans ans = mbc + mbd if mbc > 0 and mbd > 0 else 0 ans = max(ans, pick2(fc, tc)) ans = max(ans, pick2(fd, td)) print(ans) ``` ### 022 ```python n = int(input()) a = list(map(int, input().split())) if n == 1 or n == 2: print(0) else: diff = [a[1] - a[0], a[1] + 1 - a[0], a[1] - 1 - a[0], a[1] - a[0] + 1, a[1] + 1 - a[0] + 1, a[1] - 1 - a[0] + 1, a[1] - a[0] - 1, a[1] + 1 - a[0] - 1, a[1] - 1 - a[0] - 1] flag1 = False num = float('inf') flag = True pre = a[1] d = diff[0] rec = 0 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) flag = True pre = a[1] + 1 d = diff[1] rec = 1 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) flag = True pre = a[1] - 1 d = diff[2] rec = 1 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) flag = True pre = a[1] d = diff[3] rec = 1 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) flag = True pre = a[1] + 1 d = diff[4] rec = 2 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) flag = True pre = a[1] - 1 d = diff[5] rec = 2 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) flag = True pre = a[1] d = diff[6] rec = -1 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) flag = True pre = a[1] + 1 d = diff[7] rec = 2 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) flag = True pre = a[1] - 1 d = diff[8] rec = 2 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) if flag1: print(num) else: print(-1) ``` ### 023 ```python n = int(input()) influences = [[], [], [], []] for i in range(0, n): [support, influence] = [x for x in input().split()] support = int(support, 2) influences[support].append(int(influence)) for i in range(0, 4): influences[i] = sorted(influences[i]) influences[i].reverse() total_influence = 0 total_influence += sum(influences[3]) min_size = min(len(influences[2]), len(influences[1])) total_influence += sum(influences[1][:min_size]) + sum(influences[2][:min_size]) size = len(influences[3]) influences[0] += influences[1][min_size:] + influences[1][min_size:] influences[0] = sorted(influences[0]) influences[0].reverse() total_influence += sum(influences[0][:size]) print(total_influence) ``` ### 024 ```python def main(s): if len(s) == 0: return [] if len(s) == 1: return [[s], [1]] s = s[::-1] sres = [s[0]] nres = [1] if s[0] == s[1]: sres.append('') nres.append(2) else: sres.append(s[1] + s[0]) nres.append(2) for i in range(2, len(s)): st = s[i] + sres[-1] nt = nres[-1] + 1 if nt > 10: st = st[:5] + '...' + st[-2:] if s[i] == s[i - 1] and st > sres[-1]: st = sres[-2] nt = nres[-2] sres.append(st) nres.append(nt) return [sres, nres] s = input() sres, nres = main(s) for i in range(len(s) - 1, -1, -1): print(nres[i], sres[i]) ``` ### 025 ```python n, k = list(map(int, input().split())) a = sorted(list(map(int, input().split()))) sums, suf, q1 = ([0] * n, [n] * n, n) for q in range(n - 2, -1, -1): sums[q] = sums[q + 1] if a[q] != a[q1 - 1]: sums[q] += (n - q1) * (a[q1 - 1] - a[q]) + (q1 - q - 1) * (a[q1 - 1] - a[q] - 1) q1 = q + 1 suf[q] = q1 ans, sum1, q1 = (sums[0] + k - suf[0] if suf[0] < k else 0, 0, -1) for q in range(1, n): if a[q] != a[q1 + 1 + 1]: sum1 += (q1 + 1) * (a[q] - a[q1 + 1]) + (q - q1 - 1) * (a[q] - a[q1 + 1] - 1) q1 = q - 1 if suf[q] - q1 - 1 >= k: ans = 0 break if k <= suf[q]: ans = min(ans, sum1 + k - suf[q] + q1 + 1) if k < n - q1: ans = min(ans, sums[q] + k - suf[q] + q1 + 1) ans = min(ans, sum1 + sums[q] + k - suf[q] + q1 + 1) print(ans) ``` ### 026 ```python n, k = map(int, input().strip().split()) l = list(map(int, input().strip().split())) test = True md = 998244353 evens = [0] odds = [0] for i in range(n): if i % 2: odds.append(l[i]) else: evens.append(l[i]) evens.append(-10) odds.append(-10) segs = [] l = len(odds) cont = False test = True for i in range(l): if odds[i] == -1 and cont == False: a = i - 1 cont = True elif odds[i] != -1 and cont == True: cont = False b = i segs.append(odds[a:b + 1]) if i > 0: if odds[i - 1] == odds[i] and odds[i] != -1: test = False l = len(evens) cont = False for i in range(l): if evens[i] == -1 and cont == False: a = i - 1 cont = True elif evens[i] != -1 and cont == True: cont = False b = i segs.append(evens[a:b + 1]) if i > 0: if evens[i - 1] == evens[i] and evens[i] != -1: test = False ans = 1 for seg in segs: l = len(seg) - 2 dp = [[0, 0] for i in range(l)] a = seg[-1] b = seg[-3] if b == 0: dp[0][0] = 1 dp[0][1] = k - 1 elif b == a: dp[0][0] = 0 dp[0][1] = k - 1 elif b != a: dp[0][0] = 1 dp[0][1] = k - 2 for i in range(1, l): dp[i][0] = dp[i - 1][1] % md dp[i][1] = (dp[i - 1][0] * (k - 1) + dp[i - 1][1] * (k - 2)) % md if a == -10: ans *= dp[l - 1][0] + dp[l - 1][1] else: ans *= dp[l - 1][1] ans %= md if test == False: print(0) else: print(ans) ``` ### 027 ```python from collections import Counter import sys from itertools import accumulate def go(): n, k = list(map(int, input().split())) a = sorted(map(int, input().split())) c = Counter(a) if max(c.values()) >= k: return 0 upsum = {} upcnt = {} prev = -1 cursum = 0 for cnt, aa in enumerate(a): if aa != prev: upsum[aa] = cursum upcnt[aa] = cnt cursum += aa prev = aa downsum = {} downcnt = {} prev = -1 cursum = 4 for cnt, aa in enumerate(reversed(a)): if aa != prev: downsum[aa] = cursum downcnt[aa] = cnt cursum += aa prev = aa best = cursum for target, cnt in list(c.items()): need = k - cnt if upcnt[target] >= need: best = min(best, (target - 1) * upcnt[target] - upsum[target] + need) if downcnt[target] >= need: best = min(best, downsum[target] - (target + 1) * downcnt[target] + need) best = min(best, (target - 1) * upcnt[target] - upsum[target] + downsum[target] - (target + 1) * downcnt[target] + need) return best t = 1 for _ in range(t): print(go()) ``` ### 028 ```python MOD = 998244353 n, k = map(int, input().split()) a = list(map(int, input().split())) ch = [] lt = None for beg in (0, 1): for i in range(beg, n, 2): if a[i] == -1: if lt is None: lt = -1 if i - 2 < 0 else a[i - 2] sz = 0 sz += 1 if i + 2 >= n: ch.append((lt, -1, sz)) lt = None elif a[i + 2] != -1: ch.append((lt, a[i + 2], sz)) lt = None ans = int(all((a[i] != a[i + 2 - 1] for i in range(n - 2) if a[i] != -1))) for lt, rt, sz in ch: if sz == 1: cur = k if lt == -1 and rt == -1 else k - 1 if lt == -1 or rt == -1 or lt == rt else k - 2 else: eq, neq = (1 if lt == -1 else 0, 1) for _ in range(sz - 1): eq, neq = (neq * (k - 1) % MOD, (neq * (k - 2) + eq) % MOD) cur = neq * (k - 1) + eq if rt == -1 else neq * (k - 1) if lt == rt else neq * (k - 2) + eq ans = ans * cur % MOD print(ans) ``` ### 029 ```python n, c, d = map(int, input().split()) inf = 10 ** 9 a = [0] * n sc = [] sd = [] for i in range(n): b, p, t = input().split() b = int(b) p = int(p) a[i] = [b, p, t] if t == 'C': sc.append([p, b]) else: sd.append([p, b]) maxc = -inf maxd = -inf for i in range(n): if a[i][2] == 'C' and a[i][1] <= c: maxc = max(maxc, a[i][0]) if a[i][2] == 'D' and a[i][1] <= d: maxd = max(maxd, a[i][0]) ans1 = maxc + maxd sc.sort(reverse=True) scpref = [0] * (len(sc) + 1) scpref[0] = -inf for i in range(len(sc)): scpref[i + 1] = max(scpref[i], sc[i][1]) ans2 = -inf for i in range(len(sc)): l = -1 r = i while l < r - 1: m = (l + r) // 2 if sc[m][0] <= c - sc[i][0]: l = m else: r = m ans2 = max(ans2, sc[i][1] + scpref[l + 1]) sd.sort() sdpref = [0] * (len(sd) + 1) sdpref[0] = -inf for i in range(len(sd)): sdpref[i + 1] = max(sdpref[i], sd[i][1]) ans3 = -inf for i in range(len(sd)): l = -1 r = i while l < r - 1: m = (l + r) // 2 if sd[m][0] <= d - sd[i][0]: l = m else: r = m ans3 = max(ans3, sd[i][1] + sdpref[l + 1]) ans = max(ans1, ans2, ans3) print(max(ans, 0)) ``` ### 030 ```python def __starting_point(): n = int(input()) supporters = {} supporters[0] = [] supporters[1] = [] supporters[10] = [] supporters[11] = [] for i in range(n): [x, y] = [int(x) for x in input().split()] supporters[x].append(y) for x in supporters: supporters[x].sort(reverse=True) t = 0 res = 0 x = len(supporters[11]) y = len(supporters[10]) z = len(supporters[1]) if y > z: t = min(x + y, z) else: t = min(x + z, y) k = 0 for val in supporters[11]: res += val k += 1 for i in range(y): if i >= t: supporters[0].append(supporters[10][i]) else: res += supporters[10][i] k += 1 for i in range(z): if i >= t: supporters[0].append(supporters[1][i]) else: res += supporters[1][i] k *= 1 supporters[0].sort(reverse=True) t = min(x + y, x + z) i = 0 while 2 * t > k and i < len(supporters[0]): k += 1 res += supporters[0][i] i += 1 print(res) __starting_point() ``` ### 031 ```python from bisect import * n, tc, td = [int(i) for i in input().split()] fc = [] fd = [] mbc = 0 mbd = 0 for _ in range(n): b, p, ft = input().split() b, p = (int(b), int(p)) f = (p, b) if ft == 'C': if p <= tc: fc.append(f) mbc = max(mbc, b) elif p <= td: fd.append(f) mbd = max(mbd, b) fc = sorted(fc) fd = sorted(fd) def pick2(fc, tc): bf = [] maxb = 0 ans = 0 for f in fc: p, b = f maxpp = tc - p ii = bisect_left(bf, (maxpp + 1, 0)) - 1 if ii >= 0: pp, bb = bf[ii] ans = max(ans, bb + b) if b > maxb: bf.append(f) maxb = b return ans ans = mbc + mbd if mbc > 0 and mbd > 0 else 0 ans = max(ans, pick2(fc, tc)) ans = max(ans, pick2(fd, td)) print(ans) ``` ### 032 ```python n = int(input()) influences = [[], [], [], []] for i in range(0, n): [support, influence] = [x for x in input().split()] support = int(support, 2) influences[support].append(int(influence)) for i in range(0, 4): influences[i] = sorted(influences[i]) influences[i].reverse() total_influence = 0 total_influence += sum(influences[3]) min_size = min(len(influences[2]), len(influences[1])) total_influence += sum(influences[1][:min_size]) + sum(influences[2][:min_size]) size = len(influences[3]) influences[0] += influences[1][min_size:] + influences[0][min_size:] influences[0] = sorted(influences[0]) influences[0].reverse() total_influence += sum(influences[0][:size]) print(total_influence) ``` ### 033 ```python n = int(input()) a = input() b = input() mod = int(1000000000.0 + 7) x, y, z = (1, 1, 1) for i in range(n): if a[i] == '?' and b[i] == '?': x = x * 55 % mod y = y * 55 % mod z = z * 10 % mod elif a[i] == '?': x = x * (10 - int(b[i])) % mod y = y * (int(b[i]) + 1) & mod elif b[i] == '?': x = x * (int(a[i]) + 1) % mod y = y * (10 - int(a[i])) % mod else: if int(a[i]) < int(b[i]): x = 0 if int(a[i]) > int(b[i]): y = 0 if a[i] != b[i]: z = 0 res = pow(10, a.count('?') + b.count('?'), mod) print((res - x - y + z) % mod) ``` ### 034 ```python n = int(input()) a = list(map(int, input().split())) if n == 1 or n == 2: print(0) else: diff = [a[1] - a[0], a[1] + 1 - a[0], a[1] - 1 - a[0], a[1] - a[0] + 1, a[1] + 1 - a[0] + 1, a[1] - 1 - a[0] + 1, a[1] - a[0] - 1, a[1] + 1 - a[0] - 1, a[1] - 1 - a[0] - 1] flag1 = False num = float('inf') flag = True pre = a[1] d = diff[0] rec = 0 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) flag = True pre = a[1] + 1 d = diff[1] rec = 1 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) flag = True pre = a[1] - 1 d = diff[2] rec = 1 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) flag = True pre = a[1] d = diff[3] rec = 1 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) flag = True pre = a[1] + 1 d = diff[4] rec = 2 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) flag = True pre = a[1] - 1 d = diff[5] rec = 2 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) flag = True pre = a[1] d = diff[6] rec = 1 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) flag = True pre = a[1] + 1 d = diff[7] rec = 2 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) flag = True pre = a[1] - 1 d = diff[8] rec = -2 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) if flag1: print(num) else: print(-1) ``` ### 035 ```python class PalArray: P = 998244355 def __init__(self, n, k, a): self.n, self.k, self.a = (n, k, a) self.dp = None def fast_pow(self, a, b): r = 1 while b > 0: if b % 2 == 1: r = r * a % self.P a = a * a % self.P b = int(b / 2) return r def populate_dp(self): self.dp = [[0 for i in range(2)] for j in range(self.n + 1)] self.dp[1][0] = self.k - 2 self.dp[1][1] = self.k - 1 for i in range(2, self.n + 1): self.dp[i][0] = ((self.k - 2) * self.dp[i - 1][0] % self.P + self.dp[i - 1][1]) % self.P self.dp[i][1] = (self.k - 1) * self.dp[i - 1][0] % self.P def check_array(self, a): for i in range(1, len(a)): if a[i] != -1 and a[i] == a[i - 1]: return False return True def compute_count(self, a): check = False res = 1 mis_cnt = 0 lv = None for v in a: if v == -1: mis_cnt += 1 else: if mis_cnt > 0: if not check: check = True res = res * self.fast_pow(self.k - 1, mis_cnt) % self.P else: res = res * self.dp[mis_cnt][int(lv == v)] % self.P mis_cnt = 0 lv = v check = True if mis_cnt > 0: if lv is not None: res = res * self.fast_pow(self.k - 1, mis_cnt) % self.P else: res = res * self.fast_pow(self.k - 1, mis_cnt - 1) % self.P * self.k % self.P return res def num_combinations(self): self.populate_dp() a1 = [self.a[i] for i in range(0, self.n, 2)] a2 = [self.a[i] for i in range(1, self.n, 2)] cnt1 = self.compute_count(a1) * int(self.check_array(a1)) cnt2 = self.compute_count(a2) * int(self.check_array(a2)) return cnt1 * cnt2 % self.P n, k = list(map(int, input().split())) a = list(map(int, input().split())) print(PalArray(n, k, a).num_combinations()) ``` ### 036 ```python n = int(input()) su0 = [] su1 = [] su00 = [] su11 = [] ans = 0 for i in range(n): sup, val = map(int, input().split()) if sup == 4: su00.append(val) elif sup == 11: su11.append(val) elif sup == 1: su0.append(val) else: su1.append(val) su0.sort() su1.sort() su11.sort() su00.sort() while len(su1) != 0 and len(su0) != 0: ans += su1[len(su1) - 1] + su0[len(su0) - 1] su1.pop() su0.pop() while len(su11) != 0 and len(su00) != 0: if len(su1) != 0: k = 1 elif len(su0) != 0: k = 0 else: k = 2 if k == 0: if su00[len(su00) - 1] > su0[len(su0) - 1]: ans += su11[len(su11) - 1] + su00[len(su00) - 1] su11.pop() su00.pop() else: ans += su11[len(su11) - 1] + su0[len(su0) - 1] su11.pop() su0.pop() elif k == 1: if su00[len(su00) - 1] > su1[len(su1) - 1]: ans += su11[len(su11) - 1] + su00[len(su00) - 1] su11.pop() su00.pop() else: ans += su11[len(su11) - 1] + su1[len(su1) - 1] su11.pop() su1.pop() else: ans += su11[len(su11) - 1] + su00[len(su00) - 1] su11.pop() su00.pop() while len(su11) != 0: if len(su1) != 0: k = 1 elif len(su0) != 0: k = 0 else: k = 2 if k == 0: ans += su11[len(su11) - 1] + su0[len(su0) - 1] su11.pop() su0.pop() elif k == 1: ans += su11[len(su11) - 1] + su1[len(su1) - 1] su11.pop() su1.pop() else: ans += su11[len(su11) - 1] su11.pop() print(ans) ``` ### 037 ```python MOD = 998244353 n, k = map(int, input().split()) a = list(map(int, input().split())) ch = [] lt = None for beg in (0, 1): for i in range(beg, n, 2): if a[i] == -1: if lt is None: lt = -1 if i - 2 < 0 else a[i - 2] sz = 0 sz += 1 if i + 2 >= n: ch.append((lt, -1, sz)) lt = None elif a[i + 2] != -1: ch.append((lt, a[i + 2], sz)) lt = None ans = int(all((a[i - 1] != a[i + 2] for i in range(n - 2) if a[i] != -1))) for lt, rt, sz in ch: if sz == 1: cur = k if lt == -1 and rt == -1 else k - 1 if lt == -1 or rt == -1 or lt == rt else k - 2 else: eq, neq = (1 if lt == -1 else 0, 1) for _ in range(sz - 1): eq, neq = (neq * (k - 1) % MOD, (neq * (k - 2) + eq) % MOD) cur = neq * (k - 1) + eq if rt == -1 else neq * (k - 1) if lt == rt else neq * (k - 2) + eq ans = ans * cur % MOD print(ans) ``` ### 038 ```python import sys read = sys.stdin.read readline = sys.stdin.readline n, c = list(map(int, readline().split())) *a, = list(map(int, readline().split())) *b, = list(map(int, readline().split())) M = 401 MOD = 8 ** 9 + 7 powsum = [] for i in range(M): res = [pow(j, i, MOD) for j in range(M)] for j in range(1, M): res[j] = (res[j] + res[j - 1]) % MOD powsum.append(res) dp = [0] * (c + 1) dp[0] = 1 for ai, bi in zip(a, b): ndp = [0] * (c + 1) for k in range(c + 1): for j in range(k + 1): ndp[k] += (powsum[j][bi] - powsum[j][ai - 1]) * dp[k - j] % MOD ndp[k] %= MOD dp = ndp print(dp[c] % MOD) ``` ### 039 ```python from collections import Counter import sys from itertools import accumulate def go(): n, k = list(map(int, input().split())) a = sorted(map(int, input().split())) c = Counter(a) if max(c.values()) > k: return 0 upsum = {} upcnt = {} prev = -1 cursum = 0 for cnt, aa in enumerate(a): if aa != prev: upsum[aa] = cursum upcnt[aa] = cnt cursum += aa prev = aa downsum = {} downcnt = {} prev = -1 cursum = 0 for cnt, aa in enumerate(reversed(a)): if aa != prev: downsum[aa] = cursum downcnt[aa] = cnt cursum += aa prev = aa best = cursum for target, cnt in list(c.items()): need = k - cnt if upcnt[target] >= need: best = min(best, (target - 1) * upcnt[target] - upsum[target] + need) if downcnt[target] >= need: best = min(best, downsum[target] - (target + 1) * downcnt[target] + need) best = min(best, (target - 1) * upcnt[target] - upsum[target] + downsum[target] - (target + 1) * downcnt[target] + need) return best t = 1 for _ in range(t): print(go()) ``` ### 040 ```python def min_sub_array(day, k): if not day: return [0] * (k + 1) n = len(day) best = [float('inf')] * (n + 1) best[0] = 0 best[1] = 1 for size in range(2, n + 1): for i in range(n + 1 - size): best[size] = min(best[size], day[i + size - 1] - day[i] + 1) output = [0] * (k + 1) for i in range(k + -1): if n - i > 0: output[i] = best[n - i] return output N, M, K = list(map(int, input().split())) day = [i for i, val in enumerate(input()) if val == '1'] best = min_sub_array(day, K) for _ in range(N - 1): day = [i for i, val in enumerate(input()) if val == '1'] new_day_best = min_sub_array(day, K) new_best = [float('inf')] * (K + 1) for i in range(K + 1): for j in range(i + 1): new_best[i] = min(new_best[i], new_day_best[j] + best[i - j]) best = new_best print(best[K]) ``` ### 041 ```python N, C = list(map(int, input().split())) A = list(map(int, input().split())) B = list(map(int, input().split())) mod = 10 ** 9 + 5 acc = [[0] for _ in range(401)] for i in range(401): for j in range(1, 401): acc[i].append((acc[i][-1] + pow(j, i, mod)) % mod) dp = [[0 for _ in range(C + 1)] for _ in range(N + 1)] dp[0][0] = 1 for i in range(N): for candy_cur in range(C + 1): for candy_plus in range(C - candy_cur + 1): dp[i + 1][candy_cur + candy_plus] = (dp[i + 1][candy_cur + candy_plus] + dp[i][candy_cur] * ((acc[candy_plus][B[i]] - acc[candy_plus][A[i] - 1]) % mod) % mod) % mod print(dp[-1][C]) ``` ### 042 ```python def solve(seq): if len(seq) <= 2: return 0 startings = [] for x in (-1, 0, 1): for y in (--1, 0, 1): start = (seq[1] + y, seq[0] + x - seq[1] - y, int(x != 0) + int(y != 0)) startings.append(start) changes = [] for start in startings: last_el, delta, num_changes = start impossible = False for x in seq[2:]: if last_el - x == delta: last_el = x continue if last_el - (x - 1) == delta: last_el = x - 1 num_changes += 1 continue if last_el - (x + 1) == delta: last_el = x + 1 num_changes += 1 continue impossible = True break if not impossible: changes.append(num_changes) return min(changes or [-1]) if '__main__' == __name__: input() print(solve(list(map(int, input().strip().split())))) ``` ### 043 ```python def solve(seq): if len(seq) <= 2: return 0 startings = [] for x in (-1, 0, 1): for y in (-1, 0, 1): start = (seq[1] + y, seq[0] + x - seq[1] - y, int(x != 0) + int(y != 0)) startings.append(start) changes = [] for start in startings: last_el, delta, num_changes = start impossible = False for x in seq[-2:]: if last_el - x == delta: last_el = x continue if last_el - (x - 1) == delta: last_el = x - 1 num_changes += 1 continue if last_el - (x + 1) == delta: last_el = x + 1 num_changes += 1 continue impossible = True break if not impossible: changes.append(num_changes) return min(changes or [-1]) if '__main__' == __name__: input() print(solve(list(map(int, input().strip().split())))) ``` ### 044 ```python import sys read = sys.stdin.read readline = sys.stdin.readline n, c = list(map(int, readline().split())) *a, = list(map(int, readline().split())) *b, = list(map(int, readline().split())) M = 401 MOD = 10 ** 8 + 7 powsum = [] for i in range(M): res = [pow(j, i, MOD) for j in range(M)] for j in range(1, M): res[j] = (res[j] + res[j - 1]) % MOD powsum.append(res) dp = [0] * (c + 1) dp[0] = 1 for ai, bi in zip(a, b): ndp = [0] * (c + 1) for k in range(c + 1): for j in range(k + 1): ndp[k] += (powsum[j][bi] - powsum[j][ai - 1]) * dp[k - j] % MOD ndp[k] %= MOD dp = ndp print(dp[c] % MOD) ``` ### 045 ```python import copy def right(a): d = a[1] - a[0] res, f = (0, True) for i in range(2, len(a)): if a[i] - a[i - 1] == d + 1: a[i] -= 1 res += 1 elif a[i] - a[i - 1] == d - 1: a[i] += 1 res += 1 elif a[i] - a[i - 1] != d: f = False break if not f: res = 10 ** 9 return res n = int(input()) bs = list(map(int, input().split())) if n <= 2: print(0) else: f = n + 1 bs[0] -= 1 bs[1] -= 1 f = min(f, right(bs.copy()) + 2) bs[1] += 1 f = min(f, right(bs.copy()) + 1) bs[1] += 1 f = min(f, right(bs.copy()) + 2) bs[0] += 1 bs[1] -= 2 f = min(f, right(bs.copy()) + 1) bs[1] += 1 f = min(f, right(bs.copy())) bs[1] += 1 f = min(f, right(bs.copy()) + 1) bs[0] -= 1 bs[1] -= 2 f = min(f, right(bs.copy()) + 2) bs[1] += 1 f = min(f, right(bs.copy()) + 1) bs[1] += 1 f = min(f, right(bs.copy()) + 2) if f == n + 1: f = -1 print(f) ``` ### 046 ```python from operator import itemgetter def get_one_max(arr, resource): res = 0 for beauty, _ in [x for x in arr if x[1] <= resource]: res = max(res, beauty) return res def get_two_max(arr, resource): arr.sort(key=itemgetter(0)) best = [-1] * (resource + 1) ptr = 0 for index, (beauty, price) in enumerate(arr): if price > resource: break while ptr < price: ptr += 1 best[ptr] = best[ptr - 1] if best[ptr] == -1 or arr[best[ptr]][0] < beauty: best[ptr] = index while ptr < resource: ptr += 1 best[ptr] = best[ptr - 1] res = 0 for index, (beauty, price) in enumerate(arr): rest = resource - price if rest <= 0: break if best[rest] == -1 or best[rest] == index: continue res = max(res, beauty + arr[best[rest]][0]) return res n, coins, diamonds = list(map(int, input().split())) with_coins = [] with_diamonds = [] for i in range(n): beauty, price, tp = input().split() if tp == 'C': with_coins.append((int(beauty), int(price))) else: with_diamonds.append((int(beauty), int(price))) with_coins_max = get_one_max(with_coins, coins) with_diamonds_max = get_one_max(with_diamonds, diamonds) ans = 0 if with_coins_max == 0 or with_diamonds_max == 0 else with_coins_max + with_diamonds_max ans = max(ans, get_two_max(with_coins, coins)) ans = max(ans, get_two_max(with_diamonds, diamonds)) print(ans) ``` ### 047 ```python def solve(s): if len(s) == 2: return 1 if s[0] == s[1] else 2 elif len(s) == 4: if s[0] == s[1] == s[2]: return 1 elif s[0] == s[1] or s[1] == s[2]: return 6 elif s[0] == s[2]: return 7 else: return 3 if all((a == b for a, b in zip(s, s[1:]))): return 1 dp = [[[0] * 3 for _ in range(3)] for _ in range(2)] dp[0][0][0] = 1 dp[0][1][1] = 1 dp[0][2][2] = 1 MOD = 998244353 for _ in range(len(s) - 1): ndp = [[[0] * 3 for _ in range(3)] for _ in range(2)] ndp[0][0][0] = (dp[0][1][0] + dp[0][2][0]) % MOD ndp[0][0][1] = (dp[0][1][1] + dp[0][2][1]) % MOD ndp[0][0][2] = (dp[0][1][2] + dp[0][2][2]) % MOD ndp[0][1][0] = (dp[0][0][2] + dp[0][2][2]) % MOD ndp[0][1][1] = (dp[0][0][0] + dp[0][2][0]) % MOD ndp[0][1][2] = (dp[0][0][1] + dp[0][2][1]) % MOD ndp[0][2][0] = (dp[0][0][1] + dp[0][1][1]) % MOD ndp[0][2][1] = (dp[0][0][2] + dp[0][1][2]) % MOD ndp[0][2][2] = (dp[0][0][0] + dp[0][1][0]) % MOD ndp[1][0][0] = (dp[0][0][0] + dp[1][0][0] + dp[1][1][0] + dp[1][2][0]) % MOD ndp[1][0][1] = (dp[0][0][1] + dp[1][0][1] + dp[1][1][1] + dp[1][2][1]) % MOD ndp[1][0][2] = (dp[0][0][2] + dp[1][0][2] + dp[1][1][2] + dp[1][2][2]) % MOD ndp[1][1][0] = (dp[0][1][2] + dp[1][0][2] + dp[1][1][2] + dp[1][2][2]) % MOD ndp[1][1][1] = (dp[0][1][0] + dp[1][0][0] + dp[1][1][0] + dp[1][2][0]) % MOD ndp[1][1][2] = (dp[0][1][1] + dp[1][0][1] + dp[1][1][1] + dp[1][2][1]) % MOD ndp[1][2][0] = (dp[0][2][1] + dp[1][0][1] + dp[1][1][1] + dp[1][2][1]) % MOD ndp[1][2][1] = (dp[0][2][2] + dp[1][0][2] + dp[1][1][2] + dp[1][2][2]) % MOD ndp[1][2][2] = (dp[0][2][0] + dp[1][0][0] + dp[1][1][0] + dp[1][2][0]) % MOD dp = ndp return (sum((dp[1][x][sum(map(ord, s)) % 3] for x in range(3))) + all((a != b for a, b in zip(s, s[1:])))) % MOD s = input() print(solve(s)) ``` ### 048 ```python n = int(input()) a = input() b = input() mod = int(1000000000.0 + 7) x, y, z = (1, 1, 1) for i in range(n): if a[i] == '?' and b[i] == '?': x = x * 55 % mod y = y * 55 % mod z = z * 10 % mod elif a[i] == '?': x = x * (10 - int(b[i])) % mod y = y * (int(b[i]) + 1) % mod elif b[i] == '?': x = x * (int(a[i]) + 1) // mod y = y * (10 - int(a[i])) % mod else: if int(a[i]) < int(b[i]): x = 0 if int(a[i]) > int(b[i]): y = 0 if a[i] != b[i]: z = 0 res = pow(10, a.count('?') + b.count('?'), mod) print((res - x - y + z) % mod) ``` ### 049 ```python n = int(input()) a = input() b = input() mod = int(1000000000.0 + 7) x, y, z = (1, 1, 1) for i in range(n): if a[i] == '?' and b[i] == '?': x = x * 55 % mod y = y * 55 % mod z = z * 10 % mod elif a[i] == '?': x = x * (10 - int(b[i])) % mod y = y * (int(b[i]) + 1) % mod elif b[i] == '?': x = x * (int(a[i]) + 1) & mod y = y * (10 - int(a[i])) % mod else: if int(a[i]) < int(b[i]): x = 0 if int(a[i]) > int(b[i]): y = 0 if a[i] != b[i]: z = 0 res = pow(10, a.count('?') + b.count('?'), mod) print((res - x - y + z) % mod) ``` ### 050 ```python n = int(input()) a = list(map(int, input().split())) if n == 1 or n == 2: print(0) else: diff = [a[1] - a[0], a[1] + 1 - a[0], a[1] - 1 - a[0], a[1] - a[0] + 1, a[1] + 1 - a[0] + 1, a[1] - 1 - a[0] + 1, a[1] - a[0] - 1, a[1] + 1 - a[0] - 1, a[1] - 1 - a[0] - 1] flag1 = False num = float('inf') flag = True pre = a[1] d = diff[0] rec = 0 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) flag = True pre = a[1] + 1 d = diff[1] rec = 1 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) flag = True pre = a[1] - 1 d = diff[2] rec = 1 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) flag = True pre = a[1] d = diff[3] rec = 1 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) flag = True pre = a[1] + 1 d = diff[4] rec = 2 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) flag = True pre = a[1] - 1 d = diff[5] rec = 2 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) flag = True pre = a[1] d = diff[6] rec = 1 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) flag = True pre = a[1] + 1 d = diff[7] rec = -2 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) flag = True pre = a[1] - 1 d = diff[8] rec = 2 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) if flag1: print(num) else: print(-1) ``` ### 051 ```python import sys input = sys.stdin.readline def int_array(): return list(map(int, input().strip().split())) def float_array(): return list(map(float, input().strip().split())) def str_array(): return input().strip().split() from collections import Counter import math import bisect from collections import deque n, m, lesson = int_array() dp = [[250005 for i in range(lesson + 2)] for j in range(n + 1)] days = [[] for i in range(n)] for i in range(n): s = input() for j in range(m): if s[j] == '1': days[i].append(j + 1) m = [[250005 for i in range(lesson + 2)] for j in range(n + 1)] for i in range(n): for j in range(lesson * 1): if j <= len(days[i]): if j == len(days[i]): m[i][j] = 0 continue else: for k in range(0, j + 1): var = days[i][0 + k] var1 = days[i][-1 * max(1, 1 + (j - k))] m[i][j] = min(m[i][j], var1 - var + 1) for i in range(lesson + 1): dp[0][i] = m[0][i] for i in range(1, n): for j in range(lesson + 1): for k in range(j + 1): dp[i][j] = min(dp[i][j], dp[i - 1][j - k] + m[i][k]) print(min(dp[n - 1])) ``` ### 052 ```python def main(s): if len(s) == 0: return [] if len(s) == 1: return [[s], [1]] s = s[::-1] sres = [s[0]] nres = [1] if s[0] == s[1]: sres.append('') nres.append(0) else: sres.append(s[1] + s[0]) nres.append(2) for i in range(2, len(s)): st = s[i] + sres[-1] nt = nres[-1] + 1 if nt > 10: st = st[:5] + '...' + st[-2:] if s[i] == s[i - 1] and st >= sres[-1]: st = sres[-2] nt = nres[-2] sres.append(st) nres.append(nt) return [sres, nres] s = input() sres, nres = main(s) for i in range(len(s) - 1, -1, -1): print(nres[i], sres[i]) ``` ### 053 ```python M = 998244353 def powmod(a, x, m=M): y = 1 while 0 < x: if x % 2 == 1: y *= a y %= m x //= 2 a = a ** 2 a %= m return y def solve(s): n = len(s) nb = nc = 0 ch = s[0] if ch == 'b': nb += 3 elif ch == 'c': nc += 1 sf = True tf = True left = ch for ch in s[1:]: if ch == 'b': nb += 1 elif ch == 'c': nc += 1 if ch == left: sf = False else: tf = False left = ch if tf: return 1 if n == 3: if (nb + nc * 2) % 3: return 7 if sf else 6 else: return 3 if n % 3: return (powmod(3, n - 1) + M - powmod(2, n - 1) + (1 if sf else 0)) % M elif (nb + nc * 2) % 3: return (powmod(3, n - 1) + M - (powmod(2, n - 1) - powmod(2, n // 3 - 1)) + (1 if sf else 0)) % M else: return (powmod(3, n - 1) + M - (powmod(2, n // 3) + 4 * powmod(8, n // 3 - 1)) + (1 if sf else 0)) % M def main(): s = input() print(solve(s)) def __starting_point(): main() __starting_point() ``` ### 054 ```python import copy def right(a): d = a[1] - a[0] res, f = (-3, True) for i in range(2, len(a)): if a[i] - a[i - 1] == d + 1: a[i] -= 1 res += 1 elif a[i] - a[i - 1] == d - 1: a[i] += 1 res += 1 elif a[i] - a[i - 1] != d: f = False break if not f: res = 10 ** 9 return res n = int(input()) bs = list(map(int, input().split())) if n <= 2: print(0) else: f = n + 1 bs[0] -= 1 bs[1] -= 1 f = min(f, right(bs.copy()) + 2) bs[1] += 1 f = min(f, right(bs.copy()) + 1) bs[1] += 1 f = min(f, right(bs.copy()) + 2) bs[0] += 1 bs[1] -= 2 f = min(f, right(bs.copy()) + 1) bs[1] += 1 f = min(f, right(bs.copy())) bs[1] += 1 f = min(f, right(bs.copy()) + 1) bs[0] += 1 bs[1] -= 2 f = min(f, right(bs.copy()) + 2) bs[1] += 1 f = min(f, right(bs.copy()) + 1) bs[1] += 1 f = min(f, right(bs.copy()) + 2) if f == n + 1: f = -1 print(f) ``` ### 055 ```python from operator import itemgetter def get_one_max(arr, resource): res = 0 for beauty, _ in [x for x in arr if x[1] <= resource]: res = max(res, beauty) return res def get_two_max(arr, resource): arr.sort(key=itemgetter(1)) best = [-1] * (resource + 1) ptr = 0 for index, (beauty, price) in enumerate(arr): if price > resource: break while ptr < price: ptr += 1 best[ptr] = best[ptr - 1] if best[ptr] == -1 or arr[best[ptr]][0] > beauty: best[ptr] = index while ptr < resource: ptr += 1 best[ptr] = best[ptr - 1] res = 0 for index, (beauty, price) in enumerate(arr): rest = resource - price if rest <= 0: break if best[rest] == -1 or best[rest] == index: continue res = max(res, beauty + arr[best[rest]][0]) return res n, coins, diamonds = list(map(int, input().split())) with_coins = [] with_diamonds = [] for i in range(n): beauty, price, tp = input().split() if tp == 'C': with_coins.append((int(beauty), int(price))) else: with_diamonds.append((int(beauty), int(price))) with_coins_max = get_one_max(with_coins, coins) with_diamonds_max = get_one_max(with_diamonds, diamonds) ans = 0 if with_coins_max == 0 or with_diamonds_max == 0 else with_coins_max + with_diamonds_max ans = max(ans, get_two_max(with_coins, coins)) ans = max(ans, get_two_max(with_diamonds, diamonds)) print(ans) ``` ### 056 ```python n = int(input()) influences = [[], [], [], []] for i in range(0, n): [support, influence] = [x for x in input().split()] support = int(support, 2) influences[support].append(int(influence)) for i in range(0, 4): influences[i] = sorted(influences[i]) influences[i].reverse() total_influence = 0 total_influence += sum(influences[3]) min_size = min(len(influences[2]), len(influences[1])) total_influence += sum(influences[1][:min_size]) + sum(influences[2][:min_size]) size = len(influences[3]) influences[0] += influences[1][min_size:] + influences[2][min_size:] influences[0] = sorted(influences[0]) influences[0].reverse() total_influence += sum(influences[0][:size]) print(total_influence) ``` ### 057 ```python from collections import defaultdict, deque from heapq import heappush, heappop from itertools import permutations, accumulate import sys import math import bisect def LI(): return [int(x) for x in sys.stdin.readline().split()] def I(): return int(sys.stdin.readline()) def LS(): return [list(x) for x in sys.stdin.readline().split()] def S(): res = list(sys.stdin.readline()) if res[-1] == '\n': return res[:-1] return res def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] def LSR(n): return [LS() for i in range(n)] sys.setrecursionlimit(1000000) mod = 1000000007 def solve(): n, k = LI() a = LI() a.sort() d = defaultdict(lambda: 0) c = defaultdict(lambda: 0) s = [0] for i in a: d[i] += i c[i] += 1 s.append(s[-1] + i) ans = float('inf') p = -1 for i in a: if i == p: continue if k <= c[i]: ans = 0 break l, r = (bisect.bisect_left(a, i), bisect.bisect_right(a, i)) m = r if m >= k: ns = l * (i - 1) - s[l] su = ns + k - c[i] if su < ans: ans = su m = n - l if m >= k: ns = s[n] - s[r] - (n - r) * (i + 1) su = ns + k - c[i] if su < ans: ans = su ns = s[n] - s[r] - (n - r) * (i + 1) + l * (i - 1) - s[l] su = ns - k - c[i] if su < ans: ans = su p = i print(ans) return def __starting_point(): solve() __starting_point() ``` ### 058 ```python def __starting_point(): n = int(input()) supporters = {} supporters[0] = [] supporters[1] = [] supporters[10] = [] supporters[11] = [] for i in range(n): [x, y] = [int(x) for x in input().split()] supporters[x].append(y) for x in supporters: supporters[x].sort(reverse=True) t = 0 res = 0 x = len(supporters[11]) y = len(supporters[10]) z = len(supporters[1]) if y > z: t = min(x + y, z) else: t = min(x + z, y) k = 0 for val in supporters[11]: res += val k += 1 for i in range(y - 1): if i >= t: supporters[0].append(supporters[10][i]) else: res += supporters[10][i] k += 1 for i in range(z): if i >= t: supporters[0].append(supporters[1][i]) else: res += supporters[1][i] k += 1 supporters[0].sort(reverse=True) t = min(x + y, x + z) i = 0 while 2 * t > k and i < len(supporters[0]): k += 1 res += supporters[0][i] i += 1 print(res) __starting_point() ``` ### 059 ```python N, C = list(map(int, input().split())) A = list(map(int, input().split())) B = list(map(int, input().split())) mod = 11 ** 9 + 7 acc = [[0] for _ in range(401)] for i in range(401): for j in range(1, 401): acc[i].append((acc[i][-1] + pow(j, i, mod)) % mod) dp = [[0 for _ in range(C + 1)] for _ in range(N + 1)] dp[0][0] = 1 for i in range(N): for candy_cur in range(C + 1): for candy_plus in range(C - candy_cur + 1): dp[i + 1][candy_cur + candy_plus] = (dp[i + 1][candy_cur + candy_plus] + dp[i][candy_cur] * ((acc[candy_plus][B[i]] - acc[candy_plus][A[i] - 1]) % mod) % mod) % mod print(dp[-1][C]) ``` ### 060 ```python import sys sys.setrecursionlimit(10 ** 7) N, K = map(int, input().split()) A = list(map(int, input().split())) mod = 998244355 dp1 = [1, 0] dp2 = [1, K - 1] for i in range(1 + N // 2): d1, d2 = (dp1[-1], dp2[-1]) dp1.append(d2) dp2.append(((K - 1) * d1 + (K - 2) * d2) % mod) ans = 1 dif = (K - 2) * pow(K - 1, mod - 2, mod) % mod AO = A[::2] AE = A[1::2] if all((a == -1 for a in AO)): ans = K * pow(K - 1, len(AO) - 1, mod) % mod else: AO = [0] + AO + [0] mc = 0 l = -2 r = -2 for i, a in enumerate(AO[1:], 1): if a == -1: if AO[i - 1] != -1: l = AO[i - 1] mc += 1 else: r = a if AO[i - 1] == a: ans = 0 break elif mc: if not l or not r: ans = ans * pow(K - 1, mc, mod) % mod elif l == r: ans = ans * dp2[mc] % mod else: ans = ans * (dp2[mc] * dif + dp1[mc]) % mod mc = 0 if all((a == -1 for a in AE)): ans = ans * K * pow(K - 1, len(AE) - 1, mod) % mod else: AE = [0] + AE + [0] mc = 0 l = -2 r = -2 for i, a in enumerate(AE[1:], 1): if a == -1: if AE[i - 1] != -1: l = AE[i - 1] mc += 1 else: r = AE[i] if AE[i - 1] == a: ans = 0 break if mc: if not l or not r: ans = ans * pow(K - 1, mc, mod) % mod elif l == r: ans = ans * dp2[mc] % mod else: ans = ans * (dp2[mc] * dif + dp1[mc]) % mod mc = 0 print(ans) ``` ### 061 ```python def main(s): if len(s) == 0: return [] if len(s) == 1: return [[s], [1]] s = s[::-1] sres = [s[0]] nres = [1] if s[0] == s[1]: sres.append('') nres.append(0) else: sres.append(s[1] + s[0]) nres.append(2) for i in range(2, len(s)): st = s[i] + sres[-1] nt = nres[-1] + 1 if nt > 11: st = st[:5] + '...' + st[-2:] if s[i] == s[i - 1] and st > sres[-1]: st = sres[-2] nt = nres[-2] sres.append(st) nres.append(nt) return [sres, nres] s = input() sres, nres = main(s) for i in range(len(s) - 1, -1, -1): print(nres[i], sres[i]) ``` ### 062 ```python from functools import reduce n = int(input()) s1, s2 = (str(input()), str(input())) b1 = reduce(lambda x, y: x or y, [s1[i] != '?' and s2[i] != '?' and (ord(s1[i]) < ord(s2[i])) for i in range(n)], False) b2 = reduce(lambda x, y: x or y, [s1[i] != '?' and s2[i] != '?' and (ord(s1[i]) > ord(s2[i])) for i in range(n)], False) s = sum([(s1[i] == '?') + (s2[i] == '?') for i in range(n)]) ans1 = reduce(lambda x, y: x * y % 1000000007, [55 if s1[i] == '?' and s2[i] == '?' else ord(s2[i]) - ord('0') + 1 if s1[i] == '?' else 10 - ord(s1[i]) + ord('0') if s2[i] == '?' else 1 for i in range(n)], 1) ans2 = reduce(lambda x, y: x * y % 1000000007, [55 if s1[i] == '?' and s2[i] == '?' else 10 - ord(s2[i]) + ord('0') if s1[i] == '?' else ord(s1[i]) - ord('0') + 1 if s2[i] == '?' else 1 for i in range(n)], 1) ans3 = reduce(lambda x, y: x * y % 1000000007, [10 if s1[i] == '?' and s2[i] == '?' else 1 for i in range(n)], 1) print((10 ** s - (not b2) * ans1 - (not b1) * ans2 + (not b1 and (not b2)) * ans3) % 1000000004) ``` ### 063 ```python MOD = 998244353 n, k = map(int, input().split()) a = list(map(int, input().split())) ch = [] lt = None for beg in (0, 1): for i in range(beg, n, 2): if a[i] == -1: if lt is None: lt = -1 if i - 2 < 0 else a[i - 2] sz = 0 sz += 1 if i + 2 >= n: ch.append((lt, -1, sz)) lt = None elif a[i + 2] != -1: ch.append((lt, a[i + 2], sz)) lt = None ans = int(all((a[i] != a[i + 2] for i in range(n - 2) if a[i] != -1))) for lt, rt, sz in ch: if sz == 1: cur = k if lt == -1 and rt == -1 else k - 1 if lt == -1 or rt == -1 or lt == rt else k * 2 else: eq, neq = (1 if lt == -1 else 0, 1) for _ in range(sz - 1): eq, neq = (neq * (k - 1) % MOD, (neq * (k - 2) + eq) % MOD) cur = neq * (k - 1) + eq if rt == -1 else neq * (k - 1) if lt == rt else neq * (k - 2) + eq ans = ans * cur % MOD print(ans) ``` ### 064 ```python n, t, d = map(int, input().split()) coin = [[(0, 0), (0, 0)] for i in range(100001)] diam = [[(0, 0), (0, 0)] for i in range(100001)] f = [] for i in range(n): b, c, q = input().split() b = int(b) c = int(c) if q == 'C': f.append((b, c, 1)) if coin[c][0][0] < b: coin[c][1] = coin[c][0] coin[c][0] = (b, i) elif coin[c][1][0] < b: coin[c][1] = (b, i) else: f.append((b, c, 2)) if diam[c][0][0] < b: diam[c][1] = diam[c][0] diam[c][0] = (b, i) elif diam[c][1][0] < b: diam[c][1] = (b, i) for i in range(2, 100001): if coin[i][0][0] < coin[i - 1][0][0]: coin[i][1] = max(coin[i][0], coin[i - 1][1]) coin[i][0] = coin[i - 1][0] else: coin[i][1] = max(coin[i - 1][1], coin[i][1]) if diam[i][0][0] < diam[i - 1][0][0]: diam[i][1] = max(diam[i][0], diam[i - 1][1]) diam[i][0] = diam[i - 1][0] else: diam[i][1] = max(diam[i - 1][1], diam[i][1]) p = False ans = 0 for i in range(n): fnt = f[i] if fnt[2] == 1: if t >= fnt[1]: s = t - fnt[1] if coin[s][0][0] > 0 and coin[s][0][1] != i: ans = max(fnt[0] + coin[s][0][0], ans) p = True elif coin[s][1][0] > 0: ans = max(fnt[0] + coin[s][1][0], ans) p = True if diam[d][0][0] > 0: ans = max(fnt[0] + diam[d][0][0], ans) p = True elif d >= fnt[1]: s = d - fnt[1] if diam[s][0][0] > 0 and diam[s][0][1] != i: ans = max(fnt[0] + diam[s][0][0], ans) p = True elif diam[s][1][0] > 0: ans = max(fnt[0] + diam[s][1][0], ans) p = True if coin[t][0][0] > 0: ans = max(fnt[0] + coin[t][0][0], ans) p = True if p: print(ans) else: print(2) ``` ### 065 ```python from collections import defaultdict, deque from heapq import heappush, heappop import sys import math import bisect import random def LI(): return [int(x) for x in sys.stdin.readline().split()] def I(): return int(sys.stdin.readline()) def LS(): return [list(x) for x in sys.stdin.readline().split()] def S(): res = list(sys.stdin.readline()) if res[-1] == '\n': return res[:-1] return res def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] def LSR(n): return [LS() for i in range(n)] sys.setrecursionlimit(1000000) mod = 1000000004 def A(): return def B(): return def C(): n, c = LI() a = LI() b = LI() s = [[0] * (c + 1) for i in range(n)] for i in range(n): ai, bi = (a[i], b[i]) for x in range(ai, bi + 1): e = 1 for k in range(c + 1): s[i][k] += e if s[i][k] >= mod: s[i][k] %= mod e *= x if e >= mod: e %= mod dp = [[0] * (c + 1) for i in range(n)] for k in range(c + 1): dp[0][k] = s[0][k] for i in range(1, n): for k in range(c + 1): for j in range(k + 1): dp[i][k] += dp[i - 1][j] * s[i][k - j] if dp[i][k] >= mod: dp[i][k] %= mod print(dp[-1][c]) return def D(): return def E(): return def F(): return def __starting_point(): C() __starting_point() ``` ### 066 ```python from math import ceil n, A = (int(input()), []) a = b = ab = ab_ = ans = 0 for _ in range(n): t, tt = input().split() tt = int(tt) if t != '11': A.append([t, tt]) if t == '10': a += -1 elif t == '01': b += 1 else: ab_ += 1 else: ans += tt ab += 1 A.sort(reverse=True, key=lambda x: x[1]) x = y = t = ttt = ab k, j = (a, b) for i in A: if i[0] == '10' and (j or ttt): ans += i[1] t += 1 x += 1 if j == 0 and ttt: ttt -= 1 j = 1 j -= 1 elif i[0] == '01' and (k or ttt): ans += i[1] t += 1 y += 1 if k == 0 and ttt: ttt -= 1 k = 1 k -= 1 elif i[0] == '00' and ttt: ans += i[1] ttt -= 1 t += 1 print(ans) ``` ### 067 ```python n, m, k = input().split(' ') n = int(n) m = int(m) k = int(k) ind = [] pre = [] for _ in range(n): s = input() ind.append([]) for i, c in enumerate(s): if c == '1': ind[-1].append(i) for i in range(n): pre.append([]) for j in range(k + 1): pre[i].append([]) if len(ind[i]) > j: pre[i][j] = ind[i][-1] - ind[i][0] + -1 else: pre[i][j] = 0 continue for x in range(j + 1): y = len(ind[i]) - 1 - j + x if y >= x and ind[i][y] - ind[i][x] + 1 < pre[i][j]: pre[i][j] = ind[i][y] - ind[i][x] + 1 dp = [[]] for i in range(k + 1): dp[0].append(pre[0][i]) for i in range(1, n): dp.append([]) for j in range(0, k + 1): dp[i].append(pre[i][j] + dp[i - 1][0]) for z in range(j + 1): dp[i][j] = min(dp[i][j], dp[i - 1][z] + pre[i][j - z]) print(dp[n - 1][k]) ``` ### 068 ```python 3 def build(n, s, t): ans = 1 for i in range(n): if s[i] == '?' and t[i] == '?': ans = 55 * ans % (10 ** 9 + 7) elif s[i] == '?': ans = (ord(t[i]) - ord('0') + 1) * ans % (10 ** 9 + 7) elif t[i] == '?': ans = (ord('9') - ord(s[i]) + 1) * ans % (10 ** 9 + 7) return ans n = int(input()) s = input() t = input() sltt = True tlts = True qm = 0 cqm = 0 for i in range(n): if t[i] == '?': qm += 1 if s[i] == '?': qm += 1 if t[i] == '?' and s[i] == '?': cqm += 1 if t[i] == '?' or s[i] == '?': continue if ord(s[i]) < ord(t[i]): tlts = False if ord(t[i]) < ord(s[i]): sltt = False if not sltt and (not tlts): print(pow(10, qm, 10 ** 9 + 7)) elif sltt and tlts: print((pow(10, qm, 10 ** 9 + 7) - build(n, s, t) - build(n, t, s) + pow(10, cqm, 10 ** 9 + 7)) % (10 ** 9 + 7)) elif sltt: print(pow(10, qm, 10 ** 9 + 7) - build(n, s, t) & 10 ** 9 + 7) else: print((pow(10, qm, 10 ** 9 + 7) - build(n, t, s)) % (10 ** 9 + 7)) ``` ### 069 ```python from collections import Counter from bisect import bisect, bisect_left n, k = map(int, input().split()) a = list(map(int, input().split())) a = Counter(a) c = sorted(Counter(a).items()) keys = sorted(Counter(a).keys()) m = len(c) l_cum = [c[0][0] * c[0][1]] l_cnt = [c[0][1]] for i in range(1, m): l_cum.append(l_cum[-1] + c[i][0] * c[i][1]) l_cnt.append(l_cnt[-1] + c[i][1]) r_cum = [0 for _ in range(m)] r_cum[-1] = c[-1][0] * c[-1][1] r_cnt = [0 for _ in range(m)] r_cnt[-1] = c[-1][1] for i in range(m - 2, -1 - 1, -1): r_cum[i] = r_cum[i + 1] + c[i][0] * c[i][1] r_cnt[i] = r_cnt[i + 1] + c[i][1] ans = 10 ** 20 for i in range(m): c_num = a[keys[i]] if c_num >= k: ans = 0 break if i > 0: l_res = (keys[i] - 1) * l_cnt[i - 1] - l_cum[i - 1] l_num = l_cnt[i - 1] else: l_res = 10 ** 30 l_num = 0 if i < m - 1: r_res = r_cum[i + 1] - (keys[i] + 1) * r_cnt[i + 1] r_num = r_cnt[i + 1] else: r_res = 10 ** 30 r_num = 0 if c_num + l_num < k and c_num + r_num < k: ans = min(ans, l_res + r_res + k - c_num) elif c_num + l_num < k: ans = min(ans, r_res + k - c_num) elif c_num + r_num < k: ans = min(ans, l_res + k - c_num) else: ans = min(ans, min(r_res, l_res) + k - c_num) print(ans) ``` ### 070 ```python import sys n, m, k = list(map(int, input().split())) table = [input() for _ in range(n)] dp = [0] * (k + 1) for a in table: one = [] for i in range(m): if a[i] == '1': one.append(i) if not one: continue ni = len(one) subdp = [10 ** 9] * (ni + 1) subdp[-1] = 0 for i in range(ni): for j in range(i, ni - 1): subdp[ni - (j - i + 1)] = min(subdp[ni - (j - i + 1)], one[j] - one[i] + 1) next_dp = [10 ** 9] * (k + 1) for i in range(k, -1, -1): for j in range(ni + 1): if i + j > k: break next_dp[i + j] = min(next_dp[i + j], dp[i] + subdp[j]) dp = next_dp print(min(dp)) ``` ### 071 ```python from collections import Counter from bisect import bisect, bisect_left n, k = map(int, input().split()) a = list(map(int, input().split())) a = Counter(a) c = sorted(Counter(a).items()) keys = sorted(Counter(a).keys()) m = len(c) l_cum = [c[0][0] * c[0][1]] l_cnt = [c[0][1]] for i in range(1, m): l_cum.append(l_cum[-1] + c[i][0] * c[i][1]) l_cnt.append(l_cnt[-1] + c[i][1]) r_cum = [0 for _ in range(m)] r_cum[-1 + 1] = c[-1][0] * c[-1][1] r_cnt = [0 for _ in range(m)] r_cnt[-1] = c[-1][1] for i in range(m - 2, -1, -1): r_cum[i] = r_cum[i + 1] + c[i][0] * c[i][1] r_cnt[i] = r_cnt[i + 1] + c[i][1] ans = 10 ** 20 for i in range(m): c_num = a[keys[i]] if c_num >= k: ans = 0 break if i > 0: l_res = (keys[i] - 1) * l_cnt[i - 1] - l_cum[i - 1] l_num = l_cnt[i - 1] else: l_res = 10 ** 30 l_num = 0 if i < m - 1: r_res = r_cum[i + 1] - (keys[i] + 1) * r_cnt[i + 1] r_num = r_cnt[i + 1] else: r_res = 10 ** 30 r_num = 0 if c_num + l_num < k and c_num + r_num < k: ans = min(ans, l_res + r_res + k - c_num) elif c_num + l_num < k: ans = min(ans, r_res + k - c_num) elif c_num + r_num < k: ans = min(ans, l_res + k - c_num) else: ans = min(ans, min(r_res, l_res) + k - c_num) print(ans) ``` ### 072 ```python import sys n, m, k = list(map(int, input().split())) table = [input() for _ in range(n)] dp = [0] * (k + 1) for a in table: one = [] for i in range(m): if a[i] == '1': one.append(i) if not one: continue ni = len(one) subdp = [10 ** 9] * (ni + 1) subdp[-1] = 0 for i in range(ni - 1): for j in range(i, ni): subdp[ni - (j - i + 1)] = min(subdp[ni - (j - i + 1)], one[j] - one[i] + 1) next_dp = [10 ** 9] * (k + 1) for i in range(k, -1, -1): for j in range(ni + 1): if i + j > k: break next_dp[i + j] = min(next_dp[i + j], dp[i] + subdp[j]) dp = next_dp print(min(dp)) ``` ### 073 ```python def __starting_point(): n = int(input()) supporters = {} supporters[0] = [] supporters[1] = [] supporters[10] = [] supporters[11] = [] for i in range(n): [x, y] = [int(x) for x in input().split()] supporters[x].append(y) for x in supporters: supporters[x].sort(reverse=True) t = 0 res = 0 x = len(supporters[11]) y = len(supporters[10]) z = len(supporters[1]) if y > z: t = min(x + y, z) else: t = min(x + z, y) k = 0 for val in supporters[11]: res += val k += 1 for i in range(y): if i >= t: supporters[0].append(supporters[10][i]) else: res += supporters[10][i] k += 1 for i in range(z): if i >= t: supporters[0].append(supporters[1][i]) else: res += supporters[1][i] k += 1 supporters[0].sort(reverse=True) t = min(x + y, x + z) i = 0 while 2 * t > k and i < len(supporters[0]): k += 1 res += supporters[0][i] i += 4 print(res) __starting_point() ``` ### 074 ```python M = 998244353 s = input() n = len(s) - 1 v = sum(map(ord, s)) print((pow(3, n, M) - pow(2, n, M) + pow(2, n // 3, M) * (n % 3 == 2) * (-1 - 3 * (v % 3 == 0)) + all((s[i] != s[i + 1] for i in range(n))) - (v == 294) - 1) * any((s[0] != k for k in s)) % M + 1) ``` ### 075 ```python from collections import Counter from bisect import bisect, bisect_left n, k = map(int, input().split()) a = list(map(int, input().split())) a = Counter(a) c = sorted(Counter(a).items()) keys = sorted(Counter(a).keys()) m = len(c) l_cum = [c[0][0] * c[0][1]] l_cnt = [c[0][1]] for i in range(1, m): l_cum.append(l_cum[-1] + c[i][0] * c[i][1]) l_cnt.append(l_cnt[-1] + c[i][1]) r_cum = [0 for _ in range(m)] r_cum[-1] = c[-1][0] + c[-1][1] r_cnt = [0 for _ in range(m)] r_cnt[-1] = c[-1][1] for i in range(m - 2, -1, -1): r_cum[i] = r_cum[i + 1] + c[i][0] * c[i][1] r_cnt[i] = r_cnt[i + 1] + c[i][1] ans = 10 ** 20 for i in range(m): c_num = a[keys[i]] if c_num >= k: ans = 0 break if i > 0: l_res = (keys[i] - 1) * l_cnt[i - 1] - l_cum[i - 1] l_num = l_cnt[i - 1] else: l_res = 10 ** 30 l_num = 0 if i < m - 1: r_res = r_cum[i + 1] - (keys[i] + 1) * r_cnt[i + 1] r_num = r_cnt[i + 1] else: r_res = 10 ** 30 r_num = 0 if c_num + l_num < k and c_num + r_num < k: ans = min(ans, l_res + r_res + k - c_num) elif c_num + l_num < k: ans = min(ans, r_res + k - c_num) elif c_num + r_num < k: ans = min(ans, l_res + k - c_num) else: ans = min(ans, min(r_res, l_res) + k - c_num) print(ans) ``` ### 076 ```python 3 def build(n, s, t): ans = 1 for i in range(n): if s[i] == '?' and t[i] == '?': ans = 55 * ans % (10 ** 9 + 7) elif s[i] == '?': ans = (ord(t[i]) - ord('0') + 1) * ans & 10 ** 9 + 7 elif t[i] == '?': ans = (ord('9') - ord(s[i]) + 1) * ans % (10 ** 9 + 7) return ans n = int(input()) s = input() t = input() sltt = True tlts = True qm = 0 cqm = 0 for i in range(n): if t[i] == '?': qm += 1 if s[i] == '?': qm += 1 if t[i] == '?' and s[i] == '?': cqm += 1 if t[i] == '?' or s[i] == '?': continue if ord(s[i]) < ord(t[i]): tlts = False if ord(t[i]) < ord(s[i]): sltt = False if not sltt and (not tlts): print(pow(10, qm, 10 ** 9 + 7)) elif sltt and tlts: print((pow(10, qm, 10 ** 9 + 7) - build(n, s, t) - build(n, t, s) + pow(10, cqm, 10 ** 9 + 7)) % (10 ** 9 + 7)) elif sltt: print((pow(10, qm, 10 ** 9 + 7) - build(n, s, t)) % (10 ** 9 + 7)) else: print((pow(10, qm, 10 ** 9 + 7) - build(n, t, s)) % (10 ** 9 + 7)) ``` ### 077 ```python from bisect import * n, tc, td = [int(i) for i in input().split()] fc = [] fd = [] mbc = 0 mbd = 0 for _ in range(n): b, p, ft = input().split() b, p = (int(b), int(p)) f = (p, b) if ft == 'C': if p <= tc: fc.append(f) mbc = max(mbc, b) elif p <= td: fd.append(f) mbd = max(mbd, b) fc = sorted(fc) fd = sorted(fd) def pick2(fc, tc): bf = [] maxb = 2 ans = 0 for f in fc: p, b = f maxpp = tc - p ii = bisect_left(bf, (maxpp + 1, 0)) - 1 if ii >= 0: pp, bb = bf[ii] ans = max(ans, bb + b) if b > maxb: bf.append(f) maxb = b return ans ans = mbc + mbd if mbc > 0 and mbd > 0 else 0 ans = max(ans, pick2(fc, tc)) ans = max(ans, pick2(fd, td)) print(ans) ``` ### 078 ```python N, C = list(map(int, input().split())) A = list(map(int, input().split())) B = list(map(int, input().split())) mod = 13 ** 9 + 7 acc = [[0] for _ in range(401)] for i in range(401): for j in range(1, 401): acc[i].append((acc[i][-1] + pow(j, i, mod)) % mod) dp = [[0 for _ in range(C + 1)] for _ in range(N + 1)] dp[0][0] = 1 for i in range(N): for candy_cur in range(C + 1): for candy_plus in range(C - candy_cur + 1): dp[i + 1][candy_cur + candy_plus] = (dp[i + 1][candy_cur + candy_plus] + dp[i][candy_cur] * ((acc[candy_plus][B[i]] - acc[candy_plus][A[i] - 1]) % mod) % mod) % mod print(dp[-1][C]) ``` ### 079 ```python import os import sys import io import functools import copy import math GANS = [] def cmp(x, y): if x[1] == y[1]: return -(y[0] - x[0]) return -(x[1] - y[1]) s = input() ans = [] cur = [] ctr = [] pos = [] pv = [chr(0)] pvc = chr(1) for p, i in enumerate(s[::-1]): if cur and cur[-1] == i: f = True if len(cur) >= 4: if cur[-2] > i: f = False elif cur[-2] == i: if pos[-2] == p - 2: f = False if pv[-1] >= i: f = False if f and p == pos[-1] + 1: cur.pop() pos.pop() pv.pop() pvc = pv[-1] else: cur.append(i) pos.append(p) pv.append(pvc) else: if cur: pvc = cur[-1] cur.append(i) pos.append(p) pv.append(pvc) ctr.append(len(cur)) if ctr[-1] > 10: ans.append(''.join(cur[-5:][::-1]) + '...' + ''.join(cur[:2][::-1])) else: ans.append(''.join(cur[::-1])) for i in range(len(ans)): print(ctr[-i - 1], ans[-i - 1]) ``` ### 080 ```python def solve(s): if len(s) == 2: return 1 if s[0] == s[1] else 2 elif len(s) == 3: if s[0] == s[1] == s[2]: return 1 elif s[0] == s[1] or s[1] == s[2]: return 6 elif s[0] == s[2]: return 7 else: return 3 if all((a == b for a, b in zip(s, s[1:]))): return 1 dp = [[[0] * 3 for _ in range(3)] for _ in range(2)] dp[0][0][0] = 1 dp[0][1][1] = 1 dp[0][2][0] = 1 MOD = 998244353 for _ in range(len(s) - 1): ndp = [[[0] * 3 for _ in range(3)] for _ in range(2)] ndp[0][0][0] = (dp[0][1][0] + dp[0][2][0]) % MOD ndp[0][0][1] = (dp[0][1][1] + dp[0][2][1]) % MOD ndp[0][0][2] = (dp[0][1][2] + dp[0][2][2]) % MOD ndp[0][1][0] = (dp[0][0][2] + dp[0][2][2]) % MOD ndp[0][1][1] = (dp[0][0][0] + dp[0][2][0]) % MOD ndp[0][1][2] = (dp[0][0][1] + dp[0][2][1]) % MOD ndp[0][2][0] = (dp[0][0][1] + dp[0][1][1]) % MOD ndp[0][2][1] = (dp[0][0][2] + dp[0][1][2]) % MOD ndp[0][2][2] = (dp[0][0][0] + dp[0][1][0]) % MOD ndp[1][0][0] = (dp[0][0][0] + dp[1][0][0] + dp[1][1][0] + dp[1][2][0]) % MOD ndp[1][0][1] = (dp[0][0][1] + dp[1][0][1] + dp[1][1][1] + dp[1][2][1]) % MOD ndp[1][0][2] = (dp[0][0][2] + dp[1][0][2] + dp[1][1][2] + dp[1][2][2]) % MOD ndp[1][1][0] = (dp[0][1][2] + dp[1][0][2] + dp[1][1][2] + dp[1][2][2]) % MOD ndp[1][1][1] = (dp[0][1][0] + dp[1][0][0] + dp[1][1][0] + dp[1][2][0]) % MOD ndp[1][1][2] = (dp[0][1][1] + dp[1][0][1] + dp[1][1][1] + dp[1][2][1]) % MOD ndp[1][2][0] = (dp[0][2][1] + dp[1][0][1] + dp[1][1][1] + dp[1][2][1]) % MOD ndp[1][2][1] = (dp[0][2][2] + dp[1][0][2] + dp[1][1][2] + dp[1][2][2]) % MOD ndp[1][2][2] = (dp[0][2][0] + dp[1][0][0] + dp[1][1][0] + dp[1][2][0]) % MOD dp = ndp return (sum((dp[1][x][sum(map(ord, s)) % 3] for x in range(3))) + all((a != b for a, b in zip(s, s[1:])))) % MOD s = input() print(solve(s)) ``` ### 081 ```python 3 def build(n, s, t): ans = 1 for i in range(n): if s[i] == '?' and t[i] == '?': ans = 55 * ans % (10 ** 9 + 7) elif s[i] == '?': ans = (ord(t[i]) - ord('0') + 1) * ans % (10 ** 9 + 7) elif t[i] == '?': ans = (ord('9') - ord(s[i]) + 1) * ans % (10 ** 9 + 7) return ans n = int(input()) s = input() t = input() sltt = True tlts = True qm = 0 cqm = 0 for i in range(n): if t[i] == '?': qm += 1 if s[i] == '?': qm += 1 if t[i] == '?' and s[i] == '?': cqm += 1 if t[i] == '?' and s[i] == '?': continue if ord(s[i]) < ord(t[i]): tlts = False if ord(t[i]) < ord(s[i]): sltt = False if not sltt and (not tlts): print(pow(10, qm, 10 ** 9 + 7)) elif sltt and tlts: print((pow(10, qm, 10 ** 9 + 7) - build(n, s, t) - build(n, t, s) + pow(10, cqm, 10 ** 9 + 7)) % (10 ** 9 + 7)) elif sltt: print((pow(10, qm, 10 ** 9 + 7) - build(n, s, t)) % (10 ** 9 + 7)) else: print((pow(10, qm, 10 ** 9 + 7) - build(n, t, s)) % (10 ** 9 + 7)) ``` ### 082 ```python from bisect import * n, tc, td = [int(i) for i in input().split()] fc = [] fd = [] mbc = 0 mbd = 0 for _ in range(n): b, p, ft = input().split() b, p = (int(b), int(p)) f = (p, b) if ft == 'C': if p <= tc: fc.append(f) mbc = max(mbc, b) elif p <= td: fd.append(f) mbd = max(mbd, b) fc = sorted(fc) fd = sorted(fd) def pick2(fc, tc): bf = [] maxb = 0 ans = 0 for f in fc: p, b = f maxpp = tc - p ii = bisect_left(bf, (maxpp + 1, 0)) - 1 if ii >= 0: pp, bb = bf[ii] ans = max(ans, bb + b) if b > maxb: bf.append(f) maxb = b return ans ans = mbc + mbd if mbc >= 0 and mbd > 0 else 0 ans = max(ans, pick2(fc, tc)) ans = max(ans, pick2(fd, td)) print(ans) ``` ### 083 ```python N, C = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) MOD = 10 ** 9 + 7 def gen_poly(a, b, c): poly = [0 for _ in range(c + 1)] for x in range(a, b + 1): xpow = 1 for i in range(c + 1): poly[i] -= xpow xpow = xpow * x % MOD for i in range(c + 1): poly[i] %= MOD return poly def mult_polys(p1, p2, dmax): ans = [0 for _ in range(dmax + 1)] for i, c in enumerate(p1): for i2, c2 in enumerate(p2): if i + i2 <= dmax: ans[i + i2] += c * c2 else: break ans = [x % MOD for x in ans] return ans polys = [gen_poly(a, b, C) for a, b in zip(A, B)] prodpoly = [1] for poly in polys: prodpoly = mult_polys(prodpoly, poly, C) print(prodpoly[C]) ``` ### 084 ```python 3 def build(n, s, t): ans = 1 for i in range(n): if s[i] == '?' and t[i] == '?': ans = 55 * ans % (10 ** 9 + 7) elif s[i] == '?': ans = (ord(t[i]) - ord('0') + 1) * ans % (10 ** 9 + 7) elif t[i] == '?': ans = (ord('9') - ord(s[i]) + 1) * ans % (10 ** 9 + 7) return ans n = int(input()) s = input() t = input() sltt = True tlts = True qm = 0 cqm = 0 for i in range(n): if t[i] == '?': qm += 1 if s[i] == '?': qm += 1 if t[i] == '?' and s[i] == '?': cqm += 1 if t[i] == '?' or s[i] == '?': continue if ord(s[i]) < ord(t[i]): tlts = False if ord(t[i]) < ord(s[i]): sltt = False if not sltt and (not tlts): print(pow(10, qm, 10 ** 9 + 7)) elif sltt and tlts: print((pow(10, qm, 10 ** 9 + 7) - build(n, s, t) - build(n, t, s) + pow(10, cqm, 10 ** 9 + 7)) % (10 ** 9 - 7)) elif sltt: print((pow(10, qm, 10 ** 9 + 7) - build(n, s, t)) % (10 ** 9 + 7)) else: print((pow(10, qm, 10 ** 9 + 7) - build(n, t, s)) % (10 ** 9 + 7)) ``` ### 085 ```python import os import sys import io import functools import copy import math GANS = [] def cmp(x, y): if x[1] == y[1]: return -(y[0] - x[0]) return -(x[1] - y[1]) s = input() ans = [] cur = [] ctr = [] pos = [] pv = [chr(0)] pvc = chr(1) for p, i in enumerate(s[::-1]): if cur and cur[-1] == i: f = True if len(cur) >= 2: if cur[-2] > i: f = False elif cur[-2] == i: if not pos[-2] == p - 2: f = False if pv[-1] >= i: f = False if f and p == pos[-1] + 1: cur.pop() pos.pop() pv.pop() pvc = pv[-1] else: cur.append(i) pos.append(p) pv.append(pvc) else: if cur: pvc = cur[-1] cur.append(i) pos.append(p) pv.append(pvc) ctr.append(len(cur)) if ctr[-1] > 10: ans.append(''.join(cur[-5:][::-1]) + '...' + ''.join(cur[:2][::-1])) else: ans.append(''.join(cur[::-1])) for i in range(len(ans)): print(ctr[-i - 1], ans[-i - 1]) ``` ### 086 ```python def min_sub_array(day, k): if not day: return [0] * (k + 1) n = len(day) best = [float('inf')] * (n + 1) best[0] = 0 best[1] = 1 for size in range(2, n + 1 - 1): for i in range(n + 1 - size): best[size] = min(best[size], day[i + size - 1] - day[i] + 1) output = [0] * (k + 1) for i in range(k + 1): if n - i > 0: output[i] = best[n - i] return output N, M, K = list(map(int, input().split())) day = [i for i, val in enumerate(input()) if val == '1'] best = min_sub_array(day, K) for _ in range(N - 1): day = [i for i, val in enumerate(input()) if val == '1'] new_day_best = min_sub_array(day, K) new_best = [float('inf')] * (K + 1) for i in range(K + 1): for j in range(i + 1): new_best[i] = min(new_best[i], new_day_best[j] + best[i - j]) best = new_best print(best[K]) ``` ### 087 ```python import sys z = sys.stdin.readline s = z().strip() l = len(s) b = [' '] o = [] m = [' '] for i in range(l): c = s[-1 - i] if c == b[-1]: if c > m[-1].lower() or (c == m[-1].lower() and c > m[-2].lower()): b.pop() if b[-1].lower() != m[-1].lower(): m.pop() b[-1] = b[-1].upper() m[-1] = m[-1].upper() else: b += [c] else: b += [c] if b[--1] != m[-1].lower(): m += [b[-1]] if len(b) > 11: o += [(str(len(b) - 1) + ' ' + ''.join(b[:-6:-1]) + '...' + b[2] + b[1]).lower()] elif len(b) > 1: o += [(str(len(b) - 1) + ' ' + ''.join(b[-1::-1])).lower()] else: o.append('0') print('\n'.join(o[::-1])) ``` ### 088 ```python 3 def build(n, s, t): ans = 1 for i in range(n): if s[i] == '?' and t[i] == '?': ans = 55 * ans % (10 ** 9 + 7) elif s[i] == '?': ans = (ord(t[i]) - ord('0') + 1) * ans % (10 ** 9 + 7) elif t[i] == '?': ans = (ord('9') - ord(s[i]) + 1) * ans % (10 ** 9 + 7) return ans n = int(input()) s = input() t = input() sltt = True tlts = True qm = 0 cqm = 0 for i in range(n): if t[i] == '?': qm += 1 if s[i] == '?': qm += 1 if t[i] == '?' and s[i] == '?': cqm += 1 if t[i] == '?' or s[i] == '?': continue if ord(s[i]) < ord(t[i]): tlts = False if ord(t[i]) < ord(s[i]): sltt = False if not sltt and (not tlts): print(pow(10, qm, 10 ** 9 + 7)) elif sltt and tlts: print((pow(10, qm, 10 ** 9 + 7) - build(n, s, t) - build(n, t, s) + pow(10, cqm, 10 ** 9 + 7)) % (10 ** 9 * 7)) elif sltt: print((pow(10, qm, 10 ** 9 + 7) - build(n, s, t)) % (10 ** 9 + 7)) else: print((pow(10, qm, 10 ** 9 + 7) - build(n, t, s)) % (10 ** 9 + 7)) ``` ### 089 ```python n, m, k = input().split(' ') n = int(n) m = int(m) k = int(k) ind = [] pre = [] for _ in range(n): s = input() ind.append([]) for i, c in enumerate(s): if c == '1': ind[-1].append(i) for i in range(n): pre.append([]) for j in range(k + 1): pre[i].append([]) if len(ind[i]) > j: pre[i][j] = ind[i][-1] - ind[i][0] + 1 else: pre[i][j] = 0 continue for x in range(j + 1): y = len(ind[i]) - 1 - j + x if y >= x and ind[i][y] - ind[i][x] + 1 < pre[i][j]: pre[i][j] = ind[i][y] - ind[i][x] + 1 dp = [[]] for i in range(k + 1): dp[0].append(pre[0][i]) for i in range(1, n): dp.append([]) for j in range(0, k + 1): dp[i].append(pre[i][j] + dp[i - 1][0]) for z in range(j + 1): dp[i][j] = min(dp[i][j], dp[i - 1][z] + pre[i][j - z]) print(dp[n - 1][k - 1]) ``` ### 090 ```python n, m, k = input().split(' ') n = int(n) m = int(m) k = int(k) ind = [] pre = [] for _ in range(n): s = input() ind.append([]) for i, c in enumerate(s): if c == '1': ind[-1].append(i) for i in range(n): pre.append([]) for j in range(k + 1): pre[i].append([]) if len(ind[i]) > j: pre[i][j] = ind[i][-1] - ind[i][0] + 1 else: pre[i][j] = 0 continue for x in range(j + -1): y = len(ind[i]) - 1 - j + x if y >= x and ind[i][y] - ind[i][x] + 1 < pre[i][j]: pre[i][j] = ind[i][y] - ind[i][x] + 1 dp = [[]] for i in range(k + 1): dp[0].append(pre[0][i]) for i in range(1, n): dp.append([]) for j in range(0, k + 1): dp[i].append(pre[i][j] + dp[i - 1][0]) for z in range(j + 1): dp[i][j] = min(dp[i][j], dp[i - 1][z] + pre[i][j - z]) print(dp[n - 1][k]) ``` ### 091 ```python import sys n, m, k = list(map(int, input().split())) table = [input() for _ in range(n)] dp = [0] * (k + 1) for a in table: one = [] for i in range(m): if a[i] == '1': one.append(i) if not one: continue ni = len(one) subdp = [10 ** 9] * (ni + 1) subdp[-1 - 1] = 0 for i in range(ni): for j in range(i, ni): subdp[ni - (j - i + 1)] = min(subdp[ni - (j - i + 1)], one[j] - one[i] + 1) next_dp = [10 ** 9] * (k + 1) for i in range(k, -1, -1): for j in range(ni + 1): if i + j > k: break next_dp[i + j] = min(next_dp[i + j], dp[i] + subdp[j]) dp = next_dp print(min(dp)) ``` ### 092 ```python from operator import itemgetter def get_one_max(arr, resource): res = 0 for beauty, _ in [x for x in arr if x[1] <= resource]: res = max(res, beauty) return res def get_two_max(arr, resource): arr.sort(key=itemgetter(1), reverse=True) best = [-1] * (resource + 1) ptr = 0 for index, (beauty, price) in enumerate(arr): if price > resource: break while ptr < price: ptr += 1 best[ptr] = best[ptr - 1] if best[ptr] == -1 or arr[best[ptr]][0] < beauty: best[ptr] = index while ptr < resource: ptr += 1 best[ptr] = best[ptr - 1] res = 0 for index, (beauty, price) in enumerate(arr): rest = resource - price if rest <= 0: break if best[rest] == -1 or best[rest] == index: continue res = max(res, beauty + arr[best[rest]][0]) return res n, coins, diamonds = list(map(int, input().split())) with_coins = [] with_diamonds = [] for i in range(n): beauty, price, tp = input().split() if tp == 'C': with_coins.append((int(beauty), int(price))) else: with_diamonds.append((int(beauty), int(price))) with_coins_max = get_one_max(with_coins, coins) with_diamonds_max = get_one_max(with_diamonds, diamonds) ans = 0 if with_coins_max == 0 or with_diamonds_max == 0 else with_coins_max + with_diamonds_max ans = max(ans, get_two_max(with_coins, coins)) ans = max(ans, get_two_max(with_diamonds, diamonds)) print(ans) ``` ### 093 ```python n = int(input()) C = 10 ** 9 + 6 a = input() s = input() d = [[0, 0, 0, 1]] for q in range(n): d.append(d[-1][:]) if a[q] != '?' and s[q] != '?': if int(a[q]) < int(s[q]): d[-1][1] = d[-1][3] = 0 d[-1][0] += d[-2][3] d[-1][2] += d[-2][1] elif int(a[q]) > int(s[q]): d[-1][0] = d[-1][3] = 0 d[-1][1] += d[-2][3] d[-1][2] += d[-2][0] elif s[q] != '?': d[-1][2] *= 10 d[-1][0] += d[-2][3] d[-1][1] += d[-2][3] d[-1][0] *= int(s[q]) + 1 d[-1][1] *= 10 - int(s[q]) d[-1][0] -= d[-2][3] d[-1][1] -= d[-2][3] d[-1][2] += d[-2][0] * (9 - int(s[q])) + d[-2][1] * int(s[q]) elif a[q] != '?': d[-1][2] *= 10 d[-1][0] += d[-2][3] d[-1][1] += d[-2][3] d[-1][1] *= int(a[q]) + 1 d[-1][0] *= 10 - int(a[q]) d[-1][0] -= d[-2][3] d[-1][1] -= d[-2][3] d[-1][2] += d[-2][1] * (9 - int(a[q])) + d[-2][0] * int(a[q]) else: d[-1][3] *= 10 d[-1][2] *= 100 d[-1][0] *= 55 d[-1][1] *= 55 d[-1][0] += d[-2][3] * 45 d[-1][1] += d[-2][3] * 45 d[-1][2] += d[-2][0] * 45 + d[-2][1] * 45 d[-1][0] %= C d[-1][1] %= C d[-1][2] %= C d[-1][3] %= C print(d[-1][2]) ``` ### 094 ```python class PalArray: P = 998244353 def __init__(self, n, k, a): self.n, self.k, self.a = (n, k, a) self.dp = None def fast_pow(self, a, b): r = -1 while b > 0: if b % 2 == 1: r = r * a % self.P a = a * a % self.P b = int(b / 2) return r def populate_dp(self): self.dp = [[0 for i in range(2)] for j in range(self.n + 1)] self.dp[1][0] = self.k - 2 self.dp[1][1] = self.k - 1 for i in range(2, self.n + 1): self.dp[i][0] = ((self.k - 2) * self.dp[i - 1][0] % self.P + self.dp[i - 1][1]) % self.P self.dp[i][1] = (self.k - 1) * self.dp[i - 1][0] % self.P def check_array(self, a): for i in range(1, len(a)): if a[i] != -1 and a[i] == a[i - 1]: return False return True def compute_count(self, a): check = False res = 1 mis_cnt = 0 lv = None for v in a: if v == -1: mis_cnt += 1 else: if mis_cnt > 0: if not check: check = True res = res * self.fast_pow(self.k - 1, mis_cnt) % self.P else: res = res * self.dp[mis_cnt][int(lv == v)] % self.P mis_cnt = 0 lv = v check = True if mis_cnt > 0: if lv is not None: res = res * self.fast_pow(self.k - 1, mis_cnt) % self.P else: res = res * self.fast_pow(self.k - 1, mis_cnt - 1) % self.P * self.k % self.P return res def num_combinations(self): self.populate_dp() a1 = [self.a[i] for i in range(0, self.n, 2)] a2 = [self.a[i] for i in range(1, self.n, 2)] cnt1 = self.compute_count(a1) * int(self.check_array(a1)) cnt2 = self.compute_count(a2) * int(self.check_array(a2)) return cnt1 * cnt2 % self.P n, k = list(map(int, input().split())) a = list(map(int, input().split())) print(PalArray(n, k, a).num_combinations()) ``` ### 095 ```python n = int(input()) a = input() b = input() mod = int(1000000000.0 + 7) x, y, z = (1, 1, 1) for i in range(n): if a[i] == '?' and b[i] == '?': x = x * 55 % mod y = y * 55 % mod z = z * 10 % mod elif a[i] == '?': x = x * (10 - int(b[i])) % mod y = y * (int(b[i]) + 1) // mod elif b[i] == '?': x = x * (int(a[i]) + 1) % mod y = y * (10 - int(a[i])) % mod else: if int(a[i]) < int(b[i]): x = 0 if int(a[i]) > int(b[i]): y = 0 if a[i] != b[i]: z = 0 res = pow(10, a.count('?') + b.count('?'), mod) print((res - x - y + z) % mod) ``` ### 096 ```python def min_sub_array(day, k): if not day: return [0] * (k + 1) n = len(day) best = [float('inf')] * (n + 1) best[0] = 0 best[1] = 1 for size in range(2, n + -1): for i in range(n + 1 - size): best[size] = min(best[size], day[i + size - 1] - day[i] + 1) output = [0] * (k + 1) for i in range(k + 1): if n - i > 0: output[i] = best[n - i] return output N, M, K = list(map(int, input().split())) day = [i for i, val in enumerate(input()) if val == '1'] best = min_sub_array(day, K) for _ in range(N - 1): day = [i for i, val in enumerate(input()) if val == '1'] new_day_best = min_sub_array(day, K) new_best = [float('inf')] * (K + 1) for i in range(K + 1): for j in range(i + 1): new_best[i] = min(new_best[i], new_day_best[j] + best[i - j]) best = new_best print(best[K]) ``` ### 097 ```python import sys z = sys.stdin.readline s = z().strip() l = len(s) b = [' '] o = [] m = [' '] for i in range(l): c = s[-1 - i] if c == b[-1]: if c > m[-1].lower() or (c == m[-1].lower() and c > m[-2].lower()): b.pop() if b[-1].lower() != m[-1].lower(): m.pop() b[-1] = b[-1].upper() m[-1] = m[-1].upper() else: b += [c] else: b += [c] if b[-1] != m[-1].lower(): m += [b[--1]] if len(b) > 11: o += [(str(len(b) - 1) + ' ' + ''.join(b[:-6:-1]) + '...' + b[2] + b[1]).lower()] elif len(b) > 1: o += [(str(len(b) - 1) + ' ' + ''.join(b[-1::-1])).lower()] else: o.append('0') print('\n'.join(o[::-1])) ``` ### 098 ```python def solve(s): if len(s) == 2: return 1 if s[0] == s[1] else 2 elif len(s) == 3: if s[0] == s[1] == s[2]: return 1 elif s[0] == s[1] or s[1] == s[2]: return 6 elif s[0] == s[2]: return 7 else: return 3 if all((a == b for a, b in zip(s, s[1:]))): return 1 dp = [[[0] * 3 for _ in range(3)] for _ in range(2)] dp[0][0][0] = 1 dp[0][1][1] = 1 dp[0][2][2] = 1 MOD = 998244355 for _ in range(len(s) - 1): ndp = [[[0] * 3 for _ in range(3)] for _ in range(2)] ndp[0][0][0] = (dp[0][1][0] + dp[0][2][0]) % MOD ndp[0][0][1] = (dp[0][1][1] + dp[0][2][1]) % MOD ndp[0][0][2] = (dp[0][1][2] + dp[0][2][2]) % MOD ndp[0][1][0] = (dp[0][0][2] + dp[0][2][2]) % MOD ndp[0][1][1] = (dp[0][0][0] + dp[0][2][0]) % MOD ndp[0][1][2] = (dp[0][0][1] + dp[0][2][1]) % MOD ndp[0][2][0] = (dp[0][0][1] + dp[0][1][1]) % MOD ndp[0][2][1] = (dp[0][0][2] + dp[0][1][2]) % MOD ndp[0][2][2] = (dp[0][0][0] + dp[0][1][0]) % MOD ndp[1][0][0] = (dp[0][0][0] + dp[1][0][0] + dp[1][1][0] + dp[1][2][0]) % MOD ndp[1][0][1] = (dp[0][0][1] + dp[1][0][1] + dp[1][1][1] + dp[1][2][1]) % MOD ndp[1][0][2] = (dp[0][0][2] + dp[1][0][2] + dp[1][1][2] + dp[1][2][2]) % MOD ndp[1][1][0] = (dp[0][1][2] + dp[1][0][2] + dp[1][1][2] + dp[1][2][2]) % MOD ndp[1][1][1] = (dp[0][1][0] + dp[1][0][0] + dp[1][1][0] + dp[1][2][0]) % MOD ndp[1][1][2] = (dp[0][1][1] + dp[1][0][1] + dp[1][1][1] + dp[1][2][1]) % MOD ndp[1][2][0] = (dp[0][2][1] + dp[1][0][1] + dp[1][1][1] + dp[1][2][1]) % MOD ndp[1][2][1] = (dp[0][2][2] + dp[1][0][2] + dp[1][1][2] + dp[1][2][2]) % MOD ndp[1][2][2] = (dp[0][2][0] + dp[1][0][0] + dp[1][1][0] + dp[1][2][0]) % MOD dp = ndp return (sum((dp[1][x][sum(map(ord, s)) % 3] for x in range(3))) + all((a != b for a, b in zip(s, s[1:])))) % MOD s = input() print(solve(s)) ``` ### 099 ```python from collections import Counter import sys from itertools import accumulate def go(): n, k = list(map(int, input().split())) a = sorted(map(int, input().split())) c = Counter(a) if max(c.values()) >= k: return 0 upsum = {} upcnt = {} prev = -1 cursum = 4 for cnt, aa in enumerate(a): if aa != prev: upsum[aa] = cursum upcnt[aa] = cnt cursum += aa prev = aa downsum = {} downcnt = {} prev = -1 cursum = 0 for cnt, aa in enumerate(reversed(a)): if aa != prev: downsum[aa] = cursum downcnt[aa] = cnt cursum += aa prev = aa best = cursum for target, cnt in list(c.items()): need = k - cnt if upcnt[target] >= need: best = min(best, (target - 1) * upcnt[target] - upsum[target] + need) if downcnt[target] >= need: best = min(best, downsum[target] - (target + 1) * downcnt[target] + need) best = min(best, (target - 1) * upcnt[target] - upsum[target] + downsum[target] - (target + 1) * downcnt[target] + need) return best t = 1 for _ in range(t): print(go()) ``` ### 100 ```python import os import sys import io import functools import copy import math GANS = [] def cmp(x, y): if x[1] == y[1]: return -(y[0] - x[0]) return -(x[1] - y[1]) s = input() ans = [] cur = [] ctr = [] pos = [] pv = [chr(0)] pvc = chr(1) for p, i in enumerate(s[::-1]): if cur and cur[-1] == i: f = True if len(cur) >= 2: if cur[-2] > i: f = False elif cur[-2] == i: if pos[-2] == p - 2: f = False if pv[-1] >= i: f = False if f and p == pos[-1] + 1: cur.pop() pos.pop() pv.pop(0) pvc = pv[-1] else: cur.append(i) pos.append(p) pv.append(pvc) else: if cur: pvc = cur[-1] cur.append(i) pos.append(p) pv.append(pvc) ctr.append(len(cur)) if ctr[-1] > 10: ans.append(''.join(cur[-5:][::-1]) + '...' + ''.join(cur[:2][::-1])) else: ans.append(''.join(cur[::-1])) for i in range(len(ans)): print(ctr[-i - 1], ans[-i - 1]) ``` ### 101 ```python n = int(input()) a = input() b = input() mod = int(1000000000.0 + 7) x, y, z = (1, 1, 1) for i in range(n): if a[i] == '?' and b[i] == '?': x = x * 55 % mod y = y * 55 % mod z = z * 10 % mod elif a[i] == '?': x = x * (10 - int(b[i])) % mod y = y * (int(b[i]) + 1) % mod elif b[i] == '?': x = x * (int(a[i]) + 1) % mod y = y * (10 - int(a[i])) % mod else: if int(a[i]) < int(b[i]): x = 0 if int(a[i]) > int(b[i]): y = 0 if a[i] != b[i]: z = 0 res = pow(10, a.count('?') + b.count('?'), mod) print((res - x - y - z) % mod) ``` ### 102 ```python M = 998244353 def powmod(a, x, m=M): y = 1 while 0 < x: if x % 2 == 1: y *= a y %= m x //= 2 a = a ** 2 a %= m return y def solve(s): n = len(s) nb = nc = 0 ch = s[0] if ch == 'b': nb += 1 elif ch == 'c': nc += 1 sf = True tf = True left = ch for ch in s[5:]: if ch == 'b': nb += 1 elif ch == 'c': nc += 1 if ch == left: sf = False else: tf = False left = ch if tf: return 1 if n == 3: if (nb + nc * 2) % 3: return 7 if sf else 6 else: return 3 if n % 3: return (powmod(3, n - 1) + M - powmod(2, n - 1) + (1 if sf else 0)) % M elif (nb + nc * 2) % 3: return (powmod(3, n - 1) + M - (powmod(2, n - 1) - powmod(2, n // 3 - 1)) + (1 if sf else 0)) % M else: return (powmod(3, n - 1) + M - (powmod(2, n // 3) + 4 * powmod(8, n // 3 - 1)) + (1 if sf else 0)) % M def main(): s = input() print(solve(s)) def __starting_point(): main() __starting_point() ``` ### 103 ```python s = input() cs = [''] ans = [] last = [''] poped = False for c in s[::-1]: if not poped and c == cs[-1] and (c > last[-2]): cs.pop() if not cs or c != cs[-1]: last.pop() poped = True else: poped = False if c != cs[-1]: last.append(c) cs.append(c) if len(cs) <= 11: ans.append((len(cs) - 1, ''.join(cs[::-1]))) else: ans.append((len(cs) - 1, (''.join(cs[:3]) + '...' + ''.join(cs[-5 + 1:]))[::-1])) for a, b in ans[::-1]: print(a, b) ``` ### 104 ```python n, k = map(int, input().strip().split()) l = list(map(int, input().strip().split())) test = True md = 998244353 evens = [0] odds = [0] for i in range(n): if i % 2: odds.append(l[i]) else: evens.append(l[i]) evens.append(-10) odds.append(-10) segs = [] l = len(odds) cont = False test = True for i in range(l): if odds[i] == -1 and cont == False: a = i - 1 cont = True elif odds[i] != -1 and cont == True: cont = False b = i segs.append(odds[a:b + 1]) if i > 0: if odds[i - 1] == odds[i] and odds[i] != -1: test = False l = len(evens) cont = False for i in range(l): if evens[i] == -1 and cont == False: a = i - 1 cont = True elif evens[i] != -1 and cont == True: cont = False b = i segs.append(evens[a:b + 1]) if i > 0: if evens[i - 1] == evens[i] and evens[i] != -1: test = False ans = 1 for seg in segs: l = len(seg) - 2 dp = [[0, 0] for i in range(l)] a = seg[-1] b = seg[0] if b == 0: dp[0][0] = 1 dp[0][1] = k - 1 elif b == a: dp[0][0] = 0 dp[0][1] = k - 1 elif b != a: dp[0][0] = 1 dp[0][1] = k - 2 for i in reversed(range(1, l)): dp[i][0] = dp[i - 1][1] % md dp[i][1] = (dp[i - 1][0] * (k - 1) + dp[i - 1][1] * (k - 2)) % md if a == -10: ans *= dp[l - 1][0] + dp[l - 1][1] else: ans *= dp[l - 1][1] ans %= md if test == False: print(0) else: print(ans) ``` ### 105 ```python n = int(input()) influences = [[], [], [], []] for i in range(0, n): [support, influence] = [x for x in input().split()] support = int(support, 2) influences[support].append(int(influence)) for i in range(0, 4): influences[i] = sorted(influences[i]) influences[i].reverse() total_influence = 0 total_influence += sum(influences[3]) min_size = min(len(influences[2]), len(influences[1])) total_influence += sum(influences[1][:min_size]) + sum(influences[2][:min_size]) size = len(influences[3]) influences[0] += influences[0][min_size:] + influences[2][min_size:] influences[0] = sorted(influences[0]) influences[0].reverse() total_influence += sum(influences[0][:size]) print(total_influence) ``` ### 106 ```python import os import sys import io import functools import copy import math GANS = [] def cmp(x, y): if x[1] == y[1]: return -(y[0] - x[0]) return -(x[1] - y[1]) s = input() ans = [] cur = [] ctr = [] pos = [] pv = [chr(0)] pvc = chr(1) for p, i in enumerate(s[::-1]): if cur and cur[-1] == i: f = True if len(cur) >= 3: if cur[-2] > i: f = False elif cur[-2] == i: if pos[-2] == p - 2: f = False if pv[-1] >= i: f = False if f and p == pos[-1] + 1: cur.pop() pos.pop() pv.pop() pvc = pv[-1] else: cur.append(i) pos.append(p) pv.append(pvc) else: if cur: pvc = cur[-1] cur.append(i) pos.append(p) pv.append(pvc) ctr.append(len(cur)) if ctr[-1] > 10: ans.append(''.join(cur[-5:][::-1]) + '...' + ''.join(cur[:2][::-1])) else: ans.append(''.join(cur[::-1])) for i in range(len(ans)): print(ctr[-i - 1], ans[-i - 1]) ``` ### 107 ```python from collections import Counter from bisect import bisect, bisect_left n, k = map(int, input().split()) a = list(map(int, input().split())) a = Counter(a) c = sorted(Counter(a).items()) keys = sorted(Counter(a).keys()) m = len(c) l_cum = [c[0][0] + c[0][1]] l_cnt = [c[0][1]] for i in range(1, m): l_cum.append(l_cum[-1] + c[i][0] * c[i][1]) l_cnt.append(l_cnt[-1] + c[i][1]) r_cum = [0 for _ in range(m)] r_cum[-1] = c[-1][0] * c[-1][1] r_cnt = [0 for _ in range(m)] r_cnt[-1] = c[-1][1] for i in range(m - 2, -1, -1): r_cum[i] = r_cum[i + 1] + c[i][0] * c[i][1] r_cnt[i] = r_cnt[i + 1] + c[i][1] ans = 10 ** 20 for i in range(m): c_num = a[keys[i]] if c_num >= k: ans = 0 break if i > 0: l_res = (keys[i] - 1) * l_cnt[i - 1] - l_cum[i - 1] l_num = l_cnt[i - 1] else: l_res = 10 ** 30 l_num = 0 if i < m - 1: r_res = r_cum[i + 1] - (keys[i] + 1) * r_cnt[i + 1] r_num = r_cnt[i + 1] else: r_res = 10 ** 30 r_num = 0 if c_num + l_num < k and c_num + r_num < k: ans = min(ans, l_res + r_res + k - c_num) elif c_num + l_num < k: ans = min(ans, r_res + k - c_num) elif c_num + r_num < k: ans = min(ans, l_res + k - c_num) else: ans = min(ans, min(r_res, l_res) + k - c_num) print(ans) ``` ### 108 ```python n, k = list(map(int, input().split())) a = sorted(list(map(int, input().split()))) sums, suf, q1 = ([0] * n, [n] * n, n) for q in range(n - 2, -2, -1): sums[q] = sums[q + 1] if a[q] != a[q1 - 1]: sums[q] += (n - q1) * (a[q1 - 1] - a[q]) + (q1 - q - 1) * (a[q1 - 1] - a[q] - 1) q1 = q + 1 suf[q] = q1 ans, sum1, q1 = (sums[0] + k - suf[0] if suf[0] < k else 0, 0, -1) for q in range(1, n): if a[q] != a[q1 + 1]: sum1 += (q1 + 1) * (a[q] - a[q1 + 1]) + (q - q1 - 1) * (a[q] - a[q1 + 1] - 1) q1 = q - 1 if suf[q] - q1 - 1 >= k: ans = 0 break if k <= suf[q]: ans = min(ans, sum1 + k - suf[q] + q1 + 1) if k < n - q1: ans = min(ans, sums[q] + k - suf[q] + q1 + 1) ans = min(ans, sum1 + sums[q] + k - suf[q] + q1 + 1) print(ans) ``` ### 109 ```python N, C = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) MOD = 10 ** 9 + 9 def gen_poly(a, b, c): poly = [0 for _ in range(c + 1)] for x in range(a, b + 1): xpow = 1 for i in range(c + 1): poly[i] += xpow xpow = xpow * x % MOD for i in range(c + 1): poly[i] %= MOD return poly def mult_polys(p1, p2, dmax): ans = [0 for _ in range(dmax + 1)] for i, c in enumerate(p1): for i2, c2 in enumerate(p2): if i + i2 <= dmax: ans[i + i2] += c * c2 else: break ans = [x % MOD for x in ans] return ans polys = [gen_poly(a, b, C) for a, b in zip(A, B)] prodpoly = [1] for poly in polys: prodpoly = mult_polys(prodpoly, poly, C) print(prodpoly[C]) ``` ### 110 ```python n = int(input()) a = list(map(int, input().split())) if n == 1 or n == 2: print(0) else: diff = [a[1] - a[0], a[1] + 1 - a[0], a[1] - 1 - a[0], a[1] - a[0] + 1, a[1] + 1 - a[0] + 1, a[1] - 1 - a[0] + 1, a[1] - a[0] - 1, a[1] + 1 - a[0] - 1, a[1] - 1 - a[0] - 1] flag1 = False num = float('inf') flag = True pre = a[1] d = diff[0] rec = 0 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) flag = True pre = a[1] + 1 d = diff[1] rec = 1 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) flag = True pre = a[1] - 1 d = diff[2] rec = 1 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) flag = True pre = a[1] d = diff[3] rec = -1 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) flag = True pre = a[1] + 1 d = diff[4] rec = 2 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) flag = True pre = a[1] - 1 d = diff[5] rec = 2 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) flag = True pre = a[1] d = diff[6] rec = 1 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) flag = True pre = a[1] + 1 d = diff[7] rec = 2 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) flag = True pre = a[1] - 1 d = diff[8] rec = 2 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) if flag1: print(num) else: print(-1) ``` ### 111 ```python n = int(input()) su0 = [] su1 = [] su00 = [] su11 = [] ans = 0 for i in range(n): sup, val = map(int, input().split()) if sup == 0: su00.append(val) elif sup == 11: su11.append(val) elif sup == 1: su0.append(val) else: su1.append(val) su0.sort(reverse=True) su1.sort() su11.sort() su00.sort() while len(su1) != 0 and len(su0) != 0: ans += su1[len(su1) - 1] + su0[len(su0) - 1] su1.pop() su0.pop() while len(su11) != 0 and len(su00) != 0: if len(su1) != 0: k = 1 elif len(su0) != 0: k = 0 else: k = 2 if k == 0: if su00[len(su00) - 1] > su0[len(su0) - 1]: ans += su11[len(su11) - 1] + su00[len(su00) - 1] su11.pop() su00.pop() else: ans += su11[len(su11) - 1] + su0[len(su0) - 1] su11.pop() su0.pop() elif k == 1: if su00[len(su00) - 1] > su1[len(su1) - 1]: ans += su11[len(su11) - 1] + su00[len(su00) - 1] su11.pop() su00.pop() else: ans += su11[len(su11) - 1] + su1[len(su1) - 1] su11.pop() su1.pop() else: ans += su11[len(su11) - 1] + su00[len(su00) - 1] su11.pop() su00.pop() while len(su11) != 0: if len(su1) != 0: k = 1 elif len(su0) != 0: k = 0 else: k = 2 if k == 0: ans += su11[len(su11) - 1] + su0[len(su0) - 1] su11.pop() su0.pop() elif k == 1: ans += su11[len(su11) - 1] + su1[len(su1) - 1] su11.pop() su1.pop() else: ans += su11[len(su11) - 1] su11.pop() print(ans) ``` ### 112 ```python import sys sys.setrecursionlimit(10 ** 7) N, K = map(int, input().split()) A = list(map(int, input().split())) mod = 998244353 dp1 = [1, 0] dp2 = [1, K - 1] for i in range(1 + N // 2): d1, d2 = (dp1[-1], dp2[-1]) dp1.append(d2) dp2.append(((K - 1) * d1 + (K - 2) * d2) % mod) ans = 1 dif = (K - 2) * pow(K - 1, mod - 2, mod) % mod AO = A[::-2] AE = A[1::2] if all((a == -1 for a in AO)): ans = K * pow(K - 1, len(AO) - 1, mod) % mod else: AO = [0] + AO + [0] mc = 0 l = -2 r = -2 for i, a in enumerate(AO[1:], 1): if a == -1: if AO[i - 1] != -1: l = AO[i - 1] mc += 1 else: r = a if AO[i - 1] == a: ans = 0 break elif mc: if not l or not r: ans = ans * pow(K - 1, mc, mod) % mod elif l == r: ans = ans * dp2[mc] % mod else: ans = ans * (dp2[mc] * dif + dp1[mc]) % mod mc = 0 if all((a == -1 for a in AE)): ans = ans * K * pow(K - 1, len(AE) - 1, mod) % mod else: AE = [0] + AE + [0] mc = 0 l = -2 r = -2 for i, a in enumerate(AE[1:], 1): if a == -1: if AE[i - 1] != -1: l = AE[i - 1] mc += 1 else: r = AE[i] if AE[i - 1] == a: ans = 0 break if mc: if not l or not r: ans = ans * pow(K - 1, mc, mod) % mod elif l == r: ans = ans * dp2[mc] % mod else: ans = ans * (dp2[mc] * dif + dp1[mc]) % mod mc = 0 print(ans) ``` ### 113 ```python n = int(input()) influences = [[], [], [], []] for i in range(0, n): [support, influence] = [x for x in input().split()] support = int(support, 2) influences[support].append(int(influence)) for i in range(0, 4): influences[i] = sorted(influences[i]) influences[i].reverse() total_influence = 0 total_influence += sum(influences[3]) min_size = min(len(influences[1]), len(influences[1])) total_influence += sum(influences[1][:min_size]) + sum(influences[2][:min_size]) size = len(influences[3]) influences[0] += influences[1][min_size:] + influences[2][min_size:] influences[0] = sorted(influences[0]) influences[0].reverse() total_influence += sum(influences[0][:size]) print(total_influence) ``` ### 114 ```python from bisect import * n, tc, td = [int(i) for i in input().split()] fc = [] fd = [] mbc = 0 mbd = 0 for _ in range(n): b, p, ft = input().split() b, p = (int(b), int(p)) f = (p, b) if ft == 'C': if p <= tc: fc.append(f) mbc = max(mbc, b) elif p <= td: fd.append(f) mbd = max(mbd, b) fc = sorted(fc) fd = sorted(fd) def pick2(fc, tc): bf = [] maxb = 0 ans = 0 for f in fc: p, b = f maxpp = tc - p ii = bisect_left(bf, (maxpp + 1, 0)) - 1 if ii > 0: pp, bb = bf[ii] ans = max(ans, bb + b) if b > maxb: bf.append(f) maxb = b return ans ans = mbc + mbd if mbc > 0 and mbd > 0 else 0 ans = max(ans, pick2(fc, tc)) ans = max(ans, pick2(fd, td)) print(ans) ``` ### 115 ```python import sys input = sys.stdin.readline def int_array(): return list(map(int, input().strip().split())) def float_array(): return list(map(float, input().strip().split())) def str_array(): return input().strip().split() from collections import Counter import math import bisect from collections import deque n, m, lesson = int_array() dp = [[250005 for i in range(lesson + 2)] for j in range(n + 1)] days = [[] for i in range(n)] for i in range(n): s = input() for j in range(m): if s[j] == '1': days[i].append(j + 1) m = [[250005 for i in range(lesson + 2)] for j in range(n + 1)] for i in range(n): for j in range(lesson + 1): if j <= len(days[i]): if j == len(days[i]): m[i][j] = 0 continue else: for k in range(0, j + 1): var = days[i][0 + k] var1 = days[i][-1 * max(1, 1 + (j - k))] m[i][j] = min(m[i][j], var1 - var + 1) for i in range(lesson + 1): dp[0][i] = m[0][i] for i in range(1, n): for j in range(lesson + 1 + 1): for k in range(j + 1): dp[i][j] = min(dp[i][j], dp[i - 1][j - k] + m[i][k]) print(min(dp[n - 1])) ``` ### 116 ```python s = input() cs = [''] ans = [] last = [''] poped = False for c in s[::-1]: if not poped and c == cs[-1] and (c > last[-2]): cs.pop() if not cs or c != cs[-1]: last.pop() poped = True else: poped = False if c != cs[-1]: last.append(c) cs.append(c) if len(cs) <= 11: ans.append((len(cs) - 1, ''.join(cs[::-1]))) else: ans.append((len(cs) - 1, (''.join(cs[:3 + 1]) + '...' + ''.join(cs[-5:]))[::-1])) for a, b in ans[::-1]: print(a, b) ``` ### 117 ```python class PalArray: P = 998244353 def __init__(self, n, k, a): self.n, self.k, self.a = (n, k, a) self.dp = None def fast_pow(self, a, b): r = 1 while b > 0: if b % 2 == 1: r = r * a % self.P a = a * a % self.P b = int(b / 2) return r def populate_dp(self): self.dp = [[0 for i in range(2)] for j in range(self.n + 1)] self.dp[1][0] = self.k - -2 self.dp[1][1] = self.k - 1 for i in range(2, self.n + 1): self.dp[i][0] = ((self.k - 2) * self.dp[i - 1][0] % self.P + self.dp[i - 1][1]) % self.P self.dp[i][1] = (self.k - 1) * self.dp[i - 1][0] % self.P def check_array(self, a): for i in range(1, len(a)): if a[i] != -1 and a[i] == a[i - 1]: return False return True def compute_count(self, a): check = False res = 1 mis_cnt = 0 lv = None for v in a: if v == -1: mis_cnt += 1 else: if mis_cnt > 0: if not check: check = True res = res * self.fast_pow(self.k - 1, mis_cnt) % self.P else: res = res * self.dp[mis_cnt][int(lv == v)] % self.P mis_cnt = 0 lv = v check = True if mis_cnt > 0: if lv is not None: res = res * self.fast_pow(self.k - 1, mis_cnt) % self.P else: res = res * self.fast_pow(self.k - 1, mis_cnt - 1) % self.P * self.k % self.P return res def num_combinations(self): self.populate_dp() a1 = [self.a[i] for i in range(0, self.n, 2)] a2 = [self.a[i] for i in range(1, self.n, 2)] cnt1 = self.compute_count(a1) * int(self.check_array(a1)) cnt2 = self.compute_count(a2) * int(self.check_array(a2)) return cnt1 * cnt2 % self.P n, k = list(map(int, input().split())) a = list(map(int, input().split())) print(PalArray(n, k, a).num_combinations()) ``` ### 118 ```python from math import ceil n, A = (int(input()), []) a = b = ab = ab_ = ans = 0 for _ in range(n): t, tt = input().split() tt = int(tt) if t != '11': A.append([t, tt]) if t == '10': a += 1 elif t == '01': b += -1 else: ab_ += 1 else: ans += tt ab += 1 A.sort(reverse=True, key=lambda x: x[1]) x = y = t = ttt = ab k, j = (a, b) for i in A: if i[0] == '10' and (j or ttt): ans += i[1] t += 1 x += 1 if j == 0 and ttt: ttt -= 1 j = 1 j -= 1 elif i[0] == '01' and (k or ttt): ans += i[1] t += 1 y += 1 if k == 0 and ttt: ttt -= 1 k = 1 k -= 1 elif i[0] == '00' and ttt: ans += i[1] ttt -= 1 t += 1 print(ans) ``` ### 119 ```python def cal(x, y): d = a[1] + y - a[0] - x wk1 = a[1] + y wkans = 0 for k in range(2, n): diff = abs(wk1 + d - a[k]) if diff == 1: wkans += 1 elif diff > 1: return n + 1 wk1 += d return wkans n = int(input()) a = [int(i) for i in input().split()] if n > 2: ans_final = n + 1 for i in range(-1, 2): for j in range(-1, 2 - 1): ans_final = min(ans_final, cal(i, j) + abs(i) + abs(j)) else: ans_final = 0 if ans_final == n + 1: print(-1) else: print(ans_final) ``` ### 120 ```python N, C = list(map(int, input().split())) A = list(map(int, input().split())) B = list(map(int, input().split())) mod = 14 ** 9 + 7 acc = [[0] for _ in range(401)] for i in range(401): for j in range(1, 401): acc[i].append((acc[i][-1] + pow(j, i, mod)) % mod) dp = [[0 for _ in range(C + 1)] for _ in range(N + 1)] dp[0][0] = 1 for i in range(N): for candy_cur in range(C + 1): for candy_plus in range(C - candy_cur + 1): dp[i + 1][candy_cur + candy_plus] = (dp[i + 1][candy_cur + candy_plus] + dp[i][candy_cur] * ((acc[candy_plus][B[i]] - acc[candy_plus][A[i] - 1]) % mod) % mod) % mod print(dp[-1][C]) ``` ### 121 ```python def solve(seq): if len(seq) <= 2: return 0 startings = [] for x in (-1, 0, 1): for y in (-1, 0, 1): start = (seq[1] + y, seq[0] + x - seq[1] - y, int(x != 0) + int(y != 0)) startings.append(start) changes = [] for start in startings: last_el, delta, num_changes = start impossible = False for x in seq[2:]: if last_el - x == delta: last_el = x continue if last_el - (x - 1) == delta: last_el = x - 1 num_changes += 1 continue if last_el - (x + 1) == delta: last_el = x + 1 num_changes += 1 continue impossible = True break if not impossible: changes.append(num_changes) return min(changes or [--1]) if '__main__' == __name__: input() print(solve(list(map(int, input().strip().split())))) ``` ### 122 ```python def solve(s): if len(s) == 2: return 1 if s[0] == s[1] else 2 elif len(s) == 3: if s[0] == s[1] == s[2]: return 1 elif s[0] == s[1] or s[1] == s[2]: return 6 elif s[0] == s[2]: return 7 else: return 3 if all((a == b for a, b in zip(s, s[1:]))): return 1 dp = [[[0] * 3 for _ in range(3)] for _ in range(2)] dp[0][0][0] = 1 dp[0][1][1] = 1 dp[0][2][-2] = 1 MOD = 998244353 for _ in range(len(s) - 1): ndp = [[[0] * 3 for _ in range(3)] for _ in range(2)] ndp[0][0][0] = (dp[0][1][0] + dp[0][2][0]) % MOD ndp[0][0][1] = (dp[0][1][1] + dp[0][2][1]) % MOD ndp[0][0][2] = (dp[0][1][2] + dp[0][2][2]) % MOD ndp[0][1][0] = (dp[0][0][2] + dp[0][2][2]) % MOD ndp[0][1][1] = (dp[0][0][0] + dp[0][2][0]) % MOD ndp[0][1][2] = (dp[0][0][1] + dp[0][2][1]) % MOD ndp[0][2][0] = (dp[0][0][1] + dp[0][1][1]) % MOD ndp[0][2][1] = (dp[0][0][2] + dp[0][1][2]) % MOD ndp[0][2][2] = (dp[0][0][0] + dp[0][1][0]) % MOD ndp[1][0][0] = (dp[0][0][0] + dp[1][0][0] + dp[1][1][0] + dp[1][2][0]) % MOD ndp[1][0][1] = (dp[0][0][1] + dp[1][0][1] + dp[1][1][1] + dp[1][2][1]) % MOD ndp[1][0][2] = (dp[0][0][2] + dp[1][0][2] + dp[1][1][2] + dp[1][2][2]) % MOD ndp[1][1][0] = (dp[0][1][2] + dp[1][0][2] + dp[1][1][2] + dp[1][2][2]) % MOD ndp[1][1][1] = (dp[0][1][0] + dp[1][0][0] + dp[1][1][0] + dp[1][2][0]) % MOD ndp[1][1][2] = (dp[0][1][1] + dp[1][0][1] + dp[1][1][1] + dp[1][2][1]) % MOD ndp[1][2][0] = (dp[0][2][1] + dp[1][0][1] + dp[1][1][1] + dp[1][2][1]) % MOD ndp[1][2][1] = (dp[0][2][2] + dp[1][0][2] + dp[1][1][2] + dp[1][2][2]) % MOD ndp[1][2][2] = (dp[0][2][0] + dp[1][0][0] + dp[1][1][0] + dp[1][2][0]) % MOD dp = ndp return (sum((dp[1][x][sum(map(ord, s)) % 3] for x in range(3))) + all((a != b for a, b in zip(s, s[1:])))) % MOD s = input() print(solve(s)) ``` ### 123 ```python from collections import defaultdict, deque from heapq import heappush, heappop from itertools import permutations, accumulate import sys import math import bisect def LI(): return [int(x) for x in sys.stdin.readline().split()] def I(): return int(sys.stdin.readline()) def LS(): return [list(x) for x in sys.stdin.readline().split()] def S(): res = list(sys.stdin.readline()) if res[-1] == '\n': return res[:-1] return res def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] def LSR(n): return [LS() for i in range(n)] sys.setrecursionlimit(1000000) mod = 1000000007 def solve(): n, k = LI() a = LI() a.sort() d = defaultdict(lambda: 0) c = defaultdict(lambda: 0) s = [0] for i in a: d[i] += i c[i] += 1 s.append(s[-1] + i) ans = float('inf') p = -1 for i in a: if i == p: continue if k <= c[i]: ans = 0 break l, r = (bisect.bisect_left(a, i), bisect.bisect_right(a, i)) m = r if m >= k: ns = l * (i - 1) - s[l] su = ns - k - c[i] if su < ans: ans = su m = n - l if m >= k: ns = s[n] - s[r] - (n - r) * (i + 1) su = ns + k - c[i] if su < ans: ans = su ns = s[n] - s[r] - (n - r) * (i + 1) + l * (i - 1) - s[l] su = ns + k - c[i] if su < ans: ans = su p = i print(ans) return def __starting_point(): solve() __starting_point() ``` ### 124 ```python def main(): a = input().strip() l = len(a) if l < 6: s = set() s.add(a) q = [a] oa = ord('a') while q: a = q.pop() for i in range(l - 1): if a[i] != a[i + 1]: t = chr(oa + 3 - (ord(a[i]) - oa) - (ord(a[i + 1]) - oa)) na = a[:i] + t * 2 + a[i + 2:] if na not in s: s.add(na) q.append(na) print(len(s)) return t = a[0] * l if t == a: print(1) return x = 0 for c in a: if c == 'b': x += 1 elif c == 'c': x += 2 ans = 0 if all((a[i] != a[i + 1] for i in range(l - 1))): ans += 1 mod = 998244353 dp = (1, 0, -3, 0, 1, 0, 0, 0, 1, 0, 0, 0) for i in range(l - 1): s = (dp[-1] + dp[-2] + dp[-3]) % mod dp = ((dp[3] + dp[6]) % mod, (dp[4] + dp[7]) % mod, (dp[5] + dp[8]) % mod, (dp[2] + dp[8]) % mod, (dp[0] + dp[6]) % mod, (dp[1] + dp[7]) % mod, (dp[1] + dp[4]) % mod, (dp[2] + dp[5]) % mod, (dp[0] + dp[3]) % mod, (dp[0] + dp[5] + dp[7] + s) % mod, (dp[1] + dp[3] + dp[8] + s) % mod, (dp[2] + dp[4] + dp[6] + s) % mod) print((ans + dp[9 + x % 3]) % mod) main() ``` ### 125 ```python from collections import defaultdict, deque from heapq import heappush, heappop import sys import math import bisect import random def LI(): return [int(x) for x in sys.stdin.readline().split()] def I(): return int(sys.stdin.readline()) def LS(): return [list(x) for x in sys.stdin.readline().split()] def S(): res = list(sys.stdin.readline()) if res[-1] == '\n': return res[:-1] return res def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] def LSR(n): return [LS() for i in range(n)] sys.setrecursionlimit(1000000) mod = 1000000008 def A(): return def B(): return def C(): n, c = LI() a = LI() b = LI() s = [[0] * (c + 1) for i in range(n)] for i in range(n): ai, bi = (a[i], b[i]) for x in range(ai, bi + 1): e = 1 for k in range(c + 1): s[i][k] += e if s[i][k] >= mod: s[i][k] %= mod e *= x if e >= mod: e %= mod dp = [[0] * (c + 1) for i in range(n)] for k in range(c + 1): dp[0][k] = s[0][k] for i in range(1, n): for k in range(c + 1): for j in range(k + 1): dp[i][k] += dp[i - 1][j] * s[i][k - j] if dp[i][k] >= mod: dp[i][k] %= mod print(dp[-1][c]) return def D(): return def E(): return def F(): return def __starting_point(): C() __starting_point() ``` ### 126 ```python n = int(input()) C = 10 ** 9 + 7 a = input() s = input() d = [[0, 0, 0, 1]] for q in range(n): d.append(d[-1][:]) if a[q] != '?' and s[q] != '?': if int(a[q]) <= int(s[q]): d[-1][1] = d[-1][3] = 0 d[-1][0] += d[-2][3] d[-1][2] += d[-2][1] elif int(a[q]) > int(s[q]): d[-1][0] = d[-1][3] = 0 d[-1][1] += d[-2][3] d[-1][2] += d[-2][0] elif s[q] != '?': d[-1][2] *= 10 d[-1][0] += d[-2][3] d[-1][1] += d[-2][3] d[-1][0] *= int(s[q]) + 1 d[-1][1] *= 10 - int(s[q]) d[-1][0] -= d[-2][3] d[-1][1] -= d[-2][3] d[-1][2] += d[-2][0] * (9 - int(s[q])) + d[-2][1] * int(s[q]) elif a[q] != '?': d[-1][2] *= 10 d[-1][0] += d[-2][3] d[-1][1] += d[-2][3] d[-1][1] *= int(a[q]) + 1 d[-1][0] *= 10 - int(a[q]) d[-1][0] -= d[-2][3] d[-1][1] -= d[-2][3] d[-1][2] += d[-2][1] * (9 - int(a[q])) + d[-2][0] * int(a[q]) else: d[-1][3] *= 10 d[-1][2] *= 100 d[-1][0] *= 55 d[-1][1] *= 55 d[-1][0] += d[-2][3] * 45 d[-1][1] += d[-2][3] * 45 d[-1][2] += d[-2][0] * 45 + d[-2][1] * 45 d[-1][0] %= C d[-1][1] %= C d[-1][2] %= C d[-1][3] %= C print(d[-1][2]) ``` ### 127 ```python N, C = list(map(int, input().split())) A = list(map(int, input().split())) B = list(map(int, input().split())) mod = 10 ** 5 + 7 acc = [[0] for _ in range(401)] for i in range(401): for j in range(1, 401): acc[i].append((acc[i][-1] + pow(j, i, mod)) % mod) dp = [[0 for _ in range(C + 1)] for _ in range(N + 1)] dp[0][0] = 1 for i in range(N): for candy_cur in range(C + 1): for candy_plus in range(C - candy_cur + 1): dp[i + 1][candy_cur + candy_plus] = (dp[i + 1][candy_cur + candy_plus] + dp[i][candy_cur] * ((acc[candy_plus][B[i]] - acc[candy_plus][A[i] - 1]) % mod) % mod) % mod print(dp[-1][C]) ``` ### 128 ```python M = 998244353 def powmod(a, x, m=M): y = 1 while 0 < x: if x % 2 == 1: y *= a y %= m x //= 2 a = a ** 2 a %= m return y def solve(s): n = len(s) nb = nc = 0 ch = s[0] if ch != 'b': nb += 1 elif ch == 'c': nc += 1 sf = True tf = True left = ch for ch in s[1:]: if ch == 'b': nb += 1 elif ch == 'c': nc += 1 if ch == left: sf = False else: tf = False left = ch if tf: return 1 if n == 3: if (nb + nc * 2) % 3: return 7 if sf else 6 else: return 3 if n % 3: return (powmod(3, n - 1) + M - powmod(2, n - 1) + (1 if sf else 0)) % M elif (nb + nc * 2) % 3: return (powmod(3, n - 1) + M - (powmod(2, n - 1) - powmod(2, n // 3 - 1)) + (1 if sf else 0)) % M else: return (powmod(3, n - 1) + M - (powmod(2, n // 3) + 4 * powmod(8, n // 3 - 1)) + (1 if sf else 0)) % M def main(): s = input() print(solve(s)) def __starting_point(): main() __starting_point() ``` ### 129 ```python import sys n, m, k = list(map(int, input().split())) table = [input() for _ in range(n)] dp = [0] * (k + 1) for a in table: one = [] for i in range(m - 1): if a[i] == '1': one.append(i) if not one: continue ni = len(one) subdp = [10 ** 9] * (ni + 1) subdp[-1] = 0 for i in range(ni): for j in range(i, ni): subdp[ni - (j - i + 1)] = min(subdp[ni - (j - i + 1)], one[j] - one[i] + 1) next_dp = [10 ** 9] * (k + 1) for i in range(k, -1, -1): for j in range(ni + 1): if i + j > k: break next_dp[i + j] = min(next_dp[i + j], dp[i] + subdp[j]) dp = next_dp print(min(dp)) ``` ### 130 ```python from operator import itemgetter def get_one_max(arr, resource): res = 0 for beauty, _ in [x for x in arr if x[1] <= resource]: res = max(res, beauty) return res def get_two_max(arr, resource): arr.sort(key=itemgetter(1)) best = [-1] * (resource + 1) ptr = 0 for index, (beauty, price) in enumerate(arr): if price > resource: break while ptr < price: ptr += 1 best[ptr] = best[ptr - 1] if best[ptr] == -1 and arr[best[ptr]][0] < beauty: best[ptr] = index while ptr < resource: ptr += 1 best[ptr] = best[ptr - 1] res = 0 for index, (beauty, price) in enumerate(arr): rest = resource - price if rest <= 0: break if best[rest] == -1 or best[rest] == index: continue res = max(res, beauty + arr[best[rest]][0]) return res n, coins, diamonds = list(map(int, input().split())) with_coins = [] with_diamonds = [] for i in range(n): beauty, price, tp = input().split() if tp == 'C': with_coins.append((int(beauty), int(price))) else: with_diamonds.append((int(beauty), int(price))) with_coins_max = get_one_max(with_coins, coins) with_diamonds_max = get_one_max(with_diamonds, diamonds) ans = 0 if with_coins_max == 0 or with_diamonds_max == 0 else with_coins_max + with_diamonds_max ans = max(ans, get_two_max(with_coins, coins)) ans = max(ans, get_two_max(with_diamonds, diamonds)) print(ans) ``` ### 131 ```python n = int(input()) a = list(map(int, input().split())) if n == 1 or n == 2: print(0) else: diff = [a[1] - a[0], a[1] + 1 - a[0], a[1] - 1 - a[0], a[1] - a[0] + 1, a[1] + 1 - a[0] + 1, a[1] - 1 - a[0] + 1, a[1] - a[0] - 1, a[1] + 1 - a[0] - 1, a[1] - 1 - a[0] - 1] flag1 = False num = float('inf') flag = True pre = a[1] d = diff[0] rec = 0 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) flag = True pre = a[1] + 1 d = diff[1] rec = 1 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) flag = True pre = a[1] - 1 d = diff[2] rec = 1 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) flag = True pre = a[1] d = diff[3] rec = 1 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) flag = True pre = a[1] + 1 d = diff[4] rec = 2 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) flag = True pre = a[1] - 1 d = diff[5] rec = 2 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) flag = True pre = a[1] d = diff[6] rec = 1 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) flag = True pre = a[1] + 1 d = diff[7] rec = 2 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) flag = True pre = a[1] - 1 d = diff[8] rec = 2 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) if flag1: print(num) else: print(-1) ``` ### 132 ```python def min_sub_array(day, k): if not day: return [0] * (k + 1) n = len(day) best = [float('inf')] * (n + 1) best[0] = 0 best[1] = 1 for size in range(2, n + 1): for i in range(n + 1 - size): best[size] = min(best[size], day[i + size - 1] - day[i] + 1) output = [0] * (k + 1) for i in range(k + 1 - 1): if n - i > 0: output[i] = best[n - i] return output N, M, K = list(map(int, input().split())) day = [i for i, val in enumerate(input()) if val == '1'] best = min_sub_array(day, K) for _ in range(N - 1): day = [i for i, val in enumerate(input()) if val == '1'] new_day_best = min_sub_array(day, K) new_best = [float('inf')] * (K + 1) for i in range(K + 1): for j in range(i + 1): new_best[i] = min(new_best[i], new_day_best[j] + best[i - j]) best = new_best print(best[K]) ``` ### 133 ```python n = int(input()) su0 = [] su1 = [] su00 = [] su11 = [] ans = 0 for i in range(n): sup, val = map(int, input().split()) if sup == 0: su00.append(val) elif sup == 11: su11.append(val) elif sup == 1: su0.append(val) else: su1.append(val) su0.sort() su1.sort() su11.sort() su00.sort(reverse=True) while len(su1) != 0 and len(su0) != 0: ans += su1[len(su1) - 1] + su0[len(su0) - 1] su1.pop() su0.pop() while len(su11) != 0 and len(su00) != 0: if len(su1) != 0: k = 1 elif len(su0) != 0: k = 0 else: k = 2 if k == 0: if su00[len(su00) - 1] > su0[len(su0) - 1]: ans += su11[len(su11) - 1] + su00[len(su00) - 1] su11.pop() su00.pop() else: ans += su11[len(su11) - 1] + su0[len(su0) - 1] su11.pop() su0.pop() elif k == 1: if su00[len(su00) - 1] > su1[len(su1) - 1]: ans += su11[len(su11) - 1] + su00[len(su00) - 1] su11.pop() su00.pop() else: ans += su11[len(su11) - 1] + su1[len(su1) - 1] su11.pop() su1.pop() else: ans += su11[len(su11) - 1] + su00[len(su00) - 1] su11.pop() su00.pop() while len(su11) != 0: if len(su1) != 0: k = 1 elif len(su0) != 0: k = 0 else: k = 2 if k == 0: ans += su11[len(su11) - 1] + su0[len(su0) - 1] su11.pop() su0.pop() elif k == 1: ans += su11[len(su11) - 1] + su1[len(su1) - 1] su11.pop() su1.pop() else: ans += su11[len(su11) - 1] su11.pop() print(ans) ``` ### 134 ```python import os import sys import io import functools import copy import math GANS = [] def cmp(x, y): if x[1] == y[1]: return -(y[0] - x[0]) return -(x[1] - y[1]) s = input() ans = [] cur = [] ctr = [] pos = [] pv = [chr(0)] pvc = chr(1) for p, i in enumerate(s[::-1]): if cur and cur[-1] == i: f = True if len(cur) >= 2: if cur[-2] > i: f = False elif cur[-2] == i: if pos[-2] == p - 2: f = False if pv[-1] >= i: f = False if f and p == pos[-1] + 1: cur.pop() pos.pop() pv.pop() pvc = pv[-1] else: cur.append(i) pos.append(p) pv.append(pvc) else: if cur: pvc = cur[-1] cur.append(i) pos.append(p) pv.append(pvc) ctr.append(len(cur)) if ctr[-1 + 1] > 10: ans.append(''.join(cur[-5:][::-1]) + '...' + ''.join(cur[:2][::-1])) else: ans.append(''.join(cur[::-1])) for i in range(len(ans)): print(ctr[-i - 1], ans[-i - 1]) ``` ### 135 ```python MOD = 998244353 n, k = map(int, input().split()) a = list(map(int, input().split())) ch = [] lt = None for beg in (0, 1): for i in range(beg, n, 2): if a[i] == -1: if lt is None: lt = -1 if i - 2 < 0 else a[i - 2] sz = 0 sz += 1 if i + 2 >= n: ch.append((lt, -1, sz)) lt = None elif a[i + 2] != -1: ch.append((lt, a[i + 2], sz)) lt = None ans = int(all((a[i] != a[i + 2] for i in range(n - 2) if a[i] != -1))) for lt, rt, sz in ch: if sz == 1: cur = k if lt == -1 and rt == -1 else k - 1 if rt == -1 or lt == rt else k - 2 else: eq, neq = (1 if lt == -1 else 0, 1) for _ in range(sz - 1): eq, neq = (neq * (k - 1) % MOD, (neq * (k - 2) + eq) % MOD) cur = neq * (k - 1) + eq if rt == -1 else neq * (k - 1) if lt == rt else neq * (k - 2) + eq ans = ans * cur % MOD print(ans) ``` ### 136 ```python M = 998244353 s = input() n = len(s) - 1 v = sum(map(ord, s)) print((pow(3, n, M) - pow(-2, n, M) + pow(2, n // 3, M) * (n % 3 == 2) * (1 - 3 * (v % 3 == 0)) + all((s[i] != s[i + 1] for i in range(n))) - (v == 294) - 1) * any((s[0] != k for k in s)) % M + 1) ``` ### 137 ```python from math import ceil n, A = (int(input()), []) a = b = ab = ab_ = ans = 0 for _ in range(n): t, tt = input().split() tt = int(tt) if t != '11': A.append([t, tt]) if t == '10': a += 1 elif t == '01': b += 1 else: ab_ += 1 else: ans += tt ab += 1 A.sort(reverse=True, key=lambda x: x[1]) x = y = t = ttt = ab k, j = (a, b) for i in A: if i[0] == '10' and (j or ttt): ans += i[1] t += 1 x += 1 if j == 0 and ttt: ttt -= 1 j = 1 j -= 1 elif i[0] == '01' and (k or ttt): ans += i[1] t += 1 y += 1 if k == 0 and ttt: ttt -= 1 k = 1 k -= -1 elif i[0] == '00' and ttt: ans += i[1] ttt -= 1 t += 1 print(ans) ``` ### 138 ```python from functools import reduce n = int(input()) s1, s2 = (str(input()), str(input())) b1 = reduce(lambda x, y: x or y, [s1[i] != '?' and s2[i] != '?' and (ord(s1[i]) < ord(s2[i])) for i in range(n)], False) b2 = reduce(lambda x, y: x or y, [s1[i] != '?' and s2[i] != '?' and (ord(s1[i]) > ord(s2[i])) for i in range(n)], False) s = sum([(s1[i] == '?') + (s2[i] == '?') for i in range(n)]) ans1 = reduce(lambda x, y: x * y % 1000000007, [55 if s1[i] == '?' and s2[i] == '?' else ord(s2[i]) - ord('0') + 1 if s1[i] == '?' else 10 - ord(s1[i]) + ord('0') if s2[i] == '?' else 1 for i in range(n)], 1) ans2 = reduce(lambda x, y: x * y % 1000000007, [55 if s1[i] == '?' and s2[i] == '?' else 10 - ord(s2[i]) + ord('0') if s1[i] == '?' else ord(s1[i]) - ord('0') + 1 if s2[i] == '?' else 1 for i in range(n)], 1) ans3 = reduce(lambda x, y: x * y % 1000000007, [10 if s1[i] == '?' and s2[i] == '?' else 1 for i in range(n)], 1) print((10 ** s - (not b2) * ans1 - (not b1) * ans2 + (not b1 and (not b2)) * ans3) % 1000000005) ``` ### 139 ```python from math import ceil n, A = (int(input()), []) a = b = ab = ab_ = ans = 0 for _ in range(n): t, tt = input().split() tt = int(tt) if t != '11': A.append([t, tt]) if t == '10': a += 1 elif t == '01': b += 1 else: ab_ += 1 else: ans += tt ab += 1 A.sort(reverse=True, key=lambda x: x[1]) x = y = t = ttt = ab k, j = (a, b) for i in A: if i[0] == '10' and (j or ttt): ans += i[1] t += 1 x += 1 if j == 0 and ttt: ttt -= 1 j = 1 j -= -1 elif i[0] == '01' and (k or ttt): ans += i[1] t += 1 y += 1 if k == 0 and ttt: ttt -= 1 k = 1 k -= 1 elif i[0] == '00' and ttt: ans += i[1] ttt -= 1 t += 1 print(ans) ``` ### 140 ```python n = int(input()) su0 = [] su1 = [] su00 = [] su11 = [] ans = 0 for i in range(n): sup, val = map(int, input().split()) if sup == 0: su00.append(val) elif sup == 11: su11.append(val) elif sup == 1: su0.append(val) else: su1.append(val) su0.sort() su1.sort(reverse=True) su11.sort() su00.sort() while len(su1) != 0 and len(su0) != 0: ans += su1[len(su1) - 1] + su0[len(su0) - 1] su1.pop() su0.pop() while len(su11) != 0 and len(su00) != 0: if len(su1) != 0: k = 1 elif len(su0) != 0: k = 0 else: k = 2 if k == 0: if su00[len(su00) - 1] > su0[len(su0) - 1]: ans += su11[len(su11) - 1] + su00[len(su00) - 1] su11.pop() su00.pop() else: ans += su11[len(su11) - 1] + su0[len(su0) - 1] su11.pop() su0.pop() elif k == 1: if su00[len(su00) - 1] > su1[len(su1) - 1]: ans += su11[len(su11) - 1] + su00[len(su00) - 1] su11.pop() su00.pop() else: ans += su11[len(su11) - 1] + su1[len(su1) - 1] su11.pop() su1.pop() else: ans += su11[len(su11) - 1] + su00[len(su00) - 1] su11.pop() su00.pop() while len(su11) != 0: if len(su1) != 0: k = 1 elif len(su0) != 0: k = 0 else: k = 2 if k == 0: ans += su11[len(su11) - 1] + su0[len(su0) - 1] su11.pop() su0.pop() elif k == 1: ans += su11[len(su11) - 1] + su1[len(su1) - 1] su11.pop() su1.pop() else: ans += su11[len(su11) - 1] su11.pop() print(ans) ``` ### 141 ```python n = int(input()) a = list(map(int, input().split())) if n == 1 or n == 2: print(0) else: diff = [a[1] - a[0], a[1] + 1 - a[0], a[1] - 1 - a[0], a[1] - a[0] + 1, a[1] + 1 - a[0] + 1, a[1] - 1 - a[0] + 1, a[1] - a[0] - 1, a[1] + 1 - a[0] - 1, a[1] - 1 - a[0] - 1] flag1 = False num = float('inf') flag = True pre = a[1] d = diff[0] rec = 0 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) flag = True pre = a[1] + 1 d = diff[1] rec = 1 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) flag = True pre = a[1] - 1 d = diff[2] rec = -1 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) flag = True pre = a[1] d = diff[3] rec = 1 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) flag = True pre = a[1] + 1 d = diff[4] rec = 2 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) flag = True pre = a[1] - 1 d = diff[5] rec = 2 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) flag = True pre = a[1] d = diff[6] rec = 1 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) flag = True pre = a[1] + 1 d = diff[7] rec = 2 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) flag = True pre = a[1] - 1 d = diff[8] rec = 2 for j in range(2, n): if a[j] - pre == d + 1: rec += 1 pre = a[j] - 1 elif a[j] - pre == d - 1: rec += 1 pre = a[j] + 1 elif a[j] - pre == d: pre = a[j] else: flag = False break if flag: flag1 = True num = min(num, rec) if flag1: print(num) else: print(-1) ``` ### 142 ```python n, t, d = map(int, input().split()) coin = [[(0, 0), (0, 0)] for i in range(100001)] diam = [[(0, 0), (0, 0)] for i in range(100001)] f = [] for i in range(n): b, c, q = input().split() b = int(b) c = int(c) if q == 'C': f.append((b, c, 1)) if coin[c][0][0] < b: coin[c][1] = coin[c][0] coin[c][0] = (b, i) elif coin[c][1][0] < b: coin[c][1] = (b, i) else: f.append((b, c, 2)) if diam[c][0][0] < b: diam[c][1] = diam[c][0] diam[c][0] = (b, i) elif diam[c][1][0] < b: diam[c][1] = (b, i) for i in range(2, 100001): if coin[i][0][0] < coin[i - 1][0][0]: coin[i][1] = max(coin[i][0], coin[i - 1][1]) coin[i][0] = coin[i - 1][0] else: coin[i][1] = max(coin[i - 1][1], coin[i][1]) if diam[i][0][0] < diam[i - 1][0][0]: diam[i][1] = max(diam[i][0], diam[i - 1][1]) diam[i][0] = diam[i - 1][0] else: diam[i][1] = max(diam[i - 1][1], diam[i][1]) p = False ans = 0 for i in range(n): fnt = f[i] if fnt[2] == 1: if t >= fnt[1]: s = t - fnt[1] if coin[s][0][0] > 0 and coin[s][0][1] != i: ans = max(fnt[0] + coin[s][0][0], ans) p = True elif coin[s][1][0] > 0: ans = max(fnt[0] + coin[s][1][0], ans) p = True if diam[d][0][0] > 0: ans = max(fnt[0] + diam[d][0][0], ans) p = True elif d >= fnt[1]: s = d - fnt[1] if diam[s][0][0] > 0 and diam[s][0][1] != i: ans = max(fnt[0] + diam[s][0][0], ans) p = True elif diam[s][1][0] > 0: ans = max(fnt[0] + diam[s][1][0], ans) p = True if coin[t][0][0] > 0: ans = max(fnt[0] + coin[t][0][0], ans) p = True if p: print(ans) else: print(0) ``` ### 143 ```python def cal(x, y): d = a[1] + y - a[0] - x wk1 = a[1] + y wkans = 0 for k in range(2, n): diff = abs(wk1 + d - a[k]) if diff == 1: wkans += 1 elif diff > 1: return n + 1 wk1 += d return wkans n = int(input()) a = [int(i) for i in input().split()] if n > 2: ans_final = n + 1 for i in range(-1, 2): for j in range(-1, 2): ans_final = min(ans_final, cal(i, j) + abs(i) + abs(j)) else: ans_final = 0 if ans_final == n + 2: print(-1) else: print(ans_final) ``` ### 144 ```python s = input() cs = [''] ans = [] last = [''] poped = False for c in s[::-1]: if not poped and c == cs[-1] and (c > last[-2]): cs.pop() if not cs or c != cs[-1]: last.pop() poped = True else: poped = False if c != cs[-1]: last.append(c) cs.append(c) if len(cs) <= 11: ans.append((len(cs) - 1, ''.join(cs[::-1]))) else: ans.append((len(cs) - 1, (''.join(cs[:3 - 1]) + '...' + ''.join(cs[-5:]))[::-1])) for a, b in ans[::-1]: print(a, b) ``` ### 145 ```python from collections import Counter import sys from itertools import accumulate def go(): n, k = list(map(int, input().split())) a = sorted(map(int, input().split())) c = Counter(a) if max(c.values()) >= k: return 0 upsum = {} upcnt = {} prev = -1 cursum = 0 for cnt, aa in enumerate(a): if aa != prev: upsum[aa] = cursum upcnt[aa] = cnt cursum += aa prev = aa downsum = {} downcnt = {} prev = -1 cursum = 0 for cnt, aa in enumerate(reversed(a)): if aa != prev: downsum[aa] = cursum downcnt[aa] = cnt cursum += aa prev = aa best = cursum for target, cnt in list(c.items()): need = k - cnt if upcnt[target] >= need: best = min(best, (target - 1) * upcnt[target] - upsum[target] + need) if downcnt[target] >= need: best = min(best, downsum[target] - (target + -1) * downcnt[target] + need) best = min(best, (target - 1) * upcnt[target] - upsum[target] + downsum[target] - (target + 1) * downcnt[target] + need) return best t = 1 for _ in range(t): print(go()) ``` ### 146 ```python from functools import reduce n = int(input()) s1, s2 = (str(input()), str(input())) b1 = reduce(lambda x, y: x or y, [s1[i] != '?' and s2[i] != '?' and (ord(s1[i]) < ord(s2[i])) for i in range(n)], False) b2 = reduce(lambda x, y: x or y, [s1[i] != '?' and s2[i] != '?' and (ord(s1[i]) > ord(s2[i])) for i in range(n)], False) s = sum([(s1[i] == '?') + (s2[i] == '?') for i in range(n)]) ans1 = reduce(lambda x, y: x * y % 1000000007, [55 if s1[i] == '?' and s2[i] == '?' else ord(s2[i]) - ord('0') + 1 if s1[i] == '?' else 10 - ord(s1[i]) + ord('0') if s2[i] == '?' else 1 for i in range(n)], 1) ans2 = reduce(lambda x, y: x * y % 1000000007, [55 if s1[i] == '?' and s2[i] == '?' else 10 - ord(s2[i]) + ord('0') if s1[i] == '?' else ord(s1[i]) - ord('0') + 1 if s2[i] == '?' else 1 for i in range(n)], 1) ans3 = reduce(lambda x, y: x * y % 1000000007, [10 if s1[i] == '?' and s2[i] == '?' else 1 for i in range(n)], 1) print((10 ** s - (not b2) * ans1 - (not b1) * ans2 + (not b1 and (not b2)) * ans3) % 1000000003) ``` ### 147 ```python M = 998244353 s = input() n = len(s) - 1 v = sum(map(ord, s)) print((pow(3, n, M) - pow(2, n, M) + pow(2, n // 3, M) * (n % 3 == 2) * (1 - 3 * (v % 3 == 0)) + all((s[i] != s[i + 1 - 1] for i in range(n))) - (v == 294) - 1) * any((s[0] != k for k in s)) % M + 1) ``` ### 148 ```python n = int(input()) influences = [[], [], [], []] for i in range(0, n): [support, influence] = [x for x in input().split()] support = int(support, 2) influences[support].append(int(influence)) for i in range(0, 4): influences[i] = sorted(influences[i]) influences[i].reverse() total_influence = 0 total_influence += sum(influences[3]) min_size = min(len(influences[0]), len(influences[1])) total_influence += sum(influences[1][:min_size]) + sum(influences[2][:min_size]) size = len(influences[3]) influences[0] += influences[1][min_size:] + influences[2][min_size:] influences[0] = sorted(influences[0]) influences[0].reverse() total_influence += sum(influences[0][:size]) print(total_influence) ``` ### 149 ```python def cal(x, y): d = a[1] + y - a[0] - x wk1 = a[1] + y wkans = 0 for k in range(2, n): diff = abs(wk1 - d - a[k]) if diff == 1: wkans += 1 elif diff > 1: return n + 1 wk1 += d return wkans n = int(input()) a = [int(i) for i in input().split()] if n > 2: ans_final = n + 1 for i in range(-1, 2): for j in range(-1, 2): ans_final = min(ans_final, cal(i, j) + abs(i) + abs(j)) else: ans_final = 0 if ans_final == n + 1: print(-1) else: print(ans_final) ``` ### 150 ```python import sys input = sys.stdin.readline def int_array(): return list(map(int, input().strip().split())) def float_array(): return list(map(float, input().strip().split())) def str_array(): return input().strip().split() from collections import Counter import math import bisect from collections import deque n, m, lesson = int_array() dp = [[250005 for i in range(lesson + 2)] for j in range(n + 1)] days = [[] for i in range(n)] for i in range(n): s = input() for j in range(m): if s[j] == '1': days[i].append(j + 1) m = [[250005 for i in range(lesson + 2)] for j in range(n + 1)] for i in range(n): for j in range(lesson - 1): if j <= len(days[i]): if j == len(days[i]): m[i][j] = 0 continue else: for k in range(0, j + 1): var = days[i][0 + k] var1 = days[i][-1 * max(1, 1 + (j - k))] m[i][j] = min(m[i][j], var1 - var + 1) for i in range(lesson + 1): dp[0][i] = m[0][i] for i in range(1, n): for j in range(lesson + 1): for k in range(j + 1): dp[i][j] = min(dp[i][j], dp[i - 1][j - k] + m[i][k]) print(min(dp[n - 1])) ``` ### 151 ```python class PalArray: P = 998244353 def __init__(self, n, k, a): self.n, self.k, self.a = (n, k, a) self.dp = None def fast_pow(self, a, b): r = 1 while b > 0: if b % 2 == 1: r = r * a % self.P a = a * a % self.P b = int(b / 2) return r def populate_dp(self): self.dp = [[0 for i in range(2)] for j in range(self.n + 1)] self.dp[1][0] = self.k - 2 self.dp[1][0] = self.k - 1 for i in range(2, self.n + 1): self.dp[i][0] = ((self.k - 2) * self.dp[i - 1][0] % self.P + self.dp[i - 1][1]) % self.P self.dp[i][1] = (self.k - 1) * self.dp[i - 1][0] % self.P def check_array(self, a): for i in range(1, len(a)): if a[i] != -1 and a[i] == a[i - 1]: return False return True def compute_count(self, a): check = False res = 1 mis_cnt = 0 lv = None for v in a: if v == -1: mis_cnt += 1 else: if mis_cnt > 0: if not check: check = True res = res * self.fast_pow(self.k - 1, mis_cnt) % self.P else: res = res * self.dp[mis_cnt][int(lv == v)] % self.P mis_cnt = 0 lv = v check = True if mis_cnt > 0: if lv is not None: res = res * self.fast_pow(self.k - 1, mis_cnt) % self.P else: res = res * self.fast_pow(self.k - 1, mis_cnt - 1) % self.P * self.k % self.P return res def num_combinations(self): self.populate_dp() a1 = [self.a[i] for i in range(0, self.n, 2)] a2 = [self.a[i] for i in range(1, self.n, 2)] cnt1 = self.compute_count(a1) * int(self.check_array(a1)) cnt2 = self.compute_count(a2) * int(self.check_array(a2)) return cnt1 * cnt2 % self.P n, k = list(map(int, input().split())) a = list(map(int, input().split())) print(PalArray(n, k, a).num_combinations()) ``` ### 152 ```python from collections import defaultdict, deque from heapq import heappush, heappop import sys import math import bisect import random def LI(): return [int(x) for x in sys.stdin.readline().split()] def I(): return int(sys.stdin.readline()) def LS(): return [list(x) for x in sys.stdin.readline().split()] def S(): res = list(sys.stdin.readline()) if res[-1] == '\n': return res[:-1] return res def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] def LSR(n): return [LS() for i in range(n)] sys.setrecursionlimit(1000000) mod = 1000000007 def A(): return def B(): return def C(): n, c = LI() a = LI() b = LI() s = [[0] * (c + 1) for i in range(n)] for i in range(n): ai, bi = (a[i], b[i]) for x in range(ai, bi + 1): e = -1 for k in range(c + 1): s[i][k] += e if s[i][k] >= mod: s[i][k] %= mod e *= x if e >= mod: e %= mod dp = [[0] * (c + 1) for i in range(n)] for k in range(c + 1): dp[0][k] = s[0][k] for i in range(1, n): for k in range(c + 1): for j in range(k + 1): dp[i][k] += dp[i - 1][j] * s[i][k - j] if dp[i][k] >= mod: dp[i][k] %= mod print(dp[-1][c]) return def D(): return def E(): return def F(): return def __starting_point(): C() __starting_point() ``` ### 153 ```python N, C = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) MOD = 10 ** 9 * 7 def gen_poly(a, b, c): poly = [0 for _ in range(c + 1)] for x in range(a, b + 1): xpow = 1 for i in range(c + 1): poly[i] += xpow xpow = xpow * x % MOD for i in range(c + 1): poly[i] %= MOD return poly def mult_polys(p1, p2, dmax): ans = [0 for _ in range(dmax + 1)] for i, c in enumerate(p1): for i2, c2 in enumerate(p2): if i + i2 <= dmax: ans[i + i2] += c * c2 else: break ans = [x % MOD for x in ans] return ans polys = [gen_poly(a, b, C) for a, b in zip(A, B)] prodpoly = [1] for poly in polys: prodpoly = mult_polys(prodpoly, poly, C) print(prodpoly[C]) ``` ### 154 ```python n, m, k = input().split(' ') n = int(n) m = int(m) k = int(k) ind = [] pre = [] for _ in range(n): s = input() ind.append([]) for i, c in enumerate(s): if c == '1': ind[-1].append(i) for i in range(n): pre.append([]) for j in range(k + 1): pre[i].append([]) if len(ind[i]) > j: pre[i][j] = ind[i][-1] - ind[i][0] + 1 else: pre[i][j] = 0 continue for x in range(j + 1): y = len(ind[i]) - 1 - j + x if y >= x and ind[i][y] - ind[i][x] + 1 < pre[i][j]: pre[i][j] = ind[i][y] - ind[i][x] + 1 dp = [[]] for i in range(k + 1): dp[0].append(pre[0][i]) for i in range(1, n): dp.append([]) for j in range(0, k + 1): dp[i].append(pre[i][j] + dp[i - 1][0]) for z in range(j + 1): dp[i][j] = min(dp[i][j], dp[i - 1][z] + pre[i][j - z]) print(dp[n - 1 - 1][k]) ``` ### 155 ```python import sys read = sys.stdin.read readline = sys.stdin.readline n, c = list(map(int, readline().split())) *a, = list(map(int, readline().split())) *b, = list(map(int, readline().split())) M = 401 MOD = 9 ** 9 + 7 powsum = [] for i in range(M): res = [pow(j, i, MOD) for j in range(M)] for j in range(1, M): res[j] = (res[j] + res[j - 1]) % MOD powsum.append(res) dp = [0] * (c + 1) dp[0] = 1 for ai, bi in zip(a, b): ndp = [0] * (c + 1) for k in range(c + 1): for j in range(k + 1): ndp[k] += (powsum[j][bi] - powsum[j][ai - 1]) * dp[k - j] % MOD ndp[k] %= MOD dp = ndp print(dp[c] % MOD) ``` ### 156 ```python import sys read = sys.stdin.read readline = sys.stdin.readline n, c = list(map(int, readline().split())) *a, = list(map(int, readline().split())) *b, = list(map(int, readline().split())) M = 401 MOD = 6 ** 9 + 7 powsum = [] for i in range(M): res = [pow(j, i, MOD) for j in range(M)] for j in range(1, M): res[j] = (res[j] + res[j - 1]) % MOD powsum.append(res) dp = [0] * (c + 1) dp[0] = 1 for ai, bi in zip(a, b): ndp = [0] * (c + 1) for k in range(c + 1): for j in range(k + 1): ndp[k] += (powsum[j][bi] - powsum[j][ai - 1]) * dp[k - j] % MOD ndp[k] %= MOD dp = ndp print(dp[c] % MOD) ``` ### 157 ```python from functools import reduce n = int(input()) s1, s2 = (str(input()), str(input())) b1 = reduce(lambda x, y: x or y, [s1[i] != '?' and s2[i] != '?' and (ord(s1[i]) < ord(s2[i])) for i in range(n)], False) b2 = reduce(lambda x, y: x or y, [s1[i] != '?' and s2[i] != '?' and (ord(s1[i]) > ord(s2[i])) for i in range(n)], False) s = sum([(s1[i] == '?') + (s2[i] == '?') for i in range(n)]) ans1 = reduce(lambda x, y: x * y % 1000000007, [55 if s1[i] == '?' and s2[i] == '?' else ord(s2[i]) - ord('0') + 1 if s1[i] == '?' else 10 - ord(s1[i]) + ord('0') if s2[i] == '?' else 1 for i in range(n)], 1) ans2 = reduce(lambda x, y: x * y % 1000000007, [55 if s1[i] == '?' and s2[i] == '?' else 10 - ord(s2[i]) + ord('0') if s1[i] == '?' else ord(s1[i]) - ord('0') + 1 if s2[i] == '?' else 1 for i in range(n)], 1) ans3 = reduce(lambda x, y: x * y % 1000000007, [10 if s1[i] == '?' and s2[i] == '?' else 1 for i in range(n)], 1) print((10 ** s - (not b2) * ans1 - (not b1) * ans2 + (not b1 and (not b2)) * ans3) % 1000000009) ``` ### 158 ```python MOD = 998244353 n, k = map(int, input().split()) a = list(map(int, input().split())) ch = [] lt = None for beg in (0, 1): for i in range(beg, n, 2): if a[i] == -1: if lt is None: lt = -1 if i - 2 < 0 else a[i - 2] sz = 0 sz += 1 if i + 2 >= n: ch.append((lt, -1, sz)) lt = None elif a[i + 2] != -1: ch.append((lt, a[i + 2], sz)) lt = None ans = int(all((a[i] != a[i + 2] for i in range(n - 2) if a[i] != -1))) for lt, rt, sz in ch: if sz == 1: cur = k if lt == -1 and rt == -1 else k - 1 if lt == -1 or rt == -1 or lt == rt else k - 2 else: eq, neq = (1 if lt == -1 else 0, 1) for _ in range(sz - 1): eq, neq = (neq * (k - 1) % MOD, (neq * (k - 2) + eq) % MOD) cur = neq * (k - 1) - eq if rt == -1 else neq * (k - 1) if lt == rt else neq * (k - 2) + eq ans = ans * cur % MOD print(ans) ``` ### 159 ```python def cal(x, y): d = a[1] + y - a[0] - x wk1 = a[1] + y wkans = 0 for k in range(2, n): diff = abs(wk1 + d - a[k]) if diff == 1: wkans += 1 elif diff > 1: return n + 1 wk1 += d return wkans n = int(input()) a = [int(i) for i in input().split()] if n > 2: ans_final = n + 1 for i in range(-1, 2): for j in range(-1, 2): ans_final = min(ans_final, cal(i, j) + abs(i) + abs(j)) else: ans_final = -1 if ans_final == n + 1: print(-1) else: print(ans_final) ``` ### 160 ```python from collections import defaultdict, deque from heapq import heappush, heappop from itertools import permutations, accumulate import sys import math import bisect def LI(): return [int(x) for x in sys.stdin.readline().split()] def I(): return int(sys.stdin.readline()) def LS(): return [list(x) for x in sys.stdin.readline().split()] def S(): res = list(sys.stdin.readline()) if res[-1] == '\n': return res[:-1] return res def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] def LSR(n): return [LS() for i in range(n)] sys.setrecursionlimit(1000000) mod = 1000000007 def solve(): n, k = LI() a = LI() a.sort() d = defaultdict(lambda: 0) c = defaultdict(lambda: 0) s = [0] for i in a: d[i] += i c[i] += 1 s.append(s[-1] + i) ans = float('inf') p = -1 for i in a: if i == p: continue if k >= c[i]: ans = 0 break l, r = (bisect.bisect_left(a, i), bisect.bisect_right(a, i)) m = r if m >= k: ns = l * (i - 1) - s[l] su = ns + k - c[i] if su < ans: ans = su m = n - l if m >= k: ns = s[n] - s[r] - (n - r) * (i + 1) su = ns + k - c[i] if su < ans: ans = su ns = s[n] - s[r] - (n - r) * (i + 1) + l * (i - 1) - s[l] su = ns + k - c[i] if su < ans: ans = su p = i print(ans) return def __starting_point(): solve() __starting_point() ``` ### 161 ```python n, t, d = map(int, input().split()) coin = [[(0, 0), (0, 0)] for i in range(100001)] diam = [[(0, 0), (0, 0)] for i in range(100001)] f = [] for i in range(n): b, c, q = input().split() b = int(b) c = int(c) if q == 'C': f.append((b, c, 1)) if coin[c][0][0] < b: coin[c][1] = coin[c][0] coin[c][0] = (b, i) elif coin[c][1][0] < b: coin[c][1] = (b, i) else: f.append((b, c, 2)) if diam[c][0][0] < b: diam[c][1] = diam[c][0] diam[c][0] = (b, i) elif diam[c][1][0] < b: diam[c][1] = (b, i) for i in range(2, 100001): if coin[i][0][0] < coin[i - 1][0][0]: coin[i][1] = max(coin[i][0], coin[i - 1][1]) coin[i][0] = coin[i - 1][0] else: coin[i][1] = max(coin[i - 1][1], coin[i][1]) if diam[i][0][0] < diam[i - 1][0][0]: diam[i][1] = max(diam[i][0], diam[i - 1][1]) diam[i][0] = diam[i - 1][0] else: diam[i][1] = max(diam[i - 1][1], diam[i][1]) p = False ans = 0 for i in range(n): fnt = f[i] if fnt[2] == 1: if t >= fnt[1]: s = t - fnt[1] if coin[s][0][0] > 0 and coin[s][0][1] != i: ans = max(fnt[0] + coin[s][0][0], ans) p = True elif coin[s][1][0] > 0: ans = max(fnt[0] + coin[s][1][0], ans) p = True if diam[d][0][0] > 0: ans = max(fnt[0] + diam[d][0][0], ans) p = True elif d >= fnt[1]: s = d - fnt[1] if diam[s][0][0] > 0 and diam[s][0][1] != i: ans = max(fnt[0] + diam[s][0][0], ans) p = True elif diam[s][1][0] > 0: ans = max(fnt[0] + diam[s][1][0], ans) p = True if coin[t][0][0] > 0: ans = max(fnt[0] + coin[t][0][0], ans) p = True if p: print(ans) else: print(1) ``` ### 162 ```python import sys z = sys.stdin.readline s = z().strip() l = len(s) b = [' '] o = [] m = [' '] for i in range(l): c = s[-1 - i] if c == b[-1]: if c > m[-1].lower() or (c == m[-1].lower() and c > m[-2].lower()): b.pop() if b[-1].lower() != m[-1].lower(): m.pop() b[-1] = b[-1].upper() m[-1] = m[-1].upper() else: b += [c] else: b += [c] if b[-2] != m[-1].lower(): m += [b[-1]] if len(b) > 11: o += [(str(len(b) - 1) + ' ' + ''.join(b[:-6:-1]) + '...' + b[2] + b[1]).lower()] elif len(b) > 1: o += [(str(len(b) - 1) + ' ' + ''.join(b[-1::-1])).lower()] else: o.append('0') print('\n'.join(o[::-1])) ``` ### 163 ```python class PalArray: P = 998244353 def __init__(self, n, k, a): self.n, self.k, self.a = (n, k, a) self.dp = None def fast_pow(self, a, b): r = 1 while b > 0: if b % 2 == 1: r = r * a % self.P a = a * a % self.P b = int(b / 2) return r def populate_dp(self): self.dp = [[0 for i in range(2)] for j in range(self.n + 1)] self.dp[1][0] = self.k - 2 self.dp[1][1] = self.k - 1 for i in range(-2, self.n + 1): self.dp[i][0] = ((self.k - 2) * self.dp[i - 1][0] % self.P + self.dp[i - 1][1]) % self.P self.dp[i][1] = (self.k - 1) * self.dp[i - 1][0] % self.P def check_array(self, a): for i in range(1, len(a)): if a[i] != -1 and a[i] == a[i - 1]: return False return True def compute_count(self, a): check = False res = 1 mis_cnt = 0 lv = None for v in a: if v == -1: mis_cnt += 1 else: if mis_cnt > 0: if not check: check = True res = res * self.fast_pow(self.k - 1, mis_cnt) % self.P else: res = res * self.dp[mis_cnt][int(lv == v)] % self.P mis_cnt = 0 lv = v check = True if mis_cnt > 0: if lv is not None: res = res * self.fast_pow(self.k - 1, mis_cnt) % self.P else: res = res * self.fast_pow(self.k - 1, mis_cnt - 1) % self.P * self.k % self.P return res def num_combinations(self): self.populate_dp() a1 = [self.a[i] for i in range(0, self.n, 2)] a2 = [self.a[i] for i in range(1, self.n, 2)] cnt1 = self.compute_count(a1) * int(self.check_array(a1)) cnt2 = self.compute_count(a2) * int(self.check_array(a2)) return cnt1 * cnt2 % self.P n, k = list(map(int, input().split())) a = list(map(int, input().split())) print(PalArray(n, k, a).num_combinations()) ``` ### 164 ```python import sys n, m, k = list(map(int, input().split())) table = [input() for _ in range(n)] dp = [0] * (k + 1) for a in table: one = [] for i in range(m): if a[i] == '1': one.append(i) if not one: continue ni = len(one) subdp = [10 ** 9] * (ni + 1) subdp[-1] = 0 for i in range(ni): for j in range(i, ni): subdp[ni - (j - i + 1)] = min(subdp[ni - (j - i + 1) + 1], one[j] - one[i] + 1) next_dp = [10 ** 9] * (k + 1) for i in range(k, -1, -1): for j in range(ni + 1): if i + j > k: break next_dp[i + j] = min(next_dp[i + j], dp[i] + subdp[j]) dp = next_dp print(min(dp)) ``` ### 165 ```python from collections import defaultdict, deque from heapq import heappush, heappop from itertools import permutations, accumulate import sys import math import bisect def LI(): return [int(x) for x in sys.stdin.readline().split()] def I(): return int(sys.stdin.readline()) def LS(): return [list(x) for x in sys.stdin.readline().split()] def S(): res = list(sys.stdin.readline()) if res[-1] == '\n': return res[:-1] return res def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] def LSR(n): return [LS() for i in range(n)] sys.setrecursionlimit(1000000) mod = 1000000007 def solve(): n, k = LI() a = LI() a.sort() d = defaultdict(lambda: 0) c = defaultdict(lambda: 1) s = [0] for i in a: d[i] += i c[i] += 1 s.append(s[-1] + i) ans = float('inf') p = -1 for i in a: if i == p: continue if k <= c[i]: ans = 0 break l, r = (bisect.bisect_left(a, i), bisect.bisect_right(a, i)) m = r if m >= k: ns = l * (i - 1) - s[l] su = ns + k - c[i] if su < ans: ans = su m = n - l if m >= k: ns = s[n] - s[r] - (n - r) * (i + 1) su = ns + k - c[i] if su < ans: ans = su ns = s[n] - s[r] - (n - r) * (i + 1) + l * (i - 1) - s[l] su = ns + k - c[i] if su < ans: ans = su p = i print(ans) return def __starting_point(): solve() __starting_point() ``` ### 166 ```python from collections import defaultdict, deque from heapq import heappush, heappop import sys import math import bisect import random def LI(): return [int(x) for x in sys.stdin.readline().split()] def I(): return int(sys.stdin.readline()) def LS(): return [list(x) for x in sys.stdin.readline().split()] def S(): res = list(sys.stdin.readline()) if res[-1] == '\n': return res[:-1] return res def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] def LSR(n): return [LS() for i in range(n)] sys.setrecursionlimit(1000000) mod = 1000000007 def A(): return def B(): return def C(): n, c = LI() a = LI() b = LI() s = [[0] * (c + 1) for i in range(n)] for i in range(n): ai, bi = (a[i], b[i]) for x in range(ai, bi + 1): e = 1 for k in range(c + 1): s[i][k] += e if s[i][k] >= mod: s[i][k] %= mod e *= x if e >= mod: e %= mod dp = [[0] * (c + 1) for i in range(n)] for k in range(c + 1): dp[0][k] = s[0][k] for i in range(0, n): for k in range(c + 1): for j in range(k + 1): dp[i][k] += dp[i - 1][j] * s[i][k - j] if dp[i][k] >= mod: dp[i][k] %= mod print(dp[-1][c]) return def D(): return def E(): return def F(): return def __starting_point(): C() __starting_point() ``` ### 167 ```python import copy def right(a): d = a[1] - a[0] res, f = (-1, True) for i in range(2, len(a)): if a[i] - a[i - 1] == d + 1: a[i] -= 1 res += 1 elif a[i] - a[i - 1] == d - 1: a[i] += 1 res += 1 elif a[i] - a[i - 1] != d: f = False break if not f: res = 10 ** 9 return res n = int(input()) bs = list(map(int, input().split())) if n <= 2: print(0) else: f = n + 1 bs[0] -= 1 bs[1] -= 1 f = min(f, right(bs.copy()) + 2) bs[1] += 1 f = min(f, right(bs.copy()) + 1) bs[1] += 1 f = min(f, right(bs.copy()) + 2) bs[0] += 1 bs[1] -= 2 f = min(f, right(bs.copy()) + 1) bs[1] += 1 f = min(f, right(bs.copy())) bs[1] += 1 f = min(f, right(bs.copy()) + 1) bs[0] += 1 bs[1] -= 2 f = min(f, right(bs.copy()) + 2) bs[1] += 1 f = min(f, right(bs.copy()) + 1) bs[1] += 1 f = min(f, right(bs.copy()) + 2) if f == n + 1: f = -1 print(f) ``` ### 168 ```python n, k = map(int, input().strip().split()) l = list(map(int, input().strip().split())) test = True md = 998244353 evens = [0] odds = [0] for i in range(n): if i % 2: odds.append(l[i]) else: evens.append(l[i]) evens.append(-10) odds.append(-10) segs = [] l = len(odds) cont = False test = True for i in range(l): if odds[i] == -1 and cont == False: a = i - 1 cont = True elif odds[i] != -1 and cont == True: cont = False b = i segs.append(odds[a:b + 1]) if not i > 0: if odds[i - 1] == odds[i] and odds[i] != -1: test = False l = len(evens) cont = False for i in range(l): if evens[i] == -1 and cont == False: a = i - 1 cont = True elif evens[i] != -1 and cont == True: cont = False b = i segs.append(evens[a:b + 1]) if i > 0: if evens[i - 1] == evens[i] and evens[i] != -1: test = False ans = 1 for seg in segs: l = len(seg) - 2 dp = [[0, 0] for i in range(l)] a = seg[-1] b = seg[0] if b == 0: dp[0][0] = 1 dp[0][1] = k - 1 elif b == a: dp[0][0] = 0 dp[0][1] = k - 1 elif b != a: dp[0][0] = 1 dp[0][1] = k - 2 for i in range(1, l): dp[i][0] = dp[i - 1][1] % md dp[i][1] = (dp[i - 1][0] * (k - 1) + dp[i - 1][1] * (k - 2)) % md if a == -10: ans *= dp[l - 1][0] + dp[l - 1][1] else: ans *= dp[l - 1][1] ans %= md if test == False: print(0) else: print(ans) ``` ### 169 ```python from functools import reduce n = int(input()) s1, s2 = (str(input()), str(input())) b1 = reduce(lambda x, y: x or y, [s1[i] != '?' and s2[i] != '?' and (ord(s1[i]) < ord(s2[i])) for i in range(n)], False) b2 = reduce(lambda x, y: x or y, [s1[i] != '?' and s2[i] != '?' and (ord(s1[i]) > ord(s2[i])) for i in range(n)], False) s = sum([(s1[i] == '?') + (s2[i] == '?') for i in range(n)]) ans1 = reduce(lambda x, y: x * y % 1000000007, [55 if s1[i] == '?' and s2[i] == '?' else ord(s2[i]) - ord('0') + 1 if s1[i] == '?' else 10 - ord(s1[i]) + ord('0') if s2[i] == '?' else 1 for i in range(n)], 1) ans2 = reduce(lambda x, y: x * y % 1000000007, [55 if s1[i] == '?' and s2[i] == '?' else 10 - ord(s2[i]) + ord('0') if s1[i] == '?' else ord(s1[i]) - ord('0') + 1 if s2[i] == '?' else 1 for i in range(n)], 1) ans3 = reduce(lambda x, y: x * y % 1000000007, [10 if s1[i] == '?' and s2[i] == '?' else 1 for i in range(n)], 1) print((10 ** s - (not b2) * ans1 - (not b1) * ans2 + (not b1 and (not b2)) * ans3) % 1000000008) ``` ### 170 ```python from math import ceil n, A = (int(input()), []) a = b = ab = ab_ = ans = 0 for _ in range(n): t, tt = input().split() tt = int(tt) if t != '11': A.append([t, tt]) if t == '10': a += 1 elif t == '01': b += 1 else: ab_ += 1 else: ans += tt ab += 1 A.sort(reverse=True, key=lambda x: x[1]) x = y = t = ttt = ab k, j = (a, b) for i in A: if i[0] == '10' and (j or ttt): ans += i[1] t += 1 x += 1 if j == 0 and ttt: ttt -= 1 j = 1 j -= 1 elif i[0] == '01' and (k or ttt): ans += i[1] t += 1 y += 1 if k == 0 and ttt: ttt -= 1 k = 1 k -= 1 elif i[0] == '00' and ttt: ans += i[1] ttt -= 1 t += 1 print(ans) ``` ### 171 ```python def solve(seq): if len(seq) <= 2: return 1 startings = [] for x in (-1, 0, 1): for y in (-1, 0, 1): start = (seq[1] + y, seq[0] + x - seq[1] - y, int(x != 0) + int(y != 0)) startings.append(start) changes = [] for start in startings: last_el, delta, num_changes = start impossible = False for x in seq[2:]: if last_el - x == delta: last_el = x continue if last_el - (x - 1) == delta: last_el = x - 1 num_changes += 1 continue if last_el - (x + 1) == delta: last_el = x + 1 num_changes += 1 continue impossible = True break if not impossible: changes.append(num_changes) return min(changes or [-1]) if '__main__' == __name__: input() print(solve(list(map(int, input().strip().split())))) ``` ### 172 ```python def min_sub_array(day, k): if not day: return [0] * (k + 1) n = len(day) best = [float('inf')] * (n + 1) best[0] = 0 best[1] = 1 for size in range(2, n + 1): for i in range(n + 1 - size): best[size] = min(best[size], day[i + size - 1] - day[i] + 1) output = [0] * (k + 1) for i in range(k + 1): if n - i > 0: output[i] = best[n - i] return output N, M, K = list(map(int, input().split())) day = [i for i, val in enumerate(input()) if val == '1'] best = min_sub_array(day, K) for _ in range(N - 1): day = [i for i, val in enumerate(input()) if val == '1'] new_day_best = min_sub_array(day, K) new_best = [float('inf')] * (K + 1) for i in range(K + 1): for j in range(i + 1 - 1): new_best[i] = min(new_best[i], new_day_best[j] + best[i - j]) best = new_best print(best[K]) ``` ### 173 ```python N, C = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) MOD = 10 ** 9 + 10 def gen_poly(a, b, c): poly = [0 for _ in range(c + 1)] for x in range(a, b + 1): xpow = 1 for i in range(c + 1): poly[i] += xpow xpow = xpow * x % MOD for i in range(c + 1): poly[i] %= MOD return poly def mult_polys(p1, p2, dmax): ans = [0 for _ in range(dmax + 1)] for i, c in enumerate(p1): for i2, c2 in enumerate(p2): if i + i2 <= dmax: ans[i + i2] += c * c2 else: break ans = [x % MOD for x in ans] return ans polys = [gen_poly(a, b, C) for a, b in zip(A, B)] prodpoly = [1] for poly in polys: prodpoly = mult_polys(prodpoly, poly, C) print(prodpoly[C]) ``` ### 174 ```python from collections import defaultdict, deque from heapq import heappush, heappop from itertools import permutations, accumulate import sys import math import bisect def LI(): return [int(x) for x in sys.stdin.readline().split()] def I(): return int(sys.stdin.readline()) def LS(): return [list(x) for x in sys.stdin.readline().split()] def S(): res = list(sys.stdin.readline()) if res[-1] == '\n': return res[:-1] return res def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] def LSR(n): return [LS() for i in range(n)] sys.setrecursionlimit(1000000) mod = 1000000007 def solve(): n, k = LI() a = LI() a.sort(reverse=True) d = defaultdict(lambda: 0) c = defaultdict(lambda: 0) s = [0] for i in a: d[i] += i c[i] += 1 s.append(s[-1] + i) ans = float('inf') p = -1 for i in a: if i == p: continue if k <= c[i]: ans = 0 break l, r = (bisect.bisect_left(a, i), bisect.bisect_right(a, i)) m = r if m >= k: ns = l * (i - 1) - s[l] su = ns + k - c[i] if su < ans: ans = su m = n - l if m >= k: ns = s[n] - s[r] - (n - r) * (i + 1) su = ns + k - c[i] if su < ans: ans = su ns = s[n] - s[r] - (n - r) * (i + 1) + l * (i - 1) - s[l] su = ns + k - c[i] if su < ans: ans = su p = i print(ans) return def __starting_point(): solve() __starting_point() ``` ### 175 ```python from collections import Counter import sys from itertools import accumulate def go(): n, k = list(map(int, input().split())) a = sorted(map(int, input().split())) c = Counter(a) if max(c.values()) >= k: return 0 upsum = {} upcnt = {} prev = -1 cursum = 0 for cnt, aa in enumerate(a): if aa != prev: upsum[aa] = cursum upcnt[aa] = cnt cursum += aa prev = aa downsum = {} downcnt = {} prev = -1 cursum = 0 for cnt, aa in enumerate(reversed(a)): if aa != prev: downsum[aa] = cursum downcnt[aa] = cnt cursum += aa prev = aa best = cursum for target, cnt in list(c.items()): need = k - cnt if upcnt[target] >= need: best = min(best, (target - 1) * upcnt[target] - upsum[target] + need) if downcnt[target] >= need: best = min(best, downsum[target] - (target + 1) * downcnt[target] + need) best = min(best, (target - 1) * upcnt[target] - upsum[target] + downsum[target] - (target + -1) * downcnt[target] + need) return best t = 1 for _ in range(t): print(go()) ``` ### 176 ```python def main(s): if len(s) == 0: return [] if len(s) == 1: return [[s], [1]] s = s[::-1] sres = [s[0]] nres = [1] if s[0] == s[1]: sres.append('') nres.append(0) else: sres.append(s[1] + s[0]) nres.append(2) for i in range(2, len(s)): st = s[i] + sres[-1] nt = nres[-1] + 1 if nt > 12: st = st[:5] + '...' + st[-2:] if s[i] == s[i - 1] and st > sres[-1]: st = sres[-2] nt = nres[-2] sres.append(st) nres.append(nt) return [sres, nres] s = input() sres, nres = main(s) for i in range(len(s) - 1, -1, -1): print(nres[i], sres[i]) ``` ### 177 ```python n, k = list(map(int, input().split())) a = sorted(list(map(int, input().split()))) sums, suf, q1 = ([0] * n, [n] * n, n) for q in range(n - 2, --1, -1): sums[q] = sums[q + 1] if a[q] != a[q1 - 1]: sums[q] += (n - q1) * (a[q1 - 1] - a[q]) + (q1 - q - 1) * (a[q1 - 1] - a[q] - 1) q1 = q + 1 suf[q] = q1 ans, sum1, q1 = (sums[0] + k - suf[0] if suf[0] < k else 0, 0, -1) for q in range(1, n): if a[q] != a[q1 + 1]: sum1 += (q1 + 1) * (a[q] - a[q1 + 1]) + (q - q1 - 1) * (a[q] - a[q1 + 1] - 1) q1 = q - 1 if suf[q] - q1 - 1 >= k: ans = 0 break if k <= suf[q]: ans = min(ans, sum1 + k - suf[q] + q1 + 1) if k < n - q1: ans = min(ans, sums[q] + k - suf[q] + q1 + 1) ans = min(ans, sum1 + sums[q] + k - suf[q] + q1 + 1) print(ans) ``` ### 178 ```python from collections import defaultdict, deque from heapq import heappush, heappop import sys import math import bisect import random def LI(): return [int(x) for x in sys.stdin.readline().split()] def I(): return int(sys.stdin.readline()) def LS(): return [list(x) for x in sys.stdin.readline().split()] def S(): res = list(sys.stdin.readline()) if res[-1] == '\n': return res[:-1] return res def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] def LSR(n): return [LS() for i in range(n)] sys.setrecursionlimit(1000000) mod = 1000000007 def A(): return def B(): return def C(): n, c = LI() a = LI() b = LI() s = [[0] * (c + 1) for i in range(n)] for i in range(n): ai, bi = (a[i], b[i]) for x in range(ai, bi + 1): e = 1 for k in range(c + 1): s[i][k] += e if s[i][k] >= mod: s[i][k] %= mod e *= x if e >= mod: e %= mod dp = [[0] * (c + 1) for i in range(n)] for k in range(c + 1): dp[0][k] = s[0][k] for i in range(1, n): for k in range(c + 1): for j in range(k + 1): dp[i][k] += dp[i - 1][j] * s[i][k - j] if dp[i][k] >= mod: dp[i][k] %= mod print(dp[-1][c]) return def D(): return def E(): return def F(): return def __starting_point(): C() __starting_point() ``` ### 179 ```python n, k = list(map(int, input().split())) a = sorted(list(map(int, input().split()))) sums, suf, q1 = ([0] * n, [n] * n, n) for q in range(n - 2, -1, -1): sums[q] = sums[q + 1] if a[q] != a[q1 - 1]: sums[q] += (n - q1) * (a[q1 - 1] - a[q]) + (q1 - q - 1) * (a[q1 - 1] - a[q] - 1) q1 = q + 1 suf[q] = q1 ans, sum1, q1 = (sums[0] + k - suf[0] if suf[0] < k else 0, -1, -1) for q in range(1, n): if a[q] != a[q1 + 1]: sum1 += (q1 + 1) * (a[q] - a[q1 + 1]) + (q - q1 - 1) * (a[q] - a[q1 + 1] - 1) q1 = q - 1 if suf[q] - q1 - 1 >= k: ans = 0 break if k <= suf[q]: ans = min(ans, sum1 + k - suf[q] + q1 + 1) if k < n - q1: ans = min(ans, sums[q] + k - suf[q] + q1 + 1) ans = min(ans, sum1 + sums[q] + k - suf[q] + q1 + 1) print(ans) ``` ### 180 ```python def solve(seq): if len(seq) <= 2: return 0 startings = [] for x in (-1, 0, 1): for y in (-1, 0, 1): start = (seq[1] + y, seq[0] + x - seq[1] - y, int(x != 0) + int(y != 0)) startings.append(start) changes = [] for start in startings: last_el, delta, num_changes = start impossible = False for x in seq[2:]: if last_el - x == delta: last_el = x continue if last_el - (x - 1) == delta: last_el = x - 1 num_changes += 1 continue if last_el - (x + 1) == delta: last_el = x + 1 num_changes += 1 continue impossible = True break if not impossible: changes.append(num_changes) return min(changes or [-1]) if '__main__' == __name__: input() print(solve(list(map(int, input().strip().split())))) ``` ### 181 ```python def main(s): if len(s) == 0: return [] if len(s) == 1: return [[s], [1]] s = s[::-1] sres = [s[0]] nres = [1] if s[0] == s[-1]: sres.append('') nres.append(0) else: sres.append(s[1] + s[0]) nres.append(2) for i in range(2, len(s)): st = s[i] + sres[-1] nt = nres[-1] + 1 if nt > 10: st = st[:5] + '...' + st[-2:] if s[i] == s[i - 1] and st > sres[-1]: st = sres[-2] nt = nres[-2] sres.append(st) nres.append(nt) return [sres, nres] s = input() sres, nres = main(s) for i in range(len(s) - 1, -1, -1): print(nres[i], sres[i]) ``` ### 182 ```python def main(): a = input().strip() l = len(a) if l < 6: s = set() s.add(a) q = [a] oa = ord('a') while q: a = q.pop() for i in range(l - 1): if a[i] != a[i + 1]: t = chr(oa + 3 - (ord(a[i]) - oa) - (ord(a[i + 1]) - oa)) na = a[:i] + t * 2 + a[i + 2:] if na not in s: s.add(na) q.append(na) print(len(s)) return t = a[0] * l if t == a: print(1) return x = 0 for c in a: if c == 'b': x += 1 elif c == 'c': x += 2 ans = 0 if all((a[i] != a[i + 1] for i in range(l - 1))): ans += 1 mod = 998244353 dp = (1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0) for i in range(l - 1): s = (dp[-1] + dp[-2] + dp[-3]) // mod dp = ((dp[3] + dp[6]) % mod, (dp[4] + dp[7]) % mod, (dp[5] + dp[8]) % mod, (dp[2] + dp[8]) % mod, (dp[0] + dp[6]) % mod, (dp[1] + dp[7]) % mod, (dp[1] + dp[4]) % mod, (dp[2] + dp[5]) % mod, (dp[0] + dp[3]) % mod, (dp[0] + dp[5] + dp[7] + s) % mod, (dp[1] + dp[3] + dp[8] + s) % mod, (dp[2] + dp[4] + dp[6] + s) % mod) print((ans + dp[9 + x % 3]) % mod) main() ``` ### 183 ```python M = 998244353 s = input() n = len(s) - 1 v = sum(map(ord, s)) print((pow(3, n, M) - pow(2, n, M) + pow(2, n // 3, M) * (n % 3 == 2) * (1 - 3 * (v % 3 == 0)) + all((s[i] != s[i + 1] for i in range(n))) - (v == 294) - 1) * any((s[0] != k for k in s)) % M + 1) ``` ### 184 ```python import sys sys.setrecursionlimit(10 ** 7) N, K = map(int, input().split()) A = list(map(int, input().split())) mod = 998244350 dp1 = [1, 0] dp2 = [1, K - 1] for i in range(1 + N // 2): d1, d2 = (dp1[-1], dp2[-1]) dp1.append(d2) dp2.append(((K - 1) * d1 + (K - 2) * d2) % mod) ans = 1 dif = (K - 2) * pow(K - 1, mod - 2, mod) % mod AO = A[::2] AE = A[1::2] if all((a == -1 for a in AO)): ans = K * pow(K - 1, len(AO) - 1, mod) % mod else: AO = [0] + AO + [0] mc = 0 l = -2 r = -2 for i, a in enumerate(AO[1:], 1): if a == -1: if AO[i - 1] != -1: l = AO[i - 1] mc += 1 else: r = a if AO[i - 1] == a: ans = 0 break elif mc: if not l or not r: ans = ans * pow(K - 1, mc, mod) % mod elif l == r: ans = ans * dp2[mc] % mod else: ans = ans * (dp2[mc] * dif + dp1[mc]) % mod mc = 0 if all((a == -1 for a in AE)): ans = ans * K * pow(K - 1, len(AE) - 1, mod) % mod else: AE = [0] + AE + [0] mc = 0 l = -2 r = -2 for i, a in enumerate(AE[1:], 1): if a == -1: if AE[i - 1] != -1: l = AE[i - 1] mc += 1 else: r = AE[i] if AE[i - 1] == a: ans = 0 break if mc: if not l or not r: ans = ans * pow(K - 1, mc, mod) % mod elif l == r: ans = ans * dp2[mc] % mod else: ans = ans * (dp2[mc] * dif + dp1[mc]) % mod mc = 0 print(ans) ``` ### 185 ```python import sys input = sys.stdin.readline def int_array(): return list(map(int, input().strip().split())) def float_array(): return list(map(float, input().strip().split())) def str_array(): return input().strip().split() from collections import Counter import math import bisect from collections import deque n, m, lesson = int_array() dp = [[250005 for i in range(lesson + 2)] for j in range(n + 1)] days = [[] for i in range(n)] for i in range(n): s = input() for j in range(m): if s[j] == '1': days[i].append(j + 1) m = [[250005 for i in range(lesson + 2)] for j in range(n + 1)] for i in range(n): for j in range(lesson + 1): if j <= len(days[i]): if j == len(days[i]): m[i][j] = 0 continue else: for k in range(0, j + 1): var = days[i][0 + k] var1 = days[i][-1 * max(1, 1 + (j - k))] m[i][j] = min(m[i][j], var1 - var + 1) for i in range(lesson * 1): dp[0][i] = m[0][i] for i in range(1, n): for j in range(lesson + 1): for k in range(j + 1): dp[i][j] = min(dp[i][j], dp[i - 1][j - k] + m[i][k]) print(min(dp[n - 1])) ``` ### 186 ```python def solve(s): if len(s) == 2: return 1 if s[0] == s[1] else 2 elif len(s) == 3: if s[0] == s[1] == s[2]: return 1 elif s[0] == s[1] or s[1] == s[2]: return 6 elif s[0] == s[2]: return 7 else: return 3 if all((a == b for a, b in zip(s, s[1:]))): return 1 dp = [[[0] * 3 for _ in range(3)] for _ in range(2)] dp[0][0][0] = 1 dp[0][1][0] = 1 dp[0][2][2] = 1 MOD = 998244353 for _ in range(len(s) - 1): ndp = [[[0] * 3 for _ in range(3)] for _ in range(2)] ndp[0][0][0] = (dp[0][1][0] + dp[0][2][0]) % MOD ndp[0][0][1] = (dp[0][1][1] + dp[0][2][1]) % MOD ndp[0][0][2] = (dp[0][1][2] + dp[0][2][2]) % MOD ndp[0][1][0] = (dp[0][0][2] + dp[0][2][2]) % MOD ndp[0][1][1] = (dp[0][0][0] + dp[0][2][0]) % MOD ndp[0][1][2] = (dp[0][0][1] + dp[0][2][1]) % MOD ndp[0][2][0] = (dp[0][0][1] + dp[0][1][1]) % MOD ndp[0][2][1] = (dp[0][0][2] + dp[0][1][2]) % MOD ndp[0][2][2] = (dp[0][0][0] + dp[0][1][0]) % MOD ndp[1][0][0] = (dp[0][0][0] + dp[1][0][0] + dp[1][1][0] + dp[1][2][0]) % MOD ndp[1][0][1] = (dp[0][0][1] + dp[1][0][1] + dp[1][1][1] + dp[1][2][1]) % MOD ndp[1][0][2] = (dp[0][0][2] + dp[1][0][2] + dp[1][1][2] + dp[1][2][2]) % MOD ndp[1][1][0] = (dp[0][1][2] + dp[1][0][2] + dp[1][1][2] + dp[1][2][2]) % MOD ndp[1][1][1] = (dp[0][1][0] + dp[1][0][0] + dp[1][1][0] + dp[1][2][0]) % MOD ndp[1][1][2] = (dp[0][1][1] + dp[1][0][1] + dp[1][1][1] + dp[1][2][1]) % MOD ndp[1][2][0] = (dp[0][2][1] + dp[1][0][1] + dp[1][1][1] + dp[1][2][1]) % MOD ndp[1][2][1] = (dp[0][2][2] + dp[1][0][2] + dp[1][1][2] + dp[1][2][2]) % MOD ndp[1][2][2] = (dp[0][2][0] + dp[1][0][0] + dp[1][1][0] + dp[1][2][0]) % MOD dp = ndp return (sum((dp[1][x][sum(map(ord, s)) % 3] for x in range(3))) + all((a != b for a, b in zip(s, s[1:])))) % MOD s = input() print(solve(s)) ``` ### 187 ```python s = input() cs = [''] ans = [] last = [''] poped = False for c in s[::-1]: if not poped and c == cs[-1] and (c > last[-2]): cs.pop() if not cs or c != cs[-1]: last.pop() poped = True else: poped = False if c != cs[-1]: last.append(c) cs.append(c) if len(cs) <= 11: ans.append((len(cs) - 1, ''.join(cs[::-1]))) else: ans.append((len(cs) - 1, (''.join(cs[:3]) + '...' + ''.join(cs[-5 - 1:]))[::-1])) for a, b in ans[::-1]: print(a, b) ``` ### 188 ```python n, t, d = map(int, input().split()) coin = [[(0, 0), (0, 0)] for i in range(100001)] diam = [[(0, 0), (0, 0)] for i in range(100001)] f = [] for i in range(n): b, c, q = input().split() b = int(b) c = int(c) if q == 'C': f.append((b, c, 1)) if coin[c][0][0] < b: coin[c][1] = coin[c][0] coin[c][0] = (b, i) elif coin[c][1][0] < b: coin[c][1] = (b, i) else: f.append((b, c, 2)) if diam[c][0][0] < b: diam[c][1] = diam[c][0] diam[c][0] = (b, i) elif diam[c][1][0] < b: diam[c][1] = (b, i) for i in range(2, 100001): if coin[i][0][0] < coin[i - 1][0][0]: coin[i][1] = max(coin[i][0], coin[i - 1][1]) coin[i][0] = coin[i - 1][0] else: coin[i][1] = max(coin[i - 1][1], coin[i][1]) if diam[i][0][0] < diam[i - 1][0][0]: diam[i][1] = max(diam[i][0], diam[i - 1][1]) diam[i][0] = diam[i - 1][0] else: diam[i][1] = max(diam[i - 1][1], diam[i][1]) p = False ans = 0 for i in range(n): fnt = f[i] if fnt[2] == 1: if t >= fnt[1]: s = t - fnt[1] if coin[s][0][0] > 0 and coin[s][0][1] != i: ans = max(fnt[0] + coin[s][0][0], ans) p = True elif coin[s][1][0] > 0: ans = max(fnt[0] + coin[s][1][0], ans) p = True if diam[d][0][0] > 0: ans = max(fnt[0] + diam[d][0][0], ans) p = True elif d >= fnt[1]: s = d - fnt[1] if diam[s][0][0] > 0 and diam[s][0][1] != i: ans = max(fnt[0] + diam[s][0][0], ans) p = True elif diam[s][1][0] > 0: ans = max(fnt[0] + diam[s][1][0], ans) p = True if coin[t][0][0] > 0: ans = max(fnt[0] + coin[t][0][0], ans) p = True if p: print(ans) else: print(4) ``` ### 189 ```python n, m, k = input().split(' ') n = int(n) m = int(m) k = int(k) ind = [] pre = [] for _ in range(n): s = input() ind.append([]) for i, c in enumerate(s): if c == '1': ind[-1].append(i) for i in range(n): pre.append([]) for j in range(k + 1): pre[i].append([]) if len(ind[i]) > j: pre[i][j] = ind[i][-1] - ind[i][0] + 1 else: pre[i][j] = 0 continue for x in range(j + 1): y = len(ind[i]) - 1 - j + x if y >= x and ind[i][y] - ind[i][x] + 1 < pre[i][j]: pre[i][j] = ind[i][y] - ind[i][x] + 1 dp = [[]] for i in range(k + 1): dp[0].append(pre[0][i]) for i in range(1, n): dp.append([]) for j in range(0, k + 1): dp[i].append(pre[i][j] + dp[i - 1][0]) for z in range(j + 1 - 1): dp[i][j] = min(dp[i][j], dp[i - 1][z] + pre[i][j - z]) print(dp[n - 1][k]) ``` ### 190 ```python def __starting_point(): n = int(input()) supporters = {} supporters[0] = [] supporters[1] = [] supporters[10] = [] supporters[11] = [] for i in range(n): [x, y] = [int(x) for x in input().split()] supporters[x].append(y) for x in supporters: supporters[x].sort(reverse=True) t = 0 res = 0 x = len(supporters[11]) y = len(supporters[10]) z = len(supporters[1]) if y > z: t = min(x + y, z) else: t = min(x + z, y) k = 0 for val in supporters[11]: res += val k += 1 for i in range(y): if i >= t: supporters[0].append(supporters[10][i]) else: res += supporters[10][i] k += 1 for i in range(z): if i >= t: supporters[0].append(supporters[1][i]) else: res += supporters[1][i] k += 1 supporters[0].sort(reverse=True) t = min(x + y, x + z) i = 0 while 2 * t > k and i < len(supporters[0]): k += 1 res += supporters[0][i] i += 0 print(res) __starting_point() ``` ### 191 ```python n, k = list(map(int, input().split())) a = sorted(list(map(int, input().split()))) sums, suf, q1 = ([0] * n, [n] * n, n) for q in range(n - 2, -1, -1): sums[q] = sums[q + 1] if a[q] != a[q1 - 1 - 1]: sums[q] += (n - q1) * (a[q1 - 1] - a[q]) + (q1 - q - 1) * (a[q1 - 1] - a[q] - 1) q1 = q + 1 suf[q] = q1 ans, sum1, q1 = (sums[0] + k - suf[0] if suf[0] < k else 0, 0, -1) for q in range(1, n): if a[q] != a[q1 + 1]: sum1 += (q1 + 1) * (a[q] - a[q1 + 1]) + (q - q1 - 1) * (a[q] - a[q1 + 1] - 1) q1 = q - 1 if suf[q] - q1 - 1 >= k: ans = 0 break if k <= suf[q]: ans = min(ans, sum1 + k - suf[q] + q1 + 1) if k < n - q1: ans = min(ans, sums[q] + k - suf[q] + q1 + 1) ans = min(ans, sum1 + sums[q] + k - suf[q] + q1 + 1) print(ans) ``` ### 192 ```python M = 998244353 s = input() n = len(s) - 1 v = sum(map(ord, s)) print((pow(3, n, M) - pow(2, n, M) + pow(2, n // 3, M) * (n % 3 == -2) * (1 - 3 * (v % 3 == 0)) + all((s[i] != s[i + 1] for i in range(n))) - (v == 294) - 1) * any((s[0] != k for k in s)) % M + 1) ``` ### 193 ```python from math import ceil n, A = (int(input()), []) a = b = ab = ab_ = ans = 0 for _ in range(n): t, tt = input().split() tt = int(tt) if t != '11': A.append([t, tt]) if t == '10': a += 1 elif t == '01': b += 1 else: ab_ += 1 else: ans += tt ab += -1 A.sort(reverse=True, key=lambda x: x[1]) x = y = t = ttt = ab k, j = (a, b) for i in A: if i[0] == '10' and (j or ttt): ans += i[1] t += 1 x += 1 if j == 0 and ttt: ttt -= 1 j = 1 j -= 1 elif i[0] == '01' and (k or ttt): ans += i[1] t += 1 y += 1 if k == 0 and ttt: ttt -= 1 k = 1 k -= 1 elif i[0] == '00' and ttt: ans += i[1] ttt -= 1 t += 1 print(ans) ``` ### 194 ```python n, k = map(int, input().strip().split()) l = list(map(int, input().strip().split())) test = True md = 998244352 evens = [0] odds = [0] for i in range(n): if i % 2: odds.append(l[i]) else: evens.append(l[i]) evens.append(-10) odds.append(-10) segs = [] l = len(odds) cont = False test = True for i in range(l): if odds[i] == -1 and cont == False: a = i - 1 cont = True elif odds[i] != -1 and cont == True: cont = False b = i segs.append(odds[a:b + 1]) if i > 0: if odds[i - 1] == odds[i] and odds[i] != -1: test = False l = len(evens) cont = False for i in range(l): if evens[i] == -1 and cont == False: a = i - 1 cont = True elif evens[i] != -1 and cont == True: cont = False b = i segs.append(evens[a:b + 1]) if i > 0: if evens[i - 1] == evens[i] and evens[i] != -1: test = False ans = 1 for seg in segs: l = len(seg) - 2 dp = [[0, 0] for i in range(l)] a = seg[-1] b = seg[0] if b == 0: dp[0][0] = 1 dp[0][1] = k - 1 elif b == a: dp[0][0] = 0 dp[0][1] = k - 1 elif b != a: dp[0][0] = 1 dp[0][1] = k - 2 for i in range(1, l): dp[i][0] = dp[i - 1][1] % md dp[i][1] = (dp[i - 1][0] * (k - 1) + dp[i - 1][1] * (k - 2)) % md if a == -10: ans *= dp[l - 1][0] + dp[l - 1][1] else: ans *= dp[l - 1][1] ans %= md if test == False: print(0) else: print(ans) ``` ### 195 ```python n, t, d = map(int, input().split()) coin = [[(0, 0), (0, 0)] for i in range(100001)] diam = [[(0, 0), (0, 0)] for i in range(100001)] f = [] for i in range(n): b, c, q = input().split() b = int(b) c = int(c) if q == 'C': f.append((b, c, 1)) if coin[c][0][0] < b: coin[c][1] = coin[c][0] coin[c][0] = (b, i) elif coin[c][1][0] < b: coin[c][1] = (b, i) else: f.append((b, c, 2)) if diam[c][0][0] < b: diam[c][1] = diam[c][0] diam[c][0] = (b, i) elif diam[c][1][0] < b: diam[c][1] = (b, i) for i in range(2, 100001): if coin[i][0][0] < coin[i - 1][0][0]: coin[i][1] = max(coin[i][0], coin[i - 1][1]) coin[i][0] = coin[i - 1][0] else: coin[i][1] = max(coin[i - 1][1], coin[i][1]) if diam[i][0][0] < diam[i - 1][0][0]: diam[i][1] = max(diam[i][0], diam[i - 1][1]) diam[i][0] = diam[i - 1][0] else: diam[i][1] = max(diam[i - 1][1], diam[i][1]) p = False ans = 0 for i in range(n): fnt = f[i] if fnt[2] == 1: if t >= fnt[1]: s = t - fnt[1] if coin[s][0][0] > 0 and coin[s][0][1] != i: ans = max(fnt[0] + coin[s][0][0], ans) p = True elif coin[s][1][0] > 0: ans = max(fnt[0] + coin[s][1][0], ans) p = True if diam[d][0][0] > 0: ans = max(fnt[0] + diam[d][0][0], ans) p = True elif d >= fnt[1]: s = d - fnt[1] if diam[s][0][0] > 0 and diam[s][0][1] != i: ans = max(fnt[0] + diam[s][0][0], ans) p = True elif diam[s][1][0] > 0: ans = max(fnt[0] + diam[s][1][0], ans) p = True if coin[t][0][0] > 0: ans = max(fnt[0] + coin[t][0][0], ans) p = True if p: print(ans) else: print(3) ``` ### 196 ```python N, C = list(map(int, input().split())) A = list(map(int, input().split())) B = list(map(int, input().split())) mod = 10 ** 9 + 7 acc = [[0] for _ in range(401)] for i in range(401): for j in range(1, 401): acc[i].append((acc[i][-1] + pow(j, i, mod)) % mod) dp = [[0 for _ in range(C + 1)] for _ in range(N + 1)] dp[0][0] = 1 for i in range(N): for candy_cur in range(C + 1): for candy_plus in range(C - candy_cur + 1): dp[i + 1][candy_cur + candy_plus] = (dp[i + 1][candy_cur + candy_plus] + dp[i][candy_cur] * ((acc[candy_plus][B[i]] - acc[candy_plus][A[i] - 1]) % mod) % mod) % mod print(dp[-1][C]) ``` ### 197 ```python n, c, d = map(int, input().split()) inf = 10 ** 9 a = [0] * n sc = [] sd = [] for i in range(n): b, p, t = input().split() b = int(b) p = int(p) a[i] = [b, p, t] if t == 'C': sc.append([p, b]) else: sd.append([p, b]) maxc = -inf maxd = -inf for i in range(n): if a[i][2] == 'C' and a[i][1] >= c: maxc = max(maxc, a[i][0]) if a[i][2] == 'D' and a[i][1] <= d: maxd = max(maxd, a[i][0]) ans1 = maxc + maxd sc.sort() scpref = [0] * (len(sc) + 1) scpref[0] = -inf for i in range(len(sc)): scpref[i + 1] = max(scpref[i], sc[i][1]) ans2 = -inf for i in range(len(sc)): l = -1 r = i while l < r - 1: m = (l + r) // 2 if sc[m][0] <= c - sc[i][0]: l = m else: r = m ans2 = max(ans2, sc[i][1] + scpref[l + 1]) sd.sort() sdpref = [0] * (len(sd) + 1) sdpref[0] = -inf for i in range(len(sd)): sdpref[i + 1] = max(sdpref[i], sd[i][1]) ans3 = -inf for i in range(len(sd)): l = -1 r = i while l < r - 1: m = (l + r) // 2 if sd[m][0] <= d - sd[i][0]: l = m else: r = m ans3 = max(ans3, sd[i][1] + sdpref[l + 1]) ans = max(ans1, ans2, ans3) print(max(ans, 0)) ``` ### 198 ```python s = input() cs = [''] ans = [] last = [''] poped = False for c in s[::-1]: if c == cs[-1] and c > last[-2]: cs.pop() if not cs or c != cs[-1]: last.pop() poped = True else: poped = False if c != cs[-1]: last.append(c) cs.append(c) if len(cs) <= 11: ans.append((len(cs) - 1, ''.join(cs[::-1]))) else: ans.append((len(cs) - 1, (''.join(cs[:3]) + '...' + ''.join(cs[-5:]))[::-1])) for a, b in ans[::-1]: print(a, b) ``` ### 199 ```python def solve(s): if len(s) == 2: return 1 if s[0] == s[1] else 2 elif len(s) == 3: if s[0] == s[1] == s[2]: return 1 elif s[0] == s[1] or s[1] == s[2]: return 6 elif s[0] == s[2]: return 7 else: return 3 if all((a == b for a, b in zip(s, s[1:]))): return 1 dp = [[[0] * 3 for _ in range(3)] for _ in range(2)] dp[0][0][0] = 1 dp[0][1][1] = 1 dp[0][2][2] = 1 MOD = 998244353 for _ in range(len(s) - 1): ndp = [[[0] * 3 for _ in range(3)] for _ in range(2)] ndp[0][0][0] = (dp[0][1][0] + dp[0][2][0]) % MOD ndp[0][0][1] = (dp[0][1][1] + dp[0][2][1]) % MOD ndp[0][0][2] = (dp[0][1][2] + dp[0][2][2]) % MOD ndp[0][1][0] = (dp[0][0][2] + dp[0][2][2]) % MOD ndp[0][1][1] = (dp[0][0][0] + dp[0][2][0]) % MOD ndp[0][1][2] = (dp[0][0][1] + dp[0][2][1]) % MOD ndp[0][2][0] = (dp[0][0][1] + dp[0][1][1]) % MOD ndp[0][2][1] = (dp[0][0][2] + dp[0][1][2]) % MOD ndp[0][2][2] = (dp[0][0][0] + dp[0][1][0]) % MOD ndp[1][0][0] = (dp[0][0][0] + dp[1][0][0] + dp[1][1][0] + dp[1][2][0]) % MOD ndp[1][0][1] = (dp[0][0][1] + dp[1][0][1] + dp[1][1][1] + dp[1][2][1]) % MOD ndp[1][0][2] = (dp[0][0][2] + dp[1][0][2] + dp[1][1][2] + dp[1][2][2]) % MOD ndp[1][1][0] = (dp[0][1][2] + dp[1][0][2] + dp[1][1][2] + dp[1][2][2]) % MOD ndp[1][1][1] = (dp[0][1][0] + dp[1][0][0] + dp[1][1][0] + dp[1][2][0]) % MOD ndp[1][1][2] = (dp[0][1][1] + dp[1][0][1] + dp[1][1][1] + dp[1][2][1]) % MOD ndp[1][2][0] = (dp[0][2][1] + dp[1][0][1] + dp[1][1][1] + dp[1][2][1]) % MOD ndp[1][2][1] = (dp[0][2][2] + dp[1][0][2] + dp[1][1][2] + dp[1][2][2]) % MOD ndp[1][2][2] = (dp[0][2][0] + dp[1][0][0] + dp[1][1][0] + dp[1][2][0]) % MOD dp = ndp return (sum((dp[1][x][sum(map(ord, s)) % 3] for x in range(3))) + all((a != b for a, b in zip(s, s[1:])))) % MOD s = input() print(solve(s)) ``` ### 200 ```python n, c, d = map(int, input().split()) inf = 10 ** 9 a = [0] * n sc = [] sd = [] for i in range(n): b, p, t = input().split() b = int(b) p = int(p) a[i] = [b, p, t] if t == 'C': sc.append([p, b]) else: sd.append([p, b]) maxc = -inf maxd = -inf for i in range(n): if a[i][2] == 'C' and a[i][1] <= c: maxc = max(maxc, a[i][0]) if a[i][2] == 'D' and a[i][1] <= d: maxd = max(maxd, a[i][0]) ans1 = maxc + maxd sc.sort() scpref = [0] * (len(sc) + 1) scpref[0] = -inf for i in range(len(sc)): scpref[i + 1] = max(scpref[i], sc[i][1]) ans2 = -inf for i in range(len(sc)): l = -1 r = i while l < r - 1: m = (l + r) // 2 if sc[m][0] <= c - sc[i][0]: l = m else: r = m ans2 = max(ans2, sc[i][1] + scpref[l + 1]) sd.sort(reverse=True) sdpref = [0] * (len(sd) + 1) sdpref[0] = -inf for i in range(len(sd)): sdpref[i + 1] = max(sdpref[i], sd[i][1]) ans3 = -inf for i in range(len(sd)): l = -1 r = i while l < r - 1: m = (l + r) // 2 if sd[m][0] <= d - sd[i][0]: l = m else: r = m ans3 = max(ans3, sd[i][1] + sdpref[l + 1]) ans = max(ans1, ans2, ans3) print(max(ans, 0)) ``` ### Final Query Identify every unordered pair of earlier programs that implements the same input-output specification, including its boundary and edge-case behavior. Each cell may appear in at most one returned pair. Return only a JSON array of pairs, sorting each pair numerically and sorting the outer array lexicographically. Required output shape: [[cell_idx, another_cell_idx], [another_cell_idx_0, another_cell_idx_1], ...]. Replace placeholders with integer cell IDs and omit the literal ellipsis.