description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of $n$ booths, arranged in a circle. The booths are numbered $1$ through $n$ clockwise with $n$ being adjacent to $1$. The $i$-th booths sells some candies for the price of $a_i$ burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most $T$ burles at the fair. However, he has some plan in mind for his path across the booths: at first, he visits booth number $1$; if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately; then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
-----Input-----
The first line contains two integers $n$ and $T$ ($1 \le n \le 2 \cdot 10^5$, $1 \le T \le 10^{18}$) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β the price of the single candy at booth number $i$.
-----Output-----
Print a single integer β the total number of candies Polycarp will buy.
-----Examples-----
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
-----Note-----
Let's consider the first example. Here are Polycarp's moves until he runs out of money: Booth $1$, buys candy for $5$, $T = 33$; Booth $2$, buys candy for $2$, $T = 31$; Booth $3$, buys candy for $5$, $T = 26$; Booth $1$, buys candy for $5$, $T = 21$; Booth $2$, buys candy for $2$, $T = 19$; Booth $3$, buys candy for $5$, $T = 14$; Booth $1$, buys candy for $5$, $T = 9$; Booth $2$, buys candy for $2$, $T = 7$; Booth $3$, buys candy for $5$, $T = 2$; Booth $1$, buys no candy, not enough money; Booth $2$, buys candy for $2$, $T = 0$.
No candy can be bought later. The total number of candies bought is $10$.
In the second example he has $1$ burle left at the end of his path, no candy can be bought with this amount. | def main():
n, t = map(int, input().split())
a = [int(i) for i in input().split()]
x = min(a)
ans = 0
while t >= x:
cnt = 0
s = 0
tau = t
for i in range(n):
if tau >= a[i]:
tau -= a[i]
s += a[i]
cnt += 1
ans += cnt * (t // s)
t %= s
print(ans)
main() | FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of $n$ booths, arranged in a circle. The booths are numbered $1$ through $n$ clockwise with $n$ being adjacent to $1$. The $i$-th booths sells some candies for the price of $a_i$ burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most $T$ burles at the fair. However, he has some plan in mind for his path across the booths: at first, he visits booth number $1$; if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately; then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
-----Input-----
The first line contains two integers $n$ and $T$ ($1 \le n \le 2 \cdot 10^5$, $1 \le T \le 10^{18}$) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β the price of the single candy at booth number $i$.
-----Output-----
Print a single integer β the total number of candies Polycarp will buy.
-----Examples-----
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
-----Note-----
Let's consider the first example. Here are Polycarp's moves until he runs out of money: Booth $1$, buys candy for $5$, $T = 33$; Booth $2$, buys candy for $2$, $T = 31$; Booth $3$, buys candy for $5$, $T = 26$; Booth $1$, buys candy for $5$, $T = 21$; Booth $2$, buys candy for $2$, $T = 19$; Booth $3$, buys candy for $5$, $T = 14$; Booth $1$, buys candy for $5$, $T = 9$; Booth $2$, buys candy for $2$, $T = 7$; Booth $3$, buys candy for $5$, $T = 2$; Booth $1$, buys no candy, not enough money; Booth $2$, buys candy for $2$, $T = 0$.
No candy can be bought later. The total number of candies bought is $10$.
In the second example he has $1$ burle left at the end of his path, no candy can be bought with this amount. | n, t = map(int, input().split())
a = list(map(int, input().split()))
ans, nn = 0, n
while t >= min(a) and nn > 0:
tot = sum(a)
ans += t // tot * nn
t %= tot
for i in range(n):
if t < a[i]:
nn -= 1
a[i] = 0
elif a[i] > 0:
t -= a[i]
ans += 1
print(ans) | ASSIGN VAR 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 VAR NUMBER VAR WHILE VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of $n$ booths, arranged in a circle. The booths are numbered $1$ through $n$ clockwise with $n$ being adjacent to $1$. The $i$-th booths sells some candies for the price of $a_i$ burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most $T$ burles at the fair. However, he has some plan in mind for his path across the booths: at first, he visits booth number $1$; if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately; then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
-----Input-----
The first line contains two integers $n$ and $T$ ($1 \le n \le 2 \cdot 10^5$, $1 \le T \le 10^{18}$) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β the price of the single candy at booth number $i$.
-----Output-----
Print a single integer β the total number of candies Polycarp will buy.
-----Examples-----
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
-----Note-----
Let's consider the first example. Here are Polycarp's moves until he runs out of money: Booth $1$, buys candy for $5$, $T = 33$; Booth $2$, buys candy for $2$, $T = 31$; Booth $3$, buys candy for $5$, $T = 26$; Booth $1$, buys candy for $5$, $T = 21$; Booth $2$, buys candy for $2$, $T = 19$; Booth $3$, buys candy for $5$, $T = 14$; Booth $1$, buys candy for $5$, $T = 9$; Booth $2$, buys candy for $2$, $T = 7$; Booth $3$, buys candy for $5$, $T = 2$; Booth $1$, buys no candy, not enough money; Booth $2$, buys candy for $2$, $T = 0$.
No candy can be bought later. The total number of candies bought is $10$.
In the second example he has $1$ burle left at the end of his path, no candy can be bought with this amount. | n, k = map(int, input().split())
l = [int(i) for i in input().split()]
cnt = 0
n = len(l)
k1 = k % sum(l)
mini = min(l)
def calc(k1):
cnt = 0
sm = 0
for i in l:
if k1 >= i:
k1 -= i
cnt += 1
sm += i
return cnt, sm
ans = k // sum(l) * n
k1 = k % sum(l)
while k1 >= mini:
cnt, sm = calc(k1)
ans += k1 // sm * cnt
k1 = k1 % sm
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of $n$ booths, arranged in a circle. The booths are numbered $1$ through $n$ clockwise with $n$ being adjacent to $1$. The $i$-th booths sells some candies for the price of $a_i$ burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most $T$ burles at the fair. However, he has some plan in mind for his path across the booths: at first, he visits booth number $1$; if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately; then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
-----Input-----
The first line contains two integers $n$ and $T$ ($1 \le n \le 2 \cdot 10^5$, $1 \le T \le 10^{18}$) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β the price of the single candy at booth number $i$.
-----Output-----
Print a single integer β the total number of candies Polycarp will buy.
-----Examples-----
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
-----Note-----
Let's consider the first example. Here are Polycarp's moves until he runs out of money: Booth $1$, buys candy for $5$, $T = 33$; Booth $2$, buys candy for $2$, $T = 31$; Booth $3$, buys candy for $5$, $T = 26$; Booth $1$, buys candy for $5$, $T = 21$; Booth $2$, buys candy for $2$, $T = 19$; Booth $3$, buys candy for $5$, $T = 14$; Booth $1$, buys candy for $5$, $T = 9$; Booth $2$, buys candy for $2$, $T = 7$; Booth $3$, buys candy for $5$, $T = 2$; Booth $1$, buys no candy, not enough money; Booth $2$, buys candy for $2$, $T = 0$.
No candy can be bought later. The total number of candies bought is $10$.
In the second example he has $1$ burle left at the end of his path, no candy can be bought with this amount. | n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
ans = 0
while k >= min(a):
s = 0
count = 0
for i in a:
if i <= k:
k -= i
s += i
count += 1
ans += k // s * count + count
k -= k // s * s
print(ans) | ASSIGN 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 NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR |
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of $n$ booths, arranged in a circle. The booths are numbered $1$ through $n$ clockwise with $n$ being adjacent to $1$. The $i$-th booths sells some candies for the price of $a_i$ burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most $T$ burles at the fair. However, he has some plan in mind for his path across the booths: at first, he visits booth number $1$; if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately; then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
-----Input-----
The first line contains two integers $n$ and $T$ ($1 \le n \le 2 \cdot 10^5$, $1 \le T \le 10^{18}$) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β the price of the single candy at booth number $i$.
-----Output-----
Print a single integer β the total number of candies Polycarp will buy.
-----Examples-----
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
-----Note-----
Let's consider the first example. Here are Polycarp's moves until he runs out of money: Booth $1$, buys candy for $5$, $T = 33$; Booth $2$, buys candy for $2$, $T = 31$; Booth $3$, buys candy for $5$, $T = 26$; Booth $1$, buys candy for $5$, $T = 21$; Booth $2$, buys candy for $2$, $T = 19$; Booth $3$, buys candy for $5$, $T = 14$; Booth $1$, buys candy for $5$, $T = 9$; Booth $2$, buys candy for $2$, $T = 7$; Booth $3$, buys candy for $5$, $T = 2$; Booth $1$, buys no candy, not enough money; Booth $2$, buys candy for $2$, $T = 0$.
No candy can be bought later. The total number of candies bought is $10$.
In the second example he has $1$ burle left at the end of his path, no candy can be bought with this amount. | import sys
def least_significant_bit(i):
return i & -i
class FenwickTree:
def __init__(self, values):
self.n = len(values) + 1
self.data = [0] * self.n
for i, v in enumerate(values):
self.add(i, v)
def add(self, index, value):
i = index + 1
while i < self.n:
self.data[i] += value
i += least_significant_bit(i)
def prefix_sum(self, index):
i = index + 1
result = 0
while i > 0:
result += self.data[i]
i -= least_significant_bit(i)
return result
def range_sum(self, start, end):
return self.prefix_sum(end) - self.prefix_sum(start - 1)
def main(args):
n, t = list(map(int, input().split()))
prices = list(map(int, input().split()))
state = [1] * len(prices)
fprices = FenwickTree(prices)
fstate = FenwickTree(state)
tot = 0
while fstate.prefix_sum(n - 1) > 0:
loop = fprices.prefix_sum(n - 1)
c = t // loop
tot += c * fstate.prefix_sum(n - 1)
t -= c * loop
while fprices.prefix_sum(n - 1) > t:
left, right = -1, n - 1
while left + 1 < right:
mid = (left + right) // 2
p = fprices.prefix_sum(mid)
if p <= t:
left = mid
else:
right = mid
fstate.add(right, -1)
fprices.add(right, -prices[right])
print(tot)
return 0
sys.exit(main(sys.argv)) | IMPORT FUNC_DEF RETURN BIN_OP VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN 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 BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR WHILE FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of $n$ booths, arranged in a circle. The booths are numbered $1$ through $n$ clockwise with $n$ being adjacent to $1$. The $i$-th booths sells some candies for the price of $a_i$ burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most $T$ burles at the fair. However, he has some plan in mind for his path across the booths: at first, he visits booth number $1$; if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately; then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
-----Input-----
The first line contains two integers $n$ and $T$ ($1 \le n \le 2 \cdot 10^5$, $1 \le T \le 10^{18}$) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β the price of the single candy at booth number $i$.
-----Output-----
Print a single integer β the total number of candies Polycarp will buy.
-----Examples-----
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
-----Note-----
Let's consider the first example. Here are Polycarp's moves until he runs out of money: Booth $1$, buys candy for $5$, $T = 33$; Booth $2$, buys candy for $2$, $T = 31$; Booth $3$, buys candy for $5$, $T = 26$; Booth $1$, buys candy for $5$, $T = 21$; Booth $2$, buys candy for $2$, $T = 19$; Booth $3$, buys candy for $5$, $T = 14$; Booth $1$, buys candy for $5$, $T = 9$; Booth $2$, buys candy for $2$, $T = 7$; Booth $3$, buys candy for $5$, $T = 2$; Booth $1$, buys no candy, not enough money; Booth $2$, buys candy for $2$, $T = 0$.
No candy can be bought later. The total number of candies bought is $10$.
In the second example he has $1$ burle left at the end of his path, no candy can be bought with this amount. | n, T = input().split()
n = int(n)
T = int(T)
tendas = list(map(int, input().split()))
doces = 0
minimo = min(tendas)
while T >= minimo:
soma = 0
cont = 0
for i in range(n):
if soma + tendas[i] <= T:
soma += tendas[i]
cont += 1
doces += cont * (T // soma)
T = T % soma
print(doces) | ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of $n$ booths, arranged in a circle. The booths are numbered $1$ through $n$ clockwise with $n$ being adjacent to $1$. The $i$-th booths sells some candies for the price of $a_i$ burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most $T$ burles at the fair. However, he has some plan in mind for his path across the booths: at first, he visits booth number $1$; if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately; then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
-----Input-----
The first line contains two integers $n$ and $T$ ($1 \le n \le 2 \cdot 10^5$, $1 \le T \le 10^{18}$) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β the price of the single candy at booth number $i$.
-----Output-----
Print a single integer β the total number of candies Polycarp will buy.
-----Examples-----
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
-----Note-----
Let's consider the first example. Here are Polycarp's moves until he runs out of money: Booth $1$, buys candy for $5$, $T = 33$; Booth $2$, buys candy for $2$, $T = 31$; Booth $3$, buys candy for $5$, $T = 26$; Booth $1$, buys candy for $5$, $T = 21$; Booth $2$, buys candy for $2$, $T = 19$; Booth $3$, buys candy for $5$, $T = 14$; Booth $1$, buys candy for $5$, $T = 9$; Booth $2$, buys candy for $2$, $T = 7$; Booth $3$, buys candy for $5$, $T = 2$; Booth $1$, buys no candy, not enough money; Booth $2$, buys candy for $2$, $T = 0$.
No candy can be bought later. The total number of candies bought is $10$.
In the second example he has $1$ burle left at the end of his path, no candy can be bought with this amount. | n, T = map(int, input().split())
a = list(map(int, input().split()))
cur = 0
ased = a[:]
ased.sort(key=lambda x: -x)
ased += [0]
s = sum(a)
r = 0
while cur != n:
while ased[cur] > T:
s -= ased[cur]
cur += 1
if s <= T and s > 0:
d, T = divmod(T, s)
r += d * (n - cur)
continue
for i in range(n):
if a[i] <= T:
T -= a[i]
r += 1
print(r) | ASSIGN VAR 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 NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of $n$ booths, arranged in a circle. The booths are numbered $1$ through $n$ clockwise with $n$ being adjacent to $1$. The $i$-th booths sells some candies for the price of $a_i$ burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most $T$ burles at the fair. However, he has some plan in mind for his path across the booths: at first, he visits booth number $1$; if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately; then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
-----Input-----
The first line contains two integers $n$ and $T$ ($1 \le n \le 2 \cdot 10^5$, $1 \le T \le 10^{18}$) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β the price of the single candy at booth number $i$.
-----Output-----
Print a single integer β the total number of candies Polycarp will buy.
-----Examples-----
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
-----Note-----
Let's consider the first example. Here are Polycarp's moves until he runs out of money: Booth $1$, buys candy for $5$, $T = 33$; Booth $2$, buys candy for $2$, $T = 31$; Booth $3$, buys candy for $5$, $T = 26$; Booth $1$, buys candy for $5$, $T = 21$; Booth $2$, buys candy for $2$, $T = 19$; Booth $3$, buys candy for $5$, $T = 14$; Booth $1$, buys candy for $5$, $T = 9$; Booth $2$, buys candy for $2$, $T = 7$; Booth $3$, buys candy for $5$, $T = 2$; Booth $1$, buys no candy, not enough money; Booth $2$, buys candy for $2$, $T = 0$.
No candy can be bought later. The total number of candies bought is $10$.
In the second example he has $1$ burle left at the end of his path, no candy can be bought with this amount. | from sys import stdin
input = stdin.readline
n, t = map(int, input().split())
lst = list(map(int, input().split()))
s = sum(lst)
x = min(lst)
candy = n * (t // s)
t -= s * (t // s)
while t >= x:
s, c = 0, 0
for i in lst:
if i <= t - s:
c += 1
s += i
candy += c * (t // s)
t -= s * (t // s)
print(candy) | ASSIGN VAR VAR ASSIGN VAR 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 FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR WHILE VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of $n$ booths, arranged in a circle. The booths are numbered $1$ through $n$ clockwise with $n$ being adjacent to $1$. The $i$-th booths sells some candies for the price of $a_i$ burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most $T$ burles at the fair. However, he has some plan in mind for his path across the booths: at first, he visits booth number $1$; if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately; then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
-----Input-----
The first line contains two integers $n$ and $T$ ($1 \le n \le 2 \cdot 10^5$, $1 \le T \le 10^{18}$) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β the price of the single candy at booth number $i$.
-----Output-----
Print a single integer β the total number of candies Polycarp will buy.
-----Examples-----
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
-----Note-----
Let's consider the first example. Here are Polycarp's moves until he runs out of money: Booth $1$, buys candy for $5$, $T = 33$; Booth $2$, buys candy for $2$, $T = 31$; Booth $3$, buys candy for $5$, $T = 26$; Booth $1$, buys candy for $5$, $T = 21$; Booth $2$, buys candy for $2$, $T = 19$; Booth $3$, buys candy for $5$, $T = 14$; Booth $1$, buys candy for $5$, $T = 9$; Booth $2$, buys candy for $2$, $T = 7$; Booth $3$, buys candy for $5$, $T = 2$; Booth $1$, buys no candy, not enough money; Booth $2$, buys candy for $2$, $T = 0$.
No candy can be bought later. The total number of candies bought is $10$.
In the second example he has $1$ burle left at the end of his path, no candy can be bought with this amount. | n, t = map(int, input().split())
a = list(map(int, input().split()))
b = []
for i in a:
if i <= t:
b.append(i)
c = 0
n = len(b)
s = sum(b)
while len(b) > 0 and t >= min(b):
if s <= t:
k = t // s
t -= s * k
c += n * k
else:
for i in b:
if i <= t:
t -= i
c += 1
d = b
z = []
for i in d:
if i <= t:
z.append(i)
b = z
n = len(b)
s = sum(b)
if n == 0:
break
print(c) | ASSIGN VAR 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 LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR FOR VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR |
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of $n$ booths, arranged in a circle. The booths are numbered $1$ through $n$ clockwise with $n$ being adjacent to $1$. The $i$-th booths sells some candies for the price of $a_i$ burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most $T$ burles at the fair. However, he has some plan in mind for his path across the booths: at first, he visits booth number $1$; if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately; then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
-----Input-----
The first line contains two integers $n$ and $T$ ($1 \le n \le 2 \cdot 10^5$, $1 \le T \le 10^{18}$) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β the price of the single candy at booth number $i$.
-----Output-----
Print a single integer β the total number of candies Polycarp will buy.
-----Examples-----
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
-----Note-----
Let's consider the first example. Here are Polycarp's moves until he runs out of money: Booth $1$, buys candy for $5$, $T = 33$; Booth $2$, buys candy for $2$, $T = 31$; Booth $3$, buys candy for $5$, $T = 26$; Booth $1$, buys candy for $5$, $T = 21$; Booth $2$, buys candy for $2$, $T = 19$; Booth $3$, buys candy for $5$, $T = 14$; Booth $1$, buys candy for $5$, $T = 9$; Booth $2$, buys candy for $2$, $T = 7$; Booth $3$, buys candy for $5$, $T = 2$; Booth $1$, buys no candy, not enough money; Booth $2$, buys candy for $2$, $T = 0$.
No candy can be bought later. The total number of candies bought is $10$.
In the second example he has $1$ burle left at the end of his path, no candy can be bought with this amount. | def splitInput():
return list(map(int, input().split()))
nT = splitInput()
money = nT[1]
kioski = list(filter(lambda x: x <= money, splitInput()))
konfet = 0
if len(kioski) > 0:
minKioskiPrice = min(kioski)
while money >= minKioskiPrice:
sumKioski = sum(kioski)
countCycles = money // sumKioski
if countCycles > 0:
konfet += countCycles * len(kioski)
money -= sumKioski * countCycles
for k in kioski:
if k <= money:
money -= k
konfet += 1
if money < minKioskiPrice:
break
kioski = list(filter(lambda x: x <= money, kioski))
print(konfet) | FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR FOR VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of $n$ booths, arranged in a circle. The booths are numbered $1$ through $n$ clockwise with $n$ being adjacent to $1$. The $i$-th booths sells some candies for the price of $a_i$ burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most $T$ burles at the fair. However, he has some plan in mind for his path across the booths: at first, he visits booth number $1$; if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately; then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
-----Input-----
The first line contains two integers $n$ and $T$ ($1 \le n \le 2 \cdot 10^5$, $1 \le T \le 10^{18}$) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β the price of the single candy at booth number $i$.
-----Output-----
Print a single integer β the total number of candies Polycarp will buy.
-----Examples-----
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
-----Note-----
Let's consider the first example. Here are Polycarp's moves until he runs out of money: Booth $1$, buys candy for $5$, $T = 33$; Booth $2$, buys candy for $2$, $T = 31$; Booth $3$, buys candy for $5$, $T = 26$; Booth $1$, buys candy for $5$, $T = 21$; Booth $2$, buys candy for $2$, $T = 19$; Booth $3$, buys candy for $5$, $T = 14$; Booth $1$, buys candy for $5$, $T = 9$; Booth $2$, buys candy for $2$, $T = 7$; Booth $3$, buys candy for $5$, $T = 2$; Booth $1$, buys no candy, not enough money; Booth $2$, buys candy for $2$, $T = 0$.
No candy can be bought later. The total number of candies bought is $10$.
In the second example he has $1$ burle left at the end of his path, no candy can be bought with this amount. | n, t = input().strip().split(" ")
n, t = int(n), int(t)
arr = list(map(int, input().strip().split(" ")))
a = 0
flag = 1
while flag and t:
cnt = 0
x = t
y = 0
flag = 0
for i in arr:
if x >= i:
cnt += 1
x -= i
y += i
flag = 1
if flag == 0:
break
ans = t // y
t -= y * ans
cnt *= ans
a += cnt
print(a) | ASSIGN VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of $n$ booths, arranged in a circle. The booths are numbered $1$ through $n$ clockwise with $n$ being adjacent to $1$. The $i$-th booths sells some candies for the price of $a_i$ burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most $T$ burles at the fair. However, he has some plan in mind for his path across the booths: at first, he visits booth number $1$; if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately; then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
-----Input-----
The first line contains two integers $n$ and $T$ ($1 \le n \le 2 \cdot 10^5$, $1 \le T \le 10^{18}$) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β the price of the single candy at booth number $i$.
-----Output-----
Print a single integer β the total number of candies Polycarp will buy.
-----Examples-----
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
-----Note-----
Let's consider the first example. Here are Polycarp's moves until he runs out of money: Booth $1$, buys candy for $5$, $T = 33$; Booth $2$, buys candy for $2$, $T = 31$; Booth $3$, buys candy for $5$, $T = 26$; Booth $1$, buys candy for $5$, $T = 21$; Booth $2$, buys candy for $2$, $T = 19$; Booth $3$, buys candy for $5$, $T = 14$; Booth $1$, buys candy for $5$, $T = 9$; Booth $2$, buys candy for $2$, $T = 7$; Booth $3$, buys candy for $5$, $T = 2$; Booth $1$, buys no candy, not enough money; Booth $2$, buys candy for $2$, $T = 0$.
No candy can be bought later. The total number of candies bought is $10$.
In the second example he has $1$ burle left at the end of his path, no candy can be bought with this amount. | import sys
def main(args):
n, t = list(map(int, input().split()))
prices = list(map(int, input().split()))
loop_prices = sum(prices)
loop_n = n
tot = 0
while loop_n > 0:
c = t // loop_prices
tot += c * loop_n
t -= c * loop_prices
loop_prices = 0
for i, v in enumerate(prices):
if loop_prices + v > t:
prices[i] = 0
loop_n -= 1
else:
loop_prices += v
print(tot)
sys.exit(main(sys.argv)) | IMPORT FUNC_DEF ASSIGN 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 VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of $n$ booths, arranged in a circle. The booths are numbered $1$ through $n$ clockwise with $n$ being adjacent to $1$. The $i$-th booths sells some candies for the price of $a_i$ burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most $T$ burles at the fair. However, he has some plan in mind for his path across the booths: at first, he visits booth number $1$; if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately; then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
-----Input-----
The first line contains two integers $n$ and $T$ ($1 \le n \le 2 \cdot 10^5$, $1 \le T \le 10^{18}$) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β the price of the single candy at booth number $i$.
-----Output-----
Print a single integer β the total number of candies Polycarp will buy.
-----Examples-----
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
-----Note-----
Let's consider the first example. Here are Polycarp's moves until he runs out of money: Booth $1$, buys candy for $5$, $T = 33$; Booth $2$, buys candy for $2$, $T = 31$; Booth $3$, buys candy for $5$, $T = 26$; Booth $1$, buys candy for $5$, $T = 21$; Booth $2$, buys candy for $2$, $T = 19$; Booth $3$, buys candy for $5$, $T = 14$; Booth $1$, buys candy for $5$, $T = 9$; Booth $2$, buys candy for $2$, $T = 7$; Booth $3$, buys candy for $5$, $T = 2$; Booth $1$, buys no candy, not enough money; Booth $2$, buys candy for $2$, $T = 0$.
No candy can be bought later. The total number of candies bought is $10$.
In the second example he has $1$ burle left at the end of his path, no candy can be bought with this amount. | n, t = map(int, input().split())
arr = list(map(int, input().split()))
ans = 0
while True:
tmp = list()
paid = 0
for v in arr:
if v <= t:
tmp.append(v)
t -= v
paid += v
if paid != 0:
ans += len(tmp) * (1 + t // paid)
t %= paid
else:
print(ans)
exit(0)
arr = tmp | ASSIGN VAR 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 NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR |
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of $n$ booths, arranged in a circle. The booths are numbered $1$ through $n$ clockwise with $n$ being adjacent to $1$. The $i$-th booths sells some candies for the price of $a_i$ burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most $T$ burles at the fair. However, he has some plan in mind for his path across the booths: at first, he visits booth number $1$; if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately; then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
-----Input-----
The first line contains two integers $n$ and $T$ ($1 \le n \le 2 \cdot 10^5$, $1 \le T \le 10^{18}$) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β the price of the single candy at booth number $i$.
-----Output-----
Print a single integer β the total number of candies Polycarp will buy.
-----Examples-----
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
-----Note-----
Let's consider the first example. Here are Polycarp's moves until he runs out of money: Booth $1$, buys candy for $5$, $T = 33$; Booth $2$, buys candy for $2$, $T = 31$; Booth $3$, buys candy for $5$, $T = 26$; Booth $1$, buys candy for $5$, $T = 21$; Booth $2$, buys candy for $2$, $T = 19$; Booth $3$, buys candy for $5$, $T = 14$; Booth $1$, buys candy for $5$, $T = 9$; Booth $2$, buys candy for $2$, $T = 7$; Booth $3$, buys candy for $5$, $T = 2$; Booth $1$, buys no candy, not enough money; Booth $2$, buys candy for $2$, $T = 0$.
No candy can be bought later. The total number of candies bought is $10$.
In the second example he has $1$ burle left at the end of his path, no candy can be bought with this amount. | n, t = map(int, input().split())
a = list(map(int, input().split()))
sum1 = sum(a)
b = a.copy()
b.sort()
cnt = n * (t // sum1)
t %= sum1
i = 0
N = n
while n != 0 and t != 0:
while len(b) and b[-1] > t:
sum1 -= b[-1]
b.pop()
n -= 1
if not len(b):
break
while a[i] > t:
i += 1
i %= N
if sum1 == 0:
break
t -= a[i]
cnt += 1
cnt += n * (t // sum1)
t %= sum1
i += 1
i %= N
print(cnt) | ASSIGN VAR 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 FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR NUMBER WHILE FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR VAR IF VAR NUMBER VAR VAR VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of $n$ booths, arranged in a circle. The booths are numbered $1$ through $n$ clockwise with $n$ being adjacent to $1$. The $i$-th booths sells some candies for the price of $a_i$ burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most $T$ burles at the fair. However, he has some plan in mind for his path across the booths: at first, he visits booth number $1$; if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately; then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
-----Input-----
The first line contains two integers $n$ and $T$ ($1 \le n \le 2 \cdot 10^5$, $1 \le T \le 10^{18}$) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β the price of the single candy at booth number $i$.
-----Output-----
Print a single integer β the total number of candies Polycarp will buy.
-----Examples-----
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
-----Note-----
Let's consider the first example. Here are Polycarp's moves until he runs out of money: Booth $1$, buys candy for $5$, $T = 33$; Booth $2$, buys candy for $2$, $T = 31$; Booth $3$, buys candy for $5$, $T = 26$; Booth $1$, buys candy for $5$, $T = 21$; Booth $2$, buys candy for $2$, $T = 19$; Booth $3$, buys candy for $5$, $T = 14$; Booth $1$, buys candy for $5$, $T = 9$; Booth $2$, buys candy for $2$, $T = 7$; Booth $3$, buys candy for $5$, $T = 2$; Booth $1$, buys no candy, not enough money; Booth $2$, buys candy for $2$, $T = 0$.
No candy can be bought later. The total number of candies bought is $10$.
In the second example he has $1$ burle left at the end of his path, no candy can be bought with this amount. | n, t = map(int, input().split())
a = list(map(int, input().split()))
x = min(a)
cands = 0
while t >= x:
y = sum(a)
if t >= y:
f = len(a)
cands += t // y * f
t = t - t // y * y
else:
for item in a:
if t >= item:
t = t - item
cands += 1
a = [_ for _ in a if _ <= t]
print(cands) | ASSIGN VAR 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 WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of $n$ booths, arranged in a circle. The booths are numbered $1$ through $n$ clockwise with $n$ being adjacent to $1$. The $i$-th booths sells some candies for the price of $a_i$ burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most $T$ burles at the fair. However, he has some plan in mind for his path across the booths: at first, he visits booth number $1$; if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately; then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
-----Input-----
The first line contains two integers $n$ and $T$ ($1 \le n \le 2 \cdot 10^5$, $1 \le T \le 10^{18}$) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β the price of the single candy at booth number $i$.
-----Output-----
Print a single integer β the total number of candies Polycarp will buy.
-----Examples-----
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
-----Note-----
Let's consider the first example. Here are Polycarp's moves until he runs out of money: Booth $1$, buys candy for $5$, $T = 33$; Booth $2$, buys candy for $2$, $T = 31$; Booth $3$, buys candy for $5$, $T = 26$; Booth $1$, buys candy for $5$, $T = 21$; Booth $2$, buys candy for $2$, $T = 19$; Booth $3$, buys candy for $5$, $T = 14$; Booth $1$, buys candy for $5$, $T = 9$; Booth $2$, buys candy for $2$, $T = 7$; Booth $3$, buys candy for $5$, $T = 2$; Booth $1$, buys no candy, not enough money; Booth $2$, buys candy for $2$, $T = 0$.
No candy can be bought later. The total number of candies bought is $10$.
In the second example he has $1$ burle left at the end of his path, no candy can be bought with this amount. | import sys
input = sys.stdin.readline
I = lambda: list(map(int, input().split()))
n, t = I()
l = I()
s = sum(l)
an = t // s * n
t = t % s
i = 0
mi = min(l)
while t >= mi:
c = k = i = 0
while i < n:
if c + l[i] <= t:
k += 1
c += l[i]
i += 1
if c == 0:
break
an += t // c * k
t = t % c
print(an) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of $n$ booths, arranged in a circle. The booths are numbered $1$ through $n$ clockwise with $n$ being adjacent to $1$. The $i$-th booths sells some candies for the price of $a_i$ burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most $T$ burles at the fair. However, he has some plan in mind for his path across the booths: at first, he visits booth number $1$; if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately; then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
-----Input-----
The first line contains two integers $n$ and $T$ ($1 \le n \le 2 \cdot 10^5$, $1 \le T \le 10^{18}$) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β the price of the single candy at booth number $i$.
-----Output-----
Print a single integer β the total number of candies Polycarp will buy.
-----Examples-----
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
-----Note-----
Let's consider the first example. Here are Polycarp's moves until he runs out of money: Booth $1$, buys candy for $5$, $T = 33$; Booth $2$, buys candy for $2$, $T = 31$; Booth $3$, buys candy for $5$, $T = 26$; Booth $1$, buys candy for $5$, $T = 21$; Booth $2$, buys candy for $2$, $T = 19$; Booth $3$, buys candy for $5$, $T = 14$; Booth $1$, buys candy for $5$, $T = 9$; Booth $2$, buys candy for $2$, $T = 7$; Booth $3$, buys candy for $5$, $T = 2$; Booth $1$, buys no candy, not enough money; Booth $2$, buys candy for $2$, $T = 0$.
No candy can be bought later. The total number of candies bought is $10$.
In the second example he has $1$ burle left at the end of his path, no candy can be bought with this amount. | n, T = list(map(int, input().split()))
a = list(map(int, input().split()))
summ = []
cur = 0
mmin = a[0]
for item in a:
cur += item
summ.append(cur)
mmin = min(item, mmin)
def helper(x, l, r=n - 1):
z = l
while l < r:
mid = (l + r) // 2
cur = summ[mid] - summ[z] + a[z]
if cur <= x:
l = mid + 1
else:
r = mid
if summ[l] - summ[z] + a[z] > x:
return l
else:
return l + 1
def helper2(x, l, r=n - 1):
cur = l
while cur <= r and summ[cur] - summ[l] + a[l] <= x:
cur += 1
return cur
ans = 0
left = T
d = {}
while T >= mmin:
l = 0
t, num = 0, 0
left = T
while left >= mmin and l < n:
r = helper2(left, l)
if l == r:
l += 1
continue
t += summ[r - 1] - summ[l] + a[l]
num += r - l
left -= summ[r - 1] - summ[l] + a[l]
l = r + 1
ans += T // t * num
T = T % t
print(ans) | ASSIGN 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 LIST ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_DEF BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR RETURN VAR RETURN BIN_OP VAR NUMBER FUNC_DEF BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR DICT WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of $n$ booths, arranged in a circle. The booths are numbered $1$ through $n$ clockwise with $n$ being adjacent to $1$. The $i$-th booths sells some candies for the price of $a_i$ burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most $T$ burles at the fair. However, he has some plan in mind for his path across the booths: at first, he visits booth number $1$; if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately; then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
-----Input-----
The first line contains two integers $n$ and $T$ ($1 \le n \le 2 \cdot 10^5$, $1 \le T \le 10^{18}$) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β the price of the single candy at booth number $i$.
-----Output-----
Print a single integer β the total number of candies Polycarp will buy.
-----Examples-----
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
-----Note-----
Let's consider the first example. Here are Polycarp's moves until he runs out of money: Booth $1$, buys candy for $5$, $T = 33$; Booth $2$, buys candy for $2$, $T = 31$; Booth $3$, buys candy for $5$, $T = 26$; Booth $1$, buys candy for $5$, $T = 21$; Booth $2$, buys candy for $2$, $T = 19$; Booth $3$, buys candy for $5$, $T = 14$; Booth $1$, buys candy for $5$, $T = 9$; Booth $2$, buys candy for $2$, $T = 7$; Booth $3$, buys candy for $5$, $T = 2$; Booth $1$, buys no candy, not enough money; Booth $2$, buys candy for $2$, $T = 0$.
No candy can be bought later. The total number of candies bought is $10$.
In the second example he has $1$ burle left at the end of his path, no candy can be bought with this amount. | w = input().split(" ")
n = int(w[0])
t = int(w[1])
w = input().split(" ")
a = []
for i in w:
a.append(int(i))
sm = 0
while True:
c_candies = 0
c_sum = 0
for i in range(n):
if t >= a[i]:
c_candies += 1
sm += 1
c_sum += a[i]
t -= a[i]
if c_candies == 0:
print(sm)
exit(0)
sm += c_candies * (t // c_sum)
t = t % c_sum | ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR |
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of $n$ booths, arranged in a circle. The booths are numbered $1$ through $n$ clockwise with $n$ being adjacent to $1$. The $i$-th booths sells some candies for the price of $a_i$ burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most $T$ burles at the fair. However, he has some plan in mind for his path across the booths: at first, he visits booth number $1$; if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately; then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
-----Input-----
The first line contains two integers $n$ and $T$ ($1 \le n \le 2 \cdot 10^5$, $1 \le T \le 10^{18}$) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β the price of the single candy at booth number $i$.
-----Output-----
Print a single integer β the total number of candies Polycarp will buy.
-----Examples-----
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
-----Note-----
Let's consider the first example. Here are Polycarp's moves until he runs out of money: Booth $1$, buys candy for $5$, $T = 33$; Booth $2$, buys candy for $2$, $T = 31$; Booth $3$, buys candy for $5$, $T = 26$; Booth $1$, buys candy for $5$, $T = 21$; Booth $2$, buys candy for $2$, $T = 19$; Booth $3$, buys candy for $5$, $T = 14$; Booth $1$, buys candy for $5$, $T = 9$; Booth $2$, buys candy for $2$, $T = 7$; Booth $3$, buys candy for $5$, $T = 2$; Booth $1$, buys no candy, not enough money; Booth $2$, buys candy for $2$, $T = 0$.
No candy can be bought later. The total number of candies bought is $10$.
In the second example he has $1$ burle left at the end of his path, no candy can be bought with this amount. | n, t = map(int, input().split())
l = list(map(int, input().split()))
p = sum(l)
s = 0
s += n * (t // p)
t -= p * (t // p)
x = min(l)
i = 0
while t >= x:
countinround = 0
costround = 0
p = t
for i in l:
if p >= i:
countinround += 1
costround += i
p -= i
if costround == 0:
break
rounds = t // costround
t -= rounds * costround
count = rounds * countinround
s += count
print(s) | ASSIGN VAR 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 VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of $n$ booths, arranged in a circle. The booths are numbered $1$ through $n$ clockwise with $n$ being adjacent to $1$. The $i$-th booths sells some candies for the price of $a_i$ burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most $T$ burles at the fair. However, he has some plan in mind for his path across the booths: at first, he visits booth number $1$; if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately; then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
-----Input-----
The first line contains two integers $n$ and $T$ ($1 \le n \le 2 \cdot 10^5$, $1 \le T \le 10^{18}$) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β the price of the single candy at booth number $i$.
-----Output-----
Print a single integer β the total number of candies Polycarp will buy.
-----Examples-----
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
-----Note-----
Let's consider the first example. Here are Polycarp's moves until he runs out of money: Booth $1$, buys candy for $5$, $T = 33$; Booth $2$, buys candy for $2$, $T = 31$; Booth $3$, buys candy for $5$, $T = 26$; Booth $1$, buys candy for $5$, $T = 21$; Booth $2$, buys candy for $2$, $T = 19$; Booth $3$, buys candy for $5$, $T = 14$; Booth $1$, buys candy for $5$, $T = 9$; Booth $2$, buys candy for $2$, $T = 7$; Booth $3$, buys candy for $5$, $T = 2$; Booth $1$, buys no candy, not enough money; Booth $2$, buys candy for $2$, $T = 0$.
No candy can be bought later. The total number of candies bought is $10$.
In the second example he has $1$ burle left at the end of his path, no candy can be bought with this amount. | n, t = list(map(int, input().split()))
a = list(map(int, input().split()))
_ = min(a)
r = 0
s = sum(a)
n_ = n
while t >= _:
v = int(t / s)
r += v * n_
t -= v * s
for i in range(n):
if a[i] == 0:
continue
if t >= a[i]:
r += 1
t -= a[i]
else:
n_ -= 1
s -= a[i]
a[i] = 0
print(r) | ASSIGN 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 VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of $n$ booths, arranged in a circle. The booths are numbered $1$ through $n$ clockwise with $n$ being adjacent to $1$. The $i$-th booths sells some candies for the price of $a_i$ burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most $T$ burles at the fair. However, he has some plan in mind for his path across the booths: at first, he visits booth number $1$; if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately; then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
-----Input-----
The first line contains two integers $n$ and $T$ ($1 \le n \le 2 \cdot 10^5$, $1 \le T \le 10^{18}$) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β the price of the single candy at booth number $i$.
-----Output-----
Print a single integer β the total number of candies Polycarp will buy.
-----Examples-----
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
-----Note-----
Let's consider the first example. Here are Polycarp's moves until he runs out of money: Booth $1$, buys candy for $5$, $T = 33$; Booth $2$, buys candy for $2$, $T = 31$; Booth $3$, buys candy for $5$, $T = 26$; Booth $1$, buys candy for $5$, $T = 21$; Booth $2$, buys candy for $2$, $T = 19$; Booth $3$, buys candy for $5$, $T = 14$; Booth $1$, buys candy for $5$, $T = 9$; Booth $2$, buys candy for $2$, $T = 7$; Booth $3$, buys candy for $5$, $T = 2$; Booth $1$, buys no candy, not enough money; Booth $2$, buys candy for $2$, $T = 0$.
No candy can be bought later. The total number of candies bought is $10$.
In the second example he has $1$ burle left at the end of his path, no candy can be bought with this amount. | line1 = input().split()
line2 = input().split()
Booths, Money = map(int, line1)
Price = list(map(int, line2))
Count = 0
cur = 0
mini = min(Price)
Skip = set()
pre = Money
pre_count = 0
while Money >= mini:
if cur not in Skip:
if Price[cur] <= Money:
Money -= Price[cur]
Count += 1
else:
Skip.add(cur)
cur += 1
if cur == Booths:
cycle1 = pre - Money
count1 = Count - pre_count
round = Money // cycle1
Money %= cycle1
Count += count1 * round
pre = Money
pre_count = Count
cur = 0
print(Count) | ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR |
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of $n$ booths, arranged in a circle. The booths are numbered $1$ through $n$ clockwise with $n$ being adjacent to $1$. The $i$-th booths sells some candies for the price of $a_i$ burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most $T$ burles at the fair. However, he has some plan in mind for his path across the booths: at first, he visits booth number $1$; if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately; then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
-----Input-----
The first line contains two integers $n$ and $T$ ($1 \le n \le 2 \cdot 10^5$, $1 \le T \le 10^{18}$) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β the price of the single candy at booth number $i$.
-----Output-----
Print a single integer β the total number of candies Polycarp will buy.
-----Examples-----
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
-----Note-----
Let's consider the first example. Here are Polycarp's moves until he runs out of money: Booth $1$, buys candy for $5$, $T = 33$; Booth $2$, buys candy for $2$, $T = 31$; Booth $3$, buys candy for $5$, $T = 26$; Booth $1$, buys candy for $5$, $T = 21$; Booth $2$, buys candy for $2$, $T = 19$; Booth $3$, buys candy for $5$, $T = 14$; Booth $1$, buys candy for $5$, $T = 9$; Booth $2$, buys candy for $2$, $T = 7$; Booth $3$, buys candy for $5$, $T = 2$; Booth $1$, buys no candy, not enough money; Booth $2$, buys candy for $2$, $T = 0$.
No candy can be bought later. The total number of candies bought is $10$.
In the second example he has $1$ burle left at the end of his path, no candy can be bought with this amount. | def buy(n, t, c, s):
if len(c) == 0:
return 0
ans = 0
if t >= s:
ans += n * (t // s)
t %= s
nc = []
for i in range(n):
if t >= c[i]:
nc.append(c[i])
t -= c[i]
ans += 1
else:
s -= c[i]
ans += buy(len(nc), t, nc, s)
return ans
def main():
n, t = map(lambda x: int(x), input().split(" "))
c = list(map(lambda x: int(x), input().split(" ")))
s = sum(c)
print(buy(n, t, c, s))
main() | FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR |
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of $n$ booths, arranged in a circle. The booths are numbered $1$ through $n$ clockwise with $n$ being adjacent to $1$. The $i$-th booths sells some candies for the price of $a_i$ burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most $T$ burles at the fair. However, he has some plan in mind for his path across the booths: at first, he visits booth number $1$; if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately; then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
-----Input-----
The first line contains two integers $n$ and $T$ ($1 \le n \le 2 \cdot 10^5$, $1 \le T \le 10^{18}$) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β the price of the single candy at booth number $i$.
-----Output-----
Print a single integer β the total number of candies Polycarp will buy.
-----Examples-----
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
-----Note-----
Let's consider the first example. Here are Polycarp's moves until he runs out of money: Booth $1$, buys candy for $5$, $T = 33$; Booth $2$, buys candy for $2$, $T = 31$; Booth $3$, buys candy for $5$, $T = 26$; Booth $1$, buys candy for $5$, $T = 21$; Booth $2$, buys candy for $2$, $T = 19$; Booth $3$, buys candy for $5$, $T = 14$; Booth $1$, buys candy for $5$, $T = 9$; Booth $2$, buys candy for $2$, $T = 7$; Booth $3$, buys candy for $5$, $T = 2$; Booth $1$, buys no candy, not enough money; Booth $2$, buys candy for $2$, $T = 0$.
No candy can be bought later. The total number of candies bought is $10$.
In the second example he has $1$ burle left at the end of his path, no candy can be bought with this amount. | n, t = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
while True:
b = False
k = 0
s = 0
for i in range(n):
if t >= a[i]:
s += a[i]
k += 1
b = True
if not b:
break
if t < s:
for i in range(n):
if t >= a[i]:
ans += 1
t -= a[i]
else:
ans += t // s * k
t %= s
print(ans) | ASSIGN VAR 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 NUMBER WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of $n$ booths, arranged in a circle. The booths are numbered $1$ through $n$ clockwise with $n$ being adjacent to $1$. The $i$-th booths sells some candies for the price of $a_i$ burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most $T$ burles at the fair. However, he has some plan in mind for his path across the booths: at first, he visits booth number $1$; if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately; then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
-----Input-----
The first line contains two integers $n$ and $T$ ($1 \le n \le 2 \cdot 10^5$, $1 \le T \le 10^{18}$) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β the price of the single candy at booth number $i$.
-----Output-----
Print a single integer β the total number of candies Polycarp will buy.
-----Examples-----
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
-----Note-----
Let's consider the first example. Here are Polycarp's moves until he runs out of money: Booth $1$, buys candy for $5$, $T = 33$; Booth $2$, buys candy for $2$, $T = 31$; Booth $3$, buys candy for $5$, $T = 26$; Booth $1$, buys candy for $5$, $T = 21$; Booth $2$, buys candy for $2$, $T = 19$; Booth $3$, buys candy for $5$, $T = 14$; Booth $1$, buys candy for $5$, $T = 9$; Booth $2$, buys candy for $2$, $T = 7$; Booth $3$, buys candy for $5$, $T = 2$; Booth $1$, buys no candy, not enough money; Booth $2$, buys candy for $2$, $T = 0$.
No candy can be bought later. The total number of candies bought is $10$.
In the second example he has $1$ burle left at the end of his path, no candy can be bought with this amount. | n, t = map(int, input().split())
u = list(map(int, input().split()))
ans = 0
s = sum(u)
while 1 == 1:
ans += t // s * n
t = t % s
t1 = t
u1 = []
for i in range(n):
if u[i] <= t:
t -= u[i]
u1.append(u[i])
ans += 1
else:
s -= u[i]
n -= 1
u = u1[:]
if t == t1:
break
print(ans) | ASSIGN VAR 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 NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR |
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of $n$ booths, arranged in a circle. The booths are numbered $1$ through $n$ clockwise with $n$ being adjacent to $1$. The $i$-th booths sells some candies for the price of $a_i$ burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most $T$ burles at the fair. However, he has some plan in mind for his path across the booths: at first, he visits booth number $1$; if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately; then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
-----Input-----
The first line contains two integers $n$ and $T$ ($1 \le n \le 2 \cdot 10^5$, $1 \le T \le 10^{18}$) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β the price of the single candy at booth number $i$.
-----Output-----
Print a single integer β the total number of candies Polycarp will buy.
-----Examples-----
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
-----Note-----
Let's consider the first example. Here are Polycarp's moves until he runs out of money: Booth $1$, buys candy for $5$, $T = 33$; Booth $2$, buys candy for $2$, $T = 31$; Booth $3$, buys candy for $5$, $T = 26$; Booth $1$, buys candy for $5$, $T = 21$; Booth $2$, buys candy for $2$, $T = 19$; Booth $3$, buys candy for $5$, $T = 14$; Booth $1$, buys candy for $5$, $T = 9$; Booth $2$, buys candy for $2$, $T = 7$; Booth $3$, buys candy for $5$, $T = 2$; Booth $1$, buys no candy, not enough money; Booth $2$, buys candy for $2$, $T = 0$.
No candy can be bought later. The total number of candies bought is $10$.
In the second example he has $1$ burle left at the end of his path, no candy can be bought with this amount. | def inint():
return int(input())
def inlist():
return list(map(int, input().split()))
def main():
n, t = inlist()
a = inlist()
sol = 0
while True:
ss = 0
amt = 0
for i in a:
if i <= t:
sol += 1
ss += i
t -= i
amt += 1
if amt == 0:
break
sol += t // ss * amt
t = t % ss
print(sol)
main() | FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of $n$ booths, arranged in a circle. The booths are numbered $1$ through $n$ clockwise with $n$ being adjacent to $1$. The $i$-th booths sells some candies for the price of $a_i$ burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most $T$ burles at the fair. However, he has some plan in mind for his path across the booths: at first, he visits booth number $1$; if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately; then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
-----Input-----
The first line contains two integers $n$ and $T$ ($1 \le n \le 2 \cdot 10^5$, $1 \le T \le 10^{18}$) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β the price of the single candy at booth number $i$.
-----Output-----
Print a single integer β the total number of candies Polycarp will buy.
-----Examples-----
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
-----Note-----
Let's consider the first example. Here are Polycarp's moves until he runs out of money: Booth $1$, buys candy for $5$, $T = 33$; Booth $2$, buys candy for $2$, $T = 31$; Booth $3$, buys candy for $5$, $T = 26$; Booth $1$, buys candy for $5$, $T = 21$; Booth $2$, buys candy for $2$, $T = 19$; Booth $3$, buys candy for $5$, $T = 14$; Booth $1$, buys candy for $5$, $T = 9$; Booth $2$, buys candy for $2$, $T = 7$; Booth $3$, buys candy for $5$, $T = 2$; Booth $1$, buys no candy, not enough money; Booth $2$, buys candy for $2$, $T = 0$.
No candy can be bought later. The total number of candies bought is $10$.
In the second example he has $1$ burle left at the end of his path, no candy can be bought with this amount. | n, m = map(int, input().split())
t = list(map(int, input().split()))
wyn = 0
t_del = []
t2 = []
t_pom = []
t_min = 999999999999999999
tmp_sum = sum(t)
while True:
tmp_sum = 0
for y in range(0, len(t), +1):
if m < t[y]:
continue
m -= t[y]
wyn += 1
t_pom.append(t[y])
tmp_sum += t[y]
if t[y] <= t_min:
t_min = t[y]
t = list(t_pom)
t_pom = []
if len(t) == 0 or m == 0 or m < t_min:
break
if tmp_sum <= m:
wyn += len(t) * int(m / tmp_sum)
m -= int(m / tmp_sum) * tmp_sum
if m == 0:
print(wyn)
exit(0)
print(wyn) | ASSIGN VAR 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 NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR IF VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of $n$ booths, arranged in a circle. The booths are numbered $1$ through $n$ clockwise with $n$ being adjacent to $1$. The $i$-th booths sells some candies for the price of $a_i$ burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most $T$ burles at the fair. However, he has some plan in mind for his path across the booths: at first, he visits booth number $1$; if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately; then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
-----Input-----
The first line contains two integers $n$ and $T$ ($1 \le n \le 2 \cdot 10^5$, $1 \le T \le 10^{18}$) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β the price of the single candy at booth number $i$.
-----Output-----
Print a single integer β the total number of candies Polycarp will buy.
-----Examples-----
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
-----Note-----
Let's consider the first example. Here are Polycarp's moves until he runs out of money: Booth $1$, buys candy for $5$, $T = 33$; Booth $2$, buys candy for $2$, $T = 31$; Booth $3$, buys candy for $5$, $T = 26$; Booth $1$, buys candy for $5$, $T = 21$; Booth $2$, buys candy for $2$, $T = 19$; Booth $3$, buys candy for $5$, $T = 14$; Booth $1$, buys candy for $5$, $T = 9$; Booth $2$, buys candy for $2$, $T = 7$; Booth $3$, buys candy for $5$, $T = 2$; Booth $1$, buys no candy, not enough money; Booth $2$, buys candy for $2$, $T = 0$.
No candy can be bought later. The total number of candies bought is $10$.
In the second example he has $1$ burle left at the end of his path, no candy can be bought with this amount. | I = lambda: list(map(int, input().split()))
n, have = I()
l = I()
ans = 0
while have:
got = 0
temp = 0
i = 0
while i < n:
if have - (temp + l[i]) >= 0:
temp += l[i]
got += 1
i += 1
if not got:
break
ans += have // temp * got
have %= temp
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of $n$ booths, arranged in a circle. The booths are numbered $1$ through $n$ clockwise with $n$ being adjacent to $1$. The $i$-th booths sells some candies for the price of $a_i$ burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most $T$ burles at the fair. However, he has some plan in mind for his path across the booths: at first, he visits booth number $1$; if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately; then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
-----Input-----
The first line contains two integers $n$ and $T$ ($1 \le n \le 2 \cdot 10^5$, $1 \le T \le 10^{18}$) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β the price of the single candy at booth number $i$.
-----Output-----
Print a single integer β the total number of candies Polycarp will buy.
-----Examples-----
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
-----Note-----
Let's consider the first example. Here are Polycarp's moves until he runs out of money: Booth $1$, buys candy for $5$, $T = 33$; Booth $2$, buys candy for $2$, $T = 31$; Booth $3$, buys candy for $5$, $T = 26$; Booth $1$, buys candy for $5$, $T = 21$; Booth $2$, buys candy for $2$, $T = 19$; Booth $3$, buys candy for $5$, $T = 14$; Booth $1$, buys candy for $5$, $T = 9$; Booth $2$, buys candy for $2$, $T = 7$; Booth $3$, buys candy for $5$, $T = 2$; Booth $1$, buys no candy, not enough money; Booth $2$, buys candy for $2$, $T = 0$.
No candy can be bought later. The total number of candies bought is $10$.
In the second example he has $1$ burle left at the end of his path, no candy can be bought with this amount. | n = [int(i) for i in input().split()]
l = [int(i) for i in input().split()]
m = 0
while len(l) > 0:
m += n[1] // sum(l) * len(l)
n[1] %= sum(l)
l = [i for i in l if i <= n[1]]
for i in l:
if n[1] - i >= 0:
m += 1
n[1] -= i
else:
l = [i for i in l if i <= n[1]]
print(m) | ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of $n$ booths, arranged in a circle. The booths are numbered $1$ through $n$ clockwise with $n$ being adjacent to $1$. The $i$-th booths sells some candies for the price of $a_i$ burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most $T$ burles at the fair. However, he has some plan in mind for his path across the booths: at first, he visits booth number $1$; if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately; then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
-----Input-----
The first line contains two integers $n$ and $T$ ($1 \le n \le 2 \cdot 10^5$, $1 \le T \le 10^{18}$) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β the price of the single candy at booth number $i$.
-----Output-----
Print a single integer β the total number of candies Polycarp will buy.
-----Examples-----
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
-----Note-----
Let's consider the first example. Here are Polycarp's moves until he runs out of money: Booth $1$, buys candy for $5$, $T = 33$; Booth $2$, buys candy for $2$, $T = 31$; Booth $3$, buys candy for $5$, $T = 26$; Booth $1$, buys candy for $5$, $T = 21$; Booth $2$, buys candy for $2$, $T = 19$; Booth $3$, buys candy for $5$, $T = 14$; Booth $1$, buys candy for $5$, $T = 9$; Booth $2$, buys candy for $2$, $T = 7$; Booth $3$, buys candy for $5$, $T = 2$; Booth $1$, buys no candy, not enough money; Booth $2$, buys candy for $2$, $T = 0$.
No candy can be bought later. The total number of candies bought is $10$.
In the second example he has $1$ burle left at the end of his path, no candy can be bought with this amount. | n, k = map(int, input().split())
a = list(map(int, input().split()))
x = min(a)
ans = 0
sum1 = 0
cnt = 0
while k >= x:
sum1 = 0
cnt = 0
for i in range(n):
if a[i] <= k:
sum1 = sum1 + a[i]
cnt = cnt + 1
t = cnt * (k // sum1)
ans = ans + t
k = k % sum1
for i in range(n):
if a[i] <= k:
ans = ans + 1
k = k - a[i]
print(ans) | ASSIGN VAR 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 ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR |
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of $n$ booths, arranged in a circle. The booths are numbered $1$ through $n$ clockwise with $n$ being adjacent to $1$. The $i$-th booths sells some candies for the price of $a_i$ burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most $T$ burles at the fair. However, he has some plan in mind for his path across the booths: at first, he visits booth number $1$; if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately; then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
-----Input-----
The first line contains two integers $n$ and $T$ ($1 \le n \le 2 \cdot 10^5$, $1 \le T \le 10^{18}$) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β the price of the single candy at booth number $i$.
-----Output-----
Print a single integer β the total number of candies Polycarp will buy.
-----Examples-----
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
-----Note-----
Let's consider the first example. Here are Polycarp's moves until he runs out of money: Booth $1$, buys candy for $5$, $T = 33$; Booth $2$, buys candy for $2$, $T = 31$; Booth $3$, buys candy for $5$, $T = 26$; Booth $1$, buys candy for $5$, $T = 21$; Booth $2$, buys candy for $2$, $T = 19$; Booth $3$, buys candy for $5$, $T = 14$; Booth $1$, buys candy for $5$, $T = 9$; Booth $2$, buys candy for $2$, $T = 7$; Booth $3$, buys candy for $5$, $T = 2$; Booth $1$, buys no candy, not enough money; Booth $2$, buys candy for $2$, $T = 0$.
No candy can be bought later. The total number of candies bought is $10$.
In the second example he has $1$ burle left at the end of his path, no candy can be bought with this amount. | n, t = map(int, input().split())
inrr = input().split()
a = []
for i in inrr:
ii = int(i)
if ii <= t:
a.append(ii)
n = len(a)
if n == 0:
print(0)
else:
minier = min(a)
ans = 0
row = []
for x in range(n):
if a[x] <= t:
row.append(a[x])
while t >= minier:
cou = 0
summer = 0
for x in row:
if t >= x:
ans += 1
summer += x
t -= x
cou += 1
if cou > 0:
dopi = t // summer
ans += dopi * cou
t -= dopi * summer
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of $n$ booths, arranged in a circle. The booths are numbered $1$ through $n$ clockwise with $n$ being adjacent to $1$. The $i$-th booths sells some candies for the price of $a_i$ burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most $T$ burles at the fair. However, he has some plan in mind for his path across the booths: at first, he visits booth number $1$; if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately; then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
-----Input-----
The first line contains two integers $n$ and $T$ ($1 \le n \le 2 \cdot 10^5$, $1 \le T \le 10^{18}$) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β the price of the single candy at booth number $i$.
-----Output-----
Print a single integer β the total number of candies Polycarp will buy.
-----Examples-----
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
-----Note-----
Let's consider the first example. Here are Polycarp's moves until he runs out of money: Booth $1$, buys candy for $5$, $T = 33$; Booth $2$, buys candy for $2$, $T = 31$; Booth $3$, buys candy for $5$, $T = 26$; Booth $1$, buys candy for $5$, $T = 21$; Booth $2$, buys candy for $2$, $T = 19$; Booth $3$, buys candy for $5$, $T = 14$; Booth $1$, buys candy for $5$, $T = 9$; Booth $2$, buys candy for $2$, $T = 7$; Booth $3$, buys candy for $5$, $T = 2$; Booth $1$, buys no candy, not enough money; Booth $2$, buys candy for $2$, $T = 0$.
No candy can be bought later. The total number of candies bought is $10$.
In the second example he has $1$ burle left at the end of his path, no candy can be bought with this amount. | def main():
n, T = map(int, input().split())
ls = map(int, input().split())
a = list(ls)
ans = 0
while 1:
if T < min(a):
break
s = 0
cnt = 0
for x in a:
if s + x <= T:
s += x
cnt += 1
ans += T // s * cnt
T -= T // s * s
print(ans)
main() | FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of $n$ booths, arranged in a circle. The booths are numbered $1$ through $n$ clockwise with $n$ being adjacent to $1$. The $i$-th booths sells some candies for the price of $a_i$ burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most $T$ burles at the fair. However, he has some plan in mind for his path across the booths: at first, he visits booth number $1$; if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately; then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
-----Input-----
The first line contains two integers $n$ and $T$ ($1 \le n \le 2 \cdot 10^5$, $1 \le T \le 10^{18}$) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β the price of the single candy at booth number $i$.
-----Output-----
Print a single integer β the total number of candies Polycarp will buy.
-----Examples-----
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
-----Note-----
Let's consider the first example. Here are Polycarp's moves until he runs out of money: Booth $1$, buys candy for $5$, $T = 33$; Booth $2$, buys candy for $2$, $T = 31$; Booth $3$, buys candy for $5$, $T = 26$; Booth $1$, buys candy for $5$, $T = 21$; Booth $2$, buys candy for $2$, $T = 19$; Booth $3$, buys candy for $5$, $T = 14$; Booth $1$, buys candy for $5$, $T = 9$; Booth $2$, buys candy for $2$, $T = 7$; Booth $3$, buys candy for $5$, $T = 2$; Booth $1$, buys no candy, not enough money; Booth $2$, buys candy for $2$, $T = 0$.
No candy can be bought later. The total number of candies bought is $10$.
In the second example he has $1$ burle left at the end of his path, no candy can be bought with this amount. | n, k = [*map(int, input().split())]
numbers = [*map(int, input().split())]
count = 0
suma = sum(numbers)
sorted_numbers = sorted(numbers)
count_numbers = len(numbers)
while count_numbers != 0:
ile = k // suma
if ile >= 1:
count += count_numbers * ile
k -= suma * ile
else:
copy = []
for val in numbers:
if k >= val:
k -= val
count += 1
copy.append(val)
numbers = copy
while count_numbers != 0 and sorted_numbers[count_numbers - 1] > k:
count_numbers -= 1
suma -= sorted_numbers[count_numbers]
print(count) | ASSIGN VAR VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR |
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of $n$ booths, arranged in a circle. The booths are numbered $1$ through $n$ clockwise with $n$ being adjacent to $1$. The $i$-th booths sells some candies for the price of $a_i$ burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most $T$ burles at the fair. However, he has some plan in mind for his path across the booths: at first, he visits booth number $1$; if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately; then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
-----Input-----
The first line contains two integers $n$ and $T$ ($1 \le n \le 2 \cdot 10^5$, $1 \le T \le 10^{18}$) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β the price of the single candy at booth number $i$.
-----Output-----
Print a single integer β the total number of candies Polycarp will buy.
-----Examples-----
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
-----Note-----
Let's consider the first example. Here are Polycarp's moves until he runs out of money: Booth $1$, buys candy for $5$, $T = 33$; Booth $2$, buys candy for $2$, $T = 31$; Booth $3$, buys candy for $5$, $T = 26$; Booth $1$, buys candy for $5$, $T = 21$; Booth $2$, buys candy for $2$, $T = 19$; Booth $3$, buys candy for $5$, $T = 14$; Booth $1$, buys candy for $5$, $T = 9$; Booth $2$, buys candy for $2$, $T = 7$; Booth $3$, buys candy for $5$, $T = 2$; Booth $1$, buys no candy, not enough money; Booth $2$, buys candy for $2$, $T = 0$.
No candy can be bought later. The total number of candies bought is $10$.
In the second example he has $1$ burle left at the end of his path, no candy can be bought with this amount. | n, T = map(int, input().split())
costs = input().split()
costs = map(int, costs)
costs = list(costs)
min_cost = min(costs)
confets = 0
while T >= min_cost:
c = T
curr = 0
spent = 0
for x in costs:
if x <= c:
curr += 1
spent += x
c -= x
confets += T // spent * curr
T = T % spent
costs = filter(lambda x: x <= T, costs)
costs = list(costs)
print(confets) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of $n$ booths, arranged in a circle. The booths are numbered $1$ through $n$ clockwise with $n$ being adjacent to $1$. The $i$-th booths sells some candies for the price of $a_i$ burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most $T$ burles at the fair. However, he has some plan in mind for his path across the booths: at first, he visits booth number $1$; if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately; then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
-----Input-----
The first line contains two integers $n$ and $T$ ($1 \le n \le 2 \cdot 10^5$, $1 \le T \le 10^{18}$) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β the price of the single candy at booth number $i$.
-----Output-----
Print a single integer β the total number of candies Polycarp will buy.
-----Examples-----
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
-----Note-----
Let's consider the first example. Here are Polycarp's moves until he runs out of money: Booth $1$, buys candy for $5$, $T = 33$; Booth $2$, buys candy for $2$, $T = 31$; Booth $3$, buys candy for $5$, $T = 26$; Booth $1$, buys candy for $5$, $T = 21$; Booth $2$, buys candy for $2$, $T = 19$; Booth $3$, buys candy for $5$, $T = 14$; Booth $1$, buys candy for $5$, $T = 9$; Booth $2$, buys candy for $2$, $T = 7$; Booth $3$, buys candy for $5$, $T = 2$; Booth $1$, buys no candy, not enough money; Booth $2$, buys candy for $2$, $T = 0$.
No candy can be bought later. The total number of candies bought is $10$.
In the second example he has $1$ burle left at the end of his path, no candy can be bought with this amount. | n, t = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
ans = 0
while t > 0 and len(a) > 0:
s = sum(a)
ans += t // s * len(a)
t = t % s
b = []
for i in range(len(a)):
if a[i] <= t:
ans += 1
t -= a[i]
if a[i] <= t:
b.append(a[i])
if t < 0:
break
a = b
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of $n$ booths, arranged in a circle. The booths are numbered $1$ through $n$ clockwise with $n$ being adjacent to $1$. The $i$-th booths sells some candies for the price of $a_i$ burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most $T$ burles at the fair. However, he has some plan in mind for his path across the booths: at first, he visits booth number $1$; if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately; then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
-----Input-----
The first line contains two integers $n$ and $T$ ($1 \le n \le 2 \cdot 10^5$, $1 \le T \le 10^{18}$) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β the price of the single candy at booth number $i$.
-----Output-----
Print a single integer β the total number of candies Polycarp will buy.
-----Examples-----
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
-----Note-----
Let's consider the first example. Here are Polycarp's moves until he runs out of money: Booth $1$, buys candy for $5$, $T = 33$; Booth $2$, buys candy for $2$, $T = 31$; Booth $3$, buys candy for $5$, $T = 26$; Booth $1$, buys candy for $5$, $T = 21$; Booth $2$, buys candy for $2$, $T = 19$; Booth $3$, buys candy for $5$, $T = 14$; Booth $1$, buys candy for $5$, $T = 9$; Booth $2$, buys candy for $2$, $T = 7$; Booth $3$, buys candy for $5$, $T = 2$; Booth $1$, buys no candy, not enough money; Booth $2$, buys candy for $2$, $T = 0$.
No candy can be bought later. The total number of candies bought is $10$.
In the second example he has $1$ burle left at the end of his path, no candy can be bought with this amount. | import sys
n, T = map(int, sys.stdin.readline().split())
booths = list(map(int, sys.stdin.readline().split()))
total_cost = 0
result = 0
cheapest_item = float("inf")
candle_count = 0
while T >= cheapest_item or cheapest_item == float("inf"):
running_T = T
total_cost = 0
candle_count = 0
for booth in booths:
if booth < cheapest_item:
cheapest_item = booth
if booth <= running_T:
candle_count += 1
running_T -= booth
total_cost += booth
if total_cost:
result += candle_count * (T // total_cost)
T %= total_cost
print(result) | IMPORT ASSIGN VAR 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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER WHILE VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR VAR IF VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of $n$ booths, arranged in a circle. The booths are numbered $1$ through $n$ clockwise with $n$ being adjacent to $1$. The $i$-th booths sells some candies for the price of $a_i$ burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most $T$ burles at the fair. However, he has some plan in mind for his path across the booths: at first, he visits booth number $1$; if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately; then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
-----Input-----
The first line contains two integers $n$ and $T$ ($1 \le n \le 2 \cdot 10^5$, $1 \le T \le 10^{18}$) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β the price of the single candy at booth number $i$.
-----Output-----
Print a single integer β the total number of candies Polycarp will buy.
-----Examples-----
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
-----Note-----
Let's consider the first example. Here are Polycarp's moves until he runs out of money: Booth $1$, buys candy for $5$, $T = 33$; Booth $2$, buys candy for $2$, $T = 31$; Booth $3$, buys candy for $5$, $T = 26$; Booth $1$, buys candy for $5$, $T = 21$; Booth $2$, buys candy for $2$, $T = 19$; Booth $3$, buys candy for $5$, $T = 14$; Booth $1$, buys candy for $5$, $T = 9$; Booth $2$, buys candy for $2$, $T = 7$; Booth $3$, buys candy for $5$, $T = 2$; Booth $1$, buys no candy, not enough money; Booth $2$, buys candy for $2$, $T = 0$.
No candy can be bought later. The total number of candies bought is $10$.
In the second example he has $1$ burle left at the end of his path, no candy can be bought with this amount. | n, T = map(int, input().split())
a = list(map(int, input().split()))
s = sum(a)
t = n * (T // s)
left = T % s
b = {}
j = 0
temp = []
for i in range(n):
b[i] = a[i]
while n > 0 and left > 0:
for j in b:
if left >= b[j]:
t = t + 1
left = left - b[j]
else:
n = n - 1
s = s - b[j]
temp.append(j)
for k in temp:
del b[k]
temp.clear()
if left > s > 0:
t = t + n * (left // s)
left = left % s
print(t) | ASSIGN VAR 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 BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR WHILE VAR NUMBER VAR NUMBER FOR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of $n$ booths, arranged in a circle. The booths are numbered $1$ through $n$ clockwise with $n$ being adjacent to $1$. The $i$-th booths sells some candies for the price of $a_i$ burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most $T$ burles at the fair. However, he has some plan in mind for his path across the booths: at first, he visits booth number $1$; if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately; then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
-----Input-----
The first line contains two integers $n$ and $T$ ($1 \le n \le 2 \cdot 10^5$, $1 \le T \le 10^{18}$) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β the price of the single candy at booth number $i$.
-----Output-----
Print a single integer β the total number of candies Polycarp will buy.
-----Examples-----
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
-----Note-----
Let's consider the first example. Here are Polycarp's moves until he runs out of money: Booth $1$, buys candy for $5$, $T = 33$; Booth $2$, buys candy for $2$, $T = 31$; Booth $3$, buys candy for $5$, $T = 26$; Booth $1$, buys candy for $5$, $T = 21$; Booth $2$, buys candy for $2$, $T = 19$; Booth $3$, buys candy for $5$, $T = 14$; Booth $1$, buys candy for $5$, $T = 9$; Booth $2$, buys candy for $2$, $T = 7$; Booth $3$, buys candy for $5$, $T = 2$; Booth $1$, buys no candy, not enough money; Booth $2$, buys candy for $2$, $T = 0$.
No candy can be bought later. The total number of candies bought is $10$.
In the second example he has $1$ burle left at the end of his path, no candy can be bought with this amount. | R = lambda: map(int, input().split())
n, T = R()
a = list(R())
res = 0
while 1:
acc, cnt = 0, 0
for x in a:
if acc + x <= T:
acc += x
cnt += 1
if not acc:
break
res += T // acc * cnt
T %= acc
print(res) | ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of $n$ booths, arranged in a circle. The booths are numbered $1$ through $n$ clockwise with $n$ being adjacent to $1$. The $i$-th booths sells some candies for the price of $a_i$ burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most $T$ burles at the fair. However, he has some plan in mind for his path across the booths: at first, he visits booth number $1$; if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately; then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
-----Input-----
The first line contains two integers $n$ and $T$ ($1 \le n \le 2 \cdot 10^5$, $1 \le T \le 10^{18}$) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β the price of the single candy at booth number $i$.
-----Output-----
Print a single integer β the total number of candies Polycarp will buy.
-----Examples-----
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
-----Note-----
Let's consider the first example. Here are Polycarp's moves until he runs out of money: Booth $1$, buys candy for $5$, $T = 33$; Booth $2$, buys candy for $2$, $T = 31$; Booth $3$, buys candy for $5$, $T = 26$; Booth $1$, buys candy for $5$, $T = 21$; Booth $2$, buys candy for $2$, $T = 19$; Booth $3$, buys candy for $5$, $T = 14$; Booth $1$, buys candy for $5$, $T = 9$; Booth $2$, buys candy for $2$, $T = 7$; Booth $3$, buys candy for $5$, $T = 2$; Booth $1$, buys no candy, not enough money; Booth $2$, buys candy for $2$, $T = 0$.
No candy can be bought later. The total number of candies bought is $10$.
In the second example he has $1$ burle left at the end of his path, no candy can be bought with this amount. | import sys
N, T = list(map(int, sys.stdin.readline().strip().split()))
nums = list(map(int, sys.stdin.readline().strip().split()))
ret = 0
while True:
t = 0
count = 0
for item in nums:
if T >= t + item:
t += item
count += 1
if not count:
break
ret += T // t * count
T = T % t
print(ret) | IMPORT ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of $n$ booths, arranged in a circle. The booths are numbered $1$ through $n$ clockwise with $n$ being adjacent to $1$. The $i$-th booths sells some candies for the price of $a_i$ burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most $T$ burles at the fair. However, he has some plan in mind for his path across the booths: at first, he visits booth number $1$; if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately; then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
-----Input-----
The first line contains two integers $n$ and $T$ ($1 \le n \le 2 \cdot 10^5$, $1 \le T \le 10^{18}$) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β the price of the single candy at booth number $i$.
-----Output-----
Print a single integer β the total number of candies Polycarp will buy.
-----Examples-----
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
-----Note-----
Let's consider the first example. Here are Polycarp's moves until he runs out of money: Booth $1$, buys candy for $5$, $T = 33$; Booth $2$, buys candy for $2$, $T = 31$; Booth $3$, buys candy for $5$, $T = 26$; Booth $1$, buys candy for $5$, $T = 21$; Booth $2$, buys candy for $2$, $T = 19$; Booth $3$, buys candy for $5$, $T = 14$; Booth $1$, buys candy for $5$, $T = 9$; Booth $2$, buys candy for $2$, $T = 7$; Booth $3$, buys candy for $5$, $T = 2$; Booth $1$, buys no candy, not enough money; Booth $2$, buys candy for $2$, $T = 0$.
No candy can be bought later. The total number of candies bought is $10$.
In the second example he has $1$ burle left at the end of his path, no candy can be bought with this amount. | from sys import stdin
input = stdin.readline
n, t = map(int, input().split())
l = list(map(int, input().split()))
s = sum(l)
cnt, d = 0, [1] * n
tot = 0
while cnt < n:
if t >= s and s > 0:
tot += t // s * (n - cnt)
t %= s
if t < s:
curr = 0
for i in range(n):
if d[i]:
if curr + l[i] > t:
d[i] = 0
cnt += 1
else:
curr += l[i]
s = curr
print(tot) | ASSIGN VAR VAR ASSIGN VAR 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 VAR NUMBER BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of $n$ booths, arranged in a circle. The booths are numbered $1$ through $n$ clockwise with $n$ being adjacent to $1$. The $i$-th booths sells some candies for the price of $a_i$ burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most $T$ burles at the fair. However, he has some plan in mind for his path across the booths: at first, he visits booth number $1$; if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately; then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
-----Input-----
The first line contains two integers $n$ and $T$ ($1 \le n \le 2 \cdot 10^5$, $1 \le T \le 10^{18}$) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β the price of the single candy at booth number $i$.
-----Output-----
Print a single integer β the total number of candies Polycarp will buy.
-----Examples-----
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
-----Note-----
Let's consider the first example. Here are Polycarp's moves until he runs out of money: Booth $1$, buys candy for $5$, $T = 33$; Booth $2$, buys candy for $2$, $T = 31$; Booth $3$, buys candy for $5$, $T = 26$; Booth $1$, buys candy for $5$, $T = 21$; Booth $2$, buys candy for $2$, $T = 19$; Booth $3$, buys candy for $5$, $T = 14$; Booth $1$, buys candy for $5$, $T = 9$; Booth $2$, buys candy for $2$, $T = 7$; Booth $3$, buys candy for $5$, $T = 2$; Booth $1$, buys no candy, not enough money; Booth $2$, buys candy for $2$, $T = 0$.
No candy can be bought later. The total number of candies bought is $10$.
In the second example he has $1$ burle left at the end of his path, no candy can be bought with this amount. | import sys
n, T = [int(i) for i in sys.stdin.readline().strip().split()]
A = [int(i) for i in sys.stdin.readline().strip().split()]
r = T // sum(A) * n
T = T % sum(A)
S = list(range(n))
s = 0
k = 0
while S:
U = []
for i in S:
if A[i] <= T:
T = T - A[i]
r = r + 1
if A[i] <= T:
U.append(i)
k = k + 1
s = s + A[i]
S = U
if s:
r = r + T // s * k
T = T % s
k = 0
s = 0
print(r) | IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR |
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of $n$ booths, arranged in a circle. The booths are numbered $1$ through $n$ clockwise with $n$ being adjacent to $1$. The $i$-th booths sells some candies for the price of $a_i$ burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most $T$ burles at the fair. However, he has some plan in mind for his path across the booths: at first, he visits booth number $1$; if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately; then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
-----Input-----
The first line contains two integers $n$ and $T$ ($1 \le n \le 2 \cdot 10^5$, $1 \le T \le 10^{18}$) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β the price of the single candy at booth number $i$.
-----Output-----
Print a single integer β the total number of candies Polycarp will buy.
-----Examples-----
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
-----Note-----
Let's consider the first example. Here are Polycarp's moves until he runs out of money: Booth $1$, buys candy for $5$, $T = 33$; Booth $2$, buys candy for $2$, $T = 31$; Booth $3$, buys candy for $5$, $T = 26$; Booth $1$, buys candy for $5$, $T = 21$; Booth $2$, buys candy for $2$, $T = 19$; Booth $3$, buys candy for $5$, $T = 14$; Booth $1$, buys candy for $5$, $T = 9$; Booth $2$, buys candy for $2$, $T = 7$; Booth $3$, buys candy for $5$, $T = 2$; Booth $1$, buys no candy, not enough money; Booth $2$, buys candy for $2$, $T = 0$.
No candy can be bought later. The total number of candies bought is $10$.
In the second example he has $1$ burle left at the end of his path, no candy can be bought with this amount. | n, t = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
while t > 0:
k = 0
suma = 0
t1 = t
f = 0
for i in range(n):
if A[i] <= t1:
suma += A[i]
t1 -= A[i]
k += 1
f = 1
if f == 1:
ans = ans + k * (t // suma)
t = t % suma
else:
break
print(ans) | ASSIGN VAR 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 NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of $n$ booths, arranged in a circle. The booths are numbered $1$ through $n$ clockwise with $n$ being adjacent to $1$. The $i$-th booths sells some candies for the price of $a_i$ burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most $T$ burles at the fair. However, he has some plan in mind for his path across the booths: at first, he visits booth number $1$; if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately; then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
-----Input-----
The first line contains two integers $n$ and $T$ ($1 \le n \le 2 \cdot 10^5$, $1 \le T \le 10^{18}$) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β the price of the single candy at booth number $i$.
-----Output-----
Print a single integer β the total number of candies Polycarp will buy.
-----Examples-----
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
-----Note-----
Let's consider the first example. Here are Polycarp's moves until he runs out of money: Booth $1$, buys candy for $5$, $T = 33$; Booth $2$, buys candy for $2$, $T = 31$; Booth $3$, buys candy for $5$, $T = 26$; Booth $1$, buys candy for $5$, $T = 21$; Booth $2$, buys candy for $2$, $T = 19$; Booth $3$, buys candy for $5$, $T = 14$; Booth $1$, buys candy for $5$, $T = 9$; Booth $2$, buys candy for $2$, $T = 7$; Booth $3$, buys candy for $5$, $T = 2$; Booth $1$, buys no candy, not enough money; Booth $2$, buys candy for $2$, $T = 0$.
No candy can be bought later. The total number of candies bought is $10$.
In the second example he has $1$ burle left at the end of his path, no candy can be bought with this amount. | n, T = map(int, input().split(" "))
a = list(map(int, input().split(" ")))
candies = 0
remaining = len(a)
min_a = min(a)
while remaining > 0:
for i, ai in enumerate(a):
if ai > T:
a[i] = 0
remaining -= 1
if remaining == 0:
break
round_cost = sum(a)
max_full_rounds = T // round_cost
if max_full_rounds == 0:
for ai in a:
if ai != 0 and ai <= T:
T -= ai
candies += 1
candies += max_full_rounds * remaining
T -= max_full_rounds * round_cost
print(candies) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of $n$ booths, arranged in a circle. The booths are numbered $1$ through $n$ clockwise with $n$ being adjacent to $1$. The $i$-th booths sells some candies for the price of $a_i$ burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most $T$ burles at the fair. However, he has some plan in mind for his path across the booths: at first, he visits booth number $1$; if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately; then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
-----Input-----
The first line contains two integers $n$ and $T$ ($1 \le n \le 2 \cdot 10^5$, $1 \le T \le 10^{18}$) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β the price of the single candy at booth number $i$.
-----Output-----
Print a single integer β the total number of candies Polycarp will buy.
-----Examples-----
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
-----Note-----
Let's consider the first example. Here are Polycarp's moves until he runs out of money: Booth $1$, buys candy for $5$, $T = 33$; Booth $2$, buys candy for $2$, $T = 31$; Booth $3$, buys candy for $5$, $T = 26$; Booth $1$, buys candy for $5$, $T = 21$; Booth $2$, buys candy for $2$, $T = 19$; Booth $3$, buys candy for $5$, $T = 14$; Booth $1$, buys candy for $5$, $T = 9$; Booth $2$, buys candy for $2$, $T = 7$; Booth $3$, buys candy for $5$, $T = 2$; Booth $1$, buys no candy, not enough money; Booth $2$, buys candy for $2$, $T = 0$.
No candy can be bought later. The total number of candies bought is $10$.
In the second example he has $1$ burle left at the end of his path, no candy can be bought with this amount. | N, T = map(int, input().split())
A = [int(x) for x in input().split()]
ans = 0
while A and T > 0:
sa = sum(A)
if T >= sa:
ans += T // sa * len(A)
T %= sa
else:
for i, a in enumerate(A):
if T <= 0:
break
if T >= a:
T -= a
ans += 1
A = [a for a in A if a <= T]
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of $n$ booths, arranged in a circle. The booths are numbered $1$ through $n$ clockwise with $n$ being adjacent to $1$. The $i$-th booths sells some candies for the price of $a_i$ burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most $T$ burles at the fair. However, he has some plan in mind for his path across the booths: at first, he visits booth number $1$; if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately; then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
-----Input-----
The first line contains two integers $n$ and $T$ ($1 \le n \le 2 \cdot 10^5$, $1 \le T \le 10^{18}$) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β the price of the single candy at booth number $i$.
-----Output-----
Print a single integer β the total number of candies Polycarp will buy.
-----Examples-----
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
-----Note-----
Let's consider the first example. Here are Polycarp's moves until he runs out of money: Booth $1$, buys candy for $5$, $T = 33$; Booth $2$, buys candy for $2$, $T = 31$; Booth $3$, buys candy for $5$, $T = 26$; Booth $1$, buys candy for $5$, $T = 21$; Booth $2$, buys candy for $2$, $T = 19$; Booth $3$, buys candy for $5$, $T = 14$; Booth $1$, buys candy for $5$, $T = 9$; Booth $2$, buys candy for $2$, $T = 7$; Booth $3$, buys candy for $5$, $T = 2$; Booth $1$, buys no candy, not enough money; Booth $2$, buys candy for $2$, $T = 0$.
No candy can be bought later. The total number of candies bought is $10$.
In the second example he has $1$ burle left at the end of his path, no candy can be bought with this amount. | def sum(a):
s = 0
for i in a:
s += i
return s
n, T = map(int, input().split())
a = list(map(int, input().split()))
sum = sum(a)
k = 0
k += n * (T // sum)
T %= sum
new_a = []
new_sum = 0
ch = True
while ch:
for i in range(n):
if a[i] <= T:
new_a.append(a[i])
new_sum += a[i]
k += 1
T -= a[i]
n = len(new_a)
if n == 0:
ch = False
break
sum = new_sum
a = new_a
new_a = []
new_sum = 0
k += n * (T // sum)
T %= sum
print(k) | FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR VAR RETURN VAR ASSIGN VAR 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 VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Given a preorder traversal of a BST, find the leaf nodes of the tree without building the tree.
Example 1:
Input:
N = 2
arr = {2,1}
Output: {1}
Explaination: 1 is the only leaf node.
Example 2:
Input:
N = 3
Arr = {3, 2, 4}
Output: {2, 4}
Explaination: 2, 4 are the leaf nodes.
Your Task:
You don't need to read input or print anything. Your task is to complete the function leafNodes() which takes the array arr[] and its size N as input parameters and returns the leaf nodes of the tree.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{3}
1 β€ arr[i] β€ 10^{3} | class Solution:
temp = []
def leafNodes(self, arr, N):
temp = []
def fleaf(pre):
n = len(pre)
if n == 1:
temp.append(pre[0])
return
if n < 1:
return
left = pre[0]
for i in range(1, n):
if pre[i] > left:
fleaf(pre[1:i])
fleaf(pre[i:])
break
else:
fleaf(pre[1:n])
fleaf(arr)
return temp | CLASS_DEF ASSIGN VAR LIST FUNC_DEF ASSIGN VAR LIST FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN IF VAR NUMBER RETURN ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Given a preorder traversal of a BST, find the leaf nodes of the tree without building the tree.
Example 1:
Input:
N = 2
arr = {2,1}
Output: {1}
Explaination: 1 is the only leaf node.
Example 2:
Input:
N = 3
Arr = {3, 2, 4}
Output: {2, 4}
Explaination: 2, 4 are the leaf nodes.
Your Task:
You don't need to read input or print anything. Your task is to complete the function leafNodes() which takes the array arr[] and its size N as input parameters and returns the leaf nodes of the tree.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{3}
1 β€ arr[i] β€ 10^{3} | class Solution:
def leafNodes(self, arr, n):
st = []
ans = []
st.append(arr[0])
for i in range(1, n):
if arr[i] < st[-1]:
st.append(arr[i])
else:
temp = st[-1]
st.pop()
if st and arr[i] > st[-1]:
while st and arr[i] > st[-1]:
st.pop()
ans.append(temp)
st.append(arr[i])
ans.append(arr[n - 1])
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR |
Given a preorder traversal of a BST, find the leaf nodes of the tree without building the tree.
Example 1:
Input:
N = 2
arr = {2,1}
Output: {1}
Explaination: 1 is the only leaf node.
Example 2:
Input:
N = 3
Arr = {3, 2, 4}
Output: {2, 4}
Explaination: 2, 4 are the leaf nodes.
Your Task:
You don't need to read input or print anything. Your task is to complete the function leafNodes() which takes the array arr[] and its size N as input parameters and returns the leaf nodes of the tree.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{3}
1 β€ arr[i] β€ 10^{3} | class Solution:
def leafNodes(self, arr, N):
if N < 1:
return []
stack = [arr[0]]
ans = []
for i in range(1, N):
if arr[i] > stack[-1]:
iniSize = len(stack)
while len(stack) > 0 and stack[-1] < arr[i]:
stack.pop()
if iniSize - len(stack) > 1:
ans.append(arr[i - 1])
stack.append(arr[i])
ans.append(stack[-1])
return ans | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN LIST ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR IF BIN_OP VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR |
Given a preorder traversal of a BST, find the leaf nodes of the tree without building the tree.
Example 1:
Input:
N = 2
arr = {2,1}
Output: {1}
Explaination: 1 is the only leaf node.
Example 2:
Input:
N = 3
Arr = {3, 2, 4}
Output: {2, 4}
Explaination: 2, 4 are the leaf nodes.
Your Task:
You don't need to read input or print anything. Your task is to complete the function leafNodes() which takes the array arr[] and its size N as input parameters and returns the leaf nodes of the tree.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{3}
1 β€ arr[i] β€ 10^{3} | class Solution:
def leafNodes(self, arr, N):
stk = []
res = []
for i in range(N):
if len(stk) > 0 and stk[-1] < arr[i]:
leaf = stk[-1]
count = 0
while len(stk) > 0 and stk[-1] < arr[i]:
stk.pop()
count = count + 1
stk.append(arr[i])
if count > 1:
res.append(leaf)
else:
stk.append(arr[i])
res.append(stk[-1])
return res | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR |
Given a preorder traversal of a BST, find the leaf nodes of the tree without building the tree.
Example 1:
Input:
N = 2
arr = {2,1}
Output: {1}
Explaination: 1 is the only leaf node.
Example 2:
Input:
N = 3
Arr = {3, 2, 4}
Output: {2, 4}
Explaination: 2, 4 are the leaf nodes.
Your Task:
You don't need to read input or print anything. Your task is to complete the function leafNodes() which takes the array arr[] and its size N as input parameters and returns the leaf nodes of the tree.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{3}
1 β€ arr[i] β€ 10^{3} | class Solution:
def leafNodes(self, arr, N):
st = []
ans = []
for i in arr:
if st and st[-1] < i:
prev = st.pop()
if st and st[-1] < i:
ans.append(prev)
while st and st[-1] < i:
st.pop()
st.append(i)
ans.append(st[-1])
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR |
Given a preorder traversal of a BST, find the leaf nodes of the tree without building the tree.
Example 1:
Input:
N = 2
arr = {2,1}
Output: {1}
Explaination: 1 is the only leaf node.
Example 2:
Input:
N = 3
Arr = {3, 2, 4}
Output: {2, 4}
Explaination: 2, 4 are the leaf nodes.
Your Task:
You don't need to read input or print anything. Your task is to complete the function leafNodes() which takes the array arr[] and its size N as input parameters and returns the leaf nodes of the tree.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{3}
1 β€ arr[i] β€ 10^{3} | class Solution:
def leafNodes(self, arr, n):
stack = [arr[0]]
l = []
for i in range(1, n):
if arr[i] < stack[-1]:
stack.append(arr[i])
else:
presize = len(stack)
temp = stack[-1]
while stack and arr[i] > stack[-1]:
stack.pop()
stack.append(arr[i])
if presize > len(stack):
l.append(temp)
l.append(stack[-1])
return l | CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR |
Given a preorder traversal of a BST, find the leaf nodes of the tree without building the tree.
Example 1:
Input:
N = 2
arr = {2,1}
Output: {1}
Explaination: 1 is the only leaf node.
Example 2:
Input:
N = 3
Arr = {3, 2, 4}
Output: {2, 4}
Explaination: 2, 4 are the leaf nodes.
Your Task:
You don't need to read input or print anything. Your task is to complete the function leafNodes() which takes the array arr[] and its size N as input parameters and returns the leaf nodes of the tree.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{3}
1 β€ arr[i] β€ 10^{3} | class Solution:
def leafNodes(self, arr, N):
mx = []
leaf = []
prev = arr[0]
flag = 0
for i in range(1, N):
if arr[i] < prev:
mx.append(prev)
prev = arr[i]
else:
flag = 0
while mx:
root = mx.pop()
if root < arr[i]:
flag = 1
else:
mx.append(root)
break
if flag == 1:
leaf.append(prev)
prev = arr[i]
leaf.append(prev)
return leaf | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Given a preorder traversal of a BST, find the leaf nodes of the tree without building the tree.
Example 1:
Input:
N = 2
arr = {2,1}
Output: {1}
Explaination: 1 is the only leaf node.
Example 2:
Input:
N = 3
Arr = {3, 2, 4}
Output: {2, 4}
Explaination: 2, 4 are the leaf nodes.
Your Task:
You don't need to read input or print anything. Your task is to complete the function leafNodes() which takes the array arr[] and its size N as input parameters and returns the leaf nodes of the tree.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{3}
1 β€ arr[i] β€ 10^{3} | class Solution:
def leafNodes(self, arr, n):
ans = []
s = []
j = 0
for i in range(1, n):
if arr[j] > arr[i]:
s.append(arr[j])
else:
valid = False
while s:
if arr[i] > s[-1]:
s.pop()
valid = True
else:
break
if valid:
ans.append(arr[j])
i += 1
j += 1
ans.append(arr[-1])
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR |
Given a preorder traversal of a BST, find the leaf nodes of the tree without building the tree.
Example 1:
Input:
N = 2
arr = {2,1}
Output: {1}
Explaination: 1 is the only leaf node.
Example 2:
Input:
N = 3
Arr = {3, 2, 4}
Output: {2, 4}
Explaination: 2, 4 are the leaf nodes.
Your Task:
You don't need to read input or print anything. Your task is to complete the function leafNodes() which takes the array arr[] and its size N as input parameters and returns the leaf nodes of the tree.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{3}
1 β€ arr[i] β€ 10^{3} | class Solution:
def leafNodes(self, arr, N):
s = []
i = 0
ans = []
for j in range(1, N):
found = False
if arr[i] > arr[j]:
s.append(arr[i])
else:
while len(s) != 0:
if arr[j] > s[-1]:
s.pop(-1)
found = True
else:
break
if found:
ans.append(arr[i])
i += 1
ans.append(arr[N - 1])
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR |
Given a preorder traversal of a BST, find the leaf nodes of the tree without building the tree.
Example 1:
Input:
N = 2
arr = {2,1}
Output: {1}
Explaination: 1 is the only leaf node.
Example 2:
Input:
N = 3
Arr = {3, 2, 4}
Output: {2, 4}
Explaination: 2, 4 are the leaf nodes.
Your Task:
You don't need to read input or print anything. Your task is to complete the function leafNodes() which takes the array arr[] and its size N as input parameters and returns the leaf nodes of the tree.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{3}
1 β€ arr[i] β€ 10^{3} | class Solution:
def leafNodes(self, arr, N):
ans = []
def dfs(index, mmin, mmax):
nonlocal ans
if index[0] == N - 1:
ans.append(arr[index[0]])
return
node = arr[index[0]]
lindex = index[0] + 1
left, right = None, None
if lindex < N and mmin < arr[lindex] <= node:
index[0] += 1
dfs(index, mmin, node)
left = True
else:
left = None
rindex = index[0] + 1
if rindex < N and node <= arr[rindex] < mmax:
index[0] += 1
dfs(index, node, mmax)
right = True
else:
right = None
if left is None and right is None:
ans.append(node)
dfs([0], -float("inf"), float("inf"))
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NONE NONE IF VAR VAR VAR VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NONE ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NONE IF VAR NONE VAR NONE EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST NUMBER FUNC_CALL VAR STRING FUNC_CALL VAR STRING RETURN VAR |
Given a preorder traversal of a BST, find the leaf nodes of the tree without building the tree.
Example 1:
Input:
N = 2
arr = {2,1}
Output: {1}
Explaination: 1 is the only leaf node.
Example 2:
Input:
N = 3
Arr = {3, 2, 4}
Output: {2, 4}
Explaination: 2, 4 are the leaf nodes.
Your Task:
You don't need to read input or print anything. Your task is to complete the function leafNodes() which takes the array arr[] and its size N as input parameters and returns the leaf nodes of the tree.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{3}
1 β€ arr[i] β€ 10^{3} | class Solution:
def leafNodes(self, arr, N):
ans = []
s = []
i, j = 0, 1
while j < N:
flag = False
if arr[i] > arr[j]:
s.append(arr[i])
else:
while s and s[-1] < arr[j]:
s.pop()
flag = True
if flag:
ans.append(arr[i])
i += 1
j += 1
ans.append(arr[N - 1])
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR WHILE VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR |
Given a preorder traversal of a BST, find the leaf nodes of the tree without building the tree.
Example 1:
Input:
N = 2
arr = {2,1}
Output: {1}
Explaination: 1 is the only leaf node.
Example 2:
Input:
N = 3
Arr = {3, 2, 4}
Output: {2, 4}
Explaination: 2, 4 are the leaf nodes.
Your Task:
You don't need to read input or print anything. Your task is to complete the function leafNodes() which takes the array arr[] and its size N as input parameters and returns the leaf nodes of the tree.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{3}
1 β€ arr[i] β€ 10^{3} | class Solution:
def leafNodes(self, arr, N):
ans, stack = [], []
stack.append(arr[0])
for i in range(1, N):
if arr[i] < stack[-1]:
stack.append(arr[i])
else:
temp = stack[-1]
rem = 0
while stack != [] and stack[-1] < arr[i]:
stack.pop()
rem += 1
if rem > 1:
ans.append(temp)
stack.append(arr[i])
ans.append(stack.pop())
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR VAR LIST LIST EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR LIST VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN VAR |
Given a preorder traversal of a BST, find the leaf nodes of the tree without building the tree.
Example 1:
Input:
N = 2
arr = {2,1}
Output: {1}
Explaination: 1 is the only leaf node.
Example 2:
Input:
N = 3
Arr = {3, 2, 4}
Output: {2, 4}
Explaination: 2, 4 are the leaf nodes.
Your Task:
You don't need to read input or print anything. Your task is to complete the function leafNodes() which takes the array arr[] and its size N as input parameters and returns the leaf nodes of the tree.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{3}
1 β€ arr[i] β€ 10^{3} | class Solution:
def leafNodes(self, arr, N):
ret = []
def collect(xs):
nonlocal ret
if len(xs) <= 1:
if xs:
ret.append(xs[0])
return
collect([x for x in xs if x < xs[0]])
collect([x for x in xs if x > xs[0]])
collect(arr)
return ret | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF FUNC_CALL VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR |
Given a preorder traversal of a BST, find the leaf nodes of the tree without building the tree.
Example 1:
Input:
N = 2
arr = {2,1}
Output: {1}
Explaination: 1 is the only leaf node.
Example 2:
Input:
N = 3
Arr = {3, 2, 4}
Output: {2, 4}
Explaination: 2, 4 are the leaf nodes.
Your Task:
You don't need to read input or print anything. Your task is to complete the function leafNodes() which takes the array arr[] and its size N as input parameters and returns the leaf nodes of the tree.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{3}
1 β€ arr[i] β€ 10^{3} | class Solution:
def leafNodes(self, arr, N):
stack = []
res = []
for i in range(N - 1):
if arr[i] > arr[i + 1]:
stack.append(arr[i])
elif stack and arr[i + 1] > stack[-1]:
res.append(arr[i])
while stack and stack[-1] < arr[i + 1]:
stack.pop()
res.append(arr[N - 1])
return res | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR WHILE VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR |
Given a preorder traversal of a BST, find the leaf nodes of the tree without building the tree.
Example 1:
Input:
N = 2
arr = {2,1}
Output: {1}
Explaination: 1 is the only leaf node.
Example 2:
Input:
N = 3
Arr = {3, 2, 4}
Output: {2, 4}
Explaination: 2, 4 are the leaf nodes.
Your Task:
You don't need to read input or print anything. Your task is to complete the function leafNodes() which takes the array arr[] and its size N as input parameters and returns the leaf nodes of the tree.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{3}
1 β€ arr[i] β€ 10^{3} | class Tree:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def leafNodes(self, arr, N):
stack = []
ans = []
for x in arr:
if stack and stack[-1] < x:
prev = stack.pop()
if stack and stack[-1] < x:
ans.append(prev)
while stack and stack[-1] < x:
stack.pop()
stack.append(x)
ans.append(stack[-1])
return ans | CLASS_DEF FUNC_DEF NUMBER NONE NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR |
Given a preorder traversal of a BST, find the leaf nodes of the tree without building the tree.
Example 1:
Input:
N = 2
arr = {2,1}
Output: {1}
Explaination: 1 is the only leaf node.
Example 2:
Input:
N = 3
Arr = {3, 2, 4}
Output: {2, 4}
Explaination: 2, 4 are the leaf nodes.
Your Task:
You don't need to read input or print anything. Your task is to complete the function leafNodes() which takes the array arr[] and its size N as input parameters and returns the leaf nodes of the tree.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{3}
1 β€ arr[i] β€ 10^{3} | class Solution:
def leafNodes(self, arr, N):
ans = []
stack = [arr[0]]
for i in range(1, N):
if arr[i] < stack[-1]:
stack.append(arr[i])
else:
a = stack.pop()
b = 0
while len(stack) > 0 and stack[-1] < arr[i]:
stack.pop()
b = 1
if b == 1:
ans.append(a)
stack.append(arr[i])
ans.append(arr[-1])
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR |
Given a preorder traversal of a BST, find the leaf nodes of the tree without building the tree.
Example 1:
Input:
N = 2
arr = {2,1}
Output: {1}
Explaination: 1 is the only leaf node.
Example 2:
Input:
N = 3
Arr = {3, 2, 4}
Output: {2, 4}
Explaination: 2, 4 are the leaf nodes.
Your Task:
You don't need to read input or print anything. Your task is to complete the function leafNodes() which takes the array arr[] and its size N as input parameters and returns the leaf nodes of the tree.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{3}
1 β€ arr[i] β€ 10^{3} | class Solution:
def leafNodes(self, arr, N):
ans = []
st = [arr[0]]
for i in range(1, N):
if arr[i] < st[-1]:
st.append(arr[i])
else:
top = st[-1]
co = 0
while st and st[-1] < arr[i]:
co += 1
st.pop()
st.append(arr[i])
if co > 1:
ans.append(top)
ans.append(st.pop())
return sorted(ans) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR |
Given a preorder traversal of a BST, find the leaf nodes of the tree without building the tree.
Example 1:
Input:
N = 2
arr = {2,1}
Output: {1}
Explaination: 1 is the only leaf node.
Example 2:
Input:
N = 3
Arr = {3, 2, 4}
Output: {2, 4}
Explaination: 2, 4 are the leaf nodes.
Your Task:
You don't need to read input or print anything. Your task is to complete the function leafNodes() which takes the array arr[] and its size N as input parameters and returns the leaf nodes of the tree.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{3}
1 β€ arr[i] β€ 10^{3} | class Solution:
def leafNodes(self, arr, N):
n, stack, leaf_nodes = N, [arr[0]], []
for i in range(1, n):
if arr[i] < stack[-1]:
stack.append(arr[i])
else:
temp, removed_counter = stack[-1], 0
while stack and stack[-1] < arr[i]:
stack.pop()
removed_counter += 1
stack.append(arr[i])
if removed_counter >= 2:
leaf_nodes.append(temp)
leaf_nodes.append(stack[-1])
return leaf_nodes | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR LIST VAR NUMBER LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER WHILE VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR |
Given a preorder traversal of a BST, find the leaf nodes of the tree without building the tree.
Example 1:
Input:
N = 2
arr = {2,1}
Output: {1}
Explaination: 1 is the only leaf node.
Example 2:
Input:
N = 3
Arr = {3, 2, 4}
Output: {2, 4}
Explaination: 2, 4 are the leaf nodes.
Your Task:
You don't need to read input or print anything. Your task is to complete the function leafNodes() which takes the array arr[] and its size N as input parameters and returns the leaf nodes of the tree.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{3}
1 β€ arr[i] β€ 10^{3} | class Solution:
def leafNodes(self, arr, n):
ans = list()
j = 0
s = list()
for i in range(1, n):
f = 1
if arr[j] > arr[i]:
s.append(arr[j])
else:
while len(s) != 0:
if arr[i] > s[-1]:
s.pop(-1)
f = 0
else:
break
if f == 0:
ans.append(arr[j])
j += 1
ans.append(arr[n - 1])
return ans
ans.append(preorder[n - 1])
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR |
Given a preorder traversal of a BST, find the leaf nodes of the tree without building the tree.
Example 1:
Input:
N = 2
arr = {2,1}
Output: {1}
Explaination: 1 is the only leaf node.
Example 2:
Input:
N = 3
Arr = {3, 2, 4}
Output: {2, 4}
Explaination: 2, 4 are the leaf nodes.
Your Task:
You don't need to read input or print anything. Your task is to complete the function leafNodes() which takes the array arr[] and its size N as input parameters and returns the leaf nodes of the tree.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{3}
1 β€ arr[i] β€ 10^{3} | class Solution:
def leafNodes(self, arr, N):
stack, ans = [arr[0]], []
for i in range(1, len(arr)):
if arr[i] > stack[-1]:
pop_count, top_elem = 0, stack[-1]
while stack and stack[-1] < arr[i]:
stack.pop()
pop_count += 1
if pop_count >= 2:
ans.append(top_elem)
stack.append(arr[i])
ans.append(stack[-1])
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR VAR LIST VAR NUMBER LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER WHILE VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR |
Given a preorder traversal of a BST, find the leaf nodes of the tree without building the tree.
Example 1:
Input:
N = 2
arr = {2,1}
Output: {1}
Explaination: 1 is the only leaf node.
Example 2:
Input:
N = 3
Arr = {3, 2, 4}
Output: {2, 4}
Explaination: 2, 4 are the leaf nodes.
Your Task:
You don't need to read input or print anything. Your task is to complete the function leafNodes() which takes the array arr[] and its size N as input parameters and returns the leaf nodes of the tree.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{3}
1 β€ arr[i] β€ 10^{3} | class Solution:
def leafNodes(self, arr, N):
ind = -1
res = []
def recur(low, high):
nonlocal res, ind
ind += 1
if ind >= N or arr[ind] < low or arr[ind] > high:
ind -= 1
return -1
else:
val = arr[ind]
v1 = recur(low, val)
v2 = recur(val, high)
if v1 == -1 and v2 == -1:
res.append(val)
return 0
recur(-float("inf"), float("inf"))
return res | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FUNC_DEF VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING RETURN VAR |
Given a preorder traversal of a BST, find the leaf nodes of the tree without building the tree.
Example 1:
Input:
N = 2
arr = {2,1}
Output: {1}
Explaination: 1 is the only leaf node.
Example 2:
Input:
N = 3
Arr = {3, 2, 4}
Output: {2, 4}
Explaination: 2, 4 are the leaf nodes.
Your Task:
You don't need to read input or print anything. Your task is to complete the function leafNodes() which takes the array arr[] and its size N as input parameters and returns the leaf nodes of the tree.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{3}
1 β€ arr[i] β€ 10^{3} | def leafNodeHelper(preorder, inorder, pl, pu, il, ih, ans):
if pl == pu:
ans.append(preorder[pl])
return
if pl > pu:
return
rIndex = il
while True:
if preorder[pl] == inorder[rIndex]:
break
rIndex += 1
Index = rIndex - il
leafNodeHelper(preorder, inorder, pl + 1, pl + Index, il, rIndex - 1, ans)
leafNodeHelper(preorder, inorder, pl + Index + 1, pu, rIndex + 1, ih, ans)
class Solution:
def leafNodes(self, arr, N):
preorder = arr
temp = arr.copy()
temp.sort()
inorder = temp
ans = []
leafNodeHelper(preorder, inorder, 0, N - 1, 0, N - 1, ans)
return ans | FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN IF VAR VAR RETURN ASSIGN VAR VAR WHILE NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR RETURN VAR |
Given a preorder traversal of a BST, find the leaf nodes of the tree without building the tree.
Example 1:
Input:
N = 2
arr = {2,1}
Output: {1}
Explaination: 1 is the only leaf node.
Example 2:
Input:
N = 3
Arr = {3, 2, 4}
Output: {2, 4}
Explaination: 2, 4 are the leaf nodes.
Your Task:
You don't need to read input or print anything. Your task is to complete the function leafNodes() which takes the array arr[] and its size N as input parameters and returns the leaf nodes of the tree.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{3}
1 β€ arr[i] β€ 10^{3} | class Solution:
def leafNodes1(self, arr, N):
global ans
if N == 0:
return
if N == 1:
ans.append(arr[0])
return
root = arr[0]
index = 1
for i in range(1, N):
if arr[0] < arr[i]:
index = i
break
self.leafNodes1(arr[1:index], len(arr[1:index]))
self.leafNodes1(arr[index:], len(arr[index:]))
def leafNodes(self, arr, N):
global ans
ans = []
self.leafNodes1(arr, N)
return ans | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Given a preorder traversal of a BST, find the leaf nodes of the tree without building the tree.
Example 1:
Input:
N = 2
arr = {2,1}
Output: {1}
Explaination: 1 is the only leaf node.
Example 2:
Input:
N = 3
Arr = {3, 2, 4}
Output: {2, 4}
Explaination: 2, 4 are the leaf nodes.
Your Task:
You don't need to read input or print anything. Your task is to complete the function leafNodes() which takes the array arr[] and its size N as input parameters and returns the leaf nodes of the tree.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{3}
1 β€ arr[i] β€ 10^{3} | class Solution:
def leafNodes(self, arr, N):
ans = list()
self.solve(arr, ans)
return ans
def solve(self, arr, ans):
if len(arr) < 1:
return
if len(arr) == 1:
ans.append(arr[0])
return
root = arr[0]
i = 1
while len(arr) > i:
if arr[i] <= root:
i += 1
else:
break
self.solve(arr[1:i], ans)
self.solve(arr[i:], ans)
return | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN |
Given a preorder traversal of a BST, find the leaf nodes of the tree without building the tree.
Example 1:
Input:
N = 2
arr = {2,1}
Output: {1}
Explaination: 1 is the only leaf node.
Example 2:
Input:
N = 3
Arr = {3, 2, 4}
Output: {2, 4}
Explaination: 2, 4 are the leaf nodes.
Your Task:
You don't need to read input or print anything. Your task is to complete the function leafNodes() which takes the array arr[] and its size N as input parameters and returns the leaf nodes of the tree.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{3}
1 β€ arr[i] β€ 10^{3} | class Solution:
def leafNodes(self, arr, N):
i, j = 0, 1
stack = []
result = []
while j < N:
if arr[i] > arr[j]:
stack.append(arr[i])
else:
flag = False
while stack and stack[-1] < arr[j]:
flag = True
stack.pop()
if flag:
result.append(arr[i])
i += 1
j += 1
result.append(arr[i])
return result | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR LIST WHILE VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Given a preorder traversal of a BST, find the leaf nodes of the tree without building the tree.
Example 1:
Input:
N = 2
arr = {2,1}
Output: {1}
Explaination: 1 is the only leaf node.
Example 2:
Input:
N = 3
Arr = {3, 2, 4}
Output: {2, 4}
Explaination: 2, 4 are the leaf nodes.
Your Task:
You don't need to read input or print anything. Your task is to complete the function leafNodes() which takes the array arr[] and its size N as input parameters and returns the leaf nodes of the tree.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{3}
1 β€ arr[i] β€ 10^{3} | class Solution:
def leafNodes(self, arr, N):
ans = []
def solve(a):
if 0 <= len(a) < 2:
ans.extend(a)
return
root = a[0]
i = 1
while i < len(a) and root > a[i]:
i += 1
solve(a[1:i])
solve(a[i:])
solve(arr)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Given a preorder traversal of a BST, find the leaf nodes of the tree without building the tree.
Example 1:
Input:
N = 2
arr = {2,1}
Output: {1}
Explaination: 1 is the only leaf node.
Example 2:
Input:
N = 3
Arr = {3, 2, 4}
Output: {2, 4}
Explaination: 2, 4 are the leaf nodes.
Your Task:
You don't need to read input or print anything. Your task is to complete the function leafNodes() which takes the array arr[] and its size N as input parameters and returns the leaf nodes of the tree.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{3}
1 β€ arr[i] β€ 10^{3} | class Solution:
def leafNodes(self, arr, N):
st = []
leaf = []
st.append(arr[0])
t = 0
for i in range(1, N):
if arr[i] > st[t]:
if t > 0 and arr[i] > st[t - 1]:
leaf.append(st.pop())
t -= 1
while t >= 0 and st[t] < arr[i]:
st.pop()
t -= 1
st.append(arr[i])
t += 1
else:
st.append(arr[i])
t += 1
leaf.append(arr[N - 1])
return leaf | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR |
Given a preorder traversal of a BST, find the leaf nodes of the tree without building the tree.
Example 1:
Input:
N = 2
arr = {2,1}
Output: {1}
Explaination: 1 is the only leaf node.
Example 2:
Input:
N = 3
Arr = {3, 2, 4}
Output: {2, 4}
Explaination: 2, 4 are the leaf nodes.
Your Task:
You don't need to read input or print anything. Your task is to complete the function leafNodes() which takes the array arr[] and its size N as input parameters and returns the leaf nodes of the tree.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{3}
1 β€ arr[i] β€ 10^{3} | class Solution:
def leafNodes(self, arr, N):
sol = []
stk = []
i = 0
for j in range(1, N):
flag = False
if arr[i] > arr[j]:
stk.append(arr[i])
else:
while len(stk) > 0:
if arr[j] > stk[-1]:
stk.pop()
if not flag:
sol.append(arr[i])
flag = True
else:
break
i += 1
sol.append(arr[-1])
return list(sol) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR |
Given a preorder traversal of a BST, find the leaf nodes of the tree without building the tree.
Example 1:
Input:
N = 2
arr = {2,1}
Output: {1}
Explaination: 1 is the only leaf node.
Example 2:
Input:
N = 3
Arr = {3, 2, 4}
Output: {2, 4}
Explaination: 2, 4 are the leaf nodes.
Your Task:
You don't need to read input or print anything. Your task is to complete the function leafNodes() which takes the array arr[] and its size N as input parameters and returns the leaf nodes of the tree.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{3}
1 β€ arr[i] β€ 10^{3} | class Solution:
def leafNodes(self, arr, N):
if len(arr) == 1:
return arr
stack = []
leafNode = []
stack.append(arr[0])
i = 1
while i < len(arr):
if arr[i] <= stack[-1]:
stack.append(arr[i])
else:
temp = stack[-1]
removed = 0
while stack and arr[i] > stack[-1]:
stack.pop()
removed += 1
stack.append(arr[i])
if removed >= 2:
leafNode.append(temp)
i += 1
leafNode.append(stack[-1])
return leafNode | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR |
Given a preorder traversal of a BST, find the leaf nodes of the tree without building the tree.
Example 1:
Input:
N = 2
arr = {2,1}
Output: {1}
Explaination: 1 is the only leaf node.
Example 2:
Input:
N = 3
Arr = {3, 2, 4}
Output: {2, 4}
Explaination: 2, 4 are the leaf nodes.
Your Task:
You don't need to read input or print anything. Your task is to complete the function leafNodes() which takes the array arr[] and its size N as input parameters and returns the leaf nodes of the tree.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{3}
1 β€ arr[i] β€ 10^{3} | class Solution:
def leafNodes(self, arr, N):
if len(arr) == 0:
return []
if len(arr) == 1:
return [arr[0]]
for i in range(1, len(arr)):
if arr[i] > arr[0]:
idx = i
break
else:
idx = i + 1
left = self.leafNodes(arr[1:idx], len(arr[1:idx]))
right = self.leafNodes(arr[idx:], len(arr[idx:]))
return left + right | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN LIST IF FUNC_CALL VAR VAR NUMBER RETURN LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR VAR |
Given a preorder traversal of a BST, find the leaf nodes of the tree without building the tree.
Example 1:
Input:
N = 2
arr = {2,1}
Output: {1}
Explaination: 1 is the only leaf node.
Example 2:
Input:
N = 3
Arr = {3, 2, 4}
Output: {2, 4}
Explaination: 2, 4 are the leaf nodes.
Your Task:
You don't need to read input or print anything. Your task is to complete the function leafNodes() which takes the array arr[] and its size N as input parameters and returns the leaf nodes of the tree.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{3}
1 β€ arr[i] β€ 10^{3} | class Solution:
def leafNodes(self, arr, N):
if N == 1:
return [arr[0]]
if N == 2:
return [arr[-1]]
stack = [arr[0]]
res = []
for i in range(1, N):
if arr[i] < stack[-1]:
stack.append(arr[i])
else:
check = stack.pop()
if len(stack) > 0 and arr[i] >= stack[-1]:
res.append(check)
while len(stack) > 0 and arr[i] >= stack[-1]:
stack.pop()
stack.append(arr[i])
res.append(stack[-1])
return res | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN LIST VAR NUMBER IF VAR NUMBER RETURN LIST VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR |
Given a preorder traversal of a BST, find the leaf nodes of the tree without building the tree.
Example 1:
Input:
N = 2
arr = {2,1}
Output: {1}
Explaination: 1 is the only leaf node.
Example 2:
Input:
N = 3
Arr = {3, 2, 4}
Output: {2, 4}
Explaination: 2, 4 are the leaf nodes.
Your Task:
You don't need to read input or print anything. Your task is to complete the function leafNodes() which takes the array arr[] and its size N as input parameters and returns the leaf nodes of the tree.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{3}
1 β€ arr[i] β€ 10^{3} | class Solution:
def leafNodes(self, arr, N):
st = []
lis = []
st.append(arr[0])
for i in range(1, N):
if arr[i] < st[-1]:
st.append(arr[i])
else:
temp = st[-1]
count = 0
while len(st) != 0 and st[-1] < arr[i]:
st.pop()
count += 1
st.append(arr[i])
if count >= 2:
lis.append(temp)
lis.append(st[-1])
return lis | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR |
Given a preorder traversal of a BST, find the leaf nodes of the tree without building the tree.
Example 1:
Input:
N = 2
arr = {2,1}
Output: {1}
Explaination: 1 is the only leaf node.
Example 2:
Input:
N = 3
Arr = {3, 2, 4}
Output: {2, 4}
Explaination: 2, 4 are the leaf nodes.
Your Task:
You don't need to read input or print anything. Your task is to complete the function leafNodes() which takes the array arr[] and its size N as input parameters and returns the leaf nodes of the tree.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{3}
1 β€ arr[i] β€ 10^{3} | class Solution:
def leafNodes(self, arr, N):
stack = []
output = []
for num in arr:
possibleLeafNode = None
totalRemoved = 0
while stack and stack[-1] < num:
if not possibleLeafNode:
possibleLeafNode = stack[-1]
stack.pop()
totalRemoved += 1
if totalRemoved >= 2:
output.append(possibleLeafNode)
stack.append(num)
output.append(arr[-1])
return output | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NONE ASSIGN VAR NUMBER WHILE VAR VAR NUMBER VAR IF VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR |
Given a preorder traversal of a BST, find the leaf nodes of the tree without building the tree.
Example 1:
Input:
N = 2
arr = {2,1}
Output: {1}
Explaination: 1 is the only leaf node.
Example 2:
Input:
N = 3
Arr = {3, 2, 4}
Output: {2, 4}
Explaination: 2, 4 are the leaf nodes.
Your Task:
You don't need to read input or print anything. Your task is to complete the function leafNodes() which takes the array arr[] and its size N as input parameters and returns the leaf nodes of the tree.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{3}
1 β€ arr[i] β€ 10^{3} | class Solution:
def leafNodesHelper(self, l, r, arr, leaves):
if l > r:
return
if l == r:
leaves.append(arr[l])
return
mid = l + 1
while mid <= r and arr[mid] < arr[l]:
mid += 1
self.leafNodesHelper(l + 1, mid - 1, arr, leaves)
self.leafNodesHelper(mid, r, arr, leaves)
def leafNodes(self, arr, N):
leaves = []
self.leafNodesHelper(0, N - 1, arr, leaves)
return leaves | CLASS_DEF FUNC_DEF IF VAR VAR RETURN IF VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR RETURN VAR |
Given a preorder traversal of a BST, find the leaf nodes of the tree without building the tree.
Example 1:
Input:
N = 2
arr = {2,1}
Output: {1}
Explaination: 1 is the only leaf node.
Example 2:
Input:
N = 3
Arr = {3, 2, 4}
Output: {2, 4}
Explaination: 2, 4 are the leaf nodes.
Your Task:
You don't need to read input or print anything. Your task is to complete the function leafNodes() which takes the array arr[] and its size N as input parameters and returns the leaf nodes of the tree.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{3}
1 β€ arr[i] β€ 10^{3} | st = ""
out = []
def check(arr):
global st
global out
if len(arr) == 0:
return
if len(arr) == 1:
st = st[1:]
out.append(arr[0])
return
else:
j = arr.index(st[0])
st = st[1:]
check(arr[0:j])
check(arr[j + 1 :])
return
class Solution:
def leafNodes(self, arr, N):
global st
global out
out = []
st = arr
b = sorted(arr)
check(b)
return out | ASSIGN VAR STRING ASSIGN VAR LIST FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Given a preorder traversal of a BST, find the leaf nodes of the tree without building the tree.
Example 1:
Input:
N = 2
arr = {2,1}
Output: {1}
Explaination: 1 is the only leaf node.
Example 2:
Input:
N = 3
Arr = {3, 2, 4}
Output: {2, 4}
Explaination: 2, 4 are the leaf nodes.
Your Task:
You don't need to read input or print anything. Your task is to complete the function leafNodes() which takes the array arr[] and its size N as input parameters and returns the leaf nodes of the tree.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{3}
1 β€ arr[i] β€ 10^{3} | class Solution:
def leafNodes(self, arr, N):
st = []
l = []
for i in range(N - 1):
f = 0
if arr[i] > arr[i + 1]:
st.append(arr[i])
else:
while len(st) > 0:
if arr[i + 1] > st[-1]:
st.pop(-1)
f = 1
else:
break
if f == 1:
l.append(arr[i])
l.append(arr[-1])
return l | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR |
Chef has binary string A of length N. He constructs a new binary string B by concatenating M copies of A together. For example, if A = \texttt{"10010"}, M = 3, then B = \texttt{"100101001010010"}.
Chef calls an index i (1 β€ i β€ N \cdot M) *good* if:
pref_{i} = suf_{i + 1}.
Here, pref_{j} = B_{1} + B_{2} + \ldots + B_{j} and
suf_{j} = B_{j} + B_{j + 1} + \ldots + B_{N \cdot M} (Note that suf_{N \cdot M + 1} = 0 by definition)
Chef wants to find the number of good indices in B. Can you help him do so?
------ Input Format ------
- The first line contains a single integer T β the number of test cases. Then the test cases follow.
- The first line of each test case contains two space-separated integers N and M β the length of the binary string A and the number of times A is concatenated to form
- The second line of each test case contains a binary string A of length N containing 0s and 1s only.
------ Output Format ------
For each test case, output the number of good indices in B.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N, M β€ 10^{5}$
$A$ is a binary string, i.e, contains only the characters $0$ and $1$.
- The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
- The sum of $M$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
2 2
00
2 4
11
3 3
101
----- Sample Output 1 ------
4
1
2
----- explanation 1 ------
Test case $1$: $B = \texttt{"0000"}$. In this string, all the indices are good.
Test case $2$: $B = \texttt{"11111111"}$. In this string, only $i = 4$ is good.
Test case $3$: $B = \texttt{"101101101"}$. In this string, $i = 4$ and $i = 5$ are good. | for i in range(int(input())):
n, m = map(int, input().split())
s = input()
c_1 = s.count("1")
c = 0
if c_1 == 0:
print(n * m)
elif c_1 * m % 2 == 1:
print(0)
elif m % 2 == 0:
for i in range(n):
if s[i] == "0":
c = c + 1
else:
break
for i in reversed(range(n)):
if s[i] == "0":
c = c + 1
else:
break
print(c + 1)
else:
summ = 0
for i in range(n):
if s[i] == "1":
summ = summ + 1
if summ == int(c_1 / 2):
c = c + 1
print(c) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Chef has binary string A of length N. He constructs a new binary string B by concatenating M copies of A together. For example, if A = \texttt{"10010"}, M = 3, then B = \texttt{"100101001010010"}.
Chef calls an index i (1 β€ i β€ N \cdot M) *good* if:
pref_{i} = suf_{i + 1}.
Here, pref_{j} = B_{1} + B_{2} + \ldots + B_{j} and
suf_{j} = B_{j} + B_{j + 1} + \ldots + B_{N \cdot M} (Note that suf_{N \cdot M + 1} = 0 by definition)
Chef wants to find the number of good indices in B. Can you help him do so?
------ Input Format ------
- The first line contains a single integer T β the number of test cases. Then the test cases follow.
- The first line of each test case contains two space-separated integers N and M β the length of the binary string A and the number of times A is concatenated to form
- The second line of each test case contains a binary string A of length N containing 0s and 1s only.
------ Output Format ------
For each test case, output the number of good indices in B.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N, M β€ 10^{5}$
$A$ is a binary string, i.e, contains only the characters $0$ and $1$.
- The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
- The sum of $M$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
2 2
00
2 4
11
3 3
101
----- Sample Output 1 ------
4
1
2
----- explanation 1 ------
Test case $1$: $B = \texttt{"0000"}$. In this string, all the indices are good.
Test case $2$: $B = \texttt{"11111111"}$. In this string, only $i = 4$ is good.
Test case $3$: $B = \texttt{"101101101"}$. In this string, $i = 4$ and $i = 5$ are good. | t = int(input())
for _ in range(t):
n, m = map(int, input().split())
a = input()
p = []
k = 0
for i in a:
k += int(i)
p.append(k)
s = [(0) for i in range(n)]
k = 0
for i in range(n - 1, -1, -1):
k += int(a[i])
s[i] = k
s.append(0)
if int(a) == 0:
print(n * m)
elif m % 2 != 0:
c = 0
for i in range(n):
if p[i] == s[i + 1]:
c += 1
print(c)
else:
c = 1
if a[n - 1] == "0":
for i in range(n - 2, -1, -1):
c += 1
if a[i] == "1":
break
for i in range(n):
if a[i] == "1":
break
c += 1
print(c) | 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 FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER IF VAR VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR |
Chef has binary string A of length N. He constructs a new binary string B by concatenating M copies of A together. For example, if A = \texttt{"10010"}, M = 3, then B = \texttt{"100101001010010"}.
Chef calls an index i (1 β€ i β€ N \cdot M) *good* if:
pref_{i} = suf_{i + 1}.
Here, pref_{j} = B_{1} + B_{2} + \ldots + B_{j} and
suf_{j} = B_{j} + B_{j + 1} + \ldots + B_{N \cdot M} (Note that suf_{N \cdot M + 1} = 0 by definition)
Chef wants to find the number of good indices in B. Can you help him do so?
------ Input Format ------
- The first line contains a single integer T β the number of test cases. Then the test cases follow.
- The first line of each test case contains two space-separated integers N and M β the length of the binary string A and the number of times A is concatenated to form
- The second line of each test case contains a binary string A of length N containing 0s and 1s only.
------ Output Format ------
For each test case, output the number of good indices in B.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N, M β€ 10^{5}$
$A$ is a binary string, i.e, contains only the characters $0$ and $1$.
- The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
- The sum of $M$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
2 2
00
2 4
11
3 3
101
----- Sample Output 1 ------
4
1
2
----- explanation 1 ------
Test case $1$: $B = \texttt{"0000"}$. In this string, all the indices are good.
Test case $2$: $B = \texttt{"11111111"}$. In this string, only $i = 4$ is good.
Test case $3$: $B = \texttt{"101101101"}$. In this string, $i = 4$ and $i = 5$ are good. | t = int(input())
def main(s):
l = list(s)
k = l.count("1")
if k % 2 != 0:
return 0
else:
c = []
for i in range(len(s)):
if s[i] == "1":
c.append(i)
p = c[len(c) // 2] - c[len(c) // 2 - 1]
return p
for i in range(t):
l = list(map(int, input().split()))
n = l[0]
x = l[1]
a = input()
if a == "0" * n:
print(n * x)
else:
p = main(a)
s = 2 * a
q = main(s)
if x % 2 == 0:
print(q)
else:
print(p) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Chef has binary string A of length N. He constructs a new binary string B by concatenating M copies of A together. For example, if A = \texttt{"10010"}, M = 3, then B = \texttt{"100101001010010"}.
Chef calls an index i (1 β€ i β€ N \cdot M) *good* if:
pref_{i} = suf_{i + 1}.
Here, pref_{j} = B_{1} + B_{2} + \ldots + B_{j} and
suf_{j} = B_{j} + B_{j + 1} + \ldots + B_{N \cdot M} (Note that suf_{N \cdot M + 1} = 0 by definition)
Chef wants to find the number of good indices in B. Can you help him do so?
------ Input Format ------
- The first line contains a single integer T β the number of test cases. Then the test cases follow.
- The first line of each test case contains two space-separated integers N and M β the length of the binary string A and the number of times A is concatenated to form
- The second line of each test case contains a binary string A of length N containing 0s and 1s only.
------ Output Format ------
For each test case, output the number of good indices in B.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N, M β€ 10^{5}$
$A$ is a binary string, i.e, contains only the characters $0$ and $1$.
- The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
- The sum of $M$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
2 2
00
2 4
11
3 3
101
----- Sample Output 1 ------
4
1
2
----- explanation 1 ------
Test case $1$: $B = \texttt{"0000"}$. In this string, all the indices are good.
Test case $2$: $B = \texttt{"11111111"}$. In this string, only $i = 4$ is good.
Test case $3$: $B = \texttt{"101101101"}$. In this string, $i = 4$ and $i = 5$ are good. | for _ in range(int(input())):
n, m = (int(i) for i in input().split())
a = input()
if a.count("0") == n:
print(n * m)
continue
if m % 2 == 0:
a = a + a
s = list(map(int, list(a)))
pre = [0] * len(s)
pre[0] = s[0]
for i in range(1, len(s)):
pre[i] = pre[i - 1] + s[i]
sm = sum(s)
cnt = 0
for i in range(len(s)):
if pre[i] == sm - pre[i]:
cnt += 1
print(cnt) | 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 FUNC_CALL VAR IF FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Chef has binary string A of length N. He constructs a new binary string B by concatenating M copies of A together. For example, if A = \texttt{"10010"}, M = 3, then B = \texttt{"100101001010010"}.
Chef calls an index i (1 β€ i β€ N \cdot M) *good* if:
pref_{i} = suf_{i + 1}.
Here, pref_{j} = B_{1} + B_{2} + \ldots + B_{j} and
suf_{j} = B_{j} + B_{j + 1} + \ldots + B_{N \cdot M} (Note that suf_{N \cdot M + 1} = 0 by definition)
Chef wants to find the number of good indices in B. Can you help him do so?
------ Input Format ------
- The first line contains a single integer T β the number of test cases. Then the test cases follow.
- The first line of each test case contains two space-separated integers N and M β the length of the binary string A and the number of times A is concatenated to form
- The second line of each test case contains a binary string A of length N containing 0s and 1s only.
------ Output Format ------
For each test case, output the number of good indices in B.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N, M β€ 10^{5}$
$A$ is a binary string, i.e, contains only the characters $0$ and $1$.
- The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
- The sum of $M$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
2 2
00
2 4
11
3 3
101
----- Sample Output 1 ------
4
1
2
----- explanation 1 ------
Test case $1$: $B = \texttt{"0000"}$. In this string, all the indices are good.
Test case $2$: $B = \texttt{"11111111"}$. In this string, only $i = 4$ is good.
Test case $3$: $B = \texttt{"101101101"}$. In this string, $i = 4$ and $i = 5$ are good. | n = int(input())
for i in range(n):
a, b = map(int, input().split())
c = input()
ones, d, e = c.count("1"), 0, 0
if ones == 0:
print(a * b)
elif ones * b % 2 == 1:
print(0)
elif b % 2 == 0:
i = 0
while i < a:
if c[i] == "1":
break
d += 1
i += 1
j = a - 1
while j >= 0:
if c[j] == "1":
break
d += 1
j -= 1
print(d + 1)
else:
i = 0
while i < a:
if c[i] == "1":
e += 1
if e == ones / 2:
d += 1
i += 1
print(d) | 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 FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR STRING NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Chef has binary string A of length N. He constructs a new binary string B by concatenating M copies of A together. For example, if A = \texttt{"10010"}, M = 3, then B = \texttt{"100101001010010"}.
Chef calls an index i (1 β€ i β€ N \cdot M) *good* if:
pref_{i} = suf_{i + 1}.
Here, pref_{j} = B_{1} + B_{2} + \ldots + B_{j} and
suf_{j} = B_{j} + B_{j + 1} + \ldots + B_{N \cdot M} (Note that suf_{N \cdot M + 1} = 0 by definition)
Chef wants to find the number of good indices in B. Can you help him do so?
------ Input Format ------
- The first line contains a single integer T β the number of test cases. Then the test cases follow.
- The first line of each test case contains two space-separated integers N and M β the length of the binary string A and the number of times A is concatenated to form
- The second line of each test case contains a binary string A of length N containing 0s and 1s only.
------ Output Format ------
For each test case, output the number of good indices in B.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N, M β€ 10^{5}$
$A$ is a binary string, i.e, contains only the characters $0$ and $1$.
- The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
- The sum of $M$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
2 2
00
2 4
11
3 3
101
----- Sample Output 1 ------
4
1
2
----- explanation 1 ------
Test case $1$: $B = \texttt{"0000"}$. In this string, all the indices are good.
Test case $2$: $B = \texttt{"11111111"}$. In this string, only $i = 4$ is good.
Test case $3$: $B = \texttt{"101101101"}$. In this string, $i = 4$ and $i = 5$ are good. | t = int(input())
while t > 0:
t -= 1
N, M = [int(x) for x in input().split()]
A = input()
target = A.count("1") * M
res = 0
cc = 0
start = N * (int(M / 2) - 1)
cc = target / M * (int(M / 2) - 1)
end = int(N * M / 2) + N
if target == 0:
res = M * N
elif target % 2 == 0:
target /= 2
for i in range(start, end):
if A[i % N] == "1":
cc += 1
if cc == target:
res += 1
if cc > target:
break
print(res) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR VAR STRING VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR |
Chef has binary string A of length N. He constructs a new binary string B by concatenating M copies of A together. For example, if A = \texttt{"10010"}, M = 3, then B = \texttt{"100101001010010"}.
Chef calls an index i (1 β€ i β€ N \cdot M) *good* if:
pref_{i} = suf_{i + 1}.
Here, pref_{j} = B_{1} + B_{2} + \ldots + B_{j} and
suf_{j} = B_{j} + B_{j + 1} + \ldots + B_{N \cdot M} (Note that suf_{N \cdot M + 1} = 0 by definition)
Chef wants to find the number of good indices in B. Can you help him do so?
------ Input Format ------
- The first line contains a single integer T β the number of test cases. Then the test cases follow.
- The first line of each test case contains two space-separated integers N and M β the length of the binary string A and the number of times A is concatenated to form
- The second line of each test case contains a binary string A of length N containing 0s and 1s only.
------ Output Format ------
For each test case, output the number of good indices in B.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N, M β€ 10^{5}$
$A$ is a binary string, i.e, contains only the characters $0$ and $1$.
- The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
- The sum of $M$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
2 2
00
2 4
11
3 3
101
----- Sample Output 1 ------
4
1
2
----- explanation 1 ------
Test case $1$: $B = \texttt{"0000"}$. In this string, all the indices are good.
Test case $2$: $B = \texttt{"11111111"}$. In this string, only $i = 4$ is good.
Test case $3$: $B = \texttt{"101101101"}$. In this string, $i = 4$ and $i = 5$ are good. | for _ in range(int(input())):
n, m = map(int, input().split(" "))
a = input()
s = []
t = 0
for i in range(n):
t += int(a[i])
s.append(t)
res = 0
if t == 0:
print(n * m)
elif m * t % 2 == 1:
print(0)
else:
for j in range(m):
pref = j * t
suf = (m - j - 1) * t
if abs(suf - pref) <= t:
for i in range(n):
if pref + int(s[i]) == suf + t - int(s[i]):
res += 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 STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Given two integer arrays A1[ ] and A2[ ] of size N and M respectively. Sort the first array A1[ ] such that all the relative positions of the elements in the first array are the same as the elements in the second array A2[ ].
See example for better understanding.
Note: If elements are repeated in the second array, consider their first occurance only.
Example 1:
Input:
N = 11
M = 4
A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8}
A2[] = {2, 1, 8, 3}
Output:
2 2 1 1 8 8 3 5 6 7 9
Explanation: Array elements of A1[] are
sorted according to A2[]. So 2 comes first
then 1 comes, then comes 8, then finally 3
comes, now we append remaining elements in
sorted order.
Example 2:
Input:
N = 11
M = 4
A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8}
A2[] = {99, 22, 444, 56}
Output:
1 1 2 2 3 5 6 7 8 8 9
Explanation: No A1[] elements are in A2[]
so we cannot sort A1[] according to A2[].
Hence we sort the elements in non-decreasing
order.
Your Task:
You don't need to read input or print anything. Your task is to complete the function sortA1ByA2() which takes the array A1[ ], array A2[ ] and their respective size N and M as input parameters and returns the sorted array A1[ ] such that the relative positions of the elements in A1[ ] are same as the elements in A2[ ]. For the elements not present in A2[ ] but in A1[ ], it appends them at the last in increasing order.
Expected Time Complexity: O(N * Log(N)).
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N, M β€ 10^{6}
1 β€ A1[i], A2[i] β€ 10^{6} | class Solution:
def relativeSort(self, A1, N, A2, M):
dict = {}
l = []
A1.sort()
for i in A1:
if i in dict:
dict[i] += 1
else:
dict[i] = 1
for i in A2:
if i in dict:
while dict[i] > 0:
l.append(i)
dict[i] -= 1
for i in A1:
if i in dict:
while dict[i] > 0:
l.append(i)
dict[i] -= 1
return l | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR LIST EXPR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR WHILE VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR VAR IF VAR VAR WHILE VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR |
Given two integer arrays A1[ ] and A2[ ] of size N and M respectively. Sort the first array A1[ ] such that all the relative positions of the elements in the first array are the same as the elements in the second array A2[ ].
See example for better understanding.
Note: If elements are repeated in the second array, consider their first occurance only.
Example 1:
Input:
N = 11
M = 4
A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8}
A2[] = {2, 1, 8, 3}
Output:
2 2 1 1 8 8 3 5 6 7 9
Explanation: Array elements of A1[] are
sorted according to A2[]. So 2 comes first
then 1 comes, then comes 8, then finally 3
comes, now we append remaining elements in
sorted order.
Example 2:
Input:
N = 11
M = 4
A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8}
A2[] = {99, 22, 444, 56}
Output:
1 1 2 2 3 5 6 7 8 8 9
Explanation: No A1[] elements are in A2[]
so we cannot sort A1[] according to A2[].
Hence we sort the elements in non-decreasing
order.
Your Task:
You don't need to read input or print anything. Your task is to complete the function sortA1ByA2() which takes the array A1[ ], array A2[ ] and their respective size N and M as input parameters and returns the sorted array A1[ ] such that the relative positions of the elements in A1[ ] are same as the elements in A2[ ]. For the elements not present in A2[ ] but in A1[ ], it appends them at the last in increasing order.
Expected Time Complexity: O(N * Log(N)).
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N, M β€ 10^{6}
1 β€ A1[i], A2[i] β€ 10^{6} | class Solution:
def relativeSort(self, A1, N, A2, M):
d = {}
for i in A1:
d[i] = d.get(i, 0) + 1
ans = []
flg = 0
for i in A2:
if i in d:
ans.extend([i] * d[i])
d[i] = 0
flg = 1
tmp = len(ans)
for i in d:
if d[i] != 0:
ans.extend([i] * d[i])
if flg == 0:
return sorted(A1)
else:
ans[tmp:] = sorted(ans[tmp:])
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given two integer arrays A1[ ] and A2[ ] of size N and M respectively. Sort the first array A1[ ] such that all the relative positions of the elements in the first array are the same as the elements in the second array A2[ ].
See example for better understanding.
Note: If elements are repeated in the second array, consider their first occurance only.
Example 1:
Input:
N = 11
M = 4
A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8}
A2[] = {2, 1, 8, 3}
Output:
2 2 1 1 8 8 3 5 6 7 9
Explanation: Array elements of A1[] are
sorted according to A2[]. So 2 comes first
then 1 comes, then comes 8, then finally 3
comes, now we append remaining elements in
sorted order.
Example 2:
Input:
N = 11
M = 4
A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8}
A2[] = {99, 22, 444, 56}
Output:
1 1 2 2 3 5 6 7 8 8 9
Explanation: No A1[] elements are in A2[]
so we cannot sort A1[] according to A2[].
Hence we sort the elements in non-decreasing
order.
Your Task:
You don't need to read input or print anything. Your task is to complete the function sortA1ByA2() which takes the array A1[ ], array A2[ ] and their respective size N and M as input parameters and returns the sorted array A1[ ] such that the relative positions of the elements in A1[ ] are same as the elements in A2[ ]. For the elements not present in A2[ ] but in A1[ ], it appends them at the last in increasing order.
Expected Time Complexity: O(N * Log(N)).
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N, M β€ 10^{6}
1 β€ A1[i], A2[i] β€ 10^{6} | class Solution:
def relativeSort(self, A1, N, A2, M):
freq = {}
for i in range(N):
if A1[i] in freq:
freq[A1[i]] += 1
else:
freq[A1[i]] = 1
result = []
for i in range(M):
if A2[i] in freq:
for j in range(freq[A2[i]]):
result.append(A2[i])
del freq[A2[i]]
remaining = sorted(freq.keys())
for i in range(len(remaining)):
for j in range(freq[remaining[i]]):
result.append(remaining[i])
return result | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Given two integer arrays A1[ ] and A2[ ] of size N and M respectively. Sort the first array A1[ ] such that all the relative positions of the elements in the first array are the same as the elements in the second array A2[ ].
See example for better understanding.
Note: If elements are repeated in the second array, consider their first occurance only.
Example 1:
Input:
N = 11
M = 4
A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8}
A2[] = {2, 1, 8, 3}
Output:
2 2 1 1 8 8 3 5 6 7 9
Explanation: Array elements of A1[] are
sorted according to A2[]. So 2 comes first
then 1 comes, then comes 8, then finally 3
comes, now we append remaining elements in
sorted order.
Example 2:
Input:
N = 11
M = 4
A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8}
A2[] = {99, 22, 444, 56}
Output:
1 1 2 2 3 5 6 7 8 8 9
Explanation: No A1[] elements are in A2[]
so we cannot sort A1[] according to A2[].
Hence we sort the elements in non-decreasing
order.
Your Task:
You don't need to read input or print anything. Your task is to complete the function sortA1ByA2() which takes the array A1[ ], array A2[ ] and their respective size N and M as input parameters and returns the sorted array A1[ ] such that the relative positions of the elements in A1[ ] are same as the elements in A2[ ]. For the elements not present in A2[ ] but in A1[ ], it appends them at the last in increasing order.
Expected Time Complexity: O(N * Log(N)).
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N, M β€ 10^{6}
1 β€ A1[i], A2[i] β€ 10^{6} | class Solution:
def relativeSort(self, A1, N, A2, M):
dic = {}
li = []
oli = []
for i in range(M):
dic[A2[i]] = 0
for i in range(N):
if A1[i] in dic:
dic[A1[i]] += 1
else:
oli.append(A1[i])
oli.sort()
tmpli = []
for i in range(M):
tmpli.extend(A2[i] for j in range(dic[A2[i]]))
li = li + tmpli
li = li + oli
return li | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR |
Given two integer arrays A1[ ] and A2[ ] of size N and M respectively. Sort the first array A1[ ] such that all the relative positions of the elements in the first array are the same as the elements in the second array A2[ ].
See example for better understanding.
Note: If elements are repeated in the second array, consider their first occurance only.
Example 1:
Input:
N = 11
M = 4
A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8}
A2[] = {2, 1, 8, 3}
Output:
2 2 1 1 8 8 3 5 6 7 9
Explanation: Array elements of A1[] are
sorted according to A2[]. So 2 comes first
then 1 comes, then comes 8, then finally 3
comes, now we append remaining elements in
sorted order.
Example 2:
Input:
N = 11
M = 4
A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8}
A2[] = {99, 22, 444, 56}
Output:
1 1 2 2 3 5 6 7 8 8 9
Explanation: No A1[] elements are in A2[]
so we cannot sort A1[] according to A2[].
Hence we sort the elements in non-decreasing
order.
Your Task:
You don't need to read input or print anything. Your task is to complete the function sortA1ByA2() which takes the array A1[ ], array A2[ ] and their respective size N and M as input parameters and returns the sorted array A1[ ] such that the relative positions of the elements in A1[ ] are same as the elements in A2[ ]. For the elements not present in A2[ ] but in A1[ ], it appends them at the last in increasing order.
Expected Time Complexity: O(N * Log(N)).
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N, M β€ 10^{6}
1 β€ A1[i], A2[i] β€ 10^{6} | class Solution:
def relativeSort(self, A1, N, A2, M):
z = []
g = {}
for i in A1:
g.setdefault(i, 0)
g[i] += 1
for i in A2:
try:
z.extend([i] * g[i])
del g[i]
except:
continue
for i in sorted(list(g.keys())):
z.extend([i] * g[i])
return z | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR VAR RETURN VAR |
Given two integer arrays A1[ ] and A2[ ] of size N and M respectively. Sort the first array A1[ ] such that all the relative positions of the elements in the first array are the same as the elements in the second array A2[ ].
See example for better understanding.
Note: If elements are repeated in the second array, consider their first occurance only.
Example 1:
Input:
N = 11
M = 4
A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8}
A2[] = {2, 1, 8, 3}
Output:
2 2 1 1 8 8 3 5 6 7 9
Explanation: Array elements of A1[] are
sorted according to A2[]. So 2 comes first
then 1 comes, then comes 8, then finally 3
comes, now we append remaining elements in
sorted order.
Example 2:
Input:
N = 11
M = 4
A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8}
A2[] = {99, 22, 444, 56}
Output:
1 1 2 2 3 5 6 7 8 8 9
Explanation: No A1[] elements are in A2[]
so we cannot sort A1[] according to A2[].
Hence we sort the elements in non-decreasing
order.
Your Task:
You don't need to read input or print anything. Your task is to complete the function sortA1ByA2() which takes the array A1[ ], array A2[ ] and their respective size N and M as input parameters and returns the sorted array A1[ ] such that the relative positions of the elements in A1[ ] are same as the elements in A2[ ]. For the elements not present in A2[ ] but in A1[ ], it appends them at the last in increasing order.
Expected Time Complexity: O(N * Log(N)).
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N, M β€ 10^{6}
1 β€ A1[i], A2[i] β€ 10^{6} | class Solution:
def relativeSort(self, A1, N, A2, M):
a1_counts = {}
for x in A1:
if x in a1_counts:
a1_counts[x] += 1
else:
a1_counts[x] = 1
relative_sort = []
list_a1_only = []
for y in A2:
if y in a1_counts:
relative_sort += [y] * a1_counts[y]
del a1_counts[y]
for val, count in a1_counts.items():
list_a1_only += [val] * count
relative_sort += sorted(list_a1_only)
return relative_sort | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR VAR VAR BIN_OP LIST VAR VAR VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR |
Given two integer arrays A1[ ] and A2[ ] of size N and M respectively. Sort the first array A1[ ] such that all the relative positions of the elements in the first array are the same as the elements in the second array A2[ ].
See example for better understanding.
Note: If elements are repeated in the second array, consider their first occurance only.
Example 1:
Input:
N = 11
M = 4
A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8}
A2[] = {2, 1, 8, 3}
Output:
2 2 1 1 8 8 3 5 6 7 9
Explanation: Array elements of A1[] are
sorted according to A2[]. So 2 comes first
then 1 comes, then comes 8, then finally 3
comes, now we append remaining elements in
sorted order.
Example 2:
Input:
N = 11
M = 4
A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8}
A2[] = {99, 22, 444, 56}
Output:
1 1 2 2 3 5 6 7 8 8 9
Explanation: No A1[] elements are in A2[]
so we cannot sort A1[] according to A2[].
Hence we sort the elements in non-decreasing
order.
Your Task:
You don't need to read input or print anything. Your task is to complete the function sortA1ByA2() which takes the array A1[ ], array A2[ ] and their respective size N and M as input parameters and returns the sorted array A1[ ] such that the relative positions of the elements in A1[ ] are same as the elements in A2[ ]. For the elements not present in A2[ ] but in A1[ ], it appends them at the last in increasing order.
Expected Time Complexity: O(N * Log(N)).
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N, M β€ 10^{6}
1 β€ A1[i], A2[i] β€ 10^{6} | class Solution:
def relativeSort(self, A1, N, A2, M):
freq = {}
for i in A1:
freq[i] = freq.get(i, 0) + 1
ans = []
for i in A2:
if i in freq:
ans.extend([i] * freq[i])
del freq[i]
l = len(ans)
for i in freq:
ans.extend([i] * freq[i])
ans[l:] = sorted(ans[l:])
return ans
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR RETURN VAR |
Given two integer arrays A1[ ] and A2[ ] of size N and M respectively. Sort the first array A1[ ] such that all the relative positions of the elements in the first array are the same as the elements in the second array A2[ ].
See example for better understanding.
Note: If elements are repeated in the second array, consider their first occurance only.
Example 1:
Input:
N = 11
M = 4
A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8}
A2[] = {2, 1, 8, 3}
Output:
2 2 1 1 8 8 3 5 6 7 9
Explanation: Array elements of A1[] are
sorted according to A2[]. So 2 comes first
then 1 comes, then comes 8, then finally 3
comes, now we append remaining elements in
sorted order.
Example 2:
Input:
N = 11
M = 4
A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8}
A2[] = {99, 22, 444, 56}
Output:
1 1 2 2 3 5 6 7 8 8 9
Explanation: No A1[] elements are in A2[]
so we cannot sort A1[] according to A2[].
Hence we sort the elements in non-decreasing
order.
Your Task:
You don't need to read input or print anything. Your task is to complete the function sortA1ByA2() which takes the array A1[ ], array A2[ ] and their respective size N and M as input parameters and returns the sorted array A1[ ] such that the relative positions of the elements in A1[ ] are same as the elements in A2[ ]. For the elements not present in A2[ ] but in A1[ ], it appends them at the last in increasing order.
Expected Time Complexity: O(N * Log(N)).
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N, M β€ 10^{6}
1 β€ A1[i], A2[i] β€ 10^{6} | class Solution:
def relativeSort(self, A1, N, A2, M):
dictin = {}
l = []
gq = []
for i in A1:
if i not in dictin:
dictin[i] = 1
else:
dictin[i] += 1
for i in range(M):
if A2[i] in dictin:
l += [A2[i]] * dictin[A2[i]]
del dictin[A2[i]]
li = list(dictin.keys())
li.sort()
for i in li:
l += [i] * dictin[i]
return l | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP LIST VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR VAR VAR BIN_OP LIST VAR VAR VAR RETURN VAR |
Given two integer arrays A1[ ] and A2[ ] of size N and M respectively. Sort the first array A1[ ] such that all the relative positions of the elements in the first array are the same as the elements in the second array A2[ ].
See example for better understanding.
Note: If elements are repeated in the second array, consider their first occurance only.
Example 1:
Input:
N = 11
M = 4
A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8}
A2[] = {2, 1, 8, 3}
Output:
2 2 1 1 8 8 3 5 6 7 9
Explanation: Array elements of A1[] are
sorted according to A2[]. So 2 comes first
then 1 comes, then comes 8, then finally 3
comes, now we append remaining elements in
sorted order.
Example 2:
Input:
N = 11
M = 4
A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8}
A2[] = {99, 22, 444, 56}
Output:
1 1 2 2 3 5 6 7 8 8 9
Explanation: No A1[] elements are in A2[]
so we cannot sort A1[] according to A2[].
Hence we sort the elements in non-decreasing
order.
Your Task:
You don't need to read input or print anything. Your task is to complete the function sortA1ByA2() which takes the array A1[ ], array A2[ ] and their respective size N and M as input parameters and returns the sorted array A1[ ] such that the relative positions of the elements in A1[ ] are same as the elements in A2[ ]. For the elements not present in A2[ ] but in A1[ ], it appends them at the last in increasing order.
Expected Time Complexity: O(N * Log(N)).
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N, M β€ 10^{6}
1 β€ A1[i], A2[i] β€ 10^{6} | class Solution:
def relativeSort(self, A1, N, A2, M):
counts = {}
for element in A1:
if element in counts:
counts[element] += 1
else:
counts[element] = 1
sorted_A1 = []
for element in A2:
if element in counts:
sorted_A1.extend([element] * counts[element])
del counts[element]
remaining_elements = sorted(counts.keys())
for element in remaining_elements:
sorted_A1.extend([element] * counts[element])
return sorted_A1 | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR VAR RETURN VAR |
Given two integer arrays A1[ ] and A2[ ] of size N and M respectively. Sort the first array A1[ ] such that all the relative positions of the elements in the first array are the same as the elements in the second array A2[ ].
See example for better understanding.
Note: If elements are repeated in the second array, consider their first occurance only.
Example 1:
Input:
N = 11
M = 4
A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8}
A2[] = {2, 1, 8, 3}
Output:
2 2 1 1 8 8 3 5 6 7 9
Explanation: Array elements of A1[] are
sorted according to A2[]. So 2 comes first
then 1 comes, then comes 8, then finally 3
comes, now we append remaining elements in
sorted order.
Example 2:
Input:
N = 11
M = 4
A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8}
A2[] = {99, 22, 444, 56}
Output:
1 1 2 2 3 5 6 7 8 8 9
Explanation: No A1[] elements are in A2[]
so we cannot sort A1[] according to A2[].
Hence we sort the elements in non-decreasing
order.
Your Task:
You don't need to read input or print anything. Your task is to complete the function sortA1ByA2() which takes the array A1[ ], array A2[ ] and their respective size N and M as input parameters and returns the sorted array A1[ ] such that the relative positions of the elements in A1[ ] are same as the elements in A2[ ]. For the elements not present in A2[ ] but in A1[ ], it appends them at the last in increasing order.
Expected Time Complexity: O(N * Log(N)).
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N, M β€ 10^{6}
1 β€ A1[i], A2[i] β€ 10^{6} | class Solution:
def relativeSort(self, A1, N, A2, M):
A1_dict = {}
for i in A1:
if i not in A1_dict:
A1_dict[i] = 1
else:
A1_dict[i] += 1
k = 0
for i in A2:
if i in A1_dict:
while A1_dict[i] > 0:
A1[k] = i
A1_dict[i] -= 1
k += 1
for key in sorted(A1_dict.keys()):
if A1_dict[key] != 0:
while A1_dict[key] > 0:
A1[k] = key
k += 1
A1_dict[key] -= 1
return A1 | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER RETURN VAR |
Given two integer arrays A1[ ] and A2[ ] of size N and M respectively. Sort the first array A1[ ] such that all the relative positions of the elements in the first array are the same as the elements in the second array A2[ ].
See example for better understanding.
Note: If elements are repeated in the second array, consider their first occurance only.
Example 1:
Input:
N = 11
M = 4
A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8}
A2[] = {2, 1, 8, 3}
Output:
2 2 1 1 8 8 3 5 6 7 9
Explanation: Array elements of A1[] are
sorted according to A2[]. So 2 comes first
then 1 comes, then comes 8, then finally 3
comes, now we append remaining elements in
sorted order.
Example 2:
Input:
N = 11
M = 4
A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8}
A2[] = {99, 22, 444, 56}
Output:
1 1 2 2 3 5 6 7 8 8 9
Explanation: No A1[] elements are in A2[]
so we cannot sort A1[] according to A2[].
Hence we sort the elements in non-decreasing
order.
Your Task:
You don't need to read input or print anything. Your task is to complete the function sortA1ByA2() which takes the array A1[ ], array A2[ ] and their respective size N and M as input parameters and returns the sorted array A1[ ] such that the relative positions of the elements in A1[ ] are same as the elements in A2[ ]. For the elements not present in A2[ ] but in A1[ ], it appends them at the last in increasing order.
Expected Time Complexity: O(N * Log(N)).
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N, M β€ 10^{6}
1 β€ A1[i], A2[i] β€ 10^{6} | class Solution:
def relativeSort(self, A1, N, A2, M):
def bs(a, l, r, x):
if r >= l:
mid = l + (r - l) // 2
if x == a[mid]:
while mid - 1 >= 0 and a[mid - 1] == x:
mid -= 1
return mid
elif a[mid] > x:
return bs(a, l, mid - 1, x)
else:
return bs(a, mid + 1, r, x)
return -1
A1.sort()
bb = list(set(A1).difference(set(A2)))
c = []
cc = []
for i in range(len(A2)):
ind = bs(A1, 0, len(A1) - 1, A2[i])
if ind == -1:
continue
c.append(A1[ind])
while ind + 1 < len(A1) and A1[ind + 1] == A1[ind]:
c.append(A1[ind])
ind += 1
for i in range(len(bb)):
ind = bs(A1, 0, len(A1) - 1, bb[i])
if ind == -1:
continue
cc.append(A1[ind])
while ind + 1 < len(A1) and A1[ind + 1] == A1[ind]:
cc.append(A1[ind])
ind += 1
cc.sort()
return c + cc | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR WHILE BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN VAR IF VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR WHILE BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR WHILE BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR RETURN BIN_OP VAR VAR |
Given two integer arrays A1[ ] and A2[ ] of size N and M respectively. Sort the first array A1[ ] such that all the relative positions of the elements in the first array are the same as the elements in the second array A2[ ].
See example for better understanding.
Note: If elements are repeated in the second array, consider their first occurance only.
Example 1:
Input:
N = 11
M = 4
A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8}
A2[] = {2, 1, 8, 3}
Output:
2 2 1 1 8 8 3 5 6 7 9
Explanation: Array elements of A1[] are
sorted according to A2[]. So 2 comes first
then 1 comes, then comes 8, then finally 3
comes, now we append remaining elements in
sorted order.
Example 2:
Input:
N = 11
M = 4
A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8}
A2[] = {99, 22, 444, 56}
Output:
1 1 2 2 3 5 6 7 8 8 9
Explanation: No A1[] elements are in A2[]
so we cannot sort A1[] according to A2[].
Hence we sort the elements in non-decreasing
order.
Your Task:
You don't need to read input or print anything. Your task is to complete the function sortA1ByA2() which takes the array A1[ ], array A2[ ] and their respective size N and M as input parameters and returns the sorted array A1[ ] such that the relative positions of the elements in A1[ ] are same as the elements in A2[ ]. For the elements not present in A2[ ] but in A1[ ], it appends them at the last in increasing order.
Expected Time Complexity: O(N * Log(N)).
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N, M β€ 10^{6}
1 β€ A1[i], A2[i] β€ 10^{6} | class Solution:
def relativeSort(self, A1, N, A2, M):
freq = {}
for i in range(N):
if A1[i] not in freq:
freq[A1[i]] = 1
else:
freq[A1[i]] += 1
result = []
for j in range(M):
if A2[j] in freq:
result.extend([A2[j]] * freq[A2[j]])
del freq[A2[j]]
for elem in sorted(freq.keys()):
result.extend([elem] * freq[elem])
return result | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR VAR RETURN VAR |
Given two integer arrays A1[ ] and A2[ ] of size N and M respectively. Sort the first array A1[ ] such that all the relative positions of the elements in the first array are the same as the elements in the second array A2[ ].
See example for better understanding.
Note: If elements are repeated in the second array, consider their first occurance only.
Example 1:
Input:
N = 11
M = 4
A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8}
A2[] = {2, 1, 8, 3}
Output:
2 2 1 1 8 8 3 5 6 7 9
Explanation: Array elements of A1[] are
sorted according to A2[]. So 2 comes first
then 1 comes, then comes 8, then finally 3
comes, now we append remaining elements in
sorted order.
Example 2:
Input:
N = 11
M = 4
A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8}
A2[] = {99, 22, 444, 56}
Output:
1 1 2 2 3 5 6 7 8 8 9
Explanation: No A1[] elements are in A2[]
so we cannot sort A1[] according to A2[].
Hence we sort the elements in non-decreasing
order.
Your Task:
You don't need to read input or print anything. Your task is to complete the function sortA1ByA2() which takes the array A1[ ], array A2[ ] and their respective size N and M as input parameters and returns the sorted array A1[ ] such that the relative positions of the elements in A1[ ] are same as the elements in A2[ ]. For the elements not present in A2[ ] but in A1[ ], it appends them at the last in increasing order.
Expected Time Complexity: O(N * Log(N)).
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N, M β€ 10^{6}
1 β€ A1[i], A2[i] β€ 10^{6} | class Solution:
def partition(self, arr, l, h):
pivot = arr[h]
j = l
for i in range(l, h):
if arr[i] < pivot:
arr[i], arr[j] = arr[j], arr[i]
j += 1
arr[j], arr[h] = arr[h], arr[j]
return j
def quicksort(self, arr, l, h):
if l < h:
pi = self.partition(arr, l, h)
self.quicksort(arr, l, pi - 1)
self.quicksort(arr, pi + 1, h)
def relativeSort(self, A1, N, A2, M):
freq = {}
for i in A1:
freq[i] = freq.get(i, 0) + 1
res = []
for i in A2:
if i in freq:
res += [i] * freq[i]
freq.pop(i)
for i in sorted(freq.keys()):
res += [i] * freq[i]
return res | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR VAR BIN_OP LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP LIST VAR VAR VAR RETURN VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.