diff --git a/solutions/gold/usaco-694.mdx b/solutions/gold/usaco-694.mdx index f313cdab5f..a2263ff5a6 100644 --- a/solutions/gold/usaco-694.mdx +++ b/solutions/gold/usaco-694.mdx @@ -180,35 +180,36 @@ dp = [[[0] * 3 for _ in range(25)] for _ in range(MX)] A = [0] * MX with open("hps.in") as read: - n, k = map(int, read.readline().strip().split()) + n, k = map(int, read.readline().strip().split()) - for i in range(n): - a = read.readline().strip() - if a == 'H': - A[i] = 0 - elif a == 'P': - A[i] = 1 - else: - A[i] = 2 + for i in range(n): + a = read.readline().strip() + if a == "H": + A[i] = 0 + elif a == "P": + A[i] = 1 + else: + A[i] = 2 # either she switches to h or p or s or stays for i in range(n): - for j in range(k + 1): - for l in range(3): - if l == A[i]: - dp[i][j][l] += 1 - - dp[i + 1][j + 1][0] = max(dp[i + 1][j + 1][0], - dp[i][j][l]); # switch to not item - dp[i + 1][j + 1][1] = max(dp[i + 1][j + 1][1], - dp[i][j][l]); # switch to not item - dp[i + 1][j + 1][2] = max(dp[i + 1][j + 1][2], - dp[i][j][l]); # switch to not item - dp[i + 1][j][l] = max(dp[i + 1][j][l], dp[i][j][l]); # stay + for j in range(k + 1): + for l in range(3): + if l == A[i]: + dp[i][j][l] += 1 + + dp[i + 1][j + 1][0] = max(dp[i + 1][j + 1][0], dp[i][j][l]) + # switch to not item + dp[i + 1][j + 1][1] = max(dp[i + 1][j + 1][1], dp[i][j][l]) + # switch to not item + dp[i + 1][j + 1][2] = max(dp[i + 1][j + 1][2], dp[i][j][l]) + # switch to not item + dp[i + 1][j][l] = max(dp[i + 1][j][l], dp[i][j][l]) + # stay res = 0 for i in range(3): - res = max(res, dp[n - 1][k][i]) + res = max(res, dp[n - 1][k][i]) print(res, file=open("hps.out", "w")) ```