prithivMLmods/Coder-Stat huggingface dataset:
Problem p03347 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <cstring>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#pragma warning(disable : 4996)

typedef long long ll;
#define MIN(a, b) ((a) > (b) ? (b) : (a))
#define MAX(a, b) ((a) < (b) ? (b) : (a))
#define LINF 9223300000000000000
#define INF 2140000000
const long long MOD = 1000000007;
// const long long MOD = 998244353;

using namespace std;

void solve() {
  int n;
  scanf("%d", &n);
  vector<int> a(n);
  int i;
  for (i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    a[i] -= i;
  }
  if (a[0] != 0) {
    printf("-1\n");
    return;
  }
  for (i = 1; i < n; i++) {
    if (a[i] > a[i - 1]) {
      printf("-1\n");
      return;
    }
  }
  ll sum = 0;
  int curr = -INF;
  for (i = n - 1; i > 0; i--) {
    a[i] = MAX(a[i], curr);
    if (a[i] > curr) {
      assert(-a[i] >= 0 && -a[i] < i);
      sum += (i - (-a[i]));
      curr = a[i];
    }
  }
  printf("%lld\n", sum);

  return;
}

int main(int argc, char *argv[]) {
#if 1
  solve();
#else
  int T;
  scanf("%d", &T);
  while (T--) {
    solve();
  }
#endif
  return 0;
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <cstring>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#pragma warning(disable : 4996)

typedef long long ll;
#define MIN(a, b) ((a) > (b) ? (b) : (a))
#define MAX(a, b) ((a) < (b) ? (b) : (a))
#define LINF 9223300000000000000
#define INF 2140000000
const long long MOD = 1000000007;
// const long long MOD = 998244353;

using namespace std;

void solve() {
  int n;
  scanf("%d", &n);
  vector<int> a(n);
  int i;
  for (i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    a[i] -= i;
  }
  if (a[0] != 0) {
    printf("-1\n");
    return;
  }
  for (i = 1; i < n; i++) {
    if (a[i] > a[i - 1]) {
      printf("-1\n");
      return;
    }
  }
  ll sum = 0;
  int curr = -INF;
  for (i = n - 1; i > 0; i--) {
    a[i] = MAX(a[i], curr);
    if (a[i] > curr) {
      if (-a[i] > i) {
        printf("-1\n");
        return;
      }
      sum += (i - (-a[i]));
      curr = a[i];
    }
  }
  printf("%lld\n", sum);

  return;
}

int main(int argc, char *argv[]) {
#if 1
  solve();
#else
  int T;
  scanf("%d", &T);
  while (T--) {
    solve();
  }
#endif
  return 0;
}
Problem p03347 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int a[100000];
  int n;
  long long ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  if (a[0] != 0) {
    cout << -1 << endl;
    return 0;
  }
  for (int i = 1; i < n; i++) {
    if (a[i - 1] + 1 != a[i] && a[i - 1] < a[i]) {
      cout << -1 << endl;
      return 0;
    }
  }

  ans = a[n - 1];
  for (int i = n - 1 - 1; i >= 0; i--) {
    if (a[i] + 1 != a[i + 1]) {
      ans += a[i];
    }
  }

  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int a[200000];
  int n;
  long long ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  if (a[0] != 0) {
    cout << -1 << endl;
    return 0;
  }
  for (int i = 1; i < n; i++) {
    if (a[i - 1] + 1 != a[i] && a[i - 1] < a[i]) {
      cout << -1 << endl;
      return 0;
    }
  }

  ans = a[n - 1];
  for (int i = n - 1 - 1; i >= 0; i--) {
    if (a[i] + 1 != a[i + 1]) {
      ans += a[i];
    }
  }

  cout << ans << endl;
  return 0;
}
Problem p03347 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int n, a[123456];
int main() {
  cin >> n;
  bool flag = 1;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] > i)
      flag = 0;
    if (i && a[i] - a[i - 1] > 1)
      flag = 0;
  }
  if (!flag) {
    cout << "-1\n";
    return 0;
  }
  int ans = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (i == n - 1)
      ans += a[i];
    else if (a[i] + 1 != a[i + 1])
      ans += a[i];
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
#define int long long
int n, a[223456];
signed main() {
  cin >> n;
  bool flag = 1;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] > i)
      flag = 0;
    if (i && a[i] - a[i - 1] > 1)
      flag = 0;
  }
  if (!flag) {
    cout << "-1\n";
    return 0;
  }
  int ans = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (i == n - 1)
      ans += a[i];
    else if (a[i] + 1 != a[i + 1])
      ans += a[i];
  }
  cout << ans << endl;
  return 0;
}
Problem p03347 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
using ll = long long int;

template <class T> ostream &operator<<(ostream &os, vector<T> &v) {
  for (auto i = v.begin(); i != v.end(); i++) {
    os << *i << " ";
  }
  return os;
}

void solve(long long N, std::vector<long long> A) {
  bool f = true;
  for (ll i = 0; i + 1 < N; i++) {
    if (A[i] > i)
      f = false;
    if (A[i + 1] > A[i] + 1)
      f = false;
    if (!f) {
      std::cout << -1 << std::endl;
      return;
    }
  }
  vector<ll> v(N, 0);
  for (ll i = 0; i < N; i++) {
    v[i - A[i]] = A[i];
  }
  ll ans = 0;
  for (auto x : v)
    ans += x;
  std::cout << ans << std::endl;
}

int main() {
  long long N;
  scanf("%lld", &N);
  std::vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
  }
  solve(N, std::move(A));
  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
using ll = long long int;

template <class T> ostream &operator<<(ostream &os, vector<T> &v) {
  for (auto i = v.begin(); i != v.end(); i++) {
    os << *i << " ";
  }
  return os;
}

void solve(long long N, std::vector<long long> A) {
  for (ll i = 0; i < N; i++) {
    if ((A[i] > i) or (i + 1 < N and A[i + 1] - A[i] > 1)) {
      std::cout << -1 << std::endl;
      return;
    }
  }
  vector<ll> v(N, 0);
  for (ll i = 0; i < N; i++) {
    v[i - A[i]] = A[i];
  }
  ll ans = 0;
  for (auto x : v)
    ans += x;
  std::cout << ans << std::endl;
}

int main() {
  long long N;
  scanf("%lld", &N);
  std::vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
  }
  solve(N, std::move(A));
  return 0;
}
Problem p03347 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
#define ml 20005
#define ll long long
int n;
ll a[ml];
int main() {
  ll tal = 0;
  scanf("%d", &n);

  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
  }
  if (a[1] != 0) {
    printf("-1");
    return 0;
  }
  for (int i = 1; i < n; i++) {
    if (a[i + 1] - a[i] >= 2) {
      printf("-1");
      return 0;
    }
    if (a[i + 1] - a[i] == 1) {
      tal++;
    } else {
      tal += a[i + 1];
    }
  }
  printf("%lld", tal);

  // cout << "Hello world!" << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
#define ml 200005
#define ll long long
int n;
ll a[ml];
int main() {
  ll tal = 0;
  scanf("%d", &n);

  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
  }
  if (a[1] != 0) {
    printf("-1");
    return 0;
  }
  for (int i = 1; i < n; i++) {
    if (a[i + 1] - a[i] >= 2) {
      printf("-1");
      return 0;
    }
    if (a[i + 1] - a[i] == 1) {
      tal++;
    } else {
      tal += a[i + 1];
    }
  }
  printf("%lld", tal);

  // cout << "Hello world!" << endl;
  return 0;
}
Problem p03347 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, n1, n2) for (int i = n1; i < n2; i++)
#define speed_up                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);

typedef long long int ll;
typedef pair<ll, ll> Pi;
const int INF = (ll)(1LL << 31) - 1;
const ll INFl = (ll)9223372036854775807;
const int MAX = 10000;
const ll MOD = (ll)1e9 + 7;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
// int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};

template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

int n;
ll a[100100];
ll ans = 0;
map<ll, ll> m;

int main() {
  cin >> n;
  REP(i, n)
  cin >> a[i];

  if (a[0] != 0) {
    cout << -1 << endl;
    return 0;
  }
  for (int i = 1; i < n; i++) {

    if (a[i] > a[i - 1] + 1) {
      cout << -1 << endl;
      return 0;
    }
  }
  for (int i = 0; i < n; i++)
    m[i - a[i]] = a[i];
  for (auto it = m.begin(); it != m.end(); it++) {
    ans += it->second;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, n1, n2) for (int i = n1; i < n2; i++)
#define speed_up                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);

typedef long long int ll;
typedef pair<ll, ll> Pi;
const int INF = (ll)(1LL << 31) - 1;
const ll INFl = (ll)9223372036854775807;
const int MAX = 10000;
const ll MOD = (ll)1e9 + 7;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
// int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};

template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

int n;
ll a[200100];
ll ans = 0;
map<ll, ll> m;

int main() {
  cin >> n;
  REP(i, n)
  cin >> a[i];

  if (a[0] != 0) {
    cout << -1 << endl;
    return 0;
  }
  for (int i = 1; i < n; i++) {

    if (a[i] > a[i - 1] + 1) {
      cout << -1 << endl;
      return 0;
    }
  }
  for (int i = 0; i < n; i++)
    m[i - a[i]] = a[i];
  for (auto it = m.begin(); it != m.end(); it++) {
    ans += it->second;
  }
  cout << ans << endl;
  return 0;
}
Problem p03347 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define LL long long
#define N 100005
#define INFINITY 0x3f3f3f3f
#define maxn 10005
#define mase(a, b) memset(a, b, sizeof(a))
#define MIN(a, b) (a < b ? a : b)
#define MAX(a, b) (a > b ? a : b)
using namespace std;
int a[N];
int main() {
  int n;
  LL re;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  if (a[0] != 0) {
    printf("-1\n");
  } else {
    bool flag = true;
    re = 0;
    for (int i = 1; i < n; i++) {
      if (a[i] - a[i - 1] > 1) {
        printf("-1\n");
        flag = false;
        break;
      }
      if (a[i] - a[i - 1] == 1) {
        re += 1;
      } else {
        re += a[i];
      }
    }
    if (flag) {
      printf("%lld\n", re);
    }
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define LL long long
#define N 200010
#define INFINITY 0x3f3f3f3f
#define maxn 10005
#define mase(a, b) memset(a, b, sizeof(a))
#define MIN(a, b) (a < b ? a : b)
#define MAX(a, b) (a > b ? a : b)
using namespace std;
int a[N];
int main() {
  int n;
  LL re;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  if (a[0] != 0) {
    printf("-1\n");
  } else {
    bool flag = true;
    re = 0;
    for (int i = 1; i < n; i++) {
      if (a[i] - a[i - 1] > 1) {
        printf("-1\n");
        flag = false;
        break;
      }
      if (a[i] - a[i - 1] == 1) {
        re += 1;
      } else {
        re += a[i];
      }
    }
    if (flag) {
      printf("%lld\n", re);
    }
  }
  return 0;
}
Problem p03347 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>

using namespace std;
const int nmax = 100005;
int a[nmax];
long long ans;
int n, i;
int main() {
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    a[i] -= i;
    if (a[i] >= 0) {
      cout << "-1";
      return 0;
    }
    if (a[i] < a[i - 1])
      ans = ans + 1LL * (i + a[i]);
    else
      ans++;
    if (i > 1 && a[i] > a[i - 1]) {
      cout << "-1";
      return 0;
    }
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;
const int nmax = 200005;
int a[nmax];
long long ans;
int n, i;
int main() {
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    a[i] -= i;
    if (a[i] >= 0) {
      cout << "-1";
      return 0;
    }
    if (a[i] < a[i - 1])
      ans = ans + 1LL * (i + a[i]);
    else
      ans++;
    if (i > 1 && a[i] > a[i - 1]) {
      cout << "-1";
      return 0;
    }
  }
  cout << ans;
  return 0;
}
Problem p03347 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
#define ll long long int
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n; i >= 0; i--)
#define REP(i, s, t) for (int i = s; i <= t; i++)
#define RREP(i, s, t) for (int i = s; i >= t; i--)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  int A[100010];
  rep(i, N) cin >> A[i];
  ll ans = 0;
  if (A[0] != 0) {
    cout << "-1" << endl;
    return 0;
  }
  rep(i, N - 1) {
    if (A[i] + 1 < A[i + 1]) {
      cout << -1 << endl;
      return 0;
    }
  }
  int state = 0;
  RREP(i, N - 1, 0) {
    state = max(0, state - 1);
    if (i == N - 1) {
      ans += A[i];
      state = A[i];
    } else if (state < A[i]) {
      ans += A[i];
      state = A[i];
    }
  }
  cout << ans << endl;
  return 0;
}
// 0 1 2 3 4 4
//  0 0 1 2 3 4
//  0 1 2 3 4 4

// 0 1 1 0 1 2 2 1 2
//  0 0 0 0 0 0 0 1 2 (2)
//  0 0 0 0 0 1 2 1 2 (4)
//  0 0 0 0 1 2 2 1 2 (6)
//  0 0 1 0 1 2 2 1 2 (7)
//  0 1 1 0 1 2 2 1 2

// 0 1 2 3 4 2 3 4
//          1 2 3 4

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define ll long long int
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n; i >= 0; i--)
#define REP(i, s, t) for (int i = s; i <= t; i++)
#define RREP(i, s, t) for (int i = s; i >= t; i--)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  int A[200010];
  rep(i, N) cin >> A[i];
  ll ans = 0;
  if (A[0] != 0) {
    cout << "-1" << endl;
    return 0;
  }
  rep(i, N - 1) {
    if (A[i] + 1 < A[i + 1]) {
      cout << -1 << endl;
      return 0;
    }
  }
  int state = 0;
  RREP(i, N - 1, 0) {
    state = max(0, state - 1);
    if (i == N - 1) {
      ans += A[i];
      state = A[i];
    } else if (state < A[i]) {
      ans += A[i];
      state = A[i];
    }
  }
  cout << ans << endl;
  return 0;
}
// 0 1 2 3 4 4
//  0 0 1 2 3 4
//  0 1 2 3 4 4

// 0 1 1 0 1 2 2 1 2
//  0 0 0 0 0 0 0 1 2 (2)
//  0 0 0 0 0 1 2 1 2 (4)
//  0 0 0 0 1 2 2 1 2 (6)
//  0 0 1 0 1 2 2 1 2 (7)
//  0 1 1 0 1 2 2 1 2

// 0 1 2 3 4 2 3 4
//          1 2 3 4
Problem p03347 (C++)

Buggy submission (status: Time Limit Exceeded):
// #include <bits/stdc++.h>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <vector>
#define LL long long
#define ft first
#define sd second
#define mp(x, y) make_pair(x, y)
#define db double
#define int LL
using namespace std;
const int N = 2e5 + 233;
// const int mod = ;
const int INF = numeric_limits<int>::max();

#define rep(i, x, y) for (int i = x; i <= y; ++i)
void read(int &x) {
  x = 0;
  char ch = getchar();
  int f = 1;
  while (!isdigit(ch))
    (ch == '-' ? f = -1 : 0), ch = getchar();
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  x *= f;
}

void write(int x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}
#define GG return printf("-1"), 0
int n, a[N], ans;
signed main() {
  read(n);
  rep(i, 1, n) {
    read(a[i]);
    if (a[i] - a[i - 1] > 1)
      GG;
  }
  if (a[1] != 0)
    GG;
  for (int i = 1; i <= n;) {
    if (i == n)
      continue;
    int l = i, r = i + 1;
    while (a[r] != 0 && r <= n)
      ++r;
    --r;
    // if (r>n)  i=n+1;
    // else i=r+1;
    i = r + 1;
    for (int j = r; j >= l;) {
      int pos = j;
      while (a[pos] - a[pos - 1] == 1 && pos >= l)
        --pos;
      ans += a[j];
      j = pos - 1;
    }
  }
  printf("%lld", ans);
  return 0;
}

Fixed submission:
// #include <bits/stdc++.h>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <vector>
#define LL long long
#define ft first
#define sd second
#define mp(x, y) make_pair(x, y)
#define db double
#define int LL
using namespace std;
const int N = 2e5 + 233;
// const int mod = ;
const int INF = numeric_limits<int>::max();

#define rep(i, x, y) for (int i = x; i <= y; ++i)
void read(int &x) {
  x = 0;
  char ch = getchar();
  int f = 1;
  while (!isdigit(ch))
    (ch == '-' ? f = -1 : 0), ch = getchar();
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  x *= f;
}

void write(int x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}
#define GG return printf("-1"), 0
int n, a[N], ans;
signed main() {
  read(n);
  rep(i, 1, n) {
    read(a[i]);
    if (a[i] - a[i - 1] > 1)
      GG;
  }
  if (a[1] != 0)
    GG;
  for (int i = 1; i <= n;) {
    if (i == n)
      break;
    int l = i, r = i + 1;
    while (a[r] != 0 && r <= n)
      ++r;
    --r;
    // if (r>n)  i=n+1;
    // else i=r+1;
    i = r + 1;
    for (int j = r; j >= l;) {
      int pos = j;
      while (a[pos] - a[pos - 1] == 1 && pos >= l)
        --pos;
      ans += a[j];
      j = pos - 1;
    }
  }
  printf("%lld", ans);
  return 0;
}
Problem p03347 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pt(sth) cout << sth << "\n"
#define chmax(a, b)                                                            \
  {                                                                            \
    if (a < b)                                                                 \
      a = b;                                                                   \
  }
#define chmin(a, b)                                                            \
  {                                                                            \
    if (a > b)                                                                 \
      a = b;                                                                   \
  }
#define moC(a, s, b) (a) = ((a)s(b) + MOD) % MOD
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
static const ll INF = 1e18;
static const ll MAX = 101010;
static const ll MOD = 1e9 + 7;

int main(void) {
  ll N;
  cin >> N;
  ll i;
  ll a[MAX];

  for (i = 0; i < N; i++) {
    cin >> a[i];
  }
  a[N] = a[N - 1];

  if (a[0]) {
    pt(-1);
    return 0;
  }

  for (i = 0; i < N - 1; i++) {
    if (a[i + 1] - a[i] > 1) {
      pt(-1);
      return 0;
    }
  }

  ll ans = 0;
  ll t = 0;
  for (i = N - 1; i >= 1; i--) {
    if (a[i] >= a[i + 1]) {
      ans += a[i];
    }
  }

  pt(ans);
}


Fixed submission:
#include <bits/stdc++.h>
#define pt(sth) cout << sth << "\n"
#define chmax(a, b)                                                            \
  {                                                                            \
    if (a < b)                                                                 \
      a = b;                                                                   \
  }
#define chmin(a, b)                                                            \
  {                                                                            \
    if (a > b)                                                                 \
      a = b;                                                                   \
  }
#define moC(a, s, b) (a) = ((a)s(b) + MOD) % MOD
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
static const ll INF = 1e18;
static const ll MAX = 101010;
static const ll MOD = 1e9 + 7;

int main(void) {
  ll N;
  cin >> N;
  ll i;
  ll a[MAX * 2];

  for (i = 0; i < N; i++) {
    cin >> a[i];
  }
  a[N] = a[N - 1];

  if (a[0]) {
    pt(-1);
    return 0;
  }

  for (i = 0; i < N - 1; i++) {
    if (a[i + 1] - a[i] > 1) {
      pt(-1);
      return 0;
    }
  }

  ll ans = 0;
  ll t = 0;
  for (i = N - 1; i >= 1; i--) {
    if (a[i] >= a[i + 1]) {
      ans += a[i];
    }
  }

  pt(ans);
}
Problem p03347 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int N;
int A[1 << 17];
ll cnt;

int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; ++i)
    scanf("%d", A + i);
  if (A[0])
    return printf("-1\n"), 0;
  for (int i = 0; i < N - 1; ++i)
    if (A[i + 1] - A[i] > 1)
      return printf("-1\n"), 0;
  for (int i = 0; i < N; ++i)
    if (A[i] >= A[i + 1])
      cnt += A[i];
  printf("%lld\n", cnt);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int N;
int A[200005];
ll cnt;

int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; ++i)
    scanf("%d", A + i);
  if (A[0])
    return printf("-1\n"), 0;
  for (int i = 0; i < N - 1; ++i)
    if (A[i + 1] - A[i] > 1)
      return printf("-1\n"), 0;
  for (int i = 0; i < N; ++i)
    if (A[i] >= A[i + 1])
      cnt += A[i];
  printf("%lld\n", cnt);
}
Problem p03347 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int a[210005], p[200005];
long long ans;
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  bool f = true;
  for (int i = 1; i <= n; i++) {
    if (a[i] > i - 1 || i > 1 && a[i - 1] < a[i] - 1) {
      f = false;
    }
    p[i - a[i]] = a[i];
  }
  for (int i = 1; i <= n; i++)
    ans = ans + p[i];
  if (!f)
    printf("-1");
  else
    printf("%lld", ans);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int a[210005], p[200005];
long long ans;
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  bool f = true;
  for (int i = 1; i <= n; i++) {
    if (a[i] > i - 1 || i > 1 && a[i - 1] < a[i] - 1) {
      f = false;
      break;
    }
    p[i - a[i]] = a[i];
  }
  for (int i = 1; i <= n; i++)
    ans = ans + p[i];
  if (!f)
    printf("-1");
  else
    printf("%lld", ans);
}
Problem p03347 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 5;

int a[N];

void die() {
  puts("-1");
  exit(0);
}

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i)
    scanf("%d", a + i);
  if (a[0] != 0)
    die();
  int last = 0;
  long long ans = 0;
  for (int i = n - 1; i >= 0; --i) {
    if (a[i] < last - 1)
      die();
    else if (a[i] == last - 1) {
    } else
      ans += a[i];
    last = a[i];
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const int N = 1e6 + 5;

int a[N];

void die() {
  puts("-1");
  exit(0);
}

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i)
    scanf("%d", a + i);
  if (a[0] != 0)
    die();
  int last = 0;
  long long ans = 0;
  for (int i = n - 1; i >= 0; --i) {
    if (a[i] < last - 1)
      die();
    else if (a[i] == last - 1) {
    } else
      ans += a[i];
    last = a[i];
  }
  cout << ans << endl;
  return 0;
}
Problem p03347 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int a[100007], n;
ll res = 0;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  a[0] = -1;
  for (int i = 1; i <= n; i++) {
    if (a[i] - a[i - 1] > 1)
      return 0 * printf("-1\n");
    else if (a[i] - a[i - 1] == 1)
      res++;
    else
      res += a[i];
  }
  std::cout << res - 1 << '\n';
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int a[200007], n;
ll res = 0;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  a[0] = -1;
  for (int i = 1; i <= n; i++) {
    if (a[i] - a[i - 1] > 1)
      return 0 * printf("-1\n");
    else if (a[i] - a[i - 1] == 1)
      res++;
    else
      res += a[i];
  }
  std::cout << res - 1 << '\n';
}
Problem p03347 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MAXN 100000
#define rint register int
inline int read() {
  int r;
  int s = 0, c;
  for (; !isdigit(c = getchar()); s = c)
    ;
  for (r = c ^ 48; isdigit(c = getchar()); (r *= 10) += c ^ 48)
    ;
  return s ^ 45 ? r : -r;
}
int A[MAXN + 5], n;
ll Ans;
int main() {
  n = read();
  for (rint i = 1; i <= n; A[i] = read(), i++)
    ;
  if (A[1])
    return puts("-1"), 0;
  for (rint i = 1; i <= n; i++)
    if (A[i] > A[i - 1] + 1)
      return puts("-1"), 0;
  for (rint i = 1; i <= n; i++)
    Ans += A[i] == A[i - 1] + 1 ? 1 : A[i];
  cout << Ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MAXN 200000
#define rint register int
inline int read() {
  int r;
  int s = 0, c;
  for (; !isdigit(c = getchar()); s = c)
    ;
  for (r = c ^ 48; isdigit(c = getchar()); (r *= 10) += c ^ 48)
    ;
  return s ^ 45 ? r : -r;
}
int A[MAXN + 5], n;
ll Ans;
int main() {
  n = read();
  for (rint i = 1; i <= n; A[i] = read(), i++)
    ;
  if (A[1])
    return puts("-1"), 0;
  for (rint i = 1; i <= n; i++)
    if (A[i] > A[i - 1] + 1)
      return puts("-1"), 0;
  for (rint i = 1; i <= n; i++)
    Ans += A[i] == A[i - 1] + 1 ? 1 : A[i];
  cout << Ans << endl;
  return 0;
}
Problem p03347 (C++)

Buggy submission (status: Runtime Error):
// g++ -std=c++14 test.cpp -o test.out

#include <algorithm>
#include <bitset>
#include <cassert>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdlib.h>
#include <string.h>
#include <utility>
#include <vector>
using namespace std;

#define LL long long int
const LL INF = (1LL << 60);
const int INF_INT = 2147483647 - 1e6 - 1;
const LL mod = 1000000007ll;
const int mod_int = 1000000007;

LL N;
LL A[100000];

LL ans = 0;

void solve() {
  if (A[0] != 0) {
    ans = -1;
    return;
  }
  for (int i = N - 1; i >= 1; i--) {
    int seq_max = A[i];
    while (i >= 1) {
      if (A[i] == A[i - 1] + 1)
        i--;
      else
        break;
    }
    if (A[i] > A[i - 1] + 1) {
      ans = -1;
      return;
    }
    ans += seq_max;
  }
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> A[i];

  solve();

  cout << ans << endl;
  return 0;
}

Fixed submission:
// g++ -std=c++14 test.cpp -o test.out

#include <algorithm>
#include <bitset>
#include <cassert>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdlib.h>
#include <string.h>
#include <utility>
#include <vector>
using namespace std;

#define LL long long int
const LL INF = (1LL << 60);
const int INF_INT = 2147483647 - 1e6 - 1;
const LL mod = 1000000007ll;
const int mod_int = 1000000007;

LL N;
LL A[200000];

LL ans = 0;

void solve() {
  if (A[0] != 0) {
    ans = -1;
    return;
  }
  for (int i = N - 1; i >= 1; i--) {
    int seq_max = A[i];
    while (i >= 1) {
      if (A[i] == A[i - 1] + 1)
        i--;
      else
        break;
    }
    if (A[i] > A[i - 1] + 1) {
      ans = -1;
      return;
    }
    ans += seq_max;
  }
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> A[i];

  solve();

  cout << ans << endl;
  return 0;
}
Problem p03348 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <vector>
#define fi first
#define se second
using namespace std;
typedef long long i64;
typedef pair<int, i64> pii;
inline int read(int f = 1, int x = 0, char ch = ' ') {
  while (!isdigit(ch = getchar()))
    if (ch == '-')
      f = -1;
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  return f * x;
}
const int N = 1e5 + 5;
int n, f[N], d[N], dep[N];
vector<int> e[N];
pii ans;
int bfs(int u, int v) {
  queue<int> q;
  q.push(u), dep[u] = 1;
  if (v)
    q.push(v), dep[v] = 1;
  int ret = 0;
  while (q.size()) {
    int x = q.front();
    q.pop();
    f[dep[x]] = max(f[dep[x]], d[x]), ret = max(ret, dep[x]);
    for (int y : e[x])
      if (!dep[y])
        dep[y] = dep[x] + 1, q.push(y);
  }
  return ret;
}
pii solve(int u, int v) {
  for (int i = 1; i <= n; ++i)
    f[i] = dep[i] = 0;
  pii ret;
  ret.fi = bfs(u, v), ret.se = f[1] - (v != 0);
  for (int i = 2; i <= n && f[i] != 1; ++i)
    ret.se *= f[i] - 1;
  if (v)
    ret.se <<= 1;
  return ret;
}
int main() {
  n = read(), ans.fi = n;
  for (int i = 1; i < n; ++i) {
    int x = read(), y = read();
    ++d[x], ++d[y];
    e[x].push_back(y), e[y].push_back(x);
  }
  for (int i = 1; i <= n; ++i) {
    ans = min(ans, solve(i, 0));
    for (int j : e[i])
      ans = min(ans, solve(i, j));
  }
  printf("%d %lld\n", ans.fi, ans.se);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <vector>
#define fi first
#define se second
using namespace std;
typedef long long i64;
typedef pair<int, i64> pii;
inline int read(int f = 1, int x = 0, char ch = ' ') {
  while (!isdigit(ch = getchar()))
    if (ch == '-')
      f = -1;
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  return f * x;
}
const int N = 1e5 + 5;
int n, f[N], d[N], dep[N];
vector<int> e[N];
pii ans;
int bfs(int u, int v) {
  queue<int> q;
  q.push(u), dep[u] = 1;
  if (v)
    q.push(v), dep[v] = 1;
  int ret = 0;
  while (q.size()) {
    int x = q.front();
    q.pop();
    f[dep[x]] = max(f[dep[x]], d[x]), ret = max(ret, dep[x]);
    for (int y : e[x])
      if (!dep[y])
        dep[y] = dep[x] + 1, q.push(y);
  }
  return ret;
}
pii solve(int u, int v) {
  for (int i = 1; i <= n; ++i)
    f[i] = dep[i] = 0;
  pii ret;
  ret.fi = bfs(u, v), ret.se = f[1] - (v != 0);
  for (int i = 2; i <= n && f[i] != 1; ++i)
    ret.se *= f[i] - 1;
  if (v)
    ret.se <<= 1;
  return ret;
}
int main() {
  n = read(), ans.fi = n;
  if (n == 2)
    return puts("1 2"), 0;
  for (int i = 1; i < n; ++i) {
    int x = read(), y = read();
    ++d[x], ++d[y];
    e[x].push_back(y), e[y].push_back(x);
  }
  for (int i = 1; i <= n; ++i) {
    ans = min(ans, solve(i, 0));
    for (int j : e[i])
      ans = min(ans, solve(i, j));
  }
  printf("%d %lld\n", ans.fi, ans.se);
  return 0;
}
Problem p03349 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
int P, f[301][301][301];
void inc(int &a, int b) { a += b - P, a += a >> 31 & P; }
int main() {
  int n, m;
  scanf("%d%d%d", &n, &m, &P), f[0][1][0] = 1;
  for (int i = 0; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      for (int k = i; ~k; --k)
        k ? inc(f[i][j][k - 1], f[i][j][k]) : inc(f[i][j + 1][i], f[i][j][k]),
            inc(f[i + 1][j][k], (k + 1ll) * f[i][j][k] % P);
  printf("%d", f[n][m][0]);
}


Fixed submission:
#include <cstdio>
int P, f[307][307][307];
void inc(int &a, int b) { a += b - P, a += a >> 31 & P; }
int main() {
  int n, m;
  scanf("%d%d%d", &n, &m, &P), f[0][1][0] = 1;
  for (int i = 0; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      for (int k = i; ~k; --k)
        k ? inc(f[i][j][k - 1], f[i][j][k]) : inc(f[i][j + 1][i], f[i][j][k]),
            inc(f[i + 1][j][k], (k + 1ll) * f[i][j][k] % P);
  printf("%d", f[n][m][0]);
}
Problem p03350 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

const int N = 20;

int n, k;
int *dp[N << 1], pool[(N << 2) * (N + 2)], *p = pool;

void alloc() {
  for (int i = 1; i < 1 << n + 1; ++i) {
    int len = std::__lg(i);
    dp[i] = p, p += 1 << n - len + 1;
  }
}

int main() {
  std::ios::sync_with_stdio(0), std::cin.tie(0);
  std::cin >> n >> k, alloc();
  for (int i = 0; i <= n; ++i) {
    std::string s;
    std::cin >> s;
    for (int j = 0; j < 1 << i; ++j)
      if (s[j] == '1')
        dp[1][1 << i | j] = 1;
  }
  for (int i = 1; i < 1 << n + 1; ++i) {
    int len = std::__lg(i);
    for (int j = 1; j < 1 << n - len + 1; ++j)
      if (dp[i][j]) {
        int j_len = std::__lg(j), k_len;
        if (j & j + 1) {
          k_len = std::__lg(j ^ ~(-1 << j_len + 1));
          dp[i << 1 | 0][j & ~(-1 << k_len) | 1 << k_len] += dp[i][j];
        }
        if (j & j - 1) {
          k_len = std::__lg(j ^ 1 << j_len);
          dp[i << 1 | 1][j & ~(-1 << k_len) | 1 << k_len] += dp[i][j];
        }
      }
  }
  std::string ans = "";
  for (int i = 2; i < 1 << n + 1; ++i) {
    int len = std::__lg(i), sum = 0;
    for (int j = 1; j < 1 << n - len + 1; ++j)
      sum += dp[i][j];
    if (sum >= k) {
      std::string s;
      for (int t = len - 1; ~t; --t)
        s += char(i >> t & 1 | '0');
      if (len > ans.size())
        ans = s;
    }
  }
  std::cout << ans << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

const int N = 20;

int n, k;
int *dp[1 << N + 1], pool[(1 << N + 1) * (N + 2)], *p = pool;

void alloc() {
  for (int i = 1; i < 1 << n + 1; ++i) {
    int len = std::__lg(i);
    dp[i] = p, p += 1 << n - len + 1;
  }
}

int main() {
  std::ios::sync_with_stdio(0), std::cin.tie(0);
  std::cin >> n >> k, alloc();
  for (int i = 0; i <= n; ++i) {
    std::string s;
    std::cin >> s;
    for (int j = 0; j < 1 << i; ++j)
      if (s[j] == '1')
        dp[1][1 << i | j] = 1;
  }
  for (int i = 1; i < 1 << n + 1; ++i) {
    int len = std::__lg(i);
    for (int j = 1; j < 1 << n - len + 1; ++j)
      if (dp[i][j]) {
        int j_len = std::__lg(j), k_len;
        if (j & j + 1) {
          k_len = std::__lg(j ^ ~(-1 << j_len + 1));
          dp[i << 1 | 0][j & ~(-1 << k_len) | 1 << k_len] += dp[i][j];
        }
        if (j & j - 1) {
          k_len = std::__lg(j ^ 1 << j_len);
          dp[i << 1 | 1][j & ~(-1 << k_len) | 1 << k_len] += dp[i][j];
        }
      }
  }
  std::string ans = "";
  for (int i = 2; i < 1 << n + 1; ++i) {
    int len = std::__lg(i), sum = 0;
    for (int j = 1; j < 1 << n - len + 1; ++j)
      sum += dp[i][j];
    if (sum >= k) {
      std::string s;
      for (int t = len - 1; ~t; --t)
        s += char(i >> t & 1 | '0');
      if (len > ans.size())
        ans = s;
    }
  }
  std::cout << ans << '\n';
  return 0;
}
Problem p03350 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int N, K;
char X[22][1 << 20];
int dp[2][22][1 << 20];

int main() {
  scanf("%d %d", &N, &K);

  for (int i = 0; i <= N; i++) {
    scanf("\n");
    for (int j = 0; j < (1 << i); j++) {
      scanf("%c", &X[i][j]);
    }
  }

  for (int p = N; p >= 0; p--) {
    for (int len = 0; len <= N; len++) {
      for (int mask = 0; mask < (1 << (N + 1)); mask++) {
        dp[p & 1][len][mask] = 0;

        if (len <= p) {
          dp[p & 1][len][mask] += X[p][mask] - '0';
          if (p < N) {
            dp[p & 1][len][mask] += dp[(p ^ 1) & 1][len][mask];
            dp[p & 1][len][mask] += dp[(p ^ 1) & 1][len][mask ^ (1 << p)];
          }
          continue;
        }
        int t = (mask & (1 << p)) ? 1 : 0;
        int nmask = mask & ((1 << (N + 1)) - (1 << p));
        nmask <<= 1;
        nmask += (mask & ((1 << p) - 1));
        if (!t)
          nmask += (1 << p);
        dp[p & 1][len][mask] += dp[(p ^ 1) & 1][len][mask];
        if (len < N)
          dp[p & 1][len][mask] += dp[(p ^ 1) & 1][len + 1][nmask];
      }
    }
  }

  string ans = "";
  for (int i = N; i >= 0; i--) {
    for (int j = 0; j < (1 << i); j++) {
      if (dp[0][i][j] >= K) {
        string tmp;
        for (int k = i - 1; k >= 0; k--) {
          tmp.push_back((j & (1 << k)) ? '1' : '0');
        }
        if (ans == "" || ans > tmp)
          ans = tmp;
      }
    }
    if (ans != "") {
      cout << ans;
      return 0;
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int N, K;
char X[22][1 << 20];
int dp[2][22][1 << 20];

int main() {
  scanf("%d %d", &N, &K);

  for (int i = 0; i <= N; i++) {
    scanf("\n");
    for (int j = 0; j < (1 << i); j++) {
      scanf("%c", &X[i][j]);
    }
  }

  for (int p = N; p >= 0; p--) {
    for (int len = 0; len <= N; len++) {
      for (int mask = 0; mask < (1 << max(p, len)); mask++) {
        dp[p & 1][len][mask] = 0;

        if (len <= p) {
          dp[p & 1][len][mask] += X[p][mask] - '0';
          if (p < N) {
            dp[p & 1][len][mask] += dp[(p ^ 1) & 1][len][mask];
            dp[p & 1][len][mask] += dp[(p ^ 1) & 1][len][mask ^ (1 << p)];
          }
          continue;
        }
        int t = (mask & (1 << p)) ? 1 : 0;
        int nmask = mask & ((1 << (N + 1)) - (1 << p));
        nmask <<= 1;
        nmask += (mask & ((1 << p) - 1));
        if (!t)
          nmask += (1 << p);
        dp[p & 1][len][mask] += dp[(p ^ 1) & 1][len][mask];
        if (len < N)
          dp[p & 1][len][mask] += dp[(p ^ 1) & 1][len + 1][nmask];
      }
    }
  }

  string ans = "";
  for (int i = N; i >= 0; i--) {
    for (int j = 0; j < (1 << i); j++) {
      if (dp[0][i][j] >= K) {
        string tmp;
        for (int k = i - 1; k >= 0; k--) {
          tmp.push_back((j & (1 << k)) ? '1' : '0');
        }
        if (ans == "" || ans > tmp)
          ans = tmp;
      }
    }
    if (ans != "") {
      cout << ans;
      return 0;
    }
  }
}
Problem p03350 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int n, k;
vector<int> f[21][21];
char s[1 << 20];

int main() {
  scanf("%d%d", &n, &k);
  for (int i = 0; i <= n; ++i) {
    scanf("%s", s);
    for (int j = 0; j <= i; ++j)
      f[i][j].resize(1 << i);
    for (int j = 0; j < 1 << i; ++j)
      if (s[j] == '1')
        f[i][0][j] = 1;
  }
  for (int i = n; i >= 0; --i) {
    for (int j = 0; j < i; ++j)
      for (int s = 0; s < 1 << i; ++s)
        if (f[i][j][s]) {
          int a = s & (1 << j) - 1, b = s >> j;
          f[j][j][a] += f[i][j][s];
          if (b != (1 << i - j) - 1) {
            int cnt = __builtin_ctz(~b);
            f[i - cnt][j + 1][b >> cnt << j | a] += f[i][j][s];
          }
          if (b) {
            int cnt = __builtin_ctz(b);
            f[i - cnt][j + 1][b >> cnt << j | a] += f[i][j][s];
          }
        }
    for (int s = 0; s < 1 << i; ++s)
      if (f[i][i][s] >= k) {
        for (int j = i - 1; j >= 0; --j)
          putchar("01"[s >> j & 1]);
        puts("");
        return 0;
      }
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int n, k;
vector<int> f[21][21];
char s[1 << 20 | 1];

int main() {
  scanf("%d%d", &n, &k);
  for (int i = 0; i <= n; ++i) {
    scanf("%s", s);
    for (int j = 0; j <= i; ++j)
      f[i][j].resize(1 << i);
    for (int j = 0; j < 1 << i; ++j)
      if (s[j] == '1')
        f[i][0][j] = 1;
  }
  for (int i = n; i >= 0; --i) {
    for (int j = 0; j < i; ++j)
      for (int s = 0; s < 1 << i; ++s)
        if (f[i][j][s]) {
          int a = s & (1 << j) - 1, b = s >> j;
          f[j][j][a] += f[i][j][s];
          if (b != (1 << i - j) - 1) {
            int cnt = __builtin_ctz(~b);
            f[i - cnt][j + 1][b >> cnt << j | a] += f[i][j][s];
          }
          if (b) {
            int cnt = __builtin_ctz(b);
            f[i - cnt][j + 1][b >> cnt << j | a] += f[i][j][s];
          }
        }
    for (int s = 0; s < 1 << i; ++s)
      if (f[i][i][s] >= k) {
        for (int j = i - 1; j >= 0; --j)
          putchar("01"[s >> j & 1]);
        puts("");
        return 0;
      }
  }
}
Problem p03350 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define For(i, j, k) for (int i = j; i <= k; i++)
using namespace std;
const int N = 1048580, M = 21;
int n, m, len, ans;
int f[N][M], g[N][M];
char s[N];
int main() {
  scanf("%d%d", &n, &m);
  For(i, 0, n) {
    scanf("%s", s);
    For(j, 0, (1 << i) - 1) f[i][j] = s[j] - '0';
  }
  For(i, 1, n) For(j, 0, (1 << i) - 1) {
    int b = (j >> (i - 1)) & 1, r = 0;
    for (; r < i && ((j >> (i - r - 1)) & 1) == b; r++)
      ;
    g[i][j] = (r == i ? -1 : r);
  }
  For(i, 0, n) {
    For(j, 1, n - i) For(k, 0, (1 << (i + j)) - 1) {
      f[i][k >> j] += f[i + j][k];
      int t = g[j][k & ((1 << j) - 1)];
      if (t == -1)
        continue;
      f[i + j - t][((k >> j) << (j - t)) | (k & ((1 << (j - t)) - 1))] +=
          f[i + j][k];
    }
    for (int j = (1 << i) - 1; j >= 0; j--)
      if (f[i][j] >= m) {
        len = i;
        ans = j;
      }
  }
  for (int i = len - 1; i >= 0; i--)
    printf("%d", (ans >> i) & 1);
}

Fixed submission:
#include <bits/stdc++.h>
#define For(i, j, k) for (int i = j; i <= k; i++)
using namespace std;
const int N = 1048580, M = 21;
int n, m, len, ans;
int f[M][N], g[M][N];
char s[N];
int main() {
  scanf("%d%d", &n, &m);
  For(i, 0, n) {
    scanf("%s", s);
    For(j, 0, (1 << i) - 1) f[i][j] = s[j] - '0';
  }
  For(i, 1, n) For(j, 0, (1 << i) - 1) {
    int b = (j >> (i - 1)) & 1, r = 0;
    for (; r < i && ((j >> (i - r - 1)) & 1) == b; r++)
      ;
    g[i][j] = (r == i ? -1 : r);
  }
  For(i, 0, n) {
    For(j, 1, n - i) For(k, 0, (1 << (i + j)) - 1) {
      f[i][k >> j] += f[i + j][k];
      int t = g[j][k & ((1 << j) - 1)];
      if (t == -1)
        continue;
      f[i + j - t][((k >> j) << (j - t)) | (k & ((1 << (j - t)) - 1))] +=
          f[i + j][k];
    }
    for (int j = (1 << i) - 1; j >= 0; j--)
      if (f[i][j] >= m) {
        len = i;
        ans = j;
      }
  }
  for (int i = len - 1; i >= 0; i--)
    printf("%d", (ans >> i) & 1);
}
Problem p03351 (Python)

Buggy submission (status: Runtime Error):
a, b, c, d = list(map(int, input().split()))

if abs(a, c) <= d or (abs(a, b) <= d and abs(b, c) <= d):
    print("Yes")
else:
    print("No")


Fixed submission:
a, b, c, d = list(map(int, input().split()))

if abs(a - c) <= d or (abs(a - b) <= d and abs(b - c) <= d):
    print("Yes")
else:
    print("No")
Problem p03351 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
int abs(int, int);
int main(void) {
  int a, b, c, d;
  scanf("%d %d %d %d", a, b, c, d);
  if (abs(a, c) <= d || (abs(a, b) <= d && abs(b, c) <= d)) {
    printf("Yes");
  } else {
    printf("No");
  }
  return 0;
}
int abs(int x, int y) {
  int sub = x - y;
  if (sub <= 0) {
    sub = -1 * sub;
  }
  return sub;
}

Fixed submission:
#include <stdio.h>
int abs(int, int);
int main(void) {
  int a, b, c, d;
  scanf("%d %d %d %d", &a, &b, &c, &d);
  if (abs(a, c) <= d || (abs(a, b) <= d && abs(b, c) <= d)) {
    printf("Yes");
  } else {
    printf("No");
  }
  return 0;
}
int abs(int x, int y) {
  int sub = x - y;
  if (sub <= 0) {
    sub = -1 * sub;
  }
  return sub;
}
Problem p03352 (C++)

Buggy submission (status: Time Limit Exceeded):
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#include <numeric>
typedef long long Int;
#define rep(i, a, b) for (Int i = a; i < b; ++i)
#define rrep(i, a, b) for (Int i = a; i >= b; --i)
using namespace std;

int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  int X;
  cin >> X;
  int res = 1;
  for (int b = 1; b * b <= X; ++b) {
    int p = 2;
    int bp = b * b;
    while (bp <= X) {
      res = max(res, bp);
      bp *= b;
    }
  }

  cout << res << endl;
}


Fixed submission:
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#include <numeric>
typedef long long Int;
#define rep(i, a, b) for (Int i = a; i < b; ++i)
#define rrep(i, a, b) for (Int i = a; i >= b; --i)
using namespace std;

int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  int X;
  cin >> X;
  int res = 1;
  for (int b = 2; b * b <= X; ++b) {
    int bp = b * b;
    while (bp <= X) {
      res = max(res, bp);
      bp *= b;
    }
  }

  cout << res << endl;
}
Problem p03352 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int x, ans = 1;
  cin >> x;
  for (int b = 1; b <= x; b++) {
    int tmp = b * b;
    while (tmp <= x) {
      ans = max(ans, tmp);
      tmp *= b;
    }
  }
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int x, ans = 1;
  cin >> x;
  for (int b = 2; b <= x; b++) {
    int tmp = b * b;
    while (tmp <= x) {
      ans = max(ans, tmp);
      tmp *= b;
    }
  }
  cout << ans;
}
Problem p03352 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int x, a[1001] = {0, 1};
  for (int i = 0; i < 32; i++)
    for (int j = i * i; j < 1000; j *= i)
      a[j] = 1;
  cin >> x;
  for (int i = x; i >= 0; i--)
    if (a[i] == 1) {
      cout << i << endl;
      break;
    }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int x, a[1001] = {0, 1};
  for (int i = 2; i < 32; i++)
    for (int j = i * i; j <= 1000; j *= i)
      a[j] = 1;
  cin >> x;
  for (int i = x; i >= 0; i--)
    if (a[i] == 1) {
      cout << i << endl;
      break;
    }
  return 0;
}
Problem p03352 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (long long int i = 0; i < n; i++)
#define _rep(i, m, n) for (long long int i = m; i < n; i++)
#define print(n) std::cout << n << std::endl
#define _print(n) std::cout << n
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int N = 1000000;
const ll mod = 1000000007;
using Graph = vector<vector<int>>;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

ll gcd(ll A, ll B) {
  if (B == 0)
    return A;
  return gcd(B, A % B);
}
ll lcm(ll A, ll B) { return A * B / gcd(A, B); }

/*------------------------------------------------------------------*/

int main() {
  int n;
  cin >> n;
  while (true) {
    for (int i = 1; i < n; i++) {
      for (int j = 0; j < 7; j++) {
        if (pow(i, j) == n) {
          cout << n << endl;
          return 0;
        }
      }
    }
    n--;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (long long int i = 0; i < n; i++)
#define _rep(i, m, n) for (long long int i = m; i < n; i++)
#define print(n) std::cout << n << std::endl
#define _print(n) std::cout << n
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int N = 1000000;
const ll mod = 1000000007;
using Graph = vector<vector<int>>;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

ll gcd(ll A, ll B) {
  if (B == 0)
    return A;
  return gcd(B, A % B);
}
ll lcm(ll A, ll B) { return A * B / gcd(A, B); }

/*------------------------------------------------------------------*/

int main() {
  int n;
  cin >> n;
  while (true) {
    if (n == 1) {
      cout << n << endl;
      return 0;
    }
    for (int i = 1; i < n; i++) {
      for (int j = 0; j < 7; j++) {
        if (pow(i, j) == n) {
          cout << n << endl;
          return 0;
        }
      }
    }
    n--;
  }
}