Here is the code in C++ with scanf/printf. I ran it with file I/O but cin/cout on my laptop as well as command-line cin/cout. Both worked fine. The USACO grader did not work with these two either. scanf/printf does not work either. (please help, i know this will probably be a dumb mistake).
#include <cstdio>
#include <cassert>
#include <algorithm>
using namespace std;
using ll = long long;
int main(){
int n, k;
scanf("%d", &n);
scanf("%d", &k);
char s[10];
int gestures[n+1], dp[n+1][k+1][3];
for (int i = 1; i <= n; i++){
scanf("%s", s);
if (s[0]=='H') gestures[i] = 0;
else if (s[0]=='P') gestures[i] = 1;
else gestures[i] = 2;
}
int res = 0;
for (int i = 1; i <= n; i++){
for (int s = 0; s <= k; s++){
for (int p1 = 0; p1 < 3; p1++){
if (n){
dp[i][s][p1] = max(dp[i-1][s-1][(p1+1)%3], dp[i][s][p1]);
dp[i][s][p1] = max(dp[i-1][s-1][(p1+2)%3], dp[i][s][p1]);
}
dp[i][s][p1] = max(dp[i-1][s][p1], dp[i][s][p1]);
dp[i][s][p1] += (p1==gestures[i]);
if (i==n) res = max(res, dp[i][s][p1]);
}
}
}
printf("%d\n", res);
}