#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define X first
#define Y second
#define SZ(a) ((int)a.size())
#define ALL(v) v.begin(), v.end()
#define pb push_back
struct node {
int first, sz;
ll sum;
};
int lft[1000005];
list<node>::iterator arr[1000005];
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int n, k;
long long ans = 0;
string s;
cin >> n >> k >> s;
if (n > 500) return 0;
vector<int> plA, plB;
for (int i = 0; i < n + n; ++i) {
if (s[i] == 'B')
plB.pb(i);
else
plA.pb(i), lft[SZ(plA)] = SZ(plB);
}
const ll INF = 1e18;
vector<vector<ll>> dp(k + 1, vector<ll>(n + 1, INF));
ll fin = INF;
dp[0][0] = 0;
for (int i = 0; i < k; ++i) {
for (int j = 1; j <= n; ++j) {
ll sum = 0;
for (int p = j; p <= n; ++p) {
sum += max(0, lft[p] - (j - 1));
dp[i + 1][p] = min(dp[i + 1][p], dp[i][j - 1] + sum);
}
}
fin = min(fin, dp[i + 1][n]);
}
cout << ans + fin << "\n";
/*list<node> grp;
typedef pair<ll, int> pr;
priority_queue<pr, vector<pr>, greater<pr>> pq;
node cur;
cur.first = -1;
for (int i = 0; i < n; ++i) {
if (cur.first == -1 || plA[i] > plB[cur.first]) {
if (cur.first != -1)
grp.pb(cur);
cur.first = i;
cur.sz = 1;
cur.sum = lft[i];
}
else {
cur.sz += 1;
cur.sum += lft[i];
}
}
grp.pb(cur);
auto cal = [&](list<node>::iterator it) {
if (next(it) == grp.end()) return -1LL;
ll cst;
cst = next(it)->sum - it->first * next(it)->sz;
return cst;
};
auto rm = [&](list<node>::iterator it) {
it->sz += next(it)->sz;
it->sum += next(it)->sz * it->first;
arr[next(it)->first] = grp.end();
grp.erase(next(it));
if (next(it) != grp.end())
pq.push(pr(cal(it), it->first));
if (it != grp.begin())
pq.push(pr(cal(prev(it)), prev(it)->first));
};
for (int i = 0; i < n; ++i)
arr[i] = grp.end();
for (auto it = grp.begin(); next(it) != grp.end(); ++it) {
arr[it->first] = it;
pq.push(pr(cal(it), it->first));
}
cerr << s << "\n";
for (int i = 0; i < n; ++i)
cerr << plA[i] << " ";
cerr << endl;
for (int i = 0; i < n; ++i)
cerr << plB[i] << " ";
cerr << endl;
cerr << ans << "\n";
while (!pq.empty() && SZ(grp) > k) {
auto [cst, first] = pq.top();
pq.pop();
if (arr[first] == grp.end() || cst != cal(arr[first])) continue;
ans += cst;
rm(arr[first]);
}
assert(SZ(grp) <= k);
cout << ans << "\n";*/
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
8148 KB |
Output is correct |
2 |
Correct |
3 ms |
8148 KB |
Output is correct |
3 |
Correct |
4 ms |
8148 KB |
Output is correct |
4 |
Correct |
3 ms |
8148 KB |
Output is correct |
5 |
Correct |
3 ms |
8148 KB |
Output is correct |
6 |
Correct |
4 ms |
8148 KB |
Output is correct |
7 |
Correct |
5 ms |
8148 KB |
Output is correct |
8 |
Correct |
4 ms |
8148 KB |
Output is correct |
9 |
Correct |
4 ms |
8144 KB |
Output is correct |
10 |
Correct |
3 ms |
8148 KB |
Output is correct |
11 |
Correct |
5 ms |
8148 KB |
Output is correct |
12 |
Correct |
4 ms |
8148 KB |
Output is correct |
13 |
Correct |
5 ms |
8148 KB |
Output is correct |
14 |
Correct |
4 ms |
8148 KB |
Output is correct |
15 |
Correct |
3 ms |
8148 KB |
Output is correct |
16 |
Correct |
3 ms |
8148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
8148 KB |
Output is correct |
2 |
Correct |
3 ms |
8148 KB |
Output is correct |
3 |
Correct |
4 ms |
8148 KB |
Output is correct |
4 |
Correct |
3 ms |
8148 KB |
Output is correct |
5 |
Correct |
3 ms |
8148 KB |
Output is correct |
6 |
Correct |
4 ms |
8148 KB |
Output is correct |
7 |
Correct |
5 ms |
8148 KB |
Output is correct |
8 |
Correct |
4 ms |
8148 KB |
Output is correct |
9 |
Correct |
4 ms |
8144 KB |
Output is correct |
10 |
Correct |
3 ms |
8148 KB |
Output is correct |
11 |
Correct |
5 ms |
8148 KB |
Output is correct |
12 |
Correct |
4 ms |
8148 KB |
Output is correct |
13 |
Correct |
5 ms |
8148 KB |
Output is correct |
14 |
Correct |
4 ms |
8148 KB |
Output is correct |
15 |
Correct |
3 ms |
8148 KB |
Output is correct |
16 |
Correct |
3 ms |
8148 KB |
Output is correct |
17 |
Correct |
5 ms |
8148 KB |
Output is correct |
18 |
Correct |
18 ms |
8532 KB |
Output is correct |
19 |
Correct |
61 ms |
9172 KB |
Output is correct |
20 |
Correct |
6 ms |
8148 KB |
Output is correct |
21 |
Correct |
4 ms |
8148 KB |
Output is correct |
22 |
Correct |
92 ms |
10120 KB |
Output is correct |
23 |
Correct |
84 ms |
10136 KB |
Output is correct |
24 |
Correct |
5 ms |
8148 KB |
Output is correct |
25 |
Correct |
95 ms |
10128 KB |
Output is correct |
26 |
Correct |
76 ms |
9792 KB |
Output is correct |
27 |
Correct |
31 ms |
8796 KB |
Output is correct |
28 |
Correct |
31 ms |
8788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
8148 KB |
Output is correct |
2 |
Correct |
3 ms |
8148 KB |
Output is correct |
3 |
Correct |
4 ms |
8148 KB |
Output is correct |
4 |
Correct |
3 ms |
8148 KB |
Output is correct |
5 |
Correct |
3 ms |
8148 KB |
Output is correct |
6 |
Correct |
4 ms |
8148 KB |
Output is correct |
7 |
Correct |
5 ms |
8148 KB |
Output is correct |
8 |
Correct |
4 ms |
8148 KB |
Output is correct |
9 |
Correct |
4 ms |
8144 KB |
Output is correct |
10 |
Correct |
3 ms |
8148 KB |
Output is correct |
11 |
Correct |
5 ms |
8148 KB |
Output is correct |
12 |
Correct |
4 ms |
8148 KB |
Output is correct |
13 |
Correct |
5 ms |
8148 KB |
Output is correct |
14 |
Correct |
4 ms |
8148 KB |
Output is correct |
15 |
Correct |
3 ms |
8148 KB |
Output is correct |
16 |
Correct |
3 ms |
8148 KB |
Output is correct |
17 |
Correct |
5 ms |
8148 KB |
Output is correct |
18 |
Correct |
18 ms |
8532 KB |
Output is correct |
19 |
Correct |
61 ms |
9172 KB |
Output is correct |
20 |
Correct |
6 ms |
8148 KB |
Output is correct |
21 |
Correct |
4 ms |
8148 KB |
Output is correct |
22 |
Correct |
92 ms |
10120 KB |
Output is correct |
23 |
Correct |
84 ms |
10136 KB |
Output is correct |
24 |
Correct |
5 ms |
8148 KB |
Output is correct |
25 |
Correct |
95 ms |
10128 KB |
Output is correct |
26 |
Correct |
76 ms |
9792 KB |
Output is correct |
27 |
Correct |
31 ms |
8796 KB |
Output is correct |
28 |
Correct |
31 ms |
8788 KB |
Output is correct |
29 |
Incorrect |
4 ms |
8164 KB |
Output isn't correct |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
8148 KB |
Output is correct |
2 |
Correct |
3 ms |
8148 KB |
Output is correct |
3 |
Correct |
4 ms |
8148 KB |
Output is correct |
4 |
Correct |
3 ms |
8148 KB |
Output is correct |
5 |
Correct |
3 ms |
8148 KB |
Output is correct |
6 |
Correct |
4 ms |
8148 KB |
Output is correct |
7 |
Correct |
5 ms |
8148 KB |
Output is correct |
8 |
Correct |
4 ms |
8148 KB |
Output is correct |
9 |
Correct |
4 ms |
8144 KB |
Output is correct |
10 |
Correct |
3 ms |
8148 KB |
Output is correct |
11 |
Correct |
5 ms |
8148 KB |
Output is correct |
12 |
Correct |
4 ms |
8148 KB |
Output is correct |
13 |
Correct |
5 ms |
8148 KB |
Output is correct |
14 |
Correct |
4 ms |
8148 KB |
Output is correct |
15 |
Correct |
3 ms |
8148 KB |
Output is correct |
16 |
Correct |
3 ms |
8148 KB |
Output is correct |
17 |
Correct |
5 ms |
8148 KB |
Output is correct |
18 |
Correct |
18 ms |
8532 KB |
Output is correct |
19 |
Correct |
61 ms |
9172 KB |
Output is correct |
20 |
Correct |
6 ms |
8148 KB |
Output is correct |
21 |
Correct |
4 ms |
8148 KB |
Output is correct |
22 |
Correct |
92 ms |
10120 KB |
Output is correct |
23 |
Correct |
84 ms |
10136 KB |
Output is correct |
24 |
Correct |
5 ms |
8148 KB |
Output is correct |
25 |
Correct |
95 ms |
10128 KB |
Output is correct |
26 |
Correct |
76 ms |
9792 KB |
Output is correct |
27 |
Correct |
31 ms |
8796 KB |
Output is correct |
28 |
Correct |
31 ms |
8788 KB |
Output is correct |
29 |
Incorrect |
4 ms |
8164 KB |
Output isn't correct |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
8148 KB |
Output is correct |
2 |
Correct |
3 ms |
8148 KB |
Output is correct |
3 |
Correct |
4 ms |
8148 KB |
Output is correct |
4 |
Correct |
3 ms |
8148 KB |
Output is correct |
5 |
Correct |
3 ms |
8148 KB |
Output is correct |
6 |
Correct |
4 ms |
8148 KB |
Output is correct |
7 |
Correct |
5 ms |
8148 KB |
Output is correct |
8 |
Correct |
4 ms |
8148 KB |
Output is correct |
9 |
Correct |
4 ms |
8144 KB |
Output is correct |
10 |
Correct |
3 ms |
8148 KB |
Output is correct |
11 |
Correct |
5 ms |
8148 KB |
Output is correct |
12 |
Correct |
4 ms |
8148 KB |
Output is correct |
13 |
Correct |
5 ms |
8148 KB |
Output is correct |
14 |
Correct |
4 ms |
8148 KB |
Output is correct |
15 |
Correct |
3 ms |
8148 KB |
Output is correct |
16 |
Correct |
3 ms |
8148 KB |
Output is correct |
17 |
Correct |
5 ms |
8148 KB |
Output is correct |
18 |
Correct |
18 ms |
8532 KB |
Output is correct |
19 |
Correct |
61 ms |
9172 KB |
Output is correct |
20 |
Correct |
6 ms |
8148 KB |
Output is correct |
21 |
Correct |
4 ms |
8148 KB |
Output is correct |
22 |
Correct |
92 ms |
10120 KB |
Output is correct |
23 |
Correct |
84 ms |
10136 KB |
Output is correct |
24 |
Correct |
5 ms |
8148 KB |
Output is correct |
25 |
Correct |
95 ms |
10128 KB |
Output is correct |
26 |
Correct |
76 ms |
9792 KB |
Output is correct |
27 |
Correct |
31 ms |
8796 KB |
Output is correct |
28 |
Correct |
31 ms |
8788 KB |
Output is correct |
29 |
Incorrect |
4 ms |
8164 KB |
Output isn't correct |
30 |
Halted |
0 ms |
0 KB |
- |