Submission #882545

#TimeUsernameProblemLanguageResultExecution timeMemory
882545MilosMilutinovicChorus (JOI23_chorus)C++14
16 / 100
7078 ms600 KiB
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  string s;
  cin >> s;
  vector<int> a, b;
  for (int i = 0; i < 2 * n; i++) {
    if (s[i] == 'A') {
      a.push_back(i);
    } else {
      b.push_back(i);
    }
  }
  vector<int> pref(2 * n);
  for (int i = 0; i < 2 * n; i++) {
    if (i > 0) {
      pref[i] = pref[i - 1];
    }
    pref[i] += (s[i] == 'A' ? 1 : 0);
  }
  auto Get = [&](int L, int R) {
    return pref[R] - (L == 0 ? 0 : pref[L - 1]);
  };
  auto Solve = [&](int L, int R) {
    int res = 0;
    for (int i = L; i <= R; i++) {
      if (b[i] < a[R]) {
        res += Get(b[i], a[R]);
      }
    }
    return res;
  }; 
  const int inf = (int) 1e9;
  vector<int> dp(n + 1, inf);
  dp[0] = 0;
  for (int foo = 0; foo < k; foo++) {
    vector<int> new_dp(n + 1, inf);
    for (int i = 0; i < n; i++) {
      for (int j = i; j < n; j++) {
        new_dp[j + 1] = min(new_dp[j + 1], dp[i] + Solve(i, j));
      }
    }
    swap(dp, new_dp);
  }
  cout << dp[n] << '\n';
  return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...