fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. ios::sync_with_stdio(false);
  6. cin.tie(nullptr);
  7.  
  8. long long c1, c2;
  9. string s;
  10. cin >> c1 >> c2 >> s;
  11.  
  12. int n = s.size();
  13.  
  14. // pre[i] = number of adjacent equal pairs in first i characters
  15. vector<int> pre(n + 1, 0);
  16. for (int i = 0; i < n - 1; i++) {
  17. pre[i + 1] = pre[i];
  18. if (s[i] == s[i + 1]) {
  19. pre[i + 1]++;
  20. }
  21. }
  22. pre[n] = pre[n - 1];
  23.  
  24. // dp[i] = min cost to handle first i characters
  25. const long long INF = 1e18;
  26. vector<long long> dp(n + 1, INF);
  27. dp[0] = 0;
  28.  
  29. // Try every position
  30. for (int i = 1; i <= n; i++) {
  31. // Try every possible start for last piece
  32. for (int j = 0; j < i; j++) {
  33. // Count pairs in range [j, i)
  34. int cnt = pre[i] - pre[j];
  35.  
  36. // Cost of pairs in this piece
  37. long long cost = (long long)cnt * c1;
  38.  
  39. // Add split cost if not first piece
  40. if (j > 0) cost += c2;
  41.  
  42. // Update answer
  43. dp[i] = min(dp[i], dp[j] + cost);
  44. }
  45. }
  46.  
  47. cout << dp[n] << "\n";
  48.  
  49. return 0;
  50. }
Success #stdin #stdout 0.01s 5272KB
stdin
Standard input is empty
stdout
0