Problem p03620 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
using uint = unsigned int;
using ll = long long;
const int M = 1e9 + 7;
const ll MLL = 1e18L + 9;
#pragma unused(M)
#pragma unused(MLL)
#ifdef LOCAL
#include "rprint.hpp"
#else
template <ostream &out = cout, class... T> void prints(T &&...) {}
template <ostream &out = cout, class... T> void printd(T &&...) {}
template <ostream &out = cout, class... T> void printde(T &&...) {}
template <ostream &out = cout, class... T> void printb(T &&...) {}
template <ostream &out = cout, class... T> void printArr(T &&...) {}
#endif

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string a, b;
  cin >> a >> b;
  ll n = a.size(), ans = M;
  vector<ll> l1(n, M), r1(n, M);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (b[(i - j + n) % n] == '1') {
        l1[i] = j;
        break;
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (b[(i + j + n) % n] == '1') {
        r1[i] = j;
        break;
      }
    }
  }
  prints(l1, r1);
  string a2;
  a2.resize(n);
  for (int h = 0; h < 2; h++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        a2[j] = a[(j - i * (h == 0 ? 1 : -1) + n) % n];
      }
      ll cnt = 0, add = 0;
      vector<pair<ll, ll>> adds;
      for (int j = 0; j < n; j++) {
        if (a2[j] != b[j]) {
          // add = max(add, min(((h == 0 ? l1 : r1)[j] - i) * 2, (h == 0 ? r1 :
          // l1)[j] * 2));
          adds.emplace_back(max(0ll, (h == 0 ? l1 : r1)[j] - i) * 2,
                            max(0ll, (h == 0 ? r1 : l1)[j]) * 2);
          cnt++;
        }
      }
      sort(adds.begin(), adds.end());
      int n2 = adds.size();
      vector<ll> maxs(n2 + 1);
      maxs[n2 - 1] = adds[n2 - 1].second;
      for (int j = n2 - 2; j >= 0; j--) {
        maxs[j] = max(maxs[j + 1], adds[j].second);
      }
      prints(adds, maxs);
      add = adds[0].first + maxs[1];
      for (int j = 1; j < n2; j++) {
        add = min(add, adds[j].first + maxs[j + 1]);
      }
      ans = min(ans, cnt + i + add);
      prints(i, cnt + i + add, add);
    }
  }
  cout << (ans >= n * 3 ? -1 : ans) << '\n';
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
using uint = unsigned int;
using ll = long long;
const int M = 1e9 + 7;
const ll MLL = 1e18L + 9;
#pragma unused(M)
#pragma unused(MLL)
#ifdef LOCAL
#include "rprint.hpp"
#else
template <ostream &out = cout, class... T> void prints(T &&...) {}
template <ostream &out = cout, class... T> void printd(T &&...) {}
template <ostream &out = cout, class... T> void printde(T &&...) {}
template <ostream &out = cout, class... T> void printb(T &&...) {}
template <ostream &out = cout, class... T> void printArr(T &&...) {}
#endif

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string a, b;
  cin >> a >> b;
  ll n = a.size(), ans = M;
  vector<ll> l1(n, M), r1(n, M);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (b[(i - j + n) % n] == '1') {
        l1[i] = j;
        break;
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (b[(i + j + n) % n] == '1') {
        r1[i] = j;
        break;
      }
    }
  }
  prints(l1, r1);
  string a2;
  a2.resize(n);
  for (int h = 0; h < 2; h++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        a2[j] = a[(j - i * (h == 0 ? 1 : -1) + n) % n];
      }
      ll cnt = 0, add = 0;
      vector<pair<ll, ll>> adds;
      for (int j = 0; j < n; j++) {
        if (a2[j] != b[j]) {
          // add = max(add, min(((h == 0 ? l1 : r1)[j] - i) * 2, (h == 0 ? r1 :
          // l1)[j] * 2));
          adds.emplace_back(max(0ll, (h == 0 ? l1 : r1)[j] - i) * 2,
                            max(0ll, (h == 0 ? r1 : l1)[j]) * 2);
          cnt++;
        }
      }
      sort(adds.begin(), adds.end());
      int n2 = adds.size();
      if (n2) {
        vector<ll> maxs(n2 + 1);
        maxs[n2 - 1] = adds[n2 - 1].second;
        for (int j = n2 - 2; j >= 0; j--) {
          maxs[j] = max(maxs[j + 1], adds[j].second);
        }
        prints(adds, maxs);
        add = adds[0].first + maxs[1];
        for (int j = 1; j < n2; j++) {
          add = min(add, adds[j].first + maxs[j + 1]);
        }
      }
      ans = min(ans, cnt + i + add);
      prints(i, cnt + i + add, add);
    }
  }
  cout << (ans >= n * 3 ? -1 : ans) << '\n';
  return 0;
}