description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) arr = [int(i) for i in input().split()] ans = [i for i in arr] def fun(arr, n, ans): for i in range(31, -1, -1): f, v = 0, 0 for j in range(n): if arr[j] & 1 << i != 0: f += 1 v = j if f > 1: break if f == 1: ans[0], ans[v] = ans[v], ans[0] return ans return ans ans = fun(arr, n, ans) for i in ans: print(i, end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR VAR VAR NUMBER RETURN VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) a = list(map(int, input().split())) ind = -1 for i in range(32, -1, -1): ct = 0 x = 1 << i for j in range(n): if a[j] & x == x: ct += 1 if ct == 1: ind = j if ct == 1: break else: ind = -1 if ind == -1: for i in a: print(i, end=" ") else: print(a[ind], end=" ") for i in a: if i == a[ind]: continue print(i, end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR STRING FOR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
input() a = input().split() print( a.pop( next( (x for x in zip(*(f"{int(x):30b}" for x in a)) if x.count("1") == 1), "1" ).index("1") ), *a, )
EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR STRING VAR VAR FUNC_CALL VAR STRING NUMBER STRING STRING VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) a = [int(s) for s in input().split(" ")] bitmap = [[] for _ in range(33)] for i, b in enumerate(a): b_bin = bin(b)[2:] for j, b_str in enumerate(b_bin[::-1]): if b_str == "1": bitmap[j].append(i) index = -1 for i in range(32, -1, -1): if bitmap[i].__len__() == 1: index = bitmap[i][0] break if index != -1: print(a[index], end=" ") for i in range(n): if i != index: print(a[i], end=" ") else: for i in range(n): print(a[i], end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) a = list(map(int, input().split())) bits = {i: (0) for i in range(0, 32)} for x in a: b = bin(x)[2:] counter = 0 for i in range(len(b) - 1, -1, -1): bits[counter] += int(b[i]) counter += 1 bits = list(filter(lambda x: x[1] == 1, bits.items())) m = 0 n = -1 for i in a: b = bin(i)[2:] s = 0 for l in bits: if len(b) > l[0]: if b[len(b) - l[0] - 1] == "1": s += pow(2, l[0]) if s > m: n = i m = s if n == -1: print(*a) else: print(*([n] + [x for x in a if x != n]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER IF VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER STRING VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR VAR VAR VAR VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) a = list(map(int, input().split())) lis = [0] * 31 for i in a: for j in range(31): if i & 2**j > 0: lis[j] += 1 nmax = 0 maxind = 0 for ind, i in enumerate(a): for j in range(31): if lis[j] >= 2: i |= 2**j i -= 2**j if nmax < i: nmax = i maxind = ind ans = [a[maxind]] for i in range(n): if i != maxind: ans.append(a[i]) print(" ".join(map(str, ans)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) l = list(map(int, input().split())) a = [[] for i in range(32)] for i in range(n): num = l[i] j = 0 while num > 0: if num % 2 == 1: a[j].append(i) num = num // 2 j += 1 flag = 0 ind = 0 for i in range(31, -1, -1): if len(a[i]) == 1: ind = a[i][0] flag = 1 break if flag == 0: for i in l: print(i, end=" ") print() else: temp = l[0] l[0] = l[ind] l[ind] = temp for i in l: print(i, end=" ") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) a = [int(i) for i in input().split()] ones = [set() for _ in range(34)] for idx, i in enumerate(a): for j in range(34): if i >> j & 1 == 1: ones[j].add(idx) maxi = 0 for j in range(33, -1, -1): if len(ones[j]) == 1: maxi = ones[j].pop() break a[maxi], a[0] = a[0], a[maxi] print(" ".join([str(i) for i in a]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) l = list(map(int, input().split())) p = [0] * n temp = ~l[0] for i in range(1, n): p[i] = temp temp &= ~l[i] temp = ~l[-1] ans = [-1, -float("inf")] for i in range(n - 2, -1, -1): if i != 0: p[i] &= temp temp &= ~l[i] p[i] &= l[i] if ans[1] < p[i]: ans[0] = i ans[1] = p[i] else: p[i] = l[i] & temp if ans[1] < p[i]: ans[0] = i ans[1] = p[i] p[-1] &= l[-1] if ans[1] < p[-1]: ans[0] = n - 1 ans[1] = p[-1] print(l[ans[0]], end=" ") for i in range(n): if i == ans[0]: continue print(l[i], end=" ") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST NUMBER FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
import sys inp = sys.stdin.readline input = lambda: inp().strip() flush = sys.stdout.flush def iin(): return int(input()) def lin(): return list(map(int, input().split())) def main(): n = iin() a = lin() ch = [0] * 35 for i in a: ch1 = 0 x = i while x: if x % 2: ch[ch1] += 1 x //= 2 ch1 += 1 sl = [0] * n for i in range(n): ch1 = 0 x = a[i] while x: if x % 2: if ch[ch1] == 1: sl[i] += 2**ch1 x //= 2 ch1 += 1 sl = [[sl[i], i] for i in range(n)] sl.sort(reverse=True) ans = [a[j] for i, j in sl] print(*ans) main()
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR 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 BIN_OP LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR IF BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR IF BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) l = [int(x) for x in input().split()] l.sort() l.reverse() ll = [] sol = [] leng = len(bin(l[0])[2:]) i = 0 for x in l: ll.append("0" * (leng - len(bin(x)[2:])) + bin(x)[2:]) idx = -1 while i < leng: idx = -1 for x in range(n): if ll[x][i] == "1": if idx == -1: idx = x else: idx = -2 if idx == -2: break if idx >= 0: sol.append(idx) i += 1 for x in sol: if ll[x] != -1: print(int(ll[x], 2), end=" ") ll[x] = -1 for x in ll: if x != -1: print(int(x, 2), end=" ")
ASSIGN VAR FUNC_CALL VAR 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 LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER STRING ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER STRING
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) A = list(map(int, input().split())) B = [([0] * 33) for i in range(n)] C = [0] * 33 for i in range(n): t = A[i] j = 0 while t > 0: B[i][j] += t % 2 C[j] += B[i][j] t //= 2 j += 1 M2 = [1] for i in range(40): M2.append(M2[-1] * 2) S = [0] * n for i in range(n): for j in range(33): if B[i][j] == 1: if C[j] == 1: S[i] += M2[j] ind = S.index(max(S)) ANS = [A[ind]] for i in range(n): if i != ind: ANS.append(A[i]) print(" ".join([str(i) for i in ANS]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
import sys input = sys.stdin.readline n = int(input()) a = list(map(int, input().split())) ruiseki = [0] * (n + 1) ruiseki2 = [0] * (n + 1) for i in range(n): ruiseki[i + 1] = ruiseki[i] | a[i] for i in range(n): ruiseki2[i + 1] = ruiseki2[i] | a[n - (i + 1)] ans = 0 ind = 0 for i in range(n): tmp = ruiseki[i] | ruiseki2[n - (i + 1)] if ans <= (tmp | a[i]) - tmp: ans = (tmp | a[i]) - tmp ind = i ans = [a[ind]] + a[0:ind] + a[ind + 1 :] print(*ans)
IMPORT ASSIGN 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 BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) A = [int(x) for x in input().split()] bit = 2**30 while bit: B = [] for i in range(n): if A[i] & bit: B.append(i) if len(B) == 1: print(" ".join(str(x) for x in [A[B[0]]] + A[: B[0]] + A[B[0] + 1 :])) exit() bit >>= 1 print(" ".join(str(x) for x in A))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR BIN_OP BIN_OP LIST VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) arr = list(map(int, input().split())) bs = 0 for i in range(32, -1, -1): cnt = 0 for j, c in enumerate(arr): if c >> i & 1: cnt += 1 bs = j if cnt == 1: break print(arr[bs], end=" ") for j, c in enumerate(arr): if j != bs: print(c, end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR STRING
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) a = [int(i) for i in input().split()] ans = 0 for i in range(31, -1, -1): cnt = 0 cur = -1 for j in range(n): if a[j] >> i & 1 == 0: cnt += 1 else: cur = j if cnt == n - 1: ans = cur break print(a.pop(ans), *a)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) mx = 0 a = [0] * n k = 0 for i in input().split(): a[k] = int(i) mx = max(a[k], mx) k += 1 m = 0 while 2**m <= mx: m += 1 rz = 0 for i in range(m, -1, -1): if rz: break for j in range(n): if a[j] & 2**i: if rz: rz = 0 break else: rz = a[j] if rz: print(rz, "", end="") a.remove(rz) n -= 1 if n: for i in range(n): print(a[i], "", end="")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR IF VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR STRING STRING EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING STRING
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) s = list(map(int, input().split())) for i in range(30, -1, -1): if sum(1 for x in s if x & 1 << i) == 1: s.sort(key=lambda x: -(x & 1 << i)) break print(*s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) aa = [int(i) for i in input().split()] pre = [~aa[0]] suff = [~aa[-1]] for i in range(1, n): pre.append(pre[-1] & ~aa[i]) for i in range(n - 2, -1, -1): suff.append(suff[-1] & ~aa[i]) suff = suff[::-1] maxi, ind = -1, -1 for i in range(0, n): cur = aa[i] if i == 0: if n > 1: check = cur & suff[1] if maxi < check: maxi = check ind = i elif maxi < cur: maxi = cur ind = i elif i == n - 1: if n > 1: check = cur & pre[i - 1] if maxi < check: maxi = i ind = i elif maxi < cur: maxi = cur ind = i else: check = pre[i - 1] & cur & suff[i + 1] if maxi < check: maxi = check ind = i ans = [aa[ind]] ans += aa[ind + 1 :] ans += aa[:ind] print(*ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
class SegTree: def __init__(self, arr): self.arr = arr self.tree = [None for _ in range(2 * len(arr) * 2)] self.build() def build(self, node=1, start=0, end=None): if end == None: end = len(self.arr) - 1 if start == end: self.tree[node] = self.arr[start] else: mid = (start + end) // 2 self.build(node * 2, start, mid) self.build(node * 2 + 1, mid + 1, end) self.tree[node] = self.tree[node * 2] | self.tree[node * 2 + 1] def update(self, idx, val, node=1, start=0, end=None): if end == None: end = len(self.arr) - 1 if start == end: self.arr[idx] = val self.tree[node] = val else: mid = (start + end) // 2 if start <= idx <= mid: self.update(idx, val, 2 * node, start, mid) else: self.update(idx, val, 2 * node + 1, mid + 1, end) self.tree[node] = self.tree[2 * node] | self.tree[2 * node + 1] input() arr = list(map(int, input().strip().split(" "))) tree = SegTree(arr.copy()) cumilative_or = [None for _ in range(len(arr))] max_f = -1 first_idx = -1 for idx, a in enumerate(arr): tree.update(idx, 0) f = (a | tree.tree[1]) - tree.tree[1] if f > max_f: max_f = f first_idx = idx tree.update(idx, a) print(arr[first_idx], end=" ") for idx, a in enumerate(arr): if idx == first_idx: continue print(a, end=" ") print()
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_DEF NUMBER NUMBER NONE IF VAR NONE ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF NUMBER NUMBER NONE IF VAR NONE ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NONE VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
def f(x, y): return (x | y) - y n = int(input()) l = list(map(int, input().split())) mp = [(0) for _ in range(31)] for num in l: b = bin(num)[2:].zfill(31) for i in range(31): if b[i] == "1": mp[i] += 1 largest = 0 largestNum = l[0] for num in l: b = bin(num)[2:].zfill(31) tmp = int( "".join([("1" if b[i] == "1" and mp[i] == 1 else "0") for i in range(31)]), 2 ) if tmp > largest: largest = tmp largestNum = num print(largestNum, end=" ") l.remove(largestNum) for num in l: print(num, end=" ")
FUNC_DEF RETURN BIN_OP BIN_OP VAR 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 NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR STRING VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING VAR VAR STRING VAR VAR NUMBER STRING STRING VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) l = list(map(int, input().split())) try: ans = [] for i in range(31, -1, -1): c = 0 tem = -100 flag = 0 for j in l: if j >> i & 1: tem = j c += 1 if c == 1: ans.append(tem) flag = 1 break g = l.index(ans.pop(-1)) an = [l[g]] for i in range(n): if i != g: an.append(l[i]) print(*an) except: print(*l)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
def f(x, y): return (x | y) - y n = int(input()) s = list(map(int, input().split())) l = [] for i in range(32): l.append([]) for i in range(n): x = s[i] count = 0 while x > 0: if x % 2 != 0: l[count].append(s[i]) x = x // 2 count += 1 flag = 0 first = 0 for i in range(len(l)): if len(l[i]) == 1: first = max(l[i]) flag = 1 if flag == 0: for i in range(n): print(s[i], end=" ") else: print(first, end=" ") s.remove(first) for i in range(len(s)): print(s[i], end=" ")
FUNC_DEF RETURN BIN_OP BIN_OP VAR 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 LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
q = int(input()) numArray = [int(x) for x in input().split()] binaryArray = ["{:030b}".format(x) for x in numArray] max = 0 first = -1 for x in range(30): count = 0 for y in range(q): if int(binaryArray[y][x]) == 1: count += 1 if count == 1: for y in range(q): if int(binaryArray[y][x]) == 1: first = y break if first != -1: break numArray[0], numArray[first] = numArray[first], numArray[0] print(*numArray)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL STRING VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) a = list(map(int, input().split())) suf = [0] * (n + 1) for q in range(n - 1, -1, -1): suf[q] = suf[q + 1] | a[q] naw, ans = 0, [-1, -1] for q in range(n): w = a[q] & ~(naw | suf[q + 1]) if w > ans[0]: ans = [w, q] naw |= a[q] print(a[ans[1]], *a[: ans[1]], *a[ans[1] + 1 :])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR LIST VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) a = list(map(int, input().split())) b = set(a) x = max(a) d = 0 c = 0 r = 0 while x: x >>= 1 d += 1 for i in range(d - 1, -1, -1): c = 0 for j in b: if j >> i & 1: c += 1 r = j if c == 1: break a.remove(r) print(r, " ".join(map(str, a)))
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 ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) (*arr,) = map(int, input().split()) def CountBits(n): n = (n & 6148914691236517205) + ((n & 12297829382473034410) >> 1) n = (n & 3689348814741910323) + ((n & 14757395258967641292) >> 2) n = (n & 1085102592571150095) + ((n & 17361641481138401520) >> 4) n = (n & 71777214294589695) + ((n & 18374966859414961920) >> 8) n = (n & 281470681808895) + ((n & 18446462603027742720) >> 16) n = (n & 4294967295) + ((n & 18446744069414584320) >> 32) return n def f(arr): res = arr[0] for v in arr[1:]: res = (res | v) - v print(bin(res)) return res cnt = {} bits = 0 for v in arr: bits |= v stat = [0] * 32 p2idx = {(2**i): i for i in range(32)} for i in range(n): p = 1 p_idx = 0 while p < 10**9: if arr[i] & p: stat[p_idx] += 1 p_idx += 1 p *= 2 mask = 0 for i, cnt in enumerate(stat): if cnt == 1: mask |= 1 << i arr.sort(key=lambda obj: -(obj & mask)) print(*arr)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) a = list(map(int, input().split())) a.sort(reverse=True) data = [] use = [(True) for _ in range(n)] for i in range(30, -1, -1): key = -1 for j, x in enumerate(a): if x >> i & 1 and key == -1: key = j elif x >> i & 1: key = -1 break if key != -1: if use[key]: data.append(key) use[key] = False ans = [] for key in data: ans.append(a[key]) for i in range(n): if use[i]: ans.append(a[i]) print(*ans)
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 LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
import sys read = lambda: list(map(int, sys.stdin.readline().strip().split())) n = int(input()) a = read() s = 30 nu = -1 while s >= 0: count = 0 for i in range(n): if a[i] & 1 << s: count += 1 if count == 1: for i in range(n): if a[i] & 1 << s: nu = a[i] break break s -= 1 if nu == -1: for num in a: print(num, end=" ") print() else: print(nu, end=" ") for num in a: if num != nu: print(num, end=" ")
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR STRING FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR STRING
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
from sys import stdin, stdout def main(): n = int(stdin.readline()) arr = list(map(int, stdin.readline().split())) pow2 = [0] * 32 max = 0 ans = [0] * 32 for j, i in enumerate(arr): x = int(i) z = 0 while x > 0: if (x - pow(2, z)) % pow(2, z + 1) == 0: x -= pow(2, z) pow2[z] += 1 if pow2[z] == 1: ans[z] = j z += 1 for i, x in enumerate(pow2): if x == 1: max = i if pow2[max] == 1: arr[0], arr[ans[max]] = arr[ans[max]], arr[0] stdout.write(" ".join(str(x) for x in arr)) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP BIN_OP VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) arr = [int(x) for x in input().split()] for i in range(30, -1, -1): dem = 0 for x in arr: if x & 1 << i: dem += 1 if dem == 1: cur = 0 for j in range(n): if arr[j] & 1 << i: cur = j print(arr[cur], end=" ") for j in range(n): if j != cur: print(arr[j], end=" ") exit() for x in arr: print(x, end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
import sys n = int(input()) lst = list(map(int, input().split())) pre = [(0) for i in range(n + 1)] suf = [(0) for i in range(n + 1)] pre[0] = sys.maxsize for i in range(n): pre[i + 1] = pre[i] & ~lst[i] suf[n] = sys.maxsize for i in range(n - 1, -1, -1): suf[i] = suf[i + 1] & ~lst[i] Max = -sys.maxsize Ans = None for i in range(n): temp = pre[i] & lst[i] & suf[i + 1] if temp > Max: Ans = i Max = temp print(lst[Ans], end=" ") for i in range(n): if i == Ans: continue else: print(lst[i], end=" ") print()
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) al = list(map(int, input().split())) bits = [(0) for i in range(40)] for a in al: idx = 0 while a > 0: bits[idx] += a % 2 a //= 2 idx += 1 ans = 0 for idx, b in enumerate(bits): if b == 1: ans += 2**idx tmp = 0 ans2 = al[0] for a in al: k = ans & a if k > tmp: tmp = k ans2 = a al.remove(ans2) print(ans2, *al)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
def answer(n, A): if n == 1: return A pre = [0] * n pre[0] = ~A[0] for i in range(1, n): pre[i] = ~A[i] & pre[i - 1] suff = [0] * n suff[-1] = ~A[-1] for i in range(n - 2, -1, -1): suff[i] = ~A[i] & suff[i + 1] ele = A[0] maxi = A[0] & suff[1] ans = 0 for i in range(1, n): if i == n - 1: ans = A[i] & pre[i - 1] else: ans = A[i] & (pre[i - 1] & suff[i + 1]) if ans > maxi: maxi = ans ele = A[i] A.remove(ele) return [ele] + A n = int(input()) arr = list(map(int, input().split())) print(*answer(n, arr))
FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN BIN_OP LIST 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 FUNC_CALL VAR VAR VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) vals = list(map(int, input().split())) pref, suff = [0] * (n + 1), [0] * (n + 1) for i in range(n): pref[i + 1] = pref[i] | vals[i] suff[n - i - 1] = suff[n - i] | vals[n - i - 1] ret = -float("inf"), -float("inf") for i, a in enumerate(vals): b = pref[i] | suff[i + 1] ret = max(ret, ((a | b) - b, i)) print(*([vals[ret[1]]] + [v for i, v in enumerate(vals) if i != ret[1]]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
def main(): n = int(input()) a = [int(i) for i in input().split()] before = [0] * n after = [0] * n accleft = 0 for i in range(n): before[i] = accleft accleft |= a[i] accright = 0 i = n - 1 while i >= 0: after[i] = accright accright |= a[i] i -= 1 answer = a[0] & ~before[0] & ~after[0] bindex = 0 for i in range(1, n): v = a[i] & ~before[i] & ~after[i] if v > answer: bindex = i answer = v print(a[bindex], end=" ") for i in range(n): if i != bindex: print(a[i], end=" ") main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN 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 ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) a = list(map(int, input().split())) pre = [(0) for i in range(n + 1)] suf = [(0) for i in range(n + 1)] for i in range(n): if i == 0: pre[i] = a[i] else: pre[i] = pre[i - 1] | a[i] for i in range(n - 1, -1, -1): if i == n - 1: suf[i] = a[i] else: suf[i] = suf[i + 1] | a[i] ans = -1 p = 0 for i in range(n): if pre[n - 1] - (pre[i - 1] | suf[i + 1]) > ans: p = i ans = pre[n - 1] - (pre[i - 1] | suf[i + 1]) print(a[p], end=" ") for i in range(n): if i != p: print(a[i], end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) a = [int(x) for x in input().split()] l = sorted(a, reverse=True) x = l[0] bits = 0 while x > 0: x = x >> 1 bits += 1 num = 0 for i in range(bits): count = 0 ind = 0 z = 1 << bits - i - 1 for j in range(len(a)): if z & l[j] == z: count += 1 ind = j if count > 1: break if count == 1: num = ind break l[num], l[0] = l[0], l[num] print(*l)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
def Input(): tem = input().split() ans = [] for it in tem: ans.append(int(it)) return ans n = Input()[0] a = Input() l_or = [(0) for i in range(n)] tem = 0 for i in range(n): l_or[i] = tem | a[i] tem = l_or[i] inx = 0 MAX = 0 tem = 0 for i in range(n - 1, -1, -1): if i - 1 >= 0: mean = a[i] - (a[i] & (l_or[i - 1] | tem)) else: mean = a[i] - (a[i] & tem) if mean > MAX: MAX = mean inx = i tem = tem | a[i] print(a[inx], end="") for i in range(n): if i == inx: continue print("", a[i], end="") print()
FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING VAR VAR STRING EXPR FUNC_CALL VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
def wrapper(func): def inner(): return next(func) return inner n = int(input()) a = list(map(int, input().strip().split())) INF = (1 << 60) - 1 pre = [0] * (n + 1) pre[n] = INF for i in range(n): if i == 0: pre[i] = ~a[i] else: pre[i] = pre[i - 1] & ~a[i] suf = [0] * (n + 1) suf[n] = INF for i in range(n - 1, -1, -1): if i == n - 1: suf[i] = ~a[i] else: suf[i] = suf[i + 1] & ~a[i] mx, id = -1, -1 for i in range(n): tmp = a[i] & pre[i - 1] & suf[i + 1] if tmp > mx: mx = tmp id = i print(a[id], end=" ") for i in range(n): if i == id: continue print(a[i], end=" ")
FUNC_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR RETURN 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 BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) a = [int(s) for s in input().split()] b = [(-1) for i in range(32)] for i in range(n): t = a[i] j = 0 while t > 0: if t % 2 == 1: if b[j] == -1: b[j] = i elif b[j] >= 0: b[j] = -2 j += 1 t //= 2 k = 0 for i in range(31, -1, -1): if b[i] >= 0: k = b[i] break print(a[k], end=" ") for i in range(n): if i != k: print(a[i], end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
from sys import stdin input = stdin.readline n = int(input()) a = list(map(int, input().split())) mask = 2**30 while mask and len([ai for ai in a if ai & mask]) != 1: mask //= 2 if mask: i = next(i for i in range(n) if a[i] & mask) a[i], a[0] = a[0], a[i] print(*a)
ASSIGN 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 BIN_OP NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
N = int(input()) A = list(map(int, input().split())) ans = -1 for i in range(32, -1, -1): count = 0 ans = -1 for n in A: if 1 << i & n != 0: ans = n count += 1 if count == 1: break if ans != -1: print(ans, end=" ") for n in A: if n != ans: print(n, end=" ") else: ans = -1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR STRING FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR NUMBER
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) l = list(map(int, input().split())) b = [0] * 32 for i in range(n): p = l[i] for j in range(32): b[j] += p % 2 p = p // 2 ai = 0 m = -1 for i in range(n): p = l[i] q = 0 for j in range(32): if p % 2 == 1 and b[j] == 1: q += 2**j p = p // 2 if q > m: m = q ai = i ans = [l[ai]] + l[:ai] + l[ai + 1 :] print(*ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) a = list(map(int, input().split())) l = [set() for i in range(50)] for i, x in enumerate(a): y = list(reversed(str(bin(x)))) for j, k in enumerate(y): if k == "1": l[j].add(i) ans = -1 for x in l: if len(x) == 1: ans = list(x)[0] if ans == -1: a.sort(reverse=True) print(" ".join(map(str, a))) else: x = a.pop(ans) a.sort(reverse=True) b = [x] + a print(" ".join(map(str, b)))
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 FUNC_CALL VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) a = list(map(int, input().split())) b = [-1] * 32 ans = -1 for it in range(n): i = a[it] bi = bin(i)[2:][-1::-1] for ii, j in enumerate(bi): if j == "1": if b[ii] == -1: b[ii] = it else: b[ii] = -2 for i in range(31, -1, -1): if b[i] == -1 or b[i] == -2: continue else: ans = b[i] break if ans == -1: print(*a) else: print(a[ans], end=" ") for i in range(n): if i == ans: continue print(a[i], end=" ") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
import sys input = sys.stdin.readline n = int(input()) arr = [int(k) for k in input().split()] num = 0 for i in range(30, -1, -1): count = 0 for j in range(n): if arr[j] >> i & 1: count += 1 num = arr[j] if count == 1: break arr.remove(num) print(num, " ".join(map(str, arr)))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) x = list(map(int, input().split())) bi, ans, skip = [[] for i in range(31)], [], [0] * 31 ma = 0 for i in range(n): b = str(bin(x[i])) for i2 in range(len(b) - 1, -1, -1): if b[i2] == "b": break if b[i2] == "1": bi[len(b) - i2 - 1].append(i) ma = max(ma, len(b) - i2 - 1) for i in range(ma, -1, -1): if skip[i] == 1: continue if len(bi[i]) == 1: ans.append(bi[i][0]) for i2 in range(ma + 1): if bi[i][0] in bi[i]: skip[i2] = 1 for i in range(len(ans)): print(x[ans[i]], end=" ") for i in range(n): if i not in ans: print(x[i], end=" ") print("")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR LIST VAR FUNC_CALL VAR NUMBER LIST BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR STRING
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) a = list(map(int, input().split())) b = [bin(ele)[2:] for ele in a] b = [("0" * (32 - len(ele)) + ele) for ele in b] found = -1 for i in range(32): ans = -1 count = 0 for idx, ele in enumerate(b): if ele[i] == "1": count += 1 ans = idx if count == 1: found = ans break if found != -1: a[0], a[found] = a[found], a[0] print(*a)
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 VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) x = list(map(int, input().split())) y = 0 for i in range(32, -1, -1): c = 0 temp = -1 for j in range(n): if x[j] >> i & 1 == 0: c = c + 1 else: temp = j if c == n - 1: y = temp break x[0], x[y] = x[y], x[0] print(*x)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) a = [int(v) for v in input().split()] x = [[] for _ in range(64)] for i, v in enumerate(a): mask = 1 for k in range(64): if v & mask: x[k].append(i) mask <<= 1 z = 0 for k in range(63, -1, -1): if len(x[k]) == 1: z = x[k][0] break print(a[z], *(a[:z] + a[z + 1 :]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) a = list(map(int, input().split(" "))) if n == 1: print(a[0]) else: prefix = [0] * n suffix = [0] * n prefix[0] = ~a[0] suffix[n - 1] = ~a[n - 1] for i in range(1, n): prefix[i] = prefix[i - 1] & ~a[i] for i in range(2, n + 1): suffix[n - i] = suffix[n - i + 1] & ~a[n - i] element = -1 * 2**32 elem_index = 0 for i in range(n): if i == 0: if a[i] & suffix[i + 1] > element: element = a[i] & suffix[i + 1] elem_index = i elif i == n - 1: if a[i] & prefix[i - 1] > element: element = a[i] & prefix[i - 1] elem_index = i elif a[i] & prefix[i - 1] & suffix[i + 1] > element: element = a[i] & prefix[i - 1] & suffix[i + 1] elem_index = i temp = a[0] a[0] = a[elem_index] a[elem_index] = temp print(" ".join(list(map(str, a))))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
import sys n = int(sys.stdin.readline().rstrip()) num = list(map(int, sys.stdin.readline().rstrip().split())) max_len = 0 for i in num: bi = bin(i)[2:] if max_len < len(bi): max_len = len(bi) st = 0 max_len -= 1 while True: cnt = 0 for idx, i in enumerate(num): if 2**max_len & i > 0: cnt += 1 if cnt > 1: break if cnt == 1: st = idx if cnt == 1: break max_len -= 1 if max_len < 0: break print(num[st], end=" ") del num[st] for i in num: print(i, end=" ")
IMPORT 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 ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
def main(): n = int(input().strip()) A = [int(s) for s in input().strip().split()] for i in range(31, -1, -1): if sum(a >> i & 1 for a in A) == 1: break for j, a in enumerate(A): if a >> i & 1: break result = [A[j]] + A[:j] + A[j + 1 :] print(" ".join(str(x) for x in result)) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) a = list(map(int, input().split())) r = 0 for i in range(29, -1, -1): c = 0 for j in a: if j >> i & 1: c += 1 r = j if c == 1: break a.remove(r) print(r, " ".join(map(str, a))) num_inp = lambda: int(input()) arr_inp = lambda: list(map(int, input().split())) sp_inp = lambda: map(int, input().split()) str_inp = lambda: input()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL STRING FUNC_CALL VAR 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 FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) arr = [[] for i in range(32)] l = input().split() li = [int(i) for i in l] for i in li: for j in range(32): if i & 1 << j: arr[j].append(i) maxa = -1 for i in range(31, -1, -1): if len(arr[i]) == 1: maxa = arr[i][0] break if maxa == -1: for i in li: print(i, end=" ") else: done = 0 print(maxa, end=" ") for i in li: if i == maxa and done == 0: done = 1 continue print(i, end=" ") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR STRING FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
N = int(input()) L = list(map(int, input().split())) R = [] for i in range(N): R.append([0] * 32) cnt = [0] * 32 for i in range(N): x = L[i] n = 0 while x: if x % 2 == 1: R[i][n] = 1 cnt[n] += 1 else: R[i][n] = 0 n += 1 x = x // 2 index = 0 for i in range(31, 0, -1): if cnt[i] == 1: index = i break ind = 0 for i in range(N): if R[i][index] == 1: ind = i break print(L[ind], end=" ") for i in range(N): if i != ind: print(L[i], end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN 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 LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
import sys input = sys.stdin.readline N = int(input()) A = list(map(int, input().split())) if N == 1: ans = A else: dp1 = [0] * N bit = 0 for i, a in enumerate(A): bit |= a dp1[i] = bit dp2 = [0] * N bit = 0 for i in reversed(range(N)): bit |= A[i] dp2[i] = bit score = -1 ind = -1 for i in range(N): if i == 0: tmp = dp2[1] elif i == N - 1: tmp = dp1[N - 2] else: tmp = dp1[i - 1] | dp2[i + 1] if (A[i] | tmp) - tmp > score: score = (A[i] | tmp) - tmp ind = i ans = [A[ind]] for i, a in enumerate(A): if i != ind: ans.append(a) print(*ans, sep=" ")
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) v = list(map(int, input().split())) max = -1 maxi = 0 for i in range(n): x = v[i] for j in range(n): if j == i: continue if x == 0: break x = (x | v[j]) - v[j] if x > max: max = x maxi = i print(v[maxi], end=" ") for i in range(n): if i != maxi: print(v[i], end=" ") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) pw = [(2 ** (30 - i)) for i in range(31)] def kek(x): global pw ans = set() mind = 0 for i in range(31): pwi = pw[i] if pwi <= x: x -= pwi ans.add(pwi) return ans ra = list(map(int, input().split())) a = list(map(kek, ra)) t = dict([[pw[i], 0] for i in range(31)]) for ii in a: for i in ii: t[i] += 1 q = set() meme = 1 for i in range(30, -1, -1): if t[pw[i]] > 1: q.add(pw[i]) m, mv = -1, -1 for i in a: idf = sum(i.difference(q)) if idf > mv: m, mv = i, idf m = sum(m) print(m, end=" ") nmust = False ra.pop(ra.index(m)) print(" ".join(map(str, ra)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) a = list(map(int, input().split())) max_c = 0 index = 0 bits = [] for i in range(n): w = bin(a[i])[2:] bits.append(w) p = w.count("1") if p > max_c: max_c = p index = i yy = max(a) for i in range(len(bits)): bits[i] = "0" * (len(bin(yy)[2:]) - len(bits[i])) + bits[i] length = len(bin(yy)[2:]) tf = [] for i in range(length): count = 0 for j in range(len(bits)): if bits[j][i] == "0": count += 1 if count == len(bits) - 1: tf.append(1) else: tf.append(0) max_number = 0 answer = 0 for i in range(len(bits)): max_n = 0 for j in range(len(bits[i])): if tf[j] == 1 and bits[i][j] == "1": max_n += 2 << length - 1 - j if max_number < max_n: max_number = max_n answer = i print(a[answer], end=" ") del a[answer] print(*a)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP STRING BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR STRING VAR BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING VAR VAR EXPR FUNC_CALL VAR VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) ar = [int(x) for x in input().split()] ar = sorted(ar)[::-1] d = {k: (0) for k in range(32)} for x in ar: for j in range(32): if x ^ 1 << j == x - (1 << j): d[j] += 1 maxn = 0 maxi = 0 for i, e in enumerate(ar): tmp = e for j in range(32): if e ^ 1 << j == e - (1 << j) and d[j] >= 2: tmp -= 1 << j if tmp > maxn: maxn = tmp maxi = i print(ar[maxi], end=" ") for i in range(len(ar)): if i != maxi: print(ar[i], end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR NUMBER VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
def solve(a_s): bits = 31 a_bits = [[(a >> bit & 1) for bit in range(bits - 1, -1, -1)] for a in a_s] ans_i = 0 for i in range(bits): lone_j = -1 for j, a_bit in enumerate(a_bits): if a_bit[i] == 1: if lone_j != -1: lone_j = -1 break lone_j = j if lone_j != -1: ans_i = lone_j break ans = [a_s[ans_i]] for i, a in enumerate(a_s): if i == ans_i: continue ans.append(a) return ans t = int(input()) a_s = [int(ch) for ch in input().split(" ")] print(" ".join([str(val) for val in solve(a_s)]))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR 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 FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
from sys import stdin, stdout for _ in range(1): n = int(stdin.readline()) a = list(map(int, stdin.readline().split())) ans = [] bits = [[(0) for _ in range(32)] for _ in range(n)] for i in range(n): num = a[i] for j in range(32): bits[i][j] = num >> j & 1 for i in range(31, -1, -1): cnt = 0 pos = -1 for j in range(n): if bits[j][i] == 1: cnt += 1 pos = j if cnt == 1: break if pos != -1: ans += [a[pos]] for i in range(n): if i == pos: continue ans += [a[i]] print(*ans)
FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER IF VAR NUMBER VAR LIST VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) lst = list(map(int, input().split())) positions = {} for num in lst: t = str(bin(num)) t = t[2:] t = t[::-1] for j in range(len(t)): if t[j] == "1": if j in positions: positions[j][1] += 1 else: positions[j] = [0, 1] elif j in positions: positions[j][0] += 1 else: positions[j] = [1, 0] mx = 0 ans = -1 for num in lst: t = str(bin(num)) t = t[2:][::-1] unique = [0] * len(t) for j in range(len(t)): cur = t[j] if cur == "0": continue elif positions[j][1] == 1: unique[j] = 1 nm = "".join(list(map(str, unique)))[::-1] nm = int(nm, 2) if nm >= mx: ans = num mx = nm pos = lst.index(ans) print(ans, end=" ") for i in range(n): if i == pos: continue print(lst[i], end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR LIST NUMBER NUMBER IF VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR STRING IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) a = list(map(int, input().split())) b = [] for i in range(n): b.append(str(int(bin(a[i]).replace("0b", "")) + 10**30)) flag, store = 0, 0 for i in range(1, 31): add = 0 for j in range(n): if b[j][i] == str(1): add += 1 store = j if add == 1: flag = -1 break if flag == 0: store = -2 else: print(a[store], end=" ") for i in range(n): if i != store: print(a[i], end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN 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 FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR VAR STRING STRING BIN_OP NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
def checker(n, lst): a, b = [0] * n, [0] * n for i in range(1, n): a[i] = a[i - 1] | lst[i - 1] b[n - i - 1] = b[len(lst) - i] | lst[n - i] elem, pos = -1, -1 for i in range(n): y = a[i] | b[i] cur = (lst[i] | y) - y if cur > elem: elem, pos = cur, i lst[0], lst[pos] = lst[pos], lst[0] return " ".join([str(x) for x in lst]) m = int(input()) c = [int(i) for i in input().split()] print(checker(m, c))
FUNC_DEF ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER VAR VAR VAR VAR VAR NUMBER RETURN FUNC_CALL STRING FUNC_CALL VAR VAR 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 FUNC_CALL VAR VAR VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) a = list(map(int, input().split())) x = max(a).bit_length() b = [([0] * x) for i in range(n)] for i in range(n): p = a[i] for j in range(x - 1, -1, -1): b[i][j] = p % 2 p //= 2 cnt = [0] * x for i in range(x): for j in range(n): cnt[i] += b[j][i] if cnt.count(1) == 0: print(*a) else: for i in range(x): if cnt[i] == 1: for j in range(n): if b[j][i] == 1: a[0], a[j] = a[j], a[0] print(*a) exit()
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 FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) arr = list(map(int, input().split())) pref = [0] * n suf = [0] * n pref[0] = arr[0] for i in range(1, n): pref[i] = pref[i - 1] | arr[i] suf[-1] = arr[-1] for i in range(n - 2, -1, -1): suf[i] = suf[i + 1] | arr[i] maxind = -1 maxans = -1 for i in range(n): prefval = pref[i - 1] if i else 0 sufval = suf[i + 1] if i < n - 1 else 0 val = arr[i] & ~(prefval | sufval) if val > maxans: maxind = i maxans = val print(arr[maxind], *arr[:maxind], *arr[maxind + 1 :])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) arr = [int(i) for i in input().split()] dic = [0] * 34 for i in arr: temp = i bit = 0 while temp: if temp & 1: dic[bit] += 1 temp = temp >> 1 bit += 1 ans = -1 first = 0 for i in range(len(arr)): tempdic = [0] * 34 temp = arr[i] bit = 0 rangeor = 0 while temp: if temp & 1: tempdic[bit] += 1 temp = temp >> 1 bit += 1 for j in range(len(tempdic)): if dic[j] - tempdic[j] > 0: rangeor += 2**j if (arr[i] | rangeor) - rangeor > ans: first = i ans = (arr[i] | rangeor) - rangeor ansarr = [arr[first]] for i in range(len(arr)): if i != first: ansarr.append(arr[i]) print(" ".join([str(i) for i in ansarr]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP NUMBER VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR LIST VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) a = list(map(int, input().split())) if n == 1: print(a[0]) else: com_a = [(~i) for i in a] prefix = [0] * n postfix = [0] * n prefix[0] = com_a[0] postfix[-1] = com_a[-1] for i in range(1, n): prefix[i] = prefix[i - 1] & com_a[i] postfix[n - i - 1] = com_a[n - i - 1] & postfix[n - i] MAX = -(10**10) ans = 0 for i in range(n): if i == 0: tmp = a[i] & postfix[i + 1] elif i == n - 1: tmp = a[i] & prefix[-2] else: tmp = a[i] & prefix[i - 1] & postfix[i + 1] if tmp >= MAX: ans = i MAX = tmp print(a[ans], end=" ") try: print(*a[:ans], end=" ") except IndexError: pass try: print(*a[ans + 1 :], end=" ") except IndexError: pass
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR STRING VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER STRING VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) a = [int(c) for c in input().split()] only_1 = (1 << 40) - 1 left = [only_1] right = [only_1] L = only_1 for i in range(n - 1): L = L & ~a[i] left.append(L) R = only_1 for i in range(n - 1): R = R & ~a[n - 1 - i] right.append(R) res = 0 res_i = 0 for i in range(n): r = a[i] & left[i] & right[n - 1 - i] if r > res: res_i = i res = r b = [a[res_i]] + [a[j] for j in range(n) if j != res_i] print(*b)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST VAR ASSIGN VAR LIST VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) l = [(int(el), "{:032b}".format(int(el))) for el in input().split(" ")] res = [] ind = -1 for pos in range(32): count = 0 for i in range(n): if l[i][1][pos] == "1": count += 1 ind = i if count == 1: res.append(l[ind][0]) break ind = -1 for i in range(n): if i != ind: res.append(l[i][0]) for el in res: print(el, end=" ") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR STRING VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) a = list(map(int, input().split())) fl = False maxqqq = 0 maxi = 0 for i in range(n): qqq = a[i] for j in range(n): if j != i: qqq = (qqq | a[j]) - a[j] if qqq <= maxqqq: break if qqq > maxqqq: maxqqq = qqq maxi = i fl = True if fl: a[maxi], a[0] = a[0], a[maxi] for i in range(n): print(a[i], end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) A = [int(i) for i in input().split()] A.sort(reverse=True) if n == 1: print(A[0]) else: x = A[0] steps = len(bin(x)) - 3 mask = x >> steps mask = mask << steps f1 = True f2 = True while f1: ind = 0 num = 0 while f2: x = A[ind] r = x & mask if r != 0: num += 1 if num > 1: f2 = False ind += 1 if ind >= n: f2 = False if num == 1: f1 = False else: mask = mask >> 1 f2 = True if mask == 0: f1 = False if mask != 0: f = True ix = 0 sx = A[ix] while f: sx = A[ix] if mask & sx != 0: f = False ix += 1 A.pop(ix - 1) A.insert(0, sx) B = [str(a) for a in A] print(" ".join(B))
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 IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) a = [int(x) for x in input().split()] bits = {i: [] for i in range(33)} for i in range(n): cnt = 0 x = a[i] while x: if x & 1: bits[cnt].append(i) x >>= 1 cnt += 1 find = False for i in range(32, -1, -1): if len(bits[i]) == 1: k = bits[i][0] ans = [a[k]] + a[:k] + a[k + 1 :] print(*ans) find = True break if not find: print(*a)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) a = list(map(int, input().split())) for d in range(29, -1, -1): bit = 1 << d count, j = 0, 0 for i, x in enumerate(a): if bit & x: count += 1 if count == 2: break else: j = i if count == 1: print(a[j], *(a[:j] + a[j + 1 :])) break else: print(*a)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) a = list(map(int, input().split())) a.sort() a = a[::-1] k = 0 m = 0 l1 = [0] * n l1[0] = a[0] l2 = [0] * n l2[n - 1] = a[n - 1] for i in range(1, n): l1[i] = l1[i - 1] | a[i] l2[n - 1 - i] = l2[n - i] | a[n - i - 1] for i in range(n): s = 0 if i > 0: s = s | l1[i - 1] if i < n - 1: s = s | l2[i + 1] s = (a[i] | s) - s if m < s: m = s k = i a[0], a[k] = a[k], a[0] print(*a)
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 ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
s = int(input()) arr = list(map(int, input().split())) zero_bits = dict() index_max = 0 for i in range(0, 32): zero_bits[i] = [] power_ = [(2**i) for i in range(0, 32)] _enumerate = enumerate _reversed = reversed for index, i in _enumerate(arr): temp = i for index_, j in _enumerate(_reversed(power_)): if temp >= j: zero_bits[index_].append(index) temp -= j for i in _reversed(range(32)): if len(zero_bits[i]) == 1: index_max = zero_bits[i][0] max_ = arr[index_max] del arr[index_max : index_max + 1] arr = [max_] + arr for i in arr: print(i, end=" ")
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 ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR LIST ASSIGN VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
def iinput(): return [int(x) for x in input().split()] def main(): n = int(input()) data = iinput() s = 0 data1 = [] for i in range(n): data1.append(s) s = s | data[i] data2 = [] w = 0 for i in range(n - 1, -1, -1): data2.append(w) w = w | data[i] data2 = data2[::-1] f = 0 x = 0 for i in range(n): z = abs((data2[i] | data1[i]) - s) if f < z: f = z x = i y = data[x] data.remove(y) data = [y] + data for i in range(n): data[i] = str(data[i]) return " ".join(data) for t in range(1): print(main())
FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL STRING VAR FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
input() a = input().split() for x in zip(*(f"{int(x):30b}" for x in a)): if x.count("1") == 1: i = x.index("1") a = [a.pop(i)] + a break print(*a)
EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR STRING VAR VAR IF FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
import sys from itertools import permutations input = sys.stdin.readline n = int(input()) a = [int(item) for item in input().split()] cnt = [0] * 32 for item in a: for i in range(32): if item & 1 << i: cnt[i] += 1 mask = 0 for i, item in enumerate(cnt): if item == 1: mask |= 1 << i max_val = -1 index = -1 for i, item in enumerate(a): if item & mask > max_val: max_val = item & mask index = i ans = [a[index]] + a[:index] + a[index + 1 :] print(*ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
def solve(a): count = [0] * 30 for e in a: for i in range(30): if e & 1 << i: count[i] += 1 i = 29 while i >= 0 and count[i] != 1: i -= 1 if i < 0: return a for j in range(len(a)): if a[j] & 1 << i: return [a[j]] + a[:j] + a[j + 1 :] assert False n = int(input()) a = list(map(int, input().split())) print(*solve(a))
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR RETURN BIN_OP BIN_OP LIST VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER 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 FUNC_CALL VAR VAR
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) a = [int(x) for x in input().split()] val = [0] * n pos = [list() for i in range(31)] cnt = [0] * 31 ans = int(0) for i in range(n): for j in range(31): if a[i] >> j & 1: cnt[j] += 1 pos[j].append(i) for i in range(31): if cnt[i] != 1: continue for p in pos[i]: val[p] += 1 << i for i in range(n): if val[i] > val[ans]: ans = i print(a[ans], end=" ") for i in range(n): if i != ans: print(a[i], end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER FOR VAR VAR VAR VAR VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = 0 a = [0] i = 0 j = 0 x = 0 b = [0] n = int(input()) b = list(map(int, input().split())) a = a + b cnt = [0] * 32 for i in range(1, n + 1): x = a[i] for j in range(0, 32): cnt[j] += x & 1 x >>= 1 high = 0 for high in range(31, -1, -1): if cnt[high] == 1: break if high == -1: for i in range(1, n + 1): print(a[i], end=" ") else: r = 0 for r in range(1, n + 1): if 1 << high & a[r] != 0: break print(a[r], end=" ") for i in range(1, n + 1): if i == r: continue else: print(a[i], end=" ")
ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation . For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $[a_1, a_2, \dots, a_n]$ is defined as $f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$). Elements of the array are not guaranteed to be different. -----Output----- Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. -----Examples----- Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 -----Note----- In the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$. $[11, 4, 0, 6]$ is also a valid answer.
n = int(input()) arr = [int(p) for p in input().split()] pre = [~arr[0]] suff = [~arr[-1]] for i in range(1, n): pre.append(pre[-1] & ~arr[i]) for i in range(n - 2, -1, -1): suff.append(suff[-1] & ~arr[i]) curr = 0 if n == 1: print(*arr) exit(0) suff = list(reversed(suff)) l = arr[0] & suff[1] r = arr[-1] & pre[n - 2] curr = max(l, r) if curr == l: piv = arr[0] else: piv = arr[-1] for i in range(1, n - 1): prev = curr curr = max(curr, pre[i - 1] & arr[i] & suff[i + 1]) if prev != curr: piv = arr[i] ans = [piv] arr.remove(piv) arr.insert(0, piv) print(*arr)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Polycarp is a head of a circus troupe. There are n β€” an even number β€” artists in the troupe. It is known whether the i-th artist can perform as a clown (if yes, then c_i = 1, otherwise c_i = 0), and whether they can perform as an acrobat (if yes, then a_i = 1, otherwise a_i = 0). Split the artists into two performances in such a way that: * each artist plays in exactly one performance, * the number of artists in the two performances is equal (i.e. equal to n/2), * the number of artists that can perform as clowns in the first performance is the same as the number of artists that can perform as acrobats in the second performance. Input The first line contains a single integer n (2 ≀ n ≀ 5 000, n is even) β€” the number of artists in the troupe. The second line contains n digits c_1 c_2 … c_n, the i-th of which is equal to 1 if the i-th artist can perform as a clown, and 0 otherwise. The third line contains n digits a_1 a_2 … a_n, the i-th of which is equal to 1, if the i-th artist can perform as an acrobat, and 0 otherwise. Output Print n/2 distinct integers β€” the indices of the artists that should play in the first performance. If there are multiple answers, print any. If there is no solution, print a single integer -1. Examples Input 4 0011 0101 Output 1 4 Input 6 000000 111111 Output -1 Input 4 0011 1100 Output 4 3 Input 8 00100101 01111100 Output 1 2 3 6 Note In the first example, one of the possible divisions into two performances is as follows: in the first performance artists 1 and 4 should take part. Then the number of artists in the first performance who can perform as clowns is equal to 1. And the number of artists in the second performance who can perform as acrobats is 1 as well. In the second example, the division is not possible. In the third example, one of the possible divisions is as follows: in the first performance artists 3 and 4 should take part. Then in the first performance there are 2 artists who can perform as clowns. And the number of artists in the second performance who can perform as acrobats is 2 as well.
n = int(input()) cc = input() aa = input() na = 0 nb = 0 nc = 0 nd = 0 for i in range(len(aa)): if cc[i] == "0" and aa[i] == "0": na += 1 if cc[i] == "0" and aa[i] == "1": nb += 1 if cc[i] == "1" and aa[i] == "0": nc += 1 if cc[i] == "1" and aa[i] == "1": nd += 1 ans = [] f = False for c in range(nc + 1): for d in range(nd + 1): xx = n // 2 - (c + d) b = nb + nd - (2 * d + c) a = xx - b if a >= 0 and b >= 0 and a <= na and b <= nb: ans.append([a, b, c, d]) f = True break if f: break if len(ans) == 0: print(-1) exit() for i in range(len(ans)): a = ans[i][0] b = ans[i][1] c = ans[i][2] d = ans[i][3] temp = [] for j in range(len(aa)): if a > 0: if cc[j] == "0" and aa[j] == "0": a -= 1 temp.append(j) if b > 0: if cc[j] == "0" and aa[j] == "1": b -= 1 temp.append(j) if c > 0: if cc[j] == "1" and aa[j] == "0": c -= 1 temp.append(j) if d > 0: if cc[j] == "1" and aa[j] == "1": d -= 1 temp.append(j) if a == 0 and b == 0 and c == 0 and d == 0: for ii in temp: print(ii + 1, end=" ") print() exit()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Polycarp is a head of a circus troupe. There are n β€” an even number β€” artists in the troupe. It is known whether the i-th artist can perform as a clown (if yes, then c_i = 1, otherwise c_i = 0), and whether they can perform as an acrobat (if yes, then a_i = 1, otherwise a_i = 0). Split the artists into two performances in such a way that: * each artist plays in exactly one performance, * the number of artists in the two performances is equal (i.e. equal to n/2), * the number of artists that can perform as clowns in the first performance is the same as the number of artists that can perform as acrobats in the second performance. Input The first line contains a single integer n (2 ≀ n ≀ 5 000, n is even) β€” the number of artists in the troupe. The second line contains n digits c_1 c_2 … c_n, the i-th of which is equal to 1 if the i-th artist can perform as a clown, and 0 otherwise. The third line contains n digits a_1 a_2 … a_n, the i-th of which is equal to 1, if the i-th artist can perform as an acrobat, and 0 otherwise. Output Print n/2 distinct integers β€” the indices of the artists that should play in the first performance. If there are multiple answers, print any. If there is no solution, print a single integer -1. Examples Input 4 0011 0101 Output 1 4 Input 6 000000 111111 Output -1 Input 4 0011 1100 Output 4 3 Input 8 00100101 01111100 Output 1 2 3 6 Note In the first example, one of the possible divisions into two performances is as follows: in the first performance artists 1 and 4 should take part. Then the number of artists in the first performance who can perform as clowns is equal to 1. And the number of artists in the second performance who can perform as acrobats is 1 as well. In the second example, the division is not possible. In the third example, one of the possible divisions is as follows: in the first performance artists 3 and 4 should take part. Then in the first performance there are 2 artists who can perform as clowns. And the number of artists in the second performance who can perform as acrobats is 2 as well.
def func(clown, acro, x): if clown[x] == "1": if acro[x] == "1": return 3 else: return 1 elif acro[x] == "1": return 2 else: return 0 def main(): length = int(input()) clown = input() acro = input() arr = [0, 0, 0, 0] for x in range(length): arr[func(clown, acro, x)] += 1 bound = arr[2] + arr[3] - length // 2 bo = False for a in range(arr[0] + 1): d = a + bound if d >= 0 and d <= arr[3]: for b in range(arr[1] + 1): c = length // 2 - a - b - d if c >= 0 and c <= arr[2]: bo = True break if bo: break if bo: arr[0] = a arr[1] = b arr[2] = c arr[3] = d for x in range(length): test = func(clown, acro, x) if arr[test] > 0: arr[test] -= 1 print(x + 1, end=" ") else: print(-1) main()
FUNC_DEF IF VAR VAR STRING IF VAR VAR STRING RETURN NUMBER RETURN NUMBER IF VAR VAR STRING RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR IF VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
Polycarp is a head of a circus troupe. There are n β€” an even number β€” artists in the troupe. It is known whether the i-th artist can perform as a clown (if yes, then c_i = 1, otherwise c_i = 0), and whether they can perform as an acrobat (if yes, then a_i = 1, otherwise a_i = 0). Split the artists into two performances in such a way that: * each artist plays in exactly one performance, * the number of artists in the two performances is equal (i.e. equal to n/2), * the number of artists that can perform as clowns in the first performance is the same as the number of artists that can perform as acrobats in the second performance. Input The first line contains a single integer n (2 ≀ n ≀ 5 000, n is even) β€” the number of artists in the troupe. The second line contains n digits c_1 c_2 … c_n, the i-th of which is equal to 1 if the i-th artist can perform as a clown, and 0 otherwise. The third line contains n digits a_1 a_2 … a_n, the i-th of which is equal to 1, if the i-th artist can perform as an acrobat, and 0 otherwise. Output Print n/2 distinct integers β€” the indices of the artists that should play in the first performance. If there are multiple answers, print any. If there is no solution, print a single integer -1. Examples Input 4 0011 0101 Output 1 4 Input 6 000000 111111 Output -1 Input 4 0011 1100 Output 4 3 Input 8 00100101 01111100 Output 1 2 3 6 Note In the first example, one of the possible divisions into two performances is as follows: in the first performance artists 1 and 4 should take part. Then the number of artists in the first performance who can perform as clowns is equal to 1. And the number of artists in the second performance who can perform as acrobats is 1 as well. In the second example, the division is not possible. In the third example, one of the possible divisions is as follows: in the first performance artists 3 and 4 should take part. Then in the first performance there are 2 artists who can perform as clowns. And the number of artists in the second performance who can perform as acrobats is 2 as well.
def find_sol1(result, num_dict): ans_list = [] for i in range(num_dict["a"] + 1): for j in range(num_dict["d"] + 1): if i - j == result: ans_list.append((i, j)) return ans_list def find_sol2(N, num_dict, ans_list): for a, d in ans_list: for i in range(num_dict["b"] + 1): for j in range(num_dict["c"] + 1): if i + j + a + d == N // 2: b = i c = j return a, b, c, d return -1, -1, -1, -1 N = int(input().strip()) clown = [int(i) for i in input().strip()] acro = [int(i) for i in input().strip()] num_dict = {"a": 0, "b": 0, "c": 0, "d": 0} for i in range(N): if clown[i] == 0 and acro[i] == 0: num_dict["a"] += 1 elif clown[i] == 1 and acro[i] == 0: num_dict["b"] += 1 elif clown[i] == 0 and acro[i] == 1: num_dict["c"] += 1 else: num_dict["d"] += 1 a, b, c, d = 0, 0, 0, 0 result = N // 2 - num_dict["c"] - num_dict["d"] ans_list = find_sol1(result, num_dict) if ans_list == []: print(-1) else: a, b, c, d = find_sol2(N, num_dict, ans_list) if a == -1: print(-1) else: ans = [] for i in range(N): if clown[i] == 0 and acro[i] == 0: if a > 0: ans.append(i + 1) a -= 1 elif clown[i] == 1 and acro[i] == 0: if b > 0: ans.append(i + 1) b -= 1 elif clown[i] == 0 and acro[i] == 1: if c > 0: ans.append(i + 1) c -= 1 elif d > 0: ans.append(i + 1) d -= 1 print(" ".join(str(i) for i in ans))
FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR STRING NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR STRING NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF FOR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR STRING NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR STRING NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR VAR VAR VAR RETURN NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR STRING NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR STRING NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR STRING NUMBER VAR STRING NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR STRING VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR LIST EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
Polycarp is a head of a circus troupe. There are n β€” an even number β€” artists in the troupe. It is known whether the i-th artist can perform as a clown (if yes, then c_i = 1, otherwise c_i = 0), and whether they can perform as an acrobat (if yes, then a_i = 1, otherwise a_i = 0). Split the artists into two performances in such a way that: * each artist plays in exactly one performance, * the number of artists in the two performances is equal (i.e. equal to n/2), * the number of artists that can perform as clowns in the first performance is the same as the number of artists that can perform as acrobats in the second performance. Input The first line contains a single integer n (2 ≀ n ≀ 5 000, n is even) β€” the number of artists in the troupe. The second line contains n digits c_1 c_2 … c_n, the i-th of which is equal to 1 if the i-th artist can perform as a clown, and 0 otherwise. The third line contains n digits a_1 a_2 … a_n, the i-th of which is equal to 1, if the i-th artist can perform as an acrobat, and 0 otherwise. Output Print n/2 distinct integers β€” the indices of the artists that should play in the first performance. If there are multiple answers, print any. If there is no solution, print a single integer -1. Examples Input 4 0011 0101 Output 1 4 Input 6 000000 111111 Output -1 Input 4 0011 1100 Output 4 3 Input 8 00100101 01111100 Output 1 2 3 6 Note In the first example, one of the possible divisions into two performances is as follows: in the first performance artists 1 and 4 should take part. Then the number of artists in the first performance who can perform as clowns is equal to 1. And the number of artists in the second performance who can perform as acrobats is 1 as well. In the second example, the division is not possible. In the third example, one of the possible divisions is as follows: in the first performance artists 3 and 4 should take part. Then in the first performance there are 2 artists who can perform as clowns. And the number of artists in the second performance who can perform as acrobats is 2 as well.
n = int(input()) c = list(map(int, input())) a = list(map(int, input())) b = [0, 0, 0, 0] for x, y in zip(c, a): if x ^ y: b[0] += x b[1] += y elif x: b[2] += 1 else: b[3] += 1 l = [0, 0, 0, 0] r = [0, 0, 0, 0] z = min(b[0], b[1]) l[0] += z r[1] += z b[0] -= z b[1] -= z if b[0]: z = min(b[0], b[2]) l[0] += z r[2] += z b[0] -= z b[2] -= z if b[0]: r[0] += b[0] b[0] = 0 if b[2]: l[2] += b[2] // 2 r[2] += b[2] // 2 b[2] &= 1 if b[2]: l[0] -= 1 l[2] += 1 r[0] += 1 b[2] = 0 if b[1]: z = min(b[1], b[2]) l[2] += z r[1] += z b[1] -= z b[2] -= z if b[1]: l[1] += b[1] b[1] = 0 if b[2]: l[2] += b[2] // 2 r[2] += b[2] // 2 b[2] &= 1 if b[2]: r[1] -= 1 r[2] += 1 l[1] += 1 b[2] = 0 if b[2]: l[2] += b[2] // 2 r[2] += b[2] // 2 b[2] &= 1 if b[2]: if l[0]: l[0] -= 1 l[2] += 1 r[0] += 1 b[2] = 0 elif r[1]: r[1] -= 1 r[2] += 1 l[1] += 1 b[2] = 0 else: r[0] = n if sum(l) > n // 2 or sum(r) > n // 2: print(-1) else: l[3] = n // 2 - sum(l) if l[3] > b[3]: print(-1) else: d = [] for i in range(n): if c[i] ^ a[i]: if c[i]: if l[0]: d += (i + 1,) l[0] -= 1 elif l[1]: d += (i + 1,) l[1] -= 1 elif c[i]: if l[2]: d += (i + 1,) l[2] -= 1 elif l[3]: d += (i + 1,) l[3] -= 1 print(*d)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER VAR IF VAR VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR IF VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER IF VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Polycarp is a head of a circus troupe. There are n β€” an even number β€” artists in the troupe. It is known whether the i-th artist can perform as a clown (if yes, then c_i = 1, otherwise c_i = 0), and whether they can perform as an acrobat (if yes, then a_i = 1, otherwise a_i = 0). Split the artists into two performances in such a way that: * each artist plays in exactly one performance, * the number of artists in the two performances is equal (i.e. equal to n/2), * the number of artists that can perform as clowns in the first performance is the same as the number of artists that can perform as acrobats in the second performance. Input The first line contains a single integer n (2 ≀ n ≀ 5 000, n is even) β€” the number of artists in the troupe. The second line contains n digits c_1 c_2 … c_n, the i-th of which is equal to 1 if the i-th artist can perform as a clown, and 0 otherwise. The third line contains n digits a_1 a_2 … a_n, the i-th of which is equal to 1, if the i-th artist can perform as an acrobat, and 0 otherwise. Output Print n/2 distinct integers β€” the indices of the artists that should play in the first performance. If there are multiple answers, print any. If there is no solution, print a single integer -1. Examples Input 4 0011 0101 Output 1 4 Input 6 000000 111111 Output -1 Input 4 0011 1100 Output 4 3 Input 8 00100101 01111100 Output 1 2 3 6 Note In the first example, one of the possible divisions into two performances is as follows: in the first performance artists 1 and 4 should take part. Then the number of artists in the first performance who can perform as clowns is equal to 1. And the number of artists in the second performance who can perform as acrobats is 1 as well. In the second example, the division is not possible. In the third example, one of the possible divisions is as follows: in the first performance artists 3 and 4 should take part. Then in the first performance there are 2 artists who can perform as clowns. And the number of artists in the second performance who can perform as acrobats is 2 as well.
import sys def rint(): return map(int, sys.stdin.readline().split()) n = int(input()) c = input() a = input() v = [] for i in range(n): v.append(int(a[i]) + int(c[i]) * 2) cnt = [(0) for i in range(4)] c1 = [(0) for i in range(4)] for i in range(n): cnt[v[i]] += 1 t = cnt[1] + cnt[3] - n // 2 found = 0 for a0 in range(0, cnt[0] + 1): a3 = t + a0 if a3 < 0 or a3 > cnt[3]: continue for a2 in range(0, cnt[2] + 1): a1 = cnt[3] + cnt[1] - 2 * a3 - a2 if a1 < 0 or a1 > cnt[1]: continue else: c1[0] = a0 c1[1] = a1 c1[2] = a2 c1[3] = a3 found = 1 break if found == 1: break if found == 0: print(-1) exit() ans = [] for j in range(4): i = 0 while c1[j]: if v[i] == j: ans.append(i + 1) c1[j] -= 1 i += 1 print(*ans)
IMPORT FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP NUMBER VAR VAR IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Polycarp is a head of a circus troupe. There are n β€” an even number β€” artists in the troupe. It is known whether the i-th artist can perform as a clown (if yes, then c_i = 1, otherwise c_i = 0), and whether they can perform as an acrobat (if yes, then a_i = 1, otherwise a_i = 0). Split the artists into two performances in such a way that: * each artist plays in exactly one performance, * the number of artists in the two performances is equal (i.e. equal to n/2), * the number of artists that can perform as clowns in the first performance is the same as the number of artists that can perform as acrobats in the second performance. Input The first line contains a single integer n (2 ≀ n ≀ 5 000, n is even) β€” the number of artists in the troupe. The second line contains n digits c_1 c_2 … c_n, the i-th of which is equal to 1 if the i-th artist can perform as a clown, and 0 otherwise. The third line contains n digits a_1 a_2 … a_n, the i-th of which is equal to 1, if the i-th artist can perform as an acrobat, and 0 otherwise. Output Print n/2 distinct integers β€” the indices of the artists that should play in the first performance. If there are multiple answers, print any. If there is no solution, print a single integer -1. Examples Input 4 0011 0101 Output 1 4 Input 6 000000 111111 Output -1 Input 4 0011 1100 Output 4 3 Input 8 00100101 01111100 Output 1 2 3 6 Note In the first example, one of the possible divisions into two performances is as follows: in the first performance artists 1 and 4 should take part. Then the number of artists in the first performance who can perform as clowns is equal to 1. And the number of artists in the second performance who can perform as acrobats is 1 as well. In the second example, the division is not possible. In the third example, one of the possible divisions is as follows: in the first performance artists 3 and 4 should take part. Then in the first performance there are 2 artists who can perform as clowns. And the number of artists in the second performance who can perform as acrobats is 2 as well.
n = int(input()) c = input() a = input() a1 = 0 c1 = 0 ac = 0 ac0 = 0 lista = [] listc = [] listac = [] listac0 = [] output = [] p = n // 2 for i in range(0, n): if a[i] == "1" and c[i] == "0": a1 += 1 lista.append(i + 1) if c[i] == "1" and a[i] == "0": c1 += 1 listc.append(i + 1) if c[i] == "1" and a[i] == "1": ac += 1 listac.append(i + 1) if c[i] == "0" and a[i] == "0": ac0 += 1 listac0.append(i + 1) if a1 > p or c1 > p or ac % 2 == 1 and a1 == 0 and c1 == 0: print(-1) exit() d = abs(a1 - c1) if a1 >= c1: if a1 >= c1 + ac: for i in listc: output.append(i) for i in listac: output.append(i) for i in range(0, a1 - (c1 + ac)): output.append(lista[i]) elif (c1 + ac - a1) % 2 == 0: for i in listc: output.append(i) for i in range(0, a1 - c1): output.append(listac[0]) listac.pop(0) for i in range(0, len(listac) // 2): output.append(listac[i]) else: if c1 != 0: listc.pop(0) for i in listc: output.append(i) for i in range(0, a1 - c1 + 1): output.append(listac[0]) listac.pop(0) for i in range(0, len(listac) // 2): output.append(listac[i]) elif c1 <= a1 + ac: if (a1 + ac - c1) % 2 == 0: for i in listc: output.append(i) for i in range(0, (a1 + ac - c1) // 2): output.append(listac[i]) else: output.append(lista[0]) for i in listc: output.append(i) for i in range(0, (a1 + ac - 1 - c1) // 2): output.append(listac[i]) else: for i in range(0, a1 + ac): output.append(listc[i]) l = len(output) for i in range(0, p - l): output.append(listac0[i]) if len(output) != p: print(-1) exit() for i in output: print(i, end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR IF VAR BIN_OP VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Polycarp is a head of a circus troupe. There are n β€” an even number β€” artists in the troupe. It is known whether the i-th artist can perform as a clown (if yes, then c_i = 1, otherwise c_i = 0), and whether they can perform as an acrobat (if yes, then a_i = 1, otherwise a_i = 0). Split the artists into two performances in such a way that: * each artist plays in exactly one performance, * the number of artists in the two performances is equal (i.e. equal to n/2), * the number of artists that can perform as clowns in the first performance is the same as the number of artists that can perform as acrobats in the second performance. Input The first line contains a single integer n (2 ≀ n ≀ 5 000, n is even) β€” the number of artists in the troupe. The second line contains n digits c_1 c_2 … c_n, the i-th of which is equal to 1 if the i-th artist can perform as a clown, and 0 otherwise. The third line contains n digits a_1 a_2 … a_n, the i-th of which is equal to 1, if the i-th artist can perform as an acrobat, and 0 otherwise. Output Print n/2 distinct integers β€” the indices of the artists that should play in the first performance. If there are multiple answers, print any. If there is no solution, print a single integer -1. Examples Input 4 0011 0101 Output 1 4 Input 6 000000 111111 Output -1 Input 4 0011 1100 Output 4 3 Input 8 00100101 01111100 Output 1 2 3 6 Note In the first example, one of the possible divisions into two performances is as follows: in the first performance artists 1 and 4 should take part. Then the number of artists in the first performance who can perform as clowns is equal to 1. And the number of artists in the second performance who can perform as acrobats is 1 as well. In the second example, the division is not possible. In the third example, one of the possible divisions is as follows: in the first performance artists 3 and 4 should take part. Then in the first performance there are 2 artists who can perform as clowns. And the number of artists in the second performance who can perform as acrobats is 2 as well.
import sys n = int(sys.stdin.readline().strip()) c = sys.stdin.readline().strip() a = sys.stdin.readline().strip() onlyc = 0 onlya = 0 both = 0 neither = 0 for i in range(0, n): if c[i] == "1": if a[i] == "1": both = both + 1 else: onlyc = onlyc + 1 elif a[i] == "1": onlya = onlya + 1 else: neither = neither + 1 onlyc0 = 0 onlya0 = 0 both0 = 0 neither0 = 0 onlyc1 = 0 onlya1 = 0 both1 = 0 neither1 = 0 if both % 2 == 1: if onlya > onlyc: both = both - 1 onlya = onlya - 1 onlya1 = onlya1 + 1 both0 = both0 + 1 elif onlyc > 0: both = both - 1 onlyc = onlyc - 1 onlyc0 = onlyc0 + 1 both1 = both1 + 1 if neither % 2 == 1: if onlya > onlyc: neither = neither - 1 onlya = onlya - 1 onlya0 = onlya0 + 1 neither1 = neither1 + 1 elif onlyc > 0: neither = neither - 1 onlyc = onlyc - 1 onlyc1 = onlyc1 + 1 neither0 = neither0 + 1 if both % 2 == 1 or neither % 2 == 1: print(-1) else: m = min([onlya, onlyc]) onlya = onlya - m onlyc = onlyc - m onlya1 = onlya1 + m onlyc0 = onlyc0 + m m = min([onlya, neither]) onlya = onlya - m neither = neither - m onlya0 = onlya0 + m neither1 = neither1 + m m = min([onlya, both]) onlya = onlya - m both = both - m onlya1 = onlya1 + m both0 = both0 + m m = min([onlyc, neither]) onlyc = onlyc - m neither = neither - m onlyc1 = onlyc1 + m neither0 = neither0 + m m = min([onlyc, both]) onlyc = onlyc - m both = both - m onlyc0 = onlyc0 + m both1 = both1 + m both1 = both1 + both // 2 both0 = both0 + both // 2 both = both - 2 * (both // 2) neither1 = neither1 + neither // 2 neither0 = neither0 + neither // 2 neither = neither - 2 * (neither // 2) if onlya + onlyc + both + neither > 0: print(-1) else: ans = "" for i in range(0, n): if c[i] == "1" and a[i] == "1": if both1 > 0: both1 = both1 - 1 else: ans = ans + str(i + 1) + " " if c[i] == "1" and a[i] == "0": if onlyc1 > 0: onlyc1 = onlyc1 - 1 else: ans = ans + str(i + 1) + " " if c[i] == "0" and a[i] == "1": if onlya1 > 0: onlya1 = onlya1 - 1 else: ans = ans + str(i + 1) + " " if c[i] == "0" and a[i] == "0": if neither1 > 0: neither1 = neither1 - 1 else: ans = ans + str(i + 1) + " " print(ans)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF 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 IF 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 IF BIN_OP VAR NUMBER NUMBER IF 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 IF 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 IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING VAR VAR STRING IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER STRING IF VAR VAR STRING VAR VAR STRING IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER STRING IF VAR VAR STRING VAR VAR STRING IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER STRING IF VAR VAR STRING VAR VAR STRING IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR VAR
Polycarp is a head of a circus troupe. There are n β€” an even number β€” artists in the troupe. It is known whether the i-th artist can perform as a clown (if yes, then c_i = 1, otherwise c_i = 0), and whether they can perform as an acrobat (if yes, then a_i = 1, otherwise a_i = 0). Split the artists into two performances in such a way that: * each artist plays in exactly one performance, * the number of artists in the two performances is equal (i.e. equal to n/2), * the number of artists that can perform as clowns in the first performance is the same as the number of artists that can perform as acrobats in the second performance. Input The first line contains a single integer n (2 ≀ n ≀ 5 000, n is even) β€” the number of artists in the troupe. The second line contains n digits c_1 c_2 … c_n, the i-th of which is equal to 1 if the i-th artist can perform as a clown, and 0 otherwise. The third line contains n digits a_1 a_2 … a_n, the i-th of which is equal to 1, if the i-th artist can perform as an acrobat, and 0 otherwise. Output Print n/2 distinct integers β€” the indices of the artists that should play in the first performance. If there are multiple answers, print any. If there is no solution, print a single integer -1. Examples Input 4 0011 0101 Output 1 4 Input 6 000000 111111 Output -1 Input 4 0011 1100 Output 4 3 Input 8 00100101 01111100 Output 1 2 3 6 Note In the first example, one of the possible divisions into two performances is as follows: in the first performance artists 1 and 4 should take part. Then the number of artists in the first performance who can perform as clowns is equal to 1. And the number of artists in the second performance who can perform as acrobats is 1 as well. In the second example, the division is not possible. In the third example, one of the possible divisions is as follows: in the first performance artists 3 and 4 should take part. Then in the first performance there are 2 artists who can perform as clowns. And the number of artists in the second performance who can perform as acrobats is 2 as well.
from sys import stdin, stdout N = int(input()) clown = input() acro = input() clown = list(clown) acro = list(acro) k1 = 0 k2 = 0 k3 = 0 k4 = 0 for i in range(N): if clown[i] == "0" and acro[i] == "0": k1 += 1 if clown[i] == "0" and acro[i] == "1": k2 += 1 if clown[i] == "1" and acro[i] == "0": k3 += 1 if clown[i] == "1" and acro[i] == "1": k4 += 1 a = 0 b = 0 c = 0 d = 0 target = k1 + k3 - k2 - k4 if target % 2 == 1: print(-1) quit() if target < 0: a = 0 d = -target // 2 else: a = target // 2 d = 0 valid = 0 for i in range(10000): if a <= k1 and d <= k4: b = N // 2 - a - d c = 0 while b >= 0 and c >= 0: if b <= k2 and c <= k3: for j in range(N): if clown[j] == "0" and acro[j] == "0" and a > 0: print(j + 1, end=" ") a -= 1 if clown[j] == "0" and acro[j] == "1" and b > 0: print(j + 1, end=" ") b -= 1 if clown[j] == "1" and acro[j] == "0" and c > 0: print(j + 1, end=" ") c -= 1 if clown[j] == "1" and acro[j] == "1" and d > 0: print(j + 1, end=" ") d -= 1 quit() b -= 1 c += 1 else: break a += 1 d += 1 print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER
Polycarp is a head of a circus troupe. There are n β€” an even number β€” artists in the troupe. It is known whether the i-th artist can perform as a clown (if yes, then c_i = 1, otherwise c_i = 0), and whether they can perform as an acrobat (if yes, then a_i = 1, otherwise a_i = 0). Split the artists into two performances in such a way that: * each artist plays in exactly one performance, * the number of artists in the two performances is equal (i.e. equal to n/2), * the number of artists that can perform as clowns in the first performance is the same as the number of artists that can perform as acrobats in the second performance. Input The first line contains a single integer n (2 ≀ n ≀ 5 000, n is even) β€” the number of artists in the troupe. The second line contains n digits c_1 c_2 … c_n, the i-th of which is equal to 1 if the i-th artist can perform as a clown, and 0 otherwise. The third line contains n digits a_1 a_2 … a_n, the i-th of which is equal to 1, if the i-th artist can perform as an acrobat, and 0 otherwise. Output Print n/2 distinct integers β€” the indices of the artists that should play in the first performance. If there are multiple answers, print any. If there is no solution, print a single integer -1. Examples Input 4 0011 0101 Output 1 4 Input 6 000000 111111 Output -1 Input 4 0011 1100 Output 4 3 Input 8 00100101 01111100 Output 1 2 3 6 Note In the first example, one of the possible divisions into two performances is as follows: in the first performance artists 1 and 4 should take part. Then the number of artists in the first performance who can perform as clowns is equal to 1. And the number of artists in the second performance who can perform as acrobats is 1 as well. In the second example, the division is not possible. In the third example, one of the possible divisions is as follows: in the first performance artists 3 and 4 should take part. Then in the first performance there are 2 artists who can perform as clowns. And the number of artists in the second performance who can perform as acrobats is 2 as well.
n = int(input()) aa = input() bb = input() s = [0] * n t = [0] * n ans = [0] * n for i in range(n): s[i] = aa[i] t[i] = bb[i] ans[i] = i + 1 k = n // 2 cs, at = 0, 0 for i in range(n // 2): if s[i] == "1": cs += 1 if t[k + i] == "1": at += 1 if cs == at: for i in range(n // 2): print(i + 1, end=" ") print() exit() else: for i in range(k): for j in range(k, n, 1): ncs = cs nat = at if s[i] == "1": ncs -= 1 if t[j] == "1": nat -= 1 if s[j] == "1": ncs += 1 if t[i] == "1": nat += 1 if abs(ncs - nat) < abs(cs - at): s[i], s[j] = s[j], s[i] t[i], t[j] = t[j], t[i] ans[i], ans[j] = ans[j], ans[i] cs = ncs at = nat if cs == at: break if cs == at: print(*ans[: n // 2]) else: print("-1")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER 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 ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR VAR STRING VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING
Polycarp is a head of a circus troupe. There are n β€” an even number β€” artists in the troupe. It is known whether the i-th artist can perform as a clown (if yes, then c_i = 1, otherwise c_i = 0), and whether they can perform as an acrobat (if yes, then a_i = 1, otherwise a_i = 0). Split the artists into two performances in such a way that: * each artist plays in exactly one performance, * the number of artists in the two performances is equal (i.e. equal to n/2), * the number of artists that can perform as clowns in the first performance is the same as the number of artists that can perform as acrobats in the second performance. Input The first line contains a single integer n (2 ≀ n ≀ 5 000, n is even) β€” the number of artists in the troupe. The second line contains n digits c_1 c_2 … c_n, the i-th of which is equal to 1 if the i-th artist can perform as a clown, and 0 otherwise. The third line contains n digits a_1 a_2 … a_n, the i-th of which is equal to 1, if the i-th artist can perform as an acrobat, and 0 otherwise. Output Print n/2 distinct integers β€” the indices of the artists that should play in the first performance. If there are multiple answers, print any. If there is no solution, print a single integer -1. Examples Input 4 0011 0101 Output 1 4 Input 6 000000 111111 Output -1 Input 4 0011 1100 Output 4 3 Input 8 00100101 01111100 Output 1 2 3 6 Note In the first example, one of the possible divisions into two performances is as follows: in the first performance artists 1 and 4 should take part. Then the number of artists in the first performance who can perform as clowns is equal to 1. And the number of artists in the second performance who can perform as acrobats is 1 as well. In the second example, the division is not possible. In the third example, one of the possible divisions is as follows: in the first performance artists 3 and 4 should take part. Then in the first performance there are 2 artists who can perform as clowns. And the number of artists in the second performance who can perform as acrobats is 2 as well.
import sys N = int(input()) n = N // 2 A = input() B = input() C = [((a == "1") * 2 + (b == "1")) for a, b in zip(A, B)] w = C.count(0) x = C.count(1) y = C.count(2) z = N - w - x - y Ans = [] for i in range(x + 1): for j in range(y + 1): z1 = x + z - i - j if z1 % 2: continue z1 //= 2 if not 0 <= z1 <= z: continue w1 = n - i - j - z1 if not 0 <= w1 <= w: continue cnt = [w1, i, j, z1] ans = [] for k, a, b in zip(range(1, N + 1), A, B): if cnt[(a == "1") * 2 + (b == "1")] > 0: cnt[(a == "1") * 2 + (b == "1")] -= 1 Ans.append(k) print(*Ans) sys.exit() print(-1)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR STRING NUMBER VAR STRING VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER IF NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR IF NUMBER VAR VAR ASSIGN VAR LIST VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP BIN_OP VAR STRING NUMBER VAR STRING NUMBER VAR BIN_OP BIN_OP VAR STRING NUMBER VAR STRING NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER
Polycarp is a head of a circus troupe. There are n β€” an even number β€” artists in the troupe. It is known whether the i-th artist can perform as a clown (if yes, then c_i = 1, otherwise c_i = 0), and whether they can perform as an acrobat (if yes, then a_i = 1, otherwise a_i = 0). Split the artists into two performances in such a way that: * each artist plays in exactly one performance, * the number of artists in the two performances is equal (i.e. equal to n/2), * the number of artists that can perform as clowns in the first performance is the same as the number of artists that can perform as acrobats in the second performance. Input The first line contains a single integer n (2 ≀ n ≀ 5 000, n is even) β€” the number of artists in the troupe. The second line contains n digits c_1 c_2 … c_n, the i-th of which is equal to 1 if the i-th artist can perform as a clown, and 0 otherwise. The third line contains n digits a_1 a_2 … a_n, the i-th of which is equal to 1, if the i-th artist can perform as an acrobat, and 0 otherwise. Output Print n/2 distinct integers β€” the indices of the artists that should play in the first performance. If there are multiple answers, print any. If there is no solution, print a single integer -1. Examples Input 4 0011 0101 Output 1 4 Input 6 000000 111111 Output -1 Input 4 0011 1100 Output 4 3 Input 8 00100101 01111100 Output 1 2 3 6 Note In the first example, one of the possible divisions into two performances is as follows: in the first performance artists 1 and 4 should take part. Then the number of artists in the first performance who can perform as clowns is equal to 1. And the number of artists in the second performance who can perform as acrobats is 1 as well. In the second example, the division is not possible. In the third example, one of the possible divisions is as follows: in the first performance artists 3 and 4 should take part. Then in the first performance there are 2 artists who can perform as clowns. And the number of artists in the second performance who can perform as acrobats is 2 as well.
a, b, c, d = 0, 0, 0, 0 n, s, t = int(input()), input(), input() for i in range(n): if s[i] == "1" and t[i] == "1": d += 1 elif s[i] == t[i]: a += 1 elif s[i] == "1": c += 1 else: b += 1 if n % 2 == 1: print(-1) exit(0) hi = n // 2 for x in range(a + 1): for m in range(b + 1): y = b + d + x - hi n = hi - m - x - y if 0 <= y <= d and 0 <= n <= c: for i in range(hi * 2): if s[i] == "1" and t[i] == "1": if y > 0: print(i + 1, end=" ") y -= 1 elif s[i] == t[i]: if x > 0: print(i + 1, end=" ") x -= 1 elif s[i] == "1": if n > 0: print(i + 1, end=" ") n -= 1 elif m > 0: print(i + 1, end=" ") m -= 1 exit(0) print(-1)
ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR IF NUMBER VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
Polycarp is a head of a circus troupe. There are n β€” an even number β€” artists in the troupe. It is known whether the i-th artist can perform as a clown (if yes, then c_i = 1, otherwise c_i = 0), and whether they can perform as an acrobat (if yes, then a_i = 1, otherwise a_i = 0). Split the artists into two performances in such a way that: * each artist plays in exactly one performance, * the number of artists in the two performances is equal (i.e. equal to n/2), * the number of artists that can perform as clowns in the first performance is the same as the number of artists that can perform as acrobats in the second performance. Input The first line contains a single integer n (2 ≀ n ≀ 5 000, n is even) β€” the number of artists in the troupe. The second line contains n digits c_1 c_2 … c_n, the i-th of which is equal to 1 if the i-th artist can perform as a clown, and 0 otherwise. The third line contains n digits a_1 a_2 … a_n, the i-th of which is equal to 1, if the i-th artist can perform as an acrobat, and 0 otherwise. Output Print n/2 distinct integers β€” the indices of the artists that should play in the first performance. If there are multiple answers, print any. If there is no solution, print a single integer -1. Examples Input 4 0011 0101 Output 1 4 Input 6 000000 111111 Output -1 Input 4 0011 1100 Output 4 3 Input 8 00100101 01111100 Output 1 2 3 6 Note In the first example, one of the possible divisions into two performances is as follows: in the first performance artists 1 and 4 should take part. Then the number of artists in the first performance who can perform as clowns is equal to 1. And the number of artists in the second performance who can perform as acrobats is 1 as well. In the second example, the division is not possible. In the third example, one of the possible divisions is as follows: in the first performance artists 3 and 4 should take part. Then in the first performance there are 2 artists who can perform as clowns. And the number of artists in the second performance who can perform as acrobats is 2 as well.
n = int(input()) d = {v: [] for v in [(0, 0), (0, 1), (1, 0), (1, 1)]} for j, x, y in zip(range(n), map(int, input()), map(int, input())): d[x, y] += [j + 1] x = n // 2 y = len(d[0, 1]) + len(d[1, 1]) x += x - y c = min(x, y, len(d[0, 1]) + len(d[1, 0])) if c % 2 != y % 2: c -= 1 b = (y - c) // 2 a = (x - c) // 2 if a <= len(d[0, 0]) and 0 <= c and b <= len(d[1, 1]): print(*(d[0, 0][:a] + (d[0, 1] + d[1, 0])[:c] + d[1, 1][:b])) else: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR LIST VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR LIST BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR NUMBER
Polycarp is a head of a circus troupe. There are n β€” an even number β€” artists in the troupe. It is known whether the i-th artist can perform as a clown (if yes, then c_i = 1, otherwise c_i = 0), and whether they can perform as an acrobat (if yes, then a_i = 1, otherwise a_i = 0). Split the artists into two performances in such a way that: * each artist plays in exactly one performance, * the number of artists in the two performances is equal (i.e. equal to n/2), * the number of artists that can perform as clowns in the first performance is the same as the number of artists that can perform as acrobats in the second performance. Input The first line contains a single integer n (2 ≀ n ≀ 5 000, n is even) β€” the number of artists in the troupe. The second line contains n digits c_1 c_2 … c_n, the i-th of which is equal to 1 if the i-th artist can perform as a clown, and 0 otherwise. The third line contains n digits a_1 a_2 … a_n, the i-th of which is equal to 1, if the i-th artist can perform as an acrobat, and 0 otherwise. Output Print n/2 distinct integers β€” the indices of the artists that should play in the first performance. If there are multiple answers, print any. If there is no solution, print a single integer -1. Examples Input 4 0011 0101 Output 1 4 Input 6 000000 111111 Output -1 Input 4 0011 1100 Output 4 3 Input 8 00100101 01111100 Output 1 2 3 6 Note In the first example, one of the possible divisions into two performances is as follows: in the first performance artists 1 and 4 should take part. Then the number of artists in the first performance who can perform as clowns is equal to 1. And the number of artists in the second performance who can perform as acrobats is 1 as well. In the second example, the division is not possible. In the third example, one of the possible divisions is as follows: in the first performance artists 3 and 4 should take part. Then in the first performance there are 2 artists who can perform as clowns. And the number of artists in the second performance who can perform as acrobats is 2 as well.
n = int(input()) n2 = n // 2 c = input() a = input() cl = [] ac = [] uni = [] par = [] res = [] for i in range(0, n): if c[i] == "1": if a[i] == "0": cl.append(i + 1) else: uni.append(i + 1) elif a[i] == "0": par.append(i + 1) else: ac.append(i + 1) lcl = len(cl) lac = len(ac) lpar = len(par) luni = len(uni) if lcl > n2 or lac > n2 or lcl == 0 and lac == 0 and luni % 2 == 1: print(-1) else: if luni + lpar - abs(lac - lcl) < 0: print(-1) else: if luni - abs(lac - lcl) < 0: nmin = lpar if nmin > abs(lac - lcl): nmin = abs(lac - lcl) if lcl < lac: cl = cl + ac[lcl : lcl + nmin : 1] lcl = lcl + nmin lpar = lpar - nmin elif lcl > lac: cl = cl[0 : lcl - nmin : 1] + par[0:nmin:1] lac = lac + nmin par = par[nmin:] lpar = lpar - nmin x = 0 if lcl < lac: for i in range(0, lac - lcl): cl.append(uni[i]) x = lac - lcl if (luni - abs(lac - lcl)) % 2 == 1: if lac > 0: cl.append(ac[0]) else: cl = cl[1:] cl.append(uni[luni - 1]) cl.append(par[lpar - 1]) luni = luni - 1 lpar = lpar - 1 n4 = (luni - abs(lac - lcl)) // 2 for i in range(x, x + n4): cl.append(uni[i]) n5 = lpar // 2 for i in range(0, n5): cl.append(par[i]) print(*cl)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR IF VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Polycarp is a head of a circus troupe. There are n β€” an even number β€” artists in the troupe. It is known whether the i-th artist can perform as a clown (if yes, then c_i = 1, otherwise c_i = 0), and whether they can perform as an acrobat (if yes, then a_i = 1, otherwise a_i = 0). Split the artists into two performances in such a way that: * each artist plays in exactly one performance, * the number of artists in the two performances is equal (i.e. equal to n/2), * the number of artists that can perform as clowns in the first performance is the same as the number of artists that can perform as acrobats in the second performance. Input The first line contains a single integer n (2 ≀ n ≀ 5 000, n is even) β€” the number of artists in the troupe. The second line contains n digits c_1 c_2 … c_n, the i-th of which is equal to 1 if the i-th artist can perform as a clown, and 0 otherwise. The third line contains n digits a_1 a_2 … a_n, the i-th of which is equal to 1, if the i-th artist can perform as an acrobat, and 0 otherwise. Output Print n/2 distinct integers β€” the indices of the artists that should play in the first performance. If there are multiple answers, print any. If there is no solution, print a single integer -1. Examples Input 4 0011 0101 Output 1 4 Input 6 000000 111111 Output -1 Input 4 0011 1100 Output 4 3 Input 8 00100101 01111100 Output 1 2 3 6 Note In the first example, one of the possible divisions into two performances is as follows: in the first performance artists 1 and 4 should take part. Then the number of artists in the first performance who can perform as clowns is equal to 1. And the number of artists in the second performance who can perform as acrobats is 1 as well. In the second example, the division is not possible. In the third example, one of the possible divisions is as follows: in the first performance artists 3 and 4 should take part. Then in the first performance there are 2 artists who can perform as clowns. And the number of artists in the second performance who can perform as acrobats is 2 as well.
def half(a): sa = sum(a) sb = sa // 2 a13 = a[1] + a[3] for b1 in range(a[1] + 1): for b3 in range(a[3] + 1): b2 = a13 - b1 - 2 * b3 if not 0 <= b2 <= a[2]: continue b0 = sb - b1 - b2 - b3 if 0 <= b0 <= a[0]: return [b0, b1, b2, b3] return False n = int(input()) line = list(zip(input(), input())) a = [0] * 4 for i, j in line: a[int(i) * 2 + int(j)] += 1 b = half(a) if not b: print(-1) exit() i = 0 while sum(b) > 0: j = int(line[i][0]) * 2 + int(line[i][1]) if b[j] > 0: print(i + 1, end=" ") b[j] -= 1 i += 1
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR IF NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR IF NUMBER VAR VAR NUMBER RETURN LIST VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING VAR VAR NUMBER VAR NUMBER