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

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

int main() {
  cout << setprecision(15);
  char s[2000000];
  scanf("%s", s);
  int ans = 0;
  const int n = strlen(s);
  for (int length = 1; length < n; ++length) {
    if (n - length <= ans)
      break;
    int prev_length = length;
    int count = 1;
    for (int j = length; j < n;) {
      if (prev_length == 1 && s[j] == s[j - 1]) {
        if (j > n - 2)
          break;
        j += 2;
        prev_length = 2;
        count++;
        continue;
      }
      prev_length = 1;
      j++;
      count++;
    }
    ans = max(ans, count);
  }
  cout << ans << endl;
  return 0;
}


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

int main() {
  cout << setprecision(15);
  char s[2000000];
  scanf("%s", s);
  int ans = 0;
  const int n = strlen(s);
  for (int length = 1; length <= 2; ++length) {
    if (n - length <= ans)
      break;
    int prev_length = length;
    int count = 1;
    for (int j = length; j < n;) {
      if (prev_length == 1 && s[j] == s[j - 1]) {
        if (j > n - 2)
          break;
        j += 2;
        prev_length = 2;
        count++;
        continue;
      }
      prev_length = 1;
      j++;
      count++;
    }
    ans = max(ans, count);
  }
  cout << ans << endl;
  return 0;
}
Problem p02939 (C++)

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

using namespace std;

string s;

void Process() {
  int i = 0, cou = 0, state = 0;

  while (i <= s.length() - 1) {
    if (s[i] == s[i + 1]) {
      if (state == 0) {
        i += 2;
        state = 1;
      } else {
        state = 0;
        i++;
      }
    } else {
      i++;
      state = 0;
    }

    // cout << i << " ";

    cou++;
  }
  // cout << endl;

  cout << cou;
}

void Input() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  freopen("abc.txt", "r", stdin);

  cin >> s;
}

int main() {
  Input();
  Process();
  return 0;
}


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

using namespace std;

string s;

void Process() {
  int i = 0, cou = 0, state = 0;

  while (i <= s.length() - 1) {
    if (s[i] == s[i + 1]) {
      if (state == 0) {
        i += 2;
        state = 1;
      } else {
        state = 0;
        i++;
      }
    } else {
      i++;
      state = 0;
    }

    // cout << i << " ";

    cou++;
  }
  // cout << endl;

  cout << cou;
}

void Input() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  // freopen("abc.txt", "r", stdin);

  cin >> s;
}

int main() {
  Input();
  Process();
  return 0;
}
Problem p02939 (C++)

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

typedef long long ll;
typedef pair<ll, ll> pll;
typedef long double ld;
#define mp make_pair
#define f first
#define s second
#define pb push_back

const int N = 2e5 + 5;
const int M = 17 + 5;
const int W = 1000 + 5;
const int OO = 1e9;
const int mod = 1e9 + 7;

string s;
map<pair<int, string>, int> dp, vis;

int solve(int idx, string x) {

  if (idx == s.size())
    return 0;

  int &ret = dp[mp(idx, x)];

  if (vis[mp(idx, x)])
    return ret;

  ret = -OO;

  string c;
  c += s[idx];

  if (x != c)
    ret = max(ret, 1 + solve(idx + 1, c));

  if (idx < s.size() - 1) {
    c += s[idx + 1];
    if (x != c)
      ret = max(ret, 1 + solve(idx + 2, c));
  }

  return ret;
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);

  cin >> s;

  cout << solve(0, "") << '\n';

  return 0;
}

Fixed submission:
// #pragma GCC optimize ('O3')
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;
typedef long double ld;
#define mp make_pair
#define f first
#define s second
#define pb push_back

const int N = 2e5 + 5;
const int M = 17 + 5;
const int W = 1000 + 5;
const int OO = 1e9;
const int mod = 1e9 + 7;

string s;
map<pair<int, string>, int> dp, vis;

int solve(int idx, string x) {

  if (idx == s.size())
    return 0;

  int &ret = dp[mp(idx, x)];

  if (vis[mp(idx, x)])
    return ret;

  vis[mp(idx, x)] = 1;

  ret = -OO;

  string c;
  c += s[idx];

  if (x != c)
    ret = max(ret, 1 + solve(idx + 1, c));

  if (idx < s.size() - 1) {
    c += s[idx + 1];
    if (x != c)
      ret = max(ret, 1 + solve(idx + 2, c));
  }

  return ret;
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);

  cin >> s;

  cout << solve(0, "") << '\n';

  return 0;
}
Problem p02939 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define REP1(i, a, b) for (int i = a; i <= (int)(b); i++)
#define ALL(x) begin(x), end(x)
#define PB push_back
using namespace std;
typedef int64_t LL;
typedef vector<int> VI;
typedef pair<int, int> PII;

template <class T> inline bool chmax(T &a, const T &b) {
  return b > a ? a = b, true : false;
}
template <class T> inline bool chmin(T &a, const T &b) {
  return b < a ? a = b, true : false;
}
template <class T> using MaxHeap = priority_queue<T>;
template <class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>;
template <class T, class F = less<T>> void sort_uniq(vector<T> &v, F f = F()) {
  sort(begin(v), end(v), f);
  v.resize(unique(begin(v), end(v)) - begin(v));
}
const int N = 2e5 + 5;
string s;
int dp[N];
int solve(int i, int prv) {
  if (i >= s.size())
    return 0;
  if (dp[i])
    return dp[i];
  int res = 0;
  for (int j = i; j < s.size(); j++) {
    if (s.substr(prv, i - prv) == s.substr(i, j - i + 1))
      continue;
    chmax(res, solve(j + 1, i) + 1);
  }
  return dp[i] = res;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> s;
  cout << solve(0, 0) << '\n';
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define REP1(i, a, b) for (int i = a; i <= (int)(b); i++)
#define ALL(x) begin(x), end(x)
#define PB push_back
using namespace std;
typedef int64_t LL;
typedef vector<int> VI;
typedef pair<int, int> PII;

template <class T> inline bool chmax(T &a, const T &b) {
  return b > a ? a = b, true : false;
}
template <class T> inline bool chmin(T &a, const T &b) {
  return b < a ? a = b, true : false;
}
template <class T> using MaxHeap = priority_queue<T>;
template <class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>;
template <class T, class F = less<T>> void sort_uniq(vector<T> &v, F f = F()) {
  sort(begin(v), end(v), f);
  v.resize(unique(begin(v), end(v)) - begin(v));
}
const int N = 2e5 + 5;
string s;
int dp[N];
int solve(int i, int prv) {
  if (i >= s.size())
    return 0;
  if (dp[i])
    return dp[i];
  int res = 0;
  for (int j = i; j <= min(i + 2, (int)s.size()); j++) {
    if (s.substr(prv, i - prv) == s.substr(i, j - i + 1))
      continue;
    chmax(res, solve(j + 1, i) + 1);
  }
  return dp[i] = res;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> s;
  cout << solve(0, 0) << '\n';
  return 0;
}
Problem p02939 (C++)

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

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using edge = pair<ll, P>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
using Graph = vector<vector<ll>>;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

#include "cpp-pyprint/pyprint.h"

#endif

int dp[100005][2];

void Main() {
  string s;
  cin >> s;
  int n = s.size();
  if (n == 1) {
    cout << 1 << endl;
    return;
  } else {
    if (s[0] == s[1]) {
      dp[0][0] = 1;
      dp[0][1] = 0;
      dp[1][0] = 1;
      dp[1][1] = 1;
    } else {
      dp[0][0] = 1;
      dp[0][1] = 0;
      dp[1][0] = 2;
      dp[1][1] = 1;
    }
  }
  rep2(i, 2, n) {
    if (s[i] == s[i - 1]) {
      dp[i][0] += dp[i - 1][1] + 1;
      dp[i][1] += dp[i - 2][0] + 1;
    } else {
      dp[i][0] += dp[i - 1][0] + 1;
      dp[i][1] += dp[i - 2][0] + 1;
    }
  }
  cout << max(dp[n - 1][0], dp[n - 1][1]) << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
}


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

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using edge = pair<ll, P>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
using Graph = vector<vector<ll>>;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

#include "cpp-pyprint/pyprint.h"

#endif

int dp[200005][2];

void Main() {
  string s;
  cin >> s;
  int n = s.size();
  if (n == 1) {
    cout << 1 << endl;
    return;
  } else {
    if (s[0] == s[1]) {
      dp[0][0] = 1;
      dp[0][1] = 0;
      dp[1][0] = 1;
      dp[1][1] = 1;
    } else {
      dp[0][0] = 1;
      dp[0][1] = 0;
      dp[1][0] = 2;
      dp[1][1] = 1;
    }
  }
  rep2(i, 2, n) {
    if (s[i] == s[i - 1]) {
      dp[i][0] += dp[i - 1][1] + 1;
      dp[i][1] += dp[i - 2][0] + 1;
    } else {
      dp[i][0] += dp[i - 1][0] + 1;
      dp[i][1] += dp[i - 2][0] + 1;
    }
  }
  cout << max(dp[n - 1][0], dp[n - 1][1]) << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
}
Problem p02940 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5, mod = 998244353;
typedef long long ll;
int n, f[1 << 3];
char s[N];
int main() {
  scanf("%d", &n);
  f[0] = n;
  scanf("%s", s + 1);
  ll ans = 1;
  for (int l = 1; l <= n * 3; l++) {
    int t = 0;
    if (s[l] == 'R')
      t = 1;
    if (s[l] == 'G')
      t = 2;
    bool flag = true;
    for (int j = 2; j >= 0 && flag; j--) {
      for (int i = (1 << 3) - 1; i >= 0; i--)
        if ((!(1 << t & i)) && f[i] && __builtin_popcount(i) == j) {
          ans = ans * f[i] % mod;
          f[i]--;
          f[i ^ (1 << t)]++;
          flag = false;
          break;
        }
    }
  }
  printf("%lld\n", ans);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5, mod = 998244353;
typedef long long ll;
int n, f[1 << 3];
char s[N];
int main() {
  scanf("%d", &n);
  f[0] = n;
  scanf("%s", s + 1);
  ll ans = 1;
  for (int l = 1; l <= n * 3; l++) {
    int t = 0;
    if (s[l] == 'R')
      t = 1;
    if (s[l] == 'G')
      t = 2;
    bool flag = true;
    for (int j = 2; j >= 0 && flag; j--) {
      for (int i = (1 << 3) - 1; i >= 0; i--)
        if ((!(1 << t & i)) && f[i] && __builtin_popcount(i) == j) {
          ans = ans * f[i] % mod;
          f[i]--;
          f[i ^ (1 << t)]++;
          flag = false;
          break;
        }
    }
  }
  printf("%lld\n", ans);
}
Problem p02940 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair<int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcount
#define ll long long
#define mp make_pair
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define Time (double)clock() / CLOCKS_PER_SEC
const int N = 1e5 + 7, MOD = 998244353;
string s;
vector<int> a[3];
int type(char c) {
  if (c == 'R')
    return 0;
  else if (c == 'G')
    return 1;
  else
    return 2;
}
int n, lmao_rofl[N], curva[4];
signed main() {
#ifdef HOME
  freopen("input.txt", "r", stdin);
#else
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#endif
  cin >> n >> s;
  for (int i = 0; i < n * 3; ++i)
    a[type(s[i])].app(i);
  for (int i = 0; i < 3; ++i)
    sort(all(a[i]));
  for (int i = 0; i < n; ++i) {
    vector<int> t;
    for (int j = 0; j < 3; ++j)
      t.app(a[j][i]);
    sort(all(t));
    for (int j = 0; j < 3; ++j)
      lmao_rofl[t[j]] = j;
  }
  int ans = 1;
  curva[0] = n;
  for (int i = 0; i < 3 * n; ++i) {
    ans = (ans * curva[lmao_rofl[i]]) % MOD;
    --curva[lmao_rofl[i]];
    ++curva[lmao_rofl[i] + 1];
  }
  cout << ans << '\n';
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair<int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcount
#define ll long long
#define mp make_pair
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define Time (double)clock() / CLOCKS_PER_SEC
const int N = 3e5 + 7, MOD = 998244353;
string s;
vector<int> a[3];
int type(char c) {
  if (c == 'R')
    return 0;
  else if (c == 'G')
    return 1;
  else
    return 2;
}
int n, lmao_rofl[N], curva[4];
signed main() {
#ifdef HOME
  freopen("input.txt", "r", stdin);
#else
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#endif
  cin >> n >> s;
  for (int i = 0; i < n * 3; ++i)
    a[type(s[i])].app(i);
  for (int i = 0; i < 3; ++i)
    sort(all(a[i]));
  for (int i = 0; i < n; ++i) {
    vector<int> t;
    for (int j = 0; j < 3; ++j)
      t.app(a[j][i]);
    sort(all(t));
    for (int j = 0; j < 3; ++j)
      lmao_rofl[t[j]] = j;
  }
  int ans = 1;
  curva[0] = n;
  for (int i = 0; i < 3 * n; ++i) {
    ans = (ans * curva[lmao_rofl[i]]) % MOD;
    --curva[lmao_rofl[i]];
    ++curva[lmao_rofl[i] + 1];
  }
  cout << ans << '\n';
}
Problem p02940 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ss(x) (int)x.size()
#define cat(x) cerr << #x << " = " << x << endl
#define rep(i, j, n) for (int i = j; i <= n; ++i)
#define per(i, j, n) for (int i = n; j <= i; --i)

using ll = long long;
using namespace std;

const int N = 2e5 + 100;
const int MOD = 998244353;

int n, type[N], cnt[3];
char s[N];
vector<int> g[3];

int main() {
  scanf("%d%s", &n, s + 1);
  rep(i, 1, 3 * n) {
    if (s[i] == 'R')
      g[0].pb(i);
    if (s[i] == 'G')
      g[1].pb(i);
    if (s[i] == 'B')
      g[2].pb(i);
  }
  rep(i, 0, n - 1) {
    vector<int> v;
    rep(j, 0, 2) v.pb(g[j][i]);
    sort(v.begin(), v.end());
    rep(j, 0, 2) type[v[j]] = j;
  }
  ll res = 1;
  rep(i, 1, 3 * n) {
    if (type[i] == 0)
      res = res * (n - cnt[0]) % MOD;
    if (type[i] == 1)
      res = res * (cnt[0] - cnt[1]) % MOD;
    if (type[i] == 2)
      res = res * (cnt[1] - cnt[2]) % MOD;
    cnt[type[i]]++;
  }
  printf("%lld\n", res);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ss(x) (int)x.size()
#define cat(x) cerr << #x << " = " << x << endl
#define rep(i, j, n) for (int i = j; i <= n; ++i)
#define per(i, j, n) for (int i = n; j <= i; --i)

using ll = long long;
using namespace std;

const int N = 3e5 + 100;
const int MOD = 998244353;

int n, type[N], cnt[3];
char s[N];
vector<int> g[3];

int main() {
  scanf("%d%s", &n, s + 1);
  rep(i, 1, 3 * n) {
    if (s[i] == 'R')
      g[0].pb(i);
    if (s[i] == 'G')
      g[1].pb(i);
    if (s[i] == 'B')
      g[2].pb(i);
  }
  rep(i, 0, n - 1) {
    vector<int> v;
    rep(j, 0, 2) v.pb(g[j][i]);
    sort(v.begin(), v.end());
    rep(j, 0, 2) type[v[j]] = j;
  }
  ll res = 1;
  rep(i, 1, 3 * n) {
    if (type[i] == 0)
      res = res * (n - cnt[0]) % MOD;
    if (type[i] == 1)
      res = res * (cnt[0] - cnt[1]) % MOD;
    if (type[i] == 2)
      res = res * (cnt[1] - cnt[2]) % MOD;
    cnt[type[i]]++;
  }
  printf("%lld\n", res);
  return 0;
}
Problem p02940 (C++)

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

#define ll long long
#define fornum(A, B, C) for (A = B; A < C; A++)
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>

using namespace std;

/////////////////////////////////////////////////////
#define MOD 998244353ll
char S[303030];
ll N, N3, rgb[5];
ll Fa[303030];
ll i, j, k, ans;

ll moddiv(ll a, ll b) {
  a %= MOD;
  b %= MOD;
  for (ll m = MOD - 2; m > 0; m /= 2) {
    if (m & 1) {
      a = a * b % MOD;
    }
    b = b * b % MOD;
  }
  return a;
}

int main() {
  scanf("%d%s", &N, S);
  N3 = N * 3;

  fornum(i, 0, N3) {
    switch (S[i]) {
    case 'R':
      rgb[i] = 0;
      break;
    case 'G':
      rgb[i] = 1;
      break;
    case 'B':
      rgb[i] = 2;
      break;
    }
  }
  ll rgbn[] = {0, 0, 0};
  ll mk[] = {N, 0, 0, 0};
  ans = 1;
  fornum(i, 0, N3) {
    k = 0;
    fornum(j, 0, 3) {
      if (rgbn[rgb[i]] < rgbn[j]) {
        ++k;
      }
    }
    rgbn[rgb[i]]++;
    (ans *= mk[k]) %= MOD;
    --mk[k];
    ++mk[k + 1];
  }

  printf("%lld\n", ans);

  return 0;
}

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

#define ll long long
#define fornum(A, B, C) for (A = B; A < C; A++)
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>

using namespace std;

/////////////////////////////////////////////////////
#define MOD 998244353ll
char S[303030];
ll N, N3, rgb[303030];
ll Fa[303030];
ll i, j, k, ans;

ll moddiv(ll a, ll b) {
  a %= MOD;
  b %= MOD;
  for (ll m = MOD - 2; m > 0; m /= 2) {
    if (m & 1) {
      a = a * b % MOD;
    }
    b = b * b % MOD;
  }
  return a;
}

int main() {
  scanf("%d%s", &N, S);
  N3 = N * 3;

  fornum(i, 0, N3) {
    switch (S[i]) {
    case 'R':
      rgb[i] = 0;
      break;
    case 'G':
      rgb[i] = 1;
      break;
    case 'B':
      rgb[i] = 2;
      break;
    }
  }
  ll rgbn[] = {0, 0, 0};
  ll mk[] = {N, 0, 0, 0};
  ans = 1;
  fornum(i, 0, N3) {
    k = 0;
    fornum(j, 0, 3) {
      if (rgbn[rgb[i]] < rgbn[j]) {
        ++k;
      }
    }
    rgbn[rgb[i]]++;
    (ans *= mk[k]) %= MOD;
    --mk[k];
    ++mk[k + 1];
  }

  printf("%lld\n", ans);

  return 0;
}
Problem p02940 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
#define mod 998244353
#define MN 101000

int n, cnt[8];
char s[MN];

int main() {
  scanf("%d", &n);
  scanf("%s", s + 1);
  int ans = 1;
  for (int i = 1; i <= n; i++)
    ans = 1ll * ans * i % mod;

  for (int i = 1; i <= 3 * n; i++) {
    int x;
    if (s[i] == 'R')
      x = 1;
    if (s[i] == 'G')
      x = 2;
    if (s[i] == 'B')
      x = 4;
    if (cnt[x ^ 7])
      ans = 1ll * ans * cnt[x ^ 7] % mod, cnt[x ^ 7]--;
    else if (x != 1 && cnt[1])
      ans = 1ll * ans * cnt[1] % mod, cnt[1]--, cnt[x ^ 1]++;
    else if (x != 2 && cnt[2])
      ans = 1ll * ans * cnt[2] % mod, cnt[2]--, cnt[x ^ 2]++;
    else if (x != 4 && cnt[4])
      ans = 1ll * ans * cnt[4] % mod, cnt[4]--, cnt[x ^ 4]++;
    else
      cnt[x]++;
  }

  printf("%d\n", ans);
}

Fixed submission:
#include <cstdio>
#include <iostream>
#define mod 998244353
#define MN 301000

int n, cnt[8];
char s[MN];

int main() {
  scanf("%d", &n);
  scanf("%s", s + 1);
  int ans = 1;
  for (int i = 1; i <= n; i++)
    ans = 1ll * ans * i % mod;

  for (int i = 1; i <= 3 * n; i++) {
    int x;
    if (s[i] == 'R')
      x = 1;
    if (s[i] == 'G')
      x = 2;
    if (s[i] == 'B')
      x = 4;
    if (cnt[x ^ 7])
      ans = 1ll * ans * cnt[x ^ 7] % mod, cnt[x ^ 7]--;
    else if (x != 1 && cnt[1])
      ans = 1ll * ans * cnt[1] % mod, cnt[1]--, cnt[x ^ 1]++;
    else if (x != 2 && cnt[2])
      ans = 1ll * ans * cnt[2] % mod, cnt[2]--, cnt[x ^ 2]++;
    else if (x != 4 && cnt[4])
      ans = 1ll * ans * cnt[4] % mod, cnt[4]--, cnt[x ^ 4]++;
    else
      cnt[x]++;
  }

  printf("%d\n", ans);
}
Problem p02940 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 998244353;
char s[100005];
int n, cnt[3];
int main() {
  while (~scanf("%d", &n)) {
    ll ans = 1;
    scanf("%s", s + 1);
    for (int i = 1; i <= n * 3; i++) { // 思路填满再说
      int x[3], _x[3];
      x[0] = cnt[0], x[1] = cnt[1], x[2] = cnt[2]; // 现有的
      sort(x, x + 3);
      int color = (s[i] == 'R' ? 0 : (s[i] == 'G' ? 1 : 2));
      cnt[color]++;

      _x[0] = cnt[0], _x[1] = cnt[1], _x[2] = cnt[2]; // 进来的
      sort(_x, _x + 3);
      if (_x[2] != x[2]) {
        ;                         // 进了一个最多的，给下一个人
      } else if (_x[0] != x[0]) { // 进了一个最少，选一个人给
        ans = 1ll * ans * (x[1] - x[0]) % mod;
      } else { // 进了一个中间的，选一个人给
        ans = 1ll * ans * (x[2] - x[1]) % mod;
      }
    }
    for (int i = 1; i <= n; i++) // A[n,n]
      ans = 1ll * ans * i % mod;
    printf("%d\n", ans);
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 998244353;
char s[300005];
int n, cnt[3];
int main() {
  while (~scanf("%d", &n)) {
    ll ans = 1;
    scanf("%s", s + 1);
    for (int i = 1; i <= n * 3; i++) { // 思路填满再说
      int x[3], _x[3];
      x[0] = cnt[0], x[1] = cnt[1], x[2] = cnt[2]; // 现有的
      sort(x, x + 3);
      int color = (s[i] == 'R' ? 0 : (s[i] == 'G' ? 1 : 2));
      cnt[color]++;

      _x[0] = cnt[0], _x[1] = cnt[1], _x[2] = cnt[2]; // 进来的
      sort(_x, _x + 3);
      if (_x[2] != x[2]) {
        ;                         // 进了一个最多的，给下一个人
      } else if (_x[0] != x[0]) { // 进了一个最少，选一个人给
        ans = 1ll * ans * (x[1] - x[0]) % mod;
      } else { // 进了一个中间的，选一个人给
        ans = 1ll * ans * (x[2] - x[1]) % mod;
      }
    }
    for (int i = 1; i <= n; i++) // A[n,n]
      ans = 1ll * ans * i % mod;
    printf("%d\n", ans);
  }
  return 0;
}
Problem p02941 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))
using ll = long long;
using ld = long double;

int const nmax = 200000;
int target[1 + nmax], v[1 + nmax];

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

  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> target[i];
  for (int i = 0; i < n; i++)
    cin >> v[i];
  queue<int> q;
  for (int i = 0; i < n; i++)
    q.push(i);
  ll result = 0;
  while (0 < q.size()) {
    int pos = q.front();
    q.pop();
    if (v[pos] < v[(pos + n - 1) % n] || v[pos] < v[(pos + 1) % n])
      continue;
    if (v[pos] <= target[pos])
      continue;

    int stop = max(target[pos], max(v[(pos + 1) % n], v[(pos + (n - 1)) % n]));
    int jumps = (v[pos] - stop) / (v[(pos + 1) % n] + v[(pos + (n - 1)) % n]);
    jumps = max(jumps, 1);
    if (0 < jumps) {
      v[pos] -= jumps * (v[(pos + 1) % n] + v[(pos + (n - 1)) % n]);
      q.push((pos + 1) % n);
      q.push((pos + n - 1) % n);
      q.push(pos);
      result += jumps;
    }
  }
  for (int i = 0; i < n; i++)
    if (v[i] != target[i]) {
      cout << -1;
      return 0;
    }
  cout << result;
  return 0;
}


Fixed submission:
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))
using ll = long long;
using ld = long double;

int const nmax = 200000;
int target[1 + nmax], v[1 + nmax];

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

  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> target[i];
  for (int i = 0; i < n; i++)
    cin >> v[i];
  queue<int> q;
  for (int i = 0; i < n; i++)
    q.push(i);
  ll result = 0;
  while (0 < q.size()) {
    int pos = q.front();
    q.pop();
    if (v[pos] < v[(pos + n - 1) % n] || v[pos] < v[(pos + 1) % n])
      continue;
    if (v[pos] <= target[pos])
      continue;

    int stop = max(target[pos], max(v[(pos + 1) % n], v[(pos + (n - 1)) % n]));
    int jumps = (v[pos] - stop) / (v[(pos + 1) % n] + v[(pos + (n - 1)) % n]);
    jumps = max(jumps, 1);
    if (0 < jumps) {
      v[pos] -= jumps * (v[(pos + 1) % n] + v[(pos + (n - 1)) % n]);
      q.push((pos + 1) % n);
      q.push((pos + n - 1) % n);
      q.push(pos);
      result += jumps;
    }
    if (v[pos] < target[pos])
      break;
  }
  for (int i = 0; i < n; i++)
    if (v[i] != target[i]) {
      cout << -1;
      return 0;
    }
  cout << result;
  return 0;
}
Problem p02941 (C++)

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

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define fi first
#define se second
#define mp make_pair
#define fastIO                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);

const int N = (int)2e5 + 9;
int a[N], b[N];
bool is[N];

int n;
priority_queue<pii> tf;

void check(int x) {
  if (is[x]) {
    return;
  }
  if (b[x] > a[x] && b[x] > b[(x - 1 + n) % n] + b[(x + 1) % n]) {
    tf.push(mp(b[x], x));
    is[x] = true;
  }
}

int main() {
  fastIO;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i];
  }
  for (int i = 0; i < n; i++) {
    check(i);
  }
  ll res = 0;
  int idx;
  ll dd;
  while (!tf.empty()) {
    idx = tf.top().se;
    tf.pop();
    is[idx] = false;
    dd = b[idx] - a[idx];
    res += dd / (b[(idx + 1) % n] + b[(idx - 1 + n) % n]);
    b[idx] = a[idx] + (dd % (b[(idx + 1) % n] + b[(idx - 1 + n) % n]));
    check(idx);
    check((idx - 1 + n) % n);
    check((idx + 1) % n);
  }
  for (int i = 0; i < n; i++) {
    if (a[i] != b[i]) {
      cout << -1;
      return 0;
    }
  }
  cout << res << "\n";
  return 0;
}


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

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define fi first
#define se second
#define mp make_pair
#define fastIO                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);

const int N = (int)2e5 + 9;
int a[N], b[N];
bool is[N];

int n;
priority_queue<pii> tf;

void check(int x) {
  if (is[x]) {
    return;
  }
  if (b[x] > a[x] && b[x] > b[(x - 1 + n) % n] + b[(x + 1) % n]) {
    tf.push(mp(b[x], x));
    is[x] = true;
  }
}

int main() {
  fastIO;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i];
  }
  for (int i = 0; i < n; i++) {
    check(i);
  }
  ll res = 0;
  int idx;
  ll dd;
  while (!tf.empty()) {
    idx = tf.top().se;
    tf.pop();
    is[idx] = false;
    dd = b[idx] - a[idx];
    res += dd / (b[(idx + 1) % n] + b[(idx - 1 + n) % n]);
    b[idx] = a[idx] + (dd % (b[(idx + 1) % n] + b[(idx - 1 + n) % n]));
    check((idx - 1 + n) % n);
    check((idx + 1) % n);
  }
  for (int i = 0; i < n; i++) {
    if (a[i] != b[i]) {
      cout << -1;
      return 0;
    }
  }
  cout << res << "\n";
  return 0;
}
Problem p02941 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define MOD 1000000007
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define endl '\n'
#define PI 3.14159265359d
#define sz(x) (int)x.size()
#define rep(i, a, b) for (int i = a; i < b; i++)
#define magic 109
#define INF INT_MAX
using namespace std;
const int maxn = 200000;

int n;
int a[maxn], b[maxn];
priority_queue<int> pq;
void check_insert(int i) {
  int i_prev = (i + n - 1) % n, i_next = (i + 1) % n;

  if (b[i] - a[i] >= b[i_prev] + b[i_next]) {
    pq.push(i);
  }
}

void solve() {
  cin >> n;
  rep(i, 0, n) cin >> a[i];
  rep(i, 0, n) cin >> b[i];
  int res = 0;
  rep(i, 0, n) { check_insert(i); }

  while (!pq.empty()) {
    int i = pq.top();
    pq.pop();
    int i_prev = (i + n - 1) % n, i_next = (i + 1) % n;
    int k = (b[i] - a[i]) / (b[i_prev] + b[i_next]);
    b[i] -= k * (b[i_prev] + b[i_next]);
    res += k;
    check_insert(i_prev);
    check_insert(i);
    check_insert(i_next);
  }
  rep(i, 0, n) if (a[i] != b[i]) {
    cout << -1;
    return;
  }
  cout << res;
}

signed main() {
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int t = 1;
  // cin>>t;

  while (t--)
    solve();

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define MOD 1000000007
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define endl '\n'
#define PI 3.14159265359d
#define sz(x) (int)x.size()
#define rep(i, a, b) for (int i = a; i < b; i++)
#define magic 109
#define INF INT_MAX
using namespace std;
const int maxn = 200000;

int n;
int a[maxn], b[maxn];
priority_queue<int> pq;
void check_insert(int i) {
  int i_prev = (i + n - 1) % n, i_next = (i + 1) % n;

  if (b[i] - a[i] >= b[i_prev] + b[i_next]) {
    pq.push(i);
  }
}

void solve() {
  cin >> n;
  rep(i, 0, n) cin >> a[i];
  rep(i, 0, n) cin >> b[i];
  int res = 0;
  rep(i, 0, n) { check_insert(i); }

  while (!pq.empty()) {
    int i = pq.top();
    pq.pop();
    int i_prev = (i + n - 1) % n, i_next = (i + 1) % n;
    int k = (b[i] - a[i]) / (b[i_prev] + b[i_next]);
    b[i] -= k * (b[i_prev] + b[i_next]);
    res += k;
    check_insert(i_prev);
    check_insert(i);
    check_insert(i_next);
  }
  rep(i, 0, n) if (a[i] != b[i]) {
    cout << -1;
    return;
  }
  cout << res;
}

signed main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int t = 1;
  // cin>>t;

  while (t--)
    solve();

  return 0;
}
Problem p02941 (C++)

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

using namespace std;

using i64 = long long;

const i64 MOD = 998244353;
const i64 INF = i64(1e18) + 7;

template <typename T> bool chmin(T &x, T y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}

template <typename T> bool chmax(T &x, T y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}

signed main() {
  int n;
  cin >> n;
  vector<i64> a(n), v(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  for (int i = 0; i < n; ++i)
    cin >> v[i];
  priority_queue<pair<i64, int>> que;
  for (int i = 0; i < n; ++i)
    que.emplace(v[i], i);
  i64 ans = 0;
  while (!que.empty()) {
    i64 val;
    int i;
    tie(val, i) = que.top();
    que.pop();
    i64 su = v[(i + 1) % n] + v[(i + n - 1) % n];
    i64 ma = max({v[(i + 1) % n], v[(i + n - 1) % n], a[i]});

    i64 diff = val - ma;
    i64 cnt = (diff + su - 1) / su;
    ans += cnt;
    val -= su * cnt;
    v[i] = val;
    if (val > a[i])
      que.emplace(val, i);
  }
  cout << (a == v ? ans : -1) << endl;
}


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

using namespace std;

using i64 = long long;

const i64 MOD = 998244353;
const i64 INF = i64(1e18) + 7;

template <typename T> bool chmin(T &x, T y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}

template <typename T> bool chmax(T &x, T y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}

signed main() {
  int n;
  cin >> n;
  vector<i64> a(n), v(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  for (int i = 0; i < n; ++i)
    cin >> v[i];
  priority_queue<pair<i64, int>> que;
  for (int i = 0; i < n; ++i)
    que.emplace(v[i], i);
  i64 ans = 0;
  while (!que.empty()) {
    i64 val;
    int i;
    tie(val, i) = que.top();
    que.pop();
    i64 su = v[(i + 1) % n] + v[(i + n - 1) % n];
    i64 ma = max({v[(i + 1) % n], v[(i + n - 1) % n], a[i]});

    i64 diff = val - ma;
    i64 cnt = (diff + su - 1) / su;
    ans += cnt;
    val -= su * cnt;
    v[i] = val;
    if (val > a[i])
      que.emplace(val, i);
    if (val < a[i]) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << (a == v ? ans : -1) << endl;
}
Problem p02941 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define f(i, x, n) for (int i = x; i < n; i++)
#define all(c) c.begin(), c.end()
#define int ll
#define print(x) cerr << (#x) << "is " << x << "\n"
using ll = long long;
using pii = pair<int, int>;
const int MOD = 1e9 + 7, N = 1e5 + 10;

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  vector<int> a(n), b(n);
  set<pii> sa, sb;
  f(i, 0, n) {
    cin >> a[i];
    sa.insert({a[i], i});
  }
  f(i, 0, n) {
    cin >> b[i];
    sb.insert({b[i], i});
  }
  vector<int> val1(n, 0), val2(n, 0);
  while (!sa.empty()) {
    auto k = sa.end();
    k--;
    sa.erase(k);
    int in = (*k).S;
    int sm = a[(in - 1 + n) % n] + a[(in + 1) % n];
    if (!sm)
      continue;
    int kk = a[in] / sm;
    if (kk) {
      a[in] -= kk * sm;
      val1[in] += kk;
      sa.insert({a[in], in});
    }
  }
  while (!sb.empty()) {
    auto k = sb.end();
    k--;
    sb.erase(k);
    int in = (*k).S;
    int sm = b[(in - 1 + n) % n] + b[(in + 1) % n];
    if (!sm)
      continue;
    int kk = b[in] / sm;
    if (kk) {
      b[in] -= kk * sm;
      val2[in] += kk;
      sb.insert({b[in], in});
    }
  }
  int ans = 0;
  f(i, 0, n) {
    if (a[i] != b[i]) {
      cout << "-1\n";
      return 0;
    }
    ans += val2[i];
    ans -= val1[i];
  }
  cout << ans << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define f(i, x, n) for (int i = x; i < n; i++)
#define all(c) c.begin(), c.end()
#define int ll
#define print(x) cerr << (#x) << "is " << x << "\n"
using ll = long long;
using pii = pair<int, int>;
const int MOD = 1e9 + 7, N = 1e5 + 10;

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  // #ifndef ONLINE_JUDGE
  //    freopen("input.txt", "r", stdin);
  //    freopen("output.txt", "w", stdout);
  // #endif
  int n;
  cin >> n;
  vector<int> a(n), b(n);
  set<pii> sa, sb;
  f(i, 0, n) {
    cin >> a[i];
    sa.insert({a[i], i});
  }
  f(i, 0, n) {
    cin >> b[i];
    sb.insert({b[i], i});
  }
  vector<int> val1(n, 0), val2(n, 0);
  while (!sa.empty()) {
    auto k = sa.end();
    k--;
    sa.erase(k);
    int in = (*k).S;
    int sm = a[(in - 1 + n) % n] + a[(in + 1) % n];
    if (!sm)
      continue;
    int kk = a[in] / sm;
    if (kk) {
      a[in] -= kk * sm;
      val1[in] += kk;
      sa.insert({a[in], in});
    }
  }
  while (!sb.empty()) {
    auto k = sb.end();
    k--;
    sb.erase(k);
    int in = (*k).S;
    int sm = b[(in - 1 + n) % n] + b[(in + 1) % n];
    if (!sm)
      continue;
    int kk = b[in] / sm;
    if (kk) {
      b[in] -= kk * sm;
      val2[in] += kk;
      sb.insert({b[in], in});
    }
  }
  int ans = 0;
  f(i, 0, n) {
    if (a[i] != b[i]) {
      cout << "-1\n";
      return 0;
    }
    ans += val2[i];
    ans -= val1[i];
  }
  cout << ans << '\n';
  return 0;
}
Problem p02941 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef stack<int> S;
const int N = 200200;
int n;
int a[N];
int b[N];
S s;
void gg() {
  puts("-1");
  exit(0);
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  for (int i = 0; i < n; i++) {
    scanf("%d", &b[i]);
  }
  for (int i = 0; i < n; i++) {
    s.push(i);
  }
  LL ans = 0;
  while (!s.empty()) {
    int x = s.top();
    s.pop();
    int l = (x - 1 + n) % n;
    int r = (x + 1) % n;
    LL db = b[l] + b[r];
    LL da = b[x] - a[x];
    if (b[x] <= db) {
      continue;
    }
    int o = da / db;
    if (o == 0) {
      continue;
    }
    ans += o;
    b[x] -= o * db;
    assert(b[x] >= a[x]);
    s.push(l);
    s.push(r);
  }
  for (int i = 0; i < n; i++) {
    if (a[i] != b[i]) {
      gg();
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef stack<int> S;
const int N = 200200;
int n;
int a[N];
int b[N];
S s;
void gg() {
  puts("-1");
  exit(0);
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  for (int i = 0; i < n; i++) {
    scanf("%d", &b[i]);
    if (b[i] < a[i]) {
      gg();
    }
  }
  for (int i = 0; i < n; i++) {
    s.push(i);
  }
  LL ans = 0;
  while (!s.empty()) {
    int x = s.top();
    s.pop();
    int l = (x - 1 + n) % n;
    int r = (x + 1) % n;
    LL db = b[l] + b[r];
    LL da = b[x] - a[x];
    if (b[x] <= db) {
      continue;
    }
    int o = da / db;
    if (o == 0) {
      continue;
    }
    ans += o;
    b[x] -= o * db;
    assert(b[x] >= a[x]);
    s.push(l);
    s.push(r);
  }
  for (int i = 0; i < n; i++) {
    if (a[i] != b[i]) {
      gg();
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02941 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define REP(i, n) FOR(i, 0, n)
#define _ << " " <<
#define pb push_back
#define fi first
#define se second

typedef double lf;
typedef long long ll;
typedef pair<int, int> pii;

const int mod = 998244353;
int add(int x, int y) {
  x += y;
  if (x >= mod)
    return x - mod;
  return x;
}
int sub(int x, int y) {
  x -= y;
  if (x < 0)
    return x + mod;
  return x;
}
int mul(int x, int y) { return (ll)x * y % mod; }

const int MAXN = 2e5 + 5;

int a[MAXN], f[MAXN];

int n;
int M(int i) { return (i + n) % n; }

bool check(int i) { return a[i] > a[M(i + 1)] && a[i] > a[M(i - 1)]; }

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

  cin >> n;
  REP(i, n) cin >> f[i];
  REP(i, n) cin >> a[i];

  vector<int> good;
  REP(i, n) {
    if (check(i)) {
      good.pb(i);
    }
  }

  ll sol = 0;

  while (good.size()) {
    int i = good.back();
    good.pop_back();

    int sums = a[M(i + 1)] + a[M(i - 1)];
    int times =
        max(0, (a[i] - max(f[i], max(a[M(i + 1)], a[M(i - 1)])) - 1)) / sums;

    times++;
    sol += times;

    a[i] -= times * sums;
    if (check(M(i + 1)))
      good.pb(M(i + 1));
    if (check(M(i - 1)))
      good.pb(M(i - 1));
  }

  REP(i, n) if (a[i] != f[i]) {
    cout << -1 << endl;
    return 0;
  }

  cout << sol << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define REP(i, n) FOR(i, 0, n)
#define _ << " " <<
#define pb push_back
#define fi first
#define se second

typedef double lf;
typedef long long ll;
typedef pair<int, int> pii;

const int mod = 998244353;
int add(int x, int y) {
  x += y;
  if (x >= mod)
    return x - mod;
  return x;
}
int sub(int x, int y) {
  x -= y;
  if (x < 0)
    return x + mod;
  return x;
}
int mul(int x, int y) { return (ll)x * y % mod; }

const int MAXN = 2e5 + 5;

int a[MAXN], f[MAXN];

int n;
int M(int i) { return (i + n) % n; }

bool check(int i) { return a[i] > a[M(i + 1)] && a[i] > a[M(i - 1)]; }

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

  cin >> n;
  REP(i, n) cin >> f[i];
  REP(i, n) cin >> a[i];

  vector<int> good;
  REP(i, n) {
    if (check(i)) {
      good.pb(i);
    }
  }

  ll sol = 0;

  while (good.size()) {
    int i = good.back();
    good.pop_back();

    int sums = a[M(i + 1)] + a[M(i - 1)];
    int times =
        max(0, (a[i] - max(f[i], max(a[M(i + 1)], a[M(i - 1)])) - 1)) / sums;

    if (a[i] == f[i])
      times--;

    times++;
    sol += times;

    a[i] -= times * sums;
    if (check(M(i + 1)))
      good.pb(M(i + 1));
    if (check(M(i - 1)))
      good.pb(M(i - 1));
  }

  REP(i, n) if (a[i] != f[i]) {
    cout << -1 << endl;
    return 0;
  }

  cout << sol << endl;
}
Problem p02941 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
void read(int &x) {
  char ch;
  bool ok;
  for (ok = 0, ch = getchar(); !isdigit(ch); ch = getchar())
    if (ch == '-')
      ok = 1;
  for (x = 0; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
    ;
  if (ok)
    x = -x;
}
#define rg register
const int maxn = 6e5 + 10;
struct oo {
  int x, y;
};
priority_queue<oo> q;
int n, a[maxn], b[maxn], flag;
long long ans;
bool operator<(oo u, oo v) { return u.y > v.y; }
int main() {
  read(n);
  for (rg int i = 0; i < n; i++)
    read(a[i]);
  for (rg int i = 0; i < n; i++)
    read(b[i]);
  for (rg int i = 0; i < n; i++)
    if (b[i] >= b[(i + 1) % n] + b[(i - 1 + n) % n])
      q.push((oo){i, b[i]});
  while (q.size()) {
    int i = q.top().x, x = (i - 1 + n) % n, y = (i + 1) % n, now = b[x] + b[y];
    ans += b[i] / now, b[i] %= now;
    q.pop();
    if (b[x] >= b[(x + 1) % n] + b[(x - 1 + n) % n])
      q.push((oo){x, b[x]});
    if (b[y] >= b[(y - 1 + n) % n] + b[(y + 1) % n])
      q.push((oo){y, b[y]});
  }
  for (rg int i = 1; i <= n; i++)
    if (a[i] != b[i])
      flag = 1;
  printf("%lld\n", flag ? -1 : ans);
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
void read(int &x) {
  char ch;
  bool ok;
  for (ok = 0, ch = getchar(); !isdigit(ch); ch = getchar())
    if (ch == '-')
      ok = 1;
  for (x = 0; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
    ;
  if (ok)
    x = -x;
}
#define rg register
const int maxn = 6e5 + 10;
struct oo {
  int x, y;
};
priority_queue<oo> q;
int n, a[maxn], b[maxn], flag;
long long ans;
bool operator<(oo u, oo v) { return u.y > v.y; }
int main() {
  read(n);
  for (rg int i = 0; i < n; i++)
    read(a[i]);
  for (rg int i = 0; i < n; i++)
    read(b[i]);
  for (rg int i = 0; i < n; i++)
    if (b[i] >= b[(i + 1) % n] + b[(i - 1 + n) % n])
      q.push((oo){i, b[i]});
  while (q.size()) {
    int t, i = q.top().x, x = (i - 1 + n) % n, y = (i + 1) % n,
           now = b[x] + b[y];
    ans += (t = (b[i] - a[i]) / now), b[i] -= t * now;
    q.pop();
    if (b[x] >= b[(x + 1) % n] + b[(x - 1 + n) % n])
      q.push((oo){x, b[x]});
    if (b[y] >= b[(y - 1 + n) % n] + b[(y + 1) % n])
      q.push((oo){y, b[y]});
  }
  for (rg int i = 1; i <= n; i++)
    if (a[i] != b[i])
      flag = 1;
  printf("%lld\n", flag ? -1 : ans);
}
Problem p02941 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
#define mod 1000000007
#define N 200010
ll a[N], b[N], n, ans;
void calc() {
  for (int i = 0; i < n; i++) {
    int j = (i + 1) % n, k = (i + 2) % n;
    ll p = b[i] + b[k];
    ll cnt = (b[j] - a[j]) / p;
    ans += cnt;
    b[j] -= cnt * p;
    // cout<<p<<" "<<cnt<<" "<<(b[j]-a[j])<<":";
  }
  // for(int i=0;i<n;i++)cout<<b[i]<<" ";cout<<endl;
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    cin >> b[i];
  ans = 0;
  for (int i = 0; i < 300; i++)
    calc();
  bool ok = 1;
  for (int i = 0; i < n; i++)
    ok &= (a[i] == b[i]);
  if (ok)
    cout << ans << endl;
  else
    cout << -1 << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
#define mod 1000000007
#define N 200010
ll a[N], b[N], n, ans;
void calc() {
  for (int i = 0; i < n; i++) {
    int j = (i + 1) % n, k = (i + 2) % n;
    ll p = b[i] + b[k];
    ll cnt = (b[j] - a[j]) / p;
    ans += cnt;
    b[j] -= cnt * p;
    // cout<<p<<" "<<cnt<<" "<<(b[j]-a[j])<<":";
  }
  // for(int i=0;i<n;i++)cout<<b[i]<<" ";cout<<endl;
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    cin >> b[i];
  ans = 0;
  for (int i = 0; i < 100; i++)
    calc();
  bool ok = 1;
  for (int i = 0; i < n; i++)
    ok &= (a[i] == b[i]);
  if (ok)
    cout << ans << endl;
  else
    cout << -1 << endl;
  return 0;
}
Problem p02941 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef long double ld;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define F first
#define S second

const int mx = 200010;
const ll mod = 1e9 + 7;

int main() {
  int n;
  cin >> n;
  vector<ll> a(n), b(n);
  rep(i, n) { cin >> a[i]; }
  priority_queue<pair<ll, int>> q;
  rep(i, n) {
    cin >> b[i];
    q.push({b[i], i});
  }
  ll cnt = 0;
  while (!q.empty()) {
    ll p;
    int pos;
    tie(p, pos) = q.top();
    q.pop();
    int posl = pos - 1;
    if (posl < 0)
      posl += n;
    int posr = pos + 1;
    if (posr >= n)
      posr -= n;
    ll tmp = (b[pos] - a[pos]) / (b[posl] + b[posr]);
    b[pos] -= (b[posl] + b[posr]) * tmp;
    cnt += tmp;
    if (a[pos] != b[pos]) {
      q.push({b[pos], pos});
    }
  }
  if (a == b) {
    cout << cnt << endl;
  } else {
    cout << -1 << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef long double ld;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define F first
#define S second

const int mx = 200010;
const ll mod = 1e9 + 7;

int main() {
  int n;
  cin >> n;
  vector<ll> a(n), b(n);
  rep(i, n) { cin >> a[i]; }
  priority_queue<pair<ll, int>> q;
  rep(i, n) {
    cin >> b[i];
    q.push({b[i], i});
  }
  ll cnt = 0;
  while (!q.empty()) {
    ll p;
    int pos;
    tie(p, pos) = q.top();
    q.pop();
    int posl = pos - 1;
    if (posl < 0)
      posl += n;
    int posr = pos + 1;
    if (posr >= n)
      posr -= n;
    ll tmp = (b[pos] - a[pos]) / (b[posl] + b[posr]);
    b[pos] -= (b[posl] + b[posr]) * tmp;
    cnt += tmp;
    if (a[pos] != b[pos] && tmp) {
      q.push({b[pos], pos});
    }
  }
  if (a == b) {
    cout << cnt << endl;
  } else {
    cout << -1 << endl;
  }
  return 0;
}
Problem p02941 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
struct node {
  int v, p;
  bool operator<(const node &a) const { return v < a.v; }
};
int a[200005], b[200005];
priority_queue<node> q;

int main(void) {
  int n, i, x, p, l, tmp;
  long long ans = 0;
  node k;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  for (i = 0; i < n; i++) {
    scanf("%d", &b[i]);
    q.push((node){b[i], i});
  }
  while (!q.empty()) {
    k = q.top();
    q.pop();

    if (k.p == 0) {
      p = n - 1;
      l = 1;
    } else if (k.p == n - 1) {
      p = n - 2;
      l = 0;
    } else {
      p = k.p - 1;
      l = k.p + 1;
    }

    if (k.v != a[k.p]) {
      if (k.v < b[p] + b[l]) {
        printf("-1");
        return 0;
      } else {
        ans += k.v / (b[p] + b[l]);
        k.v = k.v % (b[p] + b[l]);
        if (k.v < a[k.p]) {
          tmp = (a[k.p] - k.v) / (b[p] + b[l]);
          if ((a[k.p] - k.v) % (b[p] + b[l]) != 0)
            tmp++;
          ans -= tmp;
          k.v += tmp * (b[p] + b[l]);
        }
        b[k.p] = k.v;
        q.push(k);
      }
    }
  }
  printf("%lld", ans);
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
struct node {
  int v, p;
  bool operator<(const node &a) const { return v < a.v; }
};
int a[200005], b[200005];
priority_queue<node> q;

int main(void) {
  int n, i, x, p, l, tmp;
  long long ans = 0;
  node k;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  for (i = 0; i < n; i++) {
    scanf("%d", &b[i]);
    q.push((node){b[i], i});
  }
  while (!q.empty()) {
    k = q.top();
    q.pop();

    if (k.p == 0) {
      p = n - 1;
      l = 1;
    } else if (k.p == n - 1) {
      p = n - 2;
      l = 0;
    } else {
      p = k.p - 1;
      l = k.p + 1;
    }

    if (k.v != a[k.p]) {
      if (k.v < b[p] + b[l]) {
        printf("-1");
        return 0;
      } else {
        ans += k.v / (b[p] + b[l]);
        k.v = k.v % (b[p] + b[l]);
        if (k.v < a[k.p]) {
          tmp = (a[k.p] - k.v) / (b[p] + b[l]);
          if ((a[k.p] - k.v) % (b[p] + b[l]) != 0) {
            printf("-1");
            return 0;
          }
          ans -= tmp;
          k.v += tmp * (b[p] + b[l]);
        }
        b[k.p] = k.v;
        q.push(k);
      }
    }
  }
  printf("%lld", ans);
  return 0;
}