-
Notifications
You must be signed in to change notification settings - Fork 0
/
Brutality.cpp
59 lines (52 loc) · 1.33 KB
/
Brutality.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/* https://codeforces.com/problemset/problem/1107/C
#greedy #sorting #two-pointer
*/
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
vector<int> unitsVector(n);
for (int i=0; i < n; i++) {
int tmp;
cin >> tmp;
unitsVector[i] = tmp;
}
string str;
cin >> str;
vector<vector<int> > vt('z' - 'a' + 1);
for (int i=0; i < str.length(); i++) {
vt[str[i]-'a'].push_back(i);
}
long long sum = 0;
for(auto vtc : vt) {
int pre_idx = -10;
priority_queue<int> pq;
for (int idx = 0; idx < vtc.size(); idx++) {
if (vtc[idx] != pre_idx+1) {
int count = 0;
while (!pq.empty() && count < k) {
sum += pq.top();
pq.pop();
count++;
}
pre_idx = vtc[idx];
pq = priority_queue<int>();
pq.push(unitsVector[vtc[idx]]);
} else {
pre_idx = vtc[idx];
pq.push(unitsVector[vtc[idx]]);
}
}
int count = 0;
while (!pq.empty() && count < k) {
sum += pq.top();
pq.pop();
count++;
}
}
cout << sum << endl;
return 0;
}