#define ll long long
#define INF numeric_limits<ll>::max()/2
#include <bits/stdc++.h>
using namespace std;
ll sum(ll l, ll r, vector<ll>& pref)
{
return pref[r] - (l == 0 ? 0 : pref[l-1]);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
ll n, k;
cin>>n>>k;
string s;
cin>>s;
vector<ll> a(0);
for(ll i = 0; i < 2*n; i++)
{
if(s[i] == 'A')
{
a.push_back(i+1);
}
}
ll output = 0;
for(ll i = 0; i < n; i++)
{
if(a[i] > 2*i+1)
{
output += a[i] - (2*i+1);
a[i] = 2*i+1;
}
}
vector<ll> pref(n);
pref[0] = a[0];
for(ll i = 1; i < n; i++)
{
pref[i] = a[i];
}
vector<vector<ll>> dp(n+1, vector<ll> (k+1, INF)); //n x 'A', k songs => Min operations
dp[0][0] = 0;
for(ll i = 0; i < n; i++)
{
for(ll j = 0; j < k; j++)
{
ll goal = 2*i+1;
ll cost = 0;
for(ll c = i+1; c <= n; c++)
{
if(a[c-1] > goal)
{
cost += a[c-1]-goal;
}
goal++;
dp[c][j+1] = min(dp[c][j+1], dp[i][j]+cost);
}
}
}
/*vector<vector<ll>> dp(n+1, vector<ll> (k+1, INF)); //n x 'A', k songs => Min operations
dp[0][0] = 0;
for(ll i = 1; i <= n; i++)
{
for(ll j = 1; j <= k; j++)
{
for(ll c = 0; c < i; c++)
{
ll cost = 0;
ll goal = 2*c+1;
for(ll d = c; d < i; d++)
{
if(a[d] > goal)
{
cost += a[d]-goal;
}
goal++;
}
dp[i][j] = min(dp[i][j], dp[c][j-1]+cost);
}
}
}*/
ll res = *min_element(dp[n].begin(), dp[n].end());
cout<<res+output<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
320 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
320 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
2 ms |
316 KB |
Output is correct |
18 |
Correct |
22 ms |
596 KB |
Output is correct |
19 |
Correct |
64 ms |
1412 KB |
Output is correct |
20 |
Correct |
1 ms |
316 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
123 ms |
2292 KB |
Output is correct |
23 |
Correct |
126 ms |
2296 KB |
Output is correct |
24 |
Correct |
4 ms |
340 KB |
Output is correct |
25 |
Correct |
143 ms |
2296 KB |
Output is correct |
26 |
Correct |
101 ms |
1972 KB |
Output is correct |
27 |
Correct |
38 ms |
980 KB |
Output is correct |
28 |
Correct |
38 ms |
992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
320 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
2 ms |
316 KB |
Output is correct |
18 |
Correct |
22 ms |
596 KB |
Output is correct |
19 |
Correct |
64 ms |
1412 KB |
Output is correct |
20 |
Correct |
1 ms |
316 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
123 ms |
2292 KB |
Output is correct |
23 |
Correct |
126 ms |
2296 KB |
Output is correct |
24 |
Correct |
4 ms |
340 KB |
Output is correct |
25 |
Correct |
143 ms |
2296 KB |
Output is correct |
26 |
Correct |
101 ms |
1972 KB |
Output is correct |
27 |
Correct |
38 ms |
980 KB |
Output is correct |
28 |
Correct |
38 ms |
992 KB |
Output is correct |
29 |
Execution timed out |
7007 ms |
79488 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
320 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
2 ms |
316 KB |
Output is correct |
18 |
Correct |
22 ms |
596 KB |
Output is correct |
19 |
Correct |
64 ms |
1412 KB |
Output is correct |
20 |
Correct |
1 ms |
316 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
123 ms |
2292 KB |
Output is correct |
23 |
Correct |
126 ms |
2296 KB |
Output is correct |
24 |
Correct |
4 ms |
340 KB |
Output is correct |
25 |
Correct |
143 ms |
2296 KB |
Output is correct |
26 |
Correct |
101 ms |
1972 KB |
Output is correct |
27 |
Correct |
38 ms |
980 KB |
Output is correct |
28 |
Correct |
38 ms |
992 KB |
Output is correct |
29 |
Execution timed out |
7007 ms |
79488 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
320 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
2 ms |
316 KB |
Output is correct |
18 |
Correct |
22 ms |
596 KB |
Output is correct |
19 |
Correct |
64 ms |
1412 KB |
Output is correct |
20 |
Correct |
1 ms |
316 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
123 ms |
2292 KB |
Output is correct |
23 |
Correct |
126 ms |
2296 KB |
Output is correct |
24 |
Correct |
4 ms |
340 KB |
Output is correct |
25 |
Correct |
143 ms |
2296 KB |
Output is correct |
26 |
Correct |
101 ms |
1972 KB |
Output is correct |
27 |
Correct |
38 ms |
980 KB |
Output is correct |
28 |
Correct |
38 ms |
992 KB |
Output is correct |
29 |
Execution timed out |
7007 ms |
79488 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |