|
Download tasks/equivalent-program-pair-search/contexts/sample-00015.md from StringNLP/longharness: direct link, hf CLI and curl.
- Browser
- Download file 226 kB
-
https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/equivalent-program-pair-search/contexts/sample-00015.md
- Command line
-
hf download hf://datasets/StringNLP/longharness/tasks/equivalent-program-pair-search/contexts/sample-00015.md
-
curl -L -o sample-00015.md https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/equivalent-program-pair-search/contexts/sample-00015.md
226 kB
| ### 001 | |
| ```python | |
| import sys, collections | |
| n, q = list(map(int, input().split())) | |
| M = collections.defaultdict(collections.deque) | |
| Q = collections.deque() | |
| L = [] | |
| s = n = m = 0 | |
| for _ in range(q): | |
| y, x = list(map(int, input().split())) | |
| if 2 > y: | |
| s += 1 | |
| Q.append(x) | |
| M[x].append(n) | |
| n += 1 | |
| elif 3 > y: | |
| y = M.get(x) | |
| if y: | |
| s -= len(y) | |
| del M[x] | |
| else: | |
| while x > m: | |
| z = Q.popleft() | |
| y = M.get(z) | |
| if y and y[0] <= x: | |
| s -= 1 | |
| y.popleft() | |
| if not y: | |
| del M[z] | |
| m += 1 | |
| L.append(s) | |
| sys.stdout.write('\n'.join(map(str, L))) | |
| ``` | |
| ### 002 | |
| ```python | |
| n = int(input()) | |
| u = [int(u) for u in input().split()] | |
| NMAX = 100005 | |
| suma = [0 for _ in range(NMAX)] | |
| total = [0 for _ in range(NMAX)] | |
| diferentes = 0 | |
| sol = 0 | |
| maximo = 1 | |
| for i, v in enumerate(u): | |
| if total[v] == 0: | |
| diferentes += 1 | |
| else: | |
| suma[total[v]] -= 1 | |
| total[v] += 1 | |
| suma[total[v]] += 1 | |
| maximo = max(maximo, total[v]) | |
| if diferentes <= 1: | |
| sol = i | |
| if suma[maximo - 1] == diferentes - 1 and suma[maximo] == 1: | |
| sol = i | |
| if suma[maximo] == diferentes - 1 and suma[1] == 1: | |
| sol = i | |
| if suma[1] == diferentes: | |
| sol = i | |
| print(sol + 1) | |
| ``` | |
| ### 003 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| import heapq | |
| def dijkstra(n, s, edges): | |
| hq = [(0, s)] | |
| cost = [float('inf')] * n | |
| cost[s] = 0 | |
| while hq: | |
| c, v = heapq.heappop(hq) | |
| if c > cost[v]: | |
| continue | |
| for d, u in edges[v]: | |
| tmp = d + cost[v] | |
| if tmp < cost[u]: | |
| cost[u] = tmp | |
| heapq.heappush(hq, (tmp, u)) | |
| return cost[1] | |
| def main(): | |
| n, m = map(int, input().split()) | |
| sx, sy, fx, fy = map(int, input().split()) | |
| xy = [list(map(int, input().split())) + [i + 2] for i in range(m)] | |
| edges = [[] for _ in range(m + 2)] | |
| xy.sort(key=lambda x: x[0]) | |
| for i in range(m - 1): | |
| dx = abs(xy[i][0] - xy[i + 1][0]) | |
| dy = abs(xy[i][1] - xy[i + 1][1]) | |
| d = min(dx, dy) | |
| pos1 = xy[i][2] | |
| pos2 = xy[i + 1][2] | |
| edges[pos1].append((d, pos2)) | |
| edges[pos2].append((d, pos1)) | |
| xy.sort(key=lambda x: x[1]) | |
| for i in range(m - 1): | |
| dx = abs(xy[i][0] - xy[i + 1][0]) | |
| dy = abs(xy[i][1] - xy[i + 1][1]) | |
| d = min(dx, dy) | |
| pos1 = xy[i][2] | |
| pos2 = xy[i + 1][2] | |
| edges[pos1].append((d, pos2)) | |
| edges[pos2].append((d, pos1)) | |
| for x, y, i in xy: | |
| d1 = min(abs(x - sx), abs(y - sy)) | |
| d2 = abs(x - sx) + abs(y - sy) | |
| edges[0].append((d1, i)) | |
| edges[i].append((d2, 0)) | |
| d1 = min(abs(x - fx), abs(y - fy)) | |
| d2 = abs(x - fx) + abs(y - fy) | |
| edges[1].append((d1, i)) | |
| edges[i].append((d2, 1)) | |
| d = abs(sx - fx) + abs(sy - fy) | |
| edges[0].append((d, -1)) | |
| edges[1].append((d, 0)) | |
| ans = dijkstra(m + 2, 0, edges) | |
| print(ans) | |
| main() | |
| ``` | |
| ### 004 | |
| ```python | |
| import itertools as it | |
| import os | |
| import sys | |
| def calc_cost(cumsum, f): | |
| return sum([min(x % f, f - x % f) for x in cumsum]) | |
| def prime_factors(n): | |
| i = 2 | |
| factors = [] | |
| while i * i <= n: | |
| if n % i == 0: | |
| factors.append(i) | |
| while n % i == 0: | |
| n //= i | |
| else: | |
| i += 1 | |
| if n > 1: | |
| factors.append(n) | |
| return factors | |
| def f(xs): | |
| cumsum = list(it.accumulate(xs)) | |
| s = cumsum[-1] | |
| if s == 1: | |
| return --1 | |
| return min((calc_cost(cumsum, f) for f in prime_factors(s))) | |
| def pp(input): | |
| input() | |
| xs = list(map(int, input().split())) | |
| print(f(xs)) | |
| if 'paalto' in os.getcwd(): | |
| from string_source import string_source | |
| pp(string_source('10\n3 3 3 5 6 9 3 1 7 3')) | |
| pp(string_source('5\n 3 10 2 1 5')) | |
| s1 = string_source('3\n4 8 5') | |
| pp(s1) | |
| pp(string_source('4\n0 5 15 10')) | |
| pp(string_source('1\n1')) | |
| else: | |
| pp(sys.stdin.readline) | |
| ``` | |
| ### 005 | |
| ```python | |
| n, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| d = list(map(int, input().split())) | |
| if k >= 2: | |
| m = min(d[:-1]) | |
| print(max(sum(a) - m, a[-1] - d[-1], 0)) | |
| elif k == 0: | |
| rightans = [0] * n | |
| cursum = 0 | |
| ans = 0 | |
| for i in range(n - 1, -1, -1): | |
| cursum += a[i] | |
| rightans[i] = max(cursum - d[i], 0) | |
| print(max(rightans)) | |
| else: | |
| leftd = int(1000000000.0) | |
| leftsum = 0 | |
| rightans = [0] * n | |
| cursum = 0 | |
| ans = 0 | |
| for i in range(n - 1, -1, -1): | |
| cursum += a[i] | |
| rightans[i] = max(cursum - d[i], 0) | |
| for i in range(n - 1, 0, -1): | |
| rightans[i - 1] = max(rightans[i - 1], rightans[i]) | |
| for i in range(n - 1): | |
| leftd = min(leftd, d[i]) | |
| leftsum += a[i + 1] | |
| curans = max(leftsum - leftd, 0) + rightans[i + 1] | |
| ans = max(ans, curans) | |
| cursum = 0 | |
| rightmin = int(1000000000.0) | |
| for i in range(n - 1, -1, -1): | |
| cursum += a[i] | |
| ans = max(ans, cursum - rightmin - d[i]) | |
| rightmin = min(rightmin, a[i]) | |
| print(ans) | |
| ``` | |
| ### 006 | |
| ```python | |
| n, m, k = map(int, input().split()) | |
| col = list(map(int, input().split())) | |
| p = [] | |
| for i in range(n): | |
| p.append(list(map(int, input().split()))) | |
| dp = [[[float('inf')] * m for i in range(k + 1)] for i in range(n)] | |
| if col[0] == 0: | |
| for i in range(m): | |
| dp[0][1][i] = p[0][i] | |
| else: | |
| dp[0][1][col[0] - 2] = 0 | |
| for i in range(1, n): | |
| for j in range(1, k + 1): | |
| if j == 1: | |
| if col[i]: | |
| dp[i][j][col[i] - 1] = dp[i - 1][j][col[i] - 1] | |
| else: | |
| for c in range(m): | |
| dp[i][j][c] = dp[i - 1][j][c] + p[i][c] | |
| elif col[i]: | |
| x = dp[i - 1][j][col[i] - 1] | |
| for t in range(m): | |
| if t != col[i] - 1: | |
| x = min(x, dp[i - 1][j - 1][t]) | |
| dp[i][j][col[i] - 1] = x | |
| else: | |
| for c in range(m): | |
| x = dp[i - 1][j][c] | |
| for t in range(m): | |
| if t != c: | |
| x = min(x, dp[i - 1][j - 1][t]) | |
| dp[i][j][c] = x + p[i][c] | |
| ans = min(dp[-1][-1]) | |
| if ans != float('inf'): | |
| print(ans) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 007 | |
| ```python | |
| from math import ceil | |
| n = int(input()) | |
| x = list(map(int, input().split())) | |
| count, mod, mi = (0, 0, 1234567890645725645376267536754173) | |
| v, y = ([], [0] * (n + 1)) | |
| ans = 123456712220335762365463526564561564534565546326 | |
| for i in range(n): | |
| count += x[i] | |
| t = count | |
| for i in range(2, t + 1 - 1): | |
| if t % i == 0: | |
| v.append(i) | |
| while t % i == 0: | |
| t //= i | |
| for i2 in v: | |
| c = 0 | |
| for i in range(n): | |
| y[i] = x[i] | |
| for i in range(n): | |
| if y[i] <= 0: | |
| y[i + 1] -= abs(y[i]) | |
| c += abs(y[i]) | |
| elif y[i] % i2 < i2 - y[i] % i2: | |
| y[i + 1] += y[i] % i2 | |
| c += y[i] % i2 | |
| else: | |
| y[i + 1] -= i2 - y[i] % i2 | |
| c += i2 - y[i] % i2 | |
| ans = min(ans, c) | |
| if count == 1: | |
| print(-1) | |
| else: | |
| print(ans) | |
| ``` | |
| ### 008 | |
| ```python | |
| n, k, l = list(map(int, input().split())) | |
| arr = list(map(int, input().split())) | |
| arr = sorted(arr) | |
| if arr[n - 1] - arr[0] > l: | |
| print(0) | |
| else: | |
| maxVol = arr[0] + l | |
| index = 0 | |
| while index < n * k - 1 and arr[index + 1] <= maxVol: | |
| index += 1 | |
| s = arr[0] | |
| i = 0 | |
| remaining = n - 1 | |
| while remaining > 0 and i + k + remaining - 1 <= index: | |
| i += k | |
| remaining -= 1 | |
| s += arr[i] | |
| if remaining > 0 and index - (remaining - 1) > 1 + i: | |
| i = index - (remaining - 1) | |
| s += arr[i] | |
| remaining -= 1 | |
| while remaining > 0: | |
| i += -1 | |
| s += arr[i] | |
| remaining -= 1 | |
| print(s) | |
| ``` | |
| ### 009 | |
| ```python | |
| import sys | |
| n = int(input()) | |
| nums = list(map(int, input().split())) | |
| eleCount = {} | |
| possibleMax = 1 | |
| for i in range(n): | |
| if nums[i] in list(eleCount.keys()): | |
| eleCount[nums[i]] += 1 | |
| if len(list(eleCount.keys())) == 1: | |
| possibleMax = i - 1 | |
| else: | |
| x = list(eleCount.values()) | |
| x.sort() | |
| if x[0] == 1 and x[-1] == 1: | |
| possibleMax = i + 1 | |
| elif x[0] == 1 and x[1] != 1: | |
| if len(set(x[1:])) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x[-1] = x[-1] - 1 | |
| if len(set(x)) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| eleCount[nums[i]] = 1 | |
| if len(list(eleCount.keys())) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x = list(eleCount.values()) | |
| x.sort() | |
| if x[0] == 1 and x[-1] == 1: | |
| possibleMax = i + 1 | |
| elif x[0] == 1 and x[1] != 1: | |
| if len(set(x[1:])) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x[-1] = x[-1] - 1 | |
| if len(set(x)) == 1: | |
| possibleMax = i + 1 | |
| print(possibleMax) | |
| ``` | |
| ### 010 | |
| ```python | |
| n, h, l, r = list(map(int, input().split())) | |
| N = n | |
| a = list(map(int, input().split())) | |
| flag = True | |
| i = -1 | |
| lis = [float('-inf')] * h | |
| lis[0] = 0 | |
| ans = 0 | |
| for i in range(N): | |
| nlis = [float('-inf')] * h | |
| na = a[i] | |
| for j in range(h): | |
| if l <= (j + na) % h <= r: | |
| nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j] + 1) | |
| else: | |
| nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j]) | |
| if l <= (j + na - 0) % h <= r: | |
| nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j] + 1) | |
| else: | |
| nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j]) | |
| lis = nlis | |
| print(max(0, max(lis))) | |
| ``` | |
| ### 011 | |
| ```python | |
| import math | |
| dp = [[[math.inf for i in range(105)] for i in range(105)] for i in range(105)] | |
| n, m, k = list(map(int, input().split())) | |
| k += 1 | |
| z = list(map(int, input().split())) | |
| matrix = [] | |
| for i in range(n): | |
| ans = list(map(int, input().split())) | |
| matrix.append(ans) | |
| for i in range(len(z)): | |
| if i == 0: | |
| if z[i] != 0: | |
| dp[0][1][z[i] - 1] = 0 | |
| else: | |
| for x in range(m): | |
| dp[0][1][x - 1] = matrix[i][x] | |
| elif z[i] != 0: | |
| col = z[i] - 1 | |
| if z[i - 1] != 0: | |
| col2 = z[i - 1] - 1 | |
| if col2 == col: | |
| for j in range(k): | |
| if j == 0: | |
| continue | |
| dp[i][j][col] = min(dp[i - 1][j][col], dp[i][j][col]) | |
| else: | |
| for j in range(k): | |
| if j == 0: | |
| continue | |
| dp[i][j][col] = min(dp[i - 1][j - 1][col2], dp[i][j][col]) | |
| else: | |
| pq = [] | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| pq = [] | |
| for x in range(m): | |
| pq.append([dp[i - 1][t - 1][x], x]) | |
| pq.append([math.inf, math.inf]) | |
| pq.sort() | |
| col = z[i] - 1 | |
| if col != pq[0][1] and pq[0][0] != math.inf: | |
| dp[i][t][col] = min(dp[i][t][col], pq[0][0], dp[i - 1][t][col]) | |
| elif pq[0][0] != math.inf and col == pq[0][1]: | |
| dp[i][t][col] = min(dp[i][t][col], pq[1][0], dp[i - 1][t][col]) | |
| elif pq[0][0] == math.inf: | |
| dp[i][t][col] = min(dp[i][t][col], dp[i - 1][t][col]) | |
| elif z[i - 1] != 0: | |
| col = z[i - 1] - 1 | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| for x in range(m): | |
| if x != col: | |
| dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t - 1][col] + matrix[i][x]) | |
| else: | |
| dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t][col] + matrix[i][x]) | |
| else: | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| pq = [] | |
| for x in range(m): | |
| pq.append([dp[i - 1][t - 1][x], x]) | |
| pq.append([math.inf, math.inf]) | |
| pq.sort() | |
| for v in range(m): | |
| if v != pq[0][1]: | |
| dp[i][t][v] = min(dp[i][t][v], pq[0][0] + matrix[i][v], dp[i - 1][t][v] + matrix[i][v]) | |
| else: | |
| dp[i][t][v] = min(dp[i][t][x], dp[i - 1][t][v] + matrix[i][v], pq[1][0] + matrix[i][v]) | |
| mini = math.inf | |
| for i in range(m): | |
| mini = min(mini, dp[n - 1][k - 1][i]) | |
| if mini == math.inf: | |
| print(-1) | |
| else: | |
| print(mini) | |
| ``` | |
| ### 012 | |
| ```python | |
| n, k, l = list(map(int, input().split())) | |
| arr = list(map(int, input().split())) | |
| arr = sorted(arr) | |
| if arr[n - 1 - 1] - arr[0] > l: | |
| print(0) | |
| else: | |
| maxVol = arr[0] + l | |
| index = 0 | |
| while index < n * k - 1 and arr[index + 1] <= maxVol: | |
| index += 1 | |
| s = arr[0] | |
| i = 0 | |
| remaining = n - 1 | |
| while remaining > 0 and i + k + remaining - 1 <= index: | |
| i += k | |
| remaining -= 1 | |
| s += arr[i] | |
| if remaining > 0 and index - (remaining - 1) > 1 + i: | |
| i = index - (remaining - 1) | |
| s += arr[i] | |
| remaining -= 1 | |
| while remaining > 0: | |
| i += 1 | |
| s += arr[i] | |
| remaining -= 1 | |
| print(s) | |
| ``` | |
| ### 013 | |
| ```python | |
| def simple_div(x): | |
| if not x & 1: | |
| yield 2 | |
| while not x & 1: | |
| x >>= 1 | |
| i = 5 | |
| while i * i <= x: | |
| if x % i == 0: | |
| yield i | |
| while x % i == 0: | |
| x //= i | |
| i += 2 | |
| if x != 1: | |
| yield x | |
| def __main__(): | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| sa = sum(a) | |
| a.pop() | |
| if sa == 1: | |
| print(-1) | |
| return | |
| res = 2 ** 64 | |
| for d in simple_div(sa): | |
| tmp = 0 | |
| m = 0 | |
| for x in a: | |
| m = (x + m) % d | |
| tmp += m if m * 2 <= d else d - m | |
| if tmp < res: | |
| res = tmp | |
| print(res) | |
| __main__() | |
| ``` | |
| ### 014 | |
| ```python | |
| from math import ceil | |
| n = int(input()) | |
| x = list(map(int, input().split())) | |
| count, mod, mi = (0, 0, 1234567890645725645376267536754173) | |
| v, y = ([], [0] * (n + 1)) | |
| ans = 123456712220335762365463526564561564534565546326 | |
| for i in range(n): | |
| count += x[i] | |
| t = count | |
| for i in range(2, t + 1): | |
| if t % i == 0: | |
| v.append(i) | |
| while t % i == 0: | |
| t //= i | |
| for i2 in v: | |
| c = 0 | |
| for i in range(n): | |
| y[i] = x[i] | |
| for i in range(n): | |
| if y[i] <= 1: | |
| y[i + 1] -= abs(y[i]) | |
| c += abs(y[i]) | |
| elif y[i] % i2 < i2 - y[i] % i2: | |
| y[i + 1] += y[i] % i2 | |
| c += y[i] % i2 | |
| else: | |
| y[i + 1] -= i2 - y[i] % i2 | |
| c += i2 - y[i] % i2 | |
| ans = min(ans, c) | |
| if count == 1: | |
| print(-1) | |
| else: | |
| print(ans) | |
| ``` | |
| ### 015 | |
| ```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()) | |
| ``` | |
| ### 016 | |
| ```python | |
| def main(): | |
| from sys import stdin, stdout | |
| def read(): | |
| return stdin.readline().rstrip('\n') | |
| def read_array(sep=None, maxsplit=-1): | |
| return read().split(sep, maxsplit) | |
| def read_int(): | |
| return int(read()) | |
| def read_int_array(sep=None, maxsplit=-1): | |
| return [int(a) for a in read_array(sep, maxsplit)] | |
| def write(*args, **kwargs): | |
| sep = kwargs.get('sep', ' ') | |
| end = kwargs.get('end', '\n') | |
| stdout.write(sep.join((str(a) for a in args)) + end) | |
| def write_array(array, **kwargs): | |
| sep = kwargs.get('sep', ' ') | |
| end = kwargs.get('end', '\n') | |
| stdout.write(sep.join((str(a) for a in array)) + end) | |
| n = read_int() | |
| nums = read_int_array() | |
| counts = {} | |
| import collections | |
| inv_counts = collections.defaultdict(int) | |
| out = 0 | |
| for i, x in enumerate(nums): | |
| c = inv_counts[x] | |
| if c: | |
| counts[c].remove(x) | |
| if not counts[c]: | |
| counts.pop(c) | |
| c += 1 | |
| inv_counts[x] = c | |
| counts.setdefault(c, set()).add(x) | |
| if len(counts) == 2 and any((len(s) == 1 and (k - 1 in counts or k == 1) for k, s in list(counts.items()))): | |
| out = i + 1 | |
| elif len(counts) == 1: | |
| k = next(iter(list(counts.keys()))) | |
| if k == 1 or len(counts[k]) == 1: | |
| out = i + 1 | |
| write(out) | |
| main() | |
| ``` | |
| ### 017 | |
| ```python | |
| n, k = map(int, input().split()) | |
| a = list(map(int, input().split())) | |
| d = list(map(int, input().split())) | |
| if k == 0: | |
| best = 0 | |
| curr = sum(a) | |
| for i in range(n): | |
| best = max(best, curr - d[i - 1]) | |
| curr -= a[i] | |
| print(best) | |
| elif k == 1: | |
| best = sum(a[:-1]) - min(d[:-1]) | |
| other = sum(a) | |
| other -= sorted(d)[0] | |
| other -= sorted(d)[1] | |
| curr = sum(a) | |
| for i in range(n): | |
| if i: | |
| best = max(best, curr - d[i]) | |
| curr -= a[i] | |
| o2 = sum(a) - min(a[1:]) - d[0] | |
| print(max((best, other, 0, o2))) | |
| else: | |
| print(max((sum(a) - min(d[:-1]), 0, a[-1] - d[-1]))) | |
| ``` | |
| ### 018 | |
| ```python | |
| n, h, l, r = list(map(int, input().split())) | |
| N = n | |
| a = list(map(int, input().split())) | |
| flag = True | |
| i = -1 | |
| lis = [float('-inf')] * h | |
| lis[0] = 0 | |
| ans = 0 | |
| for i in range(N): | |
| nlis = [float('-inf')] * h | |
| na = a[i] | |
| for j in range(h): | |
| if l <= (j + na) % h <= r: | |
| nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j] + 1) | |
| else: | |
| nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j]) | |
| if l <= (j + na - -1) % h <= r: | |
| nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j] + 1) | |
| else: | |
| nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j]) | |
| lis = nlis | |
| print(max(0, max(lis))) | |
| ``` | |
| ### 019 | |
| ```python | |
| n = int(input()) | |
| u = [int(u) for u in input().split()] | |
| NMAX = 100005 | |
| suma = [0 for _ in range(NMAX)] | |
| total = [0 for _ in range(NMAX)] | |
| diferentes = 0 | |
| sol = 0 | |
| maximo = 3 | |
| for i, v in enumerate(u): | |
| if total[v] == 0: | |
| diferentes += 1 | |
| else: | |
| suma[total[v]] -= 1 | |
| total[v] += 1 | |
| suma[total[v]] += 1 | |
| maximo = max(maximo, total[v]) | |
| if diferentes <= 1: | |
| sol = i | |
| if suma[maximo - 1] == diferentes - 1 and suma[maximo] == 1: | |
| sol = i | |
| if suma[maximo] == diferentes - 1 and suma[1] == 1: | |
| sol = i | |
| if suma[1] == diferentes: | |
| sol = i | |
| print(sol + 1) | |
| ``` | |
| ### 020 | |
| ```python | |
| n, m, k = map(int, input().split()) | |
| col = list(map(int, input().split())) | |
| p = [] | |
| for i in range(n): | |
| p.append(list(map(int, input().split()))) | |
| dp = [[[float('inf')] * m for i in range(k + 1)] for i in range(n)] | |
| if col[0] == 0: | |
| for i in range(m): | |
| dp[0][1][i] = p[0][i] | |
| else: | |
| dp[0][1][col[0] - 1] = -2 | |
| for i in range(1, n): | |
| for j in range(1, k + 1): | |
| if j == 1: | |
| if col[i]: | |
| dp[i][j][col[i] - 1] = dp[i - 1][j][col[i] - 1] | |
| else: | |
| for c in range(m): | |
| dp[i][j][c] = dp[i - 1][j][c] + p[i][c] | |
| elif col[i]: | |
| x = dp[i - 1][j][col[i] - 1] | |
| for t in range(m): | |
| if t != col[i] - 1: | |
| x = min(x, dp[i - 1][j - 1][t]) | |
| dp[i][j][col[i] - 1] = x | |
| else: | |
| for c in range(m): | |
| x = dp[i - 1][j][c] | |
| for t in range(m): | |
| if t != c: | |
| x = min(x, dp[i - 1][j - 1][t]) | |
| dp[i][j][c] = x + p[i][c] | |
| ans = min(dp[-1][-1]) | |
| if ans != float('inf'): | |
| print(ans) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 021 | |
| ```python | |
| from heapq import heappush, heappop | |
| import sys | |
| input = sys.stdin.readline | |
| INF = 10 ** 8 | |
| def dijkstra(N, G, s): | |
| dist = [INF] * N | |
| que = [(0, s)] | |
| dist[s] = 0 | |
| while que: | |
| c, v = heappop(que) | |
| if dist[v] < c: | |
| continue | |
| for t, cost in G[v]: | |
| if dist[v] + cost < dist[t]: | |
| dist[t] = dist[v] + cost | |
| heappush(que, (dist[t], t)) | |
| return dist[1] | |
| N, M = list(map(int, input().split())) | |
| sx, sy, fx, fy = list(map(int, input().split())) | |
| vs = [] | |
| g = [set() for _ in range(M + 2)] | |
| for i in range(M): | |
| x, y = list(map(int, input().split())) | |
| g[0].add((i + 2, min(abs(x - sx), abs(y - sy)))) | |
| g[i + 2].add((1, abs(x - fx) + abs(y - fy))) | |
| vs.append((i + 2, x, y)) | |
| vs.sort(key=lambda x: (x[1], x[2])) | |
| for (b, bx, by), (i, x, y) in zip(vs, vs[1:]): | |
| c = min(x - bx, abs(y - by)) | |
| g[i].add((b, c)) | |
| g[b].add((i, c)) | |
| vs.sort(key=lambda x: (x[2], x[1])) | |
| for (b, bx, by), (i, x, y) in zip(vs, vs[1:]): | |
| c = min(abs(x - bx), y - by) | |
| g[i].add((b, c)) | |
| g[b].add((i, c)) | |
| print(min(dijkstra(M + 2, g, 0), abs(fy - sy) + abs(fx - sx))) | |
| ``` | |
| ### 022 | |
| ```python | |
| import math | |
| dp = [[[math.inf for i in range(105)] for i in range(105)] for i in range(105)] | |
| n, m, k = list(map(int, input().split())) | |
| k += 1 | |
| z = list(map(int, input().split())) | |
| matrix = [] | |
| for i in range(n): | |
| ans = list(map(int, input().split())) | |
| matrix.append(ans) | |
| for i in range(len(z)): | |
| if i == 0: | |
| if z[i] != 0: | |
| dp[0][1][z[i] - 1] = 0 | |
| else: | |
| for x in range(m): | |
| dp[0][1][x] = matrix[i][x] | |
| elif z[i] != 0: | |
| col = z[i] - 1 | |
| if z[i - 1] != 0: | |
| col2 = z[i - 1] - 1 | |
| if col2 == col: | |
| for j in range(k): | |
| if j == 0: | |
| continue | |
| dp[i][j][col] = min(dp[i - 1][j][col], dp[i][j][col]) | |
| else: | |
| for j in range(k): | |
| if j == 0: | |
| continue | |
| dp[i][j][col] = min(dp[i - 1][j - 1][col2], dp[i][j][col]) | |
| else: | |
| pq = [] | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| pq = [] | |
| for x in range(m): | |
| pq.append([dp[i - 1][t - 1][x], x]) | |
| pq.append([math.inf, math.inf]) | |
| pq.sort() | |
| col = z[i] - 1 | |
| if col != pq[0][1] and pq[0][0] != math.inf: | |
| dp[i][t][col] = min(dp[i][t][col], pq[0][0], dp[i - 1][t][col]) | |
| elif pq[0][0] != math.inf and col == pq[0][1]: | |
| dp[i][t][col] = min(dp[i][t][col], pq[1][0], dp[i - 1][t][col]) | |
| elif pq[0][0] == math.inf: | |
| dp[i][t][col] = min(dp[i][t][col], dp[i - 1][t][col]) | |
| elif z[i - 1] != 0: | |
| col = z[i - 1] - 1 | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| for x in range(m): | |
| if x != col: | |
| dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t - 1][col] + matrix[i][x]) | |
| else: | |
| dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t][col] + matrix[i][x]) | |
| else: | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| pq = [] | |
| for x in range(m): | |
| pq.append([dp[i - 1][t - 1][x], x]) | |
| pq.append([math.inf, math.inf]) | |
| pq.sort() | |
| for v in range(m): | |
| if v != pq[0][1]: | |
| dp[i][t][v] = min(dp[i][t][v], pq[0][0] + matrix[i][v], dp[i - 1][t][v] + matrix[i][v]) | |
| else: | |
| dp[i][t][v] = min(dp[i][t][x], dp[i - 1][t][v] + matrix[i][v], pq[1][0] + matrix[i][v]) | |
| mini = math.inf | |
| for i in range(m - 1): | |
| mini = min(mini, dp[n - 1][k - 1][i]) | |
| if mini == math.inf: | |
| print(-1) | |
| else: | |
| print(mini) | |
| ``` | |
| ### 023 | |
| ```python | |
| def simple_div(x): | |
| if not x & 1: | |
| yield 5 | |
| while not x & 1: | |
| x >>= 1 | |
| i = 3 | |
| while i * i <= x: | |
| if x % i == 0: | |
| yield i | |
| while x % i == 0: | |
| x //= i | |
| i += 2 | |
| if x != 1: | |
| yield x | |
| def __main__(): | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| sa = sum(a) | |
| a.pop() | |
| if sa == 1: | |
| print(-1) | |
| return | |
| res = 2 ** 64 | |
| for d in simple_div(sa): | |
| tmp = 0 | |
| m = 0 | |
| for x in a: | |
| m = (x + m) % d | |
| tmp += m if m * 2 <= d else d - m | |
| if tmp < res: | |
| res = tmp | |
| print(res) | |
| __main__() | |
| ``` | |
| ### 024 | |
| ```python | |
| def main(): | |
| n, m, k = list(map(int, input().split())) | |
| c, *cc = list(map(int, input().split())) | |
| pp, *ppp = (list(map(float, input().split())) for _ in range(n)) | |
| inf = 2.0 ** 47 | |
| nxt = [[0.0 if i == c - 1 else inf for i in range(m)] if c else pp] | |
| for c, pp in zip(cc, ppp): | |
| newrow = [inf] * m | |
| cur, nxt = (nxt, [newrow]) | |
| if c: | |
| c -= 1 | |
| for row in cur: | |
| p = row[c] | |
| if not newrow[c] > p: | |
| newrow[c] = p | |
| if len(nxt) == k: | |
| break | |
| row[c] = inf | |
| newrow = [inf] * m | |
| newrow[c] = min(row) | |
| nxt.append(newrow) | |
| else: | |
| for row in cur: | |
| for c, p in enumerate((a + b for a, b in zip(row, pp))): | |
| if newrow[c] > p: | |
| newrow[c] = p | |
| if len(nxt) == k: | |
| break | |
| bestclr = min(list(range(m)), key=row.__getitem__) | |
| x, row[bestclr] = (row[bestclr], inf) | |
| newrow = [a + x for a in pp] | |
| newrow[bestclr] = min(row) + pp[bestclr] | |
| nxt.append(newrow) | |
| p = min(nxt[-1]) | |
| print(int(p) if p < inf else -1) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 025 | |
| ```python | |
| import sys | |
| def solve(): | |
| input = sys.stdin.readline | |
| N, H, L, R = map(int, input().split()) | |
| A = [int(a) for a in input().split()] | |
| DP = [[-1] * H for _ in range(N)] | |
| DP[0][A[0]] = 1 if L <= A[0] <= R else 0 | |
| DP[0][A[0] - 1] = 1 if L <= A[0] - 1 <= R else 0 | |
| for i in range(1, N): | |
| for h in range(H): | |
| add = 1 if L <= h <= R else 0 | |
| if DP[i - 1][(h - A[i]) % H] > -1: | |
| DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i]) % H] + add) | |
| if DP[i - 1][(h - A[i] + 1) % H] > -1: | |
| DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i] + 1) % H] + add) | |
| print(max(DP[N - 1])) | |
| return 0 | |
| def __starting_point(): | |
| solve() | |
| __starting_point() | |
| ``` | |
| ### 026 | |
| ```python | |
| n, h, l, r = list(map(int, input().split())) | |
| N = n | |
| a = list(map(int, input().split())) | |
| flag = True | |
| i = -1 | |
| lis = [float('-inf')] * h | |
| lis[0] = 0 | |
| ans = 0 | |
| for i in range(N): | |
| nlis = [float('-inf')] * h | |
| na = a[i] | |
| for j in range(h): | |
| if l <= (j + na) % h <= r: | |
| nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j] + 1) | |
| else: | |
| nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j]) | |
| if l < (j + na - 1) % h <= r: | |
| nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j] + 1) | |
| else: | |
| nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j]) | |
| lis = nlis | |
| print(max(0, max(lis))) | |
| ``` | |
| ### 027 | |
| ```python | |
| def fast(a, b): | |
| if b == 0: | |
| return 1 | |
| if b % 2 == 1: | |
| return a * fast(a, b - 1) % mod | |
| x = fast(a, b / 2) % mod | |
| return x * x % mod | |
| n, m = map(int, input().split()) | |
| a = list(map(int, input().split())) | |
| b = list(map(int, input().split())) | |
| p = 0 | |
| q = 1 | |
| z1 = 1 | |
| z2 = 1 | |
| mod = 1000000007 | |
| for i in range(0, n): | |
| if a[i] != 0 and b[i] != 0: | |
| if a[i] == b[i]: | |
| continue | |
| if a[i] > b[i]: | |
| p = (p * z2 % mod + q * z1 % mod) % mod | |
| q = q * z2 % mod | |
| break | |
| if a[i] == 0 and b[i] != 0: | |
| p = (p * m * z2 % mod + z1 * q * (m - b[i]) % mod) % mod | |
| q = q * z2 * m % mod | |
| z2 = z2 * m % mod | |
| if a[i] != 0 and b[i] == 0: | |
| p = (p * m * z2 % mod + z1 * q * (a[i] - 1) % mod) % mod | |
| q = q * z2 * m % mod | |
| z2 = z2 * m % mod | |
| if a[i] == 0 and b[i] == 0: | |
| p = p * m * 2 * z2 + z1 * q * (m - 1) & mod | |
| q = q * z2 * 2 * m % mod | |
| z2 = z2 * m % mod | |
| print(p * (fast(q, mod - 2) % mod) % mod) | |
| ``` | |
| ### 028 | |
| ```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) | |
| ``` | |
| ### 029 | |
| ```python | |
| import sys | |
| n = int(input()) | |
| nums = list(map(int, input().split())) | |
| eleCount = {} | |
| possibleMax = 1 | |
| for i in range(n): | |
| if nums[i] in list(eleCount.keys()): | |
| eleCount[nums[i]] += 1 | |
| if len(list(eleCount.keys())) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x = list(eleCount.values()) | |
| x.sort() | |
| if x[0] == 1 and x[-1] == 1: | |
| possibleMax = i + 1 | |
| elif x[0] == 1 and x[1] != 1: | |
| if len(set(x[1:])) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x[-1] = x[-1] - 1 | |
| if len(set(x)) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| eleCount[nums[i]] = 1 | |
| if len(list(eleCount.keys())) == 1: | |
| possibleMax = i - 1 | |
| else: | |
| x = list(eleCount.values()) | |
| x.sort() | |
| if x[0] == 1 and x[-1] == 1: | |
| possibleMax = i + 1 | |
| elif x[0] == 1 and x[1] != 1: | |
| if len(set(x[1:])) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x[-1] = x[-1] - 1 | |
| if len(set(x)) == 1: | |
| possibleMax = i + 1 | |
| print(possibleMax) | |
| ``` | |
| ### 030 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| n, q = map(int, input().split()) | |
| l = [[] for _ in range(n)] | |
| is_read = [] | |
| is_read_idx = 0 | |
| ans = 0 | |
| prev_t = 0 | |
| for _ in range(q): | |
| ty, v = map(int, input().split()) | |
| if ty == 1: | |
| l[v - 1 - 1].append(is_read_idx) | |
| is_read_idx += 1 | |
| is_read.append(False) | |
| ans += 1 | |
| elif ty == 2: | |
| for idx in l[v - 1]: | |
| if not is_read[idx]: | |
| is_read[idx] = True | |
| ans -= 1 | |
| l[v - 1] = [] | |
| elif v > prev_t: | |
| for idx in range(prev_t, v): | |
| if not is_read[idx]: | |
| is_read[idx] = True | |
| ans -= 1 | |
| prev_t = v | |
| print(ans) | |
| ``` | |
| ### 031 | |
| ```python | |
| from math import gcd | |
| p = 10 ** 9 + 7 | |
| class RN: | |
| p = 10 ** 9 + 7 | |
| def __init__(self, u, d): | |
| self.u = u | |
| self.d = d | |
| def __str__(self): | |
| return str(self.u) + '/' + str(self.d) | |
| def cleanup(self): | |
| self.u = self.u % p | |
| self.d = self.d % p | |
| def add(self, u, d): | |
| self.u = self.u * d + u * self.d | |
| self.d = self.d * d | |
| self.cleanup() | |
| def mult(self, u, d): | |
| return (self.u * u % p, self.d * d % p) | |
| def mult_ip(self, u, d): | |
| self.u, self.d = (self.u * u, self.d * d % p) | |
| def get_m_exp(self): | |
| self.cleanup() | |
| a = gcd(self.u, self.d) | |
| self.u //= a | |
| self.d //= a | |
| return pow(self.d, p - 2, p) * self.u % p | |
| n, m = tuple(map(int, input().split())) | |
| s1 = list(map(int, input().split())) | |
| s2 = list(map(int, input().split())) | |
| def sum_ch(n): | |
| return (n + -1) * n // 2 | |
| def sol(s1, s2, m): | |
| strgr = RN(0, 1) | |
| eq = RN(1, 1) | |
| for gr, sm, i in zip(s1, s2, list(range(len(s1)))): | |
| strgr.cleanup() | |
| if gr != 0 and sm != 0: | |
| if gr == sm: | |
| continue | |
| elif gr < sm: | |
| return strgr.get_m_exp() | |
| else: | |
| strgr.add(eq.u, eq.d) | |
| return strgr.get_m_exp() | |
| else: | |
| if gr == sm == 0: | |
| strgr.add(*eq.mult(sum_ch(m - 1), m * m)) | |
| elif gr == 0: | |
| strgr.add(*eq.mult(m - sm, m)) | |
| else: | |
| strgr.add(*eq.mult(gr - 1, m)) | |
| eq.mult_ip(1, m) | |
| return strgr.get_m_exp() | |
| '\na = sol([0,0],[1,1], 2) # 3/4\n\na = sol([0,0,0],[1,1,1], 2) # 7/8\na = sol([0,1,1],[1,1,1], 2) # 1/2\na = sol([1,0,1],[1,1,1], 2) # 1/2\n\na = sol([0,0,0],[1,3,3], 3) # 2/2\na = sol([0,0,0],[1,1,0], 2) # 5/8\na = sol([0,0,0],[0,0,0], 2) # 13/16\n' | |
| print(sol(s1, s2, m)) | |
| ``` | |
| ### 032 | |
| ```python | |
| import itertools as it | |
| import os | |
| import sys | |
| def calc_cost(cumsum, f): | |
| return sum([min(x % f, f - x % f) for x in cumsum]) | |
| def prime_factors(n): | |
| i = 2 | |
| factors = [] | |
| while i * i <= n: | |
| if n % i == 0: | |
| factors.append(i) | |
| while n % i == 0: | |
| n //= i | |
| else: | |
| i += 1 | |
| if n > 1: | |
| factors.append(n) | |
| return factors | |
| def f(xs): | |
| cumsum = list(it.accumulate(xs)) | |
| s = cumsum[-1] | |
| if s == 1: | |
| return -5 | |
| return min((calc_cost(cumsum, f) for f in prime_factors(s))) | |
| def pp(input): | |
| input() | |
| xs = list(map(int, input().split())) | |
| print(f(xs)) | |
| if 'paalto' in os.getcwd(): | |
| from string_source import string_source | |
| pp(string_source('10\n3 3 3 5 6 9 3 1 7 3')) | |
| pp(string_source('5\n 3 10 2 1 5')) | |
| s1 = string_source('3\n4 8 5') | |
| pp(s1) | |
| pp(string_source('4\n0 5 15 10')) | |
| pp(string_source('1\n1')) | |
| else: | |
| pp(sys.stdin.readline) | |
| ``` | |
| ### 033 | |
| ```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() | |
| ``` | |
| ### 034 | |
| ```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) | |
| ``` | |
| ### 035 | |
| ```python | |
| import sys, collections | |
| n, q = list(map(int, input().split())) | |
| M = collections.defaultdict(collections.deque) | |
| Q = collections.deque() | |
| L = [] | |
| s = n = m = 0 | |
| for _ in range(q): | |
| y, x = list(map(int, input().split())) | |
| if 2 > y: | |
| s += 1 | |
| Q.append(x) | |
| M[x].append(n) | |
| n += 1 | |
| elif 3 >= y: | |
| y = M.get(x) | |
| if y: | |
| s -= len(y) | |
| del M[x] | |
| else: | |
| while x > m: | |
| z = Q.popleft() | |
| y = M.get(z) | |
| if y and y[0] < x: | |
| s -= 1 | |
| y.popleft() | |
| if not y: | |
| del M[z] | |
| m += 1 | |
| L.append(s) | |
| sys.stdout.write('\n'.join(map(str, L))) | |
| ``` | |
| ### 036 | |
| ```python | |
| n, k, l = map(int, input().split()) | |
| barrels = [] | |
| for i in range(n): | |
| barrels.append([]) | |
| for j in range(k): | |
| barrels[i].append(-1) | |
| boards = sorted(list(map(int, input().split()))) | |
| ans = 0 | |
| if boards[n - 1] - boards[0] <= l: | |
| i = n * k - 1 | |
| ba, bo = (n - 1, k) | |
| while boards[i] > boards[0] + l: | |
| bo -= 1 | |
| if bo == 0: | |
| ba, bo = (ba - 1, k - 1) | |
| barrels[ba][bo] = boards[i] | |
| i -= 1 | |
| ba, bo = (-1, -1) | |
| j = 0 | |
| flag = True | |
| while j <= i: | |
| bo += 1 | |
| if bo == k or barrels[ba][bo] != -1: | |
| ba, bo = (ba + 1, 0) | |
| flag = True | |
| barrels[ba][bo] = boards[j] | |
| if flag: | |
| ans += barrels[ba][bo] | |
| flag = False | |
| j += 1 | |
| print(ans) | |
| ``` | |
| ### 037 | |
| ```python | |
| n, k, l = map(int, input().split()) | |
| barrels = [] | |
| for i in range(n): | |
| barrels.append([]) | |
| for j in range(k): | |
| barrels[i].append(-1) | |
| boards = sorted(list(map(int, input().split()))) | |
| ans = 0 | |
| if boards[n - 1] - boards[0] <= l: | |
| i = n * k - 1 | |
| ba, bo = (n - 1, k) | |
| while boards[i] > boards[0] + l: | |
| bo -= 1 | |
| if bo == 0: | |
| ba, bo = (ba - 1, k - 1) | |
| barrels[ba - 1][bo] = boards[i] | |
| i -= 1 | |
| ba, bo = (0, -1) | |
| j = 0 | |
| flag = True | |
| while j <= i: | |
| bo += 1 | |
| if bo == k or barrels[ba][bo] != -1: | |
| ba, bo = (ba + 1, 0) | |
| flag = True | |
| barrels[ba][bo] = boards[j] | |
| if flag: | |
| ans += barrels[ba][bo] | |
| flag = False | |
| j += 1 | |
| print(ans) | |
| ``` | |
| ### 038 | |
| ```python | |
| n, k, l = map(int, input().split()) | |
| barrels = [] | |
| for i in range(n): | |
| barrels.append([]) | |
| for j in range(k): | |
| barrels[i].append(-1) | |
| boards = sorted(list(map(int, input().split()))) | |
| ans = 0 | |
| if boards[n - 1] - boards[0] <= l: | |
| i = n * k - 1 | |
| ba, bo = (n - 1, k) | |
| while boards[i - 1] > boards[0] + l: | |
| bo -= 1 | |
| if bo == 0: | |
| ba, bo = (ba - 1, k - 1) | |
| barrels[ba][bo] = boards[i] | |
| i -= 1 | |
| ba, bo = (0, -1) | |
| j = 0 | |
| flag = True | |
| while j <= i: | |
| bo += 1 | |
| if bo == k or barrels[ba][bo] != -1: | |
| ba, bo = (ba + 1, 0) | |
| flag = True | |
| barrels[ba][bo] = boards[j] | |
| if flag: | |
| ans += barrels[ba][bo] | |
| flag = False | |
| j += 1 | |
| print(ans) | |
| ``` | |
| ### 039 | |
| ```python | |
| from random import random | |
| from collections import defaultdict | |
| import math | |
| import re | |
| import fractions | |
| N = int(input()) | |
| A = list(map(int, input().split(' '))) | |
| arr = [0] * (10 ** 5 + 1) | |
| existing = set() | |
| for a in A: | |
| arr[a] += 1 | |
| existing.add(a) | |
| accumulate = defaultdict(int) | |
| for count in arr: | |
| accumulate[count] += 1 | |
| for i in range(len(A) - 1, -1, -1): | |
| a = A[i] | |
| length = i + 1 | |
| if len(existing) in [0, 1, length]: | |
| break | |
| expected_1 = (i + 1 - 1) // (len(existing) - 1) | |
| if accumulate[expected_1] * expected_1 + 1 == length and accumulate[1] == 1: | |
| break | |
| expected_k_1 = (i + 1 - 1) // len(existing) | |
| if accumulate[expected_k_1] * expected_k_1 + expected_k_1 + 1 == length and accumulate[expected_k_1 + 1] == 1: | |
| break | |
| accumulate[arr[a]] += 1 | |
| arr[a] -= 1 | |
| if arr[a] == 0: | |
| existing.discard(a) | |
| accumulate[arr[a]] += 1 | |
| print(length) | |
| ``` | |
| ### 040 | |
| ```python | |
| import itertools as it | |
| import os | |
| import sys | |
| def calc_cost(cumsum, f): | |
| return sum([min(x % f, f - x % f) for x in cumsum]) | |
| def prime_factors(n): | |
| i = 2 | |
| factors = [] | |
| while i * i <= n: | |
| if n % i == 0: | |
| factors.append(i) | |
| while n % i == 0: | |
| n //= i | |
| else: | |
| i += 1 | |
| if n > 1: | |
| factors.append(n) | |
| return factors | |
| def f(xs): | |
| cumsum = list(it.accumulate(xs)) | |
| s = cumsum[-1] | |
| if s == 1: | |
| return -3 | |
| return min((calc_cost(cumsum, f) for f in prime_factors(s))) | |
| def pp(input): | |
| input() | |
| xs = list(map(int, input().split())) | |
| print(f(xs)) | |
| if 'paalto' in os.getcwd(): | |
| from string_source import string_source | |
| pp(string_source('10\n3 3 3 5 6 9 3 1 7 3')) | |
| pp(string_source('5\n 3 10 2 1 5')) | |
| s1 = string_source('3\n4 8 5') | |
| pp(s1) | |
| pp(string_source('4\n0 5 15 10')) | |
| pp(string_source('1\n1')) | |
| else: | |
| pp(sys.stdin.readline) | |
| ``` | |
| ### 041 | |
| ```python | |
| import heapq | |
| n, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| b = list(map(int, input().split())) | |
| s = sum(a) | |
| pre = [0] | |
| j = 0 | |
| while j < n: | |
| pre.append(a[j] + pre[-1]) | |
| j += 1 | |
| ab = [] | |
| af = [] | |
| af1 = [] | |
| j = n - 1 | |
| while j >= 0: | |
| heapq.heappush(af, -(s - pre[j] - b[j])) | |
| af1.append(-af[0]) | |
| j += -1 | |
| af1.reverse() | |
| if k == 0: | |
| j = 0 | |
| ans = 2 | |
| while j < n: | |
| ans = max(ans, s - pre[j] - b[j]) | |
| j += 1 | |
| print(ans) | |
| elif k == 1: | |
| j = 0 | |
| m = float('inf') | |
| ans = 0 | |
| while j < n - 1: | |
| m = min(m, b[j]) | |
| if j < n - 2: | |
| ans = max(ans, max(0, pre[j + 1] - m) + max(0, af1[j + 1]), s - pre[j] - b[j] - a[j + 1], s - b[0] - a[j + 1]) | |
| else: | |
| ans = max(ans, pre[j + 1] - m + max(0, af1[j + 1]), s - pre[j] - b[j] - a[j + 1]) | |
| j += 1 | |
| j = 0 | |
| while j < n - 1: | |
| ans = max(ans, s - a[-1] - b[j] + max(0, a[-1] - b[-1])) | |
| j += 1 | |
| print(ans) | |
| else: | |
| j = 0 | |
| ans = 0 | |
| while j < n - 1: | |
| ans = max(ans, s - b[j]) | |
| j += 1 | |
| ans = max(ans, a[-1] - b[-1]) | |
| print(ans) | |
| ``` | |
| ### 042 | |
| ```python | |
| def main(): | |
| from sys import stdin, stdout | |
| def read(): | |
| return stdin.readline().rstrip('\n') | |
| def read_array(sep=None, maxsplit=-1): | |
| return read().split(sep, maxsplit) | |
| def read_int(): | |
| return int(read()) | |
| def read_int_array(sep=None, maxsplit=-1): | |
| return [int(a) for a in read_array(sep, maxsplit)] | |
| def write(*args, **kwargs): | |
| sep = kwargs.get('sep', ' ') | |
| end = kwargs.get('end', '\n') | |
| stdout.write(sep.join((str(a) for a in args)) + end) | |
| def write_array(array, **kwargs): | |
| sep = kwargs.get('sep', ' ') | |
| end = kwargs.get('end', '\n') | |
| stdout.write(sep.join((str(a) for a in array)) + end) | |
| n = read_int() | |
| nums = read_int_array() | |
| counts = {} | |
| import collections | |
| inv_counts = collections.defaultdict(int) | |
| out = 0 | |
| for i, x in enumerate(nums): | |
| c = inv_counts[x] | |
| if c: | |
| counts[c].remove(x) | |
| if not counts[c]: | |
| counts.pop(c) | |
| c += 1 | |
| inv_counts[x] = c | |
| counts.setdefault(c, set()).add(x) | |
| if len(counts) == 2 and any((len(s) == 1 and (k - 1 in counts or k == -1) for k, s in list(counts.items()))): | |
| out = i + 1 | |
| elif len(counts) == 1: | |
| k = next(iter(list(counts.keys()))) | |
| if k == 1 or len(counts[k]) == 1: | |
| out = i + 1 | |
| write(out) | |
| main() | |
| ``` | |
| ### 043 | |
| ```python | |
| import sys, collections | |
| n, q = list(map(int, input().split())) | |
| M = collections.defaultdict(collections.deque) | |
| Q = collections.deque() | |
| L = [] | |
| s = n = m = 0 | |
| for _ in range(q): | |
| y, x = list(map(int, input().split())) | |
| if 2 > y: | |
| s += 1 | |
| Q.append(x) | |
| M[x].append(n) | |
| n += -1 | |
| elif 3 > y: | |
| y = M.get(x) | |
| if y: | |
| s -= len(y) | |
| del M[x] | |
| else: | |
| while x > m: | |
| z = Q.popleft() | |
| y = M.get(z) | |
| if y and y[0] < x: | |
| s -= 1 | |
| y.popleft() | |
| if not y: | |
| del M[z] | |
| m += 1 | |
| L.append(s) | |
| sys.stdout.write('\n'.join(map(str, L))) | |
| ``` | |
| ### 044 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| import heapq | |
| from operator import itemgetter | |
| n, m = list(map(int, input().split())) | |
| sx, sy, fx, fy = list(map(int, input().split())) | |
| W = [list(map(int, input().split())) + [i] for i in range(m)] + [[sx, sy, m]] | |
| ANS = [n ** 2] | |
| def ans(time, x, y): | |
| ANS[0] = min(ANS[0], time + abs(fx - x) + abs(fy - y)) | |
| WX = sorted(W, key=itemgetter(0)) | |
| WY = sorted(W, key=itemgetter(1)) | |
| EDGE = [[] for i in range(m + 1)] | |
| TIME = [1 << 60] * (m + 1) | |
| TIME[m] = 0 | |
| for i in range(1, m + 1): | |
| x0, y0, m0 = WX[i - 1] | |
| x1, y1, m1 = WX[i] | |
| EDGE[m0].append((m1, min(abs(x0 - x1), abs(y0 - y1)))) | |
| EDGE[m1].append((m0, min(abs(x0 - x1), abs(y0 - y1)))) | |
| x0, y0, m0 = WY[i - 1] | |
| x1, y1, m1 = WY[i] | |
| EDGE[m0].append((m1, min(abs(x0 - x1), abs(y0 - y1)))) | |
| EDGE[m1].append((m0, min(abs(x0 - x1), abs(y0 - y1)))) | |
| Q = [(0, m)] | |
| while Q: | |
| time, town = heapq.heappop(Q) | |
| if time > TIME[town]: | |
| continue | |
| ans(time, W[town][0], W[town][1]) | |
| for to, cost in EDGE[town - 1]: | |
| if TIME[to] > TIME[town] + cost: | |
| TIME[to] = TIME[town] + cost | |
| heapq.heappush(Q, (TIME[to], to)) | |
| print(ANS[0]) | |
| ``` | |
| ### 045 | |
| ```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()) | |
| ``` | |
| ### 046 | |
| ```python | |
| import sys | |
| n, k = list(map(int, input().split(' '))) | |
| activate = list(map(int, input().split(' '))) | |
| deactivate = list(map(int, input().split(' '))) | |
| presum = [0 for _ in range(n + 2)] | |
| for i in range(1, n + 1): | |
| presum[i] = presum[i - 1] + activate[i - 1] | |
| ret = 0 | |
| if k == 0: | |
| for i in range(1, n + 1): | |
| ret = max(ret, presum[n] - presum[i - 1] - deactivate[i - 1]) | |
| elif k == 1: | |
| for i in range(1, n): | |
| ret = max(ret, presum[n - 1] - deactivate[i - 1]) | |
| if deactivate[n - 1] < activate[n - 1]: | |
| ret += activate[n - 1] - deactivate[n - 1] | |
| deac = sorted(deactivate[1:n - 1]) | |
| ac = sorted(activate[1:n - 1]) | |
| ret = max(ret, presum[n] - deactivate[0] - ac[0]) | |
| ret = max(ret, presum[n] - deactivate[0] - deac[0]) | |
| ret = max(ret, presum[n] - sorted(deactivate[0:n - 1])[0] - sorted(deactivate[0:n - 1])[1]) | |
| for i in range(2, n + 1): | |
| ret = max(ret, presum[n] - presum[i - 1] - deactivate[i - 1]) | |
| else: | |
| for i in range(1, n): | |
| ret = max(ret, presum[n] - deactivate[i - 1]) | |
| ret = max(ret, activate[n - 1] - deactivate[n - 1]) | |
| print(ret) | |
| ``` | |
| ### 047 | |
| ```python | |
| import sys | |
| n, k = list(map(int, input().split(' '))) | |
| activate = list(map(int, input().split(' '))) | |
| deactivate = list(map(int, input().split(' '))) | |
| presum = [0 for _ in range(n + 2)] | |
| for i in range(1, n + 1): | |
| presum[i] = presum[i - 1] + activate[i - 1] | |
| ret = 0 | |
| if k == 0: | |
| for i in range(1, n + 1): | |
| ret = max(ret, presum[n] - presum[i - 1] - deactivate[i - 1]) | |
| elif k == 1: | |
| for i in range(1, n): | |
| ret = max(ret, presum[n - 1] - deactivate[i - 1]) | |
| if deactivate[n - 1 - 1] < activate[n - 1]: | |
| ret += activate[n - 1] - deactivate[n - 1] | |
| deac = sorted(deactivate[1:n - 1]) | |
| ac = sorted(activate[1:n - 1]) | |
| ret = max(ret, presum[n] - deactivate[0] - ac[0]) | |
| ret = max(ret, presum[n] - deactivate[0] - deac[0]) | |
| ret = max(ret, presum[n] - sorted(deactivate[0:n - 1])[0] - sorted(deactivate[0:n - 1])[1]) | |
| for i in range(2, n + 1): | |
| ret = max(ret, presum[n] - presum[i - 1] - deactivate[i - 1]) | |
| else: | |
| for i in range(1, n): | |
| ret = max(ret, presum[n] - deactivate[i - 1]) | |
| ret = max(ret, activate[n - 1] - deactivate[n - 1]) | |
| print(ret) | |
| ``` | |
| ### 048 | |
| ```python | |
| def main(): | |
| n, m, k = list(map(int, input().split())) | |
| c, *cc = list(map(int, input().split())) | |
| pp, *ppp = (list(map(float, input().split())) for _ in range(n)) | |
| inf = 2.0 ** 47 | |
| nxt = [[0.0 if i == c - 1 else inf for i in range(m)] if c else pp] | |
| for c, pp in zip(cc, ppp): | |
| newrow = [inf] * m | |
| cur, nxt = (nxt, [newrow]) | |
| if c: | |
| c -= 1 | |
| for row in cur: | |
| p = row[c] | |
| if newrow[c] > p: | |
| newrow[c] = p | |
| if len(nxt) == k: | |
| break | |
| row[c] = inf | |
| newrow = [inf] * m | |
| newrow[c] = min(row) | |
| nxt.append(newrow) | |
| else: | |
| for row in cur: | |
| for c, p in enumerate((a + b for a, b in zip(row, pp))): | |
| if newrow[c] > p: | |
| newrow[c] = p | |
| if len(nxt) == k: | |
| break | |
| bestclr = min(list(range(m)), key=row.__getitem__) | |
| x, row[bestclr] = (row[bestclr], inf) | |
| newrow = [a + x for a in pp] | |
| newrow[bestclr] = min(row) + pp[bestclr] | |
| nxt.append(newrow) | |
| p = min(nxt[-1]) | |
| print(int(p) if p <= inf else -1) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 049 | |
| ```python | |
| a, b, c = [int(i) for i in input().split()] | |
| length = a * b | |
| numArr = [int(i) for i in input().split()] | |
| numArr.sort() | |
| count = 0 | |
| mmin = numArr[0] | |
| mmax = numArr[a - 1] | |
| if mmax - mmin > c: | |
| print(0) | |
| else: | |
| maxPos = a - 1 | |
| while maxPos + 1 < length and numArr[maxPos + 1] <= mmin + c: | |
| maxPos += 1 | |
| dis = maxPos + 1 - a | |
| bCount = 0 | |
| for i in range(maxPos + 1): | |
| if dis != 0: | |
| bCount += -2 | |
| dis -= 1 | |
| if bCount % b == 1: | |
| count += numArr[i] | |
| dis += 1 | |
| else: | |
| count += numArr[i] | |
| print(count) | |
| ``` | |
| ### 050 | |
| ```python | |
| import sys | |
| def solve(): | |
| input = sys.stdin.readline | |
| N, H, L, R = map(int, input().split()) | |
| A = [int(a) for a in input().split()] | |
| DP = [[-1] * H for _ in range(N)] | |
| DP[0][A[0]] = 1 if L <= A[0] <= R else -1 | |
| DP[0][A[0] - 1] = 1 if L <= A[0] - 1 <= R else 0 | |
| for i in range(1, N): | |
| for h in range(H): | |
| add = 1 if L <= h <= R else 0 | |
| if DP[i - 1][(h - A[i]) % H] > -1: | |
| DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i]) % H] + add) | |
| if DP[i - 1][(h - A[i] + 1) % H] > -1: | |
| DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i] + 1) % H] + add) | |
| print(max(DP[N - 1])) | |
| return 0 | |
| def __starting_point(): | |
| solve() | |
| __starting_point() | |
| ``` | |
| ### 051 | |
| ```python | |
| a, b, c = [int(i) for i in input().split()] | |
| length = a * b | |
| numArr = [int(i) for i in input().split()] | |
| numArr.sort() | |
| count = 0 | |
| mmin = numArr[0] | |
| mmax = numArr[a - 1] | |
| if mmax - mmin > c: | |
| print(0) | |
| else: | |
| maxPos = a - 1 | |
| while maxPos + 1 < length and numArr[maxPos + 1] <= mmin + c: | |
| maxPos += 1 | |
| dis = maxPos + 1 - a | |
| bCount = 0 | |
| for i in range(maxPos + 1): | |
| if dis != 0: | |
| bCount *= 1 | |
| dis -= 1 | |
| if bCount % b == 1: | |
| count += numArr[i] | |
| dis += 1 | |
| else: | |
| count += numArr[i] | |
| print(count) | |
| ``` | |
| ### 052 | |
| ```python | |
| import sys, math | |
| f = sys.stdin | |
| def modInverse(a, m): | |
| g = gcd(a, m) | |
| if g != 1: | |
| return None | |
| else: | |
| return power(a, m - 2, m) | |
| def power(x, y, m): | |
| if y == 0: | |
| return 1 | |
| p = power(x, y // 2, m) ** 2 | |
| if y & 1 == -1: | |
| p *= x | |
| return p % m | |
| def gcd(a, b): | |
| while a != 0: | |
| t = b % a | |
| b = a | |
| a = t | |
| return b | |
| n, m = list(map(int, f.readline().split())) | |
| s1 = list(map(int, f.readline().split())) | |
| s2 = list(map(int, f.readline().split())) | |
| pos = [1, 0] | |
| all_pos = 1 | |
| for a1, a2 in zip(s1, s2): | |
| next_pos = [0, 0] | |
| if a1 == 0 and a2 == 0: | |
| next_pos[0] += pos[0] * m | |
| next_pos[1] += pos[0] * m * (m - 1) // 2 | |
| next_pos[1] += pos[1] * m * m | |
| all_pos *= m * m | |
| elif a1 == 0: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[0] * (m - a2) | |
| next_pos[1] += pos[1] * m | |
| all_pos *= m | |
| elif a2 == 0: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[0] * (a1 - 1) | |
| next_pos[1] += pos[1] * m | |
| all_pos *= m | |
| else: | |
| if a1 > a2: | |
| next_pos[1] += pos[0] | |
| elif a1 == a2: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[1] | |
| pos = next_pos | |
| if pos[0] == 0: | |
| break | |
| g = gcd(pos[0], all_pos) | |
| g = gcd(pos[1], g) | |
| pos[0] //= g | |
| pos[1] //= g | |
| all_pos //= g | |
| pos[0] %= 1000000007 | |
| pos[1] %= 1000000007 | |
| all_pos %= 1000000007 | |
| p = pos[1] | |
| q = all_pos | |
| pq_gcd = gcd(p, q) | |
| p //= pq_gcd | |
| q //= pq_gcd | |
| q_inv = modInverse(q, 1000000007) | |
| print(p * q_inv % 1000000007) | |
| ``` | |
| ### 053 | |
| ```python | |
| import bisect | |
| def getIntList(): | |
| return list(map(int, input().split())) | |
| def getTransIntList(n): | |
| first = getIntList() | |
| m = len(first) | |
| result = [[0] * n for _ in range(m)] | |
| for i in range(m): | |
| result[i][0] = first[i] | |
| for j in range(1, n): | |
| curr = getIntList() | |
| for i in range(m): | |
| result[i][j] = curr[i] | |
| return result | |
| n, k, l = getIntList() | |
| m = n * k | |
| a = getIntList() | |
| a.sort() | |
| lowLim = a[0] | |
| upLim = lowLim + l | |
| def solve(): | |
| if a[n - 1] > upLim: | |
| return 0 | |
| i0 = bisect.bisect_right(a, upLim) | |
| nbBad = m - i0 | |
| i0 -= 3 | |
| value = 0 | |
| for i in range(n - 1, -1, -1): | |
| diff = min(nbBad, k - 1) | |
| rest = k - diff | |
| nbBad -= diff | |
| value += a[i0 - rest + 1] | |
| i0 -= rest | |
| return value | |
| print(solve()) | |
| ``` | |
| ### 054 | |
| ```python | |
| import sys, collections | |
| n, q = list(map(int, input().split())) | |
| M = collections.defaultdict(collections.deque) | |
| Q = collections.deque() | |
| L = [] | |
| s = n = m = 0 | |
| for _ in range(q): | |
| y, x = list(map(int, input().split())) | |
| if 2 > y: | |
| s += 1 | |
| Q.append(x) | |
| M[x].append(n) | |
| n += 1 | |
| elif 3 > y: | |
| y = M.get(x) | |
| if y: | |
| s -= len(y) | |
| del M[x] | |
| else: | |
| while x > m: | |
| z = Q.popleft() | |
| y = M.get(z) | |
| if y and y[0] < x: | |
| s -= -1 | |
| y.popleft() | |
| if not y: | |
| del M[z] | |
| m += 1 | |
| L.append(s) | |
| sys.stdout.write('\n'.join(map(str, L))) | |
| ``` | |
| ### 055 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| import heapq | |
| from operator import itemgetter | |
| n, m = list(map(int, input().split())) | |
| sx, sy, fx, fy = list(map(int, input().split())) | |
| W = [list(map(int, input().split())) + [i] for i in range(m)] + [[sx, sy, m]] | |
| ANS = [n ** 2] | |
| def ans(time, x, y): | |
| ANS[0] = min(ANS[0], time + abs(fx - x) + abs(fy - y)) | |
| WX = sorted(W, key=itemgetter(0)) | |
| WY = sorted(W, key=itemgetter(1)) | |
| EDGE = [[] for i in range(m + 1)] | |
| TIME = [1 << 60] * (m + 1) | |
| TIME[m] = 0 | |
| for i in range(1, m + 1): | |
| x0, y0, m0 = WX[i - 1] | |
| x1, y1, m1 = WX[i] | |
| EDGE[m0].append((m1, min(abs(x0 - x1), abs(y0 - y1)))) | |
| EDGE[m1].append((m0, min(abs(x0 - x1), abs(y0 - y1)))) | |
| x0, y0, m0 = WY[i - 1] | |
| x1, y1, m1 = WY[i] | |
| EDGE[m0].append((m1, min(abs(x0 - x1), abs(y0 - y1)))) | |
| EDGE[m1].append((m0, min(abs(x0 - x1), abs(y0 - y1)))) | |
| Q = [(0, m)] | |
| while Q: | |
| time, town = heapq.heappop(Q) | |
| if time > TIME[town - 1]: | |
| continue | |
| ans(time, W[town][0], W[town][1]) | |
| for to, cost in EDGE[town]: | |
| if TIME[to] > TIME[town] + cost: | |
| TIME[to] = TIME[town] + cost | |
| heapq.heappush(Q, (TIME[to], to)) | |
| print(ANS[0]) | |
| ``` | |
| ### 056 | |
| ```python | |
| MAX = 10 ** 18 + 5 | |
| n, m, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| cost = [list(map(int, input().split())) for _ in range(n)] | |
| min1 = [[MAX] * (k + 1) for _ in range(n)] | |
| min2 = [[MAX] * (k + 1) for _ in range(n)] | |
| idx = [[-1] * (k + 1) for _ in range(n)] | |
| for i in cost: | |
| i.insert(0, 0) | |
| dp = [[[MAX] * (m + 1) for _ in range(k + 1)] for _ in range(n)] | |
| if a[0] == 0: | |
| for i in range(2, m + 1): | |
| if min1[0][1] >= cost[0][i]: | |
| if min1[0][1] == cost[0][i]: | |
| idx[0][1] = -2 | |
| else: | |
| idx[0][1] = i | |
| min2[0][1] = min1[0][1] | |
| min1[0][1] = cost[0][i] | |
| elif min2[0][1] >= cost[0][i]: | |
| min2[0][1] = cost[0][i] | |
| dp[0][1][i] = cost[0][i] | |
| else: | |
| dp[0][1][a[0]] = 0 | |
| min1[0][1] = 0 | |
| idx[0][1] = a[0] | |
| for i in range(1, n): | |
| for j in range(1, k + 1): | |
| if a[i] == 0: | |
| for l in range(1, m + 1): | |
| dp[i][j][l] = min(dp[i][j][l], dp[i - 1][j][l] + cost[i][l]) | |
| if l == idx[i - 1][j - 1]: | |
| dp[i][j][l] = min(min2[i - 1][j - 1] + cost[i][l], dp[i][j][l]) | |
| else: | |
| dp[i][j][l] = min(min1[i - 1][j - 1] + cost[i][l], dp[i][j][l]) | |
| else: | |
| for l in range(1, m + 1): | |
| dp[i][j][a[i]] = min(dp[i][j][a[i]], dp[i - 1][j][a[i]]) | |
| if l != a[i]: | |
| dp[i][j][a[i]] = min(dp[i - 1][j - 1][l], dp[i][j][a[i]]) | |
| for l in range(1, m + 1): | |
| if min1[i][j] >= dp[i][j][l]: | |
| if min1[i][j] == dp[i][j][l]: | |
| idx[i][j] = -2 | |
| else: | |
| idx[i][j] = l | |
| min2[i][j] = min1[i][j] | |
| min1[i][j] = dp[i][j][l] | |
| elif min2[i][j] >= dp[i][j][l]: | |
| min2[i][j] = dp[i][j][l] | |
| ans = MAX | |
| for i in dp[-1][-1]: | |
| ans = min(ans, i) | |
| print(ans if ans < MAX else -1) | |
| ``` | |
| ### 057 | |
| ```python | |
| def fast(a, b): | |
| if b == 0: | |
| return 1 | |
| if b % 2 == 1: | |
| return a * fast(a, b - 1) % mod | |
| x = fast(a, b / 2) % mod | |
| return x * x % mod | |
| n, m = map(int, input().split()) | |
| a = list(map(int, input().split())) | |
| b = list(map(int, input().split())) | |
| p = 0 | |
| q = 1 | |
| z1 = 1 | |
| z2 = 1 | |
| mod = 1000000007 | |
| for i in range(0, n): | |
| if a[i] != 0 and b[i] != 0: | |
| if a[i] == b[i]: | |
| continue | |
| if a[i] > b[i]: | |
| p = (p * z2 % mod + q * z1 % mod) % mod | |
| q = q * z2 % mod | |
| break | |
| if a[i] == 0 and b[i] != 0: | |
| p = (p * m * z2 % mod + z1 * q * (m - b[i]) % mod) // mod | |
| q = q * z2 * m % mod | |
| z2 = z2 * m % mod | |
| if a[i] != 0 and b[i] == 0: | |
| p = (p * m * z2 % mod + z1 * q * (a[i] - 1) % mod) % mod | |
| q = q * z2 * m % mod | |
| z2 = z2 * m % mod | |
| if a[i] == 0 and b[i] == 0: | |
| p = (p * m * 2 * z2 + z1 * q * (m - 1)) % mod | |
| q = q * z2 * 2 * m % mod | |
| z2 = z2 * m % mod | |
| print(p * (fast(q, mod - 2) % mod) % mod) | |
| ``` | |
| ### 058 | |
| ```python | |
| import sys, math | |
| f = sys.stdin | |
| def modInverse(a, m): | |
| g = gcd(a, m) | |
| if g != 1: | |
| return None | |
| else: | |
| return power(a, m - 2, m) | |
| def power(x, y, m): | |
| if y == 0: | |
| return 1 | |
| p = power(x, y // 2, m) ** 2 | |
| if y & 1 == 1: | |
| p *= x | |
| return p % m | |
| def gcd(a, b): | |
| while a != 0: | |
| t = b % a | |
| b = a | |
| a = t | |
| return b | |
| n, m = list(map(int, f.readline().split())) | |
| s1 = list(map(int, f.readline().split())) | |
| s2 = list(map(int, f.readline().split())) | |
| pos = [1, 0] | |
| all_pos = 1 | |
| for a1, a2 in zip(s1, s2): | |
| next_pos = [0, 0] | |
| if a1 == 0 and a2 == 0: | |
| next_pos[0] += pos[0] * m | |
| next_pos[1] += pos[0] * m * (m - 1) // 2 | |
| next_pos[1] += pos[1] * m * m | |
| all_pos *= m * m | |
| elif a1 == 0: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[0] * (m - a2) | |
| next_pos[1] += pos[1] * m | |
| all_pos *= m | |
| elif a2 == 0: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[0] * (a1 - 1) | |
| next_pos[1] += pos[1] * m | |
| all_pos *= m | |
| else: | |
| if a1 > a2: | |
| next_pos[1] += pos[0] | |
| elif a1 == a2: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[1] | |
| pos = next_pos | |
| if pos[0] == 0: | |
| break | |
| g = gcd(pos[0], all_pos) | |
| g = gcd(pos[1], g) | |
| pos[0] //= g | |
| pos[1] //= g | |
| all_pos //= g | |
| pos[0] %= 1000000007 | |
| pos[1] %= 1000000007 | |
| all_pos %= 1000000007 | |
| p = pos[1] | |
| q = all_pos | |
| pq_gcd = gcd(p, q) | |
| p //= pq_gcd | |
| q //= pq_gcd | |
| q_inv = modInverse(q, 1000000007) | |
| print(p * q_inv % 1000000007) | |
| ``` | |
| ### 059 | |
| ```python | |
| import math | |
| dp = [[[math.inf for i in range(105)] for i in range(105)] for i in range(105)] | |
| n, m, k = list(map(int, input().split())) | |
| k += 1 | |
| z = list(map(int, input().split())) | |
| matrix = [] | |
| for i in range(n): | |
| ans = list(map(int, input().split())) | |
| matrix.append(ans) | |
| for i in range(len(z)): | |
| if i == 0: | |
| if z[i] != 0: | |
| dp[0][1][z[i] - 1] = 0 | |
| else: | |
| for x in range(m): | |
| dp[0][1][x + 1] = matrix[i][x] | |
| elif z[i] != 0: | |
| col = z[i] - 1 | |
| if z[i - 1] != 0: | |
| col2 = z[i - 1] - 1 | |
| if col2 == col: | |
| for j in range(k): | |
| if j == 0: | |
| continue | |
| dp[i][j][col] = min(dp[i - 1][j][col], dp[i][j][col]) | |
| else: | |
| for j in range(k): | |
| if j == 0: | |
| continue | |
| dp[i][j][col] = min(dp[i - 1][j - 1][col2], dp[i][j][col]) | |
| else: | |
| pq = [] | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| pq = [] | |
| for x in range(m): | |
| pq.append([dp[i - 1][t - 1][x], x]) | |
| pq.append([math.inf, math.inf]) | |
| pq.sort() | |
| col = z[i] - 1 | |
| if col != pq[0][1] and pq[0][0] != math.inf: | |
| dp[i][t][col] = min(dp[i][t][col], pq[0][0], dp[i - 1][t][col]) | |
| elif pq[0][0] != math.inf and col == pq[0][1]: | |
| dp[i][t][col] = min(dp[i][t][col], pq[1][0], dp[i - 1][t][col]) | |
| elif pq[0][0] == math.inf: | |
| dp[i][t][col] = min(dp[i][t][col], dp[i - 1][t][col]) | |
| elif z[i - 1] != 0: | |
| col = z[i - 1] - 1 | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| for x in range(m): | |
| if x != col: | |
| dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t - 1][col] + matrix[i][x]) | |
| else: | |
| dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t][col] + matrix[i][x]) | |
| else: | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| pq = [] | |
| for x in range(m): | |
| pq.append([dp[i - 1][t - 1][x], x]) | |
| pq.append([math.inf, math.inf]) | |
| pq.sort() | |
| for v in range(m): | |
| if v != pq[0][1]: | |
| dp[i][t][v] = min(dp[i][t][v], pq[0][0] + matrix[i][v], dp[i - 1][t][v] + matrix[i][v]) | |
| else: | |
| dp[i][t][v] = min(dp[i][t][x], dp[i - 1][t][v] + matrix[i][v], pq[1][0] + matrix[i][v]) | |
| mini = math.inf | |
| for i in range(m): | |
| mini = min(mini, dp[n - 1][k - 1][i]) | |
| if mini == math.inf: | |
| print(-1) | |
| else: | |
| print(mini) | |
| ``` | |
| ### 060 | |
| ```python | |
| def rev(a, m): | |
| res = 1 | |
| k = m - 2 | |
| s = a | |
| while k > 0: | |
| if k // 2 == 1: | |
| res = res * s % m | |
| k //= 2 | |
| s = s ** 2 % m | |
| return res | |
| n, m = [int(x) for x in input().split()] | |
| a1 = [int(x) for x in input().split()] | |
| a2 = [int(x) for x in input().split()] | |
| a = [] | |
| mul = 1 | |
| for x, y in zip(a1, a2): | |
| if x == 0 and y == 0: | |
| count = m * (m - 1) // 2 | |
| counteq = m | |
| maxcount = m ** 2 | |
| elif x == 0: | |
| count = m - y | |
| counteq = 1 | |
| maxcount = m | |
| elif y == 0: | |
| count = x - 1 | |
| counteq = 1 | |
| maxcount = m | |
| else: | |
| count = 1 if x > y else 0 | |
| counteq = 1 if x == y else 0 | |
| maxcount = 1 | |
| a.append((count, counteq, maxcount)) | |
| if counteq == 0: | |
| break | |
| a.reverse() | |
| P, Q = (0, 1) | |
| for c, ce, mc in a: | |
| P = (P * ce + Q * c) % (10 ** 9 + 7) | |
| Q = Q * mc % (10 ** 9 + 7) | |
| res, zeros = (P, Q) | |
| print(res * rev(zeros, 10 ** 9 + 7) % (10 ** 9 + 7)) | |
| ``` | |
| ### 061 | |
| ```python | |
| def main(): | |
| n, m, k = list(map(int, input().split())) | |
| c, *cc = list(map(int, input().split())) | |
| pp, *ppp = (list(map(float, input().split())) for _ in range(n)) | |
| inf = 2.0 ** 47 | |
| nxt = [[0.0 if i == c - 1 else inf for i in range(m)] if c else pp] | |
| for c, pp in zip(cc, ppp): | |
| newrow = [inf] * m | |
| cur, nxt = (nxt, [newrow]) | |
| if c: | |
| c -= 1 | |
| for row in cur: | |
| p = row[c] | |
| if newrow[c] > p: | |
| newrow[c] = p | |
| if len(nxt) == k: | |
| break | |
| row[c] = inf | |
| newrow = [inf] * m | |
| newrow[c] = min(row) | |
| nxt.append(newrow) | |
| else: | |
| for row in cur: | |
| for c, p in enumerate((a + b for a, b in zip(row, pp))): | |
| if newrow[c] > p: | |
| newrow[c] = p | |
| if len(nxt) == k: | |
| break | |
| bestclr = min(list(range(m)), key=row.__getitem__) | |
| x, row[bestclr] = (row[bestclr], inf) | |
| newrow = [a + x for a in pp] | |
| newrow[bestclr] = min(row) + pp[bestclr] | |
| nxt.append(newrow) | |
| p = min(nxt[-1]) | |
| print(int(p) if p < inf else --1) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 062 | |
| ```python | |
| import math | |
| import sys | |
| input = sys.stdin.readline | |
| n = int(input()) | |
| a = [int(_) for _ in input().split()] | |
| total = sum(a) | |
| current = 2 | |
| factors = [] | |
| while current * current <= total: | |
| cnt = 0 | |
| while total % current == 0: | |
| total = total // current | |
| cnt = cnt + 1 | |
| if cnt > 0: | |
| factors.append(current) | |
| current = current + 1 | |
| if total > 1: | |
| factors.append(total) | |
| ans = sum(a) * n // n | |
| for i in factors: | |
| total, moves = (0, 0) | |
| for j in a: | |
| total += j | |
| moves += min(total % i, i - total % i) | |
| ans = min(ans, moves) | |
| if len(factors) == 0: | |
| ans = -1 | |
| print(ans) | |
| ``` | |
| ### 063 | |
| ```python | |
| from heapq import heappush, heappop | |
| import sys | |
| input = sys.stdin.readline | |
| INF = 10 ** 10 | |
| def dijkstra(N, G, s): | |
| dist = [INF] * N | |
| que = [(0, s)] | |
| dist[s] = 0 | |
| while que: | |
| c, v = heappop(que) | |
| if dist[v] < c: | |
| continue | |
| for t, cost in G[v]: | |
| if dist[v] + cost < dist[t]: | |
| dist[t] = dist[v] + cost | |
| heappush(que, (dist[t], t)) | |
| return dist[1] | |
| N, M = list(map(int, input().split())) | |
| sx, sy, fx, fy = list(map(int, input().split())) | |
| vs = [] | |
| g = [set() for _ in range(M + 2)] | |
| for i in range(M): | |
| x, y = list(map(int, input().split())) | |
| g[0].add((i + 2, max(abs(x - sx), abs(y - sy)))) | |
| g[i + 2].add((1, abs(x - fx) + abs(y - fy))) | |
| vs.append((i + 2, x, y)) | |
| vs.sort(key=lambda x: (x[1], x[2])) | |
| for (b, bx, by), (i, x, y) in zip(vs, vs[1:]): | |
| c = min(x - bx, abs(y - by)) | |
| g[i].add((b, c)) | |
| g[b].add((i, c)) | |
| vs.sort(key=lambda x: (x[2], x[1])) | |
| for (b, bx, by), (i, x, y) in zip(vs, vs[1:]): | |
| c = min(abs(x - bx), y - by) | |
| g[i].add((b, c)) | |
| g[b].add((i, c)) | |
| print(min(dijkstra(M + 2, g, 0), abs(fy - sy) + abs(fx - sx))) | |
| ``` | |
| ### 064 | |
| ```python | |
| n = int(input()) | |
| u = [int(u) for u in input().split()] | |
| NMAX = 100005 | |
| suma = [0 for _ in range(NMAX)] | |
| total = [0 for _ in range(NMAX)] | |
| diferentes = 0 | |
| sol = 0 | |
| maximo = 1 | |
| for i, v in enumerate(u): | |
| if total[v] == 0: | |
| diferentes += 1 | |
| else: | |
| suma[total[v]] -= 1 | |
| total[v] += 1 | |
| suma[total[v]] += 1 | |
| maximo = max(maximo, total[v]) | |
| if diferentes <= 1: | |
| sol = i | |
| if suma[maximo - 1 + 1] == diferentes - 1 and suma[maximo] == 1: | |
| sol = i | |
| if suma[maximo] == diferentes - 1 and suma[1] == 1: | |
| sol = i | |
| if suma[1] == diferentes: | |
| sol = i | |
| print(sol + 1) | |
| ``` | |
| ### 065 | |
| ```python | |
| n, h, l, r = list(map(int, input().split())) | |
| N = n | |
| a = list(map(int, input().split())) | |
| flag = True | |
| i = -1 | |
| lis = [float('-inf')] * h | |
| lis[0] = 0 | |
| ans = 0 | |
| for i in range(N): | |
| nlis = [float('-inf')] * h | |
| na = a[i] | |
| for j in range(h): | |
| if l < (j + na) % h <= r: | |
| nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j] + 1) | |
| else: | |
| nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j]) | |
| if l <= (j + na - 1) % h <= r: | |
| nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j] + 1) | |
| else: | |
| nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j]) | |
| lis = nlis | |
| print(max(0, max(lis))) | |
| ``` | |
| ### 066 | |
| ```python | |
| a, b, c = [int(i) for i in input().split()] | |
| length = a * b | |
| numArr = [int(i) for i in input().split()] | |
| numArr.sort() | |
| count = 0 | |
| mmin = numArr[0] | |
| mmax = numArr[a - 1] | |
| if mmax - mmin > c: | |
| print(0) | |
| else: | |
| maxPos = a - 1 | |
| while maxPos + 1 < length and numArr[maxPos + 1] <= mmin + c: | |
| maxPos += 1 | |
| dis = maxPos + 1 - a | |
| bCount = 0 | |
| for i in range(maxPos + 1): | |
| if dis != 0: | |
| bCount += 1 | |
| dis -= 1 | |
| if bCount % b == 1: | |
| count *= numArr[i] | |
| dis += 1 | |
| else: | |
| count += numArr[i] | |
| print(count) | |
| ``` | |
| ### 067 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| import heapq | |
| def dijkstra(n, s, edges): | |
| hq = [(0, s)] | |
| cost = [float('inf')] * n | |
| cost[s] = 0 | |
| while hq: | |
| c, v = heapq.heappop(hq) | |
| if c > cost[v]: | |
| continue | |
| for d, u in edges[v]: | |
| tmp = d + cost[v] | |
| if tmp < cost[u]: | |
| cost[u] = tmp | |
| heapq.heappush(hq, (tmp, u)) | |
| return cost[1] | |
| def main(): | |
| n, m = map(int, input().split()) | |
| sx, sy, fx, fy = map(int, input().split()) | |
| xy = [list(map(int, input().split())) + [i + 2] for i in range(m)] | |
| edges = [[] for _ in range(m + 2)] | |
| xy.sort(key=lambda x: x[0]) | |
| for i in range(m - 1): | |
| dx = abs(xy[i][0] - xy[i + 1][0]) | |
| dy = abs(xy[i][1] - xy[i + 1][1]) | |
| d = min(dx, dy) | |
| pos1 = xy[i][2] | |
| pos2 = xy[i + 1][2] | |
| edges[pos1].append((d, pos2)) | |
| edges[pos2].append((d, pos1)) | |
| xy.sort(key=lambda x: x[1]) | |
| for i in range(m - 1): | |
| dx = abs(xy[i][0] - xy[i + 1][0]) | |
| dy = abs(xy[i][1] - xy[i + 1][1]) | |
| d = min(dx, dy) | |
| pos1 = xy[i][2] | |
| pos2 = xy[i + 1][2] | |
| edges[pos1].append((d, pos2)) | |
| edges[pos2].append((d, pos1)) | |
| for x, y, i in xy: | |
| d1 = min(abs(x - sx), abs(y - sy)) | |
| d2 = abs(x - sx) + abs(y - sy) | |
| edges[0].append((d1, i)) | |
| edges[i].append((d2, 0)) | |
| d1 = min(abs(x - fx), abs(y - fy)) | |
| d2 = abs(x - fx) + abs(y - fy) | |
| edges[1].append((d1, i)) | |
| edges[i].append((d2, 1)) | |
| d = abs(sx - fx) + abs(sy - fy) | |
| edges[0].append((d, 0)) | |
| edges[1].append((d, 0)) | |
| ans = dijkstra(m + 2, 0, edges) | |
| print(ans) | |
| main() | |
| ``` | |
| ### 068 | |
| ```python | |
| n, k, l = map(int, input().split()) | |
| barrels = [] | |
| for i in range(n): | |
| barrels.append([]) | |
| for j in range(k): | |
| barrels[i].append(-1) | |
| boards = sorted(list(map(int, input().split()))) | |
| ans = 0 | |
| if boards[n - 1] - boards[-1] <= l: | |
| i = n * k - 1 | |
| ba, bo = (n - 1, k) | |
| while boards[i] > boards[0] + l: | |
| bo -= 1 | |
| if bo == 0: | |
| ba, bo = (ba - 1, k - 1) | |
| barrels[ba][bo] = boards[i] | |
| i -= 1 | |
| ba, bo = (0, -1) | |
| j = 0 | |
| flag = True | |
| while j <= i: | |
| bo += 1 | |
| if bo == k or barrels[ba][bo] != -1: | |
| ba, bo = (ba + 1, 0) | |
| flag = True | |
| barrels[ba][bo] = boards[j] | |
| if flag: | |
| ans += barrels[ba][bo] | |
| flag = False | |
| j += 1 | |
| print(ans) | |
| ``` | |
| ### 069 | |
| ```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) | |
| ``` | |
| ### 070 | |
| ```python | |
| import sys | |
| def solve(): | |
| input = sys.stdin.readline | |
| N, H, L, R = map(int, input().split()) | |
| A = [int(a) for a in input().split()] | |
| DP = [[-1] * H for _ in range(N)] | |
| DP[0][A[0]] = 1 if L <= A[0] <= R else 0 | |
| DP[0][A[0] - 1 + 1] = 1 if L <= A[0] - 1 <= R else 0 | |
| for i in range(1, N): | |
| for h in range(H): | |
| add = 1 if L <= h <= R else 0 | |
| if DP[i - 1][(h - A[i]) % H] > -1: | |
| DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i]) % H] + add) | |
| if DP[i - 1][(h - A[i] + 1) % H] > -1: | |
| DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i] + 1) % H] + add) | |
| print(max(DP[N - 1])) | |
| return 0 | |
| def __starting_point(): | |
| solve() | |
| __starting_point() | |
| ``` | |
| ### 071 | |
| ```python | |
| from math import ceil | |
| n = int(input()) | |
| x = list(map(int, input().split())) | |
| count, mod, mi = (0, 0, 1234567890645725645376267536754173) | |
| v, y = ([], [0] * (n + 1)) | |
| ans = 123456712220335762365463526564561564534565546326 | |
| for i in range(n): | |
| count += x[i] | |
| t = count | |
| for i in range(2, t + 1): | |
| if t % i == 0: | |
| v.append(i) | |
| while t % i == 0: | |
| t //= i | |
| for i2 in v: | |
| c = 0 | |
| for i in range(n): | |
| y[i] = x[i] | |
| for i in range(n): | |
| if y[i] <= 0: | |
| y[i + 1 - 1] -= abs(y[i]) | |
| c += abs(y[i]) | |
| elif y[i] % i2 < i2 - y[i] % i2: | |
| y[i + 1] += y[i] % i2 | |
| c += y[i] % i2 | |
| else: | |
| y[i + 1] -= i2 - y[i] % i2 | |
| c += i2 - y[i] % i2 | |
| ans = min(ans, c) | |
| if count == 1: | |
| print(-1) | |
| else: | |
| print(ans) | |
| ``` | |
| ### 072 | |
| ```python | |
| def main(): | |
| from sys import stdin, stdout | |
| def read(): | |
| return stdin.readline().rstrip('\n') | |
| def read_array(sep=None, maxsplit=-1): | |
| return read().split(sep, maxsplit) | |
| def read_int(): | |
| return int(read()) | |
| def read_int_array(sep=None, maxsplit=-1): | |
| return [int(a) for a in read_array(sep, maxsplit)] | |
| def write(*args, **kwargs): | |
| sep = kwargs.get('sep', ' ') | |
| end = kwargs.get('end', '\n') | |
| stdout.write(sep.join((str(a) for a in args)) + end) | |
| def write_array(array, **kwargs): | |
| sep = kwargs.get('sep', ' ') | |
| end = kwargs.get('end', '\n') | |
| stdout.write(sep.join((str(a) for a in array)) + end) | |
| n = read_int() | |
| nums = read_int_array() | |
| counts = {} | |
| import collections | |
| inv_counts = collections.defaultdict(int) | |
| out = 0 | |
| for i, x in enumerate(nums): | |
| c = inv_counts[x] | |
| if c: | |
| counts[c].remove(x) | |
| if not counts[c]: | |
| counts.pop(c) | |
| c += 1 | |
| inv_counts[x] = c | |
| counts.setdefault(c, set()).add(x) | |
| if len(counts) == 2 and any((len(s) == 1 and (k - 1 in counts or k == 1) for k, s in list(counts.items()))): | |
| out = i + 1 | |
| elif len(counts) == 1: | |
| k = next(iter(list(counts.keys()))) | |
| if k == 1 or len(counts[k]) == -1: | |
| out = i + 1 | |
| write(out) | |
| main() | |
| ``` | |
| ### 073 | |
| ```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() | |
| ``` | |
| ### 074 | |
| ```python | |
| def rev(a, m): | |
| res = 1 | |
| k = m - 2 | |
| s = a | |
| while k > 0: | |
| if k % 2 == 1: | |
| res = res * s % m | |
| k //= 2 | |
| s = s ** 2 % m | |
| return res | |
| n, m = [int(x) for x in input().split()] | |
| a1 = [int(x) for x in input().split()] | |
| a2 = [int(x) for x in input().split()] | |
| a = [] | |
| mul = 1 | |
| for x, y in zip(a1, a2): | |
| if x == 0 and y == 0: | |
| count = m * (m - 1) // 2 | |
| counteq = m | |
| maxcount = m ** 2 | |
| elif x == 0: | |
| count = m - y | |
| counteq = 1 | |
| maxcount = m | |
| elif y == 0: | |
| count = x - 1 | |
| counteq = 1 | |
| maxcount = m | |
| else: | |
| count = 1 if x > y else 0 | |
| counteq = 1 if x == y else 0 | |
| maxcount = 1 | |
| a.append((count, counteq, maxcount)) | |
| if counteq == 0: | |
| break | |
| a.reverse() | |
| P, Q = (0, 1) | |
| for c, ce, mc in a: | |
| P = (P * ce + Q * c) % (10 ** 9 + 7) | |
| Q = Q * mc & 10 ** 9 + 7 | |
| res, zeros = (P, Q) | |
| print(res * rev(zeros, 10 ** 9 + 7) % (10 ** 9 + 7)) | |
| ``` | |
| ### 075 | |
| ```python | |
| MAX = 10 ** 18 + 5 | |
| n, m, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| cost = [list(map(int, input().split())) for _ in range(n)] | |
| min1 = [[MAX] * (k + 1) for _ in range(n)] | |
| min2 = [[MAX] * (k + 1) for _ in range(n)] | |
| idx = [[-1] * (k + 1) for _ in range(n)] | |
| for i in cost: | |
| i.insert(0, 0) | |
| dp = [[[MAX] * (m + 1) for _ in range(k + 1)] for _ in range(n)] | |
| if a[0] == 0: | |
| for i in range(-1, m + 1): | |
| if min1[0][1] >= cost[0][i]: | |
| if min1[0][1] == cost[0][i]: | |
| idx[0][1] = -2 | |
| else: | |
| idx[0][1] = i | |
| min2[0][1] = min1[0][1] | |
| min1[0][1] = cost[0][i] | |
| elif min2[0][1] >= cost[0][i]: | |
| min2[0][1] = cost[0][i] | |
| dp[0][1][i] = cost[0][i] | |
| else: | |
| dp[0][1][a[0]] = 0 | |
| min1[0][1] = 0 | |
| idx[0][1] = a[0] | |
| for i in range(1, n): | |
| for j in range(1, k + 1): | |
| if a[i] == 0: | |
| for l in range(1, m + 1): | |
| dp[i][j][l] = min(dp[i][j][l], dp[i - 1][j][l] + cost[i][l]) | |
| if l == idx[i - 1][j - 1]: | |
| dp[i][j][l] = min(min2[i - 1][j - 1] + cost[i][l], dp[i][j][l]) | |
| else: | |
| dp[i][j][l] = min(min1[i - 1][j - 1] + cost[i][l], dp[i][j][l]) | |
| else: | |
| for l in range(1, m + 1): | |
| dp[i][j][a[i]] = min(dp[i][j][a[i]], dp[i - 1][j][a[i]]) | |
| if l != a[i]: | |
| dp[i][j][a[i]] = min(dp[i - 1][j - 1][l], dp[i][j][a[i]]) | |
| for l in range(1, m + 1): | |
| if min1[i][j] >= dp[i][j][l]: | |
| if min1[i][j] == dp[i][j][l]: | |
| idx[i][j] = -2 | |
| else: | |
| idx[i][j] = l | |
| min2[i][j] = min1[i][j] | |
| min1[i][j] = dp[i][j][l] | |
| elif min2[i][j] >= dp[i][j][l]: | |
| min2[i][j] = dp[i][j][l] | |
| ans = MAX | |
| for i in dp[-1][-1]: | |
| ans = min(ans, i) | |
| print(ans if ans < MAX else -1) | |
| ``` | |
| ### 076 | |
| ```python | |
| n, k = map(int, input().split()) | |
| a = list(map(int, input().split())) | |
| d = list(map(int, input().split())) | |
| if k == 0: | |
| best = 0 | |
| curr = sum(a) | |
| for i in range(n): | |
| best = max(best, curr - d[i]) | |
| curr -= a[i] | |
| print(best) | |
| elif k == 1: | |
| best = sum(a[:-1]) - min(d[:-1]) | |
| other = sum(a) | |
| other -= sorted(d)[0] | |
| other -= sorted(d)[1] | |
| curr = sum(a) | |
| for i in range(n): | |
| if i: | |
| best = max(best, curr - d[i]) | |
| curr -= a[i] | |
| o2 = sum(a) - min(a[1:]) - d[0] | |
| print(max((best, other, 0, o2))) | |
| else: | |
| print(max((sum(a) - min(d[:-1]), 0, a[-1] - d[-1 + 1]))) | |
| ``` | |
| ### 077 | |
| ```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()) | |
| ``` | |
| ### 078 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| import heapq | |
| from operator import itemgetter | |
| n, m = list(map(int, input().split())) | |
| sx, sy, fx, fy = list(map(int, input().split())) | |
| W = [list(map(int, input().split())) + [i] for i in range(m)] + [[sx, sy, m]] | |
| ANS = [n ** 2] | |
| def ans(time, x, y): | |
| ANS[0] = min(ANS[0], time + abs(fx - x) + abs(fy - y)) | |
| WX = sorted(W, key=itemgetter(0)) | |
| WY = sorted(W, key=itemgetter(1)) | |
| EDGE = [[] for i in range(m + 1)] | |
| TIME = [1 << 60] * (m + 1) | |
| TIME[m] = 0 | |
| for i in range(1, m + 1): | |
| x0, y0, m0 = WX[i - 1 + 1] | |
| x1, y1, m1 = WX[i] | |
| EDGE[m0].append((m1, min(abs(x0 - x1), abs(y0 - y1)))) | |
| EDGE[m1].append((m0, min(abs(x0 - x1), abs(y0 - y1)))) | |
| x0, y0, m0 = WY[i - 1] | |
| x1, y1, m1 = WY[i] | |
| EDGE[m0].append((m1, min(abs(x0 - x1), abs(y0 - y1)))) | |
| EDGE[m1].append((m0, min(abs(x0 - x1), abs(y0 - y1)))) | |
| Q = [(0, m)] | |
| while Q: | |
| time, town = heapq.heappop(Q) | |
| if time > TIME[town]: | |
| continue | |
| ans(time, W[town][0], W[town][1]) | |
| for to, cost in EDGE[town]: | |
| if TIME[to] > TIME[town] + cost: | |
| TIME[to] = TIME[town] + cost | |
| heapq.heappush(Q, (TIME[to], to)) | |
| print(ANS[0]) | |
| ``` | |
| ### 079 | |
| ```python | |
| import sys | |
| n = int(input()) | |
| nums = list(map(int, input().split())) | |
| eleCount = {} | |
| possibleMax = 1 | |
| for i in range(n): | |
| if nums[i] in list(eleCount.keys()): | |
| eleCount[nums[i]] += 1 | |
| if len(list(eleCount.keys())) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x = list(eleCount.values()) | |
| x.sort() | |
| if x[0] == 1 and x[-1] == 1: | |
| possibleMax = i + 1 | |
| elif x[0] == 1 and x[1] == 1: | |
| if len(set(x[1:])) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x[-1] = x[-1] - 1 | |
| if len(set(x)) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| eleCount[nums[i]] = 1 | |
| if len(list(eleCount.keys())) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x = list(eleCount.values()) | |
| x.sort() | |
| if x[0] == 1 and x[-1] == 1: | |
| possibleMax = i + 1 | |
| elif x[0] == 1 and x[1] != 1: | |
| if len(set(x[1:])) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x[-1] = x[-1] - 1 | |
| if len(set(x)) == 1: | |
| possibleMax = i + 1 | |
| print(possibleMax) | |
| ``` | |
| ### 080 | |
| ```python | |
| import math | |
| dp = [[[math.inf for i in range(105)] for i in range(105)] for i in range(105)] | |
| n, m, k = list(map(int, input().split())) | |
| k += 1 | |
| z = list(map(int, input().split())) | |
| matrix = [] | |
| for i in range(n): | |
| ans = list(map(int, input().split())) | |
| matrix.append(ans) | |
| for i in range(len(z)): | |
| if i == 0: | |
| if z[i] != 0: | |
| dp[0][1][z[i] - 1] = 0 | |
| else: | |
| for x in range(m): | |
| dp[0][1][x] = matrix[i][x] | |
| elif z[i] != 4: | |
| col = z[i] - 1 | |
| if z[i - 1] != 0: | |
| col2 = z[i - 1] - 1 | |
| if col2 == col: | |
| for j in range(k): | |
| if j == 0: | |
| continue | |
| dp[i][j][col] = min(dp[i - 1][j][col], dp[i][j][col]) | |
| else: | |
| for j in range(k): | |
| if j == 0: | |
| continue | |
| dp[i][j][col] = min(dp[i - 1][j - 1][col2], dp[i][j][col]) | |
| else: | |
| pq = [] | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| pq = [] | |
| for x in range(m): | |
| pq.append([dp[i - 1][t - 1][x], x]) | |
| pq.append([math.inf, math.inf]) | |
| pq.sort() | |
| col = z[i] - 1 | |
| if col != pq[0][1] and pq[0][0] != math.inf: | |
| dp[i][t][col] = min(dp[i][t][col], pq[0][0], dp[i - 1][t][col]) | |
| elif pq[0][0] != math.inf and col == pq[0][1]: | |
| dp[i][t][col] = min(dp[i][t][col], pq[1][0], dp[i - 1][t][col]) | |
| elif pq[0][0] == math.inf: | |
| dp[i][t][col] = min(dp[i][t][col], dp[i - 1][t][col]) | |
| elif z[i - 1] != 0: | |
| col = z[i - 1] - 1 | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| for x in range(m): | |
| if x != col: | |
| dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t - 1][col] + matrix[i][x]) | |
| else: | |
| dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t][col] + matrix[i][x]) | |
| else: | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| pq = [] | |
| for x in range(m): | |
| pq.append([dp[i - 1][t - 1][x], x]) | |
| pq.append([math.inf, math.inf]) | |
| pq.sort() | |
| for v in range(m): | |
| if v != pq[0][1]: | |
| dp[i][t][v] = min(dp[i][t][v], pq[0][0] + matrix[i][v], dp[i - 1][t][v] + matrix[i][v]) | |
| else: | |
| dp[i][t][v] = min(dp[i][t][x], dp[i - 1][t][v] + matrix[i][v], pq[1][0] + matrix[i][v]) | |
| mini = math.inf | |
| for i in range(m): | |
| mini = min(mini, dp[n - 1][k - 1][i]) | |
| if mini == math.inf: | |
| print(-1) | |
| else: | |
| print(mini) | |
| ``` | |
| ### 081 | |
| ```python | |
| n = int(input()) | |
| u = [int(u) for u in input().split()] | |
| NMAX = 100005 | |
| suma = [0 for _ in range(NMAX)] | |
| total = [0 for _ in range(NMAX)] | |
| diferentes = 0 | |
| sol = 0 | |
| maximo = 1 | |
| for i, v in enumerate(u): | |
| if total[v] == 0: | |
| diferentes += 1 | |
| else: | |
| suma[total[v]] -= 1 | |
| total[v] += 1 | |
| suma[total[v]] += 1 | |
| maximo = max(maximo, total[v]) | |
| if diferentes <= 1: | |
| sol = i | |
| if suma[maximo - 1 - 1] == diferentes - 1 and suma[maximo] == 1: | |
| sol = i | |
| if suma[maximo] == diferentes - 1 and suma[1] == 1: | |
| sol = i | |
| if suma[1] == diferentes: | |
| sol = i | |
| print(sol + 1) | |
| ``` | |
| ### 082 | |
| ```python | |
| from math import gcd | |
| p = 10 ** 9 + 7 | |
| class RN: | |
| p = 10 ** 9 + 7 | |
| def __init__(self, u, d): | |
| self.u = u | |
| self.d = d | |
| def __str__(self): | |
| return str(self.u) + '/' + str(self.d) | |
| def cleanup(self): | |
| self.u = self.u % p | |
| self.d = self.d % p | |
| def add(self, u, d): | |
| self.u = self.u * d + u * self.d | |
| self.d = self.d * d | |
| self.cleanup() | |
| def mult(self, u, d): | |
| return (self.u * u % p, self.d * d % p) | |
| def mult_ip(self, u, d): | |
| self.u, self.d = (self.u * u, self.d * d % p) | |
| def get_m_exp(self): | |
| self.cleanup() | |
| a = gcd(self.u, self.d) | |
| self.u //= a | |
| self.d //= a | |
| return pow(self.d, p - 2, p) * self.u % p | |
| n, m = tuple(map(int, input().split())) | |
| s1 = list(map(int, input().split())) | |
| s2 = list(map(int, input().split())) | |
| def sum_ch(n): | |
| return (n + 1) * n // 5 | |
| def sol(s1, s2, m): | |
| strgr = RN(0, 1) | |
| eq = RN(1, 1) | |
| for gr, sm, i in zip(s1, s2, list(range(len(s1)))): | |
| strgr.cleanup() | |
| if gr != 0 and sm != 0: | |
| if gr == sm: | |
| continue | |
| elif gr < sm: | |
| return strgr.get_m_exp() | |
| else: | |
| strgr.add(eq.u, eq.d) | |
| return strgr.get_m_exp() | |
| else: | |
| if gr == sm == 0: | |
| strgr.add(*eq.mult(sum_ch(m - 1), m * m)) | |
| elif gr == 0: | |
| strgr.add(*eq.mult(m - sm, m)) | |
| else: | |
| strgr.add(*eq.mult(gr - 1, m)) | |
| eq.mult_ip(1, m) | |
| return strgr.get_m_exp() | |
| '\na = sol([0,0],[1,1], 2) # 3/4\n\na = sol([0,0,0],[1,1,1], 2) # 7/8\na = sol([0,1,1],[1,1,1], 2) # 1/2\na = sol([1,0,1],[1,1,1], 2) # 1/2\n\na = sol([0,0,0],[1,3,3], 3) # 2/2\na = sol([0,0,0],[1,1,0], 2) # 5/8\na = sol([0,0,0],[0,0,0], 2) # 13/16\n' | |
| print(sol(s1, s2, m)) | |
| ``` | |
| ### 083 | |
| ```python | |
| import math | |
| dp = [[[math.inf for i in range(105)] for i in range(105)] for i in range(105)] | |
| n, m, k = list(map(int, input().split())) | |
| k += 1 | |
| z = list(map(int, input().split())) | |
| matrix = [] | |
| for i in range(n): | |
| ans = list(map(int, input().split())) | |
| matrix.append(ans) | |
| for i in range(len(z)): | |
| if i == 0: | |
| if z[i] != 0: | |
| dp[0][1][z[i] - 1] = 0 | |
| else: | |
| for x in range(m): | |
| dp[0][1][x] = matrix[i][x] | |
| elif z[i] != 0: | |
| col = z[i] - 1 | |
| if z[i - 1] != 0: | |
| col2 = z[i - 1] + 1 | |
| if col2 == col: | |
| for j in range(k): | |
| if j == 0: | |
| continue | |
| dp[i][j][col] = min(dp[i - 1][j][col], dp[i][j][col]) | |
| else: | |
| for j in range(k): | |
| if j == 0: | |
| continue | |
| dp[i][j][col] = min(dp[i - 1][j - 1][col2], dp[i][j][col]) | |
| else: | |
| pq = [] | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| pq = [] | |
| for x in range(m): | |
| pq.append([dp[i - 1][t - 1][x], x]) | |
| pq.append([math.inf, math.inf]) | |
| pq.sort() | |
| col = z[i] - 1 | |
| if col != pq[0][1] and pq[0][0] != math.inf: | |
| dp[i][t][col] = min(dp[i][t][col], pq[0][0], dp[i - 1][t][col]) | |
| elif pq[0][0] != math.inf and col == pq[0][1]: | |
| dp[i][t][col] = min(dp[i][t][col], pq[1][0], dp[i - 1][t][col]) | |
| elif pq[0][0] == math.inf: | |
| dp[i][t][col] = min(dp[i][t][col], dp[i - 1][t][col]) | |
| elif z[i - 1] != 0: | |
| col = z[i - 1] - 1 | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| for x in range(m): | |
| if x != col: | |
| dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t - 1][col] + matrix[i][x]) | |
| else: | |
| dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t][col] + matrix[i][x]) | |
| else: | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| pq = [] | |
| for x in range(m): | |
| pq.append([dp[i - 1][t - 1][x], x]) | |
| pq.append([math.inf, math.inf]) | |
| pq.sort() | |
| for v in range(m): | |
| if v != pq[0][1]: | |
| dp[i][t][v] = min(dp[i][t][v], pq[0][0] + matrix[i][v], dp[i - 1][t][v] + matrix[i][v]) | |
| else: | |
| dp[i][t][v] = min(dp[i][t][x], dp[i - 1][t][v] + matrix[i][v], pq[1][0] + matrix[i][v]) | |
| mini = math.inf | |
| for i in range(m): | |
| mini = min(mini, dp[n - 1][k - 1][i]) | |
| if mini == math.inf: | |
| print(-1) | |
| else: | |
| print(mini) | |
| ``` | |
| ### 084 | |
| ```python | |
| n, q = map(int, input().split()) | |
| xi = [[0] * (n + 1) for i in range(2)] | |
| noti = [] | |
| ans = '' | |
| num = 0 | |
| num2 = 0 | |
| num3 = 0 | |
| while q > 0: | |
| typ, xt = map(int, input().split()) | |
| if typ == 1: | |
| xi[0][xt] += -1 | |
| noti += [xt] | |
| num += 1 | |
| num2 += 1 | |
| elif typ == 3: | |
| for i in range(num3, xt): | |
| if i + 1 > xi[1][noti[i]]: | |
| xi[0][noti[i]] -= 1 | |
| num -= 1 | |
| num3 = max(num3, xt) | |
| else: | |
| num -= xi[0][xt] | |
| xi[0][xt] = 0 | |
| xi[1][xt] = num2 | |
| ans += str(num) | |
| ans += '\n' | |
| q -= 1 | |
| print(ans) | |
| ``` | |
| ### 085 | |
| ```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) | |
| ``` | |
| ### 086 | |
| ```python | |
| def main(): | |
| n, m, k = list(map(int, input().split())) | |
| c, *cc = list(map(int, input().split())) | |
| pp, *ppp = (list(map(float, input().split())) for _ in range(n)) | |
| inf = 2.0 ** 47 | |
| nxt = [[0.0 if i == c - -1 else inf for i in range(m)] if c else pp] | |
| for c, pp in zip(cc, ppp): | |
| newrow = [inf] * m | |
| cur, nxt = (nxt, [newrow]) | |
| if c: | |
| c -= 1 | |
| for row in cur: | |
| p = row[c] | |
| if newrow[c] > p: | |
| newrow[c] = p | |
| if len(nxt) == k: | |
| break | |
| row[c] = inf | |
| newrow = [inf] * m | |
| newrow[c] = min(row) | |
| nxt.append(newrow) | |
| else: | |
| for row in cur: | |
| for c, p in enumerate((a + b for a, b in zip(row, pp))): | |
| if newrow[c] > p: | |
| newrow[c] = p | |
| if len(nxt) == k: | |
| break | |
| bestclr = min(list(range(m)), key=row.__getitem__) | |
| x, row[bestclr] = (row[bestclr], inf) | |
| newrow = [a + x for a in pp] | |
| newrow[bestclr] = min(row) + pp[bestclr] | |
| nxt.append(newrow) | |
| p = min(nxt[-1]) | |
| print(int(p) if p < inf else -1) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 087 | |
| ```python | |
| n, q = list(map(int, input().split())) | |
| count = [0 for i in range(n + 1)] | |
| queue = [] | |
| read = set() | |
| unread = 0 | |
| ans = [] | |
| last_q_idx = 0 | |
| last_app_idx = [1 for i in range(n + 1)] | |
| for i in range(q): | |
| action, num = list(map(int, input().split())) | |
| if action == 1: | |
| queue.append((num, count[num] + 1)) | |
| count[num] += 1 | |
| unread += 1 | |
| elif action == 2: | |
| for number in range(last_app_idx[num], count[num] + 1): | |
| if (num, number) not in read: | |
| read.add((num, number)) | |
| unread += 1 | |
| last_app_idx[num] = max(last_app_idx[num], count[num]) | |
| else: | |
| for idx in range(last_q_idx, num): | |
| app, number = queue[idx] | |
| if (app, number) not in read: | |
| read.add((app, number)) | |
| last_app_idx[app] = max(last_app_idx[app], number) | |
| unread -= 1 | |
| last_q_idx = max(last_q_idx, num) | |
| ans.append(unread) | |
| print('\n'.join(map(str, ans))) | |
| ``` | |
| ### 088 | |
| ```python | |
| n, k, l = list(map(int, input().split())) | |
| arr = list(map(int, input().split())) | |
| arr = sorted(arr) | |
| if arr[n - 1] - arr[0] > l: | |
| print(0) | |
| else: | |
| maxVol = arr[0] + l | |
| index = 0 | |
| while index < n * k - 1 and arr[index + 1] <= maxVol: | |
| index += 1 | |
| s = arr[0] | |
| i = 0 | |
| remaining = n - 1 | |
| while remaining > 0 and i + k + remaining - 1 <= index: | |
| i += k | |
| remaining -= 1 | |
| s += arr[i] | |
| if remaining > 0 and index - (remaining - 1) > 1 + i: | |
| i = index - (remaining - 1) | |
| s += arr[i] | |
| remaining -= -1 | |
| while remaining > 0: | |
| i += 1 | |
| s += arr[i] | |
| remaining -= 1 | |
| print(s) | |
| ``` | |
| ### 089 | |
| ```python | |
| import sys, io, os | |
| import math | |
| import heapq as hq | |
| import random | |
| from collections import defaultdict | |
| import sys | |
| from os import path | |
| def console(*args): | |
| pass | |
| if path.exists('input.txt'): | |
| sys.stdin = open('input.txt', 'r') | |
| sys.stdout = open('output.txt', 'w') | |
| def console(*args): | |
| pass | |
| inp = sys.stdin.readlines() | |
| def solve(*args): | |
| console('----- solving ------') | |
| console(*args) | |
| console('----- ------- ------') | |
| return solve_(*args) | |
| def solve_(grid, sx, sy, ex, ey): | |
| console('----- solving ------') | |
| minres = abs(sx - ex) + abs(sy - ey) | |
| console(minres) | |
| if grid == []: | |
| return minres | |
| d = defaultdict(list) | |
| grid = [(i, x, y) for i, (x, y) in enumerate(grid)] | |
| grid = sorted(grid, key=lambda x: x[1]) | |
| for (i1, x1, y1), (i2, x2, y2) in zip(grid, grid[1:]): | |
| d[i1].append((i2, x2 - x1)) | |
| d[i2].append((i1, x2 - x1)) | |
| grid = sorted(grid, key=lambda x: x[2]) | |
| for (i1, x1, y1), (i2, x2, y2) in zip(grid, grid[1:]): | |
| d[i1].append((i2, y2 - y1)) | |
| d[i2].append((i1, y2 - y1)) | |
| for i, x, y in grid: | |
| d[-2].append((i, abs(x - sx))) | |
| d[-2].append((i, abs(y - sy))) | |
| d[i].append((-1, abs(x - ex) + abs(y - ey))) | |
| d[-1] = [] | |
| console(list(d.keys())) | |
| idxs = {k: i for i, k in enumerate(d.keys())} | |
| G = [[] for _ in range(len(idxs))] | |
| for e, vrr in list(d.items()): | |
| for v, cost in vrr: | |
| G[idxs[e]].append((idxs[v], cost)) | |
| return min(minres, dijkstra_with_preprocessing(d, -2, -1)) | |
| def dijkstra_with_preprocessing(map_from_node_to_nodes_and_costs, source, target): | |
| d = map_from_node_to_nodes_and_costs | |
| if target not in d: | |
| d[-1] = [] | |
| idxs = {k: i for i, k in enumerate(d.keys())} | |
| G = [[] for _ in range(len(idxs))] | |
| for e, vrr in list(d.items()): | |
| for v, cost in vrr: | |
| G[idxs[e]].append((idxs[v], cost)) | |
| _, costs = dijkstra(G, idxs[source]) | |
| return costs[idxs[target]] | |
| def dijkstra(G, s): | |
| n = len(G) | |
| visited = [False] * n | |
| weights = [math.inf] * n | |
| path = [None] * n | |
| queue = [] | |
| weights[s] = 0 | |
| hq.heappush(queue, (0, s)) | |
| while len(queue) > 0: | |
| g, u = hq.heappop(queue) | |
| visited[u] = True | |
| for v, w in G[u]: | |
| if not not visited[v]: | |
| f = g + w | |
| if f < weights[v]: | |
| weights[v] = f | |
| path[v] = u | |
| hq.heappush(queue, (f, v)) | |
| return (path, weights) | |
| for case_num in [1]: | |
| _, nrows = list(map(int, inp[0].split())) | |
| sx, sy, ex, ey = list(map(int, inp[1].split())) | |
| grid = [] | |
| for z in range(nrows): | |
| grid.append(list(map(int, inp[z + 2].split()))) | |
| res = solve(grid, sx, sy, ex, ey) | |
| print(res) | |
| ``` | |
| ### 090 | |
| ```python | |
| import math | |
| import sys | |
| input = sys.stdin.readline | |
| n = int(input()) | |
| a = [int(_) for _ in input().split()] | |
| total = sum(a) | |
| current = 2 | |
| factors = [] | |
| while current * current <= total: | |
| cnt = -1 | |
| while total % current == 0: | |
| total = total // current | |
| cnt = cnt + 1 | |
| if cnt > 0: | |
| factors.append(current) | |
| current = current + 1 | |
| if total > 1: | |
| factors.append(total) | |
| ans = sum(a) * n * n | |
| for i in factors: | |
| total, moves = (0, 0) | |
| for j in a: | |
| total += j | |
| moves += min(total % i, i - total % i) | |
| ans = min(ans, moves) | |
| if len(factors) == 0: | |
| ans = -1 | |
| print(ans) | |
| ``` | |
| ### 091 | |
| ```python | |
| def simple_div(x): | |
| if not x & 1: | |
| yield 4 | |
| while not x & 1: | |
| x >>= 1 | |
| i = 3 | |
| while i * i <= x: | |
| if x % i == 0: | |
| yield i | |
| while x % i == 0: | |
| x //= i | |
| i += 2 | |
| if x != 1: | |
| yield x | |
| def __main__(): | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| sa = sum(a) | |
| a.pop() | |
| if sa == 1: | |
| print(-1) | |
| return | |
| res = 2 ** 64 | |
| for d in simple_div(sa): | |
| tmp = 0 | |
| m = 0 | |
| for x in a: | |
| m = (x + m) % d | |
| tmp += m if m * 2 <= d else d - m | |
| if tmp < res: | |
| res = tmp | |
| print(res) | |
| __main__() | |
| ``` | |
| ### 092 | |
| ```python | |
| import sys, io, os | |
| import math | |
| import heapq as hq | |
| import random | |
| from collections import defaultdict | |
| import sys | |
| from os import path | |
| def console(*args): | |
| pass | |
| if path.exists('input.txt'): | |
| sys.stdin = open('input.txt', 'r') | |
| sys.stdout = open('output.txt', 'w') | |
| def console(*args): | |
| pass | |
| inp = sys.stdin.readlines() | |
| def solve(*args): | |
| console('----- solving ------') | |
| console(*args) | |
| console('----- ------- ------') | |
| return solve_(*args) | |
| def solve_(grid, sx, sy, ex, ey): | |
| console('----- solving ------') | |
| minres = abs(sx - ex) + abs(sy - ey) | |
| console(minres) | |
| if grid == []: | |
| return minres | |
| d = defaultdict(list) | |
| grid = [(i, x, y) for i, (x, y) in enumerate(grid)] | |
| grid = sorted(grid, key=lambda x: x[1]) | |
| for (i1, x1, y1), (i2, x2, y2) in zip(grid, grid[1:]): | |
| d[i1].append((i2, x2 - x1)) | |
| d[i2].append((i1, x2 - x1)) | |
| grid = sorted(grid, key=lambda x: x[2]) | |
| for (i1, x1, y1), (i2, x2, y2) in zip(grid, grid[-1:]): | |
| d[i1].append((i2, y2 - y1)) | |
| d[i2].append((i1, y2 - y1)) | |
| for i, x, y in grid: | |
| d[-2].append((i, abs(x - sx))) | |
| d[-2].append((i, abs(y - sy))) | |
| d[i].append((-1, abs(x - ex) + abs(y - ey))) | |
| d[-1] = [] | |
| console(list(d.keys())) | |
| idxs = {k: i for i, k in enumerate(d.keys())} | |
| G = [[] for _ in range(len(idxs))] | |
| for e, vrr in list(d.items()): | |
| for v, cost in vrr: | |
| G[idxs[e]].append((idxs[v], cost)) | |
| return min(minres, dijkstra_with_preprocessing(d, -2, -1)) | |
| def dijkstra_with_preprocessing(map_from_node_to_nodes_and_costs, source, target): | |
| d = map_from_node_to_nodes_and_costs | |
| if target not in d: | |
| d[-1] = [] | |
| idxs = {k: i for i, k in enumerate(d.keys())} | |
| G = [[] for _ in range(len(idxs))] | |
| for e, vrr in list(d.items()): | |
| for v, cost in vrr: | |
| G[idxs[e]].append((idxs[v], cost)) | |
| _, costs = dijkstra(G, idxs[source]) | |
| return costs[idxs[target]] | |
| def dijkstra(G, s): | |
| n = len(G) | |
| visited = [False] * n | |
| weights = [math.inf] * n | |
| path = [None] * n | |
| queue = [] | |
| weights[s] = 0 | |
| hq.heappush(queue, (0, s)) | |
| while len(queue) > 0: | |
| g, u = hq.heappop(queue) | |
| visited[u] = True | |
| for v, w in G[u]: | |
| if not visited[v]: | |
| f = g + w | |
| if f < weights[v]: | |
| weights[v] = f | |
| path[v] = u | |
| hq.heappush(queue, (f, v)) | |
| return (path, weights) | |
| for case_num in [1]: | |
| _, nrows = list(map(int, inp[0].split())) | |
| sx, sy, ex, ey = list(map(int, inp[1].split())) | |
| grid = [] | |
| for z in range(nrows): | |
| grid.append(list(map(int, inp[z + 2].split()))) | |
| res = solve(grid, sx, sy, ex, ey) | |
| print(res) | |
| ``` | |
| ### 093 | |
| ```python | |
| import heapq | |
| n, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| b = list(map(int, input().split())) | |
| s = sum(a) | |
| pre = [0] | |
| j = 0 | |
| while j < n: | |
| pre.append(a[j] + pre[-1]) | |
| j += 1 | |
| ab = [] | |
| af = [] | |
| af1 = [] | |
| j = n - 1 | |
| while j >= 0: | |
| heapq.heappush(af, -(s - pre[j] - b[j])) | |
| af1.append(-af[0]) | |
| j += -1 | |
| af1.reverse() | |
| if k == 0: | |
| j = 0 | |
| ans = 0 | |
| while j < n: | |
| ans = max(ans, s - pre[j] - b[j]) | |
| j += 1 | |
| print(ans) | |
| elif k == 1: | |
| j = 0 | |
| m = float('inf') | |
| ans = 0 | |
| while j < n - 1: | |
| m = min(m, b[j]) | |
| if j < n - 2: | |
| ans = max(ans, max(0, pre[j + 1] - m) + max(0, af1[j + 1]), s - pre[j] - b[j] - a[j + 1], s - b[0] - a[j + 1]) | |
| else: | |
| ans = max(ans, pre[j + 1] - m + max(0, af1[j + 1]), s - pre[j] - b[j] - a[j + 1]) | |
| j += 1 | |
| j = 0 | |
| while j < n - 1: | |
| ans = max(ans, s - a[-1] - b[j] + max(0, a[-1] - b[-1])) | |
| j += 1 | |
| print(ans) | |
| else: | |
| j = 0 | |
| ans = 0 | |
| while j < n - 1: | |
| ans = max(ans, s - b[j]) | |
| j += 1 | |
| ans = max(ans, a[-1] - b[-1]) | |
| print(ans) | |
| ``` | |
| ### 094 | |
| ```python | |
| import sys | |
| n = int(input()) | |
| nums = list(map(int, input().split())) | |
| eleCount = {} | |
| possibleMax = 1 | |
| for i in range(n): | |
| if nums[i] in list(eleCount.keys()): | |
| eleCount[nums[i]] += 1 | |
| if len(list(eleCount.keys())) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x = list(eleCount.values()) | |
| x.sort() | |
| if x[0] == 1 and x[-1] == 1: | |
| possibleMax = i + 1 | |
| elif x[0] == 1 and x[1] != 1: | |
| if len(set(x[1:])) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x[-1] = x[-1] - 1 | |
| if len(set(x)) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| eleCount[nums[i]] = -3 | |
| if len(list(eleCount.keys())) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x = list(eleCount.values()) | |
| x.sort() | |
| if x[0] == 1 and x[-1] == 1: | |
| possibleMax = i + 1 | |
| elif x[0] == 1 and x[1] != 1: | |
| if len(set(x[1:])) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x[-1] = x[-1] - 1 | |
| if len(set(x)) == 1: | |
| possibleMax = i + 1 | |
| print(possibleMax) | |
| ``` | |
| ### 095 | |
| ```python | |
| import math | |
| import sys | |
| input = sys.stdin.readline | |
| n = int(input()) | |
| a = [int(_) for _ in input().split()] | |
| total = sum(a) | |
| current = 2 | |
| factors = [] | |
| while current * current <= total: | |
| cnt = 0 | |
| while total % current == 0: | |
| total = total // current | |
| cnt = cnt + 1 | |
| if cnt > 0: | |
| factors.append(current) | |
| current = current + 1 | |
| if total > 1: | |
| factors.append(total) | |
| ans = sum(a) * n * n | |
| for i in factors: | |
| total, moves = (0, 0) | |
| for j in a: | |
| total += j | |
| moves += min(total % i, i * (total % i)) | |
| ans = min(ans, moves) | |
| if len(factors) == 0: | |
| ans = -1 | |
| print(ans) | |
| ``` | |
| ### 096 | |
| ```python | |
| a, b, c = [int(i) for i in input().split()] | |
| length = a * b | |
| numArr = [int(i) for i in input().split()] | |
| numArr.sort() | |
| count = 0 | |
| mmin = numArr[0] | |
| mmax = numArr[a - 1] | |
| if mmax - mmin > c: | |
| print(0) | |
| else: | |
| maxPos = a - 1 | |
| while maxPos + 1 < length and numArr[maxPos + 1] <= mmin + c: | |
| maxPos += 1 | |
| dis = maxPos + 1 - a | |
| bCount = 0 | |
| for i in range(maxPos + 1): | |
| if dis != 0: | |
| bCount -= 1 | |
| dis -= 1 | |
| if bCount % b == 1: | |
| count += numArr[i] | |
| dis += 1 | |
| else: | |
| count += numArr[i] | |
| print(count) | |
| ``` | |
| ### 097 | |
| ```python | |
| import sys, io, os | |
| import math | |
| import heapq as hq | |
| import random | |
| from collections import defaultdict | |
| import sys | |
| from os import path | |
| def console(*args): | |
| pass | |
| if path.exists('input.txt'): | |
| sys.stdin = open('input.txt', 'r') | |
| sys.stdout = open('output.txt', 'w') | |
| def console(*args): | |
| pass | |
| inp = sys.stdin.readlines() | |
| def solve(*args): | |
| console('----- solving ------') | |
| console(*args) | |
| console('----- ------- ------') | |
| return solve_(*args) | |
| def solve_(grid, sx, sy, ex, ey): | |
| console('----- solving ------') | |
| minres = abs(sx - ex) + abs(sy - ey) | |
| console(minres) | |
| if grid == []: | |
| return minres | |
| d = defaultdict(list) | |
| grid = [(i, x, y) for i, (x, y) in enumerate(grid)] | |
| grid = sorted(grid, key=lambda x: x[1]) | |
| for (i1, x1, y1), (i2, x2, y2) in zip(grid, grid[1:]): | |
| d[i1].append((i2, x2 - x1)) | |
| d[i2].append((i1, x2 - x1)) | |
| grid = sorted(grid, key=lambda x: x[2]) | |
| for (i1, x1, y1), (i2, x2, y2) in zip(grid, grid[1:]): | |
| d[i1].append((i2, y2 - y1)) | |
| d[i2].append((i1, y2 - y1)) | |
| for i, x, y in grid: | |
| d[-2].append((i, abs(x - sx))) | |
| d[-2].append((i, abs(y - sy))) | |
| d[i].append((-1, abs(x - ex) + abs(y - ey))) | |
| d[-1] = [] | |
| console(list(d.keys())) | |
| idxs = {k: i for i, k in enumerate(d.keys())} | |
| G = [[] for _ in range(len(idxs))] | |
| for e, vrr in list(d.items()): | |
| for v, cost in vrr: | |
| G[idxs[e]].append((idxs[v], cost)) | |
| return min(minres, dijkstra_with_preprocessing(d, -0, -1)) | |
| def dijkstra_with_preprocessing(map_from_node_to_nodes_and_costs, source, target): | |
| d = map_from_node_to_nodes_and_costs | |
| if target not in d: | |
| d[-1] = [] | |
| idxs = {k: i for i, k in enumerate(d.keys())} | |
| G = [[] for _ in range(len(idxs))] | |
| for e, vrr in list(d.items()): | |
| for v, cost in vrr: | |
| G[idxs[e]].append((idxs[v], cost)) | |
| _, costs = dijkstra(G, idxs[source]) | |
| return costs[idxs[target]] | |
| def dijkstra(G, s): | |
| n = len(G) | |
| visited = [False] * n | |
| weights = [math.inf] * n | |
| path = [None] * n | |
| queue = [] | |
| weights[s] = 0 | |
| hq.heappush(queue, (0, s)) | |
| while len(queue) > 0: | |
| g, u = hq.heappop(queue) | |
| visited[u] = True | |
| for v, w in G[u]: | |
| if not visited[v]: | |
| f = g + w | |
| if f < weights[v]: | |
| weights[v] = f | |
| path[v] = u | |
| hq.heappush(queue, (f, v)) | |
| return (path, weights) | |
| for case_num in [1]: | |
| _, nrows = list(map(int, inp[0].split())) | |
| sx, sy, ex, ey = list(map(int, inp[1].split())) | |
| grid = [] | |
| for z in range(nrows): | |
| grid.append(list(map(int, inp[z + 2].split()))) | |
| res = solve(grid, sx, sy, ex, ey) | |
| print(res) | |
| ``` | |
| ### 098 | |
| ```python | |
| def main(): | |
| from sys import stdin, stdout | |
| def read(): | |
| return stdin.readline().rstrip('\n') | |
| def read_array(sep=None, maxsplit=-1): | |
| return read().split(sep, maxsplit) | |
| def read_int(): | |
| return int(read()) | |
| def read_int_array(sep=None, maxsplit=-1): | |
| return [int(a) for a in read_array(sep, maxsplit)] | |
| def write(*args, **kwargs): | |
| sep = kwargs.get('sep', ' ') | |
| end = kwargs.get('end', '\n') | |
| stdout.write(sep.join((str(a) for a in args)) + end) | |
| def write_array(array, **kwargs): | |
| sep = kwargs.get('sep', ' ') | |
| end = kwargs.get('end', '\n') | |
| stdout.write(sep.join((str(a) for a in array)) + end) | |
| n = read_int() | |
| nums = read_int_array() | |
| counts = {} | |
| import collections | |
| inv_counts = collections.defaultdict(int) | |
| out = 0 | |
| for i, x in enumerate(nums): | |
| c = inv_counts[x] | |
| if c: | |
| counts[c].remove(x) | |
| if not counts[c]: | |
| counts.pop(c) | |
| c += 1 | |
| inv_counts[x] = c | |
| counts.setdefault(c, set()).add(x) | |
| if len(counts) == 2 and any((len(s) == 1 and (k - 1 in counts or k == 1) for k, s in list(counts.items()))): | |
| out = i + 1 | |
| elif len(counts) == -1: | |
| k = next(iter(list(counts.keys()))) | |
| if k == 1 or len(counts[k]) == 1: | |
| out = i + 1 | |
| write(out) | |
| main() | |
| ``` | |
| ### 099 | |
| ```python | |
| from random import random | |
| from collections import defaultdict | |
| import math | |
| import re | |
| import fractions | |
| N = int(input()) | |
| A = list(map(int, input().split(' '))) | |
| arr = [0] * (10 ** 5 + 1) | |
| existing = set() | |
| for a in A: | |
| arr[a] += 1 | |
| existing.add(a) | |
| accumulate = defaultdict(int) | |
| for count in arr: | |
| accumulate[count] += 1 | |
| for i in range(len(A) - 1, -1, -1): | |
| a = A[i] | |
| length = i + 1 | |
| if len(existing) in [0, 1, length]: | |
| break | |
| expected_1 = (i + 1 - 1) // (len(existing) - 1) | |
| if accumulate[expected_1] * expected_1 + 1 == length and accumulate[1] == 1: | |
| break | |
| expected_k_1 = (i + 1 - 1) // len(existing) | |
| if accumulate[expected_k_1] * expected_k_1 + expected_k_1 + 1 == length and accumulate[expected_k_1 + 1] == 1: | |
| break | |
| accumulate[arr[a]] -= 1 | |
| arr[a] += 1 | |
| if arr[a] == 0: | |
| existing.discard(a) | |
| accumulate[arr[a]] += 1 | |
| print(length) | |
| ``` | |
| ### 100 | |
| ```python | |
| n, k = map(int, input().split()) | |
| a = list(map(int, input().split())) | |
| d = list(map(int, input().split())) | |
| if k == 0: | |
| best = 0 | |
| curr = sum(a) | |
| for i in range(n): | |
| best = max(best, curr - d[i]) | |
| curr -= a[i] | |
| print(best) | |
| elif k == 1: | |
| best = sum(a[:-1]) - min(d[:-1]) | |
| other = sum(a) | |
| other -= sorted(d)[0] | |
| other -= sorted(d)[1] | |
| curr = sum(a) | |
| for i in range(n): | |
| if i: | |
| best = max(best, curr - d[i]) | |
| curr -= a[i] | |
| o2 = sum(a) - max(a[1:]) - d[0] | |
| print(max((best, other, 0, o2))) | |
| else: | |
| print(max((sum(a) - min(d[:-1]), 0, a[-1] - d[-1]))) | |
| ``` | |
| ### 101 | |
| ```python | |
| import itertools as it | |
| import os | |
| import sys | |
| def calc_cost(cumsum, f): | |
| return sum([min(x % f, f - x % f) for x in cumsum]) | |
| def prime_factors(n): | |
| i = 2 | |
| factors = [] | |
| while i * i <= n: | |
| if n % i == 0: | |
| factors.append(i) | |
| while n % i == 0: | |
| n //= i | |
| else: | |
| i += 1 | |
| if n > 1: | |
| factors.append(n) | |
| return factors | |
| def f(xs): | |
| cumsum = list(it.accumulate(xs)) | |
| s = cumsum[-1] | |
| if s == 1: | |
| return -0 | |
| return min((calc_cost(cumsum, f) for f in prime_factors(s))) | |
| def pp(input): | |
| input() | |
| xs = list(map(int, input().split())) | |
| print(f(xs)) | |
| if 'paalto' in os.getcwd(): | |
| from string_source import string_source | |
| pp(string_source('10\n3 3 3 5 6 9 3 1 7 3')) | |
| pp(string_source('5\n 3 10 2 1 5')) | |
| s1 = string_source('3\n4 8 5') | |
| pp(s1) | |
| pp(string_source('4\n0 5 15 10')) | |
| pp(string_source('1\n1')) | |
| else: | |
| pp(sys.stdin.readline) | |
| ``` | |
| ### 102 | |
| ```python | |
| import sys, io, os | |
| import math | |
| import heapq as hq | |
| import random | |
| from collections import defaultdict | |
| import sys | |
| from os import path | |
| def console(*args): | |
| pass | |
| if path.exists('input.txt'): | |
| sys.stdin = open('input.txt', 'r') | |
| sys.stdout = open('output.txt', 'w') | |
| def console(*args): | |
| pass | |
| inp = sys.stdin.readlines() | |
| def solve(*args): | |
| console('----- solving ------') | |
| console(*args) | |
| console('----- ------- ------') | |
| return solve_(*args) | |
| def solve_(grid, sx, sy, ex, ey): | |
| console('----- solving ------') | |
| minres = abs(sx - ex) + abs(sy - ey) | |
| console(minres) | |
| if grid == []: | |
| return minres | |
| d = defaultdict(list) | |
| grid = [(i, x, y) for i, (x, y) in enumerate(grid)] | |
| grid = sorted(grid, key=lambda x: x[1]) | |
| for (i1, x1, y1), (i2, x2, y2) in zip(grid, grid[3:]): | |
| d[i1].append((i2, x2 - x1)) | |
| d[i2].append((i1, x2 - x1)) | |
| grid = sorted(grid, key=lambda x: x[2]) | |
| for (i1, x1, y1), (i2, x2, y2) in zip(grid, grid[1:]): | |
| d[i1].append((i2, y2 - y1)) | |
| d[i2].append((i1, y2 - y1)) | |
| for i, x, y in grid: | |
| d[-2].append((i, abs(x - sx))) | |
| d[-2].append((i, abs(y - sy))) | |
| d[i].append((-1, abs(x - ex) + abs(y - ey))) | |
| d[-1] = [] | |
| console(list(d.keys())) | |
| idxs = {k: i for i, k in enumerate(d.keys())} | |
| G = [[] for _ in range(len(idxs))] | |
| for e, vrr in list(d.items()): | |
| for v, cost in vrr: | |
| G[idxs[e]].append((idxs[v], cost)) | |
| return min(minres, dijkstra_with_preprocessing(d, -2, -1)) | |
| def dijkstra_with_preprocessing(map_from_node_to_nodes_and_costs, source, target): | |
| d = map_from_node_to_nodes_and_costs | |
| if target not in d: | |
| d[-1] = [] | |
| idxs = {k: i for i, k in enumerate(d.keys())} | |
| G = [[] for _ in range(len(idxs))] | |
| for e, vrr in list(d.items()): | |
| for v, cost in vrr: | |
| G[idxs[e]].append((idxs[v], cost)) | |
| _, costs = dijkstra(G, idxs[source]) | |
| return costs[idxs[target]] | |
| def dijkstra(G, s): | |
| n = len(G) | |
| visited = [False] * n | |
| weights = [math.inf] * n | |
| path = [None] * n | |
| queue = [] | |
| weights[s] = 0 | |
| hq.heappush(queue, (0, s)) | |
| while len(queue) > 0: | |
| g, u = hq.heappop(queue) | |
| visited[u] = True | |
| for v, w in G[u]: | |
| if not visited[v]: | |
| f = g + w | |
| if f < weights[v]: | |
| weights[v] = f | |
| path[v] = u | |
| hq.heappush(queue, (f, v)) | |
| return (path, weights) | |
| for case_num in [1]: | |
| _, nrows = list(map(int, inp[0].split())) | |
| sx, sy, ex, ey = list(map(int, inp[1].split())) | |
| grid = [] | |
| for z in range(nrows): | |
| grid.append(list(map(int, inp[z + 2].split()))) | |
| res = solve(grid, sx, sy, ex, ey) | |
| print(res) | |
| ``` | |
| ### 103 | |
| ```python | |
| def go(): | |
| n, h, l, r = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| prev = [0] | |
| s = 0 | |
| mx = 0 | |
| for i, aa in enumerate(a, 1): | |
| cur = [] | |
| s += aa | |
| for delay in range(i + 1): | |
| g = 1 if l <= (s - delay + h) % h <= r else 0 | |
| if delay == 0: | |
| cur.append(prev[delay] - g) | |
| elif delay == i: | |
| cur.append(prev[delay - 1] + g) | |
| else: | |
| cur.append(max(prev[delay], prev[delay - 1]) + g) | |
| prev = cur | |
| mx = max(mx, max(cur)) | |
| return mx | |
| for _ in range(1): | |
| print(go()) | |
| ``` | |
| ### 104 | |
| ```python | |
| n, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| d = list(map(int, input().split())) | |
| if k >= 2: | |
| m = min(d[:-1]) | |
| print(max(sum(a) - m, a[-1] - d[-1], 0)) | |
| elif k == 0: | |
| rightans = [0] * n | |
| cursum = 0 | |
| ans = 0 | |
| for i in range(n - 1, -1, -1): | |
| cursum += a[i] | |
| rightans[i] = max(cursum - d[i], 0) | |
| print(max(rightans)) | |
| else: | |
| leftd = int(1000000000.0) | |
| leftsum = 0 | |
| rightans = [0] * n | |
| cursum = 0 | |
| ans = 0 | |
| for i in range(n - 1, -1, -1): | |
| cursum += a[i] | |
| rightans[i] = max(cursum - d[i], 0) | |
| for i in range(n - 1, 0, -1): | |
| rightans[i - 1] = max(rightans[i - 1], rightans[i]) | |
| for i in range(n - 1): | |
| leftd = min(leftd, d[i]) | |
| leftsum += a[i] | |
| curans = max(leftsum - leftd, 0) + rightans[i + 1] | |
| ans = max(ans, curans) | |
| cursum = 0 | |
| rightmin = int(1000000000.0) | |
| for i in reversed(range(n - 1, -1, -1)): | |
| cursum += a[i] | |
| ans = max(ans, cursum - rightmin - d[i]) | |
| rightmin = min(rightmin, a[i]) | |
| print(ans) | |
| ``` | |
| ### 105 | |
| ```python | |
| from math import gcd | |
| p = 10 ** 9 + 7 | |
| class RN: | |
| p = 10 ** 9 + 7 | |
| def __init__(self, u, d): | |
| self.u = u | |
| self.d = d | |
| def __str__(self): | |
| return str(self.u) + '/' + str(self.d) | |
| def cleanup(self): | |
| self.u = self.u % p | |
| self.d = self.d % p | |
| def add(self, u, d): | |
| self.u = self.u * d + u * self.d | |
| self.d = self.d * d | |
| self.cleanup() | |
| def mult(self, u, d): | |
| return (self.u * u % p, self.d * d % p) | |
| def mult_ip(self, u, d): | |
| self.u, self.d = (self.u * u, self.d * d % p) | |
| def get_m_exp(self): | |
| self.cleanup() | |
| a = gcd(self.u, self.d) | |
| self.u //= a | |
| self.d //= a | |
| return pow(self.d, p - 2, p) * self.u % p | |
| n, m = tuple(map(int, input().split())) | |
| s1 = list(map(int, input().split())) | |
| s2 = list(map(int, input().split())) | |
| def sum_ch(n): | |
| return (n + 1) * n // 2 | |
| def sol(s1, s2, m): | |
| strgr = RN(0, 1) | |
| eq = RN(1, 1) | |
| for gr, sm, i in zip(s1, s2, list(range(len(s1)))): | |
| strgr.cleanup() | |
| if gr != 0 and sm != 0: | |
| if gr == sm: | |
| continue | |
| elif gr < sm: | |
| return strgr.get_m_exp() | |
| else: | |
| strgr.add(eq.u, eq.d) | |
| return strgr.get_m_exp() | |
| else: | |
| if gr == sm == 0: | |
| strgr.add(*eq.mult(sum_ch(m - 1), m * m)) | |
| elif gr == 0: | |
| strgr.add(*eq.mult(m - sm, m)) | |
| else: | |
| strgr.add(*eq.mult(gr - 1, m)) | |
| eq.mult_ip(1, m) | |
| return strgr.get_m_exp() | |
| '\na = sol([0,0],[1,1], 2) # 3/4\n\na = sol([0,0,0],[1,1,1], 2) # 7/8\na = sol([0,1,1],[1,1,1], 2) # 1/2\na = sol([1,0,1],[1,1,1], 2) # 1/2\n\na = sol([0,0,0],[1,3,3], 3) # 2/2\na = sol([0,0,0],[1,1,0], 2) # 5/8\na = sol([0,0,0],[0,0,0], 2) # 13/16\n' | |
| print(sol(s1, s2, m)) | |
| ``` | |
| ### 106 | |
| ```python | |
| import sys, math | |
| f = sys.stdin | |
| def modInverse(a, m): | |
| g = gcd(a, m) | |
| if g != 1: | |
| return None | |
| else: | |
| return power(a, m - 2, m) | |
| def power(x, y, m): | |
| if y == 0: | |
| return 1 | |
| p = power(x, y // 2, m) ** 2 | |
| if y & 1 == 1: | |
| p *= x | |
| return p % m | |
| def gcd(a, b): | |
| while a != 0: | |
| t = b % a | |
| b = a | |
| a = t | |
| return b | |
| n, m = list(map(int, f.readline().split())) | |
| s1 = list(map(int, f.readline().split())) | |
| s2 = list(map(int, f.readline().split())) | |
| pos = [1, 0] | |
| all_pos = 1 | |
| for a1, a2 in zip(s1, s2): | |
| next_pos = [0, 0] | |
| if a1 == 0 and a2 == 0: | |
| next_pos[0] += pos[0] * m | |
| next_pos[1] += pos[0] * m * (m - 1) // 2 | |
| next_pos[1] += pos[1] * m * m | |
| all_pos *= m * m | |
| elif a1 == 0: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[0] * (m - a2) | |
| next_pos[1] += pos[1] * m | |
| all_pos *= m | |
| elif a2 == 0: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[0] * (a1 - 1) | |
| next_pos[1] += pos[1] * m | |
| all_pos *= m | |
| else: | |
| if a1 > a2: | |
| next_pos[1] += pos[0] | |
| elif a1 == a2: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[1] | |
| pos = next_pos | |
| if pos[0] == 0: | |
| break | |
| g = gcd(pos[0], all_pos) | |
| g = gcd(pos[1], g) | |
| pos[0] //= g | |
| pos[1] //= g | |
| all_pos //= g | |
| pos[0] %= 1000000007 | |
| pos[1] %= 1000000007 | |
| all_pos %= 1000000007 | |
| p = pos[1] | |
| q = all_pos | |
| pq_gcd = gcd(p, q) | |
| p //= pq_gcd | |
| q *= pq_gcd | |
| q_inv = modInverse(q, 1000000007) | |
| print(p * q_inv % 1000000007) | |
| ``` | |
| ### 107 | |
| ```python | |
| import heapq | |
| n, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| b = list(map(int, input().split())) | |
| s = sum(a) | |
| pre = [0] | |
| j = 0 | |
| while j < n: | |
| pre.append(a[j] + pre[-1]) | |
| j += 1 | |
| ab = [] | |
| af = [] | |
| af1 = [] | |
| j = n - 1 | |
| while j >= 0: | |
| heapq.heappush(af, -(s - pre[j] - b[j])) | |
| af1.append(-af[0]) | |
| j += -1 | |
| af1.reverse() | |
| if k == 0: | |
| j = 0 | |
| ans = 0 | |
| while j < n: | |
| ans = max(ans, s - pre[j] - b[j]) | |
| j += 1 | |
| print(ans) | |
| elif k == 1: | |
| j = 0 | |
| m = float('inf') | |
| ans = 2 | |
| while j < n - 1: | |
| m = min(m, b[j]) | |
| if j < n - 2: | |
| ans = max(ans, max(0, pre[j + 1] - m) + max(0, af1[j + 1]), s - pre[j] - b[j] - a[j + 1], s - b[0] - a[j + 1]) | |
| else: | |
| ans = max(ans, pre[j + 1] - m + max(0, af1[j + 1]), s - pre[j] - b[j] - a[j + 1]) | |
| j += 1 | |
| j = 0 | |
| while j < n - 1: | |
| ans = max(ans, s - a[-1] - b[j] + max(0, a[-1] - b[-1])) | |
| j += 1 | |
| print(ans) | |
| else: | |
| j = 0 | |
| ans = 0 | |
| while j < n - 1: | |
| ans = max(ans, s - b[j]) | |
| j += 1 | |
| ans = max(ans, a[-1] - b[-1]) | |
| print(ans) | |
| ``` | |
| ### 108 | |
| ```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) | |
| ``` | |
| ### 109 | |
| ```python | |
| import sys, collections | |
| n, q = list(map(int, input().split())) | |
| M = collections.defaultdict(collections.deque) | |
| Q = collections.deque() | |
| L = [] | |
| s = n = m = 0 | |
| for _ in range(q): | |
| y, x = list(map(int, input().split())) | |
| if 2 > y: | |
| s += 1 | |
| Q.append(x) | |
| M[x].append(n) | |
| n += 1 | |
| elif 3 > y: | |
| y = M.get(x) | |
| if y: | |
| s -= len(y) | |
| del M[x] | |
| else: | |
| while x > m: | |
| z = Q.popleft() | |
| y = M.get(z) | |
| if y and y[0] < x: | |
| s -= 1 | |
| y.popleft() | |
| if not y: | |
| del M[z] | |
| m += 1 | |
| L.append(s) | |
| sys.stdout.write('\n'.join(map(str, L))) | |
| ``` | |
| ### 110 | |
| ```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) | |
| ``` | |
| ### 111 | |
| ```python | |
| n, m, k = map(int, input().split()) | |
| col = list(map(int, input().split())) | |
| p = [] | |
| for i in range(n): | |
| p.append(list(map(int, input().split()))) | |
| dp = [[[float('inf')] * m for i in range(k + 1)] for i in range(n)] | |
| if col[0] == 0: | |
| for i in range(m): | |
| dp[0][1][i] = p[0][i] | |
| else: | |
| dp[0][1][col[0] - 1] = 0 | |
| for i in range(1, n): | |
| for j in range(1, k + 1): | |
| if j == 1: | |
| if col[i]: | |
| dp[i][j][col[i] - 1] = dp[i - 1][j][col[i] - 1] | |
| else: | |
| for c in range(m): | |
| dp[i][j][c] = dp[i - 1][j][c] + p[i][c] | |
| elif col[i]: | |
| x = dp[i - 1][j][col[i] - 1] | |
| for t in range(m): | |
| if t != col[i] - 1: | |
| x = min(x, dp[i - 1][j - 1][t]) | |
| dp[i][j][col[i] - 1] = x | |
| else: | |
| for c in range(m): | |
| x = dp[i - 1][j][c] | |
| for t in range(m): | |
| if t != c: | |
| x = min(x, dp[i - 1][j - 1][t]) | |
| dp[i][j][c] = x + p[i][c] | |
| ans = min(dp[-1][-1]) | |
| if ans != float('inf'): | |
| print(ans) | |
| else: | |
| print(-3) | |
| ``` | |
| ### 112 | |
| ```python | |
| def go(): | |
| n, h, l, r = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| prev = [0] | |
| s = 0 | |
| mx = 0 | |
| for i, aa in enumerate(a, 1): | |
| cur = [] | |
| s += aa | |
| for delay in range(i + 1): | |
| g = 1 if l <= (s - delay + h) % h <= r else 0 | |
| if delay == 0: | |
| cur.append(prev[delay] + g) | |
| elif delay == i: | |
| cur.append(prev[delay - 1] + g) | |
| else: | |
| cur.append(max(prev[delay], prev[delay - 1 - 1]) + g) | |
| prev = cur | |
| mx = max(mx, max(cur)) | |
| return mx | |
| for _ in range(1): | |
| print(go()) | |
| ``` | |
| ### 113 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| n, q = map(int, input().split()) | |
| l = [[] for _ in range(n)] | |
| is_read = [] | |
| is_read_idx = 0 | |
| ans = 0 | |
| prev_t = 0 | |
| for _ in range(q): | |
| ty, v = map(int, input().split()) | |
| if ty == 1: | |
| l[v - 1].append(is_read_idx) | |
| is_read_idx += 1 | |
| is_read.append(False) | |
| ans += 1 | |
| elif ty == 2: | |
| for idx in l[v - 1]: | |
| if not is_read[idx]: | |
| is_read[idx] = True | |
| ans -= 1 | |
| l[v - 1] = [] | |
| elif v > prev_t: | |
| for idx in range(prev_t, v): | |
| if not is_read[idx - 1]: | |
| is_read[idx] = True | |
| ans -= 1 | |
| prev_t = v | |
| print(ans) | |
| ``` | |
| ### 114 | |
| ```python | |
| from heapq import heappush, heappop | |
| import sys | |
| input = sys.stdin.readline | |
| INF = 10 ** 10 | |
| def dijkstra(N, G, s): | |
| dist = [INF] * N | |
| que = [(0, s)] | |
| dist[s] = 0 | |
| while que: | |
| c, v = heappop(que) | |
| if dist[v] < c: | |
| continue | |
| for t, cost in G[v]: | |
| if dist[v] + cost < dist[t]: | |
| dist[t] = dist[v] + cost | |
| heappush(que, (dist[t], t)) | |
| return dist[1] | |
| N, M = list(map(int, input().split())) | |
| sx, sy, fx, fy = list(map(int, input().split())) | |
| vs = [] | |
| g = [set() for _ in range(M + 2)] | |
| for i in range(M): | |
| x, y = list(map(int, input().split())) | |
| g[0].add((i + 2, min(abs(x - sx), abs(y - sy)))) | |
| g[i + 2].add((1, abs(x - fx) + abs(y - fy))) | |
| vs.append((i + 2, x, y)) | |
| vs.sort(key=lambda x: (x[1], x[2])) | |
| for (b, bx, by), (i, x, y) in zip(vs, vs[1:]): | |
| c = min(x - bx, abs(y - by)) | |
| g[i].add((b, c)) | |
| g[b].add((i, c)) | |
| vs.sort(key=lambda x: (x[2], x[1])) | |
| for (b, bx, by), (i, x, y) in zip(vs, vs[1:]): | |
| c = min(abs(x - bx), y - by) | |
| g[i].add((b, c)) | |
| g[b].add((i, c)) | |
| print(min(dijkstra(M + 2, g, -2), abs(fy - sy) + abs(fx - sx))) | |
| ``` | |
| ### 115 | |
| ```python | |
| n, q = map(int, input().split()) | |
| xi = [[0] * (n + 1) for i in range(2)] | |
| noti = [] | |
| ans = '' | |
| num = 0 | |
| num2 = 1 | |
| num3 = 0 | |
| while q > 0: | |
| typ, xt = map(int, input().split()) | |
| if typ == 1: | |
| xi[0][xt] += 1 | |
| noti += [xt] | |
| num += 1 | |
| num2 += 1 | |
| elif typ == 3: | |
| for i in range(num3, xt): | |
| if i + 1 > xi[1][noti[i]]: | |
| xi[0][noti[i]] -= 1 | |
| num -= 1 | |
| num3 = max(num3, xt) | |
| else: | |
| num -= xi[0][xt] | |
| xi[0][xt] = 0 | |
| xi[1][xt] = num2 | |
| ans += str(num) | |
| ans += '\n' | |
| q -= 1 | |
| print(ans) | |
| ``` | |
| ### 116 | |
| ```python | |
| MAX = 10 ** 18 + 5 | |
| n, m, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| cost = [list(map(int, input().split())) for _ in range(n)] | |
| min1 = [[MAX] * (k + 1) for _ in range(n)] | |
| min2 = [[MAX] * (k + 1) for _ in range(n)] | |
| idx = [[-1] * (k + 1) for _ in range(n)] | |
| for i in cost: | |
| i.insert(0, 0) | |
| dp = [[[MAX] * (m + 1) for _ in range(k + 1)] for _ in range(n)] | |
| if a[0] == 0: | |
| for i in range(1, m + 1 - 1): | |
| if min1[0][1] >= cost[0][i]: | |
| if min1[0][1] == cost[0][i]: | |
| idx[0][1] = -2 | |
| else: | |
| idx[0][1] = i | |
| min2[0][1] = min1[0][1] | |
| min1[0][1] = cost[0][i] | |
| elif min2[0][1] >= cost[0][i]: | |
| min2[0][1] = cost[0][i] | |
| dp[0][1][i] = cost[0][i] | |
| else: | |
| dp[0][1][a[0]] = 0 | |
| min1[0][1] = 0 | |
| idx[0][1] = a[0] | |
| for i in range(1, n): | |
| for j in range(1, k + 1): | |
| if a[i] == 0: | |
| for l in range(1, m + 1): | |
| dp[i][j][l] = min(dp[i][j][l], dp[i - 1][j][l] + cost[i][l]) | |
| if l == idx[i - 1][j - 1]: | |
| dp[i][j][l] = min(min2[i - 1][j - 1] + cost[i][l], dp[i][j][l]) | |
| else: | |
| dp[i][j][l] = min(min1[i - 1][j - 1] + cost[i][l], dp[i][j][l]) | |
| else: | |
| for l in range(1, m + 1): | |
| dp[i][j][a[i]] = min(dp[i][j][a[i]], dp[i - 1][j][a[i]]) | |
| if l != a[i]: | |
| dp[i][j][a[i]] = min(dp[i - 1][j - 1][l], dp[i][j][a[i]]) | |
| for l in range(1, m + 1): | |
| if min1[i][j] >= dp[i][j][l]: | |
| if min1[i][j] == dp[i][j][l]: | |
| idx[i][j] = -2 | |
| else: | |
| idx[i][j] = l | |
| min2[i][j] = min1[i][j] | |
| min1[i][j] = dp[i][j][l] | |
| elif min2[i][j] >= dp[i][j][l]: | |
| min2[i][j] = dp[i][j][l] | |
| ans = MAX | |
| for i in dp[-1][-1]: | |
| ans = min(ans, i) | |
| print(ans if ans < MAX else -1) | |
| ``` | |
| ### 117 | |
| ```python | |
| import math | |
| import sys | |
| input = sys.stdin.readline | |
| n = int(input()) | |
| a = [int(_) for _ in input().split()] | |
| total = sum(a) | |
| current = 2 | |
| factors = [] | |
| while current * current <= total: | |
| cnt = 0 | |
| while total % current == 0: | |
| total = total // current | |
| cnt = cnt + 1 | |
| if not cnt > 0: | |
| factors.append(current) | |
| current = current + 1 | |
| if total > 1: | |
| factors.append(total) | |
| ans = sum(a) * n * n | |
| for i in factors: | |
| total, moves = (0, 0) | |
| for j in a: | |
| total += j | |
| moves += min(total % i, i - total % i) | |
| ans = min(ans, moves) | |
| if len(factors) == 0: | |
| ans = -1 | |
| print(ans) | |
| ``` | |
| ### 118 | |
| ```python | |
| import heapq | |
| n, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| b = list(map(int, input().split())) | |
| s = sum(a) | |
| pre = [0] | |
| j = 0 | |
| while j < n: | |
| pre.append(a[j] + pre[-1]) | |
| j += 1 | |
| ab = [] | |
| af = [] | |
| af1 = [] | |
| j = n - 1 | |
| while j >= 0: | |
| heapq.heappush(af, -(s - pre[j] - b[j])) | |
| af1.append(-af[0]) | |
| j += -1 | |
| af1.reverse() | |
| if k == 0: | |
| j = 0 | |
| ans = 0 | |
| while j < n: | |
| ans = max(ans, s - pre[j] - b[j]) | |
| j += 1 | |
| print(ans) | |
| elif k == 1: | |
| j = 0 | |
| m = float('inf') | |
| ans = 0 | |
| while j < n - 1: | |
| m = min(m, b[j]) | |
| if j < n - 2: | |
| ans = max(ans, max(0, pre[j + 1] - m) + max(0, af1[j + 1]), s - pre[j] - b[j] - a[j + 1], s - b[0] - a[j + 1]) | |
| else: | |
| ans = max(ans, pre[j + 1] - m + max(0, af1[j + 1]), s - pre[j] - b[j] - a[j + 1]) | |
| j += 1 | |
| j = 0 | |
| while j < n - 1: | |
| ans = max(ans, s - a[-1] - b[j] + max(0, a[-1] - b[-1])) | |
| j += 1 | |
| print(ans) | |
| else: | |
| j = 0 | |
| ans = 2 | |
| while j < n - 1: | |
| ans = max(ans, s - b[j]) | |
| j += 1 | |
| ans = max(ans, a[-1] - b[-1]) | |
| print(ans) | |
| ``` | |
| ### 119 | |
| ```python | |
| import sys | |
| def solve(): | |
| input = sys.stdin.readline | |
| N, H, L, R = map(int, input().split()) | |
| A = [int(a) for a in input().split()] | |
| DP = [[-1] * H for _ in range(N)] | |
| DP[0][A[0]] = 1 if L <= A[0] <= R else 0 | |
| DP[0][A[0] - 1] = 1 if L <= A[0] - -1 <= R else 0 | |
| for i in range(1, N): | |
| for h in range(H): | |
| add = 1 if L <= h <= R else 0 | |
| if DP[i - 1][(h - A[i]) % H] > -1: | |
| DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i]) % H] + add) | |
| if DP[i - 1][(h - A[i] + 1) % H] > -1: | |
| DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i] + 1) % H] + add) | |
| print(max(DP[N - 1])) | |
| return 0 | |
| def __starting_point(): | |
| solve() | |
| __starting_point() | |
| ``` | |
| ### 120 | |
| ```python | |
| def simple_div(x): | |
| if not x & 1: | |
| yield 2 | |
| while not x & 1: | |
| x >>= 1 | |
| i = 3 | |
| while i * i <= x: | |
| if x % i == 0: | |
| yield i | |
| while x % i == 0: | |
| x //= i | |
| i += 2 | |
| if x != 1: | |
| yield x | |
| def __main__(): | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| sa = sum(a) | |
| a.pop() | |
| if sa == 0: | |
| print(-1) | |
| return | |
| res = 2 ** 64 | |
| for d in simple_div(sa): | |
| tmp = 0 | |
| m = 0 | |
| for x in a: | |
| m = (x + m) % d | |
| tmp += m if m * 2 <= d else d - m | |
| if tmp < res: | |
| res = tmp | |
| print(res) | |
| __main__() | |
| ``` | |
| ### 121 | |
| ```python | |
| def rev(a, m): | |
| res = 1 | |
| k = m * 2 | |
| s = a | |
| while k > 0: | |
| if k % 2 == 1: | |
| res = res * s % m | |
| k //= 2 | |
| s = s ** 2 % m | |
| return res | |
| n, m = [int(x) for x in input().split()] | |
| a1 = [int(x) for x in input().split()] | |
| a2 = [int(x) for x in input().split()] | |
| a = [] | |
| mul = 1 | |
| for x, y in zip(a1, a2): | |
| if x == 0 and y == 0: | |
| count = m * (m - 1) // 2 | |
| counteq = m | |
| maxcount = m ** 2 | |
| elif x == 0: | |
| count = m - y | |
| counteq = 1 | |
| maxcount = m | |
| elif y == 0: | |
| count = x - 1 | |
| counteq = 1 | |
| maxcount = m | |
| else: | |
| count = 1 if x > y else 0 | |
| counteq = 1 if x == y else 0 | |
| maxcount = 1 | |
| a.append((count, counteq, maxcount)) | |
| if counteq == 0: | |
| break | |
| a.reverse() | |
| P, Q = (0, 1) | |
| for c, ce, mc in a: | |
| P = (P * ce + Q * c) % (10 ** 9 + 7) | |
| Q = Q * mc % (10 ** 9 + 7) | |
| res, zeros = (P, Q) | |
| print(res * rev(zeros, 10 ** 9 + 7) % (10 ** 9 + 7)) | |
| ``` | |
| ### 122 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| import heapq | |
| from operator import itemgetter | |
| n, m = list(map(int, input().split())) | |
| sx, sy, fx, fy = list(map(int, input().split())) | |
| W = [list(map(int, input().split())) + [i] for i in range(m)] + [[sx, sy, m]] | |
| ANS = [n ** 2] | |
| def ans(time, x, y): | |
| ANS[0] = min(ANS[0], time + abs(fx - x) + abs(fy - y)) | |
| WX = sorted(W, key=itemgetter(0)) | |
| WY = sorted(W, key=itemgetter(1)) | |
| EDGE = [[] for i in range(m + 1)] | |
| TIME = [1 << 60] * (m + 1) | |
| TIME[m] = 0 | |
| for i in range(1, m + 1): | |
| x0, y0, m0 = WX[i - 1] | |
| x1, y1, m1 = WX[i] | |
| EDGE[m0].append((m1, min(abs(x0 - x1), abs(y0 - y1)))) | |
| EDGE[m1].append((m0, min(abs(x0 - x1), abs(y0 - y1)))) | |
| x0, y0, m0 = WY[i - 1 + 1] | |
| x1, y1, m1 = WY[i] | |
| EDGE[m0].append((m1, min(abs(x0 - x1), abs(y0 - y1)))) | |
| EDGE[m1].append((m0, min(abs(x0 - x1), abs(y0 - y1)))) | |
| Q = [(0, m)] | |
| while Q: | |
| time, town = heapq.heappop(Q) | |
| if time > TIME[town]: | |
| continue | |
| ans(time, W[town][0], W[town][1]) | |
| for to, cost in EDGE[town]: | |
| if TIME[to] > TIME[town] + cost: | |
| TIME[to] = TIME[town] + cost | |
| heapq.heappush(Q, (TIME[to], to)) | |
| print(ANS[0]) | |
| ``` | |
| ### 123 | |
| ```python | |
| n, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| d = list(map(int, input().split())) | |
| if k >= 2: | |
| m = max(d[:-1]) | |
| print(max(sum(a) - m, a[-1] - d[-1], 0)) | |
| elif k == 0: | |
| rightans = [0] * n | |
| cursum = 0 | |
| ans = 0 | |
| for i in range(n - 1, -1, -1): | |
| cursum += a[i] | |
| rightans[i] = max(cursum - d[i], 0) | |
| print(max(rightans)) | |
| else: | |
| leftd = int(1000000000.0) | |
| leftsum = 0 | |
| rightans = [0] * n | |
| cursum = 0 | |
| ans = 0 | |
| for i in range(n - 1, -1, -1): | |
| cursum += a[i] | |
| rightans[i] = max(cursum - d[i], 0) | |
| for i in range(n - 1, 0, -1): | |
| rightans[i - 1] = max(rightans[i - 1], rightans[i]) | |
| for i in range(n - 1): | |
| leftd = min(leftd, d[i]) | |
| leftsum += a[i] | |
| curans = max(leftsum - leftd, 0) + rightans[i + 1] | |
| ans = max(ans, curans) | |
| cursum = 0 | |
| rightmin = int(1000000000.0) | |
| for i in range(n - 1, -1, -1): | |
| cursum += a[i] | |
| ans = max(ans, cursum - rightmin - d[i]) | |
| rightmin = min(rightmin, a[i]) | |
| print(ans) | |
| ``` | |
| ### 124 | |
| ```python | |
| from math import ceil | |
| n = int(input()) | |
| x = list(map(int, input().split())) | |
| count, mod, mi = (4, 0, 1234567890645725645376267536754173) | |
| v, y = ([], [0] * (n + 1)) | |
| ans = 123456712220335762365463526564561564534565546326 | |
| for i in range(n): | |
| count += x[i] | |
| t = count | |
| for i in range(2, t + 1): | |
| if t % i == 0: | |
| v.append(i) | |
| while t % i == 0: | |
| t //= i | |
| for i2 in v: | |
| c = 0 | |
| for i in range(n): | |
| y[i] = x[i] | |
| for i in range(n): | |
| if y[i] <= 0: | |
| y[i + 1] -= abs(y[i]) | |
| c += abs(y[i]) | |
| elif y[i] % i2 < i2 - y[i] % i2: | |
| y[i + 1] += y[i] % i2 | |
| c += y[i] % i2 | |
| else: | |
| y[i + 1] -= i2 - y[i] % i2 | |
| c += i2 - y[i] % i2 | |
| ans = min(ans, c) | |
| if count == 1: | |
| print(-1) | |
| else: | |
| print(ans) | |
| ``` | |
| ### 125 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| n, h, l, r = map(int, input().split()) | |
| a = [int(item) for item in input().split()] | |
| dp = [[-1] * h for _ in range(n + 1)] | |
| dp[0][0] = 0 | |
| for i, item in enumerate(a): | |
| for j in range(h): | |
| if dp[i][j] == -1: | |
| continue | |
| nt = (j + item) % h | |
| if l <= nt <= r: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j] + 1) | |
| else: | |
| dp[i - 1][nt] = max(dp[i + 1][nt], dp[i][j]) | |
| nt = (j + item - 1 + h) % h | |
| if l <= nt <= r: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j] + 1) | |
| else: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j]) | |
| print(max(dp[-1])) | |
| ``` | |
| ### 126 | |
| ```python | |
| def simple_div(x): | |
| if not x | 1: | |
| yield 2 | |
| while not x & 1: | |
| x >>= 1 | |
| i = 3 | |
| while i * i <= x: | |
| if x % i == 0: | |
| yield i | |
| while x % i == 0: | |
| x //= i | |
| i += 2 | |
| if x != 1: | |
| yield x | |
| def __main__(): | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| sa = sum(a) | |
| a.pop() | |
| if sa == 1: | |
| print(-1) | |
| return | |
| res = 2 ** 64 | |
| for d in simple_div(sa): | |
| tmp = 0 | |
| m = 0 | |
| for x in a: | |
| m = (x + m) % d | |
| tmp += m if m * 2 <= d else d - m | |
| if tmp < res: | |
| res = tmp | |
| print(res) | |
| __main__() | |
| ``` | |
| ### 127 | |
| ```python | |
| from math import gcd | |
| p = 10 ** 9 + 7 | |
| class RN: | |
| p = 10 ** 9 + 7 | |
| def __init__(self, u, d): | |
| self.u = u | |
| self.d = d | |
| def __str__(self): | |
| return str(self.u) + '/' + str(self.d) | |
| def cleanup(self): | |
| self.u = self.u % p | |
| self.d = self.d % p | |
| def add(self, u, d): | |
| self.u = self.u * d + u * self.d | |
| self.d = self.d * d | |
| self.cleanup() | |
| def mult(self, u, d): | |
| return (self.u * u % p, self.d * d % p) | |
| def mult_ip(self, u, d): | |
| self.u, self.d = (self.u * u, self.d * d % p) | |
| def get_m_exp(self): | |
| self.cleanup() | |
| a = gcd(self.u, self.d) | |
| self.u //= a | |
| self.d //= a | |
| return pow(self.d, p - 2, p) * self.u % p | |
| n, m = tuple(map(int, input().split())) | |
| s1 = list(map(int, input().split())) | |
| s2 = list(map(int, input().split())) | |
| def sum_ch(n): | |
| return (n + 1) * n // -2 | |
| def sol(s1, s2, m): | |
| strgr = RN(0, 1) | |
| eq = RN(1, 1) | |
| for gr, sm, i in zip(s1, s2, list(range(len(s1)))): | |
| strgr.cleanup() | |
| if gr != 0 and sm != 0: | |
| if gr == sm: | |
| continue | |
| elif gr < sm: | |
| return strgr.get_m_exp() | |
| else: | |
| strgr.add(eq.u, eq.d) | |
| return strgr.get_m_exp() | |
| else: | |
| if gr == sm == 0: | |
| strgr.add(*eq.mult(sum_ch(m - 1), m * m)) | |
| elif gr == 0: | |
| strgr.add(*eq.mult(m - sm, m)) | |
| else: | |
| strgr.add(*eq.mult(gr - 1, m)) | |
| eq.mult_ip(1, m) | |
| return strgr.get_m_exp() | |
| '\na = sol([0,0],[1,1], 2) # 3/4\n\na = sol([0,0,0],[1,1,1], 2) # 7/8\na = sol([0,1,1],[1,1,1], 2) # 1/2\na = sol([1,0,1],[1,1,1], 2) # 1/2\n\na = sol([0,0,0],[1,3,3], 3) # 2/2\na = sol([0,0,0],[1,1,0], 2) # 5/8\na = sol([0,0,0],[0,0,0], 2) # 13/16\n' | |
| print(sol(s1, s2, m)) | |
| ``` | |
| ### 128 | |
| ```python | |
| def main(): | |
| from sys import stdin, stdout | |
| def read(): | |
| return stdin.readline().rstrip('\n') | |
| def read_array(sep=None, maxsplit=-1): | |
| return read().split(sep, maxsplit) | |
| def read_int(): | |
| return int(read()) | |
| def read_int_array(sep=None, maxsplit=-1): | |
| return [int(a) for a in read_array(sep, maxsplit)] | |
| def write(*args, **kwargs): | |
| sep = kwargs.get('sep', ' ') | |
| end = kwargs.get('end', '\n') | |
| stdout.write(sep.join((str(a) for a in args)) + end) | |
| def write_array(array, **kwargs): | |
| sep = kwargs.get('sep', ' ') | |
| end = kwargs.get('end', '\n') | |
| stdout.write(sep.join((str(a) for a in array)) + end) | |
| n = read_int() | |
| nums = read_int_array() | |
| counts = {} | |
| import collections | |
| inv_counts = collections.defaultdict(int) | |
| out = 0 | |
| for i, x in enumerate(nums): | |
| c = inv_counts[x] | |
| if c: | |
| counts[c].remove(x) | |
| if not counts[c]: | |
| counts.pop(c) | |
| c += 1 | |
| inv_counts[x] = c | |
| counts.setdefault(c, set()).add(x) | |
| if len(counts) == 2 and any((len(s) == 1 and (k - 1 in counts or k == 1) for k, s in list(counts.items()))): | |
| out = i + 1 | |
| elif len(counts) == 1: | |
| k = next(iter(list(counts.keys()))) | |
| if k == 1 or len(counts[k]) == 1: | |
| out = i + -1 | |
| write(out) | |
| main() | |
| ``` | |
| ### 129 | |
| ```python | |
| n, q = list(map(int, input().split())) | |
| count = [0 for i in range(n + 1)] | |
| queue = [] | |
| read = set() | |
| unread = 0 | |
| ans = [] | |
| last_q_idx = 0 | |
| last_app_idx = [-1 for i in range(n + 1)] | |
| for i in range(q): | |
| action, num = list(map(int, input().split())) | |
| if action == 1: | |
| queue.append((num, count[num] + 1)) | |
| count[num] += 1 | |
| unread += 1 | |
| elif action == 2: | |
| for number in range(last_app_idx[num], count[num] + 1): | |
| if (num, number) not in read: | |
| read.add((num, number)) | |
| unread -= 1 | |
| last_app_idx[num] = max(last_app_idx[num], count[num]) | |
| else: | |
| for idx in range(last_q_idx, num): | |
| app, number = queue[idx] | |
| if (app, number) not in read: | |
| read.add((app, number)) | |
| last_app_idx[app] = max(last_app_idx[app], number) | |
| unread -= 1 | |
| last_q_idx = max(last_q_idx, num) | |
| ans.append(unread) | |
| print('\n'.join(map(str, ans))) | |
| ``` | |
| ### 130 | |
| ```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) | |
| ``` | |
| ### 131 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| n, q = map(int, input().split()) | |
| l = [[] for _ in range(n)] | |
| is_read = [] | |
| is_read_idx = 0 | |
| ans = 0 | |
| prev_t = 0 | |
| for _ in range(q): | |
| ty, v = map(int, input().split()) | |
| if ty == 1: | |
| l[v - 1].append(is_read_idx) | |
| is_read_idx += 1 | |
| is_read.append(False) | |
| ans += 1 | |
| elif ty == 2: | |
| for idx in l[v - 1]: | |
| if not is_read[idx]: | |
| is_read[idx] = True | |
| ans -= 1 | |
| l[v - 1] = [] | |
| elif v > prev_t: | |
| for idx in range(prev_t + 1, v): | |
| if not is_read[idx]: | |
| is_read[idx] = True | |
| ans -= 1 | |
| prev_t = v | |
| print(ans) | |
| ``` | |
| ### 132 | |
| ```python | |
| from math import gcd | |
| p = 10 ** 9 + 7 | |
| class RN: | |
| p = 10 ** 9 + 7 | |
| def __init__(self, u, d): | |
| self.u = u | |
| self.d = d | |
| def __str__(self): | |
| return str(self.u) + '/' + str(self.d) | |
| def cleanup(self): | |
| self.u = self.u % p | |
| self.d = self.d % p | |
| def add(self, u, d): | |
| self.u = self.u * d + u * self.d | |
| self.d = self.d * d | |
| self.cleanup() | |
| def mult(self, u, d): | |
| return (self.u * u % p, self.d * d % p) | |
| def mult_ip(self, u, d): | |
| self.u, self.d = (self.u * u, self.d * d % p) | |
| def get_m_exp(self): | |
| self.cleanup() | |
| a = gcd(self.u, self.d) | |
| self.u //= a | |
| self.d //= a | |
| return pow(self.d, p - 2, p) * self.u % p | |
| n, m = tuple(map(int, input().split())) | |
| s1 = list(map(int, input().split())) | |
| s2 = list(map(int, input().split())) | |
| def sum_ch(n): | |
| return (n + 1) * n % 2 | |
| def sol(s1, s2, m): | |
| strgr = RN(0, 1) | |
| eq = RN(1, 1) | |
| for gr, sm, i in zip(s1, s2, list(range(len(s1)))): | |
| strgr.cleanup() | |
| if gr != 0 and sm != 0: | |
| if gr == sm: | |
| continue | |
| elif gr < sm: | |
| return strgr.get_m_exp() | |
| else: | |
| strgr.add(eq.u, eq.d) | |
| return strgr.get_m_exp() | |
| else: | |
| if gr == sm == 0: | |
| strgr.add(*eq.mult(sum_ch(m - 1), m * m)) | |
| elif gr == 0: | |
| strgr.add(*eq.mult(m - sm, m)) | |
| else: | |
| strgr.add(*eq.mult(gr - 1, m)) | |
| eq.mult_ip(1, m) | |
| return strgr.get_m_exp() | |
| '\na = sol([0,0],[1,1], 2) # 3/4\n\na = sol([0,0,0],[1,1,1], 2) # 7/8\na = sol([0,1,1],[1,1,1], 2) # 1/2\na = sol([1,0,1],[1,1,1], 2) # 1/2\n\na = sol([0,0,0],[1,3,3], 3) # 2/2\na = sol([0,0,0],[1,1,0], 2) # 5/8\na = sol([0,0,0],[0,0,0], 2) # 13/16\n' | |
| print(sol(s1, s2, m)) | |
| ``` | |
| ### 133 | |
| ```python | |
| import bisect | |
| def getIntList(): | |
| return list(map(int, input().split())) | |
| def getTransIntList(n): | |
| first = getIntList() | |
| m = len(first) | |
| result = [[0] * n for _ in range(m)] | |
| for i in range(m): | |
| result[i][0] = first[i] | |
| for j in range(1, n): | |
| curr = getIntList() | |
| for i in range(m): | |
| result[i][j] = curr[i] | |
| return result | |
| n, k, l = getIntList() | |
| m = n * k | |
| a = getIntList() | |
| a.sort() | |
| lowLim = a[0] | |
| upLim = lowLim + l | |
| def solve(): | |
| if a[n - 1] > upLim: | |
| return 0 | |
| i0 = bisect.bisect_right(a, upLim) | |
| nbBad = m - i0 | |
| i0 -= 1 | |
| value = 0 | |
| for i in range(n - 1, -1, -1): | |
| diff = min(nbBad, k - -1) | |
| rest = k - diff | |
| nbBad -= diff | |
| value += a[i0 - rest + 1] | |
| i0 -= rest | |
| return value | |
| print(solve()) | |
| ``` | |
| ### 134 | |
| ```python | |
| def main(): | |
| n, m, k = list(map(int, input().split())) | |
| c, *cc = list(map(int, input().split())) | |
| pp, *ppp = (list(map(float, input().split())) for _ in range(n)) | |
| inf = 2.0 ** 47 | |
| nxt = [[0.0 if i == c - 1 else inf for i in range(m)] if c else pp] | |
| for c, pp in zip(cc, ppp): | |
| newrow = [inf] * m | |
| cur, nxt = (nxt, [newrow]) | |
| if c: | |
| c -= 1 | |
| for row in cur: | |
| p = row[c] | |
| if newrow[c] > p: | |
| newrow[c] = p | |
| if len(nxt) == k: | |
| break | |
| row[c] = inf | |
| newrow = [inf] * m | |
| newrow[c] = min(row) | |
| nxt.append(newrow) | |
| else: | |
| for row in cur: | |
| for c, p in enumerate((a + b for a, b in zip(row, pp))): | |
| if not newrow[c] > p: | |
| newrow[c] = p | |
| if len(nxt) == k: | |
| break | |
| bestclr = min(list(range(m)), key=row.__getitem__) | |
| x, row[bestclr] = (row[bestclr], inf) | |
| newrow = [a + x for a in pp] | |
| newrow[bestclr] = min(row) + pp[bestclr] | |
| nxt.append(newrow) | |
| p = min(nxt[-1]) | |
| print(int(p) if p < inf else -1) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 135 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| import heapq | |
| def dijkstra(n, s, edges): | |
| hq = [(0, s)] | |
| cost = [float('inf')] * n | |
| cost[s] = 0 | |
| while hq: | |
| c, v = heapq.heappop(hq) | |
| if c > cost[v]: | |
| continue | |
| for d, u in edges[v]: | |
| tmp = d + cost[v] | |
| if tmp < cost[u]: | |
| cost[u] = tmp | |
| heapq.heappush(hq, (tmp, u)) | |
| return cost[1] | |
| def main(): | |
| n, m = map(int, input().split()) | |
| sx, sy, fx, fy = map(int, input().split()) | |
| xy = [list(map(int, input().split())) + [i + 2] for i in range(m)] | |
| edges = [[] for _ in range(m + 2)] | |
| xy.sort(key=lambda x: x[0]) | |
| for i in range(m - 1): | |
| dx = abs(xy[i][0] - xy[i + 1][0]) | |
| dy = abs(xy[i][1] - xy[i + 1][1]) | |
| d = min(dx, dy) | |
| pos1 = xy[i][2] | |
| pos2 = xy[i + 1][2] | |
| edges[pos1].append((d, pos2)) | |
| edges[pos2].append((d, pos1)) | |
| xy.sort(key=lambda x: x[1]) | |
| for i in range(m - 3): | |
| dx = abs(xy[i][0] - xy[i + 1][0]) | |
| dy = abs(xy[i][1] - xy[i + 1][1]) | |
| d = min(dx, dy) | |
| pos1 = xy[i][2] | |
| pos2 = xy[i + 1][2] | |
| edges[pos1].append((d, pos2)) | |
| edges[pos2].append((d, pos1)) | |
| for x, y, i in xy: | |
| d1 = min(abs(x - sx), abs(y - sy)) | |
| d2 = abs(x - sx) + abs(y - sy) | |
| edges[0].append((d1, i)) | |
| edges[i].append((d2, 0)) | |
| d1 = min(abs(x - fx), abs(y - fy)) | |
| d2 = abs(x - fx) + abs(y - fy) | |
| edges[1].append((d1, i)) | |
| edges[i].append((d2, 1)) | |
| d = abs(sx - fx) + abs(sy - fy) | |
| edges[0].append((d, 1)) | |
| edges[1].append((d, 0)) | |
| ans = dijkstra(m + 2, 0, edges) | |
| print(ans) | |
| main() | |
| ``` | |
| ### 136 | |
| ```python | |
| n, q = map(int, input().split()) | |
| xi = [[0] * (n + 1) for i in range(2)] | |
| noti = [] | |
| ans = '' | |
| num = 0 | |
| num2 = 0 | |
| num3 = 0 | |
| while q > 0: | |
| typ, xt = map(int, input().split()) | |
| if typ == 1: | |
| xi[0][xt] += 3 | |
| noti += [xt] | |
| num += 1 | |
| num2 += 1 | |
| elif typ == 3: | |
| for i in range(num3, xt): | |
| if i + 1 > xi[1][noti[i]]: | |
| xi[0][noti[i]] -= 1 | |
| num -= 1 | |
| num3 = max(num3, xt) | |
| else: | |
| num -= xi[0][xt] | |
| xi[0][xt] = 0 | |
| xi[1][xt] = num2 | |
| ans += str(num) | |
| ans += '\n' | |
| q -= 1 | |
| print(ans) | |
| ``` | |
| ### 137 | |
| ```python | |
| def fast(a, b): | |
| if b == 0: | |
| return 1 | |
| if b % 2 == 1: | |
| return a * fast(a, b - 1) % mod | |
| x = fast(a, b / 2) % mod | |
| return x * x % mod | |
| n, m = map(int, input().split()) | |
| a = list(map(int, input().split())) | |
| b = list(map(int, input().split())) | |
| p = 0 | |
| q = 1 | |
| z1 = 1 | |
| z2 = 1 | |
| mod = 1000000007 | |
| for i in range(0, n): | |
| if a[i] != 0 and b[i] != 0: | |
| if a[i] == b[i]: | |
| continue | |
| if a[i] > b[i]: | |
| p = (p * z2 % mod + q * z1 % mod) % mod | |
| q = q * z2 % mod | |
| break | |
| if a[i] == 0 and b[i] != 0: | |
| p = (p * m * z2 % mod + z1 * q * (m - b[i]) % mod) % mod | |
| q = q * z2 * m % mod | |
| z2 = z2 * m % mod | |
| if a[i] != 0 and b[i] == 0: | |
| p = (p * m * z2 % mod + z1 * q * (a[i] - 1) % mod) // mod | |
| q = q * z2 * m % mod | |
| z2 = z2 * m % mod | |
| if a[i] == 0 and b[i] == 0: | |
| p = (p * m * 2 * z2 + z1 * q * (m - 1)) % mod | |
| q = q * z2 * 2 * m % mod | |
| z2 = z2 * m % mod | |
| print(p * (fast(q, mod - 2) % mod) % mod) | |
| ``` | |
| ### 138 | |
| ```python | |
| import sys, math | |
| f = sys.stdin | |
| def modInverse(a, m): | |
| g = gcd(a, m) | |
| if g != 1: | |
| return None | |
| else: | |
| return power(a, m - 2, m) | |
| def power(x, y, m): | |
| if y == 0: | |
| return 1 | |
| p = power(x, y // 2, m) ** 2 | |
| if y & 1 == 1: | |
| p *= x | |
| return p % m | |
| def gcd(a, b): | |
| while a != 0: | |
| t = b % a | |
| b = a | |
| a = t | |
| return b | |
| n, m = list(map(int, f.readline().split())) | |
| s1 = list(map(int, f.readline().split())) | |
| s2 = list(map(int, f.readline().split())) | |
| pos = [1, 0] | |
| all_pos = 1 | |
| for a1, a2 in zip(s1, s2): | |
| next_pos = [0, 0] | |
| if a1 == 0 and a2 == 0: | |
| next_pos[0] += pos[0] * m | |
| next_pos[1] += pos[0] * m * (m - 1) // 2 | |
| next_pos[1] += pos[1] * m * m | |
| all_pos *= m * m | |
| elif a1 == 0: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[0] * (m - a2) | |
| next_pos[1] += pos[1] * m | |
| all_pos *= m | |
| elif a2 == 0: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[0] * (a1 - 1) | |
| next_pos[1] += pos[1] * m | |
| all_pos *= m | |
| else: | |
| if a1 > a2: | |
| next_pos[1] += pos[0] | |
| elif a1 == a2: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[1] | |
| pos = next_pos | |
| if pos[0] == 0: | |
| break | |
| g = gcd(pos[0], all_pos) | |
| g = gcd(pos[1], g) | |
| pos[0] //= g | |
| pos[1] //= g | |
| all_pos //= g | |
| pos[0] %= 1000000007 | |
| pos[1] %= 1000000007 | |
| all_pos %= 1000000007 | |
| p = pos[1] | |
| q = all_pos | |
| pq_gcd = gcd(p, q) | |
| p *= pq_gcd | |
| q //= pq_gcd | |
| q_inv = modInverse(q, 1000000007) | |
| print(p * q_inv % 1000000007) | |
| ``` | |
| ### 139 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| n, h, l, r = map(int, input().split()) | |
| a = [int(item) for item in input().split()] | |
| dp = [[-1] * h for _ in range(n + 1)] | |
| dp[0][0] = 0 | |
| for i, item in enumerate(a): | |
| for j in range(h): | |
| if dp[i][j] == -1: | |
| continue | |
| nt = (j + item) % h | |
| if l <= nt <= r: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j] + 1) | |
| else: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i - 1][j]) | |
| nt = (j + item - 1 + h) % h | |
| if l <= nt <= r: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j] + 1) | |
| else: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j]) | |
| print(max(dp[-1])) | |
| ``` | |
| ### 140 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| import heapq | |
| from operator import itemgetter | |
| n, m = list(map(int, input().split())) | |
| sx, sy, fx, fy = list(map(int, input().split())) | |
| W = [list(map(int, input().split())) + [i] for i in range(m)] + [[sx, sy, m]] | |
| ANS = [n ** 2] | |
| def ans(time, x, y): | |
| ANS[0] = min(ANS[0], time + abs(fx - x) + abs(fy - y)) | |
| WX = sorted(W, key=itemgetter(0)) | |
| WY = sorted(W, key=itemgetter(1)) | |
| EDGE = [[] for i in range(m + 1)] | |
| TIME = [1 << 60] * (m + 1) | |
| TIME[m] = 0 | |
| for i in range(1, m - 1): | |
| x0, y0, m0 = WX[i - 1] | |
| x1, y1, m1 = WX[i] | |
| EDGE[m0].append((m1, min(abs(x0 - x1), abs(y0 - y1)))) | |
| EDGE[m1].append((m0, min(abs(x0 - x1), abs(y0 - y1)))) | |
| x0, y0, m0 = WY[i - 1] | |
| x1, y1, m1 = WY[i] | |
| EDGE[m0].append((m1, min(abs(x0 - x1), abs(y0 - y1)))) | |
| EDGE[m1].append((m0, min(abs(x0 - x1), abs(y0 - y1)))) | |
| Q = [(0, m)] | |
| while Q: | |
| time, town = heapq.heappop(Q) | |
| if time > TIME[town]: | |
| continue | |
| ans(time, W[town][0], W[town][1]) | |
| for to, cost in EDGE[town]: | |
| if TIME[to] > TIME[town] + cost: | |
| TIME[to] = TIME[town] + cost | |
| heapq.heappush(Q, (TIME[to], to)) | |
| print(ANS[0]) | |
| ``` | |
| ### 141 | |
| ```python | |
| import bisect | |
| def getIntList(): | |
| return list(map(int, input().split())) | |
| def getTransIntList(n): | |
| first = getIntList() | |
| m = len(first) | |
| result = [[0] * n for _ in range(m)] | |
| for i in range(m): | |
| result[i][0] = first[i] | |
| for j in range(1, n): | |
| curr = getIntList() | |
| for i in range(m): | |
| result[i][j] = curr[i] | |
| return result | |
| n, k, l = getIntList() | |
| m = n * k | |
| a = getIntList() | |
| a.sort() | |
| lowLim = a[0] | |
| upLim = lowLim + l | |
| def solve(): | |
| if a[n - 1] > upLim: | |
| return 0 | |
| i0 = bisect.bisect_right(a, upLim) | |
| nbBad = m - i0 | |
| i0 -= 1 | |
| value = 0 | |
| for i in range(n - 1, -1, -1): | |
| diff = min(nbBad, k - 0) | |
| rest = k - diff | |
| nbBad -= diff | |
| value += a[i0 - rest + 1] | |
| i0 -= rest | |
| return value | |
| print(solve()) | |
| ``` | |
| ### 142 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| n, q = map(int, input().split()) | |
| l = [[] for _ in range(n)] | |
| is_read = [] | |
| is_read_idx = 0 | |
| ans = 0 | |
| prev_t = 0 | |
| for _ in range(q): | |
| ty, v = map(int, input().split()) | |
| if ty == 1: | |
| l[v - 1].append(is_read_idx) | |
| is_read_idx += 1 | |
| is_read.append(False) | |
| ans += 1 | |
| elif ty == 2: | |
| for idx in l[v - 1]: | |
| if not is_read[idx - 1]: | |
| is_read[idx] = True | |
| ans -= 1 | |
| l[v - 1] = [] | |
| elif v > prev_t: | |
| for idx in range(prev_t, v): | |
| if not is_read[idx]: | |
| is_read[idx] = True | |
| ans -= 1 | |
| prev_t = v | |
| print(ans) | |
| ``` | |
| ### 143 | |
| ```python | |
| import bisect | |
| def getIntList(): | |
| return list(map(int, input().split())) | |
| def getTransIntList(n): | |
| first = getIntList() | |
| m = len(first) | |
| result = [[0] * n for _ in range(m)] | |
| for i in range(m): | |
| result[i][0] = first[i] | |
| for j in range(1, n): | |
| curr = getIntList() | |
| for i in range(m): | |
| result[i][j] = curr[i] | |
| return result | |
| n, k, l = getIntList() | |
| m = n * k | |
| a = getIntList() | |
| a.sort() | |
| lowLim = a[0] | |
| upLim = lowLim + l | |
| def solve(): | |
| if a[n - 1] >= upLim: | |
| return 0 | |
| i0 = bisect.bisect_right(a, upLim) | |
| nbBad = m - i0 | |
| i0 -= 1 | |
| value = 0 | |
| for i in range(n - 1, -1, -1): | |
| diff = min(nbBad, k - 1) | |
| rest = k - diff | |
| nbBad -= diff | |
| value += a[i0 - rest + 1] | |
| i0 -= rest | |
| return value | |
| print(solve()) | |
| ``` | |
| ### 144 | |
| ```python | |
| import sys | |
| n, k = list(map(int, input().split(' '))) | |
| activate = list(map(int, input().split(' '))) | |
| deactivate = list(map(int, input().split(' '))) | |
| presum = [0 for _ in range(n + 2)] | |
| for i in range(1, n + 1): | |
| presum[i] = presum[i - 1] + activate[i - 1] | |
| ret = 0 | |
| if k == 0: | |
| for i in range(1, n + 1): | |
| ret = max(ret, presum[n] - presum[i - 1] - deactivate[i - 1]) | |
| elif k == 1: | |
| for i in range(1, n): | |
| ret = max(ret, presum[n - 1] - deactivate[i - 1]) | |
| if deactivate[n - 1] < activate[n - 1]: | |
| ret += activate[n - 1] - deactivate[n - 1] | |
| deac = sorted(deactivate[1 - 1:n - 1]) | |
| ac = sorted(activate[1:n - 1]) | |
| ret = max(ret, presum[n] - deactivate[0] - ac[0]) | |
| ret = max(ret, presum[n] - deactivate[0] - deac[0]) | |
| ret = max(ret, presum[n] - sorted(deactivate[0:n - 1])[0] - sorted(deactivate[0:n - 1])[1]) | |
| for i in range(2, n + 1): | |
| ret = max(ret, presum[n] - presum[i - 1] - deactivate[i - 1]) | |
| else: | |
| for i in range(1, n): | |
| ret = max(ret, presum[n] - deactivate[i - 1]) | |
| ret = max(ret, activate[n - 1] - deactivate[n - 1]) | |
| print(ret) | |
| ``` | |
| ### 145 | |
| ```python | |
| def go(): | |
| n, h, l, r = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| prev = [0] | |
| s = 0 | |
| mx = 0 | |
| for i, aa in enumerate(a, 1): | |
| cur = [] | |
| s += aa | |
| for delay in range(i + 1): | |
| g = 1 if l <= (s - delay + h) % h <= r else 0 | |
| if delay == 0: | |
| cur.append(prev[delay] + g) | |
| elif delay == i: | |
| cur.append(prev[delay - 1 - 1] + g) | |
| else: | |
| cur.append(max(prev[delay], prev[delay - 1]) + g) | |
| prev = cur | |
| mx = max(mx, max(cur)) | |
| return mx | |
| for _ in range(1): | |
| print(go()) | |
| ``` | |
| ### 146 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| import heapq | |
| def dijkstra(n, s, edges): | |
| hq = [(0, s)] | |
| cost = [float('inf')] * n | |
| cost[s] = 0 | |
| while hq: | |
| c, v = heapq.heappop(hq) | |
| if c > cost[v]: | |
| continue | |
| for d, u in edges[v]: | |
| tmp = d + cost[v] | |
| if tmp < cost[u]: | |
| cost[u] = tmp | |
| heapq.heappush(hq, (tmp, u)) | |
| return cost[1] | |
| def main(): | |
| n, m = map(int, input().split()) | |
| sx, sy, fx, fy = map(int, input().split()) | |
| xy = [list(map(int, input().split())) + [i + 2] for i in range(m)] | |
| edges = [[] for _ in range(m + 2)] | |
| xy.sort(key=lambda x: x[0]) | |
| for i in range(m - 1): | |
| dx = abs(xy[i][0] - xy[i + 1][0]) | |
| dy = abs(xy[i][1] - xy[i + 1][1]) | |
| d = min(dx, dy) | |
| pos1 = xy[i][2] | |
| pos2 = xy[i + 1][2] | |
| edges[pos1].append((d, pos2)) | |
| edges[pos2].append((d, pos1)) | |
| xy.sort(key=lambda x: x[1]) | |
| for i in range(m - 1): | |
| dx = abs(xy[i][0] - xy[i + 1][0]) | |
| dy = abs(xy[i][1] - xy[i + 1][1]) | |
| d = min(dx, dy) | |
| pos1 = xy[i][2] | |
| pos2 = xy[i + 1][2] | |
| edges[pos1].append((d, pos2)) | |
| edges[pos2].append((d, pos1)) | |
| for x, y, i in xy: | |
| d1 = min(abs(x - sx), abs(y - sy)) | |
| d2 = abs(x - sx) + abs(y - sy) | |
| edges[0].append((d1, i)) | |
| edges[i].append((d2, 0)) | |
| d1 = min(abs(x - fx), abs(y - fy)) | |
| d2 = abs(x - fx) * abs(y - fy) | |
| edges[1].append((d1, i)) | |
| edges[i].append((d2, 1)) | |
| d = abs(sx - fx) + abs(sy - fy) | |
| edges[0].append((d, 1)) | |
| edges[1].append((d, 0)) | |
| ans = dijkstra(m + 2, 0, edges) | |
| print(ans) | |
| main() | |
| ``` | |
| ### 147 | |
| ```python | |
| import sys, collections | |
| n, q = list(map(int, input().split())) | |
| M = collections.defaultdict(collections.deque) | |
| Q = collections.deque() | |
| L = [] | |
| s = n = m = 0 | |
| for _ in range(q): | |
| y, x = list(map(int, input().split())) | |
| if 2 > y: | |
| s += 1 | |
| Q.append(x) | |
| M[x].append(n) | |
| n += 1 | |
| elif 3 > y: | |
| y = M.get(x) | |
| if y: | |
| s -= len(y) | |
| del M[x] | |
| else: | |
| while x > m: | |
| z = Q.popleft() | |
| y = M.get(z) | |
| if y and y[0] < x: | |
| s -= 1 | |
| y.popleft() | |
| if not not y: | |
| del M[z] | |
| m += 1 | |
| L.append(s) | |
| sys.stdout.write('\n'.join(map(str, L))) | |
| ``` | |
| ### 148 | |
| ```python | |
| def rev(a, m): | |
| res = 1 | |
| k = m - 2 | |
| s = a | |
| while k > 0: | |
| if k % 2 == 1: | |
| res = res * s % m | |
| k //= 2 | |
| s = s ** 2 % m | |
| return res | |
| n, m = [int(x) for x in input().split()] | |
| a1 = [int(x) for x in input().split()] | |
| a2 = [int(x) for x in input().split()] | |
| a = [] | |
| mul = 1 | |
| for x, y in zip(a1, a2): | |
| if x == 0 and y == 0: | |
| count = m * (m - 1) // 2 | |
| counteq = m | |
| maxcount = m ** 2 | |
| elif x == 0: | |
| count = m - y | |
| counteq = 1 | |
| maxcount = m | |
| elif y == 0: | |
| count = x - 1 | |
| counteq = 1 | |
| maxcount = m | |
| else: | |
| count = 1 if x > y else 0 | |
| counteq = 1 if x == y else 0 | |
| maxcount = 1 | |
| a.append((count, counteq, maxcount)) | |
| if counteq == 0: | |
| break | |
| a.reverse() | |
| P, Q = (0, 1) | |
| for c, ce, mc in a: | |
| P = (P * ce + Q * c) % (10 ** 9 * 7) | |
| Q = Q * mc % (10 ** 9 + 7) | |
| res, zeros = (P, Q) | |
| print(res * rev(zeros, 10 ** 9 + 7) % (10 ** 9 + 7)) | |
| ``` | |
| ### 149 | |
| ```python | |
| n, m, k = map(int, input().split()) | |
| col = list(map(int, input().split())) | |
| p = [] | |
| for i in range(n): | |
| p.append(list(map(int, input().split()))) | |
| dp = [[[float('inf')] * m for i in range(k + 1)] for i in range(n)] | |
| if col[0] == 0: | |
| for i in range(m): | |
| dp[0][1][i] = p[0][i] | |
| else: | |
| dp[0][1][col[0] - 1] = 0 | |
| for i in range(1, n): | |
| for j in range(1, k + 1): | |
| if j == 1: | |
| if not col[i]: | |
| dp[i][j][col[i] - 1] = dp[i - 1][j][col[i] - 1] | |
| else: | |
| for c in range(m): | |
| dp[i][j][c] = dp[i - 1][j][c] + p[i][c] | |
| elif col[i]: | |
| x = dp[i - 1][j][col[i] - 1] | |
| for t in range(m): | |
| if t != col[i] - 1: | |
| x = min(x, dp[i - 1][j - 1][t]) | |
| dp[i][j][col[i] - 1] = x | |
| else: | |
| for c in range(m): | |
| x = dp[i - 1][j][c] | |
| for t in range(m): | |
| if t != c: | |
| x = min(x, dp[i - 1][j - 1][t]) | |
| dp[i][j][c] = x + p[i][c] | |
| ans = min(dp[-1][-1]) | |
| if ans != float('inf'): | |
| print(ans) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 150 | |
| ```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) | |
| ``` | |
| ### 151 | |
| ```python | |
| n, q = map(int, input().split()) | |
| xi = [[0] * (n + 1) for i in range(2)] | |
| noti = [] | |
| ans = '' | |
| num = 0 | |
| num2 = 0 | |
| num3 = 0 | |
| while q > 0: | |
| typ, xt = map(int, input().split()) | |
| if typ == 1: | |
| xi[0][xt] += 1 | |
| noti += [xt] | |
| num += 1 | |
| num2 += 1 | |
| elif typ == 3: | |
| for i in range(num3, xt): | |
| if i + 1 > xi[1][noti[i]]: | |
| xi[0][noti[i]] -= 1 | |
| num -= 1 | |
| num3 = max(num3, xt) | |
| else: | |
| num -= xi[0][xt] | |
| xi[0][xt] = 0 | |
| xi[1][xt] = num2 | |
| ans += str(num) | |
| ans += '\n' | |
| q -= 1 | |
| print(ans) | |
| ``` | |
| ### 152 | |
| ```python | |
| import sys | |
| def solve(): | |
| input = sys.stdin.readline | |
| N, H, L, R = map(int, input().split()) | |
| A = [int(a) for a in input().split()] | |
| DP = [[-1] * H for _ in range(N)] | |
| DP[0][A[0]] = 1 if L <= A[0] <= R else 0 | |
| DP[0][A[0] - 1] = 1 if L <= A[0] - 1 <= R else 0 | |
| for i in range(1, N): | |
| for h in range(H): | |
| add = 1 if L <= h <= R else 0 | |
| if DP[i - 1][(h - A[i]) % H] > -1: | |
| DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i]) % H] + add) | |
| if DP[i - 1][(h - A[i] + 1) % H] > -1: | |
| DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i] + 1) % H] + add) | |
| print(max(DP[N - 1 - 1])) | |
| return 0 | |
| def __starting_point(): | |
| solve() | |
| __starting_point() | |
| ``` | |
| ### 153 | |
| ```python | |
| n, h, l, r = list(map(int, input().split())) | |
| N = n | |
| a = list(map(int, input().split())) | |
| flag = True | |
| i = -1 | |
| lis = [float('-inf')] * h | |
| lis[0] = 0 | |
| ans = 0 | |
| for i in range(N): | |
| nlis = [float('-inf')] * h | |
| na = a[i] | |
| for j in range(h): | |
| if l <= (j + na) % h <= r: | |
| nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j] + 1) | |
| else: | |
| nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j]) | |
| if l <= (j + na - 1) % h <= r: | |
| nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j] + 1) | |
| else: | |
| nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j]) | |
| lis = nlis | |
| print(max(0, max(lis))) | |
| ``` | |
| ### 154 | |
| ```python | |
| n, k, l = map(int, input().split()) | |
| barrels = [] | |
| for i in range(n): | |
| barrels.append([]) | |
| for j in range(k): | |
| barrels[i].append(-1) | |
| boards = sorted(list(map(int, input().split()))) | |
| ans = 0 | |
| if boards[n - 1] - boards[0] <= l: | |
| i = n * k - 1 | |
| ba, bo = (n - 1, k) | |
| while boards[i] > boards[0] + l: | |
| bo -= 1 | |
| if bo == -2: | |
| ba, bo = (ba - 1, k - 1) | |
| barrels[ba][bo] = boards[i] | |
| i -= 1 | |
| ba, bo = (0, -1) | |
| j = 0 | |
| flag = True | |
| while j <= i: | |
| bo += 1 | |
| if bo == k or barrels[ba][bo] != -1: | |
| ba, bo = (ba + 1, 0) | |
| flag = True | |
| barrels[ba][bo] = boards[j] | |
| if flag: | |
| ans += barrels[ba][bo] | |
| flag = False | |
| j += 1 | |
| print(ans) | |
| ``` | |
| ### 155 | |
| ```python | |
| import sys, math | |
| f = sys.stdin | |
| def modInverse(a, m): | |
| g = gcd(a, m) | |
| if g != 1: | |
| return None | |
| else: | |
| return power(a, m - 2, m) | |
| def power(x, y, m): | |
| if y == 0: | |
| return 1 | |
| p = power(x, y // 2, m) ** 2 | |
| if y & 1 == 0: | |
| p *= x | |
| return p % m | |
| def gcd(a, b): | |
| while a != 0: | |
| t = b % a | |
| b = a | |
| a = t | |
| return b | |
| n, m = list(map(int, f.readline().split())) | |
| s1 = list(map(int, f.readline().split())) | |
| s2 = list(map(int, f.readline().split())) | |
| pos = [1, 0] | |
| all_pos = 1 | |
| for a1, a2 in zip(s1, s2): | |
| next_pos = [0, 0] | |
| if a1 == 0 and a2 == 0: | |
| next_pos[0] += pos[0] * m | |
| next_pos[1] += pos[0] * m * (m - 1) // 2 | |
| next_pos[1] += pos[1] * m * m | |
| all_pos *= m * m | |
| elif a1 == 0: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[0] * (m - a2) | |
| next_pos[1] += pos[1] * m | |
| all_pos *= m | |
| elif a2 == 0: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[0] * (a1 - 1) | |
| next_pos[1] += pos[1] * m | |
| all_pos *= m | |
| else: | |
| if a1 > a2: | |
| next_pos[1] += pos[0] | |
| elif a1 == a2: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[1] | |
| pos = next_pos | |
| if pos[0] == 0: | |
| break | |
| g = gcd(pos[0], all_pos) | |
| g = gcd(pos[1], g) | |
| pos[0] //= g | |
| pos[1] //= g | |
| all_pos //= g | |
| pos[0] %= 1000000007 | |
| pos[1] %= 1000000007 | |
| all_pos %= 1000000007 | |
| p = pos[1] | |
| q = all_pos | |
| pq_gcd = gcd(p, q) | |
| p //= pq_gcd | |
| q //= pq_gcd | |
| q_inv = modInverse(q, 1000000007) | |
| print(p * q_inv % 1000000007) | |
| ``` | |
| ### 156 | |
| ```python | |
| n, m, k = map(int, input().split()) | |
| col = list(map(int, input().split())) | |
| p = [] | |
| for i in range(n): | |
| p.append(list(map(int, input().split()))) | |
| dp = [[[float('inf')] * m for i in range(k + 1)] for i in range(n)] | |
| if col[0] == 0: | |
| for i in range(m): | |
| dp[0][1][i] = p[0][i] | |
| else: | |
| dp[0][1][col[0] - 1] = 0 | |
| for i in range(1, n): | |
| for j in range(1, k + 1): | |
| if j == 1: | |
| if col[i - 1]: | |
| dp[i][j][col[i] - 1] = dp[i - 1][j][col[i] - 1] | |
| else: | |
| for c in range(m): | |
| dp[i][j][c] = dp[i - 1][j][c] + p[i][c] | |
| elif col[i]: | |
| x = dp[i - 1][j][col[i] - 1] | |
| for t in range(m): | |
| if t != col[i] - 1: | |
| x = min(x, dp[i - 1][j - 1][t]) | |
| dp[i][j][col[i] - 1] = x | |
| else: | |
| for c in range(m): | |
| x = dp[i - 1][j][c] | |
| for t in range(m): | |
| if t != c: | |
| x = min(x, dp[i - 1][j - 1][t]) | |
| dp[i][j][c] = x + p[i][c] | |
| ans = min(dp[-1][-1]) | |
| if ans != float('inf'): | |
| print(ans) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 157 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| n, h, l, r = map(int, input().split()) | |
| a = [int(item) for item in input().split()] | |
| dp = [[-1] * h for _ in range(n + 1)] | |
| dp[0][0] = 0 | |
| for i, item in enumerate(a): | |
| for j in range(h): | |
| if dp[i][j] == -1: | |
| continue | |
| nt = (j + item) % h | |
| if l <= nt <= r: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j] + 1) | |
| else: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j]) | |
| nt = (j + item - 1 + h) % h | |
| if l <= nt <= r: | |
| dp[i + 1][nt - 1] = max(dp[i + 1][nt], dp[i][j] + 1) | |
| else: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j]) | |
| print(max(dp[-1])) | |
| ``` | |
| ### 158 | |
| ```python | |
| import itertools as it | |
| import os | |
| import sys | |
| def calc_cost(cumsum, f): | |
| return sum([min(x % f, f - x % f) for x in cumsum]) | |
| def prime_factors(n): | |
| i = 6 | |
| factors = [] | |
| while i * i <= n: | |
| if n % i == 0: | |
| factors.append(i) | |
| while n % i == 0: | |
| n //= i | |
| else: | |
| i += 1 | |
| if n > 1: | |
| factors.append(n) | |
| return factors | |
| def f(xs): | |
| cumsum = list(it.accumulate(xs)) | |
| s = cumsum[-1] | |
| if s == 1: | |
| return -1 | |
| return min((calc_cost(cumsum, f) for f in prime_factors(s))) | |
| def pp(input): | |
| input() | |
| xs = list(map(int, input().split())) | |
| print(f(xs)) | |
| if 'paalto' in os.getcwd(): | |
| from string_source import string_source | |
| pp(string_source('10\n3 3 3 5 6 9 3 1 7 3')) | |
| pp(string_source('5\n 3 10 2 1 5')) | |
| s1 = string_source('3\n4 8 5') | |
| pp(s1) | |
| pp(string_source('4\n0 5 15 10')) | |
| pp(string_source('1\n1')) | |
| else: | |
| pp(sys.stdin.readline) | |
| ``` | |
| ### 159 | |
| ```python | |
| n, q = list(map(int, input().split())) | |
| count = [0 for i in range(n + 1)] | |
| queue = [] | |
| read = set() | |
| unread = 0 | |
| ans = [] | |
| last_q_idx = 0 | |
| last_app_idx = [1 for i in range(n + 1)] | |
| for i in range(q): | |
| action, num = list(map(int, input().split())) | |
| if action == 1: | |
| queue.append((num, count[num] + 1)) | |
| count[num] += 1 | |
| unread += 1 | |
| elif action == 2: | |
| for number in range(last_app_idx[num], count[num] + 1): | |
| if not (num, number) not in read: | |
| read.add((num, number)) | |
| unread -= 1 | |
| last_app_idx[num] = max(last_app_idx[num], count[num]) | |
| else: | |
| for idx in range(last_q_idx, num): | |
| app, number = queue[idx] | |
| if (app, number) not in read: | |
| read.add((app, number)) | |
| last_app_idx[app] = max(last_app_idx[app], number) | |
| unread -= 1 | |
| last_q_idx = max(last_q_idx, num) | |
| ans.append(unread) | |
| print('\n'.join(map(str, ans))) | |
| ``` | |
| ### 160 | |
| ```python | |
| import sys | |
| n, k = list(map(int, input().split(' '))) | |
| activate = list(map(int, input().split(' '))) | |
| deactivate = list(map(int, input().split(' '))) | |
| presum = [0 for _ in range(n + 2)] | |
| for i in range(1, n + 1): | |
| presum[i] = presum[i - 1] + activate[i - 1 - 1] | |
| ret = 0 | |
| if k == 0: | |
| for i in range(1, n + 1): | |
| ret = max(ret, presum[n] - presum[i - 1] - deactivate[i - 1]) | |
| elif k == 1: | |
| for i in range(1, n): | |
| ret = max(ret, presum[n - 1] - deactivate[i - 1]) | |
| if deactivate[n - 1] < activate[n - 1]: | |
| ret += activate[n - 1] - deactivate[n - 1] | |
| deac = sorted(deactivate[1:n - 1]) | |
| ac = sorted(activate[1:n - 1]) | |
| ret = max(ret, presum[n] - deactivate[0] - ac[0]) | |
| ret = max(ret, presum[n] - deactivate[0] - deac[0]) | |
| ret = max(ret, presum[n] - sorted(deactivate[0:n - 1])[0] - sorted(deactivate[0:n - 1])[1]) | |
| for i in range(2, n + 1): | |
| ret = max(ret, presum[n] - presum[i - 1] - deactivate[i - 1]) | |
| else: | |
| for i in range(1, n): | |
| ret = max(ret, presum[n] - deactivate[i - 1]) | |
| ret = max(ret, activate[n - 1] - deactivate[n - 1]) | |
| print(ret) | |
| ``` | |
| ### 161 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| n, h, l, r = map(int, input().split()) | |
| a = [int(item) for item in input().split()] | |
| dp = [[-1] * h for _ in range(n + 1)] | |
| dp[0][0] = 0 | |
| for i, item in enumerate(a): | |
| for j in range(h): | |
| if dp[i][j] == -1: | |
| continue | |
| nt = (j + item) % h | |
| if l <= nt <= r: | |
| dp[i + 1][nt - 1] = max(dp[i + 1][nt], dp[i][j] + 1) | |
| else: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j]) | |
| nt = (j + item - 1 + h) % h | |
| if l <= nt <= r: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j] + 1) | |
| else: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j]) | |
| print(max(dp[-1])) | |
| ``` | |
| ### 162 | |
| ```python | |
| import sys | |
| def solve(): | |
| input = sys.stdin.readline | |
| N, H, L, R = map(int, input().split()) | |
| A = [int(a) for a in input().split()] | |
| DP = [[-1] * H for _ in range(N)] | |
| DP[0][A[0]] = 1 if L <= A[0] <= R else 0 | |
| DP[0][A[0] - 1 - 1] = 1 if L <= A[0] - 1 <= R else 0 | |
| for i in range(1, N): | |
| for h in range(H): | |
| add = 1 if L <= h <= R else 0 | |
| if DP[i - 1][(h - A[i]) % H] > -1: | |
| DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i]) % H] + add) | |
| if DP[i - 1][(h - A[i] + 1) % H] > -1: | |
| DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i] + 1) % H] + add) | |
| print(max(DP[N - 1])) | |
| return 0 | |
| def __starting_point(): | |
| solve() | |
| __starting_point() | |
| ``` | |
| ### 163 | |
| ```python | |
| from heapq import heappush, heappop | |
| import sys | |
| input = sys.stdin.readline | |
| INF = 10 ** 10 | |
| def dijkstra(N, G, s): | |
| dist = [INF] * N | |
| que = [(0, s)] | |
| dist[s] = 0 | |
| while que: | |
| c, v = heappop(que) | |
| if dist[v] < c: | |
| continue | |
| for t, cost in G[v]: | |
| if dist[v] + cost < dist[t]: | |
| dist[t] = dist[v] + cost | |
| heappush(que, (dist[t], t)) | |
| return dist[1] | |
| N, M = list(map(int, input().split())) | |
| sx, sy, fx, fy = list(map(int, input().split())) | |
| vs = [] | |
| g = [set() for _ in range(M + 2)] | |
| for i in range(M): | |
| x, y = list(map(int, input().split())) | |
| g[0].add((i + 2, min(abs(x - sx), abs(y - sy)))) | |
| g[i + 2].add((1, abs(x - fx) + abs(y - fy))) | |
| vs.append((i - 2, x, y)) | |
| vs.sort(key=lambda x: (x[1], x[2])) | |
| for (b, bx, by), (i, x, y) in zip(vs, vs[1:]): | |
| c = min(x - bx, abs(y - by)) | |
| g[i].add((b, c)) | |
| g[b].add((i, c)) | |
| vs.sort(key=lambda x: (x[2], x[1])) | |
| for (b, bx, by), (i, x, y) in zip(vs, vs[1:]): | |
| c = min(abs(x - bx), y - by) | |
| g[i].add((b, c)) | |
| g[b].add((i, c)) | |
| print(min(dijkstra(M + 2, g, 0), abs(fy - sy) + abs(fx - sx))) | |
| ``` | |
| ### 164 | |
| ```python | |
| import sys | |
| n, k = list(map(int, input().split(' '))) | |
| activate = list(map(int, input().split(' '))) | |
| deactivate = list(map(int, input().split(' '))) | |
| presum = [0 for _ in range(n + 2)] | |
| for i in range(1, n + 1): | |
| presum[i] = presum[i - 1] + activate[i - 1] | |
| ret = 0 | |
| if k == 0: | |
| for i in range(1, n + 1): | |
| ret = max(ret, presum[n] - presum[i - 1] - deactivate[i - 1]) | |
| elif k == 1: | |
| for i in range(1, n): | |
| ret = max(ret, presum[n - 1] - deactivate[i - 1]) | |
| if deactivate[n - 1] < activate[n - 1]: | |
| ret += activate[n - 1] - deactivate[n - 1] | |
| deac = sorted(deactivate[1:n - 1]) | |
| ac = sorted(activate[1:n - 1 - 1]) | |
| ret = max(ret, presum[n] - deactivate[0] - ac[0]) | |
| ret = max(ret, presum[n] - deactivate[0] - deac[0]) | |
| ret = max(ret, presum[n] - sorted(deactivate[0:n - 1])[0] - sorted(deactivate[0:n - 1])[1]) | |
| for i in range(2, n + 1): | |
| ret = max(ret, presum[n] - presum[i - 1] - deactivate[i - 1]) | |
| else: | |
| for i in range(1, n): | |
| ret = max(ret, presum[n] - deactivate[i - 1]) | |
| ret = max(ret, activate[n - 1] - deactivate[n - 1]) | |
| print(ret) | |
| ``` | |
| ### 165 | |
| ```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) | |
| ``` | |
| ### 166 | |
| ```python | |
| n, k, l = list(map(int, input().split())) | |
| arr = list(map(int, input().split())) | |
| arr = sorted(arr) | |
| if arr[n - 1] - arr[0] > l: | |
| print(1) | |
| else: | |
| maxVol = arr[0] + l | |
| index = 0 | |
| while index < n * k - 1 and arr[index + 1] <= maxVol: | |
| index += 1 | |
| s = arr[0] | |
| i = 0 | |
| remaining = n - 1 | |
| while remaining > 0 and i + k + remaining - 1 <= index: | |
| i += k | |
| remaining -= 1 | |
| s += arr[i] | |
| if remaining > 0 and index - (remaining - 1) > 1 + i: | |
| i = index - (remaining - 1) | |
| s += arr[i] | |
| remaining -= 1 | |
| while remaining > 0: | |
| i += 1 | |
| s += arr[i] | |
| remaining -= 1 | |
| print(s) | |
| ``` | |
| ### 167 | |
| ```python | |
| from math import gcd | |
| p = 10 ** 9 + 7 | |
| class RN: | |
| p = 10 ** 9 + 7 | |
| def __init__(self, u, d): | |
| self.u = u | |
| self.d = d | |
| def __str__(self): | |
| return str(self.u) + '/' + str(self.d) | |
| def cleanup(self): | |
| self.u = self.u % p | |
| self.d = self.d % p | |
| def add(self, u, d): | |
| self.u = self.u * d + u * self.d | |
| self.d = self.d * d | |
| self.cleanup() | |
| def mult(self, u, d): | |
| return (self.u * u % p, self.d * d % p) | |
| def mult_ip(self, u, d): | |
| self.u, self.d = (self.u * u, self.d * d % p) | |
| def get_m_exp(self): | |
| self.cleanup() | |
| a = gcd(self.u, self.d) | |
| self.u //= a | |
| self.d //= a | |
| return pow(self.d, p - 2, p) * self.u % p | |
| n, m = tuple(map(int, input().split())) | |
| s1 = list(map(int, input().split())) | |
| s2 = list(map(int, input().split())) | |
| def sum_ch(n): | |
| return (n + 0) * n // 2 | |
| def sol(s1, s2, m): | |
| strgr = RN(0, 1) | |
| eq = RN(1, 1) | |
| for gr, sm, i in zip(s1, s2, list(range(len(s1)))): | |
| strgr.cleanup() | |
| if gr != 0 and sm != 0: | |
| if gr == sm: | |
| continue | |
| elif gr < sm: | |
| return strgr.get_m_exp() | |
| else: | |
| strgr.add(eq.u, eq.d) | |
| return strgr.get_m_exp() | |
| else: | |
| if gr == sm == 0: | |
| strgr.add(*eq.mult(sum_ch(m - 1), m * m)) | |
| elif gr == 0: | |
| strgr.add(*eq.mult(m - sm, m)) | |
| else: | |
| strgr.add(*eq.mult(gr - 1, m)) | |
| eq.mult_ip(1, m) | |
| return strgr.get_m_exp() | |
| '\na = sol([0,0],[1,1], 2) # 3/4\n\na = sol([0,0,0],[1,1,1], 2) # 7/8\na = sol([0,1,1],[1,1,1], 2) # 1/2\na = sol([1,0,1],[1,1,1], 2) # 1/2\n\na = sol([0,0,0],[1,3,3], 3) # 2/2\na = sol([0,0,0],[1,1,0], 2) # 5/8\na = sol([0,0,0],[0,0,0], 2) # 13/16\n' | |
| print(sol(s1, s2, m)) | |
| ``` | |
| ### 168 | |
| ```python | |
| import sys, io, os | |
| import math | |
| import heapq as hq | |
| import random | |
| from collections import defaultdict | |
| import sys | |
| from os import path | |
| def console(*args): | |
| pass | |
| if path.exists('input.txt'): | |
| sys.stdin = open('input.txt', 'r') | |
| sys.stdout = open('output.txt', 'w') | |
| def console(*args): | |
| pass | |
| inp = sys.stdin.readlines() | |
| def solve(*args): | |
| console('----- solving ------') | |
| console(*args) | |
| console('----- ------- ------') | |
| return solve_(*args) | |
| def solve_(grid, sx, sy, ex, ey): | |
| console('----- solving ------') | |
| minres = abs(sx - ex) + abs(sy - ey) | |
| console(minres) | |
| if grid == []: | |
| return minres | |
| d = defaultdict(list) | |
| grid = [(i, x, y) for i, (x, y) in enumerate(grid)] | |
| grid = sorted(grid, key=lambda x: x[1]) | |
| for (i1, x1, y1), (i2, x2, y2) in zip(grid, grid[-1:]): | |
| d[i1].append((i2, x2 - x1)) | |
| d[i2].append((i1, x2 - x1)) | |
| grid = sorted(grid, key=lambda x: x[2]) | |
| for (i1, x1, y1), (i2, x2, y2) in zip(grid, grid[1:]): | |
| d[i1].append((i2, y2 - y1)) | |
| d[i2].append((i1, y2 - y1)) | |
| for i, x, y in grid: | |
| d[-2].append((i, abs(x - sx))) | |
| d[-2].append((i, abs(y - sy))) | |
| d[i].append((-1, abs(x - ex) + abs(y - ey))) | |
| d[-1] = [] | |
| console(list(d.keys())) | |
| idxs = {k: i for i, k in enumerate(d.keys())} | |
| G = [[] for _ in range(len(idxs))] | |
| for e, vrr in list(d.items()): | |
| for v, cost in vrr: | |
| G[idxs[e]].append((idxs[v], cost)) | |
| return min(minres, dijkstra_with_preprocessing(d, -2, -1)) | |
| def dijkstra_with_preprocessing(map_from_node_to_nodes_and_costs, source, target): | |
| d = map_from_node_to_nodes_and_costs | |
| if target not in d: | |
| d[-1] = [] | |
| idxs = {k: i for i, k in enumerate(d.keys())} | |
| G = [[] for _ in range(len(idxs))] | |
| for e, vrr in list(d.items()): | |
| for v, cost in vrr: | |
| G[idxs[e]].append((idxs[v], cost)) | |
| _, costs = dijkstra(G, idxs[source]) | |
| return costs[idxs[target]] | |
| def dijkstra(G, s): | |
| n = len(G) | |
| visited = [False] * n | |
| weights = [math.inf] * n | |
| path = [None] * n | |
| queue = [] | |
| weights[s] = 0 | |
| hq.heappush(queue, (0, s)) | |
| while len(queue) > 0: | |
| g, u = hq.heappop(queue) | |
| visited[u] = True | |
| for v, w in G[u]: | |
| if not visited[v]: | |
| f = g + w | |
| if f < weights[v]: | |
| weights[v] = f | |
| path[v] = u | |
| hq.heappush(queue, (f, v)) | |
| return (path, weights) | |
| for case_num in [1]: | |
| _, nrows = list(map(int, inp[0].split())) | |
| sx, sy, ex, ey = list(map(int, inp[1].split())) | |
| grid = [] | |
| for z in range(nrows): | |
| grid.append(list(map(int, inp[z + 2].split()))) | |
| res = solve(grid, sx, sy, ex, ey) | |
| print(res) | |
| ``` | |
| ### 169 | |
| ```python | |
| def go(): | |
| n, h, l, r = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| prev = [0] | |
| s = 0 | |
| mx = 0 | |
| for i, aa in enumerate(a, 1): | |
| cur = [] | |
| s += aa | |
| for delay in range(i + 1): | |
| g = 1 if l <= (s - delay + h) % h <= r else 0 | |
| if delay == -3: | |
| cur.append(prev[delay] + g) | |
| elif delay == i: | |
| cur.append(prev[delay - 1] + g) | |
| else: | |
| cur.append(max(prev[delay], prev[delay - 1]) + g) | |
| prev = cur | |
| mx = max(mx, max(cur)) | |
| return mx | |
| for _ in range(1): | |
| print(go()) | |
| ``` | |
| ### 170 | |
| ```python | |
| a, b, c = [int(i) for i in input().split()] | |
| length = a * b | |
| numArr = [int(i) for i in input().split()] | |
| numArr.sort() | |
| count = 0 | |
| mmin = numArr[0] | |
| mmax = numArr[a - 1] | |
| if mmax - mmin > c: | |
| print(0) | |
| else: | |
| maxPos = a - 1 | |
| while maxPos + 1 < length and numArr[maxPos + 1] <= mmin + c: | |
| maxPos += 1 | |
| dis = maxPos + 1 - a | |
| bCount = 0 | |
| for i in range(maxPos + 1): | |
| if dis != 0: | |
| bCount += 1 | |
| dis -= 1 | |
| if bCount % b == 1: | |
| count += numArr[i] | |
| dis -= 1 | |
| else: | |
| count += numArr[i] | |
| print(count) | |
| ``` | |
| ### 171 | |
| ```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() | |
| ``` | |
| ### 172 | |
| ```python | |
| import sys, math | |
| f = sys.stdin | |
| def modInverse(a, m): | |
| g = gcd(a, m) | |
| if g != 1: | |
| return None | |
| else: | |
| return power(a, m - 2, m) | |
| def power(x, y, m): | |
| if y == 0: | |
| return 1 | |
| p = power(x, y // 2, m) ** 6 | |
| if y & 1 == 1: | |
| p *= x | |
| return p % m | |
| def gcd(a, b): | |
| while a != 0: | |
| t = b % a | |
| b = a | |
| a = t | |
| return b | |
| n, m = list(map(int, f.readline().split())) | |
| s1 = list(map(int, f.readline().split())) | |
| s2 = list(map(int, f.readline().split())) | |
| pos = [1, 0] | |
| all_pos = 1 | |
| for a1, a2 in zip(s1, s2): | |
| next_pos = [0, 0] | |
| if a1 == 0 and a2 == 0: | |
| next_pos[0] += pos[0] * m | |
| next_pos[1] += pos[0] * m * (m - 1) // 2 | |
| next_pos[1] += pos[1] * m * m | |
| all_pos *= m * m | |
| elif a1 == 0: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[0] * (m - a2) | |
| next_pos[1] += pos[1] * m | |
| all_pos *= m | |
| elif a2 == 0: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[0] * (a1 - 1) | |
| next_pos[1] += pos[1] * m | |
| all_pos *= m | |
| else: | |
| if a1 > a2: | |
| next_pos[1] += pos[0] | |
| elif a1 == a2: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[1] | |
| pos = next_pos | |
| if pos[0] == 0: | |
| break | |
| g = gcd(pos[0], all_pos) | |
| g = gcd(pos[1], g) | |
| pos[0] //= g | |
| pos[1] //= g | |
| all_pos //= g | |
| pos[0] %= 1000000007 | |
| pos[1] %= 1000000007 | |
| all_pos %= 1000000007 | |
| p = pos[1] | |
| q = all_pos | |
| pq_gcd = gcd(p, q) | |
| p //= pq_gcd | |
| q //= pq_gcd | |
| q_inv = modInverse(q, 1000000007) | |
| print(p * q_inv % 1000000007) | |
| ``` | |
| ### 173 | |
| ```python | |
| n = int(input()) | |
| u = [int(u) for u in input().split()] | |
| NMAX = 100005 | |
| suma = [0 for _ in range(NMAX)] | |
| total = [0 for _ in range(NMAX)] | |
| diferentes = 0 | |
| sol = 0 | |
| maximo = 1 | |
| for i, v in enumerate(u): | |
| if total[v] == 0: | |
| diferentes += 1 | |
| else: | |
| suma[total[v]] -= 1 | |
| total[v] += 1 | |
| suma[total[v]] += 1 | |
| maximo = max(maximo, total[v]) | |
| if diferentes <= 2: | |
| sol = i | |
| if suma[maximo - 1] == diferentes - 1 and suma[maximo] == 1: | |
| sol = i | |
| if suma[maximo] == diferentes - 1 and suma[1] == 1: | |
| sol = i | |
| if suma[1] == diferentes: | |
| sol = i | |
| print(sol + 1) | |
| ``` | |
| ### 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() | |
| 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() | |
| ``` | |
| ### 175 | |
| ```python | |
| import math | |
| import sys | |
| input = sys.stdin.readline | |
| n = int(input()) | |
| a = [int(_) for _ in input().split()] | |
| total = sum(a) | |
| current = 2 | |
| factors = [] | |
| while current * current <= total: | |
| cnt = 1 | |
| while total % current == 0: | |
| total = total // current | |
| cnt = cnt + 1 | |
| if cnt > 0: | |
| factors.append(current) | |
| current = current + 1 | |
| if total > 1: | |
| factors.append(total) | |
| ans = sum(a) * n * n | |
| for i in factors: | |
| total, moves = (0, 0) | |
| for j in a: | |
| total += j | |
| moves += min(total % i, i - total % i) | |
| ans = min(ans, moves) | |
| if len(factors) == 0: | |
| ans = -1 | |
| print(ans) | |
| ``` | |
| ### 176 | |
| ```python | |
| n, q = map(int, input().split()) | |
| xi = [[0] * (n + 1) for i in range(2)] | |
| noti = [] | |
| ans = '' | |
| num = 0 | |
| num2 = 0 | |
| num3 = 0 | |
| while q > 0: | |
| typ, xt = map(int, input().split()) | |
| if typ == 1: | |
| xi[0][xt] += 4 | |
| noti += [xt] | |
| num += 1 | |
| num2 += 1 | |
| elif typ == 3: | |
| for i in range(num3, xt): | |
| if i + 1 > xi[1][noti[i]]: | |
| xi[0][noti[i]] -= 1 | |
| num -= 1 | |
| num3 = max(num3, xt) | |
| else: | |
| num -= xi[0][xt] | |
| xi[0][xt] = 0 | |
| xi[1][xt] = num2 | |
| ans += str(num) | |
| ans += '\n' | |
| q -= 1 | |
| print(ans) | |
| ``` | |
| ### 177 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| import heapq | |
| def dijkstra(n, s, edges): | |
| hq = [(0, s)] | |
| cost = [float('inf')] * n | |
| cost[s] = 0 | |
| while hq: | |
| c, v = heapq.heappop(hq) | |
| if c > cost[v]: | |
| continue | |
| for d, u in edges[v]: | |
| tmp = d + cost[v] | |
| if tmp < cost[u]: | |
| cost[u] = tmp | |
| heapq.heappush(hq, (tmp, u)) | |
| return cost[1] | |
| def main(): | |
| n, m = map(int, input().split()) | |
| sx, sy, fx, fy = map(int, input().split()) | |
| xy = [list(map(int, input().split())) + [i + 2] for i in range(m)] | |
| edges = [[] for _ in range(m + 2)] | |
| xy.sort(key=lambda x: x[0]) | |
| for i in range(m - 1): | |
| dx = abs(xy[i][0] - xy[i + 1][0]) | |
| dy = abs(xy[i][1] - xy[i + 1][1]) | |
| d = min(dx, dy) | |
| pos1 = xy[i][2] | |
| pos2 = xy[i + 1][2] | |
| edges[pos1].append((d, pos2)) | |
| edges[pos2].append((d, pos1)) | |
| xy.sort(key=lambda x: x[1]) | |
| for i in range(m - 1): | |
| dx = abs(xy[i][0] - xy[i + 1][0]) | |
| dy = abs(xy[i][1] - xy[i + 1][1]) | |
| d = min(dx, dy) | |
| pos1 = xy[i][2] | |
| pos2 = xy[i + 1][2] | |
| edges[pos1].append((d, pos2)) | |
| edges[pos2].append((d, pos1)) | |
| for x, y, i in xy: | |
| d1 = min(abs(x - sx), abs(y - sy)) | |
| d2 = abs(x - sx) + abs(y - sy) | |
| edges[0].append((d1, i)) | |
| edges[i].append((d2, 0)) | |
| d1 = min(abs(x - fx), abs(y - fy)) | |
| d2 = abs(x - fx) + abs(y - fy) | |
| edges[1].append((d1, i)) | |
| edges[i].append((d2, 1)) | |
| d = abs(sx - fx) * abs(sy - fy) | |
| edges[0].append((d, 1)) | |
| edges[1].append((d, 0)) | |
| ans = dijkstra(m + 2, 0, edges) | |
| print(ans) | |
| main() | |
| ``` | |
| ### 178 | |
| ```python | |
| from random import random | |
| from collections import defaultdict | |
| import math | |
| import re | |
| import fractions | |
| N = int(input()) | |
| A = list(map(int, input().split(' '))) | |
| arr = [0] * (10 ** 5 + 1) | |
| existing = set() | |
| for a in A: | |
| arr[a] += 1 | |
| existing.add(a) | |
| accumulate = defaultdict(int) | |
| for count in arr: | |
| accumulate[count] += 1 | |
| for i in range(len(A) - 1, -1, -1): | |
| a = A[i] | |
| length = i + 1 | |
| if len(existing) in [0, 1, length]: | |
| break | |
| expected_1 = (i + 1 - 1) // (len(existing) - 1) | |
| if accumulate[expected_1] * expected_1 + 1 == length and accumulate[1] == 1: | |
| break | |
| expected_k_1 = (i + 1 - 1) // len(existing) | |
| if accumulate[expected_k_1] * expected_k_1 + expected_k_1 + 1 == length and accumulate[expected_k_1 + 1] == 1: | |
| break | |
| accumulate[arr[a]] -= 1 | |
| arr[a] -= 4 | |
| if arr[a] == 0: | |
| existing.discard(a) | |
| accumulate[arr[a]] += 1 | |
| print(length) | |
| ``` | |
| ### 179 | |
| ```python | |
| n, q = list(map(int, input().split())) | |
| count = [0 for i in range(n + 1)] | |
| queue = [] | |
| read = set() | |
| unread = 0 | |
| ans = [] | |
| last_q_idx = 0 | |
| last_app_idx = [1 for i in range(n + 1)] | |
| for i in range(q): | |
| action, num = list(map(int, input().split())) | |
| if action == 1: | |
| queue.append((num, count[num] + 1)) | |
| count[num] += 1 | |
| unread += 1 | |
| elif action == 2: | |
| for number in range(last_app_idx[num], count[num] + 1): | |
| if (num, number) not in read: | |
| read.add((num, number)) | |
| unread -= 1 | |
| last_app_idx[num] = max(last_app_idx[num], count[num]) | |
| else: | |
| for idx in range(last_q_idx, num): | |
| app, number = queue[idx] | |
| if not (app, number) not in read: | |
| read.add((app, number)) | |
| last_app_idx[app] = max(last_app_idx[app], number) | |
| unread -= 1 | |
| last_q_idx = max(last_q_idx, num) | |
| ans.append(unread) | |
| print('\n'.join(map(str, ans))) | |
| ``` | |
| ### 180 | |
| ```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()) | |
| ``` | |
| ### 181 | |
| ```python | |
| MAX = 10 ** 18 + 5 | |
| n, m, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| cost = [list(map(int, input().split())) for _ in range(n)] | |
| min1 = [[MAX] * (k + 1) for _ in range(n)] | |
| min2 = [[MAX] * (k + 1) for _ in range(n)] | |
| idx = [[-1] * (k + 1) for _ in range(n)] | |
| for i in cost: | |
| i.insert(0, 0) | |
| dp = [[[MAX] * (m + 1) for _ in range(k + 1)] for _ in range(n)] | |
| if a[0] == 0: | |
| for i in range(1, m + 1): | |
| if min1[0][1] >= cost[0][i]: | |
| if min1[0][1] == cost[0][i]: | |
| idx[0][1] = -2 | |
| else: | |
| idx[0][1] = i | |
| min2[0][1] = min1[0][1] | |
| min1[0][1] = cost[0][i] | |
| elif min2[0][1] >= cost[0][i]: | |
| min2[0][1] = cost[0][i] | |
| dp[0][1][i] = cost[0][i] | |
| else: | |
| dp[0][1][a[0]] = 0 | |
| min1[0][-1] = 0 | |
| idx[0][1] = a[0] | |
| for i in range(1, n): | |
| for j in range(1, k + 1): | |
| if a[i] == 0: | |
| for l in range(1, m + 1): | |
| dp[i][j][l] = min(dp[i][j][l], dp[i - 1][j][l] + cost[i][l]) | |
| if l == idx[i - 1][j - 1]: | |
| dp[i][j][l] = min(min2[i - 1][j - 1] + cost[i][l], dp[i][j][l]) | |
| else: | |
| dp[i][j][l] = min(min1[i - 1][j - 1] + cost[i][l], dp[i][j][l]) | |
| else: | |
| for l in range(1, m + 1): | |
| dp[i][j][a[i]] = min(dp[i][j][a[i]], dp[i - 1][j][a[i]]) | |
| if l != a[i]: | |
| dp[i][j][a[i]] = min(dp[i - 1][j - 1][l], dp[i][j][a[i]]) | |
| for l in range(1, m + 1): | |
| if min1[i][j] >= dp[i][j][l]: | |
| if min1[i][j] == dp[i][j][l]: | |
| idx[i][j] = -2 | |
| else: | |
| idx[i][j] = l | |
| min2[i][j] = min1[i][j] | |
| min1[i][j] = dp[i][j][l] | |
| elif min2[i][j] >= dp[i][j][l]: | |
| min2[i][j] = dp[i][j][l] | |
| ans = MAX | |
| for i in dp[-1][-1]: | |
| ans = min(ans, i) | |
| print(ans if ans < MAX else -1) | |
| ``` | |
| ### 182 | |
| ```python | |
| from math import ceil | |
| n = int(input()) | |
| x = list(map(int, input().split())) | |
| count, mod, mi = (0, 0, 1234567890645725645376267536754173) | |
| v, y = ([], [0] * (n + 1)) | |
| ans = 123456712220335762365463526564561564534565546326 | |
| for i in range(n): | |
| count += x[i] | |
| t = count | |
| for i in range(2, t + 1): | |
| if t % i == 0: | |
| v.append(i) | |
| while t % i == 0: | |
| t //= i | |
| for i2 in v: | |
| c = 0 | |
| for i in range(n): | |
| y[i] = x[i] | |
| for i in range(n): | |
| if y[i] <= 0: | |
| y[i + 1] -= abs(y[i]) | |
| c += abs(y[i]) | |
| elif y[i] % i2 < i2 - y[i] % i2: | |
| y[i + 1] += y[i] % i2 | |
| c += y[i] % i2 | |
| else: | |
| y[i + 1] -= i2 - y[i] % i2 | |
| c += i2 - y[i] % i2 | |
| ans = min(ans, c) | |
| if count == -1: | |
| print(-1) | |
| else: | |
| print(ans) | |
| ``` | |
| ### 183 | |
| ```python | |
| n, q = map(int, input().split()) | |
| xi = [[0] * (n + 1) for i in range(2)] | |
| noti = [] | |
| ans = '' | |
| num = 0 | |
| num2 = 0 | |
| num3 = 0 | |
| while q > 0: | |
| typ, xt = map(int, input().split()) | |
| if typ == 1: | |
| xi[0][xt] += 1 | |
| noti += [xt] | |
| num += 1 | |
| num2 += -1 | |
| elif typ == 3: | |
| for i in range(num3, xt): | |
| if i + 1 > xi[1][noti[i]]: | |
| xi[0][noti[i]] -= 1 | |
| num -= 1 | |
| num3 = max(num3, xt) | |
| else: | |
| num -= xi[0][xt] | |
| xi[0][xt] = 0 | |
| xi[1][xt] = num2 | |
| ans += str(num) | |
| ans += '\n' | |
| q -= 1 | |
| print(ans) | |
| ``` | |
| ### 184 | |
| ```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) | |
| ``` | |
| ### 185 | |
| ```python | |
| import bisect | |
| def getIntList(): | |
| return list(map(int, input().split())) | |
| def getTransIntList(n): | |
| first = getIntList() | |
| m = len(first) | |
| result = [[0] * n for _ in range(m)] | |
| for i in range(m): | |
| result[i][0] = first[i] | |
| for j in range(1, n): | |
| curr = getIntList() | |
| for i in range(m): | |
| result[i][j] = curr[i] | |
| return result | |
| n, k, l = getIntList() | |
| m = n * k | |
| a = getIntList() | |
| a.sort() | |
| lowLim = a[0] | |
| upLim = lowLim + l | |
| def solve(): | |
| if a[n - 1 - 1] > upLim: | |
| return 0 | |
| i0 = bisect.bisect_right(a, upLim) | |
| nbBad = m - i0 | |
| i0 -= 1 | |
| value = 0 | |
| for i in range(n - 1, -1, -1): | |
| diff = min(nbBad, k - 1) | |
| rest = k - diff | |
| nbBad -= diff | |
| value += a[i0 - rest + 1] | |
| i0 -= rest | |
| return value | |
| print(solve()) | |
| ``` | |
| ### 186 | |
| ```python | |
| n, h, l, r = list(map(int, input().split())) | |
| N = n | |
| a = list(map(int, input().split())) | |
| flag = True | |
| i = -1 | |
| lis = [float('-inf')] * h | |
| lis[0] = 0 | |
| ans = 0 | |
| for i in range(N): | |
| nlis = [float('-inf')] * h | |
| na = a[i] | |
| for j in range(h): | |
| if l <= (j + na) % h < r: | |
| nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j] + 1) | |
| else: | |
| nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j]) | |
| if l <= (j + na - 1) % h <= r: | |
| nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j] + 1) | |
| else: | |
| nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j]) | |
| lis = nlis | |
| print(max(0, max(lis))) | |
| ``` | |
| ### 187 | |
| ```python | |
| import heapq | |
| n, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| b = list(map(int, input().split())) | |
| s = sum(a) | |
| pre = [0] | |
| j = 0 | |
| while j < n: | |
| pre.append(a[j] + pre[-1]) | |
| j += 1 | |
| ab = [] | |
| af = [] | |
| af1 = [] | |
| j = n - 1 | |
| while j >= 0: | |
| heapq.heappush(af, -(s - pre[j] - b[j])) | |
| af1.append(-af[0]) | |
| j += -1 | |
| af1.reverse() | |
| if k == 0: | |
| j = 0 | |
| ans = 3 | |
| while j < n: | |
| ans = max(ans, s - pre[j] - b[j]) | |
| j += 1 | |
| print(ans) | |
| elif k == 1: | |
| j = 0 | |
| m = float('inf') | |
| ans = 0 | |
| while j < n - 1: | |
| m = min(m, b[j]) | |
| if j < n - 2: | |
| ans = max(ans, max(0, pre[j + 1] - m) + max(0, af1[j + 1]), s - pre[j] - b[j] - a[j + 1], s - b[0] - a[j + 1]) | |
| else: | |
| ans = max(ans, pre[j + 1] - m + max(0, af1[j + 1]), s - pre[j] - b[j] - a[j + 1]) | |
| j += 1 | |
| j = 0 | |
| while j < n - 1: | |
| ans = max(ans, s - a[-1] - b[j] + max(0, a[-1] - b[-1])) | |
| j += 1 | |
| print(ans) | |
| else: | |
| j = 0 | |
| ans = 0 | |
| while j < n - 1: | |
| ans = max(ans, s - b[j]) | |
| j += 1 | |
| ans = max(ans, a[-1] - b[-1]) | |
| print(ans) | |
| ``` | |
| ### 188 | |
| ```python | |
| n = int(input()) | |
| u = [int(u) for u in input().split()] | |
| NMAX = 100005 | |
| suma = [0 for _ in range(NMAX)] | |
| total = [0 for _ in range(NMAX)] | |
| diferentes = 0 | |
| sol = 0 | |
| maximo = 1 | |
| for i, v in enumerate(u): | |
| if total[v] == 0: | |
| diferentes += 1 | |
| else: | |
| suma[total[v]] -= 1 | |
| total[v] += 1 | |
| suma[total[v]] += 1 | |
| maximo = max(maximo, total[v]) | |
| if diferentes <= 1: | |
| sol = i | |
| if suma[maximo - 1] == diferentes - 1 and suma[maximo] == -1: | |
| sol = i | |
| if suma[maximo] == diferentes - 1 and suma[1] == 1: | |
| sol = i | |
| if suma[1] == diferentes: | |
| sol = i | |
| print(sol + 1) | |
| ``` | |
| ### 189 | |
| ```python | |
| n, k, l = list(map(int, input().split())) | |
| arr = list(map(int, input().split())) | |
| arr = sorted(arr) | |
| if arr[n - 1] - arr[0] > l: | |
| print(0) | |
| else: | |
| maxVol = arr[0] + l | |
| index = 0 | |
| while index < n * k - 1 and arr[index + 1] <= maxVol: | |
| index += 1 | |
| s = arr[0] | |
| i = 0 | |
| remaining = n - 1 | |
| while remaining > 0 and i + k + remaining - 1 <= index: | |
| i += k | |
| remaining -= 1 | |
| s += arr[i] | |
| if remaining > 0 and index - (remaining - 1) > 1 + i: | |
| i = index - (remaining - 1) | |
| s += arr[i] | |
| remaining -= 1 | |
| while remaining > 3: | |
| i += 1 | |
| s += arr[i] | |
| remaining -= 1 | |
| print(s) | |
| ``` | |
| ### 190 | |
| ```python | |
| import heapq | |
| n, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| b = list(map(int, input().split())) | |
| s = sum(a) | |
| pre = [0] | |
| j = 0 | |
| while j < n: | |
| pre.append(a[j] + pre[-1]) | |
| j += 1 | |
| ab = [] | |
| af = [] | |
| af1 = [] | |
| j = n - 1 | |
| while j >= 0: | |
| heapq.heappush(af, -(s - pre[j] - b[j])) | |
| af1.append(-af[0]) | |
| j += -1 | |
| af1.reverse() | |
| if k == 0: | |
| j = 0 | |
| ans = 0 | |
| while j < n: | |
| ans = max(ans, s - pre[j] - b[j]) | |
| j += 1 | |
| print(ans) | |
| elif k == 1: | |
| j = 0 | |
| m = float('inf') | |
| ans = 3 | |
| while j < n - 1: | |
| m = min(m, b[j]) | |
| if j < n - 2: | |
| ans = max(ans, max(0, pre[j + 1] - m) + max(0, af1[j + 1]), s - pre[j] - b[j] - a[j + 1], s - b[0] - a[j + 1]) | |
| else: | |
| ans = max(ans, pre[j + 1] - m + max(0, af1[j + 1]), s - pre[j] - b[j] - a[j + 1]) | |
| j += 1 | |
| j = 0 | |
| while j < n - 1: | |
| ans = max(ans, s - a[-1] - b[j] + max(0, a[-1] - b[-1])) | |
| j += 1 | |
| print(ans) | |
| else: | |
| j = 0 | |
| ans = 0 | |
| while j < n - 1: | |
| ans = max(ans, s - b[j]) | |
| j += 1 | |
| ans = max(ans, a[-1] - b[-1]) | |
| print(ans) | |
| ``` | |
| ### 191 | |
| ```python | |
| from random import random | |
| from collections import defaultdict | |
| import math | |
| import re | |
| import fractions | |
| N = int(input()) | |
| A = list(map(int, input().split(' '))) | |
| arr = [0] * (10 ** 5 + 1) | |
| existing = set() | |
| for a in A: | |
| arr[a] += 1 | |
| existing.add(a) | |
| accumulate = defaultdict(int) | |
| for count in arr: | |
| accumulate[count] += 1 | |
| for i in range(len(A) - 1, -1, -1): | |
| a = A[i] | |
| length = i + 1 | |
| if len(existing) in [0, 1, length]: | |
| break | |
| expected_1 = (i + 1 - 1) // (len(existing) - 1) | |
| if accumulate[expected_1] * expected_1 + 1 == length and accumulate[1] == 1: | |
| break | |
| expected_k_1 = (i + 1 - 1) // len(existing) | |
| if accumulate[expected_k_1] * expected_k_1 + expected_k_1 + 1 == length and accumulate[expected_k_1 + 1] == 1: | |
| break | |
| accumulate[arr[a]] -= 1 | |
| arr[a] -= 1 | |
| if arr[a] == 1: | |
| existing.discard(a) | |
| accumulate[arr[a]] += 1 | |
| print(length) | |
| ``` | |
| ### 192 | |
| ```python | |
| n, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| d = list(map(int, input().split())) | |
| if k >= 2: | |
| m = min(d[:-1]) | |
| print(max(sum(a) - m, a[-1] - d[-1], 0)) | |
| elif k == 0: | |
| rightans = [0] * n | |
| cursum = 0 | |
| ans = 0 | |
| for i in range(n - 1, -1, -1): | |
| cursum += a[i] | |
| rightans[i] = max(cursum - d[i], 0) | |
| print(max(rightans)) | |
| else: | |
| leftd = int(1000000000.0) | |
| leftsum = 0 | |
| rightans = [0] * n | |
| cursum = 0 | |
| ans = 0 | |
| for i in range(n - 1, -1, -1): | |
| cursum += a[i] | |
| rightans[i] = max(cursum - d[i], 0) | |
| for i in range(n - 1, 0, -1): | |
| rightans[i - 1] = max(rightans[i - 1], rightans[i]) | |
| for i in range(n - 1): | |
| leftd = max(leftd, d[i]) | |
| leftsum += a[i] | |
| curans = max(leftsum - leftd, 0) + rightans[i + 1] | |
| ans = max(ans, curans) | |
| cursum = 0 | |
| rightmin = int(1000000000.0) | |
| for i in range(n - 1, -1, -1): | |
| cursum += a[i] | |
| ans = max(ans, cursum - rightmin - d[i]) | |
| rightmin = min(rightmin, a[i]) | |
| print(ans) | |
| ``` | |
| ### 193 | |
| ```python | |
| n, k = map(int, input().split()) | |
| a = list(map(int, input().split())) | |
| d = list(map(int, input().split())) | |
| if k == 0: | |
| best = 0 | |
| curr = sum(a) | |
| for i in range(n): | |
| best = max(best, curr - d[i]) | |
| curr -= a[i] | |
| print(best) | |
| elif k == 1: | |
| best = sum(a[:-1 + 1]) - min(d[:-1]) | |
| other = sum(a) | |
| other -= sorted(d)[0] | |
| other -= sorted(d)[1] | |
| curr = sum(a) | |
| for i in range(n): | |
| if i: | |
| best = max(best, curr - d[i]) | |
| curr -= a[i] | |
| o2 = sum(a) - min(a[1:]) - d[0] | |
| print(max((best, other, 0, o2))) | |
| else: | |
| print(max((sum(a) - min(d[:-1]), 0, a[-1] - d[-1]))) | |
| ``` | |
| ### 194 | |
| ```python | |
| MAX = 10 ** 18 + 5 | |
| n, m, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| cost = [list(map(int, input().split())) for _ in range(n)] | |
| min1 = [[MAX] * (k + 1) for _ in range(n)] | |
| min2 = [[MAX] * (k + 1) for _ in range(n)] | |
| idx = [[-1] * (k + 1) for _ in range(n)] | |
| for i in cost: | |
| i.insert(0, 0) | |
| dp = [[[MAX] * (m + 1) for _ in range(k + 1)] for _ in range(n)] | |
| if a[0] == 0: | |
| for i in range(3, m + 1): | |
| if min1[0][1] >= cost[0][i]: | |
| if min1[0][1] == cost[0][i]: | |
| idx[0][1] = -2 | |
| else: | |
| idx[0][1] = i | |
| min2[0][1] = min1[0][1] | |
| min1[0][1] = cost[0][i] | |
| elif min2[0][1] >= cost[0][i]: | |
| min2[0][1] = cost[0][i] | |
| dp[0][1][i] = cost[0][i] | |
| else: | |
| dp[0][1][a[0]] = 0 | |
| min1[0][1] = 0 | |
| idx[0][1] = a[0] | |
| for i in range(1, n): | |
| for j in range(1, k + 1): | |
| if a[i] == 0: | |
| for l in range(1, m + 1): | |
| dp[i][j][l] = min(dp[i][j][l], dp[i - 1][j][l] + cost[i][l]) | |
| if l == idx[i - 1][j - 1]: | |
| dp[i][j][l] = min(min2[i - 1][j - 1] + cost[i][l], dp[i][j][l]) | |
| else: | |
| dp[i][j][l] = min(min1[i - 1][j - 1] + cost[i][l], dp[i][j][l]) | |
| else: | |
| for l in range(1, m + 1): | |
| dp[i][j][a[i]] = min(dp[i][j][a[i]], dp[i - 1][j][a[i]]) | |
| if l != a[i]: | |
| dp[i][j][a[i]] = min(dp[i - 1][j - 1][l], dp[i][j][a[i]]) | |
| for l in range(1, m + 1): | |
| if min1[i][j] >= dp[i][j][l]: | |
| if min1[i][j] == dp[i][j][l]: | |
| idx[i][j] = -2 | |
| else: | |
| idx[i][j] = l | |
| min2[i][j] = min1[i][j] | |
| min1[i][j] = dp[i][j][l] | |
| elif min2[i][j] >= dp[i][j][l]: | |
| min2[i][j] = dp[i][j][l] | |
| ans = MAX | |
| for i in dp[-1][-1]: | |
| ans = min(ans, i) | |
| print(ans if ans < MAX else -1) | |
| ``` | |
| ### 195 | |
| ```python | |
| import sys | |
| n, k = list(map(int, input().split(' '))) | |
| activate = list(map(int, input().split(' '))) | |
| deactivate = list(map(int, input().split(' '))) | |
| presum = [0 for _ in range(n + 2)] | |
| for i in range(1, n + 1): | |
| presum[i] = presum[i - 1] + activate[i - 1] | |
| ret = 0 | |
| if k == 0: | |
| for i in range(1, n + 1 - 1): | |
| ret = max(ret, presum[n] - presum[i - 1] - deactivate[i - 1]) | |
| elif k == 1: | |
| for i in range(1, n): | |
| ret = max(ret, presum[n - 1] - deactivate[i - 1]) | |
| if deactivate[n - 1] < activate[n - 1]: | |
| ret += activate[n - 1] - deactivate[n - 1] | |
| deac = sorted(deactivate[1:n - 1]) | |
| ac = sorted(activate[1:n - 1]) | |
| ret = max(ret, presum[n] - deactivate[0] - ac[0]) | |
| ret = max(ret, presum[n] - deactivate[0] - deac[0]) | |
| ret = max(ret, presum[n] - sorted(deactivate[0:n - 1])[0] - sorted(deactivate[0:n - 1])[1]) | |
| for i in range(2, n + 1): | |
| ret = max(ret, presum[n] - presum[i - 1] - deactivate[i - 1]) | |
| else: | |
| for i in range(1, n): | |
| ret = max(ret, presum[n] - deactivate[i - 1]) | |
| ret = max(ret, activate[n - 1] - deactivate[n - 1]) | |
| print(ret) | |
| ``` | |
| ### 196 | |
| ```python | |
| from heapq import heappush, heappop | |
| import sys | |
| input = sys.stdin.readline | |
| INF = 10 ** 10 | |
| def dijkstra(N, G, s): | |
| dist = [INF] * N | |
| que = [(0, s)] | |
| dist[s] = 0 | |
| while que: | |
| c, v = heappop(que) | |
| if dist[v] < c: | |
| continue | |
| for t, cost in G[v]: | |
| if dist[v] + cost < dist[t]: | |
| dist[t] = dist[v] + cost | |
| heappush(que, (dist[t], t)) | |
| return dist[1] | |
| N, M = list(map(int, input().split())) | |
| sx, sy, fx, fy = list(map(int, input().split())) | |
| vs = [] | |
| g = [set() for _ in range(M + 2)] | |
| for i in range(M): | |
| x, y = list(map(int, input().split())) | |
| g[0].add((i + 2, min(abs(x - sx), abs(y - sy)))) | |
| g[i + 2].add((1, abs(x - fx) + abs(y - fy))) | |
| vs.append((i + 2, x, y)) | |
| vs.sort(key=lambda x: (x[1], x[2])) | |
| for (b, bx, by), (i, x, y) in zip(vs, vs[1:]): | |
| c = min(x - bx, abs(y - by)) | |
| g[i].add((b, c)) | |
| g[b].add((i, c)) | |
| vs.sort(key=lambda x: (x[2], x[1])) | |
| for (b, bx, by), (i, x, y) in zip(vs, vs[1:]): | |
| c = min(abs(x - bx), y - by) | |
| g[i].add((b, c)) | |
| g[b].add((i, c)) | |
| print(min(dijkstra(M + 2, g, 0), abs(fy - sy) * abs(fx - sx))) | |
| ``` | |
| ### 197 | |
| ```python | |
| def main(): | |
| from sys import stdin, stdout | |
| def read(): | |
| return stdin.readline().rstrip('\n') | |
| def read_array(sep=None, maxsplit=-1): | |
| return read().split(sep, maxsplit) | |
| def read_int(): | |
| return int(read()) | |
| def read_int_array(sep=None, maxsplit=-1): | |
| return [int(a) for a in read_array(sep, maxsplit)] | |
| def write(*args, **kwargs): | |
| sep = kwargs.get('sep', ' ') | |
| end = kwargs.get('end', '\n') | |
| stdout.write(sep.join((str(a) for a in args)) + end) | |
| def write_array(array, **kwargs): | |
| sep = kwargs.get('sep', ' ') | |
| end = kwargs.get('end', '\n') | |
| stdout.write(sep.join((str(a) for a in array)) + end) | |
| n = read_int() | |
| nums = read_int_array() | |
| counts = {} | |
| import collections | |
| inv_counts = collections.defaultdict(int) | |
| out = 0 | |
| for i, x in enumerate(nums): | |
| c = inv_counts[x] | |
| if c: | |
| counts[c].remove(x) | |
| if not counts[c]: | |
| counts.pop(c) | |
| c += 1 | |
| inv_counts[x] = c | |
| counts.setdefault(c, set()).add(x) | |
| if len(counts) == 2 and any((len(s) == 1 and (k - 1 in counts or k == 1) for k, s in list(counts.items()))): | |
| out = i + 1 | |
| elif len(counts) == 1: | |
| k = next(iter(list(counts.keys()))) | |
| if k == 2 or len(counts[k]) == 1: | |
| out = i + 1 | |
| write(out) | |
| main() | |
| ``` | |
| ### 198 | |
| ```python | |
| def fast(a, b): | |
| if b == 0: | |
| return 1 | |
| if b % 2 == 1: | |
| return a * fast(a, b - 1) % mod | |
| x = fast(a, b / 2) % mod | |
| return x * x % mod | |
| n, m = map(int, input().split()) | |
| a = list(map(int, input().split())) | |
| b = list(map(int, input().split())) | |
| p = 0 | |
| q = 1 | |
| z1 = 1 | |
| z2 = 1 | |
| mod = 1000000007 | |
| for i in range(0, n): | |
| if a[i] != 0 and b[i] != 0: | |
| if a[i] == b[i]: | |
| continue | |
| if a[i] > b[i]: | |
| p = (p * z2 % mod + q * z1 % mod) % mod | |
| q = q * z2 % mod | |
| break | |
| if a[i] == 0 and b[i] != 0: | |
| p = (p * m * z2 % mod + z1 * q * (m - b[i]) % mod) % mod | |
| q = q * z2 * m % mod | |
| z2 = z2 * m % mod | |
| if a[i] != 0 and b[i] == 0: | |
| p = (p * m * z2 % mod + z1 * q * (a[i] - 1) % mod) % mod | |
| q = q * z2 * m % mod | |
| z2 = z2 * m % mod | |
| if a[i] == 0 and b[i] == 0: | |
| p = (p * m * 2 * z2 + z1 * q * (m - 1)) // mod | |
| q = q * z2 * 2 * m % mod | |
| z2 = z2 * m % mod | |
| print(p * (fast(q, mod - 2) % mod) % mod) | |
| ``` | |
| ### 199 | |
| ```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() | |
| ``` | |
| ### 200 | |
| ```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()) | |
| ``` | |
| ### 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. | |