Advertisement
jbn6972

icc

Oct 24th, 2021
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ll long long
  5. #define vi vector<int>
  6. #define vll vector<long long int>
  7.  
  8.  
  9. int main()
  10. {
  11. #ifndef ONLINE_JUDGE
  12. freopen("in.txt","r",stdin);
  13. freopen("out.txt","w",stdout);
  14. #endif
  15.  
  16. std::ios::sync_with_stdio(false);
  17.  
  18. int t;
  19. cin >> t;
  20.  
  21. while (t--)
  22. {
  23.  
  24. int n, k;
  25. cin >> n >> k;
  26.  
  27. string s;
  28. cin >> s;
  29.  
  30. vector<int> Left_counter(n), Right_counter(n);
  31. int cnt = 0, cnt1 = 0;
  32. bool counter_present = false;
  33. for (int i = 0; i < n; ++i)
  34. {
  35.  
  36. if (s[i] == '1')
  37. {
  38. counter_present = true;
  39. cnt = 0;
  40. Left_counter[i] = 0;
  41. }
  42. else if (counter_present)
  43. {
  44. cnt++;
  45. Left_counter[i] = cnt;
  46. }
  47. else
  48. Left_counter[i] = INT_MAX;
  49. }
  50.  
  51. counter_present = false;
  52. for (int i = n - 1; i >= 0; --i)
  53. {
  54. if (s[i] == '1')
  55. {
  56. counter_present = true;
  57. cnt = 0;
  58. Right_counter[i] = 0;
  59. }
  60. else if (counter_present)
  61. {
  62. cnt++;
  63. Right_counter[i] = cnt;
  64. }
  65. else
  66. Right_counter[i] = INT_MAX;
  67. }
  68.  
  69. int cost = 0;
  70. for (int i = 0; i < n; ++i)
  71. {
  72. if (s[i] != '1')
  73. {
  74. cost += min(Left_counter[i], Right_counter[i]);
  75. }
  76. cost += k;
  77. }
  78.  
  79. cout << cost << endl;
  80. }
  81.  
  82. return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement