Advertisement
jbn6972

cpp

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