Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
r, s, p = map(int, input().split())

t = str(input())

t_ = ''
ans = 0
for i in range(n):
    if i < k:
        if t[i] == 'r':
            t_ += 'p'
            ans += p
        elif t[i] == 's':
            t_ += 'r'
            ans += r
        else:
            t_ += 's'
            ans += s
    elif i+k <= n-1:
        if t[i] == 'r':
            if t_[i-k] != 'p':
                t_ += 'p'
                ans += p
            else:
                if t[i+k] == 'p':
                    t_ += 'r'
                else:
                    t_ += 's'
        elif t[i] == 's':
            if t_[i-k] != 'r':
                t_ += 'r'
                ans += r
            else:
                if t[i+k] == 'p':
                    t_ += 'p'
                else:
                    t_ += 's'
        else:
            if t_[i-k] != 's':
                t_ += 's'
                ans += s
            else:
                if t[i+k] == 's':
                    t_ += 'p'
                else:
                    t_ += 'r'
    else:
        if t[i] == 'r':
            if t_[i-k] != 'p':
                t_ += 'p'
                ans += p
            else:
                if t[i-k] == 'r':
                    t_ += 's'
                else:
                    t_ += 'r'
        elif t[i] == 's':
            if t_[i-k] != 'r':
                t_ += 'r'
                ans += r
            else:
                if t[i-k] == 'p':
                    t_ += 's'
                else:
                    t_ += 'p'
        else:
            if t_[i-k] != 's':
                t_ += 's'
                ans += s
            else:
                if t[i-k] == 'r':
                    t_ += 'p'
                else:
                    t_ += 'r'
print(ans)
#print(t_)


Fixed submission:
n, k = map(int, input().split())
r, s, p = map(int, input().split())

t = str(input())

t_ = ''
ans = 0
for i in range(n):
    if i < k:
        if t[i] == 'r':
            t_ += 'p'
            ans += p
        elif t[i] == 's':
            t_ += 'r'
            ans += r
        else:
            t_ += 's'
            ans += s
    elif i+k <= n-1:
        if t[i] == 'r':
            if t_[i-k] != 'p':
                t_ += 'p'
                ans += p
            else:
                if t[i+k] == 'p':
                    t_ += 'r'
                else:
                    t_ += 's'
        elif t[i] == 's':
            if t_[i-k] != 'r':
                t_ += 'r'
                ans += r
            else:
                if t[i+k] == 'p':
                    t_ += 'p'
                else:
                    t_ += 's'
        else:
            if t_[i-k] != 's':
                t_ += 's'
                ans += s
            else:
                if t[i+k] == 's':
                    t_ += 'p'
                else:
                    t_ += 'r'
    else:
        if t[i] == 'r':
            if t_[i-k] != 'p':
                t_ += 'p'
                ans += p
            else:
                if t[i-k] == 'r':
                    t_ += 's'
                else:
                    t_ += 'r'
        elif t[i] == 's':
            if t_[i-k] != 'r':
                t_ += 'r'
                ans += r
            else:
                if t[i-k] == 'p':
                    t_ += 's'
                else:
                    t_ += 'p'
        else:
            if t_[i-k] != 's':
                t_ += 's'
                ans += s
            else:
                if t[i-k] == 'r':
                    t_ += 'p'
                else:
                    t_ += 'r'
print(ans)
#print(t_)