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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long int ll;
typedef pair<int, int> P;

string s;
ll l[101010], r[101010];

int main() {
  cin >> s;
  int n = s.size();
  l[0] = 0;
  r[n] = 0;
  rep(i, n) {
    if (s[i] == '<') {
      l[i + 1] = l[i] + 1LL;
    }
  }
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '>') {
      r[i] = r[i + 1] + 1LL;
    }
  }

  ll res = 0;
  rep(i, n + 1) res += max(l[i], r[i]);
  cout << res << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long int ll;
typedef pair<int, int> P;

string s;
ll l[505050], r[505050];

int main() {
  cin >> s;
  int n = s.size();
  l[0] = 0;
  r[n] = 0;
  rep(i, n) {
    if (s[i] == '<') {
      l[i + 1] = l[i] + 1LL;
    }
  }
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '>') {
      r[i] = r[i + 1] + 1LL;
    }
  }

  ll res = 0;
  rep(i, n + 1) res += max(l[i], r[i]);
  cout << res << endl;

  return 0;
}
Problem p02873 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int main() {
  string s;
  cin >> s;
  ll n = s.length();

  ll st = -1, en = -1;
  for (ll i = 0; i < n; i++) {
    if (s[i] == '<') {
      st = i;
      break;
    }
  }

  for (ll i = n - 1; i >= 0; i--) {
    if (s[i] == '>') {
      en = i;
      break;
    }
  }
  //	cout<<st<<" "<<en<<endl;
  //	if(st==-1||en==-1){
  //		cout<<(n*(n-1))/2<<endl;
  //	}
  //	else{
  ll ans = 0;
  for (ll i = st; i <= en;) {
    ll c1 = 0;
    while (i <= en && s[i] == '<') {
      c1++;
      i++;
    }
    ll c2 = 0;
    while (i <= en && s[i] == '>') {
      c2++;
      i++;
    }
    //			cout<<c1<<" "<<c2<<endl;
    if (c1 > c2)
      swap(c1, c2);
    ans += ((c1) * (c1 - 1) / 2);
    ans += ((c2) * (c2 + 1) / 2);
    //			cout<<"ans: "<<ans<<endl;
  }
  ll c1 = st;
  ans += ((c1) * (c1 + 1) / 2);
  ll c2 = n - en - 1;
  ans += ((c2) * (c2 + 1) / 2);
  cout << ans << endl;
  //	}
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int main() {
  string s;
  cin >> s;
  ll n = s.length();

  ll st = -1, en = -1;
  for (ll i = 0; i < n; i++) {
    if (s[i] == '<') {
      st = i;
      break;
    }
  }

  for (ll i = n - 1; i >= 0; i--) {
    if (s[i] == '>') {
      en = i;
      break;
    }
  }

  if (st == -1) {
    cout << (n * (n + 1)) / 2 << endl;
    return 0;
  }

  //	cout<<"fgh"<<endl;
  //	cout<<st<<" "<<en<<endl;
  //	if(st==-1||en==-1){
  //		cout<<(n*(n-1))/2<<endl;
  //	}
  //	else{
  ll ans = 0;
  for (ll i = st; i <= en;) {
    ll c1 = 0;
    while (i <= en && s[i] == '<') {
      c1++;
      i++;
    }
    ll c2 = 0;
    while (i <= en && s[i] == '>') {
      c2++;
      i++;
    }
    //			cout<<c1<<" "<<c2<<endl;
    if (c1 > c2)
      swap(c1, c2);
    ans += ((c1) * (c1 - 1) / 2);
    ans += ((c2) * (c2 + 1) / 2);
    //			cout<<"ans: "<<ans<<endl;
  }
  ll c1 = st;
  ans += ((c1) * (c1 + 1) / 2);
  ll c2 = n - en - 1;
  ans += ((c2) * (c2 + 1) / 2);
  cout << ans << endl;
  //	}
}
Problem p02873 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;

// Solve when the interval [a, b] contains no ><
// -> It has form <<<<<<>>>>>>>
ll solve(string str, int a, int b) {
  int x = 0;
  int y = 0;
  for (int i = a; i <= b; ++i) {
    if (str[i] == '<')
      ++x;
    else
      ++y;
  }
  // Middle element: max(x, y)
  ll res = max(x, y);

  // Left side: 1 + 2 + ... + x-1
  res += (ll)x * (x - 1) / 2;

  // Right side: 1 + 2 + ... + y-1
  res += (ll)y * (y - 1) / 2;

  return res;
}

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

  string str;
  cin >> str;
  int n = str.size();

  // If we have ><, we want zero in the middle
  // Divide and conquer on occurrences of ><

  ll sum = 0;
  int p = 0;
  for (int i = 0; i + 1 < n; ++i) {
    if (str[i] == '>' && str[i + 1] == '<') {
      sum += solve(str, p, i);
      p = i + 1;
    }
  }
  sum += solve(str, p, n - 1);

  cout << sum << '\n';
}


Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;

// Solve when the interval [a, b] contains no ><
// -> It has form <<<<<<>>>>>>>
ll solve(const string &str, int a, int b) {
  int x = 0;
  int y = 0;
  for (int i = a; i <= b; ++i) {
    if (str[i] == '<')
      ++x;
    else
      ++y;
  }
  // Middle element: max(x, y)
  ll res = max(x, y);

  // Left side: 1 + 2 + ... + x-1
  res += (ll)x * (x - 1) / 2;

  // Right side: 1 + 2 + ... + y-1
  res += (ll)y * (y - 1) / 2;

  return res;
}

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

  string str;
  cin >> str;
  int n = str.size();

  // If we have ><, we want zero in the middle
  // Divide and conquer on occurrences of ><

  ll sum = 0;
  int p = 0;
  for (int i = 0; i + 1 < n; ++i) {
    if (str[i] == '>' && str[i + 1] == '<') {
      sum += solve(str, p, i);
      p = i + 1;
    }
  }
  sum += solve(str, p, n - 1);

  cout << sum << '\n';
}
Problem p02873 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define sb __builtin_popcount
#define MOD (ll)1000000007

int rate[100000 + 10];
long long values[100000 + 10];

int32_t main() {
  long long ans = 0, tmp = 0;

  string s;
  cin >> s;
  int n = s.size() + 1;
  vector<int> rate;
  rate.push_back(0);
  for (int i = 1; i < n; i++) {
    if (s[i - 1] == '<') {
      rate.push_back(rate[i - 1] + 1);
    } else {
      rate.push_back(rate[i - 1] - 1);
    }
  }
  values[0] = 1;
  // case 1
  for (int i = 1; i < n; i++) { // scan input array from left to right

    if (rate[i] > rate[i - 1]) {

      values[i] = values[i - 1] + 1; // case 1, a
    } else
      values[i] = 1; // case 1,b
  }

  ans = values[n - 1]; // case 2

  for (int i = n - 2; i >= 0; i--) { // scan input array from right to left

    if (rate[i] > rate[i + 1]) {

      tmp = values[i + 1] + 1; // case 2, a
    } else
      tmp = 1; // case 2, b

    ans += max(tmp, values[i]); // maximum of the two values for child (i)
    values[i] = tmp;
  }

  cout << ans - n;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define sb __builtin_popcount
#define MOD (ll)1000000007

long long values[600000];

int32_t main() {
  long long ans = 0, tmp = 0;

  string s;
  cin >> s;
  int n = s.size() + 1;
  vector<int> rate;
  rate.push_back(0);
  for (int i = 1; i < n; i++) {
    if (s[i - 1] == '<') {
      rate.push_back(rate[i - 1] + 1);
    } else {
      rate.push_back(rate[i - 1] - 1);
    }
  }
  values[0] = 1;
  // case 1
  for (int i = 1; i < n; i++) { // scan input array from left to right

    if (rate[i] > rate[i - 1]) {

      values[i] = values[i - 1] + 1; // case 1, a
    } else
      values[i] = 1; // case 1,b
  }

  ans = values[n - 1]; // case 2

  for (int i = n - 2; i >= 0; i--) { // scan input array from right to left

    if (rate[i] > rate[i + 1]) {

      tmp = values[i + 1] + 1; // case 2, a
    } else
      tmp = 1; // case 2, b

    ans += max(tmp, values[i]); // maximum of the two values for child (i)
    values[i] = tmp;
  }

  cout << ans - n;

  return 0;
}
Problem p02873 (C++)

Buggy submission (status: Runtime Error):
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define int long long
using namespace std;
vector<vector<int>> adj;

int dp[100005];
int vis[100005];
int ans = 0;
int dfs(int ver) {
  vis[ver] = 1;
  int mx = 0;
  for (auto i : adj[ver]) {
    if (vis[i] == 0)
      mx = max(mx, dfs(i));
    else {
      mx = max(mx, dp[i]);
    }
  }
  dp[ver] = mx + 1;
  ans = max(dp[ver], ans);
  return dp[ver];
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  //  #ifndef ONLINE_JUDGE
  //     freopen ("INPUT.txt" , "r" , stdin);
  //     freopen ("OUTPUT.txt" , "w" , stdout);
  //  #endif

  clock_t clk;
  clk = clock();

  // -------------------------------------Code starts
  // here---------------------------------------------------------------------

  string s;
  cin >> s;
  int n = s.length();
  adj.resize(n + 1);
  for (int i = 0; i < n; i++) {
    if (s[i] == '>')
      adj[i].push_back(i + 1);
    else {
      adj[i + 1].push_back(i);
    }
  }
  for (int i = 0; i <= n; i++) {
    if (vis[i] == 0)
      dfs(i);
  }
  int ans = 0;
  for (int i = 0; i <= n; i++)
    ans += dp[i] - 1;
  // cout<<(ans*(ans-1))/2;
  cout << ans;
  // -------------------------------------Code ends
  // here------------------------------------------------------

  clk = clock() - clk;
  cerr << fixed << setprecision(6) << "Time: " << ((double)clk) / CLOCKS_PER_SEC
       << "\n";
  return 0;
}

Fixed submission:
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define int long long
using namespace std;
vector<vector<int>> adj;

int dp[500005];
int vis[500005];
int ans = 0;
int dfs(int ver) {
  vis[ver] = 1;
  int mx = 0;
  for (auto i : adj[ver]) {
    if (vis[i] == 0)
      mx = max(mx, dfs(i));
    else {
      mx = max(mx, dp[i]);
    }
  }
  dp[ver] = mx + 1;
  ans = max(dp[ver], ans);
  return dp[ver];
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  //  #ifndef ONLINE_JUDGE
  //     freopen ("INPUT.txt" , "r" , stdin);
  //     freopen ("OUTPUT.txt" , "w" , stdout);
  //  #endif

  clock_t clk;
  clk = clock();

  // -------------------------------------Code starts
  // here---------------------------------------------------------------------

  string s;
  cin >> s;
  int n = s.length();
  adj.resize(n + 1);
  for (int i = 0; i < n; i++) {
    if (s[i] == '>')
      adj[i].push_back(i + 1);
    else {
      adj[i + 1].push_back(i);
    }
  }
  for (int i = 0; i <= n; i++) {
    if (vis[i] == 0)
      dfs(i);
  }
  int ans = 0;
  for (int i = 0; i <= n; i++)
    ans += dp[i] - 1;
  // cout<<(ans*(ans-1))/2;
  cout << ans;
  // -------------------------------------Code ends
  // here------------------------------------------------------

  clk = clock() - clk;
  cerr << fixed << setprecision(6) << "Time: " << ((double)clk) / CLOCKS_PER_SEC
       << "\n";
  return 0;
}
Problem p02873 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define maxn 200005
#define ll long long
#define mod 1000000007

typedef pair<int, int> P;

int n;
char s[maxn];
vector<int> v[maxn];
bool used[maxn];
ll d[maxn];

void dfs(int now) {
  if (used[now])
    return;
  used[now] = 1;
  d[now] = 0;
  if (v[now].empty())
    return;
  for (int x : v[now]) {
    dfs(x);
    d[now] = max(d[now], d[x]);
  }
  d[now]++;
}

int main() {
  // cin >> n;
  scanf("%s", s + 1);
  n = strlen(s + 1) + 1;
  for (int i = 1; i <= n - 1; i++) {
    if (s[i] == '<')
      v[i + 1].push_back(i);
    else
      v[i].push_back(i + 1);
  }
  for (int i = 1; i <= n; i++) {
    if (!used[i])
      dfs(i);
    // printf("i=%d %d\n", i, d[i]);
  }
  ll ans = 0;
  for (int i = 1; i <= n; i++)
    ans += d[i];
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define maxn 500005
#define ll long long
#define mod 1000000007

typedef pair<int, int> P;

int n;
char s[maxn];
vector<int> v[maxn];
bool used[maxn];
ll d[maxn];

void dfs(int now) {
  if (used[now])
    return;
  used[now] = 1;
  d[now] = 0;
  if (v[now].empty())
    return;
  for (int x : v[now]) {
    dfs(x);
    d[now] = max(d[now], d[x]);
  }
  d[now]++;
}

int main() {
  // cin >> n;
  scanf("%s", s + 1);
  n = strlen(s + 1) + 1;
  for (int i = 1; i <= n - 1; i++) {
    if (s[i] == '<')
      v[i + 1].push_back(i);
    else
      v[i].push_back(i + 1);
  }
  for (int i = 1; i <= n; i++) {
    if (!used[i])
      dfs(i);
    // printf("i=%d %d\n", i, d[i]);
  }
  ll ans = 0;
  for (int i = 1; i <= n; i++)
    ans += d[i];
  cout << ans << endl;

  return 0;
}
Problem p02873 (C++)

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

using namespace std;

#define SPEED                                                                  \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define fileio freopen("in.in", "r", stdin), freopen("out.out", "w", stdout);
#define ll long long int
#define FF first
#define SS second
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
#define pll pair<long long int, long long int>
#define sd(x) scanf("%d", &x)
#define slld(x) scanf("%lld", &x)
#define pd(x) printf("%d\n", x)
#define plld(x) printf("%lld\n", x)
#define pss printf
#define MOD 1000000007
#define INF 1e18
#define eps 0.00001
#define endl '\n'
#define debug(n1) cout << n1 << endl

int n;
string s;
ll ans = 0;
int val[200005];

int main() {
  SPEED;

  cin >> s;

  n = s.length();
  int i = 0, j = 0;

  while (i < n && j < n) {
    while (j < n && s[i] == s[j])
      j++;
    if (s[i] == '>') {
      for (int h = j; h >= i; h--)
        val[h] = max(val[h], j - h);
    } else {
      for (int h = i; h <= j; h++)
        val[h] = max(val[h], h - i);
    }
    i = j;
  }

  for (int i = 0; i <= n; i++)
    ans += val[i];

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

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

using namespace std;

#define SPEED                                                                  \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define fileio freopen("in.in", "r", stdin), freopen("out.out", "w", stdout);
#define ll long long int
#define FF first
#define SS second
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
#define pll pair<long long int, long long int>
#define sd(x) scanf("%d", &x)
#define slld(x) scanf("%lld", &x)
#define pd(x) printf("%d\n", x)
#define plld(x) printf("%lld\n", x)
#define pss printf
#define MOD 1000000007
#define INF 1e18
#define eps 0.00001
#define endl '\n'
#define debug(n1) cout << n1 << endl

int n;
string s;
ll ans = 0;
int val[500005];

int main() {
  SPEED;

  cin >> s;

  n = s.length();
  int i = 0, j = 0;

  while (i < n && j < n) {
    while (j < n && s[i] == s[j])
      j++;
    if (s[i] == '>') {
      for (int h = j; h >= i; h--)
        val[h] = max(val[h], j - h);
    } else {
      for (int h = i; h <= j; h++)
        val[h] = max(val[h], h - i);
    }
    i = j;
  }

  for (int i = 0; i <= n; i++)
    ans += val[i];

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define ll long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rREP(i, k, n) for (int i = (int)(n)-1; i >= k; i--)

int main() {
  string s;
  cin >> s;
  int lc[50010] = {};
  int rc[50010] = {};
  int cn = 0;
  rep(i, s.size()) {
    lc[i] = cn;
    if (s[i] == '<')
      cn++;
    else
      cn = 0;
  }
  lc[s.size()] = cn;
  cn = 0;
  rrep(i, s.size()) {
    rc[i + 1] = cn;
    if (s[i] == '>')
      cn++;
    else
      cn = 0;
  }
  rc[0] = cn;
  ll sn = 0;
  rep(i, s.size() + 1) { sn += max(lc[i], rc[i]); }
  cout << sn << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define ll long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rREP(i, k, n) for (int i = (int)(n)-1; i >= k; i--)

int main() {
  string s;
  cin >> s;
  int lc[500100] = {};
  int rc[500100] = {};
  int cn = 0;
  rep(i, s.size()) {
    lc[i] = cn;
    if (s[i] == '<')
      cn++;
    else
      cn = 0;
  }
  lc[s.size()] = cn;
  cn = 0;
  rrep(i, s.size()) {
    rc[i + 1] = cn;
    if (s[i] == '>')
      cn++;
    else
      cn = 0;
  }
  rc[0] = cn;
  ll sn = 0;
  rep(i, s.size() + 1) { sn += max(lc[i], rc[i]); }
  cout << sn << endl;
  return 0;
}
Problem p02873 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
const ll inf = 3e18;
const char sp = ' ';

int N, A[100000];
ll res;
string S;

int main() {
  cin >> S;
  N = S.size() + 1;
  for (int i = 0; i < N - 1; i++) {
    if (S[i] == '<') {
      A[i + 1] = A[i] + 1;
    }
  }
  for (int i = N - 1; i >= 0; i--) {
    if (S[i] == '>') {
      A[i] = max(A[i], A[i + 1] + 1);
    }
  }
  for (int i = 0; i < N; i++) {
    res += A[i];
  }
  cout << res << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
const ll inf = 3e18;
const char sp = ' ';

int N, A[500000];
ll res;
string S;

int main() {
  cin >> S;
  N = S.size() + 1;
  for (int i = 0; i < N - 1; i++) {
    if (S[i] == '<') {
      A[i + 1] = A[i] + 1;
    }
  }
  for (int i = N - 1; i >= 0; i--) {
    if (S[i] == '>') {
      A[i] = max(A[i], A[i + 1] + 1);
    }
  }
  for (int i = 0; i < N; i++) {
    res += A[i];
  }
  cout << res << endl;
}
Problem p02873 (C++)

Buggy submission (status: Runtime Error):
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(vec) vec.begin(), vec.end()
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
const ll mod = 1e9 + 7;
const int inf = 1 << 30;

int a[200010];

int main() {
  string s;
  cin >> s;
  int n = s.size();
  rep(i, n) {

    if (s[i] == '<')
      a[i + 1] = a[i] + 1;
  }
  for (int i = n; i >= 1; i--) {
    if (s[i - 1] == '>')
      chmax(a[i - 1], a[i] + 1);
  }
  ll ans = 0;
  rep(i, n + 1) { ans += a[i]; }
  cout << ans << endl;
}

Fixed submission:
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(vec) vec.begin(), vec.end()
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
const ll mod = 1e9 + 7;
const int inf = 1 << 30;

int a[500010];

int main() {
  string s;
  cin >> s;
  int n = s.size();
  rep(i, n) {

    if (s[i] == '<')
      a[i + 1] = a[i] + 1;
  }
  for (int i = n; i >= 1; i--) {
    if (s[i - 1] == '>')
      chmax(a[i - 1], a[i] + 1);
  }
  ll ans = 0;
  rep(i, n + 1) { ans += a[i]; }
  cout << ans << endl;
}
Problem p02873 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef pair<int, int> P;
typedef long long ll;
vector<int> dp(100100, -1);
string s;
int n;
int rec(int node) {
  if (dp[node] >= 0) {
    return dp[node];
  } else {
    int res = 0;
    if (node > 0 && node <= n && s[node - 1] == '<') {
      res = max(res, rec(node - 1) + 1);
    }
    if (node < n && s[node] == '>') {
      res = max(res, rec(node + 1) + 1);
    }
    return dp[node] = res;
  }
}
int main() {
  cin >> s;
  n = s.length();
  rep(i, n + 2) { dp[i] = -1; }
  rep(i, n + 2) {
    if (dp[i] == -1) {
      rec(i);
    }
  }
  ll ans = 0;
  rep(i, n + 1) { ans += dp[i]; }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef pair<int, int> P;
typedef long long ll;
vector<int> dp(500100, -1);
string s;
int n;
int rec(int node) {
  if (dp[node] >= 0) {
    return dp[node];
  } else {
    int res = 0;
    if (node > 0 && node <= n && s[node - 1] == '<') {
      res = max(res, rec(node - 1) + 1);
    }
    if (node < n && s[node] == '>') {
      res = max(res, rec(node + 1) + 1);
    }
    return dp[node] = res;
  }
}
int main() {
  cin >> s;
  n = s.length();
  rep(i, n + 2) { dp[i] = -1; }
  rep(i, n + 2) {
    if (dp[i] == -1) {
      rec(i);
    }
  }
  ll ans = 0;
  rep(i, n + 1) { ans += dp[i]; }
  cout << ans << endl;
  return 0;
}
Problem p02873 (C++)

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>
#define deb(x) cerr << (#x) << " is " << (x) << "\n"
#define ll long long int
#define ld long double
using namespace std;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;

ll powmod(ll a, ll b) {
  ll res = 1ll;
  while (b) {
    if (b % 2 == 1)
      res = (res * a) % MOD;
    a = (a * a) % MOD;
    b /= 2;
  }
  return res;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("D:\\V S Code\\cpp\\competitiveProgramming\\Input.txt", "r", stdin);
  freopen("D:\\V S Code\\cpp\\competitiveProgramming\\OPT.txt", "w", stdout);
#endif

  ios::sync_with_stdio(false);
  cin.tie(NULL);

  string s;
  cin >> s;
  int n = s.length();

  n++;
  ll arr[n + 10];
  arr[0] = 0;
  ll x = 0;
  ll sum = 0;
  memset(arr, -1, sizeof(arr));
  for (int i = 0; i < n - 1; i++) {
    int j;
    if (s[i] == '>')
      continue;
    arr[i] = 0;
    for (j = i; j < n - 1 and s[j] == '<'; j++)
      arr[j + 1] = arr[j] + 1;
    i = j;
  }

  if (arr[n - 1] == -1)
    arr[n - 1] = 0;

  for (int i = n - 1; i > 0; i--) {
    if (arr[i] == 0) {
      for (int j = i - 1; j >= 0 and s[j] == '>'; j--)
        arr[j] = max(arr[j], arr[j + 1] + 1);
    }
  }

  sum = 0;
  for (int i = 0; i < n; i++)
    sum += arr[i];

  cout << sum;
}

Fixed submission:

#include <bits/stdc++.h>
#define deb(x) cerr << (#x) << " is " << (x) << "\n"
#define ll long long int
#define ld long double
using namespace std;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;

ll powmod(ll a, ll b) {
  ll res = 1ll;
  while (b) {
    if (b % 2 == 1)
      res = (res * a) % MOD;
    a = (a * a) % MOD;
    b /= 2;
  }
  return res;
}

int main() {

  ios::sync_with_stdio(false);
  cin.tie(NULL);

  string s;
  cin >> s;
  int n = s.length();

  n++;
  ll arr[n + 10];
  arr[0] = 0;
  ll x = 0;
  ll sum = 0;
  memset(arr, -1, sizeof(arr));
  for (int i = 0; i < n - 1; i++) {
    int j;
    if (s[i] == '>')
      continue;
    arr[i] = 0;
    for (j = i; j < n - 1 and s[j] == '<'; j++)
      arr[j + 1] = arr[j] + 1;
    i = j;
  }

  if (arr[n - 1] == -1)
    arr[n - 1] = 0;

  for (int i = n - 1; i > 0; i--) {
    if (arr[i] == 0) {
      for (int j = i - 1; j >= 0 and s[j] == '>'; j--)
        arr[j] = max(arr[j], arr[j + 1] + 1);
    }
  }

  sum = 0;
  for (int i = 0; i < n; i++)
    sum += arr[i];

  cout << sum;
}
Problem p02873 (C++)

Buggy submission (status: Runtime Error):
/*
<>>><<><<<<  <  >  >  >  <
012345678910 11 12 13 14 15
0<1>2>3>4<5<6>7<8<9<10<11<12>13>14>15<16
*/
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
int main() {
  string S;
  cin >> S;
  vector<ll> num(S.size(), 10);
  if (S[0] == '<') {
    num[0] = 0;
  }

  if (S[S.size() - 1] == '>') {
    num[S.size()] = 0;
  }
  for (int i = 0; i < S.size() - 1; i++) {
    if (S[i] == '>' && S[i + 1] == '<') {
      num[i + 1] = 0;
    }
  }
  for (int i = 0; i < S.size() - 1; i++) {
    if (S[i] == '<' && S[i + 1] == '<') {
      num[i + 1] = num[i] + 1;
    }
  }
  for (int i = S.size() - 1; i >= 0; i--) {
    if (S[i - 1] == '>' && S[i] == '>') {
      num[i] = num[i + 1] + 1;
    }
  }
  for (int i = 0; i < S.size() - 1; i++) {
    if (S[i] == '<' && S[i + 1] == '>') {
      num[i + 1] = max(num[i], num[i + 2]) + 1;
    }
  }
  if (S[0] == '>') {
    num[0] = num[1] + 1;
  }
  if (S[S.size() - 1] == '<') {
    num[S.size()] = num[S.size() - 1] + 1;
  }
  ll sum = 0;
  for (int i = 0; i < S.size() + 1; i++) {
    // cout << num[i] << " ";
    sum += num[i];
  }
  cout << sum << endl;
}

Fixed submission:
/*
<>>><<><<<<  <  >  >  >  <
012345678910 11 12 13 14 15
0<1>2>3>4<5<6>7<8<9<10<11<12>13>14>15<16
*/
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
int main() {
  string S;
  cin >> S;
  vector<int> num(S.size());
  if (S[0] == '<') {
    num[0] = 0;
  }

  if (S[S.size() - 1] == '>') {
    num[S.size()] = 0;
  }
  for (int i = 0; i < S.size() - 1; i++) {
    if (S[i] == '>' && S[i + 1] == '<') {
      num[i + 1] = 0;
    }
  }
  for (int i = 0; i < S.size() - 1; i++) {
    if (S[i] == '<' && S[i + 1] == '<') {
      num[i + 1] = num[i] + 1;
    }
  }
  for (int i = S.size() - 1; i >= 0; i--) {
    if (S[i - 1] == '>' && S[i] == '>') {
      num[i] = num[i + 1] + 1;
    }
  }
  for (int i = 0; i < S.size() - 1; i++) {
    if (S[i] == '<' && S[i + 1] == '>') {
      num[i + 1] = max(num[i], num[i + 2]) + 1;
    }
  }
  if (S[0] == '>') {
    num[0] = num[1] + 1;
  }
  if (S[S.size() - 1] == '<') {
    num[S.size()] = num[S.size() - 1] + 1;
  }
  ll sum = 0;
  for (int i = 0; i < S.size() + 1; i++) {
    // cout << num[i] << " ";
    sum += num[i];
  }
  cout << sum << endl;
}
Problem p02874 (Python)

Buggy submission (status: Runtime Error):
#!/usr/bin/env python3
n = int(input())
p = sorted(list(map(int, input().split())) for _ in range(n))

lmax = n - 1
rmin = 0
for i in range(n):
    if p[i][1] <= p[rmin][1]:
        rmin = i

ans = 0

# same(lmax, rmin)
dmax = 0
for i in range(n):
    if i == lmax or i == rmin:
        continue
    if p[i][1] - p[i][0] > p[dmax][1] - p[dmax][0]:
        dmax = i
    d1 = max(0, p[rmin][1] - p[lmax][0] + 1)
    d2 = max(0, p[dmax][1] - p[dmax][0] + 1)
ans = max(ans, d1 + d2)

# !same(lmax, rmin)
m = lmax
for i in range(rmin, lmax)[::-1]:
    if p[i + 1][1] < p[m][1]:
        m = i + 1
    d1 = max(0, p[rmin][1] - p[i][0] + 1)
    d2 = max(0, p[m][1] - p[lmax][0] + 1)
    ans = max(ans, d1 + d2)

print(ans)


Fixed submission:
#!/usr/bin/env python3
n = int(input())
p = sorted(list(map(int, input().split())) for _ in range(n))

lmax = n - 1
rmin = 0
for i in range(n):
    if p[i][1] <= p[rmin][1]:
        rmin = i

ans = 0

# same(lmax, rmin)
dmax = 0
for i in range(n):
    if i == lmax or i == rmin:
        continue
    if p[i][1] - p[i][0] > p[dmax][1] - p[dmax][0]:
        dmax = i
d1 = max(0, p[rmin][1] - p[lmax][0] + 1)
d2 = max(0, p[dmax][1] - p[dmax][0] + 1)
ans = max(ans, d1 + d2)

# !same(lmax, rmin)
m = lmax
for i in range(rmin, lmax)[::-1]:
    if p[i + 1][1] < p[m][1]:
        m = i + 1
    d1 = max(0, p[rmin][1] - p[i][0] + 1)
    d2 = max(0, p[m][1] - p[lmax][0] + 1)
    ans = max(ans, d1 + d2)

print(ans)
Problem p02874 (C++)

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

struct aa {
  ll l, r;
};

int main() {
  ll n;
  std::cin >> n;
  vector<aa> a(n);

  ll maxl = -1;
  ll minr = 1e18;
  ll maxli;
  ll minri;
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    std::cin >> a[i].l >> a[i].r;
    a[i].r++;
    ans = max(ans, a[i].r - a[i].l);
    if (a[i].l > maxl) {
      maxl = a[i].l;
      maxli = i;
    }
    if (a[i].r < minr) {
      minr = a[i].r;
      minri = i;
    }
  }
  ans += max(0ll, minr - maxl);

  vector<aa> b(n);

  for (int i = 0; i < n; i++) {
    b[i].l = max(0ll, minr - a[i].l);
    b[i].r = max(0ll, a[i].r - maxl);
  }
  sort(b.begin(), b.end(), [](auto const &lhs, auto const &rhs) {
    if (lhs.l != rhs.l)
      return lhs.l < rhs.l;
    else if (lhs.r != rhs.r)
      return lhs.r > rhs.r;
    return true;
  });

  ll lmin = b[1].l;
  ll rmin = b[0].r;
  for (int i = 1; i < n; i++) {
    rmin = min(rmin, b[i - 1].r);
    lmin = b[i].l;
    ans = max(ans, lmin + rmin);
  }
  std::cout << ans << std::endl;
}


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

struct aa {
  ll l, r;
};

int main() {
  ll n;
  std::cin >> n;
  vector<aa> a(n);

  ll maxl = -1;
  ll minr = 1e18;
  ll maxli;
  ll minri;
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    std::cin >> a[i].l >> a[i].r;
    a[i].r++;
    ans = max(ans, a[i].r - a[i].l);
    if (a[i].l > maxl) {
      maxl = a[i].l;
      maxli = i;
    }
    if (a[i].r < minr) {
      minr = a[i].r;
      minri = i;
    }
  }
  ans += max(0ll, minr - maxl);

  vector<aa> b(n);

  for (int i = 0; i < n; i++) {
    b[i].l = max(0ll, minr - a[i].l);
    b[i].r = max(0ll, a[i].r - maxl);
  }
  sort(b.begin(), b.end(), [](auto const &lhs, auto const &rhs) {
    if (lhs.l != rhs.l)
      return lhs.l < rhs.l;
    return lhs.r > rhs.r;
  });

  ll lmin = b[1].l;
  ll rmin = b[0].r;
  for (int i = 1; i < n; i++) {
    rmin = min(rmin, b[i - 1].r);
    lmin = b[i].l;
    ans = max(ans, lmin + rmin);
  }
  std::cout << ans << std::endl;
}
Problem p02874 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MOD = 1000000007;

signed main() {
  int n;
  cin >> n;
  pair<int, int> p[n], q[n];
  int l, r;
  pair<int, pair<int, int>> t[n];
  for (int i = 0; i < n; i++) {
    cin >> l >> r;
    p[i] = make_pair(r, l);
    q[i] = make_pair(l, r);
    t[i] = make_pair(r - l, make_pair(l, r));
  }
  sort(p, p + n);
  sort(q, q + n);
  sort(t, t + n);
  int mxl = q[n - 1].first, mxr = q[n - 1].second, mil = p[0].second;
  int ans1 = p[1].first - mil;
  for (int i = 0; p[i].first < mxr; i++) {
    mil = max(mil, p[i].second);
    ans1 = max(ans1, p[i + 1].first - mil);
    cerr << ans1 << endl;
  }
  int ans2, x = 0, y = 0;
  vector<int> tt;
  for (int i = 0; i < n; i++) {
    if (x == 0 && mxl == t[i].second.first) {
      x = 1;
      continue;
    }
    if (y == 0 && p[0].first == t[i].second.second) {
      y = 1;
      continue;
    }
    tt.push_back(t[i].first);
  }
  // cerr<<ans1<<' '<<ans1+1-q[n-1].first+p[0].first+1<<'
  // '<<tt[tt.size()-1]+1+max(0LL,p[0].first-mxl+1)<<' '<<t[n-1].first+1<<endl;

  cout << max(ans1 + 1 - q[n - 1].first + p[0].first + 1,
              max(tt[tt.size() - 1] + 1 + max(0LL, p[0].first - mxl + 1),
                  t[n - 1].first + 1));

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MOD = 1000000007;

signed main() {
  int n;
  cin >> n;
  pair<int, int> p[n], q[n];
  int l, r;
  pair<int, pair<int, int>> t[n];
  for (int i = 0; i < n; i++) {
    cin >> l >> r;
    p[i] = make_pair(r, l);
    q[i] = make_pair(l, r);
    t[i] = make_pair(r - l, make_pair(l, r));
  }
  sort(p, p + n);
  sort(q, q + n);
  sort(t, t + n);
  int mxl = q[n - 1].first, mxr = q[n - 1].second, mil = p[0].second;
  int ans1 = p[1].first - mil;
  for (int i = 0; p[i].first < mxr; i++) {
    mil = max(mil, p[i].second);
    ans1 = max(ans1, p[i + 1].first - mil);
    cerr << ans1 << endl;
  }
  int ans2, x = 0, y = 0;
  vector<int> tt;
  for (int i = 0; i < n; i++) {
    if (x == 0 && mxl == t[i].second.first) {
      x = 1;
      continue;
    }
    if (y == 0 && p[0].first == t[i].second.second) {
      y = 1;
      continue;
    }
    tt.push_back(t[i].first);
  }
  // cerr<<ans1<<' '<<ans1+1-q[n-1].first+p[0].first+1<<'
  // '<<tt[tt.size()-1]+1+max(0LL,p[0].first-mxl+1)<<' '<<t[n-1].first+1<<endl;
  if (tt.size() == 0) {
    cout << max(ans1 + 1 - q[n - 1].first + p[0].first + 1, t[n - 1].first + 1);
    return 0;
  }
  cout << max(ans1 + 1 - q[n - 1].first + p[0].first + 1,
              max(tt[tt.size() - 1] + 1 + max(0LL, p[0].first - mxl + 1),
                  t[n - 1].first + 1));

  return 0;
}
Problem p02875 (C++)

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

using namespace std;

const int MOD = 998244353;

long long modPow(long long a, long long p) {
  if (p == 0)
    return 1;
  auto res = modPow(a, p / 2);
  res = (res * res) % MOD;
  if (p % 2)
    res = (res * a) % MOD;
  return res;
}

long long calcInv(long long a) { return modPow(a, MOD - 2); }

int main() {
  const int maxSize = 1000001;
  vector<long long> inv(maxSize);
  vector<long long> fact(maxSize);
  vector<long long> factInv(maxSize);
  for (int i = 0; i < 2; i++)
    inv[i] = fact[i] = factInv[i] = 1;
  for (int i = 2; i < maxSize; i++) {
    inv[i] = inv[MOD % i] * (MOD - MOD / i) % MOD;
    fact[i] = fact[i - 1] * i % MOD;
    factInv[i] = factInv[i - 1] * inv[i] % MOD;
  }
  auto comb = [&](int n, int r) {
    if (n < r || r < 0)
      return 0LL;
    return fact[n] * factInv[n - r] % MOD * factInv[r] % MOD;
  };
  int N;
  while (cin >> N) {
    long long res = modPow(3, N);
    for (int i = N / 2 + 1; i <= N; i++) {
      long long sub = comb(N, i) * modPow(2, N - i);
      res = (res + MOD - 2 * sub % MOD) % MOD;
    }
    cout << res << endl;
  }
}

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

using namespace std;

const int MOD = 998244353;

long long modPow(long long a, long long p) {
  if (p == 0)
    return 1;
  auto res = modPow(a, p / 2);
  res = (res * res) % MOD;
  if (p % 2)
    res = (res * a) % MOD;
  return res;
}

long long calcInv(long long a) { return modPow(a, MOD - 2); }

int main() {
  const int maxSize = 10000001;
  vector<long long> inv(maxSize);
  vector<long long> fact(maxSize);
  vector<long long> factInv(maxSize);
  for (int i = 0; i < 2; i++)
    inv[i] = fact[i] = factInv[i] = 1;
  for (int i = 2; i < maxSize; i++) {
    inv[i] = inv[MOD % i] * (MOD - MOD / i) % MOD;
    fact[i] = fact[i - 1] * i % MOD;
    factInv[i] = factInv[i - 1] * inv[i] % MOD;
  }
  auto comb = [&](int n, int r) {
    if (n < r || r < 0)
      return 0LL;
    return fact[n] * factInv[n - r] % MOD * factInv[r] % MOD;
  };
  int N;
  while (cin >> N) {
    long long res = modPow(3, N);
    for (int i = N / 2 + 1; i <= N; i++) {
      long long sub = comb(N, i) * modPow(2, N - i);
      res = (res + MOD - 2 * sub % MOD) % MOD;
    }
    cout << res << endl;
  }
}
Problem p02875 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
const int MOD = 998244353;
const int MX = 5000005;
int n;
ll ans = 1, k;
ll exp(ll b, ll n) {
  ll res = 1;
  for (; n; n /= 2, (b *= b) %= MOD)
    if (n % 2)
      (res *= b) %= MOD;
  return res;
}
ll fact[MX] = {1}, invf[MX];
void setcomb(int n) {
  for (int i = 1; i <= n; i++)
    fact[i] = (fact[i - 1] * i) % MOD;
  invf[n] = exp(fact[n], MOD - 2);
  for (int i = n; i > 0; i--)
    invf[i - 1] = (invf[i] * i) % MOD;
}
inline ll comb(int a, int b) {
  return fact[a] * invf[a - b] % MOD * invf[b] % MOD;
}
int main() {
  scanf("%d", &n);
  setcomb(n);
  ans = exp(3, n);
  rep(i, n / 2) ans -= comb(n, i) * exp(2, i) * 2 % MOD;
  printf("%lld\n", (ans % MOD + MOD) % MOD);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
const int MOD = 998244353;
const int MX = 10000005;
int n;
ll ans = 1, k;
ll exp(ll b, ll n) {
  ll res = 1;
  for (; n; n /= 2, (b *= b) %= MOD)
    if (n % 2)
      (res *= b) %= MOD;
  return res;
}
ll fact[MX] = {1}, invf[MX];
void setcomb(int n) {
  for (int i = 1; i <= n; i++)
    fact[i] = (fact[i - 1] * i) % MOD;
  invf[n] = exp(fact[n], MOD - 2);
  for (int i = n; i > 0; i--)
    invf[i - 1] = (invf[i] * i) % MOD;
}
inline ll comb(int a, int b) {
  return fact[a] * invf[a - b] % MOD * invf[b] % MOD;
}
int main() {
  scanf("%d", &n);
  setcomb(n);
  ans = exp(3, n);
  rep(i, n / 2) ans -= comb(n, i) * exp(2, i) * 2 % MOD;
  printf("%lld\n", (ans % MOD + MOD) % MOD);
}
Problem p02875 (C++)

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

const int maxn = 2e5 + 10;
const int mod = 998244353;
int n;
ll poww(ll x, ll k) {
  ll ans = 1;
  while (k) {
    if (k & 1)
      ans = ans * x % mod;
    x = x * x % mod;
    k >>= 1;
  }
  return ans;
}
ll mul[maxn], inv[maxn], er[maxn], ans;
ll C(int a, int b) { return mul[a] * inv[b] % mod * inv[a - b] % mod; }
ll QwQ;
int main() {
  scanf("%d", &n);
  mul[0] = 1, er[0] = 1;
  for (int i = 1; i <= n; ++i)
    mul[i] = mul[i - 1] * i % mod, er[i] = er[i - 1] * 2 % mod;
  inv[n] = poww(mul[n], mod - 2);
  for (int i = n; i; i--)
    inv[i - 1] = inv[i] * i % mod;
  inv[0] = 1;
  for (int i = n / 2 + 1; i <= n; ++i) {
    QwQ = (QwQ + C(n, i) * er[n - i] % mod) % mod;
  }
  cout << (poww(3, n) - QwQ * 2 % mod + mod) % mod << "\n";
  return 0;
}


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

const int maxn = 1e7 + 10;
const int mod = 998244353;
int n;
ll poww(ll x, ll k) {
  ll ans = 1;
  while (k) {
    if (k & 1)
      ans = ans * x % mod;
    x = x * x % mod;
    k >>= 1;
  }
  return ans;
}
ll mul[maxn], inv[maxn], er[maxn], ans;
ll C(int a, int b) { return mul[a] * inv[b] % mod * inv[a - b] % mod; }
ll QwQ;
int main() {
  scanf("%d", &n);
  mul[0] = 1, er[0] = 1;
  for (int i = 1; i <= n; ++i)
    mul[i] = mul[i - 1] * i % mod, er[i] = er[i - 1] * 2 % mod;
  inv[n] = poww(mul[n], mod - 2);
  for (int i = n; i; i--)
    inv[i - 1] = inv[i] * i % mod;
  inv[0] = 1;
  for (int i = n / 2 + 1; i <= n; ++i) {
    QwQ = (QwQ + C(n, i) * er[n - i] % mod) % mod;
  }
  cout << (poww(3, n) - QwQ * 2 % mod + mod) % mod << "\n";
  return 0;
}
Problem p02875 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long int ll;

ll mod = 998244353;

ll mod_pow(ll a, ll b) {
  a %= mod;
  if (b == 0)
    return 1;
  if (b == 1)
    return a;
  ll res = mod_pow(a, b / 2) % mod;
  res *= res;
  res %= mod;
  if (b % 2)
    res *= a;
  return res % mod;
}

struct perm {
private:
  int sz;
  vector<ll> p, invp;

public:
  perm(int n) {
    sz = n + 1;
    p.resize(sz), invp.resize(sz);
    p[0] = 1;
    for (int i = 1; i <= sz - 1; i++) {
      p[i] = p[i - 1] * i % mod;
    }
    invp[sz - 1] = mod_pow(p[sz - 1], mod - 2);
    for (int i = sz - 2; i >= 0; i--) {
      invp[i] = invp[i + 1] * (i + 1) % mod;
    }
  }
  ll comb(ll x, ll y) {
    if (x < y || y < 0)
      return 0;
    return (p[x] * invp[x - y] % mod) * invp[y] % mod;
  }
};
perm p(1 << 22);

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  ll n;
  cin >> n;
  ll ans = mod_pow(3, n);
  vector<ll> p2(n + 1, 1);
  for (int i = 0; i < n; i++) {
    p2[i + 1] = p2[i] * 2 % mod;
  }
  for (int i = n / 2 + 1; i <= n; i++) {
    ans -= p.comb(n, i) * p2[n - i] * 2;
    ans += mod;
    ans %= mod;
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long int ll;

ll mod = 998244353;

ll mod_pow(ll a, ll b) {
  a %= mod;
  if (b == 0)
    return 1;
  if (b == 1)
    return a;
  ll res = mod_pow(a, b / 2) % mod;
  res *= res;
  res %= mod;
  if (b % 2)
    res *= a;
  return res % mod;
}

struct perm {
private:
  int sz;
  vector<ll> p, invp;

public:
  perm(int n) {
    sz = n + 1;
    p.resize(sz), invp.resize(sz);
    p[0] = 1;
    for (int i = 1; i <= sz - 1; i++) {
      p[i] = p[i - 1] * i % mod;
    }
    invp[sz - 1] = mod_pow(p[sz - 1], mod - 2);
    for (int i = sz - 2; i >= 0; i--) {
      invp[i] = invp[i + 1] * (i + 1) % mod;
    }
  }
  ll comb(ll x, ll y) {
    if (x < y || y < 0)
      return 0;
    return (p[x] * invp[x - y] % mod) * invp[y] % mod;
  }
};
perm p(1 << 24);

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  ll n;
  cin >> n;
  ll ans = mod_pow(3, n);
  vector<ll> p2(n + 1, 1);
  for (int i = 0; i < n; i++) {
    p2[i + 1] = p2[i] * 2 % mod;
  }
  for (int i = n / 2 + 1; i <= n; i++) {
    ans -= p.comb(n, i) * p2[n - i] * 2;
    ans += mod;
    ans %= mod;
  }
  cout << ans << endl;
}
Problem p02875 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
const int mod = 998244353;
int n;
long long ans, ji[10000007];
long long qpow(long long a, long long b) {
  long long res = 1;
  long long now = a % mod;
  while (b) {
    if (b & 1)
      res = res * now % mod;
    now = now * now % mod;
    b >>= 1;
  }
  return res;
}
void init() {
  ji[0] = 1;
  for (int i = 1; i <= n; i++)
    ji[i] = ji[i - 1] * i % mod;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  init();
  ans = qpow(3, n);
  for (int i = (n / 2) + 1; i <= n; i++) {
    ans =
        (ans -
         (((((ji[n] * qpow(ji[i], mod - 2)) % mod) * qpow(ji[n - i], mod - 2)) %
           mod) *
          qpow(2, n - i) % mod)) %
        mod;
    ans =
        (ans -
         (((((ji[n] * qpow(ji[i], mod - 2)) % mod) * qpow(ji[n - i], mod - 2)) %
           mod) *
          qpow(2, n - i) % mod)) %
        mod;
  }
  cout << (ans + mod) % mod << endl;
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
const int mod = 998244353;
int n;
long long ans, ji[10000007];
long long qpow(long long a, long long b) {
  long long res = 1;
  long long now = a % mod;
  while (b) {
    if (b & 1)
      res = res * now % mod;
    now = now * now % mod;
    b >>= 1;
  }
  return res;
}
void init() {
  ji[0] = 1;
  for (int i = 1; i <= n; i++)
    ji[i] = ji[i - 1] * i % mod;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  init();
  ans = qpow(3, n);
  for (int i = (n / 2) + 1; i <= n; i++) {
    ans = (ans - 2 * (((((ji[n] * qpow(ji[i], mod - 2)) % mod) *
                        qpow(ji[n - i], mod - 2)) %
                       mod) *
                      qpow(2, n - i) % mod)) %
          mod;
  }
  cout << (ans + mod) % mod << endl;
  return 0;
}
Problem p02877 (C++)

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

typedef long long ll;
const int maxn = 100005;

inline int gi() {
  char c = getchar();
  while (c < '0' || c > '9')
    c = getchar();
  int sum = 0;
  while ('0' <= c && c <= '9')
    sum = sum * 10 + c - 48, c = getchar();
  return sum;
}

int n, a[maxn], ans, f[3], g[3], h[3];

int main() {
  n = gi();
  for (int i = 1; i <= n; ++i)
    a[i] = gi();

  for (int i = 1; i <= n + 1; ++i) {
    f[0] = 0;
    f[1] = a[i] - a[i - 1];
    if (f[0] < f[1])
      swap(f[0], f[1]);
    g[0] = g[1] = 1e9;
    g[2] = 0;
    for (int j = 0; j < 2; ++j)
      for (int k = 0; k < 2; ++k)
        g[j + k] = min(g[j + k], max(0, f[j] + h[k]));
    int d = g[0] > a[i];
    ans += d;
    for (int j = 0; j < 2; ++j)
      h[j] = g[j + d];
  }
  printf("%d\n", ans);

  return 0;
}


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

typedef long long ll;
const int maxn = 200005;

inline int gi() {
  char c = getchar();
  while (c < '0' || c > '9')
    c = getchar();
  int sum = 0;
  while ('0' <= c && c <= '9')
    sum = sum * 10 + c - 48, c = getchar();
  return sum;
}

int n, a[maxn], ans, f[3], g[3], h[3];

int main() {
  n = gi();
  for (int i = 1; i <= n; ++i)
    a[i] = gi();

  for (int i = 1; i <= n + 1; ++i) {
    f[0] = 0;
    f[1] = a[i] - a[i - 1];
    if (f[0] < f[1])
      swap(f[0], f[1]);
    g[0] = g[1] = 1e9;
    g[2] = 0;
    for (int j = 0; j < 2; ++j)
      for (int k = 0; k < 2; ++k)
        g[j + k] = min(g[j + k], max(0, f[j] + h[k]));
    int d = g[0] > a[i];
    ans += d;
    for (int j = 0; j < 2; ++j)
      h[j] = g[j + d];
  }
  printf("%d\n", ans);

  return 0;
}
Problem p02878 (C++)

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

const int N = 1e7 + 5;
const int mod = 998244353;
int fra[N], inv[N];
int n, a, b, ans;

template <class T> inline T Min(T x, T y) { return x < y ? x : y; }

inline void add(int &x, int y) {
  x += y;
  x >= mod ? x -= mod : 0;
}

inline void dec(int &x, int y) {
  x -= y;
  x < 0 ? x += mod : 0;
}

inline void add_prod(int &x, int u, int v) { x = (1ll * u * v + x) % mod; }

inline int quick_pow(int x, int k) {
  int res = 1;
  while (k) {
    if (k & 1)
      res = 1ll * res * x % mod;
    x = 1ll * x * x % mod;
    k >>= 1;
  }
  return res;
}

inline int C(int n, int m) {
  if (n < m)
    return 0;
  return 1ll * fra[n] * inv[n - m] % mod * inv[m] % mod;
}

inline int ask(int k) {
  int res = C(k + b - 1, b - 1);
  dec(res, C(k + b - 1, k - 1));
  return res;
}

int main() {
  scanf("%d%d%d", &n, &a, &b);
  if (a == 0 && b == 0)
    return puts("1"), 0;
  fra[0] = 1;
  for (int i = 1; i <= n; ++i)
    fra[i] = 1ll * fra[i - 1] * i % mod;
  inv[n] = quick_pow(fra[n], mod - 2);
  for (int i = n; i >= 1; --i)
    inv[i - 1] = 1ll * inv[i] * i % mod;

  for (int k = 0, km = Min(a, b - 1); k <= km; ++k) {
    int tmp = k ? ask(k) : 1;
    if (k == a) {
      int x = n - b - k, y = a - k + 1;
      if (x >= 0)
        add_prod(ans, tmp, C(x + y - 1, y - 1));
    } else {
      int x = n - b - k - 1, y = a - k + 1;
      if (x >= 0)
        add_prod(ans, tmp, C(x + y - 1, y - 1));
    }
  }
  printf("%d\n", ans);
  return 0;
}

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

const int N = 1e7 + 5;
const int mod = 998244353;
int fra[N], inv[N];
int n, a, b, ans;

template <class T> inline T Min(T x, T y) { return x < y ? x : y; }

inline void add(int &x, int y) {
  x += y;
  x >= mod ? x -= mod : 0;
}

inline void dec(int &x, int y) {
  x -= y;
  x < 0 ? x += mod : 0;
}

inline void add_prod(int &x, int u, int v) { x = (1ll * u * v + x) % mod; }

inline int quick_pow(int x, int k) {
  int res = 1;
  while (k) {
    if (k & 1)
      res = 1ll * res * x % mod;
    x = 1ll * x * x % mod;
    k >>= 1;
  }
  return res;
}

inline int C(int n, int m) {
  if (n < m)
    return 0;
  return 1ll * fra[n] * inv[n - m] % mod * inv[m] % mod;
}

inline int ask(int k) {
  int res = C(k + b - 1, b - 1);
  dec(res, C(k + b - 1, k - 1));
  return res;
}

int main() {
  scanf("%d%d%d", &n, &a, &b);
  if (a == 0 && b == 0)
    return puts("1"), 0;
  fra[0] = 1;
  for (int i = 1; i <= n; ++i)
    fra[i] = 1ll * fra[i - 1] * i % mod;
  inv[n] = quick_pow(fra[n], mod - 2);
  for (int i = n; i >= 1; --i)
    inv[i - 1] = 1ll * inv[i] * i % mod;

  for (int k = 0, km = Min(n - b, Min(a, b - 1)); k <= km; ++k) {
    int tmp = k ? ask(k) : 1;
    if (k == a) {
      int x = n - b - k, y = a - k + 1;
      if (x >= 0)
        add_prod(ans, tmp, C(x + y - 1, y - 1));
    } else {
      int x = n - b - k - 1, y = a - k + 1;
      if (x >= 0)
        add_prod(ans, tmp, C(x + y - 1, y - 1));
    }
  }
  printf("%d\n", ans);
  return 0;
}