description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
We guessed a permutation $p$ consisting of $n$ integers. The permutation of length $n$ is the array of length $n$ where each element from $1$ to $n$ appears exactly once. This permutation is a secret for you. For each position $r$ from $2$ to $n$ we chose some other index $l$ ($l < r$) and gave you the segment $p_l, p_{l + 1}, \dots, p_r$ in sorted order (i.e. we rearranged the elements of this segment in a way that the elements of this segment are sorted). Thus, you are given exactly $n-1$ segments of the initial permutation but elements inside each segment are sorted. The segments are given to you in random order. For example, if the secret permutation is $p=[3, 1, 4, 6, 2, 5]$ then the possible given set of segments can be: $[2, 5, 6]$ $[4, 6]$ $[1, 3, 4]$ $[1, 3]$ $[1, 2, 4, 6]$ Your task is to find any suitable permutation (i.e. any permutation corresponding to the given input data). It is guaranteed that the input data corresponds to some permutation (i.e. such permutation exists). You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 100$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains one integer $n$ ($2 \le n \le 200$) β€” the length of the permutation. The next $n-1$ lines describe given segments. The $i$-th line contains the description of the $i$-th segment. The line starts with the integer $k_i$ ($2 \le k_i \le n$) β€” the length of the $i$-th segment. Then $k_i$ integers follow. All integers in a line are distinct, sorted in ascending order, between $1$ and $n$, inclusive. It is guaranteed that the required $p$ exists for each test case. It is also guaranteed that the sum of $n$ over all test cases does not exceed $200$ ($\sum n \le 200$). -----Output----- For each test case, print the answer: $n$ integers $p_1, p_2, \dots, p_n$ ($1 \le p_i \le n$, all $p_i$ should be distinct) β€” any suitable permutation (i.e. any permutation corresponding to the test case input). -----Example----- Input 5 6 3 2 5 6 2 4 6 3 1 3 4 2 1 3 4 1 2 4 6 5 2 2 3 2 1 2 2 1 4 2 4 5 7 3 1 2 6 4 1 3 5 6 2 1 2 3 4 5 7 6 1 2 3 4 5 6 3 1 3 6 2 2 1 2 5 2 2 5 3 2 3 5 4 2 3 4 5 5 1 2 3 4 5 Output 3 1 4 6 2 5 3 2 1 4 5 2 1 6 3 5 4 7 1 2 2 5 3 4 1
for _ in range(int(input())): n = int(input()) segs = [tuple(sorted(int(x) for x in input().split()[1:])) for __ in range(n - 1)] segs_set = set(segs) segs_having = [[j for j, seg in enumerate(segs) if i in seg] for i in range(n + 1)] unsorted = list(range(1, n + 1)) ans = [] def rb(avoid=0): if len(unsorted) > 1: candidate_lasts = [ i for i in unsorted if len(segs_having[i]) == 1 and i != avoid ] for last in candidate_lasts: seg_to_remove = segs_having[last][0] for ii in segs[seg_to_remove]: segs_having[ii].remove(seg_to_remove) unsorted.remove(last) ans.insert(0, last) if rb(avoid=next((i for i in candidate_lasts if i != last), avoid)): return True ans.pop(0) unsorted.append(last) for ii in segs[seg_to_remove]: segs_having[ii].append(seg_to_remove) return False else: ans.insert(0, unsorted[0]) valid = all( any(tuple(sorted(ans[j:i])) in segs_set for j in range(0, i - 1)) for i in range(2, n + 1) ) if not valid: ans.pop(0) return valid rb() print(*ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FUNC_DEF NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
We guessed a permutation $p$ consisting of $n$ integers. The permutation of length $n$ is the array of length $n$ where each element from $1$ to $n$ appears exactly once. This permutation is a secret for you. For each position $r$ from $2$ to $n$ we chose some other index $l$ ($l < r$) and gave you the segment $p_l, p_{l + 1}, \dots, p_r$ in sorted order (i.e. we rearranged the elements of this segment in a way that the elements of this segment are sorted). Thus, you are given exactly $n-1$ segments of the initial permutation but elements inside each segment are sorted. The segments are given to you in random order. For example, if the secret permutation is $p=[3, 1, 4, 6, 2, 5]$ then the possible given set of segments can be: $[2, 5, 6]$ $[4, 6]$ $[1, 3, 4]$ $[1, 3]$ $[1, 2, 4, 6]$ Your task is to find any suitable permutation (i.e. any permutation corresponding to the given input data). It is guaranteed that the input data corresponds to some permutation (i.e. such permutation exists). You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 100$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains one integer $n$ ($2 \le n \le 200$) β€” the length of the permutation. The next $n-1$ lines describe given segments. The $i$-th line contains the description of the $i$-th segment. The line starts with the integer $k_i$ ($2 \le k_i \le n$) β€” the length of the $i$-th segment. Then $k_i$ integers follow. All integers in a line are distinct, sorted in ascending order, between $1$ and $n$, inclusive. It is guaranteed that the required $p$ exists for each test case. It is also guaranteed that the sum of $n$ over all test cases does not exceed $200$ ($\sum n \le 200$). -----Output----- For each test case, print the answer: $n$ integers $p_1, p_2, \dots, p_n$ ($1 \le p_i \le n$, all $p_i$ should be distinct) β€” any suitable permutation (i.e. any permutation corresponding to the test case input). -----Example----- Input 5 6 3 2 5 6 2 4 6 3 1 3 4 2 1 3 4 1 2 4 6 5 2 2 3 2 1 2 2 1 4 2 4 5 7 3 1 2 6 4 1 3 5 6 2 1 2 3 4 5 7 6 1 2 3 4 5 6 3 1 3 6 2 2 1 2 5 2 2 5 3 2 3 5 4 2 3 4 5 5 1 2 3 4 5 Output 3 1 4 6 2 5 3 2 1 4 5 2 1 6 3 5 4 7 1 2 2 5 3 4 1
import sys sys.setrecursionlimit(10**6) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LI1(): return list(map(int1, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def SI(): return sys.stdin.readline()[:-1] def main(): def ok(): if len(ans) < n: return False for r, si in enumerate(sii, 2): s = ss[si] cur = set(ans[r - len(s) : r]) if s != cur: return False return True for _ in range(II()): n = II() ee = [([n**2] * n) for _ in range(n)] for ei in range(n): ee[ei][ei] = 0 ss = [set(LI()[1:]) for _ in range(n - 1)] for a0 in range(1, n + 1): used = set([a0]) ans = [a0] sii = [] for _ in range(n - 1): nxt = [] for si, s in enumerate(ss): cur = s - used if len(cur) == 1: for a in cur: nxt.append(a) sii.append(si) if len(nxt) != 1: break ans.append(nxt[0]) used.add(nxt[0]) if ok(): break print(*ans) main()
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR NUMBER FUNC_DEF FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR ASSIGN VAR LIST VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
We guessed a permutation $p$ consisting of $n$ integers. The permutation of length $n$ is the array of length $n$ where each element from $1$ to $n$ appears exactly once. This permutation is a secret for you. For each position $r$ from $2$ to $n$ we chose some other index $l$ ($l < r$) and gave you the segment $p_l, p_{l + 1}, \dots, p_r$ in sorted order (i.e. we rearranged the elements of this segment in a way that the elements of this segment are sorted). Thus, you are given exactly $n-1$ segments of the initial permutation but elements inside each segment are sorted. The segments are given to you in random order. For example, if the secret permutation is $p=[3, 1, 4, 6, 2, 5]$ then the possible given set of segments can be: $[2, 5, 6]$ $[4, 6]$ $[1, 3, 4]$ $[1, 3]$ $[1, 2, 4, 6]$ Your task is to find any suitable permutation (i.e. any permutation corresponding to the given input data). It is guaranteed that the input data corresponds to some permutation (i.e. such permutation exists). You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 100$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains one integer $n$ ($2 \le n \le 200$) β€” the length of the permutation. The next $n-1$ lines describe given segments. The $i$-th line contains the description of the $i$-th segment. The line starts with the integer $k_i$ ($2 \le k_i \le n$) β€” the length of the $i$-th segment. Then $k_i$ integers follow. All integers in a line are distinct, sorted in ascending order, between $1$ and $n$, inclusive. It is guaranteed that the required $p$ exists for each test case. It is also guaranteed that the sum of $n$ over all test cases does not exceed $200$ ($\sum n \le 200$). -----Output----- For each test case, print the answer: $n$ integers $p_1, p_2, \dots, p_n$ ($1 \le p_i \le n$, all $p_i$ should be distinct) β€” any suitable permutation (i.e. any permutation corresponding to the test case input). -----Example----- Input 5 6 3 2 5 6 2 4 6 3 1 3 4 2 1 3 4 1 2 4 6 5 2 2 3 2 1 2 2 1 4 2 4 5 7 3 1 2 6 4 1 3 5 6 2 1 2 3 4 5 7 6 1 2 3 4 5 6 3 1 3 6 2 2 1 2 5 2 2 5 3 2 3 5 4 2 3 4 5 5 1 2 3 4 5 Output 3 1 4 6 2 5 3 2 1 4 5 2 1 6 3 5 4 7 1 2 2 5 3 4 1
t = int(input()) for _ in range(t): n = int(input()) ns = {} for _ in range(n - 1): _, *p = map(int, input().split()) tp = tuple(p) for pp in p: ns.setdefault(pp, set()).add(tp) first = [(k, list(v)[0]) for k, v in ns.items() if len(v) == 1] found = False while not found: nns = {k: v.copy() for k, v in ns.items()} min_index = {} cur, cur_tp = first.pop() result = [cur] failed = False while len(nns) > 0 and not failed: nxt = set() for k in cur_tp: mi = n - len(result) - len(cur_tp) + 1 min_index[k] = max(min_index.get(k, 0), mi) nsk = nns[k] nsk.remove(cur_tp) if k == cur: nns.pop(k) elif len(nsk) == 1: nxt.add(k) if len(nns) == len(nxt) or len(nns) == 1: break if len(nxt) == 0: failed = True else: mmi = -1 for nx in nxt: if min_index[nx] > mmi: mmi = min_index[nx] cur = nx cur_tp = list(nns[cur])[0] result.append(cur) if not failed: found = True if len(nns) == 1: result.append(nns.popitem()[0]) else: a1, a2 = nns.keys() if min_index[a1] == 1: result.extend((a1, a2)) else: result.extend((a2, a1)) print(" ".join(map(str, reversed(result))))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL VAR VAR
We guessed a permutation $p$ consisting of $n$ integers. The permutation of length $n$ is the array of length $n$ where each element from $1$ to $n$ appears exactly once. This permutation is a secret for you. For each position $r$ from $2$ to $n$ we chose some other index $l$ ($l < r$) and gave you the segment $p_l, p_{l + 1}, \dots, p_r$ in sorted order (i.e. we rearranged the elements of this segment in a way that the elements of this segment are sorted). Thus, you are given exactly $n-1$ segments of the initial permutation but elements inside each segment are sorted. The segments are given to you in random order. For example, if the secret permutation is $p=[3, 1, 4, 6, 2, 5]$ then the possible given set of segments can be: $[2, 5, 6]$ $[4, 6]$ $[1, 3, 4]$ $[1, 3]$ $[1, 2, 4, 6]$ Your task is to find any suitable permutation (i.e. any permutation corresponding to the given input data). It is guaranteed that the input data corresponds to some permutation (i.e. such permutation exists). You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 100$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains one integer $n$ ($2 \le n \le 200$) β€” the length of the permutation. The next $n-1$ lines describe given segments. The $i$-th line contains the description of the $i$-th segment. The line starts with the integer $k_i$ ($2 \le k_i \le n$) β€” the length of the $i$-th segment. Then $k_i$ integers follow. All integers in a line are distinct, sorted in ascending order, between $1$ and $n$, inclusive. It is guaranteed that the required $p$ exists for each test case. It is also guaranteed that the sum of $n$ over all test cases does not exceed $200$ ($\sum n \le 200$). -----Output----- For each test case, print the answer: $n$ integers $p_1, p_2, \dots, p_n$ ($1 \le p_i \le n$, all $p_i$ should be distinct) β€” any suitable permutation (i.e. any permutation corresponding to the test case input). -----Example----- Input 5 6 3 2 5 6 2 4 6 3 1 3 4 2 1 3 4 1 2 4 6 5 2 2 3 2 1 2 2 1 4 2 4 5 7 3 1 2 6 4 1 3 5 6 2 1 2 3 4 5 7 6 1 2 3 4 5 6 3 1 3 6 2 2 1 2 5 2 2 5 3 2 3 5 4 2 3 4 5 5 1 2 3 4 5 Output 3 1 4 6 2 5 3 2 1 4 5 2 1 6 3 5 4 7 1 2 2 5 3 4 1
for _ in range(int(input())): n = int(input()) segs = [tuple(sorted(int(x) for x in input().split()[1:])) for __ in range(n - 1)] segs_having = [[j for j, seg in enumerate(segs) if i in seg] for i in range(n + 1)] segs_set = set(segs) for first in range(1, n + 1): try: segs_copy = [set(seg) for seg in segs] ans = [first] while len(ans) < n: for seg in segs_having[ans[-1]]: segs_copy[seg].remove(ans[-1]) ans.append(next(iter(next(seg for seg in segs_copy if len(seg) == 1)))) if all( any(tuple(sorted(ans[j:i])) in segs_set for j in range(0, i - 1)) for i in range(2, n + 1) ): print(*ans) break except StopIteration: pass
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST VAR WHILE FUNC_CALL VAR VAR VAR FOR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Gru has a string $S$ of length $N$, consisting of only characters $a$ and $b$ for banana and $P$ points to spend. Now Gru wants to replace and/or re-arrange characters of this given string to get the lexicographically smallest string possible. For this, he can perform the following two operations any number of times. 1) Swap any two characters in the string. This operation costs $1$ $point$. (any two, need not be adjacent) 2) Replace a character in the string with any other lower case english letter. This operation costs $2$ $points$. Help Gru in obtaining the lexicographically smallest string possible, by using at most $P$ points. -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains two lines of input, first-line containing two integers $N$ , $P$. - The second line contains a string $S$ consisting of $N$ characters. -----Output:----- For each testcase, output in a single containing the lexicographically smallest string obtained. -----Constraints----- - $1 \leq T \leq 10$ - $1 \leq N \leq 10^5$ - $0 \leq P \leq 2N$ - $S$ only consists of $'a'$ and $'b'$ -----Sample Input:----- 1 3 3 bba -----Sample Output:----- aab -----Explanation:----- We swap $S[0]$ and $S[2]$, to get $abb$. With the 2 remaining points, we replace $S[1]$ to obtain $aab$ which is the lexicographically smallest string possible for this case.
for _ in range(int(input())): n, p = map(int, input().split()) s = list(input()) a = s.count("a") b = s.count("b") swap = 0 arr = [i for i in s] for i in range(a): if s[i] == "b": swap += 1 if p <= swap: i = 0 tmp = p while p > 0 and i < n: if arr[i] == "b": arr[i] = "a" p -= 1 i += 1 p = tmp i = n - 1 while p > 0 and i > 0: if arr[i] == "a": arr[i] = "b" p -= 1 i -= 1 print("".join(arr)) else: for j in range(n): if j < a: arr[j] = "a" else: arr[j] = "b" p -= swap for k in range(n): if arr[k] == "b": if s[k] == "b" and p >= 2: p -= 2 arr[k] = "a" if s[k] == "a" and p >= 1: p -= 1 arr[k] = "a" print("".join(arr))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR VAR STRING IF VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Gru has a string $S$ of length $N$, consisting of only characters $a$ and $b$ for banana and $P$ points to spend. Now Gru wants to replace and/or re-arrange characters of this given string to get the lexicographically smallest string possible. For this, he can perform the following two operations any number of times. 1) Swap any two characters in the string. This operation costs $1$ $point$. (any two, need not be adjacent) 2) Replace a character in the string with any other lower case english letter. This operation costs $2$ $points$. Help Gru in obtaining the lexicographically smallest string possible, by using at most $P$ points. -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains two lines of input, first-line containing two integers $N$ , $P$. - The second line contains a string $S$ consisting of $N$ characters. -----Output:----- For each testcase, output in a single containing the lexicographically smallest string obtained. -----Constraints----- - $1 \leq T \leq 10$ - $1 \leq N \leq 10^5$ - $0 \leq P \leq 2N$ - $S$ only consists of $'a'$ and $'b'$ -----Sample Input:----- 1 3 3 bba -----Sample Output:----- aab -----Explanation:----- We swap $S[0]$ and $S[2]$, to get $abb$. With the 2 remaining points, we replace $S[1]$ to obtain $aab$ which is the lexicographically smallest string possible for this case.
def __starting_point(): t = int(input()) for _ in range(t): n, p = input().split() n, p = int(n), int(p) s = input() a, b = 0, 0 arr = [0] * n for i in range(n): arr[i] = s[i] for c in s: if c == "a": a += 1 else: b += 1 swap = 0 for i in range(a): if s[i] == "b": swap += 1 tmpp = p if p <= swap: for i in range(n): if p == 0: break if arr[i] == "b": arr[i] = "a" p -= 1 p = tmpp for i in range(n - 1, -1, -1): if p == 0: break if arr[i] == "a": arr[i] = "b" p -= 1 for c in arr: print(c, end="") print() else: for i in range(n): if i < a: arr[i] = "a" else: arr[i] = "b" p -= swap for i in range(n): if arr[i] == "b": if s[i] == "b" and p >= 2: p -= 2 arr[i] = "a" if s[i] == "a" and p >= 1: p -= 1 arr[i] = "a" for c in arr: print(c, end="") print() __starting_point()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR VAR STRING IF VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
You found a useless array a of 2n positive integers. You have realized that you actually don't need this array, so you decided to throw out all elements of a. It could have been an easy task, but it turned out that you should follow some rules: 1. In the beginning, you select any positive integer x. 2. Then you do the following operation n times: * select two elements of array with sum equals x; * remove them from a and replace x with maximum of that two numbers. For example, if initially a = [3, 5, 1, 2], you can select x = 6. Then you can select the second and the third elements of a with sum 5 + 1 = 6 and throw them out. After this operation, x equals 5 and there are two elements in array: 3 and 2. You can throw them out on the next operation. Note, that you choose x before the start and can't change it as you want between the operations. Determine how should you behave to throw out all elements of a. Input The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The first line of each test case contains the single integer n (1 ≀ n ≀ 1000). The second line of each test case contains 2n integers a_1, a_2, ..., a_{2n} (1 ≀ a_i ≀ 10^6) β€” the initial array a. It is guaranteed that the total sum of n over all test cases doesn't exceed 1000. Output For each test case in the first line print YES if it is possible to throw out all elements of the array and NO otherwise. If it is possible to throw out all elements, print the initial value of x you've chosen. Print description of n operations next. For each operation, print the pair of integers you remove. Example Input 4 2 3 5 1 2 3 1 1 8 8 64 64 2 1 1 2 4 5 1 2 3 4 5 6 7 14 3 11 Output YES 6 1 5 2 3 NO NO YES 21 14 7 3 11 5 6 2 4 3 1 Note The first test case was described in the statement. In the second and third test cases, we can show that it is impossible to throw out all elements of array a.
def f(first, second, a, steps): a.remove(second) ok = True sum = first while len(a) > 1: x = a.pop() y = sum - x if y in a: a.remove(y) sum = x else: ok = False break steps += [[x, y]] return ok for _ in range(int(input())): n = int(input()) a = sorted([int(x) for x in input().split()]) first = a.pop() d = dict() for x in a: if x in d: d[x] += 1 else: d[x] = 1 k = sorted(d.keys()) while len(k) > 0: second = k.pop() steps = [[first, second]] initialx = first + second ok = f(first, second, a.copy(), steps) if ok: break if ok: print("YES") print(initialx) [print(x[0], x[1]) for x in steps] else: print("NO")
FUNC_DEF EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR LIST LIST VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST LIST VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING
You found a useless array a of 2n positive integers. You have realized that you actually don't need this array, so you decided to throw out all elements of a. It could have been an easy task, but it turned out that you should follow some rules: 1. In the beginning, you select any positive integer x. 2. Then you do the following operation n times: * select two elements of array with sum equals x; * remove them from a and replace x with maximum of that two numbers. For example, if initially a = [3, 5, 1, 2], you can select x = 6. Then you can select the second and the third elements of a with sum 5 + 1 = 6 and throw them out. After this operation, x equals 5 and there are two elements in array: 3 and 2. You can throw them out on the next operation. Note, that you choose x before the start and can't change it as you want between the operations. Determine how should you behave to throw out all elements of a. Input The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The first line of each test case contains the single integer n (1 ≀ n ≀ 1000). The second line of each test case contains 2n integers a_1, a_2, ..., a_{2n} (1 ≀ a_i ≀ 10^6) β€” the initial array a. It is guaranteed that the total sum of n over all test cases doesn't exceed 1000. Output For each test case in the first line print YES if it is possible to throw out all elements of the array and NO otherwise. If it is possible to throw out all elements, print the initial value of x you've chosen. Print description of n operations next. For each operation, print the pair of integers you remove. Example Input 4 2 3 5 1 2 3 1 1 8 8 64 64 2 1 1 2 4 5 1 2 3 4 5 6 7 14 3 11 Output YES 6 1 5 2 3 NO NO YES 21 14 7 3 11 5 6 2 4 3 1 Note The first test case was described in the statement. In the second and third test cases, we can show that it is impossible to throw out all elements of array a.
import sys input = sys.stdin.readline for _ in range(int(input().strip())): q = int(input().strip()) a = list(map(int, input().strip().split(" "))) a.sort() sa = {} for i in a: if i not in sa: sa[i] = 0 sa[i] += 1 movs = [] for i in a[:-1]: sa[i] -= 1 movs.append(i) sa[a[-1]] -= 1 movs.append(a[-1]) cl = len(a) - 2 while cl > -1: if sa[a[cl]] != 0: if movs[-1] - a[cl] in sa and sa[movs[-1] - a[cl]] > 0: sa[movs[-1] - a[cl]] -= 1 movs.append(movs[-1] - a[cl]) sa[a[cl]] -= 1 movs.append(a[cl]) else: while movs: sa[movs.pop()] += 1 break else: cl -= 1 if movs: break else: print("NO") continue print("YES") print(movs[0] + movs[1]) p = 0 for i in movs: if p: print(i, p) p = 0 else: p = i
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR WHILE VAR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR
You found a useless array a of 2n positive integers. You have realized that you actually don't need this array, so you decided to throw out all elements of a. It could have been an easy task, but it turned out that you should follow some rules: 1. In the beginning, you select any positive integer x. 2. Then you do the following operation n times: * select two elements of array with sum equals x; * remove them from a and replace x with maximum of that two numbers. For example, if initially a = [3, 5, 1, 2], you can select x = 6. Then you can select the second and the third elements of a with sum 5 + 1 = 6 and throw them out. After this operation, x equals 5 and there are two elements in array: 3 and 2. You can throw them out on the next operation. Note, that you choose x before the start and can't change it as you want between the operations. Determine how should you behave to throw out all elements of a. Input The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The first line of each test case contains the single integer n (1 ≀ n ≀ 1000). The second line of each test case contains 2n integers a_1, a_2, ..., a_{2n} (1 ≀ a_i ≀ 10^6) β€” the initial array a. It is guaranteed that the total sum of n over all test cases doesn't exceed 1000. Output For each test case in the first line print YES if it is possible to throw out all elements of the array and NO otherwise. If it is possible to throw out all elements, print the initial value of x you've chosen. Print description of n operations next. For each operation, print the pair of integers you remove. Example Input 4 2 3 5 1 2 3 1 1 8 8 64 64 2 1 1 2 4 5 1 2 3 4 5 6 7 14 3 11 Output YES 6 1 5 2 3 NO NO YES 21 14 7 3 11 5 6 2 4 3 1 Note The first test case was described in the statement. In the second and third test cases, we can show that it is impossible to throw out all elements of array a.
def ints(): return list(map(int, input().split())) def go(): (n,) = ints() a = ints() ans = solve(n, a) if not ans: print("NO") else: print("YES") print(sum(ans[0])) for i, j in ans: print(i, j) def solve(n, a): a.sort() indices = dict() for i in range(2 * n - 1, -1, -1): indices[a[i]] = i used = [False] * (2 * n) res_y = [-1] * n res_z = [-1] * n for ai in sorted(set(a[:-1])): for j in range(2 * n): used[j] = False ans = simulate(n, a, ai + a[-1], indices, used, res_y, res_z) if ans: return ans def simulate(n, a, x, indices, used, res_y, res_z): yi = 2 * n - 1 for t in range(n): while yi >= 0 and used[yi]: yi -= 1 if yi < 0: return None used[yi] = True y = a[yi] res_y[t] = y z = x - y if z not in indices: return None i = indices[z] while i < 2 * n and used[i]: i += 1 if i >= 2 * n or a[i] != z: return None used[i] = True res_z[t] = z x = max(y, z) return list(zip(res_y, res_z)) (num_cases,) = ints() for case_num in range(num_cases): go()
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR IF VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER RETURN NONE ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR RETURN NONE ASSIGN VAR VAR VAR WHILE VAR BIN_OP NUMBER VAR VAR VAR VAR NUMBER IF VAR BIN_OP NUMBER VAR VAR VAR VAR RETURN NONE ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You found a useless array a of 2n positive integers. You have realized that you actually don't need this array, so you decided to throw out all elements of a. It could have been an easy task, but it turned out that you should follow some rules: 1. In the beginning, you select any positive integer x. 2. Then you do the following operation n times: * select two elements of array with sum equals x; * remove them from a and replace x with maximum of that two numbers. For example, if initially a = [3, 5, 1, 2], you can select x = 6. Then you can select the second and the third elements of a with sum 5 + 1 = 6 and throw them out. After this operation, x equals 5 and there are two elements in array: 3 and 2. You can throw them out on the next operation. Note, that you choose x before the start and can't change it as you want between the operations. Determine how should you behave to throw out all elements of a. Input The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The first line of each test case contains the single integer n (1 ≀ n ≀ 1000). The second line of each test case contains 2n integers a_1, a_2, ..., a_{2n} (1 ≀ a_i ≀ 10^6) β€” the initial array a. It is guaranteed that the total sum of n over all test cases doesn't exceed 1000. Output For each test case in the first line print YES if it is possible to throw out all elements of the array and NO otherwise. If it is possible to throw out all elements, print the initial value of x you've chosen. Print description of n operations next. For each operation, print the pair of integers you remove. Example Input 4 2 3 5 1 2 3 1 1 8 8 64 64 2 1 1 2 4 5 1 2 3 4 5 6 7 14 3 11 Output YES 6 1 5 2 3 NO NO YES 21 14 7 3 11 5 6 2 4 3 1 Note The first test case was described in the statement. In the second and third test cases, we can show that it is impossible to throw out all elements of array a.
def ch(a, p): s = {} for j in range(2 * n): s[a[j]] = s.get(a[j], 0) + 1 s[p] -= 1 s[a[0]] -= 1 an = a[0] ans = [[a[0], p]] for i in range(1, 2 * n): if s[a[i]] > 0: l = an - a[i] s[a[i]] -= 1 if l in s.keys() and s[l] > 0: s[l] -= 1 an = max(l, a[i]) ans.append([l, a[i]]) else: return [False, ans] return [True, ans] for i in range(int(input())): n = int(input()) a = list(map(int, input().split())) s = set() a.sort(reverse=True) flag = 0 for i in range(1, 2 * n): ss = ch(a, a[i]) if ss[0] == True: l = ss[1] print("YES") print(l[0][0] + l[0][1]) for j in l: print(*j) flag = -1 break if flag == 0: print("NO")
FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST LIST VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR RETURN LIST NUMBER VAR RETURN LIST NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING
You found a useless array a of 2n positive integers. You have realized that you actually don't need this array, so you decided to throw out all elements of a. It could have been an easy task, but it turned out that you should follow some rules: 1. In the beginning, you select any positive integer x. 2. Then you do the following operation n times: * select two elements of array with sum equals x; * remove them from a and replace x with maximum of that two numbers. For example, if initially a = [3, 5, 1, 2], you can select x = 6. Then you can select the second and the third elements of a with sum 5 + 1 = 6 and throw them out. After this operation, x equals 5 and there are two elements in array: 3 and 2. You can throw them out on the next operation. Note, that you choose x before the start and can't change it as you want between the operations. Determine how should you behave to throw out all elements of a. Input The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The first line of each test case contains the single integer n (1 ≀ n ≀ 1000). The second line of each test case contains 2n integers a_1, a_2, ..., a_{2n} (1 ≀ a_i ≀ 10^6) β€” the initial array a. It is guaranteed that the total sum of n over all test cases doesn't exceed 1000. Output For each test case in the first line print YES if it is possible to throw out all elements of the array and NO otherwise. If it is possible to throw out all elements, print the initial value of x you've chosen. Print description of n operations next. For each operation, print the pair of integers you remove. Example Input 4 2 3 5 1 2 3 1 1 8 8 64 64 2 1 1 2 4 5 1 2 3 4 5 6 7 14 3 11 Output YES 6 1 5 2 3 NO NO YES 21 14 7 3 11 5 6 2 4 3 1 Note The first test case was described in the statement. In the second and third test cases, we can show that it is impossible to throw out all elements of array a.
def f(a, cur, ress): g = {} ress.clear() for i in range(len(a)): if a[i] not in g: g[a[i]] = [i] else: g[a[i]].append(i) for v in a: if not v in g: continue g[v].pop() if len(g[v]) == 0: del g[v] vv = cur - v if vv not in g: return False ress.append([v, vv]) g[vv].pop() if not g[vv]: del g[vv] cur = v return True for _ in range(int(input())): n = int(input()) * 2 a = sorted([*map(int, input().split())], reverse=True) res = [] for i in range(1, n): if f(a[1:i] + a[i + 1 : n], a[0], res): res = [[a[0], a[i]]] + res print("YES") print(a[0] + a[i]) for x, y in res: print(x, y) break else: print("NO")
FUNC_DEF ASSIGN VAR DICT EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR BIN_OP LIST LIST VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING
You found a useless array a of 2n positive integers. You have realized that you actually don't need this array, so you decided to throw out all elements of a. It could have been an easy task, but it turned out that you should follow some rules: 1. In the beginning, you select any positive integer x. 2. Then you do the following operation n times: * select two elements of array with sum equals x; * remove them from a and replace x with maximum of that two numbers. For example, if initially a = [3, 5, 1, 2], you can select x = 6. Then you can select the second and the third elements of a with sum 5 + 1 = 6 and throw them out. After this operation, x equals 5 and there are two elements in array: 3 and 2. You can throw them out on the next operation. Note, that you choose x before the start and can't change it as you want between the operations. Determine how should you behave to throw out all elements of a. Input The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The first line of each test case contains the single integer n (1 ≀ n ≀ 1000). The second line of each test case contains 2n integers a_1, a_2, ..., a_{2n} (1 ≀ a_i ≀ 10^6) β€” the initial array a. It is guaranteed that the total sum of n over all test cases doesn't exceed 1000. Output For each test case in the first line print YES if it is possible to throw out all elements of the array and NO otherwise. If it is possible to throw out all elements, print the initial value of x you've chosen. Print description of n operations next. For each operation, print the pair of integers you remove. Example Input 4 2 3 5 1 2 3 1 1 8 8 64 64 2 1 1 2 4 5 1 2 3 4 5 6 7 14 3 11 Output YES 6 1 5 2 3 NO NO YES 21 14 7 3 11 5 6 2 4 3 1 Note The first test case was described in the statement. In the second and third test cases, we can show that it is impossible to throw out all elements of array a.
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) a = sorted(a, reverse=True) d = {} for i in range(len(a)): if i == 0 or a[i] != a[i - 1]: d[a[i]] = 0 d[a[i]] += 1 d[a[0]] -= 1 f = False for i in range(1, len(a)): chosen = a[i] d1 = {} for j in d: d1[j] = d[j] d1[chosen] -= 1 pre = a[0] flag = True ans = "YES\n" + str(chosen + a[0]) + "\n" + str(a[0]) + " " + str(chosen) + "\n" for i in range(len(a)): if d1[a[i]] == 0: continue d1[a[i]] -= 1 temp = pre - a[i] if not temp in d1 or d1[temp] == 0: flag = False break ans += str(a[i]) + " " + str(temp) + "\n" d1[temp] -= 1 pre = a[i] if flag: print(ans, end="") f = True break if not f: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP STRING FUNC_CALL VAR BIN_OP VAR VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER STRING FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR STRING FUNC_CALL VAR VAR STRING VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING
You found a useless array a of 2n positive integers. You have realized that you actually don't need this array, so you decided to throw out all elements of a. It could have been an easy task, but it turned out that you should follow some rules: 1. In the beginning, you select any positive integer x. 2. Then you do the following operation n times: * select two elements of array with sum equals x; * remove them from a and replace x with maximum of that two numbers. For example, if initially a = [3, 5, 1, 2], you can select x = 6. Then you can select the second and the third elements of a with sum 5 + 1 = 6 and throw them out. After this operation, x equals 5 and there are two elements in array: 3 and 2. You can throw them out on the next operation. Note, that you choose x before the start and can't change it as you want between the operations. Determine how should you behave to throw out all elements of a. Input The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The first line of each test case contains the single integer n (1 ≀ n ≀ 1000). The second line of each test case contains 2n integers a_1, a_2, ..., a_{2n} (1 ≀ a_i ≀ 10^6) β€” the initial array a. It is guaranteed that the total sum of n over all test cases doesn't exceed 1000. Output For each test case in the first line print YES if it is possible to throw out all elements of the array and NO otherwise. If it is possible to throw out all elements, print the initial value of x you've chosen. Print description of n operations next. For each operation, print the pair of integers you remove. Example Input 4 2 3 5 1 2 3 1 1 8 8 64 64 2 1 1 2 4 5 1 2 3 4 5 6 7 14 3 11 Output YES 6 1 5 2 3 NO NO YES 21 14 7 3 11 5 6 2 4 3 1 Note The first test case was described in the statement. In the second and third test cases, we can show that it is impossible to throw out all elements of array a.
t = int(input()) for _ in range(t): n = int(input()) a = [int(d) for d in input().split()] a.sort() done = 0 main = 0 for j in range(len(a) - 2, -1, -1): l = [] count = {} for i in a: if i in count: count[i] += 1 else: count[i] = 1 curr_sum = a[j] + a[len(a) - 1] main = curr_sum gone = 2 l.append([a[j], a[len(a) - 1]]) count[a[j]] -= 1 count[a[len(a) - 1]] -= 1 curr_sum = max(a[j], a[len(a) - 1]) if j != len(a) - 2: k = len(a) - 2 else: k = len(a) - 3 res = 1 while gone < 2 * n and res == 1: if 0 < count[a[k]]: need = curr_sum - a[k] count[a[k]] -= 1 if need in count: if count[need] == 0: res = 0 else: count[need] -= 1 l.append([a[k], need]) curr_sum = max(a[k], need) gone += 2 k = k - 1 else: res = 0 else: k = k - 1 if res == 1: print("YES") done = 1 break if done == 1: print(main) for i in l: print(i[0], i[1]) else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP NUMBER VAR VAR NUMBER IF NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING
You found a useless array a of 2n positive integers. You have realized that you actually don't need this array, so you decided to throw out all elements of a. It could have been an easy task, but it turned out that you should follow some rules: 1. In the beginning, you select any positive integer x. 2. Then you do the following operation n times: * select two elements of array with sum equals x; * remove them from a and replace x with maximum of that two numbers. For example, if initially a = [3, 5, 1, 2], you can select x = 6. Then you can select the second and the third elements of a with sum 5 + 1 = 6 and throw them out. After this operation, x equals 5 and there are two elements in array: 3 and 2. You can throw them out on the next operation. Note, that you choose x before the start and can't change it as you want between the operations. Determine how should you behave to throw out all elements of a. Input The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The first line of each test case contains the single integer n (1 ≀ n ≀ 1000). The second line of each test case contains 2n integers a_1, a_2, ..., a_{2n} (1 ≀ a_i ≀ 10^6) β€” the initial array a. It is guaranteed that the total sum of n over all test cases doesn't exceed 1000. Output For each test case in the first line print YES if it is possible to throw out all elements of the array and NO otherwise. If it is possible to throw out all elements, print the initial value of x you've chosen. Print description of n operations next. For each operation, print the pair of integers you remove. Example Input 4 2 3 5 1 2 3 1 1 8 8 64 64 2 1 1 2 4 5 1 2 3 4 5 6 7 14 3 11 Output YES 6 1 5 2 3 NO NO YES 21 14 7 3 11 5 6 2 4 3 1 Note The first test case was described in the statement. In the second and third test cases, we can show that it is impossible to throw out all elements of array a.
from sys import stdin input = stdin.readline q = int(input()) for _ in range(q): n = int(input()) a = list(map(int, input().split())) a.sort() l = a[-1] a.pop() for i in range(2 * n - 1): d = {} for ii in range(2 * n - 1): if ii == i: continue try: d[a[ii]].append(ii) except: d[a[ii]] = [ii] done = True ans = [] ans.append([l, a[i]]) x = l + a[i] bar = [(False) for i in range(2 * n - 1)] bar[i] = True curr = l for j in range(2 * n - 2, -1, -1): if bar[j] == True: continue bar[j] = True d[a[j]].pop() if curr - a[j] not in d: done = False break if len(d[curr - a[j]]) == 0: del d[curr - a[j]] done = False break temp = d[curr - a[j]].pop() bar[temp] = True ans.append([a[j], curr - a[j]]) done = True curr = a[j] if done == True: print("YES") print(x) for i in ans: print(*i) break if done == False: print("NO")
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR ASSIGN VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING
You found a useless array a of 2n positive integers. You have realized that you actually don't need this array, so you decided to throw out all elements of a. It could have been an easy task, but it turned out that you should follow some rules: 1. In the beginning, you select any positive integer x. 2. Then you do the following operation n times: * select two elements of array with sum equals x; * remove them from a and replace x with maximum of that two numbers. For example, if initially a = [3, 5, 1, 2], you can select x = 6. Then you can select the second and the third elements of a with sum 5 + 1 = 6 and throw them out. After this operation, x equals 5 and there are two elements in array: 3 and 2. You can throw them out on the next operation. Note, that you choose x before the start and can't change it as you want between the operations. Determine how should you behave to throw out all elements of a. Input The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The first line of each test case contains the single integer n (1 ≀ n ≀ 1000). The second line of each test case contains 2n integers a_1, a_2, ..., a_{2n} (1 ≀ a_i ≀ 10^6) β€” the initial array a. It is guaranteed that the total sum of n over all test cases doesn't exceed 1000. Output For each test case in the first line print YES if it is possible to throw out all elements of the array and NO otherwise. If it is possible to throw out all elements, print the initial value of x you've chosen. Print description of n operations next. For each operation, print the pair of integers you remove. Example Input 4 2 3 5 1 2 3 1 1 8 8 64 64 2 1 1 2 4 5 1 2 3 4 5 6 7 14 3 11 Output YES 6 1 5 2 3 NO NO YES 21 14 7 3 11 5 6 2 4 3 1 Note The first test case was described in the statement. In the second and third test cases, we can show that it is impossible to throw out all elements of array a.
import sys T = int(sys.stdin.readline().strip()) while T > 0: T -= 1 n = int(sys.stdin.readline().strip()) a = sys.stdin.readline().strip().split(" ") m = -1 cnt = {} for one in a: x = int(one) cnt[x] = cnt.get(x, 0) + 1 ml = sorted(list(cnt.keys()), reverse=True) cnt1 = cnt.copy() Flag = False for m1 in cnt1: cnt = cnt1.copy() now = 0 m = ml[now] rl = [[m, m1]] cnt[m] -= 1 if cnt[m] == 0: del cnt[m] if m1 not in cnt: continue cnt[m1] -= 1 if cnt[m1] == 0: del cnt[m1] for i in range(n - 1): while cnt.get(ml[now], 0) == 0: now += 1 x = ml[now] cnt[x] -= 1 if cnt[x] == 0: del cnt[x] y = rl[-1][0] - x if y not in cnt: break cnt[y] -= 1 if cnt[y] == 0: del cnt[y] rl.append([x, y]) if len(cnt) != 0: continue Flag = True print("YES") print(rl[0][1] + rl[0][0]) for one in rl: print(one[0], one[1]) break if not Flag: print("NO")
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR LIST LIST VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING
You found a useless array a of 2n positive integers. You have realized that you actually don't need this array, so you decided to throw out all elements of a. It could have been an easy task, but it turned out that you should follow some rules: 1. In the beginning, you select any positive integer x. 2. Then you do the following operation n times: * select two elements of array with sum equals x; * remove them from a and replace x with maximum of that two numbers. For example, if initially a = [3, 5, 1, 2], you can select x = 6. Then you can select the second and the third elements of a with sum 5 + 1 = 6 and throw them out. After this operation, x equals 5 and there are two elements in array: 3 and 2. You can throw them out on the next operation. Note, that you choose x before the start and can't change it as you want between the operations. Determine how should you behave to throw out all elements of a. Input The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The first line of each test case contains the single integer n (1 ≀ n ≀ 1000). The second line of each test case contains 2n integers a_1, a_2, ..., a_{2n} (1 ≀ a_i ≀ 10^6) β€” the initial array a. It is guaranteed that the total sum of n over all test cases doesn't exceed 1000. Output For each test case in the first line print YES if it is possible to throw out all elements of the array and NO otherwise. If it is possible to throw out all elements, print the initial value of x you've chosen. Print description of n operations next. For each operation, print the pair of integers you remove. Example Input 4 2 3 5 1 2 3 1 1 8 8 64 64 2 1 1 2 4 5 1 2 3 4 5 6 7 14 3 11 Output YES 6 1 5 2 3 NO NO YES 21 14 7 3 11 5 6 2 4 3 1 Note The first test case was described in the statement. In the second and third test cases, we can show that it is impossible to throw out all elements of array a.
from sys import stdin t = int(stdin.readline()) for _ in range(t): n = int(stdin.readline()) arr = sorted(list(map(int, stdin.readline().split())))[::-1] dct = {} flag = True for i in arr: if i not in dct: dct[i] = 1 else: dct[i] += 1 for i in arr[1:]: cnt = 0 so = i + arr[0] dict2 = dct.copy() lst = [int(so)] for j in arr: if dict2[j] == 0: continue dict2[j] -= 1 if so - j not in dict2 or dict2[so - j] == 0: break lst.append(str(j) + " " + str(so - j)) dict2[so - j] -= 1 so = j if len(lst) == n + 1: flag = False break if flag: print("no") else: print("yes") for i in lst: print(i)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR
You found a useless array a of 2n positive integers. You have realized that you actually don't need this array, so you decided to throw out all elements of a. It could have been an easy task, but it turned out that you should follow some rules: 1. In the beginning, you select any positive integer x. 2. Then you do the following operation n times: * select two elements of array with sum equals x; * remove them from a and replace x with maximum of that two numbers. For example, if initially a = [3, 5, 1, 2], you can select x = 6. Then you can select the second and the third elements of a with sum 5 + 1 = 6 and throw them out. After this operation, x equals 5 and there are two elements in array: 3 and 2. You can throw them out on the next operation. Note, that you choose x before the start and can't change it as you want between the operations. Determine how should you behave to throw out all elements of a. Input The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The first line of each test case contains the single integer n (1 ≀ n ≀ 1000). The second line of each test case contains 2n integers a_1, a_2, ..., a_{2n} (1 ≀ a_i ≀ 10^6) β€” the initial array a. It is guaranteed that the total sum of n over all test cases doesn't exceed 1000. Output For each test case in the first line print YES if it is possible to throw out all elements of the array and NO otherwise. If it is possible to throw out all elements, print the initial value of x you've chosen. Print description of n operations next. For each operation, print the pair of integers you remove. Example Input 4 2 3 5 1 2 3 1 1 8 8 64 64 2 1 1 2 4 5 1 2 3 4 5 6 7 14 3 11 Output YES 6 1 5 2 3 NO NO YES 21 14 7 3 11 5 6 2 4 3 1 Note The first test case was described in the statement. In the second and third test cases, we can show that it is impossible to throw out all elements of array a.
import sys input = sys.stdin.readline for _ in range(int(input())): n = int(input()) A = sorted(map(int, input().split()), reverse=True) for k in range(1, 2 * n): used = [0] * (2 * n) used[0] = used[k] = 1 cur = A[0] ans = [[A[0], A[k]]] s = A[0] + A[k] def search(x): left, right = 0, 2 * n - 1 while left < right: mid = left + (right - left) // 2 if A[mid] > x or A[mid] == x and used[mid]: left = mid + 1 else: right = mid return left for i in range(1, 2 * n): if used[i]: continue used[i] = 1 target = cur - A[i] j = search(target) if used[j] or A[j] != target: break used[j] = 1 ans.append([A[i], A[j]]) cur = A[i] else: print("YES") print(s) for a in ans: print(*a) break else: print("NO")
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR ASSIGN VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST LIST VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
You found a useless array a of 2n positive integers. You have realized that you actually don't need this array, so you decided to throw out all elements of a. It could have been an easy task, but it turned out that you should follow some rules: 1. In the beginning, you select any positive integer x. 2. Then you do the following operation n times: * select two elements of array with sum equals x; * remove them from a and replace x with maximum of that two numbers. For example, if initially a = [3, 5, 1, 2], you can select x = 6. Then you can select the second and the third elements of a with sum 5 + 1 = 6 and throw them out. After this operation, x equals 5 and there are two elements in array: 3 and 2. You can throw them out on the next operation. Note, that you choose x before the start and can't change it as you want between the operations. Determine how should you behave to throw out all elements of a. Input The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The first line of each test case contains the single integer n (1 ≀ n ≀ 1000). The second line of each test case contains 2n integers a_1, a_2, ..., a_{2n} (1 ≀ a_i ≀ 10^6) β€” the initial array a. It is guaranteed that the total sum of n over all test cases doesn't exceed 1000. Output For each test case in the first line print YES if it is possible to throw out all elements of the array and NO otherwise. If it is possible to throw out all elements, print the initial value of x you've chosen. Print description of n operations next. For each operation, print the pair of integers you remove. Example Input 4 2 3 5 1 2 3 1 1 8 8 64 64 2 1 1 2 4 5 1 2 3 4 5 6 7 14 3 11 Output YES 6 1 5 2 3 NO NO YES 21 14 7 3 11 5 6 2 4 3 1 Note The first test case was described in the statement. In the second and third test cases, we can show that it is impossible to throw out all elements of array a.
t = int(input()) outl = [] revo = [-1] * (10**6 + 1) rev = [-1] * (10**6 + 1) for _ in range(t): n = 2 * int(input()) l = list(map(int, input().split())) l.sort(reverse=True) for i in range(n - 1, -1, -1): revo[l[i]] = i def rem(v): if v < 0: return False if rev[v] == -1: return False if used[rev[v]]: return False used[rev[v]] = True if rev[v] + 1 != n and l[rev[v] + 1] == v: rev[v] += 1 return True for i in range(n): if i == 0: continue used = [False] * n out = [(l[0], l[i])] for v in l: rev[v] = revo[v] rem(l[0]) rem(l[i]) works = True curr = l[0] for j in range(n): if not used[j]: rem(l[j]) if rem(curr - l[j]): out.append((l[j], curr - l[j])) curr = l[j] else: works = False break if works: outl.append("YES") outl.append(str(l[0] + l[i])) for u, v in out: outl.append(f"{u} {v}") break else: outl.append("NO") for v in l: revo[v] = -1 rev[v] = -1 print("\n".join(outl))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST VAR NUMBER VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING VAR EXPR FUNC_CALL VAR STRING FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
You found a useless array a of 2n positive integers. You have realized that you actually don't need this array, so you decided to throw out all elements of a. It could have been an easy task, but it turned out that you should follow some rules: 1. In the beginning, you select any positive integer x. 2. Then you do the following operation n times: * select two elements of array with sum equals x; * remove them from a and replace x with maximum of that two numbers. For example, if initially a = [3, 5, 1, 2], you can select x = 6. Then you can select the second and the third elements of a with sum 5 + 1 = 6 and throw them out. After this operation, x equals 5 and there are two elements in array: 3 and 2. You can throw them out on the next operation. Note, that you choose x before the start and can't change it as you want between the operations. Determine how should you behave to throw out all elements of a. Input The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The first line of each test case contains the single integer n (1 ≀ n ≀ 1000). The second line of each test case contains 2n integers a_1, a_2, ..., a_{2n} (1 ≀ a_i ≀ 10^6) β€” the initial array a. It is guaranteed that the total sum of n over all test cases doesn't exceed 1000. Output For each test case in the first line print YES if it is possible to throw out all elements of the array and NO otherwise. If it is possible to throw out all elements, print the initial value of x you've chosen. Print description of n operations next. For each operation, print the pair of integers you remove. Example Input 4 2 3 5 1 2 3 1 1 8 8 64 64 2 1 1 2 4 5 1 2 3 4 5 6 7 14 3 11 Output YES 6 1 5 2 3 NO NO YES 21 14 7 3 11 5 6 2 4 3 1 Note The first test case was described in the statement. In the second and third test cases, we can show that it is impossible to throw out all elements of array a.
import sys input = sys.stdin.buffer.readline a = int(input()) for x in range(a): b = int(input()) c = list(map(int, input().split())) c.sort(reverse=True) d = c[0] h = 0 l = [] sss = {} for y in range(2 * b): if sss.get(c[y]) == None: sss[c[y]] = 1 else: sss[c[y]] += 1 for y in range(1, 2 * b): ss = sss.copy() p = c.copy() l.append([c[y], c[0]]) ss[c[0]] -= 1 ss[c[y]] -= 1 n = d for z in range(1, 2 * b - 1): if z == y: continue elif ss.get(c[z]) == 0: continue elif ss.get(n - c[z]) != None and ss.get(n - c[z]) >= 1: if n - c[z] == c[z]: if ss.get(n - c[z]) >= 2: l.append([n - c[z], c[z]]) ss[c[z]] -= 1 ss[n - c[z]] -= 1 n = max(c[z], n - c[z]) else: h = -1 break else: l.append([n - c[z], c[z]]) ss[c[z]] -= 1 ss[n - c[z]] -= 1 n = max(n - c[z], c[z]) else: h = -1 break if len(l) == b: break else: l = [] h = 0 if len(l) == 0: print("NO") else: print("YES") print(sum(l[0])) for y in l: print(*y)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF FUNC_CALL VAR VAR VAR NONE ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR NUMBER VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR NONE FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR
You found a useless array a of 2n positive integers. You have realized that you actually don't need this array, so you decided to throw out all elements of a. It could have been an easy task, but it turned out that you should follow some rules: 1. In the beginning, you select any positive integer x. 2. Then you do the following operation n times: * select two elements of array with sum equals x; * remove them from a and replace x with maximum of that two numbers. For example, if initially a = [3, 5, 1, 2], you can select x = 6. Then you can select the second and the third elements of a with sum 5 + 1 = 6 and throw them out. After this operation, x equals 5 and there are two elements in array: 3 and 2. You can throw them out on the next operation. Note, that you choose x before the start and can't change it as you want between the operations. Determine how should you behave to throw out all elements of a. Input The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The first line of each test case contains the single integer n (1 ≀ n ≀ 1000). The second line of each test case contains 2n integers a_1, a_2, ..., a_{2n} (1 ≀ a_i ≀ 10^6) β€” the initial array a. It is guaranteed that the total sum of n over all test cases doesn't exceed 1000. Output For each test case in the first line print YES if it is possible to throw out all elements of the array and NO otherwise. If it is possible to throw out all elements, print the initial value of x you've chosen. Print description of n operations next. For each operation, print the pair of integers you remove. Example Input 4 2 3 5 1 2 3 1 1 8 8 64 64 2 1 1 2 4 5 1 2 3 4 5 6 7 14 3 11 Output YES 6 1 5 2 3 NO NO YES 21 14 7 3 11 5 6 2 4 3 1 Note The first test case was described in the statement. In the second and third test cases, we can show that it is impossible to throw out all elements of array a.
from sys import stdin t = int(stdin.readline()) for _ in range(t): n = int(stdin.readline()) a = list(map(int, stdin.readline().split())) a.sort() di = {} for i in a: if i not in di: di[i] = 1 else: di[i] += 1 flag = 0 for i in range(2 * n - 2, -1, -1): tmp = {} for j in di: tmp[j] = di[j] movs = [[a[2 * n - 1], a[i]]] tmp[a[i]] = tmp[a[i]] - 1 maxi = a[2 * n - 1] counter = 0 pair = 1 for j in range(2 * n - 2, -1, -1): if j != i and tmp[a[j]] > 0: diff = maxi - a[j] if ( diff in tmp and tmp[diff] > 0 and (diff != a[j] or diff == a[j] and tmp[diff] >= 2) ): pair += 1 movs.append([diff, a[j]]) tmp[diff] = tmp[diff] - 1 tmp[a[j]] = tmp[a[j]] - 1 maxi = a[j] elif pair != n: counter = -1 break if counter == -1: break if counter != -1: flag = 1 print("YES") print(sum(movs[0])) for j in movs: print(*j) break if flag == 0: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST LIST VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING
You found a useless array a of 2n positive integers. You have realized that you actually don't need this array, so you decided to throw out all elements of a. It could have been an easy task, but it turned out that you should follow some rules: 1. In the beginning, you select any positive integer x. 2. Then you do the following operation n times: * select two elements of array with sum equals x; * remove them from a and replace x with maximum of that two numbers. For example, if initially a = [3, 5, 1, 2], you can select x = 6. Then you can select the second and the third elements of a with sum 5 + 1 = 6 and throw them out. After this operation, x equals 5 and there are two elements in array: 3 and 2. You can throw them out on the next operation. Note, that you choose x before the start and can't change it as you want between the operations. Determine how should you behave to throw out all elements of a. Input The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The first line of each test case contains the single integer n (1 ≀ n ≀ 1000). The second line of each test case contains 2n integers a_1, a_2, ..., a_{2n} (1 ≀ a_i ≀ 10^6) β€” the initial array a. It is guaranteed that the total sum of n over all test cases doesn't exceed 1000. Output For each test case in the first line print YES if it is possible to throw out all elements of the array and NO otherwise. If it is possible to throw out all elements, print the initial value of x you've chosen. Print description of n operations next. For each operation, print the pair of integers you remove. Example Input 4 2 3 5 1 2 3 1 1 8 8 64 64 2 1 1 2 4 5 1 2 3 4 5 6 7 14 3 11 Output YES 6 1 5 2 3 NO NO YES 21 14 7 3 11 5 6 2 4 3 1 Note The first test case was described in the statement. In the second and third test cases, we can show that it is impossible to throw out all elements of array a.
import sys input = sys.stdin.buffer.readline def dictonary(): dicto = {} for i in range(2 * n): if a[i] in dicto: dicto[a[i]] += 1 else: dicto[a[i]] = 1 return dicto for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) a.sort(reverse=True) i = 1 ans = [(0) for i in range(2 * n + 3)] dicto = {} while i < 2 * n: ans = [(0) for i in range(2 * n + 1)] dicty = dictonary() dicty[a[0]] -= 1 dicty[a[i]] -= 1 ans[1] = a[0] ans[2] = a[i] k = 1 j = 1 while k < 2 * n: if dicty[a[k]] > 0: dicty[a[k]] -= 1 if ans[j] - a[k] in dicty and dicty[ans[j] - a[k]] > 0: ans[j + 2] = a[k] ans[j + 3] = ans[j] - a[k] dicty[ans[j] - a[k]] -= 1 j += 2 k += 1 if j == 2 * n - 1: break i += 1 if j == 2 * n - 1: print("YES") print(ans[1] + ans[2]) for i in range(1, 2 * n + 1, 2): print(ans[i], ans[i + 1]) else: print("NO")
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR DICT WHILE VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP NUMBER VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING
You found a useless array a of 2n positive integers. You have realized that you actually don't need this array, so you decided to throw out all elements of a. It could have been an easy task, but it turned out that you should follow some rules: 1. In the beginning, you select any positive integer x. 2. Then you do the following operation n times: * select two elements of array with sum equals x; * remove them from a and replace x with maximum of that two numbers. For example, if initially a = [3, 5, 1, 2], you can select x = 6. Then you can select the second and the third elements of a with sum 5 + 1 = 6 and throw them out. After this operation, x equals 5 and there are two elements in array: 3 and 2. You can throw them out on the next operation. Note, that you choose x before the start and can't change it as you want between the operations. Determine how should you behave to throw out all elements of a. Input The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The first line of each test case contains the single integer n (1 ≀ n ≀ 1000). The second line of each test case contains 2n integers a_1, a_2, ..., a_{2n} (1 ≀ a_i ≀ 10^6) β€” the initial array a. It is guaranteed that the total sum of n over all test cases doesn't exceed 1000. Output For each test case in the first line print YES if it is possible to throw out all elements of the array and NO otherwise. If it is possible to throw out all elements, print the initial value of x you've chosen. Print description of n operations next. For each operation, print the pair of integers you remove. Example Input 4 2 3 5 1 2 3 1 1 8 8 64 64 2 1 1 2 4 5 1 2 3 4 5 6 7 14 3 11 Output YES 6 1 5 2 3 NO NO YES 21 14 7 3 11 5 6 2 4 3 1 Note The first test case was described in the statement. In the second and third test cases, we can show that it is impossible to throw out all elements of array a.
def count(): for i in a: try: d[i] += 1 except: d[i] = 1 def solve(a): for i in a[: len(a) - 1]: val = d.copy() val[i] -= 1 v = [] j = len(a) - 1 while j > 1: s = a[j] if val[s]: val[s] -= 1 k = j - 1 while k >= 0 and val[a[k]] == 0: k -= 1 if k < 0: break try: if ( val[s - a[k]] > 0 and a[k] != s - a[k] or val[s - a[k]] > 1 and a[k] == s - a[k] ): val[s - a[k]] -= 1 v.append([s - a[k], a[k]]) else: break except: break j -= 1 if len(v) == n - 1: print("YES") print(a[-1] + i) print(i, a[-1]) for i in v: print(*i) return print("NO") for T in range(int(input())): n = int(input()) a = sorted(list(map(int, input().split()))) d = dict() count() solve(a)
FUNC_DEF FOR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_DEF FOR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
You found a useless array a of 2n positive integers. You have realized that you actually don't need this array, so you decided to throw out all elements of a. It could have been an easy task, but it turned out that you should follow some rules: 1. In the beginning, you select any positive integer x. 2. Then you do the following operation n times: * select two elements of array with sum equals x; * remove them from a and replace x with maximum of that two numbers. For example, if initially a = [3, 5, 1, 2], you can select x = 6. Then you can select the second and the third elements of a with sum 5 + 1 = 6 and throw them out. After this operation, x equals 5 and there are two elements in array: 3 and 2. You can throw them out on the next operation. Note, that you choose x before the start and can't change it as you want between the operations. Determine how should you behave to throw out all elements of a. Input The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The first line of each test case contains the single integer n (1 ≀ n ≀ 1000). The second line of each test case contains 2n integers a_1, a_2, ..., a_{2n} (1 ≀ a_i ≀ 10^6) β€” the initial array a. It is guaranteed that the total sum of n over all test cases doesn't exceed 1000. Output For each test case in the first line print YES if it is possible to throw out all elements of the array and NO otherwise. If it is possible to throw out all elements, print the initial value of x you've chosen. Print description of n operations next. For each operation, print the pair of integers you remove. Example Input 4 2 3 5 1 2 3 1 1 8 8 64 64 2 1 1 2 4 5 1 2 3 4 5 6 7 14 3 11 Output YES 6 1 5 2 3 NO NO YES 21 14 7 3 11 5 6 2 4 3 1 Note The first test case was described in the statement. In the second and third test cases, we can show that it is impossible to throw out all elements of array a.
def try_it(n, a, d, idx, pr=False): current = {a[-1]: 1, a[idx]: 1} mx = 2 * n - 2 if idx != 2 * n - 2 else 2 * n - 3 x = a[-1] if pr: print(x + a[idx]) print(a[-1], a[idx]) for i in range(n - 1): high = a[mx] if high in current: if d[high] <= current[high]: return False current[high] += 1 else: current[high] = 1 lower = x - high if lower in d: if lower in current: if d[lower] - current[lower] > 0: current[lower] += 1 else: return False else: current[lower] = 1 else: return False if pr: print(lower, high) x = high if i != n - 2: z = 1 while True: des = a[mx - z] if des not in current or d[des] > current[des]: mx -= z break z += 1 return True def main(): t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) a.sort() d = {} for i in range(2 * n): if a[i] in d: d[a[i]] += 1 else: d[a[i]] = 1 good = False for i in range(2 * n - 1): if try_it(n, a, d, i): good = True print("YES") try_it(n, a, d, i, True) break if not good: print("NO") main()
FUNC_DEF NUMBER ASSIGN VAR DICT VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR IF VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER RETURN NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR
You found a useless array a of 2n positive integers. You have realized that you actually don't need this array, so you decided to throw out all elements of a. It could have been an easy task, but it turned out that you should follow some rules: 1. In the beginning, you select any positive integer x. 2. Then you do the following operation n times: * select two elements of array with sum equals x; * remove them from a and replace x with maximum of that two numbers. For example, if initially a = [3, 5, 1, 2], you can select x = 6. Then you can select the second and the third elements of a with sum 5 + 1 = 6 and throw them out. After this operation, x equals 5 and there are two elements in array: 3 and 2. You can throw them out on the next operation. Note, that you choose x before the start and can't change it as you want between the operations. Determine how should you behave to throw out all elements of a. Input The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The first line of each test case contains the single integer n (1 ≀ n ≀ 1000). The second line of each test case contains 2n integers a_1, a_2, ..., a_{2n} (1 ≀ a_i ≀ 10^6) β€” the initial array a. It is guaranteed that the total sum of n over all test cases doesn't exceed 1000. Output For each test case in the first line print YES if it is possible to throw out all elements of the array and NO otherwise. If it is possible to throw out all elements, print the initial value of x you've chosen. Print description of n operations next. For each operation, print the pair of integers you remove. Example Input 4 2 3 5 1 2 3 1 1 8 8 64 64 2 1 1 2 4 5 1 2 3 4 5 6 7 14 3 11 Output YES 6 1 5 2 3 NO NO YES 21 14 7 3 11 5 6 2 4 3 1 Note The first test case was described in the statement. In the second and third test cases, we can show that it is impossible to throw out all elements of array a.
import sys def radix_sort(lst, d): rlist = [[] for i in range(10)] llen = len(lst) for m in range(d): for j in range(llen): rlist[lst[j] // 10**m % 10].append(lst[j]) j = 0 for i in range(10): tmp = rlist[i] for k in range(len(tmp)): lst[j] = tmp[k] j += 1 rlist[i].clear() def solve2(lst, n): radix_sort(lst, 7) dic = {} for i in range(len(lst) - 1): try: dic[lst[i]] += 1 except: dic[lst[i]] = 1 se = list(dic.keys()) for i in se: ans = [[lst[-1], i]] dicc = dic.copy() if dicc[i] == 1: del dicc[i] else: dicc[i] -= 1 MAX = lst[-1] j = 2 * n - 2 while j >= 0 and len(ans) < n: try: if dicc[lst[j]] == 1: del dicc[lst[j]] else: dicc[lst[j]] -= 1 except: j -= 1 continue try: need = MAX - lst[j] if dicc[need] == 1: del dicc[need] else: dicc[need] -= 1 ans.append([lst[j], need]) MAX = lst[j] j -= 1 except: break if len(ans) == n: print("YES") print(sum(ans[0])) for i in ans: print(i[0], i[1]) return print("NO") return t = int(input()) for qwq in range(t): n = int(input()) lst = list(map(int, input().split())) solve2(lst, n)
IMPORT FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR LIST LIST VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER WHILE VAR NUMBER FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR
You found a useless array a of 2n positive integers. You have realized that you actually don't need this array, so you decided to throw out all elements of a. It could have been an easy task, but it turned out that you should follow some rules: 1. In the beginning, you select any positive integer x. 2. Then you do the following operation n times: * select two elements of array with sum equals x; * remove them from a and replace x with maximum of that two numbers. For example, if initially a = [3, 5, 1, 2], you can select x = 6. Then you can select the second and the third elements of a with sum 5 + 1 = 6 and throw them out. After this operation, x equals 5 and there are two elements in array: 3 and 2. You can throw them out on the next operation. Note, that you choose x before the start and can't change it as you want between the operations. Determine how should you behave to throw out all elements of a. Input The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The first line of each test case contains the single integer n (1 ≀ n ≀ 1000). The second line of each test case contains 2n integers a_1, a_2, ..., a_{2n} (1 ≀ a_i ≀ 10^6) β€” the initial array a. It is guaranteed that the total sum of n over all test cases doesn't exceed 1000. Output For each test case in the first line print YES if it is possible to throw out all elements of the array and NO otherwise. If it is possible to throw out all elements, print the initial value of x you've chosen. Print description of n operations next. For each operation, print the pair of integers you remove. Example Input 4 2 3 5 1 2 3 1 1 8 8 64 64 2 1 1 2 4 5 1 2 3 4 5 6 7 14 3 11 Output YES 6 1 5 2 3 NO NO YES 21 14 7 3 11 5 6 2 4 3 1 Note The first test case was described in the statement. In the second and third test cases, we can show that it is impossible to throw out all elements of array a.
t = int(input()) for i in range(t): n = int(input()) a = list(map(int, input().split())) a.sort() a.reverse() x = [] for j in range(1, 2 * n): x.append(a[0] + a[j]) for j in range(len(x)): x1 = x[j] w = [[x1]] t = 0 val = 0 d = {} aj = 0 for j1 in range(2 * n): if a[j1] not in d: d[a[j1]] = [j1] else: d[a[j1]].append(j1) for val in a: if val not in d: continue d[val].pop() y = x1 - val x1 = val + 0 w.append([val]) if len(d[val]) == 0: del d[val] if y not in d: aj = 37 break d[y].pop() w[-1].append(y) if len(d[y]) == 0: del d[y] if aj == 0: break if aj == 0: print("YES") for q in w: print(*q) else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST LIST VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
You found a useless array a of 2n positive integers. You have realized that you actually don't need this array, so you decided to throw out all elements of a. It could have been an easy task, but it turned out that you should follow some rules: 1. In the beginning, you select any positive integer x. 2. Then you do the following operation n times: * select two elements of array with sum equals x; * remove them from a and replace x with maximum of that two numbers. For example, if initially a = [3, 5, 1, 2], you can select x = 6. Then you can select the second and the third elements of a with sum 5 + 1 = 6 and throw them out. After this operation, x equals 5 and there are two elements in array: 3 and 2. You can throw them out on the next operation. Note, that you choose x before the start and can't change it as you want between the operations. Determine how should you behave to throw out all elements of a. Input The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The first line of each test case contains the single integer n (1 ≀ n ≀ 1000). The second line of each test case contains 2n integers a_1, a_2, ..., a_{2n} (1 ≀ a_i ≀ 10^6) β€” the initial array a. It is guaranteed that the total sum of n over all test cases doesn't exceed 1000. Output For each test case in the first line print YES if it is possible to throw out all elements of the array and NO otherwise. If it is possible to throw out all elements, print the initial value of x you've chosen. Print description of n operations next. For each operation, print the pair of integers you remove. Example Input 4 2 3 5 1 2 3 1 1 8 8 64 64 2 1 1 2 4 5 1 2 3 4 5 6 7 14 3 11 Output YES 6 1 5 2 3 NO NO YES 21 14 7 3 11 5 6 2 4 3 1 Note The first test case was described in the statement. In the second and third test cases, we can show that it is impossible to throw out all elements of array a.
for _ in range(int(input())): n = int(input()) a = sorted(list(map(int, input().split()))) rhs = n lhs = 0 flag2 = 0 for i in range(2 * n - 1): dic = {} for j in range(2 * n): if a[j] in dic: dic[a[j]] += 1 else: dic[a[j]] = 1 x = a[i] + a[2 * n - 1] ans = x ans2 = [] curr = 2 * n - 1 for j in range(n): flag = 0 while dic[a[curr]] == 0: curr -= 1 dic[a[curr]] -= 1 r = x - a[curr] if not r in dic or dic[r] == 0: flag = 1 break dic[r] -= 1 ans2.append(str(r)) ans2.append(str(a[curr])) x = a[curr] if flag == 0: print("YES") print(ans) print(" ".join(ans2)) flag2 = 1 break if flag2 == 0: print("NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING
You found a useless array a of 2n positive integers. You have realized that you actually don't need this array, so you decided to throw out all elements of a. It could have been an easy task, but it turned out that you should follow some rules: 1. In the beginning, you select any positive integer x. 2. Then you do the following operation n times: * select two elements of array with sum equals x; * remove them from a and replace x with maximum of that two numbers. For example, if initially a = [3, 5, 1, 2], you can select x = 6. Then you can select the second and the third elements of a with sum 5 + 1 = 6 and throw them out. After this operation, x equals 5 and there are two elements in array: 3 and 2. You can throw them out on the next operation. Note, that you choose x before the start and can't change it as you want between the operations. Determine how should you behave to throw out all elements of a. Input The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The first line of each test case contains the single integer n (1 ≀ n ≀ 1000). The second line of each test case contains 2n integers a_1, a_2, ..., a_{2n} (1 ≀ a_i ≀ 10^6) β€” the initial array a. It is guaranteed that the total sum of n over all test cases doesn't exceed 1000. Output For each test case in the first line print YES if it is possible to throw out all elements of the array and NO otherwise. If it is possible to throw out all elements, print the initial value of x you've chosen. Print description of n operations next. For each operation, print the pair of integers you remove. Example Input 4 2 3 5 1 2 3 1 1 8 8 64 64 2 1 1 2 4 5 1 2 3 4 5 6 7 14 3 11 Output YES 6 1 5 2 3 NO NO YES 21 14 7 3 11 5 6 2 4 3 1 Note The first test case was described in the statement. In the second and third test cases, we can show that it is impossible to throw out all elements of array a.
import sys input = sys.stdin.readline def debug(): for i in range(10): pass def solve(arr, x): n = len(arr) // 2 res = True ans = [] for j in range(n): j = 0 while j < len(arr) - 1 and arr[j] + arr[-1] != x: j += 1 if j == len(arr) - 1 or arr[j] + arr[-1] > x: res = False return res, ans ans.append((arr[j], arr[-1])) x = arr[-1] arr = arr[:j] + arr[j + 1 : -1] return res, ans try: for _ in range(int(input())): n = int(input()) arr = sorted([int(i) for i in input().split()]) xs = [(el + arr[-1]) for el in arr[:-1]] flg = 0 for x in xs: res, ans = solve(arr, x) if res: flg = 1 print("YES") print(x) for i in ans: print(*i) break if not flg: print("NO") except EOFError as e: print(e)
IMPORT ASSIGN VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER RETURN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR VAR
You found a useless array a of 2n positive integers. You have realized that you actually don't need this array, so you decided to throw out all elements of a. It could have been an easy task, but it turned out that you should follow some rules: 1. In the beginning, you select any positive integer x. 2. Then you do the following operation n times: * select two elements of array with sum equals x; * remove them from a and replace x with maximum of that two numbers. For example, if initially a = [3, 5, 1, 2], you can select x = 6. Then you can select the second and the third elements of a with sum 5 + 1 = 6 and throw them out. After this operation, x equals 5 and there are two elements in array: 3 and 2. You can throw them out on the next operation. Note, that you choose x before the start and can't change it as you want between the operations. Determine how should you behave to throw out all elements of a. Input The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The first line of each test case contains the single integer n (1 ≀ n ≀ 1000). The second line of each test case contains 2n integers a_1, a_2, ..., a_{2n} (1 ≀ a_i ≀ 10^6) β€” the initial array a. It is guaranteed that the total sum of n over all test cases doesn't exceed 1000. Output For each test case in the first line print YES if it is possible to throw out all elements of the array and NO otherwise. If it is possible to throw out all elements, print the initial value of x you've chosen. Print description of n operations next. For each operation, print the pair of integers you remove. Example Input 4 2 3 5 1 2 3 1 1 8 8 64 64 2 1 1 2 4 5 1 2 3 4 5 6 7 14 3 11 Output YES 6 1 5 2 3 NO NO YES 21 14 7 3 11 5 6 2 4 3 1 Note The first test case was described in the statement. In the second and third test cases, we can show that it is impossible to throw out all elements of array a.
import sys input = sys.stdin.readline def solve(): n = int(input()) a = [int(x) for x in input().split(" ")] a.sort() a_dict = {} for x in a: if x in a_dict: a_dict[x] += 1 else: a_dict[x] = 1 def poss(i): b = a.copy() b_dict = a_dict.copy() z = b.pop() y = b[i] b_dict[y] -= 1 b_dict[z] -= 1 r = [(z, y)] while b: x = max(r[-1]) z = b.pop() while b_dict[z] == 0 and b: z = b.pop() if b_dict[z] == 0: pass else: b_dict[z] -= 1 y = x - z if y < 0 and b: print(r) return False elif y not in b_dict or b_dict[y] == 0 and b: return False elif b_dict[y]: r.append((z, y)) b_dict[y] -= 1 return r for i in range(2 * n - 1): z = poss(i) if z: return [["YES"], [sum(z[0])], *z] return [["NO"]] t = int(input()) for case in range(t): x = solve() for y in x: print(*y)
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR VAR VAR NUMBER VAR RETURN NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR RETURN LIST LIST STRING LIST FUNC_CALL VAR VAR NUMBER VAR RETURN LIST LIST STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
You found a useless array a of 2n positive integers. You have realized that you actually don't need this array, so you decided to throw out all elements of a. It could have been an easy task, but it turned out that you should follow some rules: 1. In the beginning, you select any positive integer x. 2. Then you do the following operation n times: * select two elements of array with sum equals x; * remove them from a and replace x with maximum of that two numbers. For example, if initially a = [3, 5, 1, 2], you can select x = 6. Then you can select the second and the third elements of a with sum 5 + 1 = 6 and throw them out. After this operation, x equals 5 and there are two elements in array: 3 and 2. You can throw them out on the next operation. Note, that you choose x before the start and can't change it as you want between the operations. Determine how should you behave to throw out all elements of a. Input The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The first line of each test case contains the single integer n (1 ≀ n ≀ 1000). The second line of each test case contains 2n integers a_1, a_2, ..., a_{2n} (1 ≀ a_i ≀ 10^6) β€” the initial array a. It is guaranteed that the total sum of n over all test cases doesn't exceed 1000. Output For each test case in the first line print YES if it is possible to throw out all elements of the array and NO otherwise. If it is possible to throw out all elements, print the initial value of x you've chosen. Print description of n operations next. For each operation, print the pair of integers you remove. Example Input 4 2 3 5 1 2 3 1 1 8 8 64 64 2 1 1 2 4 5 1 2 3 4 5 6 7 14 3 11 Output YES 6 1 5 2 3 NO NO YES 21 14 7 3 11 5 6 2 4 3 1 Note The first test case was described in the statement. In the second and third test cases, we can show that it is impossible to throw out all elements of array a.
import sys input = sys.stdin.readline for _ in range(int(input())): n = int(input()) a = sorted(list(map(int, input().split()))) n *= 2 d = {} for e in a: if e in d: d[e] += 1 else: d[e] = 1 ans = [] for i in range(n - 2, -1, -1): td = d.copy() x, y = a[-1], a[i] td[x] -= 1 td[y] -= 1 ans.append((x, y)) tmp = a.copy() while len(ans) < n // 2: while td[tmp[-1]] == 0: tmp.pop() cmax = tmp[-1] td[cmax] -= 1 if x - cmax in td and td[x - cmax] > 0: td[x - cmax] -= 1 ans.append((cmax, x - cmax)) x = cmax else: ans = [] break if ans: break if ans: print("YES") print(sum(ans[0])) for x, y in ans: print(x, y) else: print("NO")
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST IF VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING
You found a useless array a of 2n positive integers. You have realized that you actually don't need this array, so you decided to throw out all elements of a. It could have been an easy task, but it turned out that you should follow some rules: 1. In the beginning, you select any positive integer x. 2. Then you do the following operation n times: * select two elements of array with sum equals x; * remove them from a and replace x with maximum of that two numbers. For example, if initially a = [3, 5, 1, 2], you can select x = 6. Then you can select the second and the third elements of a with sum 5 + 1 = 6 and throw them out. After this operation, x equals 5 and there are two elements in array: 3 and 2. You can throw them out on the next operation. Note, that you choose x before the start and can't change it as you want between the operations. Determine how should you behave to throw out all elements of a. Input The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The first line of each test case contains the single integer n (1 ≀ n ≀ 1000). The second line of each test case contains 2n integers a_1, a_2, ..., a_{2n} (1 ≀ a_i ≀ 10^6) β€” the initial array a. It is guaranteed that the total sum of n over all test cases doesn't exceed 1000. Output For each test case in the first line print YES if it is possible to throw out all elements of the array and NO otherwise. If it is possible to throw out all elements, print the initial value of x you've chosen. Print description of n operations next. For each operation, print the pair of integers you remove. Example Input 4 2 3 5 1 2 3 1 1 8 8 64 64 2 1 1 2 4 5 1 2 3 4 5 6 7 14 3 11 Output YES 6 1 5 2 3 NO NO YES 21 14 7 3 11 5 6 2 4 3 1 Note The first test case was described in the statement. In the second and third test cases, we can show that it is impossible to throw out all elements of array a.
def solve(): n = int(input()) a = sorted(list(map(int, input().split()))) xs = [(el + a[-1]) for el in a[:-1]] for x in xs: res, ans = foo(a, x) if res: print("YES") print(x) for t in ans: print(*t) return print("NO") def foo(a, x): n = len(a) // 2 res = True ans = [] for i in range(n): j = 0 while j < len(a) - 1 and a[j] + a[-1] != x: j += 1 if j == len(a) - 1 or a[j] + a[-1] != x: res = False return res, ans ans.append((a[j], a[-1])) x = a[-1] a = a[:j] + a[j + 1 : -1] return res, ans for _ in range(int(input())): solve()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER RETURN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
You found a useless array a of 2n positive integers. You have realized that you actually don't need this array, so you decided to throw out all elements of a. It could have been an easy task, but it turned out that you should follow some rules: 1. In the beginning, you select any positive integer x. 2. Then you do the following operation n times: * select two elements of array with sum equals x; * remove them from a and replace x with maximum of that two numbers. For example, if initially a = [3, 5, 1, 2], you can select x = 6. Then you can select the second and the third elements of a with sum 5 + 1 = 6 and throw them out. After this operation, x equals 5 and there are two elements in array: 3 and 2. You can throw them out on the next operation. Note, that you choose x before the start and can't change it as you want between the operations. Determine how should you behave to throw out all elements of a. Input The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The first line of each test case contains the single integer n (1 ≀ n ≀ 1000). The second line of each test case contains 2n integers a_1, a_2, ..., a_{2n} (1 ≀ a_i ≀ 10^6) β€” the initial array a. It is guaranteed that the total sum of n over all test cases doesn't exceed 1000. Output For each test case in the first line print YES if it is possible to throw out all elements of the array and NO otherwise. If it is possible to throw out all elements, print the initial value of x you've chosen. Print description of n operations next. For each operation, print the pair of integers you remove. Example Input 4 2 3 5 1 2 3 1 1 8 8 64 64 2 1 1 2 4 5 1 2 3 4 5 6 7 14 3 11 Output YES 6 1 5 2 3 NO NO YES 21 14 7 3 11 5 6 2 4 3 1 Note The first test case was described in the statement. In the second and third test cases, we can show that it is impossible to throw out all elements of array a.
from sys import stdin input = stdin.readline for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) arr.sort() dic = {} for d in arr: if d not in dic: dic[d] = 0 dic[d] += 1 ans = [] b = True for i in range(2 * n - 1): b = True dd = dic.copy() dd[arr[-1]] -= 1 dd[arr[i]] -= 1 x = max(arr[-1], arr[i]) ans.append([arr[-1], arr[i]]) k = 2 * n - 2 c = 1 while b and c < n and k > -1: if dd[arr[k]] < 1: k -= 1 elif x - arr[k] == arr[k] and dd[arr[k]] > 1: dd[arr[k]] -= 1 dd[x - arr[k]] -= 1 ans.append([arr[k], x - arr[k]]) x = max(arr[k], x - arr[k]) c += 1 k -= 1 elif ( x - arr[k] != arr[k] and x - arr[k] in dd and dd[arr[k]] > 0 and dd[x - arr[k]] > 0 ): dd[arr[k]] -= 1 dd[x - arr[k]] -= 1 c += 1 ans.append([arr[k], x - arr[k]]) x = max(arr[k], x - arr[k]) k -= 1 else: break if len(ans) != n: b = False ans = [] else: break if b == False: print("NO") else: print("YES") print(sum(ans[0])) for d in ans: print(" ".join(map(str, d)))
ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR LIST VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
You found a useless array a of 2n positive integers. You have realized that you actually don't need this array, so you decided to throw out all elements of a. It could have been an easy task, but it turned out that you should follow some rules: 1. In the beginning, you select any positive integer x. 2. Then you do the following operation n times: * select two elements of array with sum equals x; * remove them from a and replace x with maximum of that two numbers. For example, if initially a = [3, 5, 1, 2], you can select x = 6. Then you can select the second and the third elements of a with sum 5 + 1 = 6 and throw them out. After this operation, x equals 5 and there are two elements in array: 3 and 2. You can throw them out on the next operation. Note, that you choose x before the start and can't change it as you want between the operations. Determine how should you behave to throw out all elements of a. Input The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The first line of each test case contains the single integer n (1 ≀ n ≀ 1000). The second line of each test case contains 2n integers a_1, a_2, ..., a_{2n} (1 ≀ a_i ≀ 10^6) β€” the initial array a. It is guaranteed that the total sum of n over all test cases doesn't exceed 1000. Output For each test case in the first line print YES if it is possible to throw out all elements of the array and NO otherwise. If it is possible to throw out all elements, print the initial value of x you've chosen. Print description of n operations next. For each operation, print the pair of integers you remove. Example Input 4 2 3 5 1 2 3 1 1 8 8 64 64 2 1 1 2 4 5 1 2 3 4 5 6 7 14 3 11 Output YES 6 1 5 2 3 NO NO YES 21 14 7 3 11 5 6 2 4 3 1 Note The first test case was described in the statement. In the second and third test cases, we can show that it is impossible to throw out all elements of array a.
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) a.sort() a_set = list(set(a)) x_list = [] for i in range(len(a) - 2, -1, -1): x_list.append(a[i] + a[-1]) x_list = list(set(x_list)) i = 0 check = 0 check_list = [] check_x = 0 x = 25 for x in x_list: b = a[:] y = int(x) check_list = [] while True: if len(b) == 0: check = 1 x_check = y break big = b[-1] b.pop() if x - big in b: check_list.append([big, x - big]) b.remove(x - big) x = big else: break if check == 1: break i += 1 if check == 1: print("YES") print(x_list[i]) for e in check_list: for ee in e: print(ee, end=" ") print() else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING
You found a useless array a of 2n positive integers. You have realized that you actually don't need this array, so you decided to throw out all elements of a. It could have been an easy task, but it turned out that you should follow some rules: 1. In the beginning, you select any positive integer x. 2. Then you do the following operation n times: * select two elements of array with sum equals x; * remove them from a and replace x with maximum of that two numbers. For example, if initially a = [3, 5, 1, 2], you can select x = 6. Then you can select the second and the third elements of a with sum 5 + 1 = 6 and throw them out. After this operation, x equals 5 and there are two elements in array: 3 and 2. You can throw them out on the next operation. Note, that you choose x before the start and can't change it as you want between the operations. Determine how should you behave to throw out all elements of a. Input The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The first line of each test case contains the single integer n (1 ≀ n ≀ 1000). The second line of each test case contains 2n integers a_1, a_2, ..., a_{2n} (1 ≀ a_i ≀ 10^6) β€” the initial array a. It is guaranteed that the total sum of n over all test cases doesn't exceed 1000. Output For each test case in the first line print YES if it is possible to throw out all elements of the array and NO otherwise. If it is possible to throw out all elements, print the initial value of x you've chosen. Print description of n operations next. For each operation, print the pair of integers you remove. Example Input 4 2 3 5 1 2 3 1 1 8 8 64 64 2 1 1 2 4 5 1 2 3 4 5 6 7 14 3 11 Output YES 6 1 5 2 3 NO NO YES 21 14 7 3 11 5 6 2 4 3 1 Note The first test case was described in the statement. In the second and third test cases, we can show that it is impossible to throw out all elements of array a.
t = int(input()) def l_dec(dd, k): dd[k] -= 1 if dd[k] == 0: del dd[k] for _ in range(t): n = int(input()) a = [int(e) for e in input().split()] a.sort(reverse=True) lp = {} for e in a: if e in lp: lp[e] += 1 else: lp[e] = 1 inf = False for k in range(1, 2 * n): l = lp.copy() l_dec(l, a[k]) l_dec(l, a[0]) p = [[a[0], a[k]]] e = a[0] for i in range(2 * n): j = i + 1 while j < 2 * n and a[j] not in l: j += 1 if j == 2 * n: break l_dec(l, a[j]) ne = e - a[j] if ne not in l: break else: l_dec(l, ne) p.append([a[j], ne]) e = a[j] if len(l) > 0: inf = True else: inf = False break if inf: print("NO") else: print("YES") print(p[0][0] + p[0][1]) for e in p: print(e[0], e[1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST LIST VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
You found a useless array a of 2n positive integers. You have realized that you actually don't need this array, so you decided to throw out all elements of a. It could have been an easy task, but it turned out that you should follow some rules: 1. In the beginning, you select any positive integer x. 2. Then you do the following operation n times: * select two elements of array with sum equals x; * remove them from a and replace x with maximum of that two numbers. For example, if initially a = [3, 5, 1, 2], you can select x = 6. Then you can select the second and the third elements of a with sum 5 + 1 = 6 and throw them out. After this operation, x equals 5 and there are two elements in array: 3 and 2. You can throw them out on the next operation. Note, that you choose x before the start and can't change it as you want between the operations. Determine how should you behave to throw out all elements of a. Input The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The first line of each test case contains the single integer n (1 ≀ n ≀ 1000). The second line of each test case contains 2n integers a_1, a_2, ..., a_{2n} (1 ≀ a_i ≀ 10^6) β€” the initial array a. It is guaranteed that the total sum of n over all test cases doesn't exceed 1000. Output For each test case in the first line print YES if it is possible to throw out all elements of the array and NO otherwise. If it is possible to throw out all elements, print the initial value of x you've chosen. Print description of n operations next. For each operation, print the pair of integers you remove. Example Input 4 2 3 5 1 2 3 1 1 8 8 64 64 2 1 1 2 4 5 1 2 3 4 5 6 7 14 3 11 Output YES 6 1 5 2 3 NO NO YES 21 14 7 3 11 5 6 2 4 3 1 Note The first test case was described in the statement. In the second and third test cases, we can show that it is impossible to throw out all elements of array a.
def rec(arr, m, start, n, x, step, opp): if step >= n // 2: return True while start < n and m[arr[start]] == 0: start += 1 if start == n: return False left = arr[start] conj = x - left res = False m[left] = m[left] - 1 if conj in m and m[conj] > 0: m[conj] = m[conj] - 1 opp.append((left, conj)) res = rec(arr, m, start + 1, n, left, step + 1, opp) if res is False: m[conj] = m[conj] + 1 opp.pop() m[left] = m[left] + 1 return res def createCountMap(arr): m = {} for i in arr: m[i] = m.get(i, 0) + 1 return m t = int(input()) while t > 0: arr = [] t -= 1 n = int(input()) * 2 tem = input().strip().split() for i in range(n): val = int(tem[i]) arr.append(val) arr = sorted(arr, reverse=True) if n == 2: print("YES") print(arr[0] + arr[1]) print(arr[0], arr[1]) continue m = createCountMap(arr) opp = [] m[arr[0]] = m[arr[0]] - 1 flag = False for i in range(1, n): prospect = arr[i] m[prospect] = m[prospect] - 1 start = 1 if i == 1: start = 2 opp.append((arr[0], prospect)) flag = rec(arr, m, start, n, arr[0], 1, opp) if flag: print("YES") print(prospect + arr[0]) for a, b in opp: print(a, b) break else: m[prospect] = m[prospect] + 1 opp = [] if flag is False: print("NO")
FUNC_DEF IF VAR BIN_OP VAR NUMBER RETURN NUMBER WHILE VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER NUMBER VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR LIST IF VAR NUMBER EXPR FUNC_CALL VAR STRING
Given an array of size N, find the number of distinct pairs {i, j} (i != j) in the array such that the sum of a[i] and a[j] is greater than 0. Example 1: Input: N = 3, a[] = {3, -2, 1} Output: 2 Explanation: {3, -2}, {3, 1} are two possible pairs. Example 2: Input: N = 4, a[] = {-1, -1, -1, 0} Output: 0 Explanation: There are no possible pairs. Your Task: You don't need to read input or print anything. Complete the function ValidPair() which takes the array a[ ] and size of array N as input parameters and returns the count of such pairs. Expected Time Complexity: O(N * Log(N)) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{5} -10^{4} ≀ a[i] ≀ 10^{4}
class Solution: def ValidPair(self, a, n): t = 0 a.sort(reverse=True) low = 0 high = n - 1 while low <= high: s = a[low] + a[high] if s <= 0: high -= 1 else: t += high - low low += 1 return t
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR
Given an array of size N, find the number of distinct pairs {i, j} (i != j) in the array such that the sum of a[i] and a[j] is greater than 0. Example 1: Input: N = 3, a[] = {3, -2, 1} Output: 2 Explanation: {3, -2}, {3, 1} are two possible pairs. Example 2: Input: N = 4, a[] = {-1, -1, -1, 0} Output: 0 Explanation: There are no possible pairs. Your Task: You don't need to read input or print anything. Complete the function ValidPair() which takes the array a[ ] and size of array N as input parameters and returns the count of such pairs. Expected Time Complexity: O(N * Log(N)) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{5} -10^{4} ≀ a[i] ≀ 10^{4}
class Solution: def ValidPair(self, a, n): a = sorted(a) for i in range(n): if a[i] > 0: break e = i s = i - 1 su = 0 m = [] while e < n: if s >= 0 and a[s] + a[e] > 0: s = s - 1 else: su = su + e - s - 1 e = e + 1 return su
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR IF VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
Given an array of size N, find the number of distinct pairs {i, j} (i != j) in the array such that the sum of a[i] and a[j] is greater than 0. Example 1: Input: N = 3, a[] = {3, -2, 1} Output: 2 Explanation: {3, -2}, {3, 1} are two possible pairs. Example 2: Input: N = 4, a[] = {-1, -1, -1, 0} Output: 0 Explanation: There are no possible pairs. Your Task: You don't need to read input or print anything. Complete the function ValidPair() which takes the array a[ ] and size of array N as input parameters and returns the count of such pairs. Expected Time Complexity: O(N * Log(N)) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{5} -10^{4} ≀ a[i] ≀ 10^{4}
class Solution: def ValidPair(self, a, n): arr = a a.sort() p_index = -1 for i in range(n - 1, -1, -1): if a[i] > 0: p_index = i else: break if p_index == -1: return 0 num = n - p_index count = num * (num - 1) // 2 for j in range(p_index - 1, -1, -1): while p_index < n and arr[j] + arr[p_index] <= 0: p_index += 1 if p_index > n - 1: break count += n - p_index return count
CLASS_DEF FUNC_DEF ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE VAR VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR
Given an array of size N, find the number of distinct pairs {i, j} (i != j) in the array such that the sum of a[i] and a[j] is greater than 0. Example 1: Input: N = 3, a[] = {3, -2, 1} Output: 2 Explanation: {3, -2}, {3, 1} are two possible pairs. Example 2: Input: N = 4, a[] = {-1, -1, -1, 0} Output: 0 Explanation: There are no possible pairs. Your Task: You don't need to read input or print anything. Complete the function ValidPair() which takes the array a[ ] and size of array N as input parameters and returns the count of such pairs. Expected Time Complexity: O(N * Log(N)) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{5} -10^{4} ≀ a[i] ≀ 10^{4}
class Solution: def ValidPair(self, a, n): ans = 0 front, tail = 0, n - 1 a.sort() while front < tail: if a[front] + a[tail] > 0: ans += tail - front tail -= 1 else: front += 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
Given an array of size N, find the number of distinct pairs {i, j} (i != j) in the array such that the sum of a[i] and a[j] is greater than 0. Example 1: Input: N = 3, a[] = {3, -2, 1} Output: 2 Explanation: {3, -2}, {3, 1} are two possible pairs. Example 2: Input: N = 4, a[] = {-1, -1, -1, 0} Output: 0 Explanation: There are no possible pairs. Your Task: You don't need to read input or print anything. Complete the function ValidPair() which takes the array a[ ] and size of array N as input parameters and returns the count of such pairs. Expected Time Complexity: O(N * Log(N)) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{5} -10^{4} ≀ a[i] ≀ 10^{4}
class Solution: def ValidPair(self, arr, n): arr.sort() ans = 0 low = 0 high = n - 1 while low < high: if arr[low] + arr[high] > 0: ans += high - low high -= 1 else: low += 1 return ans
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
Given an array of size N, find the number of distinct pairs {i, j} (i != j) in the array such that the sum of a[i] and a[j] is greater than 0. Example 1: Input: N = 3, a[] = {3, -2, 1} Output: 2 Explanation: {3, -2}, {3, 1} are two possible pairs. Example 2: Input: N = 4, a[] = {-1, -1, -1, 0} Output: 0 Explanation: There are no possible pairs. Your Task: You don't need to read input or print anything. Complete the function ValidPair() which takes the array a[ ] and size of array N as input parameters and returns the count of such pairs. Expected Time Complexity: O(N * Log(N)) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{5} -10^{4} ≀ a[i] ≀ 10^{4}
class Solution: def ValidPair(self, a, n): a.sort(reverse=True) low, high = 0, n - 1 count = 0 while low <= high: msum = a[low] + a[high] if msum <= 0: high -= 1 else: count += high - low low += 1 return count if __name__ == "__main__": t = int(input()) for _ in range(t): n = int(input()) array = list(map(int, input().strip().split())) obj = Solution() ans = obj.ValidPair(array, n) print(ans)
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Given an array of size N, find the number of distinct pairs {i, j} (i != j) in the array such that the sum of a[i] and a[j] is greater than 0. Example 1: Input: N = 3, a[] = {3, -2, 1} Output: 2 Explanation: {3, -2}, {3, 1} are two possible pairs. Example 2: Input: N = 4, a[] = {-1, -1, -1, 0} Output: 0 Explanation: There are no possible pairs. Your Task: You don't need to read input or print anything. Complete the function ValidPair() which takes the array a[ ] and size of array N as input parameters and returns the count of such pairs. Expected Time Complexity: O(N * Log(N)) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{5} -10^{4} ≀ a[i] ≀ 10^{4}
class Solution: def ValidPair(self, a, n): a.sort(reverse=True) i = 0 j = n - 1 c = 0 s = 0 while i <= j: s = a[i] + a[j] if s <= 0: j -= 1 else: c += j - i i += 1 return c
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR
Given an array of size N, find the number of distinct pairs {i, j} (i != j) in the array such that the sum of a[i] and a[j] is greater than 0. Example 1: Input: N = 3, a[] = {3, -2, 1} Output: 2 Explanation: {3, -2}, {3, 1} are two possible pairs. Example 2: Input: N = 4, a[] = {-1, -1, -1, 0} Output: 0 Explanation: There are no possible pairs. Your Task: You don't need to read input or print anything. Complete the function ValidPair() which takes the array a[ ] and size of array N as input parameters and returns the count of such pairs. Expected Time Complexity: O(N * Log(N)) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{5} -10^{4} ≀ a[i] ≀ 10^{4}
class Solution: def ValidPair(self, a, n): c = 0 a.sort() i = 0 j = n - 1 while i < j: if a[i] + a[j] >= 1: c += j - i j -= 1 elif a[i] <= 1: i += 1 return c
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER RETURN VAR
Given an array of size N, find the number of distinct pairs {i, j} (i != j) in the array such that the sum of a[i] and a[j] is greater than 0. Example 1: Input: N = 3, a[] = {3, -2, 1} Output: 2 Explanation: {3, -2}, {3, 1} are two possible pairs. Example 2: Input: N = 4, a[] = {-1, -1, -1, 0} Output: 0 Explanation: There are no possible pairs. Your Task: You don't need to read input or print anything. Complete the function ValidPair() which takes the array a[ ] and size of array N as input parameters and returns the count of such pairs. Expected Time Complexity: O(N * Log(N)) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{5} -10^{4} ≀ a[i] ≀ 10^{4}
class Solution: def ValidPair(self, a, n): a.sort() for i in range(n): if a[i] >= 0: break pos = n - i - 1 j = i i = i - 1 ans = 0 cnt = 0 while j < n and i >= 0: while i >= 0 and a[i] + a[j] > 0: i -= 1 cnt += 1 ans += pos + cnt pos -= 1 j += 1 while i < 0 and j < n: ans += pos + cnt pos -= 1 j += 1 return ans
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER WHILE VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
Given an array of size N, find the number of distinct pairs {i, j} (i != j) in the array such that the sum of a[i] and a[j] is greater than 0. Example 1: Input: N = 3, a[] = {3, -2, 1} Output: 2 Explanation: {3, -2}, {3, 1} are two possible pairs. Example 2: Input: N = 4, a[] = {-1, -1, -1, 0} Output: 0 Explanation: There are no possible pairs. Your Task: You don't need to read input or print anything. Complete the function ValidPair() which takes the array a[ ] and size of array N as input parameters and returns the count of such pairs. Expected Time Complexity: O(N * Log(N)) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{5} -10^{4} ≀ a[i] ≀ 10^{4}
class Solution: def ValidPair(self, A, N): A.sort() i, j = 0, N - 1 ans = 0 while i < j: if A[i] + A[j] > 0: j -= 1 else: ans += N - j - 1 i += 1 L = N - j - 1 ans += L * (L + 1) // 2 return ans
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN VAR
Given an array of size N, find the number of distinct pairs {i, j} (i != j) in the array such that the sum of a[i] and a[j] is greater than 0. Example 1: Input: N = 3, a[] = {3, -2, 1} Output: 2 Explanation: {3, -2}, {3, 1} are two possible pairs. Example 2: Input: N = 4, a[] = {-1, -1, -1, 0} Output: 0 Explanation: There are no possible pairs. Your Task: You don't need to read input or print anything. Complete the function ValidPair() which takes the array a[ ] and size of array N as input parameters and returns the count of such pairs. Expected Time Complexity: O(N * Log(N)) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{5} -10^{4} ≀ a[i] ≀ 10^{4}
class Solution: def ValidPair(self, arr, n): arr.sort() left, right = 0, n - 1 count = 0 while left <= right: if arr[left] + arr[right] <= 0: left += 1 else: count += right - left right -= 1 return count
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR
Given an array of size N, find the number of distinct pairs {i, j} (i != j) in the array such that the sum of a[i] and a[j] is greater than 0. Example 1: Input: N = 3, a[] = {3, -2, 1} Output: 2 Explanation: {3, -2}, {3, 1} are two possible pairs. Example 2: Input: N = 4, a[] = {-1, -1, -1, 0} Output: 0 Explanation: There are no possible pairs. Your Task: You don't need to read input or print anything. Complete the function ValidPair() which takes the array a[ ] and size of array N as input parameters and returns the count of such pairs. Expected Time Complexity: O(N * Log(N)) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{5} -10^{4} ≀ a[i] ≀ 10^{4}
class Solution: def ValidPair(self, a, n): a.sort() i = 0 while a[i] < 0: i += 1 j = i t = n - j i -= 1 ans = 0 while i >= 0 and j < n: if a[i] + a[j] < 0: j += 1 elif a[i] + a[j] > 0: i -= 1 ans += n - j else: j += 1 ans += t * (t - 1) // 2 return ans
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN VAR
Given an array of size N, find the number of distinct pairs {i, j} (i != j) in the array such that the sum of a[i] and a[j] is greater than 0. Example 1: Input: N = 3, a[] = {3, -2, 1} Output: 2 Explanation: {3, -2}, {3, 1} are two possible pairs. Example 2: Input: N = 4, a[] = {-1, -1, -1, 0} Output: 0 Explanation: There are no possible pairs. Your Task: You don't need to read input or print anything. Complete the function ValidPair() which takes the array a[ ] and size of array N as input parameters and returns the count of such pairs. Expected Time Complexity: O(N * Log(N)) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{5} -10^{4} ≀ a[i] ≀ 10^{4}
class Solution: def upper_bound(self, a, l, r, k): pos = r + 1 while l <= r: mid = (l + r) // 2 if a[mid] > k: pos = mid r = mid - 1 else: l = mid + 1 return pos def ValidPair(self, a, n): ans = 0 a.sort() positive = 0 for i in range(0, n): if a[i] > 0: positive = n - i break else: pos = self.upper_bound(a, i, n - 1, abs(a[i])) ans = ans + n - pos ans = ans + positive * (positive - 1) // 2 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN VAR
Given an array of size N, find the number of distinct pairs {i, j} (i != j) in the array such that the sum of a[i] and a[j] is greater than 0. Example 1: Input: N = 3, a[] = {3, -2, 1} Output: 2 Explanation: {3, -2}, {3, 1} are two possible pairs. Example 2: Input: N = 4, a[] = {-1, -1, -1, 0} Output: 0 Explanation: There are no possible pairs. Your Task: You don't need to read input or print anything. Complete the function ValidPair() which takes the array a[ ] and size of array N as input parameters and returns the count of such pairs. Expected Time Complexity: O(N * Log(N)) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{5} -10^{4} ≀ a[i] ≀ 10^{4}
class Solution: def ValidPair(self, a, n): cnt = 0 low = 0 high = n - 1 a.sort() while low < high: _sum = a[low] + a[high] if _sum <= 0: low += 1 elif _sum > 0: cnt += high - low high -= 1 return cnt
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR
Given an array of size N, find the number of distinct pairs {i, j} (i != j) in the array such that the sum of a[i] and a[j] is greater than 0. Example 1: Input: N = 3, a[] = {3, -2, 1} Output: 2 Explanation: {3, -2}, {3, 1} are two possible pairs. Example 2: Input: N = 4, a[] = {-1, -1, -1, 0} Output: 0 Explanation: There are no possible pairs. Your Task: You don't need to read input or print anything. Complete the function ValidPair() which takes the array a[ ] and size of array N as input parameters and returns the count of such pairs. Expected Time Complexity: O(N * Log(N)) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{5} -10^{4} ≀ a[i] ≀ 10^{4}
class Solution: def ValidPair(self, a, n): a.sort() left = 0 rigth = n - 1 count = 0 while left < rigth: if a[left] + a[rigth] > 0: count = count + (rigth - left) rigth = rigth - 1 else: left = left + 1 return count
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
Given an array of size N, find the number of distinct pairs {i, j} (i != j) in the array such that the sum of a[i] and a[j] is greater than 0. Example 1: Input: N = 3, a[] = {3, -2, 1} Output: 2 Explanation: {3, -2}, {3, 1} are two possible pairs. Example 2: Input: N = 4, a[] = {-1, -1, -1, 0} Output: 0 Explanation: There are no possible pairs. Your Task: You don't need to read input or print anything. Complete the function ValidPair() which takes the array a[ ] and size of array N as input parameters and returns the count of such pairs. Expected Time Complexity: O(N * Log(N)) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{5} -10^{4} ≀ a[i] ≀ 10^{4}
class Solution: def ValidPair(self, a, n): negative = [] positive = [] for num in a: if num >= 0: positive.append(num) else: negative.append(num) positive = sorted(positive, reverse=True) negative = sorted(negative) pairs = 0 posNum = 0 for negNum in negative: while posNum < len(positive) and negNum + positive[posNum] > 0: posNum += 1 pairs = pairs + posNum numberOfPositives = len(positive) pairs += numberOfPositives * (numberOfPositives - 1) // 2 return pairs
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN VAR
Given an array of size N, find the number of distinct pairs {i, j} (i != j) in the array such that the sum of a[i] and a[j] is greater than 0. Example 1: Input: N = 3, a[] = {3, -2, 1} Output: 2 Explanation: {3, -2}, {3, 1} are two possible pairs. Example 2: Input: N = 4, a[] = {-1, -1, -1, 0} Output: 0 Explanation: There are no possible pairs. Your Task: You don't need to read input or print anything. Complete the function ValidPair() which takes the array a[ ] and size of array N as input parameters and returns the count of such pairs. Expected Time Complexity: O(N * Log(N)) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{5} -10^{4} ≀ a[i] ≀ 10^{4}
class Solution: def ValidPair(self, a, n): a = sorted(a) i = 0 j = n - 1 c = 0 u = [] while i < j: if a[i] + a[j] > 0: c += j - i j = j - 1 else: i += 1 return c
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR
Given an array of size N, find the number of distinct pairs {i, j} (i != j) in the array such that the sum of a[i] and a[j] is greater than 0. Example 1: Input: N = 3, a[] = {3, -2, 1} Output: 2 Explanation: {3, -2}, {3, 1} are two possible pairs. Example 2: Input: N = 4, a[] = {-1, -1, -1, 0} Output: 0 Explanation: There are no possible pairs. Your Task: You don't need to read input or print anything. Complete the function ValidPair() which takes the array a[ ] and size of array N as input parameters and returns the count of such pairs. Expected Time Complexity: O(N * Log(N)) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{5} -10^{4} ≀ a[i] ≀ 10^{4}
class Solution: def ValidPair(self, a, n): ans = 0 a.sort() mod = 10**9 + 7 i = 0 j = len(a) - 1 while i < j: if a[i] + a[j] > 0: ans += (j - i) % mod j -= 1 else: i += 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
Given an array of size N, find the number of distinct pairs {i, j} (i != j) in the array such that the sum of a[i] and a[j] is greater than 0. Example 1: Input: N = 3, a[] = {3, -2, 1} Output: 2 Explanation: {3, -2}, {3, 1} are two possible pairs. Example 2: Input: N = 4, a[] = {-1, -1, -1, 0} Output: 0 Explanation: There are no possible pairs. Your Task: You don't need to read input or print anything. Complete the function ValidPair() which takes the array a[ ] and size of array N as input parameters and returns the count of such pairs. Expected Time Complexity: O(N * Log(N)) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{5} -10^{4} ≀ a[i] ≀ 10^{4}
class Solution: def ValidPair(self, a, n): a.sort() count = 0 j = n - 1 for i in range(n): while j > i and a[i] + a[j] > 0: j -= 1 if j == i: count += (n - 1 - i) * (n - i) // 2 break count += n - j - 1 return count
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
For a vector \vec{v} = (x, y), define |v| = √{x^2 + y^2}. Allen had a bit too much to drink at the bar, which is at the origin. There are n vectors \vec{v_1}, \vec{v_2}, β‹…β‹…β‹…, \vec{v_n}. Allen will make n moves. As Allen's sense of direction is impaired, during the i-th move he will either move in the direction \vec{v_i} or -\vec{v_i}. In other words, if his position is currently p = (x, y), he will either move to p + \vec{v_i} or p - \vec{v_i}. Allen doesn't want to wander too far from home (which happens to also be the bar). You need to help him figure out a sequence of moves (a sequence of signs for the vectors) such that his final position p satisfies |p| ≀ 1.5 β‹… 10^6 so that he can stay safe. Input The first line contains a single integer n (1 ≀ n ≀ 10^5) β€” the number of moves. Each of the following lines contains two space-separated integers x_i and y_i, meaning that \vec{v_i} = (x_i, y_i). We have that |v_i| ≀ 10^6 for all i. Output Output a single line containing n integers c_1, c_2, β‹…β‹…β‹…, c_n, each of which is either 1 or -1. Your solution is correct if the value of p = βˆ‘_{i = 1}^n c_i \vec{v_i}, satisfies |p| ≀ 1.5 β‹… 10^6. It can be shown that a solution always exists under the given constraints. Examples Input 3 999999 0 0 999999 999999 0 Output 1 1 -1 Input 1 -824590 246031 Output 1 Input 8 -67761 603277 640586 -396671 46147 -122580 569609 -2112 400 914208 131792 309779 -850150 -486293 5272 721899 Output 1 1 1 1 1 1 1 -1
n = int(input()) l = [] def d(x, y): return x**2 + y**2 lv = [(0) for i in range(n)] for i in range(n): l1 = list(map(int, input().strip().split())) l.append(l1) vx = l[0][0] vy = l[0][1] lv[0] = 1 for i in range(1, n - 1, 2): vx1, vy1, vx2, vy2 = l[i][0], l[i][1], l[i + 1][0], l[i + 1][1] _, c1, c2 = min( (d(vx + vx1 + vx2, vy + vy1 + vy2), 1, 1), (d(vx + vx1 - vx2, vy + vy1 - vy2), 1, -1), (d(vx - vx1 + vx2, vy - vy1 + vy2), -1, 1), (d(vx - vx1 - vx2, vy - vy1 - vy2), -1, -1), ) vx = vx + c1 * vx1 + c2 * vx2 vy = vy + vy1 * c1 + vy2 * c2 lv[i] = c1 lv[i + 1] = c2 if lv[n - 1] == 0: _, c1 = min( (d(vx + l[n - 1][0], vy + l[n - 1][1]), 1), (d(vx - l[n - 1][0], vy - l[n - 1][1]), -1), ) lv[n - 1] = c1 print(" ".join(map(str, lv)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_DEF RETURN BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
You are preparing for an exam on scheduling theory. The exam will last for exactly T milliseconds and will consist of n problems. You can either solve problem i in exactly t_{i} milliseconds or ignore it and spend no time. You don't need time to rest after solving a problem, either. Unfortunately, your teacher considers some of the problems too easy for you. Thus, he assigned an integer a_{i} to every problem i meaning that the problem i can bring you a point to the final score only in case you have solved no more than a_{i} problems overall (including problem i). Formally, suppose you solve problems p_1, p_2, ..., p_{k} during the exam. Then, your final score s will be equal to the number of values of j between 1 and k such that k ≀ a_{p}_{j}. You have guessed that the real first problem of the exam is already in front of you. Therefore, you want to choose a set of problems to solve during the exam maximizing your final score in advance. Don't forget that the exam is limited in time, and you must have enough time to solve all chosen problems. If there exist different sets of problems leading to the maximum final score, any of them will do. -----Input----- The first line contains two integers n and T (1 ≀ n ≀ 2Β·10^5; 1 ≀ T ≀ 10^9)Β β€” the number of problems in the exam and the length of the exam in milliseconds, respectively. Each of the next n lines contains two integers a_{i} and t_{i} (1 ≀ a_{i} ≀ n; 1 ≀ t_{i} ≀ 10^4). The problems are numbered from 1 to n. -----Output----- In the first line, output a single integer sΒ β€” your maximum possible final score. In the second line, output a single integer k (0 ≀ k ≀ n)Β β€” the number of problems you should solve. In the third line, output k distinct integers p_1, p_2, ..., p_{k} (1 ≀ p_{i} ≀ n)Β β€” the indexes of problems you should solve, in any order. If there are several optimal sets of problems, you may output any of them. -----Examples----- Input 5 300 3 100 4 150 4 80 2 90 2 300 Output 2 3 3 1 4 Input 2 100 1 787 2 788 Output 0 0 Input 2 100 2 42 2 58 Output 2 2 1 2 -----Note----- In the first example, you should solve problems 3, 1, and 4. In this case you'll spend 80 + 100 + 90 = 270 milliseconds, falling within the length of the exam, 300 milliseconds (and even leaving yourself 30 milliseconds to have a rest). Problems 3 and 1 will bring you a point each, while problem 4 won't. You'll score two points. In the second example, the length of the exam is catastrophically not enough to solve even a single problem. In the third example, you have just enough time to solve both problems in 42 + 58 = 100 milliseconds and hand your solutions to the teacher with a smile.
import sys input = sys.stdin.readline def fun(k): nonlocal li, t tem = [] count = 0 for i in li: if i[0] >= k: tem.append(i) count += 1 if count >= k: ans = 0 for i in range(k): ans += tem[i][1] if ans <= t: return True else: return False else: return False n, t = map(int, input().split()) li = [] for _ in range(n): li.append(list(map(int, input().split())) + [_]) li.sort(key=lambda x: x[1]) l = 0 r = n while r - l > 1: mid = (l + r) // 2 if fun(mid): l = mid else: r = mid fin = 0 for i in range(l, r + 1): if fun(i): fin = i print(fin) print(fin) tem = [] for i in range(n): if li[i][0] >= fin: tem.append(li[i][2] + 1) print(*tem[:fin])
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR LIST VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR
You are preparing for an exam on scheduling theory. The exam will last for exactly T milliseconds and will consist of n problems. You can either solve problem i in exactly t_{i} milliseconds or ignore it and spend no time. You don't need time to rest after solving a problem, either. Unfortunately, your teacher considers some of the problems too easy for you. Thus, he assigned an integer a_{i} to every problem i meaning that the problem i can bring you a point to the final score only in case you have solved no more than a_{i} problems overall (including problem i). Formally, suppose you solve problems p_1, p_2, ..., p_{k} during the exam. Then, your final score s will be equal to the number of values of j between 1 and k such that k ≀ a_{p}_{j}. You have guessed that the real first problem of the exam is already in front of you. Therefore, you want to choose a set of problems to solve during the exam maximizing your final score in advance. Don't forget that the exam is limited in time, and you must have enough time to solve all chosen problems. If there exist different sets of problems leading to the maximum final score, any of them will do. -----Input----- The first line contains two integers n and T (1 ≀ n ≀ 2Β·10^5; 1 ≀ T ≀ 10^9)Β β€” the number of problems in the exam and the length of the exam in milliseconds, respectively. Each of the next n lines contains two integers a_{i} and t_{i} (1 ≀ a_{i} ≀ n; 1 ≀ t_{i} ≀ 10^4). The problems are numbered from 1 to n. -----Output----- In the first line, output a single integer sΒ β€” your maximum possible final score. In the second line, output a single integer k (0 ≀ k ≀ n)Β β€” the number of problems you should solve. In the third line, output k distinct integers p_1, p_2, ..., p_{k} (1 ≀ p_{i} ≀ n)Β β€” the indexes of problems you should solve, in any order. If there are several optimal sets of problems, you may output any of them. -----Examples----- Input 5 300 3 100 4 150 4 80 2 90 2 300 Output 2 3 3 1 4 Input 2 100 1 787 2 788 Output 0 0 Input 2 100 2 42 2 58 Output 2 2 1 2 -----Note----- In the first example, you should solve problems 3, 1, and 4. In this case you'll spend 80 + 100 + 90 = 270 milliseconds, falling within the length of the exam, 300 milliseconds (and even leaving yourself 30 milliseconds to have a rest). Problems 3 and 1 will bring you a point each, while problem 4 won't. You'll score two points. In the second example, the length of the exam is catastrophically not enough to solve even a single problem. In the third example, you have just enough time to solve both problems in 42 + 58 = 100 milliseconds and hand your solutions to the teacher with a smile.
from sys import stdin, stdout def check(k, b, T): c = [e for e in b if e[0] >= k] if len(c) < k: return False, None first_k_probs = c[:k] s = sum([e[1] for e in first_k_probs]) if s > T: return False, None return True, first_k_probs def solve(n, T, a, t): b = [] for i in range(n): b.append((a[i], t[i], i + 1)) b.sort(key=lambda x: x[1]) low, high = 0, n result = 0 final_probs = [] while low <= high: mid = (low + high) // 2 possible, probs = check(mid, b, T) if possible: result, final_probs = mid, probs low = mid + 1 else: high = mid - 1 return result, [e[2] for e in final_probs] n, T = (int(x) for x in stdin.readline().split()) a = [0] * n t = [0] * n for i in range(n): a[i], t[i] = (int(x) for x in stdin.readline().split()) point, probs = solve(n, T, a, t) stdout.write("%s\n" % point) stdout.write("%s\n" % len(probs)) if len(probs) > 0: stdout.write("%s\n" % " ".join([str(x) for x in probs]))
FUNC_DEF ASSIGN VAR VAR VAR VAR VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER NONE ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR IF VAR VAR RETURN NUMBER NONE RETURN NUMBER VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
You are preparing for an exam on scheduling theory. The exam will last for exactly T milliseconds and will consist of n problems. You can either solve problem i in exactly t_{i} milliseconds or ignore it and spend no time. You don't need time to rest after solving a problem, either. Unfortunately, your teacher considers some of the problems too easy for you. Thus, he assigned an integer a_{i} to every problem i meaning that the problem i can bring you a point to the final score only in case you have solved no more than a_{i} problems overall (including problem i). Formally, suppose you solve problems p_1, p_2, ..., p_{k} during the exam. Then, your final score s will be equal to the number of values of j between 1 and k such that k ≀ a_{p}_{j}. You have guessed that the real first problem of the exam is already in front of you. Therefore, you want to choose a set of problems to solve during the exam maximizing your final score in advance. Don't forget that the exam is limited in time, and you must have enough time to solve all chosen problems. If there exist different sets of problems leading to the maximum final score, any of them will do. -----Input----- The first line contains two integers n and T (1 ≀ n ≀ 2Β·10^5; 1 ≀ T ≀ 10^9)Β β€” the number of problems in the exam and the length of the exam in milliseconds, respectively. Each of the next n lines contains two integers a_{i} and t_{i} (1 ≀ a_{i} ≀ n; 1 ≀ t_{i} ≀ 10^4). The problems are numbered from 1 to n. -----Output----- In the first line, output a single integer sΒ β€” your maximum possible final score. In the second line, output a single integer k (0 ≀ k ≀ n)Β β€” the number of problems you should solve. In the third line, output k distinct integers p_1, p_2, ..., p_{k} (1 ≀ p_{i} ≀ n)Β β€” the indexes of problems you should solve, in any order. If there are several optimal sets of problems, you may output any of them. -----Examples----- Input 5 300 3 100 4 150 4 80 2 90 2 300 Output 2 3 3 1 4 Input 2 100 1 787 2 788 Output 0 0 Input 2 100 2 42 2 58 Output 2 2 1 2 -----Note----- In the first example, you should solve problems 3, 1, and 4. In this case you'll spend 80 + 100 + 90 = 270 milliseconds, falling within the length of the exam, 300 milliseconds (and even leaving yourself 30 milliseconds to have a rest). Problems 3 and 1 will bring you a point each, while problem 4 won't. You'll score two points. In the second example, the length of the exam is catastrophically not enough to solve even a single problem. In the third example, you have just enough time to solve both problems in 42 + 58 = 100 milliseconds and hand your solutions to the teacher with a smile.
import sys from sys import stdin, stdout input = sys.stdin.readline def solve(n, t, tasks): lo = 0 hi = n res = [] curr_res = 0 tasks.sort(key=lambda x: x[1]) while lo <= hi: mid = lo + (hi - lo) // 2 valid_tasks = [] for i in tasks: if i[0] >= mid: valid_tasks.append(i) can_do = False curr_sum = 0 total_used = 0 r = [] for i in valid_tasks: curr_sum += i[1] total_used += 1 r.append(i[2]) if curr_sum > t: break elif total_used >= mid: can_do = True curr_res = mid res = r break if can_do: lo = mid + 1 else: hi = mid - 1 return curr_res, res def main(): n, t = (int(x) for x in input().split(" ")) tasks = [] nums = n idx = 1 while n: a_i, t_i = (int(x) for x in input().split(" ")) tasks.append((a_i, t_i, idx)) idx += 1 n -= 1 res, res_arry = solve(nums, t, tasks) print(res) print(res) stdout.write(" ".join(map(str, res_arry))) stdout.write("\n") main()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR
You are preparing for an exam on scheduling theory. The exam will last for exactly T milliseconds and will consist of n problems. You can either solve problem i in exactly t_{i} milliseconds or ignore it and spend no time. You don't need time to rest after solving a problem, either. Unfortunately, your teacher considers some of the problems too easy for you. Thus, he assigned an integer a_{i} to every problem i meaning that the problem i can bring you a point to the final score only in case you have solved no more than a_{i} problems overall (including problem i). Formally, suppose you solve problems p_1, p_2, ..., p_{k} during the exam. Then, your final score s will be equal to the number of values of j between 1 and k such that k ≀ a_{p}_{j}. You have guessed that the real first problem of the exam is already in front of you. Therefore, you want to choose a set of problems to solve during the exam maximizing your final score in advance. Don't forget that the exam is limited in time, and you must have enough time to solve all chosen problems. If there exist different sets of problems leading to the maximum final score, any of them will do. -----Input----- The first line contains two integers n and T (1 ≀ n ≀ 2Β·10^5; 1 ≀ T ≀ 10^9)Β β€” the number of problems in the exam and the length of the exam in milliseconds, respectively. Each of the next n lines contains two integers a_{i} and t_{i} (1 ≀ a_{i} ≀ n; 1 ≀ t_{i} ≀ 10^4). The problems are numbered from 1 to n. -----Output----- In the first line, output a single integer sΒ β€” your maximum possible final score. In the second line, output a single integer k (0 ≀ k ≀ n)Β β€” the number of problems you should solve. In the third line, output k distinct integers p_1, p_2, ..., p_{k} (1 ≀ p_{i} ≀ n)Β β€” the indexes of problems you should solve, in any order. If there are several optimal sets of problems, you may output any of them. -----Examples----- Input 5 300 3 100 4 150 4 80 2 90 2 300 Output 2 3 3 1 4 Input 2 100 1 787 2 788 Output 0 0 Input 2 100 2 42 2 58 Output 2 2 1 2 -----Note----- In the first example, you should solve problems 3, 1, and 4. In this case you'll spend 80 + 100 + 90 = 270 milliseconds, falling within the length of the exam, 300 milliseconds (and even leaving yourself 30 milliseconds to have a rest). Problems 3 and 1 will bring you a point each, while problem 4 won't. You'll score two points. In the second example, the length of the exam is catastrophically not enough to solve even a single problem. In the third example, you have just enough time to solve both problems in 42 + 58 = 100 milliseconds and hand your solutions to the teacher with a smile.
n, T = [int(x) for x in input().split()] prob = [] vals = [set() for stuff in range(n + 2)] for i in range(n): a, t = [int(x) for x in input().split()] prob.append((i + 1, a, t)) prob.sort(key=lambda tup: tup[2]) currindex = 0 maxindex = -1 solve = set() mem = set() timeleft = T target = 1 for currindex in range(n): i, a, t = prob[currindex] if timeleft < t: break if timeleft >= t and a >= target: vals[a].add(currindex) solve.add(currindex) timeleft -= t if len(solve) == target: maxindex = currindex for p in vals[target]: solve.remove(p) timeleft += prob[p][2] target += 1 bestsolve = solve | vals[target - 1] solvelist = [x for x in bestsolve if x <= maxindex] target = len(solvelist) print(target) print(target) for p in solvelist: print(prob[p][0], end=" ") print()
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER STRING EXPR FUNC_CALL VAR
You are preparing for an exam on scheduling theory. The exam will last for exactly T milliseconds and will consist of n problems. You can either solve problem i in exactly t_{i} milliseconds or ignore it and spend no time. You don't need time to rest after solving a problem, either. Unfortunately, your teacher considers some of the problems too easy for you. Thus, he assigned an integer a_{i} to every problem i meaning that the problem i can bring you a point to the final score only in case you have solved no more than a_{i} problems overall (including problem i). Formally, suppose you solve problems p_1, p_2, ..., p_{k} during the exam. Then, your final score s will be equal to the number of values of j between 1 and k such that k ≀ a_{p}_{j}. You have guessed that the real first problem of the exam is already in front of you. Therefore, you want to choose a set of problems to solve during the exam maximizing your final score in advance. Don't forget that the exam is limited in time, and you must have enough time to solve all chosen problems. If there exist different sets of problems leading to the maximum final score, any of them will do. -----Input----- The first line contains two integers n and T (1 ≀ n ≀ 2Β·10^5; 1 ≀ T ≀ 10^9)Β β€” the number of problems in the exam and the length of the exam in milliseconds, respectively. Each of the next n lines contains two integers a_{i} and t_{i} (1 ≀ a_{i} ≀ n; 1 ≀ t_{i} ≀ 10^4). The problems are numbered from 1 to n. -----Output----- In the first line, output a single integer sΒ β€” your maximum possible final score. In the second line, output a single integer k (0 ≀ k ≀ n)Β β€” the number of problems you should solve. In the third line, output k distinct integers p_1, p_2, ..., p_{k} (1 ≀ p_{i} ≀ n)Β β€” the indexes of problems you should solve, in any order. If there are several optimal sets of problems, you may output any of them. -----Examples----- Input 5 300 3 100 4 150 4 80 2 90 2 300 Output 2 3 3 1 4 Input 2 100 1 787 2 788 Output 0 0 Input 2 100 2 42 2 58 Output 2 2 1 2 -----Note----- In the first example, you should solve problems 3, 1, and 4. In this case you'll spend 80 + 100 + 90 = 270 milliseconds, falling within the length of the exam, 300 milliseconds (and even leaving yourself 30 milliseconds to have a rest). Problems 3 and 1 will bring you a point each, while problem 4 won't. You'll score two points. In the second example, the length of the exam is catastrophically not enough to solve even a single problem. In the third example, you have just enough time to solve both problems in 42 + 58 = 100 milliseconds and hand your solutions to the teacher with a smile.
from sys import stdin, stdout def get_answer(a, mid, t, return_ans=False): left_time = t total_taken = 0 if return_ans: ans = [] for i in range(len(a)): if a[i][0] < mid: continue if a[i][1] <= left_time: total_taken += 1 if return_ans: ans.append(a[i][2]) left_time -= a[i][1] else: break if total_taken == mid: break if return_ans: return ans if total_taken == mid: return True else: return False input, print = stdin.readline, stdout.write n, t = list(map(int, input().split())) a = [] for i in range(n): a.append(list(map(int, input().split())) + [i + 1]) a = sorted(a, key=lambda x: x[1]) l = 0 r = n while r - l > 1: mid = (r + l) // 2 ans = get_answer(a, mid, t) if ans: l = mid else: r = mid ans = get_answer(a, r, t) if ans: case = r else: case = l final_ans = get_answer(a, case, t, return_ans=True) print(str(len(final_ans))) print("\n") print(str(len(final_ans))) print("\n") s = " ".join(map(str, final_ans)) print(s)
FUNC_DEF NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR IF VAR RETURN VAR IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR LIST BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are preparing for an exam on scheduling theory. The exam will last for exactly T milliseconds and will consist of n problems. You can either solve problem i in exactly t_{i} milliseconds or ignore it and spend no time. You don't need time to rest after solving a problem, either. Unfortunately, your teacher considers some of the problems too easy for you. Thus, he assigned an integer a_{i} to every problem i meaning that the problem i can bring you a point to the final score only in case you have solved no more than a_{i} problems overall (including problem i). Formally, suppose you solve problems p_1, p_2, ..., p_{k} during the exam. Then, your final score s will be equal to the number of values of j between 1 and k such that k ≀ a_{p}_{j}. You have guessed that the real first problem of the exam is already in front of you. Therefore, you want to choose a set of problems to solve during the exam maximizing your final score in advance. Don't forget that the exam is limited in time, and you must have enough time to solve all chosen problems. If there exist different sets of problems leading to the maximum final score, any of them will do. -----Input----- The first line contains two integers n and T (1 ≀ n ≀ 2Β·10^5; 1 ≀ T ≀ 10^9)Β β€” the number of problems in the exam and the length of the exam in milliseconds, respectively. Each of the next n lines contains two integers a_{i} and t_{i} (1 ≀ a_{i} ≀ n; 1 ≀ t_{i} ≀ 10^4). The problems are numbered from 1 to n. -----Output----- In the first line, output a single integer sΒ β€” your maximum possible final score. In the second line, output a single integer k (0 ≀ k ≀ n)Β β€” the number of problems you should solve. In the third line, output k distinct integers p_1, p_2, ..., p_{k} (1 ≀ p_{i} ≀ n)Β β€” the indexes of problems you should solve, in any order. If there are several optimal sets of problems, you may output any of them. -----Examples----- Input 5 300 3 100 4 150 4 80 2 90 2 300 Output 2 3 3 1 4 Input 2 100 1 787 2 788 Output 0 0 Input 2 100 2 42 2 58 Output 2 2 1 2 -----Note----- In the first example, you should solve problems 3, 1, and 4. In this case you'll spend 80 + 100 + 90 = 270 milliseconds, falling within the length of the exam, 300 milliseconds (and even leaving yourself 30 milliseconds to have a rest). Problems 3 and 1 will bring you a point each, while problem 4 won't. You'll score two points. In the second example, the length of the exam is catastrophically not enough to solve even a single problem. In the third example, you have just enough time to solve both problems in 42 + 58 = 100 milliseconds and hand your solutions to the teacher with a smile.
n, t = list(map(int, input().split())) ts = [] for i in range(n): a, c = list(map(int, input().split())) ts.append((a, c, i)) ts = sorted(ts, key=lambda x: x[1]) ans = 0 ptr = 0 hc = {} hs = {} csize = 0 ctime = 0 for i in range(n): n_ans = ans + 1 if n_ans - 1 in hc: csize -= hc[n_ans - 1] ctime -= hs[n_ans - 1] fail = False while csize < n_ans: if ptr == n: fail = True break a, c, num = ts[ptr] if a >= n_ans: csize += 1 ctime += c if a not in hc: hc[a] = 0 hc[a] += 1 if a not in hs: hs[a] = 0 hs[a] += c ptr += 1 if ctime > t: fail = True break if fail: break else: ans = n_ans print(ans) print(ans) tks = [] for i in range(ptr): if len(tks) == ans: break if ts[i][0] >= ans: tks.append(ts[i][2]) print(" ".join([str(i + 1) for i in tks]))
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR
You are preparing for an exam on scheduling theory. The exam will last for exactly T milliseconds and will consist of n problems. You can either solve problem i in exactly t_{i} milliseconds or ignore it and spend no time. You don't need time to rest after solving a problem, either. Unfortunately, your teacher considers some of the problems too easy for you. Thus, he assigned an integer a_{i} to every problem i meaning that the problem i can bring you a point to the final score only in case you have solved no more than a_{i} problems overall (including problem i). Formally, suppose you solve problems p_1, p_2, ..., p_{k} during the exam. Then, your final score s will be equal to the number of values of j between 1 and k such that k ≀ a_{p}_{j}. You have guessed that the real first problem of the exam is already in front of you. Therefore, you want to choose a set of problems to solve during the exam maximizing your final score in advance. Don't forget that the exam is limited in time, and you must have enough time to solve all chosen problems. If there exist different sets of problems leading to the maximum final score, any of them will do. -----Input----- The first line contains two integers n and T (1 ≀ n ≀ 2Β·10^5; 1 ≀ T ≀ 10^9)Β β€” the number of problems in the exam and the length of the exam in milliseconds, respectively. Each of the next n lines contains two integers a_{i} and t_{i} (1 ≀ a_{i} ≀ n; 1 ≀ t_{i} ≀ 10^4). The problems are numbered from 1 to n. -----Output----- In the first line, output a single integer sΒ β€” your maximum possible final score. In the second line, output a single integer k (0 ≀ k ≀ n)Β β€” the number of problems you should solve. In the third line, output k distinct integers p_1, p_2, ..., p_{k} (1 ≀ p_{i} ≀ n)Β β€” the indexes of problems you should solve, in any order. If there are several optimal sets of problems, you may output any of them. -----Examples----- Input 5 300 3 100 4 150 4 80 2 90 2 300 Output 2 3 3 1 4 Input 2 100 1 787 2 788 Output 0 0 Input 2 100 2 42 2 58 Output 2 2 1 2 -----Note----- In the first example, you should solve problems 3, 1, and 4. In this case you'll spend 80 + 100 + 90 = 270 milliseconds, falling within the length of the exam, 300 milliseconds (and even leaving yourself 30 milliseconds to have a rest). Problems 3 and 1 will bring you a point each, while problem 4 won't. You'll score two points. In the second example, the length of the exam is catastrophically not enough to solve even a single problem. In the third example, you have just enough time to solve both problems in 42 + 58 = 100 milliseconds and hand your solutions to the teacher with a smile.
import itertools n: int T: int n, T = list(map(int, input().split())) listi: list = [] tsum: int = 0 csum: int = 0 k: int k = 1 a: int b: int t: int ind: int for i in range(n): a, b = list(map(int, input().split())) listi.append((a, b, i)) listi = sorted(listi, key=lambda x: x[1]) time: list = [0] * (n + 1) count: list = [0] * (n + 1) for a, t, ind in listi: if k <= a: tsum += t time[a] += t if tsum > T: break count[a] += 1 csum += 1 if csum == k: csum -= count[k] tsum -= time[k] k += 1 max_score: int = max(csum, k - 1) print(max_score, max_score, sep="\n") print( *list( itertools.islice((idx + 1 for a, t, idx in listi if a >= max_score), max_score) ) )
IMPORT VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR LIST VAR VAR NUMBER VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR
For a vector $\vec{v} = (x, y)$, define $|v| = \sqrt{x^2 + y^2}$. Allen had a bit too much to drink at the bar, which is at the origin. There are $n$ vectors $\vec{v_1}, \vec{v_2}, \cdots, \vec{v_n}$. Allen will make $n$ moves. As Allen's sense of direction is impaired, during the $i$-th move he will either move in the direction $\vec{v_i}$ or $-\vec{v_i}$. In other words, if his position is currently $p = (x, y)$, he will either move to $p + \vec{v_i}$ or $p - \vec{v_i}$. Allen doesn't want to wander too far from home (which happens to also be the bar). You need to help him figure out a sequence of moves (a sequence of signs for the vectors) such that his final position $p$ satisfies $|p| \le 1.5 \cdot 10^6$ so that he can stay safe. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of moves. Each of the following lines contains two space-separated integers $x_i$ and $y_i$, meaning that $\vec{v_i} = (x_i, y_i)$. We have that $|v_i| \le 10^6$ for all $i$. -----Output----- Output a single line containing $n$ integers $c_1, c_2, \cdots, c_n$, each of which is either $1$ or $-1$. Your solution is correct if the value of $p = \sum_{i = 1}^n c_i \vec{v_i}$, satisfies $|p| \le 1.5 \cdot 10^6$. It can be shown that a solution always exists under the given constraints. -----Examples----- Input 3 999999 0 0 999999 999999 0 Output 1 1 -1 Input 1 -824590 246031 Output 1 Input 8 -67761 603277 640586 -396671 46147 -122580 569609 -2112 400 914208 131792 309779 -850150 -486293 5272 721899 Output 1 1 1 1 1 1 1 -1
import time def len2(x, y): return x * x + y * y def test(v1, v2): x1 = v1[0] + v2[0] y1 = v1[1] + v2[1] x2 = v1[0] - v2[0] y2 = v1[1] - v2[1] l1 = len2(x1, y1) l2 = len2(x2, y2) if l1 <= l2: xnew = x1 ynew = y1 l = l1 flag = 1 else: xnew = x2 ynew = y2 l = l2 flag = -1 return xnew, ynew, l, flag n = int(input()) v = [] ans = [(1) for i in range(n)] r = 1000000**2 for i in range(n): x, y = (int(j) for j in input().split()) v.append([x, y]) start = time.time() if n > 1: v1 = v[0] s1 = [0] v2 = v[1] s2 = [1] for i in range(2, n): v3 = v[i] s3 = [i] xnew, ynew, l, flag = test(v1, v2) if l > r: xnew, ynew, t, flag = test(v1, v3) if t > r: xnew, ynew, t, flag = test(v2, v3) if flag == -1: for j in s3: ans[j] *= -1 v2 = [xnew, ynew] s2 += s3 else: if flag == -1: for j in s3: ans[j] *= -1 v1 = [xnew, ynew] s1 += s3 else: if flag == -1: for j in s2: ans[j] *= -1 v1 = [xnew, ynew] s1 += s2 v2 = v3 s2 = s3 xnew, ynew, t, flag = test(v1, v2) if flag == -1: for j in s2: ans[j] *= -1 for i in range(n): print(ans[i], end=" ") print() finish = time.time()
IMPORT FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER RETURN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST VAR VAR VAR VAR IF VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST VAR VAR VAR VAR IF VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER FOR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR
For a vector $\vec{v} = (x, y)$, define $|v| = \sqrt{x^2 + y^2}$. Allen had a bit too much to drink at the bar, which is at the origin. There are $n$ vectors $\vec{v_1}, \vec{v_2}, \cdots, \vec{v_n}$. Allen will make $n$ moves. As Allen's sense of direction is impaired, during the $i$-th move he will either move in the direction $\vec{v_i}$ or $-\vec{v_i}$. In other words, if his position is currently $p = (x, y)$, he will either move to $p + \vec{v_i}$ or $p - \vec{v_i}$. Allen doesn't want to wander too far from home (which happens to also be the bar). You need to help him figure out a sequence of moves (a sequence of signs for the vectors) such that his final position $p$ satisfies $|p| \le 1.5 \cdot 10^6$ so that he can stay safe. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of moves. Each of the following lines contains two space-separated integers $x_i$ and $y_i$, meaning that $\vec{v_i} = (x_i, y_i)$. We have that $|v_i| \le 10^6$ for all $i$. -----Output----- Output a single line containing $n$ integers $c_1, c_2, \cdots, c_n$, each of which is either $1$ or $-1$. Your solution is correct if the value of $p = \sum_{i = 1}^n c_i \vec{v_i}$, satisfies $|p| \le 1.5 \cdot 10^6$. It can be shown that a solution always exists under the given constraints. -----Examples----- Input 3 999999 0 0 999999 999999 0 Output 1 1 -1 Input 1 -824590 246031 Output 1 Input 8 -67761 603277 640586 -396671 46147 -122580 569609 -2112 400 914208 131792 309779 -850150 -486293 5272 721899 Output 1 1 1 1 1 1 1 -1
def my_cmp(x): if x[0] == 0: return float("inf") return x[1] / x[0] def dis(a, b): return a * a + b * b n = int(input()) v = [] for i in range(n): x, y = map(int, input().split()) v.append((x, y, i)) v.sort(key=my_cmp) x, y = 0, 0 ans = [0] * n for i in range(n): if dis(x + v[i][0], y + v[i][1]) < dis(x - v[i][0], y - v[i][1]): ans[v[i][2]] = 1 else: ans[v[i][2]] = -1 x += v[i][0] * ans[v[i][2]] y += v[i][1] * ans[v[i][2]] for x in ans: print(x, end=" ")
FUNC_DEF IF VAR NUMBER NUMBER RETURN FUNC_CALL VAR STRING RETURN BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a tree with $N$ vertices (numbered $1$ through $N$). Its edges are numbered $1$ through $N-1$. For each valid $i$, there is an integer $a_{i}$ written on the $i$-th vertex. Also, for each valid $i$, there is an integer $b_{i}$ written on the $i$-th edge. You want the following condition to be satisfied: for each vertex $v$ and each edge $e$ adjacent to $v$, $a_{v} β‰₯ b_{e}$. In order to achieve that, you may perform an arbitrary number of steps (including zero). In one step, you may perform one the following operations: 1. Select two different vertices $u$ and $v$. Swap $a_{u}$ and $a_{v}$. 2. Select two different edges $e$ and $f$. Swap $b_{e}$ and $b_{f}$. 3. Select a vertex $v$ and an integer $x$. Change $a_{v}$ to $x$ and pay one coin. Calculate the minimum number of coins you need in order to satisfy the required condition. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N-1$ lines follow. For each $i$ ($1 ≀ i ≀ N-1$), the $i$-th of these lines contains three space-separated integers $u_{i}$, $v_{i}$ and $b_{i}$ denoting that the $i$-th edge connects vertices $u_{i}$ and $v_{i}$ and the initial value written on it is $b_{i}$. The last line contains $N$ space-separated integers $a_{1}, a_{2}, \ldots, a_{N}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum necessary number of coins. ------ Constraints ------ $1 ≀ T ≀ 10$ $2 ≀ N ≀ 100,000$ $1 ≀ u_{i}, v_{i} ≀ N$ for each valid $i$ $1 ≀ b_{i} ≀ 10^{9}$ for each valid $i$ $1 ≀ a_{i} ≀ 10^{9}$ for each valid $i$ the graph on the input is a tree ------ Subtasks ------ Subtask #1 (10 points): $N ≀ 7$ Subtask #2 (10 points): $N ≀ 10$ Subtask #3 (30 points): $N ≀ 200$ Subtask #4 (50 points): original constraints ----- Sample Input 1 ------ 1 3 1 2 4 2 3 7 1 5 10 ----- Sample Output 1 ------ 1 ----- explanation 1 ------ Example case 1: There is no way to satisfy the required condition without paying any coins. When we have one coin, we can perform the following operations: - Swap the integers written on vertices $1$ and $2$. - Pay one coin and change the integer written on vertex $2$ to $7$.
t = int(input()) for _ in range(t): n = int(input()) edges = [] for i in range(n - 1): inp = list(map(int, input().strip().split(" "))) edges.append(inp[2]) ver = list(map(int, input().strip().split(" "))) edges.sort() ver.sort() count = 0 i = 0 j = 0 while i < n and j < n - 1: if edges[j] <= ver[i]: j += 1 i += 1 else: count += 1 i += 1 print(count)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a tree with $N$ vertices (numbered $1$ through $N$). Its edges are numbered $1$ through $N-1$. For each valid $i$, there is an integer $a_{i}$ written on the $i$-th vertex. Also, for each valid $i$, there is an integer $b_{i}$ written on the $i$-th edge. You want the following condition to be satisfied: for each vertex $v$ and each edge $e$ adjacent to $v$, $a_{v} β‰₯ b_{e}$. In order to achieve that, you may perform an arbitrary number of steps (including zero). In one step, you may perform one the following operations: 1. Select two different vertices $u$ and $v$. Swap $a_{u}$ and $a_{v}$. 2. Select two different edges $e$ and $f$. Swap $b_{e}$ and $b_{f}$. 3. Select a vertex $v$ and an integer $x$. Change $a_{v}$ to $x$ and pay one coin. Calculate the minimum number of coins you need in order to satisfy the required condition. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N-1$ lines follow. For each $i$ ($1 ≀ i ≀ N-1$), the $i$-th of these lines contains three space-separated integers $u_{i}$, $v_{i}$ and $b_{i}$ denoting that the $i$-th edge connects vertices $u_{i}$ and $v_{i}$ and the initial value written on it is $b_{i}$. The last line contains $N$ space-separated integers $a_{1}, a_{2}, \ldots, a_{N}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum necessary number of coins. ------ Constraints ------ $1 ≀ T ≀ 10$ $2 ≀ N ≀ 100,000$ $1 ≀ u_{i}, v_{i} ≀ N$ for each valid $i$ $1 ≀ b_{i} ≀ 10^{9}$ for each valid $i$ $1 ≀ a_{i} ≀ 10^{9}$ for each valid $i$ the graph on the input is a tree ------ Subtasks ------ Subtask #1 (10 points): $N ≀ 7$ Subtask #2 (10 points): $N ≀ 10$ Subtask #3 (30 points): $N ≀ 200$ Subtask #4 (50 points): original constraints ----- Sample Input 1 ------ 1 3 1 2 4 2 3 7 1 5 10 ----- Sample Output 1 ------ 1 ----- explanation 1 ------ Example case 1: There is no way to satisfy the required condition without paying any coins. When we have one coin, we can perform the following operations: - Swap the integers written on vertices $1$ and $2$. - Pay one coin and change the integer written on vertex $2$ to $7$.
for i in range(int(input())): n, p = int(input()), [] for j in range(n - 1): p.append([int(j) for j in input().split()][2]) q = sorted([int(j) for j in input().split()]) p.sort() c, j, k = n, 0, 0 while k < n: if q[k] >= p[0]: k, c = k + 1, c - 1 break k += 1 while k < n and j < n - 2: if q[k] >= p[j] and q[k] >= p[j + 1]: j, k, c = j + 1, k + 1, c - 1 else: k += 1 print(c - int(k != n and q[-1] >= p[-1]))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a tree with $N$ vertices (numbered $1$ through $N$). Its edges are numbered $1$ through $N-1$. For each valid $i$, there is an integer $a_{i}$ written on the $i$-th vertex. Also, for each valid $i$, there is an integer $b_{i}$ written on the $i$-th edge. You want the following condition to be satisfied: for each vertex $v$ and each edge $e$ adjacent to $v$, $a_{v} β‰₯ b_{e}$. In order to achieve that, you may perform an arbitrary number of steps (including zero). In one step, you may perform one the following operations: 1. Select two different vertices $u$ and $v$. Swap $a_{u}$ and $a_{v}$. 2. Select two different edges $e$ and $f$. Swap $b_{e}$ and $b_{f}$. 3. Select a vertex $v$ and an integer $x$. Change $a_{v}$ to $x$ and pay one coin. Calculate the minimum number of coins you need in order to satisfy the required condition. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N-1$ lines follow. For each $i$ ($1 ≀ i ≀ N-1$), the $i$-th of these lines contains three space-separated integers $u_{i}$, $v_{i}$ and $b_{i}$ denoting that the $i$-th edge connects vertices $u_{i}$ and $v_{i}$ and the initial value written on it is $b_{i}$. The last line contains $N$ space-separated integers $a_{1}, a_{2}, \ldots, a_{N}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum necessary number of coins. ------ Constraints ------ $1 ≀ T ≀ 10$ $2 ≀ N ≀ 100,000$ $1 ≀ u_{i}, v_{i} ≀ N$ for each valid $i$ $1 ≀ b_{i} ≀ 10^{9}$ for each valid $i$ $1 ≀ a_{i} ≀ 10^{9}$ for each valid $i$ the graph on the input is a tree ------ Subtasks ------ Subtask #1 (10 points): $N ≀ 7$ Subtask #2 (10 points): $N ≀ 10$ Subtask #3 (30 points): $N ≀ 200$ Subtask #4 (50 points): original constraints ----- Sample Input 1 ------ 1 3 1 2 4 2 3 7 1 5 10 ----- Sample Output 1 ------ 1 ----- explanation 1 ------ Example case 1: There is no way to satisfy the required condition without paying any coins. When we have one coin, we can perform the following operations: - Swap the integers written on vertices $1$ and $2$. - Pay one coin and change the integer written on vertex $2$ to $7$.
for _ in range(int(input())): n = int(input()) edge_nums = [0] * (n - 1) for i in range(n - 1): x, y, z = map(int, input().split()) edge_nums[i] = z edge_nums.sort() vertex_nums = list(map(int, input().split())) vertex_nums.sort() j = 0 count = 0 for i in range(n): if vertex_nums[i] < edge_nums[j]: count += 1 else: j += 1 if j == n - 1: break print(count)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a tree with $N$ vertices (numbered $1$ through $N$). Its edges are numbered $1$ through $N-1$. For each valid $i$, there is an integer $a_{i}$ written on the $i$-th vertex. Also, for each valid $i$, there is an integer $b_{i}$ written on the $i$-th edge. You want the following condition to be satisfied: for each vertex $v$ and each edge $e$ adjacent to $v$, $a_{v} β‰₯ b_{e}$. In order to achieve that, you may perform an arbitrary number of steps (including zero). In one step, you may perform one the following operations: 1. Select two different vertices $u$ and $v$. Swap $a_{u}$ and $a_{v}$. 2. Select two different edges $e$ and $f$. Swap $b_{e}$ and $b_{f}$. 3. Select a vertex $v$ and an integer $x$. Change $a_{v}$ to $x$ and pay one coin. Calculate the minimum number of coins you need in order to satisfy the required condition. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N-1$ lines follow. For each $i$ ($1 ≀ i ≀ N-1$), the $i$-th of these lines contains three space-separated integers $u_{i}$, $v_{i}$ and $b_{i}$ denoting that the $i$-th edge connects vertices $u_{i}$ and $v_{i}$ and the initial value written on it is $b_{i}$. The last line contains $N$ space-separated integers $a_{1}, a_{2}, \ldots, a_{N}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum necessary number of coins. ------ Constraints ------ $1 ≀ T ≀ 10$ $2 ≀ N ≀ 100,000$ $1 ≀ u_{i}, v_{i} ≀ N$ for each valid $i$ $1 ≀ b_{i} ≀ 10^{9}$ for each valid $i$ $1 ≀ a_{i} ≀ 10^{9}$ for each valid $i$ the graph on the input is a tree ------ Subtasks ------ Subtask #1 (10 points): $N ≀ 7$ Subtask #2 (10 points): $N ≀ 10$ Subtask #3 (30 points): $N ≀ 200$ Subtask #4 (50 points): original constraints ----- Sample Input 1 ------ 1 3 1 2 4 2 3 7 1 5 10 ----- Sample Output 1 ------ 1 ----- explanation 1 ------ Example case 1: There is no way to satisfy the required condition without paying any coins. When we have one coin, we can perform the following operations: - Swap the integers written on vertices $1$ and $2$. - Pay one coin and change the integer written on vertex $2$ to $7$.
t = int(input()) for i in range(t): N = int(input()) edges = [(0) for x in range(N - 1)] for p in range(N - 1): _, _, edges[p] = [int(x) for x in input().split()] vertices = [int(x) for x in input().split()] vertices.sort() edges.sort() max1 = 0 count = 0 for t in range(N - 1): if vertices[t] >= edges[max1]: max1 += 1 else: count += 1 if max1 < N - 1: if vertices[-1] < edges[max1]: count += 1 print(count)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a tree with $N$ vertices (numbered $1$ through $N$). Its edges are numbered $1$ through $N-1$. For each valid $i$, there is an integer $a_{i}$ written on the $i$-th vertex. Also, for each valid $i$, there is an integer $b_{i}$ written on the $i$-th edge. You want the following condition to be satisfied: for each vertex $v$ and each edge $e$ adjacent to $v$, $a_{v} β‰₯ b_{e}$. In order to achieve that, you may perform an arbitrary number of steps (including zero). In one step, you may perform one the following operations: 1. Select two different vertices $u$ and $v$. Swap $a_{u}$ and $a_{v}$. 2. Select two different edges $e$ and $f$. Swap $b_{e}$ and $b_{f}$. 3. Select a vertex $v$ and an integer $x$. Change $a_{v}$ to $x$ and pay one coin. Calculate the minimum number of coins you need in order to satisfy the required condition. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N-1$ lines follow. For each $i$ ($1 ≀ i ≀ N-1$), the $i$-th of these lines contains three space-separated integers $u_{i}$, $v_{i}$ and $b_{i}$ denoting that the $i$-th edge connects vertices $u_{i}$ and $v_{i}$ and the initial value written on it is $b_{i}$. The last line contains $N$ space-separated integers $a_{1}, a_{2}, \ldots, a_{N}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum necessary number of coins. ------ Constraints ------ $1 ≀ T ≀ 10$ $2 ≀ N ≀ 100,000$ $1 ≀ u_{i}, v_{i} ≀ N$ for each valid $i$ $1 ≀ b_{i} ≀ 10^{9}$ for each valid $i$ $1 ≀ a_{i} ≀ 10^{9}$ for each valid $i$ the graph on the input is a tree ------ Subtasks ------ Subtask #1 (10 points): $N ≀ 7$ Subtask #2 (10 points): $N ≀ 10$ Subtask #3 (30 points): $N ≀ 200$ Subtask #4 (50 points): original constraints ----- Sample Input 1 ------ 1 3 1 2 4 2 3 7 1 5 10 ----- Sample Output 1 ------ 1 ----- explanation 1 ------ Example case 1: There is no way to satisfy the required condition without paying any coins. When we have one coin, we can perform the following operations: - Swap the integers written on vertices $1$ and $2$. - Pay one coin and change the integer written on vertex $2$ to $7$.
for i in range(int(input())): n = int(input()) edg = [] for i in range(n - 1): c = int(input().split()[2]) edg.append(c) ver = list(map(int, input().split())) edg.sort() ver.sort() a = 0 b = 0 while a < n and b < n - 1: if ver[a] >= edg[b]: b += 1 a += 1 if a == n - 1: b += 1 print(n - b)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a tree with $N$ vertices (numbered $1$ through $N$). Its edges are numbered $1$ through $N-1$. For each valid $i$, there is an integer $a_{i}$ written on the $i$-th vertex. Also, for each valid $i$, there is an integer $b_{i}$ written on the $i$-th edge. You want the following condition to be satisfied: for each vertex $v$ and each edge $e$ adjacent to $v$, $a_{v} β‰₯ b_{e}$. In order to achieve that, you may perform an arbitrary number of steps (including zero). In one step, you may perform one the following operations: 1. Select two different vertices $u$ and $v$. Swap $a_{u}$ and $a_{v}$. 2. Select two different edges $e$ and $f$. Swap $b_{e}$ and $b_{f}$. 3. Select a vertex $v$ and an integer $x$. Change $a_{v}$ to $x$ and pay one coin. Calculate the minimum number of coins you need in order to satisfy the required condition. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N-1$ lines follow. For each $i$ ($1 ≀ i ≀ N-1$), the $i$-th of these lines contains three space-separated integers $u_{i}$, $v_{i}$ and $b_{i}$ denoting that the $i$-th edge connects vertices $u_{i}$ and $v_{i}$ and the initial value written on it is $b_{i}$. The last line contains $N$ space-separated integers $a_{1}, a_{2}, \ldots, a_{N}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum necessary number of coins. ------ Constraints ------ $1 ≀ T ≀ 10$ $2 ≀ N ≀ 100,000$ $1 ≀ u_{i}, v_{i} ≀ N$ for each valid $i$ $1 ≀ b_{i} ≀ 10^{9}$ for each valid $i$ $1 ≀ a_{i} ≀ 10^{9}$ for each valid $i$ the graph on the input is a tree ------ Subtasks ------ Subtask #1 (10 points): $N ≀ 7$ Subtask #2 (10 points): $N ≀ 10$ Subtask #3 (30 points): $N ≀ 200$ Subtask #4 (50 points): original constraints ----- Sample Input 1 ------ 1 3 1 2 4 2 3 7 1 5 10 ----- Sample Output 1 ------ 1 ----- explanation 1 ------ Example case 1: There is no way to satisfy the required condition without paying any coins. When we have one coin, we can perform the following operations: - Swap the integers written on vertices $1$ and $2$. - Pay one coin and change the integer written on vertex $2$ to $7$.
for t in range(int(input())): n = int(input()) edges = [] vertices = [] for i in range(n - 1): u, v, w = map(int, input().split(" ")) edges.append(w) vertices = list(map(int, input().split(" "))) edges.sort(key=None, reverse=True) vertices.sort(key=None, reverse=True) edges.insert(0, edges[0]) i = 0 j = 0 ans = 0 while i < n and j < n: if vertices[j] >= edges[i]: j += 1 i += 1 else: ans += 1 i += 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NONE NUMBER EXPR FUNC_CALL VAR NONE NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a tree with $N$ vertices (numbered $1$ through $N$). Its edges are numbered $1$ through $N-1$. For each valid $i$, there is an integer $a_{i}$ written on the $i$-th vertex. Also, for each valid $i$, there is an integer $b_{i}$ written on the $i$-th edge. You want the following condition to be satisfied: for each vertex $v$ and each edge $e$ adjacent to $v$, $a_{v} β‰₯ b_{e}$. In order to achieve that, you may perform an arbitrary number of steps (including zero). In one step, you may perform one the following operations: 1. Select two different vertices $u$ and $v$. Swap $a_{u}$ and $a_{v}$. 2. Select two different edges $e$ and $f$. Swap $b_{e}$ and $b_{f}$. 3. Select a vertex $v$ and an integer $x$. Change $a_{v}$ to $x$ and pay one coin. Calculate the minimum number of coins you need in order to satisfy the required condition. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N-1$ lines follow. For each $i$ ($1 ≀ i ≀ N-1$), the $i$-th of these lines contains three space-separated integers $u_{i}$, $v_{i}$ and $b_{i}$ denoting that the $i$-th edge connects vertices $u_{i}$ and $v_{i}$ and the initial value written on it is $b_{i}$. The last line contains $N$ space-separated integers $a_{1}, a_{2}, \ldots, a_{N}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum necessary number of coins. ------ Constraints ------ $1 ≀ T ≀ 10$ $2 ≀ N ≀ 100,000$ $1 ≀ u_{i}, v_{i} ≀ N$ for each valid $i$ $1 ≀ b_{i} ≀ 10^{9}$ for each valid $i$ $1 ≀ a_{i} ≀ 10^{9}$ for each valid $i$ the graph on the input is a tree ------ Subtasks ------ Subtask #1 (10 points): $N ≀ 7$ Subtask #2 (10 points): $N ≀ 10$ Subtask #3 (30 points): $N ≀ 200$ Subtask #4 (50 points): original constraints ----- Sample Input 1 ------ 1 3 1 2 4 2 3 7 1 5 10 ----- Sample Output 1 ------ 1 ----- explanation 1 ------ Example case 1: There is no way to satisfy the required condition without paying any coins. When we have one coin, we can perform the following operations: - Swap the integers written on vertices $1$ and $2$. - Pay one coin and change the integer written on vertex $2$ to $7$.
T = int(input()) for _ in range(T): N = int(input()) C = [] for i in range(N - 1): u, v, b = map(int, input().split()) C.append(b) C.sort() C.append(C[len(C) - 1]) B = list(map(int, input().split())) B.sort() coin = 0 j = N - 1 for i in range(N - 1, -1, -1): if B[j] < C[i]: coin = coin + 1 else: j = j - 1 print(coin)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a tree with $N$ vertices (numbered $1$ through $N$). Its edges are numbered $1$ through $N-1$. For each valid $i$, there is an integer $a_{i}$ written on the $i$-th vertex. Also, for each valid $i$, there is an integer $b_{i}$ written on the $i$-th edge. You want the following condition to be satisfied: for each vertex $v$ and each edge $e$ adjacent to $v$, $a_{v} β‰₯ b_{e}$. In order to achieve that, you may perform an arbitrary number of steps (including zero). In one step, you may perform one the following operations: 1. Select two different vertices $u$ and $v$. Swap $a_{u}$ and $a_{v}$. 2. Select two different edges $e$ and $f$. Swap $b_{e}$ and $b_{f}$. 3. Select a vertex $v$ and an integer $x$. Change $a_{v}$ to $x$ and pay one coin. Calculate the minimum number of coins you need in order to satisfy the required condition. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N-1$ lines follow. For each $i$ ($1 ≀ i ≀ N-1$), the $i$-th of these lines contains three space-separated integers $u_{i}$, $v_{i}$ and $b_{i}$ denoting that the $i$-th edge connects vertices $u_{i}$ and $v_{i}$ and the initial value written on it is $b_{i}$. The last line contains $N$ space-separated integers $a_{1}, a_{2}, \ldots, a_{N}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum necessary number of coins. ------ Constraints ------ $1 ≀ T ≀ 10$ $2 ≀ N ≀ 100,000$ $1 ≀ u_{i}, v_{i} ≀ N$ for each valid $i$ $1 ≀ b_{i} ≀ 10^{9}$ for each valid $i$ $1 ≀ a_{i} ≀ 10^{9}$ for each valid $i$ the graph on the input is a tree ------ Subtasks ------ Subtask #1 (10 points): $N ≀ 7$ Subtask #2 (10 points): $N ≀ 10$ Subtask #3 (30 points): $N ≀ 200$ Subtask #4 (50 points): original constraints ----- Sample Input 1 ------ 1 3 1 2 4 2 3 7 1 5 10 ----- Sample Output 1 ------ 1 ----- explanation 1 ------ Example case 1: There is no way to satisfy the required condition without paying any coins. When we have one coin, we can perform the following operations: - Swap the integers written on vertices $1$ and $2$. - Pay one coin and change the integer written on vertex $2$ to $7$.
T = int(input()) for i in range(0, T): N = int(input()) e = [] r = [] v = [] h = 0 o = 0 for j in range(0, N - 1): e = input().split(" ") r.append(int(e[2])) v = list(map(int, input().split())) v.sort() r.sort() wa = N - 2 w = N - 2 z = r[N - 2] while wa >= 0: if v[w] < r[wa] and v[w + 1] < r[wa]: v.append(z) v.append(z) w = w + 2 o += 2 elif v[w] < r[wa]: v.append(z) w += 1 o += 1 w -= 1 wa -= 1 print(o)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a tree with $N$ vertices (numbered $1$ through $N$). Its edges are numbered $1$ through $N-1$. For each valid $i$, there is an integer $a_{i}$ written on the $i$-th vertex. Also, for each valid $i$, there is an integer $b_{i}$ written on the $i$-th edge. You want the following condition to be satisfied: for each vertex $v$ and each edge $e$ adjacent to $v$, $a_{v} β‰₯ b_{e}$. In order to achieve that, you may perform an arbitrary number of steps (including zero). In one step, you may perform one the following operations: 1. Select two different vertices $u$ and $v$. Swap $a_{u}$ and $a_{v}$. 2. Select two different edges $e$ and $f$. Swap $b_{e}$ and $b_{f}$. 3. Select a vertex $v$ and an integer $x$. Change $a_{v}$ to $x$ and pay one coin. Calculate the minimum number of coins you need in order to satisfy the required condition. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N-1$ lines follow. For each $i$ ($1 ≀ i ≀ N-1$), the $i$-th of these lines contains three space-separated integers $u_{i}$, $v_{i}$ and $b_{i}$ denoting that the $i$-th edge connects vertices $u_{i}$ and $v_{i}$ and the initial value written on it is $b_{i}$. The last line contains $N$ space-separated integers $a_{1}, a_{2}, \ldots, a_{N}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum necessary number of coins. ------ Constraints ------ $1 ≀ T ≀ 10$ $2 ≀ N ≀ 100,000$ $1 ≀ u_{i}, v_{i} ≀ N$ for each valid $i$ $1 ≀ b_{i} ≀ 10^{9}$ for each valid $i$ $1 ≀ a_{i} ≀ 10^{9}$ for each valid $i$ the graph on the input is a tree ------ Subtasks ------ Subtask #1 (10 points): $N ≀ 7$ Subtask #2 (10 points): $N ≀ 10$ Subtask #3 (30 points): $N ≀ 200$ Subtask #4 (50 points): original constraints ----- Sample Input 1 ------ 1 3 1 2 4 2 3 7 1 5 10 ----- Sample Output 1 ------ 1 ----- explanation 1 ------ Example case 1: There is no way to satisfy the required condition without paying any coins. When we have one coin, we can perform the following operations: - Swap the integers written on vertices $1$ and $2$. - Pay one coin and change the integer written on vertex $2$ to $7$.
from sys import stdin, stdout n = stdin.readline() n = int(n) while n: n -= 1 m = stdin.readline() m = int(m) b = [] for i in range(0, m - 1): bb = [int(x) for x in stdin.readline().split()] b.append(bb[2]) a = [int(x) for x in stdin.readline().split()] b = sorted(b) a = sorted(a) c = 0 j = 0 for i in range(0, m): if a[i] >= b[j]: j += 1 else: c += 1 if j == len(b): break print(c)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a tree with $N$ vertices (numbered $1$ through $N$). Its edges are numbered $1$ through $N-1$. For each valid $i$, there is an integer $a_{i}$ written on the $i$-th vertex. Also, for each valid $i$, there is an integer $b_{i}$ written on the $i$-th edge. You want the following condition to be satisfied: for each vertex $v$ and each edge $e$ adjacent to $v$, $a_{v} β‰₯ b_{e}$. In order to achieve that, you may perform an arbitrary number of steps (including zero). In one step, you may perform one the following operations: 1. Select two different vertices $u$ and $v$. Swap $a_{u}$ and $a_{v}$. 2. Select two different edges $e$ and $f$. Swap $b_{e}$ and $b_{f}$. 3. Select a vertex $v$ and an integer $x$. Change $a_{v}$ to $x$ and pay one coin. Calculate the minimum number of coins you need in order to satisfy the required condition. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N-1$ lines follow. For each $i$ ($1 ≀ i ≀ N-1$), the $i$-th of these lines contains three space-separated integers $u_{i}$, $v_{i}$ and $b_{i}$ denoting that the $i$-th edge connects vertices $u_{i}$ and $v_{i}$ and the initial value written on it is $b_{i}$. The last line contains $N$ space-separated integers $a_{1}, a_{2}, \ldots, a_{N}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum necessary number of coins. ------ Constraints ------ $1 ≀ T ≀ 10$ $2 ≀ N ≀ 100,000$ $1 ≀ u_{i}, v_{i} ≀ N$ for each valid $i$ $1 ≀ b_{i} ≀ 10^{9}$ for each valid $i$ $1 ≀ a_{i} ≀ 10^{9}$ for each valid $i$ the graph on the input is a tree ------ Subtasks ------ Subtask #1 (10 points): $N ≀ 7$ Subtask #2 (10 points): $N ≀ 10$ Subtask #3 (30 points): $N ≀ 200$ Subtask #4 (50 points): original constraints ----- Sample Input 1 ------ 1 3 1 2 4 2 3 7 1 5 10 ----- Sample Output 1 ------ 1 ----- explanation 1 ------ Example case 1: There is no way to satisfy the required condition without paying any coins. When we have one coin, we can perform the following operations: - Swap the integers written on vertices $1$ and $2$. - Pay one coin and change the integer written on vertex $2$ to $7$.
def ii(): a = int(input()) return a def ai(): a = list(map(int, input().split())) return a def mi(): a = map(int, input().split()) return a for _ in range(ii()): n = ii() e = [] for i in range(n - 1): a, b, c = mi() e.append(c) v = ai() v.sort(reverse=True) e.sort(reverse=True) ans = 0 e.insert(0, e[0]) j = 0 for i in range(n): ew = e[i] if v[j] < ew: ans += 1 else: j += 1 print(ans)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a tree with $N$ vertices (numbered $1$ through $N$). Its edges are numbered $1$ through $N-1$. For each valid $i$, there is an integer $a_{i}$ written on the $i$-th vertex. Also, for each valid $i$, there is an integer $b_{i}$ written on the $i$-th edge. You want the following condition to be satisfied: for each vertex $v$ and each edge $e$ adjacent to $v$, $a_{v} β‰₯ b_{e}$. In order to achieve that, you may perform an arbitrary number of steps (including zero). In one step, you may perform one the following operations: 1. Select two different vertices $u$ and $v$. Swap $a_{u}$ and $a_{v}$. 2. Select two different edges $e$ and $f$. Swap $b_{e}$ and $b_{f}$. 3. Select a vertex $v$ and an integer $x$. Change $a_{v}$ to $x$ and pay one coin. Calculate the minimum number of coins you need in order to satisfy the required condition. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N-1$ lines follow. For each $i$ ($1 ≀ i ≀ N-1$), the $i$-th of these lines contains three space-separated integers $u_{i}$, $v_{i}$ and $b_{i}$ denoting that the $i$-th edge connects vertices $u_{i}$ and $v_{i}$ and the initial value written on it is $b_{i}$. The last line contains $N$ space-separated integers $a_{1}, a_{2}, \ldots, a_{N}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum necessary number of coins. ------ Constraints ------ $1 ≀ T ≀ 10$ $2 ≀ N ≀ 100,000$ $1 ≀ u_{i}, v_{i} ≀ N$ for each valid $i$ $1 ≀ b_{i} ≀ 10^{9}$ for each valid $i$ $1 ≀ a_{i} ≀ 10^{9}$ for each valid $i$ the graph on the input is a tree ------ Subtasks ------ Subtask #1 (10 points): $N ≀ 7$ Subtask #2 (10 points): $N ≀ 10$ Subtask #3 (30 points): $N ≀ 200$ Subtask #4 (50 points): original constraints ----- Sample Input 1 ------ 1 3 1 2 4 2 3 7 1 5 10 ----- Sample Output 1 ------ 1 ----- explanation 1 ------ Example case 1: There is no way to satisfy the required condition without paying any coins. When we have one coin, we can perform the following operations: - Swap the integers written on vertices $1$ and $2$. - Pay one coin and change the integer written on vertex $2$ to $7$.
t = int(input()) for p in range(t): edges = [] n = int(input()) for i in range(n - 1): a, b, c = input().split() c = int(c) edges.append(c) vertices = [int(x) for x in input().split()] vertices.sort() edges.sort() ans = 0 count = 0 while count < n: v1 = vertices.pop() e1 = edges.pop() if count == 0: count += 2 if v1 < e1: ans += 2 vertices.append(v1) else: v2 = vertices.pop() if v2 < e1: vertices.append(v2) ans += 1 else: count += 1 if v1 < e1: vertices.append(v1) ans += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a tree with $N$ vertices (numbered $1$ through $N$). Its edges are numbered $1$ through $N-1$. For each valid $i$, there is an integer $a_{i}$ written on the $i$-th vertex. Also, for each valid $i$, there is an integer $b_{i}$ written on the $i$-th edge. You want the following condition to be satisfied: for each vertex $v$ and each edge $e$ adjacent to $v$, $a_{v} β‰₯ b_{e}$. In order to achieve that, you may perform an arbitrary number of steps (including zero). In one step, you may perform one the following operations: 1. Select two different vertices $u$ and $v$. Swap $a_{u}$ and $a_{v}$. 2. Select two different edges $e$ and $f$. Swap $b_{e}$ and $b_{f}$. 3. Select a vertex $v$ and an integer $x$. Change $a_{v}$ to $x$ and pay one coin. Calculate the minimum number of coins you need in order to satisfy the required condition. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N-1$ lines follow. For each $i$ ($1 ≀ i ≀ N-1$), the $i$-th of these lines contains three space-separated integers $u_{i}$, $v_{i}$ and $b_{i}$ denoting that the $i$-th edge connects vertices $u_{i}$ and $v_{i}$ and the initial value written on it is $b_{i}$. The last line contains $N$ space-separated integers $a_{1}, a_{2}, \ldots, a_{N}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum necessary number of coins. ------ Constraints ------ $1 ≀ T ≀ 10$ $2 ≀ N ≀ 100,000$ $1 ≀ u_{i}, v_{i} ≀ N$ for each valid $i$ $1 ≀ b_{i} ≀ 10^{9}$ for each valid $i$ $1 ≀ a_{i} ≀ 10^{9}$ for each valid $i$ the graph on the input is a tree ------ Subtasks ------ Subtask #1 (10 points): $N ≀ 7$ Subtask #2 (10 points): $N ≀ 10$ Subtask #3 (30 points): $N ≀ 200$ Subtask #4 (50 points): original constraints ----- Sample Input 1 ------ 1 3 1 2 4 2 3 7 1 5 10 ----- Sample Output 1 ------ 1 ----- explanation 1 ------ Example case 1: There is no way to satisfy the required condition without paying any coins. When we have one coin, we can perform the following operations: - Swap the integers written on vertices $1$ and $2$. - Pay one coin and change the integer written on vertex $2$ to $7$.
T = int(input()) for t in range(T): N = int(input()) weight = [] for i in range(N - 1): weight.append([int(b) for b in input().split()][2]) weight.sort(reverse=True) weight = [weight[0]] + weight nodes = [int(b) for b in input().split()] nodes.sort(reverse=True) ans = 0 j = 0 for i in weight: if i > nodes[j]: ans += 1 else: j += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a tree with $N$ vertices (numbered $1$ through $N$). Its edges are numbered $1$ through $N-1$. For each valid $i$, there is an integer $a_{i}$ written on the $i$-th vertex. Also, for each valid $i$, there is an integer $b_{i}$ written on the $i$-th edge. You want the following condition to be satisfied: for each vertex $v$ and each edge $e$ adjacent to $v$, $a_{v} β‰₯ b_{e}$. In order to achieve that, you may perform an arbitrary number of steps (including zero). In one step, you may perform one the following operations: 1. Select two different vertices $u$ and $v$. Swap $a_{u}$ and $a_{v}$. 2. Select two different edges $e$ and $f$. Swap $b_{e}$ and $b_{f}$. 3. Select a vertex $v$ and an integer $x$. Change $a_{v}$ to $x$ and pay one coin. Calculate the minimum number of coins you need in order to satisfy the required condition. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N-1$ lines follow. For each $i$ ($1 ≀ i ≀ N-1$), the $i$-th of these lines contains three space-separated integers $u_{i}$, $v_{i}$ and $b_{i}$ denoting that the $i$-th edge connects vertices $u_{i}$ and $v_{i}$ and the initial value written on it is $b_{i}$. The last line contains $N$ space-separated integers $a_{1}, a_{2}, \ldots, a_{N}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum necessary number of coins. ------ Constraints ------ $1 ≀ T ≀ 10$ $2 ≀ N ≀ 100,000$ $1 ≀ u_{i}, v_{i} ≀ N$ for each valid $i$ $1 ≀ b_{i} ≀ 10^{9}$ for each valid $i$ $1 ≀ a_{i} ≀ 10^{9}$ for each valid $i$ the graph on the input is a tree ------ Subtasks ------ Subtask #1 (10 points): $N ≀ 7$ Subtask #2 (10 points): $N ≀ 10$ Subtask #3 (30 points): $N ≀ 200$ Subtask #4 (50 points): original constraints ----- Sample Input 1 ------ 1 3 1 2 4 2 3 7 1 5 10 ----- Sample Output 1 ------ 1 ----- explanation 1 ------ Example case 1: There is no way to satisfy the required condition without paying any coins. When we have one coin, we can perform the following operations: - Swap the integers written on vertices $1$ and $2$. - Pay one coin and change the integer written on vertex $2$ to $7$.
T = int(input()) for t in range(T): N = int(input()) edges = [] for n in range(N - 1): edges.append(int(input().split()[-1])) vertices = [int(num) for num in input().split()] edges.sort() vertices.sort() edges.append(edges[-1]) e = 0 v = 0 while v < N: if vertices[v] >= edges[e]: e += 1 v += 1 print(N - e)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a tree with $N$ vertices (numbered $1$ through $N$). Its edges are numbered $1$ through $N-1$. For each valid $i$, there is an integer $a_{i}$ written on the $i$-th vertex. Also, for each valid $i$, there is an integer $b_{i}$ written on the $i$-th edge. You want the following condition to be satisfied: for each vertex $v$ and each edge $e$ adjacent to $v$, $a_{v} β‰₯ b_{e}$. In order to achieve that, you may perform an arbitrary number of steps (including zero). In one step, you may perform one the following operations: 1. Select two different vertices $u$ and $v$. Swap $a_{u}$ and $a_{v}$. 2. Select two different edges $e$ and $f$. Swap $b_{e}$ and $b_{f}$. 3. Select a vertex $v$ and an integer $x$. Change $a_{v}$ to $x$ and pay one coin. Calculate the minimum number of coins you need in order to satisfy the required condition. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N-1$ lines follow. For each $i$ ($1 ≀ i ≀ N-1$), the $i$-th of these lines contains three space-separated integers $u_{i}$, $v_{i}$ and $b_{i}$ denoting that the $i$-th edge connects vertices $u_{i}$ and $v_{i}$ and the initial value written on it is $b_{i}$. The last line contains $N$ space-separated integers $a_{1}, a_{2}, \ldots, a_{N}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum necessary number of coins. ------ Constraints ------ $1 ≀ T ≀ 10$ $2 ≀ N ≀ 100,000$ $1 ≀ u_{i}, v_{i} ≀ N$ for each valid $i$ $1 ≀ b_{i} ≀ 10^{9}$ for each valid $i$ $1 ≀ a_{i} ≀ 10^{9}$ for each valid $i$ the graph on the input is a tree ------ Subtasks ------ Subtask #1 (10 points): $N ≀ 7$ Subtask #2 (10 points): $N ≀ 10$ Subtask #3 (30 points): $N ≀ 200$ Subtask #4 (50 points): original constraints ----- Sample Input 1 ------ 1 3 1 2 4 2 3 7 1 5 10 ----- Sample Output 1 ------ 1 ----- explanation 1 ------ Example case 1: There is no way to satisfy the required condition without paying any coins. When we have one coin, we can perform the following operations: - Swap the integers written on vertices $1$ and $2$. - Pay one coin and change the integer written on vertex $2$ to $7$.
T = int(input()) while T: T -= 1 N = int(input()) e = [] for i in range(N - 1): u, v, w = map(int, input().split()) e.append(w) n = list(map(int, input().split())) n.sort(reverse=True) e.sort(reverse=True) e.insert(1, e[0]) count = 0 j = 0 for i in range(N): if n[j] < e[i]: count += 1 else: j += 1 print(count)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a tree with $N$ vertices (numbered $1$ through $N$). Its edges are numbered $1$ through $N-1$. For each valid $i$, there is an integer $a_{i}$ written on the $i$-th vertex. Also, for each valid $i$, there is an integer $b_{i}$ written on the $i$-th edge. You want the following condition to be satisfied: for each vertex $v$ and each edge $e$ adjacent to $v$, $a_{v} β‰₯ b_{e}$. In order to achieve that, you may perform an arbitrary number of steps (including zero). In one step, you may perform one the following operations: 1. Select two different vertices $u$ and $v$. Swap $a_{u}$ and $a_{v}$. 2. Select two different edges $e$ and $f$. Swap $b_{e}$ and $b_{f}$. 3. Select a vertex $v$ and an integer $x$. Change $a_{v}$ to $x$ and pay one coin. Calculate the minimum number of coins you need in order to satisfy the required condition. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N-1$ lines follow. For each $i$ ($1 ≀ i ≀ N-1$), the $i$-th of these lines contains three space-separated integers $u_{i}$, $v_{i}$ and $b_{i}$ denoting that the $i$-th edge connects vertices $u_{i}$ and $v_{i}$ and the initial value written on it is $b_{i}$. The last line contains $N$ space-separated integers $a_{1}, a_{2}, \ldots, a_{N}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum necessary number of coins. ------ Constraints ------ $1 ≀ T ≀ 10$ $2 ≀ N ≀ 100,000$ $1 ≀ u_{i}, v_{i} ≀ N$ for each valid $i$ $1 ≀ b_{i} ≀ 10^{9}$ for each valid $i$ $1 ≀ a_{i} ≀ 10^{9}$ for each valid $i$ the graph on the input is a tree ------ Subtasks ------ Subtask #1 (10 points): $N ≀ 7$ Subtask #2 (10 points): $N ≀ 10$ Subtask #3 (30 points): $N ≀ 200$ Subtask #4 (50 points): original constraints ----- Sample Input 1 ------ 1 3 1 2 4 2 3 7 1 5 10 ----- Sample Output 1 ------ 1 ----- explanation 1 ------ Example case 1: There is no way to satisfy the required condition without paying any coins. When we have one coin, we can perform the following operations: - Swap the integers written on vertices $1$ and $2$. - Pay one coin and change the integer written on vertex $2$ to $7$.
t = int(input()) while t: n = int(input()) b = [0] * (n - 1) for i in range(0, n - 1): u, v, b[i] = [int(x) for x in input().split()] a = [int(x) for x in input().split()] a.sort() b.sort() cnt = 0 j = 0 i = 0 while i < n - 1 and j < n: if b[i] > a[j]: cnt += 1 else: i += 1 j += 1 print(cnt) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a tree with $N$ vertices (numbered $1$ through $N$). Its edges are numbered $1$ through $N-1$. For each valid $i$, there is an integer $a_{i}$ written on the $i$-th vertex. Also, for each valid $i$, there is an integer $b_{i}$ written on the $i$-th edge. You want the following condition to be satisfied: for each vertex $v$ and each edge $e$ adjacent to $v$, $a_{v} β‰₯ b_{e}$. In order to achieve that, you may perform an arbitrary number of steps (including zero). In one step, you may perform one the following operations: 1. Select two different vertices $u$ and $v$. Swap $a_{u}$ and $a_{v}$. 2. Select two different edges $e$ and $f$. Swap $b_{e}$ and $b_{f}$. 3. Select a vertex $v$ and an integer $x$. Change $a_{v}$ to $x$ and pay one coin. Calculate the minimum number of coins you need in order to satisfy the required condition. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N-1$ lines follow. For each $i$ ($1 ≀ i ≀ N-1$), the $i$-th of these lines contains three space-separated integers $u_{i}$, $v_{i}$ and $b_{i}$ denoting that the $i$-th edge connects vertices $u_{i}$ and $v_{i}$ and the initial value written on it is $b_{i}$. The last line contains $N$ space-separated integers $a_{1}, a_{2}, \ldots, a_{N}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum necessary number of coins. ------ Constraints ------ $1 ≀ T ≀ 10$ $2 ≀ N ≀ 100,000$ $1 ≀ u_{i}, v_{i} ≀ N$ for each valid $i$ $1 ≀ b_{i} ≀ 10^{9}$ for each valid $i$ $1 ≀ a_{i} ≀ 10^{9}$ for each valid $i$ the graph on the input is a tree ------ Subtasks ------ Subtask #1 (10 points): $N ≀ 7$ Subtask #2 (10 points): $N ≀ 10$ Subtask #3 (30 points): $N ≀ 200$ Subtask #4 (50 points): original constraints ----- Sample Input 1 ------ 1 3 1 2 4 2 3 7 1 5 10 ----- Sample Output 1 ------ 1 ----- explanation 1 ------ Example case 1: There is no way to satisfy the required condition without paying any coins. When we have one coin, we can perform the following operations: - Swap the integers written on vertices $1$ and $2$. - Pay one coin and change the integer written on vertex $2$ to $7$.
for _ in range(int(input())): N = int(input()) alist = [[] for i in range(N)] ewt = [] for i in range(N - 1): u, v, b = [int(j) for j in input().split()] u, v = u - 1, v - 1 ewt.append(b) alist[u].append(v) alist[v].append(u) ewt.sort(reverse=True) ewt_i = 0 vwt = sorted([int(i) for i in input().split()], reverse=True) vwt_start, vwt_end = 0, len(vwt) - 1 coins = 0 start, __ = max(enumerate(alist), key=lambda x: len(x[1])) q = [start] unvisited = [True] * N nbs = alist unvisited[start] = False if vwt[vwt_start] < ewt[0]: coins += 1 vwt_end -= 1 else: vwt_start += 1 while q: curr = q.pop(0) for v in nbs[curr]: if unvisited[v]: vewt = ewt[ewt_i] ewt_i += 1 if vewt <= vwt[vwt_start]: vvwt = vwt[vwt_start] vwt_start += 1 else: vvwt = vwt[vwt_end] vwt_end -= 1 coins += vewt > vvwt q.append(v) unvisited[curr] = False print(coins)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a tree with $N$ vertices (numbered $1$ through $N$). Its edges are numbered $1$ through $N-1$. For each valid $i$, there is an integer $a_{i}$ written on the $i$-th vertex. Also, for each valid $i$, there is an integer $b_{i}$ written on the $i$-th edge. You want the following condition to be satisfied: for each vertex $v$ and each edge $e$ adjacent to $v$, $a_{v} β‰₯ b_{e}$. In order to achieve that, you may perform an arbitrary number of steps (including zero). In one step, you may perform one the following operations: 1. Select two different vertices $u$ and $v$. Swap $a_{u}$ and $a_{v}$. 2. Select two different edges $e$ and $f$. Swap $b_{e}$ and $b_{f}$. 3. Select a vertex $v$ and an integer $x$. Change $a_{v}$ to $x$ and pay one coin. Calculate the minimum number of coins you need in order to satisfy the required condition. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N-1$ lines follow. For each $i$ ($1 ≀ i ≀ N-1$), the $i$-th of these lines contains three space-separated integers $u_{i}$, $v_{i}$ and $b_{i}$ denoting that the $i$-th edge connects vertices $u_{i}$ and $v_{i}$ and the initial value written on it is $b_{i}$. The last line contains $N$ space-separated integers $a_{1}, a_{2}, \ldots, a_{N}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum necessary number of coins. ------ Constraints ------ $1 ≀ T ≀ 10$ $2 ≀ N ≀ 100,000$ $1 ≀ u_{i}, v_{i} ≀ N$ for each valid $i$ $1 ≀ b_{i} ≀ 10^{9}$ for each valid $i$ $1 ≀ a_{i} ≀ 10^{9}$ for each valid $i$ the graph on the input is a tree ------ Subtasks ------ Subtask #1 (10 points): $N ≀ 7$ Subtask #2 (10 points): $N ≀ 10$ Subtask #3 (30 points): $N ≀ 200$ Subtask #4 (50 points): original constraints ----- Sample Input 1 ------ 1 3 1 2 4 2 3 7 1 5 10 ----- Sample Output 1 ------ 1 ----- explanation 1 ------ Example case 1: There is no way to satisfy the required condition without paying any coins. When we have one coin, we can perform the following operations: - Swap the integers written on vertices $1$ and $2$. - Pay one coin and change the integer written on vertex $2$ to $7$.
for _ in range(int(input())): n = int(input()) st = [] en = [] wt = {} m = [] s_e = {} e_s = {} for __ in range(n - 1): s, e, edj = map(int, input().split()) m.append(edj) s_e[s] = e e_s[e] = s ver = list(map(int, input().split())) m.sort() ver.sort() for i in range(1, n + 1): wt[i] = 0 c = 0 v = 0 e = 0 ans = 0 while v < n and e < n - 1: i = 1 c = 0 while i < n + 1 and v < n and e < n - 1: if v < n and e < n - 1: if wt[i] <= ver[v] and ver[v] >= m[e]: try: wt[e_s[i]] = m[e] except: wt[s_e[i]] = m[e] v += 1 e += 1 else: v += 1 ans += 1 i += 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a tree with $N$ vertices (numbered $1$ through $N$). Its edges are numbered $1$ through $N-1$. For each valid $i$, there is an integer $a_{i}$ written on the $i$-th vertex. Also, for each valid $i$, there is an integer $b_{i}$ written on the $i$-th edge. You want the following condition to be satisfied: for each vertex $v$ and each edge $e$ adjacent to $v$, $a_{v} β‰₯ b_{e}$. In order to achieve that, you may perform an arbitrary number of steps (including zero). In one step, you may perform one the following operations: 1. Select two different vertices $u$ and $v$. Swap $a_{u}$ and $a_{v}$. 2. Select two different edges $e$ and $f$. Swap $b_{e}$ and $b_{f}$. 3. Select a vertex $v$ and an integer $x$. Change $a_{v}$ to $x$ and pay one coin. Calculate the minimum number of coins you need in order to satisfy the required condition. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N-1$ lines follow. For each $i$ ($1 ≀ i ≀ N-1$), the $i$-th of these lines contains three space-separated integers $u_{i}$, $v_{i}$ and $b_{i}$ denoting that the $i$-th edge connects vertices $u_{i}$ and $v_{i}$ and the initial value written on it is $b_{i}$. The last line contains $N$ space-separated integers $a_{1}, a_{2}, \ldots, a_{N}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum necessary number of coins. ------ Constraints ------ $1 ≀ T ≀ 10$ $2 ≀ N ≀ 100,000$ $1 ≀ u_{i}, v_{i} ≀ N$ for each valid $i$ $1 ≀ b_{i} ≀ 10^{9}$ for each valid $i$ $1 ≀ a_{i} ≀ 10^{9}$ for each valid $i$ the graph on the input is a tree ------ Subtasks ------ Subtask #1 (10 points): $N ≀ 7$ Subtask #2 (10 points): $N ≀ 10$ Subtask #3 (30 points): $N ≀ 200$ Subtask #4 (50 points): original constraints ----- Sample Input 1 ------ 1 3 1 2 4 2 3 7 1 5 10 ----- Sample Output 1 ------ 1 ----- explanation 1 ------ Example case 1: There is no way to satisfy the required condition without paying any coins. When we have one coin, we can perform the following operations: - Swap the integers written on vertices $1$ and $2$. - Pay one coin and change the integer written on vertex $2$ to $7$.
t = int(input()) while t: t -= 1 n = int(input()) a = [] b = [] for i in range(n - 1): b.append(int(input().split()[2])) a = list(map(int, input().split())) b.sort(reverse=1) a.sort(reverse=1) ans = 0 i = 0 j = 0 if b[j] > a[i]: ans += 2 j += 1 else: i += 1 while j < n - 1: if b[j] > a[i]: ans += 1 j += 1 else: i += 1 j += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a tree with $N$ vertices (numbered $1$ through $N$). Its edges are numbered $1$ through $N-1$. For each valid $i$, there is an integer $a_{i}$ written on the $i$-th vertex. Also, for each valid $i$, there is an integer $b_{i}$ written on the $i$-th edge. You want the following condition to be satisfied: for each vertex $v$ and each edge $e$ adjacent to $v$, $a_{v} β‰₯ b_{e}$. In order to achieve that, you may perform an arbitrary number of steps (including zero). In one step, you may perform one the following operations: 1. Select two different vertices $u$ and $v$. Swap $a_{u}$ and $a_{v}$. 2. Select two different edges $e$ and $f$. Swap $b_{e}$ and $b_{f}$. 3. Select a vertex $v$ and an integer $x$. Change $a_{v}$ to $x$ and pay one coin. Calculate the minimum number of coins you need in order to satisfy the required condition. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N-1$ lines follow. For each $i$ ($1 ≀ i ≀ N-1$), the $i$-th of these lines contains three space-separated integers $u_{i}$, $v_{i}$ and $b_{i}$ denoting that the $i$-th edge connects vertices $u_{i}$ and $v_{i}$ and the initial value written on it is $b_{i}$. The last line contains $N$ space-separated integers $a_{1}, a_{2}, \ldots, a_{N}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum necessary number of coins. ------ Constraints ------ $1 ≀ T ≀ 10$ $2 ≀ N ≀ 100,000$ $1 ≀ u_{i}, v_{i} ≀ N$ for each valid $i$ $1 ≀ b_{i} ≀ 10^{9}$ for each valid $i$ $1 ≀ a_{i} ≀ 10^{9}$ for each valid $i$ the graph on the input is a tree ------ Subtasks ------ Subtask #1 (10 points): $N ≀ 7$ Subtask #2 (10 points): $N ≀ 10$ Subtask #3 (30 points): $N ≀ 200$ Subtask #4 (50 points): original constraints ----- Sample Input 1 ------ 1 3 1 2 4 2 3 7 1 5 10 ----- Sample Output 1 ------ 1 ----- explanation 1 ------ Example case 1: There is no way to satisfy the required condition without paying any coins. When we have one coin, we can perform the following operations: - Swap the integers written on vertices $1$ and $2$. - Pay one coin and change the integer written on vertex $2$ to $7$.
from sys import stdin t = int(stdin.readline()) def makeGraph(a, b): idxA = 0 idxB = 0 k = 0 b = [b[0]] + b while idxB < n: node = a[idxA] while idxB < n and node < b[idxB]: k += 1 idxB += 1 idxA += 1 idxB += 1 return k for _ in range(t): n = int(stdin.readline()) b = [] for i in range(n - 1): ui, vi, bi = list(map(int, stdin.readline().split())) b.append(bi) a = list(map(int, stdin.readline().split())) a = sorted(a, reverse=True) b = sorted(b, reverse=True) res = makeGraph(a, b) print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a tree with $N$ vertices (numbered $1$ through $N$). Its edges are numbered $1$ through $N-1$. For each valid $i$, there is an integer $a_{i}$ written on the $i$-th vertex. Also, for each valid $i$, there is an integer $b_{i}$ written on the $i$-th edge. You want the following condition to be satisfied: for each vertex $v$ and each edge $e$ adjacent to $v$, $a_{v} β‰₯ b_{e}$. In order to achieve that, you may perform an arbitrary number of steps (including zero). In one step, you may perform one the following operations: 1. Select two different vertices $u$ and $v$. Swap $a_{u}$ and $a_{v}$. 2. Select two different edges $e$ and $f$. Swap $b_{e}$ and $b_{f}$. 3. Select a vertex $v$ and an integer $x$. Change $a_{v}$ to $x$ and pay one coin. Calculate the minimum number of coins you need in order to satisfy the required condition. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N-1$ lines follow. For each $i$ ($1 ≀ i ≀ N-1$), the $i$-th of these lines contains three space-separated integers $u_{i}$, $v_{i}$ and $b_{i}$ denoting that the $i$-th edge connects vertices $u_{i}$ and $v_{i}$ and the initial value written on it is $b_{i}$. The last line contains $N$ space-separated integers $a_{1}, a_{2}, \ldots, a_{N}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum necessary number of coins. ------ Constraints ------ $1 ≀ T ≀ 10$ $2 ≀ N ≀ 100,000$ $1 ≀ u_{i}, v_{i} ≀ N$ for each valid $i$ $1 ≀ b_{i} ≀ 10^{9}$ for each valid $i$ $1 ≀ a_{i} ≀ 10^{9}$ for each valid $i$ the graph on the input is a tree ------ Subtasks ------ Subtask #1 (10 points): $N ≀ 7$ Subtask #2 (10 points): $N ≀ 10$ Subtask #3 (30 points): $N ≀ 200$ Subtask #4 (50 points): original constraints ----- Sample Input 1 ------ 1 3 1 2 4 2 3 7 1 5 10 ----- Sample Output 1 ------ 1 ----- explanation 1 ------ Example case 1: There is no way to satisfy the required condition without paying any coins. When we have one coin, we can perform the following operations: - Swap the integers written on vertices $1$ and $2$. - Pay one coin and change the integer written on vertex $2$ to $7$.
T = int(input()) while T: T = T - 1 N = int(input()) weight = [] for edge in range(N - 1): temp = input() a = temp.split(" ") weight.append(int(a[2])) vertice = [] temp = input() b = temp.split(" ") for i in b: vertice.append(int(i)) ans = 0 weight.sort() vertice.sort() i = N - 2 o = N - 2 k = 0 j = weight[N - 2] + 1 while i >= 0: if vertice[o] < weight[i]: if vertice[o + 1] < weight[i]: vertice.append(j) vertice.append(j) o = o + 2 ans = ans + 2 else: vertice.append(j) o = o + 1 ans = ans + 1 o = o - 1 i = i - 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a tree with $N$ vertices (numbered $1$ through $N$). Its edges are numbered $1$ through $N-1$. For each valid $i$, there is an integer $a_{i}$ written on the $i$-th vertex. Also, for each valid $i$, there is an integer $b_{i}$ written on the $i$-th edge. You want the following condition to be satisfied: for each vertex $v$ and each edge $e$ adjacent to $v$, $a_{v} β‰₯ b_{e}$. In order to achieve that, you may perform an arbitrary number of steps (including zero). In one step, you may perform one the following operations: 1. Select two different vertices $u$ and $v$. Swap $a_{u}$ and $a_{v}$. 2. Select two different edges $e$ and $f$. Swap $b_{e}$ and $b_{f}$. 3. Select a vertex $v$ and an integer $x$. Change $a_{v}$ to $x$ and pay one coin. Calculate the minimum number of coins you need in order to satisfy the required condition. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N-1$ lines follow. For each $i$ ($1 ≀ i ≀ N-1$), the $i$-th of these lines contains three space-separated integers $u_{i}$, $v_{i}$ and $b_{i}$ denoting that the $i$-th edge connects vertices $u_{i}$ and $v_{i}$ and the initial value written on it is $b_{i}$. The last line contains $N$ space-separated integers $a_{1}, a_{2}, \ldots, a_{N}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum necessary number of coins. ------ Constraints ------ $1 ≀ T ≀ 10$ $2 ≀ N ≀ 100,000$ $1 ≀ u_{i}, v_{i} ≀ N$ for each valid $i$ $1 ≀ b_{i} ≀ 10^{9}$ for each valid $i$ $1 ≀ a_{i} ≀ 10^{9}$ for each valid $i$ the graph on the input is a tree ------ Subtasks ------ Subtask #1 (10 points): $N ≀ 7$ Subtask #2 (10 points): $N ≀ 10$ Subtask #3 (30 points): $N ≀ 200$ Subtask #4 (50 points): original constraints ----- Sample Input 1 ------ 1 3 1 2 4 2 3 7 1 5 10 ----- Sample Output 1 ------ 1 ----- explanation 1 ------ Example case 1: There is no way to satisfy the required condition without paying any coins. When we have one coin, we can perform the following operations: - Swap the integers written on vertices $1$ and $2$. - Pay one coin and change the integer written on vertex $2$ to $7$.
for _ in range(int(input())): N = int(input()) V = [] E = [] k = 0 for i in range(N - 1): E.append(int(input().split()[-1])) E.sort(reverse=True) E = [E[0]] + E V = sorted(map(int, input().split()), reverse=True) j = 0 for i in E: if V[j] < i: k += 1 else: j += 1 print(k)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a tree with $N$ vertices (numbered $1$ through $N$). Its edges are numbered $1$ through $N-1$. For each valid $i$, there is an integer $a_{i}$ written on the $i$-th vertex. Also, for each valid $i$, there is an integer $b_{i}$ written on the $i$-th edge. You want the following condition to be satisfied: for each vertex $v$ and each edge $e$ adjacent to $v$, $a_{v} β‰₯ b_{e}$. In order to achieve that, you may perform an arbitrary number of steps (including zero). In one step, you may perform one the following operations: 1. Select two different vertices $u$ and $v$. Swap $a_{u}$ and $a_{v}$. 2. Select two different edges $e$ and $f$. Swap $b_{e}$ and $b_{f}$. 3. Select a vertex $v$ and an integer $x$. Change $a_{v}$ to $x$ and pay one coin. Calculate the minimum number of coins you need in order to satisfy the required condition. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N-1$ lines follow. For each $i$ ($1 ≀ i ≀ N-1$), the $i$-th of these lines contains three space-separated integers $u_{i}$, $v_{i}$ and $b_{i}$ denoting that the $i$-th edge connects vertices $u_{i}$ and $v_{i}$ and the initial value written on it is $b_{i}$. The last line contains $N$ space-separated integers $a_{1}, a_{2}, \ldots, a_{N}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum necessary number of coins. ------ Constraints ------ $1 ≀ T ≀ 10$ $2 ≀ N ≀ 100,000$ $1 ≀ u_{i}, v_{i} ≀ N$ for each valid $i$ $1 ≀ b_{i} ≀ 10^{9}$ for each valid $i$ $1 ≀ a_{i} ≀ 10^{9}$ for each valid $i$ the graph on the input is a tree ------ Subtasks ------ Subtask #1 (10 points): $N ≀ 7$ Subtask #2 (10 points): $N ≀ 10$ Subtask #3 (30 points): $N ≀ 200$ Subtask #4 (50 points): original constraints ----- Sample Input 1 ------ 1 3 1 2 4 2 3 7 1 5 10 ----- Sample Output 1 ------ 1 ----- explanation 1 ------ Example case 1: There is no way to satisfy the required condition without paying any coins. When we have one coin, we can perform the following operations: - Swap the integers written on vertices $1$ and $2$. - Pay one coin and change the integer written on vertex $2$ to $7$.
t = int(input()) for i in range(t): n = int(input()) le = [] for j in range(1, n): s = input() ls = s.split(" ") le.append(int(ls[2])) le.sort() a = input() lv1 = a.split(" ") lv = [] for v in range(n): lv.append(int(lv1[v])) lv.sort() l = [] for k in range(2 * n - 1): if k % 2 == 0: l.append(lv[k // 2]) else: l.append(le[k // 2]) arr1 = [] for m in range(0, 2 * n - 1, 2): if 0 < m < 2 * n - 2: if l[m] < l[m + 1] or l[m] < l[m - 1]: if len(arr1) > 0 and arr1[0] <= l[m]: arr1.pop(0) arr1.append(l[m + 1]) elif m == 0 and l[m] < l[m + 1]: arr1.append(l[m + 1]) elif m == 2 * n - 2 and l[m] < l[m - 1]: if len(arr1) > 0 and arr1[0] <= l[m]: arr1.pop(0) arr1.append(l[m - 1]) print(len(arr1))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER IF NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a tree with $N$ vertices (numbered $1$ through $N$). Its edges are numbered $1$ through $N-1$. For each valid $i$, there is an integer $a_{i}$ written on the $i$-th vertex. Also, for each valid $i$, there is an integer $b_{i}$ written on the $i$-th edge. You want the following condition to be satisfied: for each vertex $v$ and each edge $e$ adjacent to $v$, $a_{v} β‰₯ b_{e}$. In order to achieve that, you may perform an arbitrary number of steps (including zero). In one step, you may perform one the following operations: 1. Select two different vertices $u$ and $v$. Swap $a_{u}$ and $a_{v}$. 2. Select two different edges $e$ and $f$. Swap $b_{e}$ and $b_{f}$. 3. Select a vertex $v$ and an integer $x$. Change $a_{v}$ to $x$ and pay one coin. Calculate the minimum number of coins you need in order to satisfy the required condition. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N-1$ lines follow. For each $i$ ($1 ≀ i ≀ N-1$), the $i$-th of these lines contains three space-separated integers $u_{i}$, $v_{i}$ and $b_{i}$ denoting that the $i$-th edge connects vertices $u_{i}$ and $v_{i}$ and the initial value written on it is $b_{i}$. The last line contains $N$ space-separated integers $a_{1}, a_{2}, \ldots, a_{N}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum necessary number of coins. ------ Constraints ------ $1 ≀ T ≀ 10$ $2 ≀ N ≀ 100,000$ $1 ≀ u_{i}, v_{i} ≀ N$ for each valid $i$ $1 ≀ b_{i} ≀ 10^{9}$ for each valid $i$ $1 ≀ a_{i} ≀ 10^{9}$ for each valid $i$ the graph on the input is a tree ------ Subtasks ------ Subtask #1 (10 points): $N ≀ 7$ Subtask #2 (10 points): $N ≀ 10$ Subtask #3 (30 points): $N ≀ 200$ Subtask #4 (50 points): original constraints ----- Sample Input 1 ------ 1 3 1 2 4 2 3 7 1 5 10 ----- Sample Output 1 ------ 1 ----- explanation 1 ------ Example case 1: There is no way to satisfy the required condition without paying any coins. When we have one coin, we can perform the following operations: - Swap the integers written on vertices $1$ and $2$. - Pay one coin and change the integer written on vertex $2$ to $7$.
t = int(input()) while t > 0: n = int(input()) ew = [] for i in range(n - 1): arr = [int(x) for x in input().split()] ew.append(arr[2]) vw = [int(x) for x in input().split()] vw.sort() ew.sort() ans = 0 vIt = len(vw) - 1 eIt = len(ew) - 1 req = 2 vSize = len(vw) while vSize > 0: if vIt < 0 or eIt < 0: break v1 = vw[vIt] e1 = ew[eIt] if v1 < e1: if req > 0: ans += req vSize -= req req = 0 eIt = eIt - 1 req = req + 1 else: req = req - 1 vIt = vIt - 1 vSize = vSize - 1 print(ans) t = t - 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR IF VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a tree with $N$ vertices (numbered $1$ through $N$). Its edges are numbered $1$ through $N-1$. For each valid $i$, there is an integer $a_{i}$ written on the $i$-th vertex. Also, for each valid $i$, there is an integer $b_{i}$ written on the $i$-th edge. You want the following condition to be satisfied: for each vertex $v$ and each edge $e$ adjacent to $v$, $a_{v} β‰₯ b_{e}$. In order to achieve that, you may perform an arbitrary number of steps (including zero). In one step, you may perform one the following operations: 1. Select two different vertices $u$ and $v$. Swap $a_{u}$ and $a_{v}$. 2. Select two different edges $e$ and $f$. Swap $b_{e}$ and $b_{f}$. 3. Select a vertex $v$ and an integer $x$. Change $a_{v}$ to $x$ and pay one coin. Calculate the minimum number of coins you need in order to satisfy the required condition. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N-1$ lines follow. For each $i$ ($1 ≀ i ≀ N-1$), the $i$-th of these lines contains three space-separated integers $u_{i}$, $v_{i}$ and $b_{i}$ denoting that the $i$-th edge connects vertices $u_{i}$ and $v_{i}$ and the initial value written on it is $b_{i}$. The last line contains $N$ space-separated integers $a_{1}, a_{2}, \ldots, a_{N}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum necessary number of coins. ------ Constraints ------ $1 ≀ T ≀ 10$ $2 ≀ N ≀ 100,000$ $1 ≀ u_{i}, v_{i} ≀ N$ for each valid $i$ $1 ≀ b_{i} ≀ 10^{9}$ for each valid $i$ $1 ≀ a_{i} ≀ 10^{9}$ for each valid $i$ the graph on the input is a tree ------ Subtasks ------ Subtask #1 (10 points): $N ≀ 7$ Subtask #2 (10 points): $N ≀ 10$ Subtask #3 (30 points): $N ≀ 200$ Subtask #4 (50 points): original constraints ----- Sample Input 1 ------ 1 3 1 2 4 2 3 7 1 5 10 ----- Sample Output 1 ------ 1 ----- explanation 1 ------ Example case 1: There is no way to satisfy the required condition without paying any coins. When we have one coin, we can perform the following operations: - Swap the integers written on vertices $1$ and $2$. - Pay one coin and change the integer written on vertex $2$ to $7$.
class doublylinkedlist: class _Node: __slots__ = "data", "prev", "next" def __init__(self, data, prev, next): self.data = data self.prev = prev self.next = next def __init__(self): self.head = self._Node(None, None, None) self.tail = self._Node(None, None, None) self.tail.prev = self.head self.head.next = self.tail self.size = 0 def is_empty(self): return self.size == 0 def add_last(self, value): new_node = self._Node(value, None, None) if self.is_empty(): self.head = new_node self.tail = new_node else: self.tail.next = new_node new_node.prev = self.tail self.tail = new_node self.size += 1 def remove_first(self): value = self.head.data self.head = self.head.next self.head.prev = None self.size -= 1 if self.is_empty(): self.tail = None return value def remove_last(self): value = self.tail.data self.tail = self.tail.prev self.tail.next = None self.size -= 1 if self.is_empty(): self.head = None return value for _ in range(int(input())): n = int(input()) edge = list() for i in range(n - 1): a, b, c = map(int, input().split()) edge.append(c) arr = list(map(int, input().split())) arr.sort() edge.sort() dll = doublylinkedlist() for i in range(n): dll.add_last(arr[i]) ans = 0 for i in range(n - 2, -1, -1): t1, t2 = dll.tail.data, dll.tail.prev.data if t1 >= edge[i] and t2 >= edge[i]: dll.remove_last() continue else: if t1 < edge[i]: ans += 2 dll.remove_first() dll.remove_first() dll.add_last(edge[i]) dll.add_last(edge[i]) elif t2 < edge[i]: ans += 1 dll.remove_first() dll.add_last(edge[i]) dll.remove_last() print(ans)
CLASS_DEF CLASS_DEF ASSIGN VAR STRING STRING STRING FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR NONE NONE NONE ASSIGN VAR FUNC_CALL VAR NONE NONE NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FUNC_DEF RETURN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NONE NONE IF FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE VAR NUMBER IF FUNC_CALL VAR ASSIGN VAR NONE RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE VAR NUMBER IF FUNC_CALL VAR ASSIGN VAR NONE RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a tree with $N$ vertices (numbered $1$ through $N$). Its edges are numbered $1$ through $N-1$. For each valid $i$, there is an integer $a_{i}$ written on the $i$-th vertex. Also, for each valid $i$, there is an integer $b_{i}$ written on the $i$-th edge. You want the following condition to be satisfied: for each vertex $v$ and each edge $e$ adjacent to $v$, $a_{v} β‰₯ b_{e}$. In order to achieve that, you may perform an arbitrary number of steps (including zero). In one step, you may perform one the following operations: 1. Select two different vertices $u$ and $v$. Swap $a_{u}$ and $a_{v}$. 2. Select two different edges $e$ and $f$. Swap $b_{e}$ and $b_{f}$. 3. Select a vertex $v$ and an integer $x$. Change $a_{v}$ to $x$ and pay one coin. Calculate the minimum number of coins you need in order to satisfy the required condition. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N-1$ lines follow. For each $i$ ($1 ≀ i ≀ N-1$), the $i$-th of these lines contains three space-separated integers $u_{i}$, $v_{i}$ and $b_{i}$ denoting that the $i$-th edge connects vertices $u_{i}$ and $v_{i}$ and the initial value written on it is $b_{i}$. The last line contains $N$ space-separated integers $a_{1}, a_{2}, \ldots, a_{N}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum necessary number of coins. ------ Constraints ------ $1 ≀ T ≀ 10$ $2 ≀ N ≀ 100,000$ $1 ≀ u_{i}, v_{i} ≀ N$ for each valid $i$ $1 ≀ b_{i} ≀ 10^{9}$ for each valid $i$ $1 ≀ a_{i} ≀ 10^{9}$ for each valid $i$ the graph on the input is a tree ------ Subtasks ------ Subtask #1 (10 points): $N ≀ 7$ Subtask #2 (10 points): $N ≀ 10$ Subtask #3 (30 points): $N ≀ 200$ Subtask #4 (50 points): original constraints ----- Sample Input 1 ------ 1 3 1 2 4 2 3 7 1 5 10 ----- Sample Output 1 ------ 1 ----- explanation 1 ------ Example case 1: There is no way to satisfy the required condition without paying any coins. When we have one coin, we can perform the following operations: - Swap the integers written on vertices $1$ and $2$. - Pay one coin and change the integer written on vertex $2$ to $7$.
def main(debug=False): for _ in range(int(input())): n = int(input()) W = [] for i in range(n - 1): u, v, wi = map(int, input().split()) W.append(wi) N = list(map(int, input().split())) N = sorted(N, reverse=True) W = sorted(W, reverse=True) W.insert(1, W[0]) if debug: print("W", W) print("N", N) j = 0 cnt = 0 for i in W: if debug: print("for W[i]=", i, "and N[j]=", N[j]) if i > N[j]: cnt += 1 else: j += 1 if debug: print("cnt for this iteration is", cnt) print(cnt) main(debug=False)
FUNC_DEF NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR EXPR FUNC_CALL VAR STRING VAR STRING VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a tree with $N$ vertices (numbered $1$ through $N$). Its edges are numbered $1$ through $N-1$. For each valid $i$, there is an integer $a_{i}$ written on the $i$-th vertex. Also, for each valid $i$, there is an integer $b_{i}$ written on the $i$-th edge. You want the following condition to be satisfied: for each vertex $v$ and each edge $e$ adjacent to $v$, $a_{v} β‰₯ b_{e}$. In order to achieve that, you may perform an arbitrary number of steps (including zero). In one step, you may perform one the following operations: 1. Select two different vertices $u$ and $v$. Swap $a_{u}$ and $a_{v}$. 2. Select two different edges $e$ and $f$. Swap $b_{e}$ and $b_{f}$. 3. Select a vertex $v$ and an integer $x$. Change $a_{v}$ to $x$ and pay one coin. Calculate the minimum number of coins you need in order to satisfy the required condition. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N-1$ lines follow. For each $i$ ($1 ≀ i ≀ N-1$), the $i$-th of these lines contains three space-separated integers $u_{i}$, $v_{i}$ and $b_{i}$ denoting that the $i$-th edge connects vertices $u_{i}$ and $v_{i}$ and the initial value written on it is $b_{i}$. The last line contains $N$ space-separated integers $a_{1}, a_{2}, \ldots, a_{N}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum necessary number of coins. ------ Constraints ------ $1 ≀ T ≀ 10$ $2 ≀ N ≀ 100,000$ $1 ≀ u_{i}, v_{i} ≀ N$ for each valid $i$ $1 ≀ b_{i} ≀ 10^{9}$ for each valid $i$ $1 ≀ a_{i} ≀ 10^{9}$ for each valid $i$ the graph on the input is a tree ------ Subtasks ------ Subtask #1 (10 points): $N ≀ 7$ Subtask #2 (10 points): $N ≀ 10$ Subtask #3 (30 points): $N ≀ 200$ Subtask #4 (50 points): original constraints ----- Sample Input 1 ------ 1 3 1 2 4 2 3 7 1 5 10 ----- Sample Output 1 ------ 1 ----- explanation 1 ------ Example case 1: There is no way to satisfy the required condition without paying any coins. When we have one coin, we can perform the following operations: - Swap the integers written on vertices $1$ and $2$. - Pay one coin and change the integer written on vertex $2$ to $7$.
t = int(input()) for i in range(t): n = int(input()) u = [] v = [] b = [] l = [] ans = 0 for k in range(0, n - 1): p, q, r = map(int, input().split(" ")) u.append(p) v.append(q) b.append(r) a = list(map(int, input().split(" "))) b.sort() a.sort() pa = n - 2 p = n - 2 j = b[n - 2] while pa >= 0: if a[p] < b[pa] and a[p + 1] < b[pa]: a.append(j) a.append(j) p = p + 2 ans = ans + 2 elif a[p] < b[pa]: a.append(j) p = p + 1 ans = ans + 1 p = p - 1 pa = pa - 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a tree with $N$ vertices (numbered $1$ through $N$). Its edges are numbered $1$ through $N-1$. For each valid $i$, there is an integer $a_{i}$ written on the $i$-th vertex. Also, for each valid $i$, there is an integer $b_{i}$ written on the $i$-th edge. You want the following condition to be satisfied: for each vertex $v$ and each edge $e$ adjacent to $v$, $a_{v} β‰₯ b_{e}$. In order to achieve that, you may perform an arbitrary number of steps (including zero). In one step, you may perform one the following operations: 1. Select two different vertices $u$ and $v$. Swap $a_{u}$ and $a_{v}$. 2. Select two different edges $e$ and $f$. Swap $b_{e}$ and $b_{f}$. 3. Select a vertex $v$ and an integer $x$. Change $a_{v}$ to $x$ and pay one coin. Calculate the minimum number of coins you need in order to satisfy the required condition. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N-1$ lines follow. For each $i$ ($1 ≀ i ≀ N-1$), the $i$-th of these lines contains three space-separated integers $u_{i}$, $v_{i}$ and $b_{i}$ denoting that the $i$-th edge connects vertices $u_{i}$ and $v_{i}$ and the initial value written on it is $b_{i}$. The last line contains $N$ space-separated integers $a_{1}, a_{2}, \ldots, a_{N}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum necessary number of coins. ------ Constraints ------ $1 ≀ T ≀ 10$ $2 ≀ N ≀ 100,000$ $1 ≀ u_{i}, v_{i} ≀ N$ for each valid $i$ $1 ≀ b_{i} ≀ 10^{9}$ for each valid $i$ $1 ≀ a_{i} ≀ 10^{9}$ for each valid $i$ the graph on the input is a tree ------ Subtasks ------ Subtask #1 (10 points): $N ≀ 7$ Subtask #2 (10 points): $N ≀ 10$ Subtask #3 (30 points): $N ≀ 200$ Subtask #4 (50 points): original constraints ----- Sample Input 1 ------ 1 3 1 2 4 2 3 7 1 5 10 ----- Sample Output 1 ------ 1 ----- explanation 1 ------ Example case 1: There is no way to satisfy the required condition without paying any coins. When we have one coin, we can perform the following operations: - Swap the integers written on vertices $1$ and $2$. - Pay one coin and change the integer written on vertex $2$ to $7$.
for t in range(int(input())): n = int(input()) e = [] for i in range(n - 1): x, y, z = map(int, input().split()) e.append(z) e.sort() v = list(map(int, input().split())) v.sort() h = [0] * (n - 1) hind = 0 vind = 0 while hind < n - 1 and vind < n: if v[vind] < e[hind]: vind += 1 continue if v[vind] >= e[hind]: h[hind] = vind hind += 1 continue if hind < n - 1: for i in range(hind, n - 1): h[i] = vind ans = 0 for i in range(n - 1): ans += max(0, h[i] - i - ans) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a tree with $N$ vertices (numbered $1$ through $N$). Its edges are numbered $1$ through $N-1$. For each valid $i$, there is an integer $a_{i}$ written on the $i$-th vertex. Also, for each valid $i$, there is an integer $b_{i}$ written on the $i$-th edge. You want the following condition to be satisfied: for each vertex $v$ and each edge $e$ adjacent to $v$, $a_{v} β‰₯ b_{e}$. In order to achieve that, you may perform an arbitrary number of steps (including zero). In one step, you may perform one the following operations: 1. Select two different vertices $u$ and $v$. Swap $a_{u}$ and $a_{v}$. 2. Select two different edges $e$ and $f$. Swap $b_{e}$ and $b_{f}$. 3. Select a vertex $v$ and an integer $x$. Change $a_{v}$ to $x$ and pay one coin. Calculate the minimum number of coins you need in order to satisfy the required condition. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N-1$ lines follow. For each $i$ ($1 ≀ i ≀ N-1$), the $i$-th of these lines contains three space-separated integers $u_{i}$, $v_{i}$ and $b_{i}$ denoting that the $i$-th edge connects vertices $u_{i}$ and $v_{i}$ and the initial value written on it is $b_{i}$. The last line contains $N$ space-separated integers $a_{1}, a_{2}, \ldots, a_{N}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum necessary number of coins. ------ Constraints ------ $1 ≀ T ≀ 10$ $2 ≀ N ≀ 100,000$ $1 ≀ u_{i}, v_{i} ≀ N$ for each valid $i$ $1 ≀ b_{i} ≀ 10^{9}$ for each valid $i$ $1 ≀ a_{i} ≀ 10^{9}$ for each valid $i$ the graph on the input is a tree ------ Subtasks ------ Subtask #1 (10 points): $N ≀ 7$ Subtask #2 (10 points): $N ≀ 10$ Subtask #3 (30 points): $N ≀ 200$ Subtask #4 (50 points): original constraints ----- Sample Input 1 ------ 1 3 1 2 4 2 3 7 1 5 10 ----- Sample Output 1 ------ 1 ----- explanation 1 ------ Example case 1: There is no way to satisfy the required condition without paying any coins. When we have one coin, we can perform the following operations: - Swap the integers written on vertices $1$ and $2$. - Pay one coin and change the integer written on vertex $2$ to $7$.
t = int(input()) for _ in range(t): n = int(input()) edge = [] adj = [[] for _ in range(n)] for _ in range(n - 1): a, b, c = [int(x) for x in input().split()] edge.append(c) vertex = [int(x) for x in input().split()] vertex = sorted(vertex, reverse=True) edge = sorted(edge, reverse=True) j = 1 count = 0 if vertex[0] < edge[0]: count += 1 j = 0 for i in range(n - 1): if vertex[j] < edge[i]: count += 1 j -= 1 j += 1 print(count)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a tree with $N$ vertices (numbered $1$ through $N$). Its edges are numbered $1$ through $N-1$. For each valid $i$, there is an integer $a_{i}$ written on the $i$-th vertex. Also, for each valid $i$, there is an integer $b_{i}$ written on the $i$-th edge. You want the following condition to be satisfied: for each vertex $v$ and each edge $e$ adjacent to $v$, $a_{v} β‰₯ b_{e}$. In order to achieve that, you may perform an arbitrary number of steps (including zero). In one step, you may perform one the following operations: 1. Select two different vertices $u$ and $v$. Swap $a_{u}$ and $a_{v}$. 2. Select two different edges $e$ and $f$. Swap $b_{e}$ and $b_{f}$. 3. Select a vertex $v$ and an integer $x$. Change $a_{v}$ to $x$ and pay one coin. Calculate the minimum number of coins you need in order to satisfy the required condition. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N-1$ lines follow. For each $i$ ($1 ≀ i ≀ N-1$), the $i$-th of these lines contains three space-separated integers $u_{i}$, $v_{i}$ and $b_{i}$ denoting that the $i$-th edge connects vertices $u_{i}$ and $v_{i}$ and the initial value written on it is $b_{i}$. The last line contains $N$ space-separated integers $a_{1}, a_{2}, \ldots, a_{N}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum necessary number of coins. ------ Constraints ------ $1 ≀ T ≀ 10$ $2 ≀ N ≀ 100,000$ $1 ≀ u_{i}, v_{i} ≀ N$ for each valid $i$ $1 ≀ b_{i} ≀ 10^{9}$ for each valid $i$ $1 ≀ a_{i} ≀ 10^{9}$ for each valid $i$ the graph on the input is a tree ------ Subtasks ------ Subtask #1 (10 points): $N ≀ 7$ Subtask #2 (10 points): $N ≀ 10$ Subtask #3 (30 points): $N ≀ 200$ Subtask #4 (50 points): original constraints ----- Sample Input 1 ------ 1 3 1 2 4 2 3 7 1 5 10 ----- Sample Output 1 ------ 1 ----- explanation 1 ------ Example case 1: There is no way to satisfy the required condition without paying any coins. When we have one coin, we can perform the following operations: - Swap the integers written on vertices $1$ and $2$. - Pay one coin and change the integer written on vertex $2$ to $7$.
for _ in range(int(input())): n = int(input()) a = [] for i in range(n - 1): val1, val2, val3 = list(map(int, input().split())) a.append(val3) a.sort() arr = list(map(int, input().split())) arr.sort() i, j, ans = 0, 0, 0 while True: if arr[i] >= a[j]: i += 1 j += 1 else: i += 1 ans += 1 if j == n - 1 or i == n: break print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER WHILE NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a tree with $N$ vertices (numbered $1$ through $N$). Its edges are numbered $1$ through $N-1$. For each valid $i$, there is an integer $a_{i}$ written on the $i$-th vertex. Also, for each valid $i$, there is an integer $b_{i}$ written on the $i$-th edge. You want the following condition to be satisfied: for each vertex $v$ and each edge $e$ adjacent to $v$, $a_{v} β‰₯ b_{e}$. In order to achieve that, you may perform an arbitrary number of steps (including zero). In one step, you may perform one the following operations: 1. Select two different vertices $u$ and $v$. Swap $a_{u}$ and $a_{v}$. 2. Select two different edges $e$ and $f$. Swap $b_{e}$ and $b_{f}$. 3. Select a vertex $v$ and an integer $x$. Change $a_{v}$ to $x$ and pay one coin. Calculate the minimum number of coins you need in order to satisfy the required condition. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N-1$ lines follow. For each $i$ ($1 ≀ i ≀ N-1$), the $i$-th of these lines contains three space-separated integers $u_{i}$, $v_{i}$ and $b_{i}$ denoting that the $i$-th edge connects vertices $u_{i}$ and $v_{i}$ and the initial value written on it is $b_{i}$. The last line contains $N$ space-separated integers $a_{1}, a_{2}, \ldots, a_{N}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum necessary number of coins. ------ Constraints ------ $1 ≀ T ≀ 10$ $2 ≀ N ≀ 100,000$ $1 ≀ u_{i}, v_{i} ≀ N$ for each valid $i$ $1 ≀ b_{i} ≀ 10^{9}$ for each valid $i$ $1 ≀ a_{i} ≀ 10^{9}$ for each valid $i$ the graph on the input is a tree ------ Subtasks ------ Subtask #1 (10 points): $N ≀ 7$ Subtask #2 (10 points): $N ≀ 10$ Subtask #3 (30 points): $N ≀ 200$ Subtask #4 (50 points): original constraints ----- Sample Input 1 ------ 1 3 1 2 4 2 3 7 1 5 10 ----- Sample Output 1 ------ 1 ----- explanation 1 ------ Example case 1: There is no way to satisfy the required condition without paying any coins. When we have one coin, we can perform the following operations: - Swap the integers written on vertices $1$ and $2$. - Pay one coin and change the integer written on vertex $2$ to $7$.
for case in range(int(input())): n = int(input()) b = [0] * (n - 1) for i in range(n - 1): st = input().split() b[i] = int(st[-1]) a = list(map(int, input().split())) a.sort() b.sort() vind, eind = 0, 0 ans = 0 while vind < n and eind < n - 1: if a[vind] >= b[eind]: vind += 1 eind += 1 else: ans += 1 vind += 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a tree with $N$ vertices (numbered $1$ through $N$). Its edges are numbered $1$ through $N-1$. For each valid $i$, there is an integer $a_{i}$ written on the $i$-th vertex. Also, for each valid $i$, there is an integer $b_{i}$ written on the $i$-th edge. You want the following condition to be satisfied: for each vertex $v$ and each edge $e$ adjacent to $v$, $a_{v} β‰₯ b_{e}$. In order to achieve that, you may perform an arbitrary number of steps (including zero). In one step, you may perform one the following operations: 1. Select two different vertices $u$ and $v$. Swap $a_{u}$ and $a_{v}$. 2. Select two different edges $e$ and $f$. Swap $b_{e}$ and $b_{f}$. 3. Select a vertex $v$ and an integer $x$. Change $a_{v}$ to $x$ and pay one coin. Calculate the minimum number of coins you need in order to satisfy the required condition. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N-1$ lines follow. For each $i$ ($1 ≀ i ≀ N-1$), the $i$-th of these lines contains three space-separated integers $u_{i}$, $v_{i}$ and $b_{i}$ denoting that the $i$-th edge connects vertices $u_{i}$ and $v_{i}$ and the initial value written on it is $b_{i}$. The last line contains $N$ space-separated integers $a_{1}, a_{2}, \ldots, a_{N}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum necessary number of coins. ------ Constraints ------ $1 ≀ T ≀ 10$ $2 ≀ N ≀ 100,000$ $1 ≀ u_{i}, v_{i} ≀ N$ for each valid $i$ $1 ≀ b_{i} ≀ 10^{9}$ for each valid $i$ $1 ≀ a_{i} ≀ 10^{9}$ for each valid $i$ the graph on the input is a tree ------ Subtasks ------ Subtask #1 (10 points): $N ≀ 7$ Subtask #2 (10 points): $N ≀ 10$ Subtask #3 (30 points): $N ≀ 200$ Subtask #4 (50 points): original constraints ----- Sample Input 1 ------ 1 3 1 2 4 2 3 7 1 5 10 ----- Sample Output 1 ------ 1 ----- explanation 1 ------ Example case 1: There is no way to satisfy the required condition without paying any coins. When we have one coin, we can perform the following operations: - Swap the integers written on vertices $1$ and $2$. - Pay one coin and change the integer written on vertex $2$ to $7$.
test = int(input()) for intnn in range(test): num = int(input()) array = [] for i in range(num - 1): val1, val2, val3 = list(map(int, input().split())) array.append(val3) arr = list(map(int, input().split())) arr.sort() array.sort() ar = [] for i in range(num): ar.append(i) i = 0 j = 0 ans = 0 while True: if arr[i] >= array[j]: i += 1 j += 1 else: i += 1 ans += 1 if j == num - 1 or i == num: break print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a tree with $N$ vertices (numbered $1$ through $N$). Its edges are numbered $1$ through $N-1$. For each valid $i$, there is an integer $a_{i}$ written on the $i$-th vertex. Also, for each valid $i$, there is an integer $b_{i}$ written on the $i$-th edge. You want the following condition to be satisfied: for each vertex $v$ and each edge $e$ adjacent to $v$, $a_{v} β‰₯ b_{e}$. In order to achieve that, you may perform an arbitrary number of steps (including zero). In one step, you may perform one the following operations: 1. Select two different vertices $u$ and $v$. Swap $a_{u}$ and $a_{v}$. 2. Select two different edges $e$ and $f$. Swap $b_{e}$ and $b_{f}$. 3. Select a vertex $v$ and an integer $x$. Change $a_{v}$ to $x$ and pay one coin. Calculate the minimum number of coins you need in order to satisfy the required condition. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N-1$ lines follow. For each $i$ ($1 ≀ i ≀ N-1$), the $i$-th of these lines contains three space-separated integers $u_{i}$, $v_{i}$ and $b_{i}$ denoting that the $i$-th edge connects vertices $u_{i}$ and $v_{i}$ and the initial value written on it is $b_{i}$. The last line contains $N$ space-separated integers $a_{1}, a_{2}, \ldots, a_{N}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum necessary number of coins. ------ Constraints ------ $1 ≀ T ≀ 10$ $2 ≀ N ≀ 100,000$ $1 ≀ u_{i}, v_{i} ≀ N$ for each valid $i$ $1 ≀ b_{i} ≀ 10^{9}$ for each valid $i$ $1 ≀ a_{i} ≀ 10^{9}$ for each valid $i$ the graph on the input is a tree ------ Subtasks ------ Subtask #1 (10 points): $N ≀ 7$ Subtask #2 (10 points): $N ≀ 10$ Subtask #3 (30 points): $N ≀ 200$ Subtask #4 (50 points): original constraints ----- Sample Input 1 ------ 1 3 1 2 4 2 3 7 1 5 10 ----- Sample Output 1 ------ 1 ----- explanation 1 ------ Example case 1: There is no way to satisfy the required condition without paying any coins. When we have one coin, we can perform the following operations: - Swap the integers written on vertices $1$ and $2$. - Pay one coin and change the integer written on vertex $2$ to $7$.
t = int(input()) while t > 0: t -= 1 n = int(input()) be = [] for i in range(n - 1): ui, vi, bi = map(int, input().split()) be.append(bi) ae = list(map(int, input().split())) be.sort() ae.sort() be = be + [be[-1]] i = len(ae) - 1 j = len(be) - 1 ans = 0 while i >= 0 and j >= 0: if ae[i] < be[j]: ans += 1 else: i -= 1 j -= 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR LIST VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a tree with $N$ vertices (numbered $1$ through $N$). Its edges are numbered $1$ through $N-1$. For each valid $i$, there is an integer $a_{i}$ written on the $i$-th vertex. Also, for each valid $i$, there is an integer $b_{i}$ written on the $i$-th edge. You want the following condition to be satisfied: for each vertex $v$ and each edge $e$ adjacent to $v$, $a_{v} β‰₯ b_{e}$. In order to achieve that, you may perform an arbitrary number of steps (including zero). In one step, you may perform one the following operations: 1. Select two different vertices $u$ and $v$. Swap $a_{u}$ and $a_{v}$. 2. Select two different edges $e$ and $f$. Swap $b_{e}$ and $b_{f}$. 3. Select a vertex $v$ and an integer $x$. Change $a_{v}$ to $x$ and pay one coin. Calculate the minimum number of coins you need in order to satisfy the required condition. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N-1$ lines follow. For each $i$ ($1 ≀ i ≀ N-1$), the $i$-th of these lines contains three space-separated integers $u_{i}$, $v_{i}$ and $b_{i}$ denoting that the $i$-th edge connects vertices $u_{i}$ and $v_{i}$ and the initial value written on it is $b_{i}$. The last line contains $N$ space-separated integers $a_{1}, a_{2}, \ldots, a_{N}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum necessary number of coins. ------ Constraints ------ $1 ≀ T ≀ 10$ $2 ≀ N ≀ 100,000$ $1 ≀ u_{i}, v_{i} ≀ N$ for each valid $i$ $1 ≀ b_{i} ≀ 10^{9}$ for each valid $i$ $1 ≀ a_{i} ≀ 10^{9}$ for each valid $i$ the graph on the input is a tree ------ Subtasks ------ Subtask #1 (10 points): $N ≀ 7$ Subtask #2 (10 points): $N ≀ 10$ Subtask #3 (30 points): $N ≀ 200$ Subtask #4 (50 points): original constraints ----- Sample Input 1 ------ 1 3 1 2 4 2 3 7 1 5 10 ----- Sample Output 1 ------ 1 ----- explanation 1 ------ Example case 1: There is no way to satisfy the required condition without paying any coins. When we have one coin, we can perform the following operations: - Swap the integers written on vertices $1$ and $2$. - Pay one coin and change the integer written on vertex $2$ to $7$.
test = int(input()) for k in range(test): vertices = int(input()) edges = [] vv = [] for i in range(vertices - 1): arr = list(map(int, input().split())) edges.append(arr[2]) vv = list(map(int, input().split())) vv.sort() edges.sort() coins = 0 if vv[vertices - 1] < edges[vertices - 2]: vv[0] = edges[vertices - 2] vv[1] = edges[vertices - 2] coins += 2 elif ( vv[vertices - 1] >= edges[vertices - 2] and edges[vertices - 2] > vv[vertices - 2] ): vv[0] = edges[vertices - 2] coins += 1 vv.sort() p1 = vertices - 2 for p2 in range(vertices - 2, -1, -1): if vv[p1] >= edges[p2]: p1 -= 1 else: coins += 1 print(coins)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a tree with $N$ vertices (numbered $1$ through $N$). Its edges are numbered $1$ through $N-1$. For each valid $i$, there is an integer $a_{i}$ written on the $i$-th vertex. Also, for each valid $i$, there is an integer $b_{i}$ written on the $i$-th edge. You want the following condition to be satisfied: for each vertex $v$ and each edge $e$ adjacent to $v$, $a_{v} β‰₯ b_{e}$. In order to achieve that, you may perform an arbitrary number of steps (including zero). In one step, you may perform one the following operations: 1. Select two different vertices $u$ and $v$. Swap $a_{u}$ and $a_{v}$. 2. Select two different edges $e$ and $f$. Swap $b_{e}$ and $b_{f}$. 3. Select a vertex $v$ and an integer $x$. Change $a_{v}$ to $x$ and pay one coin. Calculate the minimum number of coins you need in order to satisfy the required condition. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N-1$ lines follow. For each $i$ ($1 ≀ i ≀ N-1$), the $i$-th of these lines contains three space-separated integers $u_{i}$, $v_{i}$ and $b_{i}$ denoting that the $i$-th edge connects vertices $u_{i}$ and $v_{i}$ and the initial value written on it is $b_{i}$. The last line contains $N$ space-separated integers $a_{1}, a_{2}, \ldots, a_{N}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum necessary number of coins. ------ Constraints ------ $1 ≀ T ≀ 10$ $2 ≀ N ≀ 100,000$ $1 ≀ u_{i}, v_{i} ≀ N$ for each valid $i$ $1 ≀ b_{i} ≀ 10^{9}$ for each valid $i$ $1 ≀ a_{i} ≀ 10^{9}$ for each valid $i$ the graph on the input is a tree ------ Subtasks ------ Subtask #1 (10 points): $N ≀ 7$ Subtask #2 (10 points): $N ≀ 10$ Subtask #3 (30 points): $N ≀ 200$ Subtask #4 (50 points): original constraints ----- Sample Input 1 ------ 1 3 1 2 4 2 3 7 1 5 10 ----- Sample Output 1 ------ 1 ----- explanation 1 ------ Example case 1: There is no way to satisfy the required condition without paying any coins. When we have one coin, we can perform the following operations: - Swap the integers written on vertices $1$ and $2$. - Pay one coin and change the integer written on vertex $2$ to $7$.
def solve(V, E): E.sort() V.sort() edge_ptr, vertex_ptr, coins = 0, 0, 0 while edge_ptr < len(E) and vertex_ptr < len(V): while vertex_ptr < len(V) and V[vertex_ptr] < E[edge_ptr]: vertex_ptr += 1 coins += 1 edge_ptr += 1 vertex_ptr += 1 return coins for _ in range(int(input())): n = int(input()) E = [] for _ in range(n - 1): u, v, b = map(int, input().split()) E.append(b) V = [int(x) for x in input().split()] print(solve(V, E))
FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a tree with $N$ vertices (numbered $1$ through $N$). Its edges are numbered $1$ through $N-1$. For each valid $i$, there is an integer $a_{i}$ written on the $i$-th vertex. Also, for each valid $i$, there is an integer $b_{i}$ written on the $i$-th edge. You want the following condition to be satisfied: for each vertex $v$ and each edge $e$ adjacent to $v$, $a_{v} β‰₯ b_{e}$. In order to achieve that, you may perform an arbitrary number of steps (including zero). In one step, you may perform one the following operations: 1. Select two different vertices $u$ and $v$. Swap $a_{u}$ and $a_{v}$. 2. Select two different edges $e$ and $f$. Swap $b_{e}$ and $b_{f}$. 3. Select a vertex $v$ and an integer $x$. Change $a_{v}$ to $x$ and pay one coin. Calculate the minimum number of coins you need in order to satisfy the required condition. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N-1$ lines follow. For each $i$ ($1 ≀ i ≀ N-1$), the $i$-th of these lines contains three space-separated integers $u_{i}$, $v_{i}$ and $b_{i}$ denoting that the $i$-th edge connects vertices $u_{i}$ and $v_{i}$ and the initial value written on it is $b_{i}$. The last line contains $N$ space-separated integers $a_{1}, a_{2}, \ldots, a_{N}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum necessary number of coins. ------ Constraints ------ $1 ≀ T ≀ 10$ $2 ≀ N ≀ 100,000$ $1 ≀ u_{i}, v_{i} ≀ N$ for each valid $i$ $1 ≀ b_{i} ≀ 10^{9}$ for each valid $i$ $1 ≀ a_{i} ≀ 10^{9}$ for each valid $i$ the graph on the input is a tree ------ Subtasks ------ Subtask #1 (10 points): $N ≀ 7$ Subtask #2 (10 points): $N ≀ 10$ Subtask #3 (30 points): $N ≀ 200$ Subtask #4 (50 points): original constraints ----- Sample Input 1 ------ 1 3 1 2 4 2 3 7 1 5 10 ----- Sample Output 1 ------ 1 ----- explanation 1 ------ Example case 1: There is no way to satisfy the required condition without paying any coins. When we have one coin, we can perform the following operations: - Swap the integers written on vertices $1$ and $2$. - Pay one coin and change the integer written on vertex $2$ to $7$.
import sys T = int(input()) for t in range(T): N = int(sys.stdin.readline()) nei = [set([]) for _ in range(N + 1)] edge_values = [None] * (N - 1) for n in range(N - 1): a, b, v = list(map(int, input().split())) nei[a].add(b) nei[b].add(a) edge_values[n] = v node_values = list(map(int, input().split())) node_values.sort(reverse=True) edge_values.sort(reverse=True) todo = [1] edge_values = [edge_values[0]] + edge_values edgevalidx = 0 nodevalidx = 0 while todo: node = todo.pop() last_edge = edge_values[edgevalidx] edgevalidx += 1 if node_values[nodevalidx] >= last_edge: nodevalidx += 1 for neighbour in nei[node]: nei[neighbour].remove(node) todo.extend(nei[node]) print(N - nodevalidx)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NONE BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP LIST VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a tree with $N$ vertices (numbered $1$ through $N$). Its edges are numbered $1$ through $N-1$. For each valid $i$, there is an integer $a_{i}$ written on the $i$-th vertex. Also, for each valid $i$, there is an integer $b_{i}$ written on the $i$-th edge. You want the following condition to be satisfied: for each vertex $v$ and each edge $e$ adjacent to $v$, $a_{v} β‰₯ b_{e}$. In order to achieve that, you may perform an arbitrary number of steps (including zero). In one step, you may perform one the following operations: 1. Select two different vertices $u$ and $v$. Swap $a_{u}$ and $a_{v}$. 2. Select two different edges $e$ and $f$. Swap $b_{e}$ and $b_{f}$. 3. Select a vertex $v$ and an integer $x$. Change $a_{v}$ to $x$ and pay one coin. Calculate the minimum number of coins you need in order to satisfy the required condition. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N-1$ lines follow. For each $i$ ($1 ≀ i ≀ N-1$), the $i$-th of these lines contains three space-separated integers $u_{i}$, $v_{i}$ and $b_{i}$ denoting that the $i$-th edge connects vertices $u_{i}$ and $v_{i}$ and the initial value written on it is $b_{i}$. The last line contains $N$ space-separated integers $a_{1}, a_{2}, \ldots, a_{N}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum necessary number of coins. ------ Constraints ------ $1 ≀ T ≀ 10$ $2 ≀ N ≀ 100,000$ $1 ≀ u_{i}, v_{i} ≀ N$ for each valid $i$ $1 ≀ b_{i} ≀ 10^{9}$ for each valid $i$ $1 ≀ a_{i} ≀ 10^{9}$ for each valid $i$ the graph on the input is a tree ------ Subtasks ------ Subtask #1 (10 points): $N ≀ 7$ Subtask #2 (10 points): $N ≀ 10$ Subtask #3 (30 points): $N ≀ 200$ Subtask #4 (50 points): original constraints ----- Sample Input 1 ------ 1 3 1 2 4 2 3 7 1 5 10 ----- Sample Output 1 ------ 1 ----- explanation 1 ------ Example case 1: There is no way to satisfy the required condition without paying any coins. When we have one coin, we can perform the following operations: - Swap the integers written on vertices $1$ and $2$. - Pay one coin and change the integer written on vertex $2$ to $7$.
T = int(input()) for i in range(T): N = int(input()) b = [] a = [] for j in range(N - 1): edge = input().split() b.append(int(edge[2])) vertex_values = input().split() for j in range(N): a.append(int(vertex_values[j])) a.sort() b.sort() great = len(a) * [0] pos_b = 0 pos_a = 0 while pos_b < N - 1 and pos_a < N: if b[pos_b] <= a[pos_a]: pos_b += 1 else: great[pos_a] = pos_b pos_a += 1 while pos_a < N: great[pos_a] = N - 1 pos_a += 1 coins = 0 desired = list(range(1, N)) desired.append(N - 1) for j in range(0, N): if great[j] < desired[j - coins]: coins += 1 print(coins)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a tree with $N$ vertices (numbered $1$ through $N$). Its edges are numbered $1$ through $N-1$. For each valid $i$, there is an integer $a_{i}$ written on the $i$-th vertex. Also, for each valid $i$, there is an integer $b_{i}$ written on the $i$-th edge. You want the following condition to be satisfied: for each vertex $v$ and each edge $e$ adjacent to $v$, $a_{v} β‰₯ b_{e}$. In order to achieve that, you may perform an arbitrary number of steps (including zero). In one step, you may perform one the following operations: 1. Select two different vertices $u$ and $v$. Swap $a_{u}$ and $a_{v}$. 2. Select two different edges $e$ and $f$. Swap $b_{e}$ and $b_{f}$. 3. Select a vertex $v$ and an integer $x$. Change $a_{v}$ to $x$ and pay one coin. Calculate the minimum number of coins you need in order to satisfy the required condition. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N-1$ lines follow. For each $i$ ($1 ≀ i ≀ N-1$), the $i$-th of these lines contains three space-separated integers $u_{i}$, $v_{i}$ and $b_{i}$ denoting that the $i$-th edge connects vertices $u_{i}$ and $v_{i}$ and the initial value written on it is $b_{i}$. The last line contains $N$ space-separated integers $a_{1}, a_{2}, \ldots, a_{N}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum necessary number of coins. ------ Constraints ------ $1 ≀ T ≀ 10$ $2 ≀ N ≀ 100,000$ $1 ≀ u_{i}, v_{i} ≀ N$ for each valid $i$ $1 ≀ b_{i} ≀ 10^{9}$ for each valid $i$ $1 ≀ a_{i} ≀ 10^{9}$ for each valid $i$ the graph on the input is a tree ------ Subtasks ------ Subtask #1 (10 points): $N ≀ 7$ Subtask #2 (10 points): $N ≀ 10$ Subtask #3 (30 points): $N ≀ 200$ Subtask #4 (50 points): original constraints ----- Sample Input 1 ------ 1 3 1 2 4 2 3 7 1 5 10 ----- Sample Output 1 ------ 1 ----- explanation 1 ------ Example case 1: There is no way to satisfy the required condition without paying any coins. When we have one coin, we can perform the following operations: - Swap the integers written on vertices $1$ and $2$. - Pay one coin and change the integer written on vertex $2$ to $7$.
T = int(input()) for t in range(T): n = int(input()) edges = list(sorted(int(input().split()[-1]) for i in range(n - 1))) nodes = list(sorted(map(int, input().split()))) j, cnt = 0, 0 for i in range(n - 1): while j < len(nodes) and nodes[j] < edges[i]: j += 1 cnt += 1 j += 1 print(cnt)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR