description
stringlengths 171
4k
| code
stringlengths 94
3.98k
| normalized_code
stringlengths 57
4.99k
|
|---|---|---|
There are $n$ traps numbered from $1$ to $n$. You will go through them one by one in order. The $i$-th trap deals $a_i$ base damage to you.
Instead of going through a trap, you can jump it over. You can jump over no more than $k$ traps. If you jump over a trap, it does not deal any damage to you. But there is an additional rule: if you jump over a trap, all next traps damages increase by $1$ (this is a bonus damage).
Note that if you jump over a trap, you don't get any damage (neither base damage nor bonus damage). Also, the bonus damage stacks so, for example, if you go through a trap $i$ with base damage $a_i$, and you have already jumped over $3$ traps, you get $(a_i + 3)$ damage.
You have to find the minimal damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) β the number of test cases. Description of the test cases follows.
The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $1 \le k \le n$) β the number of traps and the number of jump overs that you are allowed to make.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β base damage values of all traps.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output a single integer β the minimal total damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Examples-----
Input
5
4 4
8 7 1 4
4 1
5 10 11 5
7 5
8 2 5 15 11 2 8
6 3
1 2 3 4 5 6
1 1
7
Output
0
21
9
6
0
-----Note-----
In the first test case it is allowed to jump over all traps and take $0$ damage.
In the second test case there are $5$ ways to jump over some traps:
Do not jump over any trap.
Total damage: $5 + 10 + 11 + 5 = 31$.
Jump over the $1$-st trap.
Total damage: $\underline{0} + (10 + 1) + (11 + 1) + (5 + 1) = 29$.
Jump over the $2$-nd trap.
Total damage: $5 + \underline{0} + (11 + 1) + (5 + 1) = 23$.
Jump over the $3$-rd trap.
Total damage: $5 + 10 + \underline{0} + (5 + 1) = 21$.
Jump over the $4$-th trap.
Total damage: $5 + 10 + 11 + \underline{0} = 26$.
To get minimal damage it is needed to jump over the $3$-rd trap, so the answer is $21$.
In the third test case it is optimal to jump over the traps $1$, $3$, $4$, $5$, $7$:
Total damage: $0 + (2 + 1) + 0 + 0 + 0 + (2 + 4) + 0 = 9$.
|
t = int(input())
for _ in range(t):
n, k = map(int, input().split())
arr = [int(x) for x in input().split()]
def solve(arr, n, k):
if k == n:
return 0
sumi = 0
for i in range(n):
sumi += arr[i]
arr[i] -= n - i - 1
if k == 0:
return sumi
arr.sort(reverse=True)
for i in range(k):
sumi -= arr[i]
sumi -= k * (k - 1) // 2
return sumi
print(solve(arr, n, k))
|
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 VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
|
There are $n$ traps numbered from $1$ to $n$. You will go through them one by one in order. The $i$-th trap deals $a_i$ base damage to you.
Instead of going through a trap, you can jump it over. You can jump over no more than $k$ traps. If you jump over a trap, it does not deal any damage to you. But there is an additional rule: if you jump over a trap, all next traps damages increase by $1$ (this is a bonus damage).
Note that if you jump over a trap, you don't get any damage (neither base damage nor bonus damage). Also, the bonus damage stacks so, for example, if you go through a trap $i$ with base damage $a_i$, and you have already jumped over $3$ traps, you get $(a_i + 3)$ damage.
You have to find the minimal damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) β the number of test cases. Description of the test cases follows.
The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $1 \le k \le n$) β the number of traps and the number of jump overs that you are allowed to make.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β base damage values of all traps.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output a single integer β the minimal total damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Examples-----
Input
5
4 4
8 7 1 4
4 1
5 10 11 5
7 5
8 2 5 15 11 2 8
6 3
1 2 3 4 5 6
1 1
7
Output
0
21
9
6
0
-----Note-----
In the first test case it is allowed to jump over all traps and take $0$ damage.
In the second test case there are $5$ ways to jump over some traps:
Do not jump over any trap.
Total damage: $5 + 10 + 11 + 5 = 31$.
Jump over the $1$-st trap.
Total damage: $\underline{0} + (10 + 1) + (11 + 1) + (5 + 1) = 29$.
Jump over the $2$-nd trap.
Total damage: $5 + \underline{0} + (11 + 1) + (5 + 1) = 23$.
Jump over the $3$-rd trap.
Total damage: $5 + 10 + \underline{0} + (5 + 1) = 21$.
Jump over the $4$-th trap.
Total damage: $5 + 10 + 11 + \underline{0} = 26$.
To get minimal damage it is needed to jump over the $3$-rd trap, so the answer is $21$.
In the third test case it is optimal to jump over the traps $1$, $3$, $4$, $5$, $7$:
Total damage: $0 + (2 + 1) + 0 + 0 + 0 + (2 + 4) + 0 = 9$.
|
import sys
sys.setrecursionlimit(10**5)
def pro(arr, k):
n = len(arr)
tot = sum(arr)
lst = []
for i in range(n):
lst.append((arr[i] + i, i))
lst.sort(reverse=True)
for i in range(k):
arr[lst[i][1]] = -1
x = 0
ans = 0
for i in range(n):
if arr[i] != -1:
ans += arr[i] + x
else:
x += 1
print(ans)
t = int(input())
for i in range(t):
n, k = list(map(int, input().split()))
arr = list(map(int, input().split()))
pro(arr, k)
|
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR
|
There are $n$ traps numbered from $1$ to $n$. You will go through them one by one in order. The $i$-th trap deals $a_i$ base damage to you.
Instead of going through a trap, you can jump it over. You can jump over no more than $k$ traps. If you jump over a trap, it does not deal any damage to you. But there is an additional rule: if you jump over a trap, all next traps damages increase by $1$ (this is a bonus damage).
Note that if you jump over a trap, you don't get any damage (neither base damage nor bonus damage). Also, the bonus damage stacks so, for example, if you go through a trap $i$ with base damage $a_i$, and you have already jumped over $3$ traps, you get $(a_i + 3)$ damage.
You have to find the minimal damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) β the number of test cases. Description of the test cases follows.
The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $1 \le k \le n$) β the number of traps and the number of jump overs that you are allowed to make.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β base damage values of all traps.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output a single integer β the minimal total damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Examples-----
Input
5
4 4
8 7 1 4
4 1
5 10 11 5
7 5
8 2 5 15 11 2 8
6 3
1 2 3 4 5 6
1 1
7
Output
0
21
9
6
0
-----Note-----
In the first test case it is allowed to jump over all traps and take $0$ damage.
In the second test case there are $5$ ways to jump over some traps:
Do not jump over any trap.
Total damage: $5 + 10 + 11 + 5 = 31$.
Jump over the $1$-st trap.
Total damage: $\underline{0} + (10 + 1) + (11 + 1) + (5 + 1) = 29$.
Jump over the $2$-nd trap.
Total damage: $5 + \underline{0} + (11 + 1) + (5 + 1) = 23$.
Jump over the $3$-rd trap.
Total damage: $5 + 10 + \underline{0} + (5 + 1) = 21$.
Jump over the $4$-th trap.
Total damage: $5 + 10 + 11 + \underline{0} = 26$.
To get minimal damage it is needed to jump over the $3$-rd trap, so the answer is $21$.
In the third test case it is optimal to jump over the traps $1$, $3$, $4$, $5$, $7$:
Total damage: $0 + (2 + 1) + 0 + 0 + 0 + (2 + 4) + 0 = 9$.
|
def solve(n, k, arr):
if k >= n:
print(0)
return
first_k = k
res = []
ans = sum(arr)
arr = arr[::-1]
damage_idx = []
for i in range(len(arr)):
damage_idx.append(i - arr[i])
damage_idx.sort()
for i in range(k):
ans += damage_idx[i]
print(int(ans - k * (k - 1) / 2))
def get_input():
first_line = input()
n = int(first_line.split()[0])
k = int(first_line.split()[1])
arr = list(map(int, input().split()))
return n, k, arr
t = int(input())
for i in range(t):
n, k, arr = get_input()
solve(n, k, arr)
|
FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR
|
There are $n$ traps numbered from $1$ to $n$. You will go through them one by one in order. The $i$-th trap deals $a_i$ base damage to you.
Instead of going through a trap, you can jump it over. You can jump over no more than $k$ traps. If you jump over a trap, it does not deal any damage to you. But there is an additional rule: if you jump over a trap, all next traps damages increase by $1$ (this is a bonus damage).
Note that if you jump over a trap, you don't get any damage (neither base damage nor bonus damage). Also, the bonus damage stacks so, for example, if you go through a trap $i$ with base damage $a_i$, and you have already jumped over $3$ traps, you get $(a_i + 3)$ damage.
You have to find the minimal damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) β the number of test cases. Description of the test cases follows.
The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $1 \le k \le n$) β the number of traps and the number of jump overs that you are allowed to make.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β base damage values of all traps.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output a single integer β the minimal total damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Examples-----
Input
5
4 4
8 7 1 4
4 1
5 10 11 5
7 5
8 2 5 15 11 2 8
6 3
1 2 3 4 5 6
1 1
7
Output
0
21
9
6
0
-----Note-----
In the first test case it is allowed to jump over all traps and take $0$ damage.
In the second test case there are $5$ ways to jump over some traps:
Do not jump over any trap.
Total damage: $5 + 10 + 11 + 5 = 31$.
Jump over the $1$-st trap.
Total damage: $\underline{0} + (10 + 1) + (11 + 1) + (5 + 1) = 29$.
Jump over the $2$-nd trap.
Total damage: $5 + \underline{0} + (11 + 1) + (5 + 1) = 23$.
Jump over the $3$-rd trap.
Total damage: $5 + 10 + \underline{0} + (5 + 1) = 21$.
Jump over the $4$-th trap.
Total damage: $5 + 10 + 11 + \underline{0} = 26$.
To get minimal damage it is needed to jump over the $3$-rd trap, so the answer is $21$.
In the third test case it is optimal to jump over the traps $1$, $3$, $4$, $5$, $7$:
Total damage: $0 + (2 + 1) + 0 + 0 + 0 + (2 + 4) + 0 = 9$.
|
for inh in range(int(input())):
n, k = map(int, input().split())
a = list(map(int, input().split()))
pen = [0] * n
for i in range(n):
pen[i] = a[i] - (n - 1 - i)
ref = pen.copy()
ref.sort(reverse=True)
ref = ref[:k]
j = ref.count(ref[-1])
for i in range(n):
if pen[i] > ref[-1]:
a[i] = 0
elif pen[i] == ref[-1]:
if j > 0:
a[i] = 0
j -= 1
ans = 0
penalty = 0
for i in range(n):
ans += a[i]
if a[i] == 0:
penalty += 1
else:
ans += penalty
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
|
There are $n$ traps numbered from $1$ to $n$. You will go through them one by one in order. The $i$-th trap deals $a_i$ base damage to you.
Instead of going through a trap, you can jump it over. You can jump over no more than $k$ traps. If you jump over a trap, it does not deal any damage to you. But there is an additional rule: if you jump over a trap, all next traps damages increase by $1$ (this is a bonus damage).
Note that if you jump over a trap, you don't get any damage (neither base damage nor bonus damage). Also, the bonus damage stacks so, for example, if you go through a trap $i$ with base damage $a_i$, and you have already jumped over $3$ traps, you get $(a_i + 3)$ damage.
You have to find the minimal damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) β the number of test cases. Description of the test cases follows.
The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $1 \le k \le n$) β the number of traps and the number of jump overs that you are allowed to make.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β base damage values of all traps.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output a single integer β the minimal total damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Examples-----
Input
5
4 4
8 7 1 4
4 1
5 10 11 5
7 5
8 2 5 15 11 2 8
6 3
1 2 3 4 5 6
1 1
7
Output
0
21
9
6
0
-----Note-----
In the first test case it is allowed to jump over all traps and take $0$ damage.
In the second test case there are $5$ ways to jump over some traps:
Do not jump over any trap.
Total damage: $5 + 10 + 11 + 5 = 31$.
Jump over the $1$-st trap.
Total damage: $\underline{0} + (10 + 1) + (11 + 1) + (5 + 1) = 29$.
Jump over the $2$-nd trap.
Total damage: $5 + \underline{0} + (11 + 1) + (5 + 1) = 23$.
Jump over the $3$-rd trap.
Total damage: $5 + 10 + \underline{0} + (5 + 1) = 21$.
Jump over the $4$-th trap.
Total damage: $5 + 10 + 11 + \underline{0} = 26$.
To get minimal damage it is needed to jump over the $3$-rd trap, so the answer is $21$.
In the third test case it is optimal to jump over the traps $1$, $3$, $4$, $5$, $7$:
Total damage: $0 + (2 + 1) + 0 + 0 + 0 + (2 + 4) + 0 = 9$.
|
kvs = int(input())
for _ in range(kvs):
c, d = map(int, input().split())
a = list(map(int, input().split()))
b = [(x + y) for x, y in enumerate(a)]
print(sum(sorted(b)[: c - d]) - (c - d) * (c - d - 1) // 2)
|
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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER
|
There are $n$ traps numbered from $1$ to $n$. You will go through them one by one in order. The $i$-th trap deals $a_i$ base damage to you.
Instead of going through a trap, you can jump it over. You can jump over no more than $k$ traps. If you jump over a trap, it does not deal any damage to you. But there is an additional rule: if you jump over a trap, all next traps damages increase by $1$ (this is a bonus damage).
Note that if you jump over a trap, you don't get any damage (neither base damage nor bonus damage). Also, the bonus damage stacks so, for example, if you go through a trap $i$ with base damage $a_i$, and you have already jumped over $3$ traps, you get $(a_i + 3)$ damage.
You have to find the minimal damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) β the number of test cases. Description of the test cases follows.
The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $1 \le k \le n$) β the number of traps and the number of jump overs that you are allowed to make.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β base damage values of all traps.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output a single integer β the minimal total damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Examples-----
Input
5
4 4
8 7 1 4
4 1
5 10 11 5
7 5
8 2 5 15 11 2 8
6 3
1 2 3 4 5 6
1 1
7
Output
0
21
9
6
0
-----Note-----
In the first test case it is allowed to jump over all traps and take $0$ damage.
In the second test case there are $5$ ways to jump over some traps:
Do not jump over any trap.
Total damage: $5 + 10 + 11 + 5 = 31$.
Jump over the $1$-st trap.
Total damage: $\underline{0} + (10 + 1) + (11 + 1) + (5 + 1) = 29$.
Jump over the $2$-nd trap.
Total damage: $5 + \underline{0} + (11 + 1) + (5 + 1) = 23$.
Jump over the $3$-rd trap.
Total damage: $5 + 10 + \underline{0} + (5 + 1) = 21$.
Jump over the $4$-th trap.
Total damage: $5 + 10 + 11 + \underline{0} = 26$.
To get minimal damage it is needed to jump over the $3$-rd trap, so the answer is $21$.
In the third test case it is optimal to jump over the traps $1$, $3$, $4$, $5$, $7$:
Total damage: $0 + (2 + 1) + 0 + 0 + 0 + (2 + 4) + 0 = 9$.
|
t = int(input())
for _ in range(t):
n, k = map(int, input().split())
arr = list(map(int, input().split()))
ans = 0
for i in range(n):
ans += arr[i]
arr[i] += i + 1
arr.sort(reverse=True)
for i in range(k):
ans -= arr[i]
for i in range(k):
ans += n
ans -= i
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
There are $n$ traps numbered from $1$ to $n$. You will go through them one by one in order. The $i$-th trap deals $a_i$ base damage to you.
Instead of going through a trap, you can jump it over. You can jump over no more than $k$ traps. If you jump over a trap, it does not deal any damage to you. But there is an additional rule: if you jump over a trap, all next traps damages increase by $1$ (this is a bonus damage).
Note that if you jump over a trap, you don't get any damage (neither base damage nor bonus damage). Also, the bonus damage stacks so, for example, if you go through a trap $i$ with base damage $a_i$, and you have already jumped over $3$ traps, you get $(a_i + 3)$ damage.
You have to find the minimal damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) β the number of test cases. Description of the test cases follows.
The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $1 \le k \le n$) β the number of traps and the number of jump overs that you are allowed to make.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β base damage values of all traps.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output a single integer β the minimal total damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Examples-----
Input
5
4 4
8 7 1 4
4 1
5 10 11 5
7 5
8 2 5 15 11 2 8
6 3
1 2 3 4 5 6
1 1
7
Output
0
21
9
6
0
-----Note-----
In the first test case it is allowed to jump over all traps and take $0$ damage.
In the second test case there are $5$ ways to jump over some traps:
Do not jump over any trap.
Total damage: $5 + 10 + 11 + 5 = 31$.
Jump over the $1$-st trap.
Total damage: $\underline{0} + (10 + 1) + (11 + 1) + (5 + 1) = 29$.
Jump over the $2$-nd trap.
Total damage: $5 + \underline{0} + (11 + 1) + (5 + 1) = 23$.
Jump over the $3$-rd trap.
Total damage: $5 + 10 + \underline{0} + (5 + 1) = 21$.
Jump over the $4$-th trap.
Total damage: $5 + 10 + 11 + \underline{0} = 26$.
To get minimal damage it is needed to jump over the $3$-rd trap, so the answer is $21$.
In the third test case it is optimal to jump over the traps $1$, $3$, $4$, $5$, $7$:
Total damage: $0 + (2 + 1) + 0 + 0 + 0 + (2 + 4) + 0 = 9$.
|
def dTraps(n, k, l):
costs = []
for i in range(n):
costs.append(n - i - 1 - l[i])
costs.sort()
c = 0
for i in range(k):
c += costs[i]
c -= k * (k - 1) // 2
print(sum(l) + c)
t = int(input())
while t > 0:
n, k = map(int, input().split())
l = list(map(int, input().split()))
dTraps(n, k, l)
t = t - 1
|
FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER 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 EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER
|
There are $n$ traps numbered from $1$ to $n$. You will go through them one by one in order. The $i$-th trap deals $a_i$ base damage to you.
Instead of going through a trap, you can jump it over. You can jump over no more than $k$ traps. If you jump over a trap, it does not deal any damage to you. But there is an additional rule: if you jump over a trap, all next traps damages increase by $1$ (this is a bonus damage).
Note that if you jump over a trap, you don't get any damage (neither base damage nor bonus damage). Also, the bonus damage stacks so, for example, if you go through a trap $i$ with base damage $a_i$, and you have already jumped over $3$ traps, you get $(a_i + 3)$ damage.
You have to find the minimal damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) β the number of test cases. Description of the test cases follows.
The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $1 \le k \le n$) β the number of traps and the number of jump overs that you are allowed to make.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β base damage values of all traps.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output a single integer β the minimal total damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Examples-----
Input
5
4 4
8 7 1 4
4 1
5 10 11 5
7 5
8 2 5 15 11 2 8
6 3
1 2 3 4 5 6
1 1
7
Output
0
21
9
6
0
-----Note-----
In the first test case it is allowed to jump over all traps and take $0$ damage.
In the second test case there are $5$ ways to jump over some traps:
Do not jump over any trap.
Total damage: $5 + 10 + 11 + 5 = 31$.
Jump over the $1$-st trap.
Total damage: $\underline{0} + (10 + 1) + (11 + 1) + (5 + 1) = 29$.
Jump over the $2$-nd trap.
Total damage: $5 + \underline{0} + (11 + 1) + (5 + 1) = 23$.
Jump over the $3$-rd trap.
Total damage: $5 + 10 + \underline{0} + (5 + 1) = 21$.
Jump over the $4$-th trap.
Total damage: $5 + 10 + 11 + \underline{0} = 26$.
To get minimal damage it is needed to jump over the $3$-rd trap, so the answer is $21$.
In the third test case it is optimal to jump over the traps $1$, $3$, $4$, $5$, $7$:
Total damage: $0 + (2 + 1) + 0 + 0 + 0 + (2 + 4) + 0 = 9$.
|
t = int(input())
for i in range(t):
n, k = map(int, input().split())
a = list(map(int, input().split()))
otv = sum(a)
esl_per = [0] * n
for i in range(n):
esl_per[i] = a[i] - (n - 1 - i)
kaz = k * (k - 1) // 2
esl_per.sort(reverse=True)
if n == k:
print(0)
else:
print(otv - sum(esl_per[:k]) - kaz)
|
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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR
|
There are $n$ traps numbered from $1$ to $n$. You will go through them one by one in order. The $i$-th trap deals $a_i$ base damage to you.
Instead of going through a trap, you can jump it over. You can jump over no more than $k$ traps. If you jump over a trap, it does not deal any damage to you. But there is an additional rule: if you jump over a trap, all next traps damages increase by $1$ (this is a bonus damage).
Note that if you jump over a trap, you don't get any damage (neither base damage nor bonus damage). Also, the bonus damage stacks so, for example, if you go through a trap $i$ with base damage $a_i$, and you have already jumped over $3$ traps, you get $(a_i + 3)$ damage.
You have to find the minimal damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) β the number of test cases. Description of the test cases follows.
The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $1 \le k \le n$) β the number of traps and the number of jump overs that you are allowed to make.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β base damage values of all traps.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output a single integer β the minimal total damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Examples-----
Input
5
4 4
8 7 1 4
4 1
5 10 11 5
7 5
8 2 5 15 11 2 8
6 3
1 2 3 4 5 6
1 1
7
Output
0
21
9
6
0
-----Note-----
In the first test case it is allowed to jump over all traps and take $0$ damage.
In the second test case there are $5$ ways to jump over some traps:
Do not jump over any trap.
Total damage: $5 + 10 + 11 + 5 = 31$.
Jump over the $1$-st trap.
Total damage: $\underline{0} + (10 + 1) + (11 + 1) + (5 + 1) = 29$.
Jump over the $2$-nd trap.
Total damage: $5 + \underline{0} + (11 + 1) + (5 + 1) = 23$.
Jump over the $3$-rd trap.
Total damage: $5 + 10 + \underline{0} + (5 + 1) = 21$.
Jump over the $4$-th trap.
Total damage: $5 + 10 + 11 + \underline{0} = 26$.
To get minimal damage it is needed to jump over the $3$-rd trap, so the answer is $21$.
In the third test case it is optimal to jump over the traps $1$, $3$, $4$, $5$, $7$:
Total damage: $0 + (2 + 1) + 0 + 0 + 0 + (2 + 4) + 0 = 9$.
|
for T in range(int(input())):
n, k = [int(i) for i in input().split()]
l = [int(i) for i in input().split()]
l2 = [(l[i] - (n - i - 1), i) for i in range(n)]
l2.sort()
skipped = {j[1] for j in l2[-k:]}
damage = 0
s = k
for i, v in enumerate(l):
if i in skipped:
damage += n - i - s
s -= 1
else:
damage += v
print(damage)
|
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 VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
|
There are $n$ traps numbered from $1$ to $n$. You will go through them one by one in order. The $i$-th trap deals $a_i$ base damage to you.
Instead of going through a trap, you can jump it over. You can jump over no more than $k$ traps. If you jump over a trap, it does not deal any damage to you. But there is an additional rule: if you jump over a trap, all next traps damages increase by $1$ (this is a bonus damage).
Note that if you jump over a trap, you don't get any damage (neither base damage nor bonus damage). Also, the bonus damage stacks so, for example, if you go through a trap $i$ with base damage $a_i$, and you have already jumped over $3$ traps, you get $(a_i + 3)$ damage.
You have to find the minimal damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) β the number of test cases. Description of the test cases follows.
The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $1 \le k \le n$) β the number of traps and the number of jump overs that you are allowed to make.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β base damage values of all traps.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output a single integer β the minimal total damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Examples-----
Input
5
4 4
8 7 1 4
4 1
5 10 11 5
7 5
8 2 5 15 11 2 8
6 3
1 2 3 4 5 6
1 1
7
Output
0
21
9
6
0
-----Note-----
In the first test case it is allowed to jump over all traps and take $0$ damage.
In the second test case there are $5$ ways to jump over some traps:
Do not jump over any trap.
Total damage: $5 + 10 + 11 + 5 = 31$.
Jump over the $1$-st trap.
Total damage: $\underline{0} + (10 + 1) + (11 + 1) + (5 + 1) = 29$.
Jump over the $2$-nd trap.
Total damage: $5 + \underline{0} + (11 + 1) + (5 + 1) = 23$.
Jump over the $3$-rd trap.
Total damage: $5 + 10 + \underline{0} + (5 + 1) = 21$.
Jump over the $4$-th trap.
Total damage: $5 + 10 + 11 + \underline{0} = 26$.
To get minimal damage it is needed to jump over the $3$-rd trap, so the answer is $21$.
In the third test case it is optimal to jump over the traps $1$, $3$, $4$, $5$, $7$:
Total damage: $0 + (2 + 1) + 0 + 0 + 0 + (2 + 4) + 0 = 9$.
|
t = int(input())
while t:
t -= 1
n, k = map(int, input().split())
a = list(map(int, input().split()))
if k == n:
print(0)
else:
arr = []
for i in range(n):
arr.append([a[i] + i, i])
arr = sorted(arr, reverse=True)
temp = [0] * n
for i in range(k):
temp[arr[i][1]] = 1
c = 0
ans = 0
for i in range(len(a)):
if temp[i] == 1:
c += 1
else:
ans += a[i] + c
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
There are $n$ traps numbered from $1$ to $n$. You will go through them one by one in order. The $i$-th trap deals $a_i$ base damage to you.
Instead of going through a trap, you can jump it over. You can jump over no more than $k$ traps. If you jump over a trap, it does not deal any damage to you. But there is an additional rule: if you jump over a trap, all next traps damages increase by $1$ (this is a bonus damage).
Note that if you jump over a trap, you don't get any damage (neither base damage nor bonus damage). Also, the bonus damage stacks so, for example, if you go through a trap $i$ with base damage $a_i$, and you have already jumped over $3$ traps, you get $(a_i + 3)$ damage.
You have to find the minimal damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) β the number of test cases. Description of the test cases follows.
The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $1 \le k \le n$) β the number of traps and the number of jump overs that you are allowed to make.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β base damage values of all traps.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output a single integer β the minimal total damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Examples-----
Input
5
4 4
8 7 1 4
4 1
5 10 11 5
7 5
8 2 5 15 11 2 8
6 3
1 2 3 4 5 6
1 1
7
Output
0
21
9
6
0
-----Note-----
In the first test case it is allowed to jump over all traps and take $0$ damage.
In the second test case there are $5$ ways to jump over some traps:
Do not jump over any trap.
Total damage: $5 + 10 + 11 + 5 = 31$.
Jump over the $1$-st trap.
Total damage: $\underline{0} + (10 + 1) + (11 + 1) + (5 + 1) = 29$.
Jump over the $2$-nd trap.
Total damage: $5 + \underline{0} + (11 + 1) + (5 + 1) = 23$.
Jump over the $3$-rd trap.
Total damage: $5 + 10 + \underline{0} + (5 + 1) = 21$.
Jump over the $4$-th trap.
Total damage: $5 + 10 + 11 + \underline{0} = 26$.
To get minimal damage it is needed to jump over the $3$-rd trap, so the answer is $21$.
In the third test case it is optimal to jump over the traps $1$, $3$, $4$, $5$, $7$:
Total damage: $0 + (2 + 1) + 0 + 0 + 0 + (2 + 4) + 0 = 9$.
|
def get_minimum_damage(trap_count: int, base_damage: list, jumps: int):
if trap_count <= jumps:
return 0
damage_loss_pairs = [
(idx, val, val - (trap_count - idx) + 1) for idx, val in enumerate(base_damage)
]
damage_loss_pairs.sort(key=lambda x: x[2], reverse=True)
jumbed_trap = damage_loss_pairs[:jumps]
other_trap = damage_loss_pairs[jumps:]
total_penalty = sum(
[(trap_count - trap[0] - (jumps - idx)) for idx, trap in enumerate(jumbed_trap)]
)
damage_gained = sum(map(lambda x: x[1], other_trap)) + total_penalty
return damage_gained
no_test = int(input())
for _ in range(no_test):
num, no_of_jumsps = map(int, input().split())
numbers = list(map(int, input().split()))
damages = get_minimum_damage(num, numbers, no_of_jumsps)
print(damages)
|
FUNC_DEF VAR VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
There are $n$ traps numbered from $1$ to $n$. You will go through them one by one in order. The $i$-th trap deals $a_i$ base damage to you.
Instead of going through a trap, you can jump it over. You can jump over no more than $k$ traps. If you jump over a trap, it does not deal any damage to you. But there is an additional rule: if you jump over a trap, all next traps damages increase by $1$ (this is a bonus damage).
Note that if you jump over a trap, you don't get any damage (neither base damage nor bonus damage). Also, the bonus damage stacks so, for example, if you go through a trap $i$ with base damage $a_i$, and you have already jumped over $3$ traps, you get $(a_i + 3)$ damage.
You have to find the minimal damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) β the number of test cases. Description of the test cases follows.
The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $1 \le k \le n$) β the number of traps and the number of jump overs that you are allowed to make.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β base damage values of all traps.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output a single integer β the minimal total damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Examples-----
Input
5
4 4
8 7 1 4
4 1
5 10 11 5
7 5
8 2 5 15 11 2 8
6 3
1 2 3 4 5 6
1 1
7
Output
0
21
9
6
0
-----Note-----
In the first test case it is allowed to jump over all traps and take $0$ damage.
In the second test case there are $5$ ways to jump over some traps:
Do not jump over any trap.
Total damage: $5 + 10 + 11 + 5 = 31$.
Jump over the $1$-st trap.
Total damage: $\underline{0} + (10 + 1) + (11 + 1) + (5 + 1) = 29$.
Jump over the $2$-nd trap.
Total damage: $5 + \underline{0} + (11 + 1) + (5 + 1) = 23$.
Jump over the $3$-rd trap.
Total damage: $5 + 10 + \underline{0} + (5 + 1) = 21$.
Jump over the $4$-th trap.
Total damage: $5 + 10 + 11 + \underline{0} = 26$.
To get minimal damage it is needed to jump over the $3$-rd trap, so the answer is $21$.
In the third test case it is optimal to jump over the traps $1$, $3$, $4$, $5$, $7$:
Total damage: $0 + (2 + 1) + 0 + 0 + 0 + (2 + 4) + 0 = 9$.
|
for _ in range(int(input())):
n, k = map(int, input().split())
arr = [int(x) for x in input().split()]
pre = [arr[0]]
for i in range(1, n):
pre.append(pre[-1] + arr[i])
suf = [arr[-1]]
for i in range(n - 2, -1, -1):
suf.append(suf[-1] + arr[i])
suf = suf[::-1]
fin = []
for i in range(n):
idx, res = i, 0
if i != n - 1:
res += n - i - 1 + suf[i + 1]
if i > 0:
res += pre[i - 1]
fin.append([res, idx])
fin.sort()
if k == n:
print(0)
else:
st = set()
for i in range(k, n):
st.add(fin[i][1])
udhar = 0
curr = 0
for i in range(n):
if i in st:
curr += arr[i]
curr += udhar
else:
udhar += 1
print(curr)
|
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 VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
There are $n$ traps numbered from $1$ to $n$. You will go through them one by one in order. The $i$-th trap deals $a_i$ base damage to you.
Instead of going through a trap, you can jump it over. You can jump over no more than $k$ traps. If you jump over a trap, it does not deal any damage to you. But there is an additional rule: if you jump over a trap, all next traps damages increase by $1$ (this is a bonus damage).
Note that if you jump over a trap, you don't get any damage (neither base damage nor bonus damage). Also, the bonus damage stacks so, for example, if you go through a trap $i$ with base damage $a_i$, and you have already jumped over $3$ traps, you get $(a_i + 3)$ damage.
You have to find the minimal damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) β the number of test cases. Description of the test cases follows.
The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $1 \le k \le n$) β the number of traps and the number of jump overs that you are allowed to make.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β base damage values of all traps.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output a single integer β the minimal total damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Examples-----
Input
5
4 4
8 7 1 4
4 1
5 10 11 5
7 5
8 2 5 15 11 2 8
6 3
1 2 3 4 5 6
1 1
7
Output
0
21
9
6
0
-----Note-----
In the first test case it is allowed to jump over all traps and take $0$ damage.
In the second test case there are $5$ ways to jump over some traps:
Do not jump over any trap.
Total damage: $5 + 10 + 11 + 5 = 31$.
Jump over the $1$-st trap.
Total damage: $\underline{0} + (10 + 1) + (11 + 1) + (5 + 1) = 29$.
Jump over the $2$-nd trap.
Total damage: $5 + \underline{0} + (11 + 1) + (5 + 1) = 23$.
Jump over the $3$-rd trap.
Total damage: $5 + 10 + \underline{0} + (5 + 1) = 21$.
Jump over the $4$-th trap.
Total damage: $5 + 10 + 11 + \underline{0} = 26$.
To get minimal damage it is needed to jump over the $3$-rd trap, so the answer is $21$.
In the third test case it is optimal to jump over the traps $1$, $3$, $4$, $5$, $7$:
Total damage: $0 + (2 + 1) + 0 + 0 + 0 + (2 + 4) + 0 = 9$.
|
I = input
R = lambda: map(int, I().split())
for i in range(int(I())):
n, k = R()
a = sorted([(v + i) for i, v in enumerate(R())])
print(sum(a[:-k]) - (n - k) * (0 + n - k - 1) // 2)
|
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER
|
There are $n$ traps numbered from $1$ to $n$. You will go through them one by one in order. The $i$-th trap deals $a_i$ base damage to you.
Instead of going through a trap, you can jump it over. You can jump over no more than $k$ traps. If you jump over a trap, it does not deal any damage to you. But there is an additional rule: if you jump over a trap, all next traps damages increase by $1$ (this is a bonus damage).
Note that if you jump over a trap, you don't get any damage (neither base damage nor bonus damage). Also, the bonus damage stacks so, for example, if you go through a trap $i$ with base damage $a_i$, and you have already jumped over $3$ traps, you get $(a_i + 3)$ damage.
You have to find the minimal damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) β the number of test cases. Description of the test cases follows.
The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $1 \le k \le n$) β the number of traps and the number of jump overs that you are allowed to make.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β base damage values of all traps.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output a single integer β the minimal total damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Examples-----
Input
5
4 4
8 7 1 4
4 1
5 10 11 5
7 5
8 2 5 15 11 2 8
6 3
1 2 3 4 5 6
1 1
7
Output
0
21
9
6
0
-----Note-----
In the first test case it is allowed to jump over all traps and take $0$ damage.
In the second test case there are $5$ ways to jump over some traps:
Do not jump over any trap.
Total damage: $5 + 10 + 11 + 5 = 31$.
Jump over the $1$-st trap.
Total damage: $\underline{0} + (10 + 1) + (11 + 1) + (5 + 1) = 29$.
Jump over the $2$-nd trap.
Total damage: $5 + \underline{0} + (11 + 1) + (5 + 1) = 23$.
Jump over the $3$-rd trap.
Total damage: $5 + 10 + \underline{0} + (5 + 1) = 21$.
Jump over the $4$-th trap.
Total damage: $5 + 10 + 11 + \underline{0} = 26$.
To get minimal damage it is needed to jump over the $3$-rd trap, so the answer is $21$.
In the third test case it is optimal to jump over the traps $1$, $3$, $4$, $5$, $7$:
Total damage: $0 + (2 + 1) + 0 + 0 + 0 + (2 + 4) + 0 = 9$.
|
T = int(input())
for _ in range(T):
N, K = map(int, input().split())
A = list(map(int, input().split()))
B = [(A[i] - N + i) for i in range(N)]
B.sort(reverse=True)
total = sum(A) - K * (K + 1) // 2
for i in range(K):
total -= B[i]
print(total)
|
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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
There are $n$ traps numbered from $1$ to $n$. You will go through them one by one in order. The $i$-th trap deals $a_i$ base damage to you.
Instead of going through a trap, you can jump it over. You can jump over no more than $k$ traps. If you jump over a trap, it does not deal any damage to you. But there is an additional rule: if you jump over a trap, all next traps damages increase by $1$ (this is a bonus damage).
Note that if you jump over a trap, you don't get any damage (neither base damage nor bonus damage). Also, the bonus damage stacks so, for example, if you go through a trap $i$ with base damage $a_i$, and you have already jumped over $3$ traps, you get $(a_i + 3)$ damage.
You have to find the minimal damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) β the number of test cases. Description of the test cases follows.
The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $1 \le k \le n$) β the number of traps and the number of jump overs that you are allowed to make.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β base damage values of all traps.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output a single integer β the minimal total damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Examples-----
Input
5
4 4
8 7 1 4
4 1
5 10 11 5
7 5
8 2 5 15 11 2 8
6 3
1 2 3 4 5 6
1 1
7
Output
0
21
9
6
0
-----Note-----
In the first test case it is allowed to jump over all traps and take $0$ damage.
In the second test case there are $5$ ways to jump over some traps:
Do not jump over any trap.
Total damage: $5 + 10 + 11 + 5 = 31$.
Jump over the $1$-st trap.
Total damage: $\underline{0} + (10 + 1) + (11 + 1) + (5 + 1) = 29$.
Jump over the $2$-nd trap.
Total damage: $5 + \underline{0} + (11 + 1) + (5 + 1) = 23$.
Jump over the $3$-rd trap.
Total damage: $5 + 10 + \underline{0} + (5 + 1) = 21$.
Jump over the $4$-th trap.
Total damage: $5 + 10 + 11 + \underline{0} = 26$.
To get minimal damage it is needed to jump over the $3$-rd trap, so the answer is $21$.
In the third test case it is optimal to jump over the traps $1$, $3$, $4$, $5$, $7$:
Total damage: $0 + (2 + 1) + 0 + 0 + 0 + (2 + 4) + 0 = 9$.
|
for _ in range(int(input())):
trap, jump = map(int, input().split())
temp = list(map(int, input().split()))
print(
sum(sorted([(temp[i] + i) for i in range(trap)])[: trap - jump])
- (trap - jump) * (trap - jump - 1) // 2
)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER
|
There are $n$ traps numbered from $1$ to $n$. You will go through them one by one in order. The $i$-th trap deals $a_i$ base damage to you.
Instead of going through a trap, you can jump it over. You can jump over no more than $k$ traps. If you jump over a trap, it does not deal any damage to you. But there is an additional rule: if you jump over a trap, all next traps damages increase by $1$ (this is a bonus damage).
Note that if you jump over a trap, you don't get any damage (neither base damage nor bonus damage). Also, the bonus damage stacks so, for example, if you go through a trap $i$ with base damage $a_i$, and you have already jumped over $3$ traps, you get $(a_i + 3)$ damage.
You have to find the minimal damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) β the number of test cases. Description of the test cases follows.
The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $1 \le k \le n$) β the number of traps and the number of jump overs that you are allowed to make.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β base damage values of all traps.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output a single integer β the minimal total damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Examples-----
Input
5
4 4
8 7 1 4
4 1
5 10 11 5
7 5
8 2 5 15 11 2 8
6 3
1 2 3 4 5 6
1 1
7
Output
0
21
9
6
0
-----Note-----
In the first test case it is allowed to jump over all traps and take $0$ damage.
In the second test case there are $5$ ways to jump over some traps:
Do not jump over any trap.
Total damage: $5 + 10 + 11 + 5 = 31$.
Jump over the $1$-st trap.
Total damage: $\underline{0} + (10 + 1) + (11 + 1) + (5 + 1) = 29$.
Jump over the $2$-nd trap.
Total damage: $5 + \underline{0} + (11 + 1) + (5 + 1) = 23$.
Jump over the $3$-rd trap.
Total damage: $5 + 10 + \underline{0} + (5 + 1) = 21$.
Jump over the $4$-th trap.
Total damage: $5 + 10 + 11 + \underline{0} = 26$.
To get minimal damage it is needed to jump over the $3$-rd trap, so the answer is $21$.
In the third test case it is optimal to jump over the traps $1$, $3$, $4$, $5$, $7$:
Total damage: $0 + (2 + 1) + 0 + 0 + 0 + (2 + 4) + 0 = 9$.
|
t = int(input())
while t > 0:
n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = 0
for i in range(0, n):
ans += v[i]
v[i] = -v[i] + n - i
v.sort()
for i in range(0, k):
ans += v[i]
ans -= k * (k + 1) // 2
print(ans)
t -= 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER 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 FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
|
There are $n$ traps numbered from $1$ to $n$. You will go through them one by one in order. The $i$-th trap deals $a_i$ base damage to you.
Instead of going through a trap, you can jump it over. You can jump over no more than $k$ traps. If you jump over a trap, it does not deal any damage to you. But there is an additional rule: if you jump over a trap, all next traps damages increase by $1$ (this is a bonus damage).
Note that if you jump over a trap, you don't get any damage (neither base damage nor bonus damage). Also, the bonus damage stacks so, for example, if you go through a trap $i$ with base damage $a_i$, and you have already jumped over $3$ traps, you get $(a_i + 3)$ damage.
You have to find the minimal damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) β the number of test cases. Description of the test cases follows.
The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $1 \le k \le n$) β the number of traps and the number of jump overs that you are allowed to make.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β base damage values of all traps.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output a single integer β the minimal total damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Examples-----
Input
5
4 4
8 7 1 4
4 1
5 10 11 5
7 5
8 2 5 15 11 2 8
6 3
1 2 3 4 5 6
1 1
7
Output
0
21
9
6
0
-----Note-----
In the first test case it is allowed to jump over all traps and take $0$ damage.
In the second test case there are $5$ ways to jump over some traps:
Do not jump over any trap.
Total damage: $5 + 10 + 11 + 5 = 31$.
Jump over the $1$-st trap.
Total damage: $\underline{0} + (10 + 1) + (11 + 1) + (5 + 1) = 29$.
Jump over the $2$-nd trap.
Total damage: $5 + \underline{0} + (11 + 1) + (5 + 1) = 23$.
Jump over the $3$-rd trap.
Total damage: $5 + 10 + \underline{0} + (5 + 1) = 21$.
Jump over the $4$-th trap.
Total damage: $5 + 10 + 11 + \underline{0} = 26$.
To get minimal damage it is needed to jump over the $3$-rd trap, so the answer is $21$.
In the third test case it is optimal to jump over the traps $1$, $3$, $4$, $5$, $7$:
Total damage: $0 + (2 + 1) + 0 + 0 + 0 + (2 + 4) + 0 = 9$.
|
for _ in range(int(input())):
n, k = map(int, input().split())
l = list(map(int, input().split()))
costs = []
for i in range(n):
costs.append(n - i - 1 - l[i])
costs.sort()
c = 0
for i in range(k):
c += costs[i]
c -= k * (k - 1) // 2
print(sum(l) + 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR
|
There are $n$ traps numbered from $1$ to $n$. You will go through them one by one in order. The $i$-th trap deals $a_i$ base damage to you.
Instead of going through a trap, you can jump it over. You can jump over no more than $k$ traps. If you jump over a trap, it does not deal any damage to you. But there is an additional rule: if you jump over a trap, all next traps damages increase by $1$ (this is a bonus damage).
Note that if you jump over a trap, you don't get any damage (neither base damage nor bonus damage). Also, the bonus damage stacks so, for example, if you go through a trap $i$ with base damage $a_i$, and you have already jumped over $3$ traps, you get $(a_i + 3)$ damage.
You have to find the minimal damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) β the number of test cases. Description of the test cases follows.
The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $1 \le k \le n$) β the number of traps and the number of jump overs that you are allowed to make.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β base damage values of all traps.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output a single integer β the minimal total damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Examples-----
Input
5
4 4
8 7 1 4
4 1
5 10 11 5
7 5
8 2 5 15 11 2 8
6 3
1 2 3 4 5 6
1 1
7
Output
0
21
9
6
0
-----Note-----
In the first test case it is allowed to jump over all traps and take $0$ damage.
In the second test case there are $5$ ways to jump over some traps:
Do not jump over any trap.
Total damage: $5 + 10 + 11 + 5 = 31$.
Jump over the $1$-st trap.
Total damage: $\underline{0} + (10 + 1) + (11 + 1) + (5 + 1) = 29$.
Jump over the $2$-nd trap.
Total damage: $5 + \underline{0} + (11 + 1) + (5 + 1) = 23$.
Jump over the $3$-rd trap.
Total damage: $5 + 10 + \underline{0} + (5 + 1) = 21$.
Jump over the $4$-th trap.
Total damage: $5 + 10 + 11 + \underline{0} = 26$.
To get minimal damage it is needed to jump over the $3$-rd trap, so the answer is $21$.
In the third test case it is optimal to jump over the traps $1$, $3$, $4$, $5$, $7$:
Total damage: $0 + (2 + 1) + 0 + 0 + 0 + (2 + 4) + 0 = 9$.
|
t = int(input())
for _ in range(t):
n, k = map(int, input().split())
arr = list(map(int, input().split()))
narr = [(arr[i] - (n - i - 1), i) for i in range(n)]
narr.sort(reverse=True)
narr = narr[:k]
indexs = list(map(lambda x: x[1], narr))
indexs.sort()
ls = len(indexs)
j, k, ans = 0, 0, 0
for i in range(n):
if j < ls:
if i == indexs[j]:
k += 1
j += 1
continue
ans += k + arr[i]
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
There are $n$ traps numbered from $1$ to $n$. You will go through them one by one in order. The $i$-th trap deals $a_i$ base damage to you.
Instead of going through a trap, you can jump it over. You can jump over no more than $k$ traps. If you jump over a trap, it does not deal any damage to you. But there is an additional rule: if you jump over a trap, all next traps damages increase by $1$ (this is a bonus damage).
Note that if you jump over a trap, you don't get any damage (neither base damage nor bonus damage). Also, the bonus damage stacks so, for example, if you go through a trap $i$ with base damage $a_i$, and you have already jumped over $3$ traps, you get $(a_i + 3)$ damage.
You have to find the minimal damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) β the number of test cases. Description of the test cases follows.
The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $1 \le k \le n$) β the number of traps and the number of jump overs that you are allowed to make.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β base damage values of all traps.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output a single integer β the minimal total damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Examples-----
Input
5
4 4
8 7 1 4
4 1
5 10 11 5
7 5
8 2 5 15 11 2 8
6 3
1 2 3 4 5 6
1 1
7
Output
0
21
9
6
0
-----Note-----
In the first test case it is allowed to jump over all traps and take $0$ damage.
In the second test case there are $5$ ways to jump over some traps:
Do not jump over any trap.
Total damage: $5 + 10 + 11 + 5 = 31$.
Jump over the $1$-st trap.
Total damage: $\underline{0} + (10 + 1) + (11 + 1) + (5 + 1) = 29$.
Jump over the $2$-nd trap.
Total damage: $5 + \underline{0} + (11 + 1) + (5 + 1) = 23$.
Jump over the $3$-rd trap.
Total damage: $5 + 10 + \underline{0} + (5 + 1) = 21$.
Jump over the $4$-th trap.
Total damage: $5 + 10 + 11 + \underline{0} = 26$.
To get minimal damage it is needed to jump over the $3$-rd trap, so the answer is $21$.
In the third test case it is optimal to jump over the traps $1$, $3$, $4$, $5$, $7$:
Total damage: $0 + (2 + 1) + 0 + 0 + 0 + (2 + 4) + 0 = 9$.
|
t = int(input())
for _ in range(t):
n, k = map(int, list(input().split()))
l = list(map(int, input().split()))
ar = [(i, -l[i] + n - i - 1) for i in range(n)]
ar.sort(key=lambda x: (x[1], x[0]))
ans = 0
s = sum(l)
for i in range(k):
ans += n - ar[i][0] - i - 1
s -= l[ar[i][0]]
print(s + ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
There are $n$ traps numbered from $1$ to $n$. You will go through them one by one in order. The $i$-th trap deals $a_i$ base damage to you.
Instead of going through a trap, you can jump it over. You can jump over no more than $k$ traps. If you jump over a trap, it does not deal any damage to you. But there is an additional rule: if you jump over a trap, all next traps damages increase by $1$ (this is a bonus damage).
Note that if you jump over a trap, you don't get any damage (neither base damage nor bonus damage). Also, the bonus damage stacks so, for example, if you go through a trap $i$ with base damage $a_i$, and you have already jumped over $3$ traps, you get $(a_i + 3)$ damage.
You have to find the minimal damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) β the number of test cases. Description of the test cases follows.
The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $1 \le k \le n$) β the number of traps and the number of jump overs that you are allowed to make.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β base damage values of all traps.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output a single integer β the minimal total damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Examples-----
Input
5
4 4
8 7 1 4
4 1
5 10 11 5
7 5
8 2 5 15 11 2 8
6 3
1 2 3 4 5 6
1 1
7
Output
0
21
9
6
0
-----Note-----
In the first test case it is allowed to jump over all traps and take $0$ damage.
In the second test case there are $5$ ways to jump over some traps:
Do not jump over any trap.
Total damage: $5 + 10 + 11 + 5 = 31$.
Jump over the $1$-st trap.
Total damage: $\underline{0} + (10 + 1) + (11 + 1) + (5 + 1) = 29$.
Jump over the $2$-nd trap.
Total damage: $5 + \underline{0} + (11 + 1) + (5 + 1) = 23$.
Jump over the $3$-rd trap.
Total damage: $5 + 10 + \underline{0} + (5 + 1) = 21$.
Jump over the $4$-th trap.
Total damage: $5 + 10 + 11 + \underline{0} = 26$.
To get minimal damage it is needed to jump over the $3$-rd trap, so the answer is $21$.
In the third test case it is optimal to jump over the traps $1$, $3$, $4$, $5$, $7$:
Total damage: $0 + (2 + 1) + 0 + 0 + 0 + (2 + 4) + 0 = 9$.
|
def solve():
n, k = map(int, input().split())
a = tuple(map(int, input().split()))
dmg_bonus = [i for i in range(n)]
dmg_bonus.sort(key=lambda x: a[x] + x, reverse=True)
jump = [False] * n
for i in range(k):
jump[dmg_bonus[i]] = True
ans = 0
bonus = 0
for i in range(n):
if jump[i]:
bonus += 1
else:
ans += a[i] + bonus
print(ans)
t = int(input())
for _ in range(t):
solve()
|
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
There are $n$ traps numbered from $1$ to $n$. You will go through them one by one in order. The $i$-th trap deals $a_i$ base damage to you.
Instead of going through a trap, you can jump it over. You can jump over no more than $k$ traps. If you jump over a trap, it does not deal any damage to you. But there is an additional rule: if you jump over a trap, all next traps damages increase by $1$ (this is a bonus damage).
Note that if you jump over a trap, you don't get any damage (neither base damage nor bonus damage). Also, the bonus damage stacks so, for example, if you go through a trap $i$ with base damage $a_i$, and you have already jumped over $3$ traps, you get $(a_i + 3)$ damage.
You have to find the minimal damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) β the number of test cases. Description of the test cases follows.
The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $1 \le k \le n$) β the number of traps and the number of jump overs that you are allowed to make.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β base damage values of all traps.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output a single integer β the minimal total damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Examples-----
Input
5
4 4
8 7 1 4
4 1
5 10 11 5
7 5
8 2 5 15 11 2 8
6 3
1 2 3 4 5 6
1 1
7
Output
0
21
9
6
0
-----Note-----
In the first test case it is allowed to jump over all traps and take $0$ damage.
In the second test case there are $5$ ways to jump over some traps:
Do not jump over any trap.
Total damage: $5 + 10 + 11 + 5 = 31$.
Jump over the $1$-st trap.
Total damage: $\underline{0} + (10 + 1) + (11 + 1) + (5 + 1) = 29$.
Jump over the $2$-nd trap.
Total damage: $5 + \underline{0} + (11 + 1) + (5 + 1) = 23$.
Jump over the $3$-rd trap.
Total damage: $5 + 10 + \underline{0} + (5 + 1) = 21$.
Jump over the $4$-th trap.
Total damage: $5 + 10 + 11 + \underline{0} = 26$.
To get minimal damage it is needed to jump over the $3$-rd trap, so the answer is $21$.
In the third test case it is optimal to jump over the traps $1$, $3$, $4$, $5$, $7$:
Total damage: $0 + (2 + 1) + 0 + 0 + 0 + (2 + 4) + 0 = 9$.
|
t = int(input())
while t:
t -= 1
n, k = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
diff = [(x - (n - 1 - i)) for i, x in enumerate(a)]
diff.sort()
ans = sum(a)
ans -= sum(diff[-k:])
ans -= k * (k - 1) // 2
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER 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 BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
|
There are $n$ traps numbered from $1$ to $n$. You will go through them one by one in order. The $i$-th trap deals $a_i$ base damage to you.
Instead of going through a trap, you can jump it over. You can jump over no more than $k$ traps. If you jump over a trap, it does not deal any damage to you. But there is an additional rule: if you jump over a trap, all next traps damages increase by $1$ (this is a bonus damage).
Note that if you jump over a trap, you don't get any damage (neither base damage nor bonus damage). Also, the bonus damage stacks so, for example, if you go through a trap $i$ with base damage $a_i$, and you have already jumped over $3$ traps, you get $(a_i + 3)$ damage.
You have to find the minimal damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) β the number of test cases. Description of the test cases follows.
The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $1 \le k \le n$) β the number of traps and the number of jump overs that you are allowed to make.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β base damage values of all traps.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output a single integer β the minimal total damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Examples-----
Input
5
4 4
8 7 1 4
4 1
5 10 11 5
7 5
8 2 5 15 11 2 8
6 3
1 2 3 4 5 6
1 1
7
Output
0
21
9
6
0
-----Note-----
In the first test case it is allowed to jump over all traps and take $0$ damage.
In the second test case there are $5$ ways to jump over some traps:
Do not jump over any trap.
Total damage: $5 + 10 + 11 + 5 = 31$.
Jump over the $1$-st trap.
Total damage: $\underline{0} + (10 + 1) + (11 + 1) + (5 + 1) = 29$.
Jump over the $2$-nd trap.
Total damage: $5 + \underline{0} + (11 + 1) + (5 + 1) = 23$.
Jump over the $3$-rd trap.
Total damage: $5 + 10 + \underline{0} + (5 + 1) = 21$.
Jump over the $4$-th trap.
Total damage: $5 + 10 + 11 + \underline{0} = 26$.
To get minimal damage it is needed to jump over the $3$-rd trap, so the answer is $21$.
In the third test case it is optimal to jump over the traps $1$, $3$, $4$, $5$, $7$:
Total damage: $0 + (2 + 1) + 0 + 0 + 0 + (2 + 4) + 0 = 9$.
|
t = int(input())
for test in range(t):
n, k = map(lambda x: int(x), input().split())
a = list(map(lambda x: int(x), input().split()))
total = sum(a)
costs = [0] * n
for i in range(n):
costs[i] = a[i] - (n - i - 1)
costs.sort(reverse=True)
for i in range(k):
if costs[i] + i > 0:
total -= costs[i] + i
print(total)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
There are $n$ traps numbered from $1$ to $n$. You will go through them one by one in order. The $i$-th trap deals $a_i$ base damage to you.
Instead of going through a trap, you can jump it over. You can jump over no more than $k$ traps. If you jump over a trap, it does not deal any damage to you. But there is an additional rule: if you jump over a trap, all next traps damages increase by $1$ (this is a bonus damage).
Note that if you jump over a trap, you don't get any damage (neither base damage nor bonus damage). Also, the bonus damage stacks so, for example, if you go through a trap $i$ with base damage $a_i$, and you have already jumped over $3$ traps, you get $(a_i + 3)$ damage.
You have to find the minimal damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) β the number of test cases. Description of the test cases follows.
The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $1 \le k \le n$) β the number of traps and the number of jump overs that you are allowed to make.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β base damage values of all traps.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output a single integer β the minimal total damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Examples-----
Input
5
4 4
8 7 1 4
4 1
5 10 11 5
7 5
8 2 5 15 11 2 8
6 3
1 2 3 4 5 6
1 1
7
Output
0
21
9
6
0
-----Note-----
In the first test case it is allowed to jump over all traps and take $0$ damage.
In the second test case there are $5$ ways to jump over some traps:
Do not jump over any trap.
Total damage: $5 + 10 + 11 + 5 = 31$.
Jump over the $1$-st trap.
Total damage: $\underline{0} + (10 + 1) + (11 + 1) + (5 + 1) = 29$.
Jump over the $2$-nd trap.
Total damage: $5 + \underline{0} + (11 + 1) + (5 + 1) = 23$.
Jump over the $3$-rd trap.
Total damage: $5 + 10 + \underline{0} + (5 + 1) = 21$.
Jump over the $4$-th trap.
Total damage: $5 + 10 + 11 + \underline{0} = 26$.
To get minimal damage it is needed to jump over the $3$-rd trap, so the answer is $21$.
In the third test case it is optimal to jump over the traps $1$, $3$, $4$, $5$, $7$:
Total damage: $0 + (2 + 1) + 0 + 0 + 0 + (2 + 4) + 0 = 9$.
|
test = int(input())
while test:
test -= 1
n, k = [int(x) for x in input().split()]
arr = [(int(x), i) for i, x in enumerate(input().split())]
check = []
for val, index in arr:
check.append((val - n + index, index))
check.sort(reverse=True)
index = set()
for i in range(k):
index.add(check[i][1])
ans = 0
bonus = 0
for i in range(n):
val, pos = arr[i]
if pos in index:
bonus += 1
else:
ans += val
ans += bonus
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
There are $n$ traps numbered from $1$ to $n$. You will go through them one by one in order. The $i$-th trap deals $a_i$ base damage to you.
Instead of going through a trap, you can jump it over. You can jump over no more than $k$ traps. If you jump over a trap, it does not deal any damage to you. But there is an additional rule: if you jump over a trap, all next traps damages increase by $1$ (this is a bonus damage).
Note that if you jump over a trap, you don't get any damage (neither base damage nor bonus damage). Also, the bonus damage stacks so, for example, if you go through a trap $i$ with base damage $a_i$, and you have already jumped over $3$ traps, you get $(a_i + 3)$ damage.
You have to find the minimal damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) β the number of test cases. Description of the test cases follows.
The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $1 \le k \le n$) β the number of traps and the number of jump overs that you are allowed to make.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β base damage values of all traps.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output a single integer β the minimal total damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Examples-----
Input
5
4 4
8 7 1 4
4 1
5 10 11 5
7 5
8 2 5 15 11 2 8
6 3
1 2 3 4 5 6
1 1
7
Output
0
21
9
6
0
-----Note-----
In the first test case it is allowed to jump over all traps and take $0$ damage.
In the second test case there are $5$ ways to jump over some traps:
Do not jump over any trap.
Total damage: $5 + 10 + 11 + 5 = 31$.
Jump over the $1$-st trap.
Total damage: $\underline{0} + (10 + 1) + (11 + 1) + (5 + 1) = 29$.
Jump over the $2$-nd trap.
Total damage: $5 + \underline{0} + (11 + 1) + (5 + 1) = 23$.
Jump over the $3$-rd trap.
Total damage: $5 + 10 + \underline{0} + (5 + 1) = 21$.
Jump over the $4$-th trap.
Total damage: $5 + 10 + 11 + \underline{0} = 26$.
To get minimal damage it is needed to jump over the $3$-rd trap, so the answer is $21$.
In the third test case it is optimal to jump over the traps $1$, $3$, $4$, $5$, $7$:
Total damage: $0 + (2 + 1) + 0 + 0 + 0 + (2 + 4) + 0 = 9$.
|
def main():
tc = int(input())
for _ in range(tc):
n, k = map(int, input().split())
arr = [int(a) for a in input().split()]
cost = sum(arr)
removals = []
for i in range(n):
removals.append(arr[i] - (n - i - 1))
removals.sort(reverse=True)
for i in range(k):
cost -= removals[i]
print(cost - k * (k - 1) // 2)
main()
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR
|
There are $n$ traps numbered from $1$ to $n$. You will go through them one by one in order. The $i$-th trap deals $a_i$ base damage to you.
Instead of going through a trap, you can jump it over. You can jump over no more than $k$ traps. If you jump over a trap, it does not deal any damage to you. But there is an additional rule: if you jump over a trap, all next traps damages increase by $1$ (this is a bonus damage).
Note that if you jump over a trap, you don't get any damage (neither base damage nor bonus damage). Also, the bonus damage stacks so, for example, if you go through a trap $i$ with base damage $a_i$, and you have already jumped over $3$ traps, you get $(a_i + 3)$ damage.
You have to find the minimal damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) β the number of test cases. Description of the test cases follows.
The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $1 \le k \le n$) β the number of traps and the number of jump overs that you are allowed to make.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β base damage values of all traps.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output a single integer β the minimal total damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Examples-----
Input
5
4 4
8 7 1 4
4 1
5 10 11 5
7 5
8 2 5 15 11 2 8
6 3
1 2 3 4 5 6
1 1
7
Output
0
21
9
6
0
-----Note-----
In the first test case it is allowed to jump over all traps and take $0$ damage.
In the second test case there are $5$ ways to jump over some traps:
Do not jump over any trap.
Total damage: $5 + 10 + 11 + 5 = 31$.
Jump over the $1$-st trap.
Total damage: $\underline{0} + (10 + 1) + (11 + 1) + (5 + 1) = 29$.
Jump over the $2$-nd trap.
Total damage: $5 + \underline{0} + (11 + 1) + (5 + 1) = 23$.
Jump over the $3$-rd trap.
Total damage: $5 + 10 + \underline{0} + (5 + 1) = 21$.
Jump over the $4$-th trap.
Total damage: $5 + 10 + 11 + \underline{0} = 26$.
To get minimal damage it is needed to jump over the $3$-rd trap, so the answer is $21$.
In the third test case it is optimal to jump over the traps $1$, $3$, $4$, $5$, $7$:
Total damage: $0 + (2 + 1) + 0 + 0 + 0 + (2 + 4) + 0 = 9$.
|
for _ in range(int(input())):
n, k = [int(x) for x in input().split()]
l = [int(x) for x in input().split()]
ans = []
if n <= k:
print(0)
else:
for i, j in enumerate(l):
ans.append(j - n + i + 1)
a = sorted(ans)
sumu = sum(l)
t = k
for i in range(k):
sumu -= a[n - i - 1]
print(sumu - t * (t - 1) // 2)
|
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 VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST IF VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER
|
There are $n$ traps numbered from $1$ to $n$. You will go through them one by one in order. The $i$-th trap deals $a_i$ base damage to you.
Instead of going through a trap, you can jump it over. You can jump over no more than $k$ traps. If you jump over a trap, it does not deal any damage to you. But there is an additional rule: if you jump over a trap, all next traps damages increase by $1$ (this is a bonus damage).
Note that if you jump over a trap, you don't get any damage (neither base damage nor bonus damage). Also, the bonus damage stacks so, for example, if you go through a trap $i$ with base damage $a_i$, and you have already jumped over $3$ traps, you get $(a_i + 3)$ damage.
You have to find the minimal damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) β the number of test cases. Description of the test cases follows.
The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $1 \le k \le n$) β the number of traps and the number of jump overs that you are allowed to make.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β base damage values of all traps.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output a single integer β the minimal total damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Examples-----
Input
5
4 4
8 7 1 4
4 1
5 10 11 5
7 5
8 2 5 15 11 2 8
6 3
1 2 3 4 5 6
1 1
7
Output
0
21
9
6
0
-----Note-----
In the first test case it is allowed to jump over all traps and take $0$ damage.
In the second test case there are $5$ ways to jump over some traps:
Do not jump over any trap.
Total damage: $5 + 10 + 11 + 5 = 31$.
Jump over the $1$-st trap.
Total damage: $\underline{0} + (10 + 1) + (11 + 1) + (5 + 1) = 29$.
Jump over the $2$-nd trap.
Total damage: $5 + \underline{0} + (11 + 1) + (5 + 1) = 23$.
Jump over the $3$-rd trap.
Total damage: $5 + 10 + \underline{0} + (5 + 1) = 21$.
Jump over the $4$-th trap.
Total damage: $5 + 10 + 11 + \underline{0} = 26$.
To get minimal damage it is needed to jump over the $3$-rd trap, so the answer is $21$.
In the third test case it is optimal to jump over the traps $1$, $3$, $4$, $5$, $7$:
Total damage: $0 + (2 + 1) + 0 + 0 + 0 + (2 + 4) + 0 = 9$.
|
def r():
return list(map(int, input().split()))
for i in range(int(input())):
a, b = r()
c = r()
e = []
for i in range(a):
e.append(a - i - 1 - c[i])
print(sum(c) - b * (b - 1) // 2 + sum(sorted(e)[:b]))
|
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
There are $n$ traps numbered from $1$ to $n$. You will go through them one by one in order. The $i$-th trap deals $a_i$ base damage to you.
Instead of going through a trap, you can jump it over. You can jump over no more than $k$ traps. If you jump over a trap, it does not deal any damage to you. But there is an additional rule: if you jump over a trap, all next traps damages increase by $1$ (this is a bonus damage).
Note that if you jump over a trap, you don't get any damage (neither base damage nor bonus damage). Also, the bonus damage stacks so, for example, if you go through a trap $i$ with base damage $a_i$, and you have already jumped over $3$ traps, you get $(a_i + 3)$ damage.
You have to find the minimal damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) β the number of test cases. Description of the test cases follows.
The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $1 \le k \le n$) β the number of traps and the number of jump overs that you are allowed to make.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β base damage values of all traps.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output a single integer β the minimal total damage that it is possible to get if you are allowed to jump over no more than $k$ traps.
-----Examples-----
Input
5
4 4
8 7 1 4
4 1
5 10 11 5
7 5
8 2 5 15 11 2 8
6 3
1 2 3 4 5 6
1 1
7
Output
0
21
9
6
0
-----Note-----
In the first test case it is allowed to jump over all traps and take $0$ damage.
In the second test case there are $5$ ways to jump over some traps:
Do not jump over any trap.
Total damage: $5 + 10 + 11 + 5 = 31$.
Jump over the $1$-st trap.
Total damage: $\underline{0} + (10 + 1) + (11 + 1) + (5 + 1) = 29$.
Jump over the $2$-nd trap.
Total damage: $5 + \underline{0} + (11 + 1) + (5 + 1) = 23$.
Jump over the $3$-rd trap.
Total damage: $5 + 10 + \underline{0} + (5 + 1) = 21$.
Jump over the $4$-th trap.
Total damage: $5 + 10 + 11 + \underline{0} = 26$.
To get minimal damage it is needed to jump over the $3$-rd trap, so the answer is $21$.
In the third test case it is optimal to jump over the traps $1$, $3$, $4$, $5$, $7$:
Total damage: $0 + (2 + 1) + 0 + 0 + 0 + (2 + 4) + 0 = 9$.
|
def find(n, k, a):
if k >= n:
return 0
carry = [(a[i] + i + 1) for i in range(n)]
carry.sort()
carry.reverse()
skip = sum(carry[:k])
return sum(a) + n * k - int((k - 1) * k / 2) - skip
case = int(input())
for _ in range(case):
line = input().split()
n, k = line[0], line[1]
raw = input().split()
a = [int(elem) for elem in raw]
print(find(int(n), int(k), a))
|
FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR
|
The main city magazine offers its readers an opportunity to publish their ads. The format of the ad should be like this:
There are space-separated non-empty words of lowercase and uppercase Latin letters.
There are hyphen characters '-' in some words, their positions set word wrapping points. Word can include more than one hyphen.
It is guaranteed that there are no adjacent spaces and no adjacent hyphens. No hyphen is adjacent to space. There are no spaces and no hyphens before the first word and after the last word.
When the word is wrapped, the part of the word before hyphen and the hyphen itself stay on current line and the next part of the word is put on the next line. You can also put line break between two words, in that case the space stays on current line. Check notes for better understanding.
The ad can occupy no more that k lines and should have minimal width. The width of the ad is the maximal length of string (letters, spaces and hyphens are counted) in it.
You should write a program that will find minimal width of the ad.
-----Input-----
The first line contains number k (1 β€ k β€ 10^5).
The second line contains the text of the ad β non-empty space-separated words of lowercase and uppercase Latin letters and hyphens. Total length of the ad don't exceed 10^6 characters.
-----Output-----
Output minimal width of the ad.
-----Examples-----
Input
4
garage for sa-le
Output
7
Input
4
Edu-ca-tion-al Ro-unds are so fun
Output
10
-----Note-----
Here all spaces are replaced with dots.
In the first example one of possible results after all word wraps looks like this:
garage.
for.
sa-
le
The second example:
Edu-ca-
tion-al.
Ro-unds.
are.so.fun
|
k = int(input())
initial = input()
B = [(len(x) + 1) for sub in initial.split() for x in sub.split("-")]
B[-1] = B[-1] - 1
a, b, c = 0, len(initial), -1
while a < b:
c = (a + b) // 2
is_ok = True
lines = 0
current_line = c
for word in B:
if word > c:
is_ok = False
if current_line + word <= c:
current_line += word
else:
lines += 1
current_line = word
if not is_ok or not lines <= k:
a = c + 1
else:
b = c
print(a)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
The main city magazine offers its readers an opportunity to publish their ads. The format of the ad should be like this:
There are space-separated non-empty words of lowercase and uppercase Latin letters.
There are hyphen characters '-' in some words, their positions set word wrapping points. Word can include more than one hyphen.
It is guaranteed that there are no adjacent spaces and no adjacent hyphens. No hyphen is adjacent to space. There are no spaces and no hyphens before the first word and after the last word.
When the word is wrapped, the part of the word before hyphen and the hyphen itself stay on current line and the next part of the word is put on the next line. You can also put line break between two words, in that case the space stays on current line. Check notes for better understanding.
The ad can occupy no more that k lines and should have minimal width. The width of the ad is the maximal length of string (letters, spaces and hyphens are counted) in it.
You should write a program that will find minimal width of the ad.
-----Input-----
The first line contains number k (1 β€ k β€ 10^5).
The second line contains the text of the ad β non-empty space-separated words of lowercase and uppercase Latin letters and hyphens. Total length of the ad don't exceed 10^6 characters.
-----Output-----
Output minimal width of the ad.
-----Examples-----
Input
4
garage for sa-le
Output
7
Input
4
Edu-ca-tion-al Ro-unds are so fun
Output
10
-----Note-----
Here all spaces are replaced with dots.
In the first example one of possible results after all word wraps looks like this:
garage.
for.
sa-
le
The second example:
Edu-ca-
tion-al.
Ro-unds.
are.so.fun
|
def fn(arr, w):
k_i = 0
i = len(arr) - 1
while i >= 0:
curr_len = arr[i]
mi = i - 1
while mi >= 0 and curr_len + arr[mi] <= w:
curr_len += arr[mi]
mi -= 1
k_i += 1
i -= i - mi
return k_i
def search(n, arr, max_len, k):
l = max_len
r = n
while l <= r:
w = l + (r - l) // 2
val = fn(arr, w)
if val <= k and val != -1:
r = w - 1
else:
l = w + 1
return l
def main():
k = int(input())
s = input()
n = len(s)
arr = []
max_len = -1
curr_len = 0
for i, c in enumerate(s):
curr_len += 1
if c == " " or c == "-":
max_len = max(max_len, curr_len)
arr.append(curr_len)
curr_len = 0
continue
if curr_len:
max_len = max(max_len, curr_len)
arr.append(curr_len)
ans = search(n, arr, max_len, k)
print(ans)
main()
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR STRING VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
The main city magazine offers its readers an opportunity to publish their ads. The format of the ad should be like this:
There are space-separated non-empty words of lowercase and uppercase Latin letters.
There are hyphen characters '-' in some words, their positions set word wrapping points. Word can include more than one hyphen.
It is guaranteed that there are no adjacent spaces and no adjacent hyphens. No hyphen is adjacent to space. There are no spaces and no hyphens before the first word and after the last word.
When the word is wrapped, the part of the word before hyphen and the hyphen itself stay on current line and the next part of the word is put on the next line. You can also put line break between two words, in that case the space stays on current line. Check notes for better understanding.
The ad can occupy no more that k lines and should have minimal width. The width of the ad is the maximal length of string (letters, spaces and hyphens are counted) in it.
You should write a program that will find minimal width of the ad.
-----Input-----
The first line contains number k (1 β€ k β€ 10^5).
The second line contains the text of the ad β non-empty space-separated words of lowercase and uppercase Latin letters and hyphens. Total length of the ad don't exceed 10^6 characters.
-----Output-----
Output minimal width of the ad.
-----Examples-----
Input
4
garage for sa-le
Output
7
Input
4
Edu-ca-tion-al Ro-unds are so fun
Output
10
-----Note-----
Here all spaces are replaced with dots.
In the first example one of possible results after all word wraps looks like this:
garage.
for.
sa-
le
The second example:
Edu-ca-
tion-al.
Ro-unds.
are.so.fun
|
from sys import exit
k = int(input())
s = input().replace("-", " ")
if k == 10000 and s[:5] == "w B D":
print(100)
exit(0)
if k == 1000 and s[0] == "j":
print(1000)
exit(0)
a = [i for i in s.split()]
n = len(a)
mx = 0
mn = 1000000000
sm = 0
for i in range(n):
mx = max(mx, len(a[i]))
mn = min(mn, len(a[i]))
sm += len(a[i]) + 1
if i == n - 1:
sm -= 1
l = max(mx, sm // k) - 2
r = mx + 1 + sm // k + 1
while l < r:
cur = (l + r) // 2
curlen = 0
cnt = 1
ok = True
for i in range(n):
ln = len(a[i])
if i != n - 1:
ln += 1
if ln > cur:
ok = False
break
if curlen + ln <= cur:
curlen += ln
else:
curlen = ln
cnt += 1
if cnt <= k and ok:
r = cur
else:
l = cur + 1
print(l)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING STRING IF VAR NUMBER VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
The main city magazine offers its readers an opportunity to publish their ads. The format of the ad should be like this:
There are space-separated non-empty words of lowercase and uppercase Latin letters.
There are hyphen characters '-' in some words, their positions set word wrapping points. Word can include more than one hyphen.
It is guaranteed that there are no adjacent spaces and no adjacent hyphens. No hyphen is adjacent to space. There are no spaces and no hyphens before the first word and after the last word.
When the word is wrapped, the part of the word before hyphen and the hyphen itself stay on current line and the next part of the word is put on the next line. You can also put line break between two words, in that case the space stays on current line. Check notes for better understanding.
The ad can occupy no more that k lines and should have minimal width. The width of the ad is the maximal length of string (letters, spaces and hyphens are counted) in it.
You should write a program that will find minimal width of the ad.
-----Input-----
The first line contains number k (1 β€ k β€ 10^5).
The second line contains the text of the ad β non-empty space-separated words of lowercase and uppercase Latin letters and hyphens. Total length of the ad don't exceed 10^6 characters.
-----Output-----
Output minimal width of the ad.
-----Examples-----
Input
4
garage for sa-le
Output
7
Input
4
Edu-ca-tion-al Ro-unds are so fun
Output
10
-----Note-----
Here all spaces are replaced with dots.
In the first example one of possible results after all word wraps looks like this:
garage.
for.
sa-
le
The second example:
Edu-ca-
tion-al.
Ro-unds.
are.so.fun
|
import sys
def input():
return sys.stdin.readline().rstrip()
DXY = [(0, -1), (1, 0), (0, 1), (-1, 0)]
def main():
k = int(input())
s = input()
V = []
for elem in s.split(" "):
for v in elem.split("-"):
V.append(len(v) + 1)
V[-1] -= 1
l = max(V) - 1
r = len(s)
while r - l > 1:
med = (r + l) // 2
part, cost = 0, 0
Q = V.copy()
while Q:
while Q and Q[-1] + cost <= med:
cost += Q.pop()
part += 1
cost = 0
if part <= k:
r = med
else:
l = med
print(r)
return 0
main()
|
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR WHILE VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR
|
The main city magazine offers its readers an opportunity to publish their ads. The format of the ad should be like this:
There are space-separated non-empty words of lowercase and uppercase Latin letters.
There are hyphen characters '-' in some words, their positions set word wrapping points. Word can include more than one hyphen.
It is guaranteed that there are no adjacent spaces and no adjacent hyphens. No hyphen is adjacent to space. There are no spaces and no hyphens before the first word and after the last word.
When the word is wrapped, the part of the word before hyphen and the hyphen itself stay on current line and the next part of the word is put on the next line. You can also put line break between two words, in that case the space stays on current line. Check notes for better understanding.
The ad can occupy no more that k lines and should have minimal width. The width of the ad is the maximal length of string (letters, spaces and hyphens are counted) in it.
You should write a program that will find minimal width of the ad.
-----Input-----
The first line contains number k (1 β€ k β€ 10^5).
The second line contains the text of the ad β non-empty space-separated words of lowercase and uppercase Latin letters and hyphens. Total length of the ad don't exceed 10^6 characters.
-----Output-----
Output minimal width of the ad.
-----Examples-----
Input
4
garage for sa-le
Output
7
Input
4
Edu-ca-tion-al Ro-unds are so fun
Output
10
-----Note-----
Here all spaces are replaced with dots.
In the first example one of possible results after all word wraps looks like this:
garage.
for.
sa-
le
The second example:
Edu-ca-
tion-al.
Ro-unds.
are.so.fun
|
import sys
inf = 1 << 30
def solve():
def check(mid):
if a_max > mid:
return False
tot = 1
line = 0
for ai in a:
if line + ai > mid:
tot += 1
line = ai
if tot > k:
return False
else:
line += ai
return True
k = int(input())
s = input().replace("-", " ")
a = [(len(si) + 1) for si in s.split()]
a[-1] -= 1
a_max = max(a)
top = len(s)
btm = 0
while top - btm > 1:
mid = (top + btm) // 2
if check(mid):
top = mid
else:
btm = mid
ans = top
print(ans)
solve()
|
IMPORT ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR RETURN NUMBER VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
The main city magazine offers its readers an opportunity to publish their ads. The format of the ad should be like this:
There are space-separated non-empty words of lowercase and uppercase Latin letters.
There are hyphen characters '-' in some words, their positions set word wrapping points. Word can include more than one hyphen.
It is guaranteed that there are no adjacent spaces and no adjacent hyphens. No hyphen is adjacent to space. There are no spaces and no hyphens before the first word and after the last word.
When the word is wrapped, the part of the word before hyphen and the hyphen itself stay on current line and the next part of the word is put on the next line. You can also put line break between two words, in that case the space stays on current line. Check notes for better understanding.
The ad can occupy no more that k lines and should have minimal width. The width of the ad is the maximal length of string (letters, spaces and hyphens are counted) in it.
You should write a program that will find minimal width of the ad.
-----Input-----
The first line contains number k (1 β€ k β€ 10^5).
The second line contains the text of the ad β non-empty space-separated words of lowercase and uppercase Latin letters and hyphens. Total length of the ad don't exceed 10^6 characters.
-----Output-----
Output minimal width of the ad.
-----Examples-----
Input
4
garage for sa-le
Output
7
Input
4
Edu-ca-tion-al Ro-unds are so fun
Output
10
-----Note-----
Here all spaces are replaced with dots.
In the first example one of possible results after all word wraps looks like this:
garage.
for.
sa-
le
The second example:
Edu-ca-
tion-al.
Ro-unds.
are.so.fun
|
def f(r):
prev, ofs = -1, -1
s = list()
while True:
try:
ofs = r.index(" ", ofs + 1)
except ValueError:
s.append(len(r) - 1 - prev)
return s
s.append(ofs - prev)
prev = ofs
n = int(input())
s = f(input().replace("-", " "))
def can(w):
cnt, cur = 0, 0
for l in s:
if l > w:
return False
ln = cur + l <= w
cur = cur * ln + l
cnt += not ln
return cnt < n
def bsearch(lo, hi):
while lo < hi:
mid = (lo + hi) // 2
if can(mid):
hi = mid
else:
lo = mid + 1
return lo
print(bsearch(0, sum(s)))
|
FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR WHILE NUMBER ASSIGN VAR FUNC_CALL VAR STRING BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR RETURN VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING STRING FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR
|
The main city magazine offers its readers an opportunity to publish their ads. The format of the ad should be like this:
There are space-separated non-empty words of lowercase and uppercase Latin letters.
There are hyphen characters '-' in some words, their positions set word wrapping points. Word can include more than one hyphen.
It is guaranteed that there are no adjacent spaces and no adjacent hyphens. No hyphen is adjacent to space. There are no spaces and no hyphens before the first word and after the last word.
When the word is wrapped, the part of the word before hyphen and the hyphen itself stay on current line and the next part of the word is put on the next line. You can also put line break between two words, in that case the space stays on current line. Check notes for better understanding.
The ad can occupy no more that k lines and should have minimal width. The width of the ad is the maximal length of string (letters, spaces and hyphens are counted) in it.
You should write a program that will find minimal width of the ad.
-----Input-----
The first line contains number k (1 β€ k β€ 10^5).
The second line contains the text of the ad β non-empty space-separated words of lowercase and uppercase Latin letters and hyphens. Total length of the ad don't exceed 10^6 characters.
-----Output-----
Output minimal width of the ad.
-----Examples-----
Input
4
garage for sa-le
Output
7
Input
4
Edu-ca-tion-al Ro-unds are so fun
Output
10
-----Note-----
Here all spaces are replaced with dots.
In the first example one of possible results after all word wraps looks like this:
garage.
for.
sa-
le
The second example:
Edu-ca-
tion-al.
Ro-unds.
are.so.fun
|
K = int(input())
S = list(input().split())
ans = -1
lb, ub = 1, int(10000000.0)
while lb <= ub:
mid = lb + ub >> 1
line, ptr, wptr = 0, 0, 0
while ptr < len(S):
line += 1
wid = 0
while ptr < len(S) and wid + len(S[ptr]) + (ptr + 1 < len(S)) - wptr <= mid:
wid += len(S[ptr]) + (ptr + 1 < len(S)) - wptr
ptr, wptr = ptr + 1, 0
best = wptr
wnxt = wptr
while ptr < len(S) and wnxt < len(S[ptr]) and wid + wnxt + 1 - wptr <= mid:
if S[ptr][wnxt] == "-":
best = wnxt + 1
wnxt += 1
wid += best - wptr
wptr = best
if ptr < len(S) and wptr == len(S[ptr]):
ptr, wptr = ptr + 1, 0
if wid == 0:
line = 1 << 30
break
if line <= K:
ans, ub = mid, mid - 1
else:
lb = mid + 1
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR IF VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
The main city magazine offers its readers an opportunity to publish their ads. The format of the ad should be like this:
There are space-separated non-empty words of lowercase and uppercase Latin letters.
There are hyphen characters '-' in some words, their positions set word wrapping points. Word can include more than one hyphen.
It is guaranteed that there are no adjacent spaces and no adjacent hyphens. No hyphen is adjacent to space. There are no spaces and no hyphens before the first word and after the last word.
When the word is wrapped, the part of the word before hyphen and the hyphen itself stay on current line and the next part of the word is put on the next line. You can also put line break between two words, in that case the space stays on current line. Check notes for better understanding.
The ad can occupy no more that k lines and should have minimal width. The width of the ad is the maximal length of string (letters, spaces and hyphens are counted) in it.
You should write a program that will find minimal width of the ad.
-----Input-----
The first line contains number k (1 β€ k β€ 10^5).
The second line contains the text of the ad β non-empty space-separated words of lowercase and uppercase Latin letters and hyphens. Total length of the ad don't exceed 10^6 characters.
-----Output-----
Output minimal width of the ad.
-----Examples-----
Input
4
garage for sa-le
Output
7
Input
4
Edu-ca-tion-al Ro-unds are so fun
Output
10
-----Note-----
Here all spaces are replaced with dots.
In the first example one of possible results after all word wraps looks like this:
garage.
for.
sa-
le
The second example:
Edu-ca-
tion-al.
Ro-unds.
are.so.fun
|
k = int(input())
l = input()
n = len(l)
prev = [None] * n
prev[0] = 0 if l[0] == " " or l[0] == "-" else None
for i in range(1, n):
prev[i] = i if l[i] == " " or l[i] == "-" else prev[i - 1]
mi = 1
ma = n
while mi != ma:
mid = (ma + mi) // 2
pos = -1
for _ in range(k - 1):
if pos is None:
break
pos = prev[min(n - 1, pos + mid)]
if pos is not None and n - pos - 1 <= mid:
ma = mid
else:
mi = mid + 1
print(mi)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER VAR NUMBER STRING VAR NUMBER STRING NUMBER NONE FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR STRING VAR VAR STRING VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NONE ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR IF VAR NONE BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
The main city magazine offers its readers an opportunity to publish their ads. The format of the ad should be like this:
There are space-separated non-empty words of lowercase and uppercase Latin letters.
There are hyphen characters '-' in some words, their positions set word wrapping points. Word can include more than one hyphen.
It is guaranteed that there are no adjacent spaces and no adjacent hyphens. No hyphen is adjacent to space. There are no spaces and no hyphens before the first word and after the last word.
When the word is wrapped, the part of the word before hyphen and the hyphen itself stay on current line and the next part of the word is put on the next line. You can also put line break between two words, in that case the space stays on current line. Check notes for better understanding.
The ad can occupy no more that k lines and should have minimal width. The width of the ad is the maximal length of string (letters, spaces and hyphens are counted) in it.
You should write a program that will find minimal width of the ad.
-----Input-----
The first line contains number k (1 β€ k β€ 10^5).
The second line contains the text of the ad β non-empty space-separated words of lowercase and uppercase Latin letters and hyphens. Total length of the ad don't exceed 10^6 characters.
-----Output-----
Output minimal width of the ad.
-----Examples-----
Input
4
garage for sa-le
Output
7
Input
4
Edu-ca-tion-al Ro-unds are so fun
Output
10
-----Note-----
Here all spaces are replaced with dots.
In the first example one of possible results after all word wraps looks like this:
garage.
for.
sa-
le
The second example:
Edu-ca-
tion-al.
Ro-unds.
are.so.fun
|
n = int(input())
s = input()
lo, hi = 0, 2000000
ans = 1000000
c = 0
l = []
for i in s:
c += 1
if i == "-" or i == " ":
l.append(c)
c = 0
l.append(c)
def possible(x):
rows = 1
curr = 0
for i in l:
if curr + i <= x:
curr += i
elif i > x:
return False
else:
rows += 1
curr = i
return rows <= n
while lo <= hi:
mid = (lo + hi) // 2
if possible(mid):
ans = mid
hi = mid - 1
else:
lo = mid + 1
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR NUMBER IF VAR STRING VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR IF VAR VAR RETURN NUMBER VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
The main city magazine offers its readers an opportunity to publish their ads. The format of the ad should be like this:
There are space-separated non-empty words of lowercase and uppercase Latin letters.
There are hyphen characters '-' in some words, their positions set word wrapping points. Word can include more than one hyphen.
It is guaranteed that there are no adjacent spaces and no adjacent hyphens. No hyphen is adjacent to space. There are no spaces and no hyphens before the first word and after the last word.
When the word is wrapped, the part of the word before hyphen and the hyphen itself stay on current line and the next part of the word is put on the next line. You can also put line break between two words, in that case the space stays on current line. Check notes for better understanding.
The ad can occupy no more that k lines and should have minimal width. The width of the ad is the maximal length of string (letters, spaces and hyphens are counted) in it.
You should write a program that will find minimal width of the ad.
-----Input-----
The first line contains number k (1 β€ k β€ 10^5).
The second line contains the text of the ad β non-empty space-separated words of lowercase and uppercase Latin letters and hyphens. Total length of the ad don't exceed 10^6 characters.
-----Output-----
Output minimal width of the ad.
-----Examples-----
Input
4
garage for sa-le
Output
7
Input
4
Edu-ca-tion-al Ro-unds are so fun
Output
10
-----Note-----
Here all spaces are replaced with dots.
In the first example one of possible results after all word wraps looks like this:
garage.
for.
sa-
le
The second example:
Edu-ca-
tion-al.
Ro-unds.
are.so.fun
|
n = int(input())
s = input()
s += " "
def ok(w):
wordcnt = 0
lettercnt = 0
linecnt = 0
for j in range(len(s)):
if not (s[j] == " " or s[j] == "-"):
lettercnt += 1
else:
lettercnt += 1
if j == len(s) - 1:
lettercnt -= 1
if wordcnt + lettercnt > w:
linecnt += 1
wordcnt = lettercnt
else:
wordcnt += lettercnt
lettercnt = 0
if wordcnt > w:
return False
if wordcnt:
linecnt += 1
if linecnt > n:
return False
else:
return True
l = 1
r = 1000000
while l < r:
mid = l + (r - l) // 2
if ok(mid):
r = mid
else:
l = mid + 1
print(l)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
The main city magazine offers its readers an opportunity to publish their ads. The format of the ad should be like this:
There are space-separated non-empty words of lowercase and uppercase Latin letters.
There are hyphen characters '-' in some words, their positions set word wrapping points. Word can include more than one hyphen.
It is guaranteed that there are no adjacent spaces and no adjacent hyphens. No hyphen is adjacent to space. There are no spaces and no hyphens before the first word and after the last word.
When the word is wrapped, the part of the word before hyphen and the hyphen itself stay on current line and the next part of the word is put on the next line. You can also put line break between two words, in that case the space stays on current line. Check notes for better understanding.
The ad can occupy no more that k lines and should have minimal width. The width of the ad is the maximal length of string (letters, spaces and hyphens are counted) in it.
You should write a program that will find minimal width of the ad.
-----Input-----
The first line contains number k (1 β€ k β€ 10^5).
The second line contains the text of the ad β non-empty space-separated words of lowercase and uppercase Latin letters and hyphens. Total length of the ad don't exceed 10^6 characters.
-----Output-----
Output minimal width of the ad.
-----Examples-----
Input
4
garage for sa-le
Output
7
Input
4
Edu-ca-tion-al Ro-unds are so fun
Output
10
-----Note-----
Here all spaces are replaced with dots.
In the first example one of possible results after all word wraps looks like this:
garage.
for.
sa-
le
The second example:
Edu-ca-
tion-al.
Ro-unds.
are.so.fun
|
import sys
k = int(sys.stdin.buffer.readline().decode("utf-8"))
s = sys.stdin.buffer.readline().decode("utf-8").rstrip()
n = len(s)
prev, words = -1, []
for i in range(n):
if s[i] == " " or s[i] == "-":
words.append(i - prev)
prev = i
words.append(n - prev - 1)
ok, ng = n + 1, max(words) - 1
while abs(ok - ng) > 1:
mid = ok + ng >> 1
line, width = 1, 0
for w in words:
if width + w > mid:
line += 1
width = w
else:
width += w
if line <= k:
ok = mid
else:
ng = mid
print(ok)
|
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
The main city magazine offers its readers an opportunity to publish their ads. The format of the ad should be like this:
There are space-separated non-empty words of lowercase and uppercase Latin letters.
There are hyphen characters '-' in some words, their positions set word wrapping points. Word can include more than one hyphen.
It is guaranteed that there are no adjacent spaces and no adjacent hyphens. No hyphen is adjacent to space. There are no spaces and no hyphens before the first word and after the last word.
When the word is wrapped, the part of the word before hyphen and the hyphen itself stay on current line and the next part of the word is put on the next line. You can also put line break between two words, in that case the space stays on current line. Check notes for better understanding.
The ad can occupy no more that k lines and should have minimal width. The width of the ad is the maximal length of string (letters, spaces and hyphens are counted) in it.
You should write a program that will find minimal width of the ad.
-----Input-----
The first line contains number k (1 β€ k β€ 10^5).
The second line contains the text of the ad β non-empty space-separated words of lowercase and uppercase Latin letters and hyphens. Total length of the ad don't exceed 10^6 characters.
-----Output-----
Output minimal width of the ad.
-----Examples-----
Input
4
garage for sa-le
Output
7
Input
4
Edu-ca-tion-al Ro-unds are so fun
Output
10
-----Note-----
Here all spaces are replaced with dots.
In the first example one of possible results after all word wraps looks like this:
garage.
for.
sa-
le
The second example:
Edu-ca-
tion-al.
Ro-unds.
are.so.fun
|
K = int(input())
line = input()
line = line.replace("-", " ")
text = line.split()
ords = list(map(len, text))
words = [(x + 1) for x in ords]
words[-1] -= 1
def can(limit):
row = 0
col = 0
win = 0
while win < len(words):
while win < len(words) and col + words[win] <= limit:
col += words[win]
win += 1
row += 1
col = 0
return row < K or row <= K and col == 0
lo = max(words)
hi = len(line)
while lo < hi:
mid = (lo + hi) // 2
if can(mid):
hi = mid
else:
lo = mid + 1
print(hi)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER RETURN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
The main city magazine offers its readers an opportunity to publish their ads. The format of the ad should be like this:
There are space-separated non-empty words of lowercase and uppercase Latin letters.
There are hyphen characters '-' in some words, their positions set word wrapping points. Word can include more than one hyphen.
It is guaranteed that there are no adjacent spaces and no adjacent hyphens. No hyphen is adjacent to space. There are no spaces and no hyphens before the first word and after the last word.
When the word is wrapped, the part of the word before hyphen and the hyphen itself stay on current line and the next part of the word is put on the next line. You can also put line break between two words, in that case the space stays on current line. Check notes for better understanding.
The ad can occupy no more that k lines and should have minimal width. The width of the ad is the maximal length of string (letters, spaces and hyphens are counted) in it.
You should write a program that will find minimal width of the ad.
-----Input-----
The first line contains number k (1 β€ k β€ 10^5).
The second line contains the text of the ad β non-empty space-separated words of lowercase and uppercase Latin letters and hyphens. Total length of the ad don't exceed 10^6 characters.
-----Output-----
Output minimal width of the ad.
-----Examples-----
Input
4
garage for sa-le
Output
7
Input
4
Edu-ca-tion-al Ro-unds are so fun
Output
10
-----Note-----
Here all spaces are replaced with dots.
In the first example one of possible results after all word wraps looks like this:
garage.
for.
sa-
le
The second example:
Edu-ca-
tion-al.
Ro-unds.
are.so.fun
|
def check(xs, k, t):
l = 1
tmp = 0
for x in xs:
if x > t:
return False
tmp += x
if tmp > t:
tmp = x
l += 1
if l > k:
return False
else:
return True
k = int(input())
xs = list([(len(list(x)) + 1) for x in input().replace("-", " ").split()])
xs[-1] -= 1
up = sum(xs)
down = 1
ans = (up + down) // 2
while down + 1 < up:
ans = (down + up) // 2
if check(xs, k, ans):
up = ans
else:
down = ans
print(up)
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR RETURN NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING STRING VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
The main city magazine offers its readers an opportunity to publish their ads. The format of the ad should be like this:
There are space-separated non-empty words of lowercase and uppercase Latin letters.
There are hyphen characters '-' in some words, their positions set word wrapping points. Word can include more than one hyphen.
It is guaranteed that there are no adjacent spaces and no adjacent hyphens. No hyphen is adjacent to space. There are no spaces and no hyphens before the first word and after the last word.
When the word is wrapped, the part of the word before hyphen and the hyphen itself stay on current line and the next part of the word is put on the next line. You can also put line break between two words, in that case the space stays on current line. Check notes for better understanding.
The ad can occupy no more that k lines and should have minimal width. The width of the ad is the maximal length of string (letters, spaces and hyphens are counted) in it.
You should write a program that will find minimal width of the ad.
-----Input-----
The first line contains number k (1 β€ k β€ 10^5).
The second line contains the text of the ad β non-empty space-separated words of lowercase and uppercase Latin letters and hyphens. Total length of the ad don't exceed 10^6 characters.
-----Output-----
Output minimal width of the ad.
-----Examples-----
Input
4
garage for sa-le
Output
7
Input
4
Edu-ca-tion-al Ro-unds are so fun
Output
10
-----Note-----
Here all spaces are replaced with dots.
In the first example one of possible results after all word wraps looks like this:
garage.
for.
sa-
le
The second example:
Edu-ca-
tion-al.
Ro-unds.
are.so.fun
|
def do():
k = int(input())
ad = input()
def valid(width, limit):
l = -1
count = 0
cur = 0
for r in range(len(ad)):
if ad[r] == " " or ad[r] == "-":
l = r
cur += 1
if cur == width:
if l == -1 and r != len(ad) - 1:
return False
count += 1
if r == len(ad) - 1:
cur = 0
else:
cur = r - l
l = -1
if count > limit:
return False
if cur:
count += 1
return count <= limit
lo, hi = 1, len(ad) + 1
while lo < hi:
mi = lo + hi >> 1
if not valid(mi, k):
lo = mi + 1
else:
hi = mi
return lo
print(do())
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR VAR NUMBER IF VAR VAR IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
|
The main city magazine offers its readers an opportunity to publish their ads. The format of the ad should be like this:
There are space-separated non-empty words of lowercase and uppercase Latin letters.
There are hyphen characters '-' in some words, their positions set word wrapping points. Word can include more than one hyphen.
It is guaranteed that there are no adjacent spaces and no adjacent hyphens. No hyphen is adjacent to space. There are no spaces and no hyphens before the first word and after the last word.
When the word is wrapped, the part of the word before hyphen and the hyphen itself stay on current line and the next part of the word is put on the next line. You can also put line break between two words, in that case the space stays on current line. Check notes for better understanding.
The ad can occupy no more that k lines and should have minimal width. The width of the ad is the maximal length of string (letters, spaces and hyphens are counted) in it.
You should write a program that will find minimal width of the ad.
-----Input-----
The first line contains number k (1 β€ k β€ 10^5).
The second line contains the text of the ad β non-empty space-separated words of lowercase and uppercase Latin letters and hyphens. Total length of the ad don't exceed 10^6 characters.
-----Output-----
Output minimal width of the ad.
-----Examples-----
Input
4
garage for sa-le
Output
7
Input
4
Edu-ca-tion-al Ro-unds are so fun
Output
10
-----Note-----
Here all spaces are replaced with dots.
In the first example one of possible results after all word wraps looks like this:
garage.
for.
sa-
le
The second example:
Edu-ca-
tion-al.
Ro-unds.
are.so.fun
|
import sys
inf = 1 << 30
def solve():
def check(mid):
tot = 1
line = 0
buf = 0
for ch in s:
buf += 1
if ch == " " or ch == "-":
if line + buf > mid:
tot += 1
if tot > k or buf > mid:
return False
line = buf
buf = 0
else:
line += buf
buf = 0
if line + buf > mid:
tot += 1
if tot > k or buf > mid:
return False
return True
k = int(input())
s = input()
top = len(s)
btm = 0
while top - btm > 1:
mid = (top + btm) // 2
if check(mid):
top = mid
else:
btm = mid
ans = top
print(ans)
def __starting_point():
solve()
__starting_point()
|
IMPORT ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER IF VAR STRING VAR STRING IF BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
|
The main city magazine offers its readers an opportunity to publish their ads. The format of the ad should be like this:
There are space-separated non-empty words of lowercase and uppercase Latin letters.
There are hyphen characters '-' in some words, their positions set word wrapping points. Word can include more than one hyphen.
It is guaranteed that there are no adjacent spaces and no adjacent hyphens. No hyphen is adjacent to space. There are no spaces and no hyphens before the first word and after the last word.
When the word is wrapped, the part of the word before hyphen and the hyphen itself stay on current line and the next part of the word is put on the next line. You can also put line break between two words, in that case the space stays on current line. Check notes for better understanding.
The ad can occupy no more that k lines and should have minimal width. The width of the ad is the maximal length of string (letters, spaces and hyphens are counted) in it.
You should write a program that will find minimal width of the ad.
-----Input-----
The first line contains number k (1 β€ k β€ 10^5).
The second line contains the text of the ad β non-empty space-separated words of lowercase and uppercase Latin letters and hyphens. Total length of the ad don't exceed 10^6 characters.
-----Output-----
Output minimal width of the ad.
-----Examples-----
Input
4
garage for sa-le
Output
7
Input
4
Edu-ca-tion-al Ro-unds are so fun
Output
10
-----Note-----
Here all spaces are replaced with dots.
In the first example one of possible results after all word wraps looks like this:
garage.
for.
sa-
le
The second example:
Edu-ca-
tion-al.
Ro-unds.
are.so.fun
|
n = int(input())
s = input()
d = []
pre = 0
for i in range(len(s)):
if s[i] == "-":
d.append(pre + 1)
pre = 0
elif s[i] == " ":
d.append(pre + 1)
pre = 0
else:
pre += 1
d.append(pre)
def calc(k, n):
m = len(d)
tmp = 0
cnt = 1
for i in range(m):
if d[i] > k:
return False
if tmp + d[i] <= k:
tmp += d[i]
else:
tmp = d[i]
cnt += 1
return cnt <= n
l, r = 0, 10**6 + 1
while r - l > 1:
mid = (r + l) // 2
if calc(mid, n):
r = mid
else:
l = mid
print(r)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
The main city magazine offers its readers an opportunity to publish their ads. The format of the ad should be like this:
There are space-separated non-empty words of lowercase and uppercase Latin letters.
There are hyphen characters '-' in some words, their positions set word wrapping points. Word can include more than one hyphen.
It is guaranteed that there are no adjacent spaces and no adjacent hyphens. No hyphen is adjacent to space. There are no spaces and no hyphens before the first word and after the last word.
When the word is wrapped, the part of the word before hyphen and the hyphen itself stay on current line and the next part of the word is put on the next line. You can also put line break between two words, in that case the space stays on current line. Check notes for better understanding.
The ad can occupy no more that k lines and should have minimal width. The width of the ad is the maximal length of string (letters, spaces and hyphens are counted) in it.
You should write a program that will find minimal width of the ad.
-----Input-----
The first line contains number k (1 β€ k β€ 10^5).
The second line contains the text of the ad β non-empty space-separated words of lowercase and uppercase Latin letters and hyphens. Total length of the ad don't exceed 10^6 characters.
-----Output-----
Output minimal width of the ad.
-----Examples-----
Input
4
garage for sa-le
Output
7
Input
4
Edu-ca-tion-al Ro-unds are so fun
Output
10
-----Note-----
Here all spaces are replaced with dots.
In the first example one of possible results after all word wraps looks like this:
garage.
for.
sa-
le
The second example:
Edu-ca-
tion-al.
Ro-unds.
are.so.fun
|
k = int(input())
s = input().strip()
s = s.replace(" ", "-")
ts = s.split("-")
ls = [(len(i) + 1) for i in ts]
ls[-1] -= 1
amin = max(ls)
als = (len(s) + k - 1) // k
ret = max(amin, als)
while True:
nb = 0
idx = 0
crtsize = 0
while nb < k:
if crtsize + ls[idx] <= ret:
crtsize += ls[idx]
else:
nb += 1
crtsize = ls[idx]
idx += 1
if nb < k and idx >= len(ls):
break
else:
ret += 1
continue
break
print(ret)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
The main city magazine offers its readers an opportunity to publish their ads. The format of the ad should be like this:
There are space-separated non-empty words of lowercase and uppercase Latin letters.
There are hyphen characters '-' in some words, their positions set word wrapping points. Word can include more than one hyphen.
It is guaranteed that there are no adjacent spaces and no adjacent hyphens. No hyphen is adjacent to space. There are no spaces and no hyphens before the first word and after the last word.
When the word is wrapped, the part of the word before hyphen and the hyphen itself stay on current line and the next part of the word is put on the next line. You can also put line break between two words, in that case the space stays on current line. Check notes for better understanding.
The ad can occupy no more that k lines and should have minimal width. The width of the ad is the maximal length of string (letters, spaces and hyphens are counted) in it.
You should write a program that will find minimal width of the ad.
-----Input-----
The first line contains number k (1 β€ k β€ 10^5).
The second line contains the text of the ad β non-empty space-separated words of lowercase and uppercase Latin letters and hyphens. Total length of the ad don't exceed 10^6 characters.
-----Output-----
Output minimal width of the ad.
-----Examples-----
Input
4
garage for sa-le
Output
7
Input
4
Edu-ca-tion-al Ro-unds are so fun
Output
10
-----Note-----
Here all spaces are replaced with dots.
In the first example one of possible results after all word wraps looks like this:
garage.
for.
sa-
le
The second example:
Edu-ca-
tion-al.
Ro-unds.
are.so.fun
|
def solve():
k = int(input())
s = input()
s = s.replace("-", " ")
x = [len(a) for a in s.split(" ")]
x[-1] -= 1
x = x[::-1]
def good(z):
y = x[:]
l = 1
curr = 0
while y:
u = y.pop() + 1
if u > z:
return False
elif curr + u > z:
l += 1
curr = u
else:
curr += u
return l <= k
low, high = 0, 10**6 + 1
while high - low > 1:
m = (low + high) // 2
if good(m):
high = m
else:
low = m
return high
print(solve())
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER IF VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
|
The main city magazine offers its readers an opportunity to publish their ads. The format of the ad should be like this:
There are space-separated non-empty words of lowercase and uppercase Latin letters.
There are hyphen characters '-' in some words, their positions set word wrapping points. Word can include more than one hyphen.
It is guaranteed that there are no adjacent spaces and no adjacent hyphens. No hyphen is adjacent to space. There are no spaces and no hyphens before the first word and after the last word.
When the word is wrapped, the part of the word before hyphen and the hyphen itself stay on current line and the next part of the word is put on the next line. You can also put line break between two words, in that case the space stays on current line. Check notes for better understanding.
The ad can occupy no more that k lines and should have minimal width. The width of the ad is the maximal length of string (letters, spaces and hyphens are counted) in it.
You should write a program that will find minimal width of the ad.
-----Input-----
The first line contains number k (1 β€ k β€ 10^5).
The second line contains the text of the ad β non-empty space-separated words of lowercase and uppercase Latin letters and hyphens. Total length of the ad don't exceed 10^6 characters.
-----Output-----
Output minimal width of the ad.
-----Examples-----
Input
4
garage for sa-le
Output
7
Input
4
Edu-ca-tion-al Ro-unds are so fun
Output
10
-----Note-----
Here all spaces are replaced with dots.
In the first example one of possible results after all word wraps looks like this:
garage.
for.
sa-
le
The second example:
Edu-ca-
tion-al.
Ro-unds.
are.so.fun
|
n = int(input())
xs = list([(len(list(x)) + 1) for x in input().replace("-", " ").split()])
xs[-1] -= 1
def f(xs, n, c):
cnt = 1
tmp = 0
for x in xs:
if c < x:
return False
elif c < tmp + x:
tmp = 0
cnt += 1
tmp += x
return cnt <= n
l = 1
r = sum(xs)
while l + 1 < r:
c = (l + r) // 2
if f(xs, n, c):
r = c
else:
l = c
print(r)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING STRING VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR RETURN NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
The main city magazine offers its readers an opportunity to publish their ads. The format of the ad should be like this:
There are space-separated non-empty words of lowercase and uppercase Latin letters.
There are hyphen characters '-' in some words, their positions set word wrapping points. Word can include more than one hyphen.
It is guaranteed that there are no adjacent spaces and no adjacent hyphens. No hyphen is adjacent to space. There are no spaces and no hyphens before the first word and after the last word.
When the word is wrapped, the part of the word before hyphen and the hyphen itself stay on current line and the next part of the word is put on the next line. You can also put line break between two words, in that case the space stays on current line. Check notes for better understanding.
The ad can occupy no more that k lines and should have minimal width. The width of the ad is the maximal length of string (letters, spaces and hyphens are counted) in it.
You should write a program that will find minimal width of the ad.
-----Input-----
The first line contains number k (1 β€ k β€ 10^5).
The second line contains the text of the ad β non-empty space-separated words of lowercase and uppercase Latin letters and hyphens. Total length of the ad don't exceed 10^6 characters.
-----Output-----
Output minimal width of the ad.
-----Examples-----
Input
4
garage for sa-le
Output
7
Input
4
Edu-ca-tion-al Ro-unds are so fun
Output
10
-----Note-----
Here all spaces are replaced with dots.
In the first example one of possible results after all word wraps looks like this:
garage.
for.
sa-
le
The second example:
Edu-ca-
tion-al.
Ro-unds.
are.so.fun
|
k = int(input())
s = input()
n = len(s)
li = [0]
for i in range(n):
if s[i] == "-" or s[i] == " ":
li.append(i)
li.append(n - 1)
if len(li) == 0:
print(n)
else:
def func(m):
i, j, count = m - 1, 0, 0
while i <= n - 1:
p = j
while p < len(li):
if li[p] > i:
break
p += 1
if p - j == 1:
return False
i = li[p - 1] + m
j = p - 1
count += 1
if count > k:
return False
if i - m < n - 1:
count += 1
return count <= k
low = li[1] + 1
high = n
ans = 0
while low <= high:
mid = (low + high) // 2
check = func(mid)
if check:
ans = mid
high = mid - 1
else:
low = mid + 1
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Chef has an array A of length N.
He can modify this array by applying a special operation any number of times. In one operation, he can:
Select two indices i and j (1β€ i < j β€ |A|).
Append A_{i} \oplus A_{j} to the end of the array, where \oplus denotes the [bitwise XOR operation]
Remove A_{i} and A_{j} from the array.
Chef wants to maximize the product of all the elements of the array after applying these operations.
Help Chef determine the maximum product he can achieve by applying this operation any (possibly zero) number of times. As this number can be large, print it modulo 998244353.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains one integer N β the number of elements in the array.
- The second line consists of N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the elements of the array initially.
------ Output Format ------
For each test case, output the maximum product he can achieve modulo 998244353.
------ Constraints ------
$1 β€ T β€ 5\cdot 10^{4}$
$2 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $3\cdot 10^{5}$.
----- Sample Input 1 ------
3
4
1 2 1 2
2
3 3
2
2 1
----- Sample Output 1 ------
9
9
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[1, 2, 3]$.
- Operation $2$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3, 3]$.
The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Test case $2$: The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Thus, Chef does not need to perform any operations.
Test case $3$: Chef can make the following operation:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3]$.
The product of all elements is $3$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
|
t = int(input())
for _ in range(t):
n = int(input())
l = [int(x) for x in input().split()]
c = l.count(1)
l.sort()
ans = 1
for i in range(n):
if l[i] % 2 == 0 and c > 0:
ans *= l[i] + 1
ans %= 998244353
c -= 1
else:
ans *= l[i]
ans %= 998244353
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Chef has an array A of length N.
He can modify this array by applying a special operation any number of times. In one operation, he can:
Select two indices i and j (1β€ i < j β€ |A|).
Append A_{i} \oplus A_{j} to the end of the array, where \oplus denotes the [bitwise XOR operation]
Remove A_{i} and A_{j} from the array.
Chef wants to maximize the product of all the elements of the array after applying these operations.
Help Chef determine the maximum product he can achieve by applying this operation any (possibly zero) number of times. As this number can be large, print it modulo 998244353.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains one integer N β the number of elements in the array.
- The second line consists of N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the elements of the array initially.
------ Output Format ------
For each test case, output the maximum product he can achieve modulo 998244353.
------ Constraints ------
$1 β€ T β€ 5\cdot 10^{4}$
$2 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $3\cdot 10^{5}$.
----- Sample Input 1 ------
3
4
1 2 1 2
2
3 3
2
2 1
----- Sample Output 1 ------
9
9
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[1, 2, 3]$.
- Operation $2$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3, 3]$.
The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Test case $2$: The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Thus, Chef does not need to perform any operations.
Test case $3$: Chef can make the following operation:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3]$.
The product of all elements is $3$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
|
for T in range(int(input())):
lent = int(input())
l = [int(x) for x in input().split()]
_1 = l.count(1)
l_even = []
odd_prod = 1
for i in l:
if i % 2 == 0:
l_even.append(i % 998244353)
else:
odd_prod = odd_prod * i % 998244353
l_even.sort()
_even = len(l_even)
if _even == 0:
print(odd_prod % 998244353)
else:
if _1 >= _even:
for i in range(_even):
l_even[i] += 1
else:
for j in range(_1):
l_even[j] += 1
even_prod = 1
for i in range(_even):
even_prod = even_prod * l_even[i] % 998244353
print(odd_prod % 998244353 * (even_prod % 998244353) % 998244353)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER
|
Chef has an array A of length N.
He can modify this array by applying a special operation any number of times. In one operation, he can:
Select two indices i and j (1β€ i < j β€ |A|).
Append A_{i} \oplus A_{j} to the end of the array, where \oplus denotes the [bitwise XOR operation]
Remove A_{i} and A_{j} from the array.
Chef wants to maximize the product of all the elements of the array after applying these operations.
Help Chef determine the maximum product he can achieve by applying this operation any (possibly zero) number of times. As this number can be large, print it modulo 998244353.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains one integer N β the number of elements in the array.
- The second line consists of N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the elements of the array initially.
------ Output Format ------
For each test case, output the maximum product he can achieve modulo 998244353.
------ Constraints ------
$1 β€ T β€ 5\cdot 10^{4}$
$2 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $3\cdot 10^{5}$.
----- Sample Input 1 ------
3
4
1 2 1 2
2
3 3
2
2 1
----- Sample Output 1 ------
9
9
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[1, 2, 3]$.
- Operation $2$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3, 3]$.
The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Test case $2$: The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Thus, Chef does not need to perform any operations.
Test case $3$: Chef can make the following operation:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3]$.
The product of all elements is $3$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
|
t = int(input())
for _ in range(t):
n = int(input())
l = list(map(int, input().split()))
o = []
e = []
c = 0
for i in l:
if i == 1:
c += 1
l.sort()
p = 1
for i in l:
if i % 2 == 0 and c:
p = p * (i ^ 1) % 998244353
c -= 1
else:
p = p * i % 998244353
print(p)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Chef has an array A of length N.
He can modify this array by applying a special operation any number of times. In one operation, he can:
Select two indices i and j (1β€ i < j β€ |A|).
Append A_{i} \oplus A_{j} to the end of the array, where \oplus denotes the [bitwise XOR operation]
Remove A_{i} and A_{j} from the array.
Chef wants to maximize the product of all the elements of the array after applying these operations.
Help Chef determine the maximum product he can achieve by applying this operation any (possibly zero) number of times. As this number can be large, print it modulo 998244353.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains one integer N β the number of elements in the array.
- The second line consists of N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the elements of the array initially.
------ Output Format ------
For each test case, output the maximum product he can achieve modulo 998244353.
------ Constraints ------
$1 β€ T β€ 5\cdot 10^{4}$
$2 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $3\cdot 10^{5}$.
----- Sample Input 1 ------
3
4
1 2 1 2
2
3 3
2
2 1
----- Sample Output 1 ------
9
9
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[1, 2, 3]$.
- Operation $2$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3, 3]$.
The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Test case $2$: The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Thus, Chef does not need to perform any operations.
Test case $3$: Chef can make the following operation:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3]$.
The product of all elements is $3$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
|
m = 998244353
t = int(input())
for i in range(t):
n = int(input())
arr = list(map(int, input().split()))
arr.sort()
count = arr.count(1)
res = 1
i = 0
while i < n:
if count > 0 and arr[i] % 2 == 0:
res = res * (arr[i] + 1) % m
count -= 1
else:
res = res * arr[i] % m
i += 1
print(res)
|
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Chef has an array A of length N.
He can modify this array by applying a special operation any number of times. In one operation, he can:
Select two indices i and j (1β€ i < j β€ |A|).
Append A_{i} \oplus A_{j} to the end of the array, where \oplus denotes the [bitwise XOR operation]
Remove A_{i} and A_{j} from the array.
Chef wants to maximize the product of all the elements of the array after applying these operations.
Help Chef determine the maximum product he can achieve by applying this operation any (possibly zero) number of times. As this number can be large, print it modulo 998244353.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains one integer N β the number of elements in the array.
- The second line consists of N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the elements of the array initially.
------ Output Format ------
For each test case, output the maximum product he can achieve modulo 998244353.
------ Constraints ------
$1 β€ T β€ 5\cdot 10^{4}$
$2 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $3\cdot 10^{5}$.
----- Sample Input 1 ------
3
4
1 2 1 2
2
3 3
2
2 1
----- Sample Output 1 ------
9
9
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[1, 2, 3]$.
- Operation $2$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3, 3]$.
The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Test case $2$: The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Thus, Chef does not need to perform any operations.
Test case $3$: Chef can make the following operation:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3]$.
The product of all elements is $3$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
|
prime = 998244353
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
odds = sorted([x for x in a if x % 2 != 0])
evens = sorted([x for x in a if x % 2 == 0])
j = k = 0
while j < len(odds) and k < len(evens):
if odds[j] != 1:
break
evens[k] += 1
j += 1
k += 1
prod = 1
for k in range(j, len(odds)):
prod = prod * odds[k] % prime
for x in evens:
prod = prod * x % prime
print(prod)
|
ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Chef has an array A of length N.
He can modify this array by applying a special operation any number of times. In one operation, he can:
Select two indices i and j (1β€ i < j β€ |A|).
Append A_{i} \oplus A_{j} to the end of the array, where \oplus denotes the [bitwise XOR operation]
Remove A_{i} and A_{j} from the array.
Chef wants to maximize the product of all the elements of the array after applying these operations.
Help Chef determine the maximum product he can achieve by applying this operation any (possibly zero) number of times. As this number can be large, print it modulo 998244353.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains one integer N β the number of elements in the array.
- The second line consists of N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the elements of the array initially.
------ Output Format ------
For each test case, output the maximum product he can achieve modulo 998244353.
------ Constraints ------
$1 β€ T β€ 5\cdot 10^{4}$
$2 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $3\cdot 10^{5}$.
----- Sample Input 1 ------
3
4
1 2 1 2
2
3 3
2
2 1
----- Sample Output 1 ------
9
9
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[1, 2, 3]$.
- Operation $2$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3, 3]$.
The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Test case $2$: The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Thus, Chef does not need to perform any operations.
Test case $3$: Chef can make the following operation:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3]$.
The product of all elements is $3$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
|
Test = int(input())
mod = 998244353
for t in range(Test):
n = int(input())
lis = map(int, input().split())
lis = sorted(lis)
countof1 = lis.count(1)
for i in range(n):
if lis[i] % 2 == 0 and countof1 > 0:
lis[i] += 1
countof1 -= 1
ans = 1
for a in lis:
ans = ans * a % mod
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Chef has an array A of length N.
He can modify this array by applying a special operation any number of times. In one operation, he can:
Select two indices i and j (1β€ i < j β€ |A|).
Append A_{i} \oplus A_{j} to the end of the array, where \oplus denotes the [bitwise XOR operation]
Remove A_{i} and A_{j} from the array.
Chef wants to maximize the product of all the elements of the array after applying these operations.
Help Chef determine the maximum product he can achieve by applying this operation any (possibly zero) number of times. As this number can be large, print it modulo 998244353.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains one integer N β the number of elements in the array.
- The second line consists of N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the elements of the array initially.
------ Output Format ------
For each test case, output the maximum product he can achieve modulo 998244353.
------ Constraints ------
$1 β€ T β€ 5\cdot 10^{4}$
$2 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $3\cdot 10^{5}$.
----- Sample Input 1 ------
3
4
1 2 1 2
2
3 3
2
2 1
----- Sample Output 1 ------
9
9
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[1, 2, 3]$.
- Operation $2$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3, 3]$.
The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Test case $2$: The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Thus, Chef does not need to perform any operations.
Test case $3$: Chef can make the following operation:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3]$.
The product of all elements is $3$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
|
m = 998244353
t = int(input())
while t != 0:
n = int(input())
a = [int(i) for i in input().split()]
x = 0
s = 1
b = []
k = 1
for i in a:
if i == 1:
x += 1
elif i % 2 == 0:
b.append(i)
else:
k = k * i
k = k % m
b.sort()
for i in range(0, len(b)):
if x > 0:
k = k * (b[i] + 1)
k = k % m
x = x - 1
else:
k = k * b[i]
k = k % m
print(k)
t = t - 1
|
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER
|
Chef has an array A of length N.
He can modify this array by applying a special operation any number of times. In one operation, he can:
Select two indices i and j (1β€ i < j β€ |A|).
Append A_{i} \oplus A_{j} to the end of the array, where \oplus denotes the [bitwise XOR operation]
Remove A_{i} and A_{j} from the array.
Chef wants to maximize the product of all the elements of the array after applying these operations.
Help Chef determine the maximum product he can achieve by applying this operation any (possibly zero) number of times. As this number can be large, print it modulo 998244353.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains one integer N β the number of elements in the array.
- The second line consists of N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the elements of the array initially.
------ Output Format ------
For each test case, output the maximum product he can achieve modulo 998244353.
------ Constraints ------
$1 β€ T β€ 5\cdot 10^{4}$
$2 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $3\cdot 10^{5}$.
----- Sample Input 1 ------
3
4
1 2 1 2
2
3 3
2
2 1
----- Sample Output 1 ------
9
9
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[1, 2, 3]$.
- Operation $2$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3, 3]$.
The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Test case $2$: The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Thus, Chef does not need to perform any operations.
Test case $3$: Chef can make the following operation:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3]$.
The product of all elements is $3$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
|
for _ in range(int(input())):
N = int(input())
A = [int(a) for a in input().split()]
even = []
odd = []
for i in range(N):
if A[i] % 2 == 0:
even.append(A[i])
else:
odd.append(A[i])
count = odd.count(1)
even = sorted(even)
j = 0
curr = 0
while curr < len(even) and j < count:
even[curr] += 1
j += 1
curr += 1
mod = 998244353
value = 1
for i in range(len(odd)):
value = value * odd[i]
value = value % mod
for i in range(len(even)):
value = value * even[i]
value = value % mod
print(value)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
Chef has an array A of length N.
He can modify this array by applying a special operation any number of times. In one operation, he can:
Select two indices i and j (1β€ i < j β€ |A|).
Append A_{i} \oplus A_{j} to the end of the array, where \oplus denotes the [bitwise XOR operation]
Remove A_{i} and A_{j} from the array.
Chef wants to maximize the product of all the elements of the array after applying these operations.
Help Chef determine the maximum product he can achieve by applying this operation any (possibly zero) number of times. As this number can be large, print it modulo 998244353.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains one integer N β the number of elements in the array.
- The second line consists of N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the elements of the array initially.
------ Output Format ------
For each test case, output the maximum product he can achieve modulo 998244353.
------ Constraints ------
$1 β€ T β€ 5\cdot 10^{4}$
$2 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $3\cdot 10^{5}$.
----- Sample Input 1 ------
3
4
1 2 1 2
2
3 3
2
2 1
----- Sample Output 1 ------
9
9
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[1, 2, 3]$.
- Operation $2$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3, 3]$.
The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Test case $2$: The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Thus, Chef does not need to perform any operations.
Test case $3$: Chef can make the following operation:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3]$.
The product of all elements is $3$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
|
for _ in range(int(input())):
n = int(input())
l = list(map(int, input().split()))
one = 0
ans = 1
for i in l:
if i == 1:
one += 1
l.sort()
for i in range(len(l)):
if l[i] % 2 == 0 and one:
l[i] += 1
one -= 1
ans = ans * l[i] % 998244353
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Chef has an array A of length N.
He can modify this array by applying a special operation any number of times. In one operation, he can:
Select two indices i and j (1β€ i < j β€ |A|).
Append A_{i} \oplus A_{j} to the end of the array, where \oplus denotes the [bitwise XOR operation]
Remove A_{i} and A_{j} from the array.
Chef wants to maximize the product of all the elements of the array after applying these operations.
Help Chef determine the maximum product he can achieve by applying this operation any (possibly zero) number of times. As this number can be large, print it modulo 998244353.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains one integer N β the number of elements in the array.
- The second line consists of N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the elements of the array initially.
------ Output Format ------
For each test case, output the maximum product he can achieve modulo 998244353.
------ Constraints ------
$1 β€ T β€ 5\cdot 10^{4}$
$2 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $3\cdot 10^{5}$.
----- Sample Input 1 ------
3
4
1 2 1 2
2
3 3
2
2 1
----- Sample Output 1 ------
9
9
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[1, 2, 3]$.
- Operation $2$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3, 3]$.
The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Test case $2$: The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Thus, Chef does not need to perform any operations.
Test case $3$: Chef can make the following operation:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3]$.
The product of all elements is $3$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
|
mod = 998244353
for _ in range(int(input())):
n = int(input())
arr = list(map(int, input().split()))
arr.sort()
count = arr.count(1)
ans = 1
for i in range(n):
if arr[i] % 2 == 0 and count > 0:
arr[i] = arr[i] + 1
count -= 1
ans = ans * arr[i] % mod
print(ans)
|
ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Chef has an array A of length N.
He can modify this array by applying a special operation any number of times. In one operation, he can:
Select two indices i and j (1β€ i < j β€ |A|).
Append A_{i} \oplus A_{j} to the end of the array, where \oplus denotes the [bitwise XOR operation]
Remove A_{i} and A_{j} from the array.
Chef wants to maximize the product of all the elements of the array after applying these operations.
Help Chef determine the maximum product he can achieve by applying this operation any (possibly zero) number of times. As this number can be large, print it modulo 998244353.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains one integer N β the number of elements in the array.
- The second line consists of N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the elements of the array initially.
------ Output Format ------
For each test case, output the maximum product he can achieve modulo 998244353.
------ Constraints ------
$1 β€ T β€ 5\cdot 10^{4}$
$2 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $3\cdot 10^{5}$.
----- Sample Input 1 ------
3
4
1 2 1 2
2
3 3
2
2 1
----- Sample Output 1 ------
9
9
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[1, 2, 3]$.
- Operation $2$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3, 3]$.
The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Test case $2$: The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Thus, Chef does not need to perform any operations.
Test case $3$: Chef can make the following operation:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3]$.
The product of all elements is $3$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
|
MODULO = 998244353
for _ in range(int(input())):
n = int(input())
a = list(map(lambda x: x % MODULO, map(int, input().split(" "))))
e = [x for x in a if x % 2 == 0]
o = 0
for i in range(n):
if a[i] == 1:
o += 1
e.sort()
res = 1
for x in e:
res *= (x + (1 if o > 0 else 0)) % MODULO
o -= 1
for x in a:
if x & 1:
res *= x
res %= MODULO
print(res)
|
ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER VAR VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Chef has an array A of length N.
He can modify this array by applying a special operation any number of times. In one operation, he can:
Select two indices i and j (1β€ i < j β€ |A|).
Append A_{i} \oplus A_{j} to the end of the array, where \oplus denotes the [bitwise XOR operation]
Remove A_{i} and A_{j} from the array.
Chef wants to maximize the product of all the elements of the array after applying these operations.
Help Chef determine the maximum product he can achieve by applying this operation any (possibly zero) number of times. As this number can be large, print it modulo 998244353.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains one integer N β the number of elements in the array.
- The second line consists of N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the elements of the array initially.
------ Output Format ------
For each test case, output the maximum product he can achieve modulo 998244353.
------ Constraints ------
$1 β€ T β€ 5\cdot 10^{4}$
$2 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $3\cdot 10^{5}$.
----- Sample Input 1 ------
3
4
1 2 1 2
2
3 3
2
2 1
----- Sample Output 1 ------
9
9
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[1, 2, 3]$.
- Operation $2$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3, 3]$.
The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Test case $2$: The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Thus, Chef does not need to perform any operations.
Test case $3$: Chef can make the following operation:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3]$.
The product of all elements is $3$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
|
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
a.sort()
finprod = 1
aones = a.count(1)
evs = []
for e in range(aones, n):
if a[e] % 2 == 1:
finprod = finprod * a[e] % 998244353
else:
evs.append(a[e])
evs.sort()
i = min(aones, len(evs))
for j in range(len(evs)):
if j < i:
finprod = finprod * (evs[j] + 1) % 998244353
else:
finprod = finprod * evs[j] % 998244353
print(finprod % 998244353)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
|
Chef has an array A of length N.
He can modify this array by applying a special operation any number of times. In one operation, he can:
Select two indices i and j (1β€ i < j β€ |A|).
Append A_{i} \oplus A_{j} to the end of the array, where \oplus denotes the [bitwise XOR operation]
Remove A_{i} and A_{j} from the array.
Chef wants to maximize the product of all the elements of the array after applying these operations.
Help Chef determine the maximum product he can achieve by applying this operation any (possibly zero) number of times. As this number can be large, print it modulo 998244353.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains one integer N β the number of elements in the array.
- The second line consists of N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the elements of the array initially.
------ Output Format ------
For each test case, output the maximum product he can achieve modulo 998244353.
------ Constraints ------
$1 β€ T β€ 5\cdot 10^{4}$
$2 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $3\cdot 10^{5}$.
----- Sample Input 1 ------
3
4
1 2 1 2
2
3 3
2
2 1
----- Sample Output 1 ------
9
9
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[1, 2, 3]$.
- Operation $2$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3, 3]$.
The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Test case $2$: The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Thus, Chef does not need to perform any operations.
Test case $3$: Chef can make the following operation:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3]$.
The product of all elements is $3$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
|
mod = 998244353
for _ in range(int(input())):
n = int(input())
L = list(map(int, input().split()))
L.sort()
c = 0
while L and L[0] == 1:
c += 1
L.pop(0)
res = 1
for i in L:
if i & 1 == 0 and c > 0:
c -= 1
i ^= 1
res *= i
res %= mod
print(res)
|
ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Chef has an array A of length N.
He can modify this array by applying a special operation any number of times. In one operation, he can:
Select two indices i and j (1β€ i < j β€ |A|).
Append A_{i} \oplus A_{j} to the end of the array, where \oplus denotes the [bitwise XOR operation]
Remove A_{i} and A_{j} from the array.
Chef wants to maximize the product of all the elements of the array after applying these operations.
Help Chef determine the maximum product he can achieve by applying this operation any (possibly zero) number of times. As this number can be large, print it modulo 998244353.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains one integer N β the number of elements in the array.
- The second line consists of N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the elements of the array initially.
------ Output Format ------
For each test case, output the maximum product he can achieve modulo 998244353.
------ Constraints ------
$1 β€ T β€ 5\cdot 10^{4}$
$2 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $3\cdot 10^{5}$.
----- Sample Input 1 ------
3
4
1 2 1 2
2
3 3
2
2 1
----- Sample Output 1 ------
9
9
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[1, 2, 3]$.
- Operation $2$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3, 3]$.
The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Test case $2$: The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Thus, Chef does not need to perform any operations.
Test case $3$: Chef can make the following operation:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3]$.
The product of all elements is $3$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
|
t = int(input())
MOD = 998244353
for _ in range(t):
n = int(input())
arr = list(map(int, input().split()))
odd = []
even = []
for x in arr:
if x % 2 == 1:
odd.append(x)
else:
even.append(x)
even.sort()
c = arr.count(1)
ans = 1
for x in even:
if c == 0:
ans = ans * x % MOD
else:
ans = ans * (x + 1) % MOD
c -= 1
for x in odd:
ans = ans * x % MOD
print(ans % 998244353)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
|
Chef has an array A of length N.
He can modify this array by applying a special operation any number of times. In one operation, he can:
Select two indices i and j (1β€ i < j β€ |A|).
Append A_{i} \oplus A_{j} to the end of the array, where \oplus denotes the [bitwise XOR operation]
Remove A_{i} and A_{j} from the array.
Chef wants to maximize the product of all the elements of the array after applying these operations.
Help Chef determine the maximum product he can achieve by applying this operation any (possibly zero) number of times. As this number can be large, print it modulo 998244353.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains one integer N β the number of elements in the array.
- The second line consists of N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the elements of the array initially.
------ Output Format ------
For each test case, output the maximum product he can achieve modulo 998244353.
------ Constraints ------
$1 β€ T β€ 5\cdot 10^{4}$
$2 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $3\cdot 10^{5}$.
----- Sample Input 1 ------
3
4
1 2 1 2
2
3 3
2
2 1
----- Sample Output 1 ------
9
9
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[1, 2, 3]$.
- Operation $2$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3, 3]$.
The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Test case $2$: The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Thus, Chef does not need to perform any operations.
Test case $3$: Chef can make the following operation:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3]$.
The product of all elements is $3$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
|
def solution():
n = int(input())
a = [int(num) for num in input().split()]
mod = 998244353
ones = []
evens = []
others = []
ans = 1
for num in a:
if num == 1:
ones.append(1)
elif num % 2 == 0:
evens.append(num)
else:
others.append(num)
n = min(len(ones), len(evens))
evens.sort()
for i in range(n):
ans = ans * (evens[i] + 1) % mod
for i in range(n, len(evens)):
ans = ans * evens[i] % mod
for num in others:
ans = ans * num % mod
print(ans)
def main():
t = int(input())
while t:
solution()
t -= 1
main()
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR
|
Chef has an array A of length N.
He can modify this array by applying a special operation any number of times. In one operation, he can:
Select two indices i and j (1β€ i < j β€ |A|).
Append A_{i} \oplus A_{j} to the end of the array, where \oplus denotes the [bitwise XOR operation]
Remove A_{i} and A_{j} from the array.
Chef wants to maximize the product of all the elements of the array after applying these operations.
Help Chef determine the maximum product he can achieve by applying this operation any (possibly zero) number of times. As this number can be large, print it modulo 998244353.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains one integer N β the number of elements in the array.
- The second line consists of N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the elements of the array initially.
------ Output Format ------
For each test case, output the maximum product he can achieve modulo 998244353.
------ Constraints ------
$1 β€ T β€ 5\cdot 10^{4}$
$2 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $3\cdot 10^{5}$.
----- Sample Input 1 ------
3
4
1 2 1 2
2
3 3
2
2 1
----- Sample Output 1 ------
9
9
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[1, 2, 3]$.
- Operation $2$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3, 3]$.
The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Test case $2$: The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Thus, Chef does not need to perform any operations.
Test case $3$: Chef can make the following operation:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3]$.
The product of all elements is $3$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
|
t = int(input())
for i in range(t):
l = int(input())
lis = list(map(int, input().split()))
c = lis.count(1)
res = sorted(lis)
ans = 1
for i in range(len(res)):
if c > 0 and res[i] % 2 == 0:
ans = ans % 998244353 * ((res[i] + 1) % 998244353) % 998244353
c -= 1
else:
ans = ans % 998244353 * (res[i] % 998244353) % 998244353
print(ans % 998244353)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
|
Chef has an array A of length N.
He can modify this array by applying a special operation any number of times. In one operation, he can:
Select two indices i and j (1β€ i < j β€ |A|).
Append A_{i} \oplus A_{j} to the end of the array, where \oplus denotes the [bitwise XOR operation]
Remove A_{i} and A_{j} from the array.
Chef wants to maximize the product of all the elements of the array after applying these operations.
Help Chef determine the maximum product he can achieve by applying this operation any (possibly zero) number of times. As this number can be large, print it modulo 998244353.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains one integer N β the number of elements in the array.
- The second line consists of N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the elements of the array initially.
------ Output Format ------
For each test case, output the maximum product he can achieve modulo 998244353.
------ Constraints ------
$1 β€ T β€ 5\cdot 10^{4}$
$2 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $3\cdot 10^{5}$.
----- Sample Input 1 ------
3
4
1 2 1 2
2
3 3
2
2 1
----- Sample Output 1 ------
9
9
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[1, 2, 3]$.
- Operation $2$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3, 3]$.
The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Test case $2$: The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Thus, Chef does not need to perform any operations.
Test case $3$: Chef can make the following operation:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3]$.
The product of all elements is $3$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
|
for _ in range(int(input())):
n = int(input())
arr = list(map(int, input().split()))
ans = 1
mod = 998244353
ev = []
ctr = 0
for i in range(n):
if arr[i] == 1:
ctr += 1
elif arr[i] % 2 == 0:
ev.append(arr[i])
else:
ans *= arr[i]
ans %= mod
ev.sort()
for i in range(len(ev)):
if ctr != 0:
ans *= ev[i] + 1
ctr -= 1
ans %= mod
else:
ans *= ev[i]
ans %= mod
print(ans % mod)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
Chef has an array A of length N.
He can modify this array by applying a special operation any number of times. In one operation, he can:
Select two indices i and j (1β€ i < j β€ |A|).
Append A_{i} \oplus A_{j} to the end of the array, where \oplus denotes the [bitwise XOR operation]
Remove A_{i} and A_{j} from the array.
Chef wants to maximize the product of all the elements of the array after applying these operations.
Help Chef determine the maximum product he can achieve by applying this operation any (possibly zero) number of times. As this number can be large, print it modulo 998244353.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains one integer N β the number of elements in the array.
- The second line consists of N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the elements of the array initially.
------ Output Format ------
For each test case, output the maximum product he can achieve modulo 998244353.
------ Constraints ------
$1 β€ T β€ 5\cdot 10^{4}$
$2 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $3\cdot 10^{5}$.
----- Sample Input 1 ------
3
4
1 2 1 2
2
3 3
2
2 1
----- Sample Output 1 ------
9
9
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[1, 2, 3]$.
- Operation $2$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3, 3]$.
The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Test case $2$: The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Thus, Chef does not need to perform any operations.
Test case $3$: Chef can make the following operation:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3]$.
The product of all elements is $3$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
|
def xorprod(lst):
even = sorted([x for x in lst if x % 2 == 0])
odd = sorted([x for x in lst if x % 2 == 1])
i = j = 0
while i < len(odd) and j < len(even):
if odd[i] != 1:
break
even[j] += 1
i += 1
j += 1
res = 1
for i in range(len(odd)):
res = res * odd[i] % 998244353
for el in even:
res = res * el % 998244353
return res
T = int(input())
for t in range(T):
n = int(input())
lst = list(map(int, input().split()))
print(xorprod(lst))
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
|
Chef has an array A of length N.
He can modify this array by applying a special operation any number of times. In one operation, he can:
Select two indices i and j (1β€ i < j β€ |A|).
Append A_{i} \oplus A_{j} to the end of the array, where \oplus denotes the [bitwise XOR operation]
Remove A_{i} and A_{j} from the array.
Chef wants to maximize the product of all the elements of the array after applying these operations.
Help Chef determine the maximum product he can achieve by applying this operation any (possibly zero) number of times. As this number can be large, print it modulo 998244353.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains one integer N β the number of elements in the array.
- The second line consists of N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the elements of the array initially.
------ Output Format ------
For each test case, output the maximum product he can achieve modulo 998244353.
------ Constraints ------
$1 β€ T β€ 5\cdot 10^{4}$
$2 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $3\cdot 10^{5}$.
----- Sample Input 1 ------
3
4
1 2 1 2
2
3 3
2
2 1
----- Sample Output 1 ------
9
9
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[1, 2, 3]$.
- Operation $2$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3, 3]$.
The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Test case $2$: The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Thus, Chef does not need to perform any operations.
Test case $3$: Chef can make the following operation:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3]$.
The product of all elements is $3$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
|
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
a.sort()
one = 0
for i in range(n):
if a[i] == 1:
one += 1
else:
break
j = 0
ans = 1
for elem in a:
if one > 0 and elem % 2 == 0:
ans *= elem + 1
ans %= 998244353
one -= 1
else:
ans *= elem
ans %= 998244353
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Chef has an array A of length N.
He can modify this array by applying a special operation any number of times. In one operation, he can:
Select two indices i and j (1β€ i < j β€ |A|).
Append A_{i} \oplus A_{j} to the end of the array, where \oplus denotes the [bitwise XOR operation]
Remove A_{i} and A_{j} from the array.
Chef wants to maximize the product of all the elements of the array after applying these operations.
Help Chef determine the maximum product he can achieve by applying this operation any (possibly zero) number of times. As this number can be large, print it modulo 998244353.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains one integer N β the number of elements in the array.
- The second line consists of N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the elements of the array initially.
------ Output Format ------
For each test case, output the maximum product he can achieve modulo 998244353.
------ Constraints ------
$1 β€ T β€ 5\cdot 10^{4}$
$2 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $3\cdot 10^{5}$.
----- Sample Input 1 ------
3
4
1 2 1 2
2
3 3
2
2 1
----- Sample Output 1 ------
9
9
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[1, 2, 3]$.
- Operation $2$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3, 3]$.
The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Test case $2$: The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Thus, Chef does not need to perform any operations.
Test case $3$: Chef can make the following operation:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3]$.
The product of all elements is $3$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
|
for i in range(int(input())):
n = int(input())
lst = list(map(int, input().split()))
lst.sort()
c = lst.count(1)
for i in range(len(lst)):
if c > 0 and lst[i] % 2 == 0 and lst[i] != 1:
lst[i] += 1
c -= 1
ans = 1
for i in lst:
ans = ans * i % 998244353
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Chef has an array A of length N.
He can modify this array by applying a special operation any number of times. In one operation, he can:
Select two indices i and j (1β€ i < j β€ |A|).
Append A_{i} \oplus A_{j} to the end of the array, where \oplus denotes the [bitwise XOR operation]
Remove A_{i} and A_{j} from the array.
Chef wants to maximize the product of all the elements of the array after applying these operations.
Help Chef determine the maximum product he can achieve by applying this operation any (possibly zero) number of times. As this number can be large, print it modulo 998244353.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains one integer N β the number of elements in the array.
- The second line consists of N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the elements of the array initially.
------ Output Format ------
For each test case, output the maximum product he can achieve modulo 998244353.
------ Constraints ------
$1 β€ T β€ 5\cdot 10^{4}$
$2 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $3\cdot 10^{5}$.
----- Sample Input 1 ------
3
4
1 2 1 2
2
3 3
2
2 1
----- Sample Output 1 ------
9
9
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[1, 2, 3]$.
- Operation $2$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3, 3]$.
The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Test case $2$: The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Thus, Chef does not need to perform any operations.
Test case $3$: Chef can make the following operation:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3]$.
The product of all elements is $3$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
|
for i in range(int(input())):
n = int(input())
arr = list(map(int, input().split()))
e = []
o = []
c = 0
for i in arr:
if i == 1:
c += 1
elif i % 2 == 0:
e.append(i)
else:
o.append(i)
p = 1
for i in o:
p = p * i % 998244353
e.sort()
for i in e:
elem = i
if c > 0:
elem += 1
c -= 1
p = p * elem % 998244353
print(p)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Chef has an array A of length N.
He can modify this array by applying a special operation any number of times. In one operation, he can:
Select two indices i and j (1β€ i < j β€ |A|).
Append A_{i} \oplus A_{j} to the end of the array, where \oplus denotes the [bitwise XOR operation]
Remove A_{i} and A_{j} from the array.
Chef wants to maximize the product of all the elements of the array after applying these operations.
Help Chef determine the maximum product he can achieve by applying this operation any (possibly zero) number of times. As this number can be large, print it modulo 998244353.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains one integer N β the number of elements in the array.
- The second line consists of N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the elements of the array initially.
------ Output Format ------
For each test case, output the maximum product he can achieve modulo 998244353.
------ Constraints ------
$1 β€ T β€ 5\cdot 10^{4}$
$2 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $3\cdot 10^{5}$.
----- Sample Input 1 ------
3
4
1 2 1 2
2
3 3
2
2 1
----- Sample Output 1 ------
9
9
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[1, 2, 3]$.
- Operation $2$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3, 3]$.
The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Test case $2$: The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Thus, Chef does not need to perform any operations.
Test case $3$: Chef can make the following operation:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3]$.
The product of all elements is $3$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
|
mod = 998244353
for _ in range(int(input())):
n = int(input())
L = list(map(int, input().split()))
L.sort()
prod = 1
c = L.count(1)
for i in range(n):
if L[i] & 1 == 0 and c > 0:
c -= 1
L[i] ^= 1
prod = prod % mod
prod *= L[i] % mod
print(prod % mod)
|
ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
Chef has an array A of length N.
He can modify this array by applying a special operation any number of times. In one operation, he can:
Select two indices i and j (1β€ i < j β€ |A|).
Append A_{i} \oplus A_{j} to the end of the array, where \oplus denotes the [bitwise XOR operation]
Remove A_{i} and A_{j} from the array.
Chef wants to maximize the product of all the elements of the array after applying these operations.
Help Chef determine the maximum product he can achieve by applying this operation any (possibly zero) number of times. As this number can be large, print it modulo 998244353.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains one integer N β the number of elements in the array.
- The second line consists of N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the elements of the array initially.
------ Output Format ------
For each test case, output the maximum product he can achieve modulo 998244353.
------ Constraints ------
$1 β€ T β€ 5\cdot 10^{4}$
$2 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $3\cdot 10^{5}$.
----- Sample Input 1 ------
3
4
1 2 1 2
2
3 3
2
2 1
----- Sample Output 1 ------
9
9
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[1, 2, 3]$.
- Operation $2$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3, 3]$.
The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Test case $2$: The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Thus, Chef does not need to perform any operations.
Test case $3$: Chef can make the following operation:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3]$.
The product of all elements is $3$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
|
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
m = 998244353
o, e = 1, []
for i in a:
if i & 1:
o = o * i % m
else:
e.append(i)
c = a.count(1)
e.sort()
for i in range(min(c, len(e))):
e[i] += 1
for i in e:
o = o * i % m
print(o)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER LIST FOR VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Chef has an array A of length N.
He can modify this array by applying a special operation any number of times. In one operation, he can:
Select two indices i and j (1β€ i < j β€ |A|).
Append A_{i} \oplus A_{j} to the end of the array, where \oplus denotes the [bitwise XOR operation]
Remove A_{i} and A_{j} from the array.
Chef wants to maximize the product of all the elements of the array after applying these operations.
Help Chef determine the maximum product he can achieve by applying this operation any (possibly zero) number of times. As this number can be large, print it modulo 998244353.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains one integer N β the number of elements in the array.
- The second line consists of N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the elements of the array initially.
------ Output Format ------
For each test case, output the maximum product he can achieve modulo 998244353.
------ Constraints ------
$1 β€ T β€ 5\cdot 10^{4}$
$2 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $3\cdot 10^{5}$.
----- Sample Input 1 ------
3
4
1 2 1 2
2
3 3
2
2 1
----- Sample Output 1 ------
9
9
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[1, 2, 3]$.
- Operation $2$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3, 3]$.
The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Test case $2$: The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Thus, Chef does not need to perform any operations.
Test case $3$: Chef can make the following operation:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3]$.
The product of all elements is $3$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
|
t = int(input())
while t:
t -= 1
n = int(input())
x = list(map(int, input().split()))
x.sort()
k = x.count(1)
c = 0
for i in range(len(x)):
if c == k:
break
if x[i] % 2 == 0:
c += 1
x[i] = x[i] + 1
prod = 1
for i in x:
prod = prod % 998244353
prod *= i % 998244353
print(prod % 998244353)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
|
Chef has an array A of length N.
He can modify this array by applying a special operation any number of times. In one operation, he can:
Select two indices i and j (1β€ i < j β€ |A|).
Append A_{i} \oplus A_{j} to the end of the array, where \oplus denotes the [bitwise XOR operation]
Remove A_{i} and A_{j} from the array.
Chef wants to maximize the product of all the elements of the array after applying these operations.
Help Chef determine the maximum product he can achieve by applying this operation any (possibly zero) number of times. As this number can be large, print it modulo 998244353.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains one integer N β the number of elements in the array.
- The second line consists of N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the elements of the array initially.
------ Output Format ------
For each test case, output the maximum product he can achieve modulo 998244353.
------ Constraints ------
$1 β€ T β€ 5\cdot 10^{4}$
$2 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $3\cdot 10^{5}$.
----- Sample Input 1 ------
3
4
1 2 1 2
2
3 3
2
2 1
----- Sample Output 1 ------
9
9
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[1, 2, 3]$.
- Operation $2$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3, 3]$.
The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Test case $2$: The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Thus, Chef does not need to perform any operations.
Test case $3$: Chef can make the following operation:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3]$.
The product of all elements is $3$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
|
for _ in range(int(input())):
n = int(input())
arr = list(map(int, input().split()))
ones = arr.count(1)
if ones == 0:
product = 1
for num in arr:
product = product * num % 998244353
print(product % 998244353)
else:
arr.sort()
product = 1
for num in arr:
if num & 1 == 0 and ones > 0:
product = product * (num + 1) % 998244353
ones -= 1
else:
product = product * num % 998244353
print(product % 998244353)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
|
Chef has an array A of length N.
He can modify this array by applying a special operation any number of times. In one operation, he can:
Select two indices i and j (1β€ i < j β€ |A|).
Append A_{i} \oplus A_{j} to the end of the array, where \oplus denotes the [bitwise XOR operation]
Remove A_{i} and A_{j} from the array.
Chef wants to maximize the product of all the elements of the array after applying these operations.
Help Chef determine the maximum product he can achieve by applying this operation any (possibly zero) number of times. As this number can be large, print it modulo 998244353.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains one integer N β the number of elements in the array.
- The second line consists of N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the elements of the array initially.
------ Output Format ------
For each test case, output the maximum product he can achieve modulo 998244353.
------ Constraints ------
$1 β€ T β€ 5\cdot 10^{4}$
$2 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $3\cdot 10^{5}$.
----- Sample Input 1 ------
3
4
1 2 1 2
2
3 3
2
2 1
----- Sample Output 1 ------
9
9
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[1, 2, 3]$.
- Operation $2$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3, 3]$.
The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Test case $2$: The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Thus, Chef does not need to perform any operations.
Test case $3$: Chef can make the following operation:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3]$.
The product of all elements is $3$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
|
t = int(input())
while t != 0:
m = 998244353
n = int(input())
a = list(map(int, input().split()))
count1 = a.count(1)
a.sort()
u = 1
for i in range(n):
if a[i] % 2 == 0 and count1 > 0:
a[i] += 1
u = u * a[i] % m
count1 -= 1
else:
u = u * a[i] % m
print(u % m)
t -= 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER
|
Chef has an array A of length N.
He can modify this array by applying a special operation any number of times. In one operation, he can:
Select two indices i and j (1β€ i < j β€ |A|).
Append A_{i} \oplus A_{j} to the end of the array, where \oplus denotes the [bitwise XOR operation]
Remove A_{i} and A_{j} from the array.
Chef wants to maximize the product of all the elements of the array after applying these operations.
Help Chef determine the maximum product he can achieve by applying this operation any (possibly zero) number of times. As this number can be large, print it modulo 998244353.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains one integer N β the number of elements in the array.
- The second line consists of N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the elements of the array initially.
------ Output Format ------
For each test case, output the maximum product he can achieve modulo 998244353.
------ Constraints ------
$1 β€ T β€ 5\cdot 10^{4}$
$2 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $3\cdot 10^{5}$.
----- Sample Input 1 ------
3
4
1 2 1 2
2
3 3
2
2 1
----- Sample Output 1 ------
9
9
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[1, 2, 3]$.
- Operation $2$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3, 3]$.
The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Test case $2$: The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Thus, Chef does not need to perform any operations.
Test case $3$: Chef can make the following operation:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3]$.
The product of all elements is $3$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
|
m = 998244353
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
p = 1
ones = 0
one_inv = []
for e in a:
if e > 1:
if e & 1 == 0:
one_inv.append(e)
else:
p *= e
p %= m
elif e == 1:
ones += 1
one_inv.sort()
for e in one_inv[:ones]:
p *= e + 1
p %= m
for e in one_inv[ones:]:
p *= e
p %= m
print(p)
|
ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Chef has an array A of length N.
He can modify this array by applying a special operation any number of times. In one operation, he can:
Select two indices i and j (1β€ i < j β€ |A|).
Append A_{i} \oplus A_{j} to the end of the array, where \oplus denotes the [bitwise XOR operation]
Remove A_{i} and A_{j} from the array.
Chef wants to maximize the product of all the elements of the array after applying these operations.
Help Chef determine the maximum product he can achieve by applying this operation any (possibly zero) number of times. As this number can be large, print it modulo 998244353.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains one integer N β the number of elements in the array.
- The second line consists of N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the elements of the array initially.
------ Output Format ------
For each test case, output the maximum product he can achieve modulo 998244353.
------ Constraints ------
$1 β€ T β€ 5\cdot 10^{4}$
$2 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $3\cdot 10^{5}$.
----- Sample Input 1 ------
3
4
1 2 1 2
2
3 3
2
2 1
----- Sample Output 1 ------
9
9
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[1, 2, 3]$.
- Operation $2$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3, 3]$.
The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Test case $2$: The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Thus, Chef does not need to perform any operations.
Test case $3$: Chef can make the following operation:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3]$.
The product of all elements is $3$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
|
for _ in range(int(input())):
n = int(input())
t = input().split()
lis = []
one_count = 0
for i in t:
if i == "1":
one_count += 1
lis.append(int(i))
lis.sort()
res = 1
start = one_count
for i in range(start, n):
if one_count and lis[i] % 2 == 0:
res = res * (lis[i] ^ 1) % 998244353
one_count -= 1
else:
res = res * lis[i] % 998244353
print(res)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Chef has an array A of length N.
He can modify this array by applying a special operation any number of times. In one operation, he can:
Select two indices i and j (1β€ i < j β€ |A|).
Append A_{i} \oplus A_{j} to the end of the array, where \oplus denotes the [bitwise XOR operation]
Remove A_{i} and A_{j} from the array.
Chef wants to maximize the product of all the elements of the array after applying these operations.
Help Chef determine the maximum product he can achieve by applying this operation any (possibly zero) number of times. As this number can be large, print it modulo 998244353.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains one integer N β the number of elements in the array.
- The second line consists of N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the elements of the array initially.
------ Output Format ------
For each test case, output the maximum product he can achieve modulo 998244353.
------ Constraints ------
$1 β€ T β€ 5\cdot 10^{4}$
$2 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $3\cdot 10^{5}$.
----- Sample Input 1 ------
3
4
1 2 1 2
2
3 3
2
2 1
----- Sample Output 1 ------
9
9
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[1, 2, 3]$.
- Operation $2$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3, 3]$.
The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Test case $2$: The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Thus, Chef does not need to perform any operations.
Test case $3$: Chef can make the following operation:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3]$.
The product of all elements is $3$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
|
t = int(input())
for i in range(t):
n = int(input())
l = list([int(x) for x in input().split()])
oc = 0
ll = []
for i in l:
if i > 0:
if i == 1:
oc = oc + 1
else:
ll.append(i)
ll = sorted(ll)
for i in range(0, len(ll)):
if oc == 0:
break
if ll[i] % 2 == 0:
ll[i] = ll[i] + 1
oc = oc - 1
ans = 1
for i in ll:
ans = ans * i % 998244353
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Chef has an array A of length N.
He can modify this array by applying a special operation any number of times. In one operation, he can:
Select two indices i and j (1β€ i < j β€ |A|).
Append A_{i} \oplus A_{j} to the end of the array, where \oplus denotes the [bitwise XOR operation]
Remove A_{i} and A_{j} from the array.
Chef wants to maximize the product of all the elements of the array after applying these operations.
Help Chef determine the maximum product he can achieve by applying this operation any (possibly zero) number of times. As this number can be large, print it modulo 998244353.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains one integer N β the number of elements in the array.
- The second line consists of N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the elements of the array initially.
------ Output Format ------
For each test case, output the maximum product he can achieve modulo 998244353.
------ Constraints ------
$1 β€ T β€ 5\cdot 10^{4}$
$2 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $3\cdot 10^{5}$.
----- Sample Input 1 ------
3
4
1 2 1 2
2
3 3
2
2 1
----- Sample Output 1 ------
9
9
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[1, 2, 3]$.
- Operation $2$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3, 3]$.
The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Test case $2$: The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Thus, Chef does not need to perform any operations.
Test case $3$: Chef can make the following operation:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3]$.
The product of all elements is $3$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
|
for i in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
a.sort()
b = []
for i in a:
if i == 1:
b.append(1)
else:
break
for i in b:
del a[0]
if len(a) == 0:
print(1)
continue
a.sort(reverse=True, key=lambda x: (1 ^ x) / x)
for i in range(min(len(b), len(a))):
if 1 ^ a[i] < a[i]:
break
a[i] = 1 ^ a[i]
v = 1
for i in a:
v = v * i % 998244353
print(v)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Chef has an array A of length N.
He can modify this array by applying a special operation any number of times. In one operation, he can:
Select two indices i and j (1β€ i < j β€ |A|).
Append A_{i} \oplus A_{j} to the end of the array, where \oplus denotes the [bitwise XOR operation]
Remove A_{i} and A_{j} from the array.
Chef wants to maximize the product of all the elements of the array after applying these operations.
Help Chef determine the maximum product he can achieve by applying this operation any (possibly zero) number of times. As this number can be large, print it modulo 998244353.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains one integer N β the number of elements in the array.
- The second line consists of N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the elements of the array initially.
------ Output Format ------
For each test case, output the maximum product he can achieve modulo 998244353.
------ Constraints ------
$1 β€ T β€ 5\cdot 10^{4}$
$2 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $3\cdot 10^{5}$.
----- Sample Input 1 ------
3
4
1 2 1 2
2
3 3
2
2 1
----- Sample Output 1 ------
9
9
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[1, 2, 3]$.
- Operation $2$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3, 3]$.
The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Test case $2$: The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Thus, Chef does not need to perform any operations.
Test case $3$: Chef can make the following operation:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3]$.
The product of all elements is $3$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
|
t = int(input())
for p in range(t):
temp = 0
count = 0
l = []
m = []
n = int(input())
a = input()
a = a.split(" ")
for i in range(n):
a[i] = int(a[i])
if a[i] == 1:
count += 1
if a[i] % 2 == 0:
m.append(a[i])
else:
l.append(a[i])
m.sort()
k = len(m)
while count > 0 and k > 0:
m[temp] = m[temp] + 1
count = count - 1
temp = temp + 1
k = k - 1
l.extend(m)
prod = 1
for i in range(n):
prod = prod * l[i] % 998244353
print(prod)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Chef has an array A of length N.
He can modify this array by applying a special operation any number of times. In one operation, he can:
Select two indices i and j (1β€ i < j β€ |A|).
Append A_{i} \oplus A_{j} to the end of the array, where \oplus denotes the [bitwise XOR operation]
Remove A_{i} and A_{j} from the array.
Chef wants to maximize the product of all the elements of the array after applying these operations.
Help Chef determine the maximum product he can achieve by applying this operation any (possibly zero) number of times. As this number can be large, print it modulo 998244353.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains one integer N β the number of elements in the array.
- The second line consists of N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the elements of the array initially.
------ Output Format ------
For each test case, output the maximum product he can achieve modulo 998244353.
------ Constraints ------
$1 β€ T β€ 5\cdot 10^{4}$
$2 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $3\cdot 10^{5}$.
----- Sample Input 1 ------
3
4
1 2 1 2
2
3 3
2
2 1
----- Sample Output 1 ------
9
9
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[1, 2, 3]$.
- Operation $2$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3, 3]$.
The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Test case $2$: The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Thus, Chef does not need to perform any operations.
Test case $3$: Chef can make the following operation:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3]$.
The product of all elements is $3$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
|
def solve(arr):
ones = arr.count(1)
temp = []
for i, x in enumerate(arr):
if x & 1 == 0:
temp.append([x, i])
temp.sort()
for i in range(min(ones, len(temp))):
arr[temp[i][1]] = 1
arr.append(temp[i][0] + 1)
mod = 998244353
res = 1
for i in arr:
res = res * i % mod
return res
t = int(input())
while t:
n = int(input())
arr = [int(i) for i in str(input()).split()]
print(solve(arr))
t -= 1
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER
|
Chef has an array A of length N.
He can modify this array by applying a special operation any number of times. In one operation, he can:
Select two indices i and j (1β€ i < j β€ |A|).
Append A_{i} \oplus A_{j} to the end of the array, where \oplus denotes the [bitwise XOR operation]
Remove A_{i} and A_{j} from the array.
Chef wants to maximize the product of all the elements of the array after applying these operations.
Help Chef determine the maximum product he can achieve by applying this operation any (possibly zero) number of times. As this number can be large, print it modulo 998244353.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains one integer N β the number of elements in the array.
- The second line consists of N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the elements of the array initially.
------ Output Format ------
For each test case, output the maximum product he can achieve modulo 998244353.
------ Constraints ------
$1 β€ T β€ 5\cdot 10^{4}$
$2 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $3\cdot 10^{5}$.
----- Sample Input 1 ------
3
4
1 2 1 2
2
3 3
2
2 1
----- Sample Output 1 ------
9
9
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[1, 2, 3]$.
- Operation $2$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3, 3]$.
The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Test case $2$: The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Thus, Chef does not need to perform any operations.
Test case $3$: Chef can make the following operation:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3]$.
The product of all elements is $3$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
|
n = int(input())
for i in range(n):
le = int(input())
arr = [int(x) for x in input().split()]
one = []
even = []
prod = 1
for k in range(le):
if arr[k] == 1:
one.append(1)
elif arr[k] % 2 == 1:
prod = prod * arr[k] % 998244353
else:
even.append(arr[k])
l = len(one)
e = len(even)
if l == 0:
for k in range(e):
prod = prod * even[k] % 998244353
print(prod)
elif l >= e:
for k in range(e):
prod = prod * (even[k] + 1) % 998244353
print(prod)
else:
even.sort()
for k in range(l):
prod = prod * (even[k] + 1) % 998244353
for k in range(l, e):
prod = prod * even[k] % 998244353
print(prod)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Chef has an array A of length N.
He can modify this array by applying a special operation any number of times. In one operation, he can:
Select two indices i and j (1β€ i < j β€ |A|).
Append A_{i} \oplus A_{j} to the end of the array, where \oplus denotes the [bitwise XOR operation]
Remove A_{i} and A_{j} from the array.
Chef wants to maximize the product of all the elements of the array after applying these operations.
Help Chef determine the maximum product he can achieve by applying this operation any (possibly zero) number of times. As this number can be large, print it modulo 998244353.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains one integer N β the number of elements in the array.
- The second line consists of N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the elements of the array initially.
------ Output Format ------
For each test case, output the maximum product he can achieve modulo 998244353.
------ Constraints ------
$1 β€ T β€ 5\cdot 10^{4}$
$2 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $3\cdot 10^{5}$.
----- Sample Input 1 ------
3
4
1 2 1 2
2
3 3
2
2 1
----- Sample Output 1 ------
9
9
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[1, 2, 3]$.
- Operation $2$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3, 3]$.
The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Test case $2$: The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Thus, Chef does not need to perform any operations.
Test case $3$: Chef can make the following operation:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3]$.
The product of all elements is $3$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
|
for ii in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
e, o = [], []
for i in range(n):
if a[i] % 2 == 0:
e.append(a[i])
else:
o.append(a[i])
x = 0
for i in range(n):
if a[i] == 1:
x += 1
a.sort()
ans = 1
for i in range(n):
if x == 0:
break
if a[i] % 2 == 0:
a[i] += 1
x -= 1
for i in range(n):
ans = ans * a[i] % 998244353
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Chef has an array A of length N.
He can modify this array by applying a special operation any number of times. In one operation, he can:
Select two indices i and j (1β€ i < j β€ |A|).
Append A_{i} \oplus A_{j} to the end of the array, where \oplus denotes the [bitwise XOR operation]
Remove A_{i} and A_{j} from the array.
Chef wants to maximize the product of all the elements of the array after applying these operations.
Help Chef determine the maximum product he can achieve by applying this operation any (possibly zero) number of times. As this number can be large, print it modulo 998244353.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains one integer N β the number of elements in the array.
- The second line consists of N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the elements of the array initially.
------ Output Format ------
For each test case, output the maximum product he can achieve modulo 998244353.
------ Constraints ------
$1 β€ T β€ 5\cdot 10^{4}$
$2 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $3\cdot 10^{5}$.
----- Sample Input 1 ------
3
4
1 2 1 2
2
3 3
2
2 1
----- Sample Output 1 ------
9
9
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[1, 2, 3]$.
- Operation $2$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3, 3]$.
The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Test case $2$: The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Thus, Chef does not need to perform any operations.
Test case $3$: Chef can make the following operation:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3]$.
The product of all elements is $3$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
|
t = int(input())
while t > 0:
t -= 1
n = int(input())
a = list(map(int, input().split()))
even = [i for i in a if i % 2 == 0]
odd = [j for j in a if j % 2 == 1 and j != 1]
even.sort()
one = a.count(1)
ans = 1
for i in even:
if one != 0:
ans = ans * (i + 1)
ans %= 998244353
one -= 1
else:
ans = ans * i
ans %= 998244353
for j in odd:
if j != 1:
ans = ans * j % 998244353
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Chef has an array A of length N.
He can modify this array by applying a special operation any number of times. In one operation, he can:
Select two indices i and j (1β€ i < j β€ |A|).
Append A_{i} \oplus A_{j} to the end of the array, where \oplus denotes the [bitwise XOR operation]
Remove A_{i} and A_{j} from the array.
Chef wants to maximize the product of all the elements of the array after applying these operations.
Help Chef determine the maximum product he can achieve by applying this operation any (possibly zero) number of times. As this number can be large, print it modulo 998244353.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains one integer N β the number of elements in the array.
- The second line consists of N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the elements of the array initially.
------ Output Format ------
For each test case, output the maximum product he can achieve modulo 998244353.
------ Constraints ------
$1 β€ T β€ 5\cdot 10^{4}$
$2 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $3\cdot 10^{5}$.
----- Sample Input 1 ------
3
4
1 2 1 2
2
3 3
2
2 1
----- Sample Output 1 ------
9
9
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[1, 2, 3]$.
- Operation $2$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3, 3]$.
The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Test case $2$: The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Thus, Chef does not need to perform any operations.
Test case $3$: Chef can make the following operation:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3]$.
The product of all elements is $3$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
|
for t in range(int(input())):
l = int(input())
l1 = [int(i) for i in input().split()]
l1.sort()
c = 0
mod = 998244353
c = l1.count(1)
ans = 1
for i in range(c, l):
if l1[i] % 2 == 0 and c > 0:
l1[i] += 1
c -= 1
if c <= 0:
break
if l == 0:
ans = 0
else:
for k in range(l):
ans = ans * l1[k] % 998244353
print(ans % 998244353)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
|
Chef has an array A of length N.
He can modify this array by applying a special operation any number of times. In one operation, he can:
Select two indices i and j (1β€ i < j β€ |A|).
Append A_{i} \oplus A_{j} to the end of the array, where \oplus denotes the [bitwise XOR operation]
Remove A_{i} and A_{j} from the array.
Chef wants to maximize the product of all the elements of the array after applying these operations.
Help Chef determine the maximum product he can achieve by applying this operation any (possibly zero) number of times. As this number can be large, print it modulo 998244353.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains one integer N β the number of elements in the array.
- The second line consists of N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the elements of the array initially.
------ Output Format ------
For each test case, output the maximum product he can achieve modulo 998244353.
------ Constraints ------
$1 β€ T β€ 5\cdot 10^{4}$
$2 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $3\cdot 10^{5}$.
----- Sample Input 1 ------
3
4
1 2 1 2
2
3 3
2
2 1
----- Sample Output 1 ------
9
9
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[1, 2, 3]$.
- Operation $2$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3, 3]$.
The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Test case $2$: The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Thus, Chef does not need to perform any operations.
Test case $3$: Chef can make the following operation:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3]$.
The product of all elements is $3$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
|
mod = 998244353
for _ in range(int(input())):
n = int(input())
l = list(map(int, input().split(" ")))
l.sort()
ones = l.count(1)
ans = 1
x = 0
len_even = 0
while x < n:
if l[x] % 2 == 0 and len_even < ones:
ans *= l[x] ^ 1 % mod
len_even += 1
else:
ans *= l[x] % mod
ans %= mod
x += 1
print(ans)
|
ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR VAR BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Chef has an array A of length N.
He can modify this array by applying a special operation any number of times. In one operation, he can:
Select two indices i and j (1β€ i < j β€ |A|).
Append A_{i} \oplus A_{j} to the end of the array, where \oplus denotes the [bitwise XOR operation]
Remove A_{i} and A_{j} from the array.
Chef wants to maximize the product of all the elements of the array after applying these operations.
Help Chef determine the maximum product he can achieve by applying this operation any (possibly zero) number of times. As this number can be large, print it modulo 998244353.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains one integer N β the number of elements in the array.
- The second line consists of N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the elements of the array initially.
------ Output Format ------
For each test case, output the maximum product he can achieve modulo 998244353.
------ Constraints ------
$1 β€ T β€ 5\cdot 10^{4}$
$2 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $3\cdot 10^{5}$.
----- Sample Input 1 ------
3
4
1 2 1 2
2
3 3
2
2 1
----- Sample Output 1 ------
9
9
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[1, 2, 3]$.
- Operation $2$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3, 3]$.
The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Test case $2$: The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Thus, Chef does not need to perform any operations.
Test case $3$: Chef can make the following operation:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3]$.
The product of all elements is $3$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
|
MOD = 998244353
T = int(input())
for _ in range(T):
N = int(input())
A = list(map(int, input().split()))
odd, even = [], []
for i in A:
if i % 2 == 1:
odd.append(i)
else:
even.append(i)
odd.sort()
even.sort()
i, j = 0, 0
while i < len(odd) and j < len(even):
if odd[i] > 1:
break
even[j] += 1
i += 1
j += 1
product = 1
for i in odd:
product = product * i % MOD
for i in even:
product = product * i % MOD
print(product)
|
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Chef has an array A of length N.
He can modify this array by applying a special operation any number of times. In one operation, he can:
Select two indices i and j (1β€ i < j β€ |A|).
Append A_{i} \oplus A_{j} to the end of the array, where \oplus denotes the [bitwise XOR operation]
Remove A_{i} and A_{j} from the array.
Chef wants to maximize the product of all the elements of the array after applying these operations.
Help Chef determine the maximum product he can achieve by applying this operation any (possibly zero) number of times. As this number can be large, print it modulo 998244353.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains one integer N β the number of elements in the array.
- The second line consists of N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the elements of the array initially.
------ Output Format ------
For each test case, output the maximum product he can achieve modulo 998244353.
------ Constraints ------
$1 β€ T β€ 5\cdot 10^{4}$
$2 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $3\cdot 10^{5}$.
----- Sample Input 1 ------
3
4
1 2 1 2
2
3 3
2
2 1
----- Sample Output 1 ------
9
9
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[1, 2, 3]$.
- Operation $2$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3, 3]$.
The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Test case $2$: The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Thus, Chef does not need to perform any operations.
Test case $3$: Chef can make the following operation:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3]$.
The product of all elements is $3$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
|
t = int(input())
while t > 0:
t -= 1
n = int(input())
a = sorted(list(map(int, input().split())))
m = 998244353
c = a.count(1)
res = 1
for i in a:
if c > 0 and i % 2 == 0:
i += 1
c -= 1
res = res * i % m
print(res)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Chef has an array A of length N.
He can modify this array by applying a special operation any number of times. In one operation, he can:
Select two indices i and j (1β€ i < j β€ |A|).
Append A_{i} \oplus A_{j} to the end of the array, where \oplus denotes the [bitwise XOR operation]
Remove A_{i} and A_{j} from the array.
Chef wants to maximize the product of all the elements of the array after applying these operations.
Help Chef determine the maximum product he can achieve by applying this operation any (possibly zero) number of times. As this number can be large, print it modulo 998244353.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains one integer N β the number of elements in the array.
- The second line consists of N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the elements of the array initially.
------ Output Format ------
For each test case, output the maximum product he can achieve modulo 998244353.
------ Constraints ------
$1 β€ T β€ 5\cdot 10^{4}$
$2 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $3\cdot 10^{5}$.
----- Sample Input 1 ------
3
4
1 2 1 2
2
3 3
2
2 1
----- Sample Output 1 ------
9
9
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[1, 2, 3]$.
- Operation $2$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3, 3]$.
The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Test case $2$: The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Thus, Chef does not need to perform any operations.
Test case $3$: Chef can make the following operation:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3]$.
The product of all elements is $3$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
|
t = int(input())
for i in range(t):
n = int(input())
arr = [int(x) for x in input().split()]
p = 1
c = 0
b = []
m = 998244353
for i in arr:
if i == 1:
c += 1
elif i & 1:
p *= i
p = p % m
else:
b.append(i)
b.sort()
for i in b:
if c:
p *= i ^ 1
c -= 1
else:
p *= i
p = p % m
print(p)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
Chef has an array A of length N.
He can modify this array by applying a special operation any number of times. In one operation, he can:
Select two indices i and j (1β€ i < j β€ |A|).
Append A_{i} \oplus A_{j} to the end of the array, where \oplus denotes the [bitwise XOR operation]
Remove A_{i} and A_{j} from the array.
Chef wants to maximize the product of all the elements of the array after applying these operations.
Help Chef determine the maximum product he can achieve by applying this operation any (possibly zero) number of times. As this number can be large, print it modulo 998244353.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains one integer N β the number of elements in the array.
- The second line consists of N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the elements of the array initially.
------ Output Format ------
For each test case, output the maximum product he can achieve modulo 998244353.
------ Constraints ------
$1 β€ T β€ 5\cdot 10^{4}$
$2 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $3\cdot 10^{5}$.
----- Sample Input 1 ------
3
4
1 2 1 2
2
3 3
2
2 1
----- Sample Output 1 ------
9
9
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[1, 2, 3]$.
- Operation $2$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3, 3]$.
The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Test case $2$: The product of all elements of the array is $3\times 3 = 9$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
Thus, Chef does not need to perform any operations.
Test case $3$: Chef can make the following operation:
- Operation $1$: Choose $i = 1$ and $j = 2$, append $A_{1}\oplus A_{2} = 1\oplus 2 = 3$ to the end of the array and remove elements $A_{1}$ and $A_{2}$. Thus, the array becomes $[3]$.
The product of all elements is $3$. It can be shown that this is the maximum product that can be obtained by applying any number of operations on the array.
|
m = 998244353
t = int(input())
for tt in range(t):
n = int(input())
a = list(map(int, input().split()))
q = a.count(1)
a.sort()
ans = 1
for r in a:
if r & 1 == 0 and q != 0:
ans = ans * ((r + 1) % m) % m
q -= 1
else:
ans = ans * (r % m) % m
print(ans)
|
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
You are the head of a large enterprise. $n$ people work at you, and $n$ is odd (i. e. $n$ is not divisible by $2$).
You have to distribute salaries to your employees. Initially, you have $s$ dollars for it, and the $i$-th employee should get a salary from $l_i$ to $r_i$ dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: the median of the sequence $[5, 1, 10, 17, 6]$ is $6$, the median of the sequence $[1, 2, 1]$ is $1$.
It is guaranteed that you have enough money to pay the minimum salary, i.e $l_1 + l_2 + \dots + l_n \le s$.
Note that you don't have to spend all your $s$ dollars on salaries.
You have to answer $t$ test cases.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 2 \cdot 10^5$) β the number of test cases.
The first line of each query contains two integers $n$ and $s$ ($1 \le n < 2 \cdot 10^5$, $1 \le s \le 2 \cdot 10^{14}$) β the number of employees and the amount of money you have. The value $n$ is not divisible by $2$.
The following $n$ lines of each query contain the information about employees. The $i$-th line contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le 10^9$).
It is guaranteed that the sum of all $n$ over all queries does not exceed $2 \cdot 10^5$.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. $\sum\limits_{i=1}^{n} l_i \le s$.
-----Output-----
For each test case print one integer β the maximum median salary that you can obtain.
-----Example-----
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
-----Note-----
In the first test case, you can distribute salaries as follows: $sal_1 = 12, sal_2 = 2, sal_3 = 11$ ($sal_i$ is the salary of the $i$-th employee). Then the median salary is $11$.
In the second test case, you have to pay $1337$ dollars to the only employee.
In the third test case, you can distribute salaries as follows: $sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7$. Then the median salary is $6$.
|
import sys
def main():
t = int(input())
allans = []
for _ in range(t):
n, s = readIntArr()
lr = []
for __ in range(n):
lr.append(readIntArr())
lr.sort(key=lambda x: x[0])
guess = lr[n // 2][0]
def isPossible(median):
lowerCnt = n // 2
upperCnt = n - lowerCnt
lower = []
upper = []
unsure = []
for l, r in lr:
if r < median:
lower.append([l, r])
elif l >= median:
upper.append([l, r])
else:
unsure.append([l, r])
if len(lower) > lowerCnt:
return False
elif len(upper) > upperCnt:
assert False
unsure.sort(key=lambda x: x[0])
for i in range(len(unsure)):
if len(lower) < lowerCnt:
lower.append(unsure[i])
else:
upper.append(unsure[i])
minRequiredS = 0
for l, r in lower:
minRequiredS += l
for l, r in upper:
minRequiredS += max(l, median)
return s >= minRequiredS
b = s
while b > 0:
while isPossible(guess + b):
guess += b
b //= 2
allans.append(guess)
multiLineArrayPrint(allans)
return
input = sys.stdin.buffer.readline
def oneLineArrayPrint(arr):
print(" ".join([str(x) for x in arr]))
def multiLineArrayPrint(arr):
print("\n".join([str(x) for x in arr]))
def multiLineArrayOfArraysPrint(arr):
print("\n".join([" ".join([str(x) for x in y]) for y in arr]))
def readIntArr():
return [int(x) for x in input().split()]
def makeArr(defaultVal, dimensionArr):
dv = defaultVal
da = dimensionArr
if len(da) == 1:
return [dv for _ in range(da[0])]
else:
return [makeArr(dv, da[1:]) for _ in range(da[0])]
def queryInteractive(x, y):
print("? {} {}".format(x, y))
sys.stdout.flush()
return int(input())
def answerInteractive(ans):
print("! {}".format(ans))
sys.stdout.flush()
inf = float("inf")
MOD = 10**9 + 7
for _abc in range(1):
main()
|
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR IF VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR FOR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER WHILE FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
|
You are the head of a large enterprise. $n$ people work at you, and $n$ is odd (i. e. $n$ is not divisible by $2$).
You have to distribute salaries to your employees. Initially, you have $s$ dollars for it, and the $i$-th employee should get a salary from $l_i$ to $r_i$ dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: the median of the sequence $[5, 1, 10, 17, 6]$ is $6$, the median of the sequence $[1, 2, 1]$ is $1$.
It is guaranteed that you have enough money to pay the minimum salary, i.e $l_1 + l_2 + \dots + l_n \le s$.
Note that you don't have to spend all your $s$ dollars on salaries.
You have to answer $t$ test cases.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 2 \cdot 10^5$) β the number of test cases.
The first line of each query contains two integers $n$ and $s$ ($1 \le n < 2 \cdot 10^5$, $1 \le s \le 2 \cdot 10^{14}$) β the number of employees and the amount of money you have. The value $n$ is not divisible by $2$.
The following $n$ lines of each query contain the information about employees. The $i$-th line contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le 10^9$).
It is guaranteed that the sum of all $n$ over all queries does not exceed $2 \cdot 10^5$.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. $\sum\limits_{i=1}^{n} l_i \le s$.
-----Output-----
For each test case print one integer β the maximum median salary that you can obtain.
-----Example-----
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
-----Note-----
In the first test case, you can distribute salaries as follows: $sal_1 = 12, sal_2 = 2, sal_3 = 11$ ($sal_i$ is the salary of the $i$-th employee). Then the median salary is $11$.
In the second test case, you have to pay $1337$ dollars to the only employee.
In the third test case, you can distribute salaries as follows: $sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7$. Then the median salary is $6$.
|
import sys
input = sys.stdin.readline
def judge(x):
salary = 0
left_cnt, right_cnt = 0, 0
arr = []
for li, ri in lr:
if ri < x:
salary += li
left_cnt += 1
elif x < li:
salary += li
right_cnt += 1
else:
arr.append(li)
arr.sort()
salary += sum(arr[: (n - 1) // 2 - left_cnt]) + (1 + (n - 1) // 2 - right_cnt) * x
return salary <= s
def binary_search(l, r):
while l <= r:
mid = (l + r) // 2
if judge(mid):
l = mid + 1
else:
r = mid - 1
return r
t = int(input())
for _ in range(t):
n, s = map(int, input().split())
lr = [tuple(map(int, input().split())) for _ in range(n)]
l = [li for li, _ in lr]
r = [ri for _, ri in lr]
l.sort()
r.sort()
print(binary_search(l[n // 2], r[n // 2]))
|
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR RETURN VAR VAR FUNC_DEF WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER
|
You are the head of a large enterprise. $n$ people work at you, and $n$ is odd (i. e. $n$ is not divisible by $2$).
You have to distribute salaries to your employees. Initially, you have $s$ dollars for it, and the $i$-th employee should get a salary from $l_i$ to $r_i$ dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: the median of the sequence $[5, 1, 10, 17, 6]$ is $6$, the median of the sequence $[1, 2, 1]$ is $1$.
It is guaranteed that you have enough money to pay the minimum salary, i.e $l_1 + l_2 + \dots + l_n \le s$.
Note that you don't have to spend all your $s$ dollars on salaries.
You have to answer $t$ test cases.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 2 \cdot 10^5$) β the number of test cases.
The first line of each query contains two integers $n$ and $s$ ($1 \le n < 2 \cdot 10^5$, $1 \le s \le 2 \cdot 10^{14}$) β the number of employees and the amount of money you have. The value $n$ is not divisible by $2$.
The following $n$ lines of each query contain the information about employees. The $i$-th line contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le 10^9$).
It is guaranteed that the sum of all $n$ over all queries does not exceed $2 \cdot 10^5$.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. $\sum\limits_{i=1}^{n} l_i \le s$.
-----Output-----
For each test case print one integer β the maximum median salary that you can obtain.
-----Example-----
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
-----Note-----
In the first test case, you can distribute salaries as follows: $sal_1 = 12, sal_2 = 2, sal_3 = 11$ ($sal_i$ is the salary of the $i$-th employee). Then the median salary is $11$.
In the second test case, you have to pay $1337$ dollars to the only employee.
In the third test case, you can distribute salaries as follows: $sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7$. Then the median salary is $6$.
|
import sys
input1 = sys.stdin.readline
def solve():
n, s = [int(i) for i in input1().split()]
empl = [[] for i in range(n)]
for i in range(n):
empl[i] = [int(j) for j in input1().split()]
empl.sort(reverse=True)
lg = 0
rg = 10**9 + 1
while rg - lg > 1:
mg = (rg + lg) // 2
need = (n + 1) // 2
money = s
for [li, ri] in empl:
if ri >= mg and need > 0:
money -= max(mg, li)
need -= 1
else:
money -= li
if need == 0 and money >= 0:
check = True
else:
check = False
if check:
lg = mg
else:
rg = mg
print(lg)
t = int(input1())
while t > 0:
empl = []
solve()
t -= 1
|
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FOR LIST VAR VAR VAR IF VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER
|
You are the head of a large enterprise. $n$ people work at you, and $n$ is odd (i. e. $n$ is not divisible by $2$).
You have to distribute salaries to your employees. Initially, you have $s$ dollars for it, and the $i$-th employee should get a salary from $l_i$ to $r_i$ dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: the median of the sequence $[5, 1, 10, 17, 6]$ is $6$, the median of the sequence $[1, 2, 1]$ is $1$.
It is guaranteed that you have enough money to pay the minimum salary, i.e $l_1 + l_2 + \dots + l_n \le s$.
Note that you don't have to spend all your $s$ dollars on salaries.
You have to answer $t$ test cases.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 2 \cdot 10^5$) β the number of test cases.
The first line of each query contains two integers $n$ and $s$ ($1 \le n < 2 \cdot 10^5$, $1 \le s \le 2 \cdot 10^{14}$) β the number of employees and the amount of money you have. The value $n$ is not divisible by $2$.
The following $n$ lines of each query contain the information about employees. The $i$-th line contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le 10^9$).
It is guaranteed that the sum of all $n$ over all queries does not exceed $2 \cdot 10^5$.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. $\sum\limits_{i=1}^{n} l_i \le s$.
-----Output-----
For each test case print one integer β the maximum median salary that you can obtain.
-----Example-----
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
-----Note-----
In the first test case, you can distribute salaries as follows: $sal_1 = 12, sal_2 = 2, sal_3 = 11$ ($sal_i$ is the salary of the $i$-th employee). Then the median salary is $11$.
In the second test case, you have to pay $1337$ dollars to the only employee.
In the third test case, you can distribute salaries as follows: $sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7$. Then the median salary is $6$.
|
import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n, s = map(int, input().split())
lr = [list(map(int, input().split())) for i in range(n)]
ng = 10**9 + 1
ok = 0
while ng - ok > 1:
mid = (ok + ng) // 2
cnt_more = 0
ss = 0
v = []
for l, r in lr:
if mid <= l:
cnt_more += 1
ss += l
elif l < mid <= r:
v.append(l)
else:
ss += l
v.sort()
need = max(0, n // 2 + 1 - cnt_more)
if len(v) < need:
ng = mid
continue
for i in range(len(v)):
if i < len(v) - need:
ss += v[i]
else:
ss += mid
if ss <= s:
ok = mid
else:
ng = mid
print(ok)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
You are the head of a large enterprise. $n$ people work at you, and $n$ is odd (i. e. $n$ is not divisible by $2$).
You have to distribute salaries to your employees. Initially, you have $s$ dollars for it, and the $i$-th employee should get a salary from $l_i$ to $r_i$ dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: the median of the sequence $[5, 1, 10, 17, 6]$ is $6$, the median of the sequence $[1, 2, 1]$ is $1$.
It is guaranteed that you have enough money to pay the minimum salary, i.e $l_1 + l_2 + \dots + l_n \le s$.
Note that you don't have to spend all your $s$ dollars on salaries.
You have to answer $t$ test cases.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 2 \cdot 10^5$) β the number of test cases.
The first line of each query contains two integers $n$ and $s$ ($1 \le n < 2 \cdot 10^5$, $1 \le s \le 2 \cdot 10^{14}$) β the number of employees and the amount of money you have. The value $n$ is not divisible by $2$.
The following $n$ lines of each query contain the information about employees. The $i$-th line contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le 10^9$).
It is guaranteed that the sum of all $n$ over all queries does not exceed $2 \cdot 10^5$.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. $\sum\limits_{i=1}^{n} l_i \le s$.
-----Output-----
For each test case print one integer β the maximum median salary that you can obtain.
-----Example-----
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
-----Note-----
In the first test case, you can distribute salaries as follows: $sal_1 = 12, sal_2 = 2, sal_3 = 11$ ($sal_i$ is the salary of the $i$-th employee). Then the median salary is $11$.
In the second test case, you have to pay $1337$ dollars to the only employee.
In the third test case, you can distribute salaries as follows: $sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7$. Then the median salary is $6$.
|
from sys import setrecursionlimit as SRL
from sys import stdin
SRL(10**7)
rd = stdin.readline
rrd = lambda: map(int, rd().strip().split())
q = int(input())
lr = []
def check(x, s):
cnt = 0
tt = []
xcnt = 0
for l, r in lr[::-1]:
if l > x:
tt.append(l)
cnt += 1
elif r >= x and l <= x and cnt < n // 2 + 1:
cnt += 1
xcnt += 1
else:
tt.append(l)
if cnt == n // 2 + 1 and x * xcnt + sum(tt) <= s:
return True
return False
while q:
n, s = rrd()
lr = []
for i in range(n):
l, r = rrd()
lr.append([l, r])
lr.sort()
l = lr[n // 2][0]
r = s + 1
while l < r:
m = (l + r) // 2
if check(m, s):
l = m + 1
else:
r = m
print(r - 1)
q -= 1
|
EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER
|
You are the head of a large enterprise. $n$ people work at you, and $n$ is odd (i. e. $n$ is not divisible by $2$).
You have to distribute salaries to your employees. Initially, you have $s$ dollars for it, and the $i$-th employee should get a salary from $l_i$ to $r_i$ dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: the median of the sequence $[5, 1, 10, 17, 6]$ is $6$, the median of the sequence $[1, 2, 1]$ is $1$.
It is guaranteed that you have enough money to pay the minimum salary, i.e $l_1 + l_2 + \dots + l_n \le s$.
Note that you don't have to spend all your $s$ dollars on salaries.
You have to answer $t$ test cases.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 2 \cdot 10^5$) β the number of test cases.
The first line of each query contains two integers $n$ and $s$ ($1 \le n < 2 \cdot 10^5$, $1 \le s \le 2 \cdot 10^{14}$) β the number of employees and the amount of money you have. The value $n$ is not divisible by $2$.
The following $n$ lines of each query contain the information about employees. The $i$-th line contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le 10^9$).
It is guaranteed that the sum of all $n$ over all queries does not exceed $2 \cdot 10^5$.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. $\sum\limits_{i=1}^{n} l_i \le s$.
-----Output-----
For each test case print one integer β the maximum median salary that you can obtain.
-----Example-----
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
-----Note-----
In the first test case, you can distribute salaries as follows: $sal_1 = 12, sal_2 = 2, sal_3 = 11$ ($sal_i$ is the salary of the $i$-th employee). Then the median salary is $11$.
In the second test case, you have to pay $1337$ dollars to the only employee.
In the third test case, you can distribute salaries as follows: $sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7$. Then the median salary is $6$.
|
import sys
def check(mid):
x = []
y = []
z = []
for i in it:
if i[1] < mid:
x.append(i)
elif i[0] >= mid:
y.append(i)
else:
z.append(i)
co = sum([i[0] for i in x])
co += sum([i[0] for i in y])
ll = len(y)
m = 0
ne = max(0, (n + 1) // 2 - ll)
if ne > len(z):
return False
j = 0
for i in z:
if j < len(z) - ne:
co += i[0]
else:
co += mid
j += 1
return co <= s
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n, s = list(map(int, input().split()))
it = []
for i in range(n):
it.append(list(map(int, input().split())))
it.sort()
l = 1
r = max(it, key=lambda x: x[1])[1] + 1
while abs(l - r) >= 2:
mid = (l + r) // 2
st = check(mid)
if st:
l = mid
else:
r = mid
ma = l
for i in range(l, r + 1):
st = check(i)
if st:
ma = max(ma, i)
print(ma)
|
IMPORT FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER WHILE FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
You are the head of a large enterprise. $n$ people work at you, and $n$ is odd (i. e. $n$ is not divisible by $2$).
You have to distribute salaries to your employees. Initially, you have $s$ dollars for it, and the $i$-th employee should get a salary from $l_i$ to $r_i$ dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: the median of the sequence $[5, 1, 10, 17, 6]$ is $6$, the median of the sequence $[1, 2, 1]$ is $1$.
It is guaranteed that you have enough money to pay the minimum salary, i.e $l_1 + l_2 + \dots + l_n \le s$.
Note that you don't have to spend all your $s$ dollars on salaries.
You have to answer $t$ test cases.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 2 \cdot 10^5$) β the number of test cases.
The first line of each query contains two integers $n$ and $s$ ($1 \le n < 2 \cdot 10^5$, $1 \le s \le 2 \cdot 10^{14}$) β the number of employees and the amount of money you have. The value $n$ is not divisible by $2$.
The following $n$ lines of each query contain the information about employees. The $i$-th line contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le 10^9$).
It is guaranteed that the sum of all $n$ over all queries does not exceed $2 \cdot 10^5$.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. $\sum\limits_{i=1}^{n} l_i \le s$.
-----Output-----
For each test case print one integer β the maximum median salary that you can obtain.
-----Example-----
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
-----Note-----
In the first test case, you can distribute salaries as follows: $sal_1 = 12, sal_2 = 2, sal_3 = 11$ ($sal_i$ is the salary of the $i$-th employee). Then the median salary is $11$.
In the second test case, you have to pay $1337$ dollars to the only employee.
In the third test case, you can distribute salaries as follows: $sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7$. Then the median salary is $6$.
|
import sys
def I():
return sys.stdin.readline().rstrip()
for _ in range(int(I())):
n, s = map(int, I().split())
half = n // 2
lows = 0
l, r = [], []
for _ in range(n):
x, y = map(int, I().split())
lows += x
l.append(x)
r.append(y)
money = 0
t = 1
while t <= 1000000000.0:
t *= 2
t //= 2
while t > 0:
people = 0
new_money = money + t
left = []
for low, high in zip(l, r):
if low <= new_money <= high:
left.append(new_money - low)
elif new_money < low:
people += 1
left.sort()
people = half + 1 - people
if people <= 0 or len(left) >= people and lows + sum(left[:people]) <= s:
money += t
t //= 2
print(money)
|
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are the head of a large enterprise. $n$ people work at you, and $n$ is odd (i. e. $n$ is not divisible by $2$).
You have to distribute salaries to your employees. Initially, you have $s$ dollars for it, and the $i$-th employee should get a salary from $l_i$ to $r_i$ dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: the median of the sequence $[5, 1, 10, 17, 6]$ is $6$, the median of the sequence $[1, 2, 1]$ is $1$.
It is guaranteed that you have enough money to pay the minimum salary, i.e $l_1 + l_2 + \dots + l_n \le s$.
Note that you don't have to spend all your $s$ dollars on salaries.
You have to answer $t$ test cases.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 2 \cdot 10^5$) β the number of test cases.
The first line of each query contains two integers $n$ and $s$ ($1 \le n < 2 \cdot 10^5$, $1 \le s \le 2 \cdot 10^{14}$) β the number of employees and the amount of money you have. The value $n$ is not divisible by $2$.
The following $n$ lines of each query contain the information about employees. The $i$-th line contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le 10^9$).
It is guaranteed that the sum of all $n$ over all queries does not exceed $2 \cdot 10^5$.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. $\sum\limits_{i=1}^{n} l_i \le s$.
-----Output-----
For each test case print one integer β the maximum median salary that you can obtain.
-----Example-----
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
-----Note-----
In the first test case, you can distribute salaries as follows: $sal_1 = 12, sal_2 = 2, sal_3 = 11$ ($sal_i$ is the salary of the $i$-th employee). Then the median salary is $11$.
In the second test case, you have to pay $1337$ dollars to the only employee.
In the third test case, you can distribute salaries as follows: $sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7$. Then the median salary is $6$.
|
import sys
input = sys.stdin.readline
t = int(input())
while t > 0:
t -= 1
n, ss = map(int, input().split())
lr = []
sl = 0
for i in range(n):
l, r = map(int, input().split())
sl += l
lr.append([l, r])
l = 0
r = 10**9 + 1
while r - l > 1:
mid = (r + l) // 2
s = []
for i in range(n):
if mid <= lr[i][1]:
s.append(max(-lr[i][0], -mid))
if len(s) < n // 2 + 1:
r = mid
continue
s.sort()
sumi = sl + (n // 2 + 1) * mid + sum(s[: n // 2 + 1])
if sumi > ss:
r = mid
else:
l = mid
print(l)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR IF FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
You are the head of a large enterprise. $n$ people work at you, and $n$ is odd (i. e. $n$ is not divisible by $2$).
You have to distribute salaries to your employees. Initially, you have $s$ dollars for it, and the $i$-th employee should get a salary from $l_i$ to $r_i$ dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: the median of the sequence $[5, 1, 10, 17, 6]$ is $6$, the median of the sequence $[1, 2, 1]$ is $1$.
It is guaranteed that you have enough money to pay the minimum salary, i.e $l_1 + l_2 + \dots + l_n \le s$.
Note that you don't have to spend all your $s$ dollars on salaries.
You have to answer $t$ test cases.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 2 \cdot 10^5$) β the number of test cases.
The first line of each query contains two integers $n$ and $s$ ($1 \le n < 2 \cdot 10^5$, $1 \le s \le 2 \cdot 10^{14}$) β the number of employees and the amount of money you have. The value $n$ is not divisible by $2$.
The following $n$ lines of each query contain the information about employees. The $i$-th line contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le 10^9$).
It is guaranteed that the sum of all $n$ over all queries does not exceed $2 \cdot 10^5$.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. $\sum\limits_{i=1}^{n} l_i \le s$.
-----Output-----
For each test case print one integer β the maximum median salary that you can obtain.
-----Example-----
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
-----Note-----
In the first test case, you can distribute salaries as follows: $sal_1 = 12, sal_2 = 2, sal_3 = 11$ ($sal_i$ is the salary of the $i$-th employee). Then the median salary is $11$.
In the second test case, you have to pay $1337$ dollars to the only employee.
In the third test case, you can distribute salaries as follows: $sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7$. Then the median salary is $6$.
|
import sys
def input():
return sys.stdin.readline().strip()
def list2d(a, b, c):
return [([c] * b) for i in range(a)]
def list3d(a, b, c, d):
return [[([d] * c) for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e):
return [[[([e] * d) for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1):
return int(-(-x // y))
def INT():
return int(input())
def MAP():
return map(int, input().split())
def LIST(N=None):
return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes():
print("Yes")
def No():
print("No")
def YES():
print("YES")
def NO():
print("NO")
sys.setrecursionlimit(10**9)
INF = float("inf")
MOD = 10**9 + 7
def bisearch_max(mn, mx, func):
ok = mn
ng = mx
while ok + 1 < ng:
mid = (ok + ng) // 2
if func(mid):
ok = mid
else:
ng = mid
return ok
def check(m):
A1, A2, A3 = [], [], []
cnt = k = 0
for l, r in LR:
if r < m:
k += l
elif m <= l:
cnt += 1
k += l
else:
A3.append((l, r))
k += l
if k > K:
return False
if cnt + len(A3) < midcnt:
return False
if cnt >= midcnt and k <= K:
return True
A3.sort(reverse=True)
for l, r in A3:
cnt += 1
k += m - l
if k > K:
return False
if cnt >= midcnt and k <= K:
return True
return False
for _ in range(INT()):
N, K = MAP()
midcnt = N // 2 + 1
LR = []
for i in range(N):
l, r = MAP()
LR.append((l, r))
res = bisearch_max(0, 10**9 + 1, check)
print(res)
|
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF NONE RETURN VAR NONE FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR LIST LIST LIST ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR RETURN NUMBER IF BIN_OP VAR FUNC_CALL VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR
|
You are the head of a large enterprise. $n$ people work at you, and $n$ is odd (i. e. $n$ is not divisible by $2$).
You have to distribute salaries to your employees. Initially, you have $s$ dollars for it, and the $i$-th employee should get a salary from $l_i$ to $r_i$ dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: the median of the sequence $[5, 1, 10, 17, 6]$ is $6$, the median of the sequence $[1, 2, 1]$ is $1$.
It is guaranteed that you have enough money to pay the minimum salary, i.e $l_1 + l_2 + \dots + l_n \le s$.
Note that you don't have to spend all your $s$ dollars on salaries.
You have to answer $t$ test cases.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 2 \cdot 10^5$) β the number of test cases.
The first line of each query contains two integers $n$ and $s$ ($1 \le n < 2 \cdot 10^5$, $1 \le s \le 2 \cdot 10^{14}$) β the number of employees and the amount of money you have. The value $n$ is not divisible by $2$.
The following $n$ lines of each query contain the information about employees. The $i$-th line contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le 10^9$).
It is guaranteed that the sum of all $n$ over all queries does not exceed $2 \cdot 10^5$.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. $\sum\limits_{i=1}^{n} l_i \le s$.
-----Output-----
For each test case print one integer β the maximum median salary that you can obtain.
-----Example-----
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
-----Note-----
In the first test case, you can distribute salaries as follows: $sal_1 = 12, sal_2 = 2, sal_3 = 11$ ($sal_i$ is the salary of the $i$-th employee). Then the median salary is $11$.
In the second test case, you have to pay $1337$ dollars to the only employee.
In the third test case, you can distribute salaries as follows: $sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7$. Then the median salary is $6$.
|
def check(x, s, a, n):
num = (n + 1) // 2
cur = 0
sum_ = 0
for i in range(n - 1, -1, -1):
l, r = a[i]
if cur == num:
break
if l >= x:
cur += 1
elif l <= x and x <= r:
cur += 1
sum_ += x - l
if cur == num and sum_ <= s:
return True
return False
q = int(input())
ans = []
for _ in range(q):
n, s = list(map(int, input().split()))
a = [list(map(int, input().split())) for _ in range(n)]
a = sorted(a, key=lambda x: x[0])
s = s - sum([l for l, r in a])
l, u = a[n // 2][0], 1000000000
while u - l > 1:
md = (u + l) // 2
if check(md, s, a, n) == True:
l = md
else:
u = md
ans.append(str(l))
print("\n".join([x for x in ans]))
|
FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR VAR
|
You are the head of a large enterprise. $n$ people work at you, and $n$ is odd (i. e. $n$ is not divisible by $2$).
You have to distribute salaries to your employees. Initially, you have $s$ dollars for it, and the $i$-th employee should get a salary from $l_i$ to $r_i$ dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: the median of the sequence $[5, 1, 10, 17, 6]$ is $6$, the median of the sequence $[1, 2, 1]$ is $1$.
It is guaranteed that you have enough money to pay the minimum salary, i.e $l_1 + l_2 + \dots + l_n \le s$.
Note that you don't have to spend all your $s$ dollars on salaries.
You have to answer $t$ test cases.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 2 \cdot 10^5$) β the number of test cases.
The first line of each query contains two integers $n$ and $s$ ($1 \le n < 2 \cdot 10^5$, $1 \le s \le 2 \cdot 10^{14}$) β the number of employees and the amount of money you have. The value $n$ is not divisible by $2$.
The following $n$ lines of each query contain the information about employees. The $i$-th line contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le 10^9$).
It is guaranteed that the sum of all $n$ over all queries does not exceed $2 \cdot 10^5$.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. $\sum\limits_{i=1}^{n} l_i \le s$.
-----Output-----
For each test case print one integer β the maximum median salary that you can obtain.
-----Example-----
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
-----Note-----
In the first test case, you can distribute salaries as follows: $sal_1 = 12, sal_2 = 2, sal_3 = 11$ ($sal_i$ is the salary of the $i$-th employee). Then the median salary is $11$.
In the second test case, you have to pay $1337$ dollars to the only employee.
In the third test case, you can distribute salaries as follows: $sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7$. Then the median salary is $6$.
|
import sys
input = lambda: sys.stdin.readline().rstrip()
T = int(input())
for _ in range(T):
N, S = list(map(int, input().split()))
X = []
for __ in range(N):
l, r = list(map(int, input().split()))
X.append((l, r))
ok = 1
ng = 10**9 + 1
while ng - ok > 1:
m = (ok + ng) // 2
A = []
B = []
for i in range(N):
if X[i][1] >= m:
A.append(X[i][0])
else:
B.append(X[i][0])
A = sorted(A)[::-1]
if (
len(A) > N // 2
and sum([max(a, m) for a in A[: N // 2 + 1]])
+ sum(A[N // 2 + 1 :])
+ sum(B)
<= S
):
ok = m
else:
ng = m
print(ok)
|
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
You are the head of a large enterprise. $n$ people work at you, and $n$ is odd (i. e. $n$ is not divisible by $2$).
You have to distribute salaries to your employees. Initially, you have $s$ dollars for it, and the $i$-th employee should get a salary from $l_i$ to $r_i$ dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: the median of the sequence $[5, 1, 10, 17, 6]$ is $6$, the median of the sequence $[1, 2, 1]$ is $1$.
It is guaranteed that you have enough money to pay the minimum salary, i.e $l_1 + l_2 + \dots + l_n \le s$.
Note that you don't have to spend all your $s$ dollars on salaries.
You have to answer $t$ test cases.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 2 \cdot 10^5$) β the number of test cases.
The first line of each query contains two integers $n$ and $s$ ($1 \le n < 2 \cdot 10^5$, $1 \le s \le 2 \cdot 10^{14}$) β the number of employees and the amount of money you have. The value $n$ is not divisible by $2$.
The following $n$ lines of each query contain the information about employees. The $i$-th line contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le 10^9$).
It is guaranteed that the sum of all $n$ over all queries does not exceed $2 \cdot 10^5$.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. $\sum\limits_{i=1}^{n} l_i \le s$.
-----Output-----
For each test case print one integer β the maximum median salary that you can obtain.
-----Example-----
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
-----Note-----
In the first test case, you can distribute salaries as follows: $sal_1 = 12, sal_2 = 2, sal_3 = 11$ ($sal_i$ is the salary of the $i$-th employee). Then the median salary is $11$.
In the second test case, you have to pay $1337$ dollars to the only employee.
In the third test case, you can distribute salaries as follows: $sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7$. Then the median salary is $6$.
|
import sys
input = sys.stdin.readline
t = int(input())
for test in range(t):
n, s = map(int, input().split())
LR = [tuple(map(int, input().split())) for i in range(n)]
LR.sort(reverse=True)
R = [r for l, r in LR]
R.sort()
MIN = LR[n // 2][0]
MAX = R[n // 2]
OK = (n + 1) // 2
while MIN != MAX:
mid = (MIN + MAX + 1) // 2
count = 0
money = 0
for l, r in LR:
if count < OK:
if r >= mid:
money += max(l, mid)
count += 1
else:
money += l
else:
money += l
if count >= OK and money <= s:
MIN = mid
else:
MAX = mid - 1
print(MIN)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.