description
stringlengths 171
4k
| code
stringlengths 94
3.98k
| normalized_code
stringlengths 57
4.99k
|
|---|---|---|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
def find(x):
first = 0
last = len(temp) - 1
while first <= last:
mid = (first + last) // 2
if temp[mid] == x:
return mid
elif x < temp[mid]:
first = mid + 1
elif x > temp[mid]:
last = mid - 1
return -1
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
arr = [(a[i], i) for i in range(n)]
arr.sort(reverse=True)
index = []
res = 0
for i in range(m * k):
res += arr[i][0]
index.append(arr[i][1])
print(res)
index.sort()
for i in range(k - 1):
print(index[(i + 1) * m - 1] + 1, end=" ")
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR 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 VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER STRING
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
v = sorted(enumerate(a), key=lambda x: x[1], reverse=True)[: m * k]
v = list(sorted(v))
print(sum(map(lambda x: x[1], v)))
for i in range(m, m * k, m):
print(v[i][0], end=" ")
print()
|
ASSIGN VAR 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 FUNC_CALL VAR VAR VAR NUMBER NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER STRING EXPR FUNC_CALL VAR
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = map(int, input().split())
l = list(map(int, input().split()))
sumi = 0
l = list(enumerate(l))
list = [0] * n
l.sort(key=lambda x: x[1])
l.reverse()
for i in range(m * k):
list[l[i][0]] = 1
sumi += l[i][1]
ans = []
count = 0
for i in range(n - 1):
if len(ans) == k - 1:
break
if list[i] == 1:
count += 1
if count == m:
ans.append(i + 1)
count = 0
print(sumi)
print(*ans)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
a = [[a[i], i] for i in range(n)]
a.sort(key=lambda x: x[0], reverse=True)
a = a[: m * k]
a.sort(key=lambda x: x[1])
s = 0
for i in range(m * k):
s += a[i][0]
print(s)
for i in range(m - 1, m * k - 1, m):
print(a[i][1] + 1, end=" ")
|
ASSIGN VAR 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 VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER STRING
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
from sys import stdin
input = stdin.readline
def f(a, m, k):
b = list(zip(a, range(0, len(a))))
b = sorted(b, key=lambda s: s[0], reverse=True)
big = b[: m * k]
s = 0
for i in big:
s += i[0]
big = sorted(big, key=lambda s: s[1])
cb = 0
pt = 0
lb = len(big)
ans = []
for i in range(len(a)):
if pt < lb and i == big[pt][1]:
cb += 1
if cb >= m:
ans.append(i + 1)
cb = 0
pt += 1
print(s)
return ans[:-1]
n, m, k = map(int, input().strip().split())
lst = list(map(int, input().strip().split()))
print(*f(lst, m, k))
|
ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
def main():
[n, m, k] = list(map(int, input().split(" ")))
ar = list(map(int, input().split(" ")))
orderedAr = ar[:]
orderedAr.sort(reverse=True)
items = m * k
new = orderedAr[:items]
f = dict()
for i in new:
if i not in f:
f[i] = 1
else:
f[i] += 1
j = 0
maxSum = sum(new)
noOfpartitions = 0
partitions = []
for i in range(len(ar)):
if noOfpartitions == k - 1:
break
elif ar[i] in f and j < m:
if f[ar[i]] > 0:
f[ar[i]] -= 1
j += 1
if j == m:
partitions.append(i + 1)
noOfpartitions += 1
j = 0
print(maxSum)
for i in partitions:
print(i, end=" ")
main()
|
FUNC_DEF ASSIGN LIST VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = map(int, input().split())
tab = map(int, input().split())
tab = [(t, i) for i, t in enumerate(tab)]
tab.sort()
want = [(False) for _ in range(n)]
for _, i in tab[n - k * m :]:
want[i] = True
counter = 0
print(sum(t for t, i in tab if want[i]))
printed = 0
for i, w in enumerate(want):
counter += w
if counter == m:
if i + 1 != n:
print(i + 1, end=" ")
printed += 1
counter = 0
if printed == k - 1:
break
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
def main():
n, m, k = map(int, input().split())
arr = list(map(int, input().split()))
max_nums = {}
arr1 = arr[:]
arr1.sort(reverse=True)
total = 0
for i in range(m * k):
if arr1[i] not in max_nums.keys():
max_nums[arr1[i]] = 1
else:
max_nums[arr1[i]] += 1
total += arr1[i]
count = 0
ans = []
for i in range(n):
if arr[i] in max_nums.keys():
max_nums[arr[i]] -= 1
if max_nums[arr[i]] == 0:
del max_nums[arr[i]]
count += 1
if count > m:
ans.append(i)
count = 1
if len(ans) == k - 1:
break
print(total)
for i in ans:
print(i, end=" ")
main()
|
FUNC_DEF ASSIGN VAR 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 DICT ASSIGN VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
def do_task(a, n, m, k):
a_sorted = sorted(enumerate(a), key=lambda x: x[1])
s = [x[0] for x in a_sorted]
s = s[::-1]
s = s[: m * k]
s.sort()
result = 0
for i in range(m * k):
result += a[s[i]]
print(result)
splits = []
for i in range(m - 1, k * m - 1, m):
splits.append(s[i] + 1)
print_s = ""
for i in range(len(splits)):
print_s += str(splits[i]) + " "
print(print_s)
splits = input().split(" ")
n = int(splits[0])
m = int(splits[1])
k = int(splits[2])
a = n * [0]
splits = input().split(" ")
for i in range(n):
a[i] = int(splits[i])
do_task(a, n, m, k)
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR LIST NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
sortedA = sorted(a, reverse=True)
freqLar = {}
sum = 0
for i in range(m * k):
sum += sortedA[i]
if i > 0 and sortedA[i] == sortedA[i - 1]:
freqLar[sortedA[i]] += 1
else:
freqLar[sortedA[i]] = 1
mn = sortedA[m * k - 1]
sub = 0
part = []
sm = 0
for i in range(n):
if a[i] >= mn and freqLar[a[i]] > 0:
sm += a[i]
sub += 1
freqLar[a[i]] -= 1
if sub == m:
sub = 0
k -= 1
if k > 0:
part.append(i + 1)
else:
break
print(sum)
for i in part:
print(i, end=" ")
|
ASSIGN VAR 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 FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
a1 = []
for i in range(n):
a1.append((a[i], i))
a1.sort(reverse=True)
_max = [0] * n
ans = 0
for i in range(m * k):
_max[a1[i][1]] = 1
ans += a1[i][0]
print(ans)
_sum = 0
ans_ar = []
for i in range(n):
_sum += _max[i]
if _sum == m:
ans_ar.append(i + 1)
_sum = 0
ans_ar.pop()
print(*ans_ar)
|
ASSIGN VAR 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 VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
x = []
for i in range(n):
x.append((a[i], i))
x.sort(reverse=True)
total = 0
check = []
len = 0
for i in range(k * m):
total += x[i][0]
check.append(x[i][1])
len += 1
check.sort()
print(total)
temp = k - 1
count = 0
for i in range(len):
count += 1
if count == m:
count = 0
print(check[i] + 1, end=" ")
temp -= 1
if temp == 0:
break
|
ASSIGN VAR 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 VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER STRING VAR NUMBER IF VAR NUMBER
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
def get():
return list(map(int, input().split()))
n, m, k = get()
a = get()
b = sorted(a)
c = b[-(m * k) :]
f = c.count(c[0])
r = 0
s = sum(b[-(m * k) :])
l = []
for i in range(n):
if a[i] > c[0] or a[i] == c[0] and f > 0:
if a[i] == c[0]:
f -= 1
r += 1
if r == m:
r = 0
l += [i + 1]
print(s)
print(*l[: k - 1])
|
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR LIST BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = list(map(int, input().split()))
nums = list(map(int, input().split()))
for i, num in enumerate(nums):
nums[i] = {"value": num, "ind": i}
nums = sorted(nums, key=lambda x: x["value"], reverse=True)
beauty, indexes = 0, []
for i in range(m * k):
indexes.append(nums[i]["ind"])
beauty += nums[i]["value"]
indexes.sort()
print(beauty)
print(*[(indexes[(i + 1) * m - 1] + 1) for i in range(k - 1)])
|
ASSIGN VAR 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 FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR DICT STRING STRING VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR STRING NUMBER ASSIGN VAR VAR NUMBER LIST FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING VAR VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = map(int, input().split())
arr = enumerate(list(map(int, input().split())), 1)
arr = sorted(arr, key=lambda x: x[1], reverse=True)
ind = [arr[i][0] for i in range(m * k)]
ind.sort()
ans = [ind[i] for i in range(m - 1, m * (k - 1), m)]
print(sum(arr[i][1] for i in range(m * k)))
print(*ans)
|
ASSIGN VAR VAR 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 NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
def solve(n, m, k, arr):
newArr = []
for i in range(n):
newArr.append((arr[i], i))
newArr.sort(key=lambda x: x[0], reverse=True)
mk = m * k
largeNums = {}
for i in range(mk):
largeNums[newArr[i]] = 1
ans = 0
ansArr = []
seg = 0
numInSeg = 0
maxNums = 0
tempArr = []
i = 0
while i < n:
if seg < k - 1:
if numInSeg >= m and maxNums >= m:
seg += 1
maxNums = 0
numInSeg = 0
ans += sum(val for val, ind in tempArr)
ansArr.append(tempArr[-1][1] + 1)
tempArr.clear()
i -= 1
else:
x = arr[i], i
if x in largeNums:
maxNums += 1
tempArr.append(x)
mk -= 1
numInSeg += 1
else:
tempArr.append(arr[i])
i += 1
tempArr.sort(reverse=True)
ans += sum(tempArr[:mk])
print(ans)
for j in ansArr:
print(j, end=" ")
def main():
n, m, k = map(int, input().split())
arr = list(map(int, input().split()))
solve(n, m, k, arr)
main()
|
FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING FUNC_DEF ASSIGN VAR 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 VAR EXPR FUNC_CALL VAR
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
lin = [int(i) for i in input().split()]
n = lin[0]
m = lin[1]
k = lin[2]
a = [int(i) for i in input().split()]
vet = []
for i in range(n):
vet.append([a[i], i])
vet.sort(reverse=True)
sp = [False] * n
ans = 0
for i in range(k * m):
sp[vet[i][1]] = True
ans += vet[i][0]
ini = []
cnt = 0
for i in range(n):
if sp[i] == True:
cnt += 1
if cnt == m:
ini.append(i + 1)
cnt = 0
print(ans)
print(*ini[0 : k - 1 : 1], sep=" ")
|
ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER STRING
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = [int(item) for item in input().split()]
a = [int(item) for item in input().split()]
best = {key: (0) for key in sorted(a)[-m * k :]}
for x in sorted(a)[-m * k :]:
best[x] += 1
print(sum(x * best[x] for x in best))
cnt = 0
parts = 1
for i in range(n):
if parts == k:
break
if a[i] in best and best[a[i]] > 0:
cnt += 1
best[a[i]] -= 1
if cnt == m:
print(i + 1, end=" ")
parts += 1
cnt = 0
print()
|
ASSIGN VAR 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 VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
def check():
n, m, k = [int(x) for x in input().split()]
s = [int(x) for x in input().split()]
a = []
stop = []
for i in range(n):
a += [(int(s[i]), i)]
aa = sorted(a, key=lambda x: x[0], reverse=True)
aa = aa[: m * k]
ans = 0
for e in aa:
ans += e[0]
counter = 0
vis = [(0) for _ in range(n)]
for i in aa:
vis[i[1]] = 1
for i in range(len(a)):
if vis[i] == 1:
counter += 1
if counter == m:
stop += [i + 1]
counter = 0
k -= 1
if k == 1:
break
print(ans)
for e in stop:
print(e, end=" ")
check()
|
FUNC_DEF ASSIGN VAR 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 ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR LIST FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR LIST BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = []
for i in range(n):
b.append((a[i], i + 1))
b.sort(reverse=True)
c = b[: m * k]
c.sort(key=lambda x: x[1])
ans = 0
for i in c:
ans += i[0]
print(ans)
for i in range(len(c) - 1):
if (i + 1) % m == 0:
print(c[i][1], end=" ")
|
ASSIGN VAR 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 VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER STRING
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = sorted(enumerate(a), key=lambda x: (x[1], x[0]), reverse=True)
b = b[: m * k]
b = sorted(b)
rec = []
s = 0
tmp = 0
for i in range(len(b)):
tmp += 1
if tmp == m:
rec.append(b[i][0] + 1)
tmp = 0
s += b[i][1]
print(s)
print(" ".join(map(str, rec[:-1])))
|
ASSIGN VAR 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 FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR NUMBER
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
b = sorted(a)
s = 0
if m * k == n:
print(sum(a))
for i in range(m, n, m):
print(i, end=" ")
else:
count = 0
p = b[n - m * k]
print(sum(b[n - m * k :]))
for i in range(n - m * k, n):
if b[i] == p:
s += 1
b = []
for j in range(n):
if a[j] > p:
count += 1
if count == m:
b.append(j + 1)
count = 0
elif a[j] == p and s != 0:
count += 1
s -= 1
if count == m:
b.append(j + 1)
count = 0
for i in range(len(b) - 1):
print(b[i], end=" ")
|
ASSIGN VAR 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 FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = map(int, input().strip().split())
l = [(int(x), i) for i, x in enumerate(input().strip().split())]
l.sort(key=lambda x: x[0], reverse=True)
u = m * k
sums = 0
l1 = []
for i in range(u):
sums += l[i][0]
l1.append(l[i][1])
l1.sort()
l2 = []
print(sums)
for i in range(k - 1):
l2.append(l1[(i + 1) * m - 1])
for i in l2:
print(i + 1, end=" ")
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = list(map(int, input().split()))
a = list(map(int, input().split()))
ind = [x for x in range(n)]
ind = [i for j, i in sorted(zip(a, ind), reverse=True)]
index = []
s = 0
for i in range(m * k):
s += a[ind[i]]
index.append(ind[i])
print(s)
index.sort()
for i in range(k - 1):
print(index[(i + 1) * m - 1] + 1, end=" ")
|
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER STRING
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = list(map(int, input().split()))
a = list(map(int, input().split()))
b = a.copy()
b.sort(reverse=True)
msum = sum(b[: m * k])
count = {}
for i in range(m * k):
count[b[i]] = count.get(b[i], 0) + 1
if n == 2:
if k == 2:
print(sum(a))
print(1)
elif m == 1:
print(max(a))
print(n)
else:
print(sum(a))
print(2)
else:
print(msum)
size = 0
c = 0
ind = []
for i in range(n):
if len(ind) == k - 1:
break
if a[i] in count:
size += 1
count[a[i]] -= 1
if count[a[i]] == 0:
del count[a[i]]
if size == m:
size = 0
ind.append(i + 1)
for i in range(len(ind)):
print(ind[i], end=" ")
print()
|
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = list(map(int, input().split()))
a = list(map(int, input().split()))
b = sorted([(a[i], i) for i in range(n)])
ok = [0] * n
ans = 0
for x, i in b[-k * m :]:
ok[i] = 1
ans += x
t = 0
li = []
for i in range(n):
t += ok[i]
if ok[i] and t % m == 0:
li += [i + 1]
print(ans)
print(*li[:-1])
|
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR BIN_OP VAR VAR NUMBER VAR LIST BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
N, M, K = map(int, input().split())
a_list = list(map(int, input().split()))
b_list = [i for i in range(N)]
b_list.sort(reverse=True, key=lambda x: a_list[x])
c_list = []
sum_beauties = 0
for i in range(K * M):
sum_beauties += a_list[b_list[i]]
c_list.append((a_list[b_list[i]], b_list[i]))
print(sum_beauties)
c_list.sort(key=lambda x: (x[1], x[0]))
for i in range(1, len(c_list)):
if i % M == 0:
print(c_list[i - 1][1] + 1, end=" ")
|
ASSIGN VAR 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 NUMBER VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER STRING
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
pairs = [(a[i], i) for i in range(len(a))]
sorted_a = sorted(pairs, reverse=True)[: m * k]
grid = [0] * len(a)
for x, i in sorted_a:
grid[i] = 1
s = 0
print(sum([p[0] for p in sorted_a]))
cnt = 0
for i, val in enumerate(grid):
s += val
if s == m:
cnt += 1
print(i + 1, end=" ")
s = 0
if cnt == k - 1:
break
|
ASSIGN VAR 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 VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
from sys import stdin
def compute():
n, m, k = map(int, stdin.readline().split())
a = list(map(int, stdin.readline().split()))
i = sorted(enumerate(a), key=lambda p: (-p[1], p[0]))
res = sum(map(lambda p: p[1], i[: m * k]))
p = list(sorted(map(lambda p: p[0], i[: m * k])))[m : m * k : m]
return res, p
res, p = compute()
print(res)
print(*p)
|
FUNC_DEF ASSIGN VAR 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 FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR RETURN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
line1 = input()
line2 = input()
n, m, k = map(int, line1.split())
nums = list(map(int, line2.split()))
nums_copy = nums.copy()
nums_copy.sort()
counter = {}
res1 = 0
res2 = []
for num in nums_copy[n - m * k :]:
counter[num] = counter.get(num, 0) + 1
res1 += num
cnt = 0
for index, num in enumerate(nums):
if num in counter.keys():
cnt += 1
counter[num] -= 1
if counter[num] == 0:
del counter[num]
if cnt == m:
res2.append(index + 1)
if len(res2) == k - 1:
break
cnt = 0
print(res1)
print(" ".join(map(str, res2)))
|
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = map(int, input().split())
p = list([x, i] for i, x in enumerate(map(int, input().split()), 1))
p.sort(reverse=True)
l = []
s = 0
for i in range(m * k):
s += p[i][0]
l.append(p[i][1])
print(s)
l.sort()
for i in range(m - 1, len(l) - 1, m):
print(l[i], end=" ")
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR LIST VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR STRING
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = map(int, input().split())
A = list(map(int, input().split()))
B = list(enumerate(A))
B.sort(key=lambda x: x[1])
B.reverse()
LIST = [0] * n
ANS = 0
for i in range(m * k):
LIST[B[i][0]] = 1
ANS += B[i][1]
ANSLIST = []
count = 0
for i in range(n):
if LIST[i] == 1:
count += 1
if count == m:
ANSLIST.append(i + 1)
count = 0
print(ANS)
for a in ANSLIST[:-1]:
print(a, end=" ")
|
ASSIGN VAR 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 FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = [int(i) for i in input().strip().split()]
nums = [int(i) for i in input().strip().split()]
beauty_num = m * k
num_sort = sorted(nums, reverse=True)[:beauty_num]
num_dict = dict()
for i in num_sort:
if i not in num_dict:
num_dict[i] = 1
else:
num_dict[i] += 1
counter = 0
ans = []
for i in range(n):
if nums[i] in num_dict and num_dict[nums[i]] > 0:
counter += 1
num_dict[nums[i]] -= 1
if counter >= m:
ans.append(i + 1)
counter = 0
print(sum(num_sort))
print(" ".join(str(i) for i in ans[:-1]))
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR NUMBER
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = map(int, input().split())
a = map(int, input().split())
lengthMemory = []
for index, elem in enumerate(a):
lengthMemory.append((elem, index))
lengthMemory.sort(reverse=True)
beauty = sum(map(lambda x: x[0], lengthMemory[: m * k]))
print(beauty)
indexGroup = []
for elem in lengthMemory[: m * k]:
indexGroup.append(elem[1])
indexGroup.sort()
ans = []
for index in range(m - 1, len(indexGroup), m):
ans.append(str(indexGroup[index] + 1))
print(" ".join(ans[:-1]))
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = sorted(a)
d = {}
for i in range(m * k):
d[b[n - 1 - i]] = d.get(b[n - 1 - i], 0) + 1
c = 0
kk = []
print(sum(b[::-1][: m * k]))
for i in range(n):
if d.get(a[i], 0) > 0:
d[a[i]] -= 1
c += 1
if c == m:
c = 0
kk.append(i + 1)
print(*kk[: len(kk) - 1])
|
ASSIGN VAR 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 DICT FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = tuple(map(int, input().split()))
A = list(map(int, input().split()))
B = A[:]
B.sort()
B = B[-(m * k) :]
maximal = {}
summa = 0
for i in B:
maximal[i] = maximal.get(i, 0) + 1
summa += i
B = set(B)
count_max = 0
counter = 0
print(summa)
for i in range(n):
if A[i] in B and maximal[A[i]] != 0:
count_max += 1
maximal[A[i]] -= 1
if count_max == m:
print(i + 1, end=" ")
counter += 1
count_max = 0
if counter == k - 1:
break
|
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = map(int, input().split())
a = [int(s) for s in input().split()]
a_sorted = sorted(a, reverse=True)
min_take = a_sorted[m * k - 1]
needed_min = 0
for i in range(m * k - 1, -1, -1):
if a_sorted[i] == min_take:
needed_min += 1
su = 0
for i in range(m * k):
su += a_sorted[i]
part = []
takes = 0
for i in range(n):
if a[i] > min_take:
takes += 1
elif a[i] == min_take:
if needed_min > 0:
takes += 1
needed_min -= 1
if takes >= m:
part.append(i + 1)
takes = 0
if len(part) == k - 1:
break
print(su)
print(*part, sep=" ")
|
ASSIGN VAR 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 NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
s = input().split()
n = int(s[0])
m = int(s[1])
k = int(s[2])
s = input().split()
a = [int(i) for i in s]
a_descend = []
for index, num in enumerate(a):
a_descend.append((num, index))
a_descend = sorted(a_descend, reverse=True)
ans_sum = 0
flg = [(False) for _ in range(n)]
for i in range(m * k):
flg[a_descend[i][1]] = True
ans_sum += a_descend[i][0]
print(ans_sum)
cnt = 0
j = 0
for i in range(n):
if flg[i]:
cnt += 1
if cnt == m:
print(i + 1, end=" ")
cnt = 0
j += 1
if j == k - 1:
exit(0)
|
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = map(int, input().split())
a = []
i = 0
for item in input().split():
a.append((int(item), i))
i += 1
a.sort(reverse=True)
c = 0
index = []
for i in range(m * k):
c += a[i][0]
index.append(a[i][1])
print(c)
index.sort()
for i in range(m - 1, m * k - 1, m):
print(index[i] + 1, end=" ")
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER STRING
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = map(int, input().split())
ls = list(map(int, input().split()))
ls2 = ls.copy()
ls2.sort()
d = {}
count = 0
ans = 0
for x in reversed(ls2):
r = int(x)
ans = ans + r
if r in d:
d[r] = d[r] + 1
else:
d[r] = 1
count = count + 1
if count == m * k:
break
ls1 = list()
count = 0
count1 = 0
for i in range(0, n):
h = ls[i]
if h in d:
d[h] = d[h] - 1
if d[h] == 0:
del d[h]
count1 = count1 + 1
if count1 == m:
count1 = 0
count = count + 1
ls1.append(i + 1)
if count == k - 1:
break
print(ans)
for i in range(0, len(ls1)):
print(ls1[i], end=" ")
|
ASSIGN VAR 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 EXPR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
B = sorted(A, reverse=True)
b = int(1000000000000000.0)
cnt = 0
ans = sum(B[: m * k])
for i in range(m * k):
if b == B[i]:
cnt += 1
else:
cnt = 1
b = B[i]
P = []
cnt_e = 0
cnt_b = 0
for i, a in enumerate(A):
if len(P) >= k - 1:
break
if a > b:
cnt_e += 1
elif a == b and cnt_b < cnt:
cnt_e += 1
cnt_b += 1
if cnt_e >= m:
P.append(i + 1)
cnt_e = 0
print(ans)
print(*P)
|
ASSIGN VAR 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 FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = map(int, input().split())
list_1 = [int(i) for i in input().split()]
total = m * k
sorter = sorted(list_1, reverse=True)[:total]
print(sum(sorter))
board = sorter.count(sorter[m * k - 1])
num = 0
out = []
for i in range(n):
if list_1[i] > sorter[m * k - 1]:
num += 1
elif board > 0 and list_1[i] == sorter[m * k - 1]:
board -= 1
num += 1
if num == m:
num = 0
out.append(i + 1)
print(" ".join([str(i) for i in out[:-1]]))
|
ASSIGN VAR 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 BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR NUMBER
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = map(int, input().split())
arr = list(map(int, input().split()))
sa = 0
arr_ind = [[c, i] for i, c in enumerate(arr)]
arr_ind.sort()
flag = [0] * n
for j in range(n - 1, n - 1 - m * k, -1):
sa += arr_ind[j][0]
flag[arr_ind[j][1]] = 1
print(sa)
s = 0
t = 1
for i in range(n):
s += flag[i]
if s // m == t:
t += 1
print(i + 1, end=" ")
if t == k:
break
print()
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING IF VAR VAR EXPR FUNC_CALL VAR
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, k, m = map(int, input().split())
xs = list(map(int, input().split()))[:n]
helper = sorted(list(zip(xs, range(len(xs)))), reverse=True)
needed = helper[: k * m]
needed = sorted(needed, key=lambda x: x[1])
result = sum([x[0] for x in needed])
print(result)
for i in range(k, len(needed), k):
print(needed[i][1], end=" ")
print()
|
ASSIGN VAR 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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER STRING EXPR FUNC_CALL VAR
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = [int(x) for x in input().strip().split(" ")]
a = [int(x) for x in input().strip().split(" ")]
limit = sorted(a)[n - m * k]
index = 0
drop = 0
while index < n and drop < n - m * k:
if a[index] < limit:
drop += 1
a[index] = None
index += 1
index = 0
while index < n and drop < n - m * k:
if a[index] == limit:
drop += 1
a[index] = None
index += 1
index = 0
segment = []
all = 0
while index < n:
sub = 0
total = 0
while sub < m and index < n:
if a[index] is not None:
sub += 1
total += a[index]
index += 1
while index < n and a[index] is None:
index += 1
all += total
segment.append(index)
print(all)
print(" ".join([str(x) for x in segment[:-1]]))
pass
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NONE VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NONE VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR NONE VAR NUMBER VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR NONE VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR NUMBER
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = map(int, input().split())
listo = list(map(int, input().split()))
lis = listo.copy()
lis.sort(reverse=True)
cand = {}
s = 0
for i in range(m * k):
s += lis[i]
if lis[i] in cand:
cand[lis[i]] += 1
else:
cand[lis[i]] = 1
cnt = 0
sol = []
for i in range(n):
if listo[i] in cand:
cnt += 1
cand[listo[i]] -= 1
if cand[listo[i]] == 0:
del cand[listo[i]]
if cnt == m:
sol.append(i + 1)
cnt = 0
print(s)
print(*sol[:-1])
|
ASSIGN VAR 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 EXPR FUNC_CALL VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = map(int, input().split())
l = list(map(int, input().split()))
ans = []
for i in range(n):
ans.append([l[i], i])
ans.sort(reverse=1)
sum = 0
ans1 = []
for i in range(m * k):
sum += ans[i][0]
ans1.append(ans[i][1] + 1)
print(sum)
ans1.sort()
for i in range(m - 1, len(ans1) - 1, m):
print(ans1[i], end=" ")
|
ASSIGN VAR 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 LIST VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR STRING
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = (int(x) for x in input().split())
arr = [int(x) for x in input().split()]
sorted_enum = list(sorted(enumerate(arr), key=lambda x: -x[1]))[: m * k]
sorted_enum = sorted(sorted_enum, key=lambda x: x[0])
max_sum = 0
arr = []
for i in range(k):
sm = sum([x[1] for x in sorted_enum[i * m : (i + 1) * m]])
arr.append(sorted_enum[i * m + m - 1][0] + 1)
max_sum += sm
print(max_sum)
print(*arr[:-1])
|
ASSIGN VAR 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 FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
fin = sorted(a, reverse=True)[: k * m]
s = sum(fin)
d = dict()
for i in fin:
if i in d:
d[i] += 1
else:
d[i] = 1
print(s)
count = 0
for i in range(n):
if a[i] in d and d[a[i]] > 0:
count += 1
d[a[i]] -= 1
if count == m and k > 1:
count = 0
k -= 1
print(i + 1, end=" ")
|
ASSIGN VAR 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 NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
r = [int(x) for x in input().split()]
n = r[0]
m = r[1]
k = r[2]
a = [int(x) for x in input().split()]
arr = []
for i in range(n):
arr.append([a[i], i])
arr.sort()
arr.reverse()
brr = []
sum = 0
for i in range(m * k):
brr.append([arr[i][1], arr[i][0]])
sum += arr[i][0]
brr.sort()
print(sum)
for i in range(m - 1, len(brr) - 1, +m):
print(brr[i][0] + 1, end=" ")
|
ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER STRING
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = sorted(a, reverse=True)
t = b[m * k - 1]
idx = b.index(t)
valid = m * k - idx
count = 0
v = 0
ans = []
for i in range(n):
if a[i] > t or a[i] == t and v < valid:
count += 1
if a[i] == t:
v += 1
if count >= m:
ans.append(i + 1)
if len(ans) == k - 1:
break
count = 0
print(sum(b[i] for i in range(m * k)))
print(*ans)
|
ASSIGN VAR 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 NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
def check(val):
return b[m * k - 1] <= val
n, m, k = map(int, input().split())
i = 0
a = []
b = []
for x in input().split():
a.append((int(x), i))
b.append((int(x), i))
i += 1
b.sort()
b.reverse()
res = 0
p = []
cnt = 0
for i in range(n):
if check(a[i]):
cnt += 1
res += a[i][0]
if cnt == m + 1:
p.append(i)
cnt = 1
print(res)
for x in p:
print(x, end=" ")
|
FUNC_DEF RETURN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
R = lambda: map(int, input().split())
n, m, k = R()
L = list(R())
S = sorted(L)
ind = n - m * k
e = S[ind]
c = 1
while ind < n - 1:
if S[ind] == S[ind + 1]:
c += 1
else:
break
ind += 1
res = []
ans = 0
p = m
for i in range(n):
if L[i] > e:
p -= 1
ans += L[i]
elif L[i] == e and c > 0:
c -= 1
p -= 1
ans += L[i]
if p == 0:
res.append(i + 1)
p = m
print(ans)
print(*res[:-1])
|
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = input().split()
n = int(n)
m = int(m)
k = int(k)
a = [int(x) for x in input().split()]
b = a.copy()
a.sort(reverse=True)
sp = sum(a[0 : m * k])
temp = a[0 : m * k]
start = 0
print(sp)
t = 0
c = 0
temp2 = dict()
for i in range(len(temp)):
if temp[i] in temp2:
temp2[temp[i]] += 1
else:
temp2[temp[i]] = 1
for i in range(n):
if b[i] in temp2 and temp2[b[i]] > 0:
t += 1
temp2[b[i]] -= 1
if t == m and i - start >= m - 1:
print(i + 1, end=" ")
start = i + 1
t = 0
c += 1
if c == k - 1:
break
|
ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = map(int, input().split())
lista = sorted(map(lambda x: (x[1], x[0]), enumerate(map(int, input().split()))))[
-m * k :
]
[l1, l2] = zip(*lista)
print(sum(l1))
print(*list(map(lambda x: x + 1, sorted(l2)[m - 1 :: m]))[: k - 1])
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR BIN_OP VAR VAR ASSIGN LIST VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = map(int, input().split())
l = [int(x) for x in input().split()]
ll = [(l[i], i) for i in range(n)]
ll.sort(key=lambda x: x[0], reverse=True)
ans, idx = 0, []
for i in range(m * k):
ans += ll[i][0]
idx.append(ll[i][1])
idx.sort()
print(ans)
for i in range(k - 1):
print(idx[(i + 1) * m - 1] + 1, end=" ")
print("")
|
ASSIGN VAR 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 VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER LIST FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER STRING EXPR FUNC_CALL VAR STRING
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = list(map(int, input().split()))
a = list(map(int, input().split()))
b = sorted(a, reverse=True)
middle = b[m * k - 1]
nm = b[: m * k].count(middle)
sum = sum(b[: m * k])
ind = []
c = 0
cut = 0
cm = 0
for i in range(len(a)):
if a[i] >= middle:
if a[i] == middle:
if cm < nm:
cm += 1
c += 1
else:
c += 1
if c == m:
c = 0
cut += 1
ind.append(i + 1)
if cut == k - 1:
break
print(sum)
print(" ".join(list(map(str, ind))))
|
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = []
ok = [1] * n
for i in range(n):
b.append([a[i], i])
b.sort()
ans = 0
for i in range(n):
ans += a[i]
for i in range(n - m * k):
ans -= b[i][0]
ok[b[i][1]] = 0
cnt = 0
print(ans)
for i in range(n - 1):
cnt += ok[i]
if cnt == m and k > 1:
k -= 1
print(i + 1, end=" ")
cnt = 0
|
ASSIGN VAR 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 ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = map(int, input().split())
arr = input()
nums = [int(n) for n in arr.split()]
s = list(enumerate(nums))
s.sort(key=lambda x: x[1], reverse=True)
s = s[: m * k]
sum = sum(x[1] for x in s)
print(sum)
s = [x[0] for x in s]
s.sort()
for i in range(m, m * k, m):
print(s[i - 1] + 1, end=" ")
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER STRING
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = input().split()
n = int(n)
m = int(m)
k = int(k)
arr = input().split()
a = [int(i) for i in arr]
c = [i for i in a]
a.sort()
rem = n - m * k
h = {}
for i in a:
h[i] = 0
for i in range(rem):
h[a[i]] += 1
l = 0
b = []
su = 0
for i in range(n):
if h[c[i]] == 0:
l += 1
su += c[i]
else:
h[c[i]] -= 1
if l == m:
l = 0
b.append(i)
print(su)
for i in range(k - 1):
print(b[i] + 1, end=" ")
|
ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER STRING
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = [(a[i], i) for i in range(n)]
b.sort(reverse=True)
c = [0] * n
sm = 0
for i in range(m * k):
sm += b[i][0]
c[b[i][1]] = 1
i = 0
p = []
while i < n:
j = i
cnt = 0
while j < n and cnt < m:
cnt += c[j]
j += 1
p.append(j)
i = j
print(sm)
print(*p[: k - 1])
|
ASSIGN VAR 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 VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
R = lambda: map(int, input().split())
n, m, k = R()
a = sorted(zip(R(), range(n)))[-m * k :]
print(sum(x for x, _ in a), *sorted(i + 1 for _, i in a)[m - 1 : -1 : m])
|
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
def main():
n, m, k = list(map(int, input().split()))
a = list(map(int, input().split()))
ret = sum(a)
index = list(range(n))
index.sort(key=lambda i: a[i])
rm_flag = [False] * n
for i in range(n - m * k):
rm_flag[index[i]] = True
ret -= a[index[i]]
pos, cnt = [], 0
for i in range(n):
if not rm_flag[i]:
cnt += 1
if cnt == m:
pos.append(i + 1)
cnt = 0
if len(pos) == k - 1:
break
print(ret)
print(*pos)
def __starting_point():
main()
__starting_point()
|
FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = map(int, input().split())
arr = list(map(int, input().split()))
arr2 = [x for x in arr]
arr2.sort()
indices = []
high = arr2[-k * m :][0]
highcount = arr2[-k * m :].count(high)
temp = m
count = 1
total = 0
for i in range(n):
if arr[i] > high or arr[i] == high and highcount > 0:
total += arr[i]
temp -= 1
if arr[i] == high:
highcount -= 1
if temp == 0 and count < k:
indices.append(i + 1)
temp = m
count += 1
print(total)
for x in indices:
print(x, end=" ")
print()
|
ASSIGN VAR 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 VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = map(int, input().split())
mas = list(map(int, input().split()))
qu = {}
for i in sorted(mas, reverse=True)[: k * m]:
if not i in qu.keys():
qu[i] = 0
qu[i] += 1
otv = []
c = 0
for i in range(n):
if mas[i] in qu.keys() and qu[mas[i]] > 0:
qu[mas[i]] -= 1
c += 1
if c == m:
otv.append(i + 1)
c = 0
print(sum(sorted(mas, reverse=True)[: k * m]))
for i in otv[:-1]:
print(i, end=" ")
|
ASSIGN VAR 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 DICT FOR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR IF VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR FOR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = map(int, input().split())
l = list(map(int, input().split()))
a = n // k
su = 0
li = (a - 1) * k
for i in range(n):
l[i] = i + 1, l[i]
l.sort(reverse=True, key=lambda x: x[1])
lol = m * k
for i in range(lol):
su = su + l[i][1]
l = l[: m * k]
print(su)
kk = 0
l.sort()
for i in range(m, m * k, m):
print(l[i][0] - 1, end=" ")
print()
|
ASSIGN VAR 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 ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER STRING EXPR FUNC_CALL VAR
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
for i in range(n):
a[i] = [a[i], i]
num_required = m * k
a.sort()
candidate = a[n - num_required : n]
len_cand = len(candidate)
beauty = 0
for x in range(len_cand):
beauty += candidate[x][0]
candidate[x][0], candidate[x][1] = candidate[x][1], candidate[x][0]
print(beauty)
candidate.sort()
ans = []
for x in range(m - 1, len_cand - 1, m):
ans.append(str(candidate[x][0] + 1))
print(" ".join(ans))
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = map(int, input().split())
cl = list(map(int, input().split()))
pl = sorted(cl)
dic = {}
for i in range(n - m * k, n):
dic.update({pl[i]: 1})
pos = pl[n - m * k]
p = pl[n - m * k :].count(pos)
for i in range(n - m * k):
if pl[i] != pos:
dic.update({pl[i]: 0})
count = 0
ans = []
sm = sum(pl[n - m * k :])
for i in range(n):
if dic[cl[i]] == 1:
if cl[i] == pos:
if p != 0:
count += 1
p -= 1
else:
count += 1
if count == m:
ans.append(i + 1)
count = 0
print(sm)
print(*ans[: k - 1])
|
ASSIGN VAR 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 DICT FOR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR DICT VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR DICT VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
nmk = input().split()
n = int(nmk[0])
m = int(nmk[1])
k = int(nmk[2])
a = input().split()
for i in range(len(a)):
a[i] = int(a[i])
b = []
for i in range(len(a)):
b.append([a[i], i])
b.sort(reverse=1)
sum = 0
c = []
for i in range(m * k):
sum = sum + b[i][0]
c.append(b[i][1])
print(sum)
c.sort()
for i in range(m - 1, len(c) - 1, m):
print(c[i] + 1, end=" ")
print()
|
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER STRING EXPR FUNC_CALL VAR
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
def inpl():
return list(map(int, input().split()))
N, M, K = inpl()
A = [(int(v), i) for i, v in enumerate(input().split(), 1)]
A.sort(reverse=True)
H = dict()
for j, (_, i) in enumerate(A, 1):
H[i] = j
ctr = 0
ans = []
sumans = 0
for i in range(1, N + 1):
pre = ctr
if H[i] <= K * M:
ctr += 1
if not ctr % M and pre != ctr:
ans.append(i)
print(sum([v for v, _ in A[: K * M]]))
print(*ans[:-1])
|
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR BIN_OP VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
c = []
for i in range(n):
c.append([a[i], i])
b = sorted(c, reverse=True)
def f(m, a):
s = 0
b = sorted(a, reverse=True)
for i in range(m):
s += b[i]
return s
ans = []
for i in range(m * k):
ans.append(b[i])
sum = 0
for elem in ans:
sum += elem[0]
anss = []
for elem in ans:
el1 = elem[0]
el2 = elem[1]
anss.append([el2, el1])
anss.sort()
z = 0
print(sum)
for i in range(m - 1, len(anss), m):
j = anss[i][0]
z += 1
print(j + 1, end=" ")
if z == k - 1:
break
|
ASSIGN VAR 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 LIST VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING IF VAR BIN_OP VAR NUMBER
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = map(int, input().split())
A = list(map(int, input().split()))
a = list(enumerate(A))
a.sort(key=lambda x: x[1])
f = [0] * (n + 100)
ans = 0
ANS = []
for i in range(n - m * k):
f[a[i][0]] = 1
t = 0
tt = 0
for i in range(n):
t += 1
ans += A[i]
if f[i] == 1:
t -= 1
ans -= A[i]
if t == m and tt != k:
ANS.append(i + 1)
t = 0
tt += 1
print(ans)
for i in ANS[:-1]:
print(i, end=" ")
|
ASSIGN VAR 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 FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
input = __import__("sys").stdin.readline
print = __import__("sys").stdout.write
n, m, k = map(int, input().split())
tmp = list(map(int, input().split()))
r = sorted(tmp, reverse=True)[: m * k]
min_val = min(r)
print(str(sum(r)) + "\n")
d = {}
for i in r:
if i not in d:
d[i] = 1
else:
d[i] += 1
idx = []
k = 0
for i in range(n):
if tmp[i] >= min_val:
if d[tmp[i]] > 0:
k += 1
d[tmp[i]] -= 1
if k == m:
k = 0
idx.append(i)
for i in idx[:-1]:
print(str(i + 1) + " ")
|
ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR 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 NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR STRING ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER STRING
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
from sys import stdin
n, m, k = map(int, stdin.readline().strip().split())
s = list(map(int, stdin.readline().strip().split()))
s1 = s.copy()
s1.sort(reverse=True)
x = m * k
s1 = s1[0:x]
d = dict()
for i in s1:
if i not in d:
d[i] = 1
else:
d[i] += 1
x = 0
ans = []
y = 1
for i in range(n):
if s[i] in d and d[s[i]] > 0:
d[s[i]] -= 1
x += 1
if x == m:
x = 0
ans.append(i + 1)
y += 1
if y == k:
break
print(sum(s1))
print(*ans)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
def main():
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
if n == k:
print(sum(a))
print(" ".join([str(i) for i in range(1, n)]))
return
r = sorted(a, reverse=True)[: m * k]
target = r[-1]
r = r.count(r[-1])
obj = 0
tmp = []
result = 0
result2 = []
ks = 0
for i in range(len(a)):
if obj == m:
if ks == k - 1:
pass
else:
result += sum(sorted(tmp, reverse=True)[:m])
tmp = []
obj = 0
ks += 1
result2.append(str(i))
if a[i] == target and r > 0:
r -= 1
obj += 1
if a[i] > target:
obj += 1
tmp.append(a[i])
result += sum(sorted(tmp, reverse=True)[:m])
print(result)
print(" ".join(result2))
main()
|
FUNC_DEF ASSIGN VAR 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 FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR LIST ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = map(int, input().split())
arr = list(map(int, input().split()))
arr1 = sorted(arr)
a = arr1[-m * k :]
d = {}
for i in a:
d[i] = d.get(i, 0) + 1
print(sum(a))
p = 0
t = 0
for i in range(n):
if arr[i] in d:
d[arr[i]] -= 1
t += 1
if d[arr[i]] == 0:
del d[arr[i]]
if t == m:
t = 0
p += 1
print(i + 1, end=" ")
if p == k - 1:
break
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING IF VAR BIN_OP VAR NUMBER
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
array_with_id = [(a[i], i + 1) for i in range(n)]
array_with_id.sort(reverse=True)
important_ids = [item[1] for item in array_with_id[: m * k]]
important_ids.sort()
sum_beauty = 0
for i in important_ids:
sum_beauty += a[i - 1]
partitions = []
for i in range(m - 1, m * k - 1, m):
partitions.append(important_ids[i])
print(sum_beauty)
print(*partitions)
|
ASSIGN VAR 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 BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
n, m, k = map(int, input().strip().split())
l = list(map(int, input().strip().split()))
b = sorted(range(len(l)), key=lambda k: l[k])
b.reverse()
b = b[: m * k]
b.sort()
sm = 0
ans = []
for i in range(len(b)):
if i % m == 0 and i != 0:
ans.append(b[i])
sm += l[b[i]]
print(sm)
for i in ans:
print(i, end=" ")
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
|
An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$.
Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.
For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.
You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:
Each element from $a$ belongs to exactly one subarray.
Each subarray has at least $m$ elements.
The sum of all beauties of $k$ subarrays is maximum possible.
-----Input-----
The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$)Β β the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).
-----Output-----
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which:
All elements with indices from $1$ to $p_1$ belong to the first subarray.
All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.
$\ldots$.
All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.
If there are several optimal partitions, print any of them.
-----Examples-----
Input
9 2 3
5 2 5 2 4 1 1 3 2
Output
21
3 5
Input
6 1 4
4 1 3 2 2 3
Output
12
1 3 5
Input
2 1 2
-1000000000 1000000000
Output
0
1
-----Note-----
In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.
The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$.
The sum of their beauties is $10 + 6 + 5 = 21$.
In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$.
|
def go():
n, m, k = [int(i) for i in input().split(" ")]
original = [int(i) for i in input().split(" ")]
a = sorted(original, reverse=True)
d = {}
for i in range(m * k):
d.setdefault(a[i], 0)
d[a[i]] += 1
c = 0
r = 0
o = []
for i in range(n):
if original[i] in d:
if d[original[i]] == 1:
d.pop(original[i])
else:
d[original[i]] -= 1
c += 1
if c == m:
c = 0
o.append(i + 1)
total = sum(a[i] for i in range(m * k))
return "{}\n{}".format(total, " ".join(str(i) for i in o[:-1]))
print(go())
|
FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN FUNC_CALL STRING VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR
|
You like playing chess tournaments online.
In your last tournament you played $n$ games. For the sake of this problem, each chess game is either won or lost (no draws). When you lose a game you get $0$ points. When you win you get $1$ or $2$ points: if you have won also the previous game you get $2$ points, otherwise you get $1$ point. If you win the very first game of the tournament you get $1$ point (since there is not a "previous game").
The outcomes of the $n$ games are represented by a string $s$ of length $n$: the $i$-th character of $s$ is W if you have won the $i$-th game, while it is L if you have lost the $i$-th game.
After the tournament, you notice a bug on the website that allows you to change the outcome of at most $k$ of your games (meaning that at most $k$ times you can change some symbol L to W, or W to L). Since your only goal is to improve your chess rating, you decide to cheat and use the bug.
Compute the maximum score you can get by cheating in the optimal way.
-----Input-----
Each test contains multiple test cases. The first line contains an integer $t$ ($1\le t \le 20,000$) β the number of test cases. The description of the test cases follows.
The first line of each testcase contains two integers $n, k$ ($1\le n\le 100,000$, $0\le k\le n$) β the number of games played and the number of outcomes that you can change.
The second line contains a string $s$ of length $n$ containing only the characters W and L. If you have won the $i$-th game then $s_i=\,$W, if you have lost the $i$-th game then $s_i=\,$L.
It is guaranteed that the sum of $n$ over all testcases does not exceed $200,000$.
-----Output-----
For each testcase, print a single integer β the maximum score you can get by cheating in the optimal way.
-----Example-----
Input
8
5 2
WLWLL
6 5
LLLWWL
7 1
LWLWLWL
15 5
WWWLLLWWWLLLWWW
40 7
LLWLWLWWWLWLLWLWWWLWLLWLLWLLLLWLLWWWLWWL
1 0
L
1 1
L
6 1
WLLWLW
Output
7
11
6
26
46
0
1
6
-----Note-----
Explanation of the first testcase. Before changing any outcome, the score is $2$. Indeed, you won the first game, so you got $1$ point, and you won also the third, so you got another $1$ point (and not $2$ because you lost the second game).
An optimal way to cheat is to change the outcomes of the second and fourth game. Doing so, you end up winning the first four games (the string of the outcomes becomes WWWWL). Hence, the new score is $7=1+2+2+2$: $1$ point for the first game and $2$ points for the second, third and fourth game.
Explanation of the second testcase. Before changing any outcome, the score is $3$. Indeed, you won the fourth game, so you got $1$ point, and you won also the fifth game, so you got $2$ more points (since you won also the previous game).
An optimal way to cheat is to change the outcomes of the first, second, third and sixth game. Doing so, you end up winning all games (the string of the outcomes becomes WWWWWW). Hence, the new score is $11 = 1+2+2+2+2+2$: $1$ point for the first game and $2$ points for all the other games.
|
def solve(n, k, s):
gaps = []
first_w_index = -1
last_w_index = -1
score = 0
win_streak = 0
for i in range(n):
if s[i] == "W":
win_streak += 1
if first_w_index == -1:
first_w_index = i
elif i - last_w_index - 1 > 0:
gaps.append(i - last_w_index - 1)
last_w_index = i
elif win_streak > 0:
score += 2 * win_streak - 1
win_streak = 0
if win_streak > 0:
score += 2 * win_streak - 1
gaps.sort()
rem = []
for g in gaps:
if g <= k:
score += 2 * g + 1
k -= g
else:
rem.append(g)
if k > 0:
if len(rem) > 0:
score += 2 * k
elif first_w_index == -1:
score += 2 * k - 1
else:
score += 2 * min(k, first_w_index + n - last_w_index - 1)
return score
T = int(input())
for _ in range(T):
n, k = map(int, input().split())
s = input()
print(solve(n, k, s))
|
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR BIN_OP NUMBER VAR IF VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
|
You like playing chess tournaments online.
In your last tournament you played $n$ games. For the sake of this problem, each chess game is either won or lost (no draws). When you lose a game you get $0$ points. When you win you get $1$ or $2$ points: if you have won also the previous game you get $2$ points, otherwise you get $1$ point. If you win the very first game of the tournament you get $1$ point (since there is not a "previous game").
The outcomes of the $n$ games are represented by a string $s$ of length $n$: the $i$-th character of $s$ is W if you have won the $i$-th game, while it is L if you have lost the $i$-th game.
After the tournament, you notice a bug on the website that allows you to change the outcome of at most $k$ of your games (meaning that at most $k$ times you can change some symbol L to W, or W to L). Since your only goal is to improve your chess rating, you decide to cheat and use the bug.
Compute the maximum score you can get by cheating in the optimal way.
-----Input-----
Each test contains multiple test cases. The first line contains an integer $t$ ($1\le t \le 20,000$) β the number of test cases. The description of the test cases follows.
The first line of each testcase contains two integers $n, k$ ($1\le n\le 100,000$, $0\le k\le n$) β the number of games played and the number of outcomes that you can change.
The second line contains a string $s$ of length $n$ containing only the characters W and L. If you have won the $i$-th game then $s_i=\,$W, if you have lost the $i$-th game then $s_i=\,$L.
It is guaranteed that the sum of $n$ over all testcases does not exceed $200,000$.
-----Output-----
For each testcase, print a single integer β the maximum score you can get by cheating in the optimal way.
-----Example-----
Input
8
5 2
WLWLL
6 5
LLLWWL
7 1
LWLWLWL
15 5
WWWLLLWWWLLLWWW
40 7
LLWLWLWWWLWLLWLWWWLWLLWLLWLLLLWLLWWWLWWL
1 0
L
1 1
L
6 1
WLLWLW
Output
7
11
6
26
46
0
1
6
-----Note-----
Explanation of the first testcase. Before changing any outcome, the score is $2$. Indeed, you won the first game, so you got $1$ point, and you won also the third, so you got another $1$ point (and not $2$ because you lost the second game).
An optimal way to cheat is to change the outcomes of the second and fourth game. Doing so, you end up winning the first four games (the string of the outcomes becomes WWWWL). Hence, the new score is $7=1+2+2+2$: $1$ point for the first game and $2$ points for the second, third and fourth game.
Explanation of the second testcase. Before changing any outcome, the score is $3$. Indeed, you won the fourth game, so you got $1$ point, and you won also the fifth game, so you got $2$ more points (since you won also the previous game).
An optimal way to cheat is to change the outcomes of the first, second, third and sixth game. Doing so, you end up winning all games (the string of the outcomes becomes WWWWWW). Hence, the new score is $11 = 1+2+2+2+2+2$: $1$ point for the first game and $2$ points for all the other games.
|
t = int(input())
for _ in range(t):
n, k = list(map(int, input().split()))
string = list(input())
lis = []
i = 0
j = n - 1
counter = 0
for _i_ in string:
if _i_ != "W":
counter += 1
i += 1
else:
break
b1 = counter, 0
counter = 0
for _i_ in reversed(string):
if _i_ != "W":
counter += 1
j -= 1
else:
break
b2 = counter, j + 1
while i <= j:
counter = 0
if string[i] == "L":
q = i
while string[i] == "L":
counter += 1
i += 1
lis.append((counter, q))
else:
i += 1
lis.sort()
for i in lis:
if k <= 0:
break
count = i[0]
j = i[1]
for i in range(j, j + count):
string[i] = "W"
k -= 1
if k <= 0:
break
c, index = b1
for i in range(index + c - 1, index - 1, -1):
if k == 0:
break
else:
string[i] = "W"
k -= 1
c, index = b2
for i in range(index, index + c):
if k == 0:
break
else:
string[i] = "W"
k -= 1
prev = "L"
count = 0
for i in string:
if i == "W" and prev == "W":
count += 2
elif i == "W":
count += 1
prev = i
print(count)
|
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 ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR WHILE VAR VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR STRING VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
You like playing chess tournaments online.
In your last tournament you played $n$ games. For the sake of this problem, each chess game is either won or lost (no draws). When you lose a game you get $0$ points. When you win you get $1$ or $2$ points: if you have won also the previous game you get $2$ points, otherwise you get $1$ point. If you win the very first game of the tournament you get $1$ point (since there is not a "previous game").
The outcomes of the $n$ games are represented by a string $s$ of length $n$: the $i$-th character of $s$ is W if you have won the $i$-th game, while it is L if you have lost the $i$-th game.
After the tournament, you notice a bug on the website that allows you to change the outcome of at most $k$ of your games (meaning that at most $k$ times you can change some symbol L to W, or W to L). Since your only goal is to improve your chess rating, you decide to cheat and use the bug.
Compute the maximum score you can get by cheating in the optimal way.
-----Input-----
Each test contains multiple test cases. The first line contains an integer $t$ ($1\le t \le 20,000$) β the number of test cases. The description of the test cases follows.
The first line of each testcase contains two integers $n, k$ ($1\le n\le 100,000$, $0\le k\le n$) β the number of games played and the number of outcomes that you can change.
The second line contains a string $s$ of length $n$ containing only the characters W and L. If you have won the $i$-th game then $s_i=\,$W, if you have lost the $i$-th game then $s_i=\,$L.
It is guaranteed that the sum of $n$ over all testcases does not exceed $200,000$.
-----Output-----
For each testcase, print a single integer β the maximum score you can get by cheating in the optimal way.
-----Example-----
Input
8
5 2
WLWLL
6 5
LLLWWL
7 1
LWLWLWL
15 5
WWWLLLWWWLLLWWW
40 7
LLWLWLWWWLWLLWLWWWLWLLWLLWLLLLWLLWWWLWWL
1 0
L
1 1
L
6 1
WLLWLW
Output
7
11
6
26
46
0
1
6
-----Note-----
Explanation of the first testcase. Before changing any outcome, the score is $2$. Indeed, you won the first game, so you got $1$ point, and you won also the third, so you got another $1$ point (and not $2$ because you lost the second game).
An optimal way to cheat is to change the outcomes of the second and fourth game. Doing so, you end up winning the first four games (the string of the outcomes becomes WWWWL). Hence, the new score is $7=1+2+2+2$: $1$ point for the first game and $2$ points for the second, third and fourth game.
Explanation of the second testcase. Before changing any outcome, the score is $3$. Indeed, you won the fourth game, so you got $1$ point, and you won also the fifth game, so you got $2$ more points (since you won also the previous game).
An optimal way to cheat is to change the outcomes of the first, second, third and sixth game. Doing so, you end up winning all games (the string of the outcomes becomes WWWWWW). Hence, the new score is $11 = 1+2+2+2+2+2$: $1$ point for the first game and $2$ points for all the other games.
|
import sys
input = lambda: sys.stdin.readline()
int_arr = lambda: list(map(int, input().split()))
str_arr = lambda: list(map(str, input().split()))
get_str = lambda: map(str, input().split())
get_int = lambda: map(int, input().split())
get_flo = lambda: map(float, input().split())
mod = 1000000007
def solve(n, k, s):
c = 0
if s[0] == "W":
c = 1
for i in range(1, n):
if s[i] == "W" and s[i - 1] == "W":
c += 2
elif s[i] == "W":
c += 1
if k == 0:
print(c)
return
l, r = 0, 0
for i in range(n):
if s[i] == "W":
l = i
break
for i in range(n - 1, -1, -1):
if s[i] == "W":
r = i
break
ans = []
tmp = 0
for i in range(l, r + 1):
if s[i] == "L":
tmp += 1
else:
if tmp != 0:
ans.append(tmp)
tmp = 0
ans.sort()
for i in ans:
if i <= k:
c += 2 * i + 1
k -= i
else:
c += 2 * k
k = 0
break
if k > 0 and c > 0:
c += min(n - (r + 1), k) * 2
k -= n - (r + 1)
if k > 0:
c += min(k, l) * 2
print(c if c != 0 else max(k, 1) * 2 - 1)
for _ in range(int(input())):
n, k = get_int()
s = str(input())[:-1]
solve(n, k, s)
|
IMPORT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR
|
You like playing chess tournaments online.
In your last tournament you played $n$ games. For the sake of this problem, each chess game is either won or lost (no draws). When you lose a game you get $0$ points. When you win you get $1$ or $2$ points: if you have won also the previous game you get $2$ points, otherwise you get $1$ point. If you win the very first game of the tournament you get $1$ point (since there is not a "previous game").
The outcomes of the $n$ games are represented by a string $s$ of length $n$: the $i$-th character of $s$ is W if you have won the $i$-th game, while it is L if you have lost the $i$-th game.
After the tournament, you notice a bug on the website that allows you to change the outcome of at most $k$ of your games (meaning that at most $k$ times you can change some symbol L to W, or W to L). Since your only goal is to improve your chess rating, you decide to cheat and use the bug.
Compute the maximum score you can get by cheating in the optimal way.
-----Input-----
Each test contains multiple test cases. The first line contains an integer $t$ ($1\le t \le 20,000$) β the number of test cases. The description of the test cases follows.
The first line of each testcase contains two integers $n, k$ ($1\le n\le 100,000$, $0\le k\le n$) β the number of games played and the number of outcomes that you can change.
The second line contains a string $s$ of length $n$ containing only the characters W and L. If you have won the $i$-th game then $s_i=\,$W, if you have lost the $i$-th game then $s_i=\,$L.
It is guaranteed that the sum of $n$ over all testcases does not exceed $200,000$.
-----Output-----
For each testcase, print a single integer β the maximum score you can get by cheating in the optimal way.
-----Example-----
Input
8
5 2
WLWLL
6 5
LLLWWL
7 1
LWLWLWL
15 5
WWWLLLWWWLLLWWW
40 7
LLWLWLWWWLWLLWLWWWLWLLWLLWLLLLWLLWWWLWWL
1 0
L
1 1
L
6 1
WLLWLW
Output
7
11
6
26
46
0
1
6
-----Note-----
Explanation of the first testcase. Before changing any outcome, the score is $2$. Indeed, you won the first game, so you got $1$ point, and you won also the third, so you got another $1$ point (and not $2$ because you lost the second game).
An optimal way to cheat is to change the outcomes of the second and fourth game. Doing so, you end up winning the first four games (the string of the outcomes becomes WWWWL). Hence, the new score is $7=1+2+2+2$: $1$ point for the first game and $2$ points for the second, third and fourth game.
Explanation of the second testcase. Before changing any outcome, the score is $3$. Indeed, you won the fourth game, so you got $1$ point, and you won also the fifth game, so you got $2$ more points (since you won also the previous game).
An optimal way to cheat is to change the outcomes of the first, second, third and sixth game. Doing so, you end up winning all games (the string of the outcomes becomes WWWWWW). Hence, the new score is $11 = 1+2+2+2+2+2$: $1$ point for the first game and $2$ points for all the other games.
|
t = int(input())
for i in range(t):
n, k = map(int, input().strip(" ").split(" "))
s = input().strip(" ")
ind = s.find("W")
if ind == -1:
if k == 0:
print(0)
else:
print(2 * k - 1)
continue
pre = 1
c = 0
lcount = []
wins = 1
winningstreak = 0
for i in range(ind + 1, n):
if s[i] == "W":
if pre == 0:
lcount.append(c)
c = 0
wins += 1
pre = 1
else:
if pre == 1:
winningstreak += 1
c += 1
pre = 0
if pre:
winningstreak += 1
wins += k
if wins >= n:
print(2 * n - 1)
continue
lcount.sort()
x = 0
for i in lcount:
if k >= i:
x += 1
k -= i
else:
break
ans = 2 * wins - winningstreak + x
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 FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
You like playing chess tournaments online.
In your last tournament you played $n$ games. For the sake of this problem, each chess game is either won or lost (no draws). When you lose a game you get $0$ points. When you win you get $1$ or $2$ points: if you have won also the previous game you get $2$ points, otherwise you get $1$ point. If you win the very first game of the tournament you get $1$ point (since there is not a "previous game").
The outcomes of the $n$ games are represented by a string $s$ of length $n$: the $i$-th character of $s$ is W if you have won the $i$-th game, while it is L if you have lost the $i$-th game.
After the tournament, you notice a bug on the website that allows you to change the outcome of at most $k$ of your games (meaning that at most $k$ times you can change some symbol L to W, or W to L). Since your only goal is to improve your chess rating, you decide to cheat and use the bug.
Compute the maximum score you can get by cheating in the optimal way.
-----Input-----
Each test contains multiple test cases. The first line contains an integer $t$ ($1\le t \le 20,000$) β the number of test cases. The description of the test cases follows.
The first line of each testcase contains two integers $n, k$ ($1\le n\le 100,000$, $0\le k\le n$) β the number of games played and the number of outcomes that you can change.
The second line contains a string $s$ of length $n$ containing only the characters W and L. If you have won the $i$-th game then $s_i=\,$W, if you have lost the $i$-th game then $s_i=\,$L.
It is guaranteed that the sum of $n$ over all testcases does not exceed $200,000$.
-----Output-----
For each testcase, print a single integer β the maximum score you can get by cheating in the optimal way.
-----Example-----
Input
8
5 2
WLWLL
6 5
LLLWWL
7 1
LWLWLWL
15 5
WWWLLLWWWLLLWWW
40 7
LLWLWLWWWLWLLWLWWWLWLLWLLWLLLLWLLWWWLWWL
1 0
L
1 1
L
6 1
WLLWLW
Output
7
11
6
26
46
0
1
6
-----Note-----
Explanation of the first testcase. Before changing any outcome, the score is $2$. Indeed, you won the first game, so you got $1$ point, and you won also the third, so you got another $1$ point (and not $2$ because you lost the second game).
An optimal way to cheat is to change the outcomes of the second and fourth game. Doing so, you end up winning the first four games (the string of the outcomes becomes WWWWL). Hence, the new score is $7=1+2+2+2$: $1$ point for the first game and $2$ points for the second, third and fourth game.
Explanation of the second testcase. Before changing any outcome, the score is $3$. Indeed, you won the fourth game, so you got $1$ point, and you won also the fifth game, so you got $2$ more points (since you won also the previous game).
An optimal way to cheat is to change the outcomes of the first, second, third and sixth game. Doing so, you end up winning all games (the string of the outcomes becomes WWWWWW). Hence, the new score is $11 = 1+2+2+2+2+2$: $1$ point for the first game and $2$ points for all the other games.
|
T = int(input())
for t in range(T):
N, K = [int(_) for _ in input().split()]
initK = K
s = input()
nb_l = len([c for c in s if c == "L"])
current_score = 0
current_nb = 0
current_c = None
loss_group_sizes = []
has_won = False
for c in s:
if c == current_c:
current_nb += 1
if c == "W":
current_score += 2
continue
if current_c == "L" and has_won:
loss_group_sizes.append(current_nb)
current_nb = 1
current_c = c
if c == "W":
current_score += 1
has_won = True
loss_group_sizes = sorted(loss_group_sizes)
i = 0
while K > 0 and i < len(loss_group_sizes):
if K >= loss_group_sizes[i]:
current_score += 2 * loss_group_sizes[i] + 1
K -= loss_group_sizes[i]
else:
break
i += 1
left_l = nb_l - (initK - K)
if "W" in s:
current_score += 2 * min(K, left_l)
else:
current_score += max(0, 2 * min(K, left_l) - 1)
print(current_score)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NONE ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER IF VAR STRING VAR NUMBER IF VAR STRING VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF STRING VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You like playing chess tournaments online.
In your last tournament you played $n$ games. For the sake of this problem, each chess game is either won or lost (no draws). When you lose a game you get $0$ points. When you win you get $1$ or $2$ points: if you have won also the previous game you get $2$ points, otherwise you get $1$ point. If you win the very first game of the tournament you get $1$ point (since there is not a "previous game").
The outcomes of the $n$ games are represented by a string $s$ of length $n$: the $i$-th character of $s$ is W if you have won the $i$-th game, while it is L if you have lost the $i$-th game.
After the tournament, you notice a bug on the website that allows you to change the outcome of at most $k$ of your games (meaning that at most $k$ times you can change some symbol L to W, or W to L). Since your only goal is to improve your chess rating, you decide to cheat and use the bug.
Compute the maximum score you can get by cheating in the optimal way.
-----Input-----
Each test contains multiple test cases. The first line contains an integer $t$ ($1\le t \le 20,000$) β the number of test cases. The description of the test cases follows.
The first line of each testcase contains two integers $n, k$ ($1\le n\le 100,000$, $0\le k\le n$) β the number of games played and the number of outcomes that you can change.
The second line contains a string $s$ of length $n$ containing only the characters W and L. If you have won the $i$-th game then $s_i=\,$W, if you have lost the $i$-th game then $s_i=\,$L.
It is guaranteed that the sum of $n$ over all testcases does not exceed $200,000$.
-----Output-----
For each testcase, print a single integer β the maximum score you can get by cheating in the optimal way.
-----Example-----
Input
8
5 2
WLWLL
6 5
LLLWWL
7 1
LWLWLWL
15 5
WWWLLLWWWLLLWWW
40 7
LLWLWLWWWLWLLWLWWWLWLLWLLWLLLLWLLWWWLWWL
1 0
L
1 1
L
6 1
WLLWLW
Output
7
11
6
26
46
0
1
6
-----Note-----
Explanation of the first testcase. Before changing any outcome, the score is $2$. Indeed, you won the first game, so you got $1$ point, and you won also the third, so you got another $1$ point (and not $2$ because you lost the second game).
An optimal way to cheat is to change the outcomes of the second and fourth game. Doing so, you end up winning the first four games (the string of the outcomes becomes WWWWL). Hence, the new score is $7=1+2+2+2$: $1$ point for the first game and $2$ points for the second, third and fourth game.
Explanation of the second testcase. Before changing any outcome, the score is $3$. Indeed, you won the fourth game, so you got $1$ point, and you won also the fifth game, so you got $2$ more points (since you won also the previous game).
An optimal way to cheat is to change the outcomes of the first, second, third and sixth game. Doing so, you end up winning all games (the string of the outcomes becomes WWWWWW). Hence, the new score is $11 = 1+2+2+2+2+2$: $1$ point for the first game and $2$ points for all the other games.
|
t = int(input())
arr = []
for _ in range(t):
n, k = tuple(map(int, input().split()))
arr.append((n, k, input()))
for n, k, string in arr:
numL = 0
Lchain = string.split("W")
Lchain = list(map(lambda x: len(x), Lchain))
Wchain = string.split("L")
Wchain = list(map(lambda x: len(x), Wchain))
if k >= sum(Lchain):
print(2 * len(string) - 1)
continue
if len(Lchain) == 1:
print(max(0, 2 * k - 1))
continue
res = 0
for i in Wchain:
if i != 0:
res += 2 * i - 1
Lchain = [Lchain[i] for i in range(len(Lchain)) if i != 0 and i != len(Lchain) - 1]
Lchain.sort()
for i in Lchain:
if i == 0:
pass
elif k >= i:
res += 2 * i + 1
k -= i
else:
res += 2 * k
k = 0
break
res += 2 * k
print(res)
|
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 EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FOR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR IF VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR
|
You like playing chess tournaments online.
In your last tournament you played $n$ games. For the sake of this problem, each chess game is either won or lost (no draws). When you lose a game you get $0$ points. When you win you get $1$ or $2$ points: if you have won also the previous game you get $2$ points, otherwise you get $1$ point. If you win the very first game of the tournament you get $1$ point (since there is not a "previous game").
The outcomes of the $n$ games are represented by a string $s$ of length $n$: the $i$-th character of $s$ is W if you have won the $i$-th game, while it is L if you have lost the $i$-th game.
After the tournament, you notice a bug on the website that allows you to change the outcome of at most $k$ of your games (meaning that at most $k$ times you can change some symbol L to W, or W to L). Since your only goal is to improve your chess rating, you decide to cheat and use the bug.
Compute the maximum score you can get by cheating in the optimal way.
-----Input-----
Each test contains multiple test cases. The first line contains an integer $t$ ($1\le t \le 20,000$) β the number of test cases. The description of the test cases follows.
The first line of each testcase contains two integers $n, k$ ($1\le n\le 100,000$, $0\le k\le n$) β the number of games played and the number of outcomes that you can change.
The second line contains a string $s$ of length $n$ containing only the characters W and L. If you have won the $i$-th game then $s_i=\,$W, if you have lost the $i$-th game then $s_i=\,$L.
It is guaranteed that the sum of $n$ over all testcases does not exceed $200,000$.
-----Output-----
For each testcase, print a single integer β the maximum score you can get by cheating in the optimal way.
-----Example-----
Input
8
5 2
WLWLL
6 5
LLLWWL
7 1
LWLWLWL
15 5
WWWLLLWWWLLLWWW
40 7
LLWLWLWWWLWLLWLWWWLWLLWLLWLLLLWLLWWWLWWL
1 0
L
1 1
L
6 1
WLLWLW
Output
7
11
6
26
46
0
1
6
-----Note-----
Explanation of the first testcase. Before changing any outcome, the score is $2$. Indeed, you won the first game, so you got $1$ point, and you won also the third, so you got another $1$ point (and not $2$ because you lost the second game).
An optimal way to cheat is to change the outcomes of the second and fourth game. Doing so, you end up winning the first four games (the string of the outcomes becomes WWWWL). Hence, the new score is $7=1+2+2+2$: $1$ point for the first game and $2$ points for the second, third and fourth game.
Explanation of the second testcase. Before changing any outcome, the score is $3$. Indeed, you won the fourth game, so you got $1$ point, and you won also the fifth game, so you got $2$ more points (since you won also the previous game).
An optimal way to cheat is to change the outcomes of the first, second, third and sixth game. Doing so, you end up winning all games (the string of the outcomes becomes WWWWWW). Hence, the new score is $11 = 1+2+2+2+2+2$: $1$ point for the first game and $2$ points for all the other games.
|
t = int(input())
for t1 in range(t):
n, final = [int(n1) for n1 in input().split()]
s = list(str(input()))
d = []
k = []
count = 0
if s[0] == "W":
count = 1
else:
count = 0
i = 0
first = -1
earlier_count = 0
prev = "L"
while i < len(s):
if s[i] == "W":
if first == -1:
first = i
if prev == "W":
earlier_count = earlier_count + 2
else:
earlier_count = earlier_count + 1
d1 = -1
if i + 1 < n:
for j in range(i + 1, n):
if s[j] == "W":
d1 = j - i - 1
break
if d1 > 0:
d.append(d1)
k.append(2)
prev = "L"
elif d1 == -1:
d.append(n - 1 - i)
k.append(3)
prev = "L"
else:
prev = "W"
i = j
else:
i = i + 1
else:
prev = "L"
i = i + 1
if first != 0 and first != -1:
d.append(first - 0)
k.append(3)
if k != []:
k, d = zip(*sorted(zip(k, d)))
k = list(k)
d = list(d)
sum1 = 0
sum2 = earlier_count
for i in range(len(d)):
if final >= sum1 + d[i]:
sum1 = sum1 + d[i]
if k[i] == 2:
sum2 = sum2 + (2 * d[i] + 1)
else:
sum2 = sum2 + 2 * d[i]
else:
sum2 = sum2 + 2 * (final - sum1)
sum1 = sum1 + (final - sum1)
break
print(sum2)
elif s[0] == "L":
print(max(0, 2 * final - 1))
else:
print(earlier_count)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NUMBER ASSIGN VAR VAR IF VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR LIST ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You like playing chess tournaments online.
In your last tournament you played $n$ games. For the sake of this problem, each chess game is either won or lost (no draws). When you lose a game you get $0$ points. When you win you get $1$ or $2$ points: if you have won also the previous game you get $2$ points, otherwise you get $1$ point. If you win the very first game of the tournament you get $1$ point (since there is not a "previous game").
The outcomes of the $n$ games are represented by a string $s$ of length $n$: the $i$-th character of $s$ is W if you have won the $i$-th game, while it is L if you have lost the $i$-th game.
After the tournament, you notice a bug on the website that allows you to change the outcome of at most $k$ of your games (meaning that at most $k$ times you can change some symbol L to W, or W to L). Since your only goal is to improve your chess rating, you decide to cheat and use the bug.
Compute the maximum score you can get by cheating in the optimal way.
-----Input-----
Each test contains multiple test cases. The first line contains an integer $t$ ($1\le t \le 20,000$) β the number of test cases. The description of the test cases follows.
The first line of each testcase contains two integers $n, k$ ($1\le n\le 100,000$, $0\le k\le n$) β the number of games played and the number of outcomes that you can change.
The second line contains a string $s$ of length $n$ containing only the characters W and L. If you have won the $i$-th game then $s_i=\,$W, if you have lost the $i$-th game then $s_i=\,$L.
It is guaranteed that the sum of $n$ over all testcases does not exceed $200,000$.
-----Output-----
For each testcase, print a single integer β the maximum score you can get by cheating in the optimal way.
-----Example-----
Input
8
5 2
WLWLL
6 5
LLLWWL
7 1
LWLWLWL
15 5
WWWLLLWWWLLLWWW
40 7
LLWLWLWWWLWLLWLWWWLWLLWLLWLLLLWLLWWWLWWL
1 0
L
1 1
L
6 1
WLLWLW
Output
7
11
6
26
46
0
1
6
-----Note-----
Explanation of the first testcase. Before changing any outcome, the score is $2$. Indeed, you won the first game, so you got $1$ point, and you won also the third, so you got another $1$ point (and not $2$ because you lost the second game).
An optimal way to cheat is to change the outcomes of the second and fourth game. Doing so, you end up winning the first four games (the string of the outcomes becomes WWWWL). Hence, the new score is $7=1+2+2+2$: $1$ point for the first game and $2$ points for the second, third and fourth game.
Explanation of the second testcase. Before changing any outcome, the score is $3$. Indeed, you won the fourth game, so you got $1$ point, and you won also the fifth game, so you got $2$ more points (since you won also the previous game).
An optimal way to cheat is to change the outcomes of the first, second, third and sixth game. Doing so, you end up winning all games (the string of the outcomes becomes WWWWWW). Hence, the new score is $11 = 1+2+2+2+2+2$: $1$ point for the first game and $2$ points for all the other games.
|
t = int(input())
for i in range(t):
n, k = map(int, input().split())
s = []
w = input()
for j in w:
s.append(j)
p = k
res = []
j = 0
while j < n:
if s[j] == "W":
res.append(j)
j += 1
if len(res) == 0:
j = 0
while j < n:
if p == 0:
break
s[j] = "W"
p += -1
j += 1
elif len(res) == 1:
j = res[0] - 1
while j >= 0:
if p == 0:
break
s[j] = "W"
p += -1
j += -1
j = res[0] + 1
while j < n:
if p == 0:
break
s[j] = "W"
p += -1
j += 1
else:
req = []
j = 1
while j < len(res):
req.append([res[j] - res[j - 1], res[j - 1], res[j]])
j += 1
req.sort()
l = 0
while l < len(req):
if p == 0:
break
j = req[l][1] + 1
while j < req[l][2]:
if p == 0:
break
s[j] = "W"
p += -1
j += 1
l += 1
j = res[0] - 1
while j >= 0:
if p == 0:
break
s[j] = "W"
p += -1
j += -1
j = res[-1] + 1
while j < n:
if p == 0:
break
s[j] = "W"
p += -1
j += 1
j = 1
ans = 0
if s[0] == "W":
ans = 1
while j < n:
if s[j] == "W" and s[j] == s[j - 1]:
ans += 2
elif s[j] == "W":
ans += 1
j += 1
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 LIST ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER WHILE VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER WHILE VAR VAR IF VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER WHILE VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER WHILE VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER WHILE VAR VAR IF VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR STRING VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You like playing chess tournaments online.
In your last tournament you played $n$ games. For the sake of this problem, each chess game is either won or lost (no draws). When you lose a game you get $0$ points. When you win you get $1$ or $2$ points: if you have won also the previous game you get $2$ points, otherwise you get $1$ point. If you win the very first game of the tournament you get $1$ point (since there is not a "previous game").
The outcomes of the $n$ games are represented by a string $s$ of length $n$: the $i$-th character of $s$ is W if you have won the $i$-th game, while it is L if you have lost the $i$-th game.
After the tournament, you notice a bug on the website that allows you to change the outcome of at most $k$ of your games (meaning that at most $k$ times you can change some symbol L to W, or W to L). Since your only goal is to improve your chess rating, you decide to cheat and use the bug.
Compute the maximum score you can get by cheating in the optimal way.
-----Input-----
Each test contains multiple test cases. The first line contains an integer $t$ ($1\le t \le 20,000$) β the number of test cases. The description of the test cases follows.
The first line of each testcase contains two integers $n, k$ ($1\le n\le 100,000$, $0\le k\le n$) β the number of games played and the number of outcomes that you can change.
The second line contains a string $s$ of length $n$ containing only the characters W and L. If you have won the $i$-th game then $s_i=\,$W, if you have lost the $i$-th game then $s_i=\,$L.
It is guaranteed that the sum of $n$ over all testcases does not exceed $200,000$.
-----Output-----
For each testcase, print a single integer β the maximum score you can get by cheating in the optimal way.
-----Example-----
Input
8
5 2
WLWLL
6 5
LLLWWL
7 1
LWLWLWL
15 5
WWWLLLWWWLLLWWW
40 7
LLWLWLWWWLWLLWLWWWLWLLWLLWLLLLWLLWWWLWWL
1 0
L
1 1
L
6 1
WLLWLW
Output
7
11
6
26
46
0
1
6
-----Note-----
Explanation of the first testcase. Before changing any outcome, the score is $2$. Indeed, you won the first game, so you got $1$ point, and you won also the third, so you got another $1$ point (and not $2$ because you lost the second game).
An optimal way to cheat is to change the outcomes of the second and fourth game. Doing so, you end up winning the first four games (the string of the outcomes becomes WWWWL). Hence, the new score is $7=1+2+2+2$: $1$ point for the first game and $2$ points for the second, third and fourth game.
Explanation of the second testcase. Before changing any outcome, the score is $3$. Indeed, you won the fourth game, so you got $1$ point, and you won also the fifth game, so you got $2$ more points (since you won also the previous game).
An optimal way to cheat is to change the outcomes of the first, second, third and sixth game. Doing so, you end up winning all games (the string of the outcomes becomes WWWWWW). Hence, the new score is $11 = 1+2+2+2+2+2$: $1$ point for the first game and $2$ points for all the other games.
|
t = int(input())
for _ in range(t):
l = list(map(int, input().split()))
n, k = l[0], l[1]
s = str(input())
score = 0
key = 1
for i in range(n):
if s[i] == "L":
key = 1
else:
score += key
key = 2
l = []
count, count2 = 0, 0
ind = 0
for i in range(n):
if s[i] == "L" and ind == 0:
count2 += 1
elif s[i] == "L" and ind == 1:
count += 1
elif s[i] == "W" and ind == 0:
ind = 1
elif count > 0:
l.append(count)
count = 0
count3 = 0
if count > 0:
count3 = count
countf = count2 + count3
l.sort()
p = len(l)
x = k
inc = 0
for i in range(p):
if x >= l[i]:
x = x - l[i]
inc += 2 * l[i] + 1
else:
inc += 2 * x
x = 0
if x == 0:
break
if x > 0:
inc += 2 * min(x, count3)
x -= min(x, count3)
if x > 0:
if "W" in s:
inc += 2 * min(x, count2)
x -= min(x, count2)
else:
inc += 2 * min(x, count2) - 1
x -= min(x, count2)
print(score + inc)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER IF STRING VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
You like playing chess tournaments online.
In your last tournament you played $n$ games. For the sake of this problem, each chess game is either won or lost (no draws). When you lose a game you get $0$ points. When you win you get $1$ or $2$ points: if you have won also the previous game you get $2$ points, otherwise you get $1$ point. If you win the very first game of the tournament you get $1$ point (since there is not a "previous game").
The outcomes of the $n$ games are represented by a string $s$ of length $n$: the $i$-th character of $s$ is W if you have won the $i$-th game, while it is L if you have lost the $i$-th game.
After the tournament, you notice a bug on the website that allows you to change the outcome of at most $k$ of your games (meaning that at most $k$ times you can change some symbol L to W, or W to L). Since your only goal is to improve your chess rating, you decide to cheat and use the bug.
Compute the maximum score you can get by cheating in the optimal way.
-----Input-----
Each test contains multiple test cases. The first line contains an integer $t$ ($1\le t \le 20,000$) β the number of test cases. The description of the test cases follows.
The first line of each testcase contains two integers $n, k$ ($1\le n\le 100,000$, $0\le k\le n$) β the number of games played and the number of outcomes that you can change.
The second line contains a string $s$ of length $n$ containing only the characters W and L. If you have won the $i$-th game then $s_i=\,$W, if you have lost the $i$-th game then $s_i=\,$L.
It is guaranteed that the sum of $n$ over all testcases does not exceed $200,000$.
-----Output-----
For each testcase, print a single integer β the maximum score you can get by cheating in the optimal way.
-----Example-----
Input
8
5 2
WLWLL
6 5
LLLWWL
7 1
LWLWLWL
15 5
WWWLLLWWWLLLWWW
40 7
LLWLWLWWWLWLLWLWWWLWLLWLLWLLLLWLLWWWLWWL
1 0
L
1 1
L
6 1
WLLWLW
Output
7
11
6
26
46
0
1
6
-----Note-----
Explanation of the first testcase. Before changing any outcome, the score is $2$. Indeed, you won the first game, so you got $1$ point, and you won also the third, so you got another $1$ point (and not $2$ because you lost the second game).
An optimal way to cheat is to change the outcomes of the second and fourth game. Doing so, you end up winning the first four games (the string of the outcomes becomes WWWWL). Hence, the new score is $7=1+2+2+2$: $1$ point for the first game and $2$ points for the second, third and fourth game.
Explanation of the second testcase. Before changing any outcome, the score is $3$. Indeed, you won the fourth game, so you got $1$ point, and you won also the fifth game, so you got $2$ more points (since you won also the previous game).
An optimal way to cheat is to change the outcomes of the first, second, third and sixth game. Doing so, you end up winning all games (the string of the outcomes becomes WWWWWW). Hence, the new score is $11 = 1+2+2+2+2+2$: $1$ point for the first game and $2$ points for all the other games.
|
import sys
input = sys.stdin.readline
def main():
n, k = map(int, input().split())
string = input().strip()
if "W" not in string:
ans = min(n, k) * 2 - 1
print(max(ans, 0))
return
L_s = []
cnt = 0
bef = string[0]
ans = 0
for s in string:
if s == bef:
cnt += 1
else:
if bef == "L":
L_s.append(cnt)
else:
ans += cnt * 2 - 1
cnt = 1
bef = s
if bef == "W":
ans += cnt * 2 - 1
cnt = 0
if string[0] == "L" and L_s:
cnt += L_s[0]
L_s = L_s[1:]
L_s.sort()
for l in L_s:
if k >= l:
ans += l * 2 + 1
k -= l
else:
ans += k * 2
k = 0
ans += 2 * min(k, cnt)
print(ans)
for _ in range(int(input())):
main()
|
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF STRING VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER RETURN ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR STRING VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER STRING VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
|
You like playing chess tournaments online.
In your last tournament you played $n$ games. For the sake of this problem, each chess game is either won or lost (no draws). When you lose a game you get $0$ points. When you win you get $1$ or $2$ points: if you have won also the previous game you get $2$ points, otherwise you get $1$ point. If you win the very first game of the tournament you get $1$ point (since there is not a "previous game").
The outcomes of the $n$ games are represented by a string $s$ of length $n$: the $i$-th character of $s$ is W if you have won the $i$-th game, while it is L if you have lost the $i$-th game.
After the tournament, you notice a bug on the website that allows you to change the outcome of at most $k$ of your games (meaning that at most $k$ times you can change some symbol L to W, or W to L). Since your only goal is to improve your chess rating, you decide to cheat and use the bug.
Compute the maximum score you can get by cheating in the optimal way.
-----Input-----
Each test contains multiple test cases. The first line contains an integer $t$ ($1\le t \le 20,000$) β the number of test cases. The description of the test cases follows.
The first line of each testcase contains two integers $n, k$ ($1\le n\le 100,000$, $0\le k\le n$) β the number of games played and the number of outcomes that you can change.
The second line contains a string $s$ of length $n$ containing only the characters W and L. If you have won the $i$-th game then $s_i=\,$W, if you have lost the $i$-th game then $s_i=\,$L.
It is guaranteed that the sum of $n$ over all testcases does not exceed $200,000$.
-----Output-----
For each testcase, print a single integer β the maximum score you can get by cheating in the optimal way.
-----Example-----
Input
8
5 2
WLWLL
6 5
LLLWWL
7 1
LWLWLWL
15 5
WWWLLLWWWLLLWWW
40 7
LLWLWLWWWLWLLWLWWWLWLLWLLWLLLLWLLWWWLWWL
1 0
L
1 1
L
6 1
WLLWLW
Output
7
11
6
26
46
0
1
6
-----Note-----
Explanation of the first testcase. Before changing any outcome, the score is $2$. Indeed, you won the first game, so you got $1$ point, and you won also the third, so you got another $1$ point (and not $2$ because you lost the second game).
An optimal way to cheat is to change the outcomes of the second and fourth game. Doing so, you end up winning the first four games (the string of the outcomes becomes WWWWL). Hence, the new score is $7=1+2+2+2$: $1$ point for the first game and $2$ points for the second, third and fourth game.
Explanation of the second testcase. Before changing any outcome, the score is $3$. Indeed, you won the fourth game, so you got $1$ point, and you won also the fifth game, so you got $2$ more points (since you won also the previous game).
An optimal way to cheat is to change the outcomes of the first, second, third and sixth game. Doing so, you end up winning all games (the string of the outcomes becomes WWWWWW). Hence, the new score is $11 = 1+2+2+2+2+2$: $1$ point for the first game and $2$ points for all the other games.
|
nums = int(input().strip())
for _ in range(nums):
n, k = map(int, input().strip().split())
s = input().strip()
lw, rw = s.find("W"), s.rfind("W")
res = cur_num = 0
if lw == rw:
if lw == -1:
res = 2 * k - 1
else:
res = 2 * k + 1
res = min(2 * len(s) - 1, res)
else:
part = []
for i in range(lw, rw + 1):
if s[i] == "W":
if i > lw and s[i - 1] == "L":
part.append(cur_num)
cur_num = 0
if i > lw and s[i - 1] == "W":
res += 2
else:
res += 1
else:
cur_num += 1
if k >= sum(part) + lw + len(s) - rw - 1:
res = 2 * len(s) - 1
else:
part.sort()
for i in range(len(part)):
if k >= part[i]:
res += 2 * part[i] + 1
k -= part[i]
else:
break
res += 2 * k
print(max(res, 0))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER IF VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING IF VAR VAR VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR VAR VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
|
You like playing chess tournaments online.
In your last tournament you played $n$ games. For the sake of this problem, each chess game is either won or lost (no draws). When you lose a game you get $0$ points. When you win you get $1$ or $2$ points: if you have won also the previous game you get $2$ points, otherwise you get $1$ point. If you win the very first game of the tournament you get $1$ point (since there is not a "previous game").
The outcomes of the $n$ games are represented by a string $s$ of length $n$: the $i$-th character of $s$ is W if you have won the $i$-th game, while it is L if you have lost the $i$-th game.
After the tournament, you notice a bug on the website that allows you to change the outcome of at most $k$ of your games (meaning that at most $k$ times you can change some symbol L to W, or W to L). Since your only goal is to improve your chess rating, you decide to cheat and use the bug.
Compute the maximum score you can get by cheating in the optimal way.
-----Input-----
Each test contains multiple test cases. The first line contains an integer $t$ ($1\le t \le 20,000$) β the number of test cases. The description of the test cases follows.
The first line of each testcase contains two integers $n, k$ ($1\le n\le 100,000$, $0\le k\le n$) β the number of games played and the number of outcomes that you can change.
The second line contains a string $s$ of length $n$ containing only the characters W and L. If you have won the $i$-th game then $s_i=\,$W, if you have lost the $i$-th game then $s_i=\,$L.
It is guaranteed that the sum of $n$ over all testcases does not exceed $200,000$.
-----Output-----
For each testcase, print a single integer β the maximum score you can get by cheating in the optimal way.
-----Example-----
Input
8
5 2
WLWLL
6 5
LLLWWL
7 1
LWLWLWL
15 5
WWWLLLWWWLLLWWW
40 7
LLWLWLWWWLWLLWLWWWLWLLWLLWLLLLWLLWWWLWWL
1 0
L
1 1
L
6 1
WLLWLW
Output
7
11
6
26
46
0
1
6
-----Note-----
Explanation of the first testcase. Before changing any outcome, the score is $2$. Indeed, you won the first game, so you got $1$ point, and you won also the third, so you got another $1$ point (and not $2$ because you lost the second game).
An optimal way to cheat is to change the outcomes of the second and fourth game. Doing so, you end up winning the first four games (the string of the outcomes becomes WWWWL). Hence, the new score is $7=1+2+2+2$: $1$ point for the first game and $2$ points for the second, third and fourth game.
Explanation of the second testcase. Before changing any outcome, the score is $3$. Indeed, you won the fourth game, so you got $1$ point, and you won also the fifth game, so you got $2$ more points (since you won also the previous game).
An optimal way to cheat is to change the outcomes of the first, second, third and sixth game. Doing so, you end up winning all games (the string of the outcomes becomes WWWWWW). Hence, the new score is $11 = 1+2+2+2+2+2$: $1$ point for the first game and $2$ points for all the other games.
|
import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n, k = map(int, input().split())
s = input().strip()
curr = n + 100
streaks = []
out = 0
wins = k
for i in range(n - 1):
if s[i] == "W" and s[i + 1] == "W":
out += 1
for c in s:
if c == "W":
streaks.append(curr)
curr = 0
out += 1
wins += 1
else:
curr += 1
streaks.sort()
out += 2 * k
for v in streaks:
if v > 0 and v <= k:
k -= v
out += 1
print(min([2 * n - 1, out, max(0, 2 * wins - 1)]))
|
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 FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP NUMBER VAR FOR VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR LIST BIN_OP BIN_OP NUMBER VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER
|
You like playing chess tournaments online.
In your last tournament you played $n$ games. For the sake of this problem, each chess game is either won or lost (no draws). When you lose a game you get $0$ points. When you win you get $1$ or $2$ points: if you have won also the previous game you get $2$ points, otherwise you get $1$ point. If you win the very first game of the tournament you get $1$ point (since there is not a "previous game").
The outcomes of the $n$ games are represented by a string $s$ of length $n$: the $i$-th character of $s$ is W if you have won the $i$-th game, while it is L if you have lost the $i$-th game.
After the tournament, you notice a bug on the website that allows you to change the outcome of at most $k$ of your games (meaning that at most $k$ times you can change some symbol L to W, or W to L). Since your only goal is to improve your chess rating, you decide to cheat and use the bug.
Compute the maximum score you can get by cheating in the optimal way.
-----Input-----
Each test contains multiple test cases. The first line contains an integer $t$ ($1\le t \le 20,000$) β the number of test cases. The description of the test cases follows.
The first line of each testcase contains two integers $n, k$ ($1\le n\le 100,000$, $0\le k\le n$) β the number of games played and the number of outcomes that you can change.
The second line contains a string $s$ of length $n$ containing only the characters W and L. If you have won the $i$-th game then $s_i=\,$W, if you have lost the $i$-th game then $s_i=\,$L.
It is guaranteed that the sum of $n$ over all testcases does not exceed $200,000$.
-----Output-----
For each testcase, print a single integer β the maximum score you can get by cheating in the optimal way.
-----Example-----
Input
8
5 2
WLWLL
6 5
LLLWWL
7 1
LWLWLWL
15 5
WWWLLLWWWLLLWWW
40 7
LLWLWLWWWLWLLWLWWWLWLLWLLWLLLLWLLWWWLWWL
1 0
L
1 1
L
6 1
WLLWLW
Output
7
11
6
26
46
0
1
6
-----Note-----
Explanation of the first testcase. Before changing any outcome, the score is $2$. Indeed, you won the first game, so you got $1$ point, and you won also the third, so you got another $1$ point (and not $2$ because you lost the second game).
An optimal way to cheat is to change the outcomes of the second and fourth game. Doing so, you end up winning the first four games (the string of the outcomes becomes WWWWL). Hence, the new score is $7=1+2+2+2$: $1$ point for the first game and $2$ points for the second, third and fourth game.
Explanation of the second testcase. Before changing any outcome, the score is $3$. Indeed, you won the fourth game, so you got $1$ point, and you won also the fifth game, so you got $2$ more points (since you won also the previous game).
An optimal way to cheat is to change the outcomes of the first, second, third and sixth game. Doing so, you end up winning all games (the string of the outcomes becomes WWWWWW). Hence, the new score is $11 = 1+2+2+2+2+2$: $1$ point for the first game and $2$ points for all the other games.
|
for _ in range(int(input())):
n, k = map(int, input().split())
s = input()
L = s.count("L")
W = s.count("W")
score = 0
if s[0] == "W":
score += 1
for i in range(1, n):
if s[i] == "W":
if s[i - 1] == "W":
score += 2
else:
score += 1
start = False
count = 0
List = []
for i in range(n):
if start:
if s[i] == "W":
if count:
List.append(count)
count = 0
else:
count += 1
elif s[i] == "W":
start = True
List.sort()
if W:
for i in List:
if k:
if k >= i:
score += (i - 1) * 2 + 3
k -= i
L -= i
else:
score += k * 2
k = 0
L -= k
if k:
score += min(k, L) * 2
print(score)
elif k:
print(min(k, L) * 2 - 1)
else:
print(0)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER STRING VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR IF VAR VAR STRING IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR IF VAR FOR VAR VAR IF VAR IF VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR IF VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER
|
You like playing chess tournaments online.
In your last tournament you played $n$ games. For the sake of this problem, each chess game is either won or lost (no draws). When you lose a game you get $0$ points. When you win you get $1$ or $2$ points: if you have won also the previous game you get $2$ points, otherwise you get $1$ point. If you win the very first game of the tournament you get $1$ point (since there is not a "previous game").
The outcomes of the $n$ games are represented by a string $s$ of length $n$: the $i$-th character of $s$ is W if you have won the $i$-th game, while it is L if you have lost the $i$-th game.
After the tournament, you notice a bug on the website that allows you to change the outcome of at most $k$ of your games (meaning that at most $k$ times you can change some symbol L to W, or W to L). Since your only goal is to improve your chess rating, you decide to cheat and use the bug.
Compute the maximum score you can get by cheating in the optimal way.
-----Input-----
Each test contains multiple test cases. The first line contains an integer $t$ ($1\le t \le 20,000$) β the number of test cases. The description of the test cases follows.
The first line of each testcase contains two integers $n, k$ ($1\le n\le 100,000$, $0\le k\le n$) β the number of games played and the number of outcomes that you can change.
The second line contains a string $s$ of length $n$ containing only the characters W and L. If you have won the $i$-th game then $s_i=\,$W, if you have lost the $i$-th game then $s_i=\,$L.
It is guaranteed that the sum of $n$ over all testcases does not exceed $200,000$.
-----Output-----
For each testcase, print a single integer β the maximum score you can get by cheating in the optimal way.
-----Example-----
Input
8
5 2
WLWLL
6 5
LLLWWL
7 1
LWLWLWL
15 5
WWWLLLWWWLLLWWW
40 7
LLWLWLWWWLWLLWLWWWLWLLWLLWLLLLWLLWWWLWWL
1 0
L
1 1
L
6 1
WLLWLW
Output
7
11
6
26
46
0
1
6
-----Note-----
Explanation of the first testcase. Before changing any outcome, the score is $2$. Indeed, you won the first game, so you got $1$ point, and you won also the third, so you got another $1$ point (and not $2$ because you lost the second game).
An optimal way to cheat is to change the outcomes of the second and fourth game. Doing so, you end up winning the first four games (the string of the outcomes becomes WWWWL). Hence, the new score is $7=1+2+2+2$: $1$ point for the first game and $2$ points for the second, third and fourth game.
Explanation of the second testcase. Before changing any outcome, the score is $3$. Indeed, you won the fourth game, so you got $1$ point, and you won also the fifth game, so you got $2$ more points (since you won also the previous game).
An optimal way to cheat is to change the outcomes of the first, second, third and sixth game. Doing so, you end up winning all games (the string of the outcomes becomes WWWWWW). Hence, the new score is $11 = 1+2+2+2+2+2$: $1$ point for the first game and $2$ points for all the other games.
|
for _ in range(int(input())):
n, k = list(map(int, input().split()))
a = list(input())
yes = 0
if a[0] == "W":
ans = 1
yes = 1
c = 0
else:
ans = 0
c = 1
r = []
first = 0
for i in range(1, n):
if a[i] == "W" and a[i - 1] == "W":
ans += 2
elif a[i] == "W" and a[i - 1] == "L" and yes != 0:
r.append(c)
c = 0
ans += 1
elif a[i] == "W" and a[i - 1] == "L":
first = c
yes = 1
ans += 1
c = 0
else:
c += 1
last = c
if ans == 0:
print(min(max(k * 2 - 1, 0), n * 2 - 1))
else:
r.sort()
s = 0
i = 0
j = len(r)
while i < j and s + r[i] <= k:
s += r[i]
ans += r[i] * 2 + 1
i += 1
k -= s
if i < j:
ans += k * 2
else:
ans += min(last * 2 + first * 2, k * 2)
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You like playing chess tournaments online.
In your last tournament you played $n$ games. For the sake of this problem, each chess game is either won or lost (no draws). When you lose a game you get $0$ points. When you win you get $1$ or $2$ points: if you have won also the previous game you get $2$ points, otherwise you get $1$ point. If you win the very first game of the tournament you get $1$ point (since there is not a "previous game").
The outcomes of the $n$ games are represented by a string $s$ of length $n$: the $i$-th character of $s$ is W if you have won the $i$-th game, while it is L if you have lost the $i$-th game.
After the tournament, you notice a bug on the website that allows you to change the outcome of at most $k$ of your games (meaning that at most $k$ times you can change some symbol L to W, or W to L). Since your only goal is to improve your chess rating, you decide to cheat and use the bug.
Compute the maximum score you can get by cheating in the optimal way.
-----Input-----
Each test contains multiple test cases. The first line contains an integer $t$ ($1\le t \le 20,000$) β the number of test cases. The description of the test cases follows.
The first line of each testcase contains two integers $n, k$ ($1\le n\le 100,000$, $0\le k\le n$) β the number of games played and the number of outcomes that you can change.
The second line contains a string $s$ of length $n$ containing only the characters W and L. If you have won the $i$-th game then $s_i=\,$W, if you have lost the $i$-th game then $s_i=\,$L.
It is guaranteed that the sum of $n$ over all testcases does not exceed $200,000$.
-----Output-----
For each testcase, print a single integer β the maximum score you can get by cheating in the optimal way.
-----Example-----
Input
8
5 2
WLWLL
6 5
LLLWWL
7 1
LWLWLWL
15 5
WWWLLLWWWLLLWWW
40 7
LLWLWLWWWLWLLWLWWWLWLLWLLWLLLLWLLWWWLWWL
1 0
L
1 1
L
6 1
WLLWLW
Output
7
11
6
26
46
0
1
6
-----Note-----
Explanation of the first testcase. Before changing any outcome, the score is $2$. Indeed, you won the first game, so you got $1$ point, and you won also the third, so you got another $1$ point (and not $2$ because you lost the second game).
An optimal way to cheat is to change the outcomes of the second and fourth game. Doing so, you end up winning the first four games (the string of the outcomes becomes WWWWL). Hence, the new score is $7=1+2+2+2$: $1$ point for the first game and $2$ points for the second, third and fourth game.
Explanation of the second testcase. Before changing any outcome, the score is $3$. Indeed, you won the fourth game, so you got $1$ point, and you won also the fifth game, so you got $2$ more points (since you won also the previous game).
An optimal way to cheat is to change the outcomes of the first, second, third and sixth game. Doing so, you end up winning all games (the string of the outcomes becomes WWWWWW). Hence, the new score is $11 = 1+2+2+2+2+2$: $1$ point for the first game and $2$ points for all the other games.
|
for _ in range(int(input())):
n, k = list(map(int, input().split()))
s = input()
i = 0
first = 0
last = 0
res = 0
while i < n and s[i] != "W":
i += 1
first += 1
intervals = []
while i < n:
start = i
while i < n and s[i] == "W":
if i > start:
res += 2
else:
res += 1
i += 1
if i == n:
break
intervals.append(0)
while i < n and s[i] != "W":
intervals[-1] += 1
i += 1
if i == n:
last = intervals.pop()
intervals.sort()
if len(intervals) == 0 and first == n:
print(max(0, min(n, k) * 2 - 1))
else:
i = 0
while k > 0 and i < len(intervals):
if intervals[i] <= k:
res += intervals[i] * 2 + 1
k -= intervals[i]
else:
res += 2 * k
k = 0
i += 1
if k > 0:
res += min(last + first, k) * 2
print(res)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR STRING IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You like playing chess tournaments online.
In your last tournament you played $n$ games. For the sake of this problem, each chess game is either won or lost (no draws). When you lose a game you get $0$ points. When you win you get $1$ or $2$ points: if you have won also the previous game you get $2$ points, otherwise you get $1$ point. If you win the very first game of the tournament you get $1$ point (since there is not a "previous game").
The outcomes of the $n$ games are represented by a string $s$ of length $n$: the $i$-th character of $s$ is W if you have won the $i$-th game, while it is L if you have lost the $i$-th game.
After the tournament, you notice a bug on the website that allows you to change the outcome of at most $k$ of your games (meaning that at most $k$ times you can change some symbol L to W, or W to L). Since your only goal is to improve your chess rating, you decide to cheat and use the bug.
Compute the maximum score you can get by cheating in the optimal way.
-----Input-----
Each test contains multiple test cases. The first line contains an integer $t$ ($1\le t \le 20,000$) β the number of test cases. The description of the test cases follows.
The first line of each testcase contains two integers $n, k$ ($1\le n\le 100,000$, $0\le k\le n$) β the number of games played and the number of outcomes that you can change.
The second line contains a string $s$ of length $n$ containing only the characters W and L. If you have won the $i$-th game then $s_i=\,$W, if you have lost the $i$-th game then $s_i=\,$L.
It is guaranteed that the sum of $n$ over all testcases does not exceed $200,000$.
-----Output-----
For each testcase, print a single integer β the maximum score you can get by cheating in the optimal way.
-----Example-----
Input
8
5 2
WLWLL
6 5
LLLWWL
7 1
LWLWLWL
15 5
WWWLLLWWWLLLWWW
40 7
LLWLWLWWWLWLLWLWWWLWLLWLLWLLLLWLLWWWLWWL
1 0
L
1 1
L
6 1
WLLWLW
Output
7
11
6
26
46
0
1
6
-----Note-----
Explanation of the first testcase. Before changing any outcome, the score is $2$. Indeed, you won the first game, so you got $1$ point, and you won also the third, so you got another $1$ point (and not $2$ because you lost the second game).
An optimal way to cheat is to change the outcomes of the second and fourth game. Doing so, you end up winning the first four games (the string of the outcomes becomes WWWWL). Hence, the new score is $7=1+2+2+2$: $1$ point for the first game and $2$ points for the second, third and fourth game.
Explanation of the second testcase. Before changing any outcome, the score is $3$. Indeed, you won the fourth game, so you got $1$ point, and you won also the fifth game, so you got $2$ more points (since you won also the previous game).
An optimal way to cheat is to change the outcomes of the first, second, third and sixth game. Doing so, you end up winning all games (the string of the outcomes becomes WWWWWW). Hence, the new score is $11 = 1+2+2+2+2+2$: $1$ point for the first game and $2$ points for all the other games.
|
import sys
inp = sys.stdin.buffer.readline
inar = lambda: list(map(int, inp().split()))
inin = lambda: int(inp())
inst = lambda: inp().decode().strip()
wrt = sys.stdout.write
pr = lambda *args, end="\n": wrt(" ".join([str(x) for x in args]) + end)
enum = enumerate
inf = float("inf")
cdiv = lambda x, y: -(-x // y)
_T_ = int(input())
for _t_ in range(_T_):
n, k = list(map(int, input().split()))
s = list(input())
if k >= s.count("L"):
print(2 * n - 1)
continue
w = 0
l = 0
prev = " "
val = []
ans = 0
for i in s:
if i == "W" and prev == "W":
ans += 2
if i == "W" and prev != "W":
ans += 1
if i != prev:
if prev == " ":
val.append(0)
elif prev == "W":
val.append(2 * w)
w = 0
else:
val.append(2 * l + 1)
l = 0
if i == "W":
w += 1
else:
l += 1
prev = i
if w == 0:
val.append(2 * l + 1)
val.append(0)
elif l == 0:
val.append(2 * w)
ar = []
for i in range(len(val) - 2):
w1, l, w2 = val[i], val[i + 1], val[i + 2]
if w1 % 2 == 0 and l % 2 == 1 and w2 % 2 == 0:
ar.append((w1, l, w2))
tmp = []
for i in ar:
if i[0] == 0 or i[2] == 0:
tmp.append(i)
ar.remove(i)
ar.sort(key=lambda x: x[1])
tmp.sort(key=lambda x: x[0] + x[2])
ar.extend(tmp)
ar.reverse()
while k > 0 and ar:
w1, l, w2 = ar.pop()
w1 //= 2
l //= 2
w2 //= 2
if l <= k:
if w1 > 0 and w2 > 0:
ans += 2 * l + 1
elif w1 == 0 and w2 == 0:
ans += 2 * l - 1
else:
ans += 2 * l
k -= l
elif w1 == w2 == 0:
ans += 2 * k - 1
k = 0
else:
ans += 2 * k
k = 0
print(ans)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR STRING FUNC_CALL VAR BIN_OP FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP 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 IF VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR STRING VAR NUMBER IF VAR STRING VAR STRING VAR NUMBER IF VAR VAR IF VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR WHILE VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR IF VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR VAR VAR IF VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You like playing chess tournaments online.
In your last tournament you played $n$ games. For the sake of this problem, each chess game is either won or lost (no draws). When you lose a game you get $0$ points. When you win you get $1$ or $2$ points: if you have won also the previous game you get $2$ points, otherwise you get $1$ point. If you win the very first game of the tournament you get $1$ point (since there is not a "previous game").
The outcomes of the $n$ games are represented by a string $s$ of length $n$: the $i$-th character of $s$ is W if you have won the $i$-th game, while it is L if you have lost the $i$-th game.
After the tournament, you notice a bug on the website that allows you to change the outcome of at most $k$ of your games (meaning that at most $k$ times you can change some symbol L to W, or W to L). Since your only goal is to improve your chess rating, you decide to cheat and use the bug.
Compute the maximum score you can get by cheating in the optimal way.
-----Input-----
Each test contains multiple test cases. The first line contains an integer $t$ ($1\le t \le 20,000$) β the number of test cases. The description of the test cases follows.
The first line of each testcase contains two integers $n, k$ ($1\le n\le 100,000$, $0\le k\le n$) β the number of games played and the number of outcomes that you can change.
The second line contains a string $s$ of length $n$ containing only the characters W and L. If you have won the $i$-th game then $s_i=\,$W, if you have lost the $i$-th game then $s_i=\,$L.
It is guaranteed that the sum of $n$ over all testcases does not exceed $200,000$.
-----Output-----
For each testcase, print a single integer β the maximum score you can get by cheating in the optimal way.
-----Example-----
Input
8
5 2
WLWLL
6 5
LLLWWL
7 1
LWLWLWL
15 5
WWWLLLWWWLLLWWW
40 7
LLWLWLWWWLWLLWLWWWLWLLWLLWLLLLWLLWWWLWWL
1 0
L
1 1
L
6 1
WLLWLW
Output
7
11
6
26
46
0
1
6
-----Note-----
Explanation of the first testcase. Before changing any outcome, the score is $2$. Indeed, you won the first game, so you got $1$ point, and you won also the third, so you got another $1$ point (and not $2$ because you lost the second game).
An optimal way to cheat is to change the outcomes of the second and fourth game. Doing so, you end up winning the first four games (the string of the outcomes becomes WWWWL). Hence, the new score is $7=1+2+2+2$: $1$ point for the first game and $2$ points for the second, third and fourth game.
Explanation of the second testcase. Before changing any outcome, the score is $3$. Indeed, you won the fourth game, so you got $1$ point, and you won also the fifth game, so you got $2$ more points (since you won also the previous game).
An optimal way to cheat is to change the outcomes of the first, second, third and sixth game. Doing so, you end up winning all games (the string of the outcomes becomes WWWWWW). Hence, the new score is $11 = 1+2+2+2+2+2$: $1$ point for the first game and $2$ points for all the other games.
|
T = int(input())
for _ in range(0, T):
n, k = map(int, input().split())
L = input()
s = []
for i in range(0, len(L)):
s.append(L[i])
ptr = len(s)
for i in range(0, len(s)):
if s[i] == "W":
ptr = i
break
dp = []
c = 0
st = ptr
for i in range(ptr + 1, len(s)):
if s[i] == "W":
if c > 0:
dp.append([c, st + 1, i])
st = i
c = 0
else:
c += 1
dp.sort()
fnl = []
for i in range(0, len(dp)):
cnt = dp[i][0]
j1 = dp[i][1]
j2 = dp[i][2]
for j in range(j1, j2):
fnl.append(j)
for i in range(st + 1, len(s)):
fnl.append(i)
for i in range(ptr - 1, -1, -1):
fnl.append(i)
for i in range(0, min(k, len(fnl))):
s[fnl[i]] = "W"
ans = 0
prev = 0
for i in range(0, len(s)):
if s[i] == "W":
if prev == 1:
ans += 2
else:
ans += 1
prev = 1
else:
prev = 0
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You like playing chess tournaments online.
In your last tournament you played $n$ games. For the sake of this problem, each chess game is either won or lost (no draws). When you lose a game you get $0$ points. When you win you get $1$ or $2$ points: if you have won also the previous game you get $2$ points, otherwise you get $1$ point. If you win the very first game of the tournament you get $1$ point (since there is not a "previous game").
The outcomes of the $n$ games are represented by a string $s$ of length $n$: the $i$-th character of $s$ is W if you have won the $i$-th game, while it is L if you have lost the $i$-th game.
After the tournament, you notice a bug on the website that allows you to change the outcome of at most $k$ of your games (meaning that at most $k$ times you can change some symbol L to W, or W to L). Since your only goal is to improve your chess rating, you decide to cheat and use the bug.
Compute the maximum score you can get by cheating in the optimal way.
-----Input-----
Each test contains multiple test cases. The first line contains an integer $t$ ($1\le t \le 20,000$) β the number of test cases. The description of the test cases follows.
The first line of each testcase contains two integers $n, k$ ($1\le n\le 100,000$, $0\le k\le n$) β the number of games played and the number of outcomes that you can change.
The second line contains a string $s$ of length $n$ containing only the characters W and L. If you have won the $i$-th game then $s_i=\,$W, if you have lost the $i$-th game then $s_i=\,$L.
It is guaranteed that the sum of $n$ over all testcases does not exceed $200,000$.
-----Output-----
For each testcase, print a single integer β the maximum score you can get by cheating in the optimal way.
-----Example-----
Input
8
5 2
WLWLL
6 5
LLLWWL
7 1
LWLWLWL
15 5
WWWLLLWWWLLLWWW
40 7
LLWLWLWWWLWLLWLWWWLWLLWLLWLLLLWLLWWWLWWL
1 0
L
1 1
L
6 1
WLLWLW
Output
7
11
6
26
46
0
1
6
-----Note-----
Explanation of the first testcase. Before changing any outcome, the score is $2$. Indeed, you won the first game, so you got $1$ point, and you won also the third, so you got another $1$ point (and not $2$ because you lost the second game).
An optimal way to cheat is to change the outcomes of the second and fourth game. Doing so, you end up winning the first four games (the string of the outcomes becomes WWWWL). Hence, the new score is $7=1+2+2+2$: $1$ point for the first game and $2$ points for the second, third and fourth game.
Explanation of the second testcase. Before changing any outcome, the score is $3$. Indeed, you won the fourth game, so you got $1$ point, and you won also the fifth game, so you got $2$ more points (since you won also the previous game).
An optimal way to cheat is to change the outcomes of the first, second, third and sixth game. Doing so, you end up winning all games (the string of the outcomes becomes WWWWWW). Hence, the new score is $11 = 1+2+2+2+2+2$: $1$ point for the first game and $2$ points for all the other games.
|
for _ in range(int(input())):
n, k = map(int, input().split())
s = list(input())
l = []
prev = 0
c = 0
f = 0
for i in range(n):
if s[i] == "L":
if f == 0:
ind = i
f = 1
prev = i
c += 1
else:
if c != 0:
l.append([c, ind, prev])
c = 0
f = 0
if c != 0:
l.append([c, ind, prev])
if s.count("L") <= k:
print(2 * n - 1)
elif s.count("L") == n:
print(max(2 * k - 1, 0))
else:
l.sort(key=lambda x: x[0])
for i in range(len(l)):
if l[i][1] == 0 or l[i][-1] == n - 1:
continue
for j in range(l[i][1], l[i][-1] + 1):
if k == 0:
break
s[j] = "W"
k = k - 1
if k == 0:
break
ans = 0
f = 0
for i in range(n):
if s[i] == "L":
f = 0
elif f == 0:
ans += 1
f = 1
else:
ans += 2
if k == 0:
print(ans)
else:
print(ans + k * 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 ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR IF FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER
|
You like playing chess tournaments online.
In your last tournament you played $n$ games. For the sake of this problem, each chess game is either won or lost (no draws). When you lose a game you get $0$ points. When you win you get $1$ or $2$ points: if you have won also the previous game you get $2$ points, otherwise you get $1$ point. If you win the very first game of the tournament you get $1$ point (since there is not a "previous game").
The outcomes of the $n$ games are represented by a string $s$ of length $n$: the $i$-th character of $s$ is W if you have won the $i$-th game, while it is L if you have lost the $i$-th game.
After the tournament, you notice a bug on the website that allows you to change the outcome of at most $k$ of your games (meaning that at most $k$ times you can change some symbol L to W, or W to L). Since your only goal is to improve your chess rating, you decide to cheat and use the bug.
Compute the maximum score you can get by cheating in the optimal way.
-----Input-----
Each test contains multiple test cases. The first line contains an integer $t$ ($1\le t \le 20,000$) β the number of test cases. The description of the test cases follows.
The first line of each testcase contains two integers $n, k$ ($1\le n\le 100,000$, $0\le k\le n$) β the number of games played and the number of outcomes that you can change.
The second line contains a string $s$ of length $n$ containing only the characters W and L. If you have won the $i$-th game then $s_i=\,$W, if you have lost the $i$-th game then $s_i=\,$L.
It is guaranteed that the sum of $n$ over all testcases does not exceed $200,000$.
-----Output-----
For each testcase, print a single integer β the maximum score you can get by cheating in the optimal way.
-----Example-----
Input
8
5 2
WLWLL
6 5
LLLWWL
7 1
LWLWLWL
15 5
WWWLLLWWWLLLWWW
40 7
LLWLWLWWWLWLLWLWWWLWLLWLLWLLLLWLLWWWLWWL
1 0
L
1 1
L
6 1
WLLWLW
Output
7
11
6
26
46
0
1
6
-----Note-----
Explanation of the first testcase. Before changing any outcome, the score is $2$. Indeed, you won the first game, so you got $1$ point, and you won also the third, so you got another $1$ point (and not $2$ because you lost the second game).
An optimal way to cheat is to change the outcomes of the second and fourth game. Doing so, you end up winning the first four games (the string of the outcomes becomes WWWWL). Hence, the new score is $7=1+2+2+2$: $1$ point for the first game and $2$ points for the second, third and fourth game.
Explanation of the second testcase. Before changing any outcome, the score is $3$. Indeed, you won the fourth game, so you got $1$ point, and you won also the fifth game, so you got $2$ more points (since you won also the previous game).
An optimal way to cheat is to change the outcomes of the first, second, third and sixth game. Doing so, you end up winning all games (the string of the outcomes becomes WWWWWW). Hence, the new score is $11 = 1+2+2+2+2+2$: $1$ point for the first game and $2$ points for all the other games.
|
for _ in range(int(input())):
n, k = map(int, input().split())
inp = input().lower()
k = min(k, inp.count("l"))
ans = inp.count("w") + tuple(zip(inp, "l" + inp)).count("ww") + k * 2
if "w" in inp:
inp2 = []
cur = -1
for c in inp:
if cur != -1:
if c == "l":
cur += 1
else:
inp2.append(cur)
if c == "w":
cur = 0
inp2.sort()
for inp2i in inp2:
if inp2i > k:
break
k -= inp2i
ans += 1
else:
ans = max(ans - 1, 0)
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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR STRING FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP STRING VAR STRING BIN_OP VAR NUMBER IF STRING VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER IF VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
|
You like playing chess tournaments online.
In your last tournament you played $n$ games. For the sake of this problem, each chess game is either won or lost (no draws). When you lose a game you get $0$ points. When you win you get $1$ or $2$ points: if you have won also the previous game you get $2$ points, otherwise you get $1$ point. If you win the very first game of the tournament you get $1$ point (since there is not a "previous game").
The outcomes of the $n$ games are represented by a string $s$ of length $n$: the $i$-th character of $s$ is W if you have won the $i$-th game, while it is L if you have lost the $i$-th game.
After the tournament, you notice a bug on the website that allows you to change the outcome of at most $k$ of your games (meaning that at most $k$ times you can change some symbol L to W, or W to L). Since your only goal is to improve your chess rating, you decide to cheat and use the bug.
Compute the maximum score you can get by cheating in the optimal way.
-----Input-----
Each test contains multiple test cases. The first line contains an integer $t$ ($1\le t \le 20,000$) β the number of test cases. The description of the test cases follows.
The first line of each testcase contains two integers $n, k$ ($1\le n\le 100,000$, $0\le k\le n$) β the number of games played and the number of outcomes that you can change.
The second line contains a string $s$ of length $n$ containing only the characters W and L. If you have won the $i$-th game then $s_i=\,$W, if you have lost the $i$-th game then $s_i=\,$L.
It is guaranteed that the sum of $n$ over all testcases does not exceed $200,000$.
-----Output-----
For each testcase, print a single integer β the maximum score you can get by cheating in the optimal way.
-----Example-----
Input
8
5 2
WLWLL
6 5
LLLWWL
7 1
LWLWLWL
15 5
WWWLLLWWWLLLWWW
40 7
LLWLWLWWWLWLLWLWWWLWLLWLLWLLLLWLLWWWLWWL
1 0
L
1 1
L
6 1
WLLWLW
Output
7
11
6
26
46
0
1
6
-----Note-----
Explanation of the first testcase. Before changing any outcome, the score is $2$. Indeed, you won the first game, so you got $1$ point, and you won also the third, so you got another $1$ point (and not $2$ because you lost the second game).
An optimal way to cheat is to change the outcomes of the second and fourth game. Doing so, you end up winning the first four games (the string of the outcomes becomes WWWWL). Hence, the new score is $7=1+2+2+2$: $1$ point for the first game and $2$ points for the second, third and fourth game.
Explanation of the second testcase. Before changing any outcome, the score is $3$. Indeed, you won the fourth game, so you got $1$ point, and you won also the fifth game, so you got $2$ more points (since you won also the previous game).
An optimal way to cheat is to change the outcomes of the first, second, third and sixth game. Doing so, you end up winning all games (the string of the outcomes becomes WWWWWW). Hence, the new score is $11 = 1+2+2+2+2+2$: $1$ point for the first game and $2$ points for all the other games.
|
cases = int(input())
for t in range(cases):
n, k = list(map(int, input().split()))
s = input()
fw = s.find("W")
lw = s.rfind("W")
s = list(s)
ls = []
for i in range(fw + 1, n):
if s[i] == "L" and s[i - 1] == "W":
st = i
elif s[i] == "W" and s[i - 1] == "L":
ls.append([i - st, st, i])
ls = sorted(ls)
f = 0
c = 0
for l, i, j in ls:
for m in range(i, j):
if c < k:
s[m] = "W"
c += 1
else:
f = 1
break
if f == 1:
break
if f == 0:
for i in range(lw + 1, n):
if c < k:
s[i] = "W"
c += 1
else:
f = 1
break
if f == 0:
for i in range(fw - 1, -1, -1):
if c < k:
s[i] = "W"
c += 1
else:
f = 1
break
out = 0
for i in range(n):
if i == 0 and s[i] == "W":
out += 1
elif i > 0 and s[i] == "W" and s[i - 1] == "L":
out += 1
elif i > 0 and s[i] == "W" and s[i - 1] == "W":
out += 2
print(out)
|
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 ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR STRING VAR NUMBER IF VAR NUMBER VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR NUMBER VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You like playing chess tournaments online.
In your last tournament you played $n$ games. For the sake of this problem, each chess game is either won or lost (no draws). When you lose a game you get $0$ points. When you win you get $1$ or $2$ points: if you have won also the previous game you get $2$ points, otherwise you get $1$ point. If you win the very first game of the tournament you get $1$ point (since there is not a "previous game").
The outcomes of the $n$ games are represented by a string $s$ of length $n$: the $i$-th character of $s$ is W if you have won the $i$-th game, while it is L if you have lost the $i$-th game.
After the tournament, you notice a bug on the website that allows you to change the outcome of at most $k$ of your games (meaning that at most $k$ times you can change some symbol L to W, or W to L). Since your only goal is to improve your chess rating, you decide to cheat and use the bug.
Compute the maximum score you can get by cheating in the optimal way.
-----Input-----
Each test contains multiple test cases. The first line contains an integer $t$ ($1\le t \le 20,000$) β the number of test cases. The description of the test cases follows.
The first line of each testcase contains two integers $n, k$ ($1\le n\le 100,000$, $0\le k\le n$) β the number of games played and the number of outcomes that you can change.
The second line contains a string $s$ of length $n$ containing only the characters W and L. If you have won the $i$-th game then $s_i=\,$W, if you have lost the $i$-th game then $s_i=\,$L.
It is guaranteed that the sum of $n$ over all testcases does not exceed $200,000$.
-----Output-----
For each testcase, print a single integer β the maximum score you can get by cheating in the optimal way.
-----Example-----
Input
8
5 2
WLWLL
6 5
LLLWWL
7 1
LWLWLWL
15 5
WWWLLLWWWLLLWWW
40 7
LLWLWLWWWLWLLWLWWWLWLLWLLWLLLLWLLWWWLWWL
1 0
L
1 1
L
6 1
WLLWLW
Output
7
11
6
26
46
0
1
6
-----Note-----
Explanation of the first testcase. Before changing any outcome, the score is $2$. Indeed, you won the first game, so you got $1$ point, and you won also the third, so you got another $1$ point (and not $2$ because you lost the second game).
An optimal way to cheat is to change the outcomes of the second and fourth game. Doing so, you end up winning the first four games (the string of the outcomes becomes WWWWL). Hence, the new score is $7=1+2+2+2$: $1$ point for the first game and $2$ points for the second, third and fourth game.
Explanation of the second testcase. Before changing any outcome, the score is $3$. Indeed, you won the fourth game, so you got $1$ point, and you won also the fifth game, so you got $2$ more points (since you won also the previous game).
An optimal way to cheat is to change the outcomes of the first, second, third and sixth game. Doing so, you end up winning all games (the string of the outcomes becomes WWWWWW). Hence, the new score is $11 = 1+2+2+2+2+2$: $1$ point for the first game and $2$ points for all the other games.
|
def solve():
n, m = map(int, input().split())
s = input()
s = s[:n]
p = 0
gaps = []
points = 0
lastsect = 0
firstsect = 0
wins = len(list(filter(lambda x: x == "W", s)))
if wins == 0:
m = min(m, n)
if m == 0:
print(0)
else:
print(2 * m - 1)
return
if m + wins >= n:
print(2 * n - 1)
return
i = 0
while i < n and s[i] == "L":
i += 1
firstsect = i
for j, v in enumerate(s[i:]):
if v == "L":
p += 1
else:
if p > 0:
gaps.append(p)
p = 0
points += 1 + (1 if j + i > 0 and s[j + i - 1] == "W" else 0)
if p > 0:
lastsect = p
gaps.sort()
i = 0
k = m
while i < len(gaps) and k > 0:
if gaps[i] > k:
points += 2 * k
k = 0
else:
points += 2 * gaps[i] + 1
k -= gaps[i]
i += 1
points += 2 * min(k, firstsect)
k -= min(k, firstsect)
points += 2 * min(k, lastsect)
print(points)
def main():
t = 1
t = int(input())
for _ in range(t):
solve()
main()
|
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR STRING VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER STRING NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.