This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 5e3;
int a[MAX_N + 1], dp[MAX_N + 1][MAX_N + 1];
int main() {
int n, k;
cin >> n >> k;
int countB = 0, countA = 0;
for ( int i = 0; i < 2 * n; i++ ) {
char ch;
cin >> ch;
if ( ch == 'A' )
countA++;
else {
countB++;
a[countB] = countA;
}
}
for ( int i = 1; i <= n; i++ ) {
dp[i][0] = n * n;
for ( int l = 1; l <= k; l++ ) {
dp[i][l] = n * n;
int swapsAdded = 0;
for ( int j = i - 1; j >= 0; j-- ) {
swapsAdded += max( 0, i - a[j + 1] );
dp[i][l] = min( dp[i][l], dp[j][l - 1] + swapsAdded );
}
}
}
cout << dp[n][k];
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |