Problem p03190 (C++)

Buggy submission (status: Runtime Error):
// BadWaper gg
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define inf 1e9
#define eps 1e-6
#define mp make_pair
#define N 5010
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
inline ll read() {
  char ch = getchar();
  ll s = 0, w = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    s = s * 10 + ch - '0';
    ch = getchar();
  }
  return s * w;
}
int a[N], b[N];
char s[N], t[N];
int n;
inline int calc() {
  int t1 = 0, t2 = 0;
  for (register int i = 1; i < n; i++)
    if (s[i] == '0' && s[i + 1] == '1')
      a[++t1] = i;
  for (register int i = 1; i <= n; i++)
    b[++t2] = 0;
  for (register int i = 1; i < n; i++)
    if (t[i] == '0' && t[i + 1] == '1')
      b[++t2] = i;
  for (register int i = 1; i <= n; i++)
    b[++t2] = n;
  int ans = inf;
  for (register int i = 1; i <= t2 - t1 + 1; i++) {
    int sum = 0;
    for (register int j = 1; j < i; j++)
      sum += b[j];
    for (register int j = 1; j <= t1; j++)
      sum += abs(a[j] - b[i + j - 1]);
    for (register int j = i + t1; j <= t2; j++)
      sum += n - b[j];
    ans = min(ans, sum);
  }
  return ans;
}
int main() {
  // freopen(".in","r",stdin);
  // freopen(".out","w",stdout);
  n = read();
  scanf("%s%s", s + 1, t + 1);
  if (n <= 2) {
    int ans = 0;
    for (register int i = 1; i <= n; i++)
      ans += (s[i] != t[i]);
    printf("%d\n", ans);
    return 0;
  }
  int ans = calc();
  for (register int i = 1; i <= n; i++)
    s[i] = '1' + '0' - s[i];
  for (register int i = 1; i <= n; i++)
    t[i] = '1' + '0' - t[i];
  ans = ans + calc();
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
// BadWaper gg
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define inf 1e9
#define eps 1e-6
#define mp make_pair
#define N 20010
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
inline ll read() {
  char ch = getchar();
  ll s = 0, w = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    s = s * 10 + ch - '0';
    ch = getchar();
  }
  return s * w;
}
int a[N], b[N];
char s[N], t[N];
int n;
inline int calc() {
  int t1 = 0, t2 = 0;
  for (register int i = 1; i < n; i++)
    if (s[i] == '0' && s[i + 1] == '1')
      a[++t1] = i;
  for (register int i = 1; i <= n; i++)
    b[++t2] = 0;
  for (register int i = 1; i < n; i++)
    if (t[i] == '0' && t[i + 1] == '1')
      b[++t2] = i;
  for (register int i = 1; i <= n; i++)
    b[++t2] = n;
  int ans = inf;
  for (register int i = 1; i <= t2 - t1 + 1; i++) {
    int sum = 0;
    for (register int j = 1; j < i; j++)
      sum += b[j];
    for (register int j = 1; j <= t1; j++)
      sum += abs(a[j] - b[i + j - 1]);
    for (register int j = i + t1; j <= t2; j++)
      sum += n - b[j];
    ans = min(ans, sum);
  }
  return ans;
}
int main() {
  // freopen(".in","r",stdin);
  // freopen(".out","w",stdout);
  n = read();
  scanf("%s%s", s + 1, t + 1);
  if (n <= 2) {
    int ans = 0;
    for (register int i = 1; i <= n; i++)
      ans += (s[i] != t[i]);
    printf("%d\n", ans);
    return 0;
  }
  int ans = calc();
  for (register int i = 1; i <= n; i++)
    s[i] = '1' + '0' - s[i];
  for (register int i = 1; i <= n; i++)
    t[i] = '1' + '0' - t[i];
  ans = ans + calc();
  printf("%d\n", ans);
  return 0;
}