This commit is contained in:
2026-01-21 20:35:30 +03:00
parent 535079b255
commit 416e31a5e8
9 changed files with 2967 additions and 0 deletions

View File

@@ -0,0 +1,61 @@
class Sequence:
def __init__(self):
self.sequence = [0]
self.current_idx = 0
def add_left(self, value):
self.sequence.insert(self.current_idx, value)
def add_right(self, value):
self.sequence.insert(self.current_idx + 1, value)
self.current_idx += 1
def task3() -> None:
N = int(input())
S = input()
seq = Sequence()
for i in range(1, N + 1):
ch = S[i - 1]
if ch == "L":
seq.add_left(i)
elif ch == "R":
seq.add_right(i)
print(" ".join(map(str, seq.sequence)))
def task4() -> None:
n, k = map(int, input().split())
a = list(map(int, input().split()))
dp = [[float("-inf")] * (k + 1) for _ in range(n + 1)]
dp[0][0] = 0
max_prev = [float("-inf")] * (k + 1)
max_prev[0] = 0
for i in range(1, n + 1):
for j in range(k + 1):
if i - 1 >= 0:
dp[i][j] = max(dp[i][j], dp[i - 1][j] + a[i - 1])
if i - 2 >= 0:
dp[i][j] = max(dp[i][j], dp[i - 2][j] + a[i - 1])
if j > 0:
dp[i][j] = max(dp[i][j], max_prev[j - 1])
for j in range(k + 1):
max_prev[j] = max(max_prev[j], dp[i][j])
print(max(dp[n]))
def main() -> None:
task4()
if __name__ == "__main__":
main()