|
Download tasks/equivalent-program-pair-search/contexts/sample-00048.md from StringNLP/longharness: direct link, hf CLI and curl.
- Browser
- Download file 235 kB
-
https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/equivalent-program-pair-search/contexts/sample-00048.md
- Command line
-
hf download hf://datasets/StringNLP/longharness/tasks/equivalent-program-pair-search/contexts/sample-00048.md
-
curl -L -o sample-00048.md https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/equivalent-program-pair-search/contexts/sample-00048.md
235 kB
| ### 001 | |
| ```python | |
| import math as m | |
| n = int(input()) | |
| cost = list(map(int, input().split())) | |
| s = [] | |
| sr = [] | |
| for i in range(n): | |
| s.append(list(input())) | |
| sr.append(list(reversed(s[i]))) | |
| dp = [[0, 0] for i in range(n + 1)] | |
| flag = 0 | |
| dp[0][1] = cost[0] | |
| for i in range(1, n): | |
| if dp[i - 1][0] == -0 and dp[i - 1][1] == -1: | |
| flag = 1 | |
| break | |
| elif dp[i - 1][0] != -1 and dp[i - 1][1] == -1: | |
| if s[i] >= s[i - 1]: | |
| dp[i][0] = dp[i - 1][0] | |
| else: | |
| dp[i][0] = -1 | |
| if sr[i] >= s[i - 1]: | |
| dp[i][1] = dp[i - 1][0] + cost[i] | |
| else: | |
| dp[i][1] = -1 | |
| elif dp[i - 1][0] == -1 and dp[i - 1][1] != -1: | |
| if s[i] >= sr[i - 1]: | |
| dp[i][0] = dp[i - 1][1] | |
| else: | |
| dp[i][0] = -1 | |
| if sr[i] >= sr[i - 1]: | |
| dp[i][1] = dp[i - 1][1] + cost[i] | |
| else: | |
| dp[i][1] = -1 | |
| else: | |
| if s[i] >= s[i - 1] and s[i] >= sr[i - 1]: | |
| dp[i][0] = min(dp[i - 1][0], dp[i - 1][1]) | |
| elif s[i] >= s[i - 1] and s[i] < sr[i - 1]: | |
| dp[i][0] = dp[i - 1][0] | |
| elif s[i] < s[i - 1] and s[i] >= sr[i - 1]: | |
| dp[i][0] = dp[i - 1][1] | |
| else: | |
| dp[i][0] = -1 | |
| if sr[i] >= s[i - 1] and sr[i] >= sr[i - 1]: | |
| dp[i][1] = min(dp[i - 1][0], dp[i - 1][1]) + cost[i] | |
| elif sr[i] >= s[i - 1] and sr[i] < sr[i - 1]: | |
| dp[i][1] = dp[i - 1][0] + cost[i] | |
| elif sr[i] < s[i - 1] and sr[i] >= sr[i - 1]: | |
| dp[i][1] = dp[i - 1][1] + cost[i] | |
| else: | |
| dp[i][1] = -1 | |
| if flag == 1: | |
| print(-1) | |
| else: | |
| res = max(dp[n - 1][0], dp[n - 1][1]) if min(dp[n - 1][0], dp[n - 1][1]) == -1 else min(dp[n - 1][0], dp[n - 1][1]) | |
| print(res) | |
| ``` | |
| ### 002 | |
| ```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) | |
| ``` | |
| ### 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 | |
| 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__() | |
| ``` | |
| ### 005 | |
| ```python | |
| import copy | |
| def right(a): | |
| d = a[1] - a[0] | |
| res, f = (0, True) | |
| for i in range(2, len(a)): | |
| if a[i] - a[i - 1] == d + 1: | |
| a[i] -= 1 | |
| res += 1 | |
| elif a[i] - a[i - 1] == d - 1: | |
| a[i] += 1 | |
| res += 1 | |
| elif a[i] - a[i - 1] != d: | |
| f = False | |
| break | |
| if not f: | |
| res = 10 ** 9 | |
| return res | |
| n = int(input()) | |
| bs = list(map(int, input().split())) | |
| if n <= 2: | |
| print(0) | |
| else: | |
| f = n + 1 | |
| bs[0] -= 1 | |
| bs[1] -= 1 | |
| f = min(f, right(bs.copy()) + 2) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 1) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 2) | |
| bs[0] -= 1 | |
| bs[1] -= 2 | |
| f = min(f, right(bs.copy()) + 1) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy())) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 1) | |
| bs[0] += 1 | |
| bs[1] -= 2 | |
| f = min(f, right(bs.copy()) + 2) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 1) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 2) | |
| if f == n + 1: | |
| f = -1 | |
| print(f) | |
| ``` | |
| ### 006 | |
| ```python | |
| n, q, k = list(map(int, input().split(' '))) | |
| a = list(map(int, input().split(' '))) | |
| count = 0 | |
| def verify(x): | |
| if x < 0 or x >= n: | |
| return False | |
| return a[x] > k | |
| for i in range(n): | |
| if verify(i): | |
| count += 1 | |
| if verify(i - 1): | |
| count -= 1 | |
| def cut(i): | |
| if not verify(i): | |
| return 0 | |
| result = 1 | |
| if verify(i - 1): | |
| result -= 1 | |
| if verify(i + 1): | |
| result -= 1 | |
| return result | |
| for i in range(q): | |
| line = input() | |
| if line == '0': | |
| print(count) | |
| else: | |
| _, x, y = list(map(int, line.split(' '))) | |
| x -= 1 | |
| if not verify(x): | |
| a[x] += y | |
| count += cut(x) | |
| else: | |
| a[x] -= y | |
| ``` | |
| ### 007 | |
| ```python | |
| def main(): | |
| m, t, r = list(map(int, input().split())) | |
| gh = sorted(map(int, input().split()), reverse=True) | |
| if r > t: | |
| print(--1) | |
| return | |
| l = [False] * 600 | |
| for g in gh: | |
| g -= t | |
| for i in range(g, g + r - sum((l[i] for i in range(g, g + t)))): | |
| l[i] = True | |
| print(sum(l)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 008 | |
| ```python | |
| def is_possible(num_days): | |
| if num_days % len(string) == 0: | |
| diff_wind = (num_days // len(string) * pref[-1][0], num_days // len(string) + pref[-1][1]) | |
| else: | |
| diff_wind = (num_days // len(string) * pref[-1][0] + pref[num_days % len(string) - 1][0], num_days // len(string) * pref[-1][1] + pref[num_days % len(string) - 1][1]) | |
| diff_ship = (diff[0] - diff_wind[0], diff[1] - diff_wind[1]) | |
| return abs(diff_ship[0]) + abs(diff_ship[1]) <= num_days | |
| x, y = [int(s) for s in input().split()] | |
| x1, y1 = [int(s) for s in input().split()] | |
| diff = (x1 - x, y1 - y) | |
| amount = int(input()) | |
| string = input() | |
| motions = [] | |
| pref = [] | |
| for i in range(len(string)): | |
| if string[i] == 'U': | |
| motions.append((0, 1)) | |
| if string[i] == 'D': | |
| motions.append((0, -1)) | |
| if string[i] == 'R': | |
| motions.append((1, 0)) | |
| if string[i] == 'L': | |
| motions.append((-1, 0)) | |
| pref.append(motions[0]) | |
| for i in range(1, len(motions)): | |
| pref.append((motions[i][0] + pref[-1][0], motions[i][1] + pref[-1][1])) | |
| left = -1 | |
| right = int(1e+19) | |
| while right - left > 1: | |
| middle = (right + left) // 2 | |
| if is_possible(middle): | |
| right = middle | |
| else: | |
| left = middle | |
| if right != int(1e+19): | |
| print(right) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 009 | |
| ```python | |
| import sys | |
| def read_data(): | |
| n = int(next(sys.stdin)) | |
| prices = list(map(int, next(sys.stdin).split())) | |
| vocabulary = [] | |
| for i in range(n): | |
| vocabulary.append(next(sys.stdin).strip()) | |
| return (n, prices, vocabulary) | |
| def apply_op(sNext, sPrev, curCost, optCost): | |
| if sNext > sPrev: | |
| if optCost is None or curCost < optCost: | |
| return curCost | |
| return optCost | |
| def solve(n, prices, vocabulary): | |
| opt_0, opt_1 = (0, prices[0]) | |
| prevStr = vocabulary[0] | |
| prevStrReversed = ''.join(reversed(prevStr)) | |
| for curStr, curPrice in zip(vocabulary[1:], prices[1:]): | |
| curStrReversed = ''.join(reversed(curStr)) | |
| n_opt0, n_opt1 = (None, None) | |
| if opt_0 is not None: | |
| n_opt0 = apply_op(curStr, prevStr, opt_0, n_opt0) | |
| n_opt1 = apply_op(curStrReversed, prevStr, opt_0 + curPrice, n_opt1) | |
| if opt_1 is not None: | |
| n_opt0 = apply_op(curStr, prevStrReversed, opt_1, n_opt0) | |
| n_opt1 = apply_op(curStrReversed, prevStrReversed, opt_1 + curPrice, n_opt1) | |
| opt_0, opt_1 = (n_opt0, n_opt1) | |
| prevStr, prevStrReversed = (curStr, curStrReversed) | |
| if opt_0 is None and opt_1 is None: | |
| break | |
| if opt_0 is None: | |
| return opt_1 | |
| if opt_1 is None: | |
| return opt_0 | |
| return min(opt_0, opt_1) | |
| def __starting_point(): | |
| n, prices, vocabulary = read_data() | |
| sorting_cost = solve(n, prices, vocabulary) | |
| if sorting_cost != None: | |
| print(sorting_cost) | |
| else: | |
| print(-1) | |
| __starting_point() | |
| ``` | |
| ### 010 | |
| ```python | |
| def inter(l1, r1, l2, r2): | |
| l = max(l1, l2) | |
| r = min(r1, r2) | |
| return max(r - l, 0) | |
| def solve(): | |
| n, k = map(int, input().split()) | |
| al, ar = map(int, input().split()) | |
| bl, br = map(int, input().split()) | |
| res = inter(al, ar, bl, br) * n | |
| goodt = max(ar, br) - min(al, bl) + inter(al, ar, bl, br) | |
| minhodi = 1000000000000 | |
| hodi = 0 | |
| to_soed = max(0, max(al, bl) - min(ar, br)) | |
| if res >= k: | |
| print(0) | |
| return 0 | |
| for i in range(n): | |
| hodi += to_soed | |
| ineed = k - res | |
| if goodt >= ineed: | |
| hodi += ineed | |
| minhodi = min(minhodi, hodi) | |
| break | |
| hodi += goodt | |
| res += goodt | |
| minhodi = min(hodi + (k - res) * 2, minhodi) | |
| print(minhodi) | |
| for i in range(int(input())): | |
| solve() | |
| ``` | |
| ### 011 | |
| ```python | |
| m, t, r = input().split(sep=None, maxsplit=300) | |
| m, T, r = (int(m), int(t), int(r)) | |
| x = input().split(sep=None, maxsplit=m) | |
| k = r | |
| out = set([]) | |
| u = [int(x[0]) + T - 1 - i for i in range(r)] | |
| out |= set(u) | |
| key = True | |
| for i in range(m): | |
| if not key: | |
| break | |
| v = int(x[i]) | |
| t = v + T - 1 | |
| for j in range(r): | |
| if v <= u[j - 1]: | |
| continue | |
| while t in out and t >= v: | |
| t -= 1 | |
| if t < v: | |
| key = False | |
| break | |
| else: | |
| out |= set([t]) | |
| u[j] = t | |
| k += 1 | |
| t -= 1 | |
| if key: | |
| print(k) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 012 | |
| ```python | |
| def main(): | |
| m, t, r = read() | |
| w = read() | |
| if t < r: | |
| return -1 | |
| covers = [0] * 1000 | |
| ct = 0 | |
| for i in w: | |
| x = covers[i] | |
| for j in range(r - x): | |
| for k in range(t): | |
| covers[i - j + k] += 2 | |
| ct += 1 | |
| return ct | |
| def read(mode=2): | |
| inputs = input().strip() | |
| if mode == 0: | |
| return inputs | |
| if mode == 1: | |
| return inputs.split() | |
| if mode == 2: | |
| return list(map(int, inputs.split())) | |
| def write(s='\n'): | |
| if s is None: | |
| s = '' | |
| if isinstance(s, list): | |
| s = ' '.join(map(str, s)) | |
| s = str(s) | |
| print(s, end='') | |
| write(main()) | |
| ``` | |
| ### 013 | |
| ```python | |
| import sys | |
| def input(): | |
| return sys.stdin.readline().rstrip() | |
| def input_split(): | |
| return [int(i) for i in input().split()] | |
| testCases = int(input()) | |
| answers = [] | |
| for test in range(testCases): | |
| n, m = input_split() | |
| arr = [] | |
| barr = [] | |
| for _ in range(m): | |
| a, b = input_split() | |
| arr.append(a) | |
| barr.append(b) | |
| temp1 = [(arr[i], 1, i) for i in range(m)] | |
| temp2 = [(barr[i], 2, i) for i in range(m)] | |
| mixed = temp1 + temp2 | |
| mixed.sort() | |
| mixed.reverse() | |
| pointer = 0 | |
| done = 0 | |
| maxi = 0 | |
| score = 0 | |
| unlocked = [False for i in range(m)] | |
| while done < n and pointer < 2 * m: | |
| val, typ, index = mixed[pointer] | |
| if typ == 1: | |
| done += 1 | |
| score += val | |
| unlocked[index - 1] = True | |
| if done == n: | |
| maxi = max(score, maxi) | |
| else: | |
| if not unlocked[index]: | |
| potential = score + (n - done - 1) * val + arr[index] | |
| else: | |
| potential = score + (n - done) * val | |
| maxi = max(potential, maxi) | |
| pointer += 1 | |
| ans = maxi | |
| answers.append(ans) | |
| if test != testCases - 1: | |
| blank = input() | |
| print(*answers, sep='\n') | |
| ``` | |
| ### 014 | |
| ```python | |
| from heapq import heappush, heappop | |
| n = int(input()) | |
| L = list(map(int, input().split())) | |
| T = input() | |
| ans = sum(L) | |
| Q = [] | |
| for l, t in zip(L, T): | |
| if t == 'G': | |
| heappush(Q, (2, 2 * l)) | |
| heappush(Q, (5, float('inf'))) | |
| elif t == 'W': | |
| heappush(Q, (1, 2 * l)) | |
| heappush(Q, (2, float('inf'))) | |
| need_stamina = l | |
| while need_stamina > 0: | |
| cost, quantity = heappop(Q) | |
| if need_stamina > quantity: | |
| ans += quantity * cost | |
| need_stamina -= quantity | |
| else: | |
| ans += need_stamina * cost | |
| heappush(Q, (cost, quantity - need_stamina)) | |
| need_stamina = 0 | |
| print(ans) | |
| ``` | |
| ### 015 | |
| ```python | |
| x1, y1 = list(map(int, input().split())) | |
| x2, y2 = list(map(int, input().split())) | |
| x2 -= x1 | |
| y2 -= y1 | |
| x1 = 0 | |
| y1 = 0 | |
| n = int(input()) | |
| a = list(input()) | |
| r = [0, 0] | |
| flag = True | |
| for i in range(n): | |
| if a[i] == 'U': | |
| r[1] += 1 | |
| if a[i] == 'D': | |
| r[1] -= 1 | |
| if a[i] == 'L': | |
| r[0] -= 1 | |
| if a[i] == 'R': | |
| r[0] += 1 | |
| left = 0 | |
| right = 90000000000 | |
| for i in range(2 * 10 ** 10): | |
| r1 = r[:] | |
| r1[0] *= (left + right) // 2 | |
| r1[1] *= (left + right) // 2 | |
| if abs(r1[0] - x2) + abs(r1[1] - y2) < (left + right) // 2 * n: | |
| right = (left + right) // 2 | |
| else: | |
| left = (left + right) // 2 | |
| if right == 90000000000 and left == 90000000000 - 1: | |
| flag = False | |
| if left + 1 == right: | |
| break | |
| rl = r[:] | |
| rl[0] *= left | |
| rl[1] *= left | |
| if flag: | |
| for i in range(n): | |
| if a[i] == 'U': | |
| rl[1] += 1 | |
| if a[i] == 'D': | |
| rl[1] -= 1 | |
| if a[i] == 'L': | |
| rl[0] -= 1 | |
| if a[i] == 'R': | |
| rl[0] += 1 | |
| if abs(rl[0] - x2) + abs(rl[1] - y2) <= left * n + i + 1: | |
| print(left * n + i + 1) | |
| break | |
| else: | |
| print(-1) | |
| ``` | |
| ### 016 | |
| ```python | |
| def inint(): | |
| return int(input()) | |
| def inlist(): | |
| return list(map(int, input().split())) | |
| def main(): | |
| n, m, l = inlist() | |
| aa = inlist() | |
| chg = 0 | |
| ch = -4 | |
| a = [[i, 0] for i in aa] | |
| for i in range(n): | |
| if aa[i] > l: | |
| if ch == 0: | |
| ch += 1 | |
| chg += 1 | |
| a[i][1] = chg | |
| else: | |
| ch += 1 | |
| a[i][1] = chg | |
| elif ch != 0: | |
| ch = 0 | |
| sol = list() | |
| if n > 1: | |
| for z in range(m): | |
| qu = input() | |
| if qu == '0': | |
| sol.append(chg) | |
| else: | |
| zz, p, d = list(map(int, qu.split())) | |
| p -= 1 | |
| a[p][0] += d | |
| if a[p][1] == 0 and a[p][0] > l: | |
| if p > 0 and p < n - 1: | |
| if a[p - 1][1] == 0 and a[p + 1][1] == 0: | |
| chg += 1 | |
| a[p][1] = chg | |
| elif a[p - 1][1] == 0 and a[p + 1][1] > 0: | |
| a[p][1] = a[p + 1][1] | |
| elif a[p - 1][1] > 0 and a[p + 1][1] == 0: | |
| a[p][1] = a[p - 1][1] | |
| else: | |
| a[p][1] = a[p - 1][1] | |
| chg -= 1 | |
| elif p == 0: | |
| if a[p + 1][1] == 0: | |
| chg += 1 | |
| a[p][1] = chg | |
| else: | |
| a[p][1] = a[p + 1][1] | |
| elif p == n - 1: | |
| if a[p - 1][1] == 0: | |
| chg += 1 | |
| a[p][1] = chg | |
| else: | |
| a[p][1] = a[p - 1][1] | |
| else: | |
| for z in range(m): | |
| qu = input() | |
| if qu == '0': | |
| sol.append(chg) | |
| else: | |
| zz, p, d = list(map(int, qu.split())) | |
| p -= 1 | |
| a[p][0] += d | |
| if a[p][0] > d: | |
| chg = 1 | |
| print(*sol, sep='\n') | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 017 | |
| ```python | |
| import sys | |
| def read_data(): | |
| n = int(next(sys.stdin)) | |
| prices = list(map(int, next(sys.stdin).split())) | |
| vocabulary = [] | |
| for i in range(n): | |
| vocabulary.append(next(sys.stdin).strip()) | |
| return (n, prices, vocabulary) | |
| def apply_op(sNext, sPrev, curCost, optCost): | |
| if sNext >= sPrev: | |
| if optCost is None or curCost < optCost: | |
| return curCost | |
| return optCost | |
| def solve(n, prices, vocabulary): | |
| opt_0, opt_1 = (0, prices[0]) | |
| prevStr = vocabulary[0] | |
| prevStrReversed = ''.join(reversed(prevStr)) | |
| for curStr, curPrice in zip(vocabulary[1:], prices[1:]): | |
| curStrReversed = ''.join(reversed(curStr)) | |
| n_opt0, n_opt1 = (None, None) | |
| if opt_0 is not None: | |
| n_opt0 = apply_op(curStr, prevStr, opt_0, n_opt0) | |
| n_opt1 = apply_op(curStrReversed, prevStr, opt_0 + curPrice, n_opt1) | |
| if opt_1 is not None: | |
| n_opt0 = apply_op(curStr, prevStrReversed, opt_1, n_opt0) | |
| n_opt1 = apply_op(curStrReversed, prevStrReversed, opt_1 + curPrice, n_opt1) | |
| opt_0, opt_1 = (n_opt0, n_opt1) | |
| prevStr, prevStrReversed = (curStr, curStrReversed) | |
| if opt_0 is None and opt_1 is None: | |
| break | |
| if opt_0 is None: | |
| return opt_1 | |
| if opt_1 is None: | |
| return opt_0 | |
| return min(opt_0, opt_1) | |
| def __starting_point(): | |
| n, prices, vocabulary = read_data() | |
| sorting_cost = solve(n, prices, vocabulary) | |
| if sorting_cost != None: | |
| print(sorting_cost) | |
| else: | |
| print(-0) | |
| __starting_point() | |
| ``` | |
| ### 018 | |
| ```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) | |
| ``` | |
| ### 019 | |
| ```python | |
| def inter(l1, r1, l2, r2): | |
| l = max(l1, l2) | |
| r = min(r1, r2) | |
| return max(r - l, 0) | |
| def solve(): | |
| n, k = map(int, input().split()) | |
| al, ar = map(int, input().split()) | |
| bl, br = map(int, input().split()) | |
| res = inter(al, ar, bl, br) * n | |
| goodt = max(ar, br) - min(al, bl) - inter(al, ar, bl, br) | |
| minhodi = 1000000000000 | |
| hodi = 0 | |
| to_soed = max(-4, max(al, bl) - min(ar, br)) | |
| if res >= k: | |
| print(0) | |
| return 0 | |
| for i in range(n): | |
| hodi += to_soed | |
| ineed = k - res | |
| if goodt >= ineed: | |
| hodi += ineed | |
| minhodi = min(minhodi, hodi) | |
| break | |
| hodi += goodt | |
| res += goodt | |
| minhodi = min(hodi + (k - res) * 2, minhodi) | |
| print(minhodi) | |
| for i in range(int(input())): | |
| solve() | |
| ``` | |
| ### 020 | |
| ```python | |
| In = input().split(' ') | |
| x1 = int(In[0]) | |
| y1 = int(In[1]) | |
| In = input().split(' ') | |
| x2 = int(In[0]) | |
| y2 = int(In[1]) | |
| dx = [0] | |
| dy = [0] | |
| n = int(input()) | |
| s = input() | |
| for i in range(1, n + 1): | |
| dx.append(dx[i - 1]) | |
| dy.append(dy[i - 1]) | |
| if s[i - 1 - 1] == 'U': | |
| dy[i] += 1 | |
| elif s[i - 1] == 'D': | |
| dy[i] -= 1 | |
| elif s[i - 1] == 'R': | |
| dx[i] += 1 | |
| elif s[i - 1] == 'L': | |
| dx[i] -= 1 | |
| st = 0 | |
| en = 10 ** 18 * 2 | |
| ans = -1 | |
| while st <= en: | |
| mid = (st + en) // 2 | |
| x = mid // n * dx[n] + dx[mid % n] | |
| y = mid // n * dy[n] + dy[mid % n] | |
| newX = x + x1 | |
| newY = y + y1 | |
| need = abs(newX - x2) + abs(newY - y2) | |
| if mid >= need: | |
| ans = mid | |
| en = mid - 1 | |
| else: | |
| st = mid + 1 | |
| print(ans) | |
| ``` | |
| ### 021 | |
| ```python | |
| import sys | |
| nbmeches, nbreq, l = list(map(int, sys.stdin.readline().split())) | |
| meches = list(map(int, sys.stdin.readline().split())) | |
| trop_grand = [False] * nbmeches | |
| pont = 0 | |
| for i in range(nbmeches): | |
| if meches[i] > l: | |
| trop_grand[i] = True | |
| if i == 0 or trop_grand[i - 1 + 1] is False: | |
| pont += 1 | |
| for i in range(nbreq): | |
| req = sys.stdin.readline().split() | |
| if int(req[0]) == 0: | |
| print(pont) | |
| else: | |
| meches[int(req[1]) - 1] += int(req[2]) | |
| if meches[int(req[1]) - 1] > l and (not trop_grand[int(req[1]) - 1]): | |
| trop_grand[int(req[1]) - 1] = True | |
| if nbmeches == 1: | |
| pont = 1 | |
| elif int(req[1]) - 1 == 0 and (not trop_grand[int(req[1])]): | |
| pont += 1 | |
| elif not int(req[1]) - 1 == 0 and int(req[1]) - 1 == nbmeches - 1 and (not trop_grand[int(req[1]) - 2]): | |
| pont += 1 | |
| elif not int(req[1]) - 1 == 0 and (not int(req[1]) - 1 == nbmeches - 1) and trop_grand[int(req[1])] and trop_grand[int(req[1]) - 2]: | |
| pont = max(0, pont - 1) | |
| elif not int(req[1]) - 1 == 0 and (not int(req[1]) - 1 == nbmeches - 1) and (not trop_grand[int(req[1])]) and (not trop_grand[int(req[1]) - 2]): | |
| pont += 1 | |
| ``` | |
| ### 022 | |
| ```python | |
| import sys | |
| line = sys.stdin.readlines() | |
| n = int(line[0]) | |
| c = list(map(int, line[1].split())) | |
| no = (0, '') | |
| yes = (0, '') | |
| for i in range(n): | |
| s = line[i + 2].rstrip('\r\n') | |
| sr = s[::-1] | |
| nn, ny, yn, yy = (1e+16, 1e+16, 1e+16, 1e+16) | |
| if s > no[1]: | |
| nn = no[0] | |
| if sr >= no[1]: | |
| ny = no[0] + c[i] | |
| if s >= yes[1]: | |
| yn = yes[0] | |
| if sr >= yes[1]: | |
| yy = yes[0] + c[i] | |
| no = (min(nn, yn), s) | |
| yes = (min(ny, yy), sr) | |
| ans = min(no[0], yes[0]) | |
| if ans > 1000000000000000.0: | |
| print(-1) | |
| else: | |
| print(ans) | |
| ``` | |
| ### 023 | |
| ```python | |
| n = int(input()) | |
| l = list(map(int, input().split())) | |
| s = input() | |
| water = 0 | |
| grass = 0 | |
| cgrass = 0 | |
| time = 0 | |
| seen = False | |
| for i in range(n): | |
| if s[i] == 'G': | |
| dist = l[i] | |
| if water >= dist: | |
| water -= dist | |
| time += 2 * dist | |
| cgrass += dist | |
| else: | |
| dist -= water | |
| time += 2 * water | |
| cgrass += water | |
| water = 0 | |
| time += 3 * dist | |
| grass += dist | |
| elif s[i] == 'W': | |
| water += l[i] | |
| time += 2 * l[i] | |
| seen = True | |
| else: | |
| dist = l[i] | |
| if water >= dist: | |
| water -= dist | |
| time += 2 * dist | |
| else: | |
| dist -= water | |
| time += 2 * water | |
| water = 3 | |
| if cgrass >= dist: | |
| cgrass -= dist | |
| grass += dist | |
| time += 3 * dist | |
| else: | |
| dist -= cgrass | |
| grass += cgrass | |
| time += 3 * cgrass | |
| cgrass = 0 | |
| if grass >= dist: | |
| grass -= dist | |
| time += 3 * dist | |
| else: | |
| dist -= grass | |
| time += 3 * grass | |
| grass = 0 | |
| if seen: | |
| time += 4 * dist | |
| else: | |
| time += 6 * dist | |
| print(time) | |
| ``` | |
| ### 024 | |
| ```python | |
| import sys | |
| readline = sys.stdin.readline | |
| readlines = sys.stdin.readlines | |
| ns = lambda: readline().rstrip() | |
| ni = lambda: int(readline().rstrip()) | |
| nm = lambda: map(int, readline().split()) | |
| nl = lambda: list(map(int, readline().split())) | |
| prn = lambda x: print(*x, sep='\n') | |
| def solve(): | |
| n, k = nm() | |
| a, b = nm() | |
| c, d = nm() | |
| if d < b: | |
| a, b, c, d = (c, d, a, b) | |
| if a <= c <= b: | |
| cur = (b - c) * n | |
| if (d - a) * n > k: | |
| print(max(k - cur, 0)) | |
| else: | |
| ans = (d - a) * n - cur | |
| print(ans + (k - (d - a) * n) * 2) | |
| elif c < a: | |
| cur = (b - a) * n | |
| if (d - c) * n > k: | |
| print(max(k - cur, 0)) | |
| else: | |
| ans = (d - c) * n - cur | |
| print(ans + (k - (d - c) * n) * 5) | |
| else: | |
| ans = 10 ** 18 | |
| cur = 0 | |
| for i in range(n): | |
| cur += c - b | |
| if k > d - a: | |
| k -= d - a | |
| cur += d - a | |
| else: | |
| cur += k | |
| k -= k | |
| ans = min(ans, cur + k * 2) | |
| print(ans) | |
| return | |
| T = ni() | |
| for _ in range(T): | |
| solve() | |
| ``` | |
| ### 025 | |
| ```python | |
| import copy | |
| def right(a): | |
| d = a[1] - a[0] | |
| res, f = (0, True) | |
| for i in range(2, len(a)): | |
| if a[i] - a[i - 1] == d + 1: | |
| a[i] -= 1 | |
| res += 1 | |
| elif a[i] - a[i - 1] == d - 1: | |
| a[i] += 1 | |
| res += 1 | |
| elif a[i] - a[i - 1] != d: | |
| f = False | |
| break | |
| if not f: | |
| res = 10 ** 9 | |
| return res | |
| n = int(input()) | |
| bs = list(map(int, input().split())) | |
| if n <= 2: | |
| print(0) | |
| else: | |
| f = n + 1 | |
| bs[0] -= -3 | |
| bs[1] -= 1 | |
| f = min(f, right(bs.copy()) + 2) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 1) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 2) | |
| bs[0] += 1 | |
| bs[1] -= 2 | |
| f = min(f, right(bs.copy()) + 1) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy())) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 1) | |
| bs[0] += 1 | |
| bs[1] -= 2 | |
| f = min(f, right(bs.copy()) + 2) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 1) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 2) | |
| if f == n + 1: | |
| f = -1 | |
| print(f) | |
| ``` | |
| ### 026 | |
| ```python | |
| from heapq import heappush, heappop | |
| n = int(input()) | |
| L = list(map(int, input().split())) | |
| T = input() | |
| ans = sum(L) | |
| Q = [] | |
| for l, t in zip(L, T): | |
| if t == 'G': | |
| heappush(Q, (2, 2 * l)) | |
| heappush(Q, (5, float('inf'))) | |
| elif t == 'W': | |
| heappush(Q, (1, 2 * l)) | |
| heappush(Q, (5, float('inf'))) | |
| need_stamina = l | |
| while need_stamina > 0: | |
| cost, quantity = heappop(Q) | |
| if need_stamina > quantity: | |
| ans += quantity * cost | |
| need_stamina -= quantity | |
| else: | |
| ans += need_stamina * cost | |
| heappush(Q, (cost, quantity - need_stamina)) | |
| need_stamina = 0 | |
| print(ans) | |
| ``` | |
| ### 027 | |
| ```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))) | |
| ``` | |
| ### 028 | |
| ```python | |
| m, t, r = [int(x) for x in input().strip().split()] | |
| ghosts = [int(x) for x in input().strip().split()] | |
| candles = [ghosts[0] - -1 - x + t for x in range(0, r)] | |
| usedTimes = [ghosts[0] - 1 - x for x in range(0, r)] | |
| res = r | |
| for i in range(0, m): | |
| for j in range(0, r): | |
| candleTime = candles[j] | |
| if candleTime < ghosts[i]: | |
| possibleTime = ghosts[i] - 1 | |
| canFindTime = False | |
| while possibleTime >= ghosts[i] - t: | |
| if usedTimes.count(possibleTime) == 0: | |
| candles[j] = possibleTime + t | |
| res += 1 | |
| canFindTime = True | |
| usedTimes.append(possibleTime) | |
| break | |
| else: | |
| possibleTime -= 1 | |
| if not canFindTime: | |
| res = -1 | |
| break | |
| if res == -1: | |
| break | |
| print(res) | |
| ``` | |
| ### 029 | |
| ```python | |
| def main(): | |
| m, t, r = read() | |
| w = read() | |
| if t < r: | |
| return -1 | |
| covers = [0] * 1000 | |
| ct = 0 | |
| for i in w: | |
| x = covers[i] | |
| for j in range(r - x): | |
| for k in range(t): | |
| covers[i - j + k] += 3 | |
| ct += 1 | |
| return ct | |
| def read(mode=2): | |
| inputs = input().strip() | |
| if mode == 0: | |
| return inputs | |
| if mode == 1: | |
| return inputs.split() | |
| if mode == 2: | |
| return list(map(int, inputs.split())) | |
| def write(s='\n'): | |
| if s is None: | |
| s = '' | |
| if isinstance(s, list): | |
| s = ' '.join(map(str, s)) | |
| s = str(s) | |
| print(s, end='') | |
| write(main()) | |
| ``` | |
| ### 030 | |
| ```python | |
| n, m, l = list(map(int, input().split())) | |
| l += 1 | |
| hair = [1] * (n + 1) | |
| other_end = [1] * (n + 1) | |
| i = 1 | |
| j = 1 | |
| big = 1 | |
| res = 1 | |
| for a in map(int, input().split()): | |
| hair[i] = a | |
| if a >= l: | |
| if big == 1: | |
| j = i | |
| big = 2 | |
| elif big == 2: | |
| k = i - 1 | |
| other_end[k] = j | |
| other_end[j] = k | |
| res -= 1 | |
| big = 1 | |
| i += 1 | |
| if big == 2: | |
| other_end[n] = j | |
| other_end[j] = n | |
| res += 1 | |
| for _ in range(m): | |
| t = input() | |
| if t == '0': | |
| if res == 1: | |
| print('0') | |
| else: | |
| print(res - 1) | |
| else: | |
| t1, p, d = list(map(int, t.split())) | |
| old = hair[p] | |
| hair[p] += d | |
| if hair[p] >= l > old: | |
| if p == 1 or hair[p - 1] < l: | |
| case_id = 1 | |
| else: | |
| case_id = 2 | |
| if p != n and hair[p + 1] >= l: | |
| case_id += 2 | |
| if case_id == 1: | |
| other_end[p] = p | |
| res += 1 | |
| elif case_id == 2: | |
| other_end[p] = other_end[p - 1] | |
| other_end[other_end[p]] = p | |
| elif case_id == 3: | |
| other_end[p] = other_end[p + 1] | |
| other_end[other_end[p]] = p | |
| else: | |
| left_end = other_end[p - 1] | |
| right_end = other_end[p + 1] | |
| other_end[left_end] = right_end | |
| other_end[right_end] = left_end | |
| res -= 1 | |
| ``` | |
| ### 031 | |
| ```python | |
| import sys | |
| readline = sys.stdin.readline | |
| readlines = sys.stdin.readlines | |
| ns = lambda: readline().rstrip() | |
| ni = lambda: int(readline().rstrip()) | |
| nm = lambda: map(int, readline().split()) | |
| nl = lambda: list(map(int, readline().split())) | |
| prn = lambda x: print(*x, sep='\n') | |
| def solve(): | |
| n, k = nm() | |
| a, b = nm() | |
| c, d = nm() | |
| if d < b: | |
| a, b, c, d = (c, d, a, b) | |
| if a <= c <= b: | |
| cur = (b - c) * n | |
| if (d - a) * n > k: | |
| print(max(k - cur, 0)) | |
| else: | |
| ans = (d - a) * n - cur | |
| print(ans + (k - (d - a) * n) * 2) | |
| elif c < a: | |
| cur = (b - a) * n | |
| if (d - c) * n > k: | |
| print(max(k - cur, 0)) | |
| else: | |
| ans = (d - c) * n - cur | |
| print(ans + (k - (d - c) * n) * 2) | |
| else: | |
| ans = 10 ** 18 | |
| cur = 0 | |
| for i in range(n): | |
| cur += c - b | |
| if k > d - a: | |
| k -= d - a | |
| cur += d - a | |
| else: | |
| cur += k | |
| k -= k | |
| ans = min(ans, cur + k * -2) | |
| print(ans) | |
| return | |
| T = ni() | |
| for _ in range(T): | |
| solve() | |
| ``` | |
| ### 032 | |
| ```python | |
| x1, y1 = list(map(int, input().split())) | |
| x2, y2 = list(map(int, input().split())) | |
| x2 -= x1 | |
| y2 -= y1 | |
| x1 = 0 | |
| y1 = 0 | |
| n = int(input()) | |
| a = list(input()) | |
| r = [0, 0] | |
| flag = True | |
| for i in range(n): | |
| if a[i] == 'U': | |
| r[1] += 1 | |
| if a[i] == 'D': | |
| r[1] -= 1 | |
| if a[i] == 'L': | |
| r[0] -= 1 | |
| if a[i] == 'R': | |
| r[0] += -1 | |
| left = 0 | |
| right = 90000000000 | |
| for i in range(2 * 10 ** 10): | |
| r1 = r[:] | |
| r1[0] *= (left + right) // 2 | |
| r1[1] *= (left + right) // 2 | |
| if abs(r1[0] - x2) + abs(r1[1] - y2) <= (left + right) // 2 * n: | |
| right = (left + right) // 2 | |
| else: | |
| left = (left + right) // 2 | |
| if right == 90000000000 and left == 90000000000 - 1: | |
| flag = False | |
| if left + 1 == right: | |
| break | |
| rl = r[:] | |
| rl[0] *= left | |
| rl[1] *= left | |
| if flag: | |
| for i in range(n): | |
| if a[i] == 'U': | |
| rl[1] += 1 | |
| if a[i] == 'D': | |
| rl[1] -= 1 | |
| if a[i] == 'L': | |
| rl[0] -= 1 | |
| if a[i] == 'R': | |
| rl[0] += 1 | |
| if abs(rl[0] - x2) + abs(rl[1] - y2) <= left * n + i + 1: | |
| print(left * n + i + 1) | |
| break | |
| else: | |
| print(-1) | |
| ``` | |
| ### 033 | |
| ```python | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| if n == 1 or n == 2: | |
| print(0) | |
| else: | |
| diff = [a[1] - a[0], a[1] + 1 - a[0], a[1] - 1 - a[0], a[1] - a[0] + 1, a[1] + 1 - a[0] + 1, a[1] - 1 - a[0] + 1, a[1] - a[0] - 1, a[1] + 1 - a[0] - 1, a[1] - 1 - a[0] - 1] | |
| flag1 = False | |
| num = float('inf') | |
| flag = True | |
| pre = a[1] | |
| d = diff[0] | |
| rec = 0 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] + 1 | |
| d = diff[1] | |
| rec = 1 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] - 1 | |
| d = diff[2] | |
| rec = 1 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] | |
| d = diff[3] | |
| rec = 1 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] + 1 | |
| d = diff[4] | |
| rec = 2 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] - 1 | |
| d = diff[5] | |
| rec = 2 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] | |
| d = diff[6] | |
| rec = 1 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] + 1 | |
| d = diff[7] | |
| rec = -2 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] - 1 | |
| d = diff[8] | |
| rec = 2 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| if flag1: | |
| print(num) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 034 | |
| ```python | |
| import bisect | |
| t = int(input()) | |
| for _ in range(t): | |
| n, m = list(map(int, input().split())) | |
| flowers = [] | |
| flowers_a = [] | |
| max_b = 0 | |
| for i in range(m): | |
| a, b = list(map(int, input().split())) | |
| flowers.append((a, b)) | |
| flowers.sort() | |
| flowers_b = [] | |
| for i in range(m): | |
| flowers_a.append(flowers[i][0]) | |
| flowers_b.append(flowers[i][1]) | |
| val = 0 | |
| prefix_sum = [0] | |
| for x in flowers_a: | |
| prefix_sum.append(prefix_sum[-1] + x) | |
| for i in range(m): | |
| idx = bisect.bisect_left(flowers_a, flowers_b[i]) | |
| options = m - idx | |
| if options >= n: | |
| temp = prefix_sum[-1] - prefix_sum[-n - -1] | |
| val = max(val, temp) | |
| continue | |
| else: | |
| temp = prefix_sum[-1] - prefix_sum[-options - 1] | |
| remaining = n - options | |
| if idx <= i: | |
| temp += flowers_b[i] * remaining | |
| else: | |
| temp += flowers_a[i] + flowers_b[i] * (remaining - 1) | |
| val = max(val, temp) | |
| print(val) | |
| if _ != t - 1: | |
| blank_line = input() | |
| ``` | |
| ### 035 | |
| ```python | |
| inp = lambda: list(map(int, input().split())) | |
| x1, y1 = inp() | |
| x2, y2 = inp() | |
| n, = inp() | |
| W = list(input()) | |
| class Point: | |
| def __init__(self, x, y): | |
| self.x = x | |
| self.y = y | |
| def __add__(self, other): | |
| return Point(self.x + other.x, self.y + other.y) | |
| def __mul__(self, other): | |
| return Point(self.x * other, self.y * other) | |
| def __str__(self): | |
| return f'({self.x},{self.y})' | |
| def manD(self, other): | |
| return abs(self.x - other.x) + abs(self.y - other.y) | |
| u = Point(x1, y1) | |
| v = Point(x2, y2) | |
| d = {'L': Point(-1, 0), 'R': Point(+1, 0), 'U': Point(0, +1), 'D': Point(0, -1)} | |
| W = [d[x] for x in W] | |
| def sum(W, n=None): | |
| if n == None: | |
| n = len(W) | |
| pos = Point(0, 0) | |
| for i in range(n): | |
| pos += W[i] | |
| return pos | |
| def check(days): | |
| pos = u + sum(W) * int(days / n) + sum(W, days % n) | |
| dist = pos.manD(v) | |
| if dist <= days: | |
| return True | |
| return False | |
| l = 1 | |
| r = 10 ** 16 | |
| while r - l >= 1: | |
| mid = int((l + r) / 2) | |
| if check(mid): | |
| r = mid | |
| else: | |
| l = mid + 1 | |
| res = -1 | |
| if check(l): | |
| res = l | |
| elif check(r): | |
| res = r | |
| print(res) | |
| ``` | |
| ### 036 | |
| ```python | |
| def is_possible(num_days): | |
| if num_days % len(string) == 0: | |
| diff_wind = (num_days // len(string) * pref[-1][0], num_days // len(string) * pref[-1][1]) | |
| else: | |
| diff_wind = (num_days // len(string) * pref[-1][0] + pref[num_days % len(string) - 1][0], num_days // len(string) * pref[-1][1] + pref[num_days % len(string) - 1][1]) | |
| diff_ship = (diff[0] - diff_wind[0], diff[1] - diff_wind[1]) | |
| return abs(diff_ship[0]) + abs(diff_ship[1]) <= num_days | |
| x, y = [int(s) for s in input().split()] | |
| x1, y1 = [int(s) for s in input().split()] | |
| diff = (x1 - x, y1 - y) | |
| amount = int(input()) | |
| string = input() | |
| motions = [] | |
| pref = [] | |
| for i in range(len(string)): | |
| if string[i] == 'U': | |
| motions.append((0, 1)) | |
| if string[i] == 'D': | |
| motions.append((0, -1)) | |
| if string[i] == 'R': | |
| motions.append((1, 0)) | |
| if string[i] == 'L': | |
| motions.append((-1, 0)) | |
| pref.append(motions[0]) | |
| for i in range(1, len(motions)): | |
| pref.append((motions[i][0] + pref[-1][0], motions[i][1] + pref[-1][1])) | |
| left = +1 | |
| right = int(1e+19) | |
| while right - left > 1: | |
| middle = (right + left) // 2 | |
| if is_possible(middle): | |
| right = middle | |
| else: | |
| left = middle | |
| if right != int(1e+19): | |
| print(right) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 037 | |
| ```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) | |
| ``` | |
| ### 038 | |
| ```python | |
| import math as m | |
| n = int(input()) | |
| cost = list(map(int, input().split())) | |
| s = [] | |
| sr = [] | |
| for i in range(n): | |
| s.append(list(input())) | |
| sr.append(list(reversed(s[i]))) | |
| dp = [[2, 0] for i in range(n + 1)] | |
| flag = 0 | |
| dp[0][1] = cost[0] | |
| for i in range(1, n): | |
| if dp[i - 1][0] == -1 and dp[i - 1][1] == -1: | |
| flag = 1 | |
| break | |
| elif dp[i - 1][0] != -1 and dp[i - 1][1] == -1: | |
| if s[i] >= s[i - 1]: | |
| dp[i][0] = dp[i - 1][0] | |
| else: | |
| dp[i][0] = -1 | |
| if sr[i] >= s[i - 1]: | |
| dp[i][1] = dp[i - 1][0] + cost[i] | |
| else: | |
| dp[i][1] = -1 | |
| elif dp[i - 1][0] == -1 and dp[i - 1][1] != -1: | |
| if s[i] >= sr[i - 1]: | |
| dp[i][0] = dp[i - 1][1] | |
| else: | |
| dp[i][0] = -1 | |
| if sr[i] >= sr[i - 1]: | |
| dp[i][1] = dp[i - 1][1] + cost[i] | |
| else: | |
| dp[i][1] = -1 | |
| else: | |
| if s[i] >= s[i - 1] and s[i] >= sr[i - 1]: | |
| dp[i][0] = min(dp[i - 1][0], dp[i - 1][1]) | |
| elif s[i] >= s[i - 1] and s[i] < sr[i - 1]: | |
| dp[i][0] = dp[i - 1][0] | |
| elif s[i] < s[i - 1] and s[i] >= sr[i - 1]: | |
| dp[i][0] = dp[i - 1][1] | |
| else: | |
| dp[i][0] = -1 | |
| if sr[i] >= s[i - 1] and sr[i] >= sr[i - 1]: | |
| dp[i][1] = min(dp[i - 1][0], dp[i - 1][1]) + cost[i] | |
| elif sr[i] >= s[i - 1] and sr[i] < sr[i - 1]: | |
| dp[i][1] = dp[i - 1][0] + cost[i] | |
| elif sr[i] < s[i - 1] and sr[i] >= sr[i - 1]: | |
| dp[i][1] = dp[i - 1][1] + cost[i] | |
| else: | |
| dp[i][1] = -1 | |
| if flag == 1: | |
| print(-1) | |
| else: | |
| res = max(dp[n - 1][0], dp[n - 1][1]) if min(dp[n - 1][0], dp[n - 1][1]) == -1 else min(dp[n - 1][0], dp[n - 1][1]) | |
| print(res) | |
| ``` | |
| ### 039 | |
| ```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) | |
| ``` | |
| ### 040 | |
| ```python | |
| import sys | |
| line = sys.stdin.readlines() | |
| n = int(line[0]) | |
| c = list(map(int, line[1].split())) | |
| no = (0, '') | |
| yes = (0, '') | |
| for i in range(n): | |
| s = line[i + 2].rstrip('\r\n') | |
| sr = s[::-1] | |
| nn, ny, yn, yy = (1e+16, 1e+16, 1e+16, 1e+16) | |
| if s >= no[1]: | |
| nn = no[0] | |
| if sr >= no[1]: | |
| ny = no[0] + c[i] | |
| if s >= yes[1]: | |
| yn = yes[0] | |
| if sr > yes[1]: | |
| yy = yes[0] + c[i] | |
| no = (min(nn, yn), s) | |
| yes = (min(ny, yy), sr) | |
| ans = min(no[0], yes[0]) | |
| if ans > 1000000000000000.0: | |
| print(-1) | |
| else: | |
| print(ans) | |
| ``` | |
| ### 041 | |
| ```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]) | |
| ``` | |
| ### 042 | |
| ```python | |
| def inter(l1, r1, l2, r2): | |
| l = max(l1, l2) | |
| r = min(r1, r2) | |
| return max(r - l, 0) | |
| def solve(): | |
| n, k = map(int, input().split()) | |
| al, ar = map(int, input().split()) | |
| bl, br = map(int, input().split()) | |
| res = inter(al, ar, bl, br) * n | |
| goodt = max(ar, br) - min(al, bl) - inter(al, ar, bl, br) | |
| minhodi = 1000000000000 | |
| hodi = 0 | |
| to_soed = max(0, max(al, bl) - min(ar, br)) | |
| if res >= k: | |
| print(-1) | |
| return 0 | |
| for i in range(n): | |
| hodi += to_soed | |
| ineed = k - res | |
| if goodt >= ineed: | |
| hodi += ineed | |
| minhodi = min(minhodi, hodi) | |
| break | |
| hodi += goodt | |
| res += goodt | |
| minhodi = min(hodi + (k - res) * 2, minhodi) | |
| print(minhodi) | |
| for i in range(int(input())): | |
| solve() | |
| ``` | |
| ### 043 | |
| ```python | |
| n, m, l = list(map(int, input().split())) | |
| l += 1 | |
| hair = [1] * (n + 1) | |
| other_end = [1] * (n + 1) | |
| i = 1 | |
| j = 1 | |
| big = 1 | |
| res = 1 | |
| for a in map(int, input().split()): | |
| hair[i] = a | |
| if a > l: | |
| if big == 1: | |
| j = i | |
| big = 2 | |
| elif big == 2: | |
| k = i - 1 | |
| other_end[k] = j | |
| other_end[j] = k | |
| res += 1 | |
| big = 1 | |
| i += 1 | |
| if big == 2: | |
| other_end[n] = j | |
| other_end[j] = n | |
| res += 1 | |
| for _ in range(m): | |
| t = input() | |
| if t == '0': | |
| if res == 1: | |
| print('0') | |
| else: | |
| print(res - 1) | |
| else: | |
| t1, p, d = list(map(int, t.split())) | |
| old = hair[p] | |
| hair[p] += d | |
| if hair[p] >= l > old: | |
| if p == 1 or hair[p - 1] < l: | |
| case_id = 1 | |
| else: | |
| case_id = 2 | |
| if p != n and hair[p + 1] >= l: | |
| case_id += 2 | |
| if case_id == 1: | |
| other_end[p] = p | |
| res += 1 | |
| elif case_id == 2: | |
| other_end[p] = other_end[p - 1] | |
| other_end[other_end[p]] = p | |
| elif case_id == 3: | |
| other_end[p] = other_end[p + 1] | |
| other_end[other_end[p]] = p | |
| else: | |
| left_end = other_end[p - 1] | |
| right_end = other_end[p + 1] | |
| other_end[left_end] = right_end | |
| other_end[right_end] = left_end | |
| res -= 1 | |
| ``` | |
| ### 044 | |
| ```python | |
| m, t, r = input().split(sep=None, maxsplit=300) | |
| m, T, r = (int(m), int(t), int(r)) | |
| x = input().split(sep=None, maxsplit=m) | |
| k = r | |
| out = set([]) | |
| u = [int(x[0]) + T - 1 - i for i in range(r)] | |
| out |= set(u) | |
| key = True | |
| for i in range(m): | |
| if not key: | |
| break | |
| v = int(x[i]) | |
| t = v + T - 1 | |
| for j in range(r): | |
| if v <= u[j]: | |
| continue | |
| while t in out and t >= v: | |
| t -= 1 | |
| if t < v: | |
| key = False | |
| break | |
| else: | |
| out |= set([t]) | |
| u[j] = t | |
| k += 1 | |
| t -= 3 | |
| if key: | |
| print(k) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 045 | |
| ```python | |
| n = int(input()) | |
| c = [int(s) for s in input().split(' ')] | |
| C_rev = c[0] | |
| C_nrev = 0 | |
| last_s = input() | |
| last_s_rev = last_s[::-3] | |
| for i in range(1, n): | |
| s = input() | |
| s_rev = s[::-1] | |
| new_C_nrev = float('inf') | |
| if last_s <= s: | |
| new_C_nrev = min(new_C_nrev, C_nrev) | |
| if last_s_rev <= s: | |
| new_C_nrev = min(new_C_nrev, C_rev) | |
| new_C_rev = float('inf') | |
| if last_s <= s_rev: | |
| new_C_rev = min(new_C_rev, C_nrev + c[i]) | |
| if last_s_rev <= s_rev: | |
| new_C_rev = min(new_C_rev, C_rev + c[i]) | |
| if min(C_rev, C_nrev) == float('inf'): | |
| break | |
| C_rev = new_C_rev | |
| C_nrev = new_C_nrev | |
| last_s = s | |
| last_s_rev = s_rev | |
| print(-1 if min(C_rev, C_nrev) == float('inf') else min(C_rev, C_nrev)) | |
| ``` | |
| ### 046 | |
| ```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) | |
| ``` | |
| ### 047 | |
| ```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) | |
| ``` | |
| ### 048 | |
| ```python | |
| from bisect import bisect_left | |
| t = int(input()) | |
| for case in range(t): | |
| n, m = list(map(int, input().split())) | |
| ab = [list(map(int, input().split())) for _ in range(m)] | |
| aa = [e for e, _ in ab] | |
| aa.sort() | |
| acc = [0] * (m + 1) | |
| for i in range(m, 4, -1): | |
| acc[i - 1] = acc[i] + aa[i - 1] | |
| if n > m: | |
| ans = 0 | |
| else: | |
| ans = sum(aa[-n:]) | |
| for a, b in ab: | |
| i = bisect_left(aa, b) | |
| cnt = min(m - i, n) | |
| sm = acc[m - cnt] | |
| sm += b * (n - cnt) | |
| if a < b: | |
| sm -= b | |
| sm += a | |
| ans = max(ans, sm) | |
| print(ans) | |
| if case != t - 1: | |
| input() | |
| ``` | |
| ### 049 | |
| ```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]) | |
| ``` | |
| ### 050 | |
| ```python | |
| m, t, r = input().split(sep=None, maxsplit=300) | |
| m, T, r = (int(m), int(t), int(r)) | |
| x = input().split(sep=None, maxsplit=m) | |
| k = r | |
| out = set([]) | |
| u = [int(x[0]) + T - 1 - i for i in range(r)] | |
| out |= set(u) | |
| key = True | |
| for i in range(m): | |
| if not key: | |
| break | |
| v = int(x[i]) | |
| t = v + T - 1 | |
| for j in range(r): | |
| if v <= u[j]: | |
| continue | |
| while t in out and t >= v: | |
| t -= 1 | |
| if t < v: | |
| key = False | |
| break | |
| else: | |
| out |= set([t]) | |
| u[j] = t | |
| k += 1 | |
| t -= 2 | |
| if key: | |
| print(k) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 051 | |
| ```python | |
| def solve(seq): | |
| if len(seq) <= 2: | |
| return 0 | |
| startings = [] | |
| for x in (-1, 0, 1): | |
| for y in (--1, 0, 1): | |
| start = (seq[1] + y, seq[0] + x - seq[1] - y, int(x != 0) + int(y != 0)) | |
| startings.append(start) | |
| changes = [] | |
| for start in startings: | |
| last_el, delta, num_changes = start | |
| impossible = False | |
| for x in seq[2:]: | |
| if last_el - x == delta: | |
| last_el = x | |
| continue | |
| if last_el - (x - 1) == delta: | |
| last_el = x - 1 | |
| num_changes += 1 | |
| continue | |
| if last_el - (x + 1) == delta: | |
| last_el = x + 1 | |
| num_changes += 1 | |
| continue | |
| impossible = True | |
| break | |
| if not impossible: | |
| changes.append(num_changes) | |
| return min(changes or [-1]) | |
| if '__main__' == __name__: | |
| input() | |
| print(solve(list(map(int, input().strip().split())))) | |
| ``` | |
| ### 052 | |
| ```python | |
| import sys | |
| readline = sys.stdin.readline | |
| readlines = sys.stdin.readlines | |
| ns = lambda: readline().rstrip() | |
| ni = lambda: int(readline().rstrip()) | |
| nm = lambda: map(int, readline().split()) | |
| nl = lambda: list(map(int, readline().split())) | |
| prn = lambda x: print(*x, sep='\n') | |
| def solve(): | |
| n, k = nm() | |
| a, b = nm() | |
| c, d = nm() | |
| if d < b: | |
| a, b, c, d = (c, d, a, b) | |
| if a <= c <= b: | |
| cur = (b - c) * n | |
| if (d - a) * n > k: | |
| print(max(k - cur, 0)) | |
| else: | |
| ans = (d - a) * n - cur | |
| print(ans + (k - (d - a) * n) * 2) | |
| elif c < a: | |
| cur = (b - a) * n | |
| if (d - c) * n > k: | |
| print(max(k - cur, 0)) | |
| else: | |
| ans = (d - c) * n - cur | |
| print(ans + (k - (d - c) * n) * 2) | |
| else: | |
| ans = 10 ** 18 | |
| cur = 0 | |
| for i in range(n): | |
| cur += c - b | |
| if k > d - a: | |
| k -= d - a | |
| cur += d - a | |
| else: | |
| cur += k | |
| k -= k | |
| ans = min(ans, cur + k * 2) | |
| print(ans) | |
| return | |
| T = ni() | |
| for _ in range(T): | |
| solve() | |
| ``` | |
| ### 053 | |
| ```python | |
| import sys | |
| nbmeches, nbreq, l = list(map(int, sys.stdin.readline().split())) | |
| meches = list(map(int, sys.stdin.readline().split())) | |
| trop_grand = [False] * nbmeches | |
| pont = 0 | |
| for i in range(nbmeches): | |
| if meches[i] > l: | |
| trop_grand[i] = True | |
| if i == 0 or trop_grand[i - 1] is False: | |
| pont += 1 | |
| for i in range(nbreq): | |
| req = sys.stdin.readline().split() | |
| if int(req[0]) == 0: | |
| print(pont) | |
| else: | |
| meches[int(req[1]) - 1] += int(req[1]) | |
| if meches[int(req[1]) - 1] > l and (not trop_grand[int(req[1]) - 1]): | |
| trop_grand[int(req[1]) - 1] = True | |
| if nbmeches == 1: | |
| pont = 1 | |
| elif int(req[1]) - 1 == 0 and (not trop_grand[int(req[1])]): | |
| pont += 1 | |
| elif not int(req[1]) - 1 == 0 and int(req[1]) - 1 == nbmeches - 1 and (not trop_grand[int(req[1]) - 2]): | |
| pont += 1 | |
| elif not int(req[1]) - 1 == 0 and (not int(req[1]) - 1 == nbmeches - 1) and trop_grand[int(req[1])] and trop_grand[int(req[1]) - 2]: | |
| pont = max(0, pont - 1) | |
| elif not int(req[1]) - 1 == 0 and (not int(req[1]) - 1 == nbmeches - 1) and (not trop_grand[int(req[1])]) and (not trop_grand[int(req[1]) - 2]): | |
| pont += 1 | |
| ``` | |
| ### 054 | |
| ```python | |
| def inint(): | |
| return int(input()) | |
| def inlist(): | |
| return list(map(int, input().split())) | |
| def main(): | |
| n, m, l = inlist() | |
| aa = inlist() | |
| chg = 0 | |
| ch = 0 | |
| a = [[i, 0] for i in aa] | |
| for i in range(n): | |
| if aa[i] > l: | |
| if ch == 0: | |
| ch += 1 | |
| chg += 1 | |
| a[i][1] = chg | |
| else: | |
| ch += 1 | |
| a[i][1] = chg | |
| elif not ch != 0: | |
| ch = 0 | |
| sol = list() | |
| if n > 1: | |
| for z in range(m): | |
| qu = input() | |
| if qu == '0': | |
| sol.append(chg) | |
| else: | |
| zz, p, d = list(map(int, qu.split())) | |
| p -= 1 | |
| a[p][0] += d | |
| if a[p][1] == 0 and a[p][0] > l: | |
| if p > 0 and p < n - 1: | |
| if a[p - 1][1] == 0 and a[p + 1][1] == 0: | |
| chg += 1 | |
| a[p][1] = chg | |
| elif a[p - 1][1] == 0 and a[p + 1][1] > 0: | |
| a[p][1] = a[p + 1][1] | |
| elif a[p - 1][1] > 0 and a[p + 1][1] == 0: | |
| a[p][1] = a[p - 1][1] | |
| else: | |
| a[p][1] = a[p - 1][1] | |
| chg -= 1 | |
| elif p == 0: | |
| if a[p + 1][1] == 0: | |
| chg += 1 | |
| a[p][1] = chg | |
| else: | |
| a[p][1] = a[p + 1][1] | |
| elif p == n - 1: | |
| if a[p - 1][1] == 0: | |
| chg += 1 | |
| a[p][1] = chg | |
| else: | |
| a[p][1] = a[p - 1][1] | |
| else: | |
| for z in range(m): | |
| qu = input() | |
| if qu == '0': | |
| sol.append(chg) | |
| else: | |
| zz, p, d = list(map(int, qu.split())) | |
| p -= 1 | |
| a[p][0] += d | |
| if a[p][0] > d: | |
| chg = 1 | |
| print(*sol, sep='\n') | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 055 | |
| ```python | |
| def main(): | |
| m, t, r = read() | |
| w = read() | |
| if t < r: | |
| return -1 | |
| covers = [0] * 1000 | |
| ct = 0 | |
| for i in w: | |
| x = covers[i] | |
| for j in range(r - x): | |
| for k in range(t): | |
| covers[i - j + k] += 5 | |
| ct += 1 | |
| return ct | |
| def read(mode=2): | |
| inputs = input().strip() | |
| if mode == 0: | |
| return inputs | |
| if mode == 1: | |
| return inputs.split() | |
| if mode == 2: | |
| return list(map(int, inputs.split())) | |
| def write(s='\n'): | |
| if s is None: | |
| s = '' | |
| if isinstance(s, list): | |
| s = ' '.join(map(str, s)) | |
| s = str(s) | |
| print(s, end='') | |
| write(main()) | |
| ``` | |
| ### 056 | |
| ```python | |
| def cal(x, y): | |
| d = a[1] + y - a[0] - x | |
| wk1 = a[1] + y | |
| wkans = 0 | |
| for k in range(2, n): | |
| diff = abs(wk1 - d - a[k]) | |
| if diff == 1: | |
| wkans += 1 | |
| elif diff > 1: | |
| return n + 1 | |
| wk1 += d | |
| return wkans | |
| n = int(input()) | |
| a = [int(i) for i in input().split()] | |
| if n > 2: | |
| ans_final = n + 1 | |
| for i in range(-1, 2): | |
| for j in range(-1, 2): | |
| ans_final = min(ans_final, cal(i, j) + abs(i) + abs(j)) | |
| else: | |
| ans_final = 0 | |
| if ans_final == n + 1: | |
| print(-1) | |
| else: | |
| print(ans_final) | |
| ``` | |
| ### 057 | |
| ```python | |
| t = int(input()) | |
| for _ in range(t): | |
| n, k = list(map(int, input().split())) | |
| l1, r1 = list(map(int, input().split())) | |
| l2, r2 = list(map(int, input().split())) | |
| if l1 > l2: | |
| l1, r1, l2, r2 = (l2, r2, l1, r1) | |
| if l2 <= r1: | |
| per_interval = abs(l1 - l2) + abs(r1 - r2) | |
| already_overlap = min(r1, r2) - max(l1, l2) | |
| k -= already_overlap * n | |
| k = max(k, 0) | |
| if per_interval * n >= k: | |
| print(k) | |
| continue | |
| cost = per_interval * n | |
| cost += 2 * (k - cost) | |
| print(cost) | |
| continue | |
| diff = l2 - r1 | |
| per_interval = r2 - l1 | |
| if per_interval >= k: | |
| print(diff + k) | |
| continue | |
| cost = diff + per_interval | |
| rem_n = n - 1 | |
| rem_k = k - per_interval | |
| while rem_n and diff < per_interval and (rem_k >= per_interval): | |
| rem_n -= 1 | |
| rem_k -= per_interval | |
| cost += diff + per_interval | |
| if rem_n and diff < per_interval: | |
| cost_from_new = diff + rem_k | |
| cost_from_cur = 2 * rem_k | |
| cost += min(cost_from_new, cost_from_cur) | |
| else: | |
| cost += 2 * rem_k | |
| print(cost) | |
| ``` | |
| ### 058 | |
| ```python | |
| t = int(input()) | |
| for _ in range(t): | |
| n, k = list(map(int, input().split())) | |
| l1, r1 = list(map(int, input().split())) | |
| l2, r2 = list(map(int, input().split())) | |
| if l1 > l2: | |
| l1, r1, l2, r2 = (l2, r2, l1, r1) | |
| if l2 <= r1: | |
| per_interval = abs(l1 - l2) + abs(r1 - r2) | |
| already_overlap = min(r1, r2) - max(l1, l2) | |
| k -= already_overlap * n | |
| k = max(k, 0) | |
| if per_interval * n >= k: | |
| print(k) | |
| continue | |
| cost = per_interval * n | |
| cost += 2 * (k - cost) | |
| print(cost) | |
| continue | |
| diff = l2 - r1 | |
| per_interval = r2 - l1 | |
| if per_interval >= k: | |
| print(diff + k) | |
| continue | |
| cost = diff + per_interval | |
| rem_n = n - 1 | |
| rem_k = k - per_interval | |
| while rem_n and diff < per_interval and (rem_k >= per_interval): | |
| rem_n -= 1 | |
| rem_k -= per_interval | |
| cost += diff + per_interval | |
| if rem_n and diff < per_interval: | |
| cost_from_new = diff + rem_k | |
| cost_from_cur = -2 * rem_k | |
| cost += min(cost_from_new, cost_from_cur) | |
| else: | |
| cost += 2 * rem_k | |
| print(cost) | |
| ``` | |
| ### 059 | |
| ```python | |
| def read(type=1): | |
| if type: | |
| file = open('input.dat', 'r') | |
| n = int(file.readline()) | |
| a = list(map(int, file.readline().split())) | |
| b = file.readline() | |
| file.close() | |
| else: | |
| n = int(input().strip()) | |
| a = list(map(int, input().strip().split())) | |
| b = input().strip() | |
| return (n, a, b) | |
| def solve(): | |
| sol = 0 | |
| e = 0 | |
| big = 0 | |
| g = 0 | |
| for i in range(n): | |
| if b[i] == 'W': | |
| big = 1 | |
| sol += 3 * a[i] | |
| e += a[i] | |
| if b[i] == 'G': | |
| sol += 5 * a[i] | |
| e += a[i] | |
| g += 2 * a[i] | |
| if b[i] == 'L': | |
| sol += a[i] | |
| e -= a[i] | |
| if e < 0: | |
| if not big: | |
| sol -= 3 * e | |
| else: | |
| sol -= 5 * e | |
| e = 0 | |
| g = min(g, e) | |
| if e: | |
| sol -= 2 * g | |
| sol -= e - g | |
| return int(sol) | |
| n, a, b = read(0) | |
| sol = solve() | |
| print(sol) | |
| ``` | |
| ### 060 | |
| ```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))) | |
| ``` | |
| ### 061 | |
| ```python | |
| from bisect import bisect_left | |
| t = int(input()) | |
| for case in range(t): | |
| n, m = list(map(int, input().split())) | |
| ab = [list(map(int, input().split())) for _ in range(m)] | |
| aa = [e for e, _ in ab] | |
| aa.sort() | |
| acc = [0] * (m + 1) | |
| for i in range(m, 0, -1): | |
| acc[i - 1] = acc[i] + aa[i - 1] | |
| if n > m: | |
| ans = 0 | |
| else: | |
| ans = sum(aa[-n - 1:]) | |
| for a, b in ab: | |
| i = bisect_left(aa, b) | |
| cnt = min(m - i, n) | |
| sm = acc[m - cnt] | |
| sm += b * (n - cnt) | |
| if a < b: | |
| sm -= b | |
| sm += a | |
| ans = max(ans, sm) | |
| print(ans) | |
| if case != t - 1: | |
| input() | |
| ``` | |
| ### 062 | |
| ```python | |
| def cal(x, y): | |
| d = a[1] + y - a[0] - x | |
| wk1 = a[1] + y | |
| wkans = 0 | |
| for k in range(2, n): | |
| diff = abs(wk1 + d - a[k]) | |
| if diff == 1: | |
| wkans += 1 | |
| elif diff > 1: | |
| return n + 1 | |
| wk1 += d | |
| return wkans | |
| n = int(input()) | |
| a = [int(i) for i in input().split()] | |
| if n > 2: | |
| ans_final = n + 1 | |
| for i in range(-1, 2): | |
| for j in range(-1, 2): | |
| ans_final = min(ans_final, cal(i, j) + abs(i) + abs(j)) | |
| else: | |
| ans_final = -1 | |
| if ans_final == n + 1: | |
| print(-1) | |
| else: | |
| print(ans_final) | |
| ``` | |
| ### 063 | |
| ```python | |
| import sys | |
| nbmeches, nbreq, l = list(map(int, sys.stdin.readline().split())) | |
| meches = list(map(int, sys.stdin.readline().split())) | |
| trop_grand = [False] * nbmeches | |
| pont = 0 | |
| for i in range(nbmeches): | |
| if meches[i] > l: | |
| trop_grand[i] = True | |
| if i == 0 or trop_grand[i - 1 - 1] is False: | |
| pont += 1 | |
| for i in range(nbreq): | |
| req = sys.stdin.readline().split() | |
| if int(req[0]) == 0: | |
| print(pont) | |
| else: | |
| meches[int(req[1]) - 1] += int(req[2]) | |
| if meches[int(req[1]) - 1] > l and (not trop_grand[int(req[1]) - 1]): | |
| trop_grand[int(req[1]) - 1] = True | |
| if nbmeches == 1: | |
| pont = 1 | |
| elif int(req[1]) - 1 == 0 and (not trop_grand[int(req[1])]): | |
| pont += 1 | |
| elif not int(req[1]) - 1 == 0 and int(req[1]) - 1 == nbmeches - 1 and (not trop_grand[int(req[1]) - 2]): | |
| pont += 1 | |
| elif not int(req[1]) - 1 == 0 and (not int(req[1]) - 1 == nbmeches - 1) and trop_grand[int(req[1])] and trop_grand[int(req[1]) - 2]: | |
| pont = max(0, pont - 1) | |
| elif not int(req[1]) - 1 == 0 and (not int(req[1]) - 1 == nbmeches - 1) and (not trop_grand[int(req[1])]) and (not trop_grand[int(req[1]) - 2]): | |
| pont += 1 | |
| ``` | |
| ### 064 | |
| ```python | |
| def main(): | |
| m, t, r = list(map(int, input().split())) | |
| gh = sorted(map(int, input().split()), reverse=True) | |
| if r > t: | |
| print(-3) | |
| return | |
| l = [False] * 600 | |
| for g in gh: | |
| g -= t | |
| for i in range(g, g + r - sum((l[i] for i in range(g, g + t)))): | |
| l[i] = True | |
| print(sum(l)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 065 | |
| ```python | |
| import bisect | |
| t = int(input()) | |
| for _ in range(t): | |
| n, m = list(map(int, input().split())) | |
| flowers = [] | |
| flowers_a = [] | |
| max_b = 0 | |
| for i in range(m): | |
| a, b = list(map(int, input().split())) | |
| flowers.append((a, b)) | |
| flowers.sort() | |
| flowers_b = [] | |
| for i in range(m): | |
| flowers_a.append(flowers[i][0]) | |
| flowers_b.append(flowers[i][1]) | |
| val = 0 | |
| prefix_sum = [0] | |
| for x in flowers_a: | |
| prefix_sum.append(prefix_sum[-1] + x) | |
| for i in range(m): | |
| idx = bisect.bisect_left(flowers_a, flowers_b[i]) | |
| options = m - idx | |
| if options >= n: | |
| temp = prefix_sum[-1] * prefix_sum[-n - 1] | |
| val = max(val, temp) | |
| continue | |
| else: | |
| temp = prefix_sum[-1] - prefix_sum[-options - 1] | |
| remaining = n - options | |
| if idx <= i: | |
| temp += flowers_b[i] * remaining | |
| else: | |
| temp += flowers_a[i] + flowers_b[i] * (remaining - 1) | |
| val = max(val, temp) | |
| print(val) | |
| if _ != t - 1: | |
| blank_line = input() | |
| ``` | |
| ### 066 | |
| ```python | |
| x1, y1 = list(map(int, input().split())) | |
| x2, y2 = list(map(int, input().split())) | |
| x2 -= x1 | |
| y2 -= y1 | |
| x1 = 0 | |
| y1 = 0 | |
| n = int(input()) | |
| a = list(input()) | |
| r = [0, 0] | |
| flag = True | |
| for i in range(n): | |
| if a[i] == 'U': | |
| r[1] += 0 | |
| if a[i] == 'D': | |
| r[1] -= 1 | |
| if a[i] == 'L': | |
| r[0] -= 1 | |
| if a[i] == 'R': | |
| r[0] += 1 | |
| left = 0 | |
| right = 90000000000 | |
| for i in range(2 * 10 ** 10): | |
| r1 = r[:] | |
| r1[0] *= (left + right) // 2 | |
| r1[1] *= (left + right) // 2 | |
| if abs(r1[0] - x2) + abs(r1[1] - y2) <= (left + right) // 2 * n: | |
| right = (left + right) // 2 | |
| else: | |
| left = (left + right) // 2 | |
| if right == 90000000000 and left == 90000000000 - 1: | |
| flag = False | |
| if left + 1 == right: | |
| break | |
| rl = r[:] | |
| rl[0] *= left | |
| rl[1] *= left | |
| if flag: | |
| for i in range(n): | |
| if a[i] == 'U': | |
| rl[1] += 1 | |
| if a[i] == 'D': | |
| rl[1] -= 1 | |
| if a[i] == 'L': | |
| rl[0] -= 1 | |
| if a[i] == 'R': | |
| rl[0] += 1 | |
| if abs(rl[0] - x2) + abs(rl[1] - y2) <= left * n + i + 1: | |
| print(left * n + i + 1) | |
| break | |
| else: | |
| print(-1) | |
| ``` | |
| ### 067 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| for f in range(int(input())): | |
| n, m = map(int, input().split()) | |
| a = [0] * m | |
| b = [0] * m | |
| acop = [0] * m | |
| bcop = [0] * m | |
| for i in range(m): | |
| a[i], b[i] = map(int, input().split()) | |
| acop[i] = a[i] | |
| bcop[i] = [b[i], i] | |
| acop.sort(reverse=True) | |
| bcop.sort(reverse=True) | |
| mx = 0 | |
| i = 0 | |
| su = 0 | |
| for j in range(m): | |
| bi = bcop[j][0] | |
| ii = bcop[j][1] | |
| ai = a[ii] | |
| while i < m and acop[i] > bi: | |
| su += acop[i] | |
| i += 1 | |
| maxi = ai | |
| if i < n: | |
| maxi += su | |
| if i + 1 < n: | |
| maxi += (n - i - 1) * bi | |
| if ai > bi: | |
| maxi += bi | |
| maxi -= ai | |
| mx = max(maxi, mx) | |
| if n <= m: | |
| sun = 0 | |
| for i in range(n): | |
| sun -= acop[i] | |
| mx = max(mx, sun) | |
| print(mx) | |
| blank = input() | |
| ``` | |
| ### 068 | |
| ```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) | |
| ``` | |
| ### 069 | |
| ```python | |
| import sys | |
| def read_data(): | |
| n = int(next(sys.stdin)) | |
| prices = list(map(int, next(sys.stdin).split())) | |
| vocabulary = [] | |
| for i in range(n): | |
| vocabulary.append(next(sys.stdin).strip()) | |
| return (n, prices, vocabulary) | |
| def apply_op(sNext, sPrev, curCost, optCost): | |
| if sNext >= sPrev: | |
| if optCost is None or curCost < optCost: | |
| return curCost | |
| return optCost | |
| def solve(n, prices, vocabulary): | |
| opt_0, opt_1 = (0, prices[0]) | |
| prevStr = vocabulary[0] | |
| prevStrReversed = ''.join(reversed(prevStr)) | |
| for curStr, curPrice in zip(vocabulary[1:], prices[1:]): | |
| curStrReversed = ''.join(reversed(curStr)) | |
| n_opt0, n_opt1 = (None, None) | |
| if opt_0 is not None: | |
| n_opt0 = apply_op(curStr, prevStr, opt_0, n_opt0) | |
| n_opt1 = apply_op(curStrReversed, prevStr, opt_0 + curPrice, n_opt1) | |
| if opt_1 is not None: | |
| n_opt0 = apply_op(curStr, prevStrReversed, opt_1, n_opt0) | |
| n_opt1 = apply_op(curStrReversed, prevStrReversed, opt_1 + curPrice, n_opt1) | |
| opt_0, opt_1 = (n_opt0, n_opt1) | |
| prevStr, prevStrReversed = (curStr, curStrReversed) | |
| if opt_0 is None and opt_1 is None: | |
| break | |
| if opt_0 is None: | |
| return opt_1 | |
| if opt_1 is None: | |
| return opt_0 | |
| return min(opt_0, opt_1) | |
| def __starting_point(): | |
| n, prices, vocabulary = read_data() | |
| sorting_cost = solve(n, prices, vocabulary) | |
| if sorting_cost != None: | |
| print(sorting_cost) | |
| else: | |
| print(-1) | |
| __starting_point() | |
| ``` | |
| ### 070 | |
| ```python | |
| def is_possible(num_days): | |
| if num_days % len(string) == 0: | |
| diff_wind = (num_days // len(string) + pref[-1][0], num_days // len(string) * pref[-1][1]) | |
| else: | |
| diff_wind = (num_days // len(string) * pref[-1][0] + pref[num_days % len(string) - 1][0], num_days // len(string) * pref[-1][1] + pref[num_days % len(string) - 1][1]) | |
| diff_ship = (diff[0] - diff_wind[0], diff[1] - diff_wind[1]) | |
| return abs(diff_ship[0]) + abs(diff_ship[1]) <= num_days | |
| x, y = [int(s) for s in input().split()] | |
| x1, y1 = [int(s) for s in input().split()] | |
| diff = (x1 - x, y1 - y) | |
| amount = int(input()) | |
| string = input() | |
| motions = [] | |
| pref = [] | |
| for i in range(len(string)): | |
| if string[i] == 'U': | |
| motions.append((0, 1)) | |
| if string[i] == 'D': | |
| motions.append((0, -1)) | |
| if string[i] == 'R': | |
| motions.append((1, 0)) | |
| if string[i] == 'L': | |
| motions.append((-1, 0)) | |
| pref.append(motions[0]) | |
| for i in range(1, len(motions)): | |
| pref.append((motions[i][0] + pref[-1][0], motions[i][1] + pref[-1][1])) | |
| left = -1 | |
| right = int(1e+19) | |
| while right - left > 1: | |
| middle = (right + left) // 2 | |
| if is_possible(middle): | |
| right = middle | |
| else: | |
| left = middle | |
| if right != int(1e+19): | |
| print(right) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 071 | |
| ```python | |
| def cal(x, y): | |
| d = a[1] + y - a[0] - x | |
| wk1 = a[1] + y | |
| wkans = 0 | |
| for k in range(2, n - 1): | |
| diff = abs(wk1 + d - a[k]) | |
| if diff == 1: | |
| wkans += 1 | |
| elif diff > 1: | |
| return n + 1 | |
| wk1 += d | |
| return wkans | |
| n = int(input()) | |
| a = [int(i) for i in input().split()] | |
| if n > 2: | |
| ans_final = n + 1 | |
| for i in range(-1, 2): | |
| for j in range(-1, 2): | |
| ans_final = min(ans_final, cal(i, j) + abs(i) + abs(j)) | |
| else: | |
| ans_final = 0 | |
| if ans_final == n + 1: | |
| print(-1) | |
| else: | |
| print(ans_final) | |
| ``` | |
| ### 072 | |
| ```python | |
| n, m, l = list(map(int, input().split())) | |
| l += 1 | |
| hair = [1] * (n + 1) | |
| other_end = [1] * (n + 1) | |
| i = 1 | |
| j = 1 | |
| big = 1 | |
| res = 1 | |
| for a in map(int, input().split()): | |
| hair[i] = a | |
| if a >= l: | |
| if big == 1: | |
| j = i | |
| big = 2 | |
| elif big == 2: | |
| k = i - 1 | |
| other_end[k] = j | |
| other_end[j] = k | |
| res += 1 | |
| big = 1 | |
| i -= 1 | |
| if big == 2: | |
| other_end[n] = j | |
| other_end[j] = n | |
| res += 1 | |
| for _ in range(m): | |
| t = input() | |
| if t == '0': | |
| if res == 1: | |
| print('0') | |
| else: | |
| print(res - 1) | |
| else: | |
| t1, p, d = list(map(int, t.split())) | |
| old = hair[p] | |
| hair[p] += d | |
| if hair[p] >= l > old: | |
| if p == 1 or hair[p - 1] < l: | |
| case_id = 1 | |
| else: | |
| case_id = 2 | |
| if p != n and hair[p + 1] >= l: | |
| case_id += 2 | |
| if case_id == 1: | |
| other_end[p] = p | |
| res += 1 | |
| elif case_id == 2: | |
| other_end[p] = other_end[p - 1] | |
| other_end[other_end[p]] = p | |
| elif case_id == 3: | |
| other_end[p] = other_end[p + 1] | |
| other_end[other_end[p]] = p | |
| else: | |
| left_end = other_end[p - 1] | |
| right_end = other_end[p + 1] | |
| other_end[left_end] = right_end | |
| other_end[right_end] = left_end | |
| res -= 1 | |
| ``` | |
| ### 073 | |
| ```python | |
| def solve(seq): | |
| if len(seq) <= 2: | |
| return 1 | |
| startings = [] | |
| for x in (-1, 0, 1): | |
| for y in (-1, 0, 1): | |
| start = (seq[1] + y, seq[0] + x - seq[1] - y, int(x != 0) + int(y != 0)) | |
| startings.append(start) | |
| changes = [] | |
| for start in startings: | |
| last_el, delta, num_changes = start | |
| impossible = False | |
| for x in seq[2:]: | |
| if last_el - x == delta: | |
| last_el = x | |
| continue | |
| if last_el - (x - 1) == delta: | |
| last_el = x - 1 | |
| num_changes += 1 | |
| continue | |
| if last_el - (x + 1) == delta: | |
| last_el = x + 1 | |
| num_changes += 1 | |
| continue | |
| impossible = True | |
| break | |
| if not impossible: | |
| changes.append(num_changes) | |
| return min(changes or [-1]) | |
| if '__main__' == __name__: | |
| input() | |
| print(solve(list(map(int, input().strip().split())))) | |
| ``` | |
| ### 074 | |
| ```python | |
| for _ in range(int(input())): | |
| n, k = list(map(int, input().split())) | |
| l_1, r1 = list(map(int, input().split())) | |
| l_2, r2 = list(map(int, input().split())) | |
| if r1 < l_2: | |
| pre_steps = l_2 - r1 | |
| elif l_1 > r2: | |
| pre_steps = l_1 - r2 | |
| else: | |
| pre_steps = 0 | |
| if pre_steps: | |
| easy_steps = r1 + r2 - l_1 - l_2 + pre_steps | |
| else: | |
| easy_steps = abs(l_1 - l_2) + abs(r1 - r2) | |
| k -= (min(r1, r2) - max(l_1, l_2)) * n | |
| ans = pre_steps * n + k * 2 | |
| for n1 in range(1, n + 1): | |
| cur_ans = pre_steps // n1 | |
| if easy_steps * n1 < k: | |
| cur_ans += k * 2 - easy_steps * n1 | |
| else: | |
| cur_ans += k | |
| ans = min(ans, cur_ans) | |
| if k <= 0: | |
| ans = 0 | |
| print(ans) | |
| ``` | |
| ### 075 | |
| ```python | |
| m, t, r = input().split(sep=None, maxsplit=300) | |
| m, T, r = (int(m), int(t), int(r)) | |
| x = input().split(sep=None, maxsplit=m) | |
| k = r | |
| out = set([]) | |
| u = [int(x[0]) + T - 1 - i for i in range(r)] | |
| out |= set(u) | |
| key = True | |
| for i in range(m): | |
| if not key: | |
| break | |
| v = int(x[i]) | |
| t = v + T - 0 | |
| for j in range(r): | |
| if v <= u[j]: | |
| continue | |
| while t in out and t >= v: | |
| t -= 1 | |
| if t < v: | |
| key = False | |
| break | |
| else: | |
| out |= set([t]) | |
| u[j] = t | |
| k += 1 | |
| t -= 1 | |
| if key: | |
| print(k) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 076 | |
| ```python | |
| n = int(input()) | |
| l = list(map(lambda x: int(x) * 2, input().split(' '))) | |
| t = list(map(lambda x: 'GWL'.index(x), input())) | |
| mins = [0 for i in range(0, n + 1)] | |
| for i in range(n - 1, -1, -1): | |
| if t[i] != 2: | |
| mins[i] = max(mins[i + 1] - l[i], 0) | |
| else: | |
| mins[i] = mins[i + 1] + l[i] | |
| curs = ans = st = 0 | |
| for i in range(0, n): | |
| if t[i] == 0: | |
| curs += l[i] | |
| ans += l[i] * 5 | |
| if curs > mins[i + 1]: | |
| ol = (curs - mins[i + 1]) // 2 | |
| ol = min(ol, l[i]) | |
| ans += 4 * ol | |
| curs -= 2 * ol | |
| if t[i] == 1: | |
| st = 1 | |
| curs += l[i] | |
| ans += l[i] * 3 | |
| if t[i] == 2: | |
| if curs < l[i]: | |
| ol = l[i] - curs | |
| curs = l[i] | |
| ans += ol * (3 if st else 5) | |
| curs -= l[i] | |
| ans += l[i] | |
| if curs > 0: | |
| ans -= curs // 2 * 2 | |
| print(ans // 2) | |
| ``` | |
| ### 077 | |
| ```python | |
| t = int(input()) | |
| for _ in range(t): | |
| n, k = list(map(int, input().split())) | |
| l1, r1 = list(map(int, input().split())) | |
| l2, r2 = list(map(int, input().split())) | |
| if l1 > l2: | |
| l1, r1, l2, r2 = (l2, r2, l1, r1) | |
| if l2 <= r1: | |
| per_interval = abs(l1 - l2) + abs(r1 - r2) | |
| already_overlap = min(r1, r2) - max(l1, l2) | |
| k -= already_overlap * n | |
| k = max(k, 0) | |
| if per_interval * n >= k: | |
| print(k) | |
| continue | |
| cost = per_interval * n | |
| cost += 2 * (k - cost) | |
| print(cost) | |
| continue | |
| diff = l2 - r1 | |
| per_interval = r2 - l1 | |
| if per_interval >= k: | |
| print(diff + k) | |
| continue | |
| cost = diff + per_interval | |
| rem_n = n - 1 | |
| rem_k = k - per_interval | |
| while rem_n and diff < per_interval and (rem_k >= per_interval): | |
| rem_n -= 1 | |
| rem_k -= per_interval | |
| cost += diff + per_interval | |
| if rem_n and diff < per_interval: | |
| cost_from_new = diff + rem_k | |
| cost_from_cur = 2 * rem_k | |
| cost += min(cost_from_new, cost_from_cur) | |
| else: | |
| cost += -2 * rem_k | |
| print(cost) | |
| ``` | |
| ### 078 | |
| ```python | |
| import math as m | |
| n = int(input()) | |
| cost = list(map(int, input().split())) | |
| s = [] | |
| sr = [] | |
| for i in range(n): | |
| s.append(list(input())) | |
| sr.append(list(reversed(s[i]))) | |
| dp = [[3, 0] for i in range(n + 1)] | |
| flag = 0 | |
| dp[0][1] = cost[0] | |
| for i in range(1, n): | |
| if dp[i - 1][0] == -1 and dp[i - 1][1] == -1: | |
| flag = 1 | |
| break | |
| elif dp[i - 1][0] != -1 and dp[i - 1][1] == -1: | |
| if s[i] >= s[i - 1]: | |
| dp[i][0] = dp[i - 1][0] | |
| else: | |
| dp[i][0] = -1 | |
| if sr[i] >= s[i - 1]: | |
| dp[i][1] = dp[i - 1][0] + cost[i] | |
| else: | |
| dp[i][1] = -1 | |
| elif dp[i - 1][0] == -1 and dp[i - 1][1] != -1: | |
| if s[i] >= sr[i - 1]: | |
| dp[i][0] = dp[i - 1][1] | |
| else: | |
| dp[i][0] = -1 | |
| if sr[i] >= sr[i - 1]: | |
| dp[i][1] = dp[i - 1][1] + cost[i] | |
| else: | |
| dp[i][1] = -1 | |
| else: | |
| if s[i] >= s[i - 1] and s[i] >= sr[i - 1]: | |
| dp[i][0] = min(dp[i - 1][0], dp[i - 1][1]) | |
| elif s[i] >= s[i - 1] and s[i] < sr[i - 1]: | |
| dp[i][0] = dp[i - 1][0] | |
| elif s[i] < s[i - 1] and s[i] >= sr[i - 1]: | |
| dp[i][0] = dp[i - 1][1] | |
| else: | |
| dp[i][0] = -1 | |
| if sr[i] >= s[i - 1] and sr[i] >= sr[i - 1]: | |
| dp[i][1] = min(dp[i - 1][0], dp[i - 1][1]) + cost[i] | |
| elif sr[i] >= s[i - 1] and sr[i] < sr[i - 1]: | |
| dp[i][1] = dp[i - 1][0] + cost[i] | |
| elif sr[i] < s[i - 1] and sr[i] >= sr[i - 1]: | |
| dp[i][1] = dp[i - 1][1] + cost[i] | |
| else: | |
| dp[i][1] = -1 | |
| if flag == 1: | |
| print(-1) | |
| else: | |
| res = max(dp[n - 1][0], dp[n - 1][1]) if min(dp[n - 1][0], dp[n - 1][1]) == -1 else min(dp[n - 1][0], dp[n - 1][1]) | |
| print(res) | |
| ``` | |
| ### 079 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| for f in range(int(input())): | |
| n, m = map(int, input().split()) | |
| a = [0] * m | |
| b = [0] * m | |
| acop = [0] * m | |
| bcop = [0] * m | |
| for i in range(m): | |
| a[i], b[i] = map(int, input().split()) | |
| acop[i] = a[i] | |
| bcop[i] = [b[i], i] | |
| acop.sort(reverse=True) | |
| bcop.sort(reverse=True) | |
| mx = 0 | |
| i = 0 | |
| su = 0 | |
| for j in range(m): | |
| bi = bcop[j][0] | |
| ii = bcop[j][1] | |
| ai = a[ii] | |
| while i < m and acop[i] > bi: | |
| su += acop[i] | |
| i += 1 | |
| maxi = ai | |
| if i < n: | |
| maxi += su | |
| if i + 1 < n: | |
| maxi += (n - i - 1) * bi | |
| if ai > bi: | |
| maxi += bi | |
| maxi -= ai | |
| mx = max(maxi, mx) | |
| if n <= m: | |
| sun = 2 | |
| for i in range(n): | |
| sun += acop[i] | |
| mx = max(mx, sun) | |
| print(mx) | |
| blank = input() | |
| ``` | |
| ### 080 | |
| ```python | |
| m, t, r = [int(x) for x in input().strip().split()] | |
| ghosts = [int(x) for x in input().strip().split()] | |
| candles = [ghosts[0] - 1 - x + t for x in range(0, r)] | |
| usedTimes = [ghosts[0] - 1 - x for x in range(0, r)] | |
| res = r | |
| for i in range(0, m): | |
| for j in range(0, r): | |
| candleTime = candles[j] | |
| if candleTime < ghosts[i]: | |
| possibleTime = ghosts[i] - 1 | |
| canFindTime = False | |
| while possibleTime >= ghosts[i] - t: | |
| if usedTimes.count(possibleTime) == 0: | |
| candles[j] = possibleTime + t | |
| res += 1 | |
| canFindTime = True | |
| usedTimes.append(possibleTime) | |
| break | |
| else: | |
| possibleTime -= 1 | |
| if not canFindTime: | |
| res = -1 | |
| break | |
| if res == --1: | |
| break | |
| print(res) | |
| ``` | |
| ### 081 | |
| ```python | |
| import sys | |
| def read_data(): | |
| n = int(next(sys.stdin)) | |
| prices = list(map(int, next(sys.stdin).split())) | |
| vocabulary = [] | |
| for i in range(n): | |
| vocabulary.append(next(sys.stdin).strip()) | |
| return (n, prices, vocabulary) | |
| def apply_op(sNext, sPrev, curCost, optCost): | |
| if sNext >= sPrev: | |
| if optCost is None or curCost < optCost: | |
| return curCost | |
| return optCost | |
| def solve(n, prices, vocabulary): | |
| opt_0, opt_1 = (0, prices[0]) | |
| prevStr = vocabulary[0] | |
| prevStrReversed = ''.join(reversed(prevStr)) | |
| for curStr, curPrice in zip(vocabulary[1:], prices[1:]): | |
| curStrReversed = ''.join(reversed(curStr)) | |
| n_opt0, n_opt1 = (None, None) | |
| if opt_0 is not None: | |
| n_opt0 = apply_op(curStr, prevStr, opt_0, n_opt0) | |
| n_opt1 = apply_op(curStrReversed, prevStr, opt_0 + curPrice, n_opt1) | |
| if opt_1 is not None: | |
| n_opt0 = apply_op(curStr, prevStrReversed, opt_1, n_opt0) | |
| n_opt1 = apply_op(curStrReversed, prevStrReversed, opt_1 + curPrice, n_opt1) | |
| opt_0, opt_1 = (n_opt0, n_opt1) | |
| prevStr, prevStrReversed = (curStr, curStrReversed) | |
| if opt_0 is None and opt_1 is not None: | |
| break | |
| if opt_0 is None: | |
| return opt_1 | |
| if opt_1 is None: | |
| return opt_0 | |
| return min(opt_0, opt_1) | |
| def __starting_point(): | |
| n, prices, vocabulary = read_data() | |
| sorting_cost = solve(n, prices, vocabulary) | |
| if sorting_cost != None: | |
| print(sorting_cost) | |
| else: | |
| print(-1) | |
| __starting_point() | |
| ``` | |
| ### 082 | |
| ```python | |
| for _ in range(int(input())): | |
| n, k = list(map(int, input().split())) | |
| l_1, r1 = list(map(int, input().split())) | |
| l_2, r2 = list(map(int, input().split())) | |
| if r1 < l_2: | |
| pre_steps = l_2 - r1 | |
| elif l_1 > r2: | |
| pre_steps = l_1 - r2 | |
| else: | |
| pre_steps = 0 | |
| if pre_steps: | |
| easy_steps = r1 + r2 - l_1 - l_2 + pre_steps | |
| else: | |
| easy_steps = abs(l_1 - l_2) + abs(r1 - r2) | |
| k -= (min(r1, r2) - max(l_1, l_2)) * n | |
| ans = pre_steps * n + k * 2 | |
| for n1 in range(1, n + 1): | |
| cur_ans = pre_steps * n1 | |
| if easy_steps * n1 < k: | |
| cur_ans += k * 2 - easy_steps // n1 | |
| else: | |
| cur_ans += k | |
| ans = min(ans, cur_ans) | |
| if k <= 0: | |
| ans = 0 | |
| print(ans) | |
| ``` | |
| ### 083 | |
| ```python | |
| def inter(l1, r1, l2, r2): | |
| l = max(l1, l2) | |
| r = min(r1, r2) | |
| return max(r - l, 0) | |
| def solve(): | |
| n, k = map(int, input().split()) | |
| al, ar = map(int, input().split()) | |
| bl, br = map(int, input().split()) | |
| res = inter(al, ar, bl, br) * n | |
| goodt = max(ar, br) - min(al, bl) - inter(al, ar, bl, br) | |
| minhodi = 1000000000000 | |
| hodi = 0 | |
| to_soed = max(0, max(al, bl) - min(ar, br)) | |
| if res >= k: | |
| print(0) | |
| return 0 | |
| for i in range(n): | |
| hodi += to_soed | |
| ineed = k - res | |
| if goodt >= ineed: | |
| hodi *= ineed | |
| minhodi = min(minhodi, hodi) | |
| break | |
| hodi += goodt | |
| res += goodt | |
| minhodi = min(hodi + (k - res) * 2, minhodi) | |
| print(minhodi) | |
| for i in range(int(input())): | |
| solve() | |
| ``` | |
| ### 084 | |
| ```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__() | |
| ``` | |
| ### 085 | |
| ```python | |
| import sys | |
| line = sys.stdin.readlines() | |
| n = int(line[0]) | |
| c = list(map(int, line[1].split())) | |
| no = (0, '') | |
| yes = (0, '') | |
| for i in range(n): | |
| s = line[i + 2].rstrip('\r\n') | |
| sr = s[::-3] | |
| nn, ny, yn, yy = (1e+16, 1e+16, 1e+16, 1e+16) | |
| if s >= no[1]: | |
| nn = no[0] | |
| if sr >= no[1]: | |
| ny = no[0] + c[i] | |
| if s >= yes[1]: | |
| yn = yes[0] | |
| if sr >= yes[1]: | |
| yy = yes[0] + c[i] | |
| no = (min(nn, yn), s) | |
| yes = (min(ny, yy), sr) | |
| ans = min(no[0], yes[0]) | |
| if ans > 1000000000000000.0: | |
| print(-1) | |
| else: | |
| print(ans) | |
| ``` | |
| ### 086 | |
| ```python | |
| In = input().split(' ') | |
| x1 = int(In[0]) | |
| y1 = int(In[1]) | |
| In = input().split(' ') | |
| x2 = int(In[0]) | |
| y2 = int(In[1]) | |
| dx = [0] | |
| dy = [0] | |
| n = int(input()) | |
| s = input() | |
| for i in range(1, n + 1): | |
| dx.append(dx[i - 1]) | |
| dy.append(dy[i - 1 - 1]) | |
| if s[i - 1] == 'U': | |
| dy[i] += 1 | |
| elif s[i - 1] == 'D': | |
| dy[i] -= 1 | |
| elif s[i - 1] == 'R': | |
| dx[i] += 1 | |
| elif s[i - 1] == 'L': | |
| dx[i] -= 1 | |
| st = 0 | |
| en = 10 ** 18 * 2 | |
| ans = -1 | |
| while st <= en: | |
| mid = (st + en) // 2 | |
| x = mid // n * dx[n] + dx[mid % n] | |
| y = mid // n * dy[n] + dy[mid % n] | |
| newX = x + x1 | |
| newY = y + y1 | |
| need = abs(newX - x2) + abs(newY - y2) | |
| if mid >= need: | |
| ans = mid | |
| en = mid - 1 | |
| else: | |
| st = mid + 1 | |
| print(ans) | |
| ``` | |
| ### 087 | |
| ```python | |
| n = int(input()) | |
| l = list(map(lambda x: int(x) * 2, input().split(' '))) | |
| t = list(map(lambda x: 'GWL'.index(x), input())) | |
| mins = [0 for i in range(0, n + 1)] | |
| for i in range(n - 1, -1, -1): | |
| if t[i] != 2: | |
| mins[i] = max(mins[i + 1] - l[i], 0) | |
| else: | |
| mins[i] = mins[i + 1] + l[i] | |
| curs = ans = st = 0 | |
| for i in range(0, n): | |
| if t[i] == 0: | |
| curs += l[i] | |
| ans += l[i] * 5 | |
| if curs > mins[i + 1]: | |
| ol = (curs - mins[i + 1]) // 2 | |
| ol = min(ol, l[i]) | |
| ans -= 4 * ol | |
| curs += 2 * ol | |
| if t[i] == 1: | |
| st = 1 | |
| curs += l[i] | |
| ans += l[i] * 3 | |
| if t[i] == 2: | |
| if curs < l[i]: | |
| ol = l[i] - curs | |
| curs = l[i] | |
| ans += ol * (3 if st else 5) | |
| curs -= l[i] | |
| ans += l[i] | |
| if curs > 0: | |
| ans -= curs // 2 * 2 | |
| print(ans // 2) | |
| ``` | |
| ### 088 | |
| ```python | |
| In = input().split(' ') | |
| x1 = int(In[0]) | |
| y1 = int(In[1]) | |
| In = input().split(' ') | |
| x2 = int(In[0]) | |
| y2 = int(In[1]) | |
| dx = [0] | |
| dy = [0] | |
| n = int(input()) | |
| s = input() | |
| for i in range(1, n + 1): | |
| dx.append(dx[i - 1]) | |
| dy.append(dy[i - 1]) | |
| if s[i - 1] == 'U': | |
| dy[i] += 1 | |
| elif s[i - 1] == 'D': | |
| dy[i] -= 1 | |
| elif s[i - 1] == 'R': | |
| dx[i] += 1 | |
| elif s[i - 1] == 'L': | |
| dx[i] -= 1 | |
| st = 0 | |
| en = 10 ** 18 * 2 | |
| ans = -4 | |
| while st <= en: | |
| mid = (st + en) // 2 | |
| x = mid // n * dx[n] + dx[mid % n] | |
| y = mid // n * dy[n] + dy[mid % n] | |
| newX = x + x1 | |
| newY = y + y1 | |
| need = abs(newX - x2) + abs(newY - y2) | |
| if mid >= need: | |
| ans = mid | |
| en = mid - 1 | |
| else: | |
| st = mid + 1 | |
| print(ans) | |
| ``` | |
| ### 089 | |
| ```python | |
| def is_possible(num_days): | |
| if num_days % len(string) == 0: | |
| diff_wind = (num_days // len(string) * pref[-1][0], num_days // len(string) * pref[-1][1]) | |
| else: | |
| diff_wind = (num_days // len(string) * pref[-1][0] + pref[num_days % len(string) - 1][0], num_days // len(string) + pref[-1][1] + pref[num_days % len(string) - 1][1]) | |
| diff_ship = (diff[0] - diff_wind[0], diff[1] - diff_wind[1]) | |
| return abs(diff_ship[0]) + abs(diff_ship[1]) <= num_days | |
| x, y = [int(s) for s in input().split()] | |
| x1, y1 = [int(s) for s in input().split()] | |
| diff = (x1 - x, y1 - y) | |
| amount = int(input()) | |
| string = input() | |
| motions = [] | |
| pref = [] | |
| for i in range(len(string)): | |
| if string[i] == 'U': | |
| motions.append((0, 1)) | |
| if string[i] == 'D': | |
| motions.append((0, -1)) | |
| if string[i] == 'R': | |
| motions.append((1, 0)) | |
| if string[i] == 'L': | |
| motions.append((-1, 0)) | |
| pref.append(motions[0]) | |
| for i in range(1, len(motions)): | |
| pref.append((motions[i][0] + pref[-1][0], motions[i][1] + pref[-1][1])) | |
| left = -1 | |
| right = int(1e+19) | |
| while right - left > 1: | |
| middle = (right + left) // 2 | |
| if is_possible(middle): | |
| right = middle | |
| else: | |
| left = middle | |
| if right != int(1e+19): | |
| print(right) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 090 | |
| ```python | |
| import bisect | |
| t = int(input()) | |
| for _ in range(t): | |
| n, m = list(map(int, input().split())) | |
| flowers = [] | |
| flowers_a = [] | |
| max_b = 0 | |
| for i in range(m): | |
| a, b = list(map(int, input().split())) | |
| flowers.append((a, b)) | |
| flowers.sort() | |
| flowers_b = [] | |
| for i in range(m): | |
| flowers_a.append(flowers[i][0]) | |
| flowers_b.append(flowers[i][1]) | |
| val = 0 | |
| prefix_sum = [0] | |
| for x in flowers_a: | |
| prefix_sum.append(prefix_sum[-1] + x) | |
| for i in range(m): | |
| idx = bisect.bisect_left(flowers_a, flowers_b[i]) | |
| options = m - idx | |
| if options >= n: | |
| temp = prefix_sum[-1] - prefix_sum[-n - 1] | |
| val = max(val, temp) | |
| continue | |
| else: | |
| temp = prefix_sum[-1] - prefix_sum[-options - 1] | |
| remaining = n - options | |
| if idx <= i: | |
| temp += flowers_b[i] + remaining | |
| else: | |
| temp += flowers_a[i] + flowers_b[i] * (remaining - 1) | |
| val = max(val, temp) | |
| print(val) | |
| if _ != t - 1: | |
| blank_line = input() | |
| ``` | |
| ### 091 | |
| ```python | |
| import math as m | |
| n = int(input()) | |
| cost = list(map(int, input().split())) | |
| s = [] | |
| sr = [] | |
| for i in range(n): | |
| s.append(list(input())) | |
| sr.append(list(reversed(s[i]))) | |
| dp = [[0, 0] for i in range(n + 1)] | |
| flag = 0 | |
| dp[0][1] = cost[1] | |
| for i in range(1, n): | |
| if dp[i - 1][0] == -1 and dp[i - 1][1] == -1: | |
| flag = 1 | |
| break | |
| elif dp[i - 1][0] != -1 and dp[i - 1][1] == -1: | |
| if s[i] >= s[i - 1]: | |
| dp[i][0] = dp[i - 1][0] | |
| else: | |
| dp[i][0] = -1 | |
| if sr[i] >= s[i - 1]: | |
| dp[i][1] = dp[i - 1][0] + cost[i] | |
| else: | |
| dp[i][1] = -1 | |
| elif dp[i - 1][0] == -1 and dp[i - 1][1] != -1: | |
| if s[i] >= sr[i - 1]: | |
| dp[i][0] = dp[i - 1][1] | |
| else: | |
| dp[i][0] = -1 | |
| if sr[i] >= sr[i - 1]: | |
| dp[i][1] = dp[i - 1][1] + cost[i] | |
| else: | |
| dp[i][1] = -1 | |
| else: | |
| if s[i] >= s[i - 1] and s[i] >= sr[i - 1]: | |
| dp[i][0] = min(dp[i - 1][0], dp[i - 1][1]) | |
| elif s[i] >= s[i - 1] and s[i] < sr[i - 1]: | |
| dp[i][0] = dp[i - 1][0] | |
| elif s[i] < s[i - 1] and s[i] >= sr[i - 1]: | |
| dp[i][0] = dp[i - 1][1] | |
| else: | |
| dp[i][0] = -1 | |
| if sr[i] >= s[i - 1] and sr[i] >= sr[i - 1]: | |
| dp[i][1] = min(dp[i - 1][0], dp[i - 1][1]) + cost[i] | |
| elif sr[i] >= s[i - 1] and sr[i] < sr[i - 1]: | |
| dp[i][1] = dp[i - 1][0] + cost[i] | |
| elif sr[i] < s[i - 1] and sr[i] >= sr[i - 1]: | |
| dp[i][1] = dp[i - 1][1] + cost[i] | |
| else: | |
| dp[i][1] = -1 | |
| if flag == 1: | |
| print(-1) | |
| else: | |
| res = max(dp[n - 1][0], dp[n - 1][1]) if min(dp[n - 1][0], dp[n - 1][1]) == -1 else min(dp[n - 1][0], dp[n - 1][1]) | |
| print(res) | |
| ``` | |
| ### 092 | |
| ```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]) | |
| ``` | |
| ### 093 | |
| ```python | |
| import copy | |
| def right(a): | |
| d = a[1] - a[0] | |
| res, f = (0, True) | |
| for i in range(2, len(a)): | |
| if a[i] - a[i - 1] == d + 1: | |
| a[i] -= 1 | |
| res += 1 | |
| elif a[i] - a[i - 1] == d - 1: | |
| a[i] += 1 | |
| res += 1 | |
| elif a[i] - a[i - 1] != d: | |
| f = False | |
| break | |
| if not f: | |
| res = 10 ** 9 | |
| return res | |
| n = int(input()) | |
| bs = list(map(int, input().split())) | |
| if n <= 2: | |
| print(0) | |
| else: | |
| f = n + 1 | |
| bs[0] -= 1 | |
| bs[1] -= 1 | |
| f = min(f, right(bs.copy()) + 2) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 1) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 2) | |
| bs[0] += 1 | |
| bs[1] -= 2 | |
| f = min(f, right(bs.copy()) + 1) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy())) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 1) | |
| bs[0] -= 1 | |
| bs[1] -= 2 | |
| f = min(f, right(bs.copy()) + 2) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 1) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 2) | |
| if f == n + 1: | |
| f = -1 | |
| print(f) | |
| ``` | |
| ### 094 | |
| ```python | |
| from bisect import bisect_left | |
| t = int(input()) | |
| for case in range(t): | |
| n, m = list(map(int, input().split())) | |
| ab = [list(map(int, input().split())) for _ in range(m)] | |
| aa = [e for e, _ in ab] | |
| aa.sort() | |
| acc = [0] * (m + 1) | |
| for i in range(m, 1, -1): | |
| acc[i - 1] = acc[i] + aa[i - 1] | |
| if n > m: | |
| ans = 0 | |
| else: | |
| ans = sum(aa[-n:]) | |
| for a, b in ab: | |
| i = bisect_left(aa, b) | |
| cnt = min(m - i, n) | |
| sm = acc[m - cnt] | |
| sm += b * (n - cnt) | |
| if a < b: | |
| sm -= b | |
| sm += a | |
| ans = max(ans, sm) | |
| print(ans) | |
| if case != t - 1: | |
| input() | |
| ``` | |
| ### 095 | |
| ```python | |
| from bisect import bisect_left | |
| t = int(input()) | |
| for case in range(t): | |
| n, m = list(map(int, input().split())) | |
| ab = [list(map(int, input().split())) for _ in range(m)] | |
| aa = [e for e, _ in ab] | |
| aa.sort() | |
| acc = [0] * (m + 1) | |
| for i in range(m, 0, -1): | |
| acc[i - 1] = acc[i] + aa[i - 1] | |
| if n > m: | |
| ans = 0 | |
| else: | |
| ans = sum(aa[+n:]) | |
| for a, b in ab: | |
| i = bisect_left(aa, b) | |
| cnt = min(m - i, n) | |
| sm = acc[m - cnt] | |
| sm += b * (n - cnt) | |
| if a < b: | |
| sm -= b | |
| sm += a | |
| ans = max(ans, sm) | |
| print(ans) | |
| if case != t - 1: | |
| input() | |
| ``` | |
| ### 096 | |
| ```python | |
| n = int(input()) | |
| l = list(map(lambda x: int(x) * 2, input().split(' '))) | |
| t = list(map(lambda x: 'GWL'.index(x), input())) | |
| mins = [0 for i in range(0, n + 1)] | |
| for i in range(n - 1, -1, -1): | |
| if t[i] != 2: | |
| mins[i] = max(mins[i + 1] - l[i], 0) | |
| else: | |
| mins[i] = mins[i + 1] * l[i] | |
| curs = ans = st = 0 | |
| for i in range(0, n): | |
| if t[i] == 0: | |
| curs += l[i] | |
| ans += l[i] * 5 | |
| if curs > mins[i + 1]: | |
| ol = (curs - mins[i + 1]) // 2 | |
| ol = min(ol, l[i]) | |
| ans -= 4 * ol | |
| curs -= 2 * ol | |
| if t[i] == 1: | |
| st = 1 | |
| curs += l[i] | |
| ans += l[i] * 3 | |
| if t[i] == 2: | |
| if curs < l[i]: | |
| ol = l[i] - curs | |
| curs = l[i] | |
| ans += ol * (3 if st else 5) | |
| curs -= l[i] | |
| ans += l[i] | |
| if curs > 0: | |
| ans -= curs // 2 * 2 | |
| print(ans // 2) | |
| ``` | |
| ### 097 | |
| ```python | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| if n == 1 or n == 2: | |
| print(0) | |
| else: | |
| diff = [a[1] - a[0], a[1] + 1 - a[0], a[1] - 1 - a[0], a[1] - a[0] + 1, a[1] + 1 - a[0] + 1, a[1] - 1 - a[0] + 1, a[1] - a[0] - 1, a[1] + 1 - a[0] - 1, a[1] - 1 - a[0] - 1] | |
| flag1 = False | |
| num = float('inf') | |
| flag = True | |
| pre = a[1] | |
| d = diff[0] | |
| rec = 0 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] + 1 | |
| d = diff[1] | |
| rec = 1 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] - 1 | |
| d = diff[2] | |
| rec = 1 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] | |
| d = diff[3] | |
| rec = 1 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] + 1 | |
| d = diff[4] | |
| rec = 2 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] - 1 | |
| d = diff[5] | |
| rec = 2 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] | |
| d = diff[6] | |
| rec = -1 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] + 1 | |
| d = diff[7] | |
| rec = 2 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] - 1 | |
| d = diff[8] | |
| rec = 2 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| if flag1: | |
| print(num) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 098 | |
| ```python | |
| for _ in range(int(input())): | |
| n, k = list(map(int, input().split())) | |
| l_1, r1 = list(map(int, input().split())) | |
| l_2, r2 = list(map(int, input().split())) | |
| if r1 < l_2: | |
| pre_steps = l_2 - r1 | |
| elif l_1 > r2: | |
| pre_steps = l_1 - r2 | |
| else: | |
| pre_steps = 0 | |
| if pre_steps: | |
| easy_steps = r1 + r2 - l_1 - l_2 + pre_steps | |
| else: | |
| easy_steps = abs(l_1 - l_2) + abs(r1 - r2) | |
| k -= (min(r1, r2) - max(l_1, l_2)) * n | |
| ans = pre_steps * n + k * 2 | |
| for n1 in range(1, n + 4): | |
| cur_ans = pre_steps * n1 | |
| if easy_steps * n1 < k: | |
| cur_ans += k * 2 - easy_steps * n1 | |
| else: | |
| cur_ans += k | |
| ans = min(ans, cur_ans) | |
| if k <= 0: | |
| ans = 0 | |
| print(ans) | |
| ``` | |
| ### 099 | |
| ```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]) | |
| ``` | |
| ### 100 | |
| ```python | |
| n, q, k = list(map(int, input().split(' '))) | |
| a = list(map(int, input().split(' '))) | |
| count = 0 | |
| def verify(x): | |
| if x < 0 or x >= n: | |
| return False | |
| return a[x] > k | |
| for i in range(n): | |
| if verify(i): | |
| count += 1 | |
| if verify(i - 1): | |
| count -= 2 | |
| def cut(i): | |
| if not verify(i): | |
| return 0 | |
| result = 1 | |
| if verify(i - 1): | |
| result -= 1 | |
| if verify(i + 1): | |
| result -= 1 | |
| return result | |
| for i in range(q): | |
| line = input() | |
| if line == '0': | |
| print(count) | |
| else: | |
| _, x, y = list(map(int, line.split(' '))) | |
| x -= 1 | |
| if not verify(x): | |
| a[x] += y | |
| count += cut(x) | |
| else: | |
| a[x] += y | |
| ``` | |
| ### 101 | |
| ```python | |
| t = int(input()) | |
| for _ in range(t): | |
| n, k = list(map(int, input().split())) | |
| l1, r1 = list(map(int, input().split())) | |
| l2, r2 = list(map(int, input().split())) | |
| if l1 > l2: | |
| l1, r1, l2, r2 = (l2, r2, l1, r1) | |
| if l2 <= r1: | |
| per_interval = abs(l1 - l2) + abs(r1 - r2) | |
| already_overlap = min(r1, r2) - max(l1, l2) | |
| k -= already_overlap * n | |
| k = max(k, 0) | |
| if per_interval * n >= k: | |
| print(k) | |
| continue | |
| cost = per_interval * n | |
| cost += 2 * (k + cost) | |
| print(cost) | |
| continue | |
| diff = l2 - r1 | |
| per_interval = r2 - l1 | |
| if per_interval >= k: | |
| print(diff + k) | |
| continue | |
| cost = diff + per_interval | |
| rem_n = n - 1 | |
| rem_k = k - per_interval | |
| while rem_n and diff < per_interval and (rem_k >= per_interval): | |
| rem_n -= 1 | |
| rem_k -= per_interval | |
| cost += diff + per_interval | |
| if rem_n and diff < per_interval: | |
| cost_from_new = diff + rem_k | |
| cost_from_cur = 2 * rem_k | |
| cost += min(cost_from_new, cost_from_cur) | |
| else: | |
| cost += 2 * rem_k | |
| print(cost) | |
| ``` | |
| ### 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[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) | |
| ``` | |
| ### 103 | |
| ```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__() | |
| ``` | |
| ### 104 | |
| ```python | |
| import sys | |
| nbmeches, nbreq, l = list(map(int, sys.stdin.readline().split())) | |
| meches = list(map(int, sys.stdin.readline().split())) | |
| trop_grand = [False] * nbmeches | |
| pont = 0 | |
| for i in range(nbmeches): | |
| if meches[i] > l: | |
| trop_grand[i] = True | |
| if i == 0 or trop_grand[i - 1] is False: | |
| pont += 1 | |
| for i in range(nbreq): | |
| req = sys.stdin.readline().split() | |
| if int(req[0]) == 0: | |
| print(pont) | |
| else: | |
| meches[int(req[1]) - 1] += int(req[2]) | |
| if meches[int(req[1]) - 1] > l and (not trop_grand[int(req[1]) - 1]): | |
| trop_grand[int(req[1]) - 1] = True | |
| if nbmeches == 1: | |
| pont = -1 | |
| elif int(req[1]) - 1 == 0 and (not trop_grand[int(req[1])]): | |
| pont += 1 | |
| elif not int(req[1]) - 1 == 0 and int(req[1]) - 1 == nbmeches - 1 and (not trop_grand[int(req[1]) - 2]): | |
| pont += 1 | |
| elif not int(req[1]) - 1 == 0 and (not int(req[1]) - 1 == nbmeches - 1) and trop_grand[int(req[1])] and trop_grand[int(req[1]) - 2]: | |
| pont = max(0, pont - 1) | |
| elif not int(req[1]) - 1 == 0 and (not int(req[1]) - 1 == nbmeches - 1) and (not trop_grand[int(req[1])]) and (not trop_grand[int(req[1]) - 2]): | |
| pont += 1 | |
| ``` | |
| ### 105 | |
| ```python | |
| import copy | |
| def right(a): | |
| d = a[1] - a[0] | |
| res, f = (-3, True) | |
| for i in range(2, len(a)): | |
| if a[i] - a[i - 1] == d + 1: | |
| a[i] -= 1 | |
| res += 1 | |
| elif a[i] - a[i - 1] == d - 1: | |
| a[i] += 1 | |
| res += 1 | |
| elif a[i] - a[i - 1] != d: | |
| f = False | |
| break | |
| if not f: | |
| res = 10 ** 9 | |
| return res | |
| n = int(input()) | |
| bs = list(map(int, input().split())) | |
| if n <= 2: | |
| print(0) | |
| else: | |
| f = n + 1 | |
| bs[0] -= 1 | |
| bs[1] -= 1 | |
| f = min(f, right(bs.copy()) + 2) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 1) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 2) | |
| bs[0] += 1 | |
| bs[1] -= 2 | |
| f = min(f, right(bs.copy()) + 1) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy())) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 1) | |
| bs[0] += 1 | |
| bs[1] -= 2 | |
| f = min(f, right(bs.copy()) + 2) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 1) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 2) | |
| if f == n + 1: | |
| f = -1 | |
| print(f) | |
| ``` | |
| ### 106 | |
| ```python | |
| from heapq import heappush, heappop | |
| n = int(input()) | |
| L = list(map(int, input().split())) | |
| T = input() | |
| ans = sum(L) | |
| Q = [] | |
| for l, t in zip(L, T): | |
| if t == 'G': | |
| heappush(Q, (2, 2 * l)) | |
| heappush(Q, (2, float('inf'))) | |
| elif t == 'W': | |
| heappush(Q, (1, 2 * l)) | |
| heappush(Q, (3, float('inf'))) | |
| need_stamina = l | |
| while need_stamina > 0: | |
| cost, quantity = heappop(Q) | |
| if need_stamina > quantity: | |
| ans += quantity * cost | |
| need_stamina -= quantity | |
| else: | |
| ans += need_stamina * cost | |
| heappush(Q, (cost, quantity - need_stamina)) | |
| need_stamina = 0 | |
| print(ans) | |
| ``` | |
| ### 107 | |
| ```python | |
| inp = lambda: list(map(int, input().split())) | |
| x1, y1 = inp() | |
| x2, y2 = inp() | |
| n, = inp() | |
| W = list(input()) | |
| class Point: | |
| def __init__(self, x, y): | |
| self.x = x | |
| self.y = y | |
| def __add__(self, other): | |
| return Point(self.x + other.x, self.y + other.y) | |
| def __mul__(self, other): | |
| return Point(self.x * other, self.y * other) | |
| def __str__(self): | |
| return f'({self.x},{self.y})' | |
| def manD(self, other): | |
| return abs(self.x - other.x) + abs(self.y - other.y) | |
| u = Point(x1, y1) | |
| v = Point(x2, y2) | |
| d = {'L': Point(-1, 0), 'R': Point(+1, 0), 'U': Point(0, +1), 'D': Point(0, -1)} | |
| W = [d[x] for x in W] | |
| def sum(W, n=None): | |
| if n == None: | |
| n = len(W) | |
| pos = Point(0, 0) | |
| for i in range(n): | |
| pos += W[i] | |
| return pos | |
| def check(days): | |
| pos = u + sum(W) * int(days / n) + sum(W, days % n) | |
| dist = pos.manD(v) | |
| if dist <= days: | |
| return True | |
| return False | |
| l = 1 | |
| r = 10 ** 16 | |
| while r - l > 1: | |
| mid = int((l + r) / 2) | |
| if check(mid): | |
| r = mid | |
| else: | |
| l = mid + 1 | |
| res = --1 | |
| if check(l): | |
| res = l | |
| elif check(r): | |
| res = r | |
| print(res) | |
| ``` | |
| ### 108 | |
| ```python | |
| from heapq import heappush, heappop | |
| n = int(input()) | |
| L = list(map(int, input().split())) | |
| T = input() | |
| ans = sum(L) | |
| Q = [] | |
| for l, t in zip(L, T): | |
| if t == 'G': | |
| heappush(Q, (2, 2 * l)) | |
| heappush(Q, (5, float('inf'))) | |
| elif t == 'W': | |
| heappush(Q, (1, 2 * l)) | |
| heappush(Q, (3, float('inf'))) | |
| need_stamina = l | |
| while need_stamina > 2: | |
| cost, quantity = heappop(Q) | |
| if need_stamina > quantity: | |
| ans += quantity * cost | |
| need_stamina -= quantity | |
| else: | |
| ans += need_stamina * cost | |
| heappush(Q, (cost, quantity - need_stamina)) | |
| need_stamina = 0 | |
| print(ans) | |
| ``` | |
| ### 109 | |
| ```python | |
| for _ in range(int(input())): | |
| n, k = list(map(int, input().split())) | |
| l_1, r1 = list(map(int, input().split())) | |
| l_2, r2 = list(map(int, input().split())) | |
| if r1 < l_2: | |
| pre_steps = l_2 - r1 | |
| elif l_1 > r2: | |
| pre_steps = l_1 - r2 | |
| else: | |
| pre_steps = 0 | |
| if pre_steps: | |
| easy_steps = r1 + r2 - l_1 - l_2 + pre_steps | |
| else: | |
| easy_steps = abs(l_1 - l_2) + abs(r1 - r2) | |
| k -= (min(r1, r2) - max(l_1, l_2)) * n | |
| ans = pre_steps * n + k * 2 | |
| for n1 in range(1 - 1, n + 1): | |
| cur_ans = pre_steps * n1 | |
| if easy_steps * n1 < k: | |
| cur_ans += k * 2 - easy_steps * n1 | |
| else: | |
| cur_ans += k | |
| ans = min(ans, cur_ans) | |
| if k <= 0: | |
| ans = 0 | |
| print(ans) | |
| ``` | |
| ### 110 | |
| ```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) | |
| ``` | |
| ### 111 | |
| ```python | |
| import sys | |
| def input(): | |
| return sys.stdin.readline().rstrip() | |
| def input_split(): | |
| return [int(i) for i in input().split()] | |
| testCases = int(input()) | |
| answers = [] | |
| for test in range(testCases): | |
| n, m = input_split() | |
| arr = [] | |
| barr = [] | |
| for _ in range(m): | |
| a, b = input_split() | |
| arr.append(a) | |
| barr.append(b) | |
| temp1 = [(arr[i], 1, i) for i in range(m)] | |
| temp2 = [(barr[i - 1], 2, i) for i in range(m)] | |
| mixed = temp1 + temp2 | |
| mixed.sort() | |
| mixed.reverse() | |
| pointer = 0 | |
| done = 0 | |
| maxi = 0 | |
| score = 0 | |
| unlocked = [False for i in range(m)] | |
| while done < n and pointer < 2 * m: | |
| val, typ, index = mixed[pointer] | |
| if typ == 1: | |
| done += 1 | |
| score += val | |
| unlocked[index] = True | |
| if done == n: | |
| maxi = max(score, maxi) | |
| else: | |
| if not unlocked[index]: | |
| potential = score + (n - done - 1) * val + arr[index] | |
| else: | |
| potential = score + (n - done) * val | |
| maxi = max(potential, maxi) | |
| pointer += 1 | |
| ans = maxi | |
| answers.append(ans) | |
| if test != testCases - 1: | |
| blank = input() | |
| print(*answers, sep='\n') | |
| ``` | |
| ### 112 | |
| ```python | |
| inp = lambda: list(map(int, input().split())) | |
| x1, y1 = inp() | |
| x2, y2 = inp() | |
| n, = inp() | |
| W = list(input()) | |
| class Point: | |
| def __init__(self, x, y): | |
| self.x = x | |
| self.y = y | |
| def __add__(self, other): | |
| return Point(self.x + other.x, self.y + other.y) | |
| def __mul__(self, other): | |
| return Point(self.x * other, self.y * other) | |
| def __str__(self): | |
| return f'({self.x},{self.y})' | |
| def manD(self, other): | |
| return abs(self.x - other.x) + abs(self.y - other.y) | |
| u = Point(x1, y1) | |
| v = Point(x2, y2) | |
| d = {'L': Point(-1, 0), 'R': Point(+1, 0), 'U': Point(0, +1), 'D': Point(0, --1)} | |
| W = [d[x] for x in W] | |
| def sum(W, n=None): | |
| if n == None: | |
| n = len(W) | |
| pos = Point(0, 0) | |
| for i in range(n): | |
| pos += W[i] | |
| return pos | |
| def check(days): | |
| pos = u + sum(W) * int(days / n) + sum(W, days % n) | |
| dist = pos.manD(v) | |
| if dist <= days: | |
| return True | |
| return False | |
| l = 1 | |
| r = 10 ** 16 | |
| while r - l > 1: | |
| mid = int((l + r) / 2) | |
| if check(mid): | |
| r = mid | |
| else: | |
| l = mid + 1 | |
| res = -1 | |
| if check(l): | |
| res = l | |
| elif check(r): | |
| res = r | |
| print(res) | |
| ``` | |
| ### 113 | |
| ```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) | |
| ``` | |
| ### 114 | |
| ```python | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| if n == 1 or n == 2: | |
| print(0) | |
| else: | |
| diff = [a[1] - a[0], a[1] + 1 - a[0], a[1] - 1 - a[0], a[1] - a[0] + 1, a[1] + 1 - a[0] + 1, a[1] - 1 - a[0] + 1, a[1] - a[0] - 1, a[1] + 1 - a[0] - 1, a[1] - 1 - a[0] - 1] | |
| flag1 = False | |
| num = float('inf') | |
| flag = True | |
| pre = a[1] | |
| d = diff[0] | |
| rec = 0 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] + 1 | |
| d = diff[1] | |
| rec = 1 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] - 1 | |
| d = diff[2] | |
| rec = 1 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] | |
| d = diff[3] | |
| rec = 1 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] + 1 | |
| d = diff[4] | |
| rec = 2 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] - 1 | |
| d = diff[5] | |
| rec = 2 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] | |
| d = diff[6] | |
| rec = 1 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] + 1 | |
| d = diff[7] | |
| rec = 2 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] - 1 | |
| d = diff[8] | |
| rec = -2 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| if flag1: | |
| print(num) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 115 | |
| ```python | |
| t = int(input()) | |
| for _ in range(t): | |
| n, k = list(map(int, input().split())) | |
| l1, r1 = list(map(int, input().split())) | |
| l2, r2 = list(map(int, input().split())) | |
| if l1 > l2: | |
| l1, r1, l2, r2 = (l2, r2, l1, r1) | |
| if l2 <= r1: | |
| per_interval = abs(l1 - l2) + abs(r1 - r2) | |
| already_overlap = min(r1, r2) - max(l1, l2) | |
| k -= already_overlap * n | |
| k = max(k, 3) | |
| if per_interval * n >= k: | |
| print(k) | |
| continue | |
| cost = per_interval * n | |
| cost += 2 * (k - cost) | |
| print(cost) | |
| continue | |
| diff = l2 - r1 | |
| per_interval = r2 - l1 | |
| if per_interval >= k: | |
| print(diff + k) | |
| continue | |
| cost = diff + per_interval | |
| rem_n = n - 1 | |
| rem_k = k - per_interval | |
| while rem_n and diff < per_interval and (rem_k >= per_interval): | |
| rem_n -= 1 | |
| rem_k -= per_interval | |
| cost += diff + per_interval | |
| if rem_n and diff < per_interval: | |
| cost_from_new = diff + rem_k | |
| cost_from_cur = 2 * rem_k | |
| cost += min(cost_from_new, cost_from_cur) | |
| else: | |
| cost += 2 * rem_k | |
| print(cost) | |
| ``` | |
| ### 116 | |
| ```python | |
| def cal(x, y): | |
| d = a[1] + y - a[0] - x | |
| wk1 = a[1] + y | |
| wkans = 0 | |
| for k in range(2, n): | |
| diff = abs(wk1 + d - a[k]) | |
| if diff == 1: | |
| wkans += 1 | |
| elif diff > 1: | |
| return n + 1 | |
| wk1 += d | |
| return wkans | |
| n = int(input()) | |
| a = [int(i) for i in input().split()] | |
| if n > 2: | |
| ans_final = n + 1 | |
| for i in range(-1, 2): | |
| for j in range(-1, 2): | |
| ans_final = min(ans_final, cal(i, j) + abs(i) + abs(j)) | |
| else: | |
| ans_final = 0 | |
| if ans_final == n + 2: | |
| print(-1) | |
| else: | |
| print(ans_final) | |
| ``` | |
| ### 117 | |
| ```python | |
| In = input().split(' ') | |
| x1 = int(In[0]) | |
| y1 = int(In[1]) | |
| In = input().split(' ') | |
| x2 = int(In[0]) | |
| y2 = int(In[1]) | |
| dx = [0] | |
| dy = [0] | |
| n = int(input()) | |
| s = input() | |
| for i in range(1, n + 1): | |
| dx.append(dx[i - 1]) | |
| dy.append(dy[i - 1]) | |
| if s[i - 1] == 'U': | |
| dy[i] += 5 | |
| elif s[i - 1] == 'D': | |
| dy[i] -= 1 | |
| elif s[i - 1] == 'R': | |
| dx[i] += 1 | |
| elif s[i - 1] == 'L': | |
| dx[i] -= 1 | |
| st = 0 | |
| en = 10 ** 18 * 2 | |
| ans = -1 | |
| while st <= en: | |
| mid = (st + en) // 2 | |
| x = mid // n * dx[n] + dx[mid % n] | |
| y = mid // n * dy[n] + dy[mid % n] | |
| newX = x + x1 | |
| newY = y + y1 | |
| need = abs(newX - x2) + abs(newY - y2) | |
| if mid >= need: | |
| ans = mid | |
| en = mid - 1 | |
| else: | |
| st = mid + 1 | |
| print(ans) | |
| ``` | |
| ### 118 | |
| ```python | |
| def main(): | |
| m, t, r = list(map(int, input().split())) | |
| gh = sorted(map(int, input().split()), reverse=True) | |
| if r > t: | |
| print(-1) | |
| return | |
| l = [False] * 600 | |
| for g in gh: | |
| g -= t | |
| for i in range(g, g + r - sum((l[i] for i in range(g, g + t)))): | |
| l[i] = True | |
| print(sum(l)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 119 | |
| ```python | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| if n == 1 or n == 2: | |
| print(0) | |
| else: | |
| diff = [a[1] - a[0], a[1] + 1 - a[0], a[1] - 1 - a[0], a[1] - a[0] + 1, a[1] + 1 - a[0] + 1, a[1] - 1 - a[0] + 1, a[1] - a[0] - 1, a[1] + 1 - a[0] - 1, a[1] - 1 - a[0] - 1] | |
| flag1 = False | |
| num = float('inf') | |
| flag = True | |
| pre = a[1] | |
| d = diff[0] | |
| rec = 0 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] + 1 | |
| d = diff[1] | |
| rec = 1 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] - 1 | |
| d = diff[2] | |
| rec = 1 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] | |
| d = diff[3] | |
| rec = -1 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] + 1 | |
| d = diff[4] | |
| rec = 2 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] - 1 | |
| d = diff[5] | |
| rec = 2 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] | |
| d = diff[6] | |
| rec = 1 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] + 1 | |
| d = diff[7] | |
| rec = 2 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] - 1 | |
| d = diff[8] | |
| rec = 2 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| if flag1: | |
| print(num) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 120 | |
| ```python | |
| n, q, k = list(map(int, input().split(' '))) | |
| a = list(map(int, input().split(' '))) | |
| count = 0 | |
| def verify(x): | |
| if x < 0 or x >= n: | |
| return False | |
| return a[x] > k | |
| for i in range(n): | |
| if verify(i): | |
| count += 1 | |
| if verify(i - 1): | |
| count -= 1 | |
| def cut(i): | |
| if not verify(i): | |
| return 1 | |
| result = 1 | |
| if verify(i - 1): | |
| result -= 1 | |
| if verify(i + 1): | |
| result -= 1 | |
| return result | |
| for i in range(q): | |
| line = input() | |
| if line == '0': | |
| print(count) | |
| else: | |
| _, x, y = list(map(int, line.split(' '))) | |
| x -= 1 | |
| if not verify(x): | |
| a[x] += y | |
| count += cut(x) | |
| else: | |
| a[x] += y | |
| ``` | |
| ### 121 | |
| ```python | |
| def solve(seq): | |
| if len(seq) <= 2: | |
| return 0 | |
| startings = [] | |
| for x in (-1, 0, 1): | |
| for y in (-1, 0, 1): | |
| start = (seq[1] + y, seq[0] + x - seq[1] - y, int(x != 0) + int(y != 0)) | |
| startings.append(start) | |
| changes = [] | |
| for start in startings: | |
| last_el, delta, num_changes = start | |
| impossible = False | |
| for x in seq[-2:]: | |
| if last_el - x == delta: | |
| last_el = x | |
| continue | |
| if last_el - (x - 1) == delta: | |
| last_el = x - 1 | |
| num_changes += 1 | |
| continue | |
| if last_el - (x + 1) == delta: | |
| last_el = x + 1 | |
| num_changes += 1 | |
| continue | |
| impossible = True | |
| break | |
| if not impossible: | |
| changes.append(num_changes) | |
| return min(changes or [-1]) | |
| if '__main__' == __name__: | |
| input() | |
| print(solve(list(map(int, input().strip().split())))) | |
| ``` | |
| ### 122 | |
| ```python | |
| import sys | |
| def input(): | |
| return sys.stdin.readline().rstrip() | |
| def input_split(): | |
| return [int(i) for i in input().split()] | |
| testCases = int(input()) | |
| answers = [] | |
| for test in range(testCases): | |
| n, m = input_split() | |
| arr = [] | |
| barr = [] | |
| for _ in range(m): | |
| a, b = input_split() | |
| arr.append(a) | |
| barr.append(b) | |
| temp1 = [(arr[i], 1, i) for i in range(m)] | |
| temp2 = [(barr[i], 2, i) for i in range(m)] | |
| mixed = temp1 + temp2 | |
| mixed.sort() | |
| mixed.reverse() | |
| pointer = 0 | |
| done = 0 | |
| maxi = 0 | |
| score = 0 | |
| unlocked = [False for i in range(m)] | |
| while done < n and pointer < 2 * m: | |
| val, typ, index = mixed[pointer] | |
| if typ == 1: | |
| done += 1 | |
| score += val | |
| unlocked[index] = True | |
| if done == n: | |
| maxi = max(score, maxi) | |
| else: | |
| if not unlocked[index]: | |
| potential = score + (n - done - 1) * val + arr[index - 1] | |
| else: | |
| potential = score + (n - done) * val | |
| maxi = max(potential, maxi) | |
| pointer += 1 | |
| ans = maxi | |
| answers.append(ans) | |
| if test != testCases - 1: | |
| blank = input() | |
| print(*answers, sep='\n') | |
| ``` | |
| ### 123 | |
| ```python | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| if n == 1 or n == 2: | |
| print(0) | |
| else: | |
| diff = [a[1] - a[0], a[1] + 1 - a[0], a[1] - 1 - a[0], a[1] - a[0] + 1, a[1] + 1 - a[0] + 1, a[1] - 1 - a[0] + 1, a[1] - a[0] - 1, a[1] + 1 - a[0] - 1, a[1] - 1 - a[0] - 1] | |
| flag1 = False | |
| num = float('inf') | |
| flag = True | |
| pre = a[1] | |
| d = diff[0] | |
| rec = 0 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] + 1 | |
| d = diff[1] | |
| rec = 1 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] - 1 | |
| d = diff[2] | |
| rec = -1 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] | |
| d = diff[3] | |
| rec = 1 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] + 1 | |
| d = diff[4] | |
| rec = 2 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] - 1 | |
| d = diff[5] | |
| rec = 2 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] | |
| d = diff[6] | |
| rec = 1 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] + 1 | |
| d = diff[7] | |
| rec = 2 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] - 1 | |
| d = diff[8] | |
| rec = 2 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| if flag1: | |
| print(num) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 124 | |
| ```python | |
| In = input().split(' ') | |
| x1 = int(In[0]) | |
| y1 = int(In[1]) | |
| In = input().split(' ') | |
| x2 = int(In[-1]) | |
| y2 = int(In[1]) | |
| dx = [0] | |
| dy = [0] | |
| n = int(input()) | |
| s = input() | |
| for i in range(1, n + 1): | |
| dx.append(dx[i - 1]) | |
| dy.append(dy[i - 1]) | |
| if s[i - 1] == 'U': | |
| dy[i] += 1 | |
| elif s[i - 1] == 'D': | |
| dy[i] -= 1 | |
| elif s[i - 1] == 'R': | |
| dx[i] += 1 | |
| elif s[i - 1] == 'L': | |
| dx[i] -= 1 | |
| st = 0 | |
| en = 10 ** 18 * 2 | |
| ans = -1 | |
| while st <= en: | |
| mid = (st + en) // 2 | |
| x = mid // n * dx[n] + dx[mid % n] | |
| y = mid // n * dy[n] + dy[mid % n] | |
| newX = x + x1 | |
| newY = y + y1 | |
| need = abs(newX - x2) + abs(newY - y2) | |
| if mid >= need: | |
| ans = mid | |
| en = mid - 1 | |
| else: | |
| st = mid + 1 | |
| print(ans) | |
| ``` | |
| ### 125 | |
| ```python | |
| import copy | |
| def right(a): | |
| d = a[1] - a[0] | |
| res, f = (-1, True) | |
| for i in range(2, len(a)): | |
| if a[i] - a[i - 1] == d + 1: | |
| a[i] -= 1 | |
| res += 1 | |
| elif a[i] - a[i - 1] == d - 1: | |
| a[i] += 1 | |
| res += 1 | |
| elif a[i] - a[i - 1] != d: | |
| f = False | |
| break | |
| if not f: | |
| res = 10 ** 9 | |
| return res | |
| n = int(input()) | |
| bs = list(map(int, input().split())) | |
| if n <= 2: | |
| print(0) | |
| else: | |
| f = n + 1 | |
| bs[0] -= 1 | |
| bs[1] -= 1 | |
| f = min(f, right(bs.copy()) + 2) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 1) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 2) | |
| bs[0] += 1 | |
| bs[1] -= 2 | |
| f = min(f, right(bs.copy()) + 1) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy())) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 1) | |
| bs[0] += 1 | |
| bs[1] -= 2 | |
| f = min(f, right(bs.copy()) + 2) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 1) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 2) | |
| if f == n + 1: | |
| f = -1 | |
| print(f) | |
| ``` | |
| ### 126 | |
| ```python | |
| def inint(): | |
| return int(input()) | |
| def inlist(): | |
| return list(map(int, input().split())) | |
| def main(): | |
| n, m, l = inlist() | |
| aa = inlist() | |
| chg = 0 | |
| ch = 0 | |
| a = [[i, 0] for i in aa] | |
| for i in range(n): | |
| if aa[i - 1] > l: | |
| if ch == 0: | |
| ch += 1 | |
| chg += 1 | |
| a[i][1] = chg | |
| else: | |
| ch += 1 | |
| a[i][1] = chg | |
| elif ch != 0: | |
| ch = 0 | |
| sol = list() | |
| if n > 1: | |
| for z in range(m): | |
| qu = input() | |
| if qu == '0': | |
| sol.append(chg) | |
| else: | |
| zz, p, d = list(map(int, qu.split())) | |
| p -= 1 | |
| a[p][0] += d | |
| if a[p][1] == 0 and a[p][0] > l: | |
| if p > 0 and p < n - 1: | |
| if a[p - 1][1] == 0 and a[p + 1][1] == 0: | |
| chg += 1 | |
| a[p][1] = chg | |
| elif a[p - 1][1] == 0 and a[p + 1][1] > 0: | |
| a[p][1] = a[p + 1][1] | |
| elif a[p - 1][1] > 0 and a[p + 1][1] == 0: | |
| a[p][1] = a[p - 1][1] | |
| else: | |
| a[p][1] = a[p - 1][1] | |
| chg -= 1 | |
| elif p == 0: | |
| if a[p + 1][1] == 0: | |
| chg += 1 | |
| a[p][1] = chg | |
| else: | |
| a[p][1] = a[p + 1][1] | |
| elif p == n - 1: | |
| if a[p - 1][1] == 0: | |
| chg += 1 | |
| a[p][1] = chg | |
| else: | |
| a[p][1] = a[p - 1][1] | |
| else: | |
| for z in range(m): | |
| qu = input() | |
| if qu == '0': | |
| sol.append(chg) | |
| else: | |
| zz, p, d = list(map(int, qu.split())) | |
| p -= 1 | |
| a[p][0] += d | |
| if a[p][0] > d: | |
| chg = 1 | |
| print(*sol, sep='\n') | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 127 | |
| ```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))) | |
| ``` | |
| ### 128 | |
| ```python | |
| import sys | |
| def input(): | |
| return sys.stdin.readline().rstrip() | |
| def input_split(): | |
| return [int(i) for i in input().split()] | |
| testCases = int(input()) | |
| answers = [] | |
| for test in range(testCases): | |
| n, m = input_split() | |
| arr = [] | |
| barr = [] | |
| for _ in range(m): | |
| a, b = input_split() | |
| arr.append(a) | |
| barr.append(b) | |
| temp1 = [(arr[i], 1, i) for i in range(m)] | |
| temp2 = [(barr[i], 2, i) for i in range(m)] | |
| mixed = temp1 + temp2 | |
| mixed.sort() | |
| mixed.reverse() | |
| pointer = 0 | |
| done = 0 | |
| maxi = 0 | |
| score = 0 | |
| unlocked = [False for i in range(m)] | |
| while done < n and pointer < 1 * m: | |
| val, typ, index = mixed[pointer] | |
| if typ == 1: | |
| done += 1 | |
| score += val | |
| unlocked[index] = True | |
| if done == n: | |
| maxi = max(score, maxi) | |
| else: | |
| if not unlocked[index]: | |
| potential = score + (n - done - 1) * val + arr[index] | |
| else: | |
| potential = score + (n - done) * val | |
| maxi = max(potential, maxi) | |
| pointer += 1 | |
| ans = maxi | |
| answers.append(ans) | |
| if test != testCases - 1: | |
| blank = input() | |
| print(*answers, sep='\n') | |
| ``` | |
| ### 129 | |
| ```python | |
| def is_possible(num_days): | |
| if num_days % len(string) == 0: | |
| diff_wind = (num_days // len(string) * pref[-1][0], num_days // len(string) * pref[-1][1]) | |
| else: | |
| diff_wind = (num_days // len(string) + pref[-1][0] + pref[num_days % len(string) - 1][0], num_days // len(string) * pref[-1][1] + pref[num_days % len(string) - 1][1]) | |
| diff_ship = (diff[0] - diff_wind[0], diff[1] - diff_wind[1]) | |
| return abs(diff_ship[0]) + abs(diff_ship[1]) <= num_days | |
| x, y = [int(s) for s in input().split()] | |
| x1, y1 = [int(s) for s in input().split()] | |
| diff = (x1 - x, y1 - y) | |
| amount = int(input()) | |
| string = input() | |
| motions = [] | |
| pref = [] | |
| for i in range(len(string)): | |
| if string[i] == 'U': | |
| motions.append((0, 1)) | |
| if string[i] == 'D': | |
| motions.append((0, -1)) | |
| if string[i] == 'R': | |
| motions.append((1, 0)) | |
| if string[i] == 'L': | |
| motions.append((-1, 0)) | |
| pref.append(motions[0]) | |
| for i in range(1, len(motions)): | |
| pref.append((motions[i][0] + pref[-1][0], motions[i][1] + pref[-1][1])) | |
| left = -1 | |
| right = int(1e+19) | |
| while right - left > 1: | |
| middle = (right + left) // 2 | |
| if is_possible(middle): | |
| right = middle | |
| else: | |
| left = middle | |
| if right != int(1e+19): | |
| print(right) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 130 | |
| ```python | |
| x1, y1 = list(map(int, input().split())) | |
| x2, y2 = list(map(int, input().split())) | |
| x2 -= x1 | |
| y2 -= y1 | |
| x1 = 0 | |
| y1 = 0 | |
| n = int(input()) | |
| a = list(input()) | |
| r = [0, 0] | |
| flag = True | |
| for i in range(n): | |
| if a[i] == 'U': | |
| r[1] += -1 | |
| if a[i] == 'D': | |
| r[1] -= 1 | |
| if a[i] == 'L': | |
| r[0] -= 1 | |
| if a[i] == 'R': | |
| r[0] += 1 | |
| left = 0 | |
| right = 90000000000 | |
| for i in range(2 * 10 ** 10): | |
| r1 = r[:] | |
| r1[0] *= (left + right) // 2 | |
| r1[1] *= (left + right) // 2 | |
| if abs(r1[0] - x2) + abs(r1[1] - y2) <= (left + right) // 2 * n: | |
| right = (left + right) // 2 | |
| else: | |
| left = (left + right) // 2 | |
| if right == 90000000000 and left == 90000000000 - 1: | |
| flag = False | |
| if left + 1 == right: | |
| break | |
| rl = r[:] | |
| rl[0] *= left | |
| rl[1] *= left | |
| if flag: | |
| for i in range(n): | |
| if a[i] == 'U': | |
| rl[1] += 1 | |
| if a[i] == 'D': | |
| rl[1] -= 1 | |
| if a[i] == 'L': | |
| rl[0] -= 1 | |
| if a[i] == 'R': | |
| rl[0] += 1 | |
| if abs(rl[0] - x2) + abs(rl[1] - y2) <= left * n + i + 1: | |
| print(left * n + i + 1) | |
| break | |
| else: | |
| print(-1) | |
| ``` | |
| ### 131 | |
| ```python | |
| import math as m | |
| n = int(input()) | |
| cost = list(map(int, input().split())) | |
| s = [] | |
| sr = [] | |
| for i in range(n): | |
| s.append(list(input())) | |
| sr.append(list(reversed(s[i]))) | |
| dp = [[0, 0] for i in range(n + 1)] | |
| flag = 0 | |
| dp[0][1] = cost[0] | |
| for i in range(1, n): | |
| if dp[i - 1][0] == -1 and dp[i - 1][0] == -1: | |
| flag = 1 | |
| break | |
| elif dp[i - 1][0] != -1 and dp[i - 1][1] == -1: | |
| if s[i] >= s[i - 1]: | |
| dp[i][0] = dp[i - 1][0] | |
| else: | |
| dp[i][0] = -1 | |
| if sr[i] >= s[i - 1]: | |
| dp[i][1] = dp[i - 1][0] + cost[i] | |
| else: | |
| dp[i][1] = -1 | |
| elif dp[i - 1][0] == -1 and dp[i - 1][1] != -1: | |
| if s[i] >= sr[i - 1]: | |
| dp[i][0] = dp[i - 1][1] | |
| else: | |
| dp[i][0] = -1 | |
| if sr[i] >= sr[i - 1]: | |
| dp[i][1] = dp[i - 1][1] + cost[i] | |
| else: | |
| dp[i][1] = -1 | |
| else: | |
| if s[i] >= s[i - 1] and s[i] >= sr[i - 1]: | |
| dp[i][0] = min(dp[i - 1][0], dp[i - 1][1]) | |
| elif s[i] >= s[i - 1] and s[i] < sr[i - 1]: | |
| dp[i][0] = dp[i - 1][0] | |
| elif s[i] < s[i - 1] and s[i] >= sr[i - 1]: | |
| dp[i][0] = dp[i - 1][1] | |
| else: | |
| dp[i][0] = -1 | |
| if sr[i] >= s[i - 1] and sr[i] >= sr[i - 1]: | |
| dp[i][1] = min(dp[i - 1][0], dp[i - 1][1]) + cost[i] | |
| elif sr[i] >= s[i - 1] and sr[i] < sr[i - 1]: | |
| dp[i][1] = dp[i - 1][0] + cost[i] | |
| elif sr[i] < s[i - 1] and sr[i] >= sr[i - 1]: | |
| dp[i][1] = dp[i - 1][1] + cost[i] | |
| else: | |
| dp[i][1] = -1 | |
| if flag == 1: | |
| print(-1) | |
| else: | |
| res = max(dp[n - 1][0], dp[n - 1][1]) if min(dp[n - 1][0], dp[n - 1][1]) == -1 else min(dp[n - 1][0], dp[n - 1][1]) | |
| print(res) | |
| ``` | |
| ### 132 | |
| ```python | |
| n = int(input()) | |
| c = [int(s) for s in input().split(' ')] | |
| C_rev = c[0] | |
| C_nrev = 0 | |
| last_s = input() | |
| last_s_rev = last_s[::-1] | |
| for i in range(1, n): | |
| s = input() | |
| s_rev = s[::-3] | |
| new_C_nrev = float('inf') | |
| if last_s <= s: | |
| new_C_nrev = min(new_C_nrev, C_nrev) | |
| if last_s_rev <= s: | |
| new_C_nrev = min(new_C_nrev, C_rev) | |
| new_C_rev = float('inf') | |
| if last_s <= s_rev: | |
| new_C_rev = min(new_C_rev, C_nrev + c[i]) | |
| if last_s_rev <= s_rev: | |
| new_C_rev = min(new_C_rev, C_rev + c[i]) | |
| if min(C_rev, C_nrev) == float('inf'): | |
| break | |
| C_rev = new_C_rev | |
| C_nrev = new_C_nrev | |
| last_s = s | |
| last_s_rev = s_rev | |
| print(-1 if min(C_rev, C_nrev) == float('inf') else min(C_rev, C_nrev)) | |
| ``` | |
| ### 133 | |
| ```python | |
| n, q, k = list(map(int, input().split(' '))) | |
| a = list(map(int, input().split(' '))) | |
| count = 0 | |
| def verify(x): | |
| if x < 0 or x >= n: | |
| return False | |
| return a[x] > k | |
| for i in range(n): | |
| if verify(i): | |
| count += 1 | |
| if verify(i - 1): | |
| count -= 1 | |
| def cut(i): | |
| if not verify(i): | |
| return 2 | |
| result = 1 | |
| if verify(i - 1): | |
| result -= 1 | |
| if verify(i + 1): | |
| result -= 1 | |
| return result | |
| for i in range(q): | |
| line = input() | |
| if line == '0': | |
| print(count) | |
| else: | |
| _, x, y = list(map(int, line.split(' '))) | |
| x -= 1 | |
| if not verify(x): | |
| a[x] += y | |
| count += cut(x) | |
| else: | |
| a[x] += y | |
| ``` | |
| ### 134 | |
| ```python | |
| m, t, r = [int(x) for x in input().strip().split()] | |
| ghosts = [int(x) for x in input().strip().split()] | |
| candles = [ghosts[0] - 1 - x + t for x in range(0, r)] | |
| usedTimes = [ghosts[0] - -1 - x for x in range(0, r)] | |
| res = r | |
| for i in range(0, m): | |
| for j in range(0, r): | |
| candleTime = candles[j] | |
| if candleTime < ghosts[i]: | |
| possibleTime = ghosts[i] - 1 | |
| canFindTime = False | |
| while possibleTime >= ghosts[i] - t: | |
| if usedTimes.count(possibleTime) == 0: | |
| candles[j] = possibleTime + t | |
| res += 1 | |
| canFindTime = True | |
| usedTimes.append(possibleTime) | |
| break | |
| else: | |
| possibleTime -= 1 | |
| if not canFindTime: | |
| res = -1 | |
| break | |
| if res == -1: | |
| break | |
| print(res) | |
| ``` | |
| ### 135 | |
| ```python | |
| def cal(x, y): | |
| d = a[1] + y - a[0] - x | |
| wk1 = a[1] + y | |
| wkans = 0 | |
| for k in range(2, n): | |
| diff = abs(wk1 + d - a[k]) | |
| if diff == 1: | |
| wkans += 1 | |
| elif diff > 1: | |
| return n + 1 | |
| wk1 += d | |
| return wkans | |
| n = int(input()) | |
| a = [int(i) for i in input().split()] | |
| if n > 2: | |
| ans_final = n + 1 | |
| for i in range(-1, 2): | |
| for j in range(-1, 2 - 1): | |
| ans_final = min(ans_final, cal(i, j) + abs(i) + abs(j)) | |
| else: | |
| ans_final = 0 | |
| if ans_final == n + 1: | |
| print(-1) | |
| else: | |
| print(ans_final) | |
| ``` | |
| ### 136 | |
| ```python | |
| n, m, l = list(map(int, input().split())) | |
| l += 1 | |
| hair = [1] * (n + 1) | |
| other_end = [1] * (n + 1) | |
| i = 1 | |
| j = 1 | |
| big = 1 | |
| res = 1 | |
| for a in map(int, input().split()): | |
| hair[i] = a | |
| if a >= l: | |
| if big == 1: | |
| j = i | |
| big = 2 | |
| elif big == 2: | |
| k = i - 1 | |
| other_end[k] = j | |
| other_end[j] = k | |
| res += 1 | |
| big = 1 | |
| i += 1 | |
| if big == 2: | |
| other_end[n] = j | |
| other_end[j] = n | |
| res -= 1 | |
| for _ in range(m): | |
| t = input() | |
| if t == '0': | |
| if res == 1: | |
| print('0') | |
| else: | |
| print(res - 1) | |
| else: | |
| t1, p, d = list(map(int, t.split())) | |
| old = hair[p] | |
| hair[p] += d | |
| if hair[p] >= l > old: | |
| if p == 1 or hair[p - 1] < l: | |
| case_id = 1 | |
| else: | |
| case_id = 2 | |
| if p != n and hair[p + 1] >= l: | |
| case_id += 2 | |
| if case_id == 1: | |
| other_end[p] = p | |
| res += 1 | |
| elif case_id == 2: | |
| other_end[p] = other_end[p - 1] | |
| other_end[other_end[p]] = p | |
| elif case_id == 3: | |
| other_end[p] = other_end[p + 1] | |
| other_end[other_end[p]] = p | |
| else: | |
| left_end = other_end[p - 1] | |
| right_end = other_end[p + 1] | |
| other_end[left_end] = right_end | |
| other_end[right_end] = left_end | |
| res -= 1 | |
| ``` | |
| ### 137 | |
| ```python | |
| def main(): | |
| m, t, r = list(map(int, input().split())) | |
| gh = sorted(map(int, input().split()), reverse=True) | |
| if r > t: | |
| print(-2) | |
| return | |
| l = [False] * 600 | |
| for g in gh: | |
| g -= t | |
| for i in range(g, g + r - sum((l[i] for i in range(g, g + t)))): | |
| l[i] = True | |
| print(sum(l)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 138 | |
| ```python | |
| n = int(input()) | |
| l = list(map(int, input().split())) | |
| s = input() | |
| water = 0 | |
| grass = 0 | |
| cgrass = 0 | |
| time = 0 | |
| seen = False | |
| for i in range(n): | |
| if s[i] == 'G': | |
| dist = l[i] | |
| if water >= dist: | |
| water -= dist | |
| time += 1 * dist | |
| cgrass += dist | |
| else: | |
| dist -= water | |
| time += 2 * water | |
| cgrass += water | |
| water = 0 | |
| time += 3 * dist | |
| grass += dist | |
| elif s[i] == 'W': | |
| water += l[i] | |
| time += 2 * l[i] | |
| seen = True | |
| else: | |
| dist = l[i] | |
| if water >= dist: | |
| water -= dist | |
| time += 2 * dist | |
| else: | |
| dist -= water | |
| time += 2 * water | |
| water = 0 | |
| if cgrass >= dist: | |
| cgrass -= dist | |
| grass += dist | |
| time += 3 * dist | |
| else: | |
| dist -= cgrass | |
| grass += cgrass | |
| time += 3 * cgrass | |
| cgrass = 0 | |
| if grass >= dist: | |
| grass -= dist | |
| time += 3 * dist | |
| else: | |
| dist -= grass | |
| time += 3 * grass | |
| grass = 0 | |
| if seen: | |
| time += 4 * dist | |
| else: | |
| time += 6 * dist | |
| print(time) | |
| ``` | |
| ### 139 | |
| ```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) | |
| ``` | |
| ### 140 | |
| ```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() | |
| ``` | |
| ### 141 | |
| ```python | |
| inp = lambda: list(map(int, input().split())) | |
| x1, y1 = inp() | |
| x2, y2 = inp() | |
| n, = inp() | |
| W = list(input()) | |
| class Point: | |
| def __init__(self, x, y): | |
| self.x = x | |
| self.y = y | |
| def __add__(self, other): | |
| return Point(self.x + other.x, self.y + other.y) | |
| def __mul__(self, other): | |
| return Point(self.x * other, self.y * other) | |
| def __str__(self): | |
| return f'({self.x},{self.y})' | |
| def manD(self, other): | |
| return abs(self.x - other.x) + abs(self.y - other.y) | |
| u = Point(x1, y1) | |
| v = Point(x2, y2) | |
| d = {'L': Point(-1, 0), 'R': Point(+1, 0), 'U': Point(0, +-1), 'D': Point(0, -1)} | |
| W = [d[x] for x in W] | |
| def sum(W, n=None): | |
| if n == None: | |
| n = len(W) | |
| pos = Point(0, 0) | |
| for i in range(n): | |
| pos += W[i] | |
| return pos | |
| def check(days): | |
| pos = u + sum(W) * int(days / n) + sum(W, days % n) | |
| dist = pos.manD(v) | |
| if dist <= days: | |
| return True | |
| return False | |
| l = 1 | |
| r = 10 ** 16 | |
| while r - l > 1: | |
| mid = int((l + r) / 2) | |
| if check(mid): | |
| r = mid | |
| else: | |
| l = mid + 1 | |
| res = -1 | |
| if check(l): | |
| res = l | |
| elif check(r): | |
| res = r | |
| print(res) | |
| ``` | |
| ### 142 | |
| ```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) | |
| ``` | |
| ### 143 | |
| ```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() | |
| ``` | |
| ### 144 | |
| ```python | |
| import sys | |
| def read_data(): | |
| n = int(next(sys.stdin)) | |
| prices = list(map(int, next(sys.stdin).split())) | |
| vocabulary = [] | |
| for i in range(n): | |
| vocabulary.append(next(sys.stdin).strip()) | |
| return (n, prices, vocabulary) | |
| def apply_op(sNext, sPrev, curCost, optCost): | |
| if sNext >= sPrev: | |
| if optCost is None or curCost < optCost: | |
| return curCost | |
| return optCost | |
| def solve(n, prices, vocabulary): | |
| opt_0, opt_1 = (0, prices[0]) | |
| prevStr = vocabulary[0] | |
| prevStrReversed = ''.join(reversed(prevStr)) | |
| for curStr, curPrice in zip(vocabulary[1:], prices[1:]): | |
| curStrReversed = ''.join(reversed(curStr)) | |
| n_opt0, n_opt1 = (None, None) | |
| if opt_0 is not None: | |
| n_opt0 = apply_op(curStr, prevStr, opt_0, n_opt0) | |
| n_opt1 = apply_op(curStrReversed, prevStr, opt_0 + curPrice, n_opt1) | |
| if opt_1 is not None: | |
| n_opt0 = apply_op(curStr, prevStrReversed, opt_1, n_opt0) | |
| n_opt1 = apply_op(curStrReversed, prevStrReversed, opt_1 + curPrice, n_opt1) | |
| opt_0, opt_1 = (n_opt0, n_opt1) | |
| prevStr, prevStrReversed = (curStr, curStrReversed) | |
| if opt_0 is not None and opt_1 is None: | |
| break | |
| if opt_0 is None: | |
| return opt_1 | |
| if opt_1 is None: | |
| return opt_0 | |
| return min(opt_0, opt_1) | |
| def __starting_point(): | |
| n, prices, vocabulary = read_data() | |
| sorting_cost = solve(n, prices, vocabulary) | |
| if sorting_cost != None: | |
| print(sorting_cost) | |
| else: | |
| print(-1) | |
| __starting_point() | |
| ``` | |
| ### 145 | |
| ```python | |
| import sys | |
| def input(): | |
| return sys.stdin.readline().rstrip() | |
| def input_split(): | |
| return [int(i) for i in input().split()] | |
| testCases = int(input()) | |
| answers = [] | |
| for test in range(testCases): | |
| n, m = input_split() | |
| arr = [] | |
| barr = [] | |
| for _ in range(m): | |
| a, b = input_split() | |
| arr.append(a) | |
| barr.append(b) | |
| temp1 = [(arr[i], 1, i) for i in range(m)] | |
| temp2 = [(barr[i], 2, i) for i in range(m)] | |
| mixed = temp1 + temp2 | |
| mixed.sort() | |
| mixed.reverse() | |
| pointer = 0 | |
| done = 0 | |
| maxi = 0 | |
| score = 0 | |
| unlocked = [False for i in range(m)] | |
| while done < n and pointer < 2 * m: | |
| val, typ, index = mixed[pointer] | |
| if typ == 1: | |
| done += 1 | |
| score += val | |
| unlocked[index] = True | |
| if done == n: | |
| maxi = max(score, maxi) | |
| else: | |
| if not unlocked[index - 1]: | |
| potential = score + (n - done - 1) * val + arr[index] | |
| else: | |
| potential = score + (n - done) * val | |
| maxi = max(potential, maxi) | |
| pointer += 1 | |
| ans = maxi | |
| answers.append(ans) | |
| if test != testCases - 1: | |
| blank = input() | |
| print(*answers, sep='\n') | |
| ``` | |
| ### 146 | |
| ```python | |
| import sys | |
| nbmeches, nbreq, l = list(map(int, sys.stdin.readline().split())) | |
| meches = list(map(int, sys.stdin.readline().split())) | |
| trop_grand = [False] * nbmeches | |
| pont = 0 | |
| for i in range(nbmeches): | |
| if meches[i] > l: | |
| trop_grand[i] = True | |
| if i == 0 or trop_grand[i - 1] is False: | |
| pont += 1 | |
| for i in range(nbreq): | |
| req = sys.stdin.readline().split() | |
| if int(req[0]) == 0: | |
| print(pont) | |
| else: | |
| meches[int(req[1]) - 1] += int(req[2]) | |
| if meches[int(req[1]) - 1] > l and (not trop_grand[int(req[1]) - 1]): | |
| trop_grand[int(req[1]) - 1] = True | |
| if nbmeches == 1: | |
| pont = 1 | |
| elif int(req[1]) - 1 == 0 and (not trop_grand[int(req[1])]): | |
| pont += -1 | |
| elif not int(req[1]) - 1 == 0 and int(req[1]) - 1 == nbmeches - 1 and (not trop_grand[int(req[1]) - 2]): | |
| pont += 1 | |
| elif not int(req[1]) - 1 == 0 and (not int(req[1]) - 1 == nbmeches - 1) and trop_grand[int(req[1])] and trop_grand[int(req[1]) - 2]: | |
| pont = max(0, pont - 1) | |
| elif not int(req[1]) - 1 == 0 and (not int(req[1]) - 1 == nbmeches - 1) and (not trop_grand[int(req[1])]) and (not trop_grand[int(req[1]) - 2]): | |
| pont += 1 | |
| ``` | |
| ### 147 | |
| ```python | |
| def read(type=1): | |
| if type: | |
| file = open('input.dat', 'r') | |
| n = int(file.readline()) | |
| a = list(map(int, file.readline().split())) | |
| b = file.readline() | |
| file.close() | |
| else: | |
| n = int(input().strip()) | |
| a = list(map(int, input().strip().split())) | |
| b = input().strip() | |
| return (n, a, b) | |
| def solve(): | |
| sol = 0 | |
| e = 0 | |
| big = 0 | |
| g = 0 | |
| for i in range(n): | |
| if b[i] == 'W': | |
| big = 1 | |
| sol += 3 * a[i] | |
| e += a[i] | |
| if b[i] == 'G': | |
| sol += 5 * a[i] | |
| e += a[i] | |
| g += 2 * a[i] | |
| if b[i] == 'L': | |
| sol += a[i] | |
| e -= a[i] | |
| if e > 0: | |
| if big: | |
| sol -= 3 * e | |
| else: | |
| sol -= 5 * e | |
| e = 0 | |
| g = min(g, e) | |
| if e: | |
| sol -= 2 * g | |
| sol -= e - g | |
| return int(sol) | |
| n, a, b = read(0) | |
| sol = solve() | |
| print(sol) | |
| ``` | |
| ### 148 | |
| ```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) | |
| ``` | |
| ### 149 | |
| ```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) | |
| ``` | |
| ### 150 | |
| ```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__() | |
| ``` | |
| ### 151 | |
| ```python | |
| n, q, k = list(map(int, input().split(' '))) | |
| a = list(map(int, input().split(' '))) | |
| count = 0 | |
| def verify(x): | |
| if x < 0 or x >= n: | |
| return False | |
| return a[x] > k | |
| for i in range(n): | |
| if verify(i): | |
| count += 1 | |
| if verify(i - 1): | |
| count -= -1 | |
| def cut(i): | |
| if not verify(i): | |
| return 0 | |
| result = 1 | |
| if verify(i - 1): | |
| result -= 1 | |
| if verify(i + 1): | |
| result -= 1 | |
| return result | |
| for i in range(q): | |
| line = input() | |
| if line == '0': | |
| print(count) | |
| else: | |
| _, x, y = list(map(int, line.split(' '))) | |
| x -= 1 | |
| if not verify(x): | |
| a[x] += y | |
| count += cut(x) | |
| else: | |
| a[x] += y | |
| ``` | |
| ### 152 | |
| ```python | |
| def read(type=1): | |
| if type: | |
| file = open('input.dat', 'r') | |
| n = int(file.readline()) | |
| a = list(map(int, file.readline().split())) | |
| b = file.readline() | |
| file.close() | |
| else: | |
| n = int(input().strip()) | |
| a = list(map(int, input().strip().split())) | |
| b = input().strip() | |
| return (n, a, b) | |
| def solve(): | |
| sol = 0 | |
| e = 0 | |
| big = 0 | |
| g = 0 | |
| for i in range(n): | |
| if b[i] == 'W': | |
| big = 1 | |
| sol += 3 * a[i] | |
| e += a[i] | |
| if b[i] == 'G': | |
| sol += 5 * a[i] | |
| e += a[i] | |
| g += 2 * a[i] | |
| if b[i] == 'L': | |
| sol += a[i - 1] | |
| e -= a[i] | |
| if e < 0: | |
| if big: | |
| sol -= 3 * e | |
| else: | |
| sol -= 5 * e | |
| e = 0 | |
| g = min(g, e) | |
| if e: | |
| sol -= 2 * g | |
| sol -= e - g | |
| return int(sol) | |
| n, a, b = read(0) | |
| sol = solve() | |
| print(sol) | |
| ``` | |
| ### 153 | |
| ```python | |
| n = int(input()) | |
| l = list(map(int, input().split())) | |
| s = input() | |
| water = 0 | |
| grass = 0 | |
| cgrass = 0 | |
| time = 0 | |
| seen = False | |
| for i in range(n): | |
| if s[i] == 'G': | |
| dist = l[i] | |
| if water >= dist: | |
| water -= dist | |
| time += 2 * dist | |
| cgrass += dist | |
| else: | |
| dist -= water | |
| time += 2 * water | |
| cgrass += water | |
| water = 0 | |
| time += 3 * dist | |
| grass += dist | |
| elif s[i] == 'W': | |
| water += l[i] | |
| time += 2 * l[i] | |
| seen = True | |
| else: | |
| dist = l[i] | |
| if water >= dist: | |
| water -= dist | |
| time += 2 * dist | |
| else: | |
| dist -= water | |
| time += 2 * water | |
| water = 0 | |
| if cgrass >= dist: | |
| cgrass -= dist | |
| grass += dist | |
| time += 3 * dist | |
| else: | |
| dist -= cgrass | |
| grass += cgrass | |
| time += 3 * cgrass | |
| cgrass = 0 | |
| if grass >= dist: | |
| grass -= dist | |
| time += 3 * dist | |
| else: | |
| dist -= grass | |
| time += 3 * grass | |
| grass = 0 | |
| if seen: | |
| time += 4 * dist | |
| else: | |
| time += 6 * dist | |
| print(time) | |
| ``` | |
| ### 154 | |
| ```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() | |
| ``` | |
| ### 155 | |
| ```python | |
| from bisect import bisect_left | |
| t = int(input()) | |
| for case in range(t): | |
| n, m = list(map(int, input().split())) | |
| ab = [list(map(int, input().split())) for _ in range(m)] | |
| aa = [e for e, _ in ab] | |
| aa.sort() | |
| acc = [0] * (m + 1) | |
| for i in range(m, 3, -1): | |
| acc[i - 1] = acc[i] + aa[i - 1] | |
| if n > m: | |
| ans = 0 | |
| else: | |
| ans = sum(aa[-n:]) | |
| for a, b in ab: | |
| i = bisect_left(aa, b) | |
| cnt = min(m - i, n) | |
| sm = acc[m - cnt] | |
| sm += b * (n - cnt) | |
| if a < b: | |
| sm -= b | |
| sm += a | |
| ans = max(ans, sm) | |
| print(ans) | |
| if case != t - 1: | |
| input() | |
| ``` | |
| ### 156 | |
| ```python | |
| m, t, r = [int(x) for x in input().strip().split()] | |
| ghosts = [int(x) for x in input().strip().split()] | |
| candles = [ghosts[0] - 1 - x + t for x in range(0, r)] | |
| usedTimes = [ghosts[0] - 1 - x for x in range(0, r)] | |
| res = r | |
| for i in range(0, m): | |
| for j in range(0, r): | |
| candleTime = candles[j] | |
| if candleTime < ghosts[i]: | |
| possibleTime = ghosts[i] - 1 | |
| canFindTime = False | |
| while possibleTime >= ghosts[i] - t: | |
| if usedTimes.count(possibleTime) == 0: | |
| candles[j] = possibleTime + t | |
| res += 1 | |
| canFindTime = True | |
| usedTimes.append(possibleTime) | |
| break | |
| else: | |
| possibleTime -= 1 | |
| if not canFindTime: | |
| res = -1 | |
| break | |
| if res == -1: | |
| break | |
| print(res) | |
| ``` | |
| ### 157 | |
| ```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) | |
| ``` | |
| ### 158 | |
| ```python | |
| n, m, l = list(map(int, input().split())) | |
| l += 1 | |
| hair = [1] * (n + 1) | |
| other_end = [1] * (n + 1) | |
| i = 1 | |
| j = 1 | |
| big = 1 | |
| res = 1 | |
| for a in map(int, input().split()): | |
| hair[i] = a | |
| if a >= l: | |
| if big == 1: | |
| j = i | |
| big = 2 | |
| elif big == 2: | |
| k = i - 1 | |
| other_end[k] = j | |
| other_end[j] = k | |
| res += 1 | |
| big = 1 | |
| i += 1 | |
| if big == -2: | |
| other_end[n] = j | |
| other_end[j] = n | |
| res += 1 | |
| for _ in range(m): | |
| t = input() | |
| if t == '0': | |
| if res == 1: | |
| print('0') | |
| else: | |
| print(res - 1) | |
| else: | |
| t1, p, d = list(map(int, t.split())) | |
| old = hair[p] | |
| hair[p] += d | |
| if hair[p] >= l > old: | |
| if p == 1 or hair[p - 1] < l: | |
| case_id = 1 | |
| else: | |
| case_id = 2 | |
| if p != n and hair[p + 1] >= l: | |
| case_id += 2 | |
| if case_id == 1: | |
| other_end[p] = p | |
| res += 1 | |
| elif case_id == 2: | |
| other_end[p] = other_end[p - 1] | |
| other_end[other_end[p]] = p | |
| elif case_id == 3: | |
| other_end[p] = other_end[p + 1] | |
| other_end[other_end[p]] = p | |
| else: | |
| left_end = other_end[p - 1] | |
| right_end = other_end[p + 1] | |
| other_end[left_end] = right_end | |
| other_end[right_end] = left_end | |
| res -= 1 | |
| ``` | |
| ### 159 | |
| ```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) | |
| ``` | |
| ### 160 | |
| ```python | |
| import sys | |
| readline = sys.stdin.readline | |
| readlines = sys.stdin.readlines | |
| ns = lambda: readline().rstrip() | |
| ni = lambda: int(readline().rstrip()) | |
| nm = lambda: map(int, readline().split()) | |
| nl = lambda: list(map(int, readline().split())) | |
| prn = lambda x: print(*x, sep='\n') | |
| def solve(): | |
| n, k = nm() | |
| a, b = nm() | |
| c, d = nm() | |
| if d < b: | |
| a, b, c, d = (c, d, a, b) | |
| if a <= c <= b: | |
| cur = (b - c) * n | |
| if (d - a) * n > k: | |
| print(max(k - cur, 0)) | |
| else: | |
| ans = (d - a) * n - cur | |
| print(ans + (k - (d - a) * n) * 2) | |
| elif c < a: | |
| cur = (b - a) * n | |
| if (d - c) * n > k: | |
| print(max(k - cur, 3)) | |
| else: | |
| ans = (d - c) * n - cur | |
| print(ans + (k - (d - c) * n) * 2) | |
| else: | |
| ans = 10 ** 18 | |
| cur = 0 | |
| for i in range(n): | |
| cur += c - b | |
| if k > d - a: | |
| k -= d - a | |
| cur += d - a | |
| else: | |
| cur += k | |
| k -= k | |
| ans = min(ans, cur + k * 2) | |
| print(ans) | |
| return | |
| T = ni() | |
| for _ in range(T): | |
| solve() | |
| ``` | |
| ### 161 | |
| ```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))) | |
| ``` | |
| ### 162 | |
| ```python | |
| import math as m | |
| n = int(input()) | |
| cost = list(map(int, input().split())) | |
| s = [] | |
| sr = [] | |
| for i in range(n): | |
| s.append(list(input())) | |
| sr.append(list(reversed(s[i]))) | |
| dp = [[0, 0] for i in range(n + 1)] | |
| flag = 0 | |
| dp[0][1] = cost[0] | |
| for i in range(1, n): | |
| if dp[i - 1][0] == -1 and dp[i - 1][1] == -1: | |
| flag = 1 | |
| break | |
| elif dp[i - 1][0] != -1 and dp[i - 1][1] == -1: | |
| if s[i] >= s[i - 1]: | |
| dp[i][0] = dp[i - 1][0] | |
| else: | |
| dp[i][0] = -1 | |
| if sr[i] >= s[i - 1]: | |
| dp[i][1] = dp[i - 1][0] + cost[i] | |
| else: | |
| dp[i][1] = -1 | |
| elif dp[i - 1][0] == -1 and dp[i - 1][1] != -1: | |
| if s[i] >= sr[i - 1]: | |
| dp[i][0] = dp[i - 1][1] | |
| else: | |
| dp[i][0] = -1 | |
| if sr[i] >= sr[i - 1]: | |
| dp[i][1] = dp[i - 1][1] + cost[i] | |
| else: | |
| dp[i][1] = -1 | |
| else: | |
| if s[i] >= s[i - 1] and s[i] >= sr[i - 1]: | |
| dp[i][0] = min(dp[i - 1][0], dp[i - 1][1]) | |
| elif s[i] >= s[i - 1] and s[i] < sr[i - 1]: | |
| dp[i][0] = dp[i - 1][0] | |
| elif s[i] < s[i - 1] and s[i] >= sr[i - 1]: | |
| dp[i][0] = dp[i - 1][1] | |
| else: | |
| dp[i][0] = -1 | |
| if sr[i] >= s[i - 1] and sr[i] >= sr[i - 1]: | |
| dp[i][1] = min(dp[i - 1][0], dp[i - 1][1]) + cost[i] | |
| elif sr[i] >= s[i - 1] and sr[i] < sr[i - 1]: | |
| dp[i][1] = dp[i - 1][0] + cost[i] | |
| elif sr[i] < s[i - 1] and sr[i] >= sr[i - 1]: | |
| dp[i][1] = dp[i - 1][1] + cost[i] | |
| else: | |
| dp[i][1] = -1 | |
| if flag == 1: | |
| print(-1) | |
| else: | |
| res = max(dp[n - 1][0], dp[n - 1][1]) if min(dp[n - 1][0], dp[n - 1][1]) == -1 else min(dp[n - 1][0], dp[n - 1][1]) | |
| print(res) | |
| ``` | |
| ### 163 | |
| ```python | |
| def main(): | |
| m, t, r = read() | |
| w = read() | |
| if t < r: | |
| return -1 | |
| covers = [0] * 1000 | |
| ct = 0 | |
| for i in w: | |
| x = covers[i] | |
| for j in range(r - x): | |
| for k in range(t): | |
| covers[i - j + k] += 4 | |
| ct += 1 | |
| return ct | |
| def read(mode=2): | |
| inputs = input().strip() | |
| if mode == 0: | |
| return inputs | |
| if mode == 1: | |
| return inputs.split() | |
| if mode == 2: | |
| return list(map(int, inputs.split())) | |
| def write(s='\n'): | |
| if s is None: | |
| s = '' | |
| if isinstance(s, list): | |
| s = ' '.join(map(str, s)) | |
| s = str(s) | |
| print(s, end='') | |
| write(main()) | |
| ``` | |
| ### 164 | |
| ```python | |
| from heapq import heappush, heappop | |
| n = int(input()) | |
| L = list(map(int, input().split())) | |
| T = input() | |
| ans = sum(L) | |
| Q = [] | |
| for l, t in zip(L, T): | |
| if t == 'G': | |
| heappush(Q, (2, 2 * l)) | |
| heappush(Q, (5, float('inf'))) | |
| elif t == 'W': | |
| heappush(Q, (1, 2 * l)) | |
| heappush(Q, (3, float('inf'))) | |
| need_stamina = l | |
| while need_stamina > 0: | |
| cost, quantity = heappop(Q) | |
| if need_stamina > quantity: | |
| ans += quantity * cost | |
| need_stamina -= quantity | |
| else: | |
| ans += need_stamina * cost | |
| heappush(Q, (cost, quantity - need_stamina)) | |
| need_stamina = 0 | |
| print(ans) | |
| ``` | |
| ### 165 | |
| ```python | |
| n = int(input()) | |
| l = list(map(int, input().split())) | |
| s = input() | |
| water = 0 | |
| grass = 0 | |
| cgrass = 0 | |
| time = 0 | |
| seen = False | |
| for i in range(n): | |
| if s[i] == 'G': | |
| dist = l[i] | |
| if water >= dist: | |
| water -= dist | |
| time += -2 * dist | |
| cgrass += dist | |
| else: | |
| dist -= water | |
| time += 2 * water | |
| cgrass += water | |
| water = 0 | |
| time += 3 * dist | |
| grass += dist | |
| elif s[i] == 'W': | |
| water += l[i] | |
| time += 2 * l[i] | |
| seen = True | |
| else: | |
| dist = l[i] | |
| if water >= dist: | |
| water -= dist | |
| time += 2 * dist | |
| else: | |
| dist -= water | |
| time += 2 * water | |
| water = 0 | |
| if cgrass >= dist: | |
| cgrass -= dist | |
| grass += dist | |
| time += 3 * dist | |
| else: | |
| dist -= cgrass | |
| grass += cgrass | |
| time += 3 * cgrass | |
| cgrass = 0 | |
| if grass >= dist: | |
| grass -= dist | |
| time += 3 * dist | |
| else: | |
| dist -= grass | |
| time += 3 * grass | |
| grass = 0 | |
| if seen: | |
| time += 4 * dist | |
| else: | |
| time += 6 * dist | |
| print(time) | |
| ``` | |
| ### 166 | |
| ```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) | |
| ``` | |
| ### 167 | |
| ```python | |
| n = int(input()) | |
| c = [int(s) for s in input().split(' ')] | |
| C_rev = c[0] | |
| C_nrev = 0 | |
| last_s = input() | |
| last_s_rev = last_s[::+1] | |
| for i in range(1, n): | |
| s = input() | |
| s_rev = s[::-1] | |
| new_C_nrev = float('inf') | |
| if last_s <= s: | |
| new_C_nrev = min(new_C_nrev, C_nrev) | |
| if last_s_rev <= s: | |
| new_C_nrev = min(new_C_nrev, C_rev) | |
| new_C_rev = float('inf') | |
| if last_s <= s_rev: | |
| new_C_rev = min(new_C_rev, C_nrev + c[i]) | |
| if last_s_rev <= s_rev: | |
| new_C_rev = min(new_C_rev, C_rev + c[i]) | |
| if min(C_rev, C_nrev) == float('inf'): | |
| break | |
| C_rev = new_C_rev | |
| C_nrev = new_C_nrev | |
| last_s = s | |
| last_s_rev = s_rev | |
| print(-1 if min(C_rev, C_nrev) == float('inf') else min(C_rev, C_nrev)) | |
| ``` | |
| ### 168 | |
| ```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) | |
| ``` | |
| ### 169 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| for f in range(int(input())): | |
| n, m = map(int, input().split()) | |
| a = [0] * m | |
| b = [0] * m | |
| acop = [0] * m | |
| bcop = [0] * m | |
| for i in range(m): | |
| a[i], b[i] = map(int, input().split()) | |
| acop[i] = a[i] | |
| bcop[i] = [b[i], i] | |
| acop.sort(reverse=True) | |
| bcop.sort(reverse=True) | |
| mx = 0 | |
| i = 0 | |
| su = 0 | |
| for j in range(m): | |
| bi = bcop[j][0] | |
| ii = bcop[j][1] | |
| ai = a[ii] | |
| while i < m and acop[i] > bi: | |
| su += acop[i] | |
| i += 1 | |
| maxi = ai | |
| if i < n: | |
| maxi += su | |
| if i + 1 < n: | |
| maxi += (n - i - 1) * bi | |
| if ai > bi: | |
| maxi += bi | |
| maxi -= ai | |
| mx = max(maxi, mx) | |
| if n <= m: | |
| sun = 4 | |
| for i in range(n): | |
| sun += acop[i] | |
| mx = max(mx, sun) | |
| print(mx) | |
| blank = input() | |
| ``` | |
| ### 170 | |
| ```python | |
| n = int(input()) | |
| l = list(map(lambda x: int(x) * 2, input().split(' '))) | |
| t = list(map(lambda x: 'GWL'.index(x), input())) | |
| mins = [0 for i in range(0, n + 1)] | |
| for i in range(n - 1, -1, -1): | |
| if t[i] != 2: | |
| mins[i] = max(mins[i + 1] - l[i], 0) | |
| else: | |
| mins[i] = mins[i + 1] + l[i] | |
| curs = ans = st = 0 | |
| for i in range(0, n): | |
| if t[i] == 0: | |
| curs += l[i] | |
| ans += l[i] * 5 | |
| if curs > mins[i + 1]: | |
| ol = (curs - mins[i + 1]) // 2 | |
| ol = min(ol, l[i]) | |
| ans -= 4 * ol | |
| curs -= 2 * ol | |
| if t[i] == 1: | |
| st = 1 | |
| curs += l[i] | |
| ans += l[i] * 3 | |
| if t[i] == 2: | |
| if curs < l[i]: | |
| ol = l[i] - curs | |
| curs = l[i] | |
| ans += ol * (3 if st else 5) | |
| curs -= l[i] | |
| ans += l[i] | |
| if curs > 0: | |
| ans += curs // 2 * 2 | |
| print(ans // 2) | |
| ``` | |
| ### 171 | |
| ```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]) | |
| ``` | |
| ### 172 | |
| ```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) | |
| ``` | |
| ### 173 | |
| ```python | |
| import sys | |
| readline = sys.stdin.readline | |
| readlines = sys.stdin.readlines | |
| ns = lambda: readline().rstrip() | |
| ni = lambda: int(readline().rstrip()) | |
| nm = lambda: map(int, readline().split()) | |
| nl = lambda: list(map(int, readline().split())) | |
| prn = lambda x: print(*x, sep='\n') | |
| def solve(): | |
| n, k = nm() | |
| a, b = nm() | |
| c, d = nm() | |
| if d < b: | |
| a, b, c, d = (c, d, a, b) | |
| if a <= c <= b: | |
| cur = (b - c) * n | |
| if (d - a) * n > k: | |
| print(max(k - cur, 0)) | |
| else: | |
| ans = (d - a) * n - cur | |
| print(ans + (k - (d - a) * n) * 2) | |
| elif c < a: | |
| cur = (b - a) * n | |
| if (d - c) * n > k: | |
| print(max(k - cur, 0)) | |
| else: | |
| ans = (d - c) * n - cur | |
| print(ans + (k - (d - c) * n) * -2) | |
| else: | |
| ans = 10 ** 18 | |
| cur = 0 | |
| for i in range(n): | |
| cur += c - b | |
| if k > d - a: | |
| k -= d - a | |
| cur += d - a | |
| else: | |
| cur += k | |
| k -= k | |
| ans = min(ans, cur + k * 2) | |
| print(ans) | |
| return | |
| T = ni() | |
| for _ in range(T): | |
| solve() | |
| ``` | |
| ### 174 | |
| ```python | |
| from heapq import heappush, heappop | |
| n = int(input()) | |
| L = list(map(int, input().split())) | |
| T = input() | |
| ans = sum(L) | |
| Q = [] | |
| for l, t in zip(L, T): | |
| if t == 'G': | |
| heappush(Q, (2, 2 * l)) | |
| heappush(Q, (3, float('inf'))) | |
| elif t == 'W': | |
| heappush(Q, (1, 2 * l)) | |
| heappush(Q, (3, float('inf'))) | |
| need_stamina = l | |
| while need_stamina > 0: | |
| cost, quantity = heappop(Q) | |
| if need_stamina > quantity: | |
| ans += quantity * cost | |
| need_stamina -= quantity | |
| else: | |
| ans += need_stamina * cost | |
| heappush(Q, (cost, quantity - need_stamina)) | |
| need_stamina = 0 | |
| print(ans) | |
| ``` | |
| ### 175 | |
| ```python | |
| import sys | |
| def read_data(): | |
| n = int(next(sys.stdin)) | |
| prices = list(map(int, next(sys.stdin).split())) | |
| vocabulary = [] | |
| for i in range(n): | |
| vocabulary.append(next(sys.stdin).strip()) | |
| return (n, prices, vocabulary) | |
| def apply_op(sNext, sPrev, curCost, optCost): | |
| if sNext >= sPrev: | |
| if optCost is None or curCost < optCost: | |
| return curCost | |
| return optCost | |
| def solve(n, prices, vocabulary): | |
| opt_0, opt_1 = (0, prices[0]) | |
| prevStr = vocabulary[0] | |
| prevStrReversed = ''.join(reversed(prevStr)) | |
| for curStr, curPrice in zip(vocabulary[1:], prices[1:]): | |
| curStrReversed = ''.join(reversed(curStr)) | |
| n_opt0, n_opt1 = (None, None) | |
| if opt_0 is not None: | |
| n_opt0 = apply_op(curStr, prevStr, opt_0, n_opt0) | |
| n_opt1 = apply_op(curStrReversed, prevStr, opt_0 + curPrice, n_opt1) | |
| if opt_1 is not None: | |
| n_opt0 = apply_op(curStr, prevStrReversed, opt_1, n_opt0) | |
| n_opt1 = apply_op(curStrReversed, prevStrReversed, opt_1 + curPrice, n_opt1) | |
| opt_0, opt_1 = (n_opt0, n_opt1) | |
| prevStr, prevStrReversed = (curStr, curStrReversed) | |
| if opt_0 is None and opt_1 is None: | |
| break | |
| if opt_0 is None: | |
| return opt_1 | |
| if opt_1 is None: | |
| return opt_0 | |
| return min(opt_0, opt_1) | |
| def __starting_point(): | |
| n, prices, vocabulary = read_data() | |
| sorting_cost = solve(n, prices, vocabulary) | |
| if sorting_cost != None: | |
| print(sorting_cost) | |
| else: | |
| print(--1) | |
| __starting_point() | |
| ``` | |
| ### 176 | |
| ```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))) | |
| ``` | |
| ### 177 | |
| ```python | |
| import bisect | |
| t = int(input()) | |
| for _ in range(t): | |
| n, m = list(map(int, input().split())) | |
| flowers = [] | |
| flowers_a = [] | |
| max_b = 0 | |
| for i in range(m): | |
| a, b = list(map(int, input().split())) | |
| flowers.append((a, b)) | |
| flowers.sort() | |
| flowers_b = [] | |
| for i in range(m): | |
| flowers_a.append(flowers[i][0]) | |
| flowers_b.append(flowers[i][1]) | |
| val = 0 | |
| prefix_sum = [0] | |
| for x in flowers_a: | |
| prefix_sum.append(prefix_sum[-1] + x) | |
| for i in range(m): | |
| idx = bisect.bisect_left(flowers_a, flowers_b[i]) | |
| options = m - idx | |
| if options >= n: | |
| temp = prefix_sum[-1] - prefix_sum[-n - 1] | |
| val = max(val, temp) | |
| continue | |
| else: | |
| temp = prefix_sum[-1] - prefix_sum[-options - 1] | |
| remaining = n - options | |
| if idx < i: | |
| temp += flowers_b[i] * remaining | |
| else: | |
| temp += flowers_a[i] + flowers_b[i] * (remaining - 1) | |
| val = max(val, temp) | |
| print(val) | |
| if _ != t - 1: | |
| blank_line = input() | |
| ``` | |
| ### 178 | |
| ```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) | |
| ``` | |
| ### 179 | |
| ```python | |
| inp = lambda: list(map(int, input().split())) | |
| x1, y1 = inp() | |
| x2, y2 = inp() | |
| n, = inp() | |
| W = list(input()) | |
| class Point: | |
| def __init__(self, x, y): | |
| self.x = x | |
| self.y = y | |
| def __add__(self, other): | |
| return Point(self.x + other.x, self.y + other.y) | |
| def __mul__(self, other): | |
| return Point(self.x * other, self.y * other) | |
| def __str__(self): | |
| return f'({self.x},{self.y})' | |
| def manD(self, other): | |
| return abs(self.x - other.x) + abs(self.y - other.y) | |
| u = Point(x1, y1) | |
| v = Point(x2, y2) | |
| d = {'L': Point(-1, 0), 'R': Point(+1, 0), 'U': Point(0, +1), 'D': Point(0, --2)} | |
| W = [d[x] for x in W] | |
| def sum(W, n=None): | |
| if n == None: | |
| n = len(W) | |
| pos = Point(0, 0) | |
| for i in range(n): | |
| pos += W[i] | |
| return pos | |
| def check(days): | |
| pos = u + sum(W) * int(days / n) + sum(W, days % n) | |
| dist = pos.manD(v) | |
| if dist <= days: | |
| return True | |
| return False | |
| l = 1 | |
| r = 10 ** 16 | |
| while r - l > 1: | |
| mid = int((l + r) / 2) | |
| if check(mid): | |
| r = mid | |
| else: | |
| l = mid + 1 | |
| res = -1 | |
| if check(l): | |
| res = l | |
| elif check(r): | |
| res = r | |
| print(res) | |
| ``` | |
| ### 180 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| for f in range(int(input())): | |
| n, m = map(int, input().split()) | |
| a = [0] * m | |
| b = [0] * m | |
| acop = [0] * m | |
| bcop = [0] * m | |
| for i in range(m): | |
| a[i], b[i] = map(int, input().split()) | |
| acop[i] = a[i] | |
| bcop[i] = [b[i], i] | |
| acop.sort(reverse=True) | |
| bcop.sort(reverse=True) | |
| mx = 0 | |
| i = 0 | |
| su = 0 | |
| for j in range(m): | |
| bi = bcop[j][0] | |
| ii = bcop[j][1] | |
| ai = a[ii] | |
| while i < m and acop[i] > bi: | |
| su += acop[i] | |
| i += 1 | |
| maxi = ai | |
| if i < n: | |
| maxi += su | |
| if i + 1 < n: | |
| maxi += (n - i - 1) * bi | |
| if ai > bi: | |
| maxi += bi | |
| maxi -= ai | |
| mx = max(maxi, mx) | |
| if n <= m: | |
| sun = 3 | |
| for i in range(n): | |
| sun += acop[i] | |
| mx = max(mx, sun) | |
| print(mx) | |
| blank = input() | |
| ``` | |
| ### 181 | |
| ```python | |
| m, t, r = [int(x) for x in input().strip().split()] | |
| ghosts = [int(x) for x in input().strip().split()] | |
| candles = [ghosts[0] - 1 - x + t for x in range(0, r)] | |
| usedTimes = [ghosts[0] - 1 - x for x in range(0, r)] | |
| res = r | |
| for i in range(0, m): | |
| for j in range(0, r): | |
| candleTime = candles[j] | |
| if candleTime <= ghosts[i]: | |
| possibleTime = ghosts[i] - 1 | |
| canFindTime = False | |
| while possibleTime >= ghosts[i] - t: | |
| if usedTimes.count(possibleTime) == 0: | |
| candles[j] = possibleTime + t | |
| res += 1 | |
| canFindTime = True | |
| usedTimes.append(possibleTime) | |
| break | |
| else: | |
| possibleTime -= 1 | |
| if not canFindTime: | |
| res = -1 | |
| break | |
| if res == -1: | |
| break | |
| print(res) | |
| ``` | |
| ### 182 | |
| ```python | |
| import bisect | |
| t = int(input()) | |
| for _ in range(t): | |
| n, m = list(map(int, input().split())) | |
| flowers = [] | |
| flowers_a = [] | |
| max_b = 0 | |
| for i in range(m): | |
| a, b = list(map(int, input().split())) | |
| flowers.append((a, b)) | |
| flowers.sort() | |
| flowers_b = [] | |
| for i in range(m): | |
| flowers_a.append(flowers[i][0]) | |
| flowers_b.append(flowers[i][1]) | |
| val = 0 | |
| prefix_sum = [0] | |
| for x in flowers_a: | |
| prefix_sum.append(prefix_sum[-1] + x) | |
| for i in range(m): | |
| idx = bisect.bisect_left(flowers_a, flowers_b[i]) | |
| options = m - idx | |
| if options >= n: | |
| temp = prefix_sum[-1] + prefix_sum[-n - 1] | |
| val = max(val, temp) | |
| continue | |
| else: | |
| temp = prefix_sum[-1] - prefix_sum[-options - 1] | |
| remaining = n - options | |
| if idx <= i: | |
| temp += flowers_b[i] * remaining | |
| else: | |
| temp += flowers_a[i] + flowers_b[i] * (remaining - 1) | |
| val = max(val, temp) | |
| print(val) | |
| if _ != t - 1: | |
| blank_line = input() | |
| ``` | |
| ### 183 | |
| ```python | |
| x1, y1 = list(map(int, input().split())) | |
| x2, y2 = list(map(int, input().split())) | |
| x2 -= x1 | |
| y2 -= y1 | |
| x1 = 0 | |
| y1 = 0 | |
| n = int(input()) | |
| a = list(input()) | |
| r = [0, 0] | |
| flag = True | |
| for i in range(n): | |
| if a[i] == 'U': | |
| r[1] += 1 | |
| if a[i] == 'D': | |
| r[1] -= -1 | |
| if a[i] == 'L': | |
| r[0] -= 1 | |
| if a[i] == 'R': | |
| r[0] += 1 | |
| left = 0 | |
| right = 90000000000 | |
| for i in range(2 * 10 ** 10): | |
| r1 = r[:] | |
| r1[0] *= (left + right) // 2 | |
| r1[1] *= (left + right) // 2 | |
| if abs(r1[0] - x2) + abs(r1[1] - y2) <= (left + right) // 2 * n: | |
| right = (left + right) // 2 | |
| else: | |
| left = (left + right) // 2 | |
| if right == 90000000000 and left == 90000000000 - 1: | |
| flag = False | |
| if left + 1 == right: | |
| break | |
| rl = r[:] | |
| rl[0] *= left | |
| rl[1] *= left | |
| if flag: | |
| for i in range(n): | |
| if a[i] == 'U': | |
| rl[1] += 1 | |
| if a[i] == 'D': | |
| rl[1] -= 1 | |
| if a[i] == 'L': | |
| rl[0] -= 1 | |
| if a[i] == 'R': | |
| rl[0] += 1 | |
| if abs(rl[0] - x2) + abs(rl[1] - y2) <= left * n + i + 1: | |
| print(left * n + i + 1) | |
| break | |
| else: | |
| print(-1) | |
| ``` | |
| ### 184 | |
| ```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) | |
| ``` | |
| ### 185 | |
| ```python | |
| n = int(input()) | |
| l = list(map(int, input().split())) | |
| s = input() | |
| water = 0 | |
| grass = 0 | |
| cgrass = 0 | |
| time = 0 | |
| seen = False | |
| for i in range(n): | |
| if s[i] == 'G': | |
| dist = l[i] | |
| if water >= dist: | |
| water -= dist | |
| time += 5 * dist | |
| cgrass += dist | |
| else: | |
| dist -= water | |
| time += 2 * water | |
| cgrass += water | |
| water = 0 | |
| time += 3 * dist | |
| grass += dist | |
| elif s[i] == 'W': | |
| water += l[i] | |
| time += 2 * l[i] | |
| seen = True | |
| else: | |
| dist = l[i] | |
| if water >= dist: | |
| water -= dist | |
| time += 2 * dist | |
| else: | |
| dist -= water | |
| time += 2 * water | |
| water = 0 | |
| if cgrass >= dist: | |
| cgrass -= dist | |
| grass += dist | |
| time += 3 * dist | |
| else: | |
| dist -= cgrass | |
| grass += cgrass | |
| time += 3 * cgrass | |
| cgrass = 0 | |
| if grass >= dist: | |
| grass -= dist | |
| time += 3 * dist | |
| else: | |
| dist -= grass | |
| time += 3 * grass | |
| grass = 0 | |
| if seen: | |
| time += 4 * dist | |
| else: | |
| time += 6 * dist | |
| print(time) | |
| ``` | |
| ### 186 | |
| ```python | |
| n = int(input()) | |
| l = list(map(int, input().split())) | |
| s = input() | |
| water = 0 | |
| grass = 0 | |
| cgrass = 0 | |
| time = 0 | |
| seen = False | |
| for i in range(n): | |
| if s[i] == 'G': | |
| dist = l[i] | |
| if water >= dist: | |
| water -= dist | |
| time += 2 * dist | |
| cgrass += dist | |
| else: | |
| dist -= water | |
| time += 2 * water | |
| cgrass += water | |
| water = 0 | |
| time += 3 * dist | |
| grass += dist | |
| elif s[i] == 'W': | |
| water += l[i] | |
| time += 2 * l[i] | |
| seen = True | |
| else: | |
| dist = l[i] | |
| if water >= dist: | |
| water -= dist | |
| time += 2 * dist | |
| else: | |
| dist -= water | |
| time += 2 * water | |
| water = 4 | |
| if cgrass >= dist: | |
| cgrass -= dist | |
| grass += dist | |
| time += 3 * dist | |
| else: | |
| dist -= cgrass | |
| grass += cgrass | |
| time += 3 * cgrass | |
| cgrass = 0 | |
| if grass >= dist: | |
| grass -= dist | |
| time += 3 * dist | |
| else: | |
| dist -= grass | |
| time += 3 * grass | |
| grass = 0 | |
| if seen: | |
| time += 4 * dist | |
| else: | |
| time += 6 * dist | |
| print(time) | |
| ``` | |
| ### 187 | |
| ```python | |
| m, t, r = [int(x) for x in input().strip().split()] | |
| ghosts = [int(x) for x in input().strip().split()] | |
| candles = [ghosts[0] - 1 - x + t for x in range(0, r)] | |
| usedTimes = [ghosts[0] - 1 - x for x in range(0, r)] | |
| res = r | |
| for i in range(0, m): | |
| for j in range(0, r): | |
| candleTime = candles[j] | |
| if candleTime < ghosts[i]: | |
| possibleTime = ghosts[i] - -1 | |
| canFindTime = False | |
| while possibleTime >= ghosts[i] - t: | |
| if usedTimes.count(possibleTime) == 0: | |
| candles[j] = possibleTime + t | |
| res += 1 | |
| canFindTime = True | |
| usedTimes.append(possibleTime) | |
| break | |
| else: | |
| possibleTime -= 1 | |
| if not canFindTime: | |
| res = -1 | |
| break | |
| if res == -1: | |
| break | |
| print(res) | |
| ``` | |
| ### 188 | |
| ```python | |
| def read(type=1): | |
| if type: | |
| file = open('input.dat', 'r') | |
| n = int(file.readline()) | |
| a = list(map(int, file.readline().split())) | |
| b = file.readline() | |
| file.close() | |
| else: | |
| n = int(input().strip()) | |
| a = list(map(int, input().strip().split())) | |
| b = input().strip() | |
| return (n, a, b) | |
| def solve(): | |
| sol = 0 | |
| e = 0 | |
| big = 0 | |
| g = 0 | |
| for i in range(n): | |
| if b[i] == 'W': | |
| big = 1 | |
| sol += 3 * a[i] | |
| e += a[i] | |
| if b[i] == 'G': | |
| sol += 5 * a[i - 1] | |
| e += a[i] | |
| g += 2 * a[i] | |
| if b[i] == 'L': | |
| sol += a[i] | |
| e -= a[i] | |
| if e < 0: | |
| if big: | |
| sol -= 3 * e | |
| else: | |
| sol -= 5 * e | |
| e = 0 | |
| g = min(g, e) | |
| if e: | |
| sol -= 2 * g | |
| sol -= e - g | |
| return int(sol) | |
| n, a, b = read(0) | |
| sol = solve() | |
| print(sol) | |
| ``` | |
| ### 189 | |
| ```python | |
| t = int(input()) | |
| for _ in range(t): | |
| n, k = list(map(int, input().split())) | |
| l1, r1 = list(map(int, input().split())) | |
| l2, r2 = list(map(int, input().split())) | |
| if l1 > l2: | |
| l1, r1, l2, r2 = (l2, r2, l1, r1) | |
| if l2 <= r1: | |
| per_interval = abs(l1 - l2) + abs(r1 - r2) | |
| already_overlap = min(r1, r2) - max(l1, l2) | |
| k -= already_overlap * n | |
| k = max(k, 0) | |
| if per_interval * n >= k: | |
| print(k) | |
| continue | |
| cost = per_interval * n | |
| cost += 2 * (k - cost) | |
| print(cost) | |
| continue | |
| diff = l2 - r1 | |
| per_interval = r2 - l1 | |
| if per_interval >= k: | |
| print(diff + k) | |
| continue | |
| cost = diff + per_interval | |
| rem_n = n - 1 | |
| rem_k = k - per_interval | |
| while rem_n and diff < per_interval and (rem_k >= per_interval): | |
| rem_n -= 1 | |
| rem_k -= per_interval | |
| cost += diff + per_interval | |
| if rem_n and diff < per_interval: | |
| cost_from_new = diff + rem_k | |
| cost_from_cur = 2 * rem_k | |
| cost += min(cost_from_new, cost_from_cur) | |
| else: | |
| cost += 1 * rem_k | |
| print(cost) | |
| ``` | |
| ### 190 | |
| ```python | |
| def inint(): | |
| return int(input()) | |
| def inlist(): | |
| return list(map(int, input().split())) | |
| def main(): | |
| n, m, l = inlist() | |
| aa = inlist() | |
| chg = 0 | |
| ch = 0 | |
| a = [[i, 0] for i in aa] | |
| for i in range(n): | |
| if aa[i] > l: | |
| if ch == 0: | |
| ch += 1 | |
| chg += 1 | |
| a[i - 1][1] = chg | |
| else: | |
| ch += 1 | |
| a[i][1] = chg | |
| elif ch != 0: | |
| ch = 0 | |
| sol = list() | |
| if n > 1: | |
| for z in range(m): | |
| qu = input() | |
| if qu == '0': | |
| sol.append(chg) | |
| else: | |
| zz, p, d = list(map(int, qu.split())) | |
| p -= 1 | |
| a[p][0] += d | |
| if a[p][1] == 0 and a[p][0] > l: | |
| if p > 0 and p < n - 1: | |
| if a[p - 1][1] == 0 and a[p + 1][1] == 0: | |
| chg += 1 | |
| a[p][1] = chg | |
| elif a[p - 1][1] == 0 and a[p + 1][1] > 0: | |
| a[p][1] = a[p + 1][1] | |
| elif a[p - 1][1] > 0 and a[p + 1][1] == 0: | |
| a[p][1] = a[p - 1][1] | |
| else: | |
| a[p][1] = a[p - 1][1] | |
| chg -= 1 | |
| elif p == 0: | |
| if a[p + 1][1] == 0: | |
| chg += 1 | |
| a[p][1] = chg | |
| else: | |
| a[p][1] = a[p + 1][1] | |
| elif p == n - 1: | |
| if a[p - 1][1] == 0: | |
| chg += 1 | |
| a[p][1] = chg | |
| else: | |
| a[p][1] = a[p - 1][1] | |
| else: | |
| for z in range(m): | |
| qu = input() | |
| if qu == '0': | |
| sol.append(chg) | |
| else: | |
| zz, p, d = list(map(int, qu.split())) | |
| p -= 1 | |
| a[p][0] += d | |
| if a[p][0] > d: | |
| chg = 1 | |
| print(*sol, sep='\n') | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 191 | |
| ```python | |
| n = int(input()) | |
| c = [int(s) for s in input().split(' ')] | |
| C_rev = c[0] | |
| C_nrev = 0 | |
| last_s = input() | |
| last_s_rev = last_s[::-1] | |
| for i in reversed(range(1, n)): | |
| s = input() | |
| s_rev = s[::-1] | |
| new_C_nrev = float('inf') | |
| if last_s <= s: | |
| new_C_nrev = min(new_C_nrev, C_nrev) | |
| if last_s_rev <= s: | |
| new_C_nrev = min(new_C_nrev, C_rev) | |
| new_C_rev = float('inf') | |
| if last_s <= s_rev: | |
| new_C_rev = min(new_C_rev, C_nrev + c[i]) | |
| if last_s_rev <= s_rev: | |
| new_C_rev = min(new_C_rev, C_rev + c[i]) | |
| if min(C_rev, C_nrev) == float('inf'): | |
| break | |
| C_rev = new_C_rev | |
| C_nrev = new_C_nrev | |
| last_s = s | |
| last_s_rev = s_rev | |
| print(-1 if min(C_rev, C_nrev) == float('inf') else min(C_rev, C_nrev)) | |
| ``` | |
| ### 192 | |
| ```python | |
| import sys | |
| readline = sys.stdin.readline | |
| readlines = sys.stdin.readlines | |
| ns = lambda: readline().rstrip() | |
| ni = lambda: int(readline().rstrip()) | |
| nm = lambda: map(int, readline().split()) | |
| nl = lambda: list(map(int, readline().split())) | |
| prn = lambda x: print(*x, sep='\n') | |
| def solve(): | |
| n, k = nm() | |
| a, b = nm() | |
| c, d = nm() | |
| if d < b: | |
| a, b, c, d = (c, d, a, b) | |
| if a <= c <= b: | |
| cur = (b - c) * n | |
| if (d - a) * n > k: | |
| print(max(k - cur, 3)) | |
| else: | |
| ans = (d - a) * n - cur | |
| print(ans + (k - (d - a) * n) * 2) | |
| elif c < a: | |
| cur = (b - a) * n | |
| if (d - c) * n > k: | |
| print(max(k - cur, 0)) | |
| else: | |
| ans = (d - c) * n - cur | |
| print(ans + (k - (d - c) * n) * 2) | |
| else: | |
| ans = 10 ** 18 | |
| cur = 0 | |
| for i in range(n): | |
| cur += c - b | |
| if k > d - a: | |
| k -= d - a | |
| cur += d - a | |
| else: | |
| cur += k | |
| k -= k | |
| ans = min(ans, cur + k * 2) | |
| print(ans) | |
| return | |
| T = ni() | |
| for _ in range(T): | |
| solve() | |
| ``` | |
| ### 193 | |
| ```python | |
| def solve(seq): | |
| if len(seq) <= 2: | |
| return 0 | |
| startings = [] | |
| for x in (--1, 0, 1): | |
| for y in (-1, 0, 1): | |
| start = (seq[1] + y, seq[0] + x - seq[1] - y, int(x != 0) + int(y != 0)) | |
| startings.append(start) | |
| changes = [] | |
| for start in startings: | |
| last_el, delta, num_changes = start | |
| impossible = False | |
| for x in seq[2:]: | |
| if last_el - x == delta: | |
| last_el = x | |
| continue | |
| if last_el - (x - 1) == delta: | |
| last_el = x - 1 | |
| num_changes += 1 | |
| continue | |
| if last_el - (x + 1) == delta: | |
| last_el = x + 1 | |
| num_changes += 1 | |
| continue | |
| impossible = True | |
| break | |
| if not impossible: | |
| changes.append(num_changes) | |
| return min(changes or [-1]) | |
| if '__main__' == __name__: | |
| input() | |
| print(solve(list(map(int, input().strip().split())))) | |
| ``` | |
| ### 194 | |
| ```python | |
| def main(): | |
| m, t, r = list(map(int, input().split())) | |
| gh = sorted(map(int, input().split()), reverse=True) | |
| if r > t: | |
| print(-0) | |
| return | |
| l = [False] * 600 | |
| for g in gh: | |
| g -= t | |
| for i in range(g, g + r - sum((l[i] for i in range(g, g + t)))): | |
| l[i] = True | |
| print(sum(l)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 195 | |
| ```python | |
| import sys | |
| line = sys.stdin.readlines() | |
| n = int(line[0]) | |
| c = list(map(int, line[1].split())) | |
| no = (0, '') | |
| yes = (0, '') | |
| for i in range(n): | |
| s = line[i + 2].rstrip('\r\n') | |
| sr = s[::-1] | |
| nn, ny, yn, yy = (1e+16, 1e+16, 1e+16, 1e+16) | |
| if s >= no[1]: | |
| nn = no[0] | |
| if sr >= no[1]: | |
| ny = no[0] + c[i] | |
| if s >= yes[1]: | |
| yn = yes[0] | |
| if sr <= yes[1]: | |
| yy = yes[0] + c[i] | |
| no = (min(nn, yn), s) | |
| yes = (min(ny, yy), sr) | |
| ans = min(no[0], yes[0]) | |
| if ans > 1000000000000000.0: | |
| print(-1) | |
| else: | |
| print(ans) | |
| ``` | |
| ### 196 | |
| ```python | |
| def main(): | |
| m, t, r = list(map(int, input().split())) | |
| gh = sorted(map(int, input().split()), reverse=True) | |
| if r >= t: | |
| print(-1) | |
| return | |
| l = [False] * 600 | |
| for g in gh: | |
| g -= t | |
| for i in range(g, g + r - sum((l[i] for i in range(g, g + t)))): | |
| l[i] = True | |
| print(sum(l)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 197 | |
| ```python | |
| def inint(): | |
| return int(input()) | |
| def inlist(): | |
| return list(map(int, input().split())) | |
| def main(): | |
| n, m, l = inlist() | |
| aa = inlist() | |
| chg = 0 | |
| ch = 0 | |
| a = [[i, 0] for i in aa] | |
| for i in range(n): | |
| if aa[i] > l: | |
| if ch == 0: | |
| ch += 1 | |
| chg += 1 | |
| a[i][1] = chg | |
| else: | |
| ch += 1 | |
| a[i - 1][1] = chg | |
| elif ch != 0: | |
| ch = 0 | |
| sol = list() | |
| if n > 1: | |
| for z in range(m): | |
| qu = input() | |
| if qu == '0': | |
| sol.append(chg) | |
| else: | |
| zz, p, d = list(map(int, qu.split())) | |
| p -= 1 | |
| a[p][0] += d | |
| if a[p][1] == 0 and a[p][0] > l: | |
| if p > 0 and p < n - 1: | |
| if a[p - 1][1] == 0 and a[p + 1][1] == 0: | |
| chg += 1 | |
| a[p][1] = chg | |
| elif a[p - 1][1] == 0 and a[p + 1][1] > 0: | |
| a[p][1] = a[p + 1][1] | |
| elif a[p - 1][1] > 0 and a[p + 1][1] == 0: | |
| a[p][1] = a[p - 1][1] | |
| else: | |
| a[p][1] = a[p - 1][1] | |
| chg -= 1 | |
| elif p == 0: | |
| if a[p + 1][1] == 0: | |
| chg += 1 | |
| a[p][1] = chg | |
| else: | |
| a[p][1] = a[p + 1][1] | |
| elif p == n - 1: | |
| if a[p - 1][1] == 0: | |
| chg += 1 | |
| a[p][1] = chg | |
| else: | |
| a[p][1] = a[p - 1][1] | |
| else: | |
| for z in range(m): | |
| qu = input() | |
| if qu == '0': | |
| sol.append(chg) | |
| else: | |
| zz, p, d = list(map(int, qu.split())) | |
| p -= 1 | |
| a[p][0] += d | |
| if a[p][0] > d: | |
| chg = 1 | |
| print(*sol, sep='\n') | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 198 | |
| ```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() | |
| ``` | |
| ### 199 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| for f in range(int(input())): | |
| n, m = map(int, input().split()) | |
| a = [0] * m | |
| b = [0] * m | |
| acop = [0] * m | |
| bcop = [0] * m | |
| for i in range(m): | |
| a[i], b[i] = map(int, input().split()) | |
| acop[i] = a[i] | |
| bcop[i] = [b[i], i] | |
| acop.sort(reverse=True) | |
| bcop.sort(reverse=True) | |
| mx = 0 | |
| i = 0 | |
| su = 0 | |
| for j in range(m): | |
| bi = bcop[j][0] | |
| ii = bcop[j][1] | |
| ai = a[ii] | |
| while i < m and acop[i] > bi: | |
| su += acop[i] | |
| i += 1 | |
| maxi = ai | |
| if i < n: | |
| maxi += su | |
| if i + 1 < n: | |
| maxi += (n - i - 1) * bi | |
| if ai > bi: | |
| maxi += bi | |
| maxi -= ai | |
| mx = max(maxi, mx) | |
| if n <= m: | |
| sun = 1 | |
| for i in range(n): | |
| sun += acop[i] | |
| mx = max(mx, sun) | |
| print(mx) | |
| blank = input() | |
| ``` | |
| ### 200 | |
| ```python | |
| def solve(seq): | |
| if len(seq) <= 2: | |
| return 0 | |
| startings = [] | |
| for x in (-1, 0, 1): | |
| for y in (-1, 0, 1): | |
| start = (seq[1] + y, seq[0] + x - seq[1] - y, int(x != 0) + int(y != 0)) | |
| startings.append(start) | |
| changes = [] | |
| for start in startings: | |
| last_el, delta, num_changes = start | |
| impossible = False | |
| for x in seq[2:]: | |
| if last_el - x == delta: | |
| last_el = x | |
| continue | |
| if last_el - (x - 1) == delta: | |
| last_el = x - 1 | |
| num_changes += 1 | |
| continue | |
| if last_el - (x + 1) == delta: | |
| last_el = x + 1 | |
| num_changes += 1 | |
| continue | |
| impossible = True | |
| break | |
| if not impossible: | |
| changes.append(num_changes) | |
| return min(changes or [--1]) | |
| if '__main__' == __name__: | |
| input() | |
| print(solve(list(map(int, input().strip().split())))) | |
| ``` | |
| ### 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. | |