Language: C++

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	r := bufio.NewReader(os.Stdin)
	var N, K, R, S, P int
	fmt.Fscan(r, &N)
	fmt.Fscan(r, &K)
	fmt.Fscan(r, &R)
	fmt.Fscan(r, &S)
	fmt.Fscan(r, &P)
	var t string
	fmt.Fscan(r, &t)

	actual := make([]byte, N)
	ans := 0
	for i := 0; i < N; i++ {
		win := getWin(t[i])
		if i-K < 0 || win != actual[i-K] {
			// 勝てる場合
			ans += getPoint(win, R, S, P)
			actual[i] = win
		} else {
			// 勝てない場合はK個先で勝てるように出す
			if i+K < N {
				tmp := t[i+K]
				actual[i] = getExclude(win, tmp)
			}
		}
	}

	fmt.Println(ans)
}

func getWin(h byte) byte {
	if h == 'r' {
		return 'p'
	} else if h == 's' {
		return 'r'
	} else {
		return 's'
	}
}

func getPoint(h byte, r, s, p int) int {
	if h == 'r' {
		return r
	} else if h == 's' {
		return s
	} else {
		return p
	}
}

func getExclude(x, y byte) byte {
	if x == 'r' {
		if y == 's' {
			return 'p'
		} else {
			return 's'
		}
	} else if x == 's' {
		if y == 'r' {
			return 'p'
		} else {
			return 'r'
		}
	} else {
		if y == 'r' {
			return 's'
		} else {
			return 'r'
		}
	}
}

// Union-Find
type unionFind struct {
	d []int
}

func newUnionFind(n int) *unionFind {
	uf := new(unionFind)
	uf.d = make([]int, n)
	for i := 0; i < n; i++ {
		uf.d[i] = -1
	}
	return uf
}

func (uf *unionFind) find(x int) int {
	if uf.d[x] < 0 {
		return x
	}
	uf.d[x] = uf.find(uf.d[x])
	return uf.d[x]
}

func (uf *unionFind) unite(x, y int) bool {
	rootX := uf.find(x)
	rootY := uf.find(y)
	if rootX == rootY {
		return false
	}

	if uf.d[rootX] < uf.d[rootY] {
		uf.d[rootX] += uf.d[rootY]
		uf.d[rootY] = rootX
	} else {
		uf.d[rootY] += uf.d[rootX]
		uf.d[rootX] = rootY
	}

	return true
}

func (uf *unionFind) same(x, y int) bool {
	return uf.find(x) == uf.find(y)
}

func (uf *unionFind) size(x int) int {
	return -uf.d[uf.find(x)]
}

// Utility

func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

func abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

func primeFactor(x int) map[int]int {
	res := make(map[int]int)
	for i := 2; i*i <= x; i++ {
		for x%i == 0 {
			res[i]++
			x = x / i
		}
	}
	if x != 1 {
		res[x] = 1
	}
	return res
}

func divisor(x int) []int {
	res := make([]int, 0)
	for i := 1; i*i <= x; i++ {
		if x%i == 0 {
			res = append(res, i)
			if i != x/i {
				res = append(res, x/i)
			}
		}
	}
	return res
}

func gcd(x, y int) int {
	if y == 0 {
		return x
	}
	return gcd(y, x%y)
}

func lcm(x, y int) int {
	return x / gcd(x, y) * y
}


Compiler output:
a.cc:1:1: error: 'package' does not name a type
    1 | package main
      | ^~~~~~~
a.cc:40:1: error: 'func' does not name a type
   40 | func getWin(h byte) byte {
      | ^~~~
a.cc:50:1: error: 'func' does not name a type
   50 | func getPoint(h byte, r, s, p int) int {
      | ^~~~
a.cc:60:1: error: 'func' does not name a type
   60 | func getExclude(x, y byte) byte {
      | ^~~~
a.cc:83:1: error: 'type' does not name a type; did you mean 'typeof'?
   83 | type unionFind struct {
      | ^~~~
      | typeof
a.cc:87:1: error: 'func' does not name a type
   87 | func newUnionFind(n int) *unionFind {
      | ^~~~
a.cc:96:6: error: expected constructor, destructor, or type conversion before '(' token
   96 | func (uf *unionFind) find(x int) int {
      |      ^
a.cc:104:6: error: expected constructor, destructor, or type conversion before '(' token
  104 | func (uf *unionFind) unite(x, y int) bool {
      |      ^
a.cc:122:6: error: expected constructor, destructor, or type conversion before '(' token
  122 | func (uf *unionFind) same(x, y int) bool {
      |      ^
a.cc:126:6: error: expected constructor, destructor, or type conversion before '(' token
  126 | func (uf *unionFind) size(x int) int {
      |      ^
a.cc:132:1: error: 'func' does not name a type
  132 | func min(x, y int) int {
      | ^~~~
a.cc:139:1: error: 'func' does not name a type
  139 | func max(x, y int) int {
      | ^~~~
a.cc:146:1: error: 'func' does not name a type
  146 | func abs(x int) int {
      | ^~~~
a.cc:153:1: error: 'func' does not name a type
  153 | func primeFactor(x int) map[int]int {
      | ^~~~
a.cc:167:1: error: 'func' does not name a type
  167 | func divisor(x int) []int {
      | ^~~~
a.cc:180:1: error: 'func' does not name a type
  180 | func gcd(x, y int) int {
      | ^~~~
a.cc:187:1: error: 'func' does not name a type
  187 | func lcm(x, y int) int {
      | ^~~~