|
| 1 | +#region imports |
| 2 | +import copy |
| 3 | +from collections import defaultdict as dd |
| 4 | +from collections import deque |
| 5 | +import math |
| 6 | +import sys |
| 7 | +import os |
| 8 | +import random |
| 9 | +import time |
| 10 | +#endregion |
| 11 | +#region @bootstrap for recursive functions |
| 12 | +# # TLDR: |
| 13 | +# # Python's recursion limit is 1000. Increasing the limit often causes MLE. |
| 14 | +# # Instead, use this decorator. See https://codeforces.com/blog/entry/80158?#comment-662204. |
| 15 | +# # Usage instructions: |
| 16 | +# # 1) Put @bootstrap 1 line before the recursive function. |
| 17 | +# # 2) Replace "return" with "yield" |
| 18 | +# # 3) You also need to wrap your recursive calls like (yield myfunction(x)). |
| 19 | +# # (applies even if the function doesn't return anything!) |
| 20 | +# (only do this for recursive calls within the recursive function though!) |
| 21 | +# # Example (original version): |
| 22 | +# def f(n): |
| 23 | +# if n <= 0: return 1 |
| 24 | +# return f(n-1) + f(n-2) |
| 25 | +# |
| 26 | +# print(f(5)) |
| 27 | +# |
| 28 | +# # Example (fixed): |
| 29 | +# @bootstrap |
| 30 | +# def f(n): |
| 31 | +# if n <= 0: yield 1 |
| 32 | +# yield (yield f(n-1)) + (yield f(n-2)) |
| 33 | +# |
| 34 | +# print(f(5)) # don't add `yield` to the call from outside! |
| 35 | +# |
| 36 | +# Extended example: |
| 37 | +# Before: https://pastebin.com/inwciZgh |
| 38 | +# After: https://pastebin.com/fKksXXLj |
| 39 | +# Try submitting at: https://codingcompetitions.withgoogle.com/codejam/round/0000000000876ff1/0000000000a45ef7 |
| 40 | + |
| 41 | +from types import GeneratorType |
| 42 | +def bootstrap(f, stack=[]): |
| 43 | + def wrappedfunc(*args, **kwargs): |
| 44 | + if stack: |
| 45 | + return f(*args, **kwargs) |
| 46 | + else: |
| 47 | + to = f(*args, **kwargs) |
| 48 | + while True: |
| 49 | + if type(to) is GeneratorType: |
| 50 | + stack.append(to) |
| 51 | + to = next(to) |
| 52 | + else: |
| 53 | + stack.pop() |
| 54 | + if not stack: |
| 55 | + break |
| 56 | + to = stack[-1].send(to) |
| 57 | + return to |
| 58 | + return wrappedfunc |
| 59 | +#endregion |
| 60 | +#region dbg commands |
| 61 | +# remember .bashrc should contain `export PYTHON_CONTEST_HELPER="dummy"` |
| 62 | + |
| 63 | +local_run = False |
| 64 | +def dbg(*args, **kwargs): pass |
| 65 | +def dbgG(*args, **kwargs): pass |
| 66 | +def dbgP(*args, **kwargs): pass |
| 67 | +def dbgY(*args, **kwargs): pass |
| 68 | +def dbgR(*args, **kwargs): pass |
| 69 | +def dbgB(*args, **kwargs): pass |
| 70 | +def dbgW(*args, **kwargs): pass |
| 71 | +def dbgBackground(*args, **kwargs): pass |
| 72 | +def dbgc(*args, **kwargs): pass |
| 73 | +def dbgcG(*args, **kwargs): pass |
| 74 | +def dbgcP(*args, **kwargs): pass |
| 75 | +def dbgcY(*args, **kwargs): pass |
| 76 | +def dbgcR(*args, **kwargs): pass |
| 77 | +def dbgcB(*args, **kwargs): pass |
| 78 | +def dbgcW(*args, **kwargs): pass |
| 79 | +def dbgcBackground(*args, **kwargs): pass |
| 80 | +def el(n=1): pass |
| 81 | +def print_details_helper(*args, **kwargs): pass |
| 82 | +def print_tsv_helper(*args, **kwargs): pass |
| 83 | +def pdh(*args, **kwargs): pass |
| 84 | +def pth(*args, **kwargs): pass |
| 85 | +if os.environ.get("PYTHON_CONTEST_HELPER"): |
| 86 | + local_run = True |
| 87 | + |
| 88 | + # call like dbg(print_details_helper(stuff)) |
| 89 | + # provides more detailed listing of complicated objects |
| 90 | + def print_details_helper(q): |
| 91 | + out = [] |
| 92 | + for k, x in enumerate(q): |
| 93 | + out.append(f"\n\t{k}:\t{x}") |
| 94 | + return ''.join(out) + '\n' |
| 95 | + |
| 96 | + pdh = print_details_helper # alternate shorter name |
| 97 | + |
| 98 | + # good if we want to see spacing, or to copy-paste into GSheets |
| 99 | + def print_tsv_helper(q): |
| 100 | + out = [] |
| 101 | + for row in q: |
| 102 | + out.append('\t'.join(str(x) for x in row)) |
| 103 | + return ''.join('\n\t' + x for x in out) + '\n' |
| 104 | + |
| 105 | + pth = print_tsv_helper |
| 106 | + |
| 107 | + OUT_RED_BOLD = "\033[31;1m" |
| 108 | + OUT_GREEN = "\033[32m" |
| 109 | + OUT_RESET = "\033[0m" |
| 110 | + OUT_BOLD = "\033[;1m" |
| 111 | + OUT_RED = "\033[31m" |
| 112 | + OUT_WHITE = "\033[97m" |
| 113 | + OUT_BLUE = "\033[34;1m" |
| 114 | + OUT_CYAN = "\033[36;1m" |
| 115 | + OUT_PURPLE = "\033[35;1m" |
| 116 | + OUT_YELLOW = "\033[33;1m" |
| 117 | + OUT_BACKGROUND = "\033[41;30;1m" |
| 118 | + |
| 119 | + # helper function so your error printouts won't show up above an earlier stdout line. |
| 120 | + def flush_stdout(): |
| 121 | + print('', flush=True, end='') |
| 122 | + |
| 123 | + def dbgBase(*args, **kwargs): |
| 124 | + flush_stdout() |
| 125 | + color_helper = kwargs.pop('color', OUT_CYAN) |
| 126 | + if kwargs.get('comment_first', False): |
| 127 | + print(f'{OUT_BOLD}{color_helper}{args[0]: >11} {OUT_RESET}', end='', file=sys.stderr) |
| 128 | + args = tuple(args[1:]) |
| 129 | + else: |
| 130 | + print(f"{'': >12}", end='', file=sys.stderr) |
| 131 | + if 'comment_first' in kwargs: |
| 132 | + del kwargs['comment_first'] |
| 133 | + print(f"{OUT_RED_BOLD}{sys._getframe().f_back.f_back.f_lineno: >7} {OUT_BOLD}: {OUT_RESET}{color_helper}", end='', file=sys.stderr) |
| 134 | + end_maybe = kwargs.get('end', '\n') |
| 135 | + kwargs['end']=f"{OUT_RESET}{end_maybe}" |
| 136 | + if 'sep' not in kwargs: |
| 137 | + kwargs['sep'] = ' '*3 |
| 138 | + kwargs['flush'] = True |
| 139 | + print(*args, file=sys.stderr, **kwargs) |
| 140 | + |
| 141 | + # Can always comment out some of these lines to disable those logs. |
| 142 | + # e.g. if you make lots of messy printouts while finding bug #1 but now it's fixed but you still want SOME printouts. |
| 143 | + def dbg(*args, **kwargs): dbgBase(color=OUT_CYAN, *args, **kwargs) |
| 144 | + def dbgG(*args, **kwargs): dbgBase(color=OUT_GREEN, *args, **kwargs) |
| 145 | + def dbgP(*args, **kwargs): dbgBase(color=OUT_PURPLE, *args, **kwargs) |
| 146 | + def dbgY(*args, **kwargs): dbgBase(color=OUT_YELLOW, *args, **kwargs) |
| 147 | + def dbgR(*args, **kwargs): dbgBase(color=OUT_RED, *args, **kwargs) |
| 148 | + def dbgB(*args, **kwargs): dbgBase(color=OUT_BLUE, *args, **kwargs) |
| 149 | + def dbgW(*args, **kwargs): dbgBase(color=OUT_WHITE, *args, **kwargs) |
| 150 | + def dbgBackground(*args, **kwargs): dbgBase(color=OUT_BACKGROUND, *args, **kwargs) |
| 151 | + def el(n=1): flush_stdout(); print('\n'*n, file=sys.stderr, end='', flush=True) |
| 152 | + def dbgc(*args, **kwargs): dbgBase(comment_first=True, color=OUT_CYAN, *args, **kwargs) |
| 153 | + def dbgcG(*args, **kwargs): dbgBase(comment_first=True, color=OUT_GREEN, *args, **kwargs) |
| 154 | + def dbgcP(*args, **kwargs): dbgBase(comment_first=True, color=OUT_PURPLE, *args, **kwargs) |
| 155 | + def dbgcY(*args, **kwargs): dbgBase(comment_first=True, color=OUT_YELLOW, *args, **kwargs) |
| 156 | + def dbgcR(*args, **kwargs): dbgBase(comment_first=True, color=OUT_RED, *args, **kwargs) |
| 157 | + def dbgcB(*args, **kwargs): dbgBase(comment_first=True, color=OUT_BLUE, *args, **kwargs) |
| 158 | + def dbgcW(*args, **kwargs): dbgBase(comment_first=True, color=OUT_WHITE, *args, **kwargs) |
| 159 | + def dbgcBackground(*args, **kwargs): dbgBase(comment_first=True, color=OUT_BACKGROUND, *args, **kwargs) |
| 160 | +#endregion |
| 161 | +#region FastIO |
| 162 | +from io import BytesIO, IOBase |
| 163 | + |
| 164 | +BUFSIZE = 8192 |
| 165 | + |
| 166 | + |
| 167 | +class FastIO(IOBase): |
| 168 | + newlines = 0 |
| 169 | + |
| 170 | + def __init__(self, file): |
| 171 | + self._fd = file.fileno() |
| 172 | + self.buffer = BytesIO() |
| 173 | + self.writable = "x" in file.mode or "r" not in file.mode |
| 174 | + self.write = self.buffer.write if self.writable else None |
| 175 | + |
| 176 | + def read(self): |
| 177 | + while True: |
| 178 | + b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) |
| 179 | + if not b: |
| 180 | + break |
| 181 | + ptr = self.buffer.tell() |
| 182 | + self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) |
| 183 | + self.newlines = 0 |
| 184 | + return self.buffer.read() |
| 185 | + |
| 186 | + def readline(self): |
| 187 | + while self.newlines == 0: |
| 188 | + b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) |
| 189 | + self.newlines = b.count(b"\n") + (not b) |
| 190 | + ptr = self.buffer.tell() |
| 191 | + self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) |
| 192 | + self.newlines -= 1 |
| 193 | + return self.buffer.readline() |
| 194 | + |
| 195 | + def flush(self): |
| 196 | + if self.writable: |
| 197 | + os.write(self._fd, self.buffer.getvalue()) |
| 198 | + self.buffer.truncate(0), self.buffer.seek(0) |
| 199 | + |
| 200 | + |
| 201 | +class IOWrapper(IOBase): |
| 202 | + def __init__(self, file): |
| 203 | + self.buffer = FastIO(file) |
| 204 | + self.flush = self.buffer.flush |
| 205 | + self.writable = self.buffer.writable |
| 206 | + self.write = lambda s: self.buffer.write(s.encode("ascii")) |
| 207 | + self.read = lambda: self.buffer.read().decode("ascii") |
| 208 | + self.readline = lambda: self.buffer.readline().decode("ascii") |
| 209 | + |
| 210 | + |
| 211 | +sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) |
| 212 | +input = lambda: sys.stdin.readline().rstrip("\r\n") |
| 213 | +#endregion |
| 214 | +#region input helpers |
| 215 | +def nn(): |
| 216 | + return int(input()) |
| 217 | + |
| 218 | +def nn1(): |
| 219 | + return nn() - 1 |
| 220 | + |
| 221 | +def li(): |
| 222 | + return list(input()) |
| 223 | + |
| 224 | +def lm(): |
| 225 | + return list(map(int, input().split())) |
| 226 | + |
| 227 | +def lm1(): |
| 228 | + return list(map(lambda x : int(x) - 1, input().split())) |
| 229 | + |
| 230 | +rv = lm |
| 231 | +rv1 = lm1 |
| 232 | + |
| 233 | +# To print while flushing output for interactive problems: |
| 234 | +# print(x, flush=True) |
| 235 | +#endregion |
| 236 | +#region output helpers |
| 237 | +def ps(*args, **kwargs): |
| 238 | + if hasattr(args[0], '__iter__') and not isinstance(args[0], str): |
| 239 | + print(' '.join(str(x) for x in args[0]), **kwargs) |
| 240 | + else: |
| 241 | + print(' '.join(str(x) for x in args), **kwargs) |
| 242 | + |
| 243 | +def pv(q): ps(*q) |
| 244 | +def ps1(*args, **kwargs): |
| 245 | + if hasattr(args[0], '__iter__'): |
| 246 | + print(' '.join(str(x+1) for x in args[0]), **kwargs) |
| 247 | + else: |
| 248 | + print(' '.join(str(x+1) for x in args), **kwargs) |
| 249 | +def pv1(q): ps1(*q) |
| 250 | +def pvn(q): |
| 251 | + for x in q: ps(x) |
| 252 | +def pvn1(q): |
| 253 | + for x in q: ps1(x) |
| 254 | +#endregion |
| 255 | +#region logic |
| 256 | + |
| 257 | +EXECUTION_START_TIME = time.time() |
| 258 | +def TIME(): |
| 259 | + # return time.time() - EXECUTION_START_TIME |
| 260 | + return int((time.time() - EXECUTION_START_TIME) * 10**6) / 10**6 |
| 261 | + |
| 262 | +def YES(): return print("YES") |
| 263 | +def NO(): return print("NO") |
| 264 | +def Yes(): return print("Yes") |
| 265 | +def No(): return print("No") |
| 266 | + |
| 267 | +def remDup(v): |
| 268 | + v = list(set(v)) |
| 269 | + |
| 270 | +steps_orthogonal = [(1,0), (0,1), (-1,0), (0,-1)] |
| 271 | +steps_diagonal = [(1,1), (1,-1), (-1,-1), (-1,1)] |
| 272 | +steps_8dirs = [(-1,-1), (0,-1), (1,-1), (-1,0), (1,0), (-1,1), (0,1), (1,1)] |
| 273 | + |
| 274 | +def fstTrue(lo, hi, f): |
| 275 | + hi += 1 |
| 276 | + assert lo <= hi |
| 277 | + while lo < hi: |
| 278 | + mid = lo + (hi-lo) // 2 |
| 279 | + if f(mid): hi = mid |
| 280 | + else: lo = mid + 1 |
| 281 | + return lo |
| 282 | + |
| 283 | +first_true = fstTrue |
| 284 | + |
| 285 | +def lstTrue(lo, hi, f): |
| 286 | + lo -= 1 |
| 287 | + assert lo <= hi |
| 288 | + while lo < hi: |
| 289 | + mid = lo + (hi-lo+1) // 2 |
| 290 | + if f(mid): lo = mid |
| 291 | + else: hi = mid-1 |
| 292 | + return lo |
| 293 | + |
| 294 | +last_true = lstTrue |
| 295 | + |
| 296 | +# "ceiling divide" and "floor divide" |
| 297 | +def cdiv(a, b): return a//b + (1 if ((a^b)>0 and a%b != 0) else 0) |
| 298 | +def fdiv(a, b): return a//b - (1 if ((a^b)<0 and a%b != 0) else 0) |
| 299 | + |
| 300 | +def bits_set(x): return bin(x).count('1') |
| 301 | +pct = bits_set |
| 302 | +popcount = bits_set |
| 303 | + |
| 304 | + |
| 305 | +#endregion |
| 306 | + |
| 307 | +######################################################################### |
| 308 | +# Problem specific code usually goes below this line. |
| 309 | +######################################################################### |
| 310 | + |
| 311 | +# ! Read the sample cases before writing code! |
| 312 | + |
| 313 | + |
| 314 | + |
| 315 | + |
| 316 | + |
| 317 | + |
| 318 | + |
| 319 | + |
| 320 | +def solve(testID): |
| 321 | + N = nn() |
| 322 | + dat = lm() |
| 323 | + dbgR(N, dat) |
| 324 | + el() |
| 325 | + |
| 326 | + # ! Read the sample cases AND EXPLANATIONS before writing code for div1 A and later! |
| 327 | + |
| 328 | + |
| 329 | + |
| 330 | + return |
| 331 | + |
| 332 | + |
| 333 | +# ! Do something instead of nothing: write out small cases, code bruteforce |
| 334 | +# ! Check bounds even if I have a solution - are they letting through simpler versions? |
| 335 | +# ! If stuck on a "should be easy" problem for 10 mins, reread statement, check bounds |
| 336 | +SINGLE_CASE = False |
| 337 | +# SINGLE_CASE = True |
| 338 | +#region main |
| 339 | +if __name__ == '__main__': |
| 340 | + T = 1 |
| 341 | + if not SINGLE_CASE: |
| 342 | + dbgBackground("Loading num cases!!!!!") |
| 343 | + T = nn() # ! Comment this out for single-case problems! |
| 344 | + for testID in range(1, T+1): |
| 345 | + el() |
| 346 | + dbgBackground(f"Case {testID}") |
| 347 | + solve(testID) |
| 348 | + dbgcR("runtime", TIME()) |
| 349 | +#endregion |
0 commit comments