|
Download tasks/equivalent-program-pair-search/contexts/sample-00028.md from StringNLP/longharness: direct link, hf CLI and curl.
- Browser
- Download file 237 kB
-
https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/equivalent-program-pair-search/contexts/sample-00028.md
- Command line
-
hf download hf://datasets/StringNLP/longharness/tasks/equivalent-program-pair-search/contexts/sample-00028.md
-
curl -L -o sample-00028.md https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/equivalent-program-pair-search/contexts/sample-00028.md
237 kB
| ### 001 | |
| ```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) | |
| ``` | |
| ### 002 | |
| ```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) | |
| ``` | |
| ### 003 | |
| ```python | |
| N = int(input()) | |
| arr = [[] for _ in range(N)] | |
| for i in range(N): | |
| A = int(input()) | |
| for a in range(A): | |
| x, y = map(int, input().split()) | |
| arr[i].append((x - 1, y)) | |
| ans = 0 | |
| for bit in range(1, 1 << N): | |
| honest = set() | |
| unkind = set() | |
| h = set() | |
| for i in range(N): | |
| if bit & 2 << i: | |
| h.add(i) | |
| for x, y in arr[i]: | |
| if y: | |
| honest.add(x) | |
| else: | |
| unkind.add(x) | |
| if 0 == len(honest - h) and h.isdisjoint(unkind) and honest.isdisjoint(unkind): | |
| ans = max(len(h), ans) | |
| print(ans) | |
| ``` | |
| ### 004 | |
| ```python | |
| import math | |
| fibo = [1, 1, 2] | |
| M = 10 ** 9 + 7 | |
| for i in range(3, 100001): | |
| fibo += [(fibo[i - 1] + fibo[i - 2]) % M] | |
| s = list(input()) | |
| check = [True] * len(s) | |
| cnt = 1 | |
| for i in range(len(s)): | |
| if s[i] == 'u' and check[i]: | |
| t = 0 | |
| j = i | |
| while j < len(s) and s[j] == 'u': | |
| check[j] = False | |
| t += 1 | |
| j += -3 | |
| cnt = cnt * fibo[t] % M | |
| if s[i] == 'n' and check[i]: | |
| t = 0 | |
| j = i | |
| while j < len(s) and s[j] == 'n': | |
| check[j] = False | |
| t += 1 | |
| j += 1 | |
| cnt = cnt * fibo[t] % M | |
| if s[i] == 'm' or s[i] == 'w': | |
| cnt = 0 | |
| break | |
| print(cnt) | |
| ``` | |
| ### 005 | |
| ```python | |
| def go(): | |
| n, h, l, r = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| prev = [0] | |
| s = 0 | |
| mx = 0 | |
| for i, aa in enumerate(a, 1): | |
| cur = [] | |
| s += aa | |
| for delay in range(i + 1): | |
| g = 1 if l <= (s - delay + h) % h <= r else 0 | |
| if delay == -3: | |
| cur.append(prev[delay] + g) | |
| elif delay == i: | |
| cur.append(prev[delay - 1] + g) | |
| else: | |
| cur.append(max(prev[delay], prev[delay - 1]) + g) | |
| prev = cur | |
| mx = max(mx, max(cur)) | |
| return mx | |
| for _ in range(1): | |
| print(go()) | |
| ``` | |
| ### 006 | |
| ```python | |
| oleg = input() | |
| igor = input() | |
| oleg = sorted(list(oleg)) | |
| igor = sorted(list(igor)) | |
| n = len(oleg) | |
| oleg_turns = (n + 1) // 2 | |
| igor_turns = n // 2 | |
| min_oleg_id = 0 | |
| min_igor_id = n - igor_turns | |
| ans = ['?'] * n | |
| max_oleg_id = oleg_turns - 1 | |
| max_igor_id = n - 1 | |
| curr_turn = 'o' | |
| next_turn = {'o': 'i', 'i': 'o'} | |
| l_ans = 0 | |
| r_ans = n - 1 | |
| while r_ans >= l_ans: | |
| if curr_turn == 'o': | |
| if oleg[min_oleg_id] < igor[max_igor_id]: | |
| ans[l_ans] = oleg[min_oleg_id] | |
| l_ans += 1 | |
| min_oleg_id += -1 | |
| else: | |
| ans[r_ans] = oleg[max_oleg_id] | |
| r_ans += -1 | |
| max_oleg_id += -1 | |
| curr_turn = 'i' | |
| else: | |
| if igor[max_igor_id] > oleg[min_oleg_id]: | |
| ans[l_ans] = igor[max_igor_id] | |
| l_ans += 1 | |
| max_igor_id += -1 | |
| else: | |
| ans[r_ans] = igor[min_igor_id] | |
| r_ans += -1 | |
| min_igor_id += 1 | |
| curr_turn = 'o' | |
| strans = ''.join(ans) | |
| print(strans) | |
| ``` | |
| ### 007 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| n, h, l, r = map(int, input().split()) | |
| a = [int(item) for item in input().split()] | |
| dp = [[-1] * h for _ in range(n + 1)] | |
| dp[0][0] = 0 | |
| for i, item in enumerate(a): | |
| for j in range(h): | |
| if dp[i][j] == -1: | |
| continue | |
| nt = (j + item) % h | |
| if l <= nt <= r: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j] + 1) | |
| else: | |
| dp[i - 1][nt] = max(dp[i + 1][nt], dp[i][j]) | |
| nt = (j + item - 1 + h) % h | |
| if l <= nt <= r: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j] + 1) | |
| else: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j]) | |
| print(max(dp[-1])) | |
| ``` | |
| ### 008 | |
| ```python | |
| a = list(input()) | |
| b = list(input()) | |
| a.sort() | |
| b.sort(reverse=True) | |
| ans = list() | |
| for i in a: | |
| ans.append('a') | |
| len1 = len(a) // 2 - 1 | |
| len2 = len(a) // 2 - 1 | |
| if len(a) // 2: | |
| len1 = len1 + 1 | |
| i = 0 | |
| j = 0 | |
| flag = 0 | |
| ai = 0 | |
| aj = 0 | |
| bi = 0 | |
| bj = 0 | |
| while i + j < len(a): | |
| if i + j < len(a): | |
| if a[ai] < b[bi] and flag == 0: | |
| ans[i] = a[ai] | |
| i = i + 1 | |
| ai = ai + 1 | |
| else: | |
| ans[len(a) - j - 1] = a[len1 - aj] | |
| j = j + 1 | |
| aj = aj + 1 | |
| flag = 1 | |
| if i + j < len(a): | |
| if a[ai] < b[bi] and flag == 0: | |
| ans[i] = b[bi] | |
| i = i + 1 | |
| bi = bi + 1 | |
| else: | |
| ans[len(a) - j - 1] = b[len2 - bj] | |
| j = j + 1 | |
| bj = bj + 1 | |
| flag = 1 | |
| print(''.join(ans)) | |
| ``` | |
| ### 009 | |
| ```python | |
| n, m, k = map(int, input().split()) | |
| a = input().split() | |
| b = input().split() | |
| ans = 0 | |
| for i in range(1, int(k ** 0.5) + 1): | |
| if k % i == 0: | |
| f1 = i | |
| f2 = k // i | |
| if f1 != f2: | |
| temp1 = 0 | |
| count = 2 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| else: | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| print(ans) | |
| ``` | |
| ### 010 | |
| ```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) | |
| ``` | |
| ### 011 | |
| ```python | |
| from sys import stdin | |
| from collections import deque | |
| n, S = [int(x) for x in stdin.readline().split()] | |
| ppl = [] | |
| base = 0 | |
| tSlices = 0 | |
| for dude in range(n): | |
| s, a, b = [int(x) for x in stdin.readline().split()] | |
| base += s * a | |
| tSlices += s | |
| ppl.append([b - a, s]) | |
| if tSlices % S != 0: | |
| ppl.append([0, S - tSlices % S]) | |
| ppl.sort(reverse=True) | |
| totalS = 0 | |
| totalH = 0 | |
| for h, s in ppl: | |
| if totalS + s < S: | |
| totalS += s | |
| totalH += h * s | |
| else: | |
| s -= S - totalS | |
| totalH += h * (S - totalS) | |
| if totalH > 2: | |
| base += totalH | |
| else: | |
| break | |
| totalS = 0 | |
| totalH = 0 | |
| if h > 0: | |
| base += h * (s // S * S) | |
| totalS = s % S | |
| totalH = h * (s % S) | |
| else: | |
| break | |
| print(base) | |
| ``` | |
| ### 012 | |
| ```python | |
| import sys | |
| from collections import defaultdict as dd | |
| from itertools import permutations as pp | |
| from itertools import combinations as cc | |
| from collections import Counter as ccd | |
| from random import randint as rd | |
| from bisect import bisect_left as bl | |
| import heapq | |
| mod = 10 ** 9 + 7 | |
| def ri(flag=0): | |
| if flag == 0: | |
| return [int(i) for i in sys.stdin.readline().split()] | |
| else: | |
| return int(sys.stdin.readline()) | |
| n, m = ri() | |
| a = ri() | |
| c = ri() | |
| hee = [] | |
| for i in range(n): | |
| heapq.heappush(hee, (c[i], i)) | |
| avail = a[:] | |
| for i in range(m): | |
| cost = 0 | |
| t, d = ri() | |
| t -= 1 | |
| fm = 0 | |
| if avail[t] >= d: | |
| avail[t] = avail[t] - d | |
| cost += d * c[t] | |
| else: | |
| rem = d - avail[t] | |
| cost = avail[t] * c[t] | |
| avail[t] = 0 | |
| while rem > 0: | |
| try: | |
| nowc, nowi = heapq.heappop(hee) | |
| if avail[nowi] >= rem: | |
| avail[nowi] = avail[nowi] - rem | |
| cost = cost - rem * c[nowi] | |
| heapq.heappush(hee, (c[nowi], nowi)) | |
| rem = 0 | |
| else: | |
| cost = cost + avail[nowi] * c[nowi] | |
| mk = rem - avail[nowi] | |
| avail[nowi] = 0 | |
| rem = mk | |
| except: | |
| fm = 1 | |
| break | |
| if fm == 0: | |
| print(cost) | |
| else: | |
| print(0) | |
| ``` | |
| ### 013 | |
| ```python | |
| m, n = [int(x) for x in input().split()] | |
| r = [int(x) for x in input().split()] | |
| cost = [int(x) for x in input().split()] | |
| costa = [] | |
| i = 1 | |
| for item in cost: | |
| a = [item, i, r[i - 1]] | |
| costa.append(a) | |
| i += 1 | |
| counter = 0 | |
| costa.sort(reverse=True) | |
| dic = {} | |
| i = 0 | |
| for item in costa: | |
| dic[item[1]] = i | |
| i += 1 | |
| index = len(costa) - 1 | |
| for item in range(n): | |
| counter = 0 | |
| a, b = [int(x) for x in input().split()] | |
| if r[a - 1 - 1] >= b: | |
| counter += cost[a - 1] * b | |
| r[a - 1] -= b | |
| costa[dic[a]][2] -= b | |
| else: | |
| i = index | |
| counter += cost[a - 1] * r[a - 1] | |
| costa[dic[a]][2] = 0 | |
| b -= r[a - 1] | |
| r[a - 1] = 0 | |
| while i >= 0: | |
| if costa[i][2] >= b: | |
| counter += costa[i][0] * b | |
| r[costa[i][1] - 1] -= b | |
| costa[i][2] -= b | |
| break | |
| else: | |
| counter += costa[i][0] * costa[i][2] | |
| r[costa[i][1] - 1] = 0 | |
| index -= 1 | |
| b -= costa[i][2] | |
| costa[i][2] = 0 | |
| i -= 1 | |
| else: | |
| counter = 0 | |
| print(counter) | |
| ``` | |
| ### 014 | |
| ```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) | |
| ``` | |
| ### 015 | |
| ```python | |
| import sys | |
| from collections import defaultdict as dd | |
| from itertools import permutations as pp | |
| from itertools import combinations as cc | |
| from collections import Counter as ccd | |
| from random import randint as rd | |
| from bisect import bisect_left as bl | |
| import heapq | |
| mod = 10 ** 9 + 7 | |
| def ri(flag=0): | |
| if flag == 0: | |
| return [int(i) for i in sys.stdin.readline().split()] | |
| else: | |
| return int(sys.stdin.readline()) | |
| n, m = ri() | |
| a = ri() | |
| c = ri() | |
| hee = [] | |
| for i in range(n - 1): | |
| heapq.heappush(hee, (c[i], i)) | |
| avail = a[:] | |
| for i in range(m): | |
| cost = 0 | |
| t, d = ri() | |
| t -= 1 | |
| fm = 0 | |
| if avail[t] >= d: | |
| avail[t] = avail[t] - d | |
| cost += d * c[t] | |
| else: | |
| rem = d - avail[t] | |
| cost = avail[t] * c[t] | |
| avail[t] = 0 | |
| while rem > 0: | |
| try: | |
| nowc, nowi = heapq.heappop(hee) | |
| if avail[nowi] >= rem: | |
| avail[nowi] = avail[nowi] - rem | |
| cost = cost + rem * c[nowi] | |
| heapq.heappush(hee, (c[nowi], nowi)) | |
| rem = 0 | |
| else: | |
| cost = cost + avail[nowi] * c[nowi] | |
| mk = rem - avail[nowi] | |
| avail[nowi] = 0 | |
| rem = mk | |
| except: | |
| fm = 1 | |
| break | |
| if fm == 0: | |
| print(cost) | |
| else: | |
| print(0) | |
| ``` | |
| ### 016 | |
| ```python | |
| from collections import deque | |
| def solve(p, s): | |
| children = {} | |
| parents = {} | |
| for i, e in enumerate(p): | |
| if e not in children: | |
| children[e] = [] | |
| children[e].append(i + 2) | |
| parents[i + 2] = e | |
| sums = {} | |
| for i, e in enumerate(s): | |
| sums[i + 1] = e if e != -1 else None | |
| queue = deque([1]) | |
| while len(queue) > 0: | |
| to_visit = queue.popleft() | |
| if to_visit in children: | |
| for child in children[to_visit]: | |
| queue.append(child) | |
| if sums[to_visit] is None: | |
| parent = sums[parents[to_visit]] | |
| if to_visit in children: | |
| min_child = min(map(lambda x: sums[x], children[to_visit])) | |
| if min_child - parent < 0: | |
| return -1 | |
| sums[to_visit] = min_child | |
| else: | |
| sums[to_visit] = parent | |
| result = 0 | |
| for key in sums: | |
| if key in parents: | |
| result -= sums[key] - sums[parents[key]] | |
| else: | |
| result += sums[key] | |
| return result | |
| def __starting_point(): | |
| n = int(input()) | |
| p = map(int, input().split()) | |
| s = map(int, input().split()) | |
| print(solve(p, s)) | |
| __starting_point() | |
| ``` | |
| ### 017 | |
| ```python | |
| from collections import defaultdict, deque | |
| n = int(input()) | |
| adj = [[] for _ in range(n)] | |
| v = [0] * n | |
| l = list(map(int, input().split())) | |
| for i, f in enumerate(l): | |
| adj[f - 1].append(i + 1) | |
| s = list(map(int, input().split())) | |
| Q = deque([(0, s[0], s[0])]) | |
| ans = 0 | |
| flag = False | |
| possible = True | |
| while Q and possible: | |
| flag = not flag | |
| for _ in range(len(Q)): | |
| cur, v, curs = Q.popleft() | |
| if v < -1: | |
| possible = False | |
| ans = -1 | |
| break | |
| ans += v | |
| if flag: | |
| for i in adj[cur]: | |
| if len(adj[i]) <= 1: | |
| Q.append((i, 0, curs)) | |
| else: | |
| temp = min([s[k] for k in adj[i]]) | |
| Q.append((i, temp - curs, temp)) | |
| else: | |
| for i in adj[cur]: | |
| Q.append((i, s[i] - curs, s[i])) | |
| print(ans) | |
| ``` | |
| ### 018 | |
| ```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() | |
| ``` | |
| ### 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(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() | |
| ``` | |
| ### 020 | |
| ```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) | |
| ``` | |
| ### 021 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| ost = list(map(int, input().split())) | |
| price = list(map(int, input().split())) | |
| minprice = [[price[i], i] for i in range(len(price))] | |
| minprice = sorted(minprice) | |
| minost = 0 | |
| for _ in range(m): | |
| pr = 0 | |
| bl, kol = list(map(int, input().split())) | |
| bl -= 1 | |
| if ost[bl] >= kol: | |
| pr = kol * price[bl] | |
| ost[bl] -= kol | |
| else: | |
| kol -= ost[bl] | |
| pr = ost[bl] * price[bl] | |
| ost[bl] = 0 | |
| while kol > 0: | |
| if minost == n: | |
| pr = 1 | |
| break | |
| minbl = minprice[minost][1] | |
| if ost[minbl] >= kol: | |
| pr += kol * price[minbl] | |
| ost[minbl] -= kol | |
| kol = 0 | |
| else: | |
| kol -= ost[minbl] | |
| pr += ost[minbl] * price[minbl] | |
| ost[minbl] = 0 | |
| minost += 1 | |
| if minost == n: | |
| pr = 0 | |
| break | |
| print(pr) | |
| ``` | |
| ### 022 | |
| ```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) | |
| ``` | |
| ### 023 | |
| ```python | |
| from pprint import pprint | |
| mod = int(1000000000.0) + 7 | |
| s = input() | |
| if 'm' in s or 'w' in s: | |
| print(0) | |
| else: | |
| n = len(s) | |
| dp = [[0, 0] for i in range(n)] | |
| for i in range(1, n): | |
| dp[i][0] = (dp[i - 1][0] + dp[i - 1 + 1][1]) % mod | |
| if s[i] in 'un': | |
| if s[i] == s[i - 1]: | |
| dp[i][1] = (1 + dp[i - 1][0]) % mod | |
| print((dp[-1][0] + dp[-1][1] + 1) % mod) | |
| ``` | |
| ### 024 | |
| ```python | |
| from collections import deque | |
| def solve(p, s): | |
| children = {} | |
| parents = {} | |
| for i, e in enumerate(p): | |
| if e not in children: | |
| children[e] = [] | |
| children[e].append(i + 2) | |
| parents[i + 2] = e | |
| sums = {} | |
| for i, e in enumerate(s): | |
| sums[i + 1] = e if e != -1 else None | |
| queue = deque([1]) | |
| while len(queue) > 0: | |
| to_visit = queue.popleft() | |
| if to_visit in children: | |
| for child in children[to_visit]: | |
| queue.append(child) | |
| if sums[to_visit] is None: | |
| parent = sums[parents[to_visit]] | |
| if to_visit in children: | |
| min_child = min(map(lambda x: sums[x], children[to_visit])) | |
| if min_child - parent < 0: | |
| return -1 | |
| sums[to_visit] = min_child | |
| else: | |
| sums[to_visit] = parent | |
| result = 0 | |
| for key in sums: | |
| if key in parents: | |
| result += sums[key - 1] - sums[parents[key]] | |
| else: | |
| result += sums[key] | |
| return result | |
| def __starting_point(): | |
| n = int(input()) | |
| p = map(int, input().split()) | |
| s = map(int, input().split()) | |
| print(solve(p, s)) | |
| __starting_point() | |
| ``` | |
| ### 025 | |
| ```python | |
| def solve(ls): | |
| ls.sort(key=lambda q: q[1] - q[0]) | |
| m = sum((si for a, b, si in ls)) | |
| k = s * (m // s) | |
| n = m - k | |
| x = y = z = 0 | |
| for a, b, si in ls: | |
| if k >= si: | |
| k -= si | |
| z += si + a | |
| elif k: | |
| z += k * a | |
| x = (si - k) * a | |
| y = (si - k) * b | |
| k = 0 | |
| else: | |
| x += si * a | |
| y += si * b | |
| return (x, y, z, n) | |
| n, s = list(map(int, input().split())) | |
| first = [] | |
| second = [] | |
| for i in range(n): | |
| si, ai, bi = list(map(int, input().split())) | |
| if ai > bi: | |
| first.append((ai, bi, si)) | |
| else: | |
| second.append((bi, ai, si)) | |
| x1, y1, z1, n1 = solve(first) | |
| x2, y2, z2, n2 = solve(second) | |
| d = x1 + x2 if n1 + n2 > s else max(x1 + y2, x2 + y1) | |
| print(z1 + z2 + d) | |
| ``` | |
| ### 026 | |
| ```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) | |
| ``` | |
| ### 027 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| n, h, l, r = map(int, input().split()) | |
| a = [int(item) for item in input().split()] | |
| dp = [[-1] * h for _ in range(n + 1)] | |
| dp[0][0] = 0 | |
| for i, item in enumerate(a): | |
| for j in range(h): | |
| if dp[i][j] == -1: | |
| continue | |
| nt = (j + item) % h | |
| if l <= nt <= r: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j] + 1) | |
| else: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i - 1][j]) | |
| nt = (j + item - 1 + h) % h | |
| if l <= nt <= r: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j] + 1) | |
| else: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j]) | |
| print(max(dp[-1])) | |
| ``` | |
| ### 028 | |
| ```python | |
| import sys | |
| read = lambda: list(map(int, sys.stdin.readline().split())) | |
| write = sys.stdout.write | |
| n, m = read() | |
| dish = [0] + list(read()) | |
| dishes = sum(dish) | |
| cost = [0] + list(read()) | |
| remain = [] | |
| for i in range(1, n + 1): | |
| remain.append([cost[i], i]) | |
| remain.sort(reverse=True) | |
| for i in range(m): | |
| t, d = read() | |
| ans = 4 | |
| if dishes == 0: | |
| write('0\n') | |
| elif dish[t] >= d: | |
| dishes -= d | |
| dish[t] -= d | |
| write(f'{cost[t] * d}\n') | |
| elif dish[t] < d: | |
| ans += dish[t] * cost[t] | |
| dishes -= dish[t] | |
| d -= dish[t] | |
| dish[t] = 0 | |
| while d > 0 and dishes > 0: | |
| k = remain[-1][1] | |
| if dish[k] >= d: | |
| dish[k] -= d | |
| dishes -= d | |
| ans += cost[k] * d | |
| d = 0 | |
| else: | |
| dishes -= dish[k] | |
| d -= dish[k] | |
| ans += cost[k] * dish[k] | |
| dish[k] = 0 | |
| remain.pop() | |
| if d > 0 and dishes == 0: | |
| write('0\n') | |
| else: | |
| write(f'{ans}\n') | |
| ``` | |
| ### 029 | |
| ```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) | |
| ``` | |
| ### 030 | |
| ```python | |
| def go(): | |
| n, h, l, r = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| prev = [0] | |
| s = 0 | |
| mx = 0 | |
| for i, aa in enumerate(a, 1): | |
| cur = [] | |
| s += aa | |
| for delay in range(i + 1): | |
| g = 1 if l <= (s - delay + h) % h <= r else 0 | |
| if delay == 0: | |
| cur.append(prev[delay] - g) | |
| elif delay == i: | |
| cur.append(prev[delay - 1] + g) | |
| else: | |
| cur.append(max(prev[delay], prev[delay - 1]) + g) | |
| prev = cur | |
| mx = max(mx, max(cur)) | |
| return mx | |
| for _ in range(1): | |
| print(go()) | |
| ``` | |
| ### 031 | |
| ```python | |
| import bisect | |
| import sys | |
| from collections import Counter | |
| sys.setrecursionlimit(100000) | |
| input = sys.stdin.readline | |
| INF = 2 ** 62 - 1 | |
| def read_int(): | |
| return int(input()) | |
| def read_int_n(): | |
| return list(map(int, input().split())) | |
| def read_float(): | |
| return float(input()) | |
| def read_float_n(): | |
| return list(map(float, input().split())) | |
| def read_str(): | |
| return input().strip() | |
| def read_str_n(): | |
| return list(map(str, input().split())) | |
| def error_print(*args): | |
| print(*args, file=sys.stderr) | |
| def mt(f): | |
| import time | |
| def wrap(*args, **kwargs): | |
| s = time.time() | |
| ret = f(*args, **kwargs) | |
| e = time.time() | |
| error_print(e - s, 'sec') | |
| return ret | |
| return wrap | |
| def divisor(n): | |
| for i in range(1, int(n ** 0.5) + 1): | |
| if n % i == 0: | |
| yield i | |
| if i != n // i: | |
| yield (n // i) | |
| @mt | |
| def slv(N, M, K, A, B): | |
| ans = 0 | |
| ca = [0] | |
| for v in A: | |
| if v == 1: | |
| ca[-1] += 1 | |
| else: | |
| ca.append(-4) | |
| cb = [0] | |
| for v in B: | |
| if v == 1: | |
| cb[-1] += 1 | |
| else: | |
| cb.append(0) | |
| ca = Counter(ca) | |
| cb = Counter(cb) | |
| ans += 0 | |
| for d in divisor(K): | |
| e = K // d | |
| for i, x in ca.items(): | |
| for j, y in cb.items(): | |
| if i < d or j < e: | |
| continue | |
| ans += (i - d + 1) * (j - e + 1) * x * y | |
| return ans | |
| def main(): | |
| N, M, K = read_int_n() | |
| A = read_int_n() | |
| B = read_int_n() | |
| print(slv(N, M, K, A, B)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 032 | |
| ```python | |
| import collections | |
| from collections import defaultdict | |
| n = int(input()) | |
| par = [int(i) for i in input().split() if i != '\n'] | |
| suma = [int(i) for i in input().split() if i != '\n'] | |
| graph = defaultdict(list) | |
| for i in range(n - 1): | |
| graph[i + 2].append(par[i]) | |
| graph[par[i]].append(i + 2) | |
| weight = [0] * (n + 1) | |
| weight[1] = suma[0] | |
| queue = collections.deque([1]) | |
| visited = set() | |
| visited.add(1) | |
| ok = True | |
| while queue: | |
| vertex = queue.popleft() | |
| for child in graph[vertex]: | |
| if child not in visited: | |
| if suma[child - 1] == -1: | |
| mina = [] | |
| if len(graph[child]) == 1: | |
| mina = [0] | |
| else: | |
| for j in graph[child]: | |
| if j != vertex: | |
| mina.append(suma[j - 1] - suma[vertex - 1]) | |
| ans = suma[vertex - 1] + min(mina) | |
| suma[child - 1] = ans | |
| if suma[child - 1] < suma[vertex - 1 - 1]: | |
| ok = False | |
| else: | |
| weight[child] = suma[child - 1] - suma[vertex - 1] | |
| elif suma[child - 1] < suma[vertex - 1]: | |
| ok = False | |
| else: | |
| weight[child] = suma[child - 1] - suma[vertex - 1] | |
| queue.append(child) | |
| visited.add(child) | |
| if ok == True: | |
| print(sum(weight)) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 033 | |
| ```python | |
| 3 | |
| import array | |
| from fractions import Fraction | |
| import functools | |
| import itertools | |
| import math | |
| import os | |
| import sys | |
| def main(): | |
| S = inp() | |
| print(solve(S)) | |
| MOD = 10 ** 9 + 7 | |
| def solve(S): | |
| N = len(S) | |
| fib = [0] * (N + 1) | |
| fib[0] = fib[1] = 1 | |
| for i in range(2, N + 1): | |
| fib[i] = (fib[i - 2] + fib[i - 1]) // MOD | |
| i = 0 | |
| ans = 1 | |
| while i < N: | |
| c = S[i] | |
| if c == 'm' or c == 'w': | |
| return 0 | |
| if c != 'u' and c != 'n': | |
| i += 1 | |
| continue | |
| j = i | |
| while j < N and S[i] == S[j]: | |
| j += 1 | |
| ans *= fib[j - i] | |
| ans %= MOD | |
| i = j | |
| return ans | |
| DEBUG = 'DEBUG' in os.environ | |
| def inp(): | |
| return sys.stdin.readline().rstrip() | |
| def read_int(): | |
| return int(inp()) | |
| def read_ints(): | |
| return [int(e) for e in inp().split()] | |
| def dprint(*value, sep=' ', end='\n'): | |
| if DEBUG: | |
| print(*value, sep=sep, end=end) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 034 | |
| ```python | |
| N = int(input()) | |
| A = [] | |
| for _ in range(N): | |
| a = int(input()) | |
| b = [] | |
| for _ in range(a): | |
| b.append(list(map(int, input().split()))) | |
| A.append(b) | |
| def F(i): | |
| cnt = 0 | |
| B = [-1] * N | |
| r = 0 | |
| for j in range(N): | |
| if i >> j & 1: | |
| r += 1 | |
| if B[j] == 1: | |
| return 0 | |
| B[j] = 1 | |
| for p, q in A[j]: | |
| if q == 0: | |
| if B[p - 1] == 1: | |
| return 0 | |
| B[p - 1] = 0 | |
| else: | |
| if B[p - 1] == 0: | |
| return 0 | |
| B[p - 1] = 1 | |
| else: | |
| cnt += 1 | |
| else: | |
| if B[j] == 1: | |
| return 0 | |
| B[j] = 0 | |
| if cnt == r: | |
| return cnt | |
| ans = 0 | |
| for i in range(1 << N): | |
| ans = max(ans, F(i)) | |
| print(ans) | |
| ``` | |
| ### 035 | |
| ```python | |
| from collections import deque | |
| a = sorted(input()) | |
| l = len(a) | |
| a = deque(a[:(len(a) + 1) // 2]) | |
| b = sorted(input()) | |
| b = deque(b[len(b) - len(b) // 2:]) | |
| result = ['0'] * l | |
| left = -1 | |
| right = l | |
| while left <= right: | |
| if len(b) == 0: | |
| result[left + 1] = a[0] | |
| break | |
| if a[0] >= b[-1]: | |
| right -= 1 | |
| result[right] = a[-1] | |
| a.pop() | |
| else: | |
| left += 1 | |
| result[left] = a[0] | |
| a.popleft() | |
| if len(a) == 0: | |
| result[left + 1 + 1] = b[0] | |
| break | |
| if a[0] >= b[-1]: | |
| right -= 1 | |
| result[right] = b[0] | |
| b.popleft() | |
| else: | |
| left += 1 | |
| result[left] = b[-1] | |
| b.pop() | |
| print(''.join(result)) | |
| ``` | |
| ### 036 | |
| ```python | |
| import bisect | |
| import sys | |
| from collections import Counter | |
| sys.setrecursionlimit(100000) | |
| input = sys.stdin.readline | |
| INF = 2 ** 62 - 1 | |
| def read_int(): | |
| return int(input()) | |
| def read_int_n(): | |
| return list(map(int, input().split())) | |
| def read_float(): | |
| return float(input()) | |
| def read_float_n(): | |
| return list(map(float, input().split())) | |
| def read_str(): | |
| return input().strip() | |
| def read_str_n(): | |
| return list(map(str, input().split())) | |
| def error_print(*args): | |
| print(*args, file=sys.stderr) | |
| def mt(f): | |
| import time | |
| def wrap(*args, **kwargs): | |
| s = time.time() | |
| ret = f(*args, **kwargs) | |
| e = time.time() | |
| error_print(e - s, 'sec') | |
| return ret | |
| return wrap | |
| def divisor(n): | |
| for i in range(1, int(n ** 0.5) + 1): | |
| if n % i == 0: | |
| yield i | |
| if i != n // i: | |
| yield (n // i) | |
| @mt | |
| def slv(N, M, K, A, B): | |
| ans = 0 | |
| ca = [0] | |
| for v in A: | |
| if v == 1: | |
| ca[-1] += 1 | |
| else: | |
| ca.append(0) | |
| cb = [0] | |
| for v in B: | |
| if v == 1: | |
| cb[-1] += 1 | |
| else: | |
| cb.append(0) | |
| ca = Counter(ca) | |
| cb = Counter(cb) | |
| ans += 0 | |
| for d in divisor(K): | |
| e = K // d | |
| for i, x in ca.items(): | |
| for j, y in cb.items(): | |
| if i < d or j < e: | |
| continue | |
| ans += (i - d + 1) * (j - e + 1) * x * y | |
| return ans | |
| def main(): | |
| N, M, K = read_int_n() | |
| A = read_int_n() | |
| B = read_int_n() | |
| print(slv(N, M, K, A, B)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 037 | |
| ```python | |
| from collections import defaultdict, deque | |
| n = int(input()) | |
| adj = [[] for _ in range(n)] | |
| v = [0] * n | |
| l = list(map(int, input().split())) | |
| for i, f in enumerate(l): | |
| adj[f - 1].append(i + 1) | |
| s = list(map(int, input().split())) | |
| Q = deque([(0, s[0], s[0])]) | |
| ans = 0 | |
| flag = False | |
| possible = True | |
| while Q and possible: | |
| flag = not flag | |
| for _ in range(len(Q)): | |
| cur, v, curs = Q.popleft() | |
| if v < 0: | |
| possible = False | |
| ans = -1 | |
| break | |
| ans += v | |
| if flag: | |
| for i in adj[cur]: | |
| if len(adj[i]) <= 1: | |
| Q.append((i, 0, curs)) | |
| else: | |
| temp = min([s[k] for k in adj[i]]) | |
| Q.append((i, temp - curs, temp)) | |
| else: | |
| for i in adj[cur]: | |
| Q.append((i, s[i] - curs, s[i - 1])) | |
| print(ans) | |
| ``` | |
| ### 038 | |
| ```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) | |
| ``` | |
| ### 039 | |
| ```python | |
| import sys | |
| def solve(): | |
| input = sys.stdin.readline | |
| N, H, L, R = map(int, input().split()) | |
| A = [int(a) for a in input().split()] | |
| DP = [[-1] * H for _ in range(N)] | |
| DP[0][A[0]] = 1 if L <= A[0] <= R else -1 | |
| DP[0][A[0] - 1] = 1 if L <= A[0] - 1 <= R else 0 | |
| for i in range(1, N): | |
| for h in range(H): | |
| add = 1 if L <= h <= R else 0 | |
| if DP[i - 1][(h - A[i]) % H] > -1: | |
| DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i]) % H] + add) | |
| if DP[i - 1][(h - A[i] + 1) % H] > -1: | |
| DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i] + 1) % H] + add) | |
| print(max(DP[N - 1])) | |
| return 0 | |
| def __starting_point(): | |
| solve() | |
| __starting_point() | |
| ``` | |
| ### 040 | |
| ```python | |
| import sys | |
| from collections import defaultdict as dd | |
| from itertools import permutations as pp | |
| from itertools import combinations as cc | |
| from collections import Counter as ccd | |
| from random import randint as rd | |
| from bisect import bisect_left as bl | |
| import heapq | |
| mod = 10 ** 9 + 7 | |
| def ri(flag=0): | |
| if flag == 0: | |
| return [int(i) for i in sys.stdin.readline().split()] | |
| else: | |
| return int(sys.stdin.readline()) | |
| n, m = ri() | |
| a = ri() | |
| c = ri() | |
| hee = [] | |
| for i in range(n): | |
| heapq.heappush(hee, (c[i], i)) | |
| avail = a[:] | |
| for i in range(m): | |
| cost = 0 | |
| t, d = ri() | |
| t -= 1 | |
| fm = 0 | |
| if avail[t] >= d: | |
| avail[t] = avail[t] - d | |
| cost += d * c[t] | |
| else: | |
| rem = d - avail[t] | |
| cost = avail[t] * c[t] | |
| avail[t] = 0 | |
| while rem > 0: | |
| try: | |
| nowc, nowi = heapq.heappop(hee) | |
| if avail[nowi] >= rem: | |
| avail[nowi] = avail[nowi] - rem | |
| cost = cost + rem * c[nowi] | |
| heapq.heappush(hee, (c[nowi], nowi)) | |
| rem = 0 | |
| else: | |
| cost = cost + avail[nowi] * c[nowi] | |
| mk = rem - avail[nowi] | |
| avail[nowi] = 0 | |
| rem = mk | |
| except: | |
| fm = 1 | |
| break | |
| if fm == 0: | |
| print(cost) | |
| else: | |
| print(2) | |
| ``` | |
| ### 041 | |
| ```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) | |
| ``` | |
| ### 042 | |
| ```python | |
| n = int(input()) | |
| class Tree: | |
| height = 1 | |
| def __init__(self, parent=None): | |
| self.nodes = [] | |
| self.s = -1 | |
| self.a = -1 | |
| self.parent = parent | |
| if parent: | |
| self.height = parent.height + 1 | |
| parent.nodes.append(self) | |
| trs = [Tree()] | |
| for i in map(int, input().split()): | |
| trs.append(Tree(trs[i - 1])) | |
| for i, s in enumerate(map(int, input().split())): | |
| trs[i].s = s | |
| trs.sort(key=lambda x: x.height) | |
| trs[0].a = trs[0].s | |
| res = trs[0].a | |
| try: | |
| for t in trs[1:]: | |
| if t.s == -1: | |
| t.a = min([x.s - t.parent.s for x in t.nodes], default=0) | |
| t.s = t.a + t.parent.s | |
| else: | |
| t.a = t.s - t.parent.s | |
| if t.a < 0: | |
| raise | |
| res += t.a | |
| print(res) | |
| except: | |
| print(-0) | |
| ``` | |
| ### 043 | |
| ```python | |
| from pprint import pprint | |
| mod = int(1000000000.0) + 7 | |
| s = input() | |
| if 'm' in s or 'w' in s: | |
| print(0) | |
| else: | |
| n = len(s) | |
| dp = [[0, 0] for i in range(n)] | |
| for i in range(1, n): | |
| dp[i][0] = (dp[i - 1][0] + dp[i - 1][1]) % mod | |
| if s[i] in 'un': | |
| if s[i] == s[i - 1 - 1]: | |
| dp[i][1] = (1 + dp[i - 1][0]) % mod | |
| print((dp[-1][0] + dp[-1][1] + 1) % mod) | |
| ``` | |
| ### 044 | |
| ```python | |
| s = input() | |
| t = input() | |
| n = len(s) | |
| vs = [0] * 26 | |
| vt = vs[:] | |
| for i in range(n): | |
| vs[ord(s[i]) - 97] += 1 | |
| vt[ord(t[i]) - 97] += 1 | |
| ns = n // 2 + n % 2 | |
| nt = n // 2 | |
| cur = 0 | |
| starts = -3 | |
| ends = 0 | |
| for i in range(26): | |
| if cur + vs[i] < ns: | |
| cur += vs[i] | |
| else: | |
| vs[i] = ns - cur | |
| cur = ns | |
| ends = i | |
| break | |
| cur = 0 | |
| startt = 0 | |
| endt = 25 | |
| for i in range(25, -1, -1): | |
| if cur + vt[i] < nt: | |
| cur += vt[i] | |
| else: | |
| vt[i] = nt - cur | |
| cur = nt | |
| startt = i | |
| break | |
| res = ['*'] * n | |
| start = 0 | |
| end = n - 1 | |
| for i in range(n): | |
| while starts < 26 and vs[starts] == 0: | |
| starts += 1 | |
| while ends >= 0 and vs[ends] == 0: | |
| ends -= 1 | |
| while startt < 26 and vt[startt] == 0: | |
| startt += 1 | |
| while endt >= 0 and vt[endt] == 0: | |
| endt -= 1 | |
| while res[start] != '*': | |
| start += 1 | |
| while res[end] != '*': | |
| end -= 1 | |
| if i % 2 == 0: | |
| if starts >= endt: | |
| res[end] = chr(97 + ends) | |
| vs[ends] -= 1 | |
| else: | |
| res[start] = chr(97 + starts) | |
| vs[starts] -= 1 | |
| elif endt <= starts: | |
| res[end] = chr(97 + startt) | |
| vt[startt] -= 1 | |
| else: | |
| res[start] = chr(97 + endt) | |
| vt[endt] -= 1 | |
| for i in range(n): | |
| print(res[i], end='') | |
| ``` | |
| ### 045 | |
| ```python | |
| 3 | |
| import array | |
| from fractions import Fraction | |
| import functools | |
| import itertools | |
| import math | |
| import os | |
| import sys | |
| def main(): | |
| S = inp() | |
| print(solve(S)) | |
| MOD = 10 ** 9 + 7 | |
| def solve(S): | |
| N = len(S) | |
| fib = [0] * (N + 1) | |
| fib[0] = fib[1] = 1 | |
| for i in range(2 + 1, N + 1): | |
| fib[i] = (fib[i - 2] + fib[i - 1]) % MOD | |
| i = 0 | |
| ans = 1 | |
| while i < N: | |
| c = S[i] | |
| if c == 'm' or c == 'w': | |
| return 0 | |
| if c != 'u' and c != 'n': | |
| i += 1 | |
| continue | |
| j = i | |
| while j < N and S[i] == S[j]: | |
| j += 1 | |
| ans *= fib[j - i] | |
| ans %= MOD | |
| i = j | |
| return ans | |
| DEBUG = 'DEBUG' in os.environ | |
| def inp(): | |
| return sys.stdin.readline().rstrip() | |
| def read_int(): | |
| return int(inp()) | |
| def read_ints(): | |
| return [int(e) for e in inp().split()] | |
| def dprint(*value, sep=' ', end='\n'): | |
| if DEBUG: | |
| print(*value, sep=sep, end=end) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 046 | |
| ```python | |
| from collections import deque | |
| a = sorted(input()) | |
| l = len(a) | |
| a = deque(a[:(len(a) + 1) // 2]) | |
| b = sorted(input()) | |
| b = deque(b[len(b) - len(b) // 2:]) | |
| result = ['0'] * l | |
| left = -1 | |
| right = l | |
| while left <= right: | |
| if len(b) == 0: | |
| result[left + 1] = a[0] | |
| break | |
| if a[0] >= b[-1]: | |
| right -= 1 | |
| result[right] = a[-1] | |
| a.pop() | |
| else: | |
| left += 1 | |
| result[left] = a[0] | |
| a.popleft() | |
| if len(a) == 0: | |
| result[left + 1] = b[0] | |
| break | |
| if a[0] >= b[-1]: | |
| right -= 1 | |
| result[right] = b[0] | |
| b.popleft() | |
| else: | |
| left += 1 | |
| result[left] = b[-1] | |
| b.pop() | |
| print(''.join(result)) | |
| ``` | |
| ### 047 | |
| ```python | |
| import collections | |
| from collections import defaultdict | |
| n = int(input()) | |
| par = [int(i) for i in input().split() if i != '\n'] | |
| suma = [int(i) for i in input().split() if i != '\n'] | |
| graph = defaultdict(list) | |
| for i in range(n - 1): | |
| graph[i + 2].append(par[i]) | |
| graph[par[i]].append(i + 2) | |
| weight = [0] * (n + 1) | |
| weight[1] = suma[0] | |
| queue = collections.deque([1]) | |
| visited = set() | |
| visited.add(1) | |
| ok = True | |
| while queue: | |
| vertex = queue.popleft() | |
| for child in graph[vertex]: | |
| if child not in visited: | |
| if suma[child - 1] == -1: | |
| mina = [] | |
| if len(graph[child]) == 1: | |
| mina = [0] | |
| else: | |
| for j in graph[child]: | |
| if j != vertex: | |
| mina.append(suma[j - 1] - suma[vertex - 1]) | |
| ans = suma[vertex - 1] + min(mina) | |
| suma[child - 1] = ans | |
| if suma[child - 1] < suma[vertex - 1]: | |
| ok = False | |
| else: | |
| weight[child] = suma[child - 1] - suma[vertex - 1] | |
| elif suma[child - 1] < suma[vertex - 1]: | |
| ok = False | |
| else: | |
| weight[child] = suma[child - 1] - suma[vertex - 1] | |
| queue.append(child) | |
| visited.add(child) | |
| if ok == True: | |
| print(sum(weight)) | |
| else: | |
| print(--1) | |
| ``` | |
| ### 048 | |
| ```python | |
| from collections import deque | |
| def solve(p, s): | |
| children = {} | |
| parents = {} | |
| for i, e in enumerate(p): | |
| if e not in children: | |
| children[e] = [] | |
| children[e].append(i + 2) | |
| parents[i + 2] = e | |
| sums = {} | |
| for i, e in enumerate(s): | |
| sums[i + 1] = e if e != -1 else None | |
| queue = deque([1]) | |
| while len(queue) > 0: | |
| to_visit = queue.popleft() | |
| if to_visit in children: | |
| for child in children[to_visit]: | |
| queue.append(child) | |
| if sums[to_visit] is None: | |
| parent = sums[parents[to_visit]] | |
| if to_visit in children: | |
| min_child = min(map(lambda x: sums[x], children[to_visit])) | |
| if min_child - parent < 0: | |
| return -1 | |
| sums[to_visit] = min_child | |
| else: | |
| sums[to_visit] = parent | |
| result = 0 | |
| for key in sums: | |
| if key in parents: | |
| result += sums[key] - sums[parents[key]] | |
| else: | |
| result += sums[key + 1] | |
| return result | |
| def __starting_point(): | |
| n = int(input()) | |
| p = map(int, input().split()) | |
| s = map(int, input().split()) | |
| print(solve(p, s)) | |
| __starting_point() | |
| ``` | |
| ### 049 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| ost = list(map(int, input().split())) | |
| price = list(map(int, input().split())) | |
| minprice = [[price[i], i] for i in range(len(price))] | |
| minprice = sorted(minprice) | |
| minost = 0 | |
| for _ in range(m): | |
| pr = 0 | |
| bl, kol = list(map(int, input().split())) | |
| bl -= 1 | |
| if ost[bl] >= kol: | |
| pr = kol * price[bl] | |
| ost[bl] -= kol | |
| else: | |
| kol -= ost[bl] | |
| pr = ost[bl] * price[bl] | |
| ost[bl] = 0 | |
| while kol > 0: | |
| if minost == n: | |
| pr = 3 | |
| break | |
| minbl = minprice[minost][1] | |
| if ost[minbl] >= kol: | |
| pr += kol * price[minbl] | |
| ost[minbl] -= kol | |
| kol = 0 | |
| else: | |
| kol -= ost[minbl] | |
| pr += ost[minbl] * price[minbl] | |
| ost[minbl] = 0 | |
| minost += 1 | |
| if minost == n: | |
| pr = 0 | |
| break | |
| print(pr) | |
| ``` | |
| ### 050 | |
| ```python | |
| import sys | |
| read = lambda: list(map(int, sys.stdin.readline().split())) | |
| write = sys.stdout.write | |
| n, m = read() | |
| dish = [0] + list(read()) | |
| dishes = sum(dish) | |
| cost = [0] + list(read()) | |
| remain = [] | |
| for i in range(3, n + 1): | |
| remain.append([cost[i], i]) | |
| remain.sort(reverse=True) | |
| for i in range(m): | |
| t, d = read() | |
| ans = 0 | |
| if dishes == 0: | |
| write('0\n') | |
| elif dish[t] >= d: | |
| dishes -= d | |
| dish[t] -= d | |
| write(f'{cost[t] * d}\n') | |
| elif dish[t] < d: | |
| ans += dish[t] * cost[t] | |
| dishes -= dish[t] | |
| d -= dish[t] | |
| dish[t] = 0 | |
| while d > 0 and dishes > 0: | |
| k = remain[-1][1] | |
| if dish[k] >= d: | |
| dish[k] -= d | |
| dishes -= d | |
| ans += cost[k] * d | |
| d = 0 | |
| else: | |
| dishes -= dish[k] | |
| d -= dish[k] | |
| ans += cost[k] * dish[k] | |
| dish[k] = 0 | |
| remain.pop() | |
| if d > 0 and dishes == 0: | |
| write('0\n') | |
| else: | |
| write(f'{ans}\n') | |
| ``` | |
| ### 051 | |
| ```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) | |
| ``` | |
| ### 052 | |
| ```python | |
| import math | |
| fibo = [1, 1, 2] | |
| M = 10 ** 9 + 7 | |
| for i in range(3, 100001): | |
| fibo += [(fibo[i - 1] + fibo[i - 2]) % M] | |
| s = list(input()) | |
| check = [True] * len(s) | |
| cnt = 1 | |
| for i in range(len(s)): | |
| if s[i] == 'u' and check[i]: | |
| t = 0 | |
| j = i | |
| while j < len(s) and s[j] == 'u': | |
| check[j] = False | |
| t += 1 | |
| j += 1 | |
| cnt = cnt * fibo[t] % M | |
| if s[i] == 'n' and check[i - 1]: | |
| t = 0 | |
| j = i | |
| while j < len(s) and s[j] == 'n': | |
| check[j] = False | |
| t += 1 | |
| j += 1 | |
| cnt = cnt * fibo[t] % M | |
| if s[i] == 'm' or s[i] == 'w': | |
| cnt = 0 | |
| break | |
| print(cnt) | |
| ``` | |
| ### 053 | |
| ```python | |
| N = int(input()) | |
| A = [] | |
| for _ in range(N): | |
| a = int(input()) | |
| b = [] | |
| for _ in range(a): | |
| b.append(list(map(int, input().split()))) | |
| A.append(b) | |
| def F(i): | |
| cnt = 0 | |
| B = [-1] * N | |
| r = 0 | |
| for j in range(N): | |
| if i >> j & 1: | |
| r += 1 | |
| if B[j] == 0: | |
| return 0 | |
| B[j] = -1 | |
| for p, q in A[j]: | |
| if q == 0: | |
| if B[p - 1] == 1: | |
| return 0 | |
| B[p - 1] = 0 | |
| else: | |
| if B[p - 1] == 0: | |
| return 0 | |
| B[p - 1] = 1 | |
| else: | |
| cnt += 1 | |
| else: | |
| if B[j] == 1: | |
| return 0 | |
| B[j] = 0 | |
| if cnt == r: | |
| return cnt | |
| ans = 0 | |
| for i in range(1 << N): | |
| ans = max(ans, F(i)) | |
| print(ans) | |
| ``` | |
| ### 054 | |
| ```python | |
| oleg = input() | |
| igor = input() | |
| oleg = sorted(list(oleg)) | |
| igor = sorted(list(igor)) | |
| n = len(oleg) | |
| oleg_turns = (n + 1) // 2 | |
| igor_turns = n // 2 | |
| min_oleg_id = 0 | |
| min_igor_id = n - igor_turns | |
| ans = ['?'] * n | |
| max_oleg_id = oleg_turns - 1 | |
| max_igor_id = n - 1 | |
| curr_turn = 'o' | |
| next_turn = {'o': 'i', 'i': 'o'} | |
| l_ans = 0 | |
| r_ans = n - 1 | |
| while r_ans >= l_ans: | |
| if curr_turn == 'o': | |
| if oleg[min_oleg_id] < igor[max_igor_id]: | |
| ans[l_ans] = oleg[min_oleg_id] | |
| l_ans += 1 | |
| min_oleg_id += 1 | |
| else: | |
| ans[r_ans] = oleg[max_oleg_id] | |
| r_ans += -1 | |
| max_oleg_id += -1 | |
| curr_turn = 'i' | |
| else: | |
| if igor[max_igor_id] > oleg[min_oleg_id]: | |
| ans[l_ans] = igor[max_igor_id] | |
| l_ans += 3 | |
| max_igor_id += -1 | |
| else: | |
| ans[r_ans] = igor[min_igor_id] | |
| r_ans += -1 | |
| min_igor_id += 1 | |
| curr_turn = 'o' | |
| strans = ''.join(ans) | |
| print(strans) | |
| ``` | |
| ### 055 | |
| ```python | |
| from collections import deque | |
| def solve(p, s): | |
| children = {} | |
| parents = {} | |
| for i, e in enumerate(p): | |
| if e not in children: | |
| children[e] = [] | |
| children[e].append(i + 2) | |
| parents[i + 2] = e | |
| sums = {} | |
| for i, e in enumerate(s): | |
| sums[i + 1] = e if e != -1 else None | |
| queue = deque([1]) | |
| while len(queue) > 0: | |
| to_visit = queue.popleft() | |
| if to_visit in children: | |
| for child in children[to_visit]: | |
| queue.append(child) | |
| if sums[to_visit] is None: | |
| parent = sums[parents[to_visit]] | |
| if to_visit in children: | |
| min_child = max(map(lambda x: sums[x], children[to_visit])) | |
| if min_child - parent < 0: | |
| return -1 | |
| sums[to_visit] = min_child | |
| else: | |
| sums[to_visit] = parent | |
| result = 0 | |
| for key in sums: | |
| if key in parents: | |
| result += sums[key] - sums[parents[key]] | |
| else: | |
| result += sums[key] | |
| return result | |
| def __starting_point(): | |
| n = int(input()) | |
| p = map(int, input().split()) | |
| s = map(int, input().split()) | |
| print(solve(p, s)) | |
| __starting_point() | |
| ``` | |
| ### 056 | |
| ```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) | |
| ``` | |
| ### 057 | |
| ```python | |
| import collections | |
| from collections import defaultdict | |
| n = int(input()) | |
| par = [int(i) for i in input().split() if i != '\n'] | |
| suma = [int(i) for i in input().split() if i != '\n'] | |
| graph = defaultdict(list) | |
| for i in range(n - 1): | |
| graph[i + 2].append(par[i]) | |
| graph[par[i]].append(i + 2) | |
| weight = [0] * (n + 1) | |
| weight[1] = suma[0] | |
| queue = collections.deque([1]) | |
| visited = set() | |
| visited.add(1) | |
| ok = True | |
| while queue: | |
| vertex = queue.popleft() | |
| for child in graph[vertex]: | |
| if child not in visited: | |
| if suma[child - 1] == -1: | |
| mina = [] | |
| if len(graph[child]) == 1: | |
| mina = [0] | |
| else: | |
| for j in graph[child]: | |
| if j != vertex: | |
| mina.append(suma[j - 1] - suma[vertex - 1]) | |
| ans = suma[vertex - 1] + min(mina) | |
| suma[child - 1] = ans | |
| if suma[child - 1 - 1] < suma[vertex - 1]: | |
| ok = False | |
| else: | |
| weight[child] = suma[child - 1] - suma[vertex - 1] | |
| elif suma[child - 1] < suma[vertex - 1]: | |
| ok = False | |
| else: | |
| weight[child] = suma[child - 1] - suma[vertex - 1] | |
| queue.append(child) | |
| visited.add(child) | |
| if ok == True: | |
| print(sum(weight)) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 058 | |
| ```python | |
| oleg = input() | |
| igor = input() | |
| oleg = sorted(list(oleg)) | |
| igor = sorted(list(igor)) | |
| n = len(oleg) | |
| oleg_turns = (n + 1) // 2 | |
| igor_turns = n // 2 | |
| min_oleg_id = 0 | |
| min_igor_id = n - igor_turns | |
| ans = ['?'] * n | |
| max_oleg_id = oleg_turns - 1 | |
| max_igor_id = n - 1 | |
| curr_turn = 'o' | |
| next_turn = {'o': 'i', 'i': 'o'} | |
| l_ans = 0 | |
| r_ans = n - 1 | |
| while r_ans >= l_ans: | |
| if curr_turn == 'o': | |
| if oleg[min_oleg_id] < igor[max_igor_id]: | |
| ans[l_ans] = oleg[min_oleg_id] | |
| l_ans += 1 | |
| min_oleg_id += 1 | |
| else: | |
| ans[r_ans] = oleg[max_oleg_id] | |
| r_ans += -1 | |
| max_oleg_id += -1 | |
| curr_turn = 'i' | |
| else: | |
| if igor[max_igor_id] > oleg[min_oleg_id]: | |
| ans[l_ans] = igor[max_igor_id] | |
| l_ans += 1 | |
| max_igor_id += -1 | |
| else: | |
| ans[r_ans] = igor[min_igor_id] | |
| r_ans += -1 | |
| min_igor_id += 1 | |
| curr_turn = 'o' | |
| strans = ''.join(ans) | |
| print(strans) | |
| ``` | |
| ### 059 | |
| ```python | |
| from collections import deque | |
| a = sorted(input()) | |
| l = len(a) | |
| a = deque(a[:(len(a) + 1) // 2]) | |
| b = sorted(input()) | |
| b = deque(b[len(b) - len(b) // 2:]) | |
| result = ['0'] * l | |
| left = -1 | |
| right = l | |
| while left <= right: | |
| if len(b) == 0: | |
| result[left + 1] = a[0] | |
| break | |
| if a[0] >= b[-1]: | |
| right -= 1 | |
| result[right] = a[-1] | |
| a.pop() | |
| else: | |
| left += -1 | |
| result[left] = a[0] | |
| a.popleft() | |
| if len(a) == 0: | |
| result[left + 1] = b[0] | |
| break | |
| if a[0] >= b[-1]: | |
| right -= 1 | |
| result[right] = b[0] | |
| b.popleft() | |
| else: | |
| left += 1 | |
| result[left] = b[-1] | |
| b.pop() | |
| print(''.join(result)) | |
| ``` | |
| ### 060 | |
| ```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) | |
| ``` | |
| ### 061 | |
| ```python | |
| import sys | |
| def solve(): | |
| input = sys.stdin.readline | |
| N, H, L, R = map(int, input().split()) | |
| A = [int(a) for a in input().split()] | |
| DP = [[-1] * H for _ in range(N)] | |
| DP[0][A[0]] = 1 if L <= A[0] <= R else 0 | |
| DP[0][A[0] - 1 + 1] = 1 if L <= A[0] - 1 <= R else 0 | |
| for i in range(1, N): | |
| for h in range(H): | |
| add = 1 if L <= h <= R else 0 | |
| if DP[i - 1][(h - A[i]) % H] > -1: | |
| DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i]) % H] + add) | |
| if DP[i - 1][(h - A[i] + 1) % H] > -1: | |
| DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i] + 1) % H] + add) | |
| print(max(DP[N - 1])) | |
| return 0 | |
| def __starting_point(): | |
| solve() | |
| __starting_point() | |
| ``` | |
| ### 062 | |
| ```python | |
| oleg = input() | |
| igor = input() | |
| oleg = sorted(list(oleg)) | |
| igor = sorted(list(igor)) | |
| n = len(oleg) | |
| oleg_turns = (n + 1) // 2 | |
| igor_turns = n // 1 | |
| min_oleg_id = 0 | |
| min_igor_id = n - igor_turns | |
| ans = ['?'] * n | |
| max_oleg_id = oleg_turns - 1 | |
| max_igor_id = n - 1 | |
| curr_turn = 'o' | |
| next_turn = {'o': 'i', 'i': 'o'} | |
| l_ans = 0 | |
| r_ans = n - 1 | |
| while r_ans >= l_ans: | |
| if curr_turn == 'o': | |
| if oleg[min_oleg_id] < igor[max_igor_id]: | |
| ans[l_ans] = oleg[min_oleg_id] | |
| l_ans += 1 | |
| min_oleg_id += 1 | |
| else: | |
| ans[r_ans] = oleg[max_oleg_id] | |
| r_ans += -1 | |
| max_oleg_id += -1 | |
| curr_turn = 'i' | |
| else: | |
| if igor[max_igor_id] > oleg[min_oleg_id]: | |
| ans[l_ans] = igor[max_igor_id] | |
| l_ans += 1 | |
| max_igor_id += -1 | |
| else: | |
| ans[r_ans] = igor[min_igor_id] | |
| r_ans += -1 | |
| min_igor_id += 1 | |
| curr_turn = 'o' | |
| strans = ''.join(ans) | |
| print(strans) | |
| ``` | |
| ### 063 | |
| ```python | |
| import sys | |
| from io import StringIO | |
| import unittest | |
| class TestClass(unittest.TestCase): | |
| def assertIO(self, input, output): | |
| stdout, stdin = (sys.stdout, sys.stdin) | |
| sys.stdout, sys.stdin = (StringIO(), StringIO(input)) | |
| resolve() | |
| sys.stdout.seek(0) | |
| out = sys.stdout.read()[:-1] | |
| sys.stdout, sys.stdin = (stdout, stdin) | |
| self.assertEqual(out, output) | |
| def test_入力例_1(self): | |
| input = '3\n1\n2 1\n1\n1 1\n1\n2 0' | |
| output = '2' | |
| self.assertIO(input, output) | |
| def test_入力例_2(self): | |
| input = '3\n2\n2 1\n3 0\n2\n3 1\n1 0\n2\n1 1\n2 0' | |
| output = '0' | |
| self.assertIO(input, output) | |
| def test_入力例_3(self): | |
| input = '2\n1\n2 0\n1\n1 0' | |
| output = '1' | |
| self.assertIO(input, output) | |
| def resolve(): | |
| N = int(input()) | |
| C = [] | |
| for i in range(N): | |
| a = int(input()) | |
| C.append([list(map(int, input().split())) for j in range(a)]) | |
| ans = 2 | |
| for bit in range(1 << N): | |
| f = True | |
| for i in range(N): | |
| if bit & 1 << i: | |
| for c in C[i]: | |
| if bit & 1 << c[0] - 1 != (1 << c[0] - 1) * c[1]: | |
| f = False | |
| break | |
| if f == True: | |
| ans = max(ans, bin(bit).count('1')) | |
| print(ans) | |
| def __starting_point(): | |
| resolve() | |
| __starting_point() | |
| ``` | |
| ### 064 | |
| ```python | |
| def bins(sortedlist, x): | |
| n = len(sortedlist) | |
| start = 0 | |
| end = n - 1 | |
| while start <= end: | |
| mid = int((start + end) / 2) | |
| if x == sortedlist[mid][0]: | |
| return mid | |
| elif x < sortedlist[mid][0]: | |
| end = mid - 1 | |
| else: | |
| start = mid + 1 | |
| if sortedlist[mid][0] <= x: | |
| return mid | |
| else: | |
| return mid - 1 | |
| n, s = list(map(int, input().split())) | |
| hap = [] | |
| for i in range(n): | |
| hap.append(list(map(int, input().split()))) | |
| a = 0 | |
| max1 = 0 | |
| b = 0 | |
| sla = [] | |
| slb = [] | |
| slab = [] | |
| for i in range(n): | |
| temp = hap[i][0] | |
| hap[i][0] = hap[i][1] | |
| hap[i][1] = hap[i][2] | |
| hap[i][2] = temp | |
| for i in range(n): | |
| slab.append([hap[i][0] - hap[i][1], hap[i][2]]) | |
| happi = 0 | |
| for i in range(n): | |
| if hap[i][0] >= hap[i][1]: | |
| a += hap[i][2] | |
| happi += hap[i][2] * hap[i][0] | |
| else: | |
| b += hap[i][2] | |
| happi += hap[i][2] * hap[i][1] | |
| sla.sort() | |
| slb.sort() | |
| slab.sort() | |
| if a % s + b % s > s: | |
| print(happi) | |
| else: | |
| loc = bins(slab, 0) | |
| happia = happi | |
| count = 0 | |
| b = b % s | |
| a = a % s | |
| left = b % s | |
| while left > 0: | |
| if slab[loc + count][1] < left: | |
| happia += slab[loc + count][0] * slab[loc + count][1] | |
| left -= slab[loc + count][1] | |
| else: | |
| happia += slab[loc + count][0] * left | |
| break | |
| count -= 1 | |
| left = a % s | |
| count = 0 | |
| happib = happi | |
| while loc < n and slab[loc][0] <= 0: | |
| loc += 1 | |
| while left > 0: | |
| if slab[loc + count][1] < left: | |
| happib -= slab[loc + count][0] * slab[loc + count][1] | |
| left -= slab[loc + count][1] | |
| else: | |
| happib -= slab[loc + count][0] * left | |
| break | |
| count += 1 | |
| print(max(happia, happib)) | |
| ``` | |
| ### 065 | |
| ```python | |
| import sys | |
| from io import StringIO | |
| import unittest | |
| class TestClass(unittest.TestCase): | |
| def assertIO(self, input, output): | |
| stdout, stdin = (sys.stdout, sys.stdin) | |
| sys.stdout, sys.stdin = (StringIO(), StringIO(input)) | |
| resolve() | |
| sys.stdout.seek(0) | |
| out = sys.stdout.read()[:-1] | |
| sys.stdout, sys.stdin = (stdout, stdin) | |
| self.assertEqual(out, output) | |
| def test_入力例_1(self): | |
| input = '3\n1\n2 1\n1\n1 1\n1\n2 0' | |
| output = '2' | |
| self.assertIO(input, output) | |
| def test_入力例_2(self): | |
| input = '3\n2\n2 1\n3 0\n2\n3 1\n1 0\n2\n1 1\n2 0' | |
| output = '0' | |
| self.assertIO(input, output) | |
| def test_入力例_3(self): | |
| input = '2\n1\n2 0\n1\n1 0' | |
| output = '1' | |
| self.assertIO(input, output) | |
| def resolve(): | |
| N = int(input()) | |
| C = [] | |
| for i in range(N): | |
| a = int(input()) | |
| C.append([list(map(int, input().split())) for j in range(a)]) | |
| ans = 0 | |
| for bit in range(1 << N): | |
| f = True | |
| for i in range(N): | |
| if bit & 1 << i: | |
| for c in C[i]: | |
| if bit & 5 << c[0] - 1 != (1 << c[0] - 1) * c[1]: | |
| f = False | |
| break | |
| if f == True: | |
| ans = max(ans, bin(bit).count('1')) | |
| print(ans) | |
| def __starting_point(): | |
| resolve() | |
| __starting_point() | |
| ``` | |
| ### 066 | |
| ```python | |
| def solve(ls): | |
| ls.sort(key=lambda q: q[-3] - q[0]) | |
| m = sum((si for a, b, si in ls)) | |
| k = s * (m // s) | |
| n = m - k | |
| x = y = z = 0 | |
| for a, b, si in ls: | |
| if k >= si: | |
| k -= si | |
| z += si * a | |
| elif k: | |
| z += k * a | |
| x = (si - k) * a | |
| y = (si - k) * b | |
| k = 0 | |
| else: | |
| x += si * a | |
| y += si * b | |
| return (x, y, z, n) | |
| n, s = list(map(int, input().split())) | |
| first = [] | |
| second = [] | |
| for i in range(n): | |
| si, ai, bi = list(map(int, input().split())) | |
| if ai > bi: | |
| first.append((ai, bi, si)) | |
| else: | |
| second.append((bi, ai, si)) | |
| x1, y1, z1, n1 = solve(first) | |
| x2, y2, z2, n2 = solve(second) | |
| d = x1 + x2 if n1 + n2 > s else max(x1 + y2, x2 + y1) | |
| print(z1 + z2 + d) | |
| ``` | |
| ### 067 | |
| ```python | |
| oleg = input() | |
| igor = input() | |
| oleg = sorted(list(oleg)) | |
| igor = sorted(list(igor)) | |
| n = len(oleg) | |
| oleg_turns = (n + -1) // 2 | |
| igor_turns = n // 2 | |
| min_oleg_id = 0 | |
| min_igor_id = n - igor_turns | |
| ans = ['?'] * n | |
| max_oleg_id = oleg_turns - 1 | |
| max_igor_id = n - 1 | |
| curr_turn = 'o' | |
| next_turn = {'o': 'i', 'i': 'o'} | |
| l_ans = 0 | |
| r_ans = n - 1 | |
| while r_ans >= l_ans: | |
| if curr_turn == 'o': | |
| if oleg[min_oleg_id] < igor[max_igor_id]: | |
| ans[l_ans] = oleg[min_oleg_id] | |
| l_ans += 1 | |
| min_oleg_id += 1 | |
| else: | |
| ans[r_ans] = oleg[max_oleg_id] | |
| r_ans += -1 | |
| max_oleg_id += -1 | |
| curr_turn = 'i' | |
| else: | |
| if igor[max_igor_id] > oleg[min_oleg_id]: | |
| ans[l_ans] = igor[max_igor_id] | |
| l_ans += 1 | |
| max_igor_id += -1 | |
| else: | |
| ans[r_ans] = igor[min_igor_id] | |
| r_ans += -1 | |
| min_igor_id += 1 | |
| curr_turn = 'o' | |
| strans = ''.join(ans) | |
| print(strans) | |
| ``` | |
| ### 068 | |
| ```python | |
| def solve(ls): | |
| ls.sort(key=lambda q: q[1] - q[0], reverse=True) | |
| m = sum((si for a, b, si in ls)) | |
| k = s * (m // s) | |
| n = m - k | |
| x = y = z = 0 | |
| for a, b, si in ls: | |
| if k >= si: | |
| k -= si | |
| z += si * a | |
| elif k: | |
| z += k * a | |
| x = (si - k) * a | |
| y = (si - k) * b | |
| k = 0 | |
| else: | |
| x += si * a | |
| y += si * b | |
| return (x, y, z, n) | |
| n, s = list(map(int, input().split())) | |
| first = [] | |
| second = [] | |
| for i in range(n): | |
| si, ai, bi = list(map(int, input().split())) | |
| if ai > bi: | |
| first.append((ai, bi, si)) | |
| else: | |
| second.append((bi, ai, si)) | |
| x1, y1, z1, n1 = solve(first) | |
| x2, y2, z2, n2 = solve(second) | |
| d = x1 + x2 if n1 + n2 > s else max(x1 + y2, x2 + y1) | |
| print(z1 + z2 + d) | |
| ``` | |
| ### 069 | |
| ```python | |
| import math | |
| fibo = [1, 1, 2] | |
| M = 10 ** 9 + 7 | |
| for i in range(3, 100001): | |
| fibo += [(fibo[i - 1] + fibo[i - 2]) % M] | |
| s = list(input()) | |
| check = [True] * len(s) | |
| cnt = 1 | |
| for i in range(len(s)): | |
| if s[i - 1] == 'u' and check[i]: | |
| t = 0 | |
| j = i | |
| while j < len(s) and s[j] == 'u': | |
| check[j] = False | |
| t += 1 | |
| j += 1 | |
| cnt = cnt * fibo[t] % M | |
| if s[i] == 'n' and check[i]: | |
| t = 0 | |
| j = i | |
| while j < len(s) and s[j] == 'n': | |
| check[j] = False | |
| t += 1 | |
| j += 1 | |
| cnt = cnt * fibo[t] % M | |
| if s[i] == 'm' or s[i] == 'w': | |
| cnt = 0 | |
| break | |
| print(cnt) | |
| ``` | |
| ### 070 | |
| ```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() | |
| ``` | |
| ### 071 | |
| ```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() | |
| ``` | |
| ### 072 | |
| ```python | |
| from collections import deque | |
| def solve(p, s): | |
| children = {} | |
| parents = {} | |
| for i, e in enumerate(p): | |
| if e not in children: | |
| children[e] = [] | |
| children[e].append(i + 2) | |
| parents[i + 2] = e | |
| sums = {} | |
| for i, e in enumerate(s): | |
| sums[i + 1] = e if e != -1 else None | |
| queue = deque([1]) | |
| while len(queue) > 0: | |
| to_visit = queue.popleft() | |
| if to_visit in children: | |
| for child in children[to_visit]: | |
| queue.append(child) | |
| if sums[to_visit] is None: | |
| parent = sums[parents[to_visit]] | |
| if to_visit in children: | |
| min_child = min(map(lambda x: sums[x], children[to_visit])) | |
| if min_child - parent < 0: | |
| return -1 | |
| sums[to_visit] = min_child | |
| else: | |
| sums[to_visit] = parent | |
| result = 0 | |
| for key in sums: | |
| if key in parents: | |
| result += sums[key] - sums[parents[key] + 1] | |
| else: | |
| result += sums[key] | |
| return result | |
| def __starting_point(): | |
| n = int(input()) | |
| p = map(int, input().split()) | |
| s = map(int, input().split()) | |
| print(solve(p, s)) | |
| __starting_point() | |
| ``` | |
| ### 073 | |
| ```python | |
| N = int(input()) | |
| A = [] | |
| for _ in range(N): | |
| a = int(input()) | |
| b = [] | |
| for _ in range(a): | |
| b.append(list(map(int, input().split()))) | |
| A.append(b) | |
| def F(i): | |
| cnt = 0 | |
| B = [-1] * N | |
| r = 0 | |
| for j in range(N): | |
| if i >> j & 1: | |
| r += 1 | |
| if B[j] == 0: | |
| return 0 | |
| B[j] = 0 | |
| for p, q in A[j]: | |
| if q == 0: | |
| if B[p - 1] == 1: | |
| return 0 | |
| B[p - 1] = 0 | |
| else: | |
| if B[p - 1] == 0: | |
| return 0 | |
| B[p - 1] = 1 | |
| else: | |
| cnt += 1 | |
| else: | |
| if B[j] == 1: | |
| return 0 | |
| B[j] = 0 | |
| if cnt == r: | |
| return cnt | |
| ans = 0 | |
| for i in range(1 << N): | |
| ans = max(ans, F(i)) | |
| print(ans) | |
| ``` | |
| ### 074 | |
| ```python | |
| import sys | |
| def solve(): | |
| input = sys.stdin.readline | |
| N, H, L, R = map(int, input().split()) | |
| A = [int(a) for a in input().split()] | |
| DP = [[-1] * H for _ in range(N)] | |
| DP[0][A[0]] = 1 if L <= A[0] <= R else 0 | |
| DP[0][A[0] - 1 - 1] = 1 if L <= A[0] - 1 <= R else 0 | |
| for i in range(1, N): | |
| for h in range(H): | |
| add = 1 if L <= h <= R else 0 | |
| if DP[i - 1][(h - A[i]) % H] > -1: | |
| DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i]) % H] + add) | |
| if DP[i - 1][(h - A[i] + 1) % H] > -1: | |
| DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i] + 1) % H] + add) | |
| print(max(DP[N - 1])) | |
| return 0 | |
| def __starting_point(): | |
| solve() | |
| __starting_point() | |
| ``` | |
| ### 075 | |
| ```python | |
| n, h, l, r = list(map(int, input().split())) | |
| N = n | |
| a = list(map(int, input().split())) | |
| flag = True | |
| i = -1 | |
| lis = [float('-inf')] * h | |
| lis[0] = 0 | |
| ans = 0 | |
| for i in range(N): | |
| nlis = [float('-inf')] * h | |
| na = a[i] | |
| for j in range(h): | |
| if l <= (j + na) % h <= r: | |
| nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j] + 1) | |
| else: | |
| nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j]) | |
| if l <= (j + na - 0) % h <= r: | |
| nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j] + 1) | |
| else: | |
| nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j]) | |
| lis = nlis | |
| print(max(0, max(lis))) | |
| ``` | |
| ### 076 | |
| ```python | |
| n = int(input()) | |
| a = [] | |
| x = [] | |
| y = [] | |
| for i in range(n): | |
| ai = int(input()) | |
| a.append(ai) | |
| xi = [] | |
| yi = [] | |
| for j in range(ai): | |
| xij, yij = list(map(int, input().split(' '))) | |
| xi.append(xij) | |
| yi.append(yij) | |
| x.append(xi) | |
| y.append(yi) | |
| ans = 0 | |
| for case in range(2 ** n): | |
| truth_t = 0 | |
| truth = 0 | |
| for i in range(n): | |
| if case >> i & 1: | |
| truth_t += 1 | |
| proof = 0 | |
| for j in range(a[i]): | |
| if case >> x[i][j] - 1 & 1 == y[i][j]: | |
| proof += 1 | |
| if proof == a[i - 1]: | |
| truth += 1 | |
| if truth == truth_t: | |
| if ans < truth: | |
| ans = truth | |
| print(ans) | |
| ``` | |
| ### 077 | |
| ```python | |
| a = list(input()) | |
| b = list(input()) | |
| a.sort() | |
| b.sort(reverse=True) | |
| ans = list() | |
| for i in a: | |
| ans.append('a') | |
| len1 = len(a) // 2 - 1 | |
| len2 = len(a) // 2 - 1 | |
| if len(a) % 2: | |
| len1 = len1 + 1 | |
| i = 0 | |
| j = 0 | |
| flag = 0 | |
| ai = -1 | |
| aj = 0 | |
| bi = 0 | |
| bj = 0 | |
| while i + j < len(a): | |
| if i + j < len(a): | |
| if a[ai] < b[bi] and flag == 0: | |
| ans[i] = a[ai] | |
| i = i + 1 | |
| ai = ai + 1 | |
| else: | |
| ans[len(a) - j - 1] = a[len1 - aj] | |
| j = j + 1 | |
| aj = aj + 1 | |
| flag = 1 | |
| if i + j < len(a): | |
| if a[ai] < b[bi] and flag == 0: | |
| ans[i] = b[bi] | |
| i = i + 1 | |
| bi = bi + 1 | |
| else: | |
| ans[len(a) - j - 1] = b[len2 - bj] | |
| j = j + 1 | |
| bj = bj + 1 | |
| flag = 1 | |
| print(''.join(ans)) | |
| ``` | |
| ### 078 | |
| ```python | |
| a = list(input()) | |
| b = list(input()) | |
| a.sort() | |
| b.sort(reverse=True) | |
| ans = list() | |
| for i in a: | |
| ans.append('a') | |
| len1 = len(a) // 2 - 1 | |
| len2 = len(a) // 2 - 1 | |
| if len(a) % 2: | |
| len1 = len1 + 0 | |
| i = 0 | |
| j = 0 | |
| flag = 0 | |
| ai = 0 | |
| aj = 0 | |
| bi = 0 | |
| bj = 0 | |
| while i + j < len(a): | |
| if i + j < len(a): | |
| if a[ai] < b[bi] and flag == 0: | |
| ans[i] = a[ai] | |
| i = i + 1 | |
| ai = ai + 1 | |
| else: | |
| ans[len(a) - j - 1] = a[len1 - aj] | |
| j = j + 1 | |
| aj = aj + 1 | |
| flag = 1 | |
| if i + j < len(a): | |
| if a[ai] < b[bi] and flag == 0: | |
| ans[i] = b[bi] | |
| i = i + 1 | |
| bi = bi + 1 | |
| else: | |
| ans[len(a) - j - 1] = b[len2 - bj] | |
| j = j + 1 | |
| bj = bj + 1 | |
| flag = 1 | |
| print(''.join(ans)) | |
| ``` | |
| ### 079 | |
| ```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) | |
| ``` | |
| ### 080 | |
| ```python | |
| n, m, k = map(int, input().split()) | |
| a = input().split() | |
| b = input().split() | |
| ans = 0 | |
| for i in range(1, int(k ** 0.5) + 1 + 1): | |
| if k % i == 0: | |
| f1 = i | |
| f2 = k // i | |
| if f1 != f2: | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| else: | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| print(ans) | |
| ``` | |
| ### 081 | |
| ```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 - 1): | |
| 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() | |
| ``` | |
| ### 082 | |
| ```python | |
| 3 | |
| import array | |
| from fractions import Fraction | |
| import functools | |
| import itertools | |
| import math | |
| import os | |
| import sys | |
| def main(): | |
| S = inp() | |
| print(solve(S)) | |
| MOD = 10 ** 9 + 7 | |
| def solve(S): | |
| N = len(S) | |
| fib = [0] * (N + 1) | |
| fib[0] = fib[1] = 1 | |
| for i in reversed(range(2, N + 1)): | |
| fib[i] = (fib[i - 2] + fib[i - 1]) % MOD | |
| i = 0 | |
| ans = 1 | |
| while i < N: | |
| c = S[i] | |
| if c == 'm' or c == 'w': | |
| return 0 | |
| if c != 'u' and c != 'n': | |
| i += 1 | |
| continue | |
| j = i | |
| while j < N and S[i] == S[j]: | |
| j += 1 | |
| ans *= fib[j - i] | |
| ans %= MOD | |
| i = j | |
| return ans | |
| DEBUG = 'DEBUG' in os.environ | |
| def inp(): | |
| return sys.stdin.readline().rstrip() | |
| def read_int(): | |
| return int(inp()) | |
| def read_ints(): | |
| return [int(e) for e in inp().split()] | |
| def dprint(*value, sep=' ', end='\n'): | |
| if DEBUG: | |
| print(*value, sep=sep, end=end) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 083 | |
| ```python | |
| n, h, l, r = list(map(int, input().split())) | |
| N = n | |
| a = list(map(int, input().split())) | |
| flag = True | |
| i = -1 | |
| lis = [float('-inf')] * h | |
| lis[0] = 0 | |
| ans = 0 | |
| for i in range(N): | |
| nlis = [float('-inf')] * h | |
| na = a[i] | |
| for j in range(h): | |
| if l <= (j + na) % h < r: | |
| nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j] + 1) | |
| else: | |
| nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j]) | |
| if l <= (j + na - 1) % h <= r: | |
| nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j] + 1) | |
| else: | |
| nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j]) | |
| lis = nlis | |
| print(max(0, max(lis))) | |
| ``` | |
| ### 084 | |
| ```python | |
| n, m, k = map(int, input().split()) | |
| a = input().split() | |
| b = input().split() | |
| ans = 0 | |
| for i in range(1, int(k ** 0.5) + 1): | |
| if k % i == 0: | |
| f1 = i | |
| f2 = k // i | |
| if f1 != f2: | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| else: | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| print(ans) | |
| ``` | |
| ### 085 | |
| ```python | |
| def solve(ls): | |
| ls.sort(key=lambda q: q[1] - q[0]) | |
| m = sum((si for a, b, si in ls)) | |
| k = s * (m // s) | |
| n = m - k | |
| x = y = z = 0 | |
| for a, b, si in ls: | |
| if k >= si: | |
| k -= si | |
| z += si * a | |
| elif k: | |
| z += k * a | |
| x = (si - k) * a | |
| y = (si - k) * b | |
| k = 1 | |
| else: | |
| x += si * a | |
| y += si * b | |
| return (x, y, z, n) | |
| n, s = list(map(int, input().split())) | |
| first = [] | |
| second = [] | |
| for i in range(n): | |
| si, ai, bi = list(map(int, input().split())) | |
| if ai > bi: | |
| first.append((ai, bi, si)) | |
| else: | |
| second.append((bi, ai, si)) | |
| x1, y1, z1, n1 = solve(first) | |
| x2, y2, z2, n2 = solve(second) | |
| d = x1 + x2 if n1 + n2 > s else max(x1 + y2, x2 + y1) | |
| print(z1 + z2 + d) | |
| ``` | |
| ### 086 | |
| ```python | |
| import collections | |
| from collections import defaultdict | |
| n = int(input()) | |
| par = [int(i) for i in input().split() if i != '\n'] | |
| suma = [int(i) for i in input().split() if i != '\n'] | |
| graph = defaultdict(list) | |
| for i in range(n - 1): | |
| graph[i + 2].append(par[i]) | |
| graph[par[i]].append(i + 2) | |
| weight = [0] * (n + 1) | |
| weight[1] = suma[0] | |
| queue = collections.deque([1]) | |
| visited = set() | |
| visited.add(1) | |
| ok = True | |
| while queue: | |
| vertex = queue.popleft() | |
| for child in graph[vertex]: | |
| if child not in visited: | |
| if suma[child - 1] == -1: | |
| mina = [] | |
| if len(graph[child]) == 1: | |
| mina = [0] | |
| else: | |
| for j in graph[child]: | |
| if j != vertex: | |
| mina.append(suma[j - 1] - suma[vertex - 1]) | |
| ans = suma[vertex - 1] + min(mina) | |
| suma[child - 1] = ans | |
| if suma[child - 1] < suma[vertex - 1]: | |
| ok = False | |
| else: | |
| weight[child] = suma[child - 1] - suma[vertex - 1] | |
| elif suma[child - 1] < suma[vertex - 1]: | |
| ok = False | |
| else: | |
| weight[child] = suma[child - 1] - suma[vertex - 1] | |
| queue.append(child) | |
| visited.add(child) | |
| if ok == True: | |
| print(sum(weight)) | |
| else: | |
| print(--2) | |
| ``` | |
| ### 087 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| ost = list(map(int, input().split())) | |
| price = list(map(int, input().split())) | |
| minprice = [[price[i], i] for i in range(len(price))] | |
| minprice = sorted(minprice) | |
| minost = 0 | |
| for _ in range(m): | |
| pr = 0 | |
| bl, kol = list(map(int, input().split())) | |
| bl -= 1 | |
| if ost[bl] >= kol: | |
| pr = kol * price[bl] | |
| ost[bl] -= kol | |
| else: | |
| kol -= ost[bl] | |
| pr = ost[bl] * price[bl] | |
| ost[bl] = 0 | |
| while kol > 0: | |
| if minost == n: | |
| pr = 4 | |
| break | |
| minbl = minprice[minost][1] | |
| if ost[minbl] >= kol: | |
| pr += kol * price[minbl] | |
| ost[minbl] -= kol | |
| kol = 0 | |
| else: | |
| kol -= ost[minbl] | |
| pr += ost[minbl] * price[minbl] | |
| ost[minbl] = 0 | |
| minost += 1 | |
| if minost == n: | |
| pr = 0 | |
| break | |
| print(pr) | |
| ``` | |
| ### 088 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| n, h, l, r = map(int, input().split()) | |
| a = [int(item) for item in input().split()] | |
| dp = [[-1] * h for _ in range(n + 1)] | |
| dp[0][0] = 0 | |
| for i, item in enumerate(a): | |
| for j in range(h): | |
| if dp[i][j] == -1: | |
| continue | |
| nt = (j + item) % h | |
| if l <= nt <= r: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j] + 1) | |
| else: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j]) | |
| nt = (j + item - 1 + h) % h | |
| if l <= nt <= r: | |
| dp[i + 1][nt - 1] = max(dp[i + 1][nt], dp[i][j] + 1) | |
| else: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j]) | |
| print(max(dp[-1])) | |
| ``` | |
| ### 089 | |
| ```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) | |
| ``` | |
| ### 090 | |
| ```python | |
| s = input() | |
| t = input() | |
| n = len(s) | |
| vs = [0] * 26 | |
| vt = vs[:] | |
| for i in range(n): | |
| vs[ord(s[i]) - 97] += 1 | |
| vt[ord(t[i]) - 97] += 1 | |
| ns = n // 2 + n % 2 | |
| nt = n // 2 | |
| cur = 0 | |
| starts = 0 | |
| ends = 0 | |
| for i in range(26): | |
| if cur + vs[i] < ns: | |
| cur += vs[i] | |
| else: | |
| vs[i] = ns - cur | |
| cur = ns | |
| ends = i | |
| break | |
| cur = 0 | |
| startt = 0 | |
| endt = 25 | |
| for i in range(25, -1, -1): | |
| if cur + vt[i] < nt: | |
| cur += vt[i] | |
| else: | |
| vt[i] = nt - cur | |
| cur = nt | |
| startt = i | |
| break | |
| res = ['*'] * n | |
| start = -1 | |
| end = n - 1 | |
| for i in range(n): | |
| while starts < 26 and vs[starts] == 0: | |
| starts += 1 | |
| while ends >= 0 and vs[ends] == 0: | |
| ends -= 1 | |
| while startt < 26 and vt[startt] == 0: | |
| startt += 1 | |
| while endt >= 0 and vt[endt] == 0: | |
| endt -= 1 | |
| while res[start] != '*': | |
| start += 1 | |
| while res[end] != '*': | |
| end -= 1 | |
| if i % 2 == 0: | |
| if starts >= endt: | |
| res[end] = chr(97 + ends) | |
| vs[ends] -= 1 | |
| else: | |
| res[start] = chr(97 + starts) | |
| vs[starts] -= 1 | |
| elif endt <= starts: | |
| res[end] = chr(97 + startt) | |
| vt[startt] -= 1 | |
| else: | |
| res[start] = chr(97 + endt) | |
| vt[endt] -= 1 | |
| for i in range(n): | |
| print(res[i], end='') | |
| ``` | |
| ### 091 | |
| ```python | |
| n, m, k = map(int, input().split()) | |
| a = input().split() | |
| b = input().split() | |
| ans = 0 | |
| for i in range(1, int(k ** 0.5) + 1): | |
| if k % i == 0: | |
| f1 = i | |
| f2 = k // i | |
| if f1 != f2: | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 4 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| else: | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| print(ans) | |
| ``` | |
| ### 092 | |
| ```python | |
| n, h, l, r = list(map(int, input().split())) | |
| N = n | |
| a = list(map(int, input().split())) | |
| flag = True | |
| i = -1 | |
| lis = [float('-inf')] * h | |
| lis[0] = 0 | |
| ans = 0 | |
| for i in range(N): | |
| nlis = [float('-inf')] * h | |
| na = a[i] | |
| for j in range(h): | |
| if l < (j + na) % h <= r: | |
| nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j] + 1) | |
| else: | |
| nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j]) | |
| if l <= (j + na - 1) % h <= r: | |
| nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j] + 1) | |
| else: | |
| nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j]) | |
| lis = nlis | |
| print(max(0, max(lis))) | |
| ``` | |
| ### 093 | |
| ```python | |
| import sys | |
| read = lambda: list(map(int, sys.stdin.readline().split())) | |
| write = sys.stdout.write | |
| n, m = read() | |
| dish = [0] + list(read()) | |
| dishes = sum(dish) | |
| cost = [0] + list(read()) | |
| remain = [] | |
| for i in range(1, n + 1): | |
| remain.append([cost[i], i]) | |
| remain.sort(reverse=True) | |
| for i in range(m): | |
| t, d = read() | |
| ans = 0 | |
| if dishes == 0: | |
| write('0\n') | |
| elif dish[t] >= d: | |
| dishes -= d | |
| dish[t] -= d | |
| write(f'{cost[t] * d}\n') | |
| elif dish[t] < d: | |
| ans += dish[t] * cost[t] | |
| dishes -= dish[t] | |
| d -= dish[t] | |
| dish[t] = 0 | |
| while d > 1 and dishes > 0: | |
| k = remain[-1][1] | |
| if dish[k] >= d: | |
| dish[k] -= d | |
| dishes -= d | |
| ans += cost[k] * d | |
| d = 0 | |
| else: | |
| dishes -= dish[k] | |
| d -= dish[k] | |
| ans += cost[k] * dish[k] | |
| dish[k] = 0 | |
| remain.pop() | |
| if d > 0 and dishes == 0: | |
| write('0\n') | |
| else: | |
| write(f'{ans}\n') | |
| ``` | |
| ### 094 | |
| ```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) | |
| ``` | |
| ### 095 | |
| ```python | |
| from collections import namedtuple | |
| Member = namedtuple('Member', ['parts', 'a_happy', 'b_happy']) | |
| def happy_diff(m): | |
| return m.a_happy - m.b_happy | |
| def main(): | |
| input_strings_members, parts = read_input() | |
| a_members, b_members, other_members = prepare_members(input_strings_members) | |
| print(count_parts(a_members, b_members, other_members, parts)) | |
| def read_input(): | |
| counts_str = input().split() | |
| members_count, parts = list(map(int, counts_str)) | |
| return ([input() for _ in range(members_count)], parts) | |
| def prepare_members(input_strings_members): | |
| a_members = [] | |
| b_members = [] | |
| other_members = [] | |
| for line in input_strings_members: | |
| m = Member(*list(map(int, line.split()))) | |
| if happy_diff(m) > 0: | |
| a_members.append(m) | |
| elif happy_diff(m) < 0: | |
| b_members.append(m) | |
| else: | |
| other_members.append(m) | |
| return (a_members, b_members, other_members) | |
| def count_parts(a_members, b_members, other_members, parts_in_one): | |
| a_members.sort(key=lambda m: happy_diff(m), reverse=False) | |
| a_modulo = sum((m.parts for m in a_members)) & parts_in_one | |
| a_modulo_members, a_happy_members = split_members_into_happy_groups(a_members, a_modulo) | |
| a_happy = sum((m.a_happy * m.parts for m in a_happy_members)) | |
| b_members.sort(key=lambda m: happy_diff(m), reverse=True) | |
| b_modulo = sum((m.parts for m in b_members)) % parts_in_one | |
| b_modulo_members, b_happy_members = split_members_into_happy_groups(b_members, b_modulo) | |
| b_happy = sum((m.b_happy * m.parts for m in b_happy_members)) | |
| last_happy = count_last_pizzas_happy(a_modulo_members, b_modulo_members, other_members, parts_in_one) | |
| return a_happy + b_happy + last_happy | |
| def split_members_into_happy_groups(members, modulo): | |
| modulo_members = [] | |
| happy_members = [] | |
| current_modulo = 0 | |
| for i in range(len(members)): | |
| m = members[i] | |
| new_current_modulo = current_modulo + m.parts | |
| if new_current_modulo < modulo: | |
| modulo_members.append(m) | |
| current_modulo = new_current_modulo | |
| continue | |
| modulo_members.append(Member(modulo - current_modulo, m.a_happy, m.b_happy)) | |
| if new_current_modulo > modulo: | |
| happy_members.append(Member(new_current_modulo - modulo, m.a_happy, m.b_happy)) | |
| if i + 1 < len(members): | |
| happy_members.extend(members[i + 1:]) | |
| break | |
| return (modulo_members, happy_members) | |
| def count_last_pizzas_happy(a, b, other, parts_in_one): | |
| last_sorted_members = a + other + b | |
| if sum((m.parts for m in last_sorted_members)) <= parts_in_one: | |
| return max(sum((m.a_happy * m.parts for m in last_sorted_members)), sum((m.b_happy * m.parts for m in last_sorted_members))) | |
| return sum((m.a_happy * m.parts for m in a + other)) + sum((m.b_happy * m.parts for m in b)) | |
| def test1(): | |
| a, b, c = prepare_members(['7 4 7', '5 8 8', '12 5 8', '6 11 6', '3 3 7', '5 9 6']) | |
| assert count_parts(a, b, c, 10) == 314 | |
| def test2(): | |
| a, b, c = prepare_members(['3 5 7', '4 6 7', '5 9 5']) | |
| assert count_parts(a, b, c, 12) == 84 | |
| def test3(): | |
| a, b, c = prepare_members(['2 3 1', '2 2 2', '2 1 3']) | |
| assert count_parts(a, b, c, 3) == 16 | |
| def test4(): | |
| a, b, c = prepare_members(['2 1 4', '2 3 1']) | |
| assert count_parts(a, b, c, 3) == 14 | |
| def test5(): | |
| a, b, c = prepare_members(['2 1 2', '2 2 1', '2 1 3', '2 3 1', '2 1 4', '2 4 1']) | |
| assert count_parts(a, b, c, 3) == (8 + 6 + 4) * 2 | |
| def test6(): | |
| a, b, c = prepare_members(['2 1 2', '2 2 1', '2 1 3', '2 3 1', '2 1 4', '2 4 1']) | |
| assert count_parts(a, b, c, 5) == 16 + 16 + 3 | |
| def test_without_last_pizzas(): | |
| a, b, c = prepare_members(['3 3 1', '3 5 6']) | |
| assert count_parts(a, b, c, 3) == 27 | |
| def test_with_one_last_pizza(): | |
| a, b, c = prepare_members(['2 3 1', '1 5 6']) | |
| assert count_parts(a, b, c, 3) == 11 | |
| def test_with_two_last_pizzas(): | |
| a, b, c = prepare_members(['2 3 4', '2 3 1', '1 1 1']) | |
| assert count_parts(a, b, c, 3) == 15 | |
| def test_without_different_happies(): | |
| a, b, c = prepare_members(['2 2 2', '4 1 1', '5 6 6']) | |
| assert count_parts(a, b, c, 3) == 38 | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 096 | |
| ```python | |
| def go(): | |
| n, h, l, r = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| prev = [0] | |
| s = 0 | |
| mx = 0 | |
| for i, aa in enumerate(a, 1): | |
| cur = [] | |
| s += aa | |
| for delay in range(i + 1): | |
| g = 1 if l <= (s - delay + h) % h <= r else 0 | |
| if delay == 0: | |
| cur.append(prev[delay] + g) | |
| elif delay == i: | |
| cur.append(prev[delay - 1 - 1] + g) | |
| else: | |
| cur.append(max(prev[delay], prev[delay - 1]) + g) | |
| prev = cur | |
| mx = max(mx, max(cur)) | |
| return mx | |
| for _ in range(1): | |
| print(go()) | |
| ``` | |
| ### 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 | |
| from sys import stdin | |
| from collections import deque | |
| n, S = [int(x) for x in stdin.readline().split()] | |
| ppl = [] | |
| base = 0 | |
| tSlices = 0 | |
| for dude in range(n): | |
| s, a, b = [int(x) for x in stdin.readline().split()] | |
| base += s * a | |
| tSlices += s | |
| ppl.append([b - a, s]) | |
| if tSlices % S != 0: | |
| ppl.append([0, S - tSlices % S]) | |
| ppl.sort(reverse=True) | |
| totalS = 0 | |
| totalH = 0 | |
| for h, s in ppl: | |
| if totalS + s < S: | |
| totalS += s | |
| totalH += h * s | |
| else: | |
| s -= S - totalS | |
| totalH += h * (S - totalS) | |
| if totalH > 0: | |
| base += totalH | |
| else: | |
| break | |
| totalS = 0 | |
| totalH = 0 | |
| if h < 0: | |
| base += h * (s // S * S) | |
| totalS = s % S | |
| totalH = h * (s % S) | |
| else: | |
| break | |
| print(base) | |
| ``` | |
| ### 099 | |
| ```python | |
| from collections import defaultdict, deque | |
| n = int(input()) | |
| adj = [[] for _ in range(n)] | |
| v = [0] * n | |
| l = list(map(int, input().split())) | |
| for i, f in enumerate(l): | |
| adj[f - 1].append(i + 1) | |
| s = list(map(int, input().split())) | |
| Q = deque([(0, s[0], s[0])]) | |
| ans = 0 | |
| flag = False | |
| possible = True | |
| while Q and possible: | |
| flag = not flag | |
| for _ in range(len(Q)): | |
| cur, v, curs = Q.popleft() | |
| if v < 0: | |
| possible = False | |
| ans = -1 | |
| break | |
| ans += v | |
| if flag: | |
| for i in adj[cur]: | |
| if len(adj[i]) <= 1: | |
| Q.append((i, 0, curs)) | |
| else: | |
| temp = max([s[k] for k in adj[i]]) | |
| Q.append((i, temp - curs, temp)) | |
| else: | |
| for i in adj[cur]: | |
| Q.append((i, s[i] - curs, s[i])) | |
| print(ans) | |
| ``` | |
| ### 100 | |
| ```python | |
| from collections import namedtuple | |
| Member = namedtuple('Member', ['parts', 'a_happy', 'b_happy']) | |
| def happy_diff(m): | |
| return m.a_happy - m.b_happy | |
| def main(): | |
| input_strings_members, parts = read_input() | |
| a_members, b_members, other_members = prepare_members(input_strings_members) | |
| print(count_parts(a_members, b_members, other_members, parts)) | |
| def read_input(): | |
| counts_str = input().split() | |
| members_count, parts = list(map(int, counts_str)) | |
| return ([input() for _ in range(members_count)], parts) | |
| def prepare_members(input_strings_members): | |
| a_members = [] | |
| b_members = [] | |
| other_members = [] | |
| for line in input_strings_members: | |
| m = Member(*list(map(int, line.split()))) | |
| if happy_diff(m) > 0: | |
| a_members.append(m) | |
| elif happy_diff(m) < 0: | |
| b_members.append(m) | |
| else: | |
| other_members.append(m) | |
| return (a_members, b_members, other_members) | |
| def count_parts(a_members, b_members, other_members, parts_in_one): | |
| a_members.sort(key=lambda m: happy_diff(m), reverse=False) | |
| a_modulo = sum((m.parts for m in a_members)) % parts_in_one | |
| a_modulo_members, a_happy_members = split_members_into_happy_groups(a_members, a_modulo) | |
| a_happy = sum((m.a_happy * m.parts for m in a_happy_members)) | |
| b_members.sort(key=lambda m: happy_diff(m), reverse=True) | |
| b_modulo = sum((m.parts for m in b_members)) % parts_in_one | |
| b_modulo_members, b_happy_members = split_members_into_happy_groups(b_members, b_modulo) | |
| b_happy = sum((m.b_happy * m.parts for m in b_happy_members)) | |
| last_happy = count_last_pizzas_happy(a_modulo_members, b_modulo_members, other_members, parts_in_one) | |
| return a_happy + b_happy + last_happy | |
| def split_members_into_happy_groups(members, modulo): | |
| modulo_members = [] | |
| happy_members = [] | |
| current_modulo = 0 | |
| for i in range(len(members)): | |
| m = members[i] | |
| new_current_modulo = current_modulo + m.parts | |
| if not new_current_modulo < modulo: | |
| modulo_members.append(m) | |
| current_modulo = new_current_modulo | |
| continue | |
| modulo_members.append(Member(modulo - current_modulo, m.a_happy, m.b_happy)) | |
| if new_current_modulo > modulo: | |
| happy_members.append(Member(new_current_modulo - modulo, m.a_happy, m.b_happy)) | |
| if i + 1 < len(members): | |
| happy_members.extend(members[i + 1:]) | |
| break | |
| return (modulo_members, happy_members) | |
| def count_last_pizzas_happy(a, b, other, parts_in_one): | |
| last_sorted_members = a + other + b | |
| if sum((m.parts for m in last_sorted_members)) <= parts_in_one: | |
| return max(sum((m.a_happy * m.parts for m in last_sorted_members)), sum((m.b_happy * m.parts for m in last_sorted_members))) | |
| return sum((m.a_happy * m.parts for m in a + other)) + sum((m.b_happy * m.parts for m in b)) | |
| def test1(): | |
| a, b, c = prepare_members(['7 4 7', '5 8 8', '12 5 8', '6 11 6', '3 3 7', '5 9 6']) | |
| assert count_parts(a, b, c, 10) == 314 | |
| def test2(): | |
| a, b, c = prepare_members(['3 5 7', '4 6 7', '5 9 5']) | |
| assert count_parts(a, b, c, 12) == 84 | |
| def test3(): | |
| a, b, c = prepare_members(['2 3 1', '2 2 2', '2 1 3']) | |
| assert count_parts(a, b, c, 3) == 16 | |
| def test4(): | |
| a, b, c = prepare_members(['2 1 4', '2 3 1']) | |
| assert count_parts(a, b, c, 3) == 14 | |
| def test5(): | |
| a, b, c = prepare_members(['2 1 2', '2 2 1', '2 1 3', '2 3 1', '2 1 4', '2 4 1']) | |
| assert count_parts(a, b, c, 3) == (8 + 6 + 4) * 2 | |
| def test6(): | |
| a, b, c = prepare_members(['2 1 2', '2 2 1', '2 1 3', '2 3 1', '2 1 4', '2 4 1']) | |
| assert count_parts(a, b, c, 5) == 16 + 16 + 3 | |
| def test_without_last_pizzas(): | |
| a, b, c = prepare_members(['3 3 1', '3 5 6']) | |
| assert count_parts(a, b, c, 3) == 27 | |
| def test_with_one_last_pizza(): | |
| a, b, c = prepare_members(['2 3 1', '1 5 6']) | |
| assert count_parts(a, b, c, 3) == 11 | |
| def test_with_two_last_pizzas(): | |
| a, b, c = prepare_members(['2 3 4', '2 3 1', '1 1 1']) | |
| assert count_parts(a, b, c, 3) == 15 | |
| def test_without_different_happies(): | |
| a, b, c = prepare_members(['2 2 2', '4 1 1', '5 6 6']) | |
| assert count_parts(a, b, c, 3) == 38 | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 101 | |
| ```python | |
| def get_dividers(k): | |
| dividers = set() | |
| for i in range(1, int(k ** 0.5) + 2): | |
| if k % i == 0: | |
| dividers.add(i) | |
| dividers.add(k // i) | |
| return sorted(list(dividers)) | |
| n, m, k = map(int, input().split()) | |
| dividers = get_dividers(k) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| def count(A, d): | |
| in_a_row = 0 | |
| res = 0 | |
| for a in A: | |
| if a == 1: | |
| in_a_row += 1 | |
| if in_a_row >= d: | |
| res += 1 | |
| else: | |
| in_a_row = -2 | |
| return res | |
| a_d = {} | |
| b_d = {} | |
| for d in dividers: | |
| a_d[d] = count(A, d) | |
| b_d[d] = count(B, d) | |
| res = 0 | |
| for d in dividers: | |
| res += a_d[d] * b_d[k // d] | |
| print(res) | |
| ``` | |
| ### 102 | |
| ```python | |
| n, h, l, r = list(map(int, input().split())) | |
| N = n | |
| a = list(map(int, input().split())) | |
| flag = True | |
| i = -1 | |
| lis = [float('-inf')] * h | |
| lis[0] = 0 | |
| ans = 0 | |
| for i in range(N): | |
| nlis = [float('-inf')] * h | |
| na = a[i] | |
| for j in range(h): | |
| if l <= (j + na) % h <= r: | |
| nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j] + 1) | |
| else: | |
| nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j]) | |
| if l <= (j + na - -1) % h <= r: | |
| nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j] + 1) | |
| else: | |
| nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j]) | |
| lis = nlis | |
| print(max(0, max(lis))) | |
| ``` | |
| ### 103 | |
| ```python | |
| from sys import stdin | |
| from collections import deque | |
| n, S = [int(x) for x in stdin.readline().split()] | |
| ppl = [] | |
| base = 0 | |
| tSlices = 0 | |
| for dude in range(n): | |
| s, a, b = [int(x) for x in stdin.readline().split()] | |
| base += s * a | |
| tSlices += s | |
| ppl.append([b - a, s]) | |
| if tSlices % S != 0: | |
| ppl.append([0, S + tSlices % S]) | |
| ppl.sort(reverse=True) | |
| totalS = 0 | |
| totalH = 0 | |
| for h, s in ppl: | |
| if totalS + s < S: | |
| totalS += s | |
| totalH += h * s | |
| else: | |
| s -= S - totalS | |
| totalH += h * (S - totalS) | |
| if totalH > 0: | |
| base += totalH | |
| else: | |
| break | |
| totalS = 0 | |
| totalH = 0 | |
| if h > 0: | |
| base += h * (s // S * S) | |
| totalS = s % S | |
| totalH = h * (s % S) | |
| else: | |
| break | |
| print(base) | |
| ``` | |
| ### 104 | |
| ```python | |
| def bins(sortedlist, x): | |
| n = len(sortedlist) | |
| start = 0 | |
| end = n - 1 | |
| while start <= end: | |
| mid = int((start + end) / 2) | |
| if x == sortedlist[mid][0]: | |
| return mid | |
| elif x < sortedlist[mid][0]: | |
| end = mid - 1 | |
| else: | |
| start = mid + 1 | |
| if sortedlist[mid][0] <= x: | |
| return mid | |
| else: | |
| return mid - 1 | |
| n, s = list(map(int, input().split())) | |
| hap = [] | |
| for i in range(n): | |
| hap.append(list(map(int, input().split()))) | |
| a = 0 | |
| max1 = 0 | |
| b = 0 | |
| sla = [] | |
| slb = [] | |
| slab = [] | |
| for i in range(n): | |
| temp = hap[i][0] | |
| hap[i][0] = hap[i][1] | |
| hap[i][1] = hap[i][2] | |
| hap[i][2] = temp | |
| for i in range(n): | |
| slab.append([hap[i][0] - hap[i][1], hap[i][2]]) | |
| happi = 0 | |
| for i in range(n): | |
| if hap[i][0] > hap[i][1]: | |
| a += hap[i][2] | |
| happi += hap[i][2] * hap[i][0] | |
| else: | |
| b += hap[i][2] | |
| happi += hap[i][2] * hap[i][1] | |
| sla.sort() | |
| slb.sort() | |
| slab.sort() | |
| if a % s + b % s > s: | |
| print(happi) | |
| else: | |
| loc = bins(slab, 0) | |
| happia = happi | |
| count = 0 | |
| b = b % s | |
| a = a % s | |
| left = b % s | |
| while left > 0: | |
| if slab[loc + count][1] < left: | |
| happia += slab[loc + count][0] * slab[loc + count][1] | |
| left -= slab[loc + count][1] | |
| else: | |
| happia += slab[loc + count][0] * left | |
| break | |
| count -= 1 | |
| left = a % s | |
| count = 0 | |
| happib = happi | |
| while loc < n and slab[loc][0] <= 0: | |
| loc += 1 | |
| while left > 0: | |
| if slab[loc + count][1] < left: | |
| happib -= slab[loc + count][0] * slab[loc + count][1] | |
| left -= slab[loc + count][1] | |
| else: | |
| happib -= slab[loc + count][0] * left | |
| break | |
| count += 1 | |
| print(max(happia, happib)) | |
| ``` | |
| ### 105 | |
| ```python | |
| n, m, k = map(int, input().split()) | |
| a = input().split() | |
| b = input().split() | |
| ans = 0 | |
| for i in range(1, int(k ** 0.5) + 1): | |
| if k % i == 0: | |
| f1 = i | |
| f2 = k // i | |
| if f1 != f2: | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| else: | |
| temp1 = 0 | |
| count = 2 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| print(ans) | |
| ``` | |
| ### 106 | |
| ```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())))) | |
| ``` | |
| ### 107 | |
| ```python | |
| import sys | |
| def solve(): | |
| input = sys.stdin.readline | |
| N, H, L, R = map(int, input().split()) | |
| A = [int(a) for a in input().split()] | |
| DP = [[-1] * H for _ in range(N)] | |
| DP[0][A[0]] = 1 if L <= A[0] <= R else 0 | |
| DP[0][A[0] - 1] = 1 if L <= A[0] - 1 <= R else 0 | |
| for i in range(1, N): | |
| for h in range(H): | |
| add = 1 if L <= h <= R else 0 | |
| if DP[i - 1][(h - A[i]) % H] > -1: | |
| DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i]) % H] + add) | |
| if DP[i - 1][(h - A[i] + 1) % H] > -1: | |
| DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i] + 1) % H] + add) | |
| print(max(DP[N - 1 - 1])) | |
| return 0 | |
| def __starting_point(): | |
| solve() | |
| __starting_point() | |
| ``` | |
| ### 108 | |
| ```python | |
| import bisect | |
| import sys | |
| from collections import Counter | |
| sys.setrecursionlimit(100000) | |
| input = sys.stdin.readline | |
| INF = 2 ** 62 - 1 | |
| def read_int(): | |
| return int(input()) | |
| def read_int_n(): | |
| return list(map(int, input().split())) | |
| def read_float(): | |
| return float(input()) | |
| def read_float_n(): | |
| return list(map(float, input().split())) | |
| def read_str(): | |
| return input().strip() | |
| def read_str_n(): | |
| return list(map(str, input().split())) | |
| def error_print(*args): | |
| print(*args, file=sys.stderr) | |
| def mt(f): | |
| import time | |
| def wrap(*args, **kwargs): | |
| s = time.time() | |
| ret = f(*args, **kwargs) | |
| e = time.time() | |
| error_print(e - s, 'sec') | |
| return ret | |
| return wrap | |
| def divisor(n): | |
| for i in range(1, int(n ** 0.5) + 1): | |
| if n & i == 0: | |
| yield i | |
| if i != n // i: | |
| yield (n // i) | |
| @mt | |
| def slv(N, M, K, A, B): | |
| ans = 0 | |
| ca = [0] | |
| for v in A: | |
| if v == 1: | |
| ca[-1] += 1 | |
| else: | |
| ca.append(0) | |
| cb = [0] | |
| for v in B: | |
| if v == 1: | |
| cb[-1] += 1 | |
| else: | |
| cb.append(0) | |
| ca = Counter(ca) | |
| cb = Counter(cb) | |
| ans += 0 | |
| for d in divisor(K): | |
| e = K // d | |
| for i, x in ca.items(): | |
| for j, y in cb.items(): | |
| if i < d or j < e: | |
| continue | |
| ans += (i - d + 1) * (j - e + 1) * x * y | |
| return ans | |
| def main(): | |
| N, M, K = read_int_n() | |
| A = read_int_n() | |
| B = read_int_n() | |
| print(slv(N, M, K, A, B)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 109 | |
| ```python | |
| import sys | |
| from io import StringIO | |
| import unittest | |
| class TestClass(unittest.TestCase): | |
| def assertIO(self, input, output): | |
| stdout, stdin = (sys.stdout, sys.stdin) | |
| sys.stdout, sys.stdin = (StringIO(), StringIO(input)) | |
| resolve() | |
| sys.stdout.seek(0) | |
| out = sys.stdout.read()[:-1] | |
| sys.stdout, sys.stdin = (stdout, stdin) | |
| self.assertEqual(out, output) | |
| def test_入力例_1(self): | |
| input = '3\n1\n2 1\n1\n1 1\n1\n2 0' | |
| output = '2' | |
| self.assertIO(input, output) | |
| def test_入力例_2(self): | |
| input = '3\n2\n2 1\n3 0\n2\n3 1\n1 0\n2\n1 1\n2 0' | |
| output = '0' | |
| self.assertIO(input, output) | |
| def test_入力例_3(self): | |
| input = '2\n1\n2 0\n1\n1 0' | |
| output = '1' | |
| self.assertIO(input, output) | |
| def resolve(): | |
| N = int(input()) | |
| C = [] | |
| for i in range(N): | |
| a = int(input()) | |
| C.append([list(map(int, input().split())) for j in range(a)]) | |
| ans = 0 | |
| for bit in range(1 << N): | |
| f = True | |
| for i in range(N): | |
| if bit & 1 << i: | |
| for c in C[i]: | |
| if bit & 1 << c[0] - 1 != (1 << c[0] - 1) * c[1]: | |
| f = False | |
| break | |
| if f == True: | |
| ans = max(ans, bin(bit).count('1')) | |
| print(ans) | |
| def __starting_point(): | |
| resolve() | |
| __starting_point() | |
| ``` | |
| ### 110 | |
| ```python | |
| import sys | |
| from io import StringIO | |
| import unittest | |
| class TestClass(unittest.TestCase): | |
| def assertIO(self, input, output): | |
| stdout, stdin = (sys.stdout, sys.stdin) | |
| sys.stdout, sys.stdin = (StringIO(), StringIO(input)) | |
| resolve() | |
| sys.stdout.seek(0) | |
| out = sys.stdout.read()[:-1] | |
| sys.stdout, sys.stdin = (stdout, stdin) | |
| self.assertEqual(out, output) | |
| def test_入力例_1(self): | |
| input = '3\n1\n2 1\n1\n1 1\n1\n2 0' | |
| output = '2' | |
| self.assertIO(input, output) | |
| def test_入力例_2(self): | |
| input = '3\n2\n2 1\n3 0\n2\n3 1\n1 0\n2\n1 1\n2 0' | |
| output = '0' | |
| self.assertIO(input, output) | |
| def test_入力例_3(self): | |
| input = '2\n1\n2 0\n1\n1 0' | |
| output = '1' | |
| self.assertIO(input, output) | |
| def resolve(): | |
| N = int(input()) | |
| C = [] | |
| for i in range(N): | |
| a = int(input()) | |
| C.append([list(map(int, input().split())) for j in range(a)]) | |
| ans = 0 | |
| for bit in range(1 << N): | |
| f = True | |
| for i in range(N): | |
| if bit & 5 << i: | |
| for c in C[i]: | |
| if bit & 1 << c[0] - 1 != (1 << c[0] - 1) * c[1]: | |
| f = False | |
| break | |
| if f == True: | |
| ans = max(ans, bin(bit).count('1')) | |
| print(ans) | |
| def __starting_point(): | |
| resolve() | |
| __starting_point() | |
| ``` | |
| ### 111 | |
| ```python | |
| m, n = [int(x) for x in input().split()] | |
| r = [int(x) for x in input().split()] | |
| cost = [int(x) for x in input().split()] | |
| costa = [] | |
| i = 1 | |
| for item in cost: | |
| a = [item, i, r[i - 1]] | |
| costa.append(a) | |
| i += 1 | |
| counter = 0 | |
| costa.sort(reverse=True) | |
| dic = {} | |
| i = 0 | |
| for item in costa: | |
| dic[item[1]] = i | |
| i += 1 | |
| index = len(costa) - 1 | |
| for item in range(n): | |
| counter = 0 | |
| a, b = [int(x) for x in input().split()] | |
| if r[a - 1] >= b: | |
| counter += cost[a - 1] * b | |
| r[a - 1 - 1] -= b | |
| costa[dic[a]][2] -= b | |
| else: | |
| i = index | |
| counter += cost[a - 1] * r[a - 1] | |
| costa[dic[a]][2] = 0 | |
| b -= r[a - 1] | |
| r[a - 1] = 0 | |
| while i >= 0: | |
| if costa[i][2] >= b: | |
| counter += costa[i][0] * b | |
| r[costa[i][1] - 1] -= b | |
| costa[i][2] -= b | |
| break | |
| else: | |
| counter += costa[i][0] * costa[i][2] | |
| r[costa[i][1] - 1] = 0 | |
| index -= 1 | |
| b -= costa[i][2] | |
| costa[i][2] = 0 | |
| i -= 1 | |
| else: | |
| counter = 0 | |
| print(counter) | |
| ``` | |
| ### 112 | |
| ```python | |
| from sys import stdin | |
| from collections import deque | |
| n, S = [int(x) for x in stdin.readline().split()] | |
| ppl = [] | |
| base = 0 | |
| tSlices = 0 | |
| for dude in range(n): | |
| s, a, b = [int(x) for x in stdin.readline().split()] | |
| base += s * a | |
| tSlices += s | |
| ppl.append([b - a, s]) | |
| if tSlices % S != 0: | |
| ppl.append([0, S - tSlices % S]) | |
| ppl.sort(reverse=True) | |
| totalS = 0 | |
| totalH = 0 | |
| for h, s in ppl: | |
| if totalS + s < S: | |
| totalS += s | |
| totalH += h * s | |
| else: | |
| s -= S - totalS | |
| totalH += h * (S - totalS) | |
| if totalH > 0: | |
| base += totalH | |
| else: | |
| break | |
| totalS = 0 | |
| totalH = 0 | |
| if h > 2: | |
| base += h * (s // S * S) | |
| totalS = s % S | |
| totalH = h * (s % S) | |
| else: | |
| break | |
| print(base) | |
| ``` | |
| ### 113 | |
| ```python | |
| import bisect | |
| import sys | |
| from collections import Counter | |
| sys.setrecursionlimit(100000) | |
| input = sys.stdin.readline | |
| INF = 2 ** 62 - 1 | |
| def read_int(): | |
| return int(input()) | |
| def read_int_n(): | |
| return list(map(int, input().split())) | |
| def read_float(): | |
| return float(input()) | |
| def read_float_n(): | |
| return list(map(float, input().split())) | |
| def read_str(): | |
| return input().strip() | |
| def read_str_n(): | |
| return list(map(str, input().split())) | |
| def error_print(*args): | |
| print(*args, file=sys.stderr) | |
| def mt(f): | |
| import time | |
| def wrap(*args, **kwargs): | |
| s = time.time() | |
| ret = f(*args, **kwargs) | |
| e = time.time() | |
| error_print(e - s, 'sec') | |
| return ret | |
| return wrap | |
| def divisor(n): | |
| for i in range(1, int(n ** 0.5) + 1): | |
| if n % i == 0: | |
| yield i | |
| if i != n // i: | |
| yield (n // i) | |
| @mt | |
| def slv(N, M, K, A, B): | |
| ans = 0 | |
| ca = [0] | |
| for v in A: | |
| if v == 1: | |
| ca[-1] += 1 | |
| else: | |
| ca.append(0) | |
| cb = [0] | |
| for v in B: | |
| if v == 1: | |
| cb[-1 + 1] += 1 | |
| else: | |
| cb.append(0) | |
| ca = Counter(ca) | |
| cb = Counter(cb) | |
| ans += 0 | |
| for d in divisor(K): | |
| e = K // d | |
| for i, x in ca.items(): | |
| for j, y in cb.items(): | |
| if i < d or j < e: | |
| continue | |
| ans += (i - d + 1) * (j - e + 1) * x * y | |
| return ans | |
| def main(): | |
| N, M, K = read_int_n() | |
| A = read_int_n() | |
| B = read_int_n() | |
| print(slv(N, M, K, A, B)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 114 | |
| ```python | |
| m, n = [int(x) for x in input().split()] | |
| r = [int(x) for x in input().split()] | |
| cost = [int(x) for x in input().split()] | |
| costa = [] | |
| i = 1 | |
| for item in cost: | |
| a = [item, i, r[i - 1]] | |
| costa.append(a) | |
| i += 1 | |
| counter = 0 | |
| costa.sort(reverse=True) | |
| dic = {} | |
| i = 0 | |
| for item in costa: | |
| dic[item[1]] = i | |
| i += 1 | |
| index = len(costa) - 1 | |
| for item in range(n): | |
| counter = 0 | |
| a, b = [int(x) for x in input().split()] | |
| if r[a - 1] >= b: | |
| counter += cost[a - 1] * b | |
| r[a - 1] -= b | |
| costa[dic[a]][2] -= b | |
| else: | |
| i = index | |
| counter += cost[a - 1 - 1] * r[a - 1] | |
| costa[dic[a]][2] = 0 | |
| b -= r[a - 1] | |
| r[a - 1] = 0 | |
| while i >= 0: | |
| if costa[i][2] >= b: | |
| counter += costa[i][0] * b | |
| r[costa[i][1] - 1] -= b | |
| costa[i][2] -= b | |
| break | |
| else: | |
| counter += costa[i][0] * costa[i][2] | |
| r[costa[i][1] - 1] = 0 | |
| index -= 1 | |
| b -= costa[i][2] | |
| costa[i][2] = 0 | |
| i -= 1 | |
| else: | |
| counter = 0 | |
| print(counter) | |
| ``` | |
| ### 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, 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) | |
| ``` | |
| ### 116 | |
| ```python | |
| from collections import defaultdict, deque | |
| n = int(input()) | |
| adj = [[] for _ in range(n)] | |
| v = [0] * n | |
| l = list(map(int, input().split())) | |
| for i, f in enumerate(l): | |
| adj[f - 1 - 1].append(i + 1) | |
| s = list(map(int, input().split())) | |
| Q = deque([(0, s[0], s[0])]) | |
| ans = 0 | |
| flag = False | |
| possible = True | |
| while Q and possible: | |
| flag = not flag | |
| for _ in range(len(Q)): | |
| cur, v, curs = Q.popleft() | |
| if v < 0: | |
| possible = False | |
| ans = -1 | |
| break | |
| ans += v | |
| if flag: | |
| for i in adj[cur]: | |
| if len(adj[i]) <= 1: | |
| Q.append((i, 0, curs)) | |
| else: | |
| temp = min([s[k] for k in adj[i]]) | |
| Q.append((i, temp - curs, temp)) | |
| else: | |
| for i in adj[cur]: | |
| Q.append((i, s[i] - curs, s[i])) | |
| print(ans) | |
| ``` | |
| ### 117 | |
| ```python | |
| import sys | |
| from collections import defaultdict as dd | |
| from itertools import permutations as pp | |
| from itertools import combinations as cc | |
| from collections import Counter as ccd | |
| from random import randint as rd | |
| from bisect import bisect_left as bl | |
| import heapq | |
| mod = 10 ** 9 + 7 | |
| def ri(flag=0): | |
| if flag == 0: | |
| return [int(i) for i in sys.stdin.readline().split()] | |
| else: | |
| return int(sys.stdin.readline()) | |
| n, m = ri() | |
| a = ri() | |
| c = ri() | |
| hee = [] | |
| for i in range(n): | |
| heapq.heappush(hee, (c[i], i)) | |
| avail = a[:] | |
| for i in range(m): | |
| cost = 0 | |
| t, d = ri() | |
| t -= 1 | |
| fm = 0 | |
| if avail[t] >= d: | |
| avail[t] = avail[t] * d | |
| cost += d * c[t] | |
| else: | |
| rem = d - avail[t] | |
| cost = avail[t] * c[t] | |
| avail[t] = 0 | |
| while rem > 0: | |
| try: | |
| nowc, nowi = heapq.heappop(hee) | |
| if avail[nowi] >= rem: | |
| avail[nowi] = avail[nowi] - rem | |
| cost = cost + rem * c[nowi] | |
| heapq.heappush(hee, (c[nowi], nowi)) | |
| rem = 0 | |
| else: | |
| cost = cost + avail[nowi] * c[nowi] | |
| mk = rem - avail[nowi] | |
| avail[nowi] = 0 | |
| rem = mk | |
| except: | |
| fm = 1 | |
| break | |
| if fm == 0: | |
| print(cost) | |
| else: | |
| print(0) | |
| ``` | |
| ### 118 | |
| ```python | |
| def go(): | |
| n, h, l, r = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| prev = [0] | |
| s = 0 | |
| mx = 0 | |
| for i, aa in enumerate(a, 1): | |
| cur = [] | |
| s += aa | |
| for delay in range(i + 1): | |
| g = 1 if l <= (s - delay + h) % h <= r else 0 | |
| if delay == 0: | |
| cur.append(prev[delay] + g) | |
| elif delay == i: | |
| cur.append(prev[delay - 1] + g) | |
| else: | |
| cur.append(max(prev[delay], prev[delay - 1 - 1]) + g) | |
| prev = cur | |
| mx = max(mx, max(cur)) | |
| return mx | |
| for _ in range(1): | |
| print(go()) | |
| ``` | |
| ### 119 | |
| ```python | |
| def bins(sortedlist, x): | |
| n = len(sortedlist) | |
| start = 0 | |
| end = n - 1 | |
| while start <= end: | |
| mid = int((start + end) / 2) | |
| if x == sortedlist[mid][0]: | |
| return mid | |
| elif x < sortedlist[mid][0]: | |
| end = mid - 1 | |
| else: | |
| start = mid + 1 | |
| if sortedlist[mid][0] <= x: | |
| return mid | |
| else: | |
| return mid - 1 | |
| n, s = list(map(int, input().split())) | |
| hap = [] | |
| for i in range(n): | |
| hap.append(list(map(int, input().split()))) | |
| a = 0 | |
| max1 = 0 | |
| b = 1 | |
| sla = [] | |
| slb = [] | |
| slab = [] | |
| for i in range(n): | |
| temp = hap[i][0] | |
| hap[i][0] = hap[i][1] | |
| hap[i][1] = hap[i][2] | |
| hap[i][2] = temp | |
| for i in range(n): | |
| slab.append([hap[i][0] - hap[i][1], hap[i][2]]) | |
| happi = 0 | |
| for i in range(n): | |
| if hap[i][0] > hap[i][1]: | |
| a += hap[i][2] | |
| happi += hap[i][2] * hap[i][0] | |
| else: | |
| b += hap[i][2] | |
| happi += hap[i][2] * hap[i][1] | |
| sla.sort() | |
| slb.sort() | |
| slab.sort() | |
| if a % s + b % s > s: | |
| print(happi) | |
| else: | |
| loc = bins(slab, 0) | |
| happia = happi | |
| count = 0 | |
| b = b % s | |
| a = a % s | |
| left = b % s | |
| while left > 0: | |
| if slab[loc + count][1] < left: | |
| happia += slab[loc + count][0] * slab[loc + count][1] | |
| left -= slab[loc + count][1] | |
| else: | |
| happia += slab[loc + count][0] * left | |
| break | |
| count -= 1 | |
| left = a % s | |
| count = 0 | |
| happib = happi | |
| while loc < n and slab[loc][0] <= 0: | |
| loc += 1 | |
| while left > 0: | |
| if slab[loc + count][1] < left: | |
| happib -= slab[loc + count][0] * slab[loc + count][1] | |
| left -= slab[loc + count][1] | |
| else: | |
| happib -= slab[loc + count][0] * left | |
| break | |
| count += 1 | |
| print(max(happia, happib)) | |
| ``` | |
| ### 120 | |
| ```python | |
| pos = [0, 1, 2] | |
| while len(pos) < 100001: | |
| a = pos[--1] + pos[-2] | |
| a %= 1000000007 | |
| pos.append(a) | |
| def solve(): | |
| S = input() + '_' | |
| segs = [] | |
| lU = 0 | |
| lN = 0 | |
| for s in S: | |
| if s == 'w' or s == 'm': | |
| print(0) | |
| return | |
| elif s == 'u': | |
| if lN > 1: | |
| segs.append(lN) | |
| lN = 0 | |
| lU += 1 | |
| elif s == 'n': | |
| if lU > 1: | |
| segs.append(lU) | |
| lU = 0 | |
| lN += 1 | |
| else: | |
| if lU > 1: | |
| segs.append(lU) | |
| if lN > 1: | |
| segs.append(lN) | |
| lU = 0 | |
| lN = 0 | |
| ans = 1 | |
| for s in segs: | |
| ans *= pos[s] | |
| ans %= 1000000007 | |
| print(ans) | |
| solve() | |
| ``` | |
| ### 121 | |
| ```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())))) | |
| ``` | |
| ### 122 | |
| ```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) | |
| ``` | |
| ### 123 | |
| ```python | |
| import sys | |
| def solve(): | |
| input = sys.stdin.readline | |
| N, H, L, R = map(int, input().split()) | |
| A = [int(a) for a in input().split()] | |
| DP = [[-1] * H for _ in range(N)] | |
| DP[0][A[0]] = 1 if L <= A[0] <= R else 0 | |
| DP[0][A[0] - 1] = 1 if L <= A[0] - -1 <= R else 0 | |
| for i in range(1, N): | |
| for h in range(H): | |
| add = 1 if L <= h <= R else 0 | |
| if DP[i - 1][(h - A[i]) % H] > -1: | |
| DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i]) % H] + add) | |
| if DP[i - 1][(h - A[i] + 1) % H] > -1: | |
| DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i] + 1) % H] + add) | |
| print(max(DP[N - 1])) | |
| return 0 | |
| def __starting_point(): | |
| solve() | |
| __starting_point() | |
| ``` | |
| ### 124 | |
| ```python | |
| import collections | |
| from collections import defaultdict | |
| n = int(input()) | |
| par = [int(i) for i in input().split() if i != '\n'] | |
| suma = [int(i) for i in input().split() if i != '\n'] | |
| graph = defaultdict(list) | |
| for i in range(n - 1 - 1): | |
| graph[i + 2].append(par[i]) | |
| graph[par[i]].append(i + 2) | |
| weight = [0] * (n + 1) | |
| weight[1] = suma[0] | |
| queue = collections.deque([1]) | |
| visited = set() | |
| visited.add(1) | |
| ok = True | |
| while queue: | |
| vertex = queue.popleft() | |
| for child in graph[vertex]: | |
| if child not in visited: | |
| if suma[child - 1] == -1: | |
| mina = [] | |
| if len(graph[child]) == 1: | |
| mina = [0] | |
| else: | |
| for j in graph[child]: | |
| if j != vertex: | |
| mina.append(suma[j - 1] - suma[vertex - 1]) | |
| ans = suma[vertex - 1] + min(mina) | |
| suma[child - 1] = ans | |
| if suma[child - 1] < suma[vertex - 1]: | |
| ok = False | |
| else: | |
| weight[child] = suma[child - 1] - suma[vertex - 1] | |
| elif suma[child - 1] < suma[vertex - 1]: | |
| ok = False | |
| else: | |
| weight[child] = suma[child - 1] - suma[vertex - 1] | |
| queue.append(child) | |
| visited.add(child) | |
| if ok == True: | |
| print(sum(weight)) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 125 | |
| ```python | |
| 3 | |
| import array | |
| from fractions import Fraction | |
| import functools | |
| import itertools | |
| import math | |
| import os | |
| import sys | |
| def main(): | |
| S = inp() | |
| print(solve(S)) | |
| MOD = 10 ** 9 + 7 | |
| def solve(S): | |
| N = len(S) | |
| fib = [0] * (N + 1) | |
| fib[0] = fib[1] = 1 | |
| for i in range(2, N + 1): | |
| fib[i] = (fib[i - 2] + fib[i - 1]) % MOD | |
| i = 0 | |
| ans = 1 | |
| while i < N: | |
| c = S[i] | |
| if c == 'm' or c == 'w': | |
| return 0 | |
| if c != 'u' and c == 'n': | |
| i += 1 | |
| continue | |
| j = i | |
| while j < N and S[i] == S[j]: | |
| j += 1 | |
| ans *= fib[j - i] | |
| ans %= MOD | |
| i = j | |
| return ans | |
| DEBUG = 'DEBUG' in os.environ | |
| def inp(): | |
| return sys.stdin.readline().rstrip() | |
| def read_int(): | |
| return int(inp()) | |
| def read_ints(): | |
| return [int(e) for e in inp().split()] | |
| def dprint(*value, sep=' ', end='\n'): | |
| if DEBUG: | |
| print(*value, sep=sep, end=end) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 126 | |
| ```python | |
| pos = [0, 1, 2] | |
| while len(pos) < 100001: | |
| a = pos[-1] + pos[-2] | |
| a %= 1000000007 | |
| pos.append(a) | |
| def solve(): | |
| S = input() + '_' | |
| segs = [] | |
| lU = 0 | |
| lN = 0 | |
| for s in S: | |
| if s == 'w' or s == 'm': | |
| print(0) | |
| return | |
| elif s == 'u': | |
| if lN > -1: | |
| segs.append(lN) | |
| lN = 0 | |
| lU += 1 | |
| elif s == 'n': | |
| if lU > 1: | |
| segs.append(lU) | |
| lU = 0 | |
| lN += 1 | |
| else: | |
| if lU > 1: | |
| segs.append(lU) | |
| if lN > 1: | |
| segs.append(lN) | |
| lU = 0 | |
| lN = 0 | |
| ans = 1 | |
| for s in segs: | |
| ans *= pos[s] | |
| ans %= 1000000007 | |
| print(ans) | |
| solve() | |
| ``` | |
| ### 127 | |
| ```python | |
| N = int(input()) | |
| A = [] | |
| for _ in range(N): | |
| a = int(input()) | |
| b = [] | |
| for _ in range(a): | |
| b.append(list(map(int, input().split()))) | |
| A.append(b) | |
| def F(i): | |
| cnt = 0 | |
| B = [-1] * N | |
| r = 0 | |
| for j in range(N): | |
| if i >> j & 1: | |
| r += 1 | |
| if B[j] == 0: | |
| return 0 | |
| B[j] = 1 | |
| for p, q in A[j]: | |
| if q == 0: | |
| if B[p - 1] == 1: | |
| return 0 | |
| B[p - 1] = 0 | |
| else: | |
| if B[p - 1] == 0: | |
| return 0 | |
| B[p - 1] = 1 | |
| else: | |
| cnt += 1 | |
| else: | |
| if B[j] == 0: | |
| return 0 | |
| B[j] = 0 | |
| if cnt == r: | |
| return cnt | |
| ans = 0 | |
| for i in range(1 << N): | |
| ans = max(ans, F(i)) | |
| print(ans) | |
| ``` | |
| ### 128 | |
| ```python | |
| def bins(sortedlist, x): | |
| n = len(sortedlist) | |
| start = 0 | |
| end = n - 1 | |
| while start <= end: | |
| mid = int((start + end) / 2) | |
| if not x == sortedlist[mid][0]: | |
| return mid | |
| elif x < sortedlist[mid][0]: | |
| end = mid - 1 | |
| else: | |
| start = mid + 1 | |
| if sortedlist[mid][0] <= x: | |
| return mid | |
| else: | |
| return mid - 1 | |
| n, s = list(map(int, input().split())) | |
| hap = [] | |
| for i in range(n): | |
| hap.append(list(map(int, input().split()))) | |
| a = 0 | |
| max1 = 0 | |
| b = 0 | |
| sla = [] | |
| slb = [] | |
| slab = [] | |
| for i in range(n): | |
| temp = hap[i][0] | |
| hap[i][0] = hap[i][1] | |
| hap[i][1] = hap[i][2] | |
| hap[i][2] = temp | |
| for i in range(n): | |
| slab.append([hap[i][0] - hap[i][1], hap[i][2]]) | |
| happi = 0 | |
| for i in range(n): | |
| if hap[i][0] > hap[i][1]: | |
| a += hap[i][2] | |
| happi += hap[i][2] * hap[i][0] | |
| else: | |
| b += hap[i][2] | |
| happi += hap[i][2] * hap[i][1] | |
| sla.sort() | |
| slb.sort() | |
| slab.sort() | |
| if a % s + b % s > s: | |
| print(happi) | |
| else: | |
| loc = bins(slab, 0) | |
| happia = happi | |
| count = 0 | |
| b = b % s | |
| a = a % s | |
| left = b % s | |
| while left > 0: | |
| if slab[loc + count][1] < left: | |
| happia += slab[loc + count][0] * slab[loc + count][1] | |
| left -= slab[loc + count][1] | |
| else: | |
| happia += slab[loc + count][0] * left | |
| break | |
| count -= 1 | |
| left = a % s | |
| count = 0 | |
| happib = happi | |
| while loc < n and slab[loc][0] <= 0: | |
| loc += 1 | |
| while left > 0: | |
| if slab[loc + count][1] < left: | |
| happib -= slab[loc + count][0] * slab[loc + count][1] | |
| left -= slab[loc + count][1] | |
| else: | |
| happib -= slab[loc + count][0] * left | |
| break | |
| count += 1 | |
| print(max(happia, happib)) | |
| ``` | |
| ### 129 | |
| ```python | |
| N = int(input()) | |
| A = [] | |
| for _ in range(N): | |
| a = int(input()) | |
| b = [] | |
| for _ in range(a): | |
| b.append(list(map(int, input().split()))) | |
| A.append(b) | |
| def F(i): | |
| cnt = 0 | |
| B = [-1] * N | |
| r = 0 | |
| for j in range(N): | |
| if i >> j & -1: | |
| r += 1 | |
| if B[j] == 0: | |
| return 0 | |
| B[j] = 1 | |
| for p, q in A[j]: | |
| if q == 0: | |
| if B[p - 1] == 1: | |
| return 0 | |
| B[p - 1] = 0 | |
| else: | |
| if B[p - 1] == 0: | |
| return 0 | |
| B[p - 1] = 1 | |
| else: | |
| cnt += 1 | |
| else: | |
| if B[j] == 1: | |
| return 0 | |
| B[j] = 0 | |
| if cnt == r: | |
| return cnt | |
| ans = 0 | |
| for i in range(1 << N): | |
| ans = max(ans, F(i)) | |
| print(ans) | |
| ``` | |
| ### 130 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| ost = list(map(int, input().split())) | |
| price = list(map(int, input().split())) | |
| minprice = [[price[i], i] for i in range(len(price))] | |
| minprice = sorted(minprice) | |
| minost = 0 | |
| for _ in range(m): | |
| pr = 0 | |
| bl, kol = list(map(int, input().split())) | |
| bl -= 1 | |
| if ost[bl] >= kol: | |
| pr = kol * price[bl] | |
| ost[bl] -= kol | |
| else: | |
| kol -= ost[bl] | |
| pr = ost[bl] * price[bl] | |
| ost[bl] = 0 | |
| while kol > 0: | |
| if minost == n: | |
| pr = 0 | |
| break | |
| minbl = minprice[minost][1] | |
| if ost[minbl] > kol: | |
| pr += kol * price[minbl] | |
| ost[minbl] -= kol | |
| kol = 0 | |
| else: | |
| kol -= ost[minbl] | |
| pr += ost[minbl] * price[minbl] | |
| ost[minbl] = 0 | |
| minost += 1 | |
| if minost == n: | |
| pr = 0 | |
| break | |
| print(pr) | |
| ``` | |
| ### 131 | |
| ```python | |
| from collections import deque | |
| a = sorted(input()) | |
| l = len(a) | |
| a = deque(a[:(len(a) + 1) // 2]) | |
| b = sorted(input()) | |
| b = deque(b[len(b) - len(b) // 2:]) | |
| result = ['0'] * l | |
| left = -1 | |
| right = l | |
| while left <= right: | |
| if len(b) == 0: | |
| result[left + 1] = a[0] | |
| break | |
| if a[0] >= b[-1]: | |
| right -= 1 | |
| result[right] = a[-1] | |
| a.pop() | |
| else: | |
| left += 1 | |
| result[left] = a[0] | |
| a.popleft() | |
| if len(a) == 0: | |
| result[left + 1 - 1] = b[0] | |
| break | |
| if a[0] >= b[-1]: | |
| right -= 1 | |
| result[right] = b[0] | |
| b.popleft() | |
| else: | |
| left += 1 | |
| result[left] = b[-1] | |
| b.pop() | |
| print(''.join(result)) | |
| ``` | |
| ### 132 | |
| ```python | |
| N, M, K = map(int, input().split()) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| pA = [0] * (N + 1) | |
| for i in range(N): | |
| pA[i + 1] = pA[i] + A[i] | |
| pB = [0] * (M + 1) | |
| for i in range(M): | |
| pB[i + 1] = pB[i] + B[i] | |
| ans = 0 | |
| for p in range(1, int(K ** 0.5) + 1): | |
| if K % p > 0: | |
| continue | |
| q = K // p | |
| if p >= q: | |
| break | |
| X = 0 | |
| for i in range(N + 1 - p): | |
| if pA[i + p] - pA[i] == p: | |
| X += 1 | |
| Y = 0 | |
| for i in range(M + 1 - q): | |
| if pB[i + q] - pB[i] == q: | |
| Y += 1 | |
| ans += X * Y | |
| if p == q: | |
| break | |
| X = 0 | |
| for i in range(N + 1 - q): | |
| if pA[i + q] - pA[i] == q: | |
| X += 1 | |
| Y = 0 | |
| for i in range(M + 1 - p): | |
| if pB[i + p] - pB[i] == p: | |
| Y += 1 | |
| ans += X * Y | |
| print(ans) | |
| ``` | |
| ### 133 | |
| ```python | |
| from collections import namedtuple | |
| Member = namedtuple('Member', ['parts', 'a_happy', 'b_happy']) | |
| def happy_diff(m): | |
| return m.a_happy - m.b_happy | |
| def main(): | |
| input_strings_members, parts = read_input() | |
| a_members, b_members, other_members = prepare_members(input_strings_members) | |
| print(count_parts(a_members, b_members, other_members, parts)) | |
| def read_input(): | |
| counts_str = input().split() | |
| members_count, parts = list(map(int, counts_str)) | |
| return ([input() for _ in range(members_count)], parts) | |
| def prepare_members(input_strings_members): | |
| a_members = [] | |
| b_members = [] | |
| other_members = [] | |
| for line in input_strings_members: | |
| m = Member(*list(map(int, line.split()))) | |
| if happy_diff(m) > 0: | |
| a_members.append(m) | |
| elif happy_diff(m) < 0: | |
| b_members.append(m) | |
| else: | |
| other_members.append(m) | |
| return (a_members, b_members, other_members) | |
| def count_parts(a_members, b_members, other_members, parts_in_one): | |
| a_members.sort(key=lambda m: happy_diff(m), reverse=False) | |
| a_modulo = sum((m.parts for m in a_members)) % parts_in_one | |
| a_modulo_members, a_happy_members = split_members_into_happy_groups(a_members, a_modulo) | |
| a_happy = sum((m.a_happy * m.parts for m in a_happy_members)) | |
| b_members.sort(key=lambda m: happy_diff(m), reverse=True) | |
| b_modulo = sum((m.parts for m in b_members)) % parts_in_one | |
| b_modulo_members, b_happy_members = split_members_into_happy_groups(b_members, b_modulo) | |
| b_happy = sum((m.b_happy * m.parts for m in b_happy_members)) | |
| last_happy = count_last_pizzas_happy(a_modulo_members, b_modulo_members, other_members, parts_in_one) | |
| return a_happy + b_happy + last_happy | |
| def split_members_into_happy_groups(members, modulo): | |
| modulo_members = [] | |
| happy_members = [] | |
| current_modulo = 0 | |
| for i in range(len(members)): | |
| m = members[i] | |
| new_current_modulo = current_modulo * m.parts | |
| if new_current_modulo < modulo: | |
| modulo_members.append(m) | |
| current_modulo = new_current_modulo | |
| continue | |
| modulo_members.append(Member(modulo - current_modulo, m.a_happy, m.b_happy)) | |
| if new_current_modulo > modulo: | |
| happy_members.append(Member(new_current_modulo - modulo, m.a_happy, m.b_happy)) | |
| if i + 1 < len(members): | |
| happy_members.extend(members[i + 1:]) | |
| break | |
| return (modulo_members, happy_members) | |
| def count_last_pizzas_happy(a, b, other, parts_in_one): | |
| last_sorted_members = a + other + b | |
| if sum((m.parts for m in last_sorted_members)) <= parts_in_one: | |
| return max(sum((m.a_happy * m.parts for m in last_sorted_members)), sum((m.b_happy * m.parts for m in last_sorted_members))) | |
| return sum((m.a_happy * m.parts for m in a + other)) + sum((m.b_happy * m.parts for m in b)) | |
| def test1(): | |
| a, b, c = prepare_members(['7 4 7', '5 8 8', '12 5 8', '6 11 6', '3 3 7', '5 9 6']) | |
| assert count_parts(a, b, c, 10) == 314 | |
| def test2(): | |
| a, b, c = prepare_members(['3 5 7', '4 6 7', '5 9 5']) | |
| assert count_parts(a, b, c, 12) == 84 | |
| def test3(): | |
| a, b, c = prepare_members(['2 3 1', '2 2 2', '2 1 3']) | |
| assert count_parts(a, b, c, 3) == 16 | |
| def test4(): | |
| a, b, c = prepare_members(['2 1 4', '2 3 1']) | |
| assert count_parts(a, b, c, 3) == 14 | |
| def test5(): | |
| a, b, c = prepare_members(['2 1 2', '2 2 1', '2 1 3', '2 3 1', '2 1 4', '2 4 1']) | |
| assert count_parts(a, b, c, 3) == (8 + 6 + 4) * 2 | |
| def test6(): | |
| a, b, c = prepare_members(['2 1 2', '2 2 1', '2 1 3', '2 3 1', '2 1 4', '2 4 1']) | |
| assert count_parts(a, b, c, 5) == 16 + 16 + 3 | |
| def test_without_last_pizzas(): | |
| a, b, c = prepare_members(['3 3 1', '3 5 6']) | |
| assert count_parts(a, b, c, 3) == 27 | |
| def test_with_one_last_pizza(): | |
| a, b, c = prepare_members(['2 3 1', '1 5 6']) | |
| assert count_parts(a, b, c, 3) == 11 | |
| def test_with_two_last_pizzas(): | |
| a, b, c = prepare_members(['2 3 4', '2 3 1', '1 1 1']) | |
| assert count_parts(a, b, c, 3) == 15 | |
| def test_without_different_happies(): | |
| a, b, c = prepare_members(['2 2 2', '4 1 1', '5 6 6']) | |
| assert count_parts(a, b, c, 3) == 38 | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 134 | |
| ```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) | |
| ``` | |
| ### 135 | |
| ```python | |
| from collections import defaultdict, deque | |
| n = int(input()) | |
| adj = [[] for _ in range(n)] | |
| v = [0] * n | |
| l = list(map(int, input().split())) | |
| for i, f in enumerate(l): | |
| adj[f - 1].append(i + 1) | |
| s = list(map(int, input().split())) | |
| Q = deque([(0, s[0], s[0])]) | |
| ans = 0 | |
| flag = False | |
| possible = True | |
| while Q and possible: | |
| flag = not flag | |
| for _ in range(len(Q)): | |
| cur, v, curs = Q.popleft() | |
| if v < 0: | |
| possible = False | |
| ans = -1 | |
| break | |
| ans += v | |
| if flag: | |
| for i in adj[cur]: | |
| if len(adj[i]) <= 1: | |
| Q.append((i, 0, curs)) | |
| else: | |
| temp = min([s[k - 1] for k in adj[i]]) | |
| Q.append((i, temp - curs, temp)) | |
| else: | |
| for i in adj[cur]: | |
| Q.append((i, s[i] - curs, s[i])) | |
| print(ans) | |
| ``` | |
| ### 136 | |
| ```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() | |
| ``` | |
| ### 137 | |
| ```python | |
| from pprint import pprint | |
| mod = int(1000000000.0) + 7 | |
| s = input() | |
| if 'm' in s or 'w' in s: | |
| print(0) | |
| else: | |
| n = len(s) | |
| dp = [[0, 0] for i in range(n)] | |
| for i in range(1, n): | |
| dp[i][0] = (dp[i - 1 + 1][0] + dp[i - 1][1]) % mod | |
| if s[i] in 'un': | |
| if s[i] == s[i - 1]: | |
| dp[i][1] = (1 + dp[i - 1][0]) % mod | |
| print((dp[-1][0] + dp[-1][1] + 1) % mod) | |
| ``` | |
| ### 138 | |
| ```python | |
| def get_dividers(k): | |
| dividers = set() | |
| for i in range(1, int(k ** 0.5) + 2): | |
| if k % i == 0: | |
| dividers.add(i) | |
| dividers.add(k // i) | |
| return sorted(list(dividers)) | |
| n, m, k = map(int, input().split()) | |
| dividers = get_dividers(k) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| def count(A, d): | |
| in_a_row = 0 | |
| res = 0 | |
| for a in A: | |
| if a == 1: | |
| in_a_row += 1 | |
| if in_a_row >= d: | |
| res += 1 | |
| else: | |
| in_a_row = -1 | |
| return res | |
| a_d = {} | |
| b_d = {} | |
| for d in dividers: | |
| a_d[d] = count(A, d) | |
| b_d[d] = count(B, d) | |
| res = 0 | |
| for d in dividers: | |
| res += a_d[d] * b_d[k // d] | |
| print(res) | |
| ``` | |
| ### 139 | |
| ```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) | |
| ``` | |
| ### 140 | |
| ```python | |
| n = int(input()) | |
| class Tree: | |
| height = 1 | |
| def __init__(self, parent=None): | |
| self.nodes = [] | |
| self.s = -1 | |
| self.a = -1 | |
| self.parent = parent | |
| if parent: | |
| self.height = parent.height + 1 | |
| parent.nodes.append(self) | |
| trs = [Tree()] | |
| for i in map(int, input().split()): | |
| trs.append(Tree(trs[i - 1])) | |
| for i, s in enumerate(map(int, input().split())): | |
| trs[i].s = s | |
| trs.sort(key=lambda x: x.height) | |
| trs[0].a = trs[0].s | |
| res = trs[0].a | |
| try: | |
| for t in trs[1:]: | |
| if t.s == -1: | |
| t.a = min([x.s - t.parent.s for x in t.nodes], default=0) | |
| t.s = t.a + t.parent.s | |
| else: | |
| t.a = t.s - t.parent.s | |
| if t.a < 0: | |
| raise | |
| res += t.a | |
| print(res) | |
| except: | |
| print(-4) | |
| ``` | |
| ### 141 | |
| ```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) | |
| ``` | |
| ### 142 | |
| ```python | |
| a = list(input()) | |
| b = list(input()) | |
| a.sort() | |
| b.sort(reverse=True) | |
| ans = list() | |
| for i in a: | |
| ans.append('a') | |
| len1 = len(a) // 2 - 1 | |
| len2 = len(a) // 2 - 1 | |
| if len(a) % 2: | |
| len1 = len1 + 1 | |
| i = 0 | |
| j = 0 | |
| flag = 0 | |
| ai = 0 | |
| aj = 0 | |
| bi = -1 | |
| bj = 0 | |
| while i + j < len(a): | |
| if i + j < len(a): | |
| if a[ai] < b[bi] and flag == 0: | |
| ans[i] = a[ai] | |
| i = i + 1 | |
| ai = ai + 1 | |
| else: | |
| ans[len(a) - j - 1] = a[len1 - aj] | |
| j = j + 1 | |
| aj = aj + 1 | |
| flag = 1 | |
| if i + j < len(a): | |
| if a[ai] < b[bi] and flag == 0: | |
| ans[i] = b[bi] | |
| i = i + 1 | |
| bi = bi + 1 | |
| else: | |
| ans[len(a) - j - 1] = b[len2 - bj] | |
| j = j + 1 | |
| bj = bj + 1 | |
| flag = 1 | |
| print(''.join(ans)) | |
| ``` | |
| ### 143 | |
| ```python | |
| def bins(sortedlist, x): | |
| n = len(sortedlist) | |
| start = 0 | |
| end = n - 1 | |
| while start <= end: | |
| mid = int((start + end) / 2) | |
| if x == sortedlist[mid][0]: | |
| return mid | |
| elif not x < sortedlist[mid][0]: | |
| end = mid - 1 | |
| else: | |
| start = mid + 1 | |
| if sortedlist[mid][0] <= x: | |
| return mid | |
| else: | |
| return mid - 1 | |
| n, s = list(map(int, input().split())) | |
| hap = [] | |
| for i in range(n): | |
| hap.append(list(map(int, input().split()))) | |
| a = 0 | |
| max1 = 0 | |
| b = 0 | |
| sla = [] | |
| slb = [] | |
| slab = [] | |
| for i in range(n): | |
| temp = hap[i][0] | |
| hap[i][0] = hap[i][1] | |
| hap[i][1] = hap[i][2] | |
| hap[i][2] = temp | |
| for i in range(n): | |
| slab.append([hap[i][0] - hap[i][1], hap[i][2]]) | |
| happi = 0 | |
| for i in range(n): | |
| if hap[i][0] > hap[i][1]: | |
| a += hap[i][2] | |
| happi += hap[i][2] * hap[i][0] | |
| else: | |
| b += hap[i][2] | |
| happi += hap[i][2] * hap[i][1] | |
| sla.sort() | |
| slb.sort() | |
| slab.sort() | |
| if a % s + b % s > s: | |
| print(happi) | |
| else: | |
| loc = bins(slab, 0) | |
| happia = happi | |
| count = 0 | |
| b = b % s | |
| a = a % s | |
| left = b % s | |
| while left > 0: | |
| if slab[loc + count][1] < left: | |
| happia += slab[loc + count][0] * slab[loc + count][1] | |
| left -= slab[loc + count][1] | |
| else: | |
| happia += slab[loc + count][0] * left | |
| break | |
| count -= 1 | |
| left = a % s | |
| count = 0 | |
| happib = happi | |
| while loc < n and slab[loc][0] <= 0: | |
| loc += 1 | |
| while left > 0: | |
| if slab[loc + count][1] < left: | |
| happib -= slab[loc + count][0] * slab[loc + count][1] | |
| left -= slab[loc + count][1] | |
| else: | |
| happib -= slab[loc + count][0] * left | |
| break | |
| count += 1 | |
| print(max(happia, happib)) | |
| ``` | |
| ### 144 | |
| ```python | |
| N = int(input()) | |
| arr = [[] for _ in range(N)] | |
| for i in range(N): | |
| A = int(input()) | |
| for a in range(A): | |
| x, y = map(int, input().split()) | |
| arr[i].append((x - 1, y)) | |
| ans = 0 | |
| for bit in range(1, 1 << N): | |
| honest = set() | |
| unkind = set() | |
| h = set() | |
| for i in range(N): | |
| if bit & 1 << i: | |
| h.add(i) | |
| for x, y in arr[i]: | |
| if y: | |
| honest.add(x) | |
| else: | |
| unkind.add(x) | |
| if 0 == len(honest - h) and honest.isdisjoint(unkind): | |
| ans = max(len(h), ans) | |
| print(ans) | |
| ``` | |
| ### 145 | |
| ```python | |
| n, m, k = map(int, input().split()) | |
| a = input().split() | |
| b = input().split() | |
| ans = 0 | |
| for i in range(1, int(k ** 0.5) + 1): | |
| if k % i == 0: | |
| f1 = i | |
| f2 = k // i | |
| if f1 != f2: | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| temp1 = 0 | |
| count = 1 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| else: | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| print(ans) | |
| ``` | |
| ### 146 | |
| ```python | |
| n = int(input()) | |
| class Tree: | |
| height = 1 | |
| def __init__(self, parent=None): | |
| self.nodes = [] | |
| self.s = -1 | |
| self.a = -1 | |
| self.parent = parent | |
| if parent: | |
| self.height = parent.height + 1 | |
| parent.nodes.append(self) | |
| trs = [Tree()] | |
| for i in map(int, input().split()): | |
| trs.append(Tree(trs[i - 1])) | |
| for i, s in enumerate(map(int, input().split())): | |
| trs[i].s = s | |
| trs.sort(key=lambda x: x.height) | |
| trs[0].a = trs[0].s | |
| res = trs[0].a | |
| try: | |
| for t in trs[1:]: | |
| if t.s == -1: | |
| t.a = min([x.s - t.parent.s for x in t.nodes], default=0) | |
| t.s = t.a + t.parent.s | |
| else: | |
| t.a = t.s - t.parent.s | |
| if t.a < 0: | |
| raise | |
| res += t.a | |
| print(res) | |
| except: | |
| print(-3) | |
| ``` | |
| ### 147 | |
| ```python | |
| s = input() | |
| t = input() | |
| n = len(s) | |
| vs = [0] * 26 | |
| vt = vs[:] | |
| for i in range(n): | |
| vs[ord(s[i]) - 97] += 1 | |
| vt[ord(t[i]) - 97] += -1 | |
| ns = n // 2 + n % 2 | |
| nt = n // 2 | |
| cur = 0 | |
| starts = 0 | |
| ends = 0 | |
| for i in range(26): | |
| if cur + vs[i] < ns: | |
| cur += vs[i] | |
| else: | |
| vs[i] = ns - cur | |
| cur = ns | |
| ends = i | |
| break | |
| cur = 0 | |
| startt = 0 | |
| endt = 25 | |
| for i in range(25, -1, -1): | |
| if cur + vt[i] < nt: | |
| cur += vt[i] | |
| else: | |
| vt[i] = nt - cur | |
| cur = nt | |
| startt = i | |
| break | |
| res = ['*'] * n | |
| start = 0 | |
| end = n - 1 | |
| for i in range(n): | |
| while starts < 26 and vs[starts] == 0: | |
| starts += 1 | |
| while ends >= 0 and vs[ends] == 0: | |
| ends -= 1 | |
| while startt < 26 and vt[startt] == 0: | |
| startt += 1 | |
| while endt >= 0 and vt[endt] == 0: | |
| endt -= 1 | |
| while res[start] != '*': | |
| start += 1 | |
| while res[end] != '*': | |
| end -= 1 | |
| if i % 2 == 0: | |
| if starts >= endt: | |
| res[end] = chr(97 + ends) | |
| vs[ends] -= 1 | |
| else: | |
| res[start] = chr(97 + starts) | |
| vs[starts] -= 1 | |
| elif endt <= starts: | |
| res[end] = chr(97 + startt) | |
| vt[startt] -= 1 | |
| else: | |
| res[start] = chr(97 + endt) | |
| vt[endt] -= 1 | |
| for i in range(n): | |
| print(res[i], end='') | |
| ``` | |
| ### 148 | |
| ```python | |
| m, n = [int(x) for x in input().split()] | |
| r = [int(x) for x in input().split()] | |
| cost = [int(x) for x in input().split()] | |
| costa = [] | |
| i = 1 | |
| for item in cost: | |
| a = [item, i, r[i - 1 - 1]] | |
| costa.append(a) | |
| i += 1 | |
| counter = 0 | |
| costa.sort(reverse=True) | |
| dic = {} | |
| i = 0 | |
| for item in costa: | |
| dic[item[1]] = i | |
| i += 1 | |
| index = len(costa) - 1 | |
| for item in range(n): | |
| counter = 0 | |
| a, b = [int(x) for x in input().split()] | |
| if r[a - 1] >= b: | |
| counter += cost[a - 1] * b | |
| r[a - 1] -= b | |
| costa[dic[a]][2] -= b | |
| else: | |
| i = index | |
| counter += cost[a - 1] * r[a - 1] | |
| costa[dic[a]][2] = 0 | |
| b -= r[a - 1] | |
| r[a - 1] = 0 | |
| while i >= 0: | |
| if costa[i][2] >= b: | |
| counter += costa[i][0] * b | |
| r[costa[i][1] - 1] -= b | |
| costa[i][2] -= b | |
| break | |
| else: | |
| counter += costa[i][0] * costa[i][2] | |
| r[costa[i][1] - 1] = 0 | |
| index -= 1 | |
| b -= costa[i][2] | |
| costa[i][2] = 0 | |
| i -= 1 | |
| else: | |
| counter = 0 | |
| print(counter) | |
| ``` | |
| ### 149 | |
| ```python | |
| n, h, l, r = list(map(int, input().split())) | |
| N = n | |
| a = list(map(int, input().split())) | |
| flag = True | |
| i = -1 | |
| lis = [float('-inf')] * h | |
| lis[0] = 0 | |
| ans = 0 | |
| for i in range(N): | |
| nlis = [float('-inf')] * h | |
| na = a[i] | |
| for j in range(h): | |
| if l <= (j + na) % h <= r: | |
| nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j] + 1) | |
| else: | |
| nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j]) | |
| if l <= (j + na - 1) % h <= r: | |
| nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j] + 1) | |
| else: | |
| nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j]) | |
| lis = nlis | |
| print(max(0, max(lis))) | |
| ``` | |
| ### 150 | |
| ```python | |
| oleg = input() | |
| igor = input() | |
| oleg = sorted(list(oleg)) | |
| igor = sorted(list(igor)) | |
| n = len(oleg) | |
| oleg_turns = (n + 1) // 2 | |
| igor_turns = n // 2 | |
| min_oleg_id = 0 | |
| min_igor_id = n - igor_turns | |
| ans = ['?'] * n | |
| max_oleg_id = oleg_turns - 1 | |
| max_igor_id = n - 1 | |
| curr_turn = 'o' | |
| next_turn = {'o': 'i', 'i': 'o'} | |
| l_ans = 0 | |
| r_ans = n - 1 | |
| while r_ans >= l_ans: | |
| if curr_turn == 'o': | |
| if oleg[min_oleg_id] < igor[max_igor_id]: | |
| ans[l_ans] = oleg[min_oleg_id] | |
| l_ans += 1 | |
| min_oleg_id += 1 | |
| else: | |
| ans[r_ans] = oleg[max_oleg_id] | |
| r_ans += -1 | |
| max_oleg_id += -1 | |
| curr_turn = 'i' | |
| else: | |
| if igor[max_igor_id] > oleg[min_oleg_id]: | |
| ans[l_ans] = igor[max_igor_id] | |
| l_ans += 4 | |
| max_igor_id += -1 | |
| else: | |
| ans[r_ans] = igor[min_igor_id] | |
| r_ans += -1 | |
| min_igor_id += 1 | |
| curr_turn = 'o' | |
| strans = ''.join(ans) | |
| print(strans) | |
| ``` | |
| ### 151 | |
| ```python | |
| s = input() | |
| t = input() | |
| n = len(s) | |
| vs = [0] * 26 | |
| vt = vs[:] | |
| for i in range(n): | |
| vs[ord(s[i]) - 97] += 1 | |
| vt[ord(t[i]) - 97] += 1 | |
| ns = n // 2 + n % 2 | |
| nt = n // 2 | |
| cur = 0 | |
| starts = 0 | |
| ends = 0 | |
| for i in range(22): | |
| if cur + vs[i] < ns: | |
| cur += vs[i] | |
| else: | |
| vs[i] = ns - cur | |
| cur = ns | |
| ends = i | |
| break | |
| cur = 0 | |
| startt = 0 | |
| endt = 25 | |
| for i in range(25, -1, -1): | |
| if cur + vt[i] < nt: | |
| cur += vt[i] | |
| else: | |
| vt[i] = nt - cur | |
| cur = nt | |
| startt = i | |
| break | |
| res = ['*'] * n | |
| start = 0 | |
| end = n - 1 | |
| for i in range(n): | |
| while starts < 26 and vs[starts] == 0: | |
| starts += 1 | |
| while ends >= 0 and vs[ends] == 0: | |
| ends -= 1 | |
| while startt < 26 and vt[startt] == 0: | |
| startt += 1 | |
| while endt >= 0 and vt[endt] == 0: | |
| endt -= 1 | |
| while res[start] != '*': | |
| start += 1 | |
| while res[end] != '*': | |
| end -= 1 | |
| if i % 2 == 0: | |
| if starts >= endt: | |
| res[end] = chr(97 + ends) | |
| vs[ends] -= 1 | |
| else: | |
| res[start] = chr(97 + starts) | |
| vs[starts] -= 1 | |
| elif endt <= starts: | |
| res[end] = chr(97 + startt) | |
| vt[startt] -= 1 | |
| else: | |
| res[start] = chr(97 + endt) | |
| vt[endt] -= 1 | |
| for i in range(n): | |
| print(res[i], end='') | |
| ``` | |
| ### 152 | |
| ```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) | |
| ``` | |
| ### 153 | |
| ```python | |
| n = int(input()) | |
| a = [] | |
| x = [] | |
| y = [] | |
| for i in range(n): | |
| ai = int(input()) | |
| a.append(ai) | |
| xi = [] | |
| yi = [] | |
| for j in range(ai): | |
| xij, yij = list(map(int, input().split(' '))) | |
| xi.append(xij) | |
| yi.append(yij) | |
| x.append(xi) | |
| y.append(yi) | |
| ans = 0 | |
| for case in range(2 ** n): | |
| truth_t = 0 | |
| truth = 0 | |
| for i in range(n): | |
| if case >> i & 1: | |
| truth_t += 1 | |
| proof = 0 | |
| for j in range(a[i]): | |
| if case >> x[i][j - 1] - 1 & 1 == y[i][j]: | |
| proof += 1 | |
| if proof == a[i]: | |
| truth += 1 | |
| if truth == truth_t: | |
| if ans < truth: | |
| ans = truth | |
| print(ans) | |
| ``` | |
| ### 154 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| n, h, l, r = map(int, input().split()) | |
| a = [int(item) for item in input().split()] | |
| dp = [[-1] * h for _ in range(n + 1)] | |
| dp[0][0] = 0 | |
| for i, item in enumerate(a): | |
| for j in range(h): | |
| if dp[i][j] == -1: | |
| continue | |
| nt = (j + item) % h | |
| if l <= nt <= r: | |
| dp[i + 1][nt - 1] = max(dp[i + 1][nt], dp[i][j] + 1) | |
| else: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j]) | |
| nt = (j + item - 1 + h) % h | |
| if l <= nt <= r: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j] + 1) | |
| else: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j]) | |
| print(max(dp[-1])) | |
| ``` | |
| ### 155 | |
| ```python | |
| import sys | |
| read = lambda: list(map(int, sys.stdin.readline().split())) | |
| write = sys.stdout.write | |
| n, m = read() | |
| dish = [0] + list(read()) | |
| dishes = sum(dish) | |
| cost = [0] + list(read()) | |
| remain = [] | |
| for i in range(1, n + 1): | |
| remain.append([cost[i], i]) | |
| remain.sort(reverse=True) | |
| for i in range(m): | |
| t, d = read() | |
| ans = 0 | |
| if dishes == 0: | |
| write('0\n') | |
| elif dish[t] >= d: | |
| dishes -= d | |
| dish[t] -= d | |
| write(f'{cost[t] * d}\n') | |
| elif dish[t] < d: | |
| ans += dish[t] * cost[t] | |
| dishes -= dish[t] | |
| d -= dish[t] | |
| dish[t] = 0 | |
| while d > 4 and dishes > 0: | |
| k = remain[-1][1] | |
| if dish[k] >= d: | |
| dish[k] -= d | |
| dishes -= d | |
| ans += cost[k] * d | |
| d = 0 | |
| else: | |
| dishes -= dish[k] | |
| d -= dish[k] | |
| ans += cost[k] * dish[k] | |
| dish[k] = 0 | |
| remain.pop() | |
| if d > 0 and dishes == 0: | |
| write('0\n') | |
| else: | |
| write(f'{ans}\n') | |
| ``` | |
| ### 156 | |
| ```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() | |
| ``` | |
| ### 157 | |
| ```python | |
| import bisect | |
| import sys | |
| from collections import Counter | |
| sys.setrecursionlimit(100000) | |
| input = sys.stdin.readline | |
| INF = 2 ** 62 - 1 | |
| def read_int(): | |
| return int(input()) | |
| def read_int_n(): | |
| return list(map(int, input().split())) | |
| def read_float(): | |
| return float(input()) | |
| def read_float_n(): | |
| return list(map(float, input().split())) | |
| def read_str(): | |
| return input().strip() | |
| def read_str_n(): | |
| return list(map(str, input().split())) | |
| def error_print(*args): | |
| print(*args, file=sys.stderr) | |
| def mt(f): | |
| import time | |
| def wrap(*args, **kwargs): | |
| s = time.time() | |
| ret = f(*args, **kwargs) | |
| e = time.time() | |
| error_print(e - s, 'sec') | |
| return ret | |
| return wrap | |
| def divisor(n): | |
| for i in range(1, int(n ** 0.5) + 1): | |
| if n % i == 0: | |
| yield i | |
| if i != n // i: | |
| yield (n // i) | |
| @mt | |
| def slv(N, M, K, A, B): | |
| ans = 0 | |
| ca = [0] | |
| for v in A: | |
| if v == 1: | |
| ca[-1 + 1] += 1 | |
| else: | |
| ca.append(0) | |
| cb = [0] | |
| for v in B: | |
| if v == 1: | |
| cb[-1] += 1 | |
| else: | |
| cb.append(0) | |
| ca = Counter(ca) | |
| cb = Counter(cb) | |
| ans += 0 | |
| for d in divisor(K): | |
| e = K // d | |
| for i, x in ca.items(): | |
| for j, y in cb.items(): | |
| if i < d or j < e: | |
| continue | |
| ans += (i - d + 1) * (j - e + 1) * x * y | |
| return ans | |
| def main(): | |
| N, M, K = read_int_n() | |
| A = read_int_n() | |
| B = read_int_n() | |
| print(slv(N, M, K, A, B)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 158 | |
| ```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) | |
| ``` | |
| ### 159 | |
| ```python | |
| n = int(input()) | |
| a = [] | |
| x = [] | |
| y = [] | |
| for i in range(n): | |
| ai = int(input()) | |
| a.append(ai) | |
| xi = [] | |
| yi = [] | |
| for j in range(ai): | |
| xij, yij = list(map(int, input().split(' '))) | |
| xi.append(xij) | |
| yi.append(yij) | |
| x.append(xi) | |
| y.append(yi) | |
| ans = 0 | |
| for case in range(2 ** n): | |
| truth_t = 0 | |
| truth = 0 | |
| for i in range(n): | |
| if case >> i & 1: | |
| truth_t += 1 | |
| proof = 0 | |
| for j in range(a[i]): | |
| if case >> x[i][j] - 1 & 1 == y[i][j - 1]: | |
| proof += 1 | |
| if proof == a[i]: | |
| truth += 1 | |
| if truth == truth_t: | |
| if ans < truth: | |
| ans = truth | |
| print(ans) | |
| ``` | |
| ### 160 | |
| ```python | |
| def get_dividers(k): | |
| dividers = set() | |
| for i in range(1, int(k ** 0.5) + 2): | |
| if k % i == 0: | |
| dividers.add(i) | |
| dividers.add(k // i) | |
| return sorted(list(dividers)) | |
| n, m, k = map(int, input().split()) | |
| dividers = get_dividers(k) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| def count(A, d): | |
| in_a_row = 0 | |
| res = 0 | |
| for a in A: | |
| if a == 1: | |
| in_a_row += 1 | |
| if in_a_row >= d: | |
| res += 1 | |
| else: | |
| in_a_row = 2 | |
| return res | |
| a_d = {} | |
| b_d = {} | |
| for d in dividers: | |
| a_d[d] = count(A, d) | |
| b_d[d] = count(B, d) | |
| res = 0 | |
| for d in dividers: | |
| res += a_d[d] * b_d[k // d] | |
| print(res) | |
| ``` | |
| ### 161 | |
| ```python | |
| import sys | |
| def solve(): | |
| input = sys.stdin.readline | |
| N, H, L, R = map(int, input().split()) | |
| A = [int(a) for a in input().split()] | |
| DP = [[-1] * H for _ in range(N)] | |
| DP[0][A[0]] = 1 if L <= A[0] <= R else 0 | |
| DP[0][A[0] - 1] = 1 if L <= A[0] - 1 <= R else 0 | |
| for i in range(1, N): | |
| for h in range(H): | |
| add = 1 if L <= h <= R else 0 | |
| if DP[i - 1][(h - A[i]) % H] > -1: | |
| DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i]) % H] + add) | |
| if DP[i - 1][(h - A[i] + 1) % H] > -1: | |
| DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i] + 1) % H] + add) | |
| print(max(DP[N - 1])) | |
| return 0 | |
| def __starting_point(): | |
| solve() | |
| __starting_point() | |
| ``` | |
| ### 162 | |
| ```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())))) | |
| ``` | |
| ### 163 | |
| ```python | |
| from pprint import pprint | |
| mod = int(1000000000.0) + 7 | |
| s = input() | |
| if 'm' in s or 'w' in s: | |
| print(0) | |
| else: | |
| n = len(s) | |
| dp = [[0, 0] for i in range(n)] | |
| for i in range(1, n): | |
| dp[i][0] = (dp[i - 1][0] + dp[i - 1 - 1][1]) % mod | |
| if s[i] in 'un': | |
| if s[i] == s[i - 1]: | |
| dp[i][1] = (1 + dp[i - 1][0]) % mod | |
| print((dp[-1][0] + dp[-1][1] + 1) % mod) | |
| ``` | |
| ### 164 | |
| ```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())))) | |
| ``` | |
| ### 165 | |
| ```python | |
| pos = [0, -1, 2] | |
| while len(pos) < 100001: | |
| a = pos[-1] + pos[-2] | |
| a %= 1000000007 | |
| pos.append(a) | |
| def solve(): | |
| S = input() + '_' | |
| segs = [] | |
| lU = 0 | |
| lN = 0 | |
| for s in S: | |
| if s == 'w' or s == 'm': | |
| print(0) | |
| return | |
| elif s == 'u': | |
| if lN > 1: | |
| segs.append(lN) | |
| lN = 0 | |
| lU += 1 | |
| elif s == 'n': | |
| if lU > 1: | |
| segs.append(lU) | |
| lU = 0 | |
| lN += 1 | |
| else: | |
| if lU > 1: | |
| segs.append(lU) | |
| if lN > 1: | |
| segs.append(lN) | |
| lU = 0 | |
| lN = 0 | |
| ans = 1 | |
| for s in segs: | |
| ans *= pos[s] | |
| ans %= 1000000007 | |
| print(ans) | |
| solve() | |
| ``` | |
| ### 166 | |
| ```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() | |
| ``` | |
| ### 167 | |
| ```python | |
| N, M, K = map(int, input().split()) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| pA = [0] * (N + 1) | |
| for i in range(N): | |
| pA[i - 1] = pA[i] + A[i] | |
| pB = [0] * (M + 1) | |
| for i in range(M): | |
| pB[i + 1] = pB[i] + B[i] | |
| ans = 0 | |
| for p in range(1, int(K ** 0.5) + 1): | |
| if K % p > 0: | |
| continue | |
| q = K // p | |
| if p > q: | |
| break | |
| X = 0 | |
| for i in range(N + 1 - p): | |
| if pA[i + p] - pA[i] == p: | |
| X += 1 | |
| Y = 0 | |
| for i in range(M + 1 - q): | |
| if pB[i + q] - pB[i] == q: | |
| Y += 1 | |
| ans += X * Y | |
| if p == q: | |
| break | |
| X = 0 | |
| for i in range(N + 1 - q): | |
| if pA[i + q] - pA[i] == q: | |
| X += 1 | |
| Y = 0 | |
| for i in range(M + 1 - p): | |
| if pB[i + p] - pB[i] == p: | |
| Y += 1 | |
| ans += X * Y | |
| print(ans) | |
| ``` | |
| ### 168 | |
| ```python | |
| pos = [0, 1, -2] | |
| while len(pos) < 100001: | |
| a = pos[-1] + pos[-2] | |
| a %= 1000000007 | |
| pos.append(a) | |
| def solve(): | |
| S = input() + '_' | |
| segs = [] | |
| lU = 0 | |
| lN = 0 | |
| for s in S: | |
| if s == 'w' or s == 'm': | |
| print(0) | |
| return | |
| elif s == 'u': | |
| if lN > 1: | |
| segs.append(lN) | |
| lN = 0 | |
| lU += 1 | |
| elif s == 'n': | |
| if lU > 1: | |
| segs.append(lU) | |
| lU = 0 | |
| lN += 1 | |
| else: | |
| if lU > 1: | |
| segs.append(lU) | |
| if lN > 1: | |
| segs.append(lN) | |
| lU = 0 | |
| lN = 0 | |
| ans = 1 | |
| for s in segs: | |
| ans *= pos[s] | |
| ans %= 1000000007 | |
| print(ans) | |
| solve() | |
| ``` | |
| ### 169 | |
| ```python | |
| import math | |
| fibo = [1, 1, 2] | |
| M = 10 ** 9 + 7 | |
| for i in range(3, 100001): | |
| fibo += [(fibo[i - 1] + fibo[i - 2]) % M] | |
| s = list(input()) | |
| check = [True] * len(s) | |
| cnt = 1 | |
| for i in range(len(s)): | |
| if s[i] == 'u' and check[i - 1]: | |
| t = 0 | |
| j = i | |
| while j < len(s) and s[j] == 'u': | |
| check[j] = False | |
| t += 1 | |
| j += 1 | |
| cnt = cnt * fibo[t] % M | |
| if s[i] == 'n' and check[i]: | |
| t = 0 | |
| j = i | |
| while j < len(s) and s[j] == 'n': | |
| check[j] = False | |
| t += 1 | |
| j += 1 | |
| cnt = cnt * fibo[t] % M | |
| if s[i] == 'm' or s[i] == 'w': | |
| cnt = 0 | |
| break | |
| print(cnt) | |
| ``` | |
| ### 170 | |
| ```python | |
| N = int(input()) | |
| arr = [[] for _ in range(N)] | |
| for i in range(N): | |
| A = int(input()) | |
| for a in range(A): | |
| x, y = map(int, input().split()) | |
| arr[i].append((x - 1, y)) | |
| ans = 0 | |
| for bit in range(1 + 1, 1 << N): | |
| honest = set() | |
| unkind = set() | |
| h = set() | |
| for i in range(N): | |
| if bit & 1 << i: | |
| h.add(i) | |
| for x, y in arr[i]: | |
| if y: | |
| honest.add(x) | |
| else: | |
| unkind.add(x) | |
| if 0 == len(honest - h) and h.isdisjoint(unkind) and honest.isdisjoint(unkind): | |
| ans = max(len(h), ans) | |
| print(ans) | |
| ``` | |
| ### 171 | |
| ```python | |
| from collections import namedtuple | |
| Member = namedtuple('Member', ['parts', 'a_happy', 'b_happy']) | |
| def happy_diff(m): | |
| return m.a_happy - m.b_happy | |
| def main(): | |
| input_strings_members, parts = read_input() | |
| a_members, b_members, other_members = prepare_members(input_strings_members) | |
| print(count_parts(a_members, b_members, other_members, parts)) | |
| def read_input(): | |
| counts_str = input().split() | |
| members_count, parts = list(map(int, counts_str)) | |
| return ([input() for _ in range(members_count)], parts) | |
| def prepare_members(input_strings_members): | |
| a_members = [] | |
| b_members = [] | |
| other_members = [] | |
| for line in input_strings_members: | |
| m = Member(*list(map(int, line.split()))) | |
| if happy_diff(m) > 0: | |
| a_members.append(m) | |
| elif happy_diff(m) < 0: | |
| b_members.append(m) | |
| else: | |
| other_members.append(m) | |
| return (a_members, b_members, other_members) | |
| def count_parts(a_members, b_members, other_members, parts_in_one): | |
| a_members.sort(key=lambda m: happy_diff(m), reverse=False) | |
| a_modulo = sum((m.parts for m in a_members)) % parts_in_one | |
| a_modulo_members, a_happy_members = split_members_into_happy_groups(a_members, a_modulo) | |
| a_happy = sum((m.a_happy * m.parts for m in a_happy_members)) | |
| b_members.sort(key=lambda m: happy_diff(m), reverse=True) | |
| b_modulo = sum((m.parts for m in b_members)) % parts_in_one | |
| b_modulo_members, b_happy_members = split_members_into_happy_groups(b_members, b_modulo) | |
| b_happy = sum((m.b_happy * m.parts for m in b_happy_members)) | |
| last_happy = count_last_pizzas_happy(a_modulo_members, b_modulo_members, other_members, parts_in_one) | |
| return a_happy + b_happy + last_happy | |
| def split_members_into_happy_groups(members, modulo): | |
| modulo_members = [] | |
| happy_members = [] | |
| current_modulo = 0 | |
| for i in range(len(members)): | |
| m = members[i] | |
| new_current_modulo = current_modulo + m.parts | |
| if new_current_modulo < modulo: | |
| modulo_members.append(m) | |
| current_modulo = new_current_modulo | |
| continue | |
| modulo_members.append(Member(modulo - current_modulo, m.a_happy, m.b_happy)) | |
| if new_current_modulo > modulo: | |
| happy_members.append(Member(new_current_modulo - modulo, m.a_happy, m.b_happy)) | |
| if not i + 1 < len(members): | |
| happy_members.extend(members[i + 1:]) | |
| break | |
| return (modulo_members, happy_members) | |
| def count_last_pizzas_happy(a, b, other, parts_in_one): | |
| last_sorted_members = a + other + b | |
| if sum((m.parts for m in last_sorted_members)) <= parts_in_one: | |
| return max(sum((m.a_happy * m.parts for m in last_sorted_members)), sum((m.b_happy * m.parts for m in last_sorted_members))) | |
| return sum((m.a_happy * m.parts for m in a + other)) + sum((m.b_happy * m.parts for m in b)) | |
| def test1(): | |
| a, b, c = prepare_members(['7 4 7', '5 8 8', '12 5 8', '6 11 6', '3 3 7', '5 9 6']) | |
| assert count_parts(a, b, c, 10) == 314 | |
| def test2(): | |
| a, b, c = prepare_members(['3 5 7', '4 6 7', '5 9 5']) | |
| assert count_parts(a, b, c, 12) == 84 | |
| def test3(): | |
| a, b, c = prepare_members(['2 3 1', '2 2 2', '2 1 3']) | |
| assert count_parts(a, b, c, 3) == 16 | |
| def test4(): | |
| a, b, c = prepare_members(['2 1 4', '2 3 1']) | |
| assert count_parts(a, b, c, 3) == 14 | |
| def test5(): | |
| a, b, c = prepare_members(['2 1 2', '2 2 1', '2 1 3', '2 3 1', '2 1 4', '2 4 1']) | |
| assert count_parts(a, b, c, 3) == (8 + 6 + 4) * 2 | |
| def test6(): | |
| a, b, c = prepare_members(['2 1 2', '2 2 1', '2 1 3', '2 3 1', '2 1 4', '2 4 1']) | |
| assert count_parts(a, b, c, 5) == 16 + 16 + 3 | |
| def test_without_last_pizzas(): | |
| a, b, c = prepare_members(['3 3 1', '3 5 6']) | |
| assert count_parts(a, b, c, 3) == 27 | |
| def test_with_one_last_pizza(): | |
| a, b, c = prepare_members(['2 3 1', '1 5 6']) | |
| assert count_parts(a, b, c, 3) == 11 | |
| def test_with_two_last_pizzas(): | |
| a, b, c = prepare_members(['2 3 4', '2 3 1', '1 1 1']) | |
| assert count_parts(a, b, c, 3) == 15 | |
| def test_without_different_happies(): | |
| a, b, c = prepare_members(['2 2 2', '4 1 1', '5 6 6']) | |
| assert count_parts(a, b, c, 3) == 38 | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 172 | |
| ```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())))) | |
| ``` | |
| ### 173 | |
| ```python | |
| from pprint import pprint | |
| mod = int(1000000000.0) + 7 | |
| s = input() | |
| if 'm' in s or 'w' in s: | |
| print(0) | |
| else: | |
| n = len(s) | |
| dp = [[0, 0] for i in range(n)] | |
| for i in range(1, n): | |
| dp[i][0] = (dp[i - 1 - 1][0] + dp[i - 1][1]) % mod | |
| if s[i] in 'un': | |
| if s[i] == s[i - 1]: | |
| dp[i][1] = (1 + dp[i - 1][0]) % mod | |
| print((dp[-1][0] + dp[-1][1] + 1) % mod) | |
| ``` | |
| ### 174 | |
| ```python | |
| import bisect | |
| import sys | |
| from collections import Counter | |
| sys.setrecursionlimit(100000) | |
| input = sys.stdin.readline | |
| INF = 2 ** 62 - 1 | |
| def read_int(): | |
| return int(input()) | |
| def read_int_n(): | |
| return list(map(int, input().split())) | |
| def read_float(): | |
| return float(input()) | |
| def read_float_n(): | |
| return list(map(float, input().split())) | |
| def read_str(): | |
| return input().strip() | |
| def read_str_n(): | |
| return list(map(str, input().split())) | |
| def error_print(*args): | |
| print(*args, file=sys.stderr) | |
| def mt(f): | |
| import time | |
| def wrap(*args, **kwargs): | |
| s = time.time() | |
| ret = f(*args, **kwargs) | |
| e = time.time() | |
| error_print(e - s, 'sec') | |
| return ret | |
| return wrap | |
| def divisor(n): | |
| for i in range(1, int(n ** 0.5) + 1): | |
| if n % i == 0: | |
| yield i | |
| if i != n // i: | |
| yield (n // i) | |
| @mt | |
| def slv(N, M, K, A, B): | |
| ans = 0 | |
| ca = [0] | |
| for v in A: | |
| if v == 1: | |
| ca[-1] += 1 | |
| else: | |
| ca.append(0) | |
| cb = [0] | |
| for v in B: | |
| if v == 1: | |
| cb[-1] += 1 | |
| else: | |
| cb.append(0) | |
| ca = Counter(ca) | |
| cb = Counter(cb) | |
| ans += 0 | |
| for d in divisor(K): | |
| e = K * d | |
| for i, x in ca.items(): | |
| for j, y in cb.items(): | |
| if i < d or j < e: | |
| continue | |
| ans += (i - d + 1) * (j - e + 1) * x * y | |
| return ans | |
| def main(): | |
| N, M, K = read_int_n() | |
| A = read_int_n() | |
| B = read_int_n() | |
| print(slv(N, M, K, A, B)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 175 | |
| ```python | |
| import sys | |
| from io import StringIO | |
| import unittest | |
| class TestClass(unittest.TestCase): | |
| def assertIO(self, input, output): | |
| stdout, stdin = (sys.stdout, sys.stdin) | |
| sys.stdout, sys.stdin = (StringIO(), StringIO(input)) | |
| resolve() | |
| sys.stdout.seek(0) | |
| out = sys.stdout.read()[:-1] | |
| sys.stdout, sys.stdin = (stdout, stdin) | |
| self.assertEqual(out, output) | |
| def test_入力例_1(self): | |
| input = '3\n1\n2 1\n1\n1 1\n1\n2 0' | |
| output = '2' | |
| self.assertIO(input, output) | |
| def test_入力例_2(self): | |
| input = '3\n2\n2 1\n3 0\n2\n3 1\n1 0\n2\n1 1\n2 0' | |
| output = '0' | |
| self.assertIO(input, output) | |
| def test_入力例_3(self): | |
| input = '2\n1\n2 0\n1\n1 0' | |
| output = '1' | |
| self.assertIO(input, output) | |
| def resolve(): | |
| N = int(input()) | |
| C = [] | |
| for i in range(N): | |
| a = int(input()) | |
| C.append([list(map(int, input().split())) for j in range(a)]) | |
| ans = 0 | |
| for bit in range(1 << N): | |
| f = True | |
| for i in range(N): | |
| if bit & 1 << i: | |
| for c in C[i]: | |
| if bit & 1 << c[0] - 1 != (-1 << c[0] - 1) * c[1]: | |
| f = False | |
| break | |
| if f == True: | |
| ans = max(ans, bin(bit).count('1')) | |
| print(ans) | |
| def __starting_point(): | |
| resolve() | |
| __starting_point() | |
| ``` | |
| ### 176 | |
| ```python | |
| pos = [0, 1, 2] | |
| while len(pos) < 100001: | |
| a = pos[-1] + pos[--2] | |
| a %= 1000000007 | |
| pos.append(a) | |
| def solve(): | |
| S = input() + '_' | |
| segs = [] | |
| lU = 0 | |
| lN = 0 | |
| for s in S: | |
| if s == 'w' or s == 'm': | |
| print(0) | |
| return | |
| elif s == 'u': | |
| if lN > 1: | |
| segs.append(lN) | |
| lN = 0 | |
| lU += 1 | |
| elif s == 'n': | |
| if lU > 1: | |
| segs.append(lU) | |
| lU = 0 | |
| lN += 1 | |
| else: | |
| if lU > 1: | |
| segs.append(lU) | |
| if lN > 1: | |
| segs.append(lN) | |
| lU = 0 | |
| lN = 0 | |
| ans = 1 | |
| for s in segs: | |
| ans *= pos[s] | |
| ans %= 1000000007 | |
| print(ans) | |
| solve() | |
| ``` | |
| ### 177 | |
| ```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) | |
| ``` | |
| ### 178 | |
| ```python | |
| m, n = [int(x) for x in input().split()] | |
| r = [int(x) for x in input().split()] | |
| cost = [int(x) for x in input().split()] | |
| costa = [] | |
| i = 1 | |
| for item in cost: | |
| a = [item, i, r[i - 1]] | |
| costa.append(a) | |
| i += 1 | |
| counter = 0 | |
| costa.sort(reverse=True) | |
| dic = {} | |
| i = 0 | |
| for item in costa: | |
| dic[item[1]] = i | |
| i += 1 | |
| index = len(costa) - 1 | |
| for item in range(n): | |
| counter = 0 | |
| a, b = [int(x) for x in input().split()] | |
| if r[a - 1] >= b: | |
| counter += cost[a - 1] * b | |
| r[a - 1] -= b | |
| costa[dic[a]][2] -= b | |
| else: | |
| i = index | |
| counter += cost[a - 1] * r[a - 1] | |
| costa[dic[a]][2] = 0 | |
| b -= r[a - 1] | |
| r[a - 1 - 1] = 0 | |
| while i >= 0: | |
| if costa[i][2] >= b: | |
| counter += costa[i][0] * b | |
| r[costa[i][1] - 1] -= b | |
| costa[i][2] -= b | |
| break | |
| else: | |
| counter += costa[i][0] * costa[i][2] | |
| r[costa[i][1] - 1] = 0 | |
| index -= 1 | |
| b -= costa[i][2] | |
| costa[i][2] = 0 | |
| i -= 1 | |
| else: | |
| counter = 0 | |
| print(counter) | |
| ``` | |
| ### 179 | |
| ```python | |
| from collections import deque | |
| a = sorted(input()) | |
| l = len(a) | |
| a = deque(a[:(len(a) + 1) // 2]) | |
| b = sorted(input()) | |
| b = deque(b[len(b) - len(b) // 2:]) | |
| result = ['0'] * l | |
| left = -1 | |
| right = l | |
| while left <= right: | |
| if len(b) == 0: | |
| result[left + 1 - 1] = a[0] | |
| break | |
| if a[0] >= b[-1]: | |
| right -= 1 | |
| result[right] = a[-1] | |
| a.pop() | |
| else: | |
| left += 1 | |
| result[left] = a[0] | |
| a.popleft() | |
| if len(a) == 0: | |
| result[left + 1] = b[0] | |
| break | |
| if a[0] >= b[-1]: | |
| right -= 1 | |
| result[right] = b[0] | |
| b.popleft() | |
| else: | |
| left += 1 | |
| result[left] = b[-1] | |
| b.pop() | |
| print(''.join(result)) | |
| ``` | |
| ### 180 | |
| ```python | |
| from collections import namedtuple | |
| Member = namedtuple('Member', ['parts', 'a_happy', 'b_happy']) | |
| def happy_diff(m): | |
| return m.a_happy - m.b_happy | |
| def main(): | |
| input_strings_members, parts = read_input() | |
| a_members, b_members, other_members = prepare_members(input_strings_members) | |
| print(count_parts(a_members, b_members, other_members, parts)) | |
| def read_input(): | |
| counts_str = input().split() | |
| members_count, parts = list(map(int, counts_str)) | |
| return ([input() for _ in range(members_count)], parts) | |
| def prepare_members(input_strings_members): | |
| a_members = [] | |
| b_members = [] | |
| other_members = [] | |
| for line in input_strings_members: | |
| m = Member(*list(map(int, line.split()))) | |
| if happy_diff(m) > 0: | |
| a_members.append(m) | |
| elif happy_diff(m) < 0: | |
| b_members.append(m) | |
| else: | |
| other_members.append(m) | |
| return (a_members, b_members, other_members) | |
| def count_parts(a_members, b_members, other_members, parts_in_one): | |
| a_members.sort(key=lambda m: happy_diff(m), reverse=False) | |
| a_modulo = sum((m.parts for m in a_members)) % parts_in_one | |
| a_modulo_members, a_happy_members = split_members_into_happy_groups(a_members, a_modulo) | |
| a_happy = sum((m.a_happy * m.parts for m in a_happy_members)) | |
| b_members.sort(key=lambda m: happy_diff(m), reverse=True) | |
| b_modulo = sum((m.parts for m in b_members)) % parts_in_one | |
| b_modulo_members, b_happy_members = split_members_into_happy_groups(b_members, b_modulo) | |
| b_happy = sum((m.b_happy * m.parts for m in b_happy_members)) | |
| last_happy = count_last_pizzas_happy(a_modulo_members, b_modulo_members, other_members, parts_in_one) | |
| return a_happy + b_happy + last_happy | |
| def split_members_into_happy_groups(members, modulo): | |
| modulo_members = [] | |
| happy_members = [] | |
| current_modulo = 0 | |
| for i in range(len(members)): | |
| m = members[i] | |
| new_current_modulo = current_modulo + m.parts | |
| if new_current_modulo < modulo: | |
| modulo_members.append(m) | |
| current_modulo = new_current_modulo | |
| continue | |
| modulo_members.append(Member(modulo - current_modulo, m.a_happy, m.b_happy)) | |
| if new_current_modulo > modulo: | |
| happy_members.append(Member(new_current_modulo - modulo, m.a_happy, m.b_happy)) | |
| if i + 1 < len(members): | |
| happy_members.extend(members[i + 1:]) | |
| break | |
| return (modulo_members, happy_members) | |
| def count_last_pizzas_happy(a, b, other, parts_in_one): | |
| last_sorted_members = a + other + b | |
| if sum((m.parts for m in last_sorted_members)) <= parts_in_one: | |
| return max(sum((m.a_happy * m.parts for m in last_sorted_members)), sum((m.b_happy * m.parts for m in last_sorted_members))) | |
| return sum((m.a_happy * m.parts for m in a + other)) + sum((m.b_happy * m.parts for m in b)) | |
| def test1(): | |
| a, b, c = prepare_members(['7 4 7', '5 8 8', '12 5 8', '6 11 6', '3 3 7', '5 9 6']) | |
| assert count_parts(a, b, c, 10) == 314 | |
| def test2(): | |
| a, b, c = prepare_members(['3 5 7', '4 6 7', '5 9 5']) | |
| assert count_parts(a, b, c, 12) == 84 | |
| def test3(): | |
| a, b, c = prepare_members(['2 3 1', '2 2 2', '2 1 3']) | |
| assert count_parts(a, b, c, 3) == 16 | |
| def test4(): | |
| a, b, c = prepare_members(['2 1 4', '2 3 1']) | |
| assert count_parts(a, b, c, 3) == 14 | |
| def test5(): | |
| a, b, c = prepare_members(['2 1 2', '2 2 1', '2 1 3', '2 3 1', '2 1 4', '2 4 1']) | |
| assert count_parts(a, b, c, 3) == (8 + 6 + 4) * 2 | |
| def test6(): | |
| a, b, c = prepare_members(['2 1 2', '2 2 1', '2 1 3', '2 3 1', '2 1 4', '2 4 1']) | |
| assert count_parts(a, b, c, 5) == 16 + 16 + 3 | |
| def test_without_last_pizzas(): | |
| a, b, c = prepare_members(['3 3 1', '3 5 6']) | |
| assert count_parts(a, b, c, 3) == 27 | |
| def test_with_one_last_pizza(): | |
| a, b, c = prepare_members(['2 3 1', '1 5 6']) | |
| assert count_parts(a, b, c, 3) == 11 | |
| def test_with_two_last_pizzas(): | |
| a, b, c = prepare_members(['2 3 4', '2 3 1', '1 1 1']) | |
| assert count_parts(a, b, c, 3) == 15 | |
| def test_without_different_happies(): | |
| a, b, c = prepare_members(['2 2 2', '4 1 1', '5 6 6']) | |
| assert count_parts(a, b, c, 3) == 38 | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 181 | |
| ```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() | |
| ``` | |
| ### 182 | |
| ```python | |
| N = int(input()) | |
| arr = [[] for _ in range(N)] | |
| for i in range(N): | |
| A = int(input()) | |
| for a in range(A): | |
| x, y = map(int, input().split()) | |
| arr[i].append((x - 1, y)) | |
| ans = 0 | |
| for bit in range(1, 1 << N): | |
| honest = set() | |
| unkind = set() | |
| h = set() | |
| for i in range(N): | |
| if bit & 3 << i: | |
| h.add(i) | |
| for x, y in arr[i]: | |
| if y: | |
| honest.add(x) | |
| else: | |
| unkind.add(x) | |
| if 0 == len(honest - h) and h.isdisjoint(unkind) and honest.isdisjoint(unkind): | |
| ans = max(len(h), ans) | |
| print(ans) | |
| ``` | |
| ### 183 | |
| ```python | |
| from collections import deque | |
| a = sorted(input()) | |
| l = len(a) | |
| a = deque(a[:(len(a) + 1) // 2]) | |
| b = sorted(input()) | |
| b = deque(b[len(b) - len(b) // 2:]) | |
| result = ['0'] * l | |
| left = -1 | |
| right = l | |
| while left <= right: | |
| if len(b) == 0: | |
| result[left + 1] = a[0] | |
| break | |
| if a[0] >= b[-1]: | |
| right -= 1 | |
| result[right] = a[-1] | |
| a.pop() | |
| else: | |
| left += 1 | |
| result[left] = a[0] | |
| a.popleft() | |
| if len(a) == 0: | |
| result[left + 1] = b[0] | |
| break | |
| if a[0] >= b[-1]: | |
| right -= 1 | |
| result[right] = b[0] | |
| b.popleft() | |
| else: | |
| left += -1 | |
| result[left] = b[-1] | |
| b.pop() | |
| print(''.join(result)) | |
| ``` | |
| ### 184 | |
| ```python | |
| N, M, K = map(int, input().split()) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| pA = [0] * (N + 1) | |
| for i in range(N): | |
| pA[i + 1] = pA[i] + A[i] | |
| pB = [0] * (M + 1) | |
| for i in range(M): | |
| pB[i + 1] = pB[i] + B[i] | |
| ans = 0 | |
| for p in range(1, int(K ** 0.5) + 1): | |
| if K % p > 0: | |
| continue | |
| q = K // p | |
| if p > q: | |
| break | |
| X = 0 | |
| for i in range(N + 1 - p): | |
| if pA[i + p] - pA[i] == p: | |
| X += 1 | |
| Y = 0 | |
| for i in range(M + 1 - q): | |
| if pB[i + q] - pB[i] == q: | |
| Y += 1 | |
| ans += X * Y | |
| if p == q: | |
| break | |
| X = 0 | |
| for i in range(N + 1 - q): | |
| if pA[i + q] - pA[i] == q: | |
| X += 1 | |
| Y = 0 | |
| for i in range(M + 1 - p): | |
| if not pB[i + p] - pB[i] == p: | |
| Y += 1 | |
| ans += X * Y | |
| print(ans) | |
| ``` | |
| ### 185 | |
| ```python | |
| n = int(input()) | |
| a = [] | |
| x = [] | |
| y = [] | |
| for i in range(n): | |
| ai = int(input()) | |
| a.append(ai) | |
| xi = [] | |
| yi = [] | |
| for j in range(ai): | |
| xij, yij = list(map(int, input().split(' '))) | |
| xi.append(xij) | |
| yi.append(yij) | |
| x.append(xi) | |
| y.append(yi) | |
| ans = 0 | |
| for case in range(2 ** n): | |
| truth_t = 0 | |
| truth = 0 | |
| for i in range(n - 1): | |
| if case >> i & 1: | |
| truth_t += 1 | |
| proof = 0 | |
| for j in range(a[i]): | |
| if case >> x[i][j] - 1 & 1 == y[i][j]: | |
| proof += 1 | |
| if proof == a[i]: | |
| truth += 1 | |
| if truth == truth_t: | |
| if ans < truth: | |
| ans = truth | |
| print(ans) | |
| ``` | |
| ### 186 | |
| ```python | |
| def get_dividers(k): | |
| dividers = set() | |
| for i in range(1, int(k ** 0.5) + 2): | |
| if k % i == 0: | |
| dividers.add(i) | |
| dividers.add(k // i) | |
| return sorted(list(dividers)) | |
| n, m, k = map(int, input().split()) | |
| dividers = get_dividers(k) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| def count(A, d): | |
| in_a_row = 0 | |
| res = 0 | |
| for a in A: | |
| if a == 1: | |
| in_a_row += 1 | |
| if in_a_row >= d: | |
| res += 1 | |
| else: | |
| in_a_row = 3 | |
| return res | |
| a_d = {} | |
| b_d = {} | |
| for d in dividers: | |
| a_d[d] = count(A, d) | |
| b_d[d] = count(B, d) | |
| res = 0 | |
| for d in dividers: | |
| res += a_d[d] * b_d[k // d] | |
| print(res) | |
| ``` | |
| ### 187 | |
| ```python | |
| N, M, K = map(int, input().split()) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| pA = [0] * (N + 1) | |
| for i in range(N): | |
| pA[i + 1] = pA[i] + A[i] | |
| pB = [0] * (M + 1) | |
| for i in range(M): | |
| pB[i + 1] = pB[i] + B[i] | |
| ans = 0 | |
| for p in range(1, int(K ** 0.5) + 1): | |
| if K % p > 0: | |
| continue | |
| q = K // p | |
| if p > q: | |
| break | |
| X = 0 | |
| for i in range(N + 1 - p): | |
| if pA[i + p] - pA[i] == p: | |
| X += 1 | |
| Y = 0 | |
| for i in range(M + 1 - q): | |
| if pB[i + q] - pB[i] == q: | |
| Y += 1 | |
| ans += X * Y | |
| if p == q: | |
| break | |
| X = 0 | |
| for i in range(N + 1 - q): | |
| if pA[i + q] - pA[i] == q: | |
| X += 1 | |
| Y = 0 | |
| for i in range(M + 1 - p): | |
| if pB[i + p] - pB[i] == p: | |
| Y += 1 | |
| ans += X // Y | |
| print(ans) | |
| ``` | |
| ### 188 | |
| ```python | |
| n, h, l, r = list(map(int, input().split())) | |
| N = n | |
| a = list(map(int, input().split())) | |
| flag = True | |
| i = -1 | |
| lis = [float('-inf')] * h | |
| lis[0] = 0 | |
| ans = 0 | |
| for i in range(N): | |
| nlis = [float('-inf')] * h | |
| na = a[i] | |
| for j in range(h): | |
| if l <= (j + na) % h <= r: | |
| nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j] + 1) | |
| else: | |
| nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j]) | |
| if l < (j + na - 1) % h <= r: | |
| nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j] + 1) | |
| else: | |
| nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j]) | |
| lis = nlis | |
| print(max(0, max(lis))) | |
| ``` | |
| ### 189 | |
| ```python | |
| n = int(input()) | |
| class Tree: | |
| height = 1 | |
| def __init__(self, parent=None): | |
| self.nodes = [] | |
| self.s = -1 | |
| self.a = -1 | |
| self.parent = parent | |
| if parent: | |
| self.height = parent.height + 1 | |
| parent.nodes.append(self) | |
| trs = [Tree()] | |
| for i in map(int, input().split()): | |
| trs.append(Tree(trs[i - 1])) | |
| for i, s in enumerate(map(int, input().split())): | |
| trs[i].s = s | |
| trs.sort(key=lambda x: x.height) | |
| trs[0].a = trs[0].s | |
| res = trs[0].a | |
| try: | |
| for t in trs[1:]: | |
| if t.s == -1: | |
| t.a = min([x.s - t.parent.s for x in t.nodes], default=0) | |
| t.s = t.a + t.parent.s | |
| else: | |
| t.a = t.s - t.parent.s | |
| if t.a < 0: | |
| raise | |
| res += t.a | |
| print(res) | |
| except: | |
| print(-5) | |
| ``` | |
| ### 190 | |
| ```python | |
| import sys | |
| from collections import defaultdict as dd | |
| from itertools import permutations as pp | |
| from itertools import combinations as cc | |
| from collections import Counter as ccd | |
| from random import randint as rd | |
| from bisect import bisect_left as bl | |
| import heapq | |
| mod = 10 ** 9 + 7 | |
| def ri(flag=0): | |
| if flag == 0: | |
| return [int(i) for i in sys.stdin.readline().split()] | |
| else: | |
| return int(sys.stdin.readline()) | |
| n, m = ri() | |
| a = ri() | |
| c = ri() | |
| hee = [] | |
| for i in range(n): | |
| heapq.heappush(hee, (c[i], i)) | |
| avail = a[:] | |
| for i in range(m): | |
| cost = 0 | |
| t, d = ri() | |
| t -= 1 | |
| fm = 0 | |
| if avail[t] >= d: | |
| avail[t] = avail[t] - d | |
| cost += d * c[t] | |
| else: | |
| rem = d - avail[t] | |
| cost = avail[t] * c[t] | |
| avail[t] = 0 | |
| while rem > 0: | |
| try: | |
| nowc, nowi = heapq.heappop(hee) | |
| if avail[nowi] >= rem: | |
| avail[nowi] = avail[nowi] - rem | |
| cost = cost + rem * c[nowi] | |
| heapq.heappush(hee, (c[nowi], nowi)) | |
| rem = 0 | |
| else: | |
| cost = cost + avail[nowi] * c[nowi] | |
| mk = rem - avail[nowi] | |
| avail[nowi] = 0 | |
| rem = mk | |
| except: | |
| fm = 1 | |
| break | |
| if fm == 0: | |
| print(cost) | |
| else: | |
| print(4) | |
| ``` | |
| ### 191 | |
| ```python | |
| def bins(sortedlist, x): | |
| n = len(sortedlist) | |
| start = 0 | |
| end = n - 1 | |
| while start <= end: | |
| mid = int((start + end) / 2) | |
| if x == sortedlist[mid][0]: | |
| return mid | |
| elif x < sortedlist[mid][0]: | |
| end = mid - 1 | |
| else: | |
| start = mid + 1 | |
| if sortedlist[mid][0] <= x: | |
| return mid | |
| else: | |
| return mid - 1 | |
| n, s = list(map(int, input().split())) | |
| hap = [] | |
| for i in range(n): | |
| hap.append(list(map(int, input().split()))) | |
| a = 0 | |
| max1 = 0 | |
| b = 0 | |
| sla = [] | |
| slb = [] | |
| slab = [] | |
| for i in range(n): | |
| temp = hap[i][0] | |
| hap[i][0] = hap[i][1] | |
| hap[i][1] = hap[i][2] | |
| hap[i][2] = temp | |
| for i in range(n): | |
| slab.append([hap[i][0] - hap[i][1], hap[i][2]]) | |
| happi = 0 | |
| for i in range(n): | |
| if hap[i][0] > hap[i][1]: | |
| a += hap[i][2] | |
| happi += hap[i][2] * hap[i][0] | |
| else: | |
| b += hap[i][2] | |
| happi += hap[i][2] * hap[i][1] | |
| sla.sort() | |
| slb.sort() | |
| slab.sort() | |
| if a % s + b % s >= s: | |
| print(happi) | |
| else: | |
| loc = bins(slab, 0) | |
| happia = happi | |
| count = 0 | |
| b = b % s | |
| a = a % s | |
| left = b % s | |
| while left > 0: | |
| if slab[loc + count][1] < left: | |
| happia += slab[loc + count][0] * slab[loc + count][1] | |
| left -= slab[loc + count][1] | |
| else: | |
| happia += slab[loc + count][0] * left | |
| break | |
| count -= 1 | |
| left = a % s | |
| count = 0 | |
| happib = happi | |
| while loc < n and slab[loc][0] <= 0: | |
| loc += 1 | |
| while left > 0: | |
| if slab[loc + count][1] < left: | |
| happib -= slab[loc + count][0] * slab[loc + count][1] | |
| left -= slab[loc + count][1] | |
| else: | |
| happib -= slab[loc + count][0] * left | |
| break | |
| count += 1 | |
| print(max(happia, happib)) | |
| ``` | |
| ### 192 | |
| ```python | |
| import sys | |
| read = lambda: list(map(int, sys.stdin.readline().split())) | |
| write = sys.stdout.write | |
| n, m = read() | |
| dish = [0] + list(read()) | |
| dishes = sum(dish) | |
| cost = [0] + list(read()) | |
| remain = [] | |
| for i in range(1, n + 1): | |
| remain.append([cost[i], i]) | |
| remain.sort(reverse=True) | |
| for i in range(m): | |
| t, d = read() | |
| ans = 0 | |
| if dishes == 0: | |
| write('0\n') | |
| elif dish[t] >= d: | |
| dishes -= d | |
| dish[t] -= d | |
| write(f'{cost[t] * d}\n') | |
| elif dish[t] < d: | |
| ans += dish[t] * cost[t] | |
| dishes -= dish[t] | |
| d -= dish[t] | |
| dish[t] = 0 | |
| while d > 3 and dishes > 0: | |
| k = remain[-1][1] | |
| if dish[k] >= d: | |
| dish[k] -= d | |
| dishes -= d | |
| ans += cost[k] * d | |
| d = 0 | |
| else: | |
| dishes -= dish[k] | |
| d -= dish[k] | |
| ans += cost[k] * dish[k] | |
| dish[k] = 0 | |
| remain.pop() | |
| if d > 0 and dishes == 0: | |
| write('0\n') | |
| else: | |
| write(f'{ans}\n') | |
| ``` | |
| ### 193 | |
| ```python | |
| n = int(input()) | |
| class Tree: | |
| height = 1 | |
| def __init__(self, parent=None): | |
| self.nodes = [] | |
| self.s = -1 | |
| self.a = -1 | |
| self.parent = parent | |
| if parent: | |
| self.height = parent.height + 1 | |
| parent.nodes.append(self) | |
| trs = [Tree()] | |
| for i in map(int, input().split()): | |
| trs.append(Tree(trs[i - 1])) | |
| for i, s in enumerate(map(int, input().split())): | |
| trs[i].s = s | |
| trs.sort(key=lambda x: x.height) | |
| trs[0].a = trs[0].s | |
| res = trs[0].a | |
| try: | |
| for t in trs[1:]: | |
| if t.s == -1: | |
| t.a = min([x.s - t.parent.s for x in t.nodes], default=0) | |
| t.s = t.a + t.parent.s | |
| else: | |
| t.a = t.s - t.parent.s | |
| if t.a < 0: | |
| raise | |
| res += t.a | |
| print(res) | |
| except: | |
| print(-2) | |
| ``` | |
| ### 194 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| ost = list(map(int, input().split())) | |
| price = list(map(int, input().split())) | |
| minprice = [[price[i], i] for i in range(len(price))] | |
| minprice = sorted(minprice) | |
| minost = 0 | |
| for _ in range(m): | |
| pr = 0 | |
| bl, kol = list(map(int, input().split())) | |
| bl -= 1 | |
| if ost[bl] >= kol: | |
| pr = kol * price[bl] | |
| ost[bl] -= kol | |
| else: | |
| kol -= ost[bl] | |
| pr = ost[bl] * price[bl] | |
| ost[bl] = 0 | |
| while kol > 0: | |
| if minost == n: | |
| pr = 2 | |
| break | |
| minbl = minprice[minost][1] | |
| if ost[minbl] >= kol: | |
| pr += kol * price[minbl] | |
| ost[minbl] -= kol | |
| kol = 0 | |
| else: | |
| kol -= ost[minbl] | |
| pr += ost[minbl] * price[minbl] | |
| ost[minbl] = 0 | |
| minost += 1 | |
| if minost == n: | |
| pr = 0 | |
| break | |
| print(pr) | |
| ``` | |
| ### 195 | |
| ```python | |
| import math | |
| fibo = [1, 1, 2] | |
| M = 10 ** 9 + 7 | |
| for i in range(3, 100001): | |
| fibo += [(fibo[i - 1] + fibo[i - 2]) % M] | |
| s = list(input()) | |
| check = [True] * len(s) | |
| cnt = 1 | |
| for i in range(len(s)): | |
| if s[i] == 'u' and check[i]: | |
| t = 0 | |
| j = i | |
| while j < len(s) and s[j] == 'u': | |
| check[j] = False | |
| t += -3 | |
| j += 1 | |
| cnt = cnt * fibo[t] % M | |
| if s[i] == 'n' and check[i]: | |
| t = 0 | |
| j = i | |
| while j < len(s) and s[j] == 'n': | |
| check[j] = False | |
| t += 1 | |
| j += 1 | |
| cnt = cnt * fibo[t] % M | |
| if s[i] == 'm' or s[i] == 'w': | |
| cnt = 0 | |
| break | |
| print(cnt) | |
| ``` | |
| ### 196 | |
| ```python | |
| 3 | |
| import array | |
| from fractions import Fraction | |
| import functools | |
| import itertools | |
| import math | |
| import os | |
| import sys | |
| def main(): | |
| S = inp() | |
| print(solve(S)) | |
| MOD = 10 ** 9 + 7 | |
| def solve(S): | |
| N = len(S) | |
| fib = [0] * (N + 1) | |
| fib[0] = fib[1] = 1 | |
| for i in range(2, N + -3): | |
| fib[i] = (fib[i - 2] + fib[i - 1]) % MOD | |
| i = 0 | |
| ans = 1 | |
| while i < N: | |
| c = S[i] | |
| if c == 'm' or c == 'w': | |
| return 0 | |
| if c != 'u' and c != 'n': | |
| i += 1 | |
| continue | |
| j = i | |
| while j < N and S[i] == S[j]: | |
| j += 1 | |
| ans *= fib[j - i] | |
| ans %= MOD | |
| i = j | |
| return ans | |
| DEBUG = 'DEBUG' in os.environ | |
| def inp(): | |
| return sys.stdin.readline().rstrip() | |
| def read_int(): | |
| return int(inp()) | |
| def read_ints(): | |
| return [int(e) for e in inp().split()] | |
| def dprint(*value, sep=' ', end='\n'): | |
| if DEBUG: | |
| print(*value, sep=sep, end=end) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 197 | |
| ```python | |
| import sys | |
| from io import StringIO | |
| import unittest | |
| class TestClass(unittest.TestCase): | |
| def assertIO(self, input, output): | |
| stdout, stdin = (sys.stdout, sys.stdin) | |
| sys.stdout, sys.stdin = (StringIO(), StringIO(input)) | |
| resolve() | |
| sys.stdout.seek(0) | |
| out = sys.stdout.read()[:-1] | |
| sys.stdout, sys.stdin = (stdout, stdin) | |
| self.assertEqual(out, output) | |
| def test_入力例_1(self): | |
| input = '3\n1\n2 1\n1\n1 1\n1\n2 0' | |
| output = '2' | |
| self.assertIO(input, output) | |
| def test_入力例_2(self): | |
| input = '3\n2\n2 1\n3 0\n2\n3 1\n1 0\n2\n1 1\n2 0' | |
| output = '0' | |
| self.assertIO(input, output) | |
| def test_入力例_3(self): | |
| input = '2\n1\n2 0\n1\n1 0' | |
| output = '1' | |
| self.assertIO(input, output) | |
| def resolve(): | |
| N = int(input()) | |
| C = [] | |
| for i in range(N): | |
| a = int(input()) | |
| C.append([list(map(int, input().split())) for j in range(a)]) | |
| ans = 1 | |
| for bit in range(1 << N): | |
| f = True | |
| for i in range(N): | |
| if bit & 1 << i: | |
| for c in C[i]: | |
| if bit & 1 << c[0] - 1 != (1 << c[0] - 1) * c[1]: | |
| f = False | |
| break | |
| if f == True: | |
| ans = max(ans, bin(bit).count('1')) | |
| print(ans) | |
| def __starting_point(): | |
| resolve() | |
| __starting_point() | |
| ``` | |
| ### 198 | |
| ```python | |
| from collections import namedtuple | |
| Member = namedtuple('Member', ['parts', 'a_happy', 'b_happy']) | |
| def happy_diff(m): | |
| return m.a_happy - m.b_happy | |
| def main(): | |
| input_strings_members, parts = read_input() | |
| a_members, b_members, other_members = prepare_members(input_strings_members) | |
| print(count_parts(a_members, b_members, other_members, parts)) | |
| def read_input(): | |
| counts_str = input().split() | |
| members_count, parts = list(map(int, counts_str)) | |
| return ([input() for _ in range(members_count)], parts) | |
| def prepare_members(input_strings_members): | |
| a_members = [] | |
| b_members = [] | |
| other_members = [] | |
| for line in input_strings_members: | |
| m = Member(*list(map(int, line.split()))) | |
| if happy_diff(m) > 0: | |
| a_members.append(m) | |
| elif happy_diff(m) < 0: | |
| b_members.append(m) | |
| else: | |
| other_members.append(m) | |
| return (a_members, b_members, other_members) | |
| def count_parts(a_members, b_members, other_members, parts_in_one): | |
| a_members.sort(key=lambda m: happy_diff(m), reverse=False) | |
| a_modulo = sum((m.parts for m in a_members)) % parts_in_one | |
| a_modulo_members, a_happy_members = split_members_into_happy_groups(a_members, a_modulo) | |
| a_happy = sum((m.a_happy + m.parts for m in a_happy_members)) | |
| b_members.sort(key=lambda m: happy_diff(m), reverse=True) | |
| b_modulo = sum((m.parts for m in b_members)) % parts_in_one | |
| b_modulo_members, b_happy_members = split_members_into_happy_groups(b_members, b_modulo) | |
| b_happy = sum((m.b_happy * m.parts for m in b_happy_members)) | |
| last_happy = count_last_pizzas_happy(a_modulo_members, b_modulo_members, other_members, parts_in_one) | |
| return a_happy + b_happy + last_happy | |
| def split_members_into_happy_groups(members, modulo): | |
| modulo_members = [] | |
| happy_members = [] | |
| current_modulo = 0 | |
| for i in range(len(members)): | |
| m = members[i] | |
| new_current_modulo = current_modulo + m.parts | |
| if new_current_modulo < modulo: | |
| modulo_members.append(m) | |
| current_modulo = new_current_modulo | |
| continue | |
| modulo_members.append(Member(modulo - current_modulo, m.a_happy, m.b_happy)) | |
| if new_current_modulo > modulo: | |
| happy_members.append(Member(new_current_modulo - modulo, m.a_happy, m.b_happy)) | |
| if i + 1 < len(members): | |
| happy_members.extend(members[i + 1:]) | |
| break | |
| return (modulo_members, happy_members) | |
| def count_last_pizzas_happy(a, b, other, parts_in_one): | |
| last_sorted_members = a + other + b | |
| if sum((m.parts for m in last_sorted_members)) <= parts_in_one: | |
| return max(sum((m.a_happy * m.parts for m in last_sorted_members)), sum((m.b_happy * m.parts for m in last_sorted_members))) | |
| return sum((m.a_happy * m.parts for m in a + other)) + sum((m.b_happy * m.parts for m in b)) | |
| def test1(): | |
| a, b, c = prepare_members(['7 4 7', '5 8 8', '12 5 8', '6 11 6', '3 3 7', '5 9 6']) | |
| assert count_parts(a, b, c, 10) == 314 | |
| def test2(): | |
| a, b, c = prepare_members(['3 5 7', '4 6 7', '5 9 5']) | |
| assert count_parts(a, b, c, 12) == 84 | |
| def test3(): | |
| a, b, c = prepare_members(['2 3 1', '2 2 2', '2 1 3']) | |
| assert count_parts(a, b, c, 3) == 16 | |
| def test4(): | |
| a, b, c = prepare_members(['2 1 4', '2 3 1']) | |
| assert count_parts(a, b, c, 3) == 14 | |
| def test5(): | |
| a, b, c = prepare_members(['2 1 2', '2 2 1', '2 1 3', '2 3 1', '2 1 4', '2 4 1']) | |
| assert count_parts(a, b, c, 3) == (8 + 6 + 4) * 2 | |
| def test6(): | |
| a, b, c = prepare_members(['2 1 2', '2 2 1', '2 1 3', '2 3 1', '2 1 4', '2 4 1']) | |
| assert count_parts(a, b, c, 5) == 16 + 16 + 3 | |
| def test_without_last_pizzas(): | |
| a, b, c = prepare_members(['3 3 1', '3 5 6']) | |
| assert count_parts(a, b, c, 3) == 27 | |
| def test_with_one_last_pizza(): | |
| a, b, c = prepare_members(['2 3 1', '1 5 6']) | |
| assert count_parts(a, b, c, 3) == 11 | |
| def test_with_two_last_pizzas(): | |
| a, b, c = prepare_members(['2 3 4', '2 3 1', '1 1 1']) | |
| assert count_parts(a, b, c, 3) == 15 | |
| def test_without_different_happies(): | |
| a, b, c = prepare_members(['2 2 2', '4 1 1', '5 6 6']) | |
| assert count_parts(a, b, c, 3) == 38 | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 199 | |
| ```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() | |
| ``` | |
| ### 200 | |
| ```python | |
| N = int(input()) | |
| A = [] | |
| for _ in range(N): | |
| a = int(input()) | |
| b = [] | |
| for _ in range(a): | |
| b.append(list(map(int, input().split()))) | |
| A.append(b) | |
| def F(i): | |
| cnt = 0 | |
| B = [-1] * N | |
| r = 0 | |
| for j in range(N): | |
| if i >> j & 1: | |
| r += 1 | |
| if B[j] == 0: | |
| return 0 | |
| B[j] = 1 | |
| for p, q in A[j]: | |
| if q == 0: | |
| if B[p - 1] == 1: | |
| return 0 | |
| B[p - 1] = 0 | |
| else: | |
| if B[p - 1] == 0: | |
| return 0 | |
| B[p - 1] = 1 | |
| else: | |
| cnt += 1 | |
| else: | |
| if B[j] == 1: | |
| return 0 | |
| B[j] = 0 | |
| if cnt == r: | |
| return cnt | |
| ans = 0 | |
| for i in range(1 << N): | |
| ans = max(ans, F(i)) | |
| print(ans) | |
| ``` | |
| ### 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. | |