Saleh127

CF 1443B

Nov 2nd, 2020
63
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define test int t; cin>>t; for(int cs=1;cs<=t;cs++)
  5. int main()
  6. {
  7. ios_base::sync_with_stdio(0);
  8. cin.tie(0);
  9. cout.tie(0);
  10.  
  11. test
  12. {
  13.  
  14. ll a,b,d,e,f,i,j,k=-1,l=-1,v;
  15.  
  16. string c;
  17. cin>>a>>b;
  18. cin>>c;
  19.  
  20. for(i=0; i<c.size(); i++)
  21. {
  22. if(c[i]=='1')
  23. {
  24. k=i;
  25. break;
  26. }
  27. }
  28.  
  29. for(i=c.size()-1; i>=0; i--)
  30. {
  31. if(c[i]=='1')
  32. {
  33. l=i;
  34. break;
  35. }
  36. }
  37. if(k==-1&&l==-1)
  38. {
  39. cout<<0<<endl;
  40. continue;
  41. }
  42. e=0;
  43. f=0;
  44. d=a;
  45. for(i=k; i<=l; i++)
  46. {
  47. if(c[i]=='0')
  48. {
  49. e++;
  50. }
  51. else if(c[i]=='1' && e!=0)
  52. {
  53. d+=min(a,e*b);
  54. e=0;
  55. }
  56. }
  57. cout<<d<<endl;
  58. }
  59.  
  60.  
  61. return 0;
  62. }
  63.  
RAW Paste Data Copied