Problem p00924 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
using namespace std;

#define ll long long

string frm, to;
ll a, b;
char ch;
ll n;

int main() {
  ll n;
  cin >> a >> b;
  ll ones = 0, zeros = 0;
  for (int i = 0; i < a; i++)
    cin >> n, frm += (n + '0'), ones += (frm[i] == '1'),
        zeros += (frm[i] == '0');
  to = "";
  ll one = 0, zero = 0;
  for (int i = 0; i < b; i++) {
    cin >> n;
    while (n--) {
      if (i % 2 == 0)
        to += '0', zero++;
      else
        to += '1', one++;
    }
  }
  string newto = "null";

  if (zero != zeros or one != ones) {
    for (int i = 0; i < to.size(); i++)
      to[i] = (!(to[i] - '0')) + '0';
  } else if (zero == one) {
    newto = "";
    for (int i = 0; i < to.size(); i++)
      newto += (!(to[i] - '0')) + '0';
  }
  ll res = 0, ans = 0;

  //    cout << frm << endl;
  //    cout << to << " " << newto << endl;

  string newfrm = frm;

  for (int i = 0; i < frm.size(); i++) {
    int tmp = i;
    if (frm[i] == '0' && to[i] == '1') {
      int cnt = 0;
      while (frm[i] == '0' && i < frm.size())
        cnt++, i++;
      swap(frm[i], frm[i - cnt]);
      res += cnt;
    } else if (frm[i] == '1' && to[i] == '0') {
      int cnt = 0;
      while (frm[i] == '1' && i < frm.size())
        cnt++, i++;
      swap(frm[i], frm[i - cnt]);
      //  cout << res << " " << cnt << endl;
      res += cnt;
    }
    i = tmp;
  }

  ans = res;

  if (newto != "null") {
    res = 0;
    for (int i = 0; i < newfrm.size(); i++) {
      int tmp = i;
      if (newfrm[i] == '0' && newto[i] == '1') {
        int cnt = 0;
        while (newfrm[i] == '0' && i < newfrm.size())
          cnt++, i++;
        swap(newfrm[i], newfrm[i - cnt]);
        res += cnt;
      } else if (newfrm[i] == '1' && newto[i] == '0') {
        int cnt = 0;
        while (newfrm[i] == '1' && i < frm.size())
          cnt++, i++;
        swap(newfrm[i], newfrm[i - cnt]);
        //  cout << res << " " << cnt << endl;
        res += cnt;
      }
      i = tmp;
    }

    ans = min(ans, res);
  }

  cout << ans << endl;

  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

const int inf = 0x3f3f3f3f;
const int maxn = (int)1e6 + 7;
const double eps = 1e-10;
const ll mod = (int)1e9 + 7;

#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#define clr(x) memset(x, 0, sizeof(x))
#define pi 3.1415926

int n, m;
int a[maxn], b1[maxn], b2[maxn], b[maxn];

int check(int a[], int b[]) {
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] == b[i]) {
      continue;
    } else {
      ans++;
      int op = i + 1;
      while (a[i] != b[op]) {
        op++;
        ans++;
      }
      swap(b[i], b[op]);
    }
  }
  return ans;
}

int main() {
  std::ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
#endif

  while (cin >> n >> m) {
    int sum11 = 0;
    int sum12 = 0;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      if (a[i] == 1) {
        sum11++;
      } else {
        sum12++;
      }
    }

    for (int i = 0; i < m; i++) {
      cin >> b[i];
    }

    int q = 0;
    int tot = 0;
    int sum1 = 0;
    int sum2 = 0;
    for (int i = 0; i < m; i++) {
      q = (q + 1) % 2;
      for (int j = 0; j < b[i]; j++) {
        b1[tot] = q;
        if (q == 1) {
          sum1++;
        } else {
          sum2++;
        }
        b2[tot++] = (q + 1) % 2;
      }
    }

    //        for(int i = 0;i < n;i++) cout << b1[i] << " ";
    //        cout << endl;
    //        for(int i = 0;i < n;i++) cout << b2[i] << " ";
    //        cout << endl;

    int ans1 = 0, ans2 = 0;

    if (sum11 == sum1)
      ans1 = check(a, b1);
    else
      ans1 = inf;

    if (sum11 == sum2)
      ans2 = check(a, b2);
    else
      ans2 = inf;

    cout << min(ans1, ans2) << endl;
  }

  return 0;
}