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

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

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  long v[r][c];
  rep(i, r) rep(j, c) v[i][j] = 0;
  long dp[4][r][c];
  rep(i, 4) rep(j, r) rep(l, c) dp[i][j][l] = 0;
  rep(i, k) {
    long ri, ci, vi;
    cin >> ri >> ci >> vi;
    ri--;
    ci--;
    v[ri][ci] = vi;
  }
  rep(i, r - 1) {
    rep(j, 4) {
      rep(l, c - 1) {
        if (j != 3)
          dp[j + 1][i][l + 1] = max(dp[j + 1][i][l + 1], dp[j][i][l] + v[i][l]);
        dp[j][i][l + 1] = max(dp[j][i][l + 1], dp[j][i][l]);
        if (j != 3)
          dp[0][i + 1][l] = max(dp[0][i + 1][l], dp[j][i][l] + v[i][l]);
        dp[0][i + 1][l] = max(dp[0][i + 1][l], dp[j][i][l]);
      }
      if (j != 3)
        dp[0][i + 1][c - 1] =
            max(dp[0][i + 1][c - 1], dp[j][i][c - 1] + v[i][c - 1]);
      dp[0][i + 1][c - 1] = max(dp[0][i + 1][c - 1], dp[j][i][c - 1]);
    }
  }
  rep(j, 4) {
    rep(l, c - 1) {
      if (j != 3)
        dp[j + 1][r - 1][l + 1] =
            max(dp[j + 1][r - 1][l + 1], dp[j][r - 1][l] + v[r - 1][l]);
      dp[j][r - 1][l + 1] = max(dp[j][r - 1][l + 1], dp[j][r - 1][l]);
    }
  }
  long ans = max(dp[0][r - 1][c - 1] + v[r - 1][c - 1],
                 dp[1][r - 1][c - 1] + v[r - 1][c - 1]);
  ans = max(ans, dp[2][r - 1][c - 1] + v[r - 1][c - 1]);
  ans = max(ans, dp[3][r - 1][c - 1]);
  cout << ans << endl;
  return 0;
}

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

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  long v[r][c];
  rep(i, r) rep(j, c) v[i][j] = 0;
  // long dp[4][r][c];
  vector<vector<vector<long>>> dp(4, vector<vector<long>>(r, vector<long>(c)));
  rep(i, 4) rep(j, r) rep(l, c) dp[i][j][l] = 0;
  rep(i, k) {
    long ri, ci, vi;
    cin >> ri >> ci >> vi;
    ri--;
    ci--;
    v[ri][ci] = vi;
  }
  rep(i, r - 1) {
    rep(j, 4) {
      rep(l, c - 1) {
        if (j != 3)
          dp[j + 1][i][l + 1] = max(dp[j + 1][i][l + 1], dp[j][i][l] + v[i][l]);
        dp[j][i][l + 1] = max(dp[j][i][l + 1], dp[j][i][l]);
        if (j != 3)
          dp[0][i + 1][l] = max(dp[0][i + 1][l], dp[j][i][l] + v[i][l]);
        dp[0][i + 1][l] = max(dp[0][i + 1][l], dp[j][i][l]);
      }
      if (j != 3)
        dp[0][i + 1][c - 1] =
            max(dp[0][i + 1][c - 1], dp[j][i][c - 1] + v[i][c - 1]);
      dp[0][i + 1][c - 1] = max(dp[0][i + 1][c - 1], dp[j][i][c - 1]);
    }
  }
  rep(j, 4) {
    rep(l, c - 1) {
      if (j != 3)
        dp[j + 1][r - 1][l + 1] =
            max(dp[j + 1][r - 1][l + 1], dp[j][r - 1][l] + v[r - 1][l]);
      dp[j][r - 1][l + 1] = max(dp[j][r - 1][l + 1], dp[j][r - 1][l]);
    }
  }
  long ans = max(dp[0][r - 1][c - 1] + v[r - 1][c - 1],
                 dp[1][r - 1][c - 1] + v[r - 1][c - 1]);
  ans = max(ans, dp[2][r - 1][c - 1] + v[r - 1][c - 1]);
  ans = max(ans, dp[3][r - 1][c - 1]);
  cout << ans << endl;
  return 0;
}
Problem p02586 (C++)

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

ll dp[10][10][5];
ll g[10][10];

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  rep(i, k) {
    int a, b, d;
    cin >> a >> b >> d;
    a--;
    b--;
    g[a][b] = d;
  }
  if (g[0][0]) {
    dp[0][0][1] = g[0][0];
    dp[0][0][0] = -inf;
  }
  rep(i, r) rep(j, c) rep(k, 4) {
    if (i - 1 >= 0) {
      if (g[i][j] != 0)
        chmax(dp[i][j][1], dp[i - 1][j][k] + g[i][j]);
      chmax(dp[i][j][0], dp[i - 1][j][k]);
    }
    if (j - 1 >= 0) {
      if (g[i][j] != 0)
        chmax(dp[i][j][k + 1], dp[i][j - 1][k] + g[i][j]);
      chmax(dp[i][j][k], dp[i][j - 1][k]);
    }
  }
  ll ans = -inf;
  rep(k, 4) { chmax(ans, dp[r - 1][c - 1][k]); }
  cout << ans << endl;
}

Fixed submission:
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define all(vec) vec.begin(), vec.end()
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef tuple<ll, ll, ll> tlll;
typedef tuple<int, int, int> tiii;
const ll mod = 1e9 + 7;
const ll inf = 1ll << 60;

ll dp[3010][3010][5];
ll g[3010][3010];

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  rep(i, k) {
    int a, b, d;
    cin >> a >> b >> d;
    a--;
    b--;
    g[a][b] = d;
  }
  if (g[0][0]) {
    dp[0][0][1] = g[0][0];
    dp[0][0][0] = -inf;
  }
  rep(i, r) rep(j, c) rep(k, 4) {
    if (i - 1 >= 0) {
      if (g[i][j] != 0)
        chmax(dp[i][j][1], dp[i - 1][j][k] + g[i][j]);
      chmax(dp[i][j][0], dp[i - 1][j][k]);
    }
    if (j - 1 >= 0) {
      if (g[i][j] != 0)
        chmax(dp[i][j][k + 1], dp[i][j - 1][k] + g[i][j]);
      chmax(dp[i][j][k], dp[i][j - 1][k]);
    }
  }
  ll ans = -inf;
  rep(k, 4) { chmax(ans, dp[r - 1][c - 1][k]); }
  cout << ans << endl;
}
Problem p02586 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define zero_pad(num) setfill('0') << std::right << setw(num)
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  map<P, ll> mp;
  rep(i, k) {
    int r, c, v;
    cin >> r >> c >> v;
    r--;
    c--;
    mp[make_pair(r, c)] = v;
  }

  vector<vector<vector<ll>>> dp(r, vector<vector<ll>>(c, vector<ll>(4, 0)));
  dp[0][0][1] = mp[make_pair(0, 0)];

  rep(i, r) rep(j, c) {
    P p = make_pair(i, j);
    if (i == 0 && j == 0)
      continue;
    else if (i == 0) {
      rep(n, 3) {
        dp[i][j][n + 1] = max(dp[i][j - 1][n + 1], dp[i][j - 1][n] + mp[p]);
      }
    } else if (j == 0) {
      dp[i][j][1] = dp[i - 1][j][1] + mp[p];
      dp[i][j][0] = dp[i - 1][j][1];
    } else {
      rep(n, 4) dp[i][j][n] = dp[i][j - 1][n];
      rep(n, 4) {
        dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][n]);
        dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][n] + mp[p]);
      }
      rep(n, 3) {
        dp[i][j][n + 1] = max({dp[i][j][n + 1], dp[i][j - 1][n] + mp[p]});
      }
    }
  }
  ll ans = 0;
  rep(i, 4) ans = max(ans, dp[r - 1][c - 1][i]);
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define zero_pad(num) setfill('0') << std::right << setw(num)
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  map<P, ll> mp;
  rep(i, k) {
    int r, c, v;
    cin >> r >> c >> v;
    r--;
    c--;
    mp[make_pair(r, c)] = v;
  }

  vector<vector<vector<ll>>> dp(r, vector<vector<ll>>(c, vector<ll>(4, 0)));
  dp[0][0][1] = mp[make_pair(0, 0)];

  rep(i, r) rep(j, c) {
    P p = make_pair(i, j);
    if (i == 0 && j == 0)
      continue;
    else if (i == 0) {
      rep(n, 3) {
        dp[i][j][n + 1] = max(dp[i][j - 1][n + 1], dp[i][j - 1][n] + mp[p]);
      }
    } else if (j == 0) {
      dp[i][j][1] = dp[i - 1][j][1] + mp[p];
      dp[i][j][0] = dp[i - 1][j][1];
    } else {
      rep(n, 4) dp[i][j][n] = dp[i][j - 1][n];
      rep(n, 4) { dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][n]); }
      if (mp.find(p) != mp.end()) {
        rep(n, 4) { dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][n] + mp[p]); }
        rep(n, 3) {
          dp[i][j][n + 1] = max({dp[i][j][n + 1], dp[i][j - 1][n] + mp[p]});
        }
      }
    }
  }
  ll ans = 0;
  rep(i, 4) ans = max(ans, dp[r - 1][c - 1][i]);
  cout << ans << endl;
}
Problem p02586 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N = 1e3 + 5;
ll r, c, k, x, y, z, g[N][N], dp[N][N][4];
int main() {
  cin >> r >> c >> k;
  while (k--) {
    cin >> x >> y >> z;
    g[x][y] = z;
  }
  for (int i = 1; i <= r; i++) {
    for (int j = 1; j <= c; j++) {
      for (int k = 1; k <= 3; k++) {
        dp[i][j][k] = max(dp[i - 1][j][3] + g[i][j],
                          max(dp[i][j - 1][k - 1] + g[i][j], dp[i][j - 1][k]));
      }
    }
  }
  cout << dp[r][c][3];
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N = 3e3 + 5;
ll r, c, k, x, y, z, g[N][N], dp[N][N][4];
int main() {
  cin >> r >> c >> k;
  while (k--) {
    cin >> x >> y >> z;
    g[x][y] = z;
  }
  for (int i = 1; i <= r; i++) {
    for (int j = 1; j <= c; j++) {
      for (int k = 1; k <= 3; k++) {
        dp[i][j][k] = max(dp[i - 1][j][3] + g[i][j],
                          max(dp[i][j - 1][k - 1] + g[i][j], dp[i][j - 1][k]));
      }
    }
  }
  cout << dp[r][c][3];
}
Problem p02586 (C++)

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

const int maxn = 3005;

int R, C, k, r, c, v, dp[4][maxn][maxn], val[maxn][maxn];
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> R >> C >> k;
  for (int i = 0; i < k; i++) {
    cin >> r >> c >> v;
    r--;
    c--;
    val[r][c] = v;
  }
  for (int i = 0; i < R; i++)
    for (int j = 0; j < C; j++)
      for (int k = 0; k < 4; k++) {
        dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
        dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
        if (k < 3) {
          dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k] + val[i][j]);
          dp[i][j + 1][k + 1] =
              max(dp[i][j + 1][k + 1], dp[i][j][k] + val[i][j]);
        }
      }
  int ans = 0;
  for (int k = 0; k < 4; k++)
    ans = max(ans, max(dp[R][C - 1][k], dp[R - 1][C][k]));
  cout << ans << "\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
using pii = std::pair<int, int>;
using namespace std;

const int maxn = 3005;

int R, C, k, r, c, v, dp[maxn][maxn][4], val[maxn][maxn];
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> R >> C >> k;
  for (int i = 0; i < k; i++) {
    cin >> r >> c >> v;
    r--;
    c--;
    val[r][c] = v;
  }
  for (int i = 0; i < R; i++)
    for (int j = 0; j < C; j++)
      for (int k = 0; k < 4; k++) {
        dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
        dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
        if (k < 3) {
          dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k] + val[i][j]);
          dp[i][j + 1][k + 1] =
              max(dp[i][j + 1][k + 1], dp[i][j][k] + val[i][j]);
        }
      }
  int ans = 0;
  for (int k = 0; k < 4; k++)
    ans = max(ans, max(dp[R][C - 1][k], dp[R - 1][C][k]));
  cout << ans << "\n";
  return 0;
}
Problem p02586 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
typedef long long ll;
const int N = 3333;
int a[N][N];
ll f[N][N][3];
int n, m, k;
int main() {
  int x, y, v;
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 1; i <= k; i++) {
    scanf("%d%d%d", &x, &y, &v);
    a[x][y] = v;
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      for (int k = 0; k <= 3; k++) {
        if (k)
          f[i][j][k] = max(f[i][j][k], f[i][j - 1][k - 1] + a[i][j]);
        f[i][j][k] = max(f[i][j][k], f[i][j - 1][k]);
      }
      for (int k = 0; k <= 3; k++) {
        f[i][j][0] = max(f[i][j][0], f[i - 1][j][k]);
        f[i][j][1] = max(f[i][j][1], f[i - 1][j][k] + a[i][j]);
      }
    }
  ll ans = 0;
  for (int i = 0; i <= 3; i++)
    ans = max(ans, f[n][m][i]);
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
typedef long long ll;
const int N = 3333;
int a[N][N];
ll f[N][N][4];
int n, m, k;
int main() {
  int x, y, v;
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 1; i <= k; i++) {
    scanf("%d%d%d", &x, &y, &v);
    a[x][y] = v;
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      for (int k = 0; k <= 3; k++) {
        if (k)
          f[i][j][k] = max(f[i][j][k], f[i][j - 1][k - 1] + a[i][j]);
        f[i][j][k] = max(f[i][j][k], f[i][j - 1][k]);
      }
      for (int k = 0; k <= 3; k++) {
        f[i][j][0] = max(f[i][j][0], f[i - 1][j][k]);
        f[i][j][1] = max(f[i][j][1], f[i - 1][j][k] + a[i][j]);
      }
    }
  ll ans = 0;
  for (int i = 0; i <= 3; i++)
    ans = max(ans, f[n][m][i]);
  printf("%lld\n", ans);
  return 0;
}
Problem p02586 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int INF = 0x3f3f3f3f;
const ll inf = 0x3f3f3f3f3f3f3f3f;
const int N = 2e6 + 10;
const ll mod = 1e9 + 7;
int n, m, k;
ll a[3010][3010];
ll dp[3010][3010][4];
int main() {
  scanf("%d%d%d", &n, &m, &k);
  // for (int i = 1; i <= n; i++)for (int j = 1; j <= m; j++)a[i][j] = -inf;
  for (int i = 1; i <= k; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    if (a[x][y] != -inf)
      while (1)
        ;
    scanf("%lld", &a[x][y]);
  }
  // for (int i = 0; i <= n; i++)for (int j = 0; j <= m; j++)for (int x = 0; x
  // <= 3; x++)dp[i][j][k] = -inf;
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= m; j++)
      dp[i][j][0] = 0;
  dp[1][1][0] = 0;
  if (a[1][1] >= 0)
    dp[1][1][1] = a[1][1];
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      for (int k = 1; k <= 3; k++) {
        dp[i][j][k] = max(dp[i - 1][j][3] + a[i][j],
                          max(dp[i][j - 1][k - 1] + a[i][j], dp[i][j - 1][k]));
        ans = max(ans, dp[i][j][k]);
      }
    }
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int INF = 0x3f3f3f3f;
const ll inf = 0x3f3f3f3f3f3f3f3f;
const int N = 2e6 + 10;
const ll mod = 1e9 + 7;
int n, m, k;
ll a[3010][3010];
ll dp[3010][3010][4];
int main() {
  scanf("%d%d%d", &n, &m, &k);
  // for (int i = 1; i <= n; i++)for (int j = 1; j <= m; j++)a[i][j] = -inf;
  for (int i = 1; i <= k; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    scanf("%lld", &a[x][y]);
  }
  // for (int i = 0; i <= n; i++)for (int j = 0; j <= m; j++)for (int x = 0; x
  // <= 3; x++)dp[i][j][k] = -inf;
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= m; j++)
      dp[i][j][0] = 0;
  dp[1][1][0] = 0;
  if (a[1][1] >= 0)
    dp[1][1][1] = a[1][1];
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      for (int k = 1; k <= 3; k++) {
        dp[i][j][k] = max(dp[i - 1][j][3] + a[i][j],
                          max(dp[i][j - 1][k - 1] + a[i][j], dp[i][j - 1][k]));
        ans = max(ans, dp[i][j][k]);
      }
    }
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p02586 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <fstream>
#include <iostream>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> llP;

ll mod(ll a, ll b) {
  ll ret = a % b;
  if (ret < 0)
    ret += b;
  return ret;
}

ll modpow(ll a, ll b, ll c) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = mod(res * a, c);
    a = mod(a * a, c);
    b >>= 1;
  }
  return res;
}

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  ll memo[r + 1][c + 1];
  for (int i = 0; i < r + 1; i++) {
    for (int j = 0; j < c + 1; j++) {
      memo[i][j] = 0;
    }
  }
  for (int i = 0; i < k; i++) {
    int R, C;
    ll v;
    cin >> R >> C >> v;
    memo[R][C] = v;
  }
  ll dp[r + 1][c + 1][4];
  for (int i = 0; i < r + 1; i++) {
    for (int j = 0; j < c + 1; j++) {
      for (int l = 0; l < 4; l++) {
        dp[i][j][l] = 0;
      }
    }
  }
  for (int i = 1; i < r + 1; i++) {
    for (int j = 1; j < c + 1; j++) {
      for (int l = 0; l < 4; l++) {
        dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][l]);
      }
      dp[i][j][0] = max(dp[i][j][0], dp[i][j - 1][0]);
      dp[i][j][1] = max(dp[i][j - 1][1], dp[i][j][0] + memo[i][j]);
      dp[i][j][2] = max(dp[i][j - 1][2], dp[i][j - 1][1] + memo[i][j]);
      dp[i][j][3] = max(dp[i][j - 1][3], dp[i][j - 1][2] + memo[i][j]);
    }
  }
  ll ans = 0;
  for (int i = 0; i < 4; i++) {
    ans = max(ans, dp[r][c][i]);
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <fstream>
#include <iostream>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> llP;

ll mod(ll a, ll b) {
  ll ret = a % b;
  if (ret < 0)
    ret += b;
  return ret;
}

ll modpow(ll a, ll b, ll c) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = mod(res * a, c);
    a = mod(a * a, c);
    b >>= 1;
  }
  return res;
}

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  ll memo[r + 1][c + 1];
  for (int i = 0; i < r + 1; i++) {
    for (int j = 0; j < c + 1; j++) {
      memo[i][j] = 0;
    }
  }
  for (int i = 0; i < k; i++) {
    int R, C;
    ll v;
    cin >> R >> C >> v;
    memo[R][C] = v;
  }
  vector<vector<vector<ll>>> dp(r + 1,
                                vector<vector<ll>>(c + 1, vector<ll>(4, 0)));
  for (int i = 0; i < r + 1; i++) {
    for (int j = 0; j < c + 1; j++) {
      for (int l = 0; l < 4; l++) {
        dp[i][j][l] = 0;
      }
    }
  }
  for (int i = 1; i < r + 1; i++) {
    for (int j = 1; j < c + 1; j++) {
      for (int l = 0; l < 4; l++) {
        dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][l]);
      }
      dp[i][j][0] = max(dp[i][j][0], dp[i][j - 1][0]);
      dp[i][j][1] = max(dp[i][j - 1][1], dp[i][j][0] + memo[i][j]);
      dp[i][j][2] = max(dp[i][j - 1][2], dp[i][j - 1][1] + memo[i][j]);
      dp[i][j][3] = max(dp[i][j - 1][3], dp[i][j - 1][2] + memo[i][j]);
    }
  }
  ll ans = 0;
  for (int i = 0; i < 4; i++) {
    ans = max(ans, dp[r][c][i]);
  }
  cout << ans << endl;
  return 0;
}
Problem p02586 (C++)

Buggy submission (status: Runtime Error):
// Hail god Yato

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

#define hs                                                                     \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
typedef long long ll;
const ll mod = 1000000007;
const ll INF = 1e18;
const ll MAX = 3005;
//
//
int r, c;
ll g[MAX][MAX];
ll dp[MAX][MAX][4]; // 0 1 2 3

ll check(int x, int y, int type) {
  if (x > r || y > c || x <= 0 || y <= 0 || type < 0)
    return LONG_LONG_MIN;

  ll &ans = dp[x][y][type];
  if (ans != -1)
    return ans;
  if (x == 1 && y == 1) {
    if (type != 0)
      return g[1][1];
    // else if(type == 0)
    return 0;
  }

  ans = max({max({check(x - 1, y, 3), check(x - 1, y, 2), check(x - 1, y, 1),
                  check(x - 1, y, 0)}) +
                 ((type > 0) ? g[x][y] : 0),
             check(x, y - 1, type), check(x, y - 1, type - 1) + g[x][y]});
  return ans;
}
void solve() {
  cin >> r >> c;
  int k;
  cin >> k;
  memset(g, 0, sizeof(g));
  memset(dp, -1, sizeof(dp));
  for (int i = 0; i < k; i++) {
    int x, y, val;
    cin >> x >> y >> val;
    g[x][y] = val;
  }

  cout << max({check(r, c, 0), check(r, c, 1), check(r, c, 2), check(r, c, 3)})
       << "\n";
}
int main() {
  hs;
  ll t;
  t = 1;
  cin >> t;
  for (int i = 1; i <= t; i++) {
    // cout<<"Case #"<<i<<": ";
    solve();
  }
  return 0;
}

Fixed submission:
// Hail god Yato

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

#define hs                                                                     \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
typedef long long ll;
const ll mod = 1000000007;
const ll INF = 1e18;
const ll MAX = 3005;
//
//
int r, c;
ll g[MAX][MAX];
ll dp[MAX][MAX][4]; // 0 1 2 3

ll check(int x, int y, int type) {
  if (x > r || y > c || x <= 0 || y <= 0 || type < 0)
    return LONG_LONG_MIN;

  ll &ans = dp[x][y][type];
  if (ans != -1)
    return ans;
  if (x == 1 && y == 1) {
    if (type != 0)
      return g[1][1];
    // else if(type == 0)
    return 0;
  }

  ans = max({max({check(x - 1, y, 3), check(x - 1, y, 2), check(x - 1, y, 1),
                  check(x - 1, y, 0)}) +
                 ((type > 0) ? g[x][y] : 0),
             check(x, y - 1, type), check(x, y - 1, type - 1) + g[x][y]});
  return ans;
}
void solve() {
  cin >> r >> c;
  int k;
  cin >> k;
  memset(g, 0, sizeof(g));
  memset(dp, -1, sizeof(dp));
  for (int i = 0; i < k; i++) {
    int x, y, val;
    cin >> x >> y >> val;
    g[x][y] = val;
  }

  cout << max({check(r, c, 0), check(r, c, 1), check(r, c, 2), check(r, c, 3)})
       << "\n";
}
int main() {
  hs;
  ll t;
  t = 1;
  // cin>>t;
  for (int i = 1; i <= t; i++) {
    // cout<<"Case #"<<i<<": ";
    solve();
  }
  return 0;
}
Problem p02586 (C++)

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

long long dp[6][6][4];

int main() {
  int R, C, K;
  cin >> R >> C >> K;
  vector<vector<long long>> table(R, vector<long long>(C));

  for (int i = 0; i < K; ++i) {
    int r, c, v;
    cin >> r >> c >> v;
    r--;
    c--;
    table[r][c] = v;
  }

  // init
  for (int i = 0; i < R; ++i) {
    for (int j = 0; j < C; ++j) {
      for (int k = 0; k < 3; ++k) {
        // 右に行く場合
        // 使う場合
        dp[i][j + 1][k + 1] =
            max(dp[i][j + 1][k + 1], dp[i][j][k] + table[i][j]);
        // 使わない場合
        dp[i][j + 1][k + 1] = max(dp[i][j + 1][k + 1], dp[i][j][k + 1]);

        // 下に行く場合
        dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j + 1][k + 1]);
      }
    }
  }

  cout << dp[R][C - 1][0] << endl;
}

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

long long dp[3005][3005][4];

int main() {
  int R, C, K;
  cin >> R >> C >> K;
  vector<vector<long long>> table(R, vector<long long>(C));

  for (int i = 0; i < K; ++i) {
    int r, c, v;
    cin >> r >> c >> v;
    r--;
    c--;
    table[r][c] = v;
  }

  // init
  for (int i = 0; i < R; ++i) {
    for (int j = 0; j < C; ++j) {
      for (int k = 0; k < 3; ++k) {
        // 右に行く場合
        // 使う場合
        dp[i][j + 1][k + 1] =
            max(dp[i][j + 1][k + 1], dp[i][j][k] + table[i][j]);
        // 使わない場合
        dp[i][j + 1][k + 1] = max(dp[i][j + 1][k + 1], dp[i][j][k + 1]);

        // 下に行く場合
        dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j + 1][k + 1]);
      }
    }
  }

  cout << dp[R][C - 1][0] << endl;
}
Problem p02586 (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++)
#define ll long long
ll gcd(int x, int y) { return (x % y) ? gcd(y, x % y) : y; } // 最大公約数
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }             // 最小公倍数
using Graph = vector<vector<int>>;
ll inf = 300000000000000000;
const double PI = 3.14159265358979323846;
int main() {
  int r, c, k;
  cin >> r >> c >> k;
  ll o[r][c];
  rep(i, r) rep(j, c) o[i][j] = 0;
  rep(i, k) {
    ll a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    o[a][b] = c;
  }
  ll dp[r][c][4];
  rep(i, r) rep(j, c) rep(l, 4) dp[i][j][l] = 0;
  dp[0][0][1] = o[0][0];
  rep(i, r) {
    rep(j, c) {
      if (i + j == 0)
        continue;
      if (o[i][j] == 0) {
        rep(l, 4) {
          if (j != 0) {
            dp[i][j][l] = max(dp[i][j][l], dp[i][j - 1][l]);
          }
          if (i != 0) {
            dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][l]);
          }
        }
      } else {
        if (j != 0) {
          rep(l, 4) {
            dp[i][j][l] = max(dp[i][j][l], dp[i][j - 1][l]);
            if (l != 3)
              dp[i][j][l + 1] = max(dp[i][j][l + 1], o[i][j] + dp[i][j - 1][l]);
          }
        }
        if (i != 0) {
          rep(l, 4) {
            dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][l]);
            dp[i][j][1] = max(dp[i][j][1], o[i][j] + dp[i - 1][j][l]);
          }
        }
      }
    }
  }
  ll ans = 0;
  rep(i, 4) ans = max(ans, dp[r - 1][c - 1][i]);
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
ll gcd(int x, int y) { return (x % y) ? gcd(y, x % y) : y; } // 最大公約数
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }             // 最小公倍数
using Graph = vector<vector<int>>;
ll inf = 300000000000000000;
const double PI = 3.14159265358979323846;
int main() {
  int r, c, k;
  cin >> r >> c >> k;
  ll o[r][c];
  rep(i, r) rep(j, c) o[i][j] = 0;
  rep(i, k) {
    ll a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    o[a][b] = c;
  }
  vector<vector<vector<ll>>> dp(r, vector<vector<ll>>(c, vector<ll>(4)));
  rep(i, r) rep(j, c) rep(l, 4) dp[i][j][l] = 0;
  dp[0][0][1] = o[0][0];
  rep(i, r) {
    rep(j, c) {
      if (i + j == 0)
        continue;
      if (o[i][j] == 0) {
        rep(l, 4) {
          if (j != 0) {
            dp[i][j][l] = max(dp[i][j][l], dp[i][j - 1][l]);
          }
          if (i != 0) {
            dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][l]);
          }
        }
      } else {
        if (j != 0) {
          rep(l, 4) {
            dp[i][j][l] = max(dp[i][j][l], dp[i][j - 1][l]);
            if (l != 3)
              dp[i][j][l + 1] = max(dp[i][j][l + 1], o[i][j] + dp[i][j - 1][l]);
          }
        }
        if (i != 0) {
          rep(l, 4) {
            dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][l]);
            dp[i][j][1] = max(dp[i][j][1], o[i][j] + dp[i - 1][j][l]);
          }
        }
      }
    }
  }
  ll ans = 0;
  rep(i, 4) ans = max(ans, dp[r - 1][c - 1][i]);
  cout << ans << endl;
}
Problem p02586 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define l long
#define mk make_pair
#define pb push_back
#define in insert
#define se second
#define fi first
#define mod 1000000007
#define watch(x) cout << (#x) << " is " << (x) << "\n"
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define pii pair<int, int>
using namespace std;
ll dp[3000][3000][4];
ll mat[3000][3000];
int main() {
  fast;

  ll n, m, k, j, i;
  cin >> n >> m >> k;
  while (k--) {
    ll r, c, v;
    cin >> r >> c >> v;
    mat[r][c] = v;
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      ll ha = max(
          {dp[i - 1][j][0], dp[i - 1][j][1], dp[i - 1][j][2], dp[i - 1][j][3]});
      dp[i][j][0] = max({ha, dp[i][j - 1][0]});
      dp[i][j][1] =
          max({ha + mat[i][j], dp[i][j - 1][0] + mat[i][j], dp[i][j - 1][1]});
      dp[i][j][2] = max(dp[i][j - 1][1] + mat[i][j], dp[i][j - 1][2]);
      dp[i][j][3] = max(dp[i][j - 1][2] + mat[i][j], dp[i][j - 1][3]);
    }
  }
  cout << max({dp[n][m][0], dp[n][m][1], dp[n][m][2], dp[n][m][3]});
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define l long
#define mk make_pair
#define pb push_back
#define in insert
#define se second
#define fi first
#define mod 1000000007
#define watch(x) cout << (#x) << " is " << (x) << "\n"
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define pii pair<int, int>
using namespace std;
ll dp[3003][3003][5];
ll mat[3003][3003];
int main() {
  fast;

  ll n, m, k, j, i;
  cin >> n >> m >> k;
  while (k--) {
    ll r, c, v;
    cin >> r >> c >> v;
    mat[r][c] = v;
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      ll ha = max(
          {dp[i - 1][j][0], dp[i - 1][j][1], dp[i - 1][j][2], dp[i - 1][j][3]});
      dp[i][j][0] = max({ha, dp[i][j - 1][0]});
      dp[i][j][1] =
          max({ha + mat[i][j], dp[i][j - 1][0] + mat[i][j], dp[i][j - 1][1]});
      dp[i][j][2] = max(dp[i][j - 1][1] + mat[i][j], dp[i][j - 1][2]);
      dp[i][j][3] = max(dp[i][j - 1][2] + mat[i][j], dp[i][j - 1][3]);
    }
  }
  cout << max({dp[n][m][0], dp[n][m][1], dp[n][m][2], dp[n][m][3]});
}
Problem p02586 (C++)

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

ll g[3001][3001]{};
ll a[3001][3001][4]{};

int main() {
  int R, C, K;
  cin >> R >> C >> K;
  int r, c, v;
  for (int i = 0; i < K; i++) {
    cin >> r >> c >> v;
    g[r][c] = v;
  }

  for (int i = 0; i <= R; i++) {
    for (int j = 0; j <= C; j++) {
      for (int k = 0; k <= 3; k++) {
        a[i + 1][j][0] = max(a[i + 1][j][0], a[i][j][k]);
        a[i][j + 1][k] = max(a[i][j + 1][k], a[i][j][k]);
        if (k == 3) {
          a[i + 1][j][1] = max(a[i + 1][j][1], a[i][j][k] + g[i + 1][j]);
        } else {
          a[i + 1][j][1] = max(a[i + 1][j][1], a[i][j][k] + g[i + 1][j]);
          a[i][j + 1][k + 1] =
              max(a[i][j + 1][k + 1], a[i][j][k] + g[i][j + 1]);
        }
      }
    }
  }
  ll best = 0;
  for (int i = 0; i <= 3; i++) {
    best = max(best, a[R][C][i]);
  }
  cout << best << "\n";
  return 0;
}

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

ll g[3002][3002]{};
ll a[3002][3002][4]{};

int main() {
  int R, C, K;
  cin >> R >> C >> K;
  int r, c, v;
  for (int i = 0; i < K; i++) {
    cin >> r >> c >> v;
    g[r][c] = v;
  }

  for (int i = 0; i <= R; i++) {
    for (int j = 0; j <= C; j++) {
      for (int k = 0; k <= 3; k++) {
        a[i + 1][j][0] = max(a[i + 1][j][0], a[i][j][k]);
        a[i][j + 1][k] = max(a[i][j + 1][k], a[i][j][k]);
        if (k == 3) {
          a[i + 1][j][1] = max(a[i + 1][j][1], a[i][j][k] + g[i + 1][j]);
        } else {
          a[i + 1][j][1] = max(a[i + 1][j][1], a[i][j][k] + g[i + 1][j]);
          a[i][j + 1][k + 1] =
              max(a[i][j + 1][k + 1], a[i][j][k] + g[i][j + 1]);
        }
      }
    }
  }
  ll best = 0;
  for (int i = 0; i <= 3; i++) {
    best = max(best, a[R][C][i]);
  }
  cout << best << "\n";
  return 0;
}
Problem p02586 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define rep1(i, n) for (int i = 1; i <= (int)n; i++)
#define sp(n) cout << fixed << setprecision(n)
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;
}
typedef long long ll;
using namespace std;
int main(void) {
  int R, C, k;
  cin >> R >> C >> k;
  vector<vector<ll>> t(R, vector<ll>(C, 0));
  rep(i, k) {
    int r, c;
    ll v;
    cin >> r >> c >> v;
    r--;
    c--;
    t[r][c] = v;
  }
  ll dp[R][C][4];
  rep(i, R) rep(j, C) rep(l, 4) dp[i][j][l] = 0;
  rep(i, R) rep(j, C) {
    if (i != R - 1) {
      rep(l, 4) chmax(dp[i + 1][j][0], dp[i][j][l]);
      rep(l, 3) chmax(dp[i + 1][j][0], dp[i][j][l] + t[i][j]);
    }
    if (j != C - 1) {
      rep(l, 4) chmax(dp[i][j + 1][l], dp[i][j][l]);
      rep(l, 3) chmax(dp[i][j + 1][l + 1], dp[i][j][l] + t[i][j]);
    }
  }
  rep(i, 3)
      chmax(dp[R - 1][C - 1][3 - i], dp[R - 1][C - 1][2 - i] + t[R - 1][C - 1]);
  ll res = 0;
  rep(i, 4) chmax(res, dp[R - 1][C - 1][i]);
  cout << res << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define rep1(i, n) for (int i = 1; i <= (int)n; i++)
#define sp(n) cout << fixed << setprecision(n)
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;
}
typedef long long ll;
using namespace std;
int main(void) {
  int R, C, k;
  cin >> R >> C >> k;
  vector<vector<ll>> t(R, vector<ll>(C, 0));
  rep(i, k) {
    int r, c;
    ll v;
    cin >> r >> c >> v;
    r--;
    c--;
    t[r][c] = v;
  }
  vector<vector<vector<ll>>> dp(R, vector<vector<ll>>(C, vector<ll>(4, 0)));
  rep(i, R) rep(j, C) {
    if (i != R - 1) {
      rep(l, 4) chmax(dp[i + 1][j][0], dp[i][j][l]);
      rep(l, 3) chmax(dp[i + 1][j][0], dp[i][j][l] + t[i][j]);
    }
    if (j != C - 1) {
      rep(l, 4) chmax(dp[i][j + 1][l], dp[i][j][l]);
      rep(l, 3) chmax(dp[i][j + 1][l + 1], dp[i][j][l] + t[i][j]);
    }
  }
  rep(i, 3)
      chmax(dp[R - 1][C - 1][3 - i], dp[R - 1][C - 1][2 - i] + t[R - 1][C - 1]);
  ll res = 0;
  rep(i, 4) chmax(res, dp[R - 1][C - 1][i]);
  cout << res << endl;
}
Problem p02586 (C++)

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

signed main() {
  // cout << fixed << setprecision(10) << flush;

  int R, C, K;
  cin >> R >> C >> K;
  vector<vector<int>> dist(R + 1, vector<int>(C + 1, 0));
  for (int i = 0; i < K; i++) {
    int r, c, v;
    cin >> r >> c >> v;
    dist[r][c] = v;
  }

  // dp[i][j][k][l] は i で k 個、j で l 個 アイテムを拾った時のmax
  int dp[R + 1][C + 1][4];
  for (int i = 0; i <= R; i++) {
    for (int j = 0; j <= C; j++) {
      for (int k = 0; k < 4; k++) {
        dp[i][j][k] = 0;
      }
    }
  }

  // 範囲チェック
  for (int i = 1; i <= R; i++) {
    for (int j = 1; j <= C; j++) {
      for (int k = 0; k < 4; k++) {
        dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
        dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][k]);
        dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][k] + dist[i][j]);
        if (k != 0 && dist[i][j] != 0) {
          dp[i][j][k] = max(dp[i][j - 1][k - 1] + dist[i][j], dp[i][j][k]);
        }
      }
    }
  }

  int ans = 0;
  for (int k = 0; k < 4; k++) {
    ans = max(ans, dp[R][C][k]);
  }
  cout << ans << endl;

  return 0;
}

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

signed main() {
  // cout << fixed << setprecision(10) << flush;

  int R, C, K;
  cin >> R >> C >> K;
  vector<vector<int>> dist(R + 1, vector<int>(C + 1, 0));
  for (int i = 0; i < K; i++) {
    int r, c, v;
    cin >> r >> c >> v;
    dist[r][c] = v;
  }

  // dp[i][j][k] は i で k 個アイテムを拾った時のmax
  vector<vector<vector<int>>> dp(R + 1,
                                 vector<vector<int>>(C + 1, vector<int>(4, 0)));
  // for(int i=0; i<=R; i++){
  //     for(int j=0; j<=C; j++){
  //         for(int k=0; k<4; k++){
  //             dp[i][j][k] = 0;
  //         }
  //     }
  // }

  // 範囲チェック
  for (int i = 1; i <= R; i++) {
    for (int j = 1; j <= C; j++) {
      for (int k = 0; k < 4; k++) {
        dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
        dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][k]);
        dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][k] + dist[i][j]);
        if (k != 0 && dist[i][j] != 0) {
          dp[i][j][k] = max(dp[i][j - 1][k - 1] + dist[i][j], dp[i][j][k]);
        }
      }
    }
  }

  int ans = 0;
  for (int k = 0; k < 4; k++) {
    ans = max(ans, dp[R][C][k]);
  }
  cout << ans << endl;

  return 0;
}
Problem p02588 (C++)

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

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<vector<int>> cnt(20, vector<int>(20));
  vector<pair<int, int>> a;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    long long x = 0;
    bool found = false;
    for (int i = 0; i < (int)s.size(); i++) {
      if (s[i] != '.')
        (x *= 10) += s[i] - '0';
      else
        found = true;
    }
    if (!found) {
      x *= (long long)1e9;
    } else {
      int cur = 0;
      while (s.back() != '.') {
        ++cur;
        s.pop_back();
      }
      for (int it = 0; it < 9 - cur; it++)
        x *= 10;
    }
    int cnt2 = 0;
    long long y = x;
    while (y % 2 == 0) {
      ++cnt2;
      y /= 2;
    }
    int cnt5 = 0;
    y = x;
    while (y % 5 == 0) {
      ++cnt5;
      y /= 5;
    }
    ++cnt[min(18, cnt2)][min(18, cnt5)];
    a.emplace_back(cnt2, cnt5);
  }
  for (int i = 18; i >= 0; i--) {
    for (int j = 18; j >= 0; j--) {
      cnt[i][j] += cnt[i + 1][j] + cnt[i][j + 1] - cnt[i + 1][j + 1];
    }
  }
  long long res = 0;
  for (auto x : a) {
    res += cnt[18 - x.first][18 - x.second] - (x.first >= 9 && x.second >= 9);
  }
  res /= 2;
  cout << res << '\n';
  return 0;
}


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

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<vector<int>> cnt(20, vector<int>(20));
  vector<pair<int, int>> a;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    long long x = 0;
    bool found = false;
    for (int i = 0; i < (int)s.size(); i++) {
      if (s[i] != '.')
        (x *= 10) += s[i] - '0';
      else
        found = true;
    }
    if (!found) {
      x *= (long long)1e9;
    } else {
      int cur = 0;
      while (s.back() != '.') {
        ++cur;
        s.pop_back();
      }
      for (int it = 0; it < 9 - cur; it++)
        x *= 10;
    }
    int cnt2 = 0;
    long long y = x;
    while (y % 2 == 0) {
      ++cnt2;
      y /= 2;
    }
    int cnt5 = 0;
    y = x;
    while (y % 5 == 0) {
      ++cnt5;
      y /= 5;
    }
    ++cnt[min(18, cnt2)][min(18, cnt5)];
    a.emplace_back(min(18, cnt2), min(18, cnt5));
  }
  for (int i = 18; i >= 0; i--) {
    for (int j = 18; j >= 0; j--) {
      cnt[i][j] += cnt[i + 1][j] + cnt[i][j + 1] - cnt[i + 1][j + 1];
    }
  }
  long long res = 0;
  for (auto x : a) {
    res += cnt[18 - x.first][18 - x.second] - (x.first >= 9 && x.second >= 9);
  }
  res /= 2;
  cout << res << '\n';
  return 0;
}
Problem p02588 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define llong long long
#define mkpr make_pair
#define x first
#define y second
#define iter iterator
#define riter reverse_iterator
#define y1 Lorem_ipsum_
#define tm dolor_sit_amet_
using namespace std;

inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar()) {
    if (ch == '-')
      f = -1;
  }
  for (; isdigit(ch); ch = getchar()) {
    x = x * 10 + ch - 48;
  }
  return x * f;
}

const int mxN = 2e5;
const int mxW = 100;
double a[mxN + 3];
int a1[mxW + 3], a2[mxW + 3];
int s[mxW + 3][mxW + 3];
int n;

int main() {
  n = read();
  for (int i = 1; i <= n; i++) {
    scanf("%lf", &a[i]);
    a[i] *= 1000000000.0;
    llong x = (llong)(a[i] + 0.5);
    while (x % 2 == 0) {
      a1[i]++;
      x /= 2;
    }
    while (x % 5 == 0) {
      a2[i]++;
      x /= 5;
    }
    s[a1[i]][a2[i]]++;
  }
  llong ans = 0ll;
  for (int i = mxW; i >= 0; i--)
    for (int j = mxW; j >= 0; j--) {
      s[i][j] += s[i + 1][j] + s[i][j + 1] - s[i + 1][j + 1];
    }
  for (int i = 1; i <= n; i++) {
    ans += s[max(0, 18 - a1[i])][max(0, 18 - a2[i])];
  }
  ans -= s[9][9];
  printf("%lld\n", ans / 2ll);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define llong long long
#define mkpr make_pair
#define x first
#define y second
#define iter iterator
#define riter reverse_iterator
#define y1 Lorem_ipsum_
#define tm dolor_sit_amet_
using namespace std;

inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar()) {
    if (ch == '-')
      f = -1;
  }
  for (; isdigit(ch); ch = getchar()) {
    x = x * 10 + ch - 48;
  }
  return x * f;
}

const int mxN = 2e5;
const int mxW = 100;
double a[mxN + 3];
int a1[mxN + 3], a2[mxN + 3];
int s[mxW + 3][mxW + 3];
int n;

int main() {
  n = read();
  for (int i = 1; i <= n; i++) {
    scanf("%lf", &a[i]);
    a[i] *= 1000000000.0;
    llong x = (llong)(a[i] + 0.5);
    while (x % 2 == 0) {
      a1[i]++;
      x /= 2;
    }
    while (x % 5 == 0) {
      a2[i]++;
      x /= 5;
    }
    s[a1[i]][a2[i]]++;
  }
  llong ans = 0ll;
  for (int i = mxW; i >= 0; i--)
    for (int j = mxW; j >= 0; j--) {
      s[i][j] += s[i + 1][j] + s[i][j + 1] - s[i + 1][j + 1];
    }
  for (int i = 1; i <= n; i++) {
    ans += s[max(0, 18 - a1[i])][max(0, 18 - a2[i])];
  }
  ans -= s[9][9];
  printf("%lld\n", ans / 2ll);
  return 0;
}
Problem p02588 (C++)

Buggy submission (status: Runtime Error):
#include "algorithm"
#include "bitset"
#include "cassert"
#include "climits"
#include "cmath"
#include "cstdio"
#include "ctime"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

using namespace std;

constexpr long long int MOD = 1000000007;
// constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
// constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-12;

// int N, M, K, T, H, W, L, R;
long long int N, M, K, T, H, W, L, R;

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

  cin >> N;
  int num[19][19] = {};
  long long int ans = 0;
  for (int i = 0; i < N; i++) {
    string s;
    cin >> s;
    if (s.find('.') == s.npos) {
      s.push_back('.');
    }
    while (s.size() - s.find('.') < 10) {
      s.push_back('0');
    }
    s.erase(s.begin() + s.find('.'));
    // cout << s << endl;
    M = stoll(s);
    while (s.front() == '0')
      s.erase(s.begin());
    //	cout << M << endl;
    int a = 0, b = 0;
    while (M % 2 == 0) {
      M /= 2;
      a++;
    }
    while (M % 5 == 0) {
      M /= 5;
      b++;
    }
    for (int j = 0; j <= 18; j++) {
      for (int k = 0; k <= 18; k++) {
        if (a + j >= 18 && b + k >= 18)
          ans += num[j][k];
      }
    }
    num[a][b]++;
  }
  cout << ans << endl;
}

Fixed submission:
#include "algorithm"
#include "bitset"
#include "cassert"
#include "climits"
#include "cmath"
#include "cstdio"
#include "ctime"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

using namespace std;

constexpr long long int MOD = 1000000007;
// constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
// constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-12;

// int N, M, K, T, H, W, L, R;
long long int N, M, K, T, H, W, L, R;

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

  cin >> N;
  int num[19][19] = {};
  long long int ans = 0;
  for (int i = 0; i < N; i++) {
    string s;
    cin >> s;
    if (s.find('.') == s.npos) {
      s.push_back('.');
    }
    while (s.size() - s.find('.') < 10) {
      s.push_back('0');
    }
    s.erase(s.begin() + s.find('.'));
    // cout << s << endl;
    M = stoll(s);
    while (s.front() == '0')
      s.erase(s.begin());
    //	cout << M << endl;
    int a = 0, b = 0;
    while (M % 2 == 0) {
      M /= 2;
      a++;
    }
    while (M % 5 == 0) {
      M /= 5;
      b++;
    }
    a = min(a, 18);
    b = min(b, 18);
    for (int j = 0; j <= 18; j++) {
      for (int k = 0; k <= 18; k++) {
        if (a + j >= 18 && b + k >= 18)
          ans += num[j][k];
      }
    }
    num[a][b]++;
  }
  cout << ans << endl;
}
Problem p02588 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, N) for (int i = 0; i < (N); i++)

int main() {
  int n;
  scanf("%d", &n);
  map<pair<int, int>, int> cnt;
  rep(i, n) {
    double x;
    cin >> x;
    long long xl = llround(x * 1e9);
    int two = 0, five = 0;
    while (xl % 2 == 0) {
      two++;
      xl /= 2;
    }
    while (i % 5 == 0) {
      five++;
      xl /= 5;
    }
    ++cnt[{two, five}];
  }
  long long answer = 0;
  for (auto p1 : cnt) {
    for (auto p2 : cnt) {
      if (p1.first.first + p2.first.first >= 18 &&
          p1.first.second + p2.first.second >= 18) {
        if (p1 < p2) {
          answer += (long long)p1.second * p2.second;
        } else if (p1 == p2) {
          answer += (long long)p1.second * (p1.second - 1) / 2;
        }
      }
    }
  }
  cout << answer << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, N) for (int i = 0; i < (N); i++)

int main() {
  int n;
  scanf("%d", &n);
  map<pair<int, int>, int> cnt;
  rep(i, n) {
    double x;
    cin >> x;
    long long xl = llround(x * 1e9);
    int two = 0, five = 0;
    while (xl % 2 == 0) {
      two++;
      xl /= 2;
    }
    while (xl % 5 == 0) {
      five++;
      xl /= 5;
    }
    ++cnt[{two, five}];
  }
  long long answer = 0;
  for (auto p1 : cnt) {
    for (auto p2 : cnt) {
      if (p1.first.first + p2.first.first >= 18 &&
          p1.first.second + p2.first.second >= 18) {
        if (p1 < p2) {
          answer += (long long)p1.second * p2.second;
        } else if (p1 == p2) {
          answer += (long long)p1.second * (p1.second - 1) / 2;
        }
      }
    }
  }
  cout << answer << endl;
}
Problem p02588 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#define val 1000000000
#define for2(x) for (x = 0; x <= 9; x++)
using namespace std;
int n, i, b1, b2, b3, b4, c1, c2, c3, c4;
long long x, y, r, sol;
int a[12][12][12][12];
double curr;
long long cmmdc(long long a, long long b) {
  long long r;
  while (b != 0) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
int main() {
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> curr;
    curr += 0.0000000001;
    x = curr * val;
    y = val;
    r = cmmdc(x, y);
    x /= r;
    y /= r;
    b1 = b2 = b3 = b4 = 0;
    if (y == 1) {
      sol--;
    }
    while (x % 2 == 0) {
      b1++;
      x /= 2;
    }
    while (x % 5 == 0) {
      b2++;
      x /= 5;
    }
    while (y % 2 == 0) {
      b3++;
      y /= 2;
    }
    while (y % 5 == 0) {
      b4++;
      y /= 5;
    }
    a[b1][b2][b3][b4]++;
  }
  for2(b1) {
    for2(b2) {
      for2(b3) {
        for2(b4) {
          if (a[b1][b2][b3][b4] == 0) {
            continue;
          }
          for2(c1) {
            for2(c2) {
              for2(c3) {
                for2(c4) {
                  if (b1 >= c3 && b2 >= c4 && c1 >= b3 && c2 >= b4) {
                    sol += a[b1][b2][b3][b4] * 1LL * a[c1][c2][c3][c4];
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  sol /= 2;
  cout << sol;
}


Fixed submission:
#include <iostream>
#define val 1000000000
#define for2(x) for (x = 0; x <= 9; x++)
using namespace std;
int n, i, b1, b2, b3, b4, c1, c2, c3, c4;
long long x, y, r, sol;
int a[12][12][12][12];
double curr;
long long cmmdc(long long a, long long b) {
  long long r;
  while (b != 0) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
int main() {
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> curr;
    curr += 0.0000000001;
    x = curr * val;
    y = val;
    r = cmmdc(x, y);
    x /= r;
    y /= r;
    b1 = b2 = b3 = b4 = 0;
    if (y == 1) {
      sol--;
    }
    while (x % 2 == 0) {
      b1++;
      x /= 2;
    }
    while (x % 5 == 0) {
      b2++;
      x /= 5;
    }
    while (y % 2 == 0) {
      b3++;
      y /= 2;
    }
    while (y % 5 == 0) {
      b4++;
      y /= 5;
    }
    b1 = min(b1, 9);
    b2 = min(b2, 9);
    a[b1][b2][b3][b4]++;
  }
  for2(b1) {
    for2(b2) {
      for2(b3) {
        for2(b4) {
          if (a[b1][b2][b3][b4] == 0) {
            continue;
          }
          for2(c1) {
            for2(c2) {
              for2(c3) {
                for2(c4) {
                  if (b1 >= c3 && b2 >= c4 && c1 >= b3 && c2 >= b4) {
                    sol += a[b1][b2][b3][b4] * 1LL * a[c1][c2][c3][c4];
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  sol /= 2;
  cout << sol;
}