#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long double ld;
typedef pair<ll, ll> pll;
#define FOR(i, a, b) for(int i = a; i < b; i++)
#define ROF(i, a, b) for(int i = a; i >= b; i--)
#define ms memset
#define pb push_back
#define fi first
#define se second
void solve(){
int n,k; cin>>n>>k;
string s; cin>>s;
vi A(n+1); int a=1, b=0;
FOR(i,0,2*n){
if (s[i]=='B') b++;
else A[a++]=b;
}
vector<ll> S(n+1); FOR(i,1,n+1) S[i]=S[i-1]+A[i];
ll dp[n+1][k+1]; ms(dp,0,sizeof(dp));
FOR(i,1,n+1) dp[i][1]=S[i];
FOR(j,2,k+1){
FOR(i,1,n+1){
dp[i][j]=dp[i][j-1];
int cur=i;
FOR(l,1,i+1){
while (cur>i-l && A[cur]>i-l) cur--;
if (cur==i) dp[i][j]=min(dp[i][j],dp[i-l][j-1]);
else dp[i][j]=min(dp[i][j], dp[i-l][j-1] + S[i]-S[cur]-(ll)(i-cur)*(ll)(i-l));
}
}
}
cout<<dp[n][k]<<"\n";
}
int main(){
//MOD=MOD1;
ios::sync_with_stdio(false);
if (fopen("input.txt", "r")) {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
}
int TC = 1;
//cin >> TC;
FOR(i, 1, TC+1){
//cout << "Case #" << i << ": ";
solve();
}
return 0;
}
Compilation message
chorus.cpp: In function 'int main()':
chorus.cpp:45:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
45 | freopen("input.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
chorus.cpp:46:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
46 | freopen("output.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
236 KB |
Output is correct |
9 |
Correct |
0 ms |
316 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
316 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
236 KB |
Output is correct |
9 |
Correct |
0 ms |
316 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
316 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
3 ms |
316 KB |
Output is correct |
18 |
Correct |
40 ms |
668 KB |
Output is correct |
19 |
Correct |
120 ms |
1376 KB |
Output is correct |
20 |
Correct |
1 ms |
388 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
157 ms |
2268 KB |
Output is correct |
23 |
Correct |
160 ms |
2272 KB |
Output is correct |
24 |
Correct |
5 ms |
340 KB |
Output is correct |
25 |
Correct |
166 ms |
2272 KB |
Output is correct |
26 |
Correct |
127 ms |
1876 KB |
Output is correct |
27 |
Correct |
58 ms |
980 KB |
Output is correct |
28 |
Correct |
54 ms |
980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
236 KB |
Output is correct |
9 |
Correct |
0 ms |
316 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
316 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
3 ms |
316 KB |
Output is correct |
18 |
Correct |
40 ms |
668 KB |
Output is correct |
19 |
Correct |
120 ms |
1376 KB |
Output is correct |
20 |
Correct |
1 ms |
388 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
157 ms |
2268 KB |
Output is correct |
23 |
Correct |
160 ms |
2272 KB |
Output is correct |
24 |
Correct |
5 ms |
340 KB |
Output is correct |
25 |
Correct |
166 ms |
2272 KB |
Output is correct |
26 |
Correct |
127 ms |
1876 KB |
Output is correct |
27 |
Correct |
58 ms |
980 KB |
Output is correct |
28 |
Correct |
54 ms |
980 KB |
Output is correct |
29 |
Execution timed out |
7021 ms |
79284 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
236 KB |
Output is correct |
9 |
Correct |
0 ms |
316 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
316 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
3 ms |
316 KB |
Output is correct |
18 |
Correct |
40 ms |
668 KB |
Output is correct |
19 |
Correct |
120 ms |
1376 KB |
Output is correct |
20 |
Correct |
1 ms |
388 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
157 ms |
2268 KB |
Output is correct |
23 |
Correct |
160 ms |
2272 KB |
Output is correct |
24 |
Correct |
5 ms |
340 KB |
Output is correct |
25 |
Correct |
166 ms |
2272 KB |
Output is correct |
26 |
Correct |
127 ms |
1876 KB |
Output is correct |
27 |
Correct |
58 ms |
980 KB |
Output is correct |
28 |
Correct |
54 ms |
980 KB |
Output is correct |
29 |
Execution timed out |
7021 ms |
79284 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
236 KB |
Output is correct |
9 |
Correct |
0 ms |
316 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
316 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
3 ms |
316 KB |
Output is correct |
18 |
Correct |
40 ms |
668 KB |
Output is correct |
19 |
Correct |
120 ms |
1376 KB |
Output is correct |
20 |
Correct |
1 ms |
388 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
157 ms |
2268 KB |
Output is correct |
23 |
Correct |
160 ms |
2272 KB |
Output is correct |
24 |
Correct |
5 ms |
340 KB |
Output is correct |
25 |
Correct |
166 ms |
2272 KB |
Output is correct |
26 |
Correct |
127 ms |
1876 KB |
Output is correct |
27 |
Correct |
58 ms |
980 KB |
Output is correct |
28 |
Correct |
54 ms |
980 KB |
Output is correct |
29 |
Execution timed out |
7021 ms |
79284 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |