description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers — a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | t = int(input())
for i in range(t):
n, l, r, s = map(int, input().split())
x = r - l + 1
st = 0
en = 0
sub = []
dic = {}
suff = []
for i in range(1, x + 1):
st += i
sub.append(i)
en += n - i + 1
if s >= st and s <= en:
diff = s - st
a = diff // x
b = diff % x
for i in range(x - 1, -1, -1):
sub[i] += a
if b > 0:
sub[i] += 1
b -= 1
for i in sub:
dic[i] = 1
y = r
for i in range(1, n + 1):
if y < n:
if i in dic:
continue
else:
sub.append(i)
dic[i] = 1
y += 1
y = 0
for i in range(1, n + 1):
if y < l:
if i in dic:
continue
else:
suff.append(i)
dic[i] = 1
y += 1
ans = suff + sub
print(*ans)
else:
print(-1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers — a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | for _ in range(int(input())):
n, l, r, s = map(int, input().split())
values = r - l + 1
min_sum = values * (values + 1) // 2
max_sum = min_sum + (n - values) * values
if s < min_sum or s > max_sum:
print(-1)
continue
subarray = [i for i in range(1, r - l + 2)]
s = s - min_sum
max_num = n - values
for i in range(len(subarray) - 1, -1, -1):
if s >= max_num:
subarray[i] += max_num
s -= max_num
else:
subarray[i] += s
break
nums = set(subarray)
other_nums = list(set(range(1, n + 1)) - nums)
for i in range(l - 1):
print(other_nums[i], end=" ")
for item in nums:
print(item, end=" ")
for i in range(l - 1, len(other_nums)):
print(other_nums[i], end=" ")
print("") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR STRING |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers — a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | import sys
for _ in range(int(sys.stdin.readline())):
n, l, r, s = map(int, sys.stdin.readline().split())
k = r - l + 1
low_sum, high_sum = sum(range(1, k + 1)), sum(range(n - k + 1, n + 1))
if s < low_sum or high_sum < s:
print(-1)
continue
idx, limit = k, n
temp = [i for i in range(k + 1)]
while sum(temp) != s:
temp[idx] += 1
if temp[idx] == limit:
idx -= 1
limit -= 1
ans, idx, num = [], 1, 1
for i in range(1, n + 1):
if l <= i <= r:
ans.append(temp[idx])
idx += 1
continue
while num in temp:
num += 1
ans.append(num)
num += 1
print(*ans) | IMPORT FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers — a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | from sys import stdin, stdout
input = stdin.readline
for _ in range(int(input())):
n, l, r, s = map(int, input().split())
i = r - l + 1
mi = i * (i + 1) // 2
mx = n * (n + 1) // 2 - (n - i) * (n - i + 1) // 2
if s < mi or s > mx:
print(-1)
continue
right = n - 1
left = right - i + 1
lis = [i for i in range(1, n + 1)]
flag = 0
while left > 0:
sm = sum(lis[left : right + 1])
if sm == s:
ans = lis[left : right + 1]
break
i = left - 1
for j in range(left, right + 1):
if sm - lis[j] + lis[i] == s:
lis[i], lis[j] = lis[j], lis[i]
ans = lis[left : right + 1]
flag = 1
break
if flag == 1:
break
left -= 1
right -= 1
if left == 0:
ans = lis[left : right + 1]
dic = {}
for i in range(1, n + 1):
dic[i] = 0
for i in ans:
dic[i] = 1
temp_lis = [x for x in range(1, n + 1) if dic[x] == 0]
final_ans = []
for i in range(1, n + 1):
if i < l or i > r:
final_ans.append(temp_lis.pop(0))
else:
final_ans.append(ans.pop(0))
print(*final_ans) | ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers — a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | def get_numbbbs(maximum, k, total):
nums = list(range(1, k + 1))
s = sum(nums)
if s > total:
return -1
pointer = -1
while s < total:
if nums[pointer] == maximum + pointer + 1:
pointer -= 1
else:
nums[pointer] += 1
s += 1
if -pointer > len(nums):
return -1
return nums
for cycle in range(int(input())):
n, l, r, s = [int(x) for x in input().split()]
a = get_numbbbs(n, r - l + 1, s)
if a == -1:
print(a)
else:
not_nuzhno = [x for x in range(1, n + 1) if x not in a]
print(*not_nuzhno[: l - 1], *a, *not_nuzhno[l - 1 :]) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers — a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | from sys import stderr, stdin
input = stdin.readline
def dbp(*args, **kwargs):
print(*args, file=stderr, **kwargs)
def get_int_list():
return [int(x) for x in input().strip().split()]
def do_thing():
n, l, r, s = get_int_list()
l -= 1
r -= 1
sec = []
for i in range(1, r - l + 2):
sec.append(i)
secsum = sum(sec)
if secsum > s:
return -1
gap = s - secsum
step, m = divmod(gap, len(sec))
for i in range(len(sec)):
if i < len(sec) - m:
sec[i] += step
else:
sec[i] += step + 1
if sec[-1] > n:
return -1
taken = set(sec)
remains = [i for i in range(1, n + 1) if i not in taken]
return " ".join(str(x) for x in remains[:l] + sec + remains[l:])
def multicase():
maxcc = int(input().strip())
for cc in range(maxcc):
print(do_thing())
multicase() | ASSIGN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR RETURN FUNC_CALL STRING FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers — a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | for _ in range(int(input())):
n, l, r, s = map(int, input().split())
k = r - l + 1
rem = set()
out = []
for i in range(n, 0, -1):
low = k * (k - 1) // 2
high = k * (2 * i - k + 1) // 2
if k and high >= s and s - i >= low:
out.append(i)
s -= i
k -= 1
else:
rem.add(i)
if s == 0:
left = []
right = []
i = 1
for num in rem:
if i < l:
left.append(num)
i += 1
else:
right.append(num)
out = left + out + right
print(*out)
else:
print(-1) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers — a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | def f(a, b):
ans = 0
for i in range(a, b + 1):
ans += i
return ans
for tc in range(int(input())):
n, l, r, s = map(int, input().split())
leng = r - l + 1
if f(1, leng) > s or f(n - leng + 1, n) < s:
print(-1)
continue
lr = [i for i in range(n - leng + 1, n + 1)]
summ = sum(lr)
t = 0
while summ > s:
summ -= lr[t]
summ += t + 1
lr[t] = t + 1
t += 1
t -= 1
while summ < s:
lr[t] += 1
summ += 1
book = [(0) for i in range(n + 5)]
for i in lr:
book[i] = 1
t = 1
for i in range(l - 1):
while book[t] == 1:
t += 1
print(t, end=" ")
book[t] = 1
print(" ".join([str(i) for i in lr]), end=" ")
for i in range(n - r):
while book[t] == 1:
t += 1
print(t, end=" ")
book[t] = 1
print() | FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR VAR WHILE VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers — a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | for y in range(int(input())):
n, l, r, s = map(int, input().split())
ln = r - l + 1
if ln * (ln + 1) // 2 <= s <= n * (n + 1) // 2 - (n - ln) * (n - ln + 1) // 2:
lst = [0] * ln
for i in range(ln):
lst[i] = i + 1
s -= sum(lst)
lst = lst + [10000000000]
p = ln - 1
while s > 0:
if lst[p] < n and lst[p] + 1 < lst[p + 1]:
lst[p] += 1
s -= 1
else:
p -= 1
vis = [0] * (n + 1)
k = 0
for i in range(ln):
vis[lst[k]] = 1
k += 1
ans = [0] + [0] * (l - 1) + lst[:-1] + [0] * (n - r)
k = 1
for i in range(1, l):
while k <= n and vis[k] == 1:
k += 1
ans[i] = k
k += 1
for i in range(r + 1, n + 1):
while k <= n and vis[k] == 1:
k += 1
ans[i] = k
k += 1
print(*ans[1:])
else:
print("-1") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR LIST NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP LIST NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR WHILE VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers — a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | t = int(input())
for _ in range(t):
n, l, r, s = list(map(int, input().split()))
d = r - l + 1
mi = d * (d + 1) // 2
ma = sum(n - x for x in range(d))
if not mi <= s <= ma:
print(-1)
continue
offset = 0
current = [(x + 1) for x in range(d)][::-1]
i = 0
while sum(current) < s and i < d:
dif = s - sum(current)
ne = min(n - offset, current[i] + dif)
current[i] = ne
i += 1
offset += 1
out = [0] * n
avail = {x for x in range(1, n + 1)}
avail = list(avail.difference(set(current)))
out[l - 1 : r] = current
curr = 0
for i, x in enumerate(out):
if x == 0:
out[i] = avail[curr]
curr += 1
print(*out) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers — a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | for _ in range(int(input())):
n, l, r, s = map(int, input().split())
a = [(i + 1) for i in range(n)]
d = r - l + 1
low = sum(a[:d])
high = sum(a[-d:])
if s > high or s < low:
print(-1)
else:
t = a[-d:]
x = 0
for i in range(d):
if high - s > 0:
if t[i] - (i + 1) <= high - s:
high -= t[i] - (i + 1)
t[i] = i + 1
else:
t[i] -= high - s
break
else:
break
check = [False] * (n + 1)
ans = [0] * n
j = 0
for i in range(l - 1, r):
ans[i] = t[j]
check[t[j]] = True
j += 1
j = 1
for i in range(n):
if ans[i] == 0:
while check[j]:
j += 1
ans[i] = j
check[j] = True
print(*ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers — a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | for _ in range(int(input())):
n, s, e, su = list(map(int, input().split()))
lis = []
for i in range(1, n + 1):
lis.append(i)
le = e - s + 1
if sum(lis[len(lis) - le :]) < su or sum(lis[:le]) > su:
print(-1)
continue
new = []
for i in lis[:le]:
new.append(i)
ad = sum(new)
need = su - ad
per = need // le
extra = need - per * le
m = lis[-1]
for i in range(le):
new[i] += per
if extra > 0:
for i in range(le - 1, le - extra - 1, -1):
new[i] += 1
final = lis.copy()
for i in new:
final.remove(i)
final = final[: s - 1] + new + final[s - 1 :]
string = ""
for i in final:
string += str(i) + " "
print(string) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR STRING FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers — a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | for _ in range(int(input())):
n, l, r, s = [int(x) for x in input().split(" ")]
k = abs(l - r) + 1
mn = k * (k + 1) / 2
mx = mn + (n - k) * k
if mn <= s and s <= mx:
a = [i for i in range(k)]
a.append(n)
curr = mn
ptr = k - 1
while curr != s:
if a[ptr] + 1 != a[ptr + 1]:
a[ptr] += 1
curr += 1
else:
ptr -= 1
a.pop()
assert len(a) == k
for i in range(len(a)):
a[i] += 1
sa = set(a)
others = []
assert len(sa) == len(a)
for i in range(1, n + 1):
if not i in sa:
others.append(i)
for i in range(l - 1):
print(others[i], end=" ")
for x in a:
print(x, end=" ")
for i in range(len(others) - (n - r), len(others)):
print(others[i], end=" ")
print()
else:
print(-1) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers — a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | def check(l, r, s, num):
tmp1 = (2 * l + num - 1) * num // 2
tmp2 = (2 * r - num + 1) * num // 2
return s >= tmp1 and s <= tmp2
cas = int(input())
while cas:
cas -= 1
n, l, r, s = map(int, input().split())
a = [(0) for i in range(n + 1)]
vis = [(0) for i in range(n + 1)]
cnt = l
for i in range(1, n + 1):
if cnt == r and s <= n and s >= 1:
a[cnt] = s
vis[s] = 1
s = 0
break
if check(i + 1, n, s - i, r - cnt):
a[cnt] = i
vis[i] = 1
cnt += 1
s -= i
if s:
print("-1")
else:
pos = 1
for i in range(1, l):
while vis[pos]:
pos += 1
a[i] = pos
pos += 1
for i in range(r + 1, n + 1):
while vis[pos]:
pos += 1
a[i] = pos
pos += 1
for i in range(1, n + 1):
if i == n:
print(a[i])
else:
print(a[i], end=" ") | FUNC_DEF ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER RETURN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR VAR IF VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers — a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | for _ in range(int(input())):
n, l, r, s = map(int, input().split())
arr = []
t = []
visited = set()
cnt = 0
warr = []
for i in range(1, n + 1):
if i > s:
t.append(i)
continue
arr.append(i)
if cnt < r - l + 1:
visited.add(i)
warr.append(i)
cnt += 1
som = sum(arr[0 : r - l + 1])
if sum(arr[-(r - l + 1) :]) < s or som > s or len(arr) < r - l + 1:
print(-1)
continue
flag = 0
for i in range(len(warr)):
for j in range(len(arr)):
if som == s:
flag = 1
break
if som - warr[i] + arr[j] > som and som < s and arr[j] not in visited:
e1 = warr[i]
e2 = arr[j]
visited.remove(warr[i])
visited.add(arr[j])
som -= warr[i]
som += arr[j]
warr[i] = e2
continue
if som > s and som - warr[i] + arr[j] < som and arr[j] not in visited:
e1 = warr[i]
e2 = arr[j]
visited.remove(warr[i])
visited.add(arr[j])
som -= warr[i]
som += arr[j]
warr[i] = e2
if som == s:
flag = 1
break
if flag:
break
par = []
for i in arr:
if i not in visited:
par.append(i)
par = par + t
ans = [0] * n
for i in range(n):
if i < r and i >= l - 1:
ans[i] = warr.pop()
else:
ans[i] = par.pop()
print(*ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers — a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | import sys
input = sys.stdin.readline
for CASES in range(int(input())):
n, l, r, s = map(int, input().split())
l -= 1
r -= 1
le = r - l + 1
now = (1 + le) * le // 2
if now > s:
print(-1)
continue
A = [0] * n
for i in range(l, r + 1):
A[i] = i - l + 1
for i in range(r, l - 1, -1):
delta = n + i - r - A[i]
if now + delta <= s:
A[i] += delta
now += delta
else:
A[i] += s - now
now += s - now
break
if now != s:
print(-1)
continue
B = set(A)
tmp = 1
for i in range(len(A)):
if A[i] == 0:
while tmp in B:
tmp += 1
B.add(tmp)
A[i] = tmp
print(*A) | IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER WHILE VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers — a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | class Solution:
def get_n_sum(self, n):
return n * (n + 1) // 2
def get_last_n_sum(self, n, k):
return k * (2 * n + 1 - k) // 2
def solve(self, n, l, r, s):
nsum = self.get_n_sum(n)
if s > nsum:
return -1
k = r - l + 1
low = self.get_n_sum(k)
high = self.get_last_n_sum(n, k)
if s < low or s > high:
return -1
result = [(-1) for _ in range(n)]
lr_set = set()
count = l - 1
for i in range(n, 0, -1):
if k > 0:
k_ = k - 1
high = k * (2 * i + 1 - k) // 2
low = k_ * (k_ + 1) // 2
if high >= s and s - i >= low:
result[count] = i
lr_set.add(i)
count += 1
s -= i
k -= 1
count = 1
for i in range(n):
if result[i] == -1:
while True:
if count in lr_set:
count += 1
else:
break
result[i] = count
count += 1
return " ".join(str(r) for r in result)
sol = Solution()
tests = int(input())
results = []
for _ in range(tests):
n, l, r, s = list(map(int, input().split()))
result = sol.solve(n, l, r, s)
results.append(result)
for result in results:
print(result) | CLASS_DEF FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER WHILE NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER RETURN FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers — a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | T = int(input())
for _ in range(T):
n, l, r, s = [int(x) for x in input().split()]
ma = (2 * n - r + l) * (r - l + 1) // 2
mi = (2 - l + r) * (r - l + 1) // 2
if s > ma or s < mi:
print(-1)
else:
x = int((2 * s / (r - l + 1) + l - r) / 2)
dad = s - int((2 * x + r - l) * (r - l + 1) / 2)
arr = [0] * n
for i in range(l - 1, r):
arr[i] = x + (i - l + 1)
cnt = 0
for i in list(range(r - l + 1))[::-1]:
add = min(n - cnt - arr[i + l - 1], dad)
cnt += 1
arr[i + l - 1] += add
dad -= add
if not dad:
break
sett = set(arr)
cur = 1
for i in range(0, l - 1):
while cur in sett:
cur += 1
arr[i] = cur
cur += 1
for i in range(r, n):
while cur in sett:
cur += 1
arr[i] = cur
cur += 1
print(" ".join([str(x) for x in arr])) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers — a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | rn = lambda: int(input())
rns = lambda: map(int, input().split())
rl = lambda: list(map(int, input().split()))
rs = lambda: input()
YN = lambda x: print("YES") if x else print("NO")
mod = 10**9 + 7
for _ in range(rn()):
n, l, r, s = rns()
diff = r - l + 1
lo = diff * (diff + 1) // 2
hi = lo + diff * (n - diff)
if lo <= s <= hi:
ans = list(range(n - diff + 1, n + 1))
i = 0
sub = hi - s
while sub > 0:
if ans[i] == i + 1:
i += 1
else:
ans[i] -= 1
sub -= 1
h = set(ans)
pre = []
suff = []
for j in range(1, n + 1):
if j not in h:
if len(pre) < l - 1:
pre.append(j)
else:
suff.append(j)
print(*(pre + ans + suff))
else:
print(-1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers — a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | for _ in range(int(input())):
n, l, r, s = map(int, input().split())
z = n
k = r - l + 1
if s < (k * k + k) // 2 or s > k * (2 * n + 1 - k) // 2:
print(-1)
else:
a = []
s -= (k * k + k) // 2
for i in range(k, 0, -1):
a.append(i)
i = 0
while s > 0:
m = min(n - a[i], s)
s -= m
n -= 1
a[i] += m
i += 1
d = {i: (0) for i in range(1, 1 + z)}
for i in a:
d[i] = 1
ans = []
b = [i for i in d if d[i] == 0]
ans = b[: l - 1]
ans.extend(a)
ans.extend(b[l - 1 :])
print(*ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers — a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | t = int(input())
for z in range(t):
n, l, r, s = map(int, input().split())
p = r - l + 1
mn = p * (p + 1) // 2
mx = n * (n + 1) // 2 - (n - p) * (n - p + 1) // 2
if s < mn or s > mx:
print(-1)
else:
brr = []
for i in range(p, 0, -1):
brr.append(i)
k = s - mn
i = 0
while k:
brr[i % len(brr)] += 1
k = k - 1
i = i + 1
arr = [0] * n
arr[l - 1 : r] = brr
p = 0
for i in range(1, n + 1):
if i not in arr:
if p == l - 1:
p = r
arr[p] = i
p = p + 1
print(*arr) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers — a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | for i in range(int(input())):
n, l, r, s = map(int, input().split())
re = n * (n + 1) // 2
t = r - l + 1
b = [0] * n
a = [0] * (r - l + 1)
su = 0
d = dict()
d1 = dict()
for i in range(1, n + 1):
d[i] = 0
d1[i] = 0
f = 0
for i in range(0, r - l + 1):
a[i] = i + 1
su += i + 1
if s > re or su > s:
f = 1
elif su == s:
for i in range(l - 1, r):
d[a[i - l + 1]] = 1
b[i] = a[i - l + 1]
val = 1
for i in range(n):
if i < r and i >= l - 1:
continue
while d[val]:
val += 1
b[i] = val
d[val] = 1
if b[i] > n:
f = 1
break
else:
while su < s:
for i in range(t - 1, -1, -1):
if s - su > 0:
s -= 1
a[i] += 1
if a[i] > n:
f = 1
else:
break
d1[1] = 0
for i in range(l - 1, r):
b[i] = a[i - l + 1]
d1[a[i - l + 1]] = 1
val1 = 1
for i in range(0, n):
if i < r and i >= l - 1:
continue
while d1[val1]:
val1 += 1
b[i] = val1
d1[val1] = 1
if b[i] > n:
f = 1
break
if f == 1:
print(-1)
else:
print(*b) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers — a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | def rec(s, l, arr):
arr_ = arr[:l]
if sum(arr_) == s:
return arr_
while arr_[-1] < arr[-1]:
for i in range(l - 1, -1, -1):
arr_[i] += 1
if sum(arr_) == s:
return arr_
return None
def solve():
n, l, r, s = map(int, input().split(" "))
l -= 1
r -= 1
re = rec(s, r - l + 1, [(i + 1) for i in range(n)])
if re == None:
print(-1)
return
st = list(set([(i + 1) for i in range(n)]) - set(re))
res = [None] * n
i = 0
for j in range(l):
res[j] = st[i]
i += 1
for j in range(r + 1, n):
res[j] = st[i]
i += 1
i = 0
for j in range(l, r + 1):
res[j] = re[i]
i += 1
print(*res)
for t in range(int(input())):
solve() | FUNC_DEF ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR RETURN VAR WHILE VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN VAR RETURN NONE FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR NONE EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers — a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | def helper(n, l, r, s):
count = r - l + 1
low = (1 + count) * count // 2
high = (n + n - count + 1) * count // 2
if s < low or s > high:
return []
res = [0] * n
used = set(list())
tl = 1
tr = n
while l < r:
if s <= (tl + tr) * count / 2:
res[l - 1] = tl
tl += 1
used.add(res[l - 1])
s -= res[l - 1]
l += 1
count -= 1
else:
res[r - 1] = tr
tr -= 1
used.add(res[r - 1])
s -= res[r - 1]
r -= 1
count -= 1
res[l - 1] = s
used.add(res[l - 1])
curr = 1
for i in range(n):
if res[i] != 0:
continue
while curr in used:
curr += 1
res[i] = curr
used.add(curr)
return res
t = int(input())
for i in range(t):
n, l, r, s = map(int, input().split(" "))
res = helper(n, l, r, s)
if not res:
print("-1")
else:
print(" ".join(list(map(str, res)))) | FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR RETURN LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers — a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | t = int(input())
for _ in range(t):
n, l, r, s = [int(i) for i in input().split()]
arr = [(-1) for i in range(n)]
l -= 1
r -= 1
fill = r - l + 1
max_possi = 0
m = n
for i in range(fill):
max_possi += m
m -= 1
min_possi = fill * (fill + 1) // 2
if s > max_possi or s < min_possi:
print(-1)
else:
m = n
for i in range(r, l - 1, -1):
arr[i] = m
m -= 1
rem = max_possi - s
start = 1
for i in range(l, r + 1):
if rem == 0:
break
avail = arr[i] - start
if rem >= avail:
rem -= avail
arr[i] = start
else:
avail -= rem
arr[i] = start + avail
rem = 0
start += 1
visited = [0] * (n + 1)
for i in range(l, r + 1):
visited[arr[i]] = 1
available = []
for i in range(1, n + 1):
if visited[i] == 0:
available.append(i)
for i in range(l):
arr[i] = available.pop(0)
for i in range(r + 1, n):
arr[i] = available.pop(0)
print(*arr) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
A permutation is a sequence of $n$ integers from $1$ to $n$, in which all the numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was given four integers $n$, $l$, $r$ ($1 \le l \le r \le n)$ and $s$ ($1 \le s \le \frac{n (n+1)}{2}$) and asked to find a permutation $p$ of numbers from $1$ to $n$ that satisfies the following condition:
$s = p_l + p_{l+1} + \ldots + p_r$.
For example, for $n=5$, $l=3$, $r=5$, and $s=8$, the following permutations are suitable (not all options are listed):
$p = [3, 4, 5, 2, 1]$;
$p = [5, 2, 4, 3, 1]$;
$p = [5, 2, 1, 3, 4]$.
But, for example, there is no permutation suitable for the condition above for $n=4$, $l=1$, $r=1$, and $s=5$.
Help Polycarp, for the given $n$, $l$, $r$, and $s$, find a permutation of numbers from $1$ to $n$ that fits the condition above. If there are several suitable permutations, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow.
Each test case consist of one line with four integers $n$ ($1 \le n \le 500$), $l$ ($1 \le l \le n$), $r$ ($l \le r \le n$), $s$ ($1 \le s \le \frac{n (n+1)}{2}$).
It is guaranteed that the sum of $n$ for all input data sets does not exceed $500$.
-----Output-----
For each test case, output on a separate line:
$n$ integers — a permutation of length $n$ that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
-----Examples-----
Input
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
Output
1 2 3 4 5
-1
1 3 2
1 2
-1
-----Note-----
None | def somme(n, k, s):
if s < k * (k + 1) // 2:
return -1
if s > n * (n + 1) // 2 - (n - k) * (n - k + 1) // 2:
return -1
l = []
rs = s
rk = k
for i in range(k):
rk -= 1
x = min(n - i, rs - rk * (rk + 1) // 2)
rs -= x
l.append(x)
return l
for kT in range(int(input())):
[n, l, r, s] = list(map(int, input().split()))
f = somme(n, r - l + 1, s)
if f == -1:
print(-1)
else:
rep = []
j = 1
for i in range(l - 1):
while j in f:
j += 1
rep.append(str(j))
j += 1
rep += map(str, f)
for i in range(r + 1, n + 1):
while j in f:
j += 1
rep.append(str(j))
j += 1
print(" ".join(rep)) | FUNC_DEF IF VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER IF VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN LIST VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
Ashish has a string $s$ of length $n$ containing only characters 'a', 'b' and 'c'.
He wants to find the length of the smallest substring, which satisfies the following conditions:
Length of the substring is at least $2$
'a' occurs strictly more times in this substring than 'b'
'a' occurs strictly more times in this substring than 'c'
Ashish is busy planning his next Codeforces round. Help him solve the problem.
A string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
-----Input-----
The first line contains a single integer $t$ $(1 \le t \le 10^{5})$ — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer $n$ $(2 \le n \le 10^{6})$ — the length of the string $s$.
The second line of each test case contains a string $s$ consisting only of characters 'a', 'b' and 'c'.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^{6}$.
-----Output-----
For each test case, output the length of the smallest substring which satisfies the given conditions or print $-1$ if there is no such substring.
-----Examples-----
Input
3
2
aa
5
cbabb
8
cacabccc
Output
2
-1
3
-----Note-----
Consider the first test case. In the substring "aa", 'a' occurs twice, while 'b' and 'c' occur zero times. Since 'a' occurs strictly more times than 'b' and 'c', the substring "aa" satisfies the condition and the answer is $2$. The substring "a" also satisfies this condition, however its length is not at least $2$.
In the second test case, it can be shown that in none of the substrings of "cbabb" does 'a' occur strictly more times than 'b' and 'c' each.
In the third test case, "cacabccc", the length of the smallest substring that satisfies the conditions is $3$. | for _ in range(int(input())):
lsen = input()
st2 = str(input())
if st2.find("aa") != -1:
print(2)
elif st2.find("aca") != -1 or st2.find("aba") != -1:
print(3)
elif st2.find("acba") != -1 or st2.find("abca") != -1:
print(4)
elif st2.find("accabba") != -1 or st2.find("abbacca") != -1:
print(7)
else:
print(-1) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR STRING NUMBER FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR STRING NUMBER FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR STRING NUMBER FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
Ashish has a string $s$ of length $n$ containing only characters 'a', 'b' and 'c'.
He wants to find the length of the smallest substring, which satisfies the following conditions:
Length of the substring is at least $2$
'a' occurs strictly more times in this substring than 'b'
'a' occurs strictly more times in this substring than 'c'
Ashish is busy planning his next Codeforces round. Help him solve the problem.
A string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
-----Input-----
The first line contains a single integer $t$ $(1 \le t \le 10^{5})$ — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer $n$ $(2 \le n \le 10^{6})$ — the length of the string $s$.
The second line of each test case contains a string $s$ consisting only of characters 'a', 'b' and 'c'.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^{6}$.
-----Output-----
For each test case, output the length of the smallest substring which satisfies the given conditions or print $-1$ if there is no such substring.
-----Examples-----
Input
3
2
aa
5
cbabb
8
cacabccc
Output
2
-1
3
-----Note-----
Consider the first test case. In the substring "aa", 'a' occurs twice, while 'b' and 'c' occur zero times. Since 'a' occurs strictly more times than 'b' and 'c', the substring "aa" satisfies the condition and the answer is $2$. The substring "a" also satisfies this condition, however its length is not at least $2$.
In the second test case, it can be shown that in none of the substrings of "cbabb" does 'a' occur strictly more times than 'b' and 'c' each.
In the third test case, "cacabccc", the length of the smallest substring that satisfies the conditions is $3$. | t = int(input())
for q in range(t):
n = int(input())
s = input()
if "aa" in s:
print(2)
elif "aba" in s or "aca" in s:
print(3)
elif "abca" in s or "acba" in s:
print(4)
elif (
"abcabca" in s
or "acbacba" in s
or "abbacca" in s
or "abcacba" in s
or "acbabc" in s
or "accabba" in s
):
print(7)
else:
print(-1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF STRING VAR EXPR FUNC_CALL VAR NUMBER IF STRING VAR STRING VAR EXPR FUNC_CALL VAR NUMBER IF STRING VAR STRING VAR EXPR FUNC_CALL VAR NUMBER IF STRING VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
Ashish has a string $s$ of length $n$ containing only characters 'a', 'b' and 'c'.
He wants to find the length of the smallest substring, which satisfies the following conditions:
Length of the substring is at least $2$
'a' occurs strictly more times in this substring than 'b'
'a' occurs strictly more times in this substring than 'c'
Ashish is busy planning his next Codeforces round. Help him solve the problem.
A string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
-----Input-----
The first line contains a single integer $t$ $(1 \le t \le 10^{5})$ — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer $n$ $(2 \le n \le 10^{6})$ — the length of the string $s$.
The second line of each test case contains a string $s$ consisting only of characters 'a', 'b' and 'c'.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^{6}$.
-----Output-----
For each test case, output the length of the smallest substring which satisfies the given conditions or print $-1$ if there is no such substring.
-----Examples-----
Input
3
2
aa
5
cbabb
8
cacabccc
Output
2
-1
3
-----Note-----
Consider the first test case. In the substring "aa", 'a' occurs twice, while 'b' and 'c' occur zero times. Since 'a' occurs strictly more times than 'b' and 'c', the substring "aa" satisfies the condition and the answer is $2$. The substring "a" also satisfies this condition, however its length is not at least $2$.
In the second test case, it can be shown that in none of the substrings of "cbabb" does 'a' occur strictly more times than 'b' and 'c' each.
In the third test case, "cacabccc", the length of the smallest substring that satisfies the conditions is $3$. | t = int(input())
for i in range(t):
l = input()
s = input()
if "aa" in s:
print(2)
elif "aba" in s:
print(3)
elif "aca" in s:
print(3)
elif "abca" in s:
print(4)
elif "acba" in s:
print(4)
elif "accabba" in s:
print(7)
elif "abbacca" in s:
print(7)
else:
print(-1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF STRING VAR EXPR FUNC_CALL VAR NUMBER IF STRING VAR EXPR FUNC_CALL VAR NUMBER IF STRING VAR EXPR FUNC_CALL VAR NUMBER IF STRING VAR EXPR FUNC_CALL VAR NUMBER IF STRING VAR EXPR FUNC_CALL VAR NUMBER IF STRING VAR EXPR FUNC_CALL VAR NUMBER IF STRING VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
Ashish has a string $s$ of length $n$ containing only characters 'a', 'b' and 'c'.
He wants to find the length of the smallest substring, which satisfies the following conditions:
Length of the substring is at least $2$
'a' occurs strictly more times in this substring than 'b'
'a' occurs strictly more times in this substring than 'c'
Ashish is busy planning his next Codeforces round. Help him solve the problem.
A string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
-----Input-----
The first line contains a single integer $t$ $(1 \le t \le 10^{5})$ — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer $n$ $(2 \le n \le 10^{6})$ — the length of the string $s$.
The second line of each test case contains a string $s$ consisting only of characters 'a', 'b' and 'c'.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^{6}$.
-----Output-----
For each test case, output the length of the smallest substring which satisfies the given conditions or print $-1$ if there is no such substring.
-----Examples-----
Input
3
2
aa
5
cbabb
8
cacabccc
Output
2
-1
3
-----Note-----
Consider the first test case. In the substring "aa", 'a' occurs twice, while 'b' and 'c' occur zero times. Since 'a' occurs strictly more times than 'b' and 'c', the substring "aa" satisfies the condition and the answer is $2$. The substring "a" also satisfies this condition, however its length is not at least $2$.
In the second test case, it can be shown that in none of the substrings of "cbabb" does 'a' occur strictly more times than 'b' and 'c' each.
In the third test case, "cacabccc", the length of the smallest substring that satisfies the conditions is $3$. | import sys
l = {"aa": 2, "aba": 3, "aca": 3, "abca": 4, "acba": 4, "abbacca": 7, "accabba": 7}
for _ in range(int(sys.stdin.readline())):
n = int(sys.stdin.readline())
s = sys.stdin.readline()
for x in l:
if x in s:
print(l[x])
break
else:
print(-1) | IMPORT ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER |
Ashish has a string $s$ of length $n$ containing only characters 'a', 'b' and 'c'.
He wants to find the length of the smallest substring, which satisfies the following conditions:
Length of the substring is at least $2$
'a' occurs strictly more times in this substring than 'b'
'a' occurs strictly more times in this substring than 'c'
Ashish is busy planning his next Codeforces round. Help him solve the problem.
A string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
-----Input-----
The first line contains a single integer $t$ $(1 \le t \le 10^{5})$ — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer $n$ $(2 \le n \le 10^{6})$ — the length of the string $s$.
The second line of each test case contains a string $s$ consisting only of characters 'a', 'b' and 'c'.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^{6}$.
-----Output-----
For each test case, output the length of the smallest substring which satisfies the given conditions or print $-1$ if there is no such substring.
-----Examples-----
Input
3
2
aa
5
cbabb
8
cacabccc
Output
2
-1
3
-----Note-----
Consider the first test case. In the substring "aa", 'a' occurs twice, while 'b' and 'c' occur zero times. Since 'a' occurs strictly more times than 'b' and 'c', the substring "aa" satisfies the condition and the answer is $2$. The substring "a" also satisfies this condition, however its length is not at least $2$.
In the second test case, it can be shown that in none of the substrings of "cbabb" does 'a' occur strictly more times than 'b' and 'c' each.
In the third test case, "cacabccc", the length of the smallest substring that satisfies the conditions is $3$. | t = int(input())
for p in range(t):
n = int(input())
s = input()
ans = 0
if "aa" in s:
ans = 2
elif "aba" in s:
ans = 3
elif "aca" in s:
ans = 3
elif "abca" in s:
ans = 4
elif "acba" in s:
ans = 4
elif "abbacca" in s:
ans = 7
elif "accabba" in s:
ans = 7
if ans == 0:
print("-1")
else:
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF STRING VAR ASSIGN VAR NUMBER IF STRING VAR ASSIGN VAR NUMBER IF STRING VAR ASSIGN VAR NUMBER IF STRING VAR ASSIGN VAR NUMBER IF STRING VAR ASSIGN VAR NUMBER IF STRING VAR ASSIGN VAR NUMBER IF STRING VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR |
Ashish has a string $s$ of length $n$ containing only characters 'a', 'b' and 'c'.
He wants to find the length of the smallest substring, which satisfies the following conditions:
Length of the substring is at least $2$
'a' occurs strictly more times in this substring than 'b'
'a' occurs strictly more times in this substring than 'c'
Ashish is busy planning his next Codeforces round. Help him solve the problem.
A string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
-----Input-----
The first line contains a single integer $t$ $(1 \le t \le 10^{5})$ — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer $n$ $(2 \le n \le 10^{6})$ — the length of the string $s$.
The second line of each test case contains a string $s$ consisting only of characters 'a', 'b' and 'c'.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^{6}$.
-----Output-----
For each test case, output the length of the smallest substring which satisfies the given conditions or print $-1$ if there is no such substring.
-----Examples-----
Input
3
2
aa
5
cbabb
8
cacabccc
Output
2
-1
3
-----Note-----
Consider the first test case. In the substring "aa", 'a' occurs twice, while 'b' and 'c' occur zero times. Since 'a' occurs strictly more times than 'b' and 'c', the substring "aa" satisfies the condition and the answer is $2$. The substring "a" also satisfies this condition, however its length is not at least $2$.
In the second test case, it can be shown that in none of the substrings of "cbabb" does 'a' occur strictly more times than 'b' and 'c' each.
In the third test case, "cacabccc", the length of the smallest substring that satisfies the conditions is $3$. | pairs = ["aa", "aba", "aca", "abca", "acba", "accabba", "abbacca"]
for s in [*open(0)][2::2]:
for i in pairs:
if i in s:
print(len(i))
break
else:
print(-1) | ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING FOR VAR LIST FUNC_CALL VAR NUMBER NUMBER NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
Ashish has a string $s$ of length $n$ containing only characters 'a', 'b' and 'c'.
He wants to find the length of the smallest substring, which satisfies the following conditions:
Length of the substring is at least $2$
'a' occurs strictly more times in this substring than 'b'
'a' occurs strictly more times in this substring than 'c'
Ashish is busy planning his next Codeforces round. Help him solve the problem.
A string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
-----Input-----
The first line contains a single integer $t$ $(1 \le t \le 10^{5})$ — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer $n$ $(2 \le n \le 10^{6})$ — the length of the string $s$.
The second line of each test case contains a string $s$ consisting only of characters 'a', 'b' and 'c'.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^{6}$.
-----Output-----
For each test case, output the length of the smallest substring which satisfies the given conditions or print $-1$ if there is no such substring.
-----Examples-----
Input
3
2
aa
5
cbabb
8
cacabccc
Output
2
-1
3
-----Note-----
Consider the first test case. In the substring "aa", 'a' occurs twice, while 'b' and 'c' occur zero times. Since 'a' occurs strictly more times than 'b' and 'c', the substring "aa" satisfies the condition and the answer is $2$. The substring "a" also satisfies this condition, however its length is not at least $2$.
In the second test case, it can be shown that in none of the substrings of "cbabb" does 'a' occur strictly more times than 'b' and 'c' each.
In the third test case, "cacabccc", the length of the smallest substring that satisfies the conditions is $3$. | t = int(input())
for i in range(t):
n = int(input())
s = input()
b = False
allowed = ["aa", "aba", "aca", "abca", "acba", "abbacca", "accabba"]
for i in allowed:
if i in s:
print(len(i))
b = True
break
if not b:
print(-1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER |
Ashish has a string $s$ of length $n$ containing only characters 'a', 'b' and 'c'.
He wants to find the length of the smallest substring, which satisfies the following conditions:
Length of the substring is at least $2$
'a' occurs strictly more times in this substring than 'b'
'a' occurs strictly more times in this substring than 'c'
Ashish is busy planning his next Codeforces round. Help him solve the problem.
A string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
-----Input-----
The first line contains a single integer $t$ $(1 \le t \le 10^{5})$ — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer $n$ $(2 \le n \le 10^{6})$ — the length of the string $s$.
The second line of each test case contains a string $s$ consisting only of characters 'a', 'b' and 'c'.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^{6}$.
-----Output-----
For each test case, output the length of the smallest substring which satisfies the given conditions or print $-1$ if there is no such substring.
-----Examples-----
Input
3
2
aa
5
cbabb
8
cacabccc
Output
2
-1
3
-----Note-----
Consider the first test case. In the substring "aa", 'a' occurs twice, while 'b' and 'c' occur zero times. Since 'a' occurs strictly more times than 'b' and 'c', the substring "aa" satisfies the condition and the answer is $2$. The substring "a" also satisfies this condition, however its length is not at least $2$.
In the second test case, it can be shown that in none of the substrings of "cbabb" does 'a' occur strictly more times than 'b' and 'c' each.
In the third test case, "cacabccc", the length of the smallest substring that satisfies the conditions is $3$. | import sys
input = sys.stdin.readline
def solve(n, S):
if "aa" in S:
print(2)
else:
ans = float("inf")
for i in range(1, n - 1):
if S[i - 1] == S[i + 1] == "a":
ans = min(ans, 3)
elif i < n - 2 and S[i - 1] == S[i + 2] == "a" and S[i] != S[i + 1]:
ans = min(ans, 4)
elif (
2 < i < n - 3
and S[i] == "a"
and S[i - 1] != S[i + 1]
and S[i - 3] == S[i + 3] == "a"
):
ans = min(ans, 7)
print(ans) if ans < float("inf") else print(-1)
for _ in range(int(input())):
n = int(input())
S = input().strip()
solve(n, S) | IMPORT ASSIGN VAR VAR FUNC_DEF IF STRING VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF NUMBER VAR BIN_OP VAR NUMBER VAR VAR STRING VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR VAR FUNC_CALL VAR STRING FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR |
Ashish has a string $s$ of length $n$ containing only characters 'a', 'b' and 'c'.
He wants to find the length of the smallest substring, which satisfies the following conditions:
Length of the substring is at least $2$
'a' occurs strictly more times in this substring than 'b'
'a' occurs strictly more times in this substring than 'c'
Ashish is busy planning his next Codeforces round. Help him solve the problem.
A string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
-----Input-----
The first line contains a single integer $t$ $(1 \le t \le 10^{5})$ — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer $n$ $(2 \le n \le 10^{6})$ — the length of the string $s$.
The second line of each test case contains a string $s$ consisting only of characters 'a', 'b' and 'c'.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^{6}$.
-----Output-----
For each test case, output the length of the smallest substring which satisfies the given conditions or print $-1$ if there is no such substring.
-----Examples-----
Input
3
2
aa
5
cbabb
8
cacabccc
Output
2
-1
3
-----Note-----
Consider the first test case. In the substring "aa", 'a' occurs twice, while 'b' and 'c' occur zero times. Since 'a' occurs strictly more times than 'b' and 'c', the substring "aa" satisfies the condition and the answer is $2$. The substring "a" also satisfies this condition, however its length is not at least $2$.
In the second test case, it can be shown that in none of the substrings of "cbabb" does 'a' occur strictly more times than 'b' and 'c' each.
In the third test case, "cacabccc", the length of the smallest substring that satisfies the conditions is $3$. | import sys
ans = []
t = int(sys.stdin.readline())
for _ in range(t):
n = int(sys.stdin.readline())
s = sys.stdin.readline().strip()
if s.find("aa") > -1:
ans.append("2")
elif s.find("aca") > -1 or s.find("aba") > -1:
ans.append("3")
elif s.find("abca") > -1 or s.find("acba") > -1:
ans.append("4")
elif s.find("abbacca") > -1 or s.find("accabba") > -1:
ans.append("7")
else:
ans.append("-1")
print("\n".join(ans)) | IMPORT ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR STRING NUMBER FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR STRING NUMBER FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR STRING NUMBER FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
Ashish has a string $s$ of length $n$ containing only characters 'a', 'b' and 'c'.
He wants to find the length of the smallest substring, which satisfies the following conditions:
Length of the substring is at least $2$
'a' occurs strictly more times in this substring than 'b'
'a' occurs strictly more times in this substring than 'c'
Ashish is busy planning his next Codeforces round. Help him solve the problem.
A string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
-----Input-----
The first line contains a single integer $t$ $(1 \le t \le 10^{5})$ — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer $n$ $(2 \le n \le 10^{6})$ — the length of the string $s$.
The second line of each test case contains a string $s$ consisting only of characters 'a', 'b' and 'c'.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^{6}$.
-----Output-----
For each test case, output the length of the smallest substring which satisfies the given conditions or print $-1$ if there is no such substring.
-----Examples-----
Input
3
2
aa
5
cbabb
8
cacabccc
Output
2
-1
3
-----Note-----
Consider the first test case. In the substring "aa", 'a' occurs twice, while 'b' and 'c' occur zero times. Since 'a' occurs strictly more times than 'b' and 'c', the substring "aa" satisfies the condition and the answer is $2$. The substring "a" also satisfies this condition, however its length is not at least $2$.
In the second test case, it can be shown that in none of the substrings of "cbabb" does 'a' occur strictly more times than 'b' and 'c' each.
In the third test case, "cacabccc", the length of the smallest substring that satisfies the conditions is $3$. | for _ in range(int(input())):
n = int(input())
s = input()
l = ["aa", "aba", "aca", "abca", "acba", "abbacca", "accabba"]
test = 0
for i in l:
if i in s:
test = 1
print(len(i))
break
if not test:
print(-1) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER |
Ashish has a string $s$ of length $n$ containing only characters 'a', 'b' and 'c'.
He wants to find the length of the smallest substring, which satisfies the following conditions:
Length of the substring is at least $2$
'a' occurs strictly more times in this substring than 'b'
'a' occurs strictly more times in this substring than 'c'
Ashish is busy planning his next Codeforces round. Help him solve the problem.
A string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
-----Input-----
The first line contains a single integer $t$ $(1 \le t \le 10^{5})$ — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer $n$ $(2 \le n \le 10^{6})$ — the length of the string $s$.
The second line of each test case contains a string $s$ consisting only of characters 'a', 'b' and 'c'.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^{6}$.
-----Output-----
For each test case, output the length of the smallest substring which satisfies the given conditions or print $-1$ if there is no such substring.
-----Examples-----
Input
3
2
aa
5
cbabb
8
cacabccc
Output
2
-1
3
-----Note-----
Consider the first test case. In the substring "aa", 'a' occurs twice, while 'b' and 'c' occur zero times. Since 'a' occurs strictly more times than 'b' and 'c', the substring "aa" satisfies the condition and the answer is $2$. The substring "a" also satisfies this condition, however its length is not at least $2$.
In the second test case, it can be shown that in none of the substrings of "cbabb" does 'a' occur strictly more times than 'b' and 'c' each.
In the third test case, "cacabccc", the length of the smallest substring that satisfies the conditions is $3$. | for s in [*open(0)][2::2]:
a = -1
for i in ("abbacca", "accabba", "acba", "abca", "aca", "aba", "aa"):
if i in s:
a = len(i)
print(a) | FOR VAR LIST FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR STRING STRING STRING STRING STRING STRING STRING IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Ashish has a string $s$ of length $n$ containing only characters 'a', 'b' and 'c'.
He wants to find the length of the smallest substring, which satisfies the following conditions:
Length of the substring is at least $2$
'a' occurs strictly more times in this substring than 'b'
'a' occurs strictly more times in this substring than 'c'
Ashish is busy planning his next Codeforces round. Help him solve the problem.
A string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
-----Input-----
The first line contains a single integer $t$ $(1 \le t \le 10^{5})$ — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer $n$ $(2 \le n \le 10^{6})$ — the length of the string $s$.
The second line of each test case contains a string $s$ consisting only of characters 'a', 'b' and 'c'.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^{6}$.
-----Output-----
For each test case, output the length of the smallest substring which satisfies the given conditions or print $-1$ if there is no such substring.
-----Examples-----
Input
3
2
aa
5
cbabb
8
cacabccc
Output
2
-1
3
-----Note-----
Consider the first test case. In the substring "aa", 'a' occurs twice, while 'b' and 'c' occur zero times. Since 'a' occurs strictly more times than 'b' and 'c', the substring "aa" satisfies the condition and the answer is $2$. The substring "a" also satisfies this condition, however its length is not at least $2$.
In the second test case, it can be shown that in none of the substrings of "cbabb" does 'a' occur strictly more times than 'b' and 'c' each.
In the third test case, "cacabccc", the length of the smallest substring that satisfies the conditions is $3$. | L = ["aa", "aba", "aca", "abca", "acba", "abbacca", "accabba"]
for t in range(int(input())):
n = int(input())
s = input()
ans = -1
for i in L:
if i in s:
ans = len(i)
break
print(ans) | ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Ashish has a string $s$ of length $n$ containing only characters 'a', 'b' and 'c'.
He wants to find the length of the smallest substring, which satisfies the following conditions:
Length of the substring is at least $2$
'a' occurs strictly more times in this substring than 'b'
'a' occurs strictly more times in this substring than 'c'
Ashish is busy planning his next Codeforces round. Help him solve the problem.
A string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
-----Input-----
The first line contains a single integer $t$ $(1 \le t \le 10^{5})$ — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer $n$ $(2 \le n \le 10^{6})$ — the length of the string $s$.
The second line of each test case contains a string $s$ consisting only of characters 'a', 'b' and 'c'.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^{6}$.
-----Output-----
For each test case, output the length of the smallest substring which satisfies the given conditions or print $-1$ if there is no such substring.
-----Examples-----
Input
3
2
aa
5
cbabb
8
cacabccc
Output
2
-1
3
-----Note-----
Consider the first test case. In the substring "aa", 'a' occurs twice, while 'b' and 'c' occur zero times. Since 'a' occurs strictly more times than 'b' and 'c', the substring "aa" satisfies the condition and the answer is $2$. The substring "a" also satisfies this condition, however its length is not at least $2$.
In the second test case, it can be shown that in none of the substrings of "cbabb" does 'a' occur strictly more times than 'b' and 'c' each.
In the third test case, "cacabccc", the length of the smallest substring that satisfies the conditions is $3$. | def solve():
_ = int(input())
s = input()
words = ["aa", "aba", "aca", "abca", "acba", "abbacca", "accabba"]
for word in words:
if word in s:
print(len(word))
return
print(-1)
for _ in range(int(input())):
solve() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR |
Ashish has a string $s$ of length $n$ containing only characters 'a', 'b' and 'c'.
He wants to find the length of the smallest substring, which satisfies the following conditions:
Length of the substring is at least $2$
'a' occurs strictly more times in this substring than 'b'
'a' occurs strictly more times in this substring than 'c'
Ashish is busy planning his next Codeforces round. Help him solve the problem.
A string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
-----Input-----
The first line contains a single integer $t$ $(1 \le t \le 10^{5})$ — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer $n$ $(2 \le n \le 10^{6})$ — the length of the string $s$.
The second line of each test case contains a string $s$ consisting only of characters 'a', 'b' and 'c'.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^{6}$.
-----Output-----
For each test case, output the length of the smallest substring which satisfies the given conditions or print $-1$ if there is no such substring.
-----Examples-----
Input
3
2
aa
5
cbabb
8
cacabccc
Output
2
-1
3
-----Note-----
Consider the first test case. In the substring "aa", 'a' occurs twice, while 'b' and 'c' occur zero times. Since 'a' occurs strictly more times than 'b' and 'c', the substring "aa" satisfies the condition and the answer is $2$. The substring "a" also satisfies this condition, however its length is not at least $2$.
In the second test case, it can be shown that in none of the substrings of "cbabb" does 'a' occur strictly more times than 'b' and 'c' each.
In the third test case, "cacabccc", the length of the smallest substring that satisfies the conditions is $3$. | import sys
input = sys.stdin.readline
for _ in range(int(input().strip())):
n = int(input().strip())
s = input().strip()
if "aa" in s:
print(2)
elif "aba" in s or "aca" in s:
print(3)
elif "abca" in s or "acba" in s:
print(4)
elif "abbacca" in s or "accabba" in s:
print(7)
else:
print(-1) | IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF STRING VAR EXPR FUNC_CALL VAR NUMBER IF STRING VAR STRING VAR EXPR FUNC_CALL VAR NUMBER IF STRING VAR STRING VAR EXPR FUNC_CALL VAR NUMBER IF STRING VAR STRING VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
Ashish has a string $s$ of length $n$ containing only characters 'a', 'b' and 'c'.
He wants to find the length of the smallest substring, which satisfies the following conditions:
Length of the substring is at least $2$
'a' occurs strictly more times in this substring than 'b'
'a' occurs strictly more times in this substring than 'c'
Ashish is busy planning his next Codeforces round. Help him solve the problem.
A string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
-----Input-----
The first line contains a single integer $t$ $(1 \le t \le 10^{5})$ — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer $n$ $(2 \le n \le 10^{6})$ — the length of the string $s$.
The second line of each test case contains a string $s$ consisting only of characters 'a', 'b' and 'c'.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^{6}$.
-----Output-----
For each test case, output the length of the smallest substring which satisfies the given conditions or print $-1$ if there is no such substring.
-----Examples-----
Input
3
2
aa
5
cbabb
8
cacabccc
Output
2
-1
3
-----Note-----
Consider the first test case. In the substring "aa", 'a' occurs twice, while 'b' and 'c' occur zero times. Since 'a' occurs strictly more times than 'b' and 'c', the substring "aa" satisfies the condition and the answer is $2$. The substring "a" also satisfies this condition, however its length is not at least $2$.
In the second test case, it can be shown that in none of the substrings of "cbabb" does 'a' occur strictly more times than 'b' and 'c' each.
In the third test case, "cacabccc", the length of the smallest substring that satisfies the conditions is $3$. | for _ in range(int(input())):
n = int(input())
s = input()
a = -1
pos = ["aa", "aba", "aca", "abca", "acba", "abbacca", "accabba"]
for i in pos:
if i in s:
a = len(i)
break
print(a) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Ashish has a string $s$ of length $n$ containing only characters 'a', 'b' and 'c'.
He wants to find the length of the smallest substring, which satisfies the following conditions:
Length of the substring is at least $2$
'a' occurs strictly more times in this substring than 'b'
'a' occurs strictly more times in this substring than 'c'
Ashish is busy planning his next Codeforces round. Help him solve the problem.
A string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
-----Input-----
The first line contains a single integer $t$ $(1 \le t \le 10^{5})$ — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer $n$ $(2 \le n \le 10^{6})$ — the length of the string $s$.
The second line of each test case contains a string $s$ consisting only of characters 'a', 'b' and 'c'.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^{6}$.
-----Output-----
For each test case, output the length of the smallest substring which satisfies the given conditions or print $-1$ if there is no such substring.
-----Examples-----
Input
3
2
aa
5
cbabb
8
cacabccc
Output
2
-1
3
-----Note-----
Consider the first test case. In the substring "aa", 'a' occurs twice, while 'b' and 'c' occur zero times. Since 'a' occurs strictly more times than 'b' and 'c', the substring "aa" satisfies the condition and the answer is $2$. The substring "a" also satisfies this condition, however its length is not at least $2$.
In the second test case, it can be shown that in none of the substrings of "cbabb" does 'a' occur strictly more times than 'b' and 'c' each.
In the third test case, "cacabccc", the length of the smallest substring that satisfies the conditions is $3$. | def solve():
input()
s = input().strip()
if "aa" in s:
return print(2)
elif "aba" in s or "aca" in s:
return print(3)
elif "abca" in s or "acba" in s:
print(4)
elif "abbacca" in s or "accabba" in s:
print(7)
else:
print(-1)
n = int(input())
for i in range(n):
solve() | FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF STRING VAR RETURN FUNC_CALL VAR NUMBER IF STRING VAR STRING VAR RETURN FUNC_CALL VAR NUMBER IF STRING VAR STRING VAR EXPR FUNC_CALL VAR NUMBER IF STRING VAR STRING VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Ashish has a string $s$ of length $n$ containing only characters 'a', 'b' and 'c'.
He wants to find the length of the smallest substring, which satisfies the following conditions:
Length of the substring is at least $2$
'a' occurs strictly more times in this substring than 'b'
'a' occurs strictly more times in this substring than 'c'
Ashish is busy planning his next Codeforces round. Help him solve the problem.
A string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
-----Input-----
The first line contains a single integer $t$ $(1 \le t \le 10^{5})$ — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer $n$ $(2 \le n \le 10^{6})$ — the length of the string $s$.
The second line of each test case contains a string $s$ consisting only of characters 'a', 'b' and 'c'.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^{6}$.
-----Output-----
For each test case, output the length of the smallest substring which satisfies the given conditions or print $-1$ if there is no such substring.
-----Examples-----
Input
3
2
aa
5
cbabb
8
cacabccc
Output
2
-1
3
-----Note-----
Consider the first test case. In the substring "aa", 'a' occurs twice, while 'b' and 'c' occur zero times. Since 'a' occurs strictly more times than 'b' and 'c', the substring "aa" satisfies the condition and the answer is $2$. The substring "a" also satisfies this condition, however its length is not at least $2$.
In the second test case, it can be shown that in none of the substrings of "cbabb" does 'a' occur strictly more times than 'b' and 'c' each.
In the third test case, "cacabccc", the length of the smallest substring that satisfies the conditions is $3$. | for i in range(int(input())):
num = int(input())
w = input()
for i in ("aa", "aba", "aca", "abca", "acba", "accabba", "abbacca"):
if i in w:
print(i.__len__())
break
else:
print(-1) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR STRING STRING STRING STRING STRING STRING STRING IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER |
Ashish has a string $s$ of length $n$ containing only characters 'a', 'b' and 'c'.
He wants to find the length of the smallest substring, which satisfies the following conditions:
Length of the substring is at least $2$
'a' occurs strictly more times in this substring than 'b'
'a' occurs strictly more times in this substring than 'c'
Ashish is busy planning his next Codeforces round. Help him solve the problem.
A string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
-----Input-----
The first line contains a single integer $t$ $(1 \le t \le 10^{5})$ — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer $n$ $(2 \le n \le 10^{6})$ — the length of the string $s$.
The second line of each test case contains a string $s$ consisting only of characters 'a', 'b' and 'c'.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^{6}$.
-----Output-----
For each test case, output the length of the smallest substring which satisfies the given conditions or print $-1$ if there is no such substring.
-----Examples-----
Input
3
2
aa
5
cbabb
8
cacabccc
Output
2
-1
3
-----Note-----
Consider the first test case. In the substring "aa", 'a' occurs twice, while 'b' and 'c' occur zero times. Since 'a' occurs strictly more times than 'b' and 'c', the substring "aa" satisfies the condition and the answer is $2$. The substring "a" also satisfies this condition, however its length is not at least $2$.
In the second test case, it can be shown that in none of the substrings of "cbabb" does 'a' occur strictly more times than 'b' and 'c' each.
In the third test case, "cacabccc", the length of the smallest substring that satisfies the conditions is $3$. | for i in range(int(input())):
length = int(input())
string = input()
master_list = ["aa", "aba", "aca", "abca", "acba", "abbacca", "accabba"]
ans_list = [i for i in master_list if i in string]
if len(ans_list) == 0:
print(-1)
else:
ans_list.sort(key=len)
print(len(ans_list[0])) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER |
Ashish has a string $s$ of length $n$ containing only characters 'a', 'b' and 'c'.
He wants to find the length of the smallest substring, which satisfies the following conditions:
Length of the substring is at least $2$
'a' occurs strictly more times in this substring than 'b'
'a' occurs strictly more times in this substring than 'c'
Ashish is busy planning his next Codeforces round. Help him solve the problem.
A string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
-----Input-----
The first line contains a single integer $t$ $(1 \le t \le 10^{5})$ — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer $n$ $(2 \le n \le 10^{6})$ — the length of the string $s$.
The second line of each test case contains a string $s$ consisting only of characters 'a', 'b' and 'c'.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^{6}$.
-----Output-----
For each test case, output the length of the smallest substring which satisfies the given conditions or print $-1$ if there is no such substring.
-----Examples-----
Input
3
2
aa
5
cbabb
8
cacabccc
Output
2
-1
3
-----Note-----
Consider the first test case. In the substring "aa", 'a' occurs twice, while 'b' and 'c' occur zero times. Since 'a' occurs strictly more times than 'b' and 'c', the substring "aa" satisfies the condition and the answer is $2$. The substring "a" also satisfies this condition, however its length is not at least $2$.
In the second test case, it can be shown that in none of the substrings of "cbabb" does 'a' occur strictly more times than 'b' and 'c' each.
In the third test case, "cacabccc", the length of the smallest substring that satisfies the conditions is $3$. | n_test = int(input())
ee = ["aa", "aba", "aca", "abca", "acba", "abbacca", "accabba"]
def solve(s):
for e in ee:
if s.count(e) > 0:
print(len(e))
return
print(-1)
for t in range(n_test):
n = int(input())
s = input()
solve(s) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING FUNC_DEF FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR |
Ashish has a string $s$ of length $n$ containing only characters 'a', 'b' and 'c'.
He wants to find the length of the smallest substring, which satisfies the following conditions:
Length of the substring is at least $2$
'a' occurs strictly more times in this substring than 'b'
'a' occurs strictly more times in this substring than 'c'
Ashish is busy planning his next Codeforces round. Help him solve the problem.
A string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
-----Input-----
The first line contains a single integer $t$ $(1 \le t \le 10^{5})$ — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer $n$ $(2 \le n \le 10^{6})$ — the length of the string $s$.
The second line of each test case contains a string $s$ consisting only of characters 'a', 'b' and 'c'.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^{6}$.
-----Output-----
For each test case, output the length of the smallest substring which satisfies the given conditions or print $-1$ if there is no such substring.
-----Examples-----
Input
3
2
aa
5
cbabb
8
cacabccc
Output
2
-1
3
-----Note-----
Consider the first test case. In the substring "aa", 'a' occurs twice, while 'b' and 'c' occur zero times. Since 'a' occurs strictly more times than 'b' and 'c', the substring "aa" satisfies the condition and the answer is $2$. The substring "a" also satisfies this condition, however its length is not at least $2$.
In the second test case, it can be shown that in none of the substrings of "cbabb" does 'a' occur strictly more times than 'b' and 'c' each.
In the third test case, "cacabccc", the length of the smallest substring that satisfies the conditions is $3$. | strs = ["aa", "aba", "aca", "abca", "acba", "abbacca", "accabba"]
t = int(input())
for p in range(t):
l, s = int(input()), input()
print(min([len(i) for i in strs if i in s], default=-1)) | ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER |
Ashish has a string $s$ of length $n$ containing only characters 'a', 'b' and 'c'.
He wants to find the length of the smallest substring, which satisfies the following conditions:
Length of the substring is at least $2$
'a' occurs strictly more times in this substring than 'b'
'a' occurs strictly more times in this substring than 'c'
Ashish is busy planning his next Codeforces round. Help him solve the problem.
A string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
-----Input-----
The first line contains a single integer $t$ $(1 \le t \le 10^{5})$ — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer $n$ $(2 \le n \le 10^{6})$ — the length of the string $s$.
The second line of each test case contains a string $s$ consisting only of characters 'a', 'b' and 'c'.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^{6}$.
-----Output-----
For each test case, output the length of the smallest substring which satisfies the given conditions or print $-1$ if there is no such substring.
-----Examples-----
Input
3
2
aa
5
cbabb
8
cacabccc
Output
2
-1
3
-----Note-----
Consider the first test case. In the substring "aa", 'a' occurs twice, while 'b' and 'c' occur zero times. Since 'a' occurs strictly more times than 'b' and 'c', the substring "aa" satisfies the condition and the answer is $2$. The substring "a" also satisfies this condition, however its length is not at least $2$.
In the second test case, it can be shown that in none of the substrings of "cbabb" does 'a' occur strictly more times than 'b' and 'c' each.
In the third test case, "cacabccc", the length of the smallest substring that satisfies the conditions is $3$. | t = int(input())
for _ in range(t):
n = int(input())
string = input()
if string.find("aa") != -1:
print(2)
elif string.find("aca") != -1:
print(3)
elif string.find("aba") != -1:
print(3)
elif string.find("abca") != -1:
print(4)
elif string.find("acba") != -1:
print(4)
elif string.find("accabba") != -1:
print(7)
elif string.find("abbacca") != -1:
print(7)
else:
print(-1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
Ashish has a string $s$ of length $n$ containing only characters 'a', 'b' and 'c'.
He wants to find the length of the smallest substring, which satisfies the following conditions:
Length of the substring is at least $2$
'a' occurs strictly more times in this substring than 'b'
'a' occurs strictly more times in this substring than 'c'
Ashish is busy planning his next Codeforces round. Help him solve the problem.
A string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
-----Input-----
The first line contains a single integer $t$ $(1 \le t \le 10^{5})$ — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer $n$ $(2 \le n \le 10^{6})$ — the length of the string $s$.
The second line of each test case contains a string $s$ consisting only of characters 'a', 'b' and 'c'.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^{6}$.
-----Output-----
For each test case, output the length of the smallest substring which satisfies the given conditions or print $-1$ if there is no such substring.
-----Examples-----
Input
3
2
aa
5
cbabb
8
cacabccc
Output
2
-1
3
-----Note-----
Consider the first test case. In the substring "aa", 'a' occurs twice, while 'b' and 'c' occur zero times. Since 'a' occurs strictly more times than 'b' and 'c', the substring "aa" satisfies the condition and the answer is $2$. The substring "a" also satisfies this condition, however its length is not at least $2$.
In the second test case, it can be shown that in none of the substrings of "cbabb" does 'a' occur strictly more times than 'b' and 'c' each.
In the third test case, "cacabccc", the length of the smallest substring that satisfies the conditions is $3$. | def solve(n, s):
if s.find("aa") != -1:
return 2
elif s.find("aca") != -1 or s.find("aba") != -1:
return 3
elif s.find("abca") != -1 or s.find("acba") != -1:
return 4
elif s.find("abbacca") != -1 or s.find("accabba") != -1:
return 7
else:
return -1
t = int(input())
for _ in range(t):
n = int(input())
s = input()
print(solve(n, s)) | FUNC_DEF IF FUNC_CALL VAR STRING NUMBER RETURN NUMBER IF FUNC_CALL VAR STRING NUMBER FUNC_CALL VAR STRING NUMBER RETURN NUMBER IF FUNC_CALL VAR STRING NUMBER FUNC_CALL VAR STRING NUMBER RETURN NUMBER IF FUNC_CALL VAR STRING NUMBER FUNC_CALL VAR STRING NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Ashish has a string $s$ of length $n$ containing only characters 'a', 'b' and 'c'.
He wants to find the length of the smallest substring, which satisfies the following conditions:
Length of the substring is at least $2$
'a' occurs strictly more times in this substring than 'b'
'a' occurs strictly more times in this substring than 'c'
Ashish is busy planning his next Codeforces round. Help him solve the problem.
A string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
-----Input-----
The first line contains a single integer $t$ $(1 \le t \le 10^{5})$ — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer $n$ $(2 \le n \le 10^{6})$ — the length of the string $s$.
The second line of each test case contains a string $s$ consisting only of characters 'a', 'b' and 'c'.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^{6}$.
-----Output-----
For each test case, output the length of the smallest substring which satisfies the given conditions or print $-1$ if there is no such substring.
-----Examples-----
Input
3
2
aa
5
cbabb
8
cacabccc
Output
2
-1
3
-----Note-----
Consider the first test case. In the substring "aa", 'a' occurs twice, while 'b' and 'c' occur zero times. Since 'a' occurs strictly more times than 'b' and 'c', the substring "aa" satisfies the condition and the answer is $2$. The substring "a" also satisfies this condition, however its length is not at least $2$.
In the second test case, it can be shown that in none of the substrings of "cbabb" does 'a' occur strictly more times than 'b' and 'c' each.
In the third test case, "cacabccc", the length of the smallest substring that satisfies the conditions is $3$. | import sys
input = sys.stdin.readline
def printL(list):
sys.stdout.write(" ".join(map(str, list)) + "\n")
def printN(n):
sys.stdout.write(str(n) + "\n")
t = int(input())
for _ in range(t):
n = int(input())
s = input()
b = False
for i in ("aa", "aba", "aca", "abca", "acba", "accabba", "abbacca"):
if i in s:
printN(len(i))
b = True
break
if not b:
printN(-1) | IMPORT ASSIGN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL STRING FUNC_CALL VAR VAR VAR STRING FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR STRING STRING STRING STRING STRING STRING STRING IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER |
Ashish has a string $s$ of length $n$ containing only characters 'a', 'b' and 'c'.
He wants to find the length of the smallest substring, which satisfies the following conditions:
Length of the substring is at least $2$
'a' occurs strictly more times in this substring than 'b'
'a' occurs strictly more times in this substring than 'c'
Ashish is busy planning his next Codeforces round. Help him solve the problem.
A string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
-----Input-----
The first line contains a single integer $t$ $(1 \le t \le 10^{5})$ — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer $n$ $(2 \le n \le 10^{6})$ — the length of the string $s$.
The second line of each test case contains a string $s$ consisting only of characters 'a', 'b' and 'c'.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^{6}$.
-----Output-----
For each test case, output the length of the smallest substring which satisfies the given conditions or print $-1$ if there is no such substring.
-----Examples-----
Input
3
2
aa
5
cbabb
8
cacabccc
Output
2
-1
3
-----Note-----
Consider the first test case. In the substring "aa", 'a' occurs twice, while 'b' and 'c' occur zero times. Since 'a' occurs strictly more times than 'b' and 'c', the substring "aa" satisfies the condition and the answer is $2$. The substring "a" also satisfies this condition, however its length is not at least $2$.
In the second test case, it can be shown that in none of the substrings of "cbabb" does 'a' occur strictly more times than 'b' and 'c' each.
In the third test case, "cacabccc", the length of the smallest substring that satisfies the conditions is $3$. | t = int(input())
for i in range(t):
n = int(input())
s = input()
y = 0
le = 0
if "aa" in s:
y = 1
le = 2
elif "aba" in s:
y = 1
le = 3
elif "aca" in s:
y = 1
le = 3
elif "abca" in s:
y = 1
le = 4
elif "acba" in s:
y = 1
le = 4
elif "abbacca" in s:
y = 1
le = 7
elif "accabba" in s:
y = 1
le = 7
if y == 1:
print(le)
else:
print(-1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
Ashish has a string $s$ of length $n$ containing only characters 'a', 'b' and 'c'.
He wants to find the length of the smallest substring, which satisfies the following conditions:
Length of the substring is at least $2$
'a' occurs strictly more times in this substring than 'b'
'a' occurs strictly more times in this substring than 'c'
Ashish is busy planning his next Codeforces round. Help him solve the problem.
A string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
-----Input-----
The first line contains a single integer $t$ $(1 \le t \le 10^{5})$ — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer $n$ $(2 \le n \le 10^{6})$ — the length of the string $s$.
The second line of each test case contains a string $s$ consisting only of characters 'a', 'b' and 'c'.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^{6}$.
-----Output-----
For each test case, output the length of the smallest substring which satisfies the given conditions or print $-1$ if there is no such substring.
-----Examples-----
Input
3
2
aa
5
cbabb
8
cacabccc
Output
2
-1
3
-----Note-----
Consider the first test case. In the substring "aa", 'a' occurs twice, while 'b' and 'c' occur zero times. Since 'a' occurs strictly more times than 'b' and 'c', the substring "aa" satisfies the condition and the answer is $2$. The substring "a" also satisfies this condition, however its length is not at least $2$.
In the second test case, it can be shown that in none of the substrings of "cbabb" does 'a' occur strictly more times than 'b' and 'c' each.
In the third test case, "cacabccc", the length of the smallest substring that satisfies the conditions is $3$. | t, S = int(input()), ["aa", "aba", "aca", "abca", "acba", "abbacca", "accabba"]
while t != 0:
n, T, flag, ans = input(), input(), False, 100000000
for s in S:
if s in T:
flag = True
ans = ans if ans < len(s) else len(s)
print(-1 if flag == False else ans)
t = t - 1 | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR LIST STRING STRING STRING STRING STRING STRING STRING WHILE VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER |
Ashish has a string $s$ of length $n$ containing only characters 'a', 'b' and 'c'.
He wants to find the length of the smallest substring, which satisfies the following conditions:
Length of the substring is at least $2$
'a' occurs strictly more times in this substring than 'b'
'a' occurs strictly more times in this substring than 'c'
Ashish is busy planning his next Codeforces round. Help him solve the problem.
A string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
-----Input-----
The first line contains a single integer $t$ $(1 \le t \le 10^{5})$ — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer $n$ $(2 \le n \le 10^{6})$ — the length of the string $s$.
The second line of each test case contains a string $s$ consisting only of characters 'a', 'b' and 'c'.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^{6}$.
-----Output-----
For each test case, output the length of the smallest substring which satisfies the given conditions or print $-1$ if there is no such substring.
-----Examples-----
Input
3
2
aa
5
cbabb
8
cacabccc
Output
2
-1
3
-----Note-----
Consider the first test case. In the substring "aa", 'a' occurs twice, while 'b' and 'c' occur zero times. Since 'a' occurs strictly more times than 'b' and 'c', the substring "aa" satisfies the condition and the answer is $2$. The substring "a" also satisfies this condition, however its length is not at least $2$.
In the second test case, it can be shown that in none of the substrings of "cbabb" does 'a' occur strictly more times than 'b' and 'c' each.
In the third test case, "cacabccc", the length of the smallest substring that satisfies the conditions is $3$. | t = int(input())
a = ["aa", "aba", "aca", "abca", "acba", "abbacca", "accabba"]
while t:
t -= 1
n = int(input())
s = input().strip()
ok = False
for x in a:
if x in s:
print(len(x))
ok = True
break
if not ok:
print("-1\n") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING |
Ashish has a string $s$ of length $n$ containing only characters 'a', 'b' and 'c'.
He wants to find the length of the smallest substring, which satisfies the following conditions:
Length of the substring is at least $2$
'a' occurs strictly more times in this substring than 'b'
'a' occurs strictly more times in this substring than 'c'
Ashish is busy planning his next Codeforces round. Help him solve the problem.
A string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
-----Input-----
The first line contains a single integer $t$ $(1 \le t \le 10^{5})$ — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer $n$ $(2 \le n \le 10^{6})$ — the length of the string $s$.
The second line of each test case contains a string $s$ consisting only of characters 'a', 'b' and 'c'.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^{6}$.
-----Output-----
For each test case, output the length of the smallest substring which satisfies the given conditions or print $-1$ if there is no such substring.
-----Examples-----
Input
3
2
aa
5
cbabb
8
cacabccc
Output
2
-1
3
-----Note-----
Consider the first test case. In the substring "aa", 'a' occurs twice, while 'b' and 'c' occur zero times. Since 'a' occurs strictly more times than 'b' and 'c', the substring "aa" satisfies the condition and the answer is $2$. The substring "a" also satisfies this condition, however its length is not at least $2$.
In the second test case, it can be shown that in none of the substrings of "cbabb" does 'a' occur strictly more times than 'b' and 'c' each.
In the third test case, "cacabccc", the length of the smallest substring that satisfies the conditions is $3$. | def main():
input()
string = input()
for st in ["aa", "aba", "aca", "abca", "acba", "abbacca", "accabba"]:
if st in string:
return len(st)
return -1
tests = int(input())
while tests:
tests -= 1
print(main()) | FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR LIST STRING STRING STRING STRING STRING STRING STRING IF VAR VAR RETURN FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR |
Ashish has a string $s$ of length $n$ containing only characters 'a', 'b' and 'c'.
He wants to find the length of the smallest substring, which satisfies the following conditions:
Length of the substring is at least $2$
'a' occurs strictly more times in this substring than 'b'
'a' occurs strictly more times in this substring than 'c'
Ashish is busy planning his next Codeforces round. Help him solve the problem.
A string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
-----Input-----
The first line contains a single integer $t$ $(1 \le t \le 10^{5})$ — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer $n$ $(2 \le n \le 10^{6})$ — the length of the string $s$.
The second line of each test case contains a string $s$ consisting only of characters 'a', 'b' and 'c'.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^{6}$.
-----Output-----
For each test case, output the length of the smallest substring which satisfies the given conditions or print $-1$ if there is no such substring.
-----Examples-----
Input
3
2
aa
5
cbabb
8
cacabccc
Output
2
-1
3
-----Note-----
Consider the first test case. In the substring "aa", 'a' occurs twice, while 'b' and 'c' occur zero times. Since 'a' occurs strictly more times than 'b' and 'c', the substring "aa" satisfies the condition and the answer is $2$. The substring "a" also satisfies this condition, however its length is not at least $2$.
In the second test case, it can be shown that in none of the substrings of "cbabb" does 'a' occur strictly more times than 'b' and 'c' each.
In the third test case, "cacabccc", the length of the smallest substring that satisfies the conditions is $3$. | import sys
input = sys.stdin.readline
for _ in range(int(input())):
n = int(input())
s = input().rstrip()
p = q = -1
for i in range(n):
if s[i] == "a" and p == -1:
p = i
elif s[i] == "a" and p != -1:
q = i
if q == -1:
print(-1)
continue
s = s[p : q + 1]
k = []
t = ""
for i in s:
t += i
if i == "a" and len(t) != 1:
k += [t]
t = i
k.sort(key=lambda x: len(x))
flag = 0
for i in k:
if flag:
break
if i == "aa":
print(2)
flag = 1
elif i in ["aba", "aca"]:
print(3)
flag = 1
elif i in ["abca", "acba"]:
print(4)
flag = 1
if flag:
continue
if "abbacca" in s or "accabba" in s:
print(7)
else:
print(-1) | IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR STRING FOR VAR VAR VAR VAR IF VAR STRING FUNC_CALL VAR VAR NUMBER VAR LIST VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR IF VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR LIST STRING STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR LIST STRING STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR IF STRING VAR STRING VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
Ashish has a string $s$ of length $n$ containing only characters 'a', 'b' and 'c'.
He wants to find the length of the smallest substring, which satisfies the following conditions:
Length of the substring is at least $2$
'a' occurs strictly more times in this substring than 'b'
'a' occurs strictly more times in this substring than 'c'
Ashish is busy planning his next Codeforces round. Help him solve the problem.
A string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
-----Input-----
The first line contains a single integer $t$ $(1 \le t \le 10^{5})$ — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer $n$ $(2 \le n \le 10^{6})$ — the length of the string $s$.
The second line of each test case contains a string $s$ consisting only of characters 'a', 'b' and 'c'.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^{6}$.
-----Output-----
For each test case, output the length of the smallest substring which satisfies the given conditions or print $-1$ if there is no such substring.
-----Examples-----
Input
3
2
aa
5
cbabb
8
cacabccc
Output
2
-1
3
-----Note-----
Consider the first test case. In the substring "aa", 'a' occurs twice, while 'b' and 'c' occur zero times. Since 'a' occurs strictly more times than 'b' and 'c', the substring "aa" satisfies the condition and the answer is $2$. The substring "a" also satisfies this condition, however its length is not at least $2$.
In the second test case, it can be shown that in none of the substrings of "cbabb" does 'a' occur strictly more times than 'b' and 'c' each.
In the third test case, "cacabccc", the length of the smallest substring that satisfies the conditions is $3$. | secs = ["aa", "aba", "aca", "abca", "acba", "abbacca", "accabba"]
for _ in range(int(input())):
input()
string = input()
ended = False
for j in secs:
if j in string:
print(len(j))
ended = True
break
if not ended:
print(-1) | ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER |
You are given an array $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$.
Your problem is to find such pair of indices $i, j$ ($1 \le i < j \le n$) that $lcm(a_i, a_j)$ is minimum possible.
$lcm(x, y)$ is the least common multiple of $x$ and $y$ (minimum positive number such that both $x$ and $y$ are divisors of this number).
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 10^6$) — the number of elements in $a$.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^7$), where $a_i$ is the $i$-th element of $a$.
-----Output-----
Print two integers $i$ and $j$ ($1 \le i < j \le n$) such that the value of $lcm(a_i, a_j)$ is minimum among all valid pairs $i, j$. If there are multiple answers, you can print any.
-----Examples-----
Input
5
2 4 8 3 6
Output
1 2
Input
5
5 2 11 3 7
Output
2 4
Input
6
2 5 10 1 10 2
Output
1 4 | import sys
__version__ = "3.4"
__date__ = "2019-04-17"
def find_minimum_lcm(n, integers):
already_seen = [0] * 10000001
repeated_value = 10000001 * 10000001
repeated_indices = None
for index, value in enumerate(integers):
if already_seen[value]:
if value < repeated_value:
repeated_value = value
repeated_indices = index, already_seen[value] - 1
else:
already_seen[value] = index + 1
answer_value = repeated_value
answer_indices = repeated_indices
sorted_integers = sorted(integers)
min_1, min_2 = sorted_integers[:2]
if min_1 == min_2:
return sorted(repeated_indices)
else:
factor_lcm = min_1 * min_2
if factor_lcm < answer_value:
answer_value = factor_lcm
answer_indices = already_seen[min_1] - 1, already_seen[min_2] - 1
for factor in range(2, 10000001):
if factor > answer_value:
break
seen_once = -1
for multiple in range(max(1, min_1 // factor) * factor, 10000001, factor):
if already_seen[multiple]:
if seen_once > 0:
factor_lcm = seen_once * multiple // factor
if factor_lcm < answer_value:
answer_value = factor_lcm
answer_indices = (
already_seen[seen_once] - 1,
already_seen[multiple] - 1,
)
break
else:
seen_once = multiple
return sorted(answer_indices)
def main(argv=None):
n = int(input())
integers = list(map(int, input().split()))
i, j = find_minimum_lcm(n, integers)
print("{0} {1}".format(i + 1, j + 1))
return 0
STATUS = main()
sys.exit(STATUS) | IMPORT ASSIGN VAR STRING ASSIGN VAR STRING FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NONE FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR IF VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR FUNC_DEF NONE 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 FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR |
Aleksey has $n$ friends. He is also on a vacation right now, so he has $m$ days to play this new viral cooperative game! But since it's cooperative, Aleksey will need one teammate in each of these $m$ days.
On each of these days some friends will be available for playing, and all others will not. On each day Aleksey must choose one of his available friends to offer him playing the game (and they, of course, always agree). However, if any of them happens to be chosen strictly more than $\left\lceil\dfrac{m}{2}\right\rceil$ times, then all other friends are offended. Of course, Aleksey doesn't want to offend anyone.
Help him to choose teammates so that nobody is chosen strictly more than $\left\lceil\dfrac{m}{2}\right\rceil$ times.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10000$). Description of the test cases follows.
The first line of each test case contains two integers $n$ and $m$ ($1\leq n, m\leq 100000$) standing for the number of friends and the number of days to play, respectively.
The $i$-th of the following $m$ lines contains an integer $k_i$ ($1\leq k_i\leq n$), followed by $k_i$ distinct integers $f_{i1}$, ..., $f_{ik_i}$ ($1\leq f_{ij}\leq n$), separated by spaces — indices of available friends on the day $i$.
It is guaranteed that the sums of $n$ and $m$ over all test cases do not exceed $100000$. It's guaranteed that the sum of all $k_i$ over all days of all test cases doesn't exceed $200000$.
-----Output-----
Print an answer for each test case. If there is no way to achieve the goal, print "NO".
Otherwise, in the first line print "YES", and in the second line print $m$ space separated integers $c_1$, ..., $c_m$. Each $c_i$ must denote the chosen friend on day $i$ (and therefore must be one of $f_{ij}$).
No value must occur more than $\left\lceil\dfrac{m}{2}\right\rceil$ times. If there is more than one possible answer, print any of them.
-----Examples-----
Input
2
4 6
1 1
2 1 2
3 1 2 3
4 1 2 3 4
2 2 3
1 3
2 2
1 1
1 1
Output
YES
1 2 1 1 2 3
NO
-----Note-----
None | for _ in range(int(input())):
n, m = map(int, input().split())
f = [(m // 2 + m % 2) for i in range(n)]
ans = "YES"
a = []
fl = [None] * m
for i in range(m):
a.append(([int(i) for i in input().split()], i))
a.sort()
for i in range(m):
for j in a[i][0][1:]:
if f[j - 1]:
fl[a[i][1]] = j
f[j - 1] -= 1
break
else:
ans = "NO"
print(ans)
if ans == "YES":
print(*fl) | 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 BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR |
Aleksey has $n$ friends. He is also on a vacation right now, so he has $m$ days to play this new viral cooperative game! But since it's cooperative, Aleksey will need one teammate in each of these $m$ days.
On each of these days some friends will be available for playing, and all others will not. On each day Aleksey must choose one of his available friends to offer him playing the game (and they, of course, always agree). However, if any of them happens to be chosen strictly more than $\left\lceil\dfrac{m}{2}\right\rceil$ times, then all other friends are offended. Of course, Aleksey doesn't want to offend anyone.
Help him to choose teammates so that nobody is chosen strictly more than $\left\lceil\dfrac{m}{2}\right\rceil$ times.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10000$). Description of the test cases follows.
The first line of each test case contains two integers $n$ and $m$ ($1\leq n, m\leq 100000$) standing for the number of friends and the number of days to play, respectively.
The $i$-th of the following $m$ lines contains an integer $k_i$ ($1\leq k_i\leq n$), followed by $k_i$ distinct integers $f_{i1}$, ..., $f_{ik_i}$ ($1\leq f_{ij}\leq n$), separated by spaces — indices of available friends on the day $i$.
It is guaranteed that the sums of $n$ and $m$ over all test cases do not exceed $100000$. It's guaranteed that the sum of all $k_i$ over all days of all test cases doesn't exceed $200000$.
-----Output-----
Print an answer for each test case. If there is no way to achieve the goal, print "NO".
Otherwise, in the first line print "YES", and in the second line print $m$ space separated integers $c_1$, ..., $c_m$. Each $c_i$ must denote the chosen friend on day $i$ (and therefore must be one of $f_{ij}$).
No value must occur more than $\left\lceil\dfrac{m}{2}\right\rceil$ times. If there is more than one possible answer, print any of them.
-----Examples-----
Input
2
4 6
1 1
2 1 2
3 1 2 3
4 1 2 3 4
2 2 3
1 3
2 2
1 1
1 1
Output
YES
1 2 1 1 2 3
NO
-----Note-----
None | tc = int(input())
while tc > 0:
n, m = map(int, input().strip().split())
vv, ans, tmp = [], [], []
for i in range(n + 9):
empt = []
ans.append(empt)
for i in range(m):
aa = list(map(int, input().strip().split()))
x = aa[0]
tmp.append((x, i))
a = []
for j in range(1, x + 1):
v = aa[j]
a.append(v - 1)
vv.append(a)
tmp.sort()
k = (m + 1) // 2
for i in range(m):
d = tmp[i][1]
sz = tmp[i][0]
for j in range(sz):
u = vv[d][j]
if len(ans[u]) < k:
ans[u].append(d)
break
day = [(-1) for i in range(m + 9)]
for i in range(n):
for j in range(len(ans[i])):
d = ans[i][j]
day[d] = i
flag = True
for i in range(m):
if day[i] == -1:
flag = False
if flag == False:
print("NO")
else:
print("YES")
for i in range(m):
print(day[i] + 1, end=" ")
print()
tc -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR LIST LIST LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER STRING EXPR FUNC_CALL VAR VAR NUMBER |
Aleksey has $n$ friends. He is also on a vacation right now, so he has $m$ days to play this new viral cooperative game! But since it's cooperative, Aleksey will need one teammate in each of these $m$ days.
On each of these days some friends will be available for playing, and all others will not. On each day Aleksey must choose one of his available friends to offer him playing the game (and they, of course, always agree). However, if any of them happens to be chosen strictly more than $\left\lceil\dfrac{m}{2}\right\rceil$ times, then all other friends are offended. Of course, Aleksey doesn't want to offend anyone.
Help him to choose teammates so that nobody is chosen strictly more than $\left\lceil\dfrac{m}{2}\right\rceil$ times.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10000$). Description of the test cases follows.
The first line of each test case contains two integers $n$ and $m$ ($1\leq n, m\leq 100000$) standing for the number of friends and the number of days to play, respectively.
The $i$-th of the following $m$ lines contains an integer $k_i$ ($1\leq k_i\leq n$), followed by $k_i$ distinct integers $f_{i1}$, ..., $f_{ik_i}$ ($1\leq f_{ij}\leq n$), separated by spaces — indices of available friends on the day $i$.
It is guaranteed that the sums of $n$ and $m$ over all test cases do not exceed $100000$. It's guaranteed that the sum of all $k_i$ over all days of all test cases doesn't exceed $200000$.
-----Output-----
Print an answer for each test case. If there is no way to achieve the goal, print "NO".
Otherwise, in the first line print "YES", and in the second line print $m$ space separated integers $c_1$, ..., $c_m$. Each $c_i$ must denote the chosen friend on day $i$ (and therefore must be one of $f_{ij}$).
No value must occur more than $\left\lceil\dfrac{m}{2}\right\rceil$ times. If there is more than one possible answer, print any of them.
-----Examples-----
Input
2
4 6
1 1
2 1 2
3 1 2 3
4 1 2 3 4
2 2 3
1 3
2 2
1 1
1 1
Output
YES
1 2 1 1 2 3
NO
-----Note-----
None | import sys
input = sys.stdin.readline
for _ in range(int(input())):
n, m = map(int, input().split())
days = []
ans = []
for i in range(m):
days.append(list(map(int, input().split())))
ans.append(days[i][1])
freq = {}
for num in ans:
if num in freq:
freq[num] += 1
else:
freq[num] = 1
f = max(freq.keys(), key=lambda k: freq[k])
z = freq[f]
if z <= (m + 1) // 2:
print("YES")
print(*ans)
else:
for i in range(m):
if ans[i] == f:
if days[i][0] > 1:
z -= 1
ans[i] = days[i][2]
if z <= (m + 1) // 2:
break
else:
print("NO")
continue
print("YES")
print(*ans) | 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 LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR |
Aleksey has $n$ friends. He is also on a vacation right now, so he has $m$ days to play this new viral cooperative game! But since it's cooperative, Aleksey will need one teammate in each of these $m$ days.
On each of these days some friends will be available for playing, and all others will not. On each day Aleksey must choose one of his available friends to offer him playing the game (and they, of course, always agree). However, if any of them happens to be chosen strictly more than $\left\lceil\dfrac{m}{2}\right\rceil$ times, then all other friends are offended. Of course, Aleksey doesn't want to offend anyone.
Help him to choose teammates so that nobody is chosen strictly more than $\left\lceil\dfrac{m}{2}\right\rceil$ times.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10000$). Description of the test cases follows.
The first line of each test case contains two integers $n$ and $m$ ($1\leq n, m\leq 100000$) standing for the number of friends and the number of days to play, respectively.
The $i$-th of the following $m$ lines contains an integer $k_i$ ($1\leq k_i\leq n$), followed by $k_i$ distinct integers $f_{i1}$, ..., $f_{ik_i}$ ($1\leq f_{ij}\leq n$), separated by spaces — indices of available friends on the day $i$.
It is guaranteed that the sums of $n$ and $m$ over all test cases do not exceed $100000$. It's guaranteed that the sum of all $k_i$ over all days of all test cases doesn't exceed $200000$.
-----Output-----
Print an answer for each test case. If there is no way to achieve the goal, print "NO".
Otherwise, in the first line print "YES", and in the second line print $m$ space separated integers $c_1$, ..., $c_m$. Each $c_i$ must denote the chosen friend on day $i$ (and therefore must be one of $f_{ij}$).
No value must occur more than $\left\lceil\dfrac{m}{2}\right\rceil$ times. If there is more than one possible answer, print any of them.
-----Examples-----
Input
2
4 6
1 1
2 1 2
3 1 2 3
4 1 2 3 4
2 2 3
1 3
2 2
1 1
1 1
Output
YES
1 2 1 1 2 3
NO
-----Note-----
None | def solve(M, n, m):
I = {}
S = {}
result = [0] * m
for i in range(m):
k = M[i][0]
for j in range(1, k + 1):
I[M[i][j]] = I.get(M[i][j], 0) + 1
if k == 1:
S[M[i][1]] = S.get(M[i][1], 0) + 1
result[i] = M[i][1]
maxFreq = (m + 1) // 2
for k, f in S.items():
if f > maxFreq:
return ["NO"]
A = {}
for k, f in I.items():
A[k] = min(f, maxFreq) - S.get(k, 0)
for i in range(m):
if result[i] > 0:
continue
for j in range(1, M[i][0] + 1):
if A[M[i][j]] > 0:
result[i] = M[i][j]
A[M[i][j]] -= 1
break
return ["YES", result]
t = int(input())
for tc in range(t):
n, m = map(int, input().split())
M = []
for i in range(m):
M.append(list(map(int, input().split())))
result = solve(M, n, m)
print(result[0])
if len(result) > 1:
print(*result[1]) | FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR IF VAR VAR RETURN LIST STRING ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN LIST STRING 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 LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER |
Aleksey has $n$ friends. He is also on a vacation right now, so he has $m$ days to play this new viral cooperative game! But since it's cooperative, Aleksey will need one teammate in each of these $m$ days.
On each of these days some friends will be available for playing, and all others will not. On each day Aleksey must choose one of his available friends to offer him playing the game (and they, of course, always agree). However, if any of them happens to be chosen strictly more than $\left\lceil\dfrac{m}{2}\right\rceil$ times, then all other friends are offended. Of course, Aleksey doesn't want to offend anyone.
Help him to choose teammates so that nobody is chosen strictly more than $\left\lceil\dfrac{m}{2}\right\rceil$ times.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10000$). Description of the test cases follows.
The first line of each test case contains two integers $n$ and $m$ ($1\leq n, m\leq 100000$) standing for the number of friends and the number of days to play, respectively.
The $i$-th of the following $m$ lines contains an integer $k_i$ ($1\leq k_i\leq n$), followed by $k_i$ distinct integers $f_{i1}$, ..., $f_{ik_i}$ ($1\leq f_{ij}\leq n$), separated by spaces — indices of available friends on the day $i$.
It is guaranteed that the sums of $n$ and $m$ over all test cases do not exceed $100000$. It's guaranteed that the sum of all $k_i$ over all days of all test cases doesn't exceed $200000$.
-----Output-----
Print an answer for each test case. If there is no way to achieve the goal, print "NO".
Otherwise, in the first line print "YES", and in the second line print $m$ space separated integers $c_1$, ..., $c_m$. Each $c_i$ must denote the chosen friend on day $i$ (and therefore must be one of $f_{ij}$).
No value must occur more than $\left\lceil\dfrac{m}{2}\right\rceil$ times. If there is more than one possible answer, print any of them.
-----Examples-----
Input
2
4 6
1 1
2 1 2
3 1 2 3
4 1 2 3 4
2 2 3
1 3
2 2
1 1
1 1
Output
YES
1 2 1 1 2 3
NO
-----Note-----
None | t = int(input())
for i in range(t):
n, m = map(int, input().split())
f = []
tot = [0] * n
uni = [0] * n
pop = 0
popind = -1
threshold = (m + 1) // 2
for j in range(m):
f.append(input().split())
for k in range(len(f[j])):
cur = int(f[j][k])
f[j][k] = cur
if k > 0:
tot[cur - 1] += 1
if tot[cur - 1] > pop:
pop = tot[cur - 1]
popind = cur
if len(f[j]) == 2:
uni[cur - 1] += 1
if uni[popind - 1] > threshold:
print("NO")
else:
print("YES")
already = 0
answer = ""
for j in range(m):
f[j][0] = -1
if len(f[j]) == 2:
answer += str(f[j][1])
elif popind in f[j]:
if already == threshold - uni[popind - 1]:
if f[j][1] == popind:
answer += str(f[j][2])
else:
answer += str(f[j][1])
else:
answer += str(popind)
already += 1
else:
answer += str(f[j][1])
if j != m:
answer += " "
print(answer) | 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 LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR IF VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR |
Aleksey has $n$ friends. He is also on a vacation right now, so he has $m$ days to play this new viral cooperative game! But since it's cooperative, Aleksey will need one teammate in each of these $m$ days.
On each of these days some friends will be available for playing, and all others will not. On each day Aleksey must choose one of his available friends to offer him playing the game (and they, of course, always agree). However, if any of them happens to be chosen strictly more than $\left\lceil\dfrac{m}{2}\right\rceil$ times, then all other friends are offended. Of course, Aleksey doesn't want to offend anyone.
Help him to choose teammates so that nobody is chosen strictly more than $\left\lceil\dfrac{m}{2}\right\rceil$ times.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10000$). Description of the test cases follows.
The first line of each test case contains two integers $n$ and $m$ ($1\leq n, m\leq 100000$) standing for the number of friends and the number of days to play, respectively.
The $i$-th of the following $m$ lines contains an integer $k_i$ ($1\leq k_i\leq n$), followed by $k_i$ distinct integers $f_{i1}$, ..., $f_{ik_i}$ ($1\leq f_{ij}\leq n$), separated by spaces — indices of available friends on the day $i$.
It is guaranteed that the sums of $n$ and $m$ over all test cases do not exceed $100000$. It's guaranteed that the sum of all $k_i$ over all days of all test cases doesn't exceed $200000$.
-----Output-----
Print an answer for each test case. If there is no way to achieve the goal, print "NO".
Otherwise, in the first line print "YES", and in the second line print $m$ space separated integers $c_1$, ..., $c_m$. Each $c_i$ must denote the chosen friend on day $i$ (and therefore must be one of $f_{ij}$).
No value must occur more than $\left\lceil\dfrac{m}{2}\right\rceil$ times. If there is more than one possible answer, print any of them.
-----Examples-----
Input
2
4 6
1 1
2 1 2
3 1 2 3
4 1 2 3 4
2 2 3
1 3
2 2
1 1
1 1
Output
YES
1 2 1 1 2 3
NO
-----Note-----
None | t = int(input())
for _ in range(t):
n, m = map(int, input().split())
mat = []
for __ in range(m):
l = list(map(int, input().split()))
mat.append(l)
d = [0] * m
flag = 0
f = [0] * (n + 1)
mx = (m + 1) // 2
for i in range(m):
if mat[i][0] == 1:
d[i] = mat[i][1]
f[mat[i][1]] += 1
if f[mat[i][1]] > mx:
flag = 1
break
if flag == 1:
print("NO")
else:
print("YES")
for i in range(m):
if d[i] != 0:
print(d[i], end=" ")
else:
g = 0
for j in range(1, mat[i][0] + 1):
if g == 1:
break
if f[mat[i][j]] < mx:
f[mat[i][j]] += 1
print(mat[i][j], end=" ")
g = 1
print() | 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 LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER IF VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR |
Amr loves Chemistry, and specially doing experiments. He is preparing for a new interesting experiment.
Amr has n different types of chemicals. Each chemical i has an initial volume of a_{i} liters. For this experiment, Amr has to mix all the chemicals together, but all the chemicals volumes must be equal first. So his task is to make all the chemicals volumes equal.
To do this, Amr can do two different kind of operations. Choose some chemical i and double its current volume so the new volume will be 2a_{i} Choose some chemical i and divide its volume by two (integer division) so the new volume will be $\lfloor \frac{a_{i}}{2} \rfloor$
Suppose that each chemical is contained in a vessel of infinite volume. Now Amr wonders what is the minimum number of operations required to make all the chemicals volumes equal?
-----Input-----
The first line contains one number n (1 ≤ n ≤ 10^5), the number of chemicals.
The second line contains n space separated integers a_{i} (1 ≤ a_{i} ≤ 10^5), representing the initial volume of the i-th chemical in liters.
-----Output-----
Output one integer the minimum number of operations required to make all the chemicals volumes equal.
-----Examples-----
Input
3
4 8 2
Output
2
Input
3
3 5 6
Output
5
-----Note-----
In the first sample test, the optimal solution is to divide the second chemical volume by two, and multiply the third chemical volume by two to make all the volumes equal 4.
In the second sample test, the optimal solution is to divide the first chemical volume by two, and divide the second and the third chemical volumes by two twice to make all the volumes equal 1. | n = int(input())
s = list(map(int, input().split()))
l = [bin(i)[2:] for i in s]
length = [len(i) for i in l]
maxLen = max(length)
minLen = min(length)
loc = 0
flag = False
for j in range(minLen):
for i in range(n):
if l[i][j] != l[0][j]:
flag = True
break
if flag:
break
loc += 1
result = sum(length) - loc * n
best = result
change = n * [-1]
for j in range(loc, maxLen):
for i in range(n):
if j >= length[i] or l[i][j] == "1":
change[i] = 1
result += sum(change)
if result > best:
break
best = result
print(best) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR STRING ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
Amr loves Chemistry, and specially doing experiments. He is preparing for a new interesting experiment.
Amr has n different types of chemicals. Each chemical i has an initial volume of a_{i} liters. For this experiment, Amr has to mix all the chemicals together, but all the chemicals volumes must be equal first. So his task is to make all the chemicals volumes equal.
To do this, Amr can do two different kind of operations. Choose some chemical i and double its current volume so the new volume will be 2a_{i} Choose some chemical i and divide its volume by two (integer division) so the new volume will be $\lfloor \frac{a_{i}}{2} \rfloor$
Suppose that each chemical is contained in a vessel of infinite volume. Now Amr wonders what is the minimum number of operations required to make all the chemicals volumes equal?
-----Input-----
The first line contains one number n (1 ≤ n ≤ 10^5), the number of chemicals.
The second line contains n space separated integers a_{i} (1 ≤ a_{i} ≤ 10^5), representing the initial volume of the i-th chemical in liters.
-----Output-----
Output one integer the minimum number of operations required to make all the chemicals volumes equal.
-----Examples-----
Input
3
4 8 2
Output
2
Input
3
3 5 6
Output
5
-----Note-----
In the first sample test, the optimal solution is to divide the second chemical volume by two, and multiply the third chemical volume by two to make all the volumes equal 4.
In the second sample test, the optimal solution is to divide the first chemical volume by two, and divide the second and the third chemical volumes by two twice to make all the volumes equal 1. | def main():
input()
aa = list(map(int, input().split()))
le, l = max(aa).bit_length(), []
for i, a in enumerate(aa):
j = le - a.bit_length()
aa[i] = a << j
l.append(j)
mi, ma = min(aa), max(aa)
a = mask = (1 << le) - 1
if mi == ma:
while mi == mi & a:
mask = a
a &= a << 1
else:
while mi != ma:
mask &= mask << 1
mi >>= 1
ma >>= 1
while not mi & 1:
mask &= mask << 1
mi >>= 1
mask ^= (1 << le) - 1
le = mask.bit_length() + 1
res = [0] * le
cache = {}
for a, i in zip(aa, l):
a &= mask
if a:
a = a.bit_length()
tmp = cache.get((i, a))
if tmp is None:
cache[i, a] = tmp = [0] * le
if a:
base, baseidx = a - i, le - a - 1
else:
base, baseidx = 0, le - i - 1
i, j = baseidx, base
while i:
i -= 1
j += 1
tmp[i] = j
i, j = baseidx, base
while i < le:
tmp[i] = j
i += 1
j += 1
for i, j in enumerate(tmp):
res[i] += j
print(min(res))
main() | FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR VAR LIST FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR WHILE VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER WHILE BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE ASSIGN VAR VAR VAR VAR BIN_OP LIST NUMBER VAR IF VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR WHILE VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Amr loves Chemistry, and specially doing experiments. He is preparing for a new interesting experiment.
Amr has n different types of chemicals. Each chemical i has an initial volume of a_{i} liters. For this experiment, Amr has to mix all the chemicals together, but all the chemicals volumes must be equal first. So his task is to make all the chemicals volumes equal.
To do this, Amr can do two different kind of operations. Choose some chemical i and double its current volume so the new volume will be 2a_{i} Choose some chemical i and divide its volume by two (integer division) so the new volume will be $\lfloor \frac{a_{i}}{2} \rfloor$
Suppose that each chemical is contained in a vessel of infinite volume. Now Amr wonders what is the minimum number of operations required to make all the chemicals volumes equal?
-----Input-----
The first line contains one number n (1 ≤ n ≤ 10^5), the number of chemicals.
The second line contains n space separated integers a_{i} (1 ≤ a_{i} ≤ 10^5), representing the initial volume of the i-th chemical in liters.
-----Output-----
Output one integer the minimum number of operations required to make all the chemicals volumes equal.
-----Examples-----
Input
3
4 8 2
Output
2
Input
3
3 5 6
Output
5
-----Note-----
In the first sample test, the optimal solution is to divide the second chemical volume by two, and multiply the third chemical volume by two to make all the volumes equal 4.
In the second sample test, the optimal solution is to divide the first chemical volume by two, and divide the second and the third chemical volumes by two twice to make all the volumes equal 1. | N = 300000
A, L, R, level, ans, H, UP = (
[0] * N,
[0] * N,
[0] * N,
[0] * N,
[0] * N,
[0] * N,
[0] * N,
)
x = 0
for i in range(N):
level[i] = level[i // 2] + 1
n = int(input())
arr = list(map(int, input().rstrip().split()))
for i in range(n):
A[i] = arr[i]
for i in range(n):
x = A[i]
H[x] += 1
ans[1] += level[x] - level[1]
while x != 1:
if x & 1:
R[x // 2] += 1
else:
L[x // 2] += 1
x //= 2
result = ans[1]
up = False
i = 1
while i < N:
if not L[i] and not H[i] and not up:
i = 2 * i + 1
else:
if 2 * i >= N:
break
if R[i] or H[i]:
up = True
UP[2 * i] += UP[i] + H[i] + R[i]
i = 2 * i
if i >= N:
break
if i & 1:
ans[i] = ans[i // 2] - R[i // 2]
else:
ans[i] = ans[i // 2] + UP[i] - L[i // 2]
result = min(result, ans[i])
print(result) | ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER BIN_OP VAR VAR VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF BIN_OP NUMBER VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR IF VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Amr loves Chemistry, and specially doing experiments. He is preparing for a new interesting experiment.
Amr has n different types of chemicals. Each chemical i has an initial volume of a_{i} liters. For this experiment, Amr has to mix all the chemicals together, but all the chemicals volumes must be equal first. So his task is to make all the chemicals volumes equal.
To do this, Amr can do two different kind of operations. Choose some chemical i and double its current volume so the new volume will be 2a_{i} Choose some chemical i and divide its volume by two (integer division) so the new volume will be $\lfloor \frac{a_{i}}{2} \rfloor$
Suppose that each chemical is contained in a vessel of infinite volume. Now Amr wonders what is the minimum number of operations required to make all the chemicals volumes equal?
-----Input-----
The first line contains one number n (1 ≤ n ≤ 10^5), the number of chemicals.
The second line contains n space separated integers a_{i} (1 ≤ a_{i} ≤ 10^5), representing the initial volume of the i-th chemical in liters.
-----Output-----
Output one integer the minimum number of operations required to make all the chemicals volumes equal.
-----Examples-----
Input
3
4 8 2
Output
2
Input
3
3 5 6
Output
5
-----Note-----
In the first sample test, the optimal solution is to divide the second chemical volume by two, and multiply the third chemical volume by two to make all the volumes equal 4.
In the second sample test, the optimal solution is to divide the first chemical volume by two, and divide the second and the third chemical volumes by two twice to make all the volumes equal 1. | import itertools
n = int(input())
l = list(map(int, input().split()))
m = max(l) + 5
freq = [(0) for _ in range(m)]
count = [(0) for _ in range(m)]
vis = [(-1) for _ in range(m)]
for i in range(n):
q = [(l[i], 0)]
pos = 0
while len(q) > pos:
top, c = q[pos]
pos += 1
if top >= m or vis[top] == i:
continue
vis[top] = i
freq[top] += 1
count[top] += c
q.append((2 * top, c + 1))
q.append((top // 2, c + 1))
ans = min(j for i, j in zip(freq, count) if i == n)
print(ans) | 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 FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Amr loves Chemistry, and specially doing experiments. He is preparing for a new interesting experiment.
Amr has n different types of chemicals. Each chemical i has an initial volume of a_{i} liters. For this experiment, Amr has to mix all the chemicals together, but all the chemicals volumes must be equal first. So his task is to make all the chemicals volumes equal.
To do this, Amr can do two different kind of operations. Choose some chemical i and double its current volume so the new volume will be 2a_{i} Choose some chemical i and divide its volume by two (integer division) so the new volume will be $\lfloor \frac{a_{i}}{2} \rfloor$
Suppose that each chemical is contained in a vessel of infinite volume. Now Amr wonders what is the minimum number of operations required to make all the chemicals volumes equal?
-----Input-----
The first line contains one number n (1 ≤ n ≤ 10^5), the number of chemicals.
The second line contains n space separated integers a_{i} (1 ≤ a_{i} ≤ 10^5), representing the initial volume of the i-th chemical in liters.
-----Output-----
Output one integer the minimum number of operations required to make all the chemicals volumes equal.
-----Examples-----
Input
3
4 8 2
Output
2
Input
3
3 5 6
Output
5
-----Note-----
In the first sample test, the optimal solution is to divide the second chemical volume by two, and multiply the third chemical volume by two to make all the volumes equal 4.
In the second sample test, the optimal solution is to divide the first chemical volume by two, and divide the second and the third chemical volumes by two twice to make all the volumes equal 1. | read = lambda: map(int, input().split())
n = int(input())
a = list(read())
m = max(a) + 5
cnt = [0] * m
step = [0] * m
was = [-1] * m
for i in range(n):
x = a[i]
q = [(x, 0)]
st = 0
while st < len(q):
x, y = q[st]
st += 1
if x >= m or was[x] == i:
continue
was[x] = i
step[x] += y
cnt[x] += 1
q.append((x * 2, y + 1))
q.append((x // 2, y + 1))
ans = min(step[x] for x in range(m) if cnt[x] == n)
print(ans) | ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Amr loves Chemistry, and specially doing experiments. He is preparing for a new interesting experiment.
Amr has n different types of chemicals. Each chemical i has an initial volume of a_{i} liters. For this experiment, Amr has to mix all the chemicals together, but all the chemicals volumes must be equal first. So his task is to make all the chemicals volumes equal.
To do this, Amr can do two different kind of operations. Choose some chemical i and double its current volume so the new volume will be 2a_{i} Choose some chemical i and divide its volume by two (integer division) so the new volume will be $\lfloor \frac{a_{i}}{2} \rfloor$
Suppose that each chemical is contained in a vessel of infinite volume. Now Amr wonders what is the minimum number of operations required to make all the chemicals volumes equal?
-----Input-----
The first line contains one number n (1 ≤ n ≤ 10^5), the number of chemicals.
The second line contains n space separated integers a_{i} (1 ≤ a_{i} ≤ 10^5), representing the initial volume of the i-th chemical in liters.
-----Output-----
Output one integer the minimum number of operations required to make all the chemicals volumes equal.
-----Examples-----
Input
3
4 8 2
Output
2
Input
3
3 5 6
Output
5
-----Note-----
In the first sample test, the optimal solution is to divide the second chemical volume by two, and multiply the third chemical volume by two to make all the volumes equal 4.
In the second sample test, the optimal solution is to divide the first chemical volume by two, and divide the second and the third chemical volumes by two twice to make all the volumes equal 1. | n = int(input())
As = list(map(int, input().split()))
def solve(n, As):
if n == 1:
return 0
Bs = [bin(a)[2:] for a in As]
Bs = remove_common_area(Bs)
long = max(map(len, Bs))
costs = [0] * (long + 1)
for b in Bs:
update_costs(costs, b, long)
return min(costs)
def remove_common_area(Bs):
for i in range(1000):
if len(Bs[0]) <= i:
return [b[i:] for b in Bs]
bi = Bs[0][i]
for b in Bs[1:]:
if len(b) <= i or b[i] != bi:
return [b[i:] for b in Bs]
return Bs
def update_costs(costs, b, long):
len_b = len(b)
found_1 = -1
for i in range(long + 1):
if found_1 == -1 and i < len_b and b[i] == "1":
found_1 = i
if found_1 == -1:
costs[i] += abs(len_b - i)
else:
costs[i] += len_b - found_1 + i - found_1
print(solve(n, As)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR RETURN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR FOR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR STRING ASSIGN VAR VAR IF VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Amr loves Chemistry, and specially doing experiments. He is preparing for a new interesting experiment.
Amr has n different types of chemicals. Each chemical i has an initial volume of a_{i} liters. For this experiment, Amr has to mix all the chemicals together, but all the chemicals volumes must be equal first. So his task is to make all the chemicals volumes equal.
To do this, Amr can do two different kind of operations. Choose some chemical i and double its current volume so the new volume will be 2a_{i} Choose some chemical i and divide its volume by two (integer division) so the new volume will be $\lfloor \frac{a_{i}}{2} \rfloor$
Suppose that each chemical is contained in a vessel of infinite volume. Now Amr wonders what is the minimum number of operations required to make all the chemicals volumes equal?
-----Input-----
The first line contains one number n (1 ≤ n ≤ 10^5), the number of chemicals.
The second line contains n space separated integers a_{i} (1 ≤ a_{i} ≤ 10^5), representing the initial volume of the i-th chemical in liters.
-----Output-----
Output one integer the minimum number of operations required to make all the chemicals volumes equal.
-----Examples-----
Input
3
4 8 2
Output
2
Input
3
3 5 6
Output
5
-----Note-----
In the first sample test, the optimal solution is to divide the second chemical volume by two, and multiply the third chemical volume by two to make all the volumes equal 4.
In the second sample test, the optimal solution is to divide the first chemical volume by two, and divide the second and the third chemical volumes by two twice to make all the volumes equal 1. | n = int(input())
a = [bin(int(i)) for i in input().split()]
j = 2
s = "0b"
while True:
ok = True
i = a[0]
if j < len(i):
c = i[j]
else:
c = "0"
for i in a:
if j < len(i):
cc = i[j]
else:
cc = "0"
if cc != c:
ok = False
break
if not ok or j > 20:
break
s += c
j += 1
b = []
r = 0
for i in a:
pos = i.find("1", len(s))
if pos == -1:
b.append(len(i))
else:
b.append(pos)
r += len(i) - pos
b.sort()
m = b[len(b) // 2]
for i in b:
r += abs(i - m)
print(r) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR STRING WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR STRING FOR VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR STRING IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR BIN_OP 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 _ in range(int(input())):
p, f = map(int, input().split())
ns, nx = map(int, input().split())
ws, wx = map(int, input().split())
ans = 0
if wx < ws:
wx, ws = ws, wx
ns, nx = nx, ns
if ws <= wx:
ans = 0
for i in range(ns + 1):
ui = i
uj = 0
tmp = 0
if p - i * ws >= 0:
if (p - i * ws) // wx >= nx:
tmp += nx
uj = nx
else:
tmp += (p - i * ws) // wx
uj = tmp
tmp += i
else:
tmp += p // ws
ui = tmp
if f - (ns - ui) * ws >= 0:
if (f - (ns - ui) * ws) // wx >= nx - uj:
tmp += nx - uj
else:
tmp += (f - (ns - ui) * ws) // wx
tmp += ns - ui
else:
tmp += f // ws
ans = max(ans, tmp)
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 ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR IF BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR 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$. | t = lambda: range(int(input()))
a = lambda: list(map(int, input().split()))
for _ in t():
p, f = a()
cs, cw = a()
s, w = a()
if s > w:
t = w
ts = cw
w = s
cw = cs
s = t
cs = ts
ans = 0
for a0 in range(0, min(cs, p // s) + 1):
b0 = min(cw, (p - a0 * s) // w)
a1 = min(cs - a0, f // s)
b1 = min(cw - b0, (f - a1 * s) // w)
ans = max(ans, a0 + a1 + b0 + b1)
print(ans) | ASSIGN VAR FUNC_CALL 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 ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR 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 NUMBER 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 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$. | t = int(input())
while t > 0:
c1, c2 = [int(x) for x in input().split()]
n1, n2 = [int(x) for x in input().split()]
w1, w2 = [int(x) for x in input().split()]
initial = min(n1, c1 // w1)
maxi = 0
for i in range(initial, -1, -1):
ans = i
rest_c1 = c1 - i * w1
ans += min(n2, rest_c1 // w2)
t_n1 = n1 - i
t_n2 = n2 - min(n2, rest_c1 // w2)
if w1 < w2:
take_n1 = min(t_n1, c2 // w1)
rest_c2 = c2 - take_n1 * w1
ans += take_n1
ans += min(t_n2, rest_c2 // w2)
else:
take_n2 = min(t_n2, c2 // w2)
rest_c2 = c2 - take_n2 * w2
ans += take_n2
ans += min(t_n1, rest_c2 // w1)
maxi = max(maxi, ans)
print(maxi)
t -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN 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 VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP 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 VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER |
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())):
inip, inif = [int(i) for i in input().split()]
cnts, cntw = [int(i) for i in input().split()]
s, w = [int(i) for i in input().split()]
if s > w:
s, w = w, s
cnts, cntw = cntw, cnts
if inif > inip:
inif, inip = inip, inif
ini = cnts + cntw
inis = cnts
iniw = cntw
ans = 0
for i in range(cnts + 1):
p = inip
f = inif
cnts = inis
cntw = iniw
if p >= i * s:
p -= i * s
cnts -= i
else:
break
if p > cntw * w:
cp = cntw * w
cntw = 0
else:
cp = p // w * w
cntw -= p // w
if f > cnts * s:
cf = cnts * s
cnts = 0
else:
cf = f // s * s
cnts -= f // s
f -= cf
if f > cntw * w:
cf = cntw * w
cntw = 0
else:
cf = f // w * w
cntw -= f // w
f -= cf
ans = max(ans, ini - cnts - cntw)
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP 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 ik in range(int(input())):
n, k = map(int, input().split())
ca, cw = map(int, input().split())
s, w1 = map(int, input().split())
if s > w1:
s, w1 = w1, s
ca, cw = cw, ca
tot = 0
lwe = [n, k, ca, cw, s, w1]
for i in range(ca + 1):
ans = 0
n, k, ca, cw, s, w1 = lwe[0], lwe[1], lwe[2], lwe[3], lwe[4], lwe[5]
w = n // s
if w < i:
continue
ans += i
n -= i * s
ans += min(cw, n // w1)
cw -= min(cw, n // w1)
ca -= i
ans += min(ca, k // s)
k -= min(ca, k // s) * s
ans += min(cw, k // w1)
tot = max(tot, ans)
print(tot) | 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 ASSIGN VAR LIST VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR 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$. | for _ in range(int(input())):
p, f = map(int, input().split())
cs, cw = map(int, input().split())
s, w = map(int, input().split())
ans = 0
if s > w:
s, w = w, s
cs, cw = cw, cs
for i in range(min(cs + 1, p // s + 1)):
t1 = cs - i
t2 = (p - s * i) // w
t3 = cw - t2
t4 = min(f // s, t1)
t5 = min((f - s * t4) // w, t3)
ans = max(ans, i + t2 + t4 + t5)
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 ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR 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$. | for _ in range(int(input())):
p, f = map(int, input().split())
cs, cw = map(int, input().split())
s, w = map(int, input().split())
ans = 0
if w < s:
s, w = w, s
cs, cw = cw, cs
if f < p:
f, p = p, f
maxl = f // s + p // s
maxl = min(maxl, cs)
maximum = 0
for i in range(maxl + 1):
ans = 0
if i * s > f or (maxl - i) * s > p:
continue
ans += maxl
tf = f - i * s
tp = p - (maxl - i) * s
ans += min(tf // w + tp // w, cw)
if ans > maximum:
maximum = ans
print(maximum) | 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 ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR 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 forf(cap, cs, cw, s, w):
if s > w:
return forf(cap, cw, cs, w, s)
elif cs * s >= cap:
return cap // s
else:
return cs + min(cw, (cap - cs * s) // w)
for t 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()))
ans = 0
for cnt_s in range(cs + 1):
if cnt_s * s <= p:
cnt_w = min(cw, (p - cnt_s * s) // w)
ans = max(ans, cnt_s + cnt_w + forf(f, cs - cnt_s, cw - cnt_w, s, w))
print(ans) | FUNC_DEF IF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR RETURN BIN_OP VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR 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 ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF 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 VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR 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$. | t = int(input())
def max_count(cnts, s, cntw, w, l):
cs = min(cnts, l // s)
cw = min(cntw, (l - cs * s) // w)
return cs, cw
while t:
p, f = map(int, input().split())
cnts, cntw = map(int, input().split())
s, w = map(int, input().split())
if w < s:
cnts, cntw = cntw, cnts
s, w = w, s
ans = 0
for i in range(cnts + 1):
cs1, cw1 = max_count(i, s, cntw, w, p)
cs2, cw2 = max_count(cnts - cs1, s, cntw - cw1, w, f)
ans = max(ans, cs1 + cw1 + cs2 + cw2)
print(ans)
t -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF 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 RETURN VAR VAR WHILE 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 FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER |
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,) = map(int, input().split())
for _ in range(T):
p, q = map(int, input().split())
n, m = map(int, input().split())
w, v = map(int, input().split())
if w > v:
w, v = v, w
n, m = m, n
R = 0
for c in range(n + 1):
if p < c * w:
break
pp = p - c * w
if q >= (n - c) * w:
qq = q - (n - c) * w
r = min(pp // v + qq // v, m) + n
else:
r, qq = divmod(q, w)
r = r + min(pp // v, m) + c
R = max(R, r)
print(R) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL 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 IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL 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$. | import sys
def main():
inp = [int(x) for x in sys.stdin.read().split()]
ii = 0
t = inp[ii]
ii += 1
for _ in range(t):
p = inp[ii]
ii += 1
f = inp[ii]
ii += 1
cnts = inp[ii]
ii += 1
cntw = inp[ii]
ii += 1
s = inp[ii]
ii += 1
w = inp[ii]
ii += 1
best = 0
for i in range(cnts + 1):
counter = i
rems = cnts - i
pbal = p - i * s
if pbal < 0:
break
wtake = min(pbal // w, cntw)
counter += wtake
remw = cntw - wtake
if s <= w:
stake = min(f // s, rems)
fbal = f - stake * s
counter += stake
counter += min(fbal // w, remw)
else:
wtakef = min(f // w, remw)
fbal = f - wtakef * w
counter += wtakef
counter += min(fbal // s, rems)
best = max(best, counter)
print(best)
main() | IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP 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 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$. | for i in range(int(input())):
mca, mcb = list(map(int, input().split()))
cs, ca = list(map(int, input().split()))
ws, wa = list(map(int, input().split()))
if ws > wa:
temp = wa
temp1 = ws
temp2 = cs
temp3 = ca
ws = temp
wa = temp1
cs = temp3
ca = temp2
ans = 0
for i in range(cs + 1):
if ws * i > mca:
continue
temp0 = min(cs - i, mcb // ws)
myl = mca - i * ws
myr = mcb - temp0 * ws
temp1 = min(myl // wa, ca)
temp2 = min(myr // wa, ca - temp1)
ans = max(ans, i + temp0 + temp1 + temp2)
print(round(ans)) | 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 ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR 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 BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR 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 = map(int, input().split())
cs, cw = map(int, input().split())
s, w = map(int, input().split())
res = 0
if (s * cs + cw * w <= p) | (s * cs + cw * w <= f):
print(cs + cw)
continue
if s > w:
s, w = w, s
cs, cw = cw, cs
for i in range(cs + 1):
if i * s > p:
break
count_p_w = (p - i * s) // w
count_f_s = min(cs - i, f // s)
count_f_w = min((f - count_f_s * s) // w, cw - count_p_w)
res = max(res, i + count_p_w + count_f_s + count_f_w)
print(res) | 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 ASSIGN VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN 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 BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR 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$. | T = int(input())
for _ in range(T):
p, f = tuple(map(int, input().split()))
cnts, cntw = tuple(map(int, input().split()))
s, w = tuple(map(int, input().split()))
if s > w:
s, w = w, s
cnts, cntw = cntw, cnts
ans = 0
for i in range(cnts + 1):
if i * s > p:
continue
sf = i
ss = min(int(f / s), cnts - i)
left_f = p - sf * s
left_s = f - ss * s
wf = min(cntw, int(left_f / w))
ws = min(int(left_s / w), cntw - wf)
ans = max(ans, sf + ss + wf + ws)
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR 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 NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR 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$. | t = int(input())
for i in range(t):
p, f = list(map(int, input().split()))
c1, c2 = list(map(int, input().split()))
s, w = list(map(int, input().split()))
if s <= w:
a = min(c1, p // s)
ans = 0
for s1 in range(a + 1):
s2 = min(c1 - s1, f // s)
w1 = min(c2, (p - s1 * s) // w)
w2 = min(c2 - w1, (f - s * s2) // w)
ans = max(ans, s1 + s2 + w1 + w2)
print(ans)
else:
b = min(c2, p // w)
ans = 0
for w1 in range(b + 1):
w2 = min(c2 - w1, f // w)
s1 = min(c1, (p - w * w1) // s)
s2 = min(c1 - s1, (f - w * w2) // s)
ans = max(ans, w1 + w2 + s1 + s2)
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR 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 FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP 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 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 ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP 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 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 |
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 _ in range(t):
p, f = map(int, input().split())
cnts, cntw = map(int, input().split())
s, w = map(int, input().split())
mx = 0
for i in range(min(p // s + 1, cnts + 1)):
cnts_c = cnts
p_c = p
cntw_c = cntw
f_c = f
ans = 0
cnts_c -= i
ans += i
p_c -= i * s
a = min(cntw_c, p_c // w)
ans += a
cntw_c -= a
p_c -= w * a
a = min(cnts_c, p_c // s)
ans += a
cnts_c -= a
p_c -= s * a
if s > w:
a = min(cntw_c, f_c // w)
ans += a
cntw_c -= a
f_c -= w * a
a = min(cnts_c, f_c // s)
ans += a
cnts_c -= a
f_c -= s * a
else:
a = min(cnts_c, f_c // s)
ans += a
cnts_c -= a
f_c -= s * a
a = min(cntw_c, f_c // w)
ans += a
cntw_c -= a
f_c -= w * a
mx = max(mx, ans)
print(mx) | 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 FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR 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$. | import sys
T = int(sys.stdin.readline())
def hwa_ga_nan_da(p, f, cs, cw, s, w):
a = min(p // w, cw)
p -= a * w
cw -= a
b = min(f // s, cs)
f -= b * s
cs -= b
c = min(f // w, cw)
f -= c * w
cs -= c
return a + b + c
for _ in range(T):
p, f = map(int, sys.stdin.readline().split())
cs, cw = map(int, sys.stdin.readline().split())
s, w = list(map(int, sys.stdin.readline().split()))
if s > w:
s, w = w, s
cs, cw = cw, cs
res = 0
for i in range(cs + 1):
if i * s > p:
break
res = max(res, hwa_ga_nan_da(p - i * s, f, cs - i, cw, s, w) + i)
print(res) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR 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 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 FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR 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$. | for _ in range(int(input())):
p, f = map(int, input().split())
cnta, cntb = map(int, input().split())
wa, wb = map(int, input().split())
if wa > wb:
wa, wb = wb, wa
cnta, cntb = cntb, cnta
res = 0
pa = 0
while pa * wa <= p and pa <= cnta:
pb = min((p - pa * wa) // wb, cntb)
fa = min(cnta - pa, f // wa)
fb = min((f - fa * wa) // wb, cntb - pb)
res = max(res, pa + pb + fa + fb)
pa += 1
print(res) | 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 ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR 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 VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP 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$. | ttt = int(input())
for _ in range(ttt):
p, f = [int(x) for x in input().split()]
cnts, cntw = [int(x) for x in input().split()]
s, w = [int(x) for x in input().split()]
if s < w:
s, w = w, s
cnts, cntw = cntw, cnts
qq = -1
for swords in range(cnts + 1):
count = swords
left = p - s * swords
lefts = cnts - swords
leftw = cntw
if left < 0:
continue
x = min(left // w, leftw)
leftw -= x
left -= w * x
count += x
left2 = f
x = min(left2 // w, leftw)
leftw -= x
left2 -= w * x
count += x
x = min(left2 // s, lefts)
lefts -= x
left2 -= s * x
count += x
qq = max(qq, count)
print(qq) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR 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 ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR 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$. | def solve2(p1, p2, cnt1, cnt2, w1, w2):
ans = 0
ans += min(p1 // w2, cnt2)
cnt2 -= ans
carry = min(p2 // w1, cnt1)
ans += carry
cnt1 -= carry
p2 -= carry * w1
carry = min(p2 // w2, cnt2)
ans += carry
return ans
def solve(p1, p2, cnt1, cnt2, w1, w2):
ans = 0
for i in range(cnt1 + 1):
if i * w1 > p1:
break
cur_ans = i + solve2(p1 - i * w1, p2, cnt1 - i, cnt2, w1, w2)
ans = max(ans, cur_ans)
return ans
for _ 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_s, cnt_w = cnt_w, cnt_s
print(solve(p, f, cnt_s, cnt_w, s, w)) | FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR 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$. | t = int(input())
for _ in range(t):
[p1, p2] = list(map(int, input().split()))
cnt = list(map(int, input().split()))
w = list(map(int, input().split()))
tot = sum(cnt)
MIN = -1
if w[0] < w[1]:
MIN = 0
else:
MIN = 1
MAX = int(not MIN)
max1 = p1 // w[MIN]
max2 = p2 // w[MIN]
maxpos = max1 + max2
if cnt[MIN] >= maxpos:
print(maxpos)
else:
maximum = -1
for i in range(0, min(cnt[MIN] + 1, max1 + 1)):
i2 = cnt[MIN] - i
if i2 <= max2:
j1 = min((p1 - i * w[MIN]) // w[MAX], cnt[MAX])
j2 = min((p2 - i2 * w[MIN]) // w[MAX], cnt[MAX] - j1)
final = i + i2 + j1 + j2
if final > maximum:
maximum = final
print(maximum) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN LIST VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL 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 FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN 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 IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR 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$. | for i in range(int(input())):
p, f = map(int, input().split())
cnts, cntw = map(int, input().split())
s, w = map(int, input().split())
if w <= s:
if p // w + f // w <= cntw:
print(p // w + f // w)
else:
answer = cntw
k = 0
while k * w <= p and k <= cntw:
rem1 = p - k * w
curr = k + min(cntw - k, f // w)
rem2 = max(0, f - (cntw - k) * w)
curr += min(rem1 // s + rem2 // s, cnts)
answer = max(answer, curr)
k += 1
print(answer)
elif p // s + f // s <= cnts:
print(p // s + f // s)
else:
answer = cnts
k = 0
while k * s <= p and k <= cnts:
rem1 = p - k * s
curr = k + min(cnts - k, f // s)
rem2 = max(0, f - (cnts - k) * s)
curr += min(rem1 // w + rem2 // w, cntw)
answer = max(answer, curr)
k += 1
print(answer) | 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 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 VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER 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 VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL 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$. | t = int(input())
for _ in range(t):
p, f = map(int, input().split())
cs, cw = map(int, input().split())
s, w = map(int, input().split())
if s > w:
s, cs, w, cw = w, cw, s, cs
res = 0
for i in range(cs + 1):
if p < s * i:
continue
val = i
pw = min(int((p - s * i) / w), cw)
val += pw
rs, rw = cs - i, cw - pw
fs = min(int(f / s), rs)
val += fs
fw = min(int((f - s * fs) / w), rw)
val += fw
res = max(res, val)
print(res) | 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 VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP 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$. | for _ in range(int(input())):
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
cntw, cnts = cnts, cntw
kek = p // s + f // s
if kek <= cnts:
print(kek)
else:
ans = cnts
for sk in range(cnts + 1):
p2, f2 = p, f
p2 -= sk * s
f2 -= (cnts - sk) * s
if p2 >= 0 and f2 >= 0:
ans = max(ans, cnts + p2 // w + f2 // w)
ans = min(ans, cnts + cntw)
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 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 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 ASSIGN VAR FUNC_CALL VAR VAR BIN_OP 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$. | z = lambda: map(int, input().split())
zz = lambda a, b: (min(a, b), max(a, b))
for _ in range(int(input())):
p, f = z()
x, y = z()
s, w = z()
a, b = zz((s, x), (w, y))
if p // a[0] + f // a[0] <= a[1]:
print(p // a[0] + f // a[0])
continue
m = 0
for i in range(a[1] + 1):
if min(p - i * a[0], f - (a[1] - i) * a[0]) >= 0:
v = (p - i * a[0]) // b[0] + (f - (a[1] - i) * a[0]) // b[0]
if v > m:
m = v
if m >= b[1]:
m = b[1]
break
print(a[1] + m) | ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR 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 ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER 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 print(val):
sys.stdout.write(str(val) + "\n")
def prog():
for _ in range(int(input())):
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
mx = 0
for i in range(cnts + 1):
curr = 0
lefts = cnts - i
pleft = p
fleft = f
if i * s > p:
curr += p // s
pleft = p % s
else:
curr += i
pleft -= i * s
if lefts * s > f:
curr += f // s
fleft = f % s
else:
fleft -= lefts * s
curr += lefts
could_add = pleft // w + fleft // w
curr += min(could_add, cntw)
mx = max(mx, curr)
print(mx)
prog() | IMPORT ASSIGN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FUNC_DEF 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 ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR 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$. | def take_item(p, f, s_n, a_n, s_w, a_w):
axe_p = min(p // a_w, a_n)
a_n -= axe_p
sword_f = min(f // s_w, s_n)
f -= sword_f * s_w
axe_f = min(f // a_w, a_n)
return axe_p + axe_f + sword_f
for _ in range(int(input())):
p, f = [int(x) for x in input().split()]
s_n, a_n = [int(x) for x in input().split()]
s_w, a_w = [int(x) for x in input().split()]
res = 0
if s_w > a_w:
s_w, a_w = a_w, s_w
s_n, a_n = a_n, s_n
for i in range(min(s_n, p // s_w) + 1):
tmp = i
p1 = p - i * s_w
s_n_1 = s_n - i
tmp += take_item(p1, f, s_n_1, a_n, s_w, a_w)
res = max(res, tmp)
print(res) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR 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 BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR 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())
cs, cw = map(int, input().split())
s, w = map(int, input().split())
ans = 0
for k in range(0, cs + 1):
max_pcs = min(p // s, k)
max_fcs = min(f // s, cs - k)
rp = p - s * max_pcs
rf = f - s * max_fcs
max_pcw = rp // w
max_fcw = rf // w
total = max_pcs + max_fcs + min(max_pcw + max_fcw, cw)
ans = max(ans, total)
for k in range(0, cw + 1):
max_pcw = min(p // w, k)
max_fcw = min(f // w, cw - k)
rp = p - w * max_pcw
rf = f - w * max_fcw
max_pcs = rp // s
max_fcs = rf // s
total = max_pcw + max_fcw + min(max_pcs + max_fcs, cs)
ans = max(ans, total)
print(ans)
t = int(input())
for _ in range(t):
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 ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR 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
input = sys.stdin.readline
def inInt():
return int(input())
def inStr():
return input().strip("\n")
def inIList():
return list(map(int, input().split()))
def inSList():
return input().split()
def solve(p, f, cs, cw, s, w):
pc, fc = 0, 0
sols = []
ss = False
if s < w:
ss = True
if ss:
for i in range(cs + 1):
pc = min(p // s, i)
pr = p - pc * s
fc = min(f // s, cs - i)
fr = f - fc * s
o = 0
o += pr // w
o += fr // w
o = min(cw, o)
sols.append(pc + fc + o)
print(max(sols))
else:
for i in range(cw + 1):
pc = min(p // w, i)
pr = p - pc * w
fc = min(f // w, cw - i)
fr = f - fc * w
o = 0
o += pr // s
o += fr // s
o = min(cs, o)
sols.append(pc + fc + o)
print(max(sols))
tests = inInt()
for t in range(tests):
p_and_f = inIList()
p = p_and_f[0]
f = p_and_f[1]
cs_and_cw = inIList()
cs = cs_and_cw[0]
cw = cs_and_cw[1]
s_and_w = inIList()
s = s_and_w[0]
w = s_and_w[1]
solve(p, f, cs, cw, s, w) | IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP 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 ASSIGN VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP 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 ASSIGN VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR 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$. | t = int(input())
while t:
t -= 1
p, f = map(int, input().split())
cs, cw = map(int, input().split())
s, w = map(int, input().split())
ans = 0
if s > w:
s, w = w, s
cs, cw = cw, cs
for n1 in range(cs + 1):
if n1 * s > p:
break
n2 = min((p - s * n1) // w, cw)
cs1 = cs - n1
cw1 = cw - n2
m1, m2 = 0, 0
if f > s * cs1:
m1 = cs1
m2 = min((f - s * cs1) // w, cw1)
else:
m1 = f // s
m2 = 0
ans = max(ans, n1 + n2 + m1 + m2)
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER 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 IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR 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 VAR NUMBER NUMBER IF VAR BIN_OP VAR VAR ASSIGN 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 NUMBER ASSIGN VAR FUNC_CALL VAR 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$. | 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())
maxi = 0
for j in range(min(cs, p // s) + 1):
k = min((p - s * j) // w, cw)
xcs = cs - j
xcw = cw - k
if s > w:
if w * xcw >= f:
m = f // w
l = 0
else:
m = xcw
l = min((f - m * w) // s, xcs)
elif s * xcs >= f:
l = f // s
m = 0
else:
l = xcs
m = min((f - s * l) // w, xcw)
maxi = max(maxi, l + m + j + k)
print(maxi) | 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 FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR 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 IF VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR 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.