You are playing a new famous fighting game: Kortal Mombat XII. You have to perform a brutality on your opponent's character.

You are playing the game on the new generation console so your gamepad have $26$ buttons. Each button has a single lowercase Latin letter from 'a' to 'z' written on it. All the letters on buttons are pairwise distinct.

You are given a sequence of hits, the $i$-th hit deals $a_i$ units of damage to the opponent's character. To perform the $i$-th hit you have to press the button $s_i$ on your gamepad. Hits are numbered from $1$ to $n$.

You know that if you press some button more than $k$ times in a row then it'll break. You cherish your gamepad and don't want to break any of its buttons.

To perform a brutality you have to land some of the hits of the given sequence. You are allowed to skip any of them, however changing the initial order of the sequence is prohibited. The total damage dealt is the sum of $a_i$ over all $i$ for the hits which weren't skipped.

Note that if you skip the hit then the counter of consecutive presses the button won't reset.

Your task is to skip some hits to deal the maximum possible total damage to the opponent's character and not break your gamepad buttons.


-----Input-----

The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the number of hits and the maximum number of times you can push the same button in a row.

The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the damage of the $i$-th hit.

The third line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters — the sequence of hits (each character is the letter on the button you need to press to perform the corresponding hit).


-----Output-----

Print one integer $dmg$ — the maximum possible damage to the opponent's character you can deal without breaking your gamepad buttons.


-----Examples-----
Input
7 3
1 5 16 18 7 2 10
baaaaca

Output
54

Input
5 5
2 4 1 3 1000
aaaaa

Output
1010

Input
5 4
2 4 1 3 1000
aaaaa

Output
1009

Input
8 1
10 15 2 1 4 8 15 16
qqwweerr

Output
41

Input
6 3
14 18 9 19 2 15
cccccc

Output
52

Input
2 1
10 10
qq

Output
10



-----Note-----

In the first example you can choose hits with numbers $[1, 3, 4, 5, 6, 7]$ with the total damage $1 + 16 + 18 + 7 + 2 + 10 = 54$.

In the second example you can choose all hits so the total damage is $2 + 4 + 1 + 3 + 1000 = 1010$.

In the third example you can choose all hits expect the third one so the total damage is $2 + 4 + 3 + 1000 = 1009$.

In the fourth example you can choose hits with numbers $[2, 3, 6, 8]$. Only this way you can reach the maximum total damage $15 + 2 + 8 + 16 = 41$.

In the fifth example you can choose only hits with numbers $[2, 4, 6]$ with the total damage $18 + 19 + 15 = 52$.

In the sixth example you can change either first hit or the second hit (it does not matter) with the total damage $10$.

Difficulty: EASY
Tags: Sorting

Amortized analysis

Greedy algorithms
Skill types: Sorting

Amortized analysis

Greedy algorithms

Starter code:

Solutions:
(n, k) = map(int, input().split())
l = list(map(int, input().split()))
s = input()
i = ans = 0
while i < n:
	j = i
	while i + 1 < n and s[i + 1] == s[i]:
		i += 1
	ans += sum(sorted(l[j:i + 1])[-k:])
	i += 1
print(ans)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = input()
re = sum(a)
cc = []
(f, t) = (0, 0)
while t < n:
	t += 1
	while t < n and s[t] == s[t - 1]:
		t += 1
	cc = a[f:t]
	if len(cc) > k:
		cc.sort()
		for i in range(len(cc) - k):
			re -= cc[i]
	f = t
print(re)


(n, k) = map(int, input().split())
A = list(map(int, input().split()))
S = input()
ans = j = 0
n = len(S)
for i in range(n):
	if i == n - 1 or S[i] != S[i + 1]:
		ans += sum(sorted(A[j:i + 1])[-k:])
		j = i + 1
print(ans)


import os
import sys
from atexit import register
from io import BytesIO
sys.stdin = BytesIO(os.read(0, os.fstat(0).st_size))
sys.stdout = BytesIO()
register(lambda : os.write(1, sys.stdout.getvalue()))
input = lambda : sys.stdin.readline().rstrip(b'\r\n')

def print(*args, sep=b' ', end=b'\n'):
	sys.stdout.write(sep.join(map(lambda x: str(x).encode(), args)))
	sys.stdout.write(end)

def main():
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	s = list(input())
	(res, cnt) = (0, 1)
	parts = []
	prev = s[0]
	for i in range(1, n):
		if s[i] == prev:
			cnt += 1
		else:
			parts.append(cnt)
			cnt = 1
		prev = s[i]
	parts.append(cnt)
	i = 0
	for part in parts:
		segment = []
		for j in range(i, i + part):
			segment.append(a[j])
		res += sum(sorted(segment, reverse=True)[:k])
		i += part
	print(res)
main()


y = input().split()
n = int(y[0])
k = int(y[1])
a = list(map(int, input().split()))
b = ''
t = 0
g = []
p = 0
a.append(0)
s = input() + '0'
for i in range(len(s)):
	if b != s[i]:
		b = s[i]
		p = k
		g.sort()
		for j in range(len(g) - 1, -1, -1):
			t += g[j]
			p -= 1
			if p == 0:
				break
		g = []
		g.append(a[i])
	else:
		g.append(a[i])
print(t)


import itertools
(n, k) = list(map(int, input().split()))
an = list(map(int, input().split()))
s = input()
res = 0
now = 0
for (c, g) in itertools.groupby(s):
	l = len(list(g))
	if l <= k:
		res += sum(an[now:now + l])
	if l > k:
		res += sum(sorted(an[now:now + l])[-k:])
	now += l
print(res)


(n, k) = list(map(int, input().split()))
a = list(map(int, input().split()))
s = list(input())
ls = []
cur = s[0]
temp = [a[0]]
(start, end) = (0, 0)
ind = []
for i in range(1, n):
	if s[i] == cur:
		temp.append(a[i])
		end = i
		if i == n - 1 and len(temp) > k:
			ls.append(temp)
			ind.append((start, end))
	else:
		cur = s[i]
		if len(temp) > k:
			ls.append(temp)
			ind.append((start, end))
		temp = [a[i]]
		start = i
		end = i
ans = 0
for i in range(len(ls)):
	temp = ls[i]
	temp.sort(reverse=True)
	ans += sum(temp[:k])
val = {}
for i in range(len(ind)):
	(x, y) = ind[i]
	y += 1
	for i in range(x, y):
		a[i] = 0
for i in range(len(s)):
	if a[i] != ' ':
		ans += a[i]
print(ans)


import string
(n, k) = map(int, input().split())
num = [0] + list(map(int, input().split())) + [0]
s = '$' + input() + '$'
val = 1
data = []
data.append(num[0])
if n == 1:
	print(num[1])
	exit()
ans = 0
for i in range(1, n + 2):
	if s[i] == s[i - 1]:
		data.append(num[i])
	else:
		data.sort(reverse=True)
		if len(data) > k:
			ans += sum(data[:k])
		else:
			ans += sum(data)
		data = [num[i]]
print(ans)


def cal(array, k):
	temp2 = 0
	ans = 0
	while temp2 < k:
		ans += array[temp2]
		temp2 += 1
	return ans
(n, k) = map(int, input().split())
l = list(map(int, input().split()))
s = input()
i = 0
j = 0
ans = 0
while j < n:
	if s[i] == s[j]:
		j += 1
	elif j - i + 1 <= k:
		ans += sum(l[i:j])
		i = j
	else:
		temp = l[i:j]
		temp.sort(reverse=True)
		ans += cal(temp, k)
		i = j
if j - i + 1 < k:
	ans += sum(l[i:j])
else:
	temp = l[i:j]
	temp.sort(reverse=True)
	ans += cal(temp, k)
print(ans)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = input() + '0'
i = S = 0
while i < n:
	pos1 = i
	while s[i + 1] == s[i]:
		i += 1
	pos2 = i
	d = pos2 - pos1 + 1
	if d <= k:
		S += sum(a[pos1:pos2 + 1])
	else:
		S += sum(sorted(a[pos1:pos2 + 1])[-k:])
	i += 1
print(S)


from heapq import heapify, heappush, heappop
(n, k) = map(int, input().split())
A = list(map(int, input().split()))
s = input()
h = [A[0]]
heapify(h)
ans = A[0]
for i in range(1, n):
	if s[i] == s[i - 1]:
		if len(h) == k:
			elem = heappop(h)
			if elem > A[i]:
				heappush(h, elem)
			else:
				heappush(h, A[i])
				ans += A[i] - elem
		else:
			heappush(h, A[i])
			ans += A[i]
	else:
		h = [A[i]]
		heapify(h)
		ans += A[i]
print(ans)


(n, k) = map(int, input().split())
arr = list(map(int, input().split()))
s = input()
stack = []
c = 0
temp = []
ans = 0
for i in range(len(s)):
	if len(stack) == 0:
		stack.append(s[i])
		temp.append(arr[i])
		continue
	elif s[i] == stack[-1]:
		c += 1
		temp.append(arr[i])
	else:
		c = 0
		temp = sorted(temp, reverse=True)
		ans += sum(temp[0:k])
		xx = []
		temp = xx[:]
		temp.append(arr[i])
		stack.append(s[i])
		c += 1
temp = sorted(temp, reverse=True)
ans += sum(temp[0:k])
print(ans)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = input()
l = 0
r = 1
ans = 0
while l < n:
	ls = [a[l]]
	r = l + 1
	while r < n and s[l] == s[r]:
		ls.append(a[r])
		r += 1
	seql = r - l
	nn = 0
	if seql > k:
		nn = seql // k
		if nn > 1:
			nn -= 1
	ls.sort(reverse=True)
	ans += sum(ls[0:min(k, len(ls))])
	l = r
print(ans)


(n, lim) = map(int, input().split())
a = [int(i) for i in input().split()]
b = input()
i = 0
d = [1]
now = b[0]
for i in range(1, n):
	if b[i] == now:
		d[-1] += 1
	else:
		now = b[i]
		d.append(1)
dmg = 0
now = 0
for r in d:
	c = a[now:now + r]
	now = now + r
	c.sort(reverse=True)
	dmg += sum(c[:lim])
print(dmg)


def max_sum(m, n):
	if n >= len(m):
		return sum(m)
	else:
		return sum(sorted(m)[len(m) - n:])

def main():
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	s = []
	prev_c = ''
	it = -1
	pos = -1
	for c in input():
		pos += 1
		if c != prev_c:
			prev_c = c
			it += 1
			s.append([pos, pos + 1])
		else:
			s[it][1] += 1
	ans = 0
	for ss in s:
		ans += max_sum(a[ss[0]:ss[1]], k)
	print(ans)
main()


from sys import stdin
(n, m) = list(map(int, stdin.readline().strip().split()))
s = list(map(int, stdin.readline().strip().split()))
s1 = stdin.readline().strip()
x = [s[0]]
cur = s1[0]
y = 1
ans = 0
while y < n:
	if s1[y] == cur:
		x.append(s[y])
	else:
		cur = s1[y]
		if len(x) > m:
			x.sort()
			for i in range(m):
				ans += x[-1]
				x.pop()
		else:
			ans += sum(x)
		x = [s[y]]
	y += 1
if len(x) > m:
	x.sort()
	for i in range(m):
		ans += x[-1]
		x.pop()
else:
	ans += sum(x)
print(ans)


def main():
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	s = input().strip()
	last = s[0]
	seg = [a[0]]
	all = []
	for i in range(1, len(s)):
		if last == s[i]:
			seg.append(a[i])
		else:
			last = s[i]
			all.append(seg)
			seg = [a[i]]
	if seg:
		all.append(seg)
	ans = 0
	for seg in all:
		if k < len(seg):
			seg = sorted(seg, reverse=True)[:k]
		ans += sum(seg)
	print(ans)
main()


(n, m) = map(int, input().split())
d = list(map(int, input().split()))
s = input()
i = res = 0
while i < len(s):
	j = i
	while i + 1 < len(s) and s[i + 1] == s[i]:
		i += 1
	res += sum(sorted(d[j:i + 1])[-m:])
	i += 1
print(res)


(n, k) = map(int, input().split())
X = [int(a) for a in input().split()]
s = input()
i = 1
p = s[0]
l = [X[0]]
c = 1
ans = 0
while i <= n:
	if i < n and p == s[i]:
		l.append(X[i])
		c += 1
	else:
		if len(l) <= k:
			ans += sum(l)
		else:
			l = sorted(l)
			ans += sum(l[-k:])
		if i == n:
			break
		l = [X[i]]
		p = s[i]
		c = 1
	i += 1
print(ans)


import os
import sys
from io import BytesIO, IOBase
from collections import defaultdict, deque, Counter, OrderedDict
import threading
from heapq import *

def main():
	(n, k) = map(int, input().split())
	a = [*map(int, input().split())]
	s = input()
	ans = 0
	i = 1
	D = [0]
	while 1:
		if i >= n:
			break
		if s[D[0]] == s[i]:
			D.append(i)
		else:
			z = 0
			D.sort(key=lambda z: a[z], reverse=True)
			for j in range(len(D)):
				if z == k:
					break
				ans += a[D[j]]
				z += 1
			D = [i]
		i += 1
	z = 0
	D.sort(key=lambda z: a[z], reverse=True)
	for i in range(len(D)):
		if z == k:
			break
		ans += a[D[i]]
		z += 1
	print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


from string import ascii_lowercase
(n, k) = map(int, input().split())
a = list(map(int, input().split()))
keys = input()
ans = 0
pos = 0
while pos < n:
	end = pos
	while end < n - 1 and keys[pos] == keys[end + 1]:
		end += 1
	if end - pos + 1 <= k:
		for i in range(pos, end + 1):
			ans += a[i]
	else:
		x = sorted(a[pos:end + 1], reverse=True)
		for i in range(k):
			ans += x[i]
	pos = end + 1
print(ans)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = input()
i = 0
ans = 0
while i < n:
	c = 0
	for j in range(i, n):
		if s[i] == s[j]:
			c += 1
		else:
			break
	x = a[i:i + c]
	x.sort(reverse=True)
	x = x[0:k]
	ans += sum(x)
	if c == 0:
		ans += a[i]
	i += c
print(ans)


(n, k) = map(int, input().split())
v = [int(i) for i in input().split()]
a = input()
(i, j) = (0, 1)
dmg = 0
while i < n:
	while j < n and a[i] == a[j]:
		j += 1
	if j - i > k:
		b = sorted(v[i:j])
		for i in range(k):
			dmg += b[-i - 1]
	else:
		for e in v[i:j]:
			dmg += e
	i = j
print(dmg)


(n, k) = list(map(int, input().split()))
a = list(map(int, input().split()))
s = input()
now = 0
count = 0
i = 0
dam = 0
while now < n:
	i = now
	while i < n and s[now] == s[i]:
		i += 1
	ud = a[now:i]
	ud = sorted(ud)
	c = len(ud) - 1
	while now != i and len(ud) - c - 1 < k:
		dam += ud[c]
		c -= 1
		now += 1
	now = i
print(dam)


(n, k) = map(int, input().split())
fat = list(map(int, input().split()))
comb = input()
b = []
ans = 0
last = -1
for i in range(n):
	if comb[i] != last:
		b.sort()
		sch = k
		for j in range(len(b) - 1, -1, -1):
			if sch == 0:
				break
			ans += b[j]
			sch -= 1
		b = []
	b.append(fat[i])
	last = comb[i]
b.sort()
sch = k
for j in range(len(b) - 1, -1, -1):
	if sch == 0:
		break
	ans += b[j]
	sch -= 1
print(ans)


R = lambda : map(int, input().split())
(n, k) = R()
L = list(R())
s = [i for i in input()]
i = 0
pre = s[0]
res = 0
while i < n:
	j = i
	while i < n and s[i] == pre:
		i += 1
	l = i - j
	if l > k:
		res += sum(sorted(L[j:i], reverse=True)[:k])
	else:
		for p in range(j, i):
			res += L[p]
	if i < n:
		pre = s[i]
	else:
		break
print(res)


import itertools
(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = input()
z = itertools.groupby(s)
z = [(i, sum((1 for t in j))) for (i, j) in z]
count = 0
ans = 0
for (i, j) in z:
	if j < k:
		ans += sum(a[count:count + j])
	else:
		ans += sum(sorted(a[count:count + j])[-k:])
	count += j
print(ans)


(n, k) = list(map(int, input().split()))
l = list(map(int, input().split()))
s = list(input())
ans = 0
tmp = [l[0]]
for i in range(1, n):
	if s[i] == s[i - 1]:
		tmp.append(l[i])
	else:
		tmp.sort(reverse=True)
		for j in range(min(len(tmp), k)):
			ans += tmp[j]
		tmp = [l[i]]
tmp.sort(reverse=True)
for j in range(min(len(tmp), k)):
	ans += tmp[j]
print(ans)


import heapq
n = input().split()
k = int(n[1])
lst = input().split()
string = input()
t = 0
i = 0
while i < int(n[0]):
	a = []
	count = 1
	j = i
	if i < int(n[0]) - 1 and string[i] != string[i + 1]:
		t += int(lst[i])
	else:
		while j < int(n[0]) - 1 and string[j] == string[j + 1]:
			count += 1
			a.append(int(lst[j]))
			j += 1
		a.append(int(lst[j]))
		if count <= k:
			t += sum(a)
		else:
			t += sum(heapq.nlargest(k, a))
	i = j + 1
print(t)


for _ in range(1):
	(n, k) = map(int, input().split())
	l = list(map(int, input().split()))
	s = input()
	ans = 0
	i = 0
	while i <= n - 1:
		j = i + 1
		while j <= n - 1 and s[i] == s[j]:
			j += 1
		ans += sum(sorted(l[i:j], reverse=True)[:k])
		i = j
	print(ans)


import heapq
(n, k) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
s = input()
heap = []
total = 0
last = None
for (ai, si) in zip(a, s):
	if si == last:
		heapq.heappush(heap, -ai)
	else:
		smallest = heapq.nsmallest(k, heap)
		total += sum(smallest)
		heap = []
		last = si
		heapq.heappush(heap, -ai)
smallest = heapq.nsmallest(k, heap)
total += sum(smallest)
heap = []
last = si
heapq.heappush(heap, -ai)
print(-total)


(n, k) = map(int, input().split())
arr = list(map(int, input().split()))
string = input()
if n == 1 or n < k:
	print(sum(arr))
	exit()
(i, j) = (0, 1)
(sums, add) = ([0] * n, 0)
(sums[0], ans) = (arr[0], 0)
while j < n:
	if string[i] == string[j]:
		while j < n and string[i] == string[j]:
			j += 1
		if string[i] == string[j - 1]:
			modi = sorted(arr[i:j])
			if j - i > k:
				ans += sum(modi[len(modi) - k:])
			else:
				ans += sum(modi)
			i = j
			j += 1
	else:
		ans += arr[i]
		i = j
		j += 1
if i < n:
	ans += arr[i]
print(ans)


(n, k) = list(map(int, input().split()))
scorelist = list(map(int, input().split()))
keylist = list(input())
keystreaks = []
currentstreak = [[scorelist[0], keylist[0]]]
for i in range(1, n):
	if keylist[i] == keylist[i - 1]:
		currentstreak.append([scorelist[i], keylist[i]])
	else:
		keystreaks.append(currentstreak)
		currentstreak = [[scorelist[i], keylist[i]]]
keystreaks.append(currentstreak)
bigtotal = 0
for streak in keystreaks:
	total = 0
	streak.sort(key=lambda x: -x[0])
	tally = 0
	while tally < k and tally < len(streak):
		total = total + streak[tally][0]
		tally += 1
	bigtotal = bigtotal + total
print(bigtotal)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
st = input()
pr = None
cl = 0
ans = 0
vs = []
for (i, s) in enumerate(st):
	if s == pr:
		cl += 1
		vs.append(a[i])
		pr = s
	elif pr is not None:
		vs.sort(reverse=True)
		ans += sum(vs[:k])
		pr = s
		cl = 1
		vs = [a[i]]
	else:
		pr = s
		cl += 1
		vs.append(a[i])
vs.sort(reverse=True)
ans += sum(vs[:k])
print(ans)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = input()
last = s[0]
s1 = s[1:]
a1 = a[1:]
current = [a[0]]
dmg = 0
for i in range(len(s1)):
	if s1[i] == last:
		current.append(a1[i])
	else:
		dmg += sum(sorted(current, reverse=True)[0:k])
		last = s1[i]
		current = [a1[i]]
dmg += sum(sorted(current, reverse=True)[0:k])
print(dmg)


import sys
input = sys.stdin.readline
(n, k) = map(int, input().split())
l = list(map(int, input().split()))
s = input()
s += '*'
summ = sum(l)
l.append(999999999999)
count = 1
temp = [l[0]]
for i in range(n):
	if s[i] == s[i + 1]:
		count += 1
		temp.append(l[i + 1])
	else:
		if count > k:
			temp.sort()
			for j in range(count - k):
				summ -= temp[j]
		temp = [l[i + 1]]
		count = 1
print(summ)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
a.append(0)
s = input() + '!'
(ans, L, R) = (0, 0, 1)
while R <= n:
	if s[L] == s[R]:
		R += 1
	else:
		ans += sum(sorted(a[L:R])[-k:])
		L = R
		R += 1
print(ans)


(n, k) = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
s = input()
temp = []
q = [a[0]]
j = 1
p = s[0]
for i in range(1, n):
	if s[i] != p:
		temp.append(q)
		q = []
		j = 0
		p = s[i]
		j = 1
		q = [a[i]]
	else:
		q.append(a[i])
		j += 1
if len(q) != 0:
	temp.append(q)
for i in temp:
	i.sort()
ans = 0
for i in range(len(temp)):
	ans += sum(temp[i])
	if len(temp[i]) > k:
		for j in range(len(temp[i]) - k):
			ans -= temp[i][j]
print(ans)


from sys import stdin, stdout
from math import gcd, sqrt
from collections import deque
input = stdin.readline
R = lambda : map(int, input().split())
I = lambda : int(input())
S = lambda : input().rstrip('\n')
L = lambda : list(R())
P = lambda x: stdout.write(x)
hg = lambda x, y: (y + x - 1) // x * x
pw = lambda x: 1 if x == 1 else 1 + pw(x // 2)
chk = lambda x: chk(x // 2) if not x % 2 else True if x == 1 else False
N = 10 ** 6 + 7
(n, k) = R()
a = L()
s = S() + '1'
v = {chr(i + 97): 0 for i in range(26)}
bck = '1'
l = r = 0
ans = 0
for i in range(n + 1):
	if s[i] != bck:
		if r - l + 1 > k:
			ans += sum(sorted(a[l:r], reverse=True)[:k])
		else:
			ans += sum(a[l:r])
		l = i
		r = i + 1
		bck = s[i]
	else:
		r += 1
print(ans)


(n, k) = list(map(int, input().split()))
ka = list(map(int, input().split()))
l = input()
ja = []
s = 0
sa = 0
for x in range(n):
	ja.append(ka[x])
	s += 1
	if x != n - 1 and l[x] == l[x + 1]:
		pass
	elif x == n - 1:
		if s == 1:
			sa += ka[-1]
		else:
			ja.sort(reverse=True)
			sa += sum(ja[0:k])
	else:
		if s <= k:
			sa += sum(ja)
			s = 0
		else:
			ja.sort(reverse=True)
			sa += sum(ja[0:k])
			s = 0
		ja = []
print(sa)


k = int(input().split()[1])
dmg = [int(i) for i in input().split()]
dmg.append(0)
letters = input() + '*'
max_map = {}
times = 0
let = '*'
arr = []
result = 0
for i in range(len(dmg)):
	if not let == letters[i]:
		if times > k:
			arr.sort(reverse=True)
			result += sum(arr[0:k])
		else:
			result += sum(arr)
		let = letters[i]
		times = 1
		arr = [dmg[i]]
	else:
		times += 1
		arr.append(dmg[i])
print(result)


(n, k) = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
s = input()
dmg = 0
x = 0
while x < n:
	idx = x
	for y in range(x + 1, n):
		if s[x] == s[y]:
			idx = y
		else:
			break
	if x == idx:
		dmg += a[x]
		x += 1
	else:
		b = a[x:idx + 1]
		b.sort(reverse=True)
		for y in range(min(len(b), k)):
			dmg += b[y]
		x = idx + 1
print(dmg)


def brutal(n1, n2, m, comb):
	m = [int(m[i]) for i in range(len(m))]
	if n2 > n1:
		return 'Incorrect input'
	count = []
	new = comb[0]
	result = 0
	for i in range(len(comb)):
		if comb[i] != new:
			if len(count) <= n2:
				result += sum(count)
			else:
				count.sort()
				for j in range(len(count) - 1, len(count) - n2 - 1, -1):
					result += count[j]
			new = comb[i]
			count = []
		if comb[i] == new:
			count.append(m[i])
	if len(count) <= n2:
		result += sum(count)
	else:
		count.sort()
		for j in range(len(count) - 1, len(count) - n2 - 1, -1):
			result += count[j]
	return result
n = input().strip().split()
print(brutal(int(n[0]), int(n[1]), input().strip().split(), input().strip()))


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = input()
start = 0
intervals = []
for i in range(n - 1):
	if s[i] != s[i + 1]:
		intervals.append((start, i + 1))
		start = i + 1
intervals.append((start, n))
total = 0
for interval in intervals:
	if interval[1] - interval[0] > k:
		remove = interval[1] - interval[0] - k
		order = sorted(a[interval[0]:interval[1]])
		total += sum(order[:remove])
print(sum(a) - total)


(n, m) = map(int, input().split())
nums = list(map(int, input().split()))
string = list(input())
dmg = 0
start = 0
end = 0
while end < n:
	while end < n and string[start] == string[end]:
		end += 1
	if end - start <= m:
		dmg += sum(nums[start:end])
	else:
		dmg += sum(sorted(nums[start:end], reverse=True)[:m])
	start = end
print(dmg)


(n, k) = list(map(int, input().split()))
a = list(map(int, input().split()))
s = input()
l = []
prev = None
cnt = 0
for i in s:
	if prev is None:
		prev = i
		cnt += 1
		continue
	if i != prev:
		l.append(cnt)
		cnt = 1
		prev = i
	else:
		cnt += 1
l.append(cnt)
left = 0
ans = 0
for cnt in l:
	if cnt > k:
		ans += sum(sorted(a[left:left + cnt], reverse=True)[:k])
	else:
		ans += sum(a[left:left + cnt])
	left += cnt
print(ans)


(n, k) = list(map(int, input().split()))
pl = list(map(int, input().split()))
s = input()
count = 1
res = 0
for i in range(n - 1):
	if s[i] == s[i + 1]:
		count += 1
	elif count > k:
		el = sorted(pl[i - count + 1:i + 1])
		res += sum(el[count - k:])
		count = 1
	else:
		res += sum(pl[i - count + 1:i + 1])
		count = 1
if count > k:
	el = sorted(pl[n - count:])
	res += sum(el[count - k:])
else:
	el = pl[n - count:]
	res += sum(el)
print(res)


from itertools import combinations
from sys import stdin, stdout

def listIn():
	return list(map(int, stdin.readline().strip().split()))

def stringListIn():
	return [x for x in stdin.readline().split()]

def intIn():
	return int(stdin.readline())

def stringIn():
	return stdin.readline()
(n, k) = listIn()
a = listIn()
s = stringIn()
if n == 1:
	print(a[0])
	exit(0)
ans = [0] * n
cnt = 1
for i in range(1, n):
	if s[i] == s[i - 1]:
		cnt += 1
	else:
		ans[i - cnt] = cnt
		cnt = 1
	if i == n - 1:
		if cnt != 1:
			ans[i - cnt + 1] = cnt
		else:
			ans[i] = cnt
total = 0
i = 0
while i < n:
	if ans[i] > k:
		m = a[i:i + ans[i]]
		m = sorted(m)[::-1]
		total += sum(m[:k])
	else:
		m = a[i:i + ans[i]]
		m = sorted(m)
		total += sum(m)
	i = i + ans[i]
print(total)


(n, k) = map(int, input().split())
hits = list(map(int, input().split()))
sequence = input()
count = 1
total = 0
for i in range(1, n + 1):
	if i == n or sequence[i] != sequence[i - 1]:
		x = hits[i - count:i]
		x.sort()
		total += sum(x[max(0, len(x) - k):len(x)])
		count = 1
	else:
		count += 1
print(total)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = input()
i = 0
tot = 0
while i < n:
	count = 0
	ch = s[i]
	arr = []
	while i < n and s[i] == ch:
		arr.append(a[i])
		count += 1
		i += 1
	if count <= k:
		tot += sum(arr)
	else:
		arr.sort()
		tot += sum(arr[count - k:count])
print(tot)


from sys import stdin, stdout
(n, k) = map(int, stdin.readline().rstrip().split(' '))
lst = list(map(int, stdin.readline().rstrip().split()))
s = stdin.readline()
ss = 0
s += ' '
num = []
sss = 0
i = 0
while i < n:
	cnt = 0
	sss = 0
	if s[i] == s[i + 1]:
		for j in range(i, n):
			num.append(lst[j])
			cnt += 1
			sss += lst[j]
			if s[j] != s[j + 1]:
				break
		i += cnt
		if cnt <= k:
			ss += sss
		else:
			num.sort(reverse=True)
			for j in range(k):
				ss += num[j]
		num = []
	else:
		ss += lst[i]
		i += 1
print(ss)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = input('')
b = []
c = []
su = 0
for i in range(n):
	if len(b) == 0:
		b.append(s[i])
		c.append(a[i])
	elif b[-1] == s[i]:
		b.append(s[i])
		c.append(a[i])
	elif b[-1] != s[i]:
		c.sort(reverse=True)
		for j in range(min(k, len(c))):
			su = su + c[j]
		b = []
		c = []
		b.append(s[i])
		c.append(a[i])
c.sort(reverse=True)
for j in range(min(k, len(c))):
	su = su + c[j]
print(su)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = input()
r = 0
i = 0
dup_count = 0
while i < n:
	j = i + 1
	while j < n and s[i] == s[j]:
		j += 1
	if j - i <= k:
		r += sum(a[i:j])
	else:
		r += sum(sorted(a[i:j], reverse=True)[:k])
	i = j
print(r)


from itertools import groupby
(lens, k) = [int(x) for x in input().split()]
vals = [int(x) for x in input().split()]
ins = input()
insg = [len(list(g)) for (k, g) in groupby(ins)]
i = 0
ans = 0
for x in insg:
	ans += sum(sorted(vals[i:i + x], reverse=True)[:k])
	i += x
print(ans)


def STR():
	return list(input())

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def MAP2():
	return map(float, input().split())

def LIST():
	return list(map(int, input().split()))

def STRING():
	return input()
import string
import sys
from heapq import heappop, heappush
from bisect import *
from collections import deque, Counter, defaultdict
from math import *
from itertools import permutations, accumulate
dx = [-1, 1, 0, 0]
dy = [0, 0, 1, -1]
(n, k) = MAP()
l = LIST()
s = STR()
if len(set(s)) == 1:
	if k == len(s):
		print(sum(l))
	else:
		l.sort(reverse=True)
		sm = 0
		for i in range(k):
			sm += l[i]
		print(sm)
else:
	ans = 0
	i = 0
	while i < n:
		v = [l[i]]
		mins = [l[i]]
		x = s[i]
		j = i + 1
		while j < n and s[j] == x:
			v.append(v[-1] + l[j])
			mins.append(l[j])
			j += 1
		mins.sort()
		if len(v) <= k:
			ans += v[-1]
		else:
			t = v[-1]
			r = min(len(v), k)
			r2 = len(v) - r
			r3 = sum(mins[:r2])
			t -= r3
			ans += t
		i = j
	print(ans)


(n, k) = map(int, input().split())
A = list(map(int, input().split()))
s = input()
sf = 0
tot = 0
while sf < n:
	c = s[sf]
	t = sf + 1
	while t < n and s[t] == c:
		t += 1
	sl = sorted(A[sf:t], reverse=True)
	if t - sf > k:
		tot += sum(sl[:k])
	else:
		tot += sum(sl)
	sf = t
print(tot)


(n, k) = map(int, input().split())
A = [int(x) for x in input().split()] + [0]
B = input() + '#'
result = 0
last = -1
count = 0
C = []
for i in range(len(B)):
	if last == B[i]:
		C += [A[i]]
	else:
		C.sort()
		result += sum(C[max([0, len(C) - k]):])
		last = B[i]
		C = [A[i]]
print(result)


(a, b) = map(int, input().split())
z = list(map(int, input().split()))
s = input()
ans = []
count = 1
tam = 1
for i in range(1, len(z)):
	if s[i] == s[i - 1]:
		tam += 1
	else:
		ans.append(tam)
		tam = 1
ans.append(tam)
ta = []
j = 0
for i in range(len(ans)):
	ta.append(sorted(z[j:j + ans[i]], reverse=True))
	j = ans[i] + j
total = 0
for i in range(len(ta)):
	r = ta[i]
	total += sum(r[0:min(len(r), b)])
print(total)


(n, k) = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
str = input()
c = str[0]
count = 0
master_list = []
while count < len(str):
	list = []
	alpha = c
	while count < len(str) and str[count] == alpha:
		list.append(a[count])
		count = count + 1
	if count < len(str):
		c = str[count]
	master_list.append(list)
count = 0
total = 0
for elemnents in master_list:
	if len(elemnents) > k:
		elemnents.sort(reverse=True)
		master_list[count] = [elemnents[x] for x in range(k)]
	total = total + sum(master_list[count])
	count = count + 1
print(total)


import itertools
(n, k) = (int(s) for s in input().split())
a = [int(s) for s in input().split()]
s = input()
dmg = 0
for (_, g) in itertools.groupby(enumerate(s), key=lambda t: t[1]):
	dmgs = list(sorted((a[i] for (i, _) in g)))
	dmg += sum(dmgs[-k:])
print(dmg)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = input()
suma = 0
arr = [a[0]]
for i in range(1, n):
	if s[i - 1] == s[i]:
		arr.append(a[i])
	else:
		arr.sort(reverse=True)
		suma += sum(arr[:k])
		arr = [a[i]]
arr.sort(reverse=True)
suma += sum(arr[:k])
print(suma)


(lambda N, K, n, s: (lambda c, t: [[[s[i] != s[i - 1] and [c.__setitem__(0, c[0] + sum(sorted(t)[-K:])), t.clear()], t.append(n[i])] for i in range(1, N)], print(c[0] + sum(sorted(t)[-K:]))])([0], [n[0]]))(*map(int, input().split()), list(map(int, input().split())), input())


from itertools import groupby
(n, k) = map(int, input().split())
dmg = map(int, input().split())
seq = input()
pairs = list(zip(seq, dmg))
ret = 0
for subseq in groupby(pairs, lambda x: x[0]):
	(_, subseq) = subseq
	subseq = [i[1] for i in subseq]
	subseq.sort()
	start = max(len(subseq) - k, 0)
	ret += sum(subseq[start:])
print(ret)


(n, k) = map(int, input().split())
a = [int(x) for x in input().split()]
s = input()
prev = s[0]
dmg = 0
l = [a[0]]
for i in range(1, n):
	if s[i] != prev:
		l.sort(reverse=True)
		dmg += sum(l[:k])
		prev = s[i]
		l = [a[i]]
	else:
		l.append(a[i])
l.sort(reverse=True)
dmg += sum(l[:k])
print(dmg)


import sys
from atexit import register
from io import FileIO, StringIO
sys.stdin = StringIO(FileIO(0).read().decode())
input = lambda : sys.stdin.readline().rstrip('\r\n')
sys.stdout = StringIO()
register(lambda : FileIO(1, 'w').write(sys.stdout.getvalue().encode()))

def main():
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	s = list(input())
	(res, cnt) = (0, 1)
	parts = []
	prev = s[0]
	for i in range(1, n):
		if s[i] == prev:
			cnt += 1
		else:
			parts.append(cnt)
			cnt = 1
		prev = s[i]
	parts.append(cnt)
	i = 0
	for part in parts:
		segment = []
		for j in range(i, i + part):
			segment.append(a[j])
		res += sum(sorted(segment, reverse=True)[:k])
		i += part
	print(res)
main()


(n, k) = map(int, input().split())
a = [int(s) for s in input().split()]
s = input()
pt = 0
dmg = 0
while pt < n:
	c = s[pt]
	l = pt
	r = pt + 1
	while r < n and s[r] == c:
		r += 1
	dmgs = a[l:r]
	dmg += sum(sorted(dmgs, reverse=True)[0:k])
	pt = r
print(dmg)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = input().strip()
(l, r, dmg) = (0, 0, 0)
while r != n:
	if r == n - 1:
		h = sorted(a[l:r + 1])
		if len(h) > k:
			dmg += sum(h[-k:])
		else:
			dmg += sum(h)
		(l, r) = (r + 1, r + 1)
	elif s[r] != s[r + 1]:
		h = sorted(a[l:r + 1])
		if len(h) > k:
			dmg += sum(h[-k:])
		else:
			dmg += sum(h)
		(l, r) = (r + 1, r + 1)
	elif s[r] == s[r + 1]:
		r += 1
print(dmg)


from heapq import heappush, heappop
t = 1
for test in range(t):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	s = input()
	heap = []
	ans = 0
	prev = s[0]
	heappush(heap, -a[0])
	for i in range(1, n):
		if s[i] == prev:
			heappush(heap, -a[i])
		else:
			cntr = 0
			while heap and cntr < k:
				ans += -heappop(heap)
				cntr += 1
			heap = []
			heappush(heap, -a[i])
		prev = s[i]
	cntr = 0
	while heap and cntr < k:
		ans += -heappop(heap)
		cntr += 1
	print(ans)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = input()
t = [[0]]
for i in range(1, n):
	if s[i] == s[i - 1]:
		t[-1].append(i)
	else:
		t.append([i])
ans = 0
for seg in t:
	seg.sort(key=lambda x: a[x], reverse=True)
	for i in range(min(k, len(seg))):
		ans += a[seg[i]]
print(ans)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = input()
ans = 0
temp = [a[0]]
for i in range(1, n):
	if s[i] != s[i - 1]:
		temp.sort()
		temp.reverse()
		for j in range(0, min(k, len(temp))):
			ans += temp[j]
		temp = []
	temp.append(a[i])
temp.sort()
temp.reverse()
for i in range(0, min(k, len(temp))):
	ans += temp[i]
print(ans)


(n, k) = map(int, input().split())
d = list(map(int, input().split()))
o = list(input())
p = 0
r = 1
result = 0
while p < n:
	if p + 1 < n and o[p] == o[p + 1]:
		r += 1
		p += 1
	else:
		start = p - r + 1
		end = p
		h = sorted(d[start:end + 1], reverse=True)
		if r > k:
			h = sorted(d[start:end + 1], reverse=True)
			result += sum(h[:k])
		else:
			h = d[start:end + 1]
			result += sum(h)
		p += 1
		r = 1
print(result)


def binNumber(n, size):
	return bin(n)[2:].zfill(size)

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)
(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = str(input())
start = 0
end = 0
su = 0
for i in range(0, n):
	b = []
	if i + 1 < n:
		if s[i] == s[i + 1]:
			end = i + 1
		else:
			b = a[start:end + 1]
			b.sort()
			b = b[::-1]
			if end - start + 1 > k:
				b = b[0:k]
			else:
				b = b[0:end - start + 1]
			su += sum(b)
			start = i + 1
			end = i + 1
	else:
		b = a[start:end + 1]
		b.sort()
		b = b[::-1]
		if end - start + 1 > k:
			b = b[0:k]
		else:
			b = b[0:end - start + 1]
		su += sum(b)
print(su)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
combat = input()
tmp = [a[0]]
curL = combat[0]
dmg = 0
for i in range(1, n):
	if combat[i] == curL:
		tmp.append(a[i])
	else:
		curL = combat[i]
		if len(tmp) > k:
			tmp.sort()
			dmg += sum(tmp[len(tmp) - k:])
		else:
			dmg += sum(tmp)
		tmp.clear()
		tmp.append(a[i])
if len(tmp) > k:
	tmp.sort()
	dmg += sum(tmp[len(tmp) - k:])
else:
	dmg += sum(tmp)
print(dmg)


(n, k) = input().split()
n = int(n)
k = int(k)
dmg = list(map(int, input().split()))
s = input()
res = 0
tmp = [dmg[0]]
for i in range(1, n):
	if s[i] == s[i - 1]:
		tmp.append(dmg[i])
	else:
		tmp = list(reversed(sorted(tmp)))
		res += sum(tmp[:k])
		tmp = [dmg[i]]
tmp = list(reversed(sorted(tmp)))
res += sum(tmp[:k])
print(res)


import sys, string, heapq
(n, k) = map(int, sys.stdin.readline().split())
arr = list(map(int, sys.stdin.readline().split()))
order = list(sys.stdin.readline().rstrip())
ans = 0
for i in string.ascii_lowercase:
	idx = 0
	while idx < n:
		if i == order[idx]:
			q = []
			j = idx
			while j < n:
				if order[j] != order[idx]:
					break
				heapq.heappush(q, -1 * arr[j])
				j += 1
			times = min(k, j - idx)
			while times:
				if q:
					ans += -1 * heapq.heappop(q)
				else:
					break
				times -= 1
			idx = j
		else:
			idx += 1
print(ans)


l = [int(x) for x in input().split()]
m = [int(x) for x in input().split()]
s = input()
n = []
p = []
c = 1
cu = 0
p.append(0)
su = 0
for i in range(1, len(s)):
	if s[i] == s[i - 1]:
		c = c + 1
	else:
		p.append(i)
		n.append(c)
		c = 1
n.append(c)
p.append(len(s) + 1)
for j in range(len(n)):
	if n[j] <= l[1]:
		su = su + sum(m[p[j]:p[j] + n[j]])
	else:
		d = sorted(m[p[j]:p[j] + n[j]], reverse=True)
		su = su + sum(d[0:l[1]])
print(su)


ll = lambda : map(int, input().split())
t = lambda : int(input())
ss = lambda : input()
lx = lambda x: map(int, input().split(x))
yy = lambda : print('YES')
nn = lambda : print('NO')
from math import log10, log2, ceil, factorial as fac, gcd, inf, sqrt, log
from bisect import bisect_right as br, bisect_left as bl
from collections import Counter
mod = 10 ** 9 + 7

def f():
	(n, m) = ll()
	a = list(ll())
	s = ss()
	su = 0
	i = 0
	while i < n:
		temp = [a[i]]
		for j in range(i, n - 1):
			if s[j + 1] == s[j]:
				temp.append(a[j + 1])
			else:
				i = j + 1
				break
		else:
			i = n
		temp.sort(reverse=1)
		su += sum(temp[:m])
	print(su)
f()


(n, k) = map(int, input().split())
l = list(map(int, input().split()))
slist = list(input())
dmg = 0
i = 0
while i < n:
	j = i + 1
	count = 1
	l_tmp = [l[i]]
	while j < n and slist[i] == slist[j]:
		count += 1
		l_tmp.append(l[j])
		j += 1
	l_tmp.sort(reverse=True)
	dmg += sum(l_tmp[:min(count, k)])
	i = j
print(dmg)


import heapq
nks = input().split()
n = int(nks[0])
k = int(nks[1])
c = list(map(int, input().split()))
s = input()
t_cost = 0
last_ch = s[0]
ch_len = 0
ch_l = []
i = 0
while i <= n:
	if i < n and s[i] == last_ch:
		ch_len += 1
		heapq.heappush(ch_l, c[i])
	else:
		t_cost += sum(ch_l)
		while ch_len > k:
			t_cost -= heapq.heappop(ch_l)
			ch_len -= 1
		if i < n:
			last_ch = s[i]
			ch_l = [c[i]]
			ch_len = 1
	i += 1
print(t_cost)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = input()
i = 0
res = 0
while i <= n - 1:
	j = i + 1
	while j <= n - 1 and s[j] == s[i]:
		j += 1
	res += sum(sorted(a[i:j], reverse=True)[:k])
	i = j
print(res)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = list(input()) + ['*']
d = []
c = 1
for i in range(1, n + 1):
	if s[i] == s[i - 1]:
		c += 1
	else:
		d.append(c)
		c = 1
l = len(d)
ne = [0] * (l + 1)
ne[0] = d[0]
for i in range(1, l):
	ne[i] = ne[i - 1] + d[i]
ne[l] = 0
ans = 0
for i in range(l):
	p = a[ne[i - 1]:ne[i]]
	p.sort()
	le = len(p)
	ans += sum(p[max(0, le - k):])
print(ans)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = input()
dic = [[a[0]]]
last = s[0]
last_i = 0
for i in range(1, n):
	if s[i] != last:
		dic.append([a[i]])
		last = s[i]
		last_i = i
	else:
		dic[-1].append(a[i])
for i in range(len(dic)):
	dic[i] = sorted(dic[i], reverse=True)
res = 0
for i in dic:
	for j in range(min(k, len(i))):
		res += i[j]
print(res)


(n, k) = [int(i) for i in input().strip().split(' ')]
a = [int(i) for i in input().strip().split(' ')]
s = input().strip()
res = 0
j = 0
for i in range(1, n):
	(ai, si) = (a[i], s[i])
	if si != s[i - 1]:
		res += sum(sorted(a[j:i])[-k:])
		j = i
res += sum(sorted(a[j:])[-k:])
print(res)


def f(a, k):
	curr = a[0][0]
	temp = [a[0][1]]
	ans = 0
	for i in a[1:]:
		if i[0] == curr:
			temp.append(i[1])
		else:
			temp = sorted(temp, reverse=True)
			ans += sum(temp[:k])
			temp = [i[1]]
			curr = i[0]
	if temp:
		temp = sorted(temp, reverse=True)
		ans += sum(temp[:k])
	return ans
(n, k) = map(int, input().strip().split())
a = list(map(int, input().strip().split()))
s = list(input().strip())
a = list(zip(s, a))
print(f(a, k))


(n, m) = map(int, input().split())
ar = list(map(int, input().split()))
s = input()
kek = [0]
last = 'A'
res = 0
for x in range(n):
	if s[x] == last:
		kek.append(ar[x])
	if s[x] != last:
		if len(kek) > m:
			kek.sort(reverse=True)
			kek = kek[:m]
		res += sum(kek)
		kek = [ar[x]]
		last = s[x]
	if x == n - 1:
		if len(kek) > m:
			kek.sort(reverse=True)
			kek = kek[:m]
		res += sum(kek)
		kek = [ar[x]]
		last = s[x]
print(res)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
S = input()
i = res = 0
while i < len(S):
	j = i
	while i + 1 < len(S) and S[i + 1] == S[i]:
		i += 1
	res += sum(sorted(a[j:i + 1])[-k:])
	i += 1
print(res)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = input()
sp = 0
ep = 0
temp = s[0]
result = 0
happened = False
for i in range(1, n):
	if s[i] == temp:
		ep += 1
	else:
		temp = s[i]
		flag = a[sp:ep + 1]
		flag.sort(reverse=True)
		if ep - sp + 1 > k:
			result += sum(flag[:k])
			sp = i
			ep = i
		else:
			result += sum(flag)
			sp = i
			ep = i
	if i == n - 1:
		temp = a[sp:ep + 1]
		temp.sort(reverse=True)
		result += sum(temp[:k])
if n == 1:
	print(a[0])
else:
	print(result)


def main():
	(n, k) = [int(x) for x in input().split()]
	a = [int(x) for x in input().split()]
	s = input().strip()
	last = s[0]
	regs = []
	rsf = [a[0]]
	for i in range(1, len(s)):
		if s[i] != last:
			regs.append(rsf)
			rsf = []
		rsf.append(a[i])
		last = s[i]
	regs.append(rsf)
	S = 0
	for reg in regs:
		if len(reg) > k:
			reg = sorted(reg, reverse=True)[:k]
		S += sum(reg)
	print(S)
main()


(n, k) = map(int, input().split())
A = list(map(int, input().split()))
if n == 1:
	print(A[0])
else:
	s = input()
	hop = dict()
	hop[0] = [A[0]]
	cnt = 0
	if s[0] == s[1]:
		q = 0
	else:
		q = 1
	fl = 1
	for t in range(1, n):
		if s[t] != s[t - 1]:
			q += 1
			hop[q] = [A[t]]
		else:
			hop[q].append(A[t])
	for y in hop:
		z = hop[y]
		z = sorted(z, reverse=True)
		if len(z) > k:
			cnt += sum(z[:k])
		else:
			cnt += sum(z)
	print(cnt)


from sys import stdin, stdout
from math import *

def main():
	(n, k) = [int(x) for x in stdin.readline().split()]
	a = [int(x) for x in stdin.readline().split()]
	s = stdin.readline() + ' '
	a.append(0)
	f = [a[0]]
	res = 0
	for i in range(1, n + 1):
		if s[i - 1] != s[i]:
			f.sort(reverse=True)
			res += sum(f[:min(len(f), k)])
			f.clear()
		f.append(a[i])
	stdout.write(str(res))
	return 0
main()


(n, k) = map(int, input().strip().split())
a = list(map(int, input().strip().split()))
s = input()
changes = [0]
for idx in range(1, len(s)):
	if s[idx] != s[idx - 1]:
		changes.append(idx)
changes.append(len(s))

def find_max_hits(a, k):
	sorted_a = sorted(a, reverse=True)
	return sum(sorted_a[:k])
sum_hits = 0
for (start, end) in zip(changes[:-1], changes[1:]):
	curr_streak = s[start:end]
	curr_hits = a[start:end]
	if len(curr_streak) <= k:
		sum_hits += sum(curr_hits)
	else:
		sum_hits += find_max_hits(curr_hits, k)
print(sum_hits)


(n, k) = map(int, input().split())
a = []
a = input().split()
a = [int(i) for i in a]
s = input()
(l, r) = (0, 0)
ans = 0
for i in range(1, len(s)):
	if s[i] == s[i - 1]:
		r += 1
	else:
		b = []
		b = a[l:r + 1]
		b.sort()
		b = b[::-1]
		for j in range(min(k, len(b))):
			ans += b[j]
		l = i
		r = i
if s[len(s) - 1] == s[len(s) - 2]:
	b = []
	b = a[l:r + 1]
	b.sort()
	b = b[::-1]
	for j in range(min(k, len(b))):
		ans += b[j]
else:
	ans += a[len(s) - 1]
print(ans)


import re, math, decimal, bisect, collections

def read():
	return input().strip()

def iread():
	return int(input().strip())

def viread():
	return [int(_) for _ in input().strip().split()]
(n, k) = viread()
d = viread()
s = list(read())
processed = 0
damage = 0
while processed < n:
	damages = []
	c = s[processed]
	damages.append(d[processed])
	processed += 1
	while processed < n and s[processed] == c:
		damages.append(d[processed])
		processed += 1
	if len(damages) > k:
		damages.sort()
		damage += sum(damages[len(damages) - k:])
	else:
		damage += sum(damages)
print(damage)


import sys
from heapq import heappush, heappop

def get_array():
	return list(map(int, sys.stdin.readline().split()))

def get_ints():
	return map(int, sys.stdin.readline().split())

def input():
	return sys.stdin.readline().strip('\n')
(n, k) = get_ints()
a = get_array()
s = list(input())
heap = []
ans = i = 0
flag = False
while i < n:
	heappush(heap, -a[i])
	j = i + 1
	while j < n and s[i] == s[j]:
		heappush(heap, -a[j])
		j += 1
	count = 0
	while len(heap) != 0:
		count += 1
		item = -heappop(heap)
		if count > k:
			pass
		else:
			ans += item
	i = j
print(ans)


from heapq import *
(n, k) = map(int, input().split())
dmg = list(map(int, input().split()))
arr = input()
hp = []
tot = 0
for i in range(n):
	if len(hp) == 0:
		heappush(hp, dmg[i])
	elif arr[i] != arr[i - 1]:
		for j in hp:
			tot = tot + j
		hp = []
		heappush(hp, dmg[i])
	else:
		heappush(hp, dmg[i])
		if len(hp) > k:
			heappop(hp)
for i in hp:
	tot = tot + i
print(tot)


(n, k) = [*map(int, input().split())]
a = [*map(int, input().split())]
s = input()
b = []
ans = []
count = 0
su = 0
c = []
for i in range(0, n - 1):
	if s[i] == s[i + 1]:
		if i == n - 2:
			count += 1
			b.append(i)
			b.append(i + 1)
			ans.append(b)
		else:
			b.append(i)
	else:
		b.append(i)
		ans.append(b)
		b = []
if count != 1:
	b.append(n - 1)
	ans.append(b)
for i in ans:
	if len(i) <= k:
		for j in i:
			su += a[j]
	else:
		for j in i:
			c.append(a[j])
		c.sort()
		c = c[::-1]
		for l in range(0, k):
			su += c[l]
		c = []
print(su)


(n, k) = list(map(int, input().split()))
a = list(map(int, input().split()))
s = input()
sum = 0
ans = 0
i = 0
while i < n:
	r = i
	while r < n and s[i] == s[r]:
		r += 1
	e = min(k, r - i)
	a[i:r] = sorted(a[i:r])
	j = r - 1
	while e > 0:
		sum += a[j]
		j -= 1
		e -= 1
	ans += sum
	sum = 0
	i = r
print(ans)


import sys
import math
import time
(n, k) = [int(j) for j in input().split()]
a = [int(j) for j in input().split()]
h = input()
ld = []
last_l = ''
ans = 0
for (n, v) in enumerate(a):
	if h[n] != last_l:
		ld.append([])
		last_l = h[n]
	ld[-1].append(v)
for l in ld:
	l = sorted(l)
	if len(l) <= k:
		ans += sum(l)
	else:
		ans += sum(l[-k:])
print(ans)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = input()
o = 1
pr = s[0]
n = 0
l = []
ot = 0
while o <= len(s):
	n = o - 1
	while o < len(s) and str(s[o]) == str(pr):
		pr = s[o]
		o += 1
	l = sorted(a[n:o])
	ot += sum(l[max(0, len(l) - k):len(l)])
	if o != len(s):
		pr = s[o]
	o += 1
print(ot)


from sys import stdin
read = stdin.readline
(n, k) = map(int, read().split())
A = list(map(int, read().split()))
s = read()
pc = pi = 0
tot = 0
for (i, c) in enumerate(s):
	if c != pc:
		if i - pi > k:
			tot += sum(sorted(A[pi:i])[-k:])
		else:
			tot += sum(A[pi:i])
		(pi, pc) = (i, c)
print(tot)


(n, k) = map(int, input().split())
l = list(map(int, input().split()))
s = str(input())
t = 0
i = 0
while i < n:
	j = i
	p = []
	while j < n and s[i] == s[j]:
		p.append(l[j])
		j += 1
	p.sort(reverse=True)
	c = min(k, len(p))
	t = t + sum(p[0:c])
	i = j
print(t)


class Heap:

	def __init__(self, arr, key=None, heap_type='min'):
		self.arr = arr
		self.key = key
		self.heap_type = heap_type
		if key == None:
			self.key = lambda x: x
		self.heapify()

	def __repr__(self):
		return str(self.arr)

	def __compare(self, index1, index2):
		return self.__compare_keys(self[index1], self[index2])

	def __compare_keys(self, key1, key2):
		ans = 0
		if key1 > key2:
			ans = 1
		elif key1 < key2:
			ans = -1
		else:
			ans = 0
		return ans if self.heap_type == 'min' else -1 * ans

	def heapify(self):
		for i in range(0, len(self)):
			self.__bubble_up(i)

	def insert(self, item):
		self.arr.append(item)
		self.__bubble_up(len(self) - 1)

	def __getitem__(self, index):
		return self.key(self.arr[index])

	def __bubble_up(self, index):
		parent = (index - 1) // 2
		while parent >= 0 and self.__compare(index, parent) == -1:
			(self.arr[parent], self.arr[index]) = (self.arr[index], self.arr[parent])
			(index, parent) = (parent, (parent - 1) // 2)

	def __bubble_down(self, index):
		high = len(self)
		while True:
			(l, r) = (index * 2 + 1, index * 2 + 2)
			if r < high:
				if self.__compare(r, l) == -1:
					if self.__compare(r, index) >= 0:
						break
					(self.arr[r], self.arr[index]) = (self.arr[index], self.arr[r])
					index = r
				else:
					if self.__compare(l, index) >= 0:
						break
					(self.arr[l], self.arr[index]) = (self.arr[index], self.arr[l])
					index = l
			elif l < high:
				if self.__compare(l, index) >= 0:
					break
				(self.arr[l], self.arr[index]) = (self.arr[index], self.arr[l])
				break
			else:
				break

	def pop(self):
		(self.arr[0], self.arr[-1]) = (self.arr[-1], self.arr[0])
		top = self.arr.pop(-1)
		self.__bubble_down(0)
		return top

	def __len__(self):
		return len(self.arr)
(n, k) = map(int, input().split())
arr = list(map(int, input().split()))
moves = input()
last = moves[0]
heap = Heap([arr[0]], heap_type='max')
damage = 0
for i in range(1, n):
	if moves[i] == last:
		heap.insert(arr[i])
	else:
		cnt = min(len(heap), k)
		while cnt != 0:
			damage += heap.pop()
			cnt -= 1
		heap = Heap([arr[i]], heap_type='max')
		last = moves[i]
cnt = min(len(heap), k)
while cnt != 0:
	damage += heap.pop()
	cnt -= 1
print(damage)


(n, k) = map(int, input().strip().split())
shots = list(map(int, input().strip().split()))
s = list(input().strip())

def choose(l, r):
	tmp = sorted(shots[l:r])
	return sum(tmp[0:r - l - k])
summa = sum(shots)
current_i = ''
current_num = 0
for i in range(n):
	if s[i] == current_i:
		current_num += 1
	else:
		if current_num > k:
			summa -= choose(i - current_num, i)
		current_i = s[i]
		current_num = 1
if current_num > k:
	summa -= choose(n - current_num, n)
print(summa)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
s = str(input())
k = 0
ans = 0
i = 0
while i < n:
	k = 0
	if i == n - 1:
		ans += a[i]
		break
	if s[i] == s[i + 1]:
		ls1 = []
		count = 0
		k += 1
		ls1.append(a[i])
		count += a[i]
		for j in range(i + 1, n):
			if s[j] == s[j - 1]:
				k += 1
				ls1.append(a[j])
				count += a[j]
			else:
				break
		if k <= m:
			ans += count
		else:
			ls1.sort()
			l = 0
			for x in reversed(ls1):
				ans += x
				l += 1
				if l == m:
					break
		i += k
	else:
		ans += a[i]
		i += 1
print(ans)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = input()
c = 1
d = []
x = s[0]
for i in range(1, len(s)):
	if x == s[i]:
		c = c + 1
	else:
		if c > k:
			t = a[i - c:i]
			t.sort()
			d += t[:c - k]
		c = 1
		x = s[i]
if c > k:
	t = a[i - c + 1:i + 1]
	t.sort()
	d += t[:c - k]
print(sum(a) - sum(d))


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = input()
ans = 0
i = 0
while i < n:
	j = i
	b = []
	while j < n and s[i] == s[j]:
		b.append(a[j])
		j += 1
	b.sort(reverse=True)
	ans += sum(b[0:min(len(b), k)])
	i = j
print(ans)


(n, k) = map(int, input().split())
s = [[0, 0] for i in range(n)]
a = list(map(int, input().split()))
s = input()
dmg = 0
start = 0
end = 0
while end < n:
	while end < n and s[end] == s[start]:
		end += 1
	if end - start <= k:
		dmg += sum(a[start:end])
	else:
		dmg += sum(sorted(a[start:end], reverse=True)[:k])
	start = end
print(dmg)


(n, k) = map(int, input().split())
a = [int(i) for i in input().split()]
s = list(input())
i = 0
b = []
cnt = 0
if n == 1:
	print(a[0])
else:
	while i != n - 1:
		b.append((s[i], a[i]))
		if s[i] != s[i + 1]:
			b.sort(reverse=True)
			j = 0
			while j < len(b) and j < k:
				cnt += b[j][1]
				j += 1
			b = []
		i += 1
	b.append((s[-1], a[-1]))
	b.sort(reverse=True)
	j = 0
	while j < len(b) and j < k:
		cnt += b[j][1]
		j += 1
	print(cnt)


from operator import itemgetter
(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = input()
ar = []
for i in range(n):
	ar.append([ord(s[i]) - 96, a[i]])
prev = 0
su = 0
co = 1
i = 0
while i < n:
	li = []
	su2 = ar[i][1]
	li.append(ar[i])
	j = i
	if j < n - 1 and ar[i][0] == ar[j][0]:
		while j < n:
			if j + 1 < n and ar[j][0] == ar[j + 1][0]:
				li.append(ar[j + 1])
				su2 += ar[j + 1][1]
				j += 1
			else:
				break
	if len(li) <= k:
		su += su2
	else:
		li = sorted(li, key=itemgetter(1), reverse=True)
		for ko in range(k):
			su += li[ko][1]
	i = j + 1
print(su)


(n, b) = input().split()
n = int(n)
b = int(b)
j = 0
lt = []
tempLt = []
lt = list(map(int, input().split()))
str = input()
for i in range(len(str)):
	if i == 0:
		tempLt.append(lt[i])
		j = 0
	elif str[i] == str[i - 1]:
		tempLt.append(lt[i])
	else:
		tempLt.sort()
		tempLt.reverse()
		for k in range(j, i):
			lt[k] = tempLt[k - j]
		del tempLt
		tempLt = []
		5
		tempLt.append(lt[i])
		j = i
tempLt.sort()
tempLt.reverse()
for k in range(j, i + 1):
	lt[k] = tempLt[k - j]
dmg = lt[0]
counter = 1
for i in range(1, len(str)):
	if str[i] == str[i - 1]:
		if counter < b:
			counter += 1
			dmg += lt[i]
	else:
		counter = 1
		dmg += lt[i]
print(dmg)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = input()
c = []
l = []
if len(a) == 1:
	print(a[0])
	exit()
for i in range(len(s) - 1):
	if s[i] == s[i + 1]:
		l.append(a[i])
	else:
		l.append(a[i])
		c.append(l)
		l = []
if s[-1] == s[-2]:
	if len(l) != 0:
		c.append(l)
	c[-1].append(a[-1])
else:
	if len(l) != 0:
		c.append(l)
	c.append([a[-1]])
for i in range(len(c)):
	c[i] = list(reversed(sorted(c[i])))
l = 0
for i in range(len(c)):
	l += sum(c[i][:k])
print(l)


import sys
input = sys.stdin.readline
(n, k) = map(int, input().split())
A = list(map(int, input().split()))
S = input()
LIST = [0]
for i in range(1, n):
	if S[i] != S[i - 1]:
		LIST.append(i)
LIST.append(n)
ANS = 0
for j in range(len(LIST) - 1):
	if LIST[j + 1] - LIST[j] <= k:
		ANS += sum(A[LIST[j]:LIST[j + 1]])
	else:
		ANS += sum(sorted(A[LIST[j]:LIST[j + 1]], reverse=True)[:k])
print(ANS)


(n, k) = map(int, input().split())
ar = list(map(int, input().split()))
arr = []
s = input()
ans = 0
v = []
i = 0
j = 0
while j < n:
	while j < n and s[i] == s[j]:
		v.append(ar[j])
		j += 1
	if j - i + 1 <= k:
		ans += sum(v)
		i = j
		v = []
	else:
		ans += sum(sorted(v)[-k:])
		i = j
		v = []
if len(v) <= k:
	ans += sum(v)
else:
	ans += sum(sorted(v)[-k:])
print(ans)


t = list(map(int, input().split(' ')))
n = t[0]
k = t[1]
a = list(map(int, input().split(' ')))
s = list(input())
c = 0
if n == 1:
	c = a[0]
t = []
t.append(a[0])
for i in range(1, n):
	if s[i] == s[i - 1]:
		if i == n - 1:
			t.append(a[i])
			t.sort(reverse=True)
			t = t[:k]
			q = sum(t)
			c = c + q
		else:
			t.append(a[i])
	elif i == n - 1:
		t.sort(reverse=True)
		t = t[:k]
		q = sum(t) + a[i]
		c = c + q
		t = []
	else:
		t.sort(reverse=True)
		t = t[:k]
		q = sum(t)
		c = c + q
		t = []
		t.append(a[i])
print(c)


i = 0
(n, k) = map(int, input().split())
damages = list(map(int, input().split()))
hits = input()
dmg = 0
while i < n:
	j = i + 1
	while j < n and hits[i] == hits[j]:
		j += 1
	dmg += sum(sorted(damages[i:j], reverse=True)[:k])
	i = j
print(dmg)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = input()
(i, j, dmg) = (0, 1, 0)
while j < n:
	if s[j] == s[i]:
		j += 1
	else:
		dmg += sum(sorted(a[i:j])[-k:])
		(i, j) = (j, j + 1)
dmg += sum(sorted(a[i:])[-k:])
print(dmg)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = input()
i = 0
b = []
dmg = 0
c = 0
while i <= n - 1:
	m = s[i]
	while i <= n - 1 and s[i] == m:
		b.append(a[i])
		i += 1
	b = sorted(b)
	for j in range(len(b)):
		if c < k:
			dmg += b[len(b) - 1]
			del b[len(b) - 1]
			c += 1
		else:
			break
	c = 0
	b = []
print(dmg)


import sys
import math
import itertools
import heapq
input = sys.stdin.readline

def main():
	(n, k) = map(int, input().strip().split())
	a = [int(x) for x in input().strip().split()]
	s = input().strip()
	S = list(s)
	ans = 0
	idx = 0
	for (key, grp) in itertools.groupby(s):
		grp = list(grp)
		selected = sorted(a[idx:idx + len(grp)], reverse=True)
		ans += sum(selected[:k])
		idx += len(grp)
	print(ans)
main()


from sys import stdin
(n, k) = map(int, input().split())
l = list(map(int, stdin.readline().split()))
string = stdin.readline().strip()
ans = 0
i = 0
while i < n:
	if i == n - 1 or string[i] != string[i + 1]:
		ans += l[i]
	else:
		cnt = 1
		start = i
		while i < n - 1 and string[i] == string[i + 1]:
			cnt += 1
			i += 1
		if cnt < k:
			ans += sum(l[start:i + 1])
		else:
			ans += sum(sorted(l[start:i + 1], reverse=True)[:k])
	i += 1
print(ans)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
str = input()
ans = i = 0
while i < n:
	j = i
	while i < n - 1 and str[i + 1] == str[i]:
		i += 1
	ans += sum(sorted(a[j:i + 1])[-k:])
	i += 1
print(ans)


def fastio():
	import sys
	from io import StringIO
	from atexit import register
	global input
	sys.stdin = StringIO(sys.stdin.read())
	input = lambda : sys.stdin.readline().rstrip('\r\n')
	sys.stdout = StringIO()
	register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()
MOD = 10 ** 9 + 7
I = lambda : list(map(int, input().split()))
(n, k) = I()
a = I()
s = input()
ans = [[0] for i in range(n)]
i = 1
j = 0
ans[0].append(a[0])
while i < n:
	while i < n and s[i] == s[i - 1]:
		ans[j].append(a[i])
		i += 1
	if i < n and s[i] != s[i - 1]:
		j += 1
		ans[j].append(a[i])
		i += 1
		continue
	j += 1
res = 0
for i in ans:
	i.sort(reverse=True)
	res += sum(i[:min(k, len(i))])
print(res)


(n, k) = [int(x) for x in input().split()]
L = [int(x) for x in input().split()]
s = input()
temp = [1, 0, 0]
R = []
for i in range(n - 1):
	if s[i + 1] == s[i]:
		temp[0] += 1
	else:
		temp[-1] = i
		R.append(temp)
		temp = [1, i + 1, i + 1]
temp[-1] = n - 1
R.append(temp)
Dam = 0
for i in R:
	if i[0] > k:
		q = L[i[1]:i[2] + 1]
		q.sort(reverse=True)
		t = 0
		for i in range(k):
			t += q[i]
		Dam += t
	else:
		Dam += sum(L[i[1]:i[2] + 1])
print(Dam)


(n, k) = map(int, input().split())
A = [int(x) for x in input().split()]
A.append(-1)
s = input() + '$'
dmg = 0
lc = None
count = 0
atk = []
for i in range(n + 1):
	c = s[i]
	a = A[i]
	if c != lc:
		if lc:
			dmg += sum(sorted(atk)[-k:])
		count = 0
		atk = []
	count += 1
	lc = c
	atk.append(a)
print(dmg)


import heapq
(n, k) = map(int, input().split(' '))
a = [int(x) for x in input().split(' ')]
m = list(input())
heap = []
p = -1
s = 0
for i in range(n):
	if p == m[i]:
		heapq.heappush(heap, (m[i], a[i]))
		if len(heap) > k:
			s -= heapq.heappop(heap)[1]
	else:
		heap.clear()
		heapq.heappush(heap, (m[i], a[i]))
	s += a[i]
	p = m[i]
print(s)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
aa = input()
sm = 0
sm1 = a[0]
i = 0
cc = [a[0]]
jj = 0
for j in range(1, n):
	jj = j
	if aa[j] != aa[j - 1]:
		if j - i >= k:
			cc.sort()
			for i in range(1, k + 1):
				sm += cc[-i]
		else:
			sm += sm1
		i = j
		cc = [a[j]]
		sm1 = a[j]
	else:
		sm1 += a[j]
		cc.append(a[j])
else:
	if jj != i:
		if jj - i >= k:
			cc.sort()
			for i in range(1, k + 1):
				sm += cc[-i]
		else:
			sm += sm1
if n > 2:
	if aa[-1] != aa[-2]:
		sm += a[-1]
if n == 1:
	sm = a[0]
print(sm)


(n, maxhits) = map(int, input().split())
(ls, line) = (list(map(int, input().split())), list(input()))
(currbutt, moves, dmg) = ('a', [], 0)
for (i, button) in enumerate(line):
	if button == currbutt:
		moves.append(ls[i])
	else:
		if len(moves) > maxhits:
			moves.sort(reverse=True)
			moves = moves[:maxhits]
		dmg += sum(moves)
		(currbutt, moves) = (button, [ls[i]])
if len(moves) > maxhits:
	moves.sort(reverse=True)
	moves = moves[:maxhits]
print(dmg + sum(moves))


(n, k) = map(int, input().split())
s = list(map(int, input().split()))
a = input()
ans = []
i = 0
kol = 1
j = 0
dmg = 0
l = []
while j < n:
	if a[j] == a[i]:
		j += 1
	else:
		ans.append(s[i:j])
		l.append(j - i)
		kol += 1
		i = j
ans.append(s[i:j])
l.append(j - i)
for i in range(kol):
	st = ans[i]
	st.sort()
	for j in range(max(l[i] - k, 0), l[i]):
		dmg += st[j]
print(dmg)


from itertools import groupby
(n, k) = map(int, input().split())
arr = list(map(int, input().split()))
st = input()
pos = 0
l = 0
ans = 0
for (a, b) in groupby(st):
	l += len(list(b))
	brr = arr[pos:l]
	brr.sort(reverse=True)
	pos = l
	try:
		ans += sum(brr[0:k])
	except:
		ans += sum(brr)
print(ans)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = input()
s = list(s)
cnt = []
c = 1
for i in range(1, n):
	if s[i] == s[i - 1]:
		c += 1
		if i == n - 1:
			cnt.append(c)
	else:
		cnt.append(c)
		c = 1
		if i == n - 1:
			cnt.append(c)
if n == 1:
	cnt = [1]
m = len(cnt)
ans = 0
i = 0
kk = 0
while kk < m and i < n:
	if cnt[kk] <= k:
		ans += sum(a[i:i + cnt[kk]])
		i += cnt[kk]
		kk += 1
	else:
		arr = a[i:i + cnt[kk]]
		arr.sort(reverse=True)
		for j in range(k):
			ans += arr[j]
		i += cnt[kk]
		kk += 1
print(ans)


(a, b) = map(int, input().split())
c = list(map(int, input().split()))
d = input()
(i, s) = (0, 0)
while i < a:
	k = d[i]
	f = []
	while i < a and d[i] == k:
		f += [c[i]]
		i += 1
	s += sum(sorted(f)[-b:])
print(s)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = input()
answer = i = 0
p = 1
while p < n:
	if s[i] == s[p]:
		p += 1
	else:
		part = a[i:p]
		part.sort()
		answer += sum(part[-k:])
		i = p
		p += 1
part = a[i:p]
part.sort()
answer += sum(part[-k:])
print(answer)


(n, k) = input().split(' ')
(n, k) = (int(n), int(k))
m = input().split(' ')
s = str(input())
damages = []
for i in m:
	damages.append(int(i))

def choose_best():
	pass
j = 0
suma = 0
while j < n:
	z = j
	counter = 0
	while z + 1 < n and s[j] == s[z + 1]:
		z += 1
		counter += 1
	if counter >= k:
		new = damages[j:j + counter + 1]
		new.sort(reverse=True)
		for i in range(k):
			suma += new[i]
		j += counter + 1
	else:
		for i in range(counter + 1):
			suma += damages[j]
			j += 1
print(suma)


(n, k) = map(int, input().split())
shots = list(map(int, input().split()))
s = input()
prev = ''
segments = [0]
for i in range(1, len(s)):
	if s[i] != s[i - 1]:
		segments.append(i)
segments.append(len(s))
ans = 0
for i in range(1, len(segments)):
	if segments[i] - segments[i - 1] > k:
		sorted_stuff = sorted(shots[segments[i - 1]:segments[i]], reverse=True)[:k]
		ans += sum(sorted_stuff)
	else:
		ans += sum(shots[segments[i - 1]:segments[i]])
print(ans)


(n, k) = [int(s) for s in input().split(' ')]
num = [int(s) for s in input().split(' ')]
string = input()
prev = None
total = 0
same = []
for i in range(n):
	if string[i] == prev:
		same.append(num[i])
	else:
		total += sum(sorted(same, reverse=True)[:k])
		prev = string[i]
		same = [num[i]]
total += sum(sorted(same, reverse=True)[:k])
print(total)


(n, k) = map(int, input().split())
arr = list(map(int, input().split()))
s = input()
dmg = arr[0]
prs = []
ch = s[0]
beg = 0
for i in range(1, n):
	if s[i] != ch:
		if i - beg > k:
			prs.append((beg, i))
		ch = s[i]
		beg = i
	dmg += arr[i]
if n - beg > k:
	prs.append((beg, n))
for pair in prs:
	_k = pair[1] - pair[0] - k
	notdmg = sum(sorted(arr[pair[0]:pair[1]])[:_k])
	dmg -= notdmg
print(dmg)


import sys
sin = sys.stdin
nk = sin.readline().split()
nk = [int(x) for x in nk]
n = nk[0]
k = nk[1]
damages = sin.readline().split()
damages = [int(x) for x in damages]
hits = list(sin.readline())[:-1]
totalScore = 0
score = 0
prevLetter = ''
istart = 0
icurr = 0

def findBest(lst, k):
	s = sorted(lst)
	return sum(s[len(s) - k:])
while icurr < len(damages):
	if hits[icurr] == prevLetter:
		score += damages[icurr]
	elif icurr - istart > k:
		totalScore += findBest(damages[istart:icurr], k)
		istart = icurr
		score = damages[icurr]
	else:
		totalScore += score
		istart = icurr
		score = damages[icurr]
	prevLetter = hits[icurr]
	icurr += 1
if icurr - istart > k:
	totalScore += findBest(damages[istart:], k)
else:
	totalScore += score
print(totalScore)


(n, k) = map(int, input().split())
dmg = list(map(int, input().split()))
keys = input() + '!'
summary = 0
i = 0
while i < n:
	if keys[i + 1] == keys[i]:
		accu = keys[i]
		start = i
		i += 1
		while keys[i] == accu and i < n:
			i += 1
		mono = sorted(dmg[start:i], reverse=True)
		maykick = min(len(mono), k)
		summary += sum(mono[:maykick])
	else:
		summary += dmg[i]
		i += 1
print(summary)


list_int_input = lambda inp: list(map(int, inp.split()))
int_input = lambda inp: int(inp)
string_to_list_input = lambda inp: list(inp)
nk = list_int_input(input())
a = list_int_input(input())
s = string_to_list_input(input())
(i, j) = (0, 0)
sm = 0
while i < nk[0]:
	while j < nk[0] and s[i] == s[j]:
		j += 1
	if j - i > nk[1]:
		sa = sorted(a[i:j])
		sm += sum(sa[-nk[1]:])
	else:
		sm += sum(a[i:j])
	i = j
print(sm)


(n, k) = [int(t) for t in input().split()]
a = list([int(t) for t in input().split()])
s = input()
dmg = 0
subs = []
for l in range(0, n):
	subs.append(a[l])
	if not (l + 1 < len(s) and s[l] == s[l + 1]):
		subs.sort(reverse=True)
		for i in range(min(k, len(subs))):
			dmg += subs[i]
		subs = []
print(dmg)


import sys
[n1, k1] = sys.stdin.readline().split(' ')
n = int(n1)
k = int(k1)
v = list(map(lambda x: int(x), sys.stdin.readline().split(' ')))
s = sys.stdin.readline()
v = list(zip(v, range(0, len(v)), s))
current_char = s[0]
sequence_start = 0
total_dmg = 0
for i in range(0, len(s)):
	if s[i] != current_char:
		substring = v[sequence_start:i]
		sorted_substring = list(sorted(substring, key=lambda x: x[0], reverse=True))
		if len(sorted_substring) > k:
			sorted_substring = sorted_substring[0:k]
		for x in sorted_substring:
			total_dmg += x[0]
		current_char = s[i]
		sequence_start = i
print(total_dmg)


(n, m) = map(int, input().split())
a = list(map(int, input().split())) + [1]
s = input() + '0'
last = '1'
l = [0]
ans = 0
for i in range(n + 1):
	if last != s[i]:
		l.sort()
		if len(l) < m:
			ans += sum(l)
		else:
			ans += sum(l[len(l) - m:])
		l.clear()
	l.append(a[i])
	last = s[i]
print(ans)


import math
import sys
nk = input().split()
n = int(nk[0])
k = int(nk[1])
nums = list(map(int, input().split()))
s = input()
startIndex = 0
prevChar = ' '
chainCount = 0
result = sum(nums)
for i in range(len(s)):
	if prevChar == s[i]:
		chainCount += 1
	else:
		if chainCount > k:
			tempArr = nums[startIndex:i]
			tempArr.sort()
			for j in range(len(tempArr) - k):
				result -= tempArr[j]
		chainCount = 1
		prevChar = s[i]
		startIndex = i
if chainCount > k:
	tempArr = nums[startIndex:n]
	tempArr.sort()
	for j in range(len(tempArr) - k):
		result -= tempArr[j]
print(result)


import sys
from math import *
from copy import copy
from collections import deque
(n, k) = input().split()
(n, k) = (int(n), int(k))
a = [int(i) for i in input().split()]
s = input()
out = 0
i = 0
while i < len(s):
	l = i
	r = i
	while 1:
		if i == len(s) - 1 or s[i] != s[i + 1]:
			break
		i += 1
		r += 1
	ar = copy(a[l:r + 1])
	ar.sort()
	ar.reverse()
	out += sum(ar[:k])
	i += 1
print(out)


(n, k) = map(int, input().split())
l = [int(i) for i in input().split()]
s = input()
i = 0
ans = 0
import heapq as h
while i < n:
	curr = s[i]
	c = 0
	a = []
	sm = 0
	f = 0
	h.heapify(a)
	while i < n and s[i] == curr:
		c += 1
		if c <= k:
			sm += l[i]
			h.heappush(a, l[i])
		else:
			f = 1
			h.heappush(a, l[i])
			h.heappop(a)
		i += 1
	if c <= k:
		ans += sm
	else:
		while a:
			t = h.heappop(a)
			ans += t
print(ans)


from math import *
from collections import *
import sys
sys.setrecursionlimit(10 ** 9)
(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = input()
i = 0
ans = 0
while i < n:
	ch = s[i]
	j = i
	while i < n and s[i] == ch:
		i += 1
	l = a[j:i]
	l.sort(reverse=True)
	m = min(i - j, k)
	ans += sum(l[:m])
print(ans)


def mergeSort(nlist):
	if len(nlist) > 1:
		mid = len(nlist) // 2
		lefthalf = nlist[:mid]
		righthalf = nlist[mid:]
		mergeSort(lefthalf)
		mergeSort(righthalf)
		i = j = k = 0
		while i < len(lefthalf) and j < len(righthalf):
			if lefthalf[i] < righthalf[j]:
				nlist[k] = lefthalf[i]
				i = i + 1
			else:
				nlist[k] = righthalf[j]
				j = j + 1
			k = k + 1
		while i < len(lefthalf):
			nlist[k] = lefthalf[i]
			i = i + 1
			k = k + 1
		while j < len(righthalf):
			nlist[k] = righthalf[j]
			j = j + 1
			k = k + 1
line1 = input()
l1 = line1.split(' ')
n = int(l1[0])
k = int(l1[1])
attack = input()
att = attack.split(' ')
att = list(map(lambda x: int(x), att))
seq = input()
ss = 0
i = -1
while i < n:
	i += 1
	if i >= n:
		break
	if i == n - 1:
		ss += att[i]
	elif seq[i] != seq[i + 1]:
		ss += att[i]
	else:
		arr = []
		while True:
			if i == n - 1:
				arr.append(att[i])
				break
			elif seq[i] == seq[i + 1]:
				arr.append(att[i])
				i += 1
			else:
				arr.append(att[i])
				break
		mergeSort(arr)
		count = 0
		for g in arr[::-1]:
			if count < k:
				ss += int(g)
				count += 1
	if i >= n:
		break
print(ss)


(n, k) = map(int, input().split())
l = list(map(int, input().split()))
s = input()
ch = ''
c = 0
li = []
for i in range(n):
	if s[i] == ch:
		li.append(l[i])
	else:
		li.sort(reverse=True)
		c += sum(li[:k])
		ch = s[i]
		li = [l[i]]
li.sort(reverse=True)
c += sum(li[:k])
ch = s[i]
li = [l[i]]
print(c)


from sys import stdin
stdin.readline

def mp():
	return list(map(int, stdin.readline().strip().split()))

def it():
	return int(stdin.readline().strip())

def fun(l, k):
	l.sort(reverse=True)
	return sum(l[:k])
(n, k) = mp()
l = mp()
s = input()
if n == 1:
	print(l[0])
else:
	v = []
	ans = 0
	(i, j) = (0, 1)
	while j < n:
		if s[i] != s[j]:
			if v:
				v.append(l[i])
				if len(v) <= k:
					ans += sum(v)
				else:
					ans += fun(v, k)
				v = []
			else:
				ans += l[i]
		else:
			v.append(l[i])
		i += 1
		j += 1
		if j == n:
			if s[-2] == s[-1]:
				v.append(l[-1])
				if len(v) <= k:
					ans += sum(v)
				else:
					ans += fun(v, k)
				v = []
			else:
				ans += l[-1]
	print(ans)


def main():
	(n, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	s = input() + '0'
	ans = sum(arr)
	(prev, cnt) = (s[0], 1)
	for i in range(1, len(s)):
		sym = s[i]
		if sym == prev:
			cnt += 1
		else:
			if cnt > k:
				ans -= sum(sorted(arr[i - cnt:i])[:cnt - k])
			cnt = 1
		prev = sym
	print(ans)
main()


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = input()
num = 0
tmp = [a[0]]
pre = s[0]
for i in range(1, n):
	if pre != s[i]:
		tmp = sorted(tmp, reverse=True)
		num += sum(tmp[:k])
		tmp = [a[i]]
	else:
		tmp.append(a[i])
	pre = s[i]
tmp = sorted(tmp, reverse=True)
num += sum(tmp[:k])
print(num)


(n, k) = map(int, input().split())
string_a = input().split()
a = []
for i in range(n):
	a.append(int(string_a[i]))
s = input()
dmg = 0
i = 0
while i <= len(s) - 1:
	c = s[i]
	j = i + 1
	while j <= len(s) - 1:
		if s[j] == s[i]:
			j += 1
			continue
		else:
			break
	if j - i <= k:
		for m in range(i, j):
			dmg += a[m]
	else:
		temp_a = a[i:j]
		temp_a.sort(reverse=True)
		for m in range(k):
			dmg += temp_a[m]
	i = j
print(dmg)


import copy
import fractions
import itertools
import string
import sys

def powmod(x, p, m):
	if p <= 0:
		return 1
	if p <= 1:
		return x % m
	return powmod(x * x % m, p // 2, m) * (x % m) ** (p % 2) % m

def to_basex(num, x):
	while num > 0:
		yield (num % x)
		num //= x

def from_basex(it, x):
	ret = 0
	p = 1
	for d in it:
		ret += d * p
		p *= x
	return ret

def core():
	(n, k) = [int(x) for x in input().split()]
	a = [int(x) for x in input().split()]
	s = input()
	groups = []
	last_ch = None
	for i in range(len(s)):
		ch = s[i]
		if ch != last_ch:
			groups.append([])
		groups[-1].append(a[i])
		last_ch = ch
	ans = 0
	for g in groups:
		g.sort()
		ans += sum(g[-k:])
	print(ans)
core()


import heapq

def rlist():
	return list(map(int, input().split()))

def rdi():
	return int(input())
(n, k) = rlist()
hp = rlist()
line = input()
last = ''
cnt = 0
dmg = 0
ll = list()
for i in range(n):
	elem = line[i]
	if elem != last:
		ll.append([])
		last = elem
	heapq.heappush(ll[-1], -hp[i])
for heap in ll:
	for i in range(min(len(heap), k)):
		e = -heapq.heappop(heap)
		dmg += e
print(dmg)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = input()
co = 0
pre = 0
b = []
ans = 0
for i in range(n):
	if pre != s[i]:
		pre = s[i]
		kk = min(k, len(b))
		b.sort(reverse=True)
		ans += sum(b[:kk])
		b = []
		b.append(a[i])
	else:
		b.append(a[i])
kk = min(k, len(b))
b.sort(reverse=True)
ans += sum(b[:kk])
print(ans)


(n, k) = list(map(int, input().split()))
a = list(map(int, input().split()))
s = list(input())
i = 0
sth = []
sts = []
ans = 0
while i < n:
	if len(sts) == 0 or sts[-1] == s[i]:
		sts.append(s[i])
		sth.append(a[i])
		i += 1
	else:
		sts = []
		sth.sort(reverse=True)
		ans += sum(sth[:min(k, len(sth))])
		sth = []
sth.sort(reverse=True)
ans += sum(sth[:min(k, len(sth))])
sth = []
print(ans)


(n, k) = map(int, input().split())
l = list(map(int, input().split()))
s = input()
s = list(s)
i = 0
ans = 0
while i < n:
	key = s[i]
	l2 = []
	l3 = []
	j = i
	if j < n - 1 and s[j + 1] == key:
		while j < n and s[j] == key:
			l2.append(l[j])
			j = j + 1
		i = j
		l2 = sorted(l2, reverse=True)
		if len(l2) < k:
			ans = ans + sum(l2)
		else:
			ans = ans + sum(l2[:k])
	else:
		ans = ans + l[i]
		i = i + 1
print(ans)


(n, k) = map(int, input().split())
a = [int(num) for num in input().split()]
s = input()
stack = []
count = 0
maxm = 0
for i in range(n):
	if not stack:
		stack.append(a[i])
		count += 1
	elif s[i] != s[i - 1]:
		stack.sort()
		maxm += sum(stack[-1 * k:])
		stack = [a[i]]
		count = 1
	else:
		count += 1
		stack.append(a[i])
stack.sort()
maxm += sum(stack[-1 * k:])
print(maxm)


(k, n) = map(int, input().split())
hits = list(map(int, input().split()))
hits.append(0)
buttons = input()
buttons += '0'
hits2 = []
key = buttons[0]
temp = [hits[0]]
for i in range(1, k + 1):
	if buttons[i] == key:
		temp.append(hits[i])
	else:
		hits2.append(temp)
		key = buttons[i]
		temp = [hits[i]]
sum = 0
for i in range(len(hits2)):
	hits2[i].sort()
	for j in range(len(hits2[i]) - 1, max(0, len(hits2[i]) - n) - 1, -1):
		sum += hits2[i][j]
print(sum)


(n, k) = map(int, input().split())
damage = list(map(int, input().split()))
seq = list(input())
total = 0
check = [damage[0]]
prev = seq[0]
for i in range(1, n):
	if seq[i] == prev:
		check.append(damage[i])
	else:
		check.sort(reverse=True)
		if len(check) <= k:
			total += sum(check)
		else:
			total += sum(check[0:k])
		check = [damage[i]]
		prev = seq[i]
check.sort(reverse=True)
if len(check) <= k:
	total += sum(check)
else:
	total += sum(check[0:k])
print(total)


(n, k) = map(int, input().split())
cnt = 1
dmg = list(map(int, input().split()))
s = input() + '#'
ans = 0
psymb = s[0]
i = 0
while i < n:
	t = []
	while i < n and s[i] == psymb:
		t.append(dmg[i])
		psymb = s[i]
		i += 1
	ans += sum(sorted(t)[-k:])
	psymb = s[i]
print(ans)


(n, k) = [int(x) for x in input().split()]
num = [int(x) for x in input().split()]
s = list(input())
literal = s[0]
total = []
pref = []
for item in range(n):
	if s[item] == literal:
		pref.append(num[item])
	else:
		total.append(pref)
		pref = [num[item]]
		literal = s[item]
total.append(pref)
brutality = 0
for item in total:
	item.sort(reverse=True)
	for i in range(min(k, len(item))):
		brutality += item[i]
print(brutality)


from math import *
from itertools import *
from collections import *
from functools import reduce
Q = q = quit
inf = float('inf')

def input_ints():
	return list(map(int, input().split()))

def input_floats():
	return list(map(float, input().split()))

def input_strings():
	return input().split()
(n, k) = input_ints()
a = input_ints()
s = input()
l = 0
r = 0
while r < len(s):
	l = r
	c = s[r]
	while r < len(s) and s[r] == c:
		r += 1
	i_remove = list(range(l, r))
	i_remove.sort(key=lambda x: a[x])
	n_remove = max(0, r - l - k)
	for i in i_remove[:n_remove]:
		a[i] = 0
print(sum(a))


def findmax(arr):
	arr.sort()
	c = 0
	ans = 0
	for y in range(len(arr) - 1, -1, -1):
		ans += arr[y]
		c += 1
		if c == k:
			return ans
	return ans
[n, k] = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
s = input()
if n == 1:
	print(a[0])
else:
	start = 0
	total = 0
	for x in range(n - 1):
		if s[x] != s[x + 1]:
			total += findmax(a[start:x + 1])
			start = x + 1
	total += findmax(a[start:x + 2])
	print(total)


(n, k) = map(int, input().split())
lst = list(map(int, input().split()))
s = input()
(result, shet, res, mas) = (0, 1, 0, [lst[0]])
for i in range(1, n):
	if s[i] == s[i - 1]:
		shet += 1
		mas.append(lst[i])
	else:
		if shet > k:
			mas.sort()
			mas.reverse()
			for j in range(k):
				res += mas[j]
		else:
			res = sum(mas)
		result += res
		(shet, res, mas) = (1, 0, [lst[i]])
if shet > k:
	mas.sort()
	mas.reverse()
	for j in range(k):
		res += mas[j]
else:
	res = sum(mas)
result += res
print(result)


from sys import stdin
import math

def break_string(string, damage):
	arr = []
	dam = []
	aux1 = []
	aux2 = []
	for i in range(len(string)):
		if len(aux1) > 0:
			if aux1[-1] == string[i]:
				aux1.append(string[i])
				aux2.append(damage[i])
			else:
				arr.append(aux1)
				dam.append(aux2)
				aux1 = []
				aux2 = []
				aux1.append(string[i])
				aux2.append(damage[i])
		else:
			aux1.append(string[i])
			aux2.append(damage[i])
	arr.append(aux1)
	dam.append(aux2)
	return (arr, dam)
(n, k) = list(map(int, stdin.readline().split()))
damage = list(map(int, stdin.readline().split()))
string = list(stdin.readline().strip())
(arr, dam) = break_string(string, damage)
j = 0
total = 0
for i in range(len(arr)):
	if len(arr[i]) <= k:
		total += sum(dam[i])
		continue
	else:
		dam[i].sort()
		total += sum(dam[i][len(dam[i]) - k:])
print(total)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = str(input())
ans = 0
i = 0
b = []
while i < n:
	b.append(a[i])
	if i + 1 >= n or (i + 1 < n and s[i + 1] != s[i]):
		b.sort()
		b.reverse()
		p = 0
		i += 1
		while p < len(b) and p < k:
			ans += b[p]
			p += 1
		b = []
		continue
	i += 1
print(ans)


I = lambda : map(int, input().rstrip().split())
(n, k) = I()
a = list(I())
s = input()
ans = 0
j = 0
for i in range(n):
	if i == n - 1 or s[i] != s[i + 1]:
		ans += sum(sorted(a[j:i + 1])[-k:])
		j = i + 1
print(ans)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = input()
ans = 0
i = 0
j = 0
while i < n:
	while j < n and s[i] == s[j]:
		j += 1
	list = a[i:j]
	list.sort()
	list.reverse()
	i = j
	for m in range(min(len(list), k)):
		ans += list[m]
print(ans)


(n, k) = input().split()
n = int(n)
k = int(k)
ds = input().split()
ds = [-int(d) for d in ds]
ds.append(0)
s = input() + ';'
total = 0
pl = s[0]
x = [ds[0]]
for i in range(1, n + 1):
	if s[i] == pl:
		x.append(ds[i])
	else:
		if len(x) > k:
			x = sorted(x)
			total += -sum(x[:k])
		else:
			total += -sum(x)
		x = [ds[i]]
		pl = s[i]
print(total)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
str = input()
str = str + 'E'
s = sum(a)
len = 1
al = ar = 0
for i in range(n):
	if str[i] == str[i + 1]:
		len += 1
	else:
		if len > k:
			a_sub = []
			ar = i
			for j in range(al, ar + 1):
				a_sub.append(a[j])
			a_sub.sort()
			s_sub = 0
			for q in range(len - k):
				s_sub += a_sub[q]
			s -= s_sub
		al = i + 1
		len = 1
print(s)


from collections import defaultdict
(n, k) = map(int, input().split())
mas = list(map(int, input().split()))
s = list(input())
sm = 0
new_mas = [mas[0]]
for i in range(1, len(s)):
	if s[i - 1] == s[i]:
		new_mas.append(mas[i])
	else:
		new_mas.sort(reverse=True)
		sm += sum(new_mas[:k])
		new_mas = []
		new_mas.append(mas[i])
		continue
new_mas.sort(reverse=True)
sm += sum(new_mas[:k])
print(sm)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = input()
(i, j) = (0, 0)
total = 0
while i < n:
	while j < n and s[i] == s[j]:
		j += 1
	l = sorted(a[i:j], reverse=True)
	if len(l) == k:
		total += sum(l)
	elif len(l) < k:
		total += sum(l[0:])
	else:
		total += sum(l[0:k])
	i = j
print(total)


(n, k) = list(map(int, input().split()))
arr = list(map(int, input().split()))
s = str(input())
i = 0
ans = 0
while i < n:
	j = i + 1
	count = 1
	arrx = [arr[i]]
	while j < n and s[i] == s[j]:
		count += 1
		arrx.append(arr[j])
		j += 1
	arrx.sort(reverse=True)
	ans += sum(arrx[:min(count, k)])
	i = j
print(ans)


(numOfHits, maxPHit) = map(int, input().split(' '))
ithDmg = list(map(int, input().split(' ')))
hitPattern = input()

def getMaxDmg(ithDmg, start, end):
	if len(hitPattern) <= maxPHit:
		return sum(ithDmg[start:end])
	else:
		sList = ithDmg[start:end]
		sList.sort(reverse=True)
		return sum(sList[0:maxPHit])
if maxPHit == numOfHits:
	print(sum(ithDmg))
elif numOfHits == 1:
	print(sum(ithDmg))
else:
	hitPos = 0
	totDmg = 0
	while hitPos < len(hitPattern) - 1:
		if hitPattern[hitPos] == hitPattern[hitPos + 1]:
			start = hitPos
			while hitPos < len(hitPattern) - 1 and hitPattern[hitPos] == hitPattern[hitPos + 1]:
				hitPos += 1
			end = hitPos
			totDmg += getMaxDmg(ithDmg, start, end + 1)
		else:
			totDmg += ithDmg[hitPos]
		hitPos += 1
	if hitPos == len(hitPattern) - 1:
		totDmg += ithDmg[hitPos]
	print(totDmg)


(n, k) = map(int, input().split())
a = [int(i) for i in input().split()]
s = input()
prev = None
bag = list()
dmg = 0
i = 0
while i < n:
	if prev is None or s[i] == prev:
		bag.append(a[i])
		prev = s[i]
		i += 1
	else:
		bag.sort(reverse=True)
		dmg += sum(bag[:k])
		bag = []
		prev = None
bag.sort(reverse=True)
dmg += sum(bag[:k])
print(dmg)


(n, k) = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
s = input()
ans = 0
i = 0
while i < len(s):
	cur = []
	j = 0
	while i + j < n and s[i + j] == s[i]:
		cur.append(A[i + j])
		j += 1
	if len(cur) <= k:
		ans += sum(cur)
	else:
		cur.sort(reverse=True)
		ans += sum(cur[:k])
	i += j
print(ans)


(n, k) = map(int, input().split())
lis = list(map(int, input().split()))
s = list(input())
i = 0
ans = 0
while i < n:
	j = i
	tmp = []
	while j < n and s[j] == s[i]:
		tmp.append(lis[j])
		j += 1
	tmp = sorted(tmp, reverse=True)
	ans += sum(tmp[:k])
	i = j
print(ans)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = input()
c = '0'
cnt = 1
result = 0
mn = 1000000007
curr = []
i = 0
while i < n:
	pos = i
	while pos < n - 1 and s[pos] == s[pos + 1]:
		curr.append(int(a[pos]))
		pos += 1
	curr.append(a[pos])
	curr.sort()
	sz = min(int(len(curr)), k)
	for j in range(sz):
		result += curr[-(j + 1)]
	curr.clear()
	i = pos
	i += 1
print(result)


import sys
(n, k) = [int(s) for s in input().split()]
array = [int(s) for s in input().split()]
string = input()
counter = 0
extra = []
sum_ = 0
if len(string) == 1:
	print(array[0])
	sys.exit(0)
for i in range(len(string) - 1):
	if string[i] == string[i + 1]:
		counter += 1
		right = i + 1
		if i + 2 == len(string):
			left = i + 1 - counter
			new_array = array[left:right + 1]
			sorted_array = sorted(new_array)
			sum_ += sum(sorted_array[len(sorted_array) - min(k, counter + 1):])
			counter = 0
	elif string[i] != string[i + 1]:
		left = i - counter
		right = i
		new_array = array[left:right + 1]
		sorted_array = sorted(new_array)
		sum_ += sum(sorted_array[len(sorted_array) - min(k, counter + 1):])
		counter = 0
if string[len(string) - 1] != string[len(string) - 2]:
	sum_ += array[len(string) - 1]
print(sum_)


(N, K) = [int(x) for x in input().split()]
W = [int(x) for x in input().split()]
S = input()
index = 0
intervals = []
while index < N:
	i = index
	while i < N - 1:
		if S[i + 1] == S[i]:
			i += 1
		else:
			break
	intervals.append((index, i))
	index = i + 1
result = 0
for (x, y) in intervals:
	result += sum(sorted(W[x:y + 1], reverse=True)[:K])
print(result)


from sys import stdin, stdout
from collections import defaultdict, Counter
import math
I = stdin.readline
P = stdout.write
(n, k) = map(int, I().split())
arr = [int(x) for x in I().split()]
s = I().rstrip()
dmg = 0
i = 0
if n == 1:
	print(arr[0])
else:
	while i < n:
		if i < n - 1:
			if s[i] != s[i + 1]:
				dmg += arr[i]
			else:
				for j in range(i + 1, n):
					if s[j] != s[j - 1]:
						print
						break
				j -= 1
				if j == n - 2 and s[-1] == s[-2]:
					j += 1
				cou = j - i + 1
				new = arr[i:j + 1]
				if cou <= k:
					dmg += sum(new)
				else:
					new.sort(reverse=True)
					dmg += sum(new[0:k])
				i = j
		i += 1
		if i == n and s[-1] != s[-2]:
			dmg += arr[-1]
	print(dmg)


(n, k) = map(int, input().strip().split(' '))
lst = list(map(int, input().strip().split(' ')))
s = input()
ss = 0
s += ' '
num = []
sss = 0
i = 0
while i < n:
	cnt = 0
	sss = 0
	if s[i] == s[i + 1]:
		for j in range(i, n):
			num.append(lst[j])
			cnt += 1
			sss += lst[j]
			if s[j] != s[j + 1]:
				break
		i += cnt
		if cnt <= k:
			ss += sss
		else:
			num.sort(reverse=True)
			for j in range(k):
				ss += num[j]
		num = []
	else:
		ss += lst[i]
		i += 1
print(ss)


import heapq
(n, k) = list(map(int, input().split()))
a = list(map(int, input().split()))
a.append(-1)
s = input() + '!'
c = 1
ss = 0
temp = []
heapq.heappush(temp, a[0])
for i in range(1, n + 1):
	if s[i] == s[i - 1]:
		heapq.heappush(temp, a[i])
		c += 1
	else:
		heapq._heapify_max(temp)
		mv = min(c, k)
		s1 = 0
		for j in range(mv):
			val = heapq._heappop_max(temp)
			s1 += val
		ss += s1
		c = 1
		temp = []
		heapq.heappush(temp, a[i])
print(ss)


(n, k) = map(int, input().split())
aa = list(map(int, input().split()))
ss = list(input())
grp = [[aa[0]]]
for i in range(1, n):
	if ss[i] == ss[i - 1]:
		grp[-1].append(aa[i])
	else:
		grp.append([aa[i]])
sm = 0
for i in range(len(grp)):
	grp[i].sort()
	sm += sum(grp[i][-k:])
print(sm)


import sys
inf = float('inf')
mod = 1000000007

def get_array():
	return list(map(int, sys.stdin.readline().split()))

def get_ints():
	return map(int, sys.stdin.readline().split())

def input():
	return sys.stdin.readline()
import bisect

def main():
	(n, k) = get_ints()
	arr = get_array()
	s = input().strip()
	if k >= n:
		print(sum(arr))
		return
	from collections import Counter
	co = Counter(s)
	if len(co) == 1:
		arr.sort(reverse=True)
		print(sum(arr[:k]))
		return
	i = 0
	ans = 0
	count = 0
	while i < n - 1:
		if s[i] != s[i + 1]:
			count = 0
			i += 1
		else:
			start = i
			while i < n - 1 and s[i] == s[i + 1]:
				count += 1
				i += 1
			end = i
			if end - start < k:
				continue
			else:
				x = sorted(arr[start:end + 1])
				ans -= sum(x[:count - k + 1])
	ans += sum(arr)
	print(ans)
main()


(n, k) = map(int, input().split())
p = list(map(int, input().split()))
s = input() + '.'
cc = s[0]
ii = 0
r = 0
for (i, c) in enumerate(s):
	if c != cc:
		r += sum(sorted(p[ii:i], reverse=True)[:k])
		ii = i
		cc = c
print(r)


x = input()
x = x.split()
x = list(map(int, x))
s = input()
s = s.split()
s = list(map(int, s))
ss = input()
li = []
k = 0
li.append([])
li[0].append([ss[0], s[0]])
for i in range(1, len(ss)):
	if ss[i - 1] == ss[i]:
		li[k].append([ss[i], s[i]])
	else:
		k += 1
		li.append([])
		li[k].append([ss[i], s[i]])
count = 0
for i in range(k + 1):
	if len(li[i]) > x[1]:
		lii = []
		for j in range(len(li[i])):
			lii.append(li[i][j][1])
		lii.sort(reverse=True)
		lii = lii[:x[1]]
		count += sum(lii)
	else:
		for j in range(len(li[i])):
			count += li[i][j][1]
print(count)


(n, k) = list(map(int, input().split()))
a = list(map(int, input().split()))
s = input()
st = []
dmg = 0
st.append(a[0])
for i in range(1, n):
	if s[i] == s[i - 1]:
		st.append(a[i])
	else:
		dmg += sum(sorted(list(st))[-k:])
		st = []
		st.append(a[i])
dmg += sum(sorted(list(st))[-k:])
print(dmg)


import heapq
(n, k) = map(int, input().split())
arr = list(map(int, input().split()))
s = input()
heap = []
result = 0
for j in range(len(s) - 1):
	c_e = s[j]
	n_e = s[j + 1]
	w = arr[j]
	heapq.heappush(heap, -w)
	if c_e != n_e:
		for i in range(k):
			if heap:
				result -= heapq.heappop(heap)
			else:
				break
		heap = []
heapq.heappush(heap, -arr[-1])
for i in range(k):
	if heap:
		result -= heapq.heappop(heap)
	else:
		break
print(result)


(n, k) = map(int, input().strip().split())
ar = list(map(int, input().strip().split()))
arr = input()
arr += '|'
dic = {}
c = 1
a = []
x = []
for i in range(1, n + 1):
	if arr[i - 1] == arr[i]:
		c += 1
		a.append(i - 1)
	elif c > 1:
		a.append(i - 1)
		if c <= k:
			a = []
		else:
			z = sorted(ar[a[0]:a[len(a) - 1] + 1])
			x.extend(z[:c - k])
		c = 1
		a = []
if len(x) > 0:
	print(sum(ar) - sum(x))
else:
	print(sum(ar))


import math
import bisect
import itertools
import sys
I = lambda : sys.stdin.readline()
mod = 10 ** 9 + 7

def mindiff(a):
	b = a[:]
	b.sort()
	m = 10000000000
	for i in range(len(b) - 1):
		if b[i + 1] - b[i] < m:
			m = b[i + 1] - b[i]
	return m

def lcm(a, b):
	return a * b // math.gcd(a, b)

def merge(a, b):
	i = 0
	j = 0
	c = 0
	ans = []
	while i < len(a) and j < len(b):
		if a[i] < b[j]:
			ans.append(a[i])
			i += 1
		else:
			ans.append(b[j])
			c += len(a) - i
			j += 1
	ans += a[i:]
	ans += b[j:]
	return (ans, c)

def mergesort(a):
	if len(a) == 1:
		return (a, 0)
	mid = len(a) // 2
	(left, left_inversion) = mergesort(a[:mid])
	(right, right_inversion) = mergesort(a[mid:])
	(m, c) = merge(left, right)
	c += left_inversion + right_inversion
	return (m, c)

def is_prime(num):
	if num == 1:
		return False
	if num == 2:
		return True
	if num == 3:
		return True
	if num % 2 == 0:
		return False
	if num % 3 == 0:
		return False
	t = 5
	a = 2
	while t <= int(math.sqrt(num)):
		if num % t == 0:
			return False
		t += a
		a = 6 - a
	return True

def ceil(a, b):
	if a % b == 0:
		return a // b
	else:
		return a // b + 1

def binsearch(arr, b, low, high):
	if low == high:
		return low
	if arr[math.ceil((low + high) / 2)] < b:
		return binsearch(arr, b, low, math.ceil((low + high) / 2) - 1)
	else:
		return binsearch(arr, b, math.ceil((low + high) / 2), high)

def ncr1(n, r):
	s = 1
	for i in range(min(n - r, r)):
		s *= n - i
		s %= mod
		s *= pow(i + 1, mod - 2, mod)
		s %= mod
	return s

def calc(n, m, r):
	s = 0
	for i in range(0, r + 1, 2):
		s += ncr1(n, i) * ncr1(m, i)
		s %= mod
	return s

def modu(a, n):
	if a % n == 0:
		return n
	return a % n
for i in range(1):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	s = input()
	c = 1
	b = []
	for i in range(1, n):
		if s[i - 1] == s[i]:
			c += 1
			f = True
		else:
			b.append(c)
			c = 1
			f = False
	if True:
		b.append(c)
	ans = 0
	su = 0
	for i in b:
		c = a[su:min(su + i, 10 ** 9)]
		c.sort(reverse=True)
		c = c[:min(i, k)]
		su += i
		ans += sum(c)
	print(ans)


(n, k) = map(int, input().split())
a = [int(x) for x in input().split()]
a.append(-1)
dmg = 0
d = {}
s = input() + '#'
for i in range(n + 1):
	if s[i] not in d:
		d[s[i]] = [a[i]]
	if s[i] in d and i > 0 and (s[i - 1] != s[i]):
		dmg += sum(sorted(d.pop(s[i - 1]))[-k:])
	elif s[i] in d and i > 0 and (s[i - 1] == s[i]):
		d[s[i]].append(a[i])
print(dmg)


import heapq
(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = input()
d = 0
p = ''
heap = []
for (i, c) in enumerate(s):
	if c != p:
		d += sum(heapq.nlargest(k, heap))
		heap.clear()
	heapq.heappush(heap, a[i])
	p = c
d += sum(heapq.nlargest(k, heap))
print(d)


import heapq

class MaxHeapObj(object):

	def __init__(self, val):
		self.val = val

	def __lt__(self, other):
		return self.val > other.val

	def __eq__(self, other):
		return self.val == other.val

	def __str__(self):
		return str(self.val)

class MinHeap(object):

	def __init__(self):
		self.h = []

	def heappush(self, x):
		heapq.heappush(self.h, x)

	def heappop(self):
		return heapq.heappop(self.h)

	def __getitem__(self, i):
		return self.h[i]

	def __len__(self):
		return len(self.h)

class MaxHeap(MinHeap):

	def heappush(self, x):
		heapq.heappush(self.h, MaxHeapObj(x))

	def heappop(self):
		return heapq.heappop(self.h).val

	def __getitem__(self, i):
		return self.h[i].val
(n, m) = map(int, input().split())
arr = list(map(int, input().split()))
s = input()
ans = 0
mn = MinHeap()
mx = MaxHeap()
mn.heappush(arr[0])
mx.heappush(arr[0])
j = 1
for i in range(1, len(s)):
	if s[i] != s[i - 1]:
		cnt = 0
		while cnt < min(m, j):
			ans += mx.heappop()
			cnt += 1
		j = 0
		mx = MaxHeap()
		mx.heappush(arr[i])
		j += 1
	else:
		mx.heappush(arr[i])
		j += 1
cnt = 0
while cnt < min(m, j):
	ans += mx.heappop()
	cnt += 1
print(ans)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = input()
(ans, s1) = (0, 0)
(i, j) = (0, 0)
while i < n:
	while j < n and s[i] == s[j]:
		j += 1
	l = sorted(a[i:j], reverse=True)
	s1 = j - s1
	if s1 > k:
		ans += sum(l[0:k])
	else:
		ans += sum(l)
	i = j
print(ans)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = list(input())
count = 0
l = [a[0]]
le = 1
for i in range(1, n):
	if s[i] != s[i - 1]:
		l.sort(reverse=True)
		count += sum(l[:min(le, k)])
		l = [a[i]]
		le = 1
	else:
		le += 1
		l.append(a[i])
l.sort(reverse=True)
count += sum(l[:min(le, k)])
print(count)


from math import sqrt
import heapq

def main():
	(n, k) = [int(x) for x in input().split(' ')]
	damage = [int(x) for x in input().split(' ')]
	s = input()
	pq = []
	ans = 0
	for i in range(len(s)):
		heapq.heappush(pq, damage[i])
		if len(pq) > k:
			heapq.heappop(pq)
		if i == len(s) - 1 or s[i] != s[i + 1]:
			ans += sum(pq)
			pq.clear()
	print(ans)
main()


(a, b) = map(int, input().split())
c = list(map(int, input().split()))
d = input()
pred = d[0]
cnt = 1
su = 0
l = 0
r = 0
d += '9'
for i in range(1, a + 1):
	if d[i] == pred:
		cnt += 1
		r += 1
	else:
		if cnt <= b:
			su += sum(c[l:r + 1])
		else:
			s = c[l:r + 1]
			s.sort()
			for j in range(b):
				su += s[cnt - j - 1]
		cnt = 1
		l = r + 1
		r += 1
	pred = d[i]
print(su)


(n, k) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
x = input() + '#'
used = x[0]
dmg = 0
dmgt = []
for i in range(1, n + 1):
	if x[i] != used:
		if dmgt == []:
			dmg += a[i - 1]
		else:
			dmgt.append(a[i - 1])
			dmgt = sorted(dmgt, reverse=True)
			dmg += sum(dmgt[:k])
		used = x[i]
		dmgt = []
	else:
		dmgt.append(a[i - 1])
print(dmg)


import os
import sys
from io import BytesIO, IOBase
import math

def main():
	pass
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def binary(n):
	return bin(n).replace('0b', '')

def decimal(s):
	return int(s, 2)

def pow2(n):
	p = 0
	while n > 1:
		n //= 2
		p += 1
	return p

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(i)
			n = n / i
	if n > 2:
		l.append(int(n))
	return l

def isPrime(n):
	if n == 1:
		return False
	else:
		root = int(n ** 0.5)
		root += 1
		for i in range(2, root):
			if n % i == 0:
				return False
		return True

def maxPrimeFactors(n):
	maxPrime = -1
	while n % 2 == 0:
		maxPrime = 2
		n >>= 1
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			maxPrime = i
			n = n / i
	if n > 2:
		maxPrime = n
	return int(maxPrime)

def countcon(s, i):
	c = 0
	ch = s[i]
	for i in range(i, len(s)):
		if s[i] == ch:
			c += 1
		else:
			break
	return c
(n, k) = map(int, input().split())
l = list(map(int, input().split()))
s = input()
ans = sum(l)
i = 0
while i < n:
	c = countcon(s, i)
	if c > k:
		t = sorted(l[i:i + c])
		ans -= sum(t[:c - k])
	i += c
print(ans)


import bisect
from sys import stdin, stdout
(n, k) = map(int, stdin.readline().split())
l = list(map(int, stdin.readline().split()))
s = input()
suma = 0
arr = [l[0]]
for i in range(1, n):
	if s[i - 1] == s[i]:
		arr.append(l[i])
	else:
		arr.sort(reverse=True)
		suma += sum(arr[:k])
		arr = [l[i]]
arr.sort(reverse=True)
suma += sum(arr[:k])
print(suma)


letter = []
summ = 0
(n, k) = map(int, input().split())
num = list(map(int, input().split()))
string = input()
l = [num[0]]
for i in range(1, n):
	if string[i] == string[i - 1]:
		l.append(num[i])
	else:
		letter.append(l)
		l = [num[i]]
if len(l) > 0:
	letter.append(l)
for i in range(len(letter)):
	letter[i].sort(reverse=True)
	b = 0
	for j in range(len(letter[i])):
		if b < k:
			summ += letter[i][j]
			b += 1
		else:
			break
print(summ)


(N, K) = list(map(int, input().split()))
L = list(map(int, input().split()))
s = input()
d = {}
for i in s:
	try:
		if d[i] == []:
			pass
	except:
		d[i] = []
a = s[0]
l = [L[0]]
for i in range(1, N):
	if s[i] == a:
		l.append(L[i])
	else:
		l.sort(reverse=True)
		d[a].append(l)
		l = [L[i]]
		a = s[i]
l.sort(reverse=True)
d[a].append(l)
ans = 0
for i in d:
	temp = d[i]
	for j in temp:
		for k in range(min(len(j), K)):
			ans += j[k]
print(ans)


import heapq as hq
(n, k) = map(int, input().split())
l = list(map(int, input().split()))
s = input()
h = [l[0]]
hq.heapify(h)
a = l[0]
c = 1
for i in range(1, n):
	if s[i] == s[i - 1]:
		a += l[i]
		hq.heappush(h, l[i])
		c += 1
	else:
		while c > k:
			a -= hq.heappop(h)
			c -= 1
		c = 1
		h = []
		hq.heapify(h)
		a += l[i]
		hq.heappush(h, l[i])
while c > k:
	a -= hq.heappop(h)
	c -= 1
print(a)


(n, k) = [int(s) for s in input().split(' ')]
damages = [int(s) for s in input().split(' ')]
string = input()
totalDamage = 0
prevButton = None
prevDamages = []
for i in range(n):
	if string[i] == prevButton:
		prevDamages.append(damages[i])
	else:
		totalDamage += sum(sorted(prevDamages, reverse=True)[:k])
		prevDamages = [damages[i]]
		prevButton = string[i]
totalDamage += sum(sorted(prevDamages, reverse=True)[:k])
print(totalDamage)


def sumn(l, r):
	nonlocal dmgs
	tmp = []
	for i in range(l, r + 1):
		tmp.append(dmgs[i])
	tmp.sort(reverse=True)
	return sum(tmp[:k])
(n, k) = map(int, input().split())
dmgs = [int(x) for x in input().split()]
s = input()
d = {}
key = 'abcdefghijklnmopqrstuvwxyz'
l = 0
r = 0
total = 0
for i in range(len(s) - 1):
	if s[i] != s[i + 1]:
		r = i
		total += sumn(l, r)
		l = i + 1
total += sumn(l, n - 1)
print(total)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
seq = input()
(active, start) = (seq[0], 0)
summ = sum(a)
for (i, j) in enumerate(seq):
	if j != active:
		if i - start > k:
			ch = a[start:i]
			ch.sort()
			rem = sum(ch[0:i - start - k])
			summ -= rem
		(start, active) = (i, j)
	if i == len(seq) - 1:
		i += 1
		if i - start > k:
			ch = a[start:i]
			ch.sort()
			rem = sum(ch[0:i - start - k])
			summ -= rem
		(start, active) = (i, j)
print(summ)


import bisect
import decimal
from decimal import Decimal
import os
from collections import Counter
import bisect
from collections import defaultdict
import math
import random
import heapq
from math import sqrt
import sys
from functools import reduce, cmp_to_key
from collections import deque
import threading
from itertools import combinations
from io import BytesIO, IOBase
from itertools import accumulate
sys.setrecursionlimit(200000)
decimal.getcontext().prec = 46

def primeFactors(n):
	prime = set()
	while n % 2 == 0:
		prime.add(2)
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			prime.add(i)
			n = n // i
	if n > 2:
		prime.add(n)
	return list(prime)

def getFactors(n):
	factors = []
	i = 1
	while i <= math.sqrt(n):
		if n % i == 0:
			if n // i == i:
				factors.append(i)
			else:
				factors.append(i)
				factors.append(n // i)
		i = i + 1
	return factors

def modefiedSieve():
	mx = 10 ** 7 + 1
	sieve = [-1] * mx
	for i in range(2, mx):
		if sieve[i] == -1:
			sieve[i] = i
			for j in range(i * i, mx, i):
				if sieve[j] == -1:
					sieve[j] = i
	return sieve

def SieveOfEratosthenes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	num = []
	for p in range(2, n + 1):
		if prime[p]:
			num.append(p)
	return num

def lcm(a, b):
	return a * b // math.gcd(a, b)

def sort_dict(key_value):
	return sorted(key_value.items(), key=lambda kv: (kv[1], kv[0]), reverse=True)

def list_input():
	return list(map(int, input().split()))

def num_input():
	return map(int, input().split())

def string_list():
	return list(input())

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def binaryToDecimal(n):
	return int(n, 2)

def DFS(n, s, adj):
	visited = [False for i in range(n + 1)]
	stack = []
	stack.append(s)
	while len(stack):
		s = stack[-1]
		stack.pop()
		if not visited[s]:
			visited[s] = True
		for node in adj[s]:
			if not visited[node]:
				stack.append(node)

def maxSubArraySum(a, size):
	maxint = 10 ** 10
	max_so_far = -maxint - 1
	max_ending_here = 0
	for i in range(0, size):
		max_ending_here = max_ending_here + a[i]
		if max_so_far < max_ending_here:
			max_so_far = max_ending_here
		if max_ending_here < 0:
			max_ending_here = 0
	return max_so_far

def solve():
	(n, k) = num_input()
	arr = list_input()
	seq = input()
	i = 0
	ans = 0
	while i < n:
		j = i
		temp = []
		while j < n and seq[i] == seq[j]:
			temp.append(arr[j])
			j += 1
		if len(temp) <= k:
			ans += sum(temp)
		else:
			temp.sort(reverse=True)
			ans += sum(temp[:k])
		i = j
	print(ans)
t = 1
for _ in range(t):
	solve()


(n, k) = map(int, input().split())
damage = list(map(int, input().split()))
string = input()
count = 0
prev = ''
ans = 0
for i in range(n):
	if string[i] == prev:
		count += 1
	else:
		l = sorted(damage[i - count:i], reverse=True)
		ans += sum(l[:k])
		count = 1
	prev = string[i]
l = sorted(damage[i - count + 1:i + 1], reverse=True)
ans += sum(l[:k])
print(ans)


import math, sys
from sys import stdin, stdout
from collections import Counter, defaultdict, deque
input = stdin.readline
I = lambda : int(input())
li = lambda : list(map(int, input().split()))

def case():
	(n, k) = li()
	a = li()
	s = input().strip()
	i = j = 0
	ans = 0
	while True:
		j = i
		temp = []
		while j < n - 1 and s[j] == s[j + 1]:
			temp.append(a[j])
			j += 1
		temp.append(a[j])
		temp.sort(reverse=True)
		ans += sum(temp[:min(k, len(temp))])
		j += 1
		i = j
		if i == n:
			break
	print(ans)
for _ in range(1):
	case()


(n, k) = [int(x) for x in input().split()]
arr = [int(x) for x in input().split()]
s = input()
pts = []
i = 0
while i < n:
	j = i
	while j + 1 < n and s[j + 1] == s[j]:
		j += 1
	pts.append(arr[i:j + 1])
	i = j + 1
ans = 0
for x in pts:
	if len(x) <= k:
		ans += sum(x)
	else:
		y = sorted(x, reverse=True)
		ans += sum(y[:k])
print(ans)


def get():
	return list(map(int, input().split()))
(n, k) = get()
a = list(get())
s = input()
i = 0
dmg = 0
while i < n:
	j = i + 1
	while j < n and s[i] == s[j]:
		j += 1
	j -= 1
	if j - i + 1 <= k:
		dmg += sum(a[i:j + 1])
	else:
		g = a[i:j + 1]
		g.sort()
		dmg += sum(g[len(g) - k:])
	i = j + 1
print(dmg)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = input()
b = [[s[i], a[i]] for i in range(n)]
ans = 0
temp = []
c = b[0][0]
for i in b:
	if i[0] != c:
		c = i[0]
		temp.sort(reverse=True)
		ans += sum(temp[:min(len(temp), k)])
		temp.clear()
	if i[0] == c:
		temp.append(i[1])
temp.sort(reverse=True)
ans += sum(temp[:min(len(temp), k)])
print(ans)


def Read(f=int):
	return [f(x) for x in input().split()]

def FloatsToString(arr):
	return ' '.join(('{:.10f}'.format(x) for x in arr))
(n, k) = Read(int)
A = Read(int)
s = input()
tmp = []
prev = ' '
dmg = 0
for i in range(n):
	if s[i] != prev:
		if k < len(tmp):
			tmp.sort(reverse=True)
			dmg += sum(tmp[:k])
		else:
			dmg += sum(tmp)
		tmp.clear()
	tmp.append(A[i])
	prev = s[i]
if k < len(tmp):
	tmp.sort(reverse=True)
	dmg += sum(tmp[:k])
else:
	dmg += sum(tmp)
print(dmg)


(n, k) = map(int, input().split())
l = list(map(int, input().split()))
s = input()
i = 0
ln = len(s)
som = 0
p1 = 0
p2 = 0

def sltice(l, k, p1, p2):
	sub = p2 - p1
	if sub >= k:
		cp = l[p1:p2]
		cp.sort(reverse=True)
		kom = cp[0:k]
	elif p1 == p2:
		kom = [l[p1], 0]
	else:
		kom = l[p1:p2]
	return sum(kom)
if ln == 1:
	som = l[0]
else:
	while i < ln - 1:
		if s[i] == s[i + 1] and i == ln - 2:
			p2 = i + 2
			som = som + sltice(l, k, p1, p2)
			i = i + 1
		elif s[i] != s[i + 1] and i == ln - 2:
			p2 = i + 1
			som = som + sltice(l, k, p1, p2)
			p1 = i + 1
			p2 = i + 1
			som = som + sltice(l, k, p1, p2)
			i = i + 1
		elif s[i] == s[i + 1]:
			i = i + 1
		else:
			p2 = i + 1
			som = som + sltice(l, k, p1, p2)
			p1 = i + 1
			i = i + 1
print(som)


import heapq
(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = input()
cur = 'aa'
res = 0
temp_arr = []
cur_counter = 0
temp_sum = 0
for (index, value) in enumerate(a):
	if s[index] != cur:
		cur = s[index]
		res += temp_sum
		cur_counter = 1
		temp_arr = []
		heapq.heappush(temp_arr, value)
		temp_sum = value
	else:
		cur_counter += 1
		if cur_counter > k and value > temp_arr[0]:
			temp_sum += value
			smallest = heapq.heappushpop(temp_arr, value)
			temp_sum -= smallest
		elif cur_counter <= k:
			heapq.heappush(temp_arr, value)
			temp_sum += value
res += temp_sum
print(res)


(n, k) = map(int, input().split())
l = list(map(int, input().split()))
s = input()
if n == 1 and k == 1:
	print(l[0])
	exit()

def f(beg, i):
	x = l[beg:i]
	if i - beg <= k:
		return sum((i for i in x))
	else:
		x = sorted(x)
		return sum(x) - sum(x[0:i - beg - k])
i = 1
beg = 0
prev = s[0]
sm = 0
while i < n:
	if s[i] == s[i - 1]:
		prev = prev + s[i]
	else:
		sm += f(beg, i)
		beg = i
		prev = s[i]
	if i == n - 1:
		sm += f(beg, i + 1)
	i += 1
print(sm)


(n, k) = map(int, input().split())
damage = list(map(int, input().split()))
s = input()
i = 0
totalDmg = 0
while i < n:
	l = 1
	while i + l < n and s[i] == s[i + l]:
		l += 1
	temp = damage[i:i + l]
	temp.sort(reverse=True)
	if len(temp) > k:
		temp = temp[:k]
	totalDmg += sum(temp)
	i += l
print(totalDmg)


def fur(B, n, k):
	B.sort(reverse=True)
	sum = 0
	if n <= k:
		for i in range(0, n):
			sum += B[i]
	else:
		for i in range(k):
			sum += B[i]
	return sum
(n, k) = list(map(int, input().split()))
A = list(map(int, input().split()))
s = input()
B = [A[0]]
ans = 0
for i in range(1, n):
	if s[i] == s[i - 1]:
		B.append(A[i])
	if s[i] != s[i - 1]:
		ans += fur(B, len(B), k)
		B = [A[i]]
if len(B) > 0:
	ans += fur(B, len(B), k)
print(ans)


import sys
import itertools
import re
import heapq
data = sys.stdin.read().splitlines()
(n, m) = map(int, data[0].split())
nums = tuple(map(int, data[1].split()))
s = data[2]
ans = 0
i = 0
for (ch, g) in itertools.groupby(s):
	l = len(list(g))
	j = i + l
	ans += sum(heapq.nlargest(min(l, m), nums[i:j]))
	i = j
print(ans)


import collections, atexit, math, sys, bisect
sys.setrecursionlimit(1000000)

def getIntList():
	return list(map(int, input().split()))
try:
	import numpy

	def dprint(*args, **kwargs):
		print(*args, file=sys.stderr)
	dprint('debug mode')
except Exception:

	def dprint(*args, **kwargs):
		pass
inId = 0
outId = 0
if inId > 0:
	dprint('use input', inId)
	try:
		f = open('input' + str(inId) + '.txt', 'r')
		sys.stdin = f
	except Exception:
		dprint('invalid input file')
if outId > 0:
	dprint('use output', outId)
	try:
		f = open('stdout' + str(outId) + '.txt', 'w')
		sys.stdout = f
	except Exception:
		dprint('invalid output file')
	atexit.register(lambda : sys.stdout.close())
(N, K) = getIntList()
za = getIntList()
S = input()
zz = []
lc = ' '
for i in range(N):
	if S[i] == lc:
		zz[-1].append(za[i])
	else:
		lc = S[i]
		zz.append([za[i]])
r = 0
for x in zz:
	x.sort(reverse=True)
	r += sum(x[:K])
print(r)


def main():
	(n, k) = map(int, input().split())
	damage = list(map(int, input().split()))
	s = input()
	hits = []
	ans = 0
	curr = s[0]
	for i in range(n):
		if s[i] == curr:
			hits.append(damage[i])
		else:
			hits.sort(reverse=True)
			ans += sum(hits[:k])
			curr = s[i]
			hits = [damage[i]]
	hits.sort(reverse=True)
	ans += sum(hits[:k])
	print(ans)
main()


import heapq
from collections import defaultdict
(n, k) = map(int, input().split())
a = [int(i) for i in input().split()]
chars = [c for c in input()]
heap = []
ans = 0
for (char, score) in zip(chars, a):
	if heap and char != heap[0][1]:
		while heap:
			(s, c) = heapq.heappop(heap)
			ans += s
		heapq.heappush(heap, (score, char))
	elif len(heap) == k and heap[0][0] < score:
		heapq.heappop(heap)
		heapq.heappush(heap, (score, char))
	elif len(heap) < k:
		heapq.heappush(heap, (score, char))
while heap:
	(score, _) = heapq.heappop(heap)
	ans += score
print(ans)


mod = 1000000007
ii = lambda : int(input())
si = lambda : input()
dgl = lambda : list(map(int, input()))
f = lambda : map(int, input().split())
il = lambda : list(map(int, input().split()))
ls = lambda : list(input())
from itertools import *
(n, k) = f()
l = il()
s = si()
l1 = []
for (i, j) in groupby(s):
	l1.append(len(list(j)))
n1 = len(l1)
j = 0
sm = 0
for i in l1:
	if i > k:
		sm += sum(sorted(l[j:j + i], reverse=True)[:k])
	else:
		sm += sum(l[j:j + i])
	j += i
print(sm)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
s = input()
k = 0
l = 0
while l < n:
	f = l + 1
	while f < n and s[f] == s[l]:
		f += 1
	b = sorted(a[l:f])
	k += sum(b[-m:])
	l = f
print(k)


(n, k) = [int(x) for x in input().split()]
arr = [int(x) for x in input().split()]
buttons = input()
dmg = 0
i = 0
while i < n:
	subset = []
	subset.append(arr[i])
	j = i + 1
	if j >= len(arr):
		dmg += arr[i]
		break
	consec = 1
	currTot = 0
	currTot += arr[i]
	while j < n and buttons[j] == buttons[i]:
		consec += 1
		currTot += arr[j]
		subset.append(arr[j])
		j += 1
	if consec <= k:
		dmg += currTot
	else:
		subset.sort(reverse=True)
		dmg += sum(subset[:k])
	i = j
print(dmg)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = input()
ans = 0
now = []
now.append(a[0])
for i in range(1, n):
	if s[i] == s[i - 1]:
		now.append(a[i])
	else:
		if len(now) > k:
			now.sort(key=lambda x: -x)
			ans += sum(now[:k])
		else:
			ans += sum(now)
		now = [a[i]]
if len(now) > k:
	now.sort(key=lambda x: -x)
	ans += sum(now[:k])
else:
	ans += sum(now)
now = []
print(ans)


inputs1 = input().split(' ')
n = int(inputs1[0])
k = int(inputs1[1])
damages = [int(x) for x in input().split(' ')]
buttons = input()
i = 0
suma = 0
while i < n:
	left = i
	right = i + 1
	while right < n and buttons[right] == buttons[i]:
		right += 1
	subarray_damages = damages[left:right]
	suma += sum(sorted(subarray_damages)[::-1][0:k])
	i = right
print(suma)


import sys
import math

def read_int():
	return int(input().strip())

def read_int_list():
	return list(map(int, input().strip().split()))

def read_string():
	return input().strip()

def read_string_list(delim=' '):
	return input().strip().split(delim)
(n, k) = read_int_list()
damages = read_int_list()
keys = read_string()
start = 0
damage = 0
while start < n:
	end = start + 1
	while end < n and keys[start] == keys[end]:
		end += 1
	sub_list = damages[start:end]
	sub_list.sort(reverse=True)
	damage += sum(sub_list[:k])
	start = end
print(damage)


(n, k) = list(map(int, input().split()))
a = list(map(int, input().split()))
s = input()
(l, r, ans) = (0, 0, 0)
(cnt, summ) = (0, [])
while r < n:
	if s[r] != s[l]:
		if r - l <= k:
			for i in range(l, r):
				ans += a[i]
		else:
			tmp = sorted(a[l:r], reverse=True)
			for i in range(k):
				ans += tmp[i]
		l = r
	r += 1
if r - l <= k:
	for i in range(l, r):
		ans += a[i]
else:
	tmp = sorted(a[l:r], reverse=True)
	for i in range(k):
		ans += tmp[i]
print(ans)


import math
(n, maxhit) = list(map(int, input().split()))
dmgs = list(map(int, input().split()))
btns = input()
maxdmg = 0
prevbtn = None
eq_hits_count = 0
for i in range(n):
	if btns[i] == prevbtn:
		eq_hits_count += 1
	else:
		maxdmg += sum(sorted(dmgs[i - eq_hits_count:i], reverse=True)[:maxhit])
		eq_hits_count = 1
		prevbtn = btns[i]
maxdmg += sum(sorted(dmgs[i - eq_hits_count + 1:], reverse=True)[:maxhit])
print(maxdmg)


info = input().split(' ')
damages = list(map(int, input().split(' ')))
keys = input() + ' '
final = 0
counter = 0
while counter < len(keys) - 1:
	fixed = keys[counter]
	current = [damages[counter]]
	counter += 1
	while keys[counter] == fixed:
		current.append(damages[counter])
		counter += 1
	chosen = min(int(info[1]), len(current))
	current = sorted(current)[::-1]
	final += sum(current[:chosen])
print(final)


import math
import sys
import bisect
t = 1
for _ in range(t):
	(n, k) = map(int, input().split())
	l = list(map(int, input().split()))
	s = input()
	d = {}
	x = s[0]
	cnt = 1
	l1 = []
	l2 = []
	l2.append(l[0])
	for i in range(1, n):
		if s[i - 1] == s[i]:
			l2.append(l[i])
		else:
			l1.append(l2)
			l2 = []
			l2.append(l[i])
	l1.append(l2)
	ans = 0
	for i in range(len(l1)):
		l1[i].sort(reverse=True)
		ans += sum(l1[i][:min(len(l1[i]), k)])
	print(ans)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = input()
ans = 0
ls = []
lstmp = []
for i in range(n):
	if i == 0 or s[i] == s[i - 1]:
		lstmp.append(a[i])
	else:
		ls.append(lstmp)
		lstmp = [a[i]]
	if i == n - 1 and lstmp:
		ls.append(lstmp)
for i in ls:
	if len(i) <= k:
		ans += sum(i)
	else:
		i.sort(reverse=True)
		ans += sum(i[:k])
print(ans)


(n, t) = map(int, input().strip().split())
L = list(map(int, input().strip().split()))
k = list(input().strip())
a = k[0]
sums = 0
j = 0
for i in range(1, n):
	if a != k[i]:
		G = L[j:i]
		G.sort(reverse=True)
		if len(G) <= t:
			sums += sum(G)
		else:
			sums += sum(G[:t])
		j = i
		a = k[i]
G = L[j:]
G.sort(reverse=True)
if len(G) <= t:
	sums += sum(G)
else:
	sums += sum(G[:t])
print(sums)


from itertools import groupby
(n, k) = [int(x) for x in input().split(' ')]
arr = [int(x) for x in input().split(' ')]
s = [list(g) for (k, g) in groupby(input())]
res = 0
curr_i = 0
for i in range(len(s)):
	use = len(s[i])
	start = curr_i
	curr_i += use
	stop = start + use
	if use > k:
		a = sorted(arr[start:stop])[::-1]
		res += sum(a[:k])
	else:
		res += sum(arr[start:stop])
print(res)


(n, k) = map(int, input().split())
arr = list(zip(map(int, input().split()), range(n)))
s = input()
segments = {}
start = 0
last = s[0]
st = []
for i in range(n):
	if last != s[i]:
		segments[start, i - 1] = k
		start = i
		last = s[i]
		st.append(start)
	else:
		st.append(start)
segments[start, n - 1] = k
nd = [0] * n
for x in segments:
	for i in range(x[0], x[1] + 1):
		nd[i] = x[1]
arr.sort(reverse=True)
sm = 0
for i in range(n):
	index = arr[i][1]
	segment = (st[index], nd[index])
	if segments[segment] > 0:
		sm += arr[i][0]
		segments[segment] -= 1
print(sm)


import heapq

def solve():
	(n, k) = [int(x) for x in input().split()]
	vals = [int(x) for x in input().split()]
	buttons = input()
	seqs = split_seqs(vals, buttons)
	print(sum((seq_val(seq, k) for seq in seqs)))

def split_seqs(vals, buttons):
	seqs = []
	begin = 0
	for i in range(1, len(buttons)):
		if buttons[i - 1] != buttons[i]:
			seqs.append(vals[begin:i])
			begin = i
	seqs.append(vals[begin:])
	return seqs

def seq_val(seq, k):
	return sum(heapq.nlargest(k, seq))
solve()


fl = list(map(int, input().split(' ')))
sl = list(map(int, input().split(' ')))
s = input()
i = 0
t = 0
while i < len(s):
	c = 1
	j = i + 1
	while j < len(s):
		if s[j] == s[i]:
			c += 1
		else:
			break
		j += 1
	if c > fl[1]:
		sub = sorted(sl[i:j])
		t += sum(sub[c - fl[1]:c])
	else:
		t += sum(sl[i:j])
	i = j
print(t)


(n, k) = map(int, input().split())
l = [int(i) for i in input().split()]
s = input()
i = 0
ans = 0
while i < n:
	curr = s[i]
	c = 0
	a = []
	sm = 0
	f = 0
	while i < n and s[i] == curr:
		c += 1
		a.append(l[i])
		i += 1
	a.sort(reverse=1)
	ans += sum(a[:k])
print(ans)


k = [int(e) for e in input().split()]
l1 = [int(e) for e in input().split()]
s = input()
l = [[l1[0]]]
for i in range(1, k[0]):
	if s[i] == s[i - 1]:
		l[-1].append(l1[i])
	else:
		l.append([l1[i]])
sm = 0
for i in l:
	if len(i) <= k[1]:
		sm += sum(i)
	else:
		i.sort(reverse=True)
		sm += sum(i[:k[1]])
print(sm)


(n, k) = list(map(int, input().strip().split()))
hits = list(map(int, input().strip().split()))
s = input().strip()
dmg = 0
p = 0
while p < n:
	letter = s[p]
	pot = []
	while p < n and s[p] == letter:
		pot.append(hits[p])
		p += 1
	pot.sort()
	dmg += sum(pot[-k:])
print(dmg)


from heapq import heappush, heappop
(n, k) = list(map(int, input().split()))
a = list(map(int, input().split()))
a.append(0)
s = input().strip() + '#'
h = []
ret = 0
for (i, c) in enumerate(s):
	if i != 0 and s[i - 1] != c:
		for j in range(min(len(h), k)):
			ret -= heappop(h)
		h = []
	heappush(h, -a[i])
print(ret)


(n, r) = input().split()
l = input().split()
l = list(map(int, l))
s = input()
r = int(r)
q = []
m = s[0]
f = 0
q = [l[0]]
for i in range(1, len(s)):
	if s[i] == m:
		q.append(l[i])
	else:
		m = s[i]
		q.sort(reverse=True)
		f = f + sum(q[:r])
		q = []
		q.append(l[i])
q.sort(reverse=True)
f = f + sum(q[:r])
print(f)


(n, k) = tuple(map(int, input().split()))
hit = list(map(int, input().split()))
s = input()
cons_count = 1
su = 0
ar = [hit[0]]
i = 0
for i in range(1, len(s)):
	if s[i] == s[i - 1]:
		cons_count = cons_count + 1
	else:
		ar = hit[i - cons_count:i]
		if cons_count > k:
			ar.sort()
			su = su + sum(ar[-k:])
		else:
			su = su + sum(ar)
		cons_count = 1
		ar = [hit[i]]
if cons_count > k:
	ar = hit[-cons_count:]
	ar.sort()
	su = su + sum(ar[-k:])
else:
	su = su + sum(hit[-cons_count:])
print(su)


def main():
	(n, k) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	s = input()
	ret = p1 = 0
	while p1 < n:
		(p2, temp) = (p1, [])
		while p2 < n and s[p2] == s[p1]:
			temp.append(a[p2])
			p2 += 1
		temp.sort(reverse=True)
		ret += sum(temp[:k])
		p1 = p2
	print(ret)

def __starting_point():
	main()
__starting_point()


def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(mi())
(n, k) = mi()
a = li()
s = input().strip()
ans = 0
i = 0
while i < n:
	j = i + 1
	while j < n and s[j] == s[i]:
		j += 1
	b = sorted(a[i:j])
	ans += sum(b[-k:])
	i = j
print(ans)


(n, k) = list(map(int, input().split()))
a = list(map(int, input().split()))
s = input() + '-'
d = [0] * 26
ll = '0'
dmg = 0
cur = []
ll = s[0]
for i in range(len(s)):
	if ll != s[i]:
		cur.sort(key=lambda x: -x)
		dmg += sum(cur[:k])
		if i < n:
			cur = [a[i]]
	else:
		cur.append(a[i])
	ll = s[i]
print(dmg)


(n, k) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
s = str(input())
c = 0
q = []
p = []
for i in range(n - 1):
	if s[i] == s[i + 1]:
		c = c + 1
		p.append(a[i])
	else:
		if c != 0:
			p.append(a[i])
		else:
			q.append(a[i])
		if len(p) > k:
			p.sort()
			q.append(sum(p[len(p) - k:]))
		else:
			q.append(sum(p))
		p = []
		c = 0
if s[n - 1] == s[n - 2]:
	p.append(a[n - 1])
	if len(p) > k:
		p.sort()
		q.append(sum(p[len(p) - k:]))
	else:
		q.append(sum(p))
else:
	if len(p) > k:
		p.sort()
		q.append(sum(p[len(p) - k:]))
	else:
		q.append(sum(p))
	q.append(a[n - 1])
print(sum(q))


def C():
	(n, k) = map(int, input().split())
	a = [int(x) for x in input().split()]
	s = input()
	dmg = 0
	i = 0
	c = []
	cnt = 1
	while i < n - 1:
		if s[i + 1] == s[i]:
			cnt += 1
		else:
			c.append(cnt)
			cnt = 1
		i += 1
	c.append(cnt)
	i = 0
	seq = 0
	while i < n:
		ordered = sorted(a[i:i + c[seq]], reverse=True)
		dmg += sum(ordered[:k])
		i += c[seq]
		seq += 1
	print(dmg)
C()


pairnk = input().split(' ')
n = int(pairnk[0])
k = int(pairnk[1])
arr = list()
alls = input().split(' ')
for ele in alls:
	arr.append(int(ele))
str = input()
cur = 0
ans = 0
while cur < n:
	cur1 = cur + 1
	vec = list()
	vec.append(arr[cur])
	while cur1 < n:
		if str[cur1] == str[cur]:
			vec.append(arr[cur1])
			cur1 = cur1 + 1
		else:
			break
	cur = cur1
	vec.sort(reverse=True)
	for i in range(len(vec)):
		if i < k:
			ans += vec[i]
		else:
			break
print(ans)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = str(input())
x = []
st = []
for i in range(len(s)):
	if i != 0 and s[i] != s[i - 1]:
		x.append(st)
		st = [a[i]]
	elif i == 0:
		st = [a[i]]
	else:
		st.append(a[i])
	if i == n - 1:
		x.append(st)
dm = 0
for i in x:
	if len(i) >= k:
		i.sort(reverse=True)
		for j in range(k):
			if j < len(i):
				dm += i[j]
	else:
		dm += sum(i)
print(dm)


import sys
import bisect
from collections import Counter
read_int = lambda : int(sys.stdin.readline())
read_ints = lambda : map(int, sys.stdin.readline().split())
read_int_list = lambda : list(read_ints())
read = lambda : sys.stdin.readline().strip()
read_list = lambda : sys.stdin.readline().split()

def main():
	(n, k) = read_ints()
	a = read_int_list()
	s = read()
	cur = ('', 0)
	split = []
	res = 0
	for i in range(n):
		if s[i] == cur[0]:
			split[-1].append(a[i])
		else:
			split.append([a[i]])
			cur = (s[i], 0)
	for li in split:
		res += sum(sorted(li, reverse=True)[:k])
	print(res)
main()


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = input()
li = []
i = 0
while i < n - 1:
	st = s[i]
	li.append(i)
	while i < n - 1 and s[i + 1] == st:
		i += 1
	i += 1
if s[n - 1] != s[n - 2]:
	li.append(n - 1)
li.append(n)
c = 0
if len(li) == 1 and k == 1:
	print(a[0])
else:
	for i in range(len(li) - 1):
		t = a[li[i]:li[i + 1]]
		t.sort()
		t = t[::-1]
		t = t[:k]
		c += sum(t)
	print(c)