fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. string P;
  6.  
  7. int count_B;
  8.  
  9. int A,B;
  10.  
  11. int Bpos[5011];
  12.  
  13. int main() {
  14.  
  15. while (scanf("%d %d",&A,&B)!=EOF){
  16.  
  17. memset(Bpos,-1,sizeof(Bpos));
  18.  
  19. int idx=0;
  20. count_B=0;
  21. cin>> P;
  22.  
  23. for (int i=0;i<P.size();++i){
  24. if (P[i]=='B'){
  25. count_B++;
  26. Bpos[idx]=i;
  27. idx++;
  28. }
  29. }
  30. string aux;
  31. int minimo=0x3f3f3f3f;
  32. for (int i=0;i<=count_B;++i){
  33. int Arot=i;
  34.  
  35. int cata=idx-1;
  36.  
  37. int costo=0;
  38.  
  39. for (int j=0;j<count_B;++j){
  40.  
  41. if (P[j]=='B'){
  42. if (Bpos[cata]==j)
  43. cata--;
  44. if (cata<0) break;
  45. }
  46. else{
  47.  
  48. if (Arot){
  49. Arot--;
  50. costo+=A;
  51.  
  52. cata--;
  53. }else{
  54. costo+=((A-B)*( abs(Bpos[cata]-j ) ));
  55.  
  56.  
  57. cata--;
  58. if (cata<0) break;
  59. }
  60. }
  61.  
  62. }
  63.  
  64. minimo=min(minimo, costo);
  65.  
  66. }
  67.  
  68. printf("%d\n",minimo);
  69. }
  70.  
  71.  
  72.  
  73.  
  74. return 0;
  75. }
Success #stdin #stdout 0s 3360KB
stdin
Standard input is empty
stdout
Standard output is empty