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

#include <bits/stdc++.h>
#define meow(args...) fprintf(stderr, args)
typedef unsigned u32;
typedef long long s64;
typedef unsigned long long u64;
template <class T1, class T2> inline bool cmin(T1 &a, const T2 &b) {
  return b < a ? (a = b, true) : false;
}
template <class T1, class T2> inline bool cmax(T1 &a, const T2 &b) {
  return a < b ? (a = b, true) : false;
}
template <class Type> Type read() {
  Type a;
  bool b;
  unsigned char c;
  while (c = getchar() - 48, (c > 9) & (c != 253))
    ;
  for (a = (b = c == 253) ? 0 : c; (c = getchar() - 48) <= 9; a = a * 10 + c)
    ;
  return b ? -a : a;
}
int (*rd)() = read<int>;
// const u32 P=;
// inline u32 &inc(u32 &a, u32 b) {return (a+=b)<P?a:(a-=P);}
// inline u32 &dec(u32 &a, u32 b) {return (a-=b)&0x80000000?(a+=P):a;}
// inline u32 sum(u32 a, u32 b) {return (a+=b)<P?a:a-P;}
// inline u32 dif(u32 a, u32 b) {return (a-=b)&0x80000000?a+P:a;}
// u64 power(u64 a, int b) {
//	u64 ans=1;
//	for(; b; a=a*a%P, b/=2) if(b&1) ans=ans*a%P;
//	return ans;
// }

const int N = 2e5 + 1;
char t[N];
int main() {
  assert(scanf("%s", t) == 1);
  int n = strlen(t), n1 = std::count(t, t + n, '1');
  long long ans = 0;
  for (int i = 1; i <= n1; ++i)
    ans += (i + 1) / 2;
  int len = 0, pair = 0;
  for (int i = 0; i < n; ++i) {
    if (t[i] == '0')
      t[len++] = '0';
    else if (i + 1 == n || t[i + 1] == '0')
      t[len++] = '1';
    else {
      ++pair;
      ++i;
    }
  }
  int no = 0, ne = 0;
  for (int i = 0; i < len; i += 2)
    no += t[i] - '0';
  for (int i = 1; i < len; i += 2)
    ne += t[i] - '0';
  n1 = n = 0;
  for (int i = 0; i < len; ++i) {
    if (t[i] == '1') {
      if (n & 1) {
        ans += pair + n1 + no;
        std::swap(no, ne);
        --n;
      }
      ++n1;
      --no;
    }
    t[n++] = t[i];
  }
  no = ne = 0;
  for (int i = n; i--;) {
    if (t[i] == '1')
      ++no;
    else {
      ans += pair + n1 - ne;
      std::swap(no, ne);
    }
  }
  printf("%lld\\n", ans);
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define meow(args...) fprintf(stderr, args)
typedef unsigned u32;
typedef long long s64;
typedef unsigned long long u64;
template <class T1, class T2> inline bool cmin(T1 &a, const T2 &b) {
  return b < a ? (a = b, true) : false;
}
template <class T1, class T2> inline bool cmax(T1 &a, const T2 &b) {
  return a < b ? (a = b, true) : false;
}
template <class Type> Type read() {
  Type a;
  bool b;
  unsigned char c;
  while (c = getchar() - 48, (c > 9) & (c != 253))
    ;
  for (a = (b = c == 253) ? 0 : c; (c = getchar() - 48) <= 9; a = a * 10 + c)
    ;
  return b ? -a : a;
}
int (*rd)() = read<int>;
// const u32 P=;
// inline u32 &inc(u32 &a, u32 b) {return (a+=b)<P?a:(a-=P);}
// inline u32 &dec(u32 &a, u32 b) {return (a-=b)&0x80000000?(a+=P):a;}
// inline u32 sum(u32 a, u32 b) {return (a+=b)<P?a:a-P;}
// inline u32 dif(u32 a, u32 b) {return (a-=b)&0x80000000?a+P:a;}
// u64 power(u64 a, int b) {
//	u64 ans=1;
//	for(; b; a=a*a%P, b/=2) if(b&1) ans=ans*a%P;
//	return ans;
// }

const int N = 5005;
char t[N];
int m, d[N];
bool vis[N];
int main() {
  assert(scanf("%s", t) == 1);
  int n = strlen(t);
  int cur = 0;
  for (int i = 0; i < n; i += 2)
    cur += t[i] - 48;
  int diff = 0;
  for (int i = n; i--;) {
    if (t[i] == '1') {
      if (i & 1)
        ++diff;
      else
        --diff;
    } else
      d[m++] = diff;
  }
  std::reverse(d, d + m);
  int one = n - m;
  long long ans = 0;
  for (int i = 0; i < m; ++i) {
    int dif = -1e9, k = -1;
    for (int j = 0; j < m; ++j) {
      if (!vis[j] && d[j] > dif) {
        dif = d[j];
        k = j;
      }
    }
    for (int j = 0; j < k; ++j)
      d[j] -= d[k];
    for (int j = k + 1; j < m; ++j)
      d[j] *= -1;
    ans += cur;
    cur += d[k];
    vis[k] = true;
  }
  while (one >= 1) {
    ans += (one + 1) / 2;
    --one;
  }
  printf("%lld\\n", ans);
  return 0;
}