Problem p03190 (C++)

Buggy submission (status: Runtime Error):
/*

  _|_|                              _|  _|    _|
_|    _|  _|  _|_|  _|_|_|_|        _|  _|  _|
_|    _|  _|_|          _|          _|  _|_|
_|    _|  _|          _|      _|    _|  _|  _|
  _|_|    _|        _|_|_|_|    _|_|    _|    _|

*/
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
// #define ls (rt<<1)
// #define rs (rt<<1|1)
#define vi vector<int>
#define pb push_back
#define mk make_pair
#define pii pair<int, int>
#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; i++)
#define fi first
#define se second
typedef long long ll;
using namespace std;
const int maxn = 5005;
char s[maxn], t[maxn];
vi marks, markt;
int a[maxn], b[maxn];
int main() {
  int n;
  scanf("%d%s%s", &n, s + 1, t + 1);
  if (s[1] == '1')
    marks.pb(0);
  rep(i, 1, n - 1) if (s[i] != s[i + 1]) marks.pb(i);
  if (s[n] == '1')
    marks.pb(n);
  if (t[1] == '1')
    markt.pb(0);
  rep(i, 1, n - 1) if (t[i] != t[i + 1]) markt.pb(i);
  if (t[n] == '1')
    markt.pb(n);
  int cnts = marks.size(), cntt = markt.size(), add = cntt - cnts;
  int ans = 1e9 + 100;
  rep(i, 1, cnts) a[i + cntt] = marks[i - 1];
  rep(i, 1, cntt) a[i + cnts + cntt] = n;
  for (int i = 0; i <= cnts + cntt; i += 2) {
    int tmp = 0;
    rep(j, 1, i) b[j] = 0;
    // cout<<markt[0]<<endl;
    rep(j, 1, cntt) b[i + j] = markt[j - 1];
    rep(j, 1, cnts + cntt - i) b[j + i + cntt] = n;
    rep(j, 1, cnts + cntt * 2) tmp += abs(a[j] - b[j]);
    ans = min(ans, tmp);
  }
  /*   rep(i,-cnts,cnts+add){//从开头通过的
         int tmp=0;
         int but=add-i;//从结尾通过的
         if(i<0){
             rep(j,1,-i)tmp+=abs(s[i-1]-0);
             rep(j,-i+1,cnts)tmp+=abs(s[i-1]-t[j+i-1]);
             rep(j,cnts+i+1,cnts)tmp+=abs(n-t[j-1]);
         }
         else{
             rep(j,cnts+but+1,cnts)tmp+=abs(s[i-1]-n);
             for(int j=cnts+but;j>=i;j--)tmp+=abs(s[i-1]-t[i-1]);
             rep(j,1,i)tmp+=t[i];
         }
         ans=min(ans,tmp);
         cout<<tmp<<endl;
     }*/
  cout << ans;
  return 0;
}

Fixed submission:
/*

  _|_|                              _|  _|    _|
_|    _|  _|  _|_|  _|_|_|_|        _|  _|  _|
_|    _|  _|_|          _|          _|  _|_|
_|    _|  _|          _|      _|    _|  _|  _|
  _|_|    _|        _|_|_|_|    _|_|    _|    _|

*/
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
// #define ls (rt<<1)
// #define rs (rt<<1|1)
#define vi vector<int>
#define pb push_back
#define mk make_pair
#define pii pair<int, int>
#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; i++)
#define fi first
#define se second
typedef long long ll;
using namespace std;
const int maxn = 3 * 5005;
char s[maxn], t[maxn];
vi marks, markt;
int a[maxn], b[maxn];
int main() {
  int n;
  scanf("%d%s%s", &n, s + 1, t + 1);
  if (s[1] == '1')
    marks.pb(0);
  rep(i, 1, n - 1) if (s[i] != s[i + 1]) marks.pb(i);
  if (s[n] == '1')
    marks.pb(n);
  if (t[1] == '1')
    markt.pb(0);
  rep(i, 1, n - 1) if (t[i] != t[i + 1]) markt.pb(i);
  if (t[n] == '1')
    markt.pb(n);
  int cnts = marks.size(), cntt = markt.size(), add = cntt - cnts;
  int ans = 1e9 + 100;
  rep(i, 1, cnts) a[i + cntt] = marks[i - 1];
  rep(i, 1, cntt) a[i + cnts + cntt] = n;
  for (int i = 0; i <= cnts + cntt; i += 2) {
    int tmp = 0;
    rep(j, 1, i) b[j] = 0;
    // cout<<markt[0]<<endl;
    rep(j, 1, cntt) b[i + j] = markt[j - 1];
    rep(j, 1, cnts + cntt - i) b[j + i + cntt] = n;
    rep(j, 1, cnts + cntt * 2) tmp += abs(a[j] - b[j]);
    ans = min(ans, tmp);
  }
  /*   rep(i,-cnts,cnts+add){//从开头通过的
         int tmp=0;
         int but=add-i;//从结尾通过的
         if(i<0){
             rep(j,1,-i)tmp+=abs(s[i-1]-0);
             rep(j,-i+1,cnts)tmp+=abs(s[i-1]-t[j+i-1]);
             rep(j,cnts+i+1,cnts)tmp+=abs(n-t[j-1]);
         }
         else{
             rep(j,cnts+but+1,cnts)tmp+=abs(s[i-1]-n);
             for(int j=cnts+but;j>=i;j--)tmp+=abs(s[i-1]-t[i-1]);
             rep(j,1,i)tmp+=t[i];
         }
         ans=min(ans,tmp);
         cout<<tmp<<endl;
     }*/
  cout << ans;
  return 0;
}