description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.
You decided to rob a town's blacksmith and you take a follower with you. You can carry at most $p$ units and your follower — at most $f$ units.
In the blacksmith shop, you found $cnt_s$ swords and $cnt_w$ war axes. Each sword weights $s$ units and each war axe — $w$ units. You don't care what to take, since each of them will melt into one steel ingot.
What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
The first line of each test case contains two integers $p$ and $f$ ($1 \le p, f \le 10^9$) — yours and your follower's capacities.
The second line of each test case contains two integers $cnt_s$ and $cnt_w$ ($1 \le cnt_s, cnt_w \le 2 \cdot 10^5$) — the number of swords and war axes in the shop.
The third line of each test case contains two integers $s$ and $w$ ($1 \le s, w \le 10^9$) — the weights of each sword and each war axe.
It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.
-----Example-----
Input
3
33 27
6 10
5 6
100 200
10 10
5 5
1 19
1 3
19 5
Output
11
20
3
-----Note-----
In the first test case: you should take $3$ swords and $3$ war axes: $3 \cdot 5 + 3 \cdot 6 = 33 \le 33$ and your follower — $3$ swords and $2$ war axes: $3 \cdot 5 + 2 \cdot 6 = 27 \le 27$. $3 + 3 + 3 + 2 = 11$ weapons in total.
In the second test case, you can take all available weapons even without your follower's help, since $5 \cdot 10 + 5 \cdot 10 \le 100$.
In the third test case, you can't take anything, but your follower can take $3$ war axes: $3 \cdot 5 \le 19$. | def multiple_input():
return map(int, input().split())
def list_input():
return list(map(int, input().split()))
for _ in range(int(input())):
p, f = multiple_input()
cs, cw = multiple_input()
s, w = multiple_input()
if s > w:
s, w = w, s
cs, cw = cw, cs
s1, s2, w1, w2 = 0, 0, 0, 0
i = 0
end = min(cs, p // s)
m = 0
while i <= end:
s1 = i
w1 = min(cw, (p - s1 * s) // w)
s2 = min(cs - s1, f // s)
w2 = min(cw - w1, (f - s2 * s) // w)
m = max(m, s1 + s2 + w1 + w2)
i += 1
print(m) | FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.
You decided to rob a town's blacksmith and you take a follower with you. You can carry at most $p$ units and your follower — at most $f$ units.
In the blacksmith shop, you found $cnt_s$ swords and $cnt_w$ war axes. Each sword weights $s$ units and each war axe — $w$ units. You don't care what to take, since each of them will melt into one steel ingot.
What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
The first line of each test case contains two integers $p$ and $f$ ($1 \le p, f \le 10^9$) — yours and your follower's capacities.
The second line of each test case contains two integers $cnt_s$ and $cnt_w$ ($1 \le cnt_s, cnt_w \le 2 \cdot 10^5$) — the number of swords and war axes in the shop.
The third line of each test case contains two integers $s$ and $w$ ($1 \le s, w \le 10^9$) — the weights of each sword and each war axe.
It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.
-----Example-----
Input
3
33 27
6 10
5 6
100 200
10 10
5 5
1 19
1 3
19 5
Output
11
20
3
-----Note-----
In the first test case: you should take $3$ swords and $3$ war axes: $3 \cdot 5 + 3 \cdot 6 = 33 \le 33$ and your follower — $3$ swords and $2$ war axes: $3 \cdot 5 + 2 \cdot 6 = 27 \le 27$. $3 + 3 + 3 + 2 = 11$ weapons in total.
In the second test case, you can take all available weapons even without your follower's help, since $5 \cdot 10 + 5 \cdot 10 \le 100$.
In the third test case, you can't take anything, but your follower can take $3$ war axes: $3 \cdot 5 \le 19$. | for _ in range(int(input())):
p, f = list(map(int, input().split()))
cs, cw = list(map(int, input().split()))
s, w = list(map(int, input().split()))
if w < s:
w, s = s, w
cs, cw = cw, cs
ps = p // s
fs = f // s
if ps + fs < cs:
print(ps + fs)
continue
best = 0
for i in range(cs + 1):
if p < i * s or f < (cs - i) * s:
continue
best = max(best, min(cw, (p - i * s) // w + (f - (cs - i) * s) // w) + cs)
print(best) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.
You decided to rob a town's blacksmith and you take a follower with you. You can carry at most $p$ units and your follower — at most $f$ units.
In the blacksmith shop, you found $cnt_s$ swords and $cnt_w$ war axes. Each sword weights $s$ units and each war axe — $w$ units. You don't care what to take, since each of them will melt into one steel ingot.
What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
The first line of each test case contains two integers $p$ and $f$ ($1 \le p, f \le 10^9$) — yours and your follower's capacities.
The second line of each test case contains two integers $cnt_s$ and $cnt_w$ ($1 \le cnt_s, cnt_w \le 2 \cdot 10^5$) — the number of swords and war axes in the shop.
The third line of each test case contains two integers $s$ and $w$ ($1 \le s, w \le 10^9$) — the weights of each sword and each war axe.
It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.
-----Example-----
Input
3
33 27
6 10
5 6
100 200
10 10
5 5
1 19
1 3
19 5
Output
11
20
3
-----Note-----
In the first test case: you should take $3$ swords and $3$ war axes: $3 \cdot 5 + 3 \cdot 6 = 33 \le 33$ and your follower — $3$ swords and $2$ war axes: $3 \cdot 5 + 2 \cdot 6 = 27 \le 27$. $3 + 3 + 3 + 2 = 11$ weapons in total.
In the second test case, you can take all available weapons even without your follower's help, since $5 \cdot 10 + 5 \cdot 10 \le 100$.
In the third test case, you can't take anything, but your follower can take $3$ war axes: $3 \cdot 5 \le 19$. | tc = int(input())
for t in range(tc):
p, f = map(int, input().split())
sw_cnt, ax_cnt = map(int, input().split())
s_weight, ax_weight = map(int, input().split())
ans = 0
k = min(sw_cnt + 1, p // s_weight + 1)
for my_sw_cnt in range(k):
ans_ = 0
sw_cnt_ = sw_cnt
ax_cnt_ = ax_cnt
sw_cnt_ -= my_sw_cnt
my_cap = p - my_sw_cnt * s_weight
ans_ += my_sw_cnt
my_ax_cnt = min(ax_cnt_, my_cap // ax_weight)
ax_cnt_ -= my_ax_cnt
my_cap = p - my_ax_cnt * ax_weight
ans_ += my_ax_cnt
if s_weight <= ax_weight:
f_sw_cnt = min(sw_cnt_, f // s_weight)
f_cap = f - f_sw_cnt * s_weight
ans_ += f_sw_cnt
f_ax_cnt = min(ax_cnt_, f_cap // ax_weight)
ans_ += f_ax_cnt
else:
f_ax_cnt = min(ax_cnt_, f // ax_weight)
f_cap = f - f_ax_cnt * ax_weight
ans_ += f_ax_cnt
f_sw_cnt = min(sw_cnt_, f_cap // s_weight)
ans_ += f_sw_cnt
ans = max(ans, ans_)
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.
You decided to rob a town's blacksmith and you take a follower with you. You can carry at most $p$ units and your follower — at most $f$ units.
In the blacksmith shop, you found $cnt_s$ swords and $cnt_w$ war axes. Each sword weights $s$ units and each war axe — $w$ units. You don't care what to take, since each of them will melt into one steel ingot.
What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
The first line of each test case contains two integers $p$ and $f$ ($1 \le p, f \le 10^9$) — yours and your follower's capacities.
The second line of each test case contains two integers $cnt_s$ and $cnt_w$ ($1 \le cnt_s, cnt_w \le 2 \cdot 10^5$) — the number of swords and war axes in the shop.
The third line of each test case contains two integers $s$ and $w$ ($1 \le s, w \le 10^9$) — the weights of each sword and each war axe.
It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.
-----Example-----
Input
3
33 27
6 10
5 6
100 200
10 10
5 5
1 19
1 3
19 5
Output
11
20
3
-----Note-----
In the first test case: you should take $3$ swords and $3$ war axes: $3 \cdot 5 + 3 \cdot 6 = 33 \le 33$ and your follower — $3$ swords and $2$ war axes: $3 \cdot 5 + 2 \cdot 6 = 27 \le 27$. $3 + 3 + 3 + 2 = 11$ weapons in total.
In the second test case, you can take all available weapons even without your follower's help, since $5 \cdot 10 + 5 \cdot 10 \le 100$.
In the third test case, you can't take anything, but your follower can take $3$ war axes: $3 \cdot 5 \le 19$. | for T in range(int(input())):
p, f = map(int, input().split())
cnt_s, cnt_w = map(int, input().split())
s, w = map(int, input().split())
if s > w:
cnt_s, cnt_w = cnt_w, cnt_s
s, w = w, s
ans = 0
for i in range(cnt_s + 1):
if i * s > p:
break
cur = 0
cur_s, cur_w = cnt_s, cnt_w
cur = cur + i
cur_s = cur_s - i
temp = min((p - i * s) // w, cur_w)
cur = cur + temp
cur_w = cur_w - temp
temp = min(f // s, cur_s)
cur = cur + temp
cur_s = cur_s - temp
temp = min((f - temp * s) // w, cur_w)
cur = cur + temp
ans = max(ans, cur)
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.
You decided to rob a town's blacksmith and you take a follower with you. You can carry at most $p$ units and your follower — at most $f$ units.
In the blacksmith shop, you found $cnt_s$ swords and $cnt_w$ war axes. Each sword weights $s$ units and each war axe — $w$ units. You don't care what to take, since each of them will melt into one steel ingot.
What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
The first line of each test case contains two integers $p$ and $f$ ($1 \le p, f \le 10^9$) — yours and your follower's capacities.
The second line of each test case contains two integers $cnt_s$ and $cnt_w$ ($1 \le cnt_s, cnt_w \le 2 \cdot 10^5$) — the number of swords and war axes in the shop.
The third line of each test case contains two integers $s$ and $w$ ($1 \le s, w \le 10^9$) — the weights of each sword and each war axe.
It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.
-----Example-----
Input
3
33 27
6 10
5 6
100 200
10 10
5 5
1 19
1 3
19 5
Output
11
20
3
-----Note-----
In the first test case: you should take $3$ swords and $3$ war axes: $3 \cdot 5 + 3 \cdot 6 = 33 \le 33$ and your follower — $3$ swords and $2$ war axes: $3 \cdot 5 + 2 \cdot 6 = 27 \le 27$. $3 + 3 + 3 + 2 = 11$ weapons in total.
In the second test case, you can take all available weapons even without your follower's help, since $5 \cdot 10 + 5 \cdot 10 \le 100$.
In the third test case, you can't take anything, but your follower can take $3$ war axes: $3 \cdot 5 \le 19$. | def main():
for _ in range(Iint()):
myCapacity, friendCapcity = Ilist()
countSword, countWarAxe = Ilist()
SwordWeight, WarAxeWeight = Ilist()
if SwordWeight > WarAxeWeight:
SwordWeight, WarAxeWeight = WarAxeWeight, SwordWeight
countSword, countWarAxe = countWarAxe, countSword
TotalCount = myCapacity // SwordWeight + friendCapcity // SwordWeight
if TotalCount <= countSword:
print(TotalCount)
continue
else:
TotalCount = countSword
for i in range(0, countSword + 1):
p, f = myCapacity, friendCapcity
p -= SwordWeight * i
f -= (countSword - i) * SwordWeight
if p >= 0 and f >= 0:
TotalCount = max(
TotalCount, countSword + p // WarAxeWeight + f // WarAxeWeight
)
print(min(TotalCount, countSword + countWarAxe))
def I():
return input()
def Iint():
return int(input())
def Ilist():
return list(map(int, input().split()))
def Imap():
return map(int, input().split())
def Plist(li, s=" "):
print(s.join(map(str, li)))
main() | FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_DEF RETURN FUNC_CALL 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 RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF STRING EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR |
You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.
You decided to rob a town's blacksmith and you take a follower with you. You can carry at most $p$ units and your follower — at most $f$ units.
In the blacksmith shop, you found $cnt_s$ swords and $cnt_w$ war axes. Each sword weights $s$ units and each war axe — $w$ units. You don't care what to take, since each of them will melt into one steel ingot.
What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
The first line of each test case contains two integers $p$ and $f$ ($1 \le p, f \le 10^9$) — yours and your follower's capacities.
The second line of each test case contains two integers $cnt_s$ and $cnt_w$ ($1 \le cnt_s, cnt_w \le 2 \cdot 10^5$) — the number of swords and war axes in the shop.
The third line of each test case contains two integers $s$ and $w$ ($1 \le s, w \le 10^9$) — the weights of each sword and each war axe.
It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.
-----Example-----
Input
3
33 27
6 10
5 6
100 200
10 10
5 5
1 19
1 3
19 5
Output
11
20
3
-----Note-----
In the first test case: you should take $3$ swords and $3$ war axes: $3 \cdot 5 + 3 \cdot 6 = 33 \le 33$ and your follower — $3$ swords and $2$ war axes: $3 \cdot 5 + 2 \cdot 6 = 27 \le 27$. $3 + 3 + 3 + 2 = 11$ weapons in total.
In the second test case, you can take all available weapons even without your follower's help, since $5 \cdot 10 + 5 \cdot 10 \le 100$.
In the third test case, you can't take anything, but your follower can take $3$ war axes: $3 \cdot 5 \le 19$. | import sys
def input():
return sys.stdin.readline().strip()
def list2d(a, b, c):
return [([c] * b) for i in range(a)]
def list3d(a, b, c, d):
return [[([d] * c) for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e):
return [[[([e] * d) for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1):
return int(-(-x // y))
def INT():
return int(input())
def MAP():
return map(int, input().split())
def LIST(N=None):
return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes():
print("Yes")
def No():
print("No")
def YES():
print("YES")
def NO():
print("NO")
INF = 10**19
MOD = 10**9 + 7
for _ in range(INT()):
F, P = MAP()
N, M = MAP()
s, w = MAP()
if s > w:
s, w = w, s
N, M = M, N
ans = 0
for i in range(N + 1):
cnt = 0
f, p = F, P
cs, cw = N, M
use = min(f // s, i)
f -= use * s
cs -= use
cnt += use
use = min(p // s, N - i)
p -= use * s
cs -= use
cnt += use
use = min(f // w, cw)
f -= use * w
cw -= use
cnt += use
use = min(p // w, cw)
p -= use * w
cw -= use
cnt += use
ans = max(ans, cnt)
print(ans) | IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF NONE RETURN VAR NONE FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.
You decided to rob a town's blacksmith and you take a follower with you. You can carry at most $p$ units and your follower — at most $f$ units.
In the blacksmith shop, you found $cnt_s$ swords and $cnt_w$ war axes. Each sword weights $s$ units and each war axe — $w$ units. You don't care what to take, since each of them will melt into one steel ingot.
What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
The first line of each test case contains two integers $p$ and $f$ ($1 \le p, f \le 10^9$) — yours and your follower's capacities.
The second line of each test case contains two integers $cnt_s$ and $cnt_w$ ($1 \le cnt_s, cnt_w \le 2 \cdot 10^5$) — the number of swords and war axes in the shop.
The third line of each test case contains two integers $s$ and $w$ ($1 \le s, w \le 10^9$) — the weights of each sword and each war axe.
It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.
-----Example-----
Input
3
33 27
6 10
5 6
100 200
10 10
5 5
1 19
1 3
19 5
Output
11
20
3
-----Note-----
In the first test case: you should take $3$ swords and $3$ war axes: $3 \cdot 5 + 3 \cdot 6 = 33 \le 33$ and your follower — $3$ swords and $2$ war axes: $3 \cdot 5 + 2 \cdot 6 = 27 \le 27$. $3 + 3 + 3 + 2 = 11$ weapons in total.
In the second test case, you can take all available weapons even without your follower's help, since $5 \cdot 10 + 5 \cdot 10 \le 100$.
In the third test case, you can't take anything, but your follower can take $3$ war axes: $3 \cdot 5 \le 19$. | def solve():
p, f = map(int, input().split())
cnts, cntw = map(int, input().split())
s, w = map(int, input().split())
if s > w:
s, w = w, s
cnts, cntw = cntw, cnts
mans = 0
for i in range(cnts + 1):
ans = 0
if p >= s * i:
ans += i
cntsnow = cnts - i
pnow = p - s * i
ans += min(cntw, pnow // w)
cntwnow = cntw - min(cntw, pnow // w)
fn = f
ans += min(fn // s, cntsnow)
fn -= min(fn // s, cntsnow) * s
ans += min(fn // w, cntwnow)
mans = max(ans, mans)
print(mans)
for i in range(int(input())):
solve() | FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR |
You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.
You decided to rob a town's blacksmith and you take a follower with you. You can carry at most $p$ units and your follower — at most $f$ units.
In the blacksmith shop, you found $cnt_s$ swords and $cnt_w$ war axes. Each sword weights $s$ units and each war axe — $w$ units. You don't care what to take, since each of them will melt into one steel ingot.
What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
The first line of each test case contains two integers $p$ and $f$ ($1 \le p, f \le 10^9$) — yours and your follower's capacities.
The second line of each test case contains two integers $cnt_s$ and $cnt_w$ ($1 \le cnt_s, cnt_w \le 2 \cdot 10^5$) — the number of swords and war axes in the shop.
The third line of each test case contains two integers $s$ and $w$ ($1 \le s, w \le 10^9$) — the weights of each sword and each war axe.
It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.
-----Example-----
Input
3
33 27
6 10
5 6
100 200
10 10
5 5
1 19
1 3
19 5
Output
11
20
3
-----Note-----
In the first test case: you should take $3$ swords and $3$ war axes: $3 \cdot 5 + 3 \cdot 6 = 33 \le 33$ and your follower — $3$ swords and $2$ war axes: $3 \cdot 5 + 2 \cdot 6 = 27 \le 27$. $3 + 3 + 3 + 2 = 11$ weapons in total.
In the second test case, you can take all available weapons even without your follower's help, since $5 \cdot 10 + 5 \cdot 10 \le 100$.
In the third test case, you can't take anything, but your follower can take $3$ war axes: $3 \cdot 5 \le 19$. | t = int(input())
for case in range(t):
p, f = map(int, input().split())
swords, axes = map(int, input().split())
s, w = map(int, input().split())
capacity = p + f
if s <= w:
if p // s + f // s <= swords:
print(p // s + f // s)
else:
maxWeapons = 0
for i in range(max(0, swords - f // s), min(p // s + 1, swords + 1)):
weapons = 0
if (p - i * s) // w + (f - (swords - i) * s) // w <= axes:
weapons = (p - i * s) // w + (f - (swords - i) * s) // w + swords
else:
weapons = swords + axes
if weapons > maxWeapons:
maxWeapons = weapons
print(maxWeapons)
elif p // w + f // w <= axes:
print(p // w + f // w)
else:
maxWeapons = 0
for i in range(max(0, axes - f // w), min(p // w + 1, axes + 1)):
weapons = 0
if (p - i * w) // s + (f - (axes - i) * w) // s <= swords:
weapons = (p - i * w) // s + (f - (axes - i) * w) // s + axes
else:
weapons = swords + axes
if weapons > maxWeapons:
maxWeapons = weapons
print(maxWeapons) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR IF BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.
You decided to rob a town's blacksmith and you take a follower with you. You can carry at most $p$ units and your follower — at most $f$ units.
In the blacksmith shop, you found $cnt_s$ swords and $cnt_w$ war axes. Each sword weights $s$ units and each war axe — $w$ units. You don't care what to take, since each of them will melt into one steel ingot.
What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
The first line of each test case contains two integers $p$ and $f$ ($1 \le p, f \le 10^9$) — yours and your follower's capacities.
The second line of each test case contains two integers $cnt_s$ and $cnt_w$ ($1 \le cnt_s, cnt_w \le 2 \cdot 10^5$) — the number of swords and war axes in the shop.
The third line of each test case contains two integers $s$ and $w$ ($1 \le s, w \le 10^9$) — the weights of each sword and each war axe.
It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.
-----Example-----
Input
3
33 27
6 10
5 6
100 200
10 10
5 5
1 19
1 3
19 5
Output
11
20
3
-----Note-----
In the first test case: you should take $3$ swords and $3$ war axes: $3 \cdot 5 + 3 \cdot 6 = 33 \le 33$ and your follower — $3$ swords and $2$ war axes: $3 \cdot 5 + 2 \cdot 6 = 27 \le 27$. $3 + 3 + 3 + 2 = 11$ weapons in total.
In the second test case, you can take all available weapons even without your follower's help, since $5 \cdot 10 + 5 \cdot 10 \le 100$.
In the third test case, you can't take anything, but your follower can take $3$ war axes: $3 \cdot 5 \le 19$. | def get_results():
cap1, cap2 = list(map(int, input().split()))
n1, n2 = list(map(int, input().split()))
w1, w2 = list(map(int, input().split()))
max_w = 0
if w2 < w1:
n1, n2 = n2, n1
w1, w2 = w2, w1
for your_lighter in range(0, min(n1, cap1 // w1) + 1):
your_heavier = min(n2, (cap1 - your_lighter * w1) // w2)
follower_lighter = min(n1 - your_lighter, cap2 // w1)
follower_heavier = min(n2 - your_heavier, (cap2 - follower_lighter * w1) // w2)
max_w = max(
max_w, your_lighter + your_heavier + follower_lighter + follower_heavier
)
return max_w
def main():
t = int(input())
res = []
for i in range(t):
print(get_results())
main() | FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR |
You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.
You decided to rob a town's blacksmith and you take a follower with you. You can carry at most $p$ units and your follower — at most $f$ units.
In the blacksmith shop, you found $cnt_s$ swords and $cnt_w$ war axes. Each sword weights $s$ units and each war axe — $w$ units. You don't care what to take, since each of them will melt into one steel ingot.
What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
The first line of each test case contains two integers $p$ and $f$ ($1 \le p, f \le 10^9$) — yours and your follower's capacities.
The second line of each test case contains two integers $cnt_s$ and $cnt_w$ ($1 \le cnt_s, cnt_w \le 2 \cdot 10^5$) — the number of swords and war axes in the shop.
The third line of each test case contains two integers $s$ and $w$ ($1 \le s, w \le 10^9$) — the weights of each sword and each war axe.
It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.
-----Example-----
Input
3
33 27
6 10
5 6
100 200
10 10
5 5
1 19
1 3
19 5
Output
11
20
3
-----Note-----
In the first test case: you should take $3$ swords and $3$ war axes: $3 \cdot 5 + 3 \cdot 6 = 33 \le 33$ and your follower — $3$ swords and $2$ war axes: $3 \cdot 5 + 2 \cdot 6 = 27 \le 27$. $3 + 3 + 3 + 2 = 11$ weapons in total.
In the second test case, you can take all available weapons even without your follower's help, since $5 \cdot 10 + 5 \cdot 10 \le 100$.
In the third test case, you can't take anything, but your follower can take $3$ war axes: $3 \cdot 5 \le 19$. | import sys
LI = lambda: list(map(int, sys.stdin.readline().strip("\n").split()))
MI = lambda: map(int, sys.stdin.readline().strip("\n").split())
SI = lambda: sys.stdin.readline().strip("\n")
II = lambda: int(sys.stdin.readline().strip("\n"))
for _ in range(II()):
p, f = MI()
cs, cw = MI()
s, w = MI()
if s > w:
s, w = w, s
cs, cw = cw, cs
s1, ans = 0, 0
while s1 <= cs and s1 * s <= p:
w1 = min(cw, (p - s1 * s) // w)
s2 = min(cs - s1, f // s)
w2 = min(cw - w1, (f - s2 * s) // w)
ans = max(ans, s1 + w1 + s2 + w2)
s1 += 1
print(ans) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.
You decided to rob a town's blacksmith and you take a follower with you. You can carry at most $p$ units and your follower — at most $f$ units.
In the blacksmith shop, you found $cnt_s$ swords and $cnt_w$ war axes. Each sword weights $s$ units and each war axe — $w$ units. You don't care what to take, since each of them will melt into one steel ingot.
What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
The first line of each test case contains two integers $p$ and $f$ ($1 \le p, f \le 10^9$) — yours and your follower's capacities.
The second line of each test case contains two integers $cnt_s$ and $cnt_w$ ($1 \le cnt_s, cnt_w \le 2 \cdot 10^5$) — the number of swords and war axes in the shop.
The third line of each test case contains two integers $s$ and $w$ ($1 \le s, w \le 10^9$) — the weights of each sword and each war axe.
It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.
-----Example-----
Input
3
33 27
6 10
5 6
100 200
10 10
5 5
1 19
1 3
19 5
Output
11
20
3
-----Note-----
In the first test case: you should take $3$ swords and $3$ war axes: $3 \cdot 5 + 3 \cdot 6 = 33 \le 33$ and your follower — $3$ swords and $2$ war axes: $3 \cdot 5 + 2 \cdot 6 = 27 \le 27$. $3 + 3 + 3 + 2 = 11$ weapons in total.
In the second test case, you can take all available weapons even without your follower's help, since $5 \cdot 10 + 5 \cdot 10 \le 100$.
In the third test case, you can't take anything, but your follower can take $3$ war axes: $3 \cdot 5 \le 19$. | import sys
input = sys.stdin.buffer.readline
def solution():
p, f = map(int, input().split())
cnts, cntw = map(int, input().split())
s, w = map(int, input().split())
num1 = 0
num2 = 0
ans = 0
for i in range(cnts + 1):
num1 = i
if i * s > p:
continue
x = p - i * s
num2 = min(cntw, x // w)
x = num2 * w
num1x = min(f // s, cnts - num1)
num2x = num1x * s
num1y = min(f // w, cntw - num2)
num2y = num1y * w
num1yy = min((f - num2y) // s, cnts - num1)
num1xx = min((f - num2x) // w, cntw - num2)
z = max(num1x + num1xx, num1y + num1yy)
ans = max(ans, num1 + num2 + z)
print(ans)
t = int(input())
for _ in range(t):
solution() | IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.
You decided to rob a town's blacksmith and you take a follower with you. You can carry at most $p$ units and your follower — at most $f$ units.
In the blacksmith shop, you found $cnt_s$ swords and $cnt_w$ war axes. Each sword weights $s$ units and each war axe — $w$ units. You don't care what to take, since each of them will melt into one steel ingot.
What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
The first line of each test case contains two integers $p$ and $f$ ($1 \le p, f \le 10^9$) — yours and your follower's capacities.
The second line of each test case contains two integers $cnt_s$ and $cnt_w$ ($1 \le cnt_s, cnt_w \le 2 \cdot 10^5$) — the number of swords and war axes in the shop.
The third line of each test case contains two integers $s$ and $w$ ($1 \le s, w \le 10^9$) — the weights of each sword and each war axe.
It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.
-----Example-----
Input
3
33 27
6 10
5 6
100 200
10 10
5 5
1 19
1 3
19 5
Output
11
20
3
-----Note-----
In the first test case: you should take $3$ swords and $3$ war axes: $3 \cdot 5 + 3 \cdot 6 = 33 \le 33$ and your follower — $3$ swords and $2$ war axes: $3 \cdot 5 + 2 \cdot 6 = 27 \le 27$. $3 + 3 + 3 + 2 = 11$ weapons in total.
In the second test case, you can take all available weapons even without your follower's help, since $5 \cdot 10 + 5 \cdot 10 \le 100$.
In the third test case, you can't take anything, but your follower can take $3$ war axes: $3 \cdot 5 \le 19$. | import sys
input = sys.stdin.readline
t = int(input())
for i in range(t):
p, f = map(int, input().split())
cs, cw = map(int, input().split())
s, w = map(int, input().split())
maxa, mina = max(p, f), min(p, f)
maxa1, mina1 = maxa, mina
l = [[s, cs], [w, cw]]
l.sort()
l1 = []
for i in l:
xy = i[:]
l1.append(xy)
ans = 0
for i in range(l[0][1]):
if (i + 1) * l[0][0] <= mina:
one = i + 1
left = mina - one * l[0][0]
two = min(left // l[1][0], l[1][1])
tmp1 = l[0][1] - one
tmp2 = l[1][1] - two
three = min(maxa // l[0][0], tmp1)
left = maxa - three * l[0][0]
four = min(left // l[1][0], tmp2)
ans = max(ans, one + two + three + four)
for i in range(l[0][1]):
if (i + 1) * l[0][0] <= maxa1:
one = i + 1
left = maxa1 - one * l1[0][0]
two = min(left // l1[1][0], l1[1][1])
tmp1 = l1[0][1] - one
tmp2 = l1[1][1] - two
three = min(mina1 // l1[0][0], tmp1)
left = mina1 - three * l1[0][0]
four = min(left // l1[1][0], tmp2)
ans = max(ans, one + two + three + four)
sys.stdout.write(str(ans) + "\n") | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST LIST VAR VAR LIST VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING |
You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.
You decided to rob a town's blacksmith and you take a follower with you. You can carry at most $p$ units and your follower — at most $f$ units.
In the blacksmith shop, you found $cnt_s$ swords and $cnt_w$ war axes. Each sword weights $s$ units and each war axe — $w$ units. You don't care what to take, since each of them will melt into one steel ingot.
What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
The first line of each test case contains two integers $p$ and $f$ ($1 \le p, f \le 10^9$) — yours and your follower's capacities.
The second line of each test case contains two integers $cnt_s$ and $cnt_w$ ($1 \le cnt_s, cnt_w \le 2 \cdot 10^5$) — the number of swords and war axes in the shop.
The third line of each test case contains two integers $s$ and $w$ ($1 \le s, w \le 10^9$) — the weights of each sword and each war axe.
It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.
-----Example-----
Input
3
33 27
6 10
5 6
100 200
10 10
5 5
1 19
1 3
19 5
Output
11
20
3
-----Note-----
In the first test case: you should take $3$ swords and $3$ war axes: $3 \cdot 5 + 3 \cdot 6 = 33 \le 33$ and your follower — $3$ swords and $2$ war axes: $3 \cdot 5 + 2 \cdot 6 = 27 \le 27$. $3 + 3 + 3 + 2 = 11$ weapons in total.
In the second test case, you can take all available weapons even without your follower's help, since $5 \cdot 10 + 5 \cdot 10 \le 100$.
In the third test case, you can't take anything, but your follower can take $3$ war axes: $3 \cdot 5 \le 19$. | import sys
input = sys.stdin.readline
for kek in range(int(input())):
p, f = map(int, input().split())
cnt_s, cnt_w = map(int, input().split())
s, w = map(int, input().split())
if s >= w:
s, w = w, s
cnt_w, cnt_s = cnt_s, cnt_w
ma_s = min(cnt_s, p // s + f // s)
ma_w = 0
for i in range(ma_s + 1):
zz = 0
if p - i * s >= 0:
zz += (p - i * s) // w
else:
continue
if f - (ma_s - i) * s >= 0:
zz += (f - (ma_s - i) * s) // w
else:
continue
ma_w = max(ma_w, zz)
print(min(ma_w, cnt_w) + ma_s) | IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR IF BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR |
You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.
You decided to rob a town's blacksmith and you take a follower with you. You can carry at most $p$ units and your follower — at most $f$ units.
In the blacksmith shop, you found $cnt_s$ swords and $cnt_w$ war axes. Each sword weights $s$ units and each war axe — $w$ units. You don't care what to take, since each of them will melt into one steel ingot.
What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
The first line of each test case contains two integers $p$ and $f$ ($1 \le p, f \le 10^9$) — yours and your follower's capacities.
The second line of each test case contains two integers $cnt_s$ and $cnt_w$ ($1 \le cnt_s, cnt_w \le 2 \cdot 10^5$) — the number of swords and war axes in the shop.
The third line of each test case contains two integers $s$ and $w$ ($1 \le s, w \le 10^9$) — the weights of each sword and each war axe.
It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.
-----Example-----
Input
3
33 27
6 10
5 6
100 200
10 10
5 5
1 19
1 3
19 5
Output
11
20
3
-----Note-----
In the first test case: you should take $3$ swords and $3$ war axes: $3 \cdot 5 + 3 \cdot 6 = 33 \le 33$ and your follower — $3$ swords and $2$ war axes: $3 \cdot 5 + 2 \cdot 6 = 27 \le 27$. $3 + 3 + 3 + 2 = 11$ weapons in total.
In the second test case, you can take all available weapons even without your follower's help, since $5 \cdot 10 + 5 \cdot 10 \le 100$.
In the third test case, you can't take anything, but your follower can take $3$ war axes: $3 \cdot 5 \le 19$. | rr = lambda: input().strip()
rrm = lambda: map(int, rr().split())
def solve():
p, f = rrm()
scnt, wcnt = rrm()
s, w = rrm()
ans = 0
if w < s:
s, w = w, s
wcnt, scnt = scnt, wcnt
x = min(scnt, p // s)
for s1 in range(x + 1):
w1 = min(wcnt, (p - s1 * s) // w)
s2 = min(scnt - s1, f // s)
w2 = min(wcnt - w1, (f - s2 * s) // w)
ans = max(ans, s1 + s2 + w1 + w2)
return ans
T = int(rr())
for _ in range(T):
ans = solve()
print(ans) | ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR |
You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.
You decided to rob a town's blacksmith and you take a follower with you. You can carry at most $p$ units and your follower — at most $f$ units.
In the blacksmith shop, you found $cnt_s$ swords and $cnt_w$ war axes. Each sword weights $s$ units and each war axe — $w$ units. You don't care what to take, since each of them will melt into one steel ingot.
What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
The first line of each test case contains two integers $p$ and $f$ ($1 \le p, f \le 10^9$) — yours and your follower's capacities.
The second line of each test case contains two integers $cnt_s$ and $cnt_w$ ($1 \le cnt_s, cnt_w \le 2 \cdot 10^5$) — the number of swords and war axes in the shop.
The third line of each test case contains two integers $s$ and $w$ ($1 \le s, w \le 10^9$) — the weights of each sword and each war axe.
It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.
-----Example-----
Input
3
33 27
6 10
5 6
100 200
10 10
5 5
1 19
1 3
19 5
Output
11
20
3
-----Note-----
In the first test case: you should take $3$ swords and $3$ war axes: $3 \cdot 5 + 3 \cdot 6 = 33 \le 33$ and your follower — $3$ swords and $2$ war axes: $3 \cdot 5 + 2 \cdot 6 = 27 \le 27$. $3 + 3 + 3 + 2 = 11$ weapons in total.
In the second test case, you can take all available weapons even without your follower's help, since $5 \cdot 10 + 5 \cdot 10 \le 100$.
In the third test case, you can't take anything, but your follower can take $3$ war axes: $3 \cdot 5 \le 19$. | import sys
input = sys.stdin.readline
def solve():
p, f = list(map(int, input().split()))
cntS, cntW = list(map(int, input().split()))
s, w = list(map(int, input().split()))
if s < w:
a = s
cntA = cntS
b = w
cntB = cntW
else:
a = w
cntA = cntW
b = s
cntB = cntS
if cntA >= p // a + f // a:
print(p // a + f // a)
else:
ans = 0
for i in range(min(p // a + 1, cntA + 1)):
cntP = min((p - i * a) // b, cntB)
dB = cntB - cntP
c = i + cntP
cntF = min(f // a, cntA - i)
d = f - cntF * a
c += cntF + min(d // b, dB)
ans = max(ans, c)
print(ans)
t = int(input())
for _ in range(t):
solve() | IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.
You decided to rob a town's blacksmith and you take a follower with you. You can carry at most $p$ units and your follower — at most $f$ units.
In the blacksmith shop, you found $cnt_s$ swords and $cnt_w$ war axes. Each sword weights $s$ units and each war axe — $w$ units. You don't care what to take, since each of them will melt into one steel ingot.
What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
The first line of each test case contains two integers $p$ and $f$ ($1 \le p, f \le 10^9$) — yours and your follower's capacities.
The second line of each test case contains two integers $cnt_s$ and $cnt_w$ ($1 \le cnt_s, cnt_w \le 2 \cdot 10^5$) — the number of swords and war axes in the shop.
The third line of each test case contains two integers $s$ and $w$ ($1 \le s, w \le 10^9$) — the weights of each sword and each war axe.
It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.
-----Example-----
Input
3
33 27
6 10
5 6
100 200
10 10
5 5
1 19
1 3
19 5
Output
11
20
3
-----Note-----
In the first test case: you should take $3$ swords and $3$ war axes: $3 \cdot 5 + 3 \cdot 6 = 33 \le 33$ and your follower — $3$ swords and $2$ war axes: $3 \cdot 5 + 2 \cdot 6 = 27 \le 27$. $3 + 3 + 3 + 2 = 11$ weapons in total.
In the second test case, you can take all available weapons even without your follower's help, since $5 \cdot 10 + 5 \cdot 10 \le 100$.
In the third test case, you can't take anything, but your follower can take $3$ war axes: $3 \cdot 5 \le 19$. | import sys
def i():
return sys.stdin.readline()[:-1]
cases = int(i())
for x in range(cases):
p, f = map(int, i().split())
cs, cw = map(int, i().split())
s, w = map(int, i().split())
if s > w:
s, w = w, s
cs, cw = cw, cs
pMax = p // s
fMax = f // s
if pMax + fMax <= cs:
print(pMax + fMax)
else:
currTotal = 0
for y in range(cs + 1):
pHold = p - y * s
fHold = f - (cs - y) * s
if pHold >= 0 and fHold >= 0:
currTotal = max(currTotal, min(cw, pHold // w + fHold // w))
print(cs + currTotal) | IMPORT FUNC_DEF RETURN FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR |
You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.
You decided to rob a town's blacksmith and you take a follower with you. You can carry at most $p$ units and your follower — at most $f$ units.
In the blacksmith shop, you found $cnt_s$ swords and $cnt_w$ war axes. Each sword weights $s$ units and each war axe — $w$ units. You don't care what to take, since each of them will melt into one steel ingot.
What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
The first line of each test case contains two integers $p$ and $f$ ($1 \le p, f \le 10^9$) — yours and your follower's capacities.
The second line of each test case contains two integers $cnt_s$ and $cnt_w$ ($1 \le cnt_s, cnt_w \le 2 \cdot 10^5$) — the number of swords and war axes in the shop.
The third line of each test case contains two integers $s$ and $w$ ($1 \le s, w \le 10^9$) — the weights of each sword and each war axe.
It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.
-----Example-----
Input
3
33 27
6 10
5 6
100 200
10 10
5 5
1 19
1 3
19 5
Output
11
20
3
-----Note-----
In the first test case: you should take $3$ swords and $3$ war axes: $3 \cdot 5 + 3 \cdot 6 = 33 \le 33$ and your follower — $3$ swords and $2$ war axes: $3 \cdot 5 + 2 \cdot 6 = 27 \le 27$. $3 + 3 + 3 + 2 = 11$ weapons in total.
In the second test case, you can take all available weapons even without your follower's help, since $5 \cdot 10 + 5 \cdot 10 \le 100$.
In the third test case, you can't take anything, but your follower can take $3$ war axes: $3 \cdot 5 \le 19$. | t = int(input().split()[0])
for case in range(t):
p, f = map(int, input().split())
cnts, cntw = map(int, input().split())
s, w = map(int, input().split())
if w < s:
s, w = w, s
cnts, cntw = cntw, cnts
ans = 0
fwmax = min(p // w, cntw)
for fw in range(fwmax + 1):
fs = min(cnts, (p - fw * w) // s)
news = cnts - fs
neww = cntw - fw
ss = min(news, f // s)
sw = min(neww, (f - ss * s) // w)
if fs + fw + ss + sw > ans:
ans = fs + fw + ss + sw
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.
You decided to rob a town's blacksmith and you take a follower with you. You can carry at most $p$ units and your follower — at most $f$ units.
In the blacksmith shop, you found $cnt_s$ swords and $cnt_w$ war axes. Each sword weights $s$ units and each war axe — $w$ units. You don't care what to take, since each of them will melt into one steel ingot.
What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
The first line of each test case contains two integers $p$ and $f$ ($1 \le p, f \le 10^9$) — yours and your follower's capacities.
The second line of each test case contains two integers $cnt_s$ and $cnt_w$ ($1 \le cnt_s, cnt_w \le 2 \cdot 10^5$) — the number of swords and war axes in the shop.
The third line of each test case contains two integers $s$ and $w$ ($1 \le s, w \le 10^9$) — the weights of each sword and each war axe.
It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.
-----Example-----
Input
3
33 27
6 10
5 6
100 200
10 10
5 5
1 19
1 3
19 5
Output
11
20
3
-----Note-----
In the first test case: you should take $3$ swords and $3$ war axes: $3 \cdot 5 + 3 \cdot 6 = 33 \le 33$ and your follower — $3$ swords and $2$ war axes: $3 \cdot 5 + 2 \cdot 6 = 27 \le 27$. $3 + 3 + 3 + 2 = 11$ weapons in total.
In the second test case, you can take all available weapons even without your follower's help, since $5 \cdot 10 + 5 \cdot 10 \le 100$.
In the third test case, you can't take anything, but your follower can take $3$ war axes: $3 \cdot 5 \le 19$. | def main():
t = int(input())
for _ in range(t):
p, f = map(int, input().split())
ns, na = map(int, input().split())
ws, wa = map(int, input().split())
if ws > wa:
ws, wa = wa, ws
ns, na = na, ns
ans = -1
for i in range(ns + 1):
sp = i
if p < sp * ws:
break
sf = min(ns - sp, f // ws)
ap = min((p - sp * ws) // wa, na)
af = min(na - ap, (f - ws * sf) // wa)
ans = max(ans, sp + ap + sf + af)
p, f = f, p
for i in range(ns + 1):
sp = i
if p < sp * ws:
break
sf = min(ns - sp, f // ws)
ap = min((p - sp * ws) // wa, na)
af = min(na - ap, (f - ws * sf) // wa)
ans = max(ans, sp + ap + sf + af)
print(ans)
return
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN 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()))
if n == 1:
print(a[0])
else:
gauche = [~a[0]] * n
droite = [~a[-1]] * n
for i in range(1, n):
gauche[i] = gauche[i - 1] & ~a[i]
for i in range(n - 2, -1, -1):
droite[i] = droite[i + 1] & ~a[i]
maxi = a[i] & droite[i + 1]
index = 0
for i in range(1, n):
res = (
a[i] & gauche[i - 1] if i == n - 1 else a[i] & gauche[i - 1] & droite[i + 1]
)
if res > maxi:
maxi = res
index = i
print(" ".join(map(str, [a[index]] + a[0:index] + a[index + 1 : n]))) | 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 BIN_OP LIST VAR NUMBER VAR ASSIGN VAR BIN_OP LIST VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER 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 BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER 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 FUNC_CALL STRING FUNC_CALL VAR VAR BIN_OP BIN_OP LIST VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER 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()]
if n == 1:
print(l[0])
exit(0)
pre = l[0:n]
suf = l[0:n]
for i in range(1, n):
pre[i] |= pre[i - 1]
for i in range(n - 2, -1, -1):
suf[i] |= suf[i + 1]
ans = 0
cur = 0
k = 0
for i in range(n):
if i == 0:
cur = ~(~l[i] | suf[i + 1])
elif i == n - 1:
cur = ~(pre[i - 1] | ~l[i])
else:
cur = ~(pre[i - 1] | ~l[i] | suf[i + 1])
if cur > ans:
ans = cur
k = i
ans = [l[k]]
ans.extend([x for i, x in enumerate(l) if i != k])
ans = [str(x) for x in ans]
print(" ".join(ans)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR 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 BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR 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. | import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
n = int(input())
a = list(map(int, input().split()))
left = [0]
right = [0]
for ai in a:
left.append(left[-1] | ai)
for ai in reversed(a):
right.append(right[-1] | ai)
maximum = 0
max_ind = 0
for i, ai in enumerate(a):
mask = left[i] | right[n - 1 - i]
tmp = (ai | mask) - mask
if tmp > maximum:
maximum = tmp
max_ind = i
ans = [a[max_ind]]
for i in range(n):
if i != max_ind:
ans.append(a[i])
print(*ans) | IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR 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 LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR 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 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()
s = {*range(len(a))}
for x in zip(*(f"{int(x):30b}" for x in a)):
if x.count("1") == 1:
t = {i for i, y in enumerate(x) if "0" < y}
if s & t:
s &= t
print(*([a.pop(s.pop())] + a)) | EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR STRING VAR VAR IF FUNC_CALL VAR STRING NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR STRING VAR IF BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST 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. | def decimalToBinary(n):
return bin(n).replace("0b", "")
n = int(input())
a = list(map(int, input().split()))
a.sort(reverse=True)
b = []
for i in range(n):
s = decimalToBinary(a[i])
s = "0" * (32 - len(s)) + s
b.append(s)
ans = -1
for i in range(32):
ind = -1
for j in range(n):
if b[j][i] == "1" and ind == -1:
ind = j
ans = j
elif b[j][i] == "1" and ind != -1:
ans = -1
break
if ans != -1:
break
if ans != -1:
print(a[ans], end=" ")
for i in range(n):
if i != ans:
print(a[i], end=" ")
print()
else:
print(*a) | FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR VAR STRING STRING 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 FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER IF 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 EXPR FUNC_CALL 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
def solve(a):
bit = 33
while bit >= 0:
count = 0
for item in a:
if 1 << bit & item:
count += 1
if count == 1:
break
bit -= 1
if bit < 0:
return a
result = []
skip = None
for i, item in enumerate(a):
if 1 << bit & item:
result.append(item)
skip = i
for i, item in enumerate(a):
if i != skip:
result.append(item)
return result
n = int(input())
a = [int(x) for x in input().split()]
print(" ".join(map(str, solve(a)))) | FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR LIST ASSIGN VAR NONE FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN 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 EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL 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. | def cal(x):
if x == 0:
return 1
else:
ret = 0
while x > 0:
ret += 1
x //= 2
return ret
def flip(x, nbits):
more = nbits - cal(x)
d = []
if x == 0:
d.append(0)
else:
while x > 0:
d.append(x % 2)
x //= 2
for i in range(more):
d.append(0)
ret = 0
p = 1
for i in d:
ret += (1 - i) * p
p *= 2
return ret
n = int(input())
a = list(map(int, input().split()))
if n == 1:
print(*a)
exit(0)
nbits = cal(max(a, key=cal))
pref = [flip(a[0], nbits)] * n
suf = [flip(a[-1], nbits)] * n
for i in range(1, n):
pref[i] = pref[i - 1] & flip(a[i], nbits)
for i in range(n - 2, -1, -1):
suf[i] = suf[i + 1] & flip(a[i], nbits)
v = []
for i in range(n):
if i == 0:
v.append(a[i] & suf[i + 1])
elif i == n - 1:
v.append(a[i] & pref[i - 1])
else:
v.append(a[i] & pref[i - 1] & suf[i + 1])
pos = v.index(max(v))
print(a[pos], end=" ")
for i in range(n):
if i != pos:
print(a[i], end=" ") | FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR LIST IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER RETURN 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 EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR 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 |
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()]
once = 0
twice = 2**64
for i in a:
twice |= once & i
once |= i
a = sorted([(i & ~twice, i) for i in a], reverse=True)
a = [j for i, j in a]
print(*a) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR 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. | from sys import stdin
def iinput():
return int(stdin.readline())
def sinput():
return input()
def minput():
return map(int, stdin.readline().split())
def linput():
return list(map(int, stdin.readline().split()))
n = iinput()
a = linput()
k = 1 << 31
x = -1
while k:
c = 0
for i in range(n):
if a[i] & k:
c += 1
x = i
if c == 1:
break
k >>= 1
r = a[x]
a.remove(a[x])
print(r, *a) | FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR 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. | import sys
n = int(sys.stdin.readline())
arr = list(map(int, sys.stdin.readline().split()))
l = [0] * 50
for i in range(n):
s = bin(arr[i])
a, j = 49, len(s) - 1
while j > 1:
if s[j] == "1":
l[a] += 1
a -= 1
j -= 1
ind = -1
ans = -1
for i in range(n):
s = bin(arr[i])
a, j = 49, len(s) - 1
while j > 1:
if s[j] == "1":
l[a] -= 1
a -= 1
j -= 1
a, j = 49, len(s) - 1
num, count = 0, 0
while j > 1:
if s[j] == "1" and l[a] == 0:
num += 2**count
count += 1
j -= 1
a -= 1
else:
j -= 1
a -= 1
count += 1
if num > ans:
ans = num
ind = i
a, j = 49, len(s) - 1
while j > 1:
if s[j] == "1":
l[a] += 1
a -= 1
j -= 1
b = [arr[ind]] + arr[:ind] + arr[ind + 1 :]
print(*b) | 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 BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER IF VAR VAR STRING VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER IF VAR VAR STRING VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR NUMBER IF VAR VAR STRING VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER IF VAR VAR STRING VAR VAR NUMBER VAR NUMBER VAR NUMBER 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. | import sys
reader = (line.rstrip() for line in sys.stdin)
input = reader.__next__
n = int(input())
a = list(map(int, input().split()))
digits = [[] for _ in range(32)]
for i, val in enumerate(a):
shift = 0
while val > 0:
if val & 1:
digits[shift].append(i)
val >>= 1
shift += 1
for i in reversed(range(32)):
if len(digits[i]) == 1:
idx = digits[i][0]
a[0], a[idx] = a[idx], a[0]
break
print(*a) | IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR 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 LIST VAR FUNC_CALL VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR 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. | import sys
n = int(sys.stdin.readline().split()[0])
a = list(map(int, sys.stdin.readline().split()))
b = [x for x in a]
idx = -1
while max(b) != 0:
c = [(x & 1) for x in b]
if c.count(1) == 1:
idx = c.index(1)
b = [(x >> 1) for x in b]
if idx >= 0:
sys.stdout.write(str(a[idx]) + " ")
for i in range(n):
if i != idx:
sys.stdout.write(str(a[i]) + " ")
else:
for i in range(n):
sys.stdout.write(str(a[i]) + " ") | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP 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()))
bitsum = [0] * 32
for i in a:
for j in range(32):
bitsum[j] += i >> j & 1
onebit = None
for i in range(31, -1, -1):
if bitsum[i] == 1:
onebit = i
break
if onebit is None:
print(*a)
exit(0)
for i in range(n):
if a[i] >> onebit & 1:
first = i
break
a = [a[first]] + a[:first] + a[first + 1 :]
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 BIN_OP LIST NUMBER NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NONE FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR IF VAR NONE EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR NUMBER 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())
l = list(map(int, input().split()))
cnt = [0] * 30
for i in range(n):
for j in range(30):
if l[i] & 1 << j:
cnt[j] += 1
options = [i for i in range(n)]
for i in range(29, -1, -1):
newoptions = []
if cnt[i] == 1:
for idx in options:
if l[idx] & 1 << i:
newoptions.append(idx)
if len(newoptions) > 0:
options = newoptions
print(l[options[0]], end=" ")
for i in range(n):
if i != options[0]:
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 BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST IF VAR VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR 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 |
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()))
s = set()
for i in range(32, -1, -1):
cnt = 0
bs = []
for c in arr:
if c >> i & 1:
cnt += 1
bs.append(c)
if cnt == 1 and bs[0] not in s:
print(bs[0], end=" ")
s.add(bs[0])
for c in arr:
if c not in s:
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 FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER STRING EXPR FUNC_CALL VAR VAR NUMBER 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. | t = 1
while t:
n = int(input())
l = list(map(int, input().split()))
s = [0] * (n + 1)
p = [0] * (n + 1)
p[0] = ~l[0]
for i in range(1, n):
p[i] = p[i - 1] & ~l[i]
s[n - 1] = ~l[-1]
for i in range(n - 2, -1, -1):
s[i] = s[i + 1] & ~l[i]
max_ind = 0
maxi = 0
if n == 1:
print(l[0])
else:
for i in range(n):
if i == 0:
if maxi < l[i] & s[i + 1]:
maxi = l[i] & s[i + 1]
max_ind = i
elif i == n - 1:
if maxi < p[i - 1] & l[i]:
maxi = p[i - 1] & l[i]
max_ind = i
elif maxi < p[i - 1] & l[i] & s[i + 1]:
maxi = p[i - 1] & l[i] & s[i + 1]
max_ind = i
print(l[max_ind], end=" ")
for i in range(n):
if i != max_ind:
print(l[i], end=" ")
t -= 1 | ASSIGN VAR NUMBER WHILE 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 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 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 IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR IF VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP 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 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()))
mask = 0
for i in range(31):
if [(j >> i & 1) for j in a].count(1) == 1:
mask += 1 << i
ans = [-1, -1]
for i in range(n):
if ans[0] < a[i] & mask:
ans[0] = a[i] & mask
ans[1] = i
realans = []
realans.append(a[ans[1]])
for i in range(n):
if i != ans[1]:
realans.append(a[i])
print(" ".join(map(str, realans))) | 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 IF FUNC_CALL BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER 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())
a = list(map(int, input().split()))
if n == 1:
print(a[0])
else:
pref = [0] * n
suff = [0] * n
pref[1] = ~a[0]
suff[n - 2] = ~a[-1]
for i in range(2, n):
pref[i] = pref[i - 1] & ~a[i - 1]
for i in range(n - 3, -1, -1):
suff[i] = suff[i + 1] & ~a[i + 1]
max = a[0] & suff[0]
pos = 0
for i in range(1, n):
if i == n - 1:
A = pref[i] & a[i]
else:
A = pref[i] & a[i] & suff[i]
if A > max:
max = A
pos = i
print(a[pos], end=" ")
for i in range(n):
if i != pos:
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 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 NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR 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 BIN_OP 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 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 |
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().strip())
a = list(map(int, sys.stdin.readline().strip().split()))
b = a[:]
C = [0] * n
x = [0] * n
for i in range(0, 40):
for j in range(0, n):
x[j] = b[j] % 2
b[j] = b[j] // 2
if sum(x) == 1:
for j in range(0, n):
if x[j] == 1:
C[j] = C[j] + 2**i
l = C.index(max(C))
print(" ".join(list(map(str, [a[l]] + a[0:l] + a[l + 1 :])))) | 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 VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP LIST VAR VAR VAR NUMBER 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()))
ans = []
for i in range(32, -1, -1):
flag = -1
badflag = 0
for j in range(n):
if a[j] & 1 << i:
if flag == -1:
flag = j
else:
badflag = 1
if badflag == 1:
break
if flag != -1 and badflag == 0:
ans.append(str(a[flag]))
for j in range(n):
if j != flag:
ans.append(str(a[j]))
break
if len(ans) == 0:
for i in range(n):
ans.append(str(a[i]))
print(" ".join(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 LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL 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. | import sys
input = sys.stdin.buffer.readline
n = int(input())
a = [int(i) for i in input().split()]
for i in range(34, -1, -1):
b = 1 << i
cnt = 0
idx = -1
for x in range(n):
if b & a[x] != 0:
cnt += 1
idx = x
if cnt == 1:
print(a[idx], end=" ")
for x in range(n):
if x != idx:
print(a[x], end=" ")
exit(0)
else:
print(*a) | IMPORT ASSIGN VAR VAR 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 BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR 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 EXPR FUNC_CALL 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().strip())
A = list(map(int, input().strip().split()))
A_set = list(set(A))
if len(A) == 1 or len(A_set) == 1:
print(" ".join(list(map(str, A))))
else:
m = len(A_set)
prefix = [~A_set[0]] * m
postfix = [~A_set[-1]] * m
for i in range(1, m):
prefix[i] = prefix[i - 1] & ~A_set[i]
for i in range(m - 2, -1, -1):
postfix[i] = postfix[i + 1] & ~A_set[i]
max_ans = -1
index = -1
for i in range(1, m - 1):
ans = A_set[i] & prefix[i - 1] & postfix[i + 1]
if ans > max_ans:
max_ans = ans
index = i
ans = A_set[0] & postfix[1]
if ans > max_ans:
max_ans = ans
index = 0
ans = A_set[m - 1] & prefix[m - 2]
if ans > max_ans:
max_ans = ans
index = m - 1
ans = A_set[index]
A.remove(ans)
A.insert(0, ans)
print(" ".join(list(map(str, A)))) | 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 FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR NUMBER VAR ASSIGN VAR BIN_OP LIST VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER 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 NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP 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 ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER 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
u = sys.stdin.readline
n = int(u())
a = list(map(int, u().split()))
ind = -1
for i in range(40, -1, -1):
for j in range(n):
if a[j] & 1 << i:
if ind == -1:
ind = j
else:
ind = -1
break
if ind != -1:
print(a[ind], end=" ")
break
for i in range(n):
if i != ind:
print(a[i], end=" ") | 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 NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN 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 |
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())
aa = [int(a) for a in input().split()]
counts = [0] * 30
candidate = [None] * 30
for a in aa:
i = 0
b = a
while b > 0:
if b % 2 == 1:
counts[i] += 1
if counts[i] == 1:
candidate[i] = a
else:
candidate[i] = None
b = b // 2
i += 1
for c in candidate[::-1]:
if c:
aa.remove(c)
print(c, " ".join(map(str, aa)))
break
else:
print(" ".join(map(str, aa)))
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 NUMBER ASSIGN VAR BIN_OP LIST NONE NUMBER FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NONE ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL 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. | input()
l = [0] * 30
c = [0] * 30
a = [*map(int, input().split())]
i = 0
for x in a:
for j in range(30):
if x >> 29 - j & 1:
c[j] += 1
l[j] = i
i += 1
if 1 in c:
i = l[c.index(1)]
a = [a.pop(i)] + a
print(*a) | EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER 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. | n = int(input())
array = list(map(int, input().split()))
max = 0
for i in array:
if i > max:
max = i
k = max
max = 0
while k != 0:
k = int(k / 2)
max += 1
array_of_arrays = []
for i in range(0, max):
array_of_arrays.append([])
for i in range(0, n):
k = array[i]
counter = 0
while k != 0:
rem = k % 2
if rem == 1:
array_of_arrays[counter].append(i)
k = int(k / 2)
counter += 1
for i in range(0, max):
if len(array_of_arrays[max - 1 - i]) == 1:
first = array_of_arrays[max - 1 - i][0]
temp = array[first]
array[first] = array[0]
array[0] = temp
break
print(*array) | 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 VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR 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. | from sys import stdin, stdout
n = int(stdin.readline().strip())
alist = list(map(int, stdin.readline().split()))
cnt = {}
for a in alist:
k = 1
while k <= a:
if a & k:
cnt.setdefault(k, []).append(a)
k *= 2
mx = None
for k, v in sorted(list(cnt.items()), reverse=True):
if len(v) == 1:
mx = v[0]
break
if mx:
alist.remove(mx)
alist.insert(0, mx)
stdout.write(" ".join([str(a) for a in alist])) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL 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 NUMBER WHILE VAR VAR IF BIN_OP VAR VAR EXPR FUNC_CALL FUNC_CALL VAR VAR LIST VAR VAR NUMBER ASSIGN VAR NONE FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER 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. | def h(n):
if n == 1:
return "1"
if n == 0:
return "0"
return h(n % 2) + h(n // 2)
n = int(input())
a = list(map(int, input().split()))
b = list(map(h, a))
res = 0
for i in range(31, 0, -1):
ind = -1
col = 0
for j in range(n):
if len(b[j]) >= i and b[j][i - 1] == "1":
col += 1
ind = j
if col == 1:
res = ind
break
print(a.pop(res), " ".join(map(str, a))) | FUNC_DEF IF VAR NUMBER RETURN STRING IF VAR NUMBER RETURN STRING RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP 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 FUNC_CALL VAR FUNC_CALL VAR VAR 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 FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR 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())
a = input().split()
ls = [[(0) for i in range(31)] for j in range(n)]
for i in range(n):
a[i] = int(a[i])
k = 1
while k < 32:
if a[i] & 1 << k - 1:
ls[i][k - 1] = 1
k += 1
j = 30
while j >= 0:
count = 0
store = 0
for i in range(n):
if ls[i][j]:
count += 1
store = i
if count == 1:
break
j -= 1
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 FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER 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())
li = [int(i) for i in input().split(" ")]
valid = {}
for i in li:
for p in range(31):
c = i & 2**p
if c:
valid[c] = valid.get(c, 0) + 1
s = 0
for k, v in valid.items():
if v == 1:
s += k
m = 0
a = li[0]
for i in li:
if i & s > m:
m = i & s
a = i
li.remove(a)
print(a, end=" ")
for i in li:
print(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 DICT FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING 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 func(start, b):
temp = start
for j in range(len(b)):
temp = (temp | b[j]) - b[j]
return temp
n = int(input())
a = list(map(int, input().split()))
count = []
for i in range(32):
count.append([])
for i in a:
b = bin(i)[2:].rjust(32, "0")
for j in range(32):
if b[j] == "1":
count[j].append(i)
ans = a[:]
maxi = -1
for i in range(32):
if len(count[i]) == 1:
b = a[:]
elem = count[i][0]
b.remove(elem)
f = func(elem, b)
if f > maxi:
maxi = f
ans = [elem] + b
print(*ans) | FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR 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 VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP 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())
a = list(map(int, input().split()))
c = -float("inf")
val = 0
for i in range(32, -1, -1):
ct = 0
for j in a:
v = 1 << i
if j & v > 0:
ct += 1
val = j
if ct == 1:
c = val
break
a.sort(reverse=True)
ans = []
if c > 0:
ans.append(c)
for i in a:
if i != c:
ans.append(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 ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR IF 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. | n = int(input())
a = list(map(int, input().split()))
count = 0
index = 0
for i in range(30, -1, -1):
count = 0
for j in range(0, n):
if a[j] & 1 << i:
count = count + 1
index = j
if count == 1:
break
print(a[index], *a[:index], *a[index + 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 ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER 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())
a = list(map(int, input().split()))
exist = 0
final = max(a)
ok = 2**30 - 1
for i in range(n):
tmp = a[i] ^ exist
tmp2 = a[i] & exist
ok = ok & ~tmp2
final = (final | tmp) & ok
exist = exist | a[i]
final = format(final, "b").zfill(30)
idx = 0
for i in range(30):
if final[i] == "1":
idx = i
break
for i in range(n):
if format(a[i], "b").zfill(30)[idx] == "1":
idx = i
break
print(a[idx], end=" ")
for i in range(n):
if i != idx:
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 FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL FUNC_CALL VAR VAR VAR STRING NUMBER VAR STRING 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 = list(map(int, input().split()))
m = max(a)
b = 0
while 1 << b < m:
b += 1
for i in reversed(range(b)):
s = 0
for j in range(n):
if 1 << i & a[j] != 0:
if s == 1:
break
s += 1
ans = j
else:
if s == 1:
a[0], a[ans] = a[ans], a[0]
break
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 ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER IF VAR NUMBER 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 = [int(x) for x in input().split()]
complementPrefixes = [(-1) for _ in range(n)]
complementSuffixes = [(-1) for _ in range(n)]
for i in range(n):
if i == 0:
complementPrefixes[i] = ~0
else:
complementPrefixes[i] = complementPrefixes[i - 1] & ~a[i - 1]
for i in range(n - 1, -1, -1):
if i == n - 1:
complementSuffixes[i] = ~0
else:
complementSuffixes[i] = complementSuffixes[i + 1] & ~a[i + 1]
currMax = 0
first = 0
for i in range(n):
temp = complementPrefixes[i] & a[i] & complementSuffixes[i]
if temp > currMax:
first = i
currMax = temp
ans = [a[first]]
for i in range(n):
if i != first:
ans.append(a[i])
print(" ".join([str(x) for x in ans])) | 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 VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR 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 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
n = int(input())
a = list(map(int, stdin.readline().split()))
used = [False] * n
ans = []
for k in range(30, -1, -1):
cnt1, pos1 = 0, -1
for i in range(n):
if used[i]:
continue
mask = 1 << k
if mask & a[i]:
cnt1 += 1
pos1 = i
if cnt1 == 1:
ans.append(a[pos1])
used[pos1] = True
for i in range(n):
if not used[i]:
ans.append(a[i])
for i in ans:
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 BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL 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())
numbers = list(map(int, input().split()))
candidate = 0
prefix = [0] * n
suffix = [0] * n
prefix[0] = suffix[n - 1] = 2147483647
for i in range(1, n):
prefix[i] = prefix[i - 1] & ~numbers[i - 1]
i = n - 2
while i >= 0:
suffix[i] = suffix[i + 1] & ~numbers[i + 1]
i -= 1
ans = -1
pos = -1
for i in range(n):
tmp = prefix[i] & numbers[i] & suffix[i]
if tmp > ans:
ans = tmp
pos = i
print(numbers[pos], end=" ")
for i in range(n):
if i != pos:
print(numbers[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 BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER 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 VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR 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 |
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()))
m = [0] * n
d = 0
for i in range(n):
bin_str = format(l[i], "b")
d = max(d, len(bin_str))
m[i] = bin_str
for i in range(n):
m[i] = "0" * (d - len(m[i])) + m[i]
i_ = -1
for j in range(d):
cnt = 0
for i in range(n):
if m[i][j] != "0":
i_ = i
cnt += 1
if cnt == 1:
break
if i_ != -1:
temp = l.pop(i_)
l_ = [temp] + l
ans = [str(c) for c in l_]
print(" ".join(ans))
else:
l_ = sorted(l, reverse=True)
ans = [str(c) for c in l_]
print(" ".join(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 VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER 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(""))
arr = list(map(int, input().split()))
arr.sort(reverse=True)
suff = [0] * (n + 1)
pre = [0] * n
mx = -100000000000000
ind = -1
suff[n - 1] = ~arr[n - 1]
for i in range(n - 2, -1, -1):
suff[i] = suff[i + 1] & ~arr[i]
for i in range(n):
if i == 0:
ase = ~arr[0]
now = arr[i] & suff[1]
else:
now = arr[i]
if i != n - 1:
now = arr[i] & suff[i + 1]
now = now & ase
ase = ase & ~arr[i]
if now > mx:
mx = now
ind = i
for i in range(n):
if i == 0:
ase = ~arr[0]
now = arr[i] & suff[1]
else:
now = arr[i]
if i != n - 1:
now = arr[i] & suff[i + 1]
now = now & ase
ase = ase & ~arr[i]
if now > mx:
mx = now
ind = i
temp = arr[ind]
arr[ind] = arr[0]
arr[0] = temp
print(*arr) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR 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 FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR 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. | def main():
n = int(input())
ints = list(map(int, input().split(" ")))
bins = []
for i in range(31):
bins.append([])
for num in ints:
s = bin(num)[2:]
prefix = "0" * (31 - len(s))
s = prefix + s
i = 0
while i <= 30:
if s[i] == "1":
bins[i].append(num)
i += 1
i = 0
found = False
while i <= 30:
if len(bins[i]) == 1:
to_print = bins[i][0]
found = True
break
i += 1
if found:
answer = str(to_print) + " "
for num in ints:
if num != to_print:
answer += str(num) + " "
else:
answer = ""
for num in ints:
answer += str(num) + " "
print(answer)
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 STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP STRING BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR STRING FOR VAR VAR IF VAR VAR VAR BIN_OP FUNC_CALL VAR VAR STRING ASSIGN VAR STRING FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR STRING 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. | def main():
n = int(input())
aa = list(map(int, input().split()))
ll = [0] * n
rr = [0] * n
s = 0
for i in range(n):
s |= aa[i]
ll[i] = s
s = 0
for i in range(n - 1, -1, -1):
s |= aa[i]
rr[i] = s
mx = 0
mxi = 0
for i in range(n):
s = 0
if i - 1 >= 0:
s |= ll[i - 1]
if i + 1 < n:
s |= rr[i + 1]
cur = (aa[i] | s) - s
if cur > mx:
mx = cur
mxi = i
aa[0], aa[mxi] = aa[mxi], aa[0]
print(*aa)
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 VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR 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 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())
XX = list(map(int, input().split()))
X = ["{:030b}".format(x) for x in XX]
for i in range(30):
xj = None
for j, x in enumerate(X):
if x[i] == "1":
if xj != None:
xj = None
break
xj = j
else:
if xj != None:
break
if xj == None:
print(*XX)
else:
R = [XX[xj]]
for i in range(N):
if i == xj:
continue
R.append(XX[i])
print(*R) | 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 STRING VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NONE FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NONE ASSIGN VAR NONE ASSIGN VAR VAR IF VAR NONE IF VAR NONE EXPR 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 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. | y = input
z = print
r = range
n = int(y())
l = list(map(int, y().split()))
d = {}
for i in l:
b = bin(i)[:1:-1]
for j in r(len(b)):
if int(b[j]):
d[j] = d.get(j, [])
d[j].append(i)
for i in r(31, -1, -1):
if len(d.get(i, [])) == 1:
v = d[i][0]
print(v, end=" ")
k = 0
for j in l:
if j == v and k < 1:
k += 1
else:
z(j, end=" ")
quit()
z(" ".join(map(str, l))) | ASSIGN VAR VAR ASSIGN VAR VAR 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 DICT FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR LIST NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL 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. | def solve():
n = int(input())
a = list(map(int, input().split()))
idx = 0
bit = 30
while bit >= 0:
count = 0
for i, num in enumerate(a):
if num >> bit & 1 == 1:
count += 1
idx = i
if count > 1:
break
if count == 1:
break
bit -= 1
print(f"{a[idx]} ", end="")
del a[idx]
print(" ".join(map(str, a)))
def main():
solve()
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 NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING STRING VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_DEF EXPR FUNC_CALL 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. | def solution():
n = int(input())
l = list(map(int, input().split()))
nl = []
pre = [~l[0]]
suff = []
for i in range(n):
x = ~l[i]
nl.append(x)
pre.append(x)
suff.append(x)
suff.append(~l[-1])
for i in range(1, n + 1):
pre[i] &= pre[i - 1]
for i in range(n - 1, -1, -1):
suff[i] &= suff[i + 1]
m = -1
for i in range(n):
if i == 0:
x = l[i] & suff[i + 1]
elif i == n - 1:
x = l[i] & pre[i]
else:
x = l[i] & suff[i + 1] & pre[i]
if x > m:
m = x
j = i
l[0], l[j] = l[j], l[0]
print(*l)
solution() | 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 LIST ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR BIN_OP VAR 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 VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER 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 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
t = 1
input = sys.stdin.readline
while t > 0:
t -= 1
n = int(input())
a = [int(x) for x in input().split()]
d = {}
l = [(0) for i in range(40)]
for i in range(n):
u = bin(a[i])[2:]
for k in range(len(u)):
if u[k] == "1":
l[len(u) - k - 1] += 1
a.sort(reverse=True)
maxi = -1
for k in range(39, -1, -1):
for i in range(n):
if a[i] & 1 << k > 0 and l[k] == 1:
maxi = a[i]
break
if maxi != -1:
break
if maxi == -1:
maxi = a[0]
a.remove(maxi)
print(maxi, *a) | IMPORT ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER 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. | n = int(input())
a = list(map(int, input().split()))
ans = 0
f = [(0) for i in range(n)]
b = [(0) for i in range(n)]
for i in range(n):
f[i] = a[i] | (f[i - 1] if i - 1 >= 0 else 0)
for i in range(n - 1, -1, -1):
b[i] = a[i] | (b[i + 1] if i + 1 < n else 0)
index = 0
for i in range(n):
left = (f[i - 1] if i - 1 >= 0 else 0) | (b[i + 1] if i + 1 < n else 0)
temp = a[i] - (a[i] & left)
ans = max(temp, ans)
if temp == ans:
index = i
print(a[index], end=" ")
for i in range(n):
if i != index:
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 NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF 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 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. | get_bin = lambda x: format(int(x), "b").zfill(32)
n = int(input())
seq = list(map(int, input().split()))
s = list(map(get_bin, seq))
arr = [[] for i1 in range(32)]
for i1 in range(32):
for i2 in range(n):
if s[i2][i1] == "1":
arr[i1].append(i2)
result = None
for i1 in arr:
if len(i1) == 1:
result = i1[0]
break
if result is None:
print(*seq)
else:
print(seq[result], *seq[0:result], *seq[result + 1 : n]) | ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR STRING 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 FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NONE FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NONE EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER 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())
arrr = [int(j) for j in input().split()]
arr = arrr[:]
for i in range(n):
arr[i] = bin(arr[i])[2:]
arr[i] = (30 - len(arr[i])) * "0" + arr[i]
fflag = 0
ans = -1
for i in range(30):
flag = 0
ttemp = 0
for j in range(n):
if arr[j][i] == "1":
flag += 1
if flag == 1 and ttemp == 0:
ttemp = 1
ans = j
if flag == 2:
break
if flag == 1:
fflag = 1
break
if fflag == 0:
print(*arrr)
else:
kek = [arrr[ans]]
for i in range(n):
if i != ans:
kek += [arrr[i]]
print(*kek) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR VAR STRING VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN 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())
a = list(map(int, input().split()))
l = [bin(i)[2:].zfill(32) for i in a]
ind = -1
for i in range(32):
c = 0
flag = False
for j in range(n):
if l[j][i] == "1":
if flag:
ind = -1
flag = False
break
flag = True
ind = j
if flag:
break
if ind == -1:
print(*a)
else:
print(a[ind], *(a[i] for i in range(n) if i != ind)) | 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 NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING IF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR 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())
s = list(map(int, input().split()))
dig = [0] * 50
record = [None] * 50
for x in s:
i = 0
tmp = x
while tmp != 0:
if tmp % 2 == 1:
record[i] = x
dig[i] += 1
tmp //= 2
i += 1
ans = []
head = None
for i in range(40, -1, -1):
if dig[i] == 1:
head = record[i]
ans.append(head)
break
for x in s:
if x != head:
ans.append(x)
for i, x in enumerate(ans):
ans[i] = str(x)
print(" ".join(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 ASSIGN VAR BIN_OP LIST NONE NUMBER FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NONE FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL 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. | t = int(input())
a = list(map(int, input().split()))
if t == 1:
print(a[0])
else:
b = [(~i) for i in a]
pos = [0] * t
pre = [0] * t
pre[0] = b[0]
pos[t - 1] = b[-1]
for i in range(1, t):
pre[i] = pre[i - 1] & b[i]
pos[t - i - 1] = pos[t - i] & b[t - i - 1]
mmax = 0
ans = 0
for i in range(t):
if i == 0:
temp = pos[i + 1]
if mmax < temp & a[i]:
mmax = temp & a[i]
ans = i
elif i == t - 1:
temp = pre[i - 1]
if mmax < temp & a[i]:
mmax = temp & a[i]
ans = i
else:
temp = pre[i - 1] & pos[i + 1]
if mmax < temp & a[i]:
mmax = temp & a[i]
ans = i
a[0], a[ans] = a[ans], a[0]
print(*a, sep=" ") | 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 BIN_OP 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 VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR VAR VAR VAR VAR NUMBER 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 digit(n, d):
return n % 2 ** (d + 1) // 2**d
def get_ans(nums):
for d in range(30, -1, -1):
s = 0
b = None
for i, n in enumerate(nums):
if digit(n, d):
s += 1
b = i
if s == 1:
return [nums[b]] + nums[:b] + nums[b + 1 :]
return nums
input()
nums = list(map(int, input().strip().split()))
nums = get_ans(nums)
print(" ".join(map(str, nums))) | FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NONE FOR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER RETURN BIN_OP BIN_OP LIST VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN 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. | import sys
n = int(input())
arr = list(map(int, input().split()))
pref = [0] * n
suff = [0] * n
pref[0], suff[n - 1] = sys.maxsize, sys.maxsize
for i in range(1, n):
pref[i] = pref[i - 1] & ~arr[i - 1]
for i in range(n - 2, -1, -1):
suff[i] = suff[i + 1] & ~arr[i + 1]
ans = -sys.maxsize - 1
temp = 0
for i in range(n):
temp = pref[i] & arr[i] & suff[i]
if temp > ans:
ans = temp
pos = i
print(arr[pos], *arr[:pos], *arr[pos + 1 :]) | 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 BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR 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 BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR 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 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 = list(map(int, input().split(" ")))
arr.sort()
arr.reverse()
binarr = list(map(lambda s: str(bin(s))[2:], arr))
l = len(binarr[0])
start = 0
placed = []
placed2 = {}
for pos in range(l):
yes = None
for a in binarr:
a = a.zfill(l)
if a[pos] == "1" and yes == None:
yes = a
elif a[pos] == "1" and yes != None:
yes = None
break
if yes != None:
if not placed2.get(yes):
placed.append(yes)
placed2[yes] = True
placed = list(map(lambda s: int(s, 2), placed))
for p in placed:
print(p, end=" ")
for a in arr:
if a not in placed:
print(a, end=" ") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NONE FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NONE ASSIGN VAR VAR IF VAR VAR STRING VAR NONE ASSIGN VAR NONE IF VAR NONE IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FOR VAR 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. | def pro(x, pos):
global cou
if x == 0:
return
y = 1
z = 0
while y <= x:
y *= 2
z += 1
y //= 2
z -= 1
while z != -1:
if x >= y:
x -= y
if cou[z] == -1:
cou[z] = pos
else:
cou[z] = -2
y //= 2
z -= 1
n = int(input())
mas = list(map(int, input().split()))
cou = []
for i in range(30):
cou.append(-1)
for i in range(n):
pro(mas[i], i)
ans = -1
for i in range(29, -1, -1):
if cou[i] >= 0:
ans = cou[i]
break
if ans != -1:
print(mas[ans], end=" ")
for i in range(n):
if i != ans:
print(mas[i], end=" ")
print() | FUNC_DEF IF VAR NUMBER RETURN ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER 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 FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR 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 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())
s = [int(x) for x in input().split()]
pos = [0] * 32
for i in range(0, len(s)):
t = bin(s[i])
t = t[2:]
t = "0" * (32 - len(t)) + t
for j in range(0, len(t)):
pos[j] += int(t[j])
ptr = -1
for i in range(0, len(pos)):
if pos[i] == 1:
ptr = i
break
if ptr == -1:
print(*s)
else:
for i in range(0, len(s)):
t = bin(s[i])
t = t[2:]
t = "0" * (32 - len(t)) + t
if t[ptr] == "1":
temp = s[0]
s[0] = s[i]
s[i] = temp
print(*s) | 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 FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP NUMBER FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP NUMBER FUNC_CALL VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN 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())
a = list(map(int, input().split()))
a.sort(reverse=True)
freq = {}
can = 0
for msb in range(32):
for i in a:
if i & 1 << msb > 0:
if freq.get(msb) == None:
freq[msb] = [i]
else:
freq[msb].append(i)
k = list(freq.keys())
k.sort(reverse=True)
can = -float("inf")
flag = False
for i in k:
ll = freq[i]
if len(ll) == 1:
can = ll[0]
flag = True
break
if flag:
ans = [can]
else:
ans = []
for i in a:
if can != i:
ans.append(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 DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NONE ASSIGN VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR LIST VAR ASSIGN VAR LIST FOR VAR VAR IF 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. | n = int(input())
a = list(map(int, input().split()))
scale = 30
btc = [(10**6) for _ in range(scale)]
for i in range(n):
x = a[i]
for d in range(scale):
if x % 2 == 1:
if btc[d] == 10**6:
btc[d] = i
else:
btc[d] = 10**6 + 1
x //= 2
btc.reverse()
for y in btc:
if y < 10**6:
tmp = y
ans = [a[y]]
for i in range(n):
if i != y:
ans.append(a[i])
break
else:
ans = a
print(" ".join(list(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 NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR IF VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR LIST VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN 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. | __author__ = "tanunia"
n = int(input())
a = list(map(int, input().split()))
left, right = [(0) for _ in range(len(a))], [(0) for _ in range(len(a))]
for i in range(1, len(a)):
left[i] = left[i - 1] | a[i - 1]
right[len(a) - i - 1] = right[len(a) - i] | a[len(a) - i]
best, best_ind = -1, -1
for i in range(len(a)):
y = left[i] | right[i]
cur = (a[i] | y) - y
if cur > best:
best, best_ind = cur, i
a[0], a[best_ind] = a[best_ind], a[0]
print(" ".join([str(x) for x in a])) | ASSIGN VAR STRING 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 FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL 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 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
sys.stdin.readline()
nums = list(map(int, sys.stdin.readline().split()))
bitRecords = [0] * 32
occurrences = [None] * 32
for n in nums:
for i in range(32):
if n & 1 << i != 0:
bitRecords[i] += 1
if bitRecords[i] == 1:
occurrences[i] = n
firstNum = nums[0]
for i in reversed(range(32)):
if bitRecords[i] == 1:
firstNum = occurrences[i]
break
nums.remove(firstNum)
nums.insert(0, firstNum)
print(*nums) | IMPORT EXPR 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 BIN_OP LIST NONE NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR 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. | from sys import stdin, stdout
get_string = lambda: stdin.readline().strip("\n")
get_intmap = lambda: map(int, get_string().split(" "))
def testcase():
n = int(input())
a = list(get_intmap())
not_mask = (1 << 32) - 1
a_not = tuple([(not_mask ^ i) for i in a])
dp_left, dp_right = [0] * n, [0] * n
dp_left[0], dp_right[-1] = a_not[0], a_not[-1]
for ind in range(1, n):
dp_left[ind] = dp_left[ind - 1] & a_not[ind]
for ind in range(n - 2, -1, -1):
dp_right[ind] = dp_right[ind + 1] & a_not[ind]
max_ind = -1
max_val = -1
for ind in range(n):
left = not_mask if ind == 0 else dp_left[ind - 1]
right = not_mask if ind == n - 1 else dp_right[ind + 1]
tmp = left & a[ind] & right
if tmp > max_val:
max_val = tmp
max_ind = ind
a.insert(0, a.pop(max_ind))
print(" ".join([str(i) for i in a]))
testcase()
quit()
for t in range(int(input())):
testcase() | ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER 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 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 NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER 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 EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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())
nums = [int(x) for x in input().split()]
bin_nums = [bin(x)[2:] for x in nums]
for pos in range(N):
length = len(bin_nums[pos])
bin_nums[pos] = "0" * (32 - length) + bin_nums[pos]
ind = 0
for bit in range(32):
one = 0
for pos in range(N):
if bin_nums[pos][bit] == "1":
one += 1
ind = pos
if one == 1:
break
output = [nums[ind]]
nums.remove(nums[ind])
output += nums
output = [str(x) for x in output]
print(" ".join(output)) | 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 VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP STRING BIN_OP NUMBER VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR 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 = list(map(int, input().split()))
k = 1 << 31
X = -1
while k:
T = []
for i in range(N):
if A[i] & k:
T.append(i)
if len(T) == 1:
X = T[0]
break
else:
k >>= 1
if X < 0:
print(*A)
else:
ret = A[X]
A.pop(X)
print(ret, *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 BIN_OP NUMBER NUMBER ASSIGN VAR 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 ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR 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. | import sys
readline = sys.stdin.readline
N = int(readline())
A = list(map(int, readline().split()))
M = (1 << 32) - 1
nA = [(M ^ a) for a in A]
ar = [M] + nA[:] + [M]
al = ar[:]
for i in range(1, N + 2):
al[i] &= al[i - 1]
for i in range(N, -1, -1):
ar[i] &= ar[i + 1]
d = -1
ans = None
for i in range(N):
a = A[i]
res = a & al[i] & ar[i + 2]
if res > d:
d = res
ans = i
Ans = A[:]
Ans.remove(A[ans])
print(*([A[ans]] + 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 BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST VAR VAR LIST VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST 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()))
w = []
for i in range(33):
w.append([])
for i in range(n):
h = a[i]
for j in range(30, -1, -1):
if 2**j <= h:
h -= 2**j
w[j].append(a[i])
else:
continue
t = 0
for i in range(33):
if len(w[32 - i]) == 1:
print(w[32 - i][0], end=" ")
a.remove(w[32 - i][0])
break
else:
t += 1
if t == 33:
print(*a)
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 ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP NUMBER VAR VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP NUMBER VAR NUMBER STRING EXPR FUNC_CALL VAR VAR BIN_OP NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER 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. | n = int(input())
l = list(map(int, input().split()))
bitcount = [0] * 64
for v in l:
i = 0
while v:
if v & 1:
bitcount[i] += 1
v >>= 1
i += 1
bitstart = 0
for i in range(64):
if bitcount[i] == 1:
bitstart = i
start = 0
for v in l:
if 1 << bitstart & v:
start = v
break
print(" ".join(map(str, sorted(l, key=lambda x: abs(x - start))))) | 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 ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP 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()]
a.sort(reverse=True)
mask = 2**30
while mask and len([ai for ai in a if ai & mask]) != 1:
mask >>= 1
for i in range(n):
if mask & a[i]:
break
a[i], a[0] = a[0], a[i]
print(*a) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF 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. | import sys
def main():
import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int, input().split()))
for j in range(32, -1, -1):
flg = 0
for i, a in enumerate(A):
if a >> j & 1:
flg += 1
idx = i
if flg == 2:
break
if flg == 1:
A[0], A[idx] = A[idx], A[0]
print(*A)
exit()
print(*A)
main() | IMPORT FUNC_DEF 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 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 IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL 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. | onbits = [[] for i in range(33)]
n = int(input())
nums = [int(i) for i in input().split()]
for i in range(n):
temp = bin(nums[i])[2:][::-1]
for j in range(len(temp)):
if temp[j] == "1":
onbits[j].append(i)
onbits = onbits[::-1]
for i in onbits:
if len(i) == 1:
nums[0], nums[i[0]] = nums[i[0]], nums[0]
break
print(" ".join([str(i) for i in nums])) | ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER 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 VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER 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. | def calc(s):
b = bin(s)[2:]
b = "0" * (x - len(b)) + b
c = ""
for i in b:
if i == "1":
c += "0"
else:
c += "1"
return int(c, 2)
n = int(input())
a = list(map(int, input().split()))
x = len(bin(max(a))[2:])
if n == 1:
print(a[0])
exit()
p = [calc(a[0])]
s = [0] * n
s[-1] = calc(a[-1])
for i in range(1, n):
p.append(p[-1] & calc(a[i]))
for i in range(n - 2, -1, -1):
s[i] = s[i + 1] & calc(a[i])
maxx = a[0] & s[1]
ind = 0
for i in range(1, n - 1):
if p[i - 1] & a[i] & s[i + 1] > maxx:
maxx = p[i - 1] & a[i] & s[i + 1]
ind = i
if p[-2] & a[-1] > maxx:
maxx = p[-2] & a[-1]
ind = n - 1
print(a[ind], *(a[0:ind] + a[ind + 1 :])) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR STRING FOR VAR VAR IF VAR STRING VAR STRING VAR STRING RETURN FUNC_CALL VAR 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 FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER 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()))
bits = [0] * 65
for i in a:
s = bin(i)[2:]
s = s[::-1]
for i1 in range(len(s)):
if s[i1] == "1":
bits[i1] += 1
ans = [-1, -1]
for i2 in range(len(a)):
i = a[i2]
unique = 0
s = bin(i)[2:]
s = s[::-1]
summ = 0
sd = 1
for i1 in range(len(s)):
if s[i1] == "1" and bits[i1] == 1:
unique += 1
summ += sd
sd *= 2
if summ >= ans[0]:
ans = [summ, i2]
print(a[ans[1]], end=" ")
for i in range(len(a)):
if i != ans[1]:
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 BIN_OP LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER 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. | I = lambda: int(input())
IL = lambda: list(map(int, input().split()))
n = I()
A = IL()
smask = 0
for i in range(32):
mask = 1 << i
c = sum(a & mask > 0 for a in A)
if c > 1:
smask += mask
B = [(a - (a & smask)) for a in A]
ma = max(B)
mi = [i for i, a in enumerate(B) if a == ma][0]
mA = A.pop(mi)
print(mA, *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 ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR 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. | n = int(input())
num = input().split()
for i in range(n):
num[i] = int(num[i])
finals = []
l = [0] * n
for i in range(32, -1, -1):
t = 0
k = 0
for j in range(n):
a = num[j]
if (a >> i) % 2 == 1 and l[j] != 1:
k = j
t += 1
if t == 1:
finals.append(num[k])
l[k] = 1
for i in range(n):
if l[i] != 1:
finals.append(num[i])
for i in range(n):
print(finals[i], end=" ") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR 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. | import sys
n = int(sys.stdin.readline().split()[0])
a = list(map(int, sys.stdin.readline().split()))
b = [x for x in a]
idx = -1
while max(b) != 0:
c = [(x & 1) for x in b]
if c.count(1) == 1:
idx = c.index(1)
b = [(x >> 1) for x in b]
if idx >= 0:
tmp = a[idx]
a.remove(tmp)
print(tmp, *a)
else:
print(*a) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL 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. | def f(n):
pw = 30
k = ""
for j in range(pw):
if n >> j & 1:
k = "1" + k
else:
k = "0" + k
return k
n = int(input())
a = input().split(" ")
bit = []
used = []
for i in range(30):
used.append([])
for i in range(n):
bit.append(f(int(a[i])))
for j in range(len(bit[i])):
if bit[i][j] == "1":
used[j].append(i)
for l in range(30):
if len(used[l]) != 1:
continue
a[0], a[used[l][0]] = a[used[l][0]], a[0]
break
ans = " ".join(a)
print(ans) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR BIN_OP STRING VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL STRING 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. | sp = lambda: map(int, input().split())
si = lambda: int(input())
n = si()
rawa = list(sp())
a = list(map(lambda x: list(map(int, format(x, "030b"))), rawa))
bitcount = [0] * 30
for x in a:
for b in range(30):
bitcount[b] += x[b]
bitcount2 = 0
for bit in bitcount:
bitcount2 <<= 1
bitcount2 += bit == 1
maxv = -1
maxi = -1
for i, x in enumerate(rawa):
if x & bitcount2 > maxv:
maxv = x & bitcount2
maxi = i
print(" ".join(map(str, [rawa[maxi]] + rawa[:maxi] + rawa[maxi + 1 :]))) | ASSIGN VAR 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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR STRING VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER VAR VAR NUMBER 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 EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR BIN_OP BIN_OP LIST 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. | def solve():
n = int(input())
arr = list(map(int, input().split()))
x_a = [[] for _ in range(31)]
for i in range(n):
for x in range(31):
if arr[i] & 1 << x:
x_a[x].append(i)
m_i = 0
for x in range(30, -1, -1):
if len(x_a[x]) == 1:
m_i = x_a[x][0]
break
new_arr = [str(arr[m_i])]
for i in range(n):
if i != m_i:
new_arr.append(str(arr[i]))
print(" ".join(new_arr))
solve() | 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 LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR 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 ASSIGN VAR LIST FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING 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()))
bitFreq = [0] * 32
for i in a:
for j in range(0, 32):
if i & 1 << j > 0:
bitFreq[j] += 1
currBest = 0
currIn = 0
for i in range(len(a)):
currScore = 0
for j in range(0, 32):
if a[i] & 1 << j > 0 and bitFreq[j] == 1:
currScore = currScore | 1 << j
if currScore > currBest:
currBest = currScore
currIn = i
tup = a[0], a[currIn]
a[currIn], a[0] = tup
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 BIN_OP LIST NUMBER NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR 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 NUMBER NUMBER IF BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR 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. | def main():
n = int(input())
a = list(map(int, input().split()))
st = 2**30
while st > 0:
ans = 0
ind = 0
for i in range(n):
if a[i] % (st * 2) >= st:
ans += 1
ind = i
if ans == 1:
a[0], a[ind] = a[ind], a[0]
print(*a)
return
st = st // 2
print(*a)
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 NUMBER NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR BIN_OP 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())
l = list(map(int, input().split()))
b = [0] * 32
for i in l:
x = bin(i)[2:]
d = 32 - len(x)
s = "0" * d + x
j = 0
while j < 32:
if s[j] == "1":
b[j] += 1
j += 1
f = -1
for i in range(len(b)):
if b[i] == 1:
f = i
break
if f == -1:
print(*l)
else:
p = []
v = set()
for i in l:
x = bin(i)[2:]
d = 32 - len(x)
s = "0" * d + x
if s[f] == "1":
p.append(i)
v.add(i)
for i in l:
if i not in v:
p.append(i)
print(*p) | 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 ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR STRING VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.