description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
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 = map(int, input().split()) s = input() s = s.replace("WL", "W L").replace("LW", "L W") s = s.split() ls = [len(li) for li in s if li[0] == "L"] wstr = 0 for si in s: wstr += si[0] == "W" wins = n - sum(ls) if wins + k >= n: print(n * 2 - 1) continue if not wins: if k == 0: print(k) else: print(2 * k - 1) continue wins += k if s[0][0] == "L": ls[0] = 10000000000.0 if s[-1][0] == "L": ls[-1] = 10000000000.0 ls.sort() for li in ls: if li > k: break wstr -= 1 k -= li print(wins * 2 - wstr)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING STRING STRING STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER STRING ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER STRING ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR IF VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER NUMBER IF VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER NUMBER EXPR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER 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(record, length, changes): if record == "L" * length: return changes * 2 - 1 if changes > 0 else 0 state = "N" loss_runs = [] win_runs = [] counter = 0 bonus = 0 score = 0 for i in record: if state == "N": if i == "L": bonus += 1 else: state = "W" counter += 1 elif state == "W": if i == "W": counter += 1 else: win_runs.append(counter) counter = 1 state = "L" elif i == "L": counter += 1 else: loss_runs.append(counter) counter = 1 state = "W" if state == "W": win_runs.append(counter) else: bonus += counter for i in win_runs: score += 2 * i - 1 loss_runs = sorted(loss_runs) for i in loss_runs: if changes >= i: score += 2 * i + 1 changes -= i else: score += 2 * changes changes = 0 if changes != 0: score += min(bonus * 2, changes * 2) return score t = int(input()) for _ in range(t): l, c = [int(i) for i in input().split()] arr = input() print(solve(arr, l, c))
FUNC_DEF IF VAR BIN_OP STRING VAR RETURN VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING IF VAR STRING VAR NUMBER ASSIGN VAR STRING VAR NUMBER IF VAR STRING IF VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING IF VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING IF VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR 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 FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR 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.
for __ in range(int(input())): n, m = list(map(int, input().split())) ar = list(input()) kek = [] ans = 0 if "W" in ar: i = ar.index("W") flag = 0 cnt = 0 while i < n: if ar[i] == "L" and flag == 0: flag = 1 cnt = 1 elif ar[i] == "L": cnt += 1 elif ar[i] == "W" and flag == 1: kek.append(cnt) flag = 0 i += 1 else: ans -= 1 kek.sort() bebe = m j = 0 while bebe > 0 and j < len(kek): if bebe - kek[j] >= 0: ans += kek[j] * 2 + 1 else: break bebe -= kek[j] j += 1 ans += 2 * (bebe - max(0, m - ar.count("L"))) for i in range(n): if i == 0: if ar[i] == "W": ans += 1 elif ar[i] == ar[i - 1] == "W": ans += 2 elif ar[i] == "W": ans += 1 print(max(ans, 0))
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 LIST ASSIGN VAR NUMBER IF STRING VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER 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.
for nt in range(int(input())): n, k = map(int, input().split()) f = input() if "W" not in f: if k != 0: print(1 + 2 * (k - 1)) else: print(0) continue if f.count("L") <= k: print(1 + 2 * (n - 1)) continue s = f.split("W") start, end = s[0], s[-1] s.pop() s.pop(0) s.sort() ans = 0 for i in s: if len(i) == 0: continue if len(i) <= k: ans += 2 * len(i) + 1 k -= len(i) else: ans += 2 * k k = 0 ans += k * 2 for i in range(n): if f[i] == "W": if i == 0 or f[i - 1] == "L": ans += 1 else: ans += 2 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF STRING VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR BIN_OP NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NUMBER VAR BIN_OP VAR NUMBER 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.
for _ in range(int(input())): n, k = map(int, input().split()) string = input() string = "LL" + string wcount = 0 lcount = 0 wlw = [] pre1 = "L" pre2 = "L" wstart = -100000000000 for j, i in enumerate(string[2:]): if i == "W": wcount += 1 wstart = j - wstart if wstart > 1: wlw.append(wstart - 1) else: wcount += 1 wstart = j else: lcount += 1 wlw = wlw[1:] wlw.sort() result = -1 if k <= 0: result = wcount elif lcount <= k: result = n * 2 - 1 else: kcopy = k val = 0 for i in wlw: if i <= kcopy: val += i * 2 + 1 kcopy -= i else: break val += kcopy * 2 result = wcount + val if wcount == 0: result -= 1 print(result)
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 BIN_OP STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR STRING VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF 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 i in range(0, t): n, m = list(map(int, input().split())) z = input() a = [z[j] for j in range(0, n)] s = 0 cw = 0 q = 0 b = [] d = [] for j in range(0, n): if a[j] == "W": cw = cw + 1 b.append(j) if q == 1: s = s + 2 if q == 0: s = s + 1 q = 1 else: q = 0 g = n - cw if m == 0: print(s) continue if cw == 0: print(2 * m - 1) continue elif g <= m: s = 1 + 2 * (n - 1) elif g > m: le = len(b) for j in range(1, le): c = b[j] - b[j - 1] - 1 if c > 0: d.append(c) d.sort() le = len(d) for j in range(0, le): if d[j] <= m: s = s + 2 * d[j] + 1 m = m - d[j] else: break s = s + 2 * m print(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP 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.
import sys lines = sys.stdin.readlines() cur_line = -1 def input(): global lines, cur_line cur_line += 1 return lines[cur_line][:-1] for _ in range(int(input())): n, k = map(int, input().split()) s = input() base = 0 gaps = [] for i in range(n): if s[i] == "W": base += 1 if i == 0 or s[i - 1] == "L" else 2 elif s[i] == "L": if i == 0 or s[i - 1] == "W": gaps.append(1) else: gaps[-1] += 1 if len(gaps) > 0: if s[0] == "L": gaps[0] = 9999999 if s[-1] == "L": gaps[-1] = 9999999 gaps.sort() c = 0 su = 0 for i in gaps: su += i if su <= k: base += 1 t = sum(1 for c in s if c == "L") off = 1 if t == n else 0 print(max(0, base + min(k, t) * 2 - off))
IMPORT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF VAR NUMBER RETURN VAR VAR NUMBER 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 NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR NUMBER VAR BIN_OP VAR NUMBER STRING NUMBER NUMBER IF VAR VAR STRING IF VAR NUMBER VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR VAR STRING ASSIGN VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER 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 T = int(sys.stdin.readline()) for t in range(0, T): n, k = list(map(int, sys.stdin.readline().strip().split())) s = sys.stdin.readline().strip() ans = 0 v = False k2 = k x = 0 y = 0 if s[0] == "W": y = 1 ans = 1 v = True else: x = 1 L = [] for i in range(1, n): if s[i] == "W": if x > 0: L.append(x) if v == True: ans = ans + 2 else: ans = ans + 1 v = True x = 0 y = y + 1 else: v = False x = x + 1 L2 = [] if x > 0: L2.append(x) if s[0] == "L" and len(L) > 0: L2.append(L[0]) L = L[1:] L.sort() L.reverse() while k > 0 and len(L) > 0: l = L.pop() if l <= k: k = k - l ans = ans + 2 * l + 1 else: ans = ans + 2 * k k = 0 if k > 0: ans = ans + min(sum(L2), k) * 2 if y > 0: print(ans) else: print(max(0, 2 * k2 - 1))
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR WHILE VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL 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.
t = int(input()) for _ in range(t): n, k = map(int, input().split()) a = input() if k == 0: c = a.split("L") ans = 0 for i in range(len(c)): if len(c[i]) >= 1: ans = ans + ((len(c[i]) - 1) * 2 + 1) print(ans) continue c = a.split("W") counter = {} for i in range(1, len(c) - 1): if len(c[i]) > 0: zz = len(c[i]) if zz in counter: counter[zz] += 1 else: counter[zz] = 1 ans = 0 counter1 = [] for i in counter: counter1.append([i, counter[i]]) if len(c) == 1: print(max((min(len(c[0]), k) - 1) * 2 + 1, 0)) continue counter = counter1 counter.sort() for i in range(len(counter)): if counter[i][0] * counter[i][1] <= k: k -= counter[i][1] * counter[i][0] ans += ((counter[i][0] - 1) * 2 + 3) * counter[i][1] else: ans1 = k // counter[i][0] ans += ((counter[i][0] - 1) * 2 + 3) * ans1 ans1 = k % counter[i][0] k = 0 ans += ans1 * 2 break ramu = c[0].count("L") + c[-1].count("L") h = min(ramu, k) ans += h * 2 c = a.split("L") for i in range(len(c)): if len(c[i]) >= 1: ans = ans + ((len(c[i]) - 1) * 2 + 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 FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER STRING FUNC_CALL VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER 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.
import sys input = sys.stdin.readline def print(val): sys.stdout.write(str(val) + "\n") def prog(): for _ in range(int(input())): n, k = map(int, input().split()) s = list(input().strip() + "W") bounds = [] if s[0] == "L": bounds.append([1, 1, 0]) for i in range(len(s) - 1): if s[i] == "L": if s[i + 1] == "L": bounds[-1][1] += 1 if s[i] == "W": if s[i + 1] == "L": bounds.append([0, 1, i + 1]) if s[-2] == "L": bounds[-1][0] = 2 bounds.sort() for i in range(len(bounds)): if k >= bounds[i][1]: k -= bounds[i][1] for j in range(bounds[i][2], bounds[i][2] + bounds[i][1]): s[j] = "W" else: if bounds[i][0] == 1: for j in range( bounds[i][2] + bounds[i][1] - 1, bounds[i][2] + bounds[i][1] - 1 - k, -1, ): s[j] = "W" else: for j in range(bounds[i][2], bounds[i][2] + k): s[j] = "W" break tot = 0 for i in range(len(s) - 1): tot += (s[i] == "W") + (s[i] == "W" and i > 0 and s[i - 1] == "W") print(tot) prog()
IMPORT ASSIGN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST IF VAR NUMBER STRING EXPR FUNC_CALL VAR LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER NUMBER NUMBER IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR LIST NUMBER NUMBER BIN_OP VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR STRING IF VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR STRING FOR VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR VAR STRING VAR VAR STRING VAR NUMBER VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR 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.
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n, k = map(int, input().split()) s = input() streaks = [] firstwin = -1 for i in range(n): if s[i] == "W": firstwin = i break if firstwin == -1: print(max(0, min(2 * n - 1, 2 * k - 1))) continue ans = 0 curr = 0 for i in range(firstwin, n): if s[i] == "W": if i > 0 and s[i - 1] == "W": ans += 2 else: ans += 1 if curr > 0: streaks.append(curr) curr = 0 else: curr += 1 streaks.sort() m = len(streaks) pointer = -1 while pointer + 1 < m and k >= streaks[pointer + 1]: ans += streaks[pointer + 1] * 2 + 1 k -= streaks[pointer + 1] pointer += 1 ans += 2 * k print(min(ans, 2 * n - 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 VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING IF VAR NUMBER VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR 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, x = map(int, input().split()) l = list(input()) t = [] temp = x c = 0 m = 0 s = 0 f = 0 m2 = 0 e = 0 for i in range(1, n): if l[i] == "W": if l[i - 1] == "W": s += 2 else: s += 1 if l[0] == "W": s += 1 for i in range(n): if l[i] == "L": if i == 0: f = 1 c += 1 else: if c != 0 and f == 0: t.append(c) if f == 1: m2 = c c = 0 f = 0 if c != 0: if f == 1: e = 1 m2 = c else: m = c c = 0 t.sort() for i in t: if x >= i: s += 2 * i s += 1 x -= i else: s += 2 * x x -= x if x != 0 and m != 0: if x >= m: s += 2 * m x -= m else: s += 2 * x x -= x if x != 0 and m2 != 0: if x >= m2: s += 2 * m2 x -= m2 else: s += 2 * x x -= x if e == 1: if temp != 0: print(2 * (temp - 1) + 1) else: print(0) else: print(s)
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 VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER IF VAR NUMBER STRING VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR BIN_OP NUMBER VAR VAR NUMBER VAR VAR VAR BIN_OP NUMBER VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP NUMBER VAR VAR VAR VAR BIN_OP NUMBER VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP NUMBER VAR VAR VAR VAR BIN_OP NUMBER VAR VAR VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL 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 = input() K = k s = [] for i in S: s.append(i) a = [] if s[0] == "L": ans = 0 else: ans = 1 for i in range(1, n): if s[i] == "W": ans += 1 if s[i - 1] == "W": ans += 1 cnt = 0 for i in range(n): if s[i] == "L": cnt += 1 else: if cnt != 0: a.append(cnt) cnt = 0 if cnt != 0: a.append(cnt) if len(a) != 0 and s[0] == "L": a.remove(a[0]) if len(a) != 0 and s[-1] == "L": a.remove(a[-1]) mx = a.count(0) a.sort() for i in a: if i <= k: k -= i ans += 1 + 2 * i else: ans += 2 * k k = 0 break cnt = 0 for i in range(n - 1, -1, -1): if s[i] == "L": cnt += 1 else: break mn = min(k, cnt) k -= mn ans += 2 * mn cnt = 0 for i in range(n): if s[i] == "L": cnt += 1 else: break mn = min(k, cnt) ans += 2 * mn if s.count("W") == 0: mn = min(K, n) if mn >= 1: ans = 1 + 2 * (mn - 1) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST IF VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR VAR VAR BIN_OP NUMBER BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER VAR IF FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP 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.
t = int(input()) def solve(s, k): gaps = [] last_pos = None if s.count("L") == len(s): if k: return k * 2 - 1 else: return 0 for i, x in enumerate(s): if x == "W": if last_pos is not None: gaps.append((i - last_pos - 1, last_pos + 1, i)) last_pos = i gaps.sort(reverse=True) score = 0 while gaps and k: width, start, end = gaps.pop() if width <= k and width: score += 1 k -= width score += width * 2 if k: score += 2 * k return score for case in range(t): n, k = map(int, input().split()) s = input() k = min(k, s.count("L")) score = 0 for i, x in enumerate(s): score += x == "W" if x == "W" and i > 0: score += s[i - 1] == "W" print(score + solve(s, k))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NONE IF FUNC_CALL VAR STRING FUNC_CALL VAR VAR IF VAR RETURN BIN_OP BIN_OP VAR NUMBER NUMBER RETURN NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING IF VAR NONE EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR IF VAR VAR VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP NUMBER VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR STRING IF VAR STRING VAR NUMBER VAR VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL 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.
import sys t = int(input()) for _ in range(t): n, k = map(int, input().split()) s = input() i = 0 l = [] el = 0 while i < n and s[i] == "L": i += 1 el += 1 j = n - 1 while j > i and s[j] == "L": j -= 1 el += 1 if i > j: print(max(0, 2 * k - 1)) continue ans = 0 while i <= j: c = 0 while i <= j and s[i] == "W": i += 1 c += 1 ans += 2 * c - 1 c = 0 while i <= j and s[i] == "L": i += 1 c += 1 if c != 0: l.append(c) l.sort() i, le = 0, len(l) while i < le and k > 0: if k >= l[i]: ans += 2 * l[i] + 1 else: ans += 2 * k k -= l[i] i += 1 print(ans + 2 * max(0, min(k, el)))
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER IF VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER FUNC_CALL VAR NUMBER FUNC_CALL 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())) s = list(input()) ans = 0 for i in range(1, n): if s[i] == "W": if s[i - 1] == "W": ans += 2 else: ans += 1 if s[0] == "W": ans += 1 k = min(k, s.count("L")) diff = [] if s.count("L") == n: print(max(2 * k - 1, 0)) continue start = 0 pre = s.index("W") start = pre for i in range(pre + 1, n): if s[i] == "W": if i - pre - 1 != 0: diff.append(i - pre - 1) pre = i diff.sort() for i in diff: if i <= k: ans += 2 * i + 1 k -= i else: ans += 2 * k k = 0 break if k > 0: ans += 2 * k print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER IF VAR NUMBER STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR LIST IF FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR STRING IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR 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 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.
def solve(): n, k = map(int, input().split()) K = k s = list(input()) if s.count("W") + k > n: print(2 * n - 1) return if s.count("W") == 0: print(min(k, n) * 2 - 1 if k != 0 else 0) return arr = [] streak = 0 i = 0 while i < n: j = i while j < n and s[j] == s[i]: j += 1 if s[i] == "W": streak += 1 elif 0 < i and j < n: arr.append(j - i) i = j arr.sort() for c in arr: if c > k: break streak -= 1 k -= c print(2 * (K + s.count("W")) - streak) return t = int(input()) for _ in range(t): solve()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR STRING VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN IF FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER RETURN ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR STRING VAR NUMBER IF NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP VAR FUNC_CALL VAR STRING VAR RETURN ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR 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.
t = int(input()) final = [] for _ in range(t): n, k = map(int, input().split()) s = input() st = 0 end = n - 1 sumw = s.count("W") if sumw == 0: if k >= n: final.append(str(n * 2 - 1)) elif k: final.append(str(k * 2 - 1)) else: final.append("0") continue for i in range(n): if s[i] == "W": st = i break for i in range(n - 1, -1, -1): if s[i] == "W": end = i break temp = 0 sums = [] ans = 0 flag = 0 for i in range(st, end + 1): if s[i] == "L": temp += 1 flag = 0 else: ans += 1 + flag flag = 1 if temp: sums.append(temp) temp = 0 sums.sort() for i in sums: if k >= i: ans += i * 2 + 1 k -= i else: ans += k * 2 k = 0 left = st + n - 1 - end if k >= left: ans += left * 2 else: ans += k * 2 final.append(str(ans)) print("\n".join(final))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING 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 NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR ASSIGN 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 ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING 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 t in range(int(input())): n, k = map(int, input().split()) h = input() if k >= h.count("L"): print(2 * n - 1) elif n == 0: print(0) elif "W" not in h: print(max(2 * k - 1, 0)) else: h = h.strip("L") n = len(h) score = 0 prev = "L" i = 0 l = [] while i < n: if h[i] == "W": if prev == "L": prev = "W" score += 1 else: score += 2 elif prev == "W": l.append(1) prev = "L" else: l[-1] += 1 i += 1 l.sort() while l and k >= l[0]: temp = l.pop(0) score += 2 * temp + 1 k -= temp score += 2 * k print(score)
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 IF VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF STRING VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR IF VAR VAR STRING IF VAR STRING ASSIGN VAR STRING VAR NUMBER VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR STRING VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR 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.
for _ in range(int(input())): n, k = map(int, input().split()) a = input() b = [] b1 = [] c = [] t = 0 count = 0 ans = 0 m = n - 1 if a[0] == "L": while t < n and a[t] == "L": count += 1 t += 1 b1.append(count) count = 0 if a[-1] == "L": while m >= 0 and a[m] == "L": count += 1 m -= 1 b1.append(count) count = 0 while t <= m: if a[t] == "W": while t <= m and a[t] == "W": count += 1 t += 1 c.append(count) count = 0 else: while t <= m and a[t] == "L": count += 1 t += 1 b.append(count) count = 0 for i in c: ans += 2 * i - 1 b.sort() if b1 and b1[-1] == n: ans += max(min(n, k) * 2 - 1, 0) else: for i in b: if i <= k: ans += 2 * i + 1 k -= i else: ans += k * 2 k = 0 if not k: break if k and b1: ans += min(sum(b1), k) * 2 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER STRING WHILE VAR VAR VAR VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER STRING WHILE VAR NUMBER VAR VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR STRING WHILE VAR VAR VAR VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR IF VAR VAR VAR BIN_OP FUNC_CALL VAR 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.
def calculate(game): if not game: return 0 su = 1 if game[0] == "W" else 0 prev = game[0] for g in game[1:]: if g == "W": su += 2 if prev == "W" else 1 prev = "W" else: prev = "L" return su for t in range(int(input())): n, k = map(int, input().split()) games = input() gaps = list(map(len, games.split("W"))) front_end = [] bonus = 0 if len(gaps): front_end.append(gaps.pop(0)) if len(gaps): front_end.append(gaps.pop()) else: bonus = -1 if k else 0 front_end = [f for f in front_end if f] front_end.sort() gaps = [g for g in gaps if g] gaps.sort() while k: midgap = 0 if gaps: top = gaps.pop(0) midgap = 1 elif front_end: top = front_end.pop(0) else: break if top > k: bonus += k * 2 k = 0 else: bonus += top * 2 + midgap k -= top print(calculate(games) + bonus)
FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR VAR NUMBER STRING NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR NUMBER IF VAR STRING VAR VAR STRING NUMBER NUMBER ASSIGN VAR STRING ASSIGN VAR STRING RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR WHILE VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL 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, m = map(int, input().split()) s = input() scoreinitial = 0 if s[0] == "W": scoreinitial += 1 for i in range(1, len(s)): if s[i] == "W" and s[i - 1] == "W": scoreinitial += 2 elif s[i] == "W": scoreinitial += 1 paq = [] cuts = s.split("W") extr = [len(x) for x in cuts] nbL = sum(extr) stock = min(m, nbL) if stock == 0: print(scoreinitial) elif nbL < n: paq = sorted(extr[1:-1]) rep = 0 for i in paq: if i <= stock and i > 0: rep += i * 2 + 1 stock -= i rep += 2 * stock print(rep + scoreinitial) else: print(stock * 2 - 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER STRING VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER 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() s = list(s) cw = 0 w = [] idx = -1 cl = 0 fw = -1 lw = -1 ans = 0 for i in range(n): if s[i] == "W": if i > 0 and s[i - 1] == "W": ans += 2 else: ans += 1 if fw == -1: fw = i lw = i cw += 1 if idx != -1: if i - idx - 1: w.append(i - idx - 1) idx = i else: cl += 1 w.sort() for i in w: if k == 0: break if i <= k: k -= i ans += 2 * (i - 1) + 3 else: ans += 2 * k k -= k if k > 0: if k >= cl: ans = 1 + (n - 1) * 2 elif cw == 0: if k >= n: ans = 1 + (n - 1) * 2 k = 0 else: ans = 1 + (k - 1) * 2 k = 0 else: for i in range(lw + 1, n): if k == 0: break ans += 2 k -= 1 if k > 0: for i in range(fw - 1, -1, -1): if k == 0: break ans += 2 k -= 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NUMBER VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR IF VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP NUMBER VAR VAR VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER 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.
for _ in range(int(input())): n, k = [*map(int, input().split())] s = input() c = s.count("W") z = [0] * (c + 1) x = 0 kk = k ans = 0 if c == 0 and k > 0: ans -= 1 for i in range(n): if i == 0: if s[i] == "W": ans += 1 if i != 0: if s[i] == "W": if s[i - 1] == "W": ans += 2 else: ans += 1 if s[i] == "W": x += 1 if s[i] == "L": z[x] += 1 a = z[0] + z[-1] z = z[1:c] z.sort() for i in z: if i == 0: continue elif k - i >= 0: ans += 1 k -= i else: k = 0 break if k > 0: if k >= a: k -= a ans += (kk - k) * 2 else: ans += kk * 2 else: ans += kk * 2 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR NUMBER IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FOR VAR VAR IF VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR 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 = map(int, input().split()) s = input() check = [0] * (n + 1) loose = 0 a, b = 0, 0 wins, win_streak = 0, 1 c = 0 for i in s: if i == "L": c += 1 else: break for i in range(c, n): if s[i] == "W": wins += 1 if loose > 0: check[loose] += 1 loose = 0 else: loose += 1 if i < n - 1: if s[i : i + 2] == "WL" and b == 0: a = 1 if s[i : i + 2] == "LW" and a == 1: b = 1 if a == 1 and b == 1: a = b = 0 win_streak += 1 if n - wins > k: wins += k for i in range(1, n + 1): temp = i * check[i] if k >= temp: k -= temp win_streak -= check[i] else: win_streak -= k // i break print(max(2 * wins - win_streak, 0)) else: print(2 * n - 1)
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 BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR 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.
def main(): t = int(input()) for _ in range(t): n, k = (int(x) for x in input().split()) s = input() print(solver(s, n, k)) def solver(s, n, k): cur_score = getCurScore(s) lcount = s.count("L") if lcount <= k: return 2 * n - 1 elif lcount == n: return max(2 * k - 1, 0) llist = [] i = s.find("W") cur = 0 for c in s[i:]: if c == "L": cur += 1 elif cur != 0: llist.append(cur) cur = 0 llist.sort() point_increase = 0 for x in llist: if x <= k: point_increase += 2 * x + 1 k -= x else: point_increase += 2 * k return cur_score + point_increase point_increase += 2 * k return cur_score + point_increase def getCurScore(s): score = 0 if s[0] == "W": score += 1 for i in range(1, len(s)): if s[i] == "W": if s[i - 1] == "W": score += 2 else: score += 1 return score main()
FUNC_DEF 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR RETURN BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR RETURN FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR BIN_OP NUMBER VAR RETURN BIN_OP VAR VAR VAR BIN_OP NUMBER VAR RETURN BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER STRING VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER RETURN 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.
t = int(input()) for i in range(t): n, k = map(int, input().split()) binstr = list(input()) losearr = [] current = 0 active = 0 wincntr = 0 for result in binstr: if result == "L": if active: wincntr += active active = 0 current += 1 elif current: losearr.append(current) current = 0 active += 1 elif active: active += 2 else: active += 1 wincntr += active start = 0 if current: start += current if binstr[0] == "L" and losearr: start += losearr[0] del losearr[0] losearr.sort() for rest in losearr: if k >= rest: wincntr += rest * 2 + 1 k -= rest else: wincntr += k * 2 k = 0 break if k: if k >= start: if wincntr: wincntr += 2 * start else: wincntr += 2 * start - 1 elif wincntr: wincntr += k * 2 else: wincntr += k * 2 - 1 print(wincntr)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING IF VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR IF VAR NUMBER STRING VAR VAR VAR NUMBER 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 IF VAR IF VAR VAR IF VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR BIN_OP 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.
for _ in range(int(input())): n, k = map(int, input().split()) ar = list(input()) fro = 0 bac = 0 li = [] for i in range(n): if ar[i] == "L": fro += 1 else: break for i in range(1, n + 1): if ar[-i] == "L": bac += 1 else: break win = ar.count("W") if win == 0: print(max(2 * (k - 1) + 1, 0)) elif win == 1: ind = ar.index("W") i = ind + 1 ans = 1 while i < n and k != 0: ans += 2 i += 1 k -= 1 i = ind - 1 while i >= 0 and k != 0: ans += 2 i -= 1 k -= 1 print(ans) else: count = 0 for i in range(fro, n - bac): if ar[i] == "L": count += 1 else: li.append(count) count = 0 li.sort() ans = int(ar[0] == "W") for i in range(1, n): if ar[i] == "W": if ar[i - 1] == "W": ans += 2 else: ans += 1 for i in li: if i <= k and i != 0: ans += 2 * i + 1 k -= i elif i != 0: ans += 2 * k k = 0 for i in [fro, bac]: if i <= k: ans += 2 * i k -= i else: ans += 2 * k k = 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 VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR IF VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR LIST VAR VAR IF VAR VAR VAR BIN_OP NUMBER VAR VAR VAR 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.
for _ in range(int(input())): n, k = list(map(int, input().split())) s = input() k = min(k, s.count("L")) arr = [] cur = 0 sc = 0 se = False if s[0] == "W": sc += 1 for e in s: if e == "L": cur += 1 else: if cur > 0 and se: arr.append(cur) se = True cur = 0 for i in range(1, n): if s[i] == "W": if s[i - 1] == "W": sc += 2 else: sc += 1 arr.sort() arr.reverse() while len(arr) > 0 and arr[-1] <= k: k -= arr[-1] sc += arr[-1] * 2 + 1 arr.pop() sc += k * 2 if k > 0 and s.count("W") == 0: sc -= 1 print(sc)
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 FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER STRING VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER FUNC_CALL VAR STRING 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): n, k = map(int, input().split()) s = input() if all(i == "L" for i in s): print(max(0, 2 * k - 1)) continue f = False if s[0] == "L": f = True cur = s[0] l = [] cnt = 1 f2 = True score = 0 for i in range(1, n): if cur == s[i]: cnt += 1 else: if f2: if f: l.append((cnt, 2 * cnt)) else: score += cnt * 2 - 1 f2 = False elif cur == "W": score += cnt * 2 - 1 else: l.append((cnt, 2 * cnt + 1)) cnt = 1 cur = s[i] if s[-1] == "W": score += 2 * cnt - 1 elif f2: l.append((cnt, 2 * cnt - 1)) else: l.append((cnt, cnt * 2)) l.sort(key=lambda x: x[1] / x[0], reverse=True) cnt = s.count("L") for i, j in l: if k - i < 0: continue else: score += j k -= i cnt -= i print(score + 2 * min(k, cnt))
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 IF FUNC_CALL VAR VAR STRING VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR NUMBER IF VAR IF VAR EXPR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR STRING VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER STRING VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER FUNC_CALL 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 inx in range(t): n, k = map(int, input().split()) l = input() b = sc = 0 g = [] p = "L" for c in l: if c == "W": if p == "W": sc += 2 else: sc += 1 p = "W" if b: g.append(b) b = 0 else: p = "L" b += 1 if b: g.append(b) cbg = cen = 0 if l[0] == "L": cbg = g.pop(0) if l[-1] == "L": if sc: cen = g.pop() g.sort() while k and g: b = g.pop(0) if k >= b: k -= b sc += 2 * b + 1 else: sc += 2 * k k = 0 if k: if sc: sc += 2 * min(k, cbg + cen) else: sc += 2 * min(k, cbg) - 1 print(sc)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR STRING FOR VAR VAR IF VAR STRING IF VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR NUMBER STRING IF VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER IF VAR IF VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR VAR 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.
def chess(string, n): temp = sorted(map(len, string.strip("L").split("W"))) m = len(temp) + n while temp and temp[0] <= n: n -= temp.pop(0) return (2 * min(len(string), m - 1) - len(temp) or 1) - 1 t = int(input()) for i in range(t): a, b = map(int, input().strip().split()) print(chess(input(), b))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR WHILE VAR VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR 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): n, k = map(int, input().split()) s = str(input()) cw = 0 cnt = 0 lcount = -1 llen = dict() for i in range(n): if i == 0 and s[i] == "W": cnt += 1 lcount = 0 cw += 1 elif s[i] == "W": if s[i - 1] == "W": cnt += 2 else: cnt += 1 cw += 1 if lcount > 0: if lcount not in llen.keys(): llen[lcount] = 0 llen[lcount] += 1 lcount = 0 elif lcount < 0: lcount = 0 elif lcount >= 0: lcount += 1 cl = n - cw if k == 0: print(cnt) else: lens = [] for e in llen.keys(): lens.append(e) lens.sort() delta = 0 for l in lens: if k < l: break count = min(k // l, llen[l]) delta += count * (l * 2 + 1) k -= count * l cl -= count * l if k > 0: if k <= cl: if cnt == 0: delta += (k - 1) * 2 + 1 else: delta += k * 2 else: delta += cl * 2 print(cnt + delta)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER IF VAR VAR IF VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER 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.
for x in range(int(input())): n, k = map(int, input().split()) string = list(input()) lose = [] count = 0 score, first, last = 0, 0, 0 for i in range(n - 1): if string[i] == string[i + 1]: count += 1 if i == n - 2: if string[i] == "L": lose.append(count + 1) if string[i] == "W": score = score + count * 2 + 1 break elif string[i] == "W": score = score + count * 2 + 1 count = 0 if i == n - 2: lose.append(1) else: lose.append(count + 1) count = 0 if i == n - 2: score += 1 if n == 1: if string[0] == "W": score = 1 else: last = 1 else: if string[-1] == "L" and lose: last = lose.pop() if string[0] == "L" and lose: first = lose.pop(0) lose = sorted(lose) for i in lose: if k - i < 0: score = score + k * 2 k = 0 break else: k -= i score = score + i * 2 + 1 if k >= last and score > 0: score = score + last * 2 k -= last elif last > k and score > 0: score = score + k * 2 k = 0 elif last > 0 and k >= last: score = score + last * 2 - 1 k -= last elif k > 0 and last > k: score = score + k * 2 - 1 k = 0 if k >= first and score > 0: score = score + first * 2 k -= first elif first > k and score > 0: score = score + k * 2 k = 0 print(score)
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 VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER STRING VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER STRING VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP 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 t in range(int(input())): n, k = [int(i) for i in input().split()] wd = input() sc = 0 if wd[0] == "W": sc += 1 ls = [] l_cnt = 0 for i in range(1, n): if wd[i] == "W": if wd[i - 1] == "W": sc += 2 else: sc += 1 for i in range(n): if wd[i] == "W": if len(ls) == 0 or ls[-1] != 0: ls.append(0) else: l_cnt += 1 if ls: ls[-1] += 1 if l_cnt <= k: print(2 * n - 1) continue if ls: ls.pop() else: print(max(0, 2 * k - 1)) continue sm = 0 ls.sort() ext = 0 for ll in ls: if sm + ll <= k: sm += ll ext += 1 else: break assert sc + 2 * k + ext <= 2 * n - 1 print(sc + 2 * k + ext)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER STRING VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP NUMBER 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())): a, b = map(int, input().split()) s = input() if s == "L" * a: if b: print(b * 2 - 1) else: print(0) elif s == "W" * a: print(a * 2 - 1) else: ans = 0 fst, lst = -1, -1 if s[0] == "W": ans += 1 fst = 0 lst = 0 for i in range(1, a): if s[i] == "W": if fst == -1: fst = i if s[i - 1] == "W": ans += 1 ans += 1 lst = i if b == 0: print(ans) continue k = s[fst : lst + 1].split("W") k.sort() for i in k: if len(i): if b >= len(i): ans += len(i) * 2 + 1 b -= len(i) else: ans += b * 2 b = 0 if b == 0: break ans += min(b, fst + a - lst - 1) * 2 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR BIN_OP STRING VAR IF VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR NUMBER STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING IF VAR NUMBER ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR FOR VAR VAR IF FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR 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.
def solve(): n, k = map(int, input().split()) s = input() wn = 0 for c in s: if c == "W": wn += 1 ln = 0 c = 0 while c < n and s[c] == "L": c += 1 ln += 1 a = c c = len(s) - 1 ln = 0 while c >= 0 and s[c] == "L": c -= 1 ln += 1 b = c ln = 0 d = [(0) for _ in range(n)] for i in range(a, b + 1): if s[i] == "L": ln += 1 else: d[ln] += 1 ln = 0 ls = 0 K = k for i in range(1, len(d)): mn = min(k // i, d[i]) ls += d[i] - mn k = k - mn * i score = 2 * min(n, wn + K) - (ls + 1) score = max(0, score) print(score) def main(): 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 NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR 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.
for w in range(int(input())): n, k = tuple(map(int, input().split())) s = str(input()) if k == n: print(1 + 2 * (n - 1)) elif n == 1 and k > 0: print(1) elif n == 1 and k == 0: if s[0] == "W": print(1) else: print(0) elif k >= s.count("L"): print(1 + 2 * (n - 1)) else: ans = 0 t1 = 0 l = [] for i in range(n): if s[i] == "W": if i == 0 or s[i - 1] != "W": ans += 1 else: ans += 2 i = 0 while i < n: if s[i] == "L": if i == 0: while i < n and s[i] == "L": t1 += 1 i += 1 elif i == n - 1: t1 += 1 i += 1 else: x = 0 while i < n and s[i] == "L": x += 1 i += 1 if i != n: l.append(x) else: t1 += x else: i += 1 if k == 0: print(ans) elif ans == 0: print(1 + 2 * max(k - 1, 0)) elif k >= sum(l): print(ans + 2 * k + len(l)) else: l = sorted(l) x = 0 for i in range(len(l)): x += l[i] if x > k: break print(ans + 2 * k + i)
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 IF VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NUMBER VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR STRING IF VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP NUMBER 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 i in range(int(input())): n, k = map(int, input().split()) lst = input() count = 0 last = -1 ans = [] for i in range(len(lst)): if lst[i] == "L": continue count += 1 if last != -1 and last != i - 1: ans += [i - last - 1] last = i ans.sort() mx = len(ans) + 1 for i in range(len(ans)): if k >= ans[i]: count += ans[i] k -= ans[i] mx -= 1 print(max(0, min(2 * n - 1, (count + k) * 2 - mx)))
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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR LIST BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER 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.
from itertools import groupby for _ in range(int(input())): n, k = map(int, input().split()) s = input() if k >= s.count("L"): print(n * 2 - 1) else: s = s.strip("L") group = [] for i, g in groupby(s): if i == "L": group.append(len(list(g))) group.sort() i, m = 0, len(group) r = k while i < m and r >= group[i]: r -= group[i] i += 1 ans = (s.count("W") + k) * 2 - (m + 1 - i) print(max(0, ans))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR STRING VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER 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 = map(int, input().split()) s = input()[:n] z = s.count("W") leftL = n - z if z == 0: if k != 0: print(1 + 2 * (k - 1)) else: print(0) else: total = 0 for i in range(n): if s[i] == "W": if i - 1 >= 0: if s[i - 1] == "W": total += 2 else: total += 1 else: total += 1 cont_seg = [] fir, sec = -1, -1 for i in range(n): if s[i] == "W": if fir == -1: fir = i else: sec = i if fir != -1 and sec != -1: temp = sec - fir - 1 if temp > 0: cont_seg.append(temp) fir = sec cont_seg.sort() for i in cont_seg: if i <= k: total += 1 + i * 2 k -= i leftL -= i if leftL != 0 and k != 0: if leftL <= k: total += leftL * 2 elif leftL > k: total += k * 2 print(total) for _ in range(int(input())): solve()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER 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.
t = int(input()) for it in range(0, t): n, k = tuple(list(map(int, input().split(" ")))) results = [char for char in input()] initial_score = 0 loss_amount = 0 for i in range(0, len(results)): if i > 0 and results[i] == "W" and results[i - 1] == "W": initial_score += 1 if results[i] == "L": loss_amount += 1 k = min(k, loss_amount) initial_score += n - loss_amount streak_increase_added_score = 2 * k if loss_amount == n and streak_increase_added_score > 0: streak_increase_added_score -= 1 streak_diffs = [] current_streak_diff = 0 streak_found = 0 for i in range(0, n): if results[i] == "W": streak_found = True if current_streak_diff != 0: streak_diffs.append(current_streak_diff) current_streak_diff = 0 if results[i] == "L" and streak_found: current_streak_diff += 1 sorted_streak_diffs = sorted(streak_diffs) disjoint_streak_decrease_added_score = 0 for i in range(0, len(sorted_streak_diffs)): if k >= sorted_streak_diffs[i]: disjoint_streak_decrease_added_score += 1 k -= sorted_streak_diffs[i] else: break final_score = ( initial_score + streak_increase_added_score + disjoint_streak_decrease_added_score ) print(final_score)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP NUMBER VAR IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR STRING VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP 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()) arr = [0] for i in range(t): n, sm = map(int, input().split()) count = 0 count1 = 0 count2 = -2 a = input() arr.clear() for j in range(n): if a[j] == "W": count += 1 count1 += 1 if count2 + 1 == j: count1 += 1 if count != 1 and count2 + 1 != j: arr.append(j - count2 - 1) count2 = j sm = min(sm, n - count) arr = sorted(arr) if count == 0: print(max(0, 2 * sm - 1)) else: sz = len(arr) for k in range(sz): if sm >= arr[k]: count1 += 2 * arr[k] + 1 sm -= arr[k] else: count1 += 2 * sm sm = 0 print(count1 + 2 * sm) arr.clear()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR 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 ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER 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.
t = int(input()) for _ in range(t): n, k = map(int, input().split()) a = list(input()) m = a.count("L") d = a.count("W") if m <= k: print(2 * (n - 1) + 1) continue if d == 0: print(max(2 * (k - 1) + 1, 0)) else: for i in range(n): if a[i] == "W": b = i break a.reverse() for i in range(n): if a[i] == "W": c = i break j = n - c x = c c = j a.reverse() l = [] s = 0 for i in range(b, c): if a[i] == "L": s = s + 1 elif s == 0: pass else: l.append(s) s = 0 l.sort() ans = 0 for i in range(len(l)): if k >= l[i]: k = k - l[i] ans = ans + 2 * l[i] + 1 else: ans = ans + 2 * k k = 0 break if k == 0: pass else: z = min(k, b + x) ans = ans + 2 * z if a[0] == "W": ans = ans + 1 for i in range(1, n): if a[i] == "W" and a[i - 1] == "W": ans = ans + 2 elif a[i] == "W" and a[i - 1] == "L": ans = ans + 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 FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR 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 test in range(int(input())): [n, k] = list(map(int, input().split(" "))) s = input() w = False l = 0 sl = [] pg = False res = 0 z = 0 ko = 0 for r in s: if r == "W": if pg: res += 2 else: res += 1 pg = True if w: sl.append(l) l = 0 else: w = True elif w: l += 1 pg = False else: z += 1 if len(sl) > 0: sl.sort() c = 0 j = 0 while j < len(sl) and c + sl[j] <= k: c += sl[j] if sl[j] > 0: res += sl[j] * 2 + 1 j += 1 if j == len(sl): res = res + min(k - c, z + l) * 2 else: res = res + (k - c) * 2 elif w: res = res + min(k, z + l) * 2 elif k > 0: res = res + min(k, z) * 2 - 1 print(res)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN LIST VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR 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.
import sys def minp(): return sys.stdin.readline().strip() def mint(): return int(minp()) def mints(): return map(int, minp().split()) def solve(): n, k = mints() i = 0 p = None pos = [] a = list(minp()) for i in range(n): if a[i] == "W": if p is not None: pos.append((i - p - 1, p + 1)) else: first = i p = i pos.sort() for l, f in pos: if k == 0: break for i in range(f, f + l): if k > 0: k -= 1 a[i] = "W" else: break if p is not None: for i in range(p - 1, -1, -1): if a[i] != "W": if k > 0: a[i] = "W" k -= 1 else: break for i in range(n): if a[i] != "W": if k > 0: a[i] = "W" k -= 1 else: break ans = 0 cnt = 0 for i in range(n): if a[i] == "W": cnt = min(cnt + 1, 2) ans += cnt else: cnt = 0 print(ans) for i in range(mint()): solve()
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NONE ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NONE EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR STRING IF VAR NONE FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING IF VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR FOR 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.
import sys for _ in range(int(sys.stdin.readline())): N, k = map(int, sys.stdin.readline().split()) arr = list(map(str, input().strip())) dic = [] temp = k lose = 0 streak = 0 if "W" not in arr: print(max(0, 2 * k - 1)) elif "L" not in arr: print(2 * N - 1) else: for i in range(N): if arr[i] == "L": lose += 1 if arr[i - 1] == "W" or i == 0: dic.append(1) else: dic[len(dic) - 1] += 1 else: if i > 0: if arr[i - 1] == "W": continue streak += 1 first, last = 0, 0 if arr[0] == "L": first = dic[0] if arr[N - 1] == "L": last = dic[len(dic) - 1] dic.sort() for item in dic: if k - item >= 0: if item == first: first = 0 continue elif item == last: last = 0 continue k -= item streak -= 1 else: break wins = min(N, N - lose + temp) print(max(2 * wins - streak, 0))
IMPORT FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF STRING VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER IF STRING VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR NUMBER STRING ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER 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 as _sys WIN = "W" LOSE = "L" def main(): t = int(input()) for i_t in range(t): n, k = _read_ints() s = _read_line() result = find_max_score_after_changing(s, k) print(result) def _read_line(): result = _sys.stdin.readline() assert result[-1] == "\n" return result[:-1] def _read_ints(): return map(int, _read_line().split()) def find_max_score_after_changing(games_results, changes_n): if len(games_results) == 0: return 0 if len(games_results) == 1: if games_results[0] == WIN: result = 1 elif changes_n > 0: result = 1 else: result = 0 return result lose_n = games_results.count(LOSE) changes_n = min(changes_n, lose_n) if lose_n == len(games_results): if changes_n == 0: result = 0 else: result = 2 * changes_n - 1 return result curr_score = 1 if games_results[0] == WIN else 0 for i in range(1, len(games_results)): if games_results[i] == WIN: if games_results[i - 1] == WIN: curr_score += 2 else: curr_score += 1 empty_spaces = games_results.replace(WIN, " ").split() if games_results[0] == LOSE: empty_spaces = empty_spaces[1:] if games_results[-1] == LOSE: empty_spaces = empty_spaces[:-1] distances_between_islands = sorted(map(len, empty_spaces)) changes_lost = 0 islands_united = 0 for distance in distances_between_islands: if distance <= changes_n: changes_lost += distance changes_n -= distance islands_united += 1 else: changes_lost += changes_n changes_n = 0 break curr_score += (changes_lost + changes_n) * 2 curr_score += islands_united return curr_score main()
IMPORT ASSIGN VAR STRING ASSIGN VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER STRING RETURN VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN VAR ASSIGN VAR VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR RETURN 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.
for _ in range(int(input())): n, k = map(int, input().split()) a = input() bet = [] score = 0 counter = 0 total = 0 flag = -1 for x in a: if x == "W": if flag == 0: bet.append(counter) counter = 0 if flag <= 0: score += 1 else: score += 2 flag = 1 else: if flag == 1 or flag == 0: counter += 1 flag = 0 total += 1 if total == n and k > 0: score = -1 else: bet.sort() for x in bet: if x <= k: k = k - x total = total - x score += x * 2 + 1 if k > total: k = total score += k * 2 print(score)
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 LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR VAR 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.
t = int(input()) def binsearch(nums, target): left = 0 right = len(nums) - 1 while left <= right: mid = (left + right) // 2 if nums[mid] == target: return mid elif nums[mid] > target: right = mid - 1 elif nums[mid] < target: left = mid + 1 return left for tc in range(t): n, k = map(int, input().split()) s = input() if n == 1: print(min(1, k + s.count("W"))) continue wins = s.count("W") if k >= n - wins: print(2 * n - 1) continue if wins == 0: print(max(0, 2 * k - 1)) continue gaps = [] last = "0" winstreaks = 1 c = 0 fl = 0 for i in range(n): if s[i] == "L" and not fl: continue if s[i] == "W" and not fl: fl = 1 elif s[i] == "W" and s[i - 1] == "L" and fl: winstreaks += 1 gaps.append(c) c = 0 elif s[i] == "L": c += 1 gaps1 = sorted(gaps) r = 0 rc = 0 while r < len(gaps1): if rc + gaps1[r] <= k: rc += gaps1[r] winstreaks -= 1 r += 1 else: break res = 2 * (k + wins) - winstreaks print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR 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 VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR IF VAR VAR STRING VAR ASSIGN VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP 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.
for j in range(int(input())): n, k = map(int, input().split()) r = input() a = [] t = r[0] s = 1 for i in range(1, n): if r[i] == t: s += 1 else: a.append([t, s]) t = r[i] s = 1 a.append([t, s]) b = [] h = len(a) s = 0 m = 0 t = 0 if a[0][0] == "L": s = 1 if a[-1][0] == "L": h -= 1 for i in range(s, h): if a[i][0] == "W": m += a[i][1] t += 1 else: b.append(a[i][1]) b.sort() i = 0 s = 0 while i < len(b) and s + b[i] <= k: t -= 1 s += b[i] i += 1 m += s if i < len(b) and s < k: m += k - s elif i == len(b) and s < k: f = 0 if a[0][0] == "L": f += a[0][1] if a[-1][0] == "L": f += a[-1][1] if m == 0: t = 1 m += min(k - s, f) print(2 * m - t)
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 LIST ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER IF VAR NUMBER NUMBER STRING VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER STRING VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER NUMBER STRING VAR VAR NUMBER NUMBER IF VAR NUMBER NUMBER STRING VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER 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 cheat(n, k, origames): kk = k Llis, Wlis = origames.split("W"), origames.split("L") Wlis = sorted(Wlis, key=lambda x: len(x)) if Wlis[-1] == "": if 2 * k - 1 >= 0: return 2 * k - 1 else: return 0 if k >= origames.count("L"): return 2 * len(origames) - 1 else: Llis.pop(0) Llis.pop(-1) Llis = sorted(Llis, key=lambda x: len(x)) link = 0 for i in range(len(Llis)): if Llis[i] != "": if len(Llis[i]) <= k: k -= len(Llis[i]) link += 1 else: break score = 0 for j in range(len(Wlis)): if len(Wlis[j]) >= 1: score += 2 * len(Wlis[j]) - 1 score += 2 * kk + link return score t = int(input()) for _ in range(t): n, k = map(int, input().split()) origames = input() print(cheat(n, k, origames))
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER STRING IF BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER RETURN BIN_OP BIN_OP NUMBER VAR NUMBER RETURN NUMBER IF VAR FUNC_CALL VAR STRING RETURN BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR 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 = map(int, input().split()) s = input() diff = [0] * (n + 1) prev = 0 I = 0 J = n - 1 flag1, flag2 = True, True for k in range(n): if s[k] == "W": I = k flag1 = False break for k in range(n - 1, -1, -1): if s[k] == "W": J = k flag2 = False break if flag1 and flag2: if K > 0: print(2 * K - 1) else: print(0) continue prev = I for k in range(I, J + 1): if s[k] == "W": if k > prev + 1: diff[k - prev - 1] += 1 prev = k j = n ans = 0 for i in range(1, n + 1): if K < i: j = i break ans += (2 * i + 1) * min(K // i, diff[i]) temp = diff[i] diff[i] = diff[i] - min(K // i, diff[i]) K = K - i * min(K // i, temp) ans += 2 * min(K, I) K = K - min(K, I) ans += 2 * min(K, n - 1 - J) K = K - min(K, n - 1 - J) for k in range(1, n + 1): if K <= 0: break if diff[k] != 0: ans += 2 * K K = 0 prev = False for i in range(n): if s[i] == "W": if prev: ans += 2 else: ans += 1 prev = True continue prev = False print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR 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.
import sys input = lambda: sys.stdin.readline().strip() for i in range(int(input())): n, k = map(int, input().split()) a = list(input()) ans = [0] lol = [0] lk = 0 if len(a) == 1: if a[0] == "W": print(1) elif k: print(1) else: print(0) continue for i in range(n): if a[i] == "W": if lol[-1] != 0: lol.append(0) lk = 1 if ans[-1] == 0: ans.append(1) else: ans.append(2) else: ans.append(0) lol[-1] += 1 if lol[-1] == 0: del lol[-1] answ = sum(ans) lox = [] if len(lol) > 1: lox = lol[1:-1] popa = 0 if len(lol) == 0: print(answ) continue if a[0] == "W" and lk: lox.append(lol[0]) else: popa = lol[0] if len(lol) == 1: pass elif a[-1] == "W" and lk: lox.append(lol[-1]) else: popa += lol[-1] lox.sort() l = len(lox) for i in range(len(lox)): if k >= lox[i]: k -= lox[i] answ += 2 * lox[i] + 1 else: answ += 2 * k k = 0 k = min(k, popa) if "W" in a: answ += 2 * k print(answ) else: answ += max(0, 2 * k - 1) print(answ)
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER STRING VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER STRING VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF STRING VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR 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.
for _ in range(int(input())): n, k = map(int, input().split()) s = input() ar = [] i = 0 single = [] while i < n and s[i] == "L": i += 1 if i == n: if k > 0: print(k * 2 - 1) else: print(0) continue if i != 0: single.append(i) double = [] f = False cnt = 0 ans = 0 while i < n: if s[i] == "L": if not f: f = True cnt += 1 elif f: double.append(cnt) f = False cnt = 0 ans += 1 else: add = 0 if i > 0 and s[i - 1] == "W": add = 1 ans += 1 + add i += 1 if cnt: single.append(cnt) double.sort() single.sort() i = 0 while k > 0 and i < len(double): if double[i] <= k: k -= double[i] ans += double[i] * 2 + 1 else: ans += k * 2 k = 0 i += 1 i = 0 while k > 0 and i < len(single): if single[i] <= k: k -= single[i] ans += single[i] * 2 else: ans += k * 2 k = 0 i += 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR VAR VAR STRING VAR NUMBER IF VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR STRING IF VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN 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.
def solve(): n, k = map(int, input().split()) s = input() ans = 0 prev = False c = [] cc = 0 for i in range(n): if s[i] == "W": if cc: if cc != i: c.append(cc) cc = 0 if prev: ans += 2 else: ans += 1 prev = True else: prev = False cc += 1 c.sort() for i in range(len(c)): if c[i] <= k: k -= c[i] ans += c[i] * 2 + 1 if "W" in s: ans += k * 2 else: ans += max(k * 2 - 1, 0) ans = min(ans, n * 2 - 1) print(ans) t = int(input()) for _ in range(t): solve()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF STRING VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR 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.
t = int(input()) tests = [] for i in range(t): n, k = map(int, input().split()) s = input() tests.append([n, k, s]) for test in tests: n, k, s = test result = 0 w = s.count("W") if w == 0: if k == 0: print(0) else: print(min(n, k) * 2 - 1) continue results = [] for x in s: if results and results[-1][0] == x: results[-1][1] += 1 else: results.append([x, 1]) first_loses = 0 if results[0][0] == "L": first_loses = results[0][1] results.pop(0) last_loses = 0 if results[-1][0] == "L": last_loses = results[-1][1] results.pop() loses = [] for x, y in results: if x == "W": result += 2 * y - 1 else: loses.append([x, y]) loses.sort() for x, y in loses: if x == "L": if k >= y: result += 2 * y + 1 k -= y else: result += 2 * k k = 0 break if last_loses > 0: if k >= last_loses: result += 2 * last_loses k -= last_loses else: result += 2 * k k = 0 if first_loses > 0: if k >= first_loses: result += 2 * first_loses else: result += 2 * k print(result)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST 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 LIST VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER 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 FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER STRING ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER STRING ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR VAR IF VAR STRING VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR VAR IF VAR STRING IF VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR VAR VAR BIN_OP NUMBER VAR VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR VAR VAR BIN_OP NUMBER VAR 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()) tag = [] for i in range(t): n, k = map(int, input().split()) s = input() cur, ans = k + 5, 0 tag.clear() for j in range(n): if s[j] == "L": cur += 1 else: ans += 1 if cur > 0 else 2 if cur > 0: tag.append(cur) cur = 0 if cur == k + n + 5: print(max(0, 2 * min(k, n) - 1)) continue tag.sort() for j in range(len(tag)): if k >= tag[j]: ans = ans + 2 * tag[j] ans += 1 k -= tag[j] ans += 2 * k ans = min(ans, 2 * n - 1) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR VAR NUMBER NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR 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.
def main(): n, k = map(int, input().split()) line = input() was = False prev = False res = 0 before = 0 spare_sizes = [] sz = 0 for i in line: if not was: if i == "L": before += 1 else: was = True if was: if i == "L": sz += 1 elif sz > 0: spare_sizes.append(sz) sz = 0 if i == "W": if prev: res += 2 else: res += 1 prev = True else: prev = False if was: before += sz if res == 0 and k > 0: res -= 1 spare_sizes = sorted(spare_sizes) for i in range(len(spare_sizes)): if spare_sizes[i] > k: res += 2 * k k = 0 break else: res += 2 * spare_sizes[i] k -= spare_sizes[i] res += 1 if k == 0: break res += 2 * min(k, before) print(res) t = int(input()) for i in range(t): main()
FUNC_DEF ASSIGN VAR 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 ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR IF VAR STRING VAR NUMBER ASSIGN VAR NUMBER IF VAR IF VAR STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR STRING IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP NUMBER VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You 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 = [int(m) for m in input().split()] s = list(input()) firstwin = s.index("W") if "W" in s else -1 if firstwin == -1: print(max(min(k, len(s)) * 2 - 1, 0)) continue left = firstwin noleft = True A = [] front, back = None, None if left != 0: front = 0, left for i in range(firstwin, len(s)): if s[i] == "L" and noleft: left = i noleft = False elif s[i] == "W" and not noleft: A.append((left, i)) noleft = True if not noleft: back = left, len(s) A.sort(key=lambda a: a[1] - a[0]) if k > 0: for a in A: for i in range(*a): k -= 1 s[i] = "W" if k <= 0: break if k <= 0: break if k > 0: if back: for i in range(*back): s[i] = "W" k -= 1 if k <= 0: break if front and k > 0: for i in reversed(range(*front)): s[i] = "W" k -= 1 if k <= 0: break res = 0 if s[0] == "L" else 1 for i in range(1, len(s)): if s[i] == "W" == s[i - 1]: res += 2 elif s[i] == "W": res += 1 print(res)
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 ASSIGN VAR STRING VAR FUNC_CALL VAR STRING NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR NONE NONE IF VAR NUMBER ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR STRING VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR STRING IF VAR NUMBER IF VAR NUMBER IF VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR STRING VAR NUMBER IF VAR NUMBER IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR STRING VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER STRING NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR 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.
import sys readline = sys.stdin.readline T = int(readline()) Ans = [None] * T for qu in range(T): N, K = map(int, readline().split()) S = [(1 if s == "W" else 0) for s in readline().strip()] if all(s == 0 for s in S): Ans[qu] = max(0, 2 * K - 1) continue ans = 0 ctr = 0 st = [] L = [] res = 0 hh = False for i in range(N): s = S[i] if s == 1: if i == 0 or S[i - 1] == 0: ans += 1 else: ans += 2 if ctr: st.append(ctr) ctr = 0 hh = True elif hh: ctr += 1 else: res += 1 res += ctr st.sort() J = [] for s in st: J.extend([2] * (s - 1) + [3]) J.extend([2] * res) Ans[qu] = ans + sum(J[: min(len(J), K)]) print("\n".join(map(str, Ans)))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR STRING NUMBER NUMBER VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP LIST NUMBER BIN_OP VAR NUMBER LIST NUMBER EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL 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 ii in range(t): n, k = map(int, input().split()) s = input() a = [0] * n for i in range(n): if s[i] == "W": if i == 0: a[i] = 1 elif a[i - 1] != 0: a[i] = 2 else: a[i] = 1 stretch = [] extras = [] count = 0 index = None for i in range(n): if a[i] == 0: if index == None: index = i count += 1 else: if index is not None: if index == 0: extras.append((count, index)) else: stretch.append((count, index)) count = 0 index = None if a[-1] == 0: extras.append((count, index)) stretch.sort() extras.sort() cnt = 0 ind = 0 while cnt < k and ind < len(stretch): inti = stretch[ind][1] while cnt < k and inti < n: if a[inti] == 0: a[inti] = 1 inti += 1 cnt += 1 else: break ind += 1 ind = 0 while cnt < k and ind < len(extras): inti = extras[ind][1] if inti != 0: while cnt < k and inti < n: if a[inti] == 0: a[inti] = 1 inti += 1 cnt += 1 else: break else: while inti < n and a[inti] == 0: inti += 1 if inti == n: inti = n - 1 if a[inti] != 0: inti -= 1 while cnt < k and inti > -1: if a[inti] == 0: a[inti] = 1 inti -= 1 cnt += 1 else: break ind += 1 for i in range(n): if a[i] != 0: if i == 0: pass elif a[i - 1] != 0: a[i] = 2 print(sum(a))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR NONE ASSIGN VAR VAR VAR NUMBER IF VAR NONE IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NONE IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR 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.
a = int(input()) for k in range(a): b, n = [int(k) for k in input().split()] r = input() len_r = len(r) p_W = [] for x in range(len_r): if r[x] == "W": p_W.append(x) len_p_W = len(p_W) l_d = [] for y in range(len(p_W) - 1): d = p_W[y + 1] - p_W[y] l_d.append(d) len_l_d = len(l_d) if n == 0: if p_W == []: ans = 0 else: ans = 1 for x in l_d: if x > 1: ans += 1 else: ans += 2 elif p_W == [] or l_d.count(1) == len_l_d or sum(l_d) - len_l_d <= n: ans = min(n, 1) + 2 * min(len_p_W + n - 1, len_r - 1) else: ans = 1 for x in l_d: if x > 1: ans += 1 else: ans += 2 l_d = list(filter(lambda a: a > 1, l_d)) l_d.sort() for x in l_d: if x - 1 <= n: ans += 2 * (x - 1) + 1 n -= x - 1 else: ans += 2 * n break print(ans)
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 ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR LIST FUNC_CALL VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FOR VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP 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 _ in range(t): n, k = map(int, input().split()) s = list(input()) w = 0 for x in s: if x == "W": w += 1 if k >= n - w: print(1 + (n - 1) * 2) else: a = -1 b = -3 for i in range(n): if s[i] == "W": a = i break for i in range(n): if s[n - i - 1] == "W": b = n - i - 1 break prev = False holes = {} h = 0 for i in range(a, b + 1): if s[i] == "W": if h in holes: holes[h].append(i - 1) elif h > 0: holes[h] = [i - 1] h = 0 if not prev: prev = True else: h += 1 if prev: prev = False nums = {} sorted_keys_holes = sorted(holes.keys()) for h in sorted_keys_holes: if h > k: break else: x = min(k // h, len(holes[h])) nums[h] = [] for r in range(x): nums[h].append((holes[h][r] - h, holes[h][r])) k -= x * h for h in nums.keys(): for r in nums[h]: i, j = r for l in range(i, j + 1): s[l] = "W" c = 0 prev = False for i in range(a, b + 1): if s[i] == "W": if prev: c += 2 else: c += 1 prev = True else: prev = False if c > 0: print(c + 2 * k) elif k > 0: print(1 + 2 * (k - 1)) else: print(0)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR LIST BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FOR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER BIN_OP NUMBER BIN_OP VAR 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 t in range(int(input())): n, k = map(int, input().split()) a = input() b = [0] * n s = 0 c = 0 w = a.count("W") if k >= n - w: print(2 * n - 1) elif w == 0: print(max(0, 2 * k - 1)) else: f = a.find("W") for i in range(f, n): if a[i] == "W": s += 1 if i > 0 and a[i - 1] == "W": s += 1 b[c] += 1 c = 0 else: c += 1 for j in range(1, n): if k >= j * b[j]: s += b[j] * (2 * j + 1) k -= j * b[j] else: m = k // j r = k % j s += m * (2 * j + 1) + 2 * r k = 0 break s += 2 * k print(s)
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 BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER 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.
def solve(): x, y = map(int, input().split()) s = list(input()) v = [] ind = -1 ct = 0 st = 0 for n in range(len(s)): if s[n] == "L": if st: if ind == -1: ind = n ct += 1 else: if ct: v.append([ct, ind]) ct = 0 ind = -1 st = 1 v.sort(key=lambda x: x[0]) for n in v: for k in range(n[1], n[1] + n[0]): if y: s[k] = "W" y -= 1 id = 0 for n in range(x): if s[n] == "L": id += 1 if s[n] == "W": break id -= 1 while id >= 0 and y: s[id] = "W" id -= 1 y -= 1 id = x - 1 while s[id] == "L" and id > 0: id -= 1 id += 1 while id < x and y: s[id] = "W" id += 1 y -= 1 res = 0 st = 0 for n in s: if n == "W": if st == 0: res += 1 st = 1 else: res += 2 else: st = 0 print(res) for _ in range(int(input())): solve()
FUNC_DEF 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 FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER WHILE VAR NUMBER VAR ASSIGN VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR VAR ASSIGN VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER 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.
TC = int(input()) for tc in range(TC): N, K = map(int, input().split()) S = list(input()) w = 0 l = [] rw = 0 rl = 0 start = 0 for s in S: if s == "W": if rl > 0: if w > 0: l.append(rl) else: start = rl rl = 0 if rw > 0: rw += 2 else: rw = 1 else: if rw > 0: w += rw rw = 0 rl += 1 if rl > 0: start += rl if rw > 0: w += rw l.sort() for lc in l: if K >= lc: w += 2 * lc + 1 K -= lc else: w += 2 * K K = 0 break if K > start: if w > 0: w += start * 2 else: w = start * 2 - 1 elif w > 0: w += K * 2 elif K > 0: w = K * 2 - 1 print(w)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR IF VAR NUMBER VAR VAR 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 VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP 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.
T = int(input()) for case in range(T): m, n = [int(x) for x in input().split()] s = input() lsw = sorted([x for x in s.split("L") if len(x)]) points = 0 for e in lsw: points += len(e) * 2 - 1 ll = [x for x in s.split("W") if len(x)] ends = 0 if s[0] == "L": ends += len(ll.pop(0)) if len(ll) and s[-1] == "L": ends += len(ll.pop()) ls = sorted(ll) c = 0 full = True for e in ls: if n >= len(e): n -= len(e) c += len(e) * 2 + 1 else: c += n * 2 n = 0 full = False if n != 0: c += min(n, ends) * 2 if "W" not in s: c -= 1 print(c + points)
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 ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER STRING VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER STRING VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF STRING VAR VAR NUMBER 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.
t = int(input()) for _ in range(t): n, k = map(int, input().split()) s = list(input()) ind = [] ans = 0 for i in range(n): if s[i] == "W": ind.append(i) if i - 1 >= 0 and s[i - 1] == "W": ans += 1 ans += 1 if k == 0: print(ans) elif len(ind) == 0: print(min(k, n) * 2 - 1) else: gap = [] for i in range(len(ind) - 1): gap.append(ind[i + 1] - ind[i] - 1) gap.sort() k = min(k, n - len(ind)) ans += 2 * k for i in gap: if i > 0: if k >= i: ans += 1 k -= i else: break print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR FOR VAR VAR IF VAR NUMBER IF VAR VAR VAR NUMBER 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().rstrip() for _ in range(int(input())): n, k = [int(i) for i in input().split()] s = [i for i in input()] if s.count("W") == 0: print(max(2 * k - 1, 0)) continue if s[0] == "W": ans = 1 else: ans = 0 for i in range(1, n): if s[i - 1] == "W" and s[i] == "W": ans += 2 elif s[i - 1] == "L" and s[i] == "W": ans += 1 beg = 0 end = 0 while s and s[-1] == "L": end += 1 s.pop() s.reverse() while s and s[-1] == "L": beg += 1 s.pop() s.reverse() cnt = [] for i in range(1, len(s)): if s[i] == "L" and s[i - 1] == "W": cnt.append(1) elif s[i] == "L" and s[i - 1] == "L": cnt[-1] += 1 cnt.sort() cnt.reverse() while k and cnt: if k >= cnt[-1]: k -= cnt[-1] ans += 2 * cnt[-1] + 1 cnt.pop() else: ans += 2 * k k = 0 cnt.pop() ans += 2 * min(k, end) k -= min(k, end) ans += 2 * min(k, beg) k -= min(k, beg) print(ans)
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR IF FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER STRING VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR WHILE VAR VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR WHILE VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR FUNC_CALL 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.
def find_consecutive_lose_rounds(match_log): lose_sections_length = [] start_winning = False length = 0 original_point = 0 for i in range(len(match_log)): char = match_log[i] if char == "L": length += 1 start_winning = False else: if start_winning: original_point += 2 else: original_point += 1 if length > 0: lose_sections_length.append(length) start_winning = True length = 0 return lose_sections_length, original_point def remove_head_tail_losing_streak(match_log): head_length = 0 tail_length = 0 head_tail_length = 0 is_full_loses = False for i in range(len(match_log)): if match_log[i] == "W": match_log = match_log[i:] head_length = i break if i == len(match_log) - 1: is_full_loses = True return match_log, head_tail_length, is_full_loses for j in range(len(match_log) - 1, -1, -1): if match_log[j] == "W": tail_length = len(match_log) - j - 1 match_log = match_log[: j + 1] break head_tail_length = head_length + tail_length return match_log, head_tail_length, is_full_loses def find_optimal_cheat_points(head_tail_length, lose_sections_length, cheat_times): cheat_points = 0 lose_sections_length = sorted(lose_sections_length) for lose_section_length in lose_sections_length: if cheat_times < lose_section_length: cheat_points += cheat_times * 2 cheat_times = 0 return cheat_points cheat_points += (lose_section_length - 1) * 2 + 3 cheat_times -= lose_section_length if cheat_times > 0: cheat_points += min(cheat_times, head_tail_length) * 2 return cheat_points def catch_all_lose_exception(match_log, cheat_times): if cheat_times == 0: return 0 return (min(len(match_log), cheat_times) - 1) * 2 + 1 test_amount = int(input()) for i in range(test_amount): rounds, cheat_times = tuple(map(int, input().split())) match_log = input() head_tail_length = 0 match_log, head_tail_length, is_full_loses = remove_head_tail_losing_streak( match_log ) if is_full_loses: print(catch_all_lose_exception(match_log, cheat_times)) else: lose_sections_length, original_points = find_consecutive_lose_rounds(match_log) print( find_optimal_cheat_points( head_tail_length, lose_sections_length, cheat_times ) + original_points )
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR STRING VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER RETURN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER RETURN VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER RETURN BIN_OP BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER 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 NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR 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 case in range(t): n, k = map(int, input().split()) s = input().split()[0] prev = "L" score = 0 curr = 0 l = [] foundW = False for i in range(n): if s[i] == "W": foundW = True if curr != 0: l.append(curr) curr = 0 if prev == "L": score += 1 else: score += 2 else: curr += 1 prev = s[i] if curr != 0: l.append(curr) if len(l) == 0: print(score) continue beg, end = False, False if s[0] == "L": beg = True if s[n - 1] == "L": end = True if foundW == False: score = max(2 * k - 1, 0) print(score) continue bonus = sum(l) bonusPts = 2 * sum(l) + len(l) reg = 0 regPts = 0 if beg: bonus -= l[0] reg += l[0] bonusPts -= 2 * l[0] + 1 regPts += 2 * l[0] if end: bonus -= l[len(l) - 1] reg += l[len(l) - 1] bonusPts -= 2 * l[len(l) - 1] + 1 regPts += 2 * l[len(l) - 1] if k > bonus: score += bonusPts k -= bonus score += 2 * min(reg, k) else: if beg: del l[0] if end: del l[-1] l.sort() for i in range(len(l)): if k >= l[i]: score += 2 * l[i] + 1 k -= l[i] else: score += 2 * k break print(score)
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 NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER VAR BIN_OP NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER IF 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 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 reverse(li, i, k): i -= 1 t = 0 while k > 0 and i >= 0: if li[i] == "L": li[i] = "W" t += 1 else: return t i -= 1 k -= 1 return t def process(): n, k = list(map(int, input().split())) li = list(input()) if n == 1 and k == 1: print(1) return if n == 1 and k == 0: if li[0] == "W": print(1) else: print(0) return last_win = 0 if li[0] == "W" else -1 flag = 1 if li[0] == "W" else 0 between_wins = [] for i in range(1, n): if flag == 1 and li[i] == "W": last_win += 1 elif li[i] == "L": flag = 0 elif li[i] == "W": flag = 1 if last_win != -1: between_wins.append((last_win + 1, i - 1)) last_win = i between_wins.sort(key=lambda x: x[1] - x[0] + 1) temp = [] if li[0] == "L": ans = 0 flag = 0 for i in range(n): if li[i] == "W": ans = i flag = 1 break if flag: temp.append((ans - 1, 0)) if li[-1] == "L": ans = n - 1 flag = 0 for i in reversed(range(n)): if li[i] == "W": flag = 1 ans = i + 1 break if flag: temp.append((ans, n - 1)) temp.sort(key=lambda x: abs(x[1] - x[0]) + 1) between_wins.extend(temp) for i in between_wins: if i[1] >= i[0]: for j in range(i[0], i[1] + 1): if k > 0: k -= 1 li[j] = "W" else: break else: for j in reversed(range(i[1], i[0] + 1)): if k > 0: k -= 1 li[j] = "W" else: break if between_wins == []: for i in range(min(n, k)): li[i] = "W" points = 1 if li[0] == "W" else 0 for i in range(1, n): if li[i] == "W" and li[i - 1] == "W": points += 2 elif li[i] == "W": points += 1 print(points) tests = int(input()) for i in range(tests): process()
FUNC_DEF VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER RETURN VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN IF VAR NUMBER VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR VAR NUMBER STRING NUMBER NUMBER ASSIGN VAR VAR NUMBER STRING NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER VAR VAR STRING VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR LIST IF VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR STRING IF VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR STRING ASSIGN VAR VAR NUMBER STRING NUMBER 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 EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR 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.
for _ in range(int(input())): n, k = list(map(int, input().split())) arr = list(input()) last = False tot = 0 for i in range(n): if arr[i] == "W": if last: tot += 2 else: tot += 1 last = True else: last = False globalFlag = tot > 0 dists = [] last = -1 flag = False prefix = 0 sufix = 0 for i in range(n): if not flag: if arr[i] == "W": prefix = i last = i flag = True elif arr[i] == "W": if i - last > 1: dists.append(i - last - 1) last = i sufix = n - last - 1 if not flag: sufix = n dists.sort() for i in range(len(dists)): canAtThis = min(k, dists[i]) if k == 0: break if k >= dists[i]: tot += 2 * canAtThis + 1 k -= canAtThis else: tot += 2 * canAtThis k -= canAtThis if k == 0: break if k != 0 and sufix != 0: sufixGive = min(k, sufix) tot += 1 tot += sufixGive * 2 - 1 k -= sufixGive if not globalFlag: tot -= 1 if globalFlag: if k != 0 and prefix != 0: prefixGive = min(k, prefix) tot += prefixGive * 2 print(tot)
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 ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR STRING IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER IF VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR BIN_OP NUMBER VAR VAR VAR IF VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR IF VAR VAR NUMBER IF VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR 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 = map(int, input().split()) s = input() a = [] prev = -1 for i in range(n): if s[i] == "W": if prev != -1 and i != prev + 1: a.append((prev, i - 1)) prev = i a.sort(key=lambda x: x[1] - x[0]) ans = 0 if s[0] == "L" else 1 for i in range(1, n): if s[i] == "W": if s[i - 1] == "W": ans += 2 else: ans += 1 for i in range(len(a)): ln = a[i][1] - a[i][0] if k >= ln: ans += ln * 2 + 1 k -= ln else: ans += k * 2 k = 0 lf = s.find("W") if lf == -1: ans = k * 2 - 1 else: ans += min(k, lf) * 2 k -= min(k, lf) rf = s.rfind("W") ans += min(k, n - rf - 1) * 2 print(max(0, ans))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER STRING NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER 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 compress(string): string = string + "#" n = len(string) begin, end, cnt = 0, 1, 1 ans = [] while end < n: if string[begin] == string[end]: end, cnt = end + 1, cnt + 1 else: ans.append((string[begin], cnt)) begin, end, cnt = end, end + 1, 1 return ans t = int(input()) for _ in range(t): n, k = map(int, input().split()) s = input()[:-1] s = compress(s) w_groups = 0 w_cnt = 0 l_cnt = 0 li = [] for i, (char, cnt) in enumerate(s): if char == "W": w_groups += 1 w_cnt += cnt if char == "L": l_cnt += cnt if 1 <= i < len(s) - 1: li.append(cnt) if w_cnt == 0: print(max(min(k, l_cnt) * 2 - 1, 0)) continue ans = w_cnt * 2 - w_groups ans += min(k, l_cnt) * 2 li.sort() for val in li: if k >= val: ans += 1 k -= val print(ans)
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER 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 NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR FUNC_CALL VAR VAR IF VAR STRING VAR NUMBER VAR VAR IF VAR STRING VAR VAR IF NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR NUMBER 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 itertools def remove_beginning(lst, bad): idx = 0 while lst[idx] == bad: idx += 1 if len(lst) == idx: break return idx def solve(): n, k = [int(x) for x in input().strip().split()] game_scores = list(input().strip()) win, lose = "W", "L" first_good_pos = remove_beginning(game_scores, lose) first_range = first_good_pos game_scores = game_scores[first_good_pos:] game_scores.reverse() if not game_scores: return max(2 * min(k, first_range) - 1, 0) last_good_pos = remove_beginning(game_scores, lose) last_range = last_good_pos game_scores = game_scores[last_good_pos:] game_scores.reverse() assignable_points_from_list_ends = first_range + last_range lose_lengths = [] for key, group in itertools.groupby(game_scores): lose_lengths.append([key, len(list(group))]) initial_points = sum( map(lambda t: 0 if t[0] == lose else 2 * t[1] - 1, lose_lengths) ) lose_lengths = list( filter(lambda t: t > 0, map(lambda t: 0 if t[0] == win else t[1], lose_lengths)) ) if n == 1: if game_scores[0] == win: return 1 return 1 if k == 1 else 0 lose_lengths.sort() for l in lose_lengths: if k >= l: k, initial_points = k - l, initial_points + 2 * (l - 1) + 3 else: k, initial_points = 0, initial_points + 2 * k initial_points += 2 * min(k, assignable_points_from_list_ends) return initial_points for _ in range(int(input().strip())): print(solve())
IMPORT FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR STRING STRING ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR IF VAR RETURN FUNC_CALL VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR IF VAR NUMBER IF VAR NUMBER VAR RETURN NUMBER RETURN VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR 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.
class Streak: def __init__(self, start_ind, end_ind): self.start_ind = start_ind self.end_ind = end_ind class StreakNbors: def __init__(self, streak_a, streak_b): self.streak_a = streak_a self.streak_b = streak_b def get_gap(self): return self.streak_b.start_ind - self.streak_a.end_ind - 1 def close_gap(self): self.streak_a.end_ind += 1 t = int(input()) for it in range(0, t): n, k = tuple(list(map(int, input().split(" ")))) results = [char for char in input()] if n == 1: if k >= 1: results[0] = "W" else: streak_list = [] current_streak = None for i in range(0, len(results)): if results[i] == "W": if i == 0 or results[i - 1] == "L": current_streak = Streak(i, i) streak_list.append(current_streak) else: current_streak.end_ind = i if streak_list: streak_nbors_list = [] for i in range(0, len(streak_list) - 1): streak_nbors_list.append( StreakNbors(streak_list[i], streak_list[i + 1]) ) streak_nbors_list = sorted( streak_nbors_list, key=lambda streak_nbors: streak_nbors.get_gap() ) streak_nbors_ind = 0 changes = 0 while changes < k and streak_nbors_ind < len(streak_nbors_list): current_streak_nbors = streak_nbors_list[streak_nbors_ind] if current_streak_nbors.get_gap() > 0: current_streak_nbors.close_gap() changes += 1 else: streak_nbors_ind += 1 for i in range(0, len(streak_nbors_list)): current_streak_nbors = streak_nbors_list[i] streak_a_start_ind = current_streak_nbors.streak_a.start_ind streak_a_end_ind = current_streak_nbors.streak_a.end_ind for j in range(streak_a_start_ind, streak_a_end_ind + 1): results[j] = "W" streak_b_start_ind = current_streak_nbors.streak_b.start_ind streak_b_end_ind = current_streak_nbors.streak_b.end_ind for j in range(streak_b_start_ind, streak_b_end_ind + 1): results[j] = "W" min_streak_ind = 200001 max_streak_ind = -1 for i in range(0, len(results)): if results[i] == "W": min_streak_ind = min(i, min_streak_ind) max_streak_ind = max(i, min_streak_ind) while changes < k: if min_streak_ind > 0: min_streak_ind -= 1 results[min_streak_ind] = "W" changes += 1 elif max_streak_ind < len(results) - 1: max_streak_ind += 1 results[max_streak_ind] = "W" changes += 1 else: break else: for i in range(0, k): results[i] = "W" score = 0 for i in range(0, n): if results[i] == "W": score += 1 if i > 0 and results[i - 1] == "W": score += 1 print(score)
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR NUMBER FUNC_DEF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER STRING ASSIGN VAR LIST ASSIGN VAR NONE FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NUMBER VAR BIN_OP VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING VAR NUMBER IF VAR NUMBER 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.
t = int(input()) def answer(n, k, s): win = [] for i in range(n): if s[i] == "W": win.append(i) l = len(win) if l <= 1: print(min(max(2 * (k + l) - 1, 0), 2 * n - 1)) elif win[-1] - l + 1 <= k: ans = min(2 * (k + l) - 1, 2 * n - 1) print(ans) else: ans = 1 for i in range(l - 1): if win[i + 1] - win[i] == 1: ans += 2 else: ans += 1 delta = [(win[i + 1] - win[i] - 1) for i in range(l - 1)] delta.sort() flag = k j = 0 while flag > 0 and j < l - 1: d = delta[j] if d != 0: if flag >= d: flag -= d ans += 2 * d + 1 else: ans += 2 * flag flag = -1 j += 1 if flag > 0: ans += 2 * flag print(ans) for i in range(t): n, k = map(int, input().split()) s = list(str(input())) answer(n, k, s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR 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.
import sys t = int(sys.stdin.readline().strip()) for _ in range(t): arr = [] n, k = map(int, sys.stdin.readline().strip().split()) s = input() ind = 0 while ind != n and s[ind] == "L": ind += 1 if ind == n: if k >= n: print(2 * n - 1) else: print(max(2 * k - 1, 0)) continue count = -1 current = 0 for i in range(ind, n): if s[i] == "W": if count > 0: current += 1 arr += [count] elif count < 0: current += 1 else: current += 2 count = 0 else: count += 1 change = 0 arr.sort() delta = 0 for i in range(len(arr)): if change + arr[i] <= k: change += arr[i] delta += arr[i] * 2 + 1 print(min(current + delta + 2 * (k - change), 2 * n - 1))
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING IF VAR NUMBER VAR NUMBER VAR LIST VAR IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER BIN_OP VAR VAR 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.
import sys t = int(input()) for _ in range(t): [n, k] = list(map(int, input().split())) temp = 0 for line in sys.stdin: s = line break prev = "L" current = "L" lsum = 0 tempsum = 0 losses = [] startind = 0 endind = n - 1 while startind != n and s[startind] == "L": startind += 1 while endind != -1 and s[endind] == "L": endind -= 1 for i in range(startind, endind + 1): current = s[i] if current == "L" and prev == "L": lsum += 1 tempsum += 1 elif current == "L" and prev == "W": tempsum = 1 lsum += 1 elif current == "W" and prev == "L": if i != startind: losses.append(tempsum) prev = current losses.sort() startloss = startind endloss = n - 1 - endind lsum += startloss + endloss starts = len(losses) + 1 losses.append(startloss) losses.append(endloss) if startind == n: print(max(2 * k - 1, k)) else: ind = 0 remk = k while ind < len(losses) and remk > 0: if remk >= losses[ind]: remk -= losses[ind] lsum -= losses[ind] if ind < len(losses) - 2: starts -= 1 else: lsum -= remk break ind += 1 print((n - lsum) * 2 - starts)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN LIST VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER WHILE VAR NUMBER VAR VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR STRING VAR STRING VAR NUMBER VAR NUMBER IF VAR STRING VAR STRING ASSIGN VAR NUMBER VAR NUMBER IF VAR STRING VAR STRING IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER 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().replace("\n", "")) for i in range(t): line1 = list(map(int, input().replace("\n", "").split(" "))) n = line1[0] k = line1[1] s = list(input().replace("\n", "")) wins, losses, gap_wins = 0, 0, 0 losses_gaps_len = [] for i in range(n): if s[i] == "W": if i == 0 or s[i - 1] == "L": gap_wins += 1 wins += 1 else: losses += 1 if i == 0 or s[i - 1] == "W": losses_gaps_len.append(0) losses_gaps_len[-1] += 1 if k > losses: print(2 * n - 1) continue if wins == 0: if k == 0: print(0) else: print(2 * k - 1) continue inf = float("inf") if s[0] == "L": losses_gaps_len[0] = inf if s[n - 1] == "L": losses_gaps_len[-1] = inf sorted_losses_gaps_len = sorted(losses_gaps_len) wins += k for i in range(len(sorted_losses_gaps_len)): if sorted_losses_gaps_len[i] > k: break else: gap_wins -= 1 k -= sorted_losses_gaps_len[i] print(2 * wins - gap_wins)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING STRING STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING STRING ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NUMBER VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER STRING ASSIGN VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER 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 = input() L = s.count("L") W = s.count("W") if L <= k: print(2 * (n - 1) + 1) continue if L == n and k != 0: print((k - 1) * 2 + 1) continue count = 0 p = 0 if s[0] == "W": count += 1 p = 1 for i in range(1, n): if s[i] == "W" and p: count += 2 elif s[i] == "W": count += 1 p = 1 else: p = 0 continue if k == 0: print(count) continue temp = s[::-1] t = s for i in range(n): if temp[i] == "L": continue else: break temp = temp[i:] temp = temp[::-1] s = temp m = i l = [] p = 0 c = 0 for i in range(1, len(s)): if s[i - 1] == "W" and s[i] == "L": c += 1 p = 1 elif s[i - 1] == "L" and s[i] == "L" and p: c += 1 elif s[i - 1] == "L" and s[i] == "W": p = 0 if c != 0: l.append(c) c = 0 else: continue if c != 0: l.append(c) l.sort() for i in l: if k <= 0: break if k >= i: k -= i count += 2 * i + 1 else: break if len(l) and k <= max(l): count += k * 2 print(count) elif m > k: count = count + k * 2 k = 0 print(count) else: count += m * 2 k -= m for i in range(n): if t[i] == "W": break if i > k: print(count + k * 2) else: print(count + (k - i) * 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 ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER STRING VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING VAR VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER STRING VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR VAR STRING VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR IF VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP 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.
for _ in range(int(input())): n, k = map(int, input().split(" ")) s = input() if s[0] == "W": score = 1 else: score = 0 for i in range(1, n): if s[i] == "W": if s[i - 1] == "W": score = score + 2 else: score = score + 1 start = [] i = 0 while i < n: if s[i] == "L": j = i + 1 a = [i] while j < n and s[j] == "L": j = j + 1 i = j a.append(i) start.append(a) else: i = i + 1 add_score = 0 f = 0 p = 0 if len(start) > 0: if start[0][0] == 0: p = start[0][1] - start[0][0] s1 = start[1 : len(start)] f = 1 else: s1 = start g = 0 p1 = 0 if len(s1) != 0: if s1[-1][1] == n: p1 = s1[-1][1] - s1[-1][0] s1 = s1[0 : len(s1) - 1] g = 1 for item in s1: item.append(item[1] - item[0]) s1.sort(key=lambda x: x[2]) t = len(s1) for item in s1[0:t]: if k >= item[2]: k = k - item[2] add_score = add_score + 2 * item[2] + 1 else: add_score = add_score + 2 * k k = 0 if k == 0: break if f == 1 or g == 1 and k != 0: add_score = add_score + 2 * min(p + p1, k) if n == 1: if k >= n: print(1) elif s == "W": print(1) else: print(0) elif len(set(s)) == 1 and s[0] == "L": print(max(0, score + add_score - 1)) else: print(score + add_score) else: print(score)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR WHILE VAR VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP 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 _ in range(t): n, k = map(int, input().split()) s = input() ans = 0 prev = "L" for i in s: if i == "W" and prev == "W": ans += 2 elif i == "W": ans += 1 prev = i temp = [] a = 0 b = 0 x = 0 y = 0 for i in s: if i == "W": break else: x += 1 else: if k >= n: ans = 1 + 2 * (n - 1) else: ans = 1 + 2 * (k - 1) print(max(ans, 0)) continue for i in s: if i == "W": b = 1 if a != 0: temp.append(a) a = 0 elif b == 0: continue else: a += 1 if a != 0: y = a temp = sorted(temp) for i in temp: if i <= k: ans += 2 * i + 1 k -= i else: ans += 2 * k k = 0 if k > 0: if x + y >= k: ans += 2 * k else: ans += 2 * (x + y) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR VAR IF VAR STRING VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR 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 IF BIN_OP VAR VAR VAR VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER BIN_OP 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 x in range(t): n, k = input().split(" ") n = int(n) k = int(k) s = input() count_ = s.count("W") calculate = 0 prev = 0 li = [] pre = -1 for x in range(n): if s[x] == "W": if pre == -1: pre = x calculate = 1 else: li.append(x - pre - 1) if x - pre == 1: calculate += 2 else: calculate += 1 pre = x li.sort() for x in li: if x != 0: if k >= x: calculate += 2 * x + 1 k = k - x count_ += x elif k < x: calculate += 2 * k count_ += k k = 0 break if calculate == 0 and k > 0: calculate = -1 if k != 0: if n - count_ >= k: calculate += 2 * k count_ += k elif n - count_ < k: calculate += 2 * (n - count_) count_ += n - count_ print(calculate)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR IF VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP NUMBER VAR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP NUMBER BIN_OP VAR VAR VAR BIN_OP 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.
for _ in range(int(input())): n, k = map(int, input().split()) s = input() if s.count("W") == n: print(2 * n - 1) elif s.count("L") == n: print(max(2 * k - 1, 0)) else: score = 0 if s[0] == "W": score += 1 for i in range(1, n): if s[i] == s[i - 1] == "W": score += 2 elif s[i] == "W": score += 1 left = 0 right = n - 1 while left < n and s[left] == "L": left += 1 while right >= 0 and s[right] == "L": right -= 1 left_dist = left right_dist = n - right - 1 s = s[left + 1 : right] s = s.split("W") s = [len(line) for line in s if line] s.sort(reverse=True) while s and s[-1] <= k: score += s[-1] * 2 + 1 k -= s[-1] s.pop() score += k * 2 print(min(score, 2 * n - 1))
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 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 ASSIGN VAR NUMBER IF VAR NUMBER STRING VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER WHILE VAR NUMBER VAR VAR STRING VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER WHILE VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR 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 i in range(int(input())): n, k = map(int, input().split()) s = input() wins = s.count("W") + k if wins >= n: print(2 * n - 1) else: streaks = int(s[0] == "W") + s.count("LW") or int(wins > 0) gaps = s.strip("L").replace("W", " ").strip().split() for g in sorted(map(len, gaps)): if g > k: break k -= g streaks -= 1 print(wins * 2 - streaks)
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 BIN_OP FUNC_CALL VAR STRING VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER STRING FUNC_CALL VAR STRING FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING STRING STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER 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 t = int(input()) for i in range(t): n, k = map(int, input().split()) s = sys.stdin.readline().strip() s = list(s) q = 0 p = [] for j in range(n): if s[j] == "W": if q > 0: p.append([q, j - 1]) q = 0 else: q = q + 1 if q > 0: if p == [] and s[0] != "W": p.append([q, n - 1]) else: p.append([q, n - q]) q = 0 if p: e1 = [] e2 = [] if s[0] == "L": e1.append(p[0]) del p[0] if p: if s[-1] == "L": e2.append(p[-1]) del p[-1] p.sort() p = p + e1 j = 0 l = len(p) while k > 0 and j < l: v = p[j][1] while v >= 0 and s[v] == "L" and k > 0: s[v] = "W" v = v - 1 k = k - 1 j = j + 1 if k > 0 and e2: j = e2[0][1] while k > 0 and j < n: s[j] = "W" k = k - 1 j = j + 1 w = 0 for j in range(1, n): if s[j] == s[j - 1] and s[j] == "W": w = w + 2 elif s[j] == "W": w = w + 1 if s[0] == "W": w = w + 1 print(w)
IMPORT 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 FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR LIST VAR NUMBER STRING EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR LIST ASSIGN VAR LIST IF VAR NUMBER STRING EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER WHILE VAR NUMBER VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR NUMBER VAR VAR ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR 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.
import sys input = sys.stdin.readline for _ in range(int(input())): n, k = map(int, input().split()) s = input() s = [s[i] for i in range(n)] base = s.count("W") if base == 0: if k: print(2 * k - 1) else: print(0) elif base + k >= n: print(2 * n - 1) else: interval = [] while s and s[-1] == "L": s.pop() s = s[::-1] while s and s[-1] == "L": s.pop() while s: if s[-1] == "W": while s and s[-1] == "W": s.pop() else: tmp = 0 while s and s[-1] == "L": s.pop() tmp += 1 interval.append(tmp) interval.sort(reverse=True) K = k while interval and k: if k >= interval[-1]: k -= interval.pop() else: break print(2 * (base + K) - 1 - len(interval))
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER IF VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR NUMBER STRING EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER WHILE VAR VAR NUMBER STRING EXPR FUNC_CALL VAR WHILE VAR IF VAR NUMBER STRING WHILE VAR VAR NUMBER STRING EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER STRING EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR NUMBER VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR VAR NUMBER 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()) def cheatScore(s, n, k): lose = 0 score = 0 start = 0 tmp = [] flag = 0 Alllose = 1 for c in s: if c != "W": continue else: Alllose = 0 break if Alllose: return max(0, 2 * min(n, k) - 1) for i in range(len(s)): if i == 0: if s[i] == "L": count = 1 flag = 1 else: continue if s[i] == "L": if s[i - 1] == "W": count = 1 elif s[i - 1] == "L": count += 1 elif s[i - 1] == "L": tmp.append(count) else: continue stEnd = 0 if flag: stEnd += tmp.pop(0) if s[-1] == "L": stEnd += count tmp = sorted(tmp) for i in range(len(s)): if i == 0: if s[i] == "W": score += 1 else: lose += 1 continue if s[i] == "W": if s[i - 1] == "W": score += 2 else: score += 1 else: lose += 1 score += 2 * min(lose, k) bridge = 0 leave = 0 for gap in tmp: if k >= gap: bridge += 1 k -= gap else: leave += gap score += bridge return score while t: t -= 1 n, k = list(map(int, input().strip().split())) s = input() maxScore = cheatScore(s, n, k) print(maxScore)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING ASSIGN VAR NUMBER IF VAR RETURN FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER STRING VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR RETURN VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR 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.
for _ in range(int(input())): n, k = map(int, input().split()) s = input() ans, t, b, f = 0, 0, 0, 0 l = [] for i in range(n): if s[i] == "W": ans += f + 1 f = 1 if b: l.append(b) b = 0 else: f = 0 b += 1 if b: l.append(b) m, v = 0, 0 if s[0] == "L": m = l.pop(0) if s[-1] == "L": if ans: v = l.pop() l.sort() while k and l: t = l.pop(0) if k >= t: ans += t * 2 + 1 k -= t else: ans += 2 * k k = 0 if k: ans = ans + 2 * min(k, m + v) if ans else ans + 2 * min(k, m) - 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER IF VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR NUMBER STRING IF VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ traps numbered from $1$ to $n$. You will go through them one by one in order. The $i$-th trap deals $a_i$ base damage to you. Instead of going through a trap, you can jump it over. You can jump over no more than $k$ traps. If you jump over a trap, it does not deal any damage to you. But there is an additional rule: if you jump over a trap, all next traps damages increase by $1$ (this is a bonus damage). Note that if you jump over a trap, you don't get any damage (neither base damage nor bonus damage). Also, the bonus damage stacks so, for example, if you go through a trap $i$ with base damage $a_i$, and you have already jumped over $3$ traps, you get $(a_i + 3)$ damage. You have to find the minimal damage that it is possible to get if you are allowed to jump over no more than $k$ traps. -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) β€” the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $1 \le k \le n$) β€” the number of traps and the number of jump overs that you are allowed to make. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” base damage values of all traps. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case output a single integer β€” the minimal total damage that it is possible to get if you are allowed to jump over no more than $k$ traps. -----Examples----- Input 5 4 4 8 7 1 4 4 1 5 10 11 5 7 5 8 2 5 15 11 2 8 6 3 1 2 3 4 5 6 1 1 7 Output 0 21 9 6 0 -----Note----- In the first test case it is allowed to jump over all traps and take $0$ damage. In the second test case there are $5$ ways to jump over some traps: Do not jump over any trap. Total damage: $5 + 10 + 11 + 5 = 31$. Jump over the $1$-st trap. Total damage: $\underline{0} + (10 + 1) + (11 + 1) + (5 + 1) = 29$. Jump over the $2$-nd trap. Total damage: $5 + \underline{0} + (11 + 1) + (5 + 1) = 23$. Jump over the $3$-rd trap. Total damage: $5 + 10 + \underline{0} + (5 + 1) = 21$. Jump over the $4$-th trap. Total damage: $5 + 10 + 11 + \underline{0} = 26$. To get minimal damage it is needed to jump over the $3$-rd trap, so the answer is $21$. In the third test case it is optimal to jump over the traps $1$, $3$, $4$, $5$, $7$: Total damage: $0 + (2 + 1) + 0 + 0 + 0 + (2 + 4) + 0 = 9$.
def resi(): n, k = map(int, input().split()) a = list(map(int, input().split())) s = sum(a) for i in range(n): a[i] = a[i] - (n - (i + 1)) a.sort() print(s - sum(a[-k:]) - k * (k - 1) // 2) return for _ in range(int(input())): resi()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
There are $n$ traps numbered from $1$ to $n$. You will go through them one by one in order. The $i$-th trap deals $a_i$ base damage to you. Instead of going through a trap, you can jump it over. You can jump over no more than $k$ traps. If you jump over a trap, it does not deal any damage to you. But there is an additional rule: if you jump over a trap, all next traps damages increase by $1$ (this is a bonus damage). Note that if you jump over a trap, you don't get any damage (neither base damage nor bonus damage). Also, the bonus damage stacks so, for example, if you go through a trap $i$ with base damage $a_i$, and you have already jumped over $3$ traps, you get $(a_i + 3)$ damage. You have to find the minimal damage that it is possible to get if you are allowed to jump over no more than $k$ traps. -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) β€” the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $1 \le k \le n$) β€” the number of traps and the number of jump overs that you are allowed to make. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” base damage values of all traps. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case output a single integer β€” the minimal total damage that it is possible to get if you are allowed to jump over no more than $k$ traps. -----Examples----- Input 5 4 4 8 7 1 4 4 1 5 10 11 5 7 5 8 2 5 15 11 2 8 6 3 1 2 3 4 5 6 1 1 7 Output 0 21 9 6 0 -----Note----- In the first test case it is allowed to jump over all traps and take $0$ damage. In the second test case there are $5$ ways to jump over some traps: Do not jump over any trap. Total damage: $5 + 10 + 11 + 5 = 31$. Jump over the $1$-st trap. Total damage: $\underline{0} + (10 + 1) + (11 + 1) + (5 + 1) = 29$. Jump over the $2$-nd trap. Total damage: $5 + \underline{0} + (11 + 1) + (5 + 1) = 23$. Jump over the $3$-rd trap. Total damage: $5 + 10 + \underline{0} + (5 + 1) = 21$. Jump over the $4$-th trap. Total damage: $5 + 10 + 11 + \underline{0} = 26$. To get minimal damage it is needed to jump over the $3$-rd trap, so the answer is $21$. In the third test case it is optimal to jump over the traps $1$, $3$, $4$, $5$, $7$: Total damage: $0 + (2 + 1) + 0 + 0 + 0 + (2 + 4) + 0 = 9$.
for _ in range(int(input())): n, k = map(int, input().split()) l = [int(i) for i in input().split()] ll = sorted([(-l[i] + (n - 1 - i)) for i in range(n)]) base = sum(l) poss = [base] for i in range(k): base += ll[i] - i poss.append(base) print(min(poss))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
There are $n$ traps numbered from $1$ to $n$. You will go through them one by one in order. The $i$-th trap deals $a_i$ base damage to you. Instead of going through a trap, you can jump it over. You can jump over no more than $k$ traps. If you jump over a trap, it does not deal any damage to you. But there is an additional rule: if you jump over a trap, all next traps damages increase by $1$ (this is a bonus damage). Note that if you jump over a trap, you don't get any damage (neither base damage nor bonus damage). Also, the bonus damage stacks so, for example, if you go through a trap $i$ with base damage $a_i$, and you have already jumped over $3$ traps, you get $(a_i + 3)$ damage. You have to find the minimal damage that it is possible to get if you are allowed to jump over no more than $k$ traps. -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) β€” the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $1 \le k \le n$) β€” the number of traps and the number of jump overs that you are allowed to make. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” base damage values of all traps. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case output a single integer β€” the minimal total damage that it is possible to get if you are allowed to jump over no more than $k$ traps. -----Examples----- Input 5 4 4 8 7 1 4 4 1 5 10 11 5 7 5 8 2 5 15 11 2 8 6 3 1 2 3 4 5 6 1 1 7 Output 0 21 9 6 0 -----Note----- In the first test case it is allowed to jump over all traps and take $0$ damage. In the second test case there are $5$ ways to jump over some traps: Do not jump over any trap. Total damage: $5 + 10 + 11 + 5 = 31$. Jump over the $1$-st trap. Total damage: $\underline{0} + (10 + 1) + (11 + 1) + (5 + 1) = 29$. Jump over the $2$-nd trap. Total damage: $5 + \underline{0} + (11 + 1) + (5 + 1) = 23$. Jump over the $3$-rd trap. Total damage: $5 + 10 + \underline{0} + (5 + 1) = 21$. Jump over the $4$-th trap. Total damage: $5 + 10 + 11 + \underline{0} = 26$. To get minimal damage it is needed to jump over the $3$-rd trap, so the answer is $21$. In the third test case it is optimal to jump over the traps $1$, $3$, $4$, $5$, $7$: Total damage: $0 + (2 + 1) + 0 + 0 + 0 + (2 + 4) + 0 = 9$.
for _ in range(int(input())): n, k = map(int, input().split()) l = list(map(int, input().split())) t = [] for i in range(n): t.append([l[i] - (n - i - 1), i]) t = sorted(t)[::-1] x = [(0) for _ in range(n)] for i in range(k): x[t[i][1]] = 1 ans = 0 p = 0 for i in range(n): if x[i] == 1: p += 1 else: ans += l[i] + p print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ traps numbered from $1$ to $n$. You will go through them one by one in order. The $i$-th trap deals $a_i$ base damage to you. Instead of going through a trap, you can jump it over. You can jump over no more than $k$ traps. If you jump over a trap, it does not deal any damage to you. But there is an additional rule: if you jump over a trap, all next traps damages increase by $1$ (this is a bonus damage). Note that if you jump over a trap, you don't get any damage (neither base damage nor bonus damage). Also, the bonus damage stacks so, for example, if you go through a trap $i$ with base damage $a_i$, and you have already jumped over $3$ traps, you get $(a_i + 3)$ damage. You have to find the minimal damage that it is possible to get if you are allowed to jump over no more than $k$ traps. -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) β€” the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $1 \le k \le n$) β€” the number of traps and the number of jump overs that you are allowed to make. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” base damage values of all traps. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case output a single integer β€” the minimal total damage that it is possible to get if you are allowed to jump over no more than $k$ traps. -----Examples----- Input 5 4 4 8 7 1 4 4 1 5 10 11 5 7 5 8 2 5 15 11 2 8 6 3 1 2 3 4 5 6 1 1 7 Output 0 21 9 6 0 -----Note----- In the first test case it is allowed to jump over all traps and take $0$ damage. In the second test case there are $5$ ways to jump over some traps: Do not jump over any trap. Total damage: $5 + 10 + 11 + 5 = 31$. Jump over the $1$-st trap. Total damage: $\underline{0} + (10 + 1) + (11 + 1) + (5 + 1) = 29$. Jump over the $2$-nd trap. Total damage: $5 + \underline{0} + (11 + 1) + (5 + 1) = 23$. Jump over the $3$-rd trap. Total damage: $5 + 10 + \underline{0} + (5 + 1) = 21$. Jump over the $4$-th trap. Total damage: $5 + 10 + 11 + \underline{0} = 26$. To get minimal damage it is needed to jump over the $3$-rd trap, so the answer is $21$. In the third test case it is optimal to jump over the traps $1$, $3$, $4$, $5$, $7$: Total damage: $0 + (2 + 1) + 0 + 0 + 0 + (2 + 4) + 0 = 9$.
for _ in range(int(input())): n, k = map(int, input().split()) d = list(map(int, input().split())) sm = sum(d) for i in range(n): d[i] = -d[i] + (n - 1 - i) d.sort() for i in range(k): sm += d[i] - i print(sm)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ traps numbered from $1$ to $n$. You will go through them one by one in order. The $i$-th trap deals $a_i$ base damage to you. Instead of going through a trap, you can jump it over. You can jump over no more than $k$ traps. If you jump over a trap, it does not deal any damage to you. But there is an additional rule: if you jump over a trap, all next traps damages increase by $1$ (this is a bonus damage). Note that if you jump over a trap, you don't get any damage (neither base damage nor bonus damage). Also, the bonus damage stacks so, for example, if you go through a trap $i$ with base damage $a_i$, and you have already jumped over $3$ traps, you get $(a_i + 3)$ damage. You have to find the minimal damage that it is possible to get if you are allowed to jump over no more than $k$ traps. -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) β€” the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $1 \le k \le n$) β€” the number of traps and the number of jump overs that you are allowed to make. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” base damage values of all traps. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case output a single integer β€” the minimal total damage that it is possible to get if you are allowed to jump over no more than $k$ traps. -----Examples----- Input 5 4 4 8 7 1 4 4 1 5 10 11 5 7 5 8 2 5 15 11 2 8 6 3 1 2 3 4 5 6 1 1 7 Output 0 21 9 6 0 -----Note----- In the first test case it is allowed to jump over all traps and take $0$ damage. In the second test case there are $5$ ways to jump over some traps: Do not jump over any trap. Total damage: $5 + 10 + 11 + 5 = 31$. Jump over the $1$-st trap. Total damage: $\underline{0} + (10 + 1) + (11 + 1) + (5 + 1) = 29$. Jump over the $2$-nd trap. Total damage: $5 + \underline{0} + (11 + 1) + (5 + 1) = 23$. Jump over the $3$-rd trap. Total damage: $5 + 10 + \underline{0} + (5 + 1) = 21$. Jump over the $4$-th trap. Total damage: $5 + 10 + 11 + \underline{0} = 26$. To get minimal damage it is needed to jump over the $3$-rd trap, so the answer is $21$. In the third test case it is optimal to jump over the traps $1$, $3$, $4$, $5$, $7$: Total damage: $0 + (2 + 1) + 0 + 0 + 0 + (2 + 4) + 0 = 9$.
def find_minimal_damage_count(n, jumps, attacks): damageCount = 0 if n == jumps: return damageCount for i in range(n): damageCount += attacks[i] attacks[i] += i + 1 attacks.sort() attacks.reverse() for i in range(jumps): damageCount -= attacks[i] damageCount += n - i return damageCount t = int(input()) for i in range(t): lens = input().split(" ") n = int(lens[0]) jumps = int(lens[1]) attacks = list(map(int, input().split(" "))) print(str(find_minimal_damage_count(n, jumps, attacks)))
FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
There are $n$ traps numbered from $1$ to $n$. You will go through them one by one in order. The $i$-th trap deals $a_i$ base damage to you. Instead of going through a trap, you can jump it over. You can jump over no more than $k$ traps. If you jump over a trap, it does not deal any damage to you. But there is an additional rule: if you jump over a trap, all next traps damages increase by $1$ (this is a bonus damage). Note that if you jump over a trap, you don't get any damage (neither base damage nor bonus damage). Also, the bonus damage stacks so, for example, if you go through a trap $i$ with base damage $a_i$, and you have already jumped over $3$ traps, you get $(a_i + 3)$ damage. You have to find the minimal damage that it is possible to get if you are allowed to jump over no more than $k$ traps. -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) β€” the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $1 \le k \le n$) β€” the number of traps and the number of jump overs that you are allowed to make. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” base damage values of all traps. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case output a single integer β€” the minimal total damage that it is possible to get if you are allowed to jump over no more than $k$ traps. -----Examples----- Input 5 4 4 8 7 1 4 4 1 5 10 11 5 7 5 8 2 5 15 11 2 8 6 3 1 2 3 4 5 6 1 1 7 Output 0 21 9 6 0 -----Note----- In the first test case it is allowed to jump over all traps and take $0$ damage. In the second test case there are $5$ ways to jump over some traps: Do not jump over any trap. Total damage: $5 + 10 + 11 + 5 = 31$. Jump over the $1$-st trap. Total damage: $\underline{0} + (10 + 1) + (11 + 1) + (5 + 1) = 29$. Jump over the $2$-nd trap. Total damage: $5 + \underline{0} + (11 + 1) + (5 + 1) = 23$. Jump over the $3$-rd trap. Total damage: $5 + 10 + \underline{0} + (5 + 1) = 21$. Jump over the $4$-th trap. Total damage: $5 + 10 + 11 + \underline{0} = 26$. To get minimal damage it is needed to jump over the $3$-rd trap, so the answer is $21$. In the third test case it is optimal to jump over the traps $1$, $3$, $4$, $5$, $7$: Total damage: $0 + (2 + 1) + 0 + 0 + 0 + (2 + 4) + 0 = 9$.
t = int(input()) for _ in range(t): n, k = map(int, input().split()) traps = list(map(int, input().split())) if k >= n: print(0) continue sortedred = [] for i in range(n): sortedred.append((i, traps[i] - (n - i - 1))) sortedred.sort(key=lambda x: x[1], reverse=True) totalred = 0 j = 0 for i in range(k): totalred += sortedred[i][1] j += 1 print(sum(traps) - totalred - j * (j - 1) // 2)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER
There are $n$ traps numbered from $1$ to $n$. You will go through them one by one in order. The $i$-th trap deals $a_i$ base damage to you. Instead of going through a trap, you can jump it over. You can jump over no more than $k$ traps. If you jump over a trap, it does not deal any damage to you. But there is an additional rule: if you jump over a trap, all next traps damages increase by $1$ (this is a bonus damage). Note that if you jump over a trap, you don't get any damage (neither base damage nor bonus damage). Also, the bonus damage stacks so, for example, if you go through a trap $i$ with base damage $a_i$, and you have already jumped over $3$ traps, you get $(a_i + 3)$ damage. You have to find the minimal damage that it is possible to get if you are allowed to jump over no more than $k$ traps. -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) β€” the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $1 \le k \le n$) β€” the number of traps and the number of jump overs that you are allowed to make. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” base damage values of all traps. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case output a single integer β€” the minimal total damage that it is possible to get if you are allowed to jump over no more than $k$ traps. -----Examples----- Input 5 4 4 8 7 1 4 4 1 5 10 11 5 7 5 8 2 5 15 11 2 8 6 3 1 2 3 4 5 6 1 1 7 Output 0 21 9 6 0 -----Note----- In the first test case it is allowed to jump over all traps and take $0$ damage. In the second test case there are $5$ ways to jump over some traps: Do not jump over any trap. Total damage: $5 + 10 + 11 + 5 = 31$. Jump over the $1$-st trap. Total damage: $\underline{0} + (10 + 1) + (11 + 1) + (5 + 1) = 29$. Jump over the $2$-nd trap. Total damage: $5 + \underline{0} + (11 + 1) + (5 + 1) = 23$. Jump over the $3$-rd trap. Total damage: $5 + 10 + \underline{0} + (5 + 1) = 21$. Jump over the $4$-th trap. Total damage: $5 + 10 + 11 + \underline{0} = 26$. To get minimal damage it is needed to jump over the $3$-rd trap, so the answer is $21$. In the third test case it is optimal to jump over the traps $1$, $3$, $4$, $5$, $7$: Total damage: $0 + (2 + 1) + 0 + 0 + 0 + (2 + 4) + 0 = 9$.
for _ in range(int(input())): n, k = [int(i) for i in input().split()] A = [int(i) for i in input().split()] res = sum(A) - k * (k - 1) // 2 for i in range(n): A[i] -= n - i - 1 A.sort() for i in range(k): res -= A[-i - 1] print(res)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ traps numbered from $1$ to $n$. You will go through them one by one in order. The $i$-th trap deals $a_i$ base damage to you. Instead of going through a trap, you can jump it over. You can jump over no more than $k$ traps. If you jump over a trap, it does not deal any damage to you. But there is an additional rule: if you jump over a trap, all next traps damages increase by $1$ (this is a bonus damage). Note that if you jump over a trap, you don't get any damage (neither base damage nor bonus damage). Also, the bonus damage stacks so, for example, if you go through a trap $i$ with base damage $a_i$, and you have already jumped over $3$ traps, you get $(a_i + 3)$ damage. You have to find the minimal damage that it is possible to get if you are allowed to jump over no more than $k$ traps. -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) β€” the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $1 \le k \le n$) β€” the number of traps and the number of jump overs that you are allowed to make. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” base damage values of all traps. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case output a single integer β€” the minimal total damage that it is possible to get if you are allowed to jump over no more than $k$ traps. -----Examples----- Input 5 4 4 8 7 1 4 4 1 5 10 11 5 7 5 8 2 5 15 11 2 8 6 3 1 2 3 4 5 6 1 1 7 Output 0 21 9 6 0 -----Note----- In the first test case it is allowed to jump over all traps and take $0$ damage. In the second test case there are $5$ ways to jump over some traps: Do not jump over any trap. Total damage: $5 + 10 + 11 + 5 = 31$. Jump over the $1$-st trap. Total damage: $\underline{0} + (10 + 1) + (11 + 1) + (5 + 1) = 29$. Jump over the $2$-nd trap. Total damage: $5 + \underline{0} + (11 + 1) + (5 + 1) = 23$. Jump over the $3$-rd trap. Total damage: $5 + 10 + \underline{0} + (5 + 1) = 21$. Jump over the $4$-th trap. Total damage: $5 + 10 + 11 + \underline{0} = 26$. To get minimal damage it is needed to jump over the $3$-rd trap, so the answer is $21$. In the third test case it is optimal to jump over the traps $1$, $3$, $4$, $5$, $7$: Total damage: $0 + (2 + 1) + 0 + 0 + 0 + (2 + 4) + 0 = 9$.
def getMin(d, k): l = [] for i in range(len(d)): penalty = len(d) - i l.append((d[i] - penalty, i)) l.sort(reverse=True) total = 0 indices = set([l[i][1] for i in range(k, len(d))]) penalty = 0 for i in range(len(d)): if i in indices: total += d[i] + penalty else: penalty += 1 return total cases = int(input()) for i in range(cases): n, k = map(int, input().split()) d = list(map(int, input().split())) print(getMin(d, k))
FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR