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

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

int dp[2001][2001][2];
//// < "D:\D_Download\Visual Studio
///2015\Projects\programing_contest_c++\Debug\a.txt"
int main() {
  int N, M;
  cin >> N >> M;
  string a, b;
  cin >> a >> b;
  // reverse(a.begin(), a.end());
  // reverse(b.begin(), b.end());
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i <= 2000; ++i) {
    for (int j = 0; j <= 2000; ++j) {
      dp[i][j][1] = 0;
      dp[i][j][0] = -1e9;
    }
  }
  int ans = 0;
  for (int i = 0; i <= N; ++i) {
    for (int j = 0; j <= M; ++j) {
      for (int k = 0; k < 2; ++k) {
        if (i != N) {
          if ((a[i] == 'I') == k) {
            dp[i + 1][j][!k] = max(dp[i + 1][j][!k], dp[i][j][k] + 1);
          }
        }
        if (j != M) {
          if ((b[j] == 'I') == k) {
            dp[i][j + 1][!k] = max(dp[i][j + 1][!k], dp[i][j][k] + 1);
          }
        }
      }
      ans = max(ans, dp[i][j][0]);
    }
  }
  if (ans % 2 == 0) {
    assert(false);
    ans--;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

int dp[2001][2001][2];
//// < "D:\D_Download\Visual Studio
///2015\Projects\programing_contest_c++\Debug\a.txt"
int main() {
  int N, M;
  cin >> N >> M;
  string a, b;
  cin >> a >> b;
  // reverse(a.begin(), a.end());
  // reverse(b.begin(), b.end());
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i <= 2000; ++i) {
    for (int j = 0; j <= 2000; ++j) {
      dp[i][j][1] = 0;
      dp[i][j][0] = -1e9;
    }
  }
  int ans = 0;
  for (int i = 0; i <= N; ++i) {
    for (int j = 0; j <= M; ++j) {
      for (int k = 0; k < 2; ++k) {
        if (i != N) {
          if ((a[i] == 'I') == k) {
            dp[i + 1][j][!k] = max(dp[i + 1][j][!k], dp[i][j][k] + 1);
          }
        }
        if (j != M) {
          if ((b[j] == 'I') == k) {
            dp[i][j + 1][!k] = max(dp[i][j + 1][!k], dp[i][j][k] + 1);
          }
        }
      }
      ans = max(ans, dp[i][j][0]);
    }
  }
  if (ans && ans % 2 == 0) {
    ans--;
  }
  cout << ans << endl;
  return 0;
}
Problem p00527 (C++)

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

char s[2][2001];
int dp[2001][2001][2], n[2];
int main() {
  scanf("%d%d%s%s", &n[0], &n[1], s[0], s[1]);
  int Max = 0;
  for (int i = n[0]; i >= 0; i--)
    for (int j = n[1]; j >= 0; j--)
      rep(t, 2) rep(k, 2) {
        int a = (k ? dp[i][j + 1][!t] : dp[i + 1][j][!t]), b = (k ? j : i);
        if (b == n[k] || a % 2 == t)
          continue;
        if (t != (s[k][b] == 'I' ? 0 : 1))
          dp[i][j][t] = max(dp[i][j][t], a + 1);
        if (dp[i][j][t] % 2)
          Max = max(Max, dp[i][j][t]);
      }
  printf("%d\n", Max);
}

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

char s[2][2001];
int dp[2002][2002][2], n[2];
int main() {
  scanf("%d%d%s%s", &n[0], &n[1], s[0], s[1]);
  int Max = 0;
  for (int i = n[0]; i >= 0; i--)
    for (int j = n[1]; j >= 0; j--)
      rep(t, 2) rep(k, 2) {
        int a = (k ? dp[i][j + 1][!t] : dp[i + 1][j][!t]), b = (k ? j : i);
        if (b == n[k] || a % 2 == t)
          continue;
        if (t != (s[k][b] == 'I' ? 0 : 1))
          dp[i][j][t] = max(dp[i][j][t], a + 1);
        if (dp[i][j][t] % 2)
          Max = max(Max, dp[i][j][t]);
      }
  printf("%d\n", Max);
}
Problem p00527 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int main() {
  int i, j, m, n, ans = 0, l;
  static int r[52][52][2] = {0};
  char s[4001], t[4001];
  scanf("%d%d", &m, &n);
  scanf("%s", s);
  scanf("%s", t);
  for (i = 0; i <= m; i++) {
    for (j = 0; j <= n; j++) {
      for (l = 0; l < 2; l++) {
        if (l == 0) {
          if (s[i] == 'I')
            r[i + 1][j][1] = max(r[i + 1][j][1], r[i][j][0] + 1);
          if (t[j] == 'I')
            r[i][j + 1][1] = max(r[i][j + 1][1], r[i][j][0] + 1);
        } else {
          if (s[i] == 'O' && r[i][j][1] != 0)
            r[i + 1][j][0] = max(r[i + 1][j][0], r[i][j][1] + 1);
          if (t[j] == 'O' && r[i][j][1] != 0)
            r[i][j + 1][0] = max(r[i][j + 1][0], r[i][j][1] + 1);
        }
      }
    }
  }
  for (i = 0; i <= m; i++) {
    for (j = 0; j <= n; j++) {
      ans = max(ans, r[i][j][1]);
    }
  }
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int main() {
  int i, j, m, n, ans = 0, l;
  static int r[2001][2001][2] = {0};
  char s[4001], t[4001];
  scanf("%d%d", &m, &n);
  scanf("%s", s);
  scanf("%s", t);
  for (i = 0; i <= m; i++) {
    for (j = 0; j <= n; j++) {
      for (l = 0; l < 2; l++) {
        if (l == 0) {
          if (s[i] == 'I')
            r[i + 1][j][1] = max(r[i + 1][j][1], r[i][j][0] + 1);
          if (t[j] == 'I')
            r[i][j + 1][1] = max(r[i][j + 1][1], r[i][j][0] + 1);
        } else {
          if (s[i] == 'O' && r[i][j][1] != 0)
            r[i + 1][j][0] = max(r[i + 1][j][0], r[i][j][1] + 1);
          if (t[j] == 'O' && r[i][j][1] != 0)
            r[i][j + 1][0] = max(r[i][j + 1][0], r[i][j][1] + 1);
        }
      }
    }
  }
  for (i = 0; i <= m; i++) {
    for (j = 0; j <= n; j++) {
      ans = max(ans, r[i][j][1]);
    }
  }
  printf("%d\n", ans);
  return 0;
}
Problem p00527 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;

int dp[2000][2000][2]; // a???n??????b???m????????§?????????(1,0)=(I,O)
int a, b;
string c, d;
int main() {
  cin >> a >> b >> c >> d;
  c += " ";
  d += " ";
  for (int e = a + b - 1; e >= 0; e--) {
    for (int x = 0; x <= a; x++) {
      int y = e - x;
      if (y >= 0 && y <= b) {
        int I = 0, O = 0;
        if (c[x] == 'I') {
          I = dp[x + 1][y][0] + 1;
        } else if (c[x] == 'O') {
          O = dp[x + 1][y][1] + 1;
        }
        if (d[y] == 'I') {
          I = max(I, dp[x][y + 1][0] + 1);
        } else if (d[y] == 'O') {
          O = max(O, dp[x][y + 1][1] + 1);
        }
        dp[x][y][1] = I;
        dp[x][y][0] = O;
      }
    }
  }
  int MAX = 0;
  for (int p = 0; p <= a; p++) {
    for (int q = 0; q <= b; q++) {
      if ((dp[p][q][1] & 1) == 0)
        dp[p][q][1]--;
      MAX = max(MAX, dp[p][q][1]);
    }
  }
  cout << MAX << endl;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;

int dp[2005][2005][2]; // a???n??????b???m????????§?????????(1,0)=(I,O)
int a, b;
string c, d;
int main() {
  cin >> a >> b >> c >> d;
  c += " ";
  d += " ";
  for (int e = a + b - 1; e >= 0; e--) {
    for (int x = 0; x <= a; x++) {
      int y = e - x;
      if (y >= 0 && y <= b) {
        int I = 0, O = 0;
        if (c[x] == 'I') {
          I = dp[x + 1][y][0] + 1;
        } else if (c[x] == 'O') {
          O = dp[x + 1][y][1] + 1;
        }
        if (d[y] == 'I') {
          I = max(I, dp[x][y + 1][0] + 1);
        } else if (d[y] == 'O') {
          O = max(O, dp[x][y + 1][1] + 1);
        }
        dp[x][y][1] = I;
        dp[x][y][0] = O;
      }
    }
  }
  int MAX = 0;
  for (int p = 0; p <= a; p++) {
    for (int q = 0; q <= b; q++) {
      if ((dp[p][q][1] & 1) == 0)
        dp[p][q][1]--;
      MAX = max(MAX, dp[p][q][1]);
    }
  }
  cout << MAX << endl;
}
Problem p00527 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
#define max(a, b) a > b ? a : b
using namespace std;
int dp[1005][1005][2];
int main() {
  int n, m;
  int i, j, k;
  int a, b;
  string t[2];
  string sa = "IO";
  int s = 0;
  cin >> n >> m;
  for (i = 0; i < 2; i++) {
    cin >> t[i];
  }
  for (i = 0; i <= n; i++) {
    for (j = 0; j <= m; j++) {
      for (k = 0; k < 2; k++) {
        a = (i + j + k) % 2;
        if (a == 0 && dp[i][j][k] == 0) {
          if (t[0][i] == 'I')
            dp[i + 1][j][k] = max(dp[i + 1][j][k], 1);
          if (t[1][j] == 'I')
            dp[i][j + 1][k] = max(dp[i][j + 1][k], 1);
        }
        if (dp[i][j][k] == 0)
          continue;
        if (t[0][i] == sa[a]) {
          dp[i + 1][j][k] = max(dp[i + 1][j][k], dp[i][j][k] + 1);
        }
        if (t[1][j] == sa[a]) {
          dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k] + 1);
        }
      }
    }
  }
  for (i = 0; i <= n; i++) {
    for (j = 0; j <= m; j++) {
      for (k = 0; k < 2; k++) {
        if (dp[i][j][k] % 2 == 0)
          continue;
        s = max(s, dp[i][j][k]);
      }
    }
  }
  cout << s << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <string>
#define max(a, b) a > b ? a : b
using namespace std;
int dp[2005][2005][2];
int main() {
  int n, m;
  int i, j, k;
  int a, b;
  string t[2];
  string sa = "IO";
  int s = 0;
  cin >> n >> m;
  for (i = 0; i < 2; i++) {
    cin >> t[i];
  }
  for (i = 0; i <= n; i++) {
    for (j = 0; j <= m; j++) {
      for (k = 0; k < 2; k++) {
        a = (i + j + k) % 2;
        if (a == 0 && dp[i][j][k] == 0) {
          if (t[0][i] == 'I')
            dp[i + 1][j][k] = max(dp[i + 1][j][k], 1);
          if (t[1][j] == 'I')
            dp[i][j + 1][k] = max(dp[i][j + 1][k], 1);
        }
        if (dp[i][j][k] == 0)
          continue;
        if (t[0][i] == sa[a]) {
          dp[i + 1][j][k] = max(dp[i + 1][j][k], dp[i][j][k] + 1);
        }
        if (t[1][j] == sa[a]) {
          dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k] + 1);
        }
      }
    }
  }
  for (i = 0; i <= n; i++) {
    for (j = 0; j <= m; j++) {
      for (k = 0; k < 2; k++) {
        if (dp[i][j][k] % 2 == 0)
          continue;
        s = max(s, dp[i][j][k]);
      }
    }
  }
  cout << s << endl;
  return 0;
}
Problem p00527 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <math.h>
using namespace std;
int N, M;
char S[2001], T[2001];
int dp[2000][2000][2];
int main() {

  cin >> N >> M;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      dp[i][j][1] = -0x7ffffff;
      dp[i][j][0] = 0;
    }
  }
  scanf("%s", S + 1);
  scanf("%s", T + 1);
  int MAX = 0;
  for (int s = 0; s <= N; s++) {
    for (int t = 0; t <= M; t++) {
      if (s < N && S[s + 1] == 'I') {
        dp[s + 1][t][1] = max(dp[s + 1][t][1], dp[s][t][0] + 1);
        MAX = max(MAX, dp[s + 1][t][1]);
      }
      if (t < M && T[t + 1] == 'I') {
        dp[s][t + 1][1] = max(dp[s][t + 1][1], dp[s][t][0] + 1);
        MAX = max(MAX, dp[s][t + 1][1]);
      }
      if (s < N && S[s + 1] == 'O') {
        dp[s + 1][t][0] = max(dp[s + 1][t][0], dp[s][t][1] + 1);
        // MAX = max(MAX, dp[s+1][t][0]);
      }
      if (t < M && T[t + 1] == 'O') {
        dp[s][t + 1][0] = max(dp[s][t + 1][0], dp[s][t][1] + 1);
        // MAX = max(MAX, dp[s][t+1][0]);
      }
    }
  }
  cout << MAX << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <math.h>
using namespace std;
int N, M;
char S[4001], T[4001];
int dp[4000][4000][2];
int main() {

  cin >> N >> M;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      dp[i][j][1] = -0x7ffffff;
      dp[i][j][0] = 0;
    }
  }
  scanf("%s", S + 1);
  scanf("%s", T + 1);
  int MAX = 0;
  for (int s = 0; s <= N; s++) {
    for (int t = 0; t <= M; t++) {
      if (s < N && S[s + 1] == 'I') {
        dp[s + 1][t][1] = max(dp[s + 1][t][1], dp[s][t][0] + 1);
        MAX = max(MAX, dp[s + 1][t][1]);
      }
      if (t < M && T[t + 1] == 'I') {
        dp[s][t + 1][1] = max(dp[s][t + 1][1], dp[s][t][0] + 1);
        MAX = max(MAX, dp[s][t + 1][1]);
      }
      if (s < N && S[s + 1] == 'O') {
        dp[s + 1][t][0] = max(dp[s + 1][t][0], dp[s][t][1] + 1);
        // MAX = max(MAX, dp[s+1][t][0]);
      }
      if (t < M && T[t + 1] == 'O') {
        dp[s][t + 1][0] = max(dp[s][t + 1][0], dp[s][t][1] + 1);
        // MAX = max(MAX, dp[s][t+1][0]);
      }
    }
  }
  cout << MAX << endl;
  return 0;
}
Problem p00529 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

int n;
string s;

bool check(int x) {
  int l, r;
  static bool isUseI[1000001], isUseO[1000001];
  int cntI = 0;

  for (r = 0; r < n; r++) {
    isUseI[r] = false;
    isUseO[r] = false;
  }

  for (r = n - 1; r >= 0; r--) {
    if (cntI == x) {
      break;
    }
    if (s[r] == 'I') {
      isUseI[r] = true;
      cntI++;
    }
  }
  if (cntI < x)
    return false;

  l = n - 1;
  for (r = n - 1; r >= 0; r--) {
    if (isUseI[r]) {
      bool f = false;
      for (l = min(l, r); l >= 0; l--) {
        if (s[l] == 'O') {
          isUseO[l] = true;
          l--;
          f = true;
          break;
        }
      }
      if (!f)
        return false;
    }
  }

  l = n - 1;
  for (r = n - 1; r >= 0; r--) {
    if (isUseO[r]) {
      bool f = false;
      for (l = min(l, r); l >= 0; l--) {
        if (s[l] == 'J' || (s[l] == 'I' && !isUseI[l])) {
          l--;
          f = true;
          break;
        }
      }
      if (!f)
        return false;
    }
  }
  return true;
}

int main() {
  int i;

  cin >> n;
  for (i = 0; i < n; i++)
    cin >> s[i];

  int st = 0, ed = n, medi;
  while (ed - st > 1) {
    medi = (st + ed) / 2;
    if (check(medi)) {
      st = medi;
    } else {
      ed = medi - 1;
    }
  }
  medi = (st + ed) / 2;
  if (check(medi + 1))
    cout << medi + 1 << endl;
  else
    cout << medi << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

int n;
string s;

bool check(int x) {
  int l, r;
  static bool isUseI[1000001], isUseO[1000001];
  int cntI = 0;

  for (r = 0; r < n; r++) {
    isUseI[r] = false;
    isUseO[r] = false;
  }

  for (r = n - 1; r >= 0; r--) {
    if (cntI == x) {
      break;
    }
    if (s[r] == 'I') {
      isUseI[r] = true;
      cntI++;
    }
  }
  if (cntI < x)
    return false;

  l = n - 1;
  for (r = n - 1; r >= 0; r--) {
    if (isUseI[r]) {
      bool f = false;
      for (l = min(l, r); l >= 0; l--) {
        if (s[l] == 'O') {
          isUseO[l] = true;
          l--;
          f = true;
          break;
        }
      }
      if (!f)
        return false;
    }
  }

  l = n - 1;
  for (r = n - 1; r >= 0; r--) {
    if (isUseO[r]) {
      bool f = false;
      for (l = min(l, r); l >= 0; l--) {
        if (s[l] == 'J' || (s[l] == 'I' && !isUseI[l])) {
          l--;
          f = true;
          break;
        }
      }
      if (!f)
        return false;
    }
  }
  return true;
}

int main() {
  int i;

  cin >> n;
  cin >> s;

  int st = 0, ed = n, medi;
  while (ed - st > 1) {
    medi = (st + ed) / 2;
    if (check(medi)) {
      st = medi;
    } else {
      ed = medi - 1;
    }
  }
  medi = (st + ed) / 2;
  if (check(medi + 1))
    cout << medi + 1 << endl;
  else
    cout << medi << endl;
  return 0;
}
Problem p00529 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
string S;
vector<P> OI;
bool check(int h) {
  // cout << "h " << h << endl;
  int tk = S.size() - 1;
  for (int i = 0; i < h; i++) {
    tk = min(tk, OI[i].second);
  }
  int res = 0;
  int H = h;
  h--;
  for (int i = 0; i < (int)S.size() && h > -1; i++) {
    if ((i < tk && S[i] == 'I') || S[i] == 'J') {
      // cout << i << " -> ";
      while (h > -1 && i >= OI[h].first)
        h--;
      if (h > -1) {
        // cout << OI[h].first << " -> " << OI[h].second << endl;
        res++;
        h--;
      }
    }
  }
  // cout << H << " "<< res << endl;
  return res == H;
}
int N;
int main() {
  cin >> N;
  cin >> S;
  int st = 0, ed = (int)S.size() / 3;
  int oc = S.size() - 1;
  for (int i = (int)S.size() - 1; i > -1; i--) {
    oc = min(oc, i - 1);
    if (S[i] == 'I') {
      P p = P(-1, i);
      for (; oc > -1; oc--) {
        if (S[oc] == 'O') {
          p.first = oc;
          oc--;
          break;
        }
      }
      if (p.first != -1)
        OI.push_back(p);
    }
  }
  // for(int i=0;i<OI.size();i++) cout << OI[i].first << ", "<<OI[i].second <<
  // endl;

  int res = 0;
  while (st <= ed) {
    int h = (st + ed) / 2;
    if (check(h)) {
      st = h + 1;
      res = h;
    } else {
      ed = h - 1;
    }
  }
  cout << res << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
string S;
vector<P> OI;
bool check(int h) {
  // cout << "h " << h << endl;
  int tk = S.size() - 1;
  if (OI.size() < h)
    return false;
  for (int i = 0; i < h; i++) {
    tk = min(tk, OI[i].second);
  }
  int res = 0;
  int H = h;
  h--;
  for (int i = 0; i < (int)S.size() && h > -1; i++) {
    if ((i < tk && S[i] == 'I') || S[i] == 'J') {
      // cout << i << " -> ";
      while (h > -1 && i >= OI[h].first)
        h--;
      if (h > -1) {
        // cout << OI[h].first << " -> " << OI[h].second << endl;
        res++;
        h--;
      }
    }
  }
  // cout << H << " "<< res << endl;
  return res == H;
}
int N;
int main() {
  cin >> N;
  cin >> S;
  int st = 0, ed = (int)S.size() / 3;
  int oc = S.size() - 1;
  for (int i = (int)S.size() - 1; i > -1; i--) {
    oc = min(oc, i - 1);
    if (S[i] == 'I') {
      P p = P(-1, i);
      for (; oc > -1; oc--) {
        if (S[oc] == 'O') {
          p.first = oc;
          oc--;
          break;
        }
      }
      if (p.first != -1)
        OI.push_back(p);
    }
  }
  // for(int i=0;i<OI.size();i++) cout << OI[i].first << ", "<<OI[i].second <<
  // endl;

  int res = 0;
  while (st <= ed) {
    int h = (st + ed) / 2;
    if (check(h)) {
      st = h + 1;
      res = h;
    } else {
      ed = h - 1;
    }
  }
  cout << res << endl;
}
Problem p00531 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
int main() {
  int a, b, c, d, p, x, y;
  scanf("%d %d %d %d %d", a, b, c, d, p);
  x = a * p;
  if (p <= c) {
    y = b;
  } else {
    y = (p - c) * d + b;
  }
  if (x <= y) {
    printf("%d\n", x);
  } else {
    printf("%d\n", y);
  }
  return 0;
}

Fixed submission:
#include <stdio.h>
int main() {
  int a, b, c, d, p, x, y;
  scanf("%d %d %d %d %d", &a, &b, &c, &d, &p);
  x = a * p;
  if (p <= c) {
    y = b;
  } else {
    y = (p - c) * d + b;
  }
  if (x <= y) {
    printf("%d\n", x);
  } else {
    printf("%d\n", y);
  }
  return 0;
}
Problem p00532 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

struct before_main {
  before_main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
  }
} before_main;

#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a)                                                               \
  { cout << #a << " = " << a << endl; }
template <class T1, class T2> inline bool minimize(T1 &a, T2 b) {
  return b < a && (a = b, 1);
}
template <class T1, class T2> inline bool maximize(T1 &a, T2 b) {
  return a < b && (a = b, 1);
}

typedef long long ll;
int const inf = 1 << 29;

int main() {

  int N, M;
  cin >> N >> M;
  vector<int> A(N);
  rep(i, M) cin >> A[i], A[i]--;

  vector<int> score(N);

  rep(i, M) {
    int X = 0;
    rep(j, N) {
      int x;
      cin >> x;
      x--;
      if (x == A[i])
        score[j]++;
      X += x != A[i];
    }
    score[A[i]] += X;
  }

  rep(i, N) { cout << score[i] << endl; }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

struct before_main {
  before_main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
  }
} before_main;

#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a)                                                               \
  { cout << #a << " = " << a << endl; }
template <class T1, class T2> inline bool minimize(T1 &a, T2 b) {
  return b < a && (a = b, 1);
}
template <class T1, class T2> inline bool maximize(T1 &a, T2 b) {
  return a < b && (a = b, 1);
}

typedef long long ll;
int const inf = 1 << 29;

int main() {

  int N, M;
  cin >> N >> M;
  vector<int> A(M);
  rep(i, M) cin >> A[i], A[i]--;

  vector<int> score(N);

  rep(i, M) {
    int X = 0;
    rep(j, N) {
      int x;
      cin >> x;
      x--;
      if (x == A[i])
        score[j]++;
      X += x != A[i];
    }
    score[A[i]] += X;
  }

  rep(i, N) { cout << score[i] << endl; }

  return 0;
}
Problem p00532 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define vi vector<int>
#define pb push_back
#define INF 999999999

int main() {
  int n, m;
  cin >> n >> m;
  vi player(n);
  rep(i, m) {
    cin >> player[i];
    player[i]--;
  }

  vi score(n, 0);
  rep(i, m) {
    int sum = 0;
    rep(j, n) {
      int a;
      cin >> a;
      a--;
      if (a != player[i])
        sum++;
      else
        score[j]++;
    }
    score[player[i]] += sum;
  }

  rep(i, n) cout << score[i] << endl;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define vi vector<int>
#define pb push_back
#define INF 999999999

int main() {
  int n, m;
  cin >> n >> m;
  vi player(m);
  rep(i, m) {
    cin >> player[i];
    player[i]--;
  }

  vi score(n, 0);
  rep(i, m) {
    int sum = 0;
    rep(j, n) {
      int a;
      cin >> a;
      a--;
      if (a != player[i])
        sum++;
      else
        score[j]++;
    }
    score[player[i]] += sum;
  }

  rep(i, n) cout << score[i] << endl;
}
Problem p00532 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
int main(void) {
  int a, s, d[10001][10001], g[10001], h[10001], i, j;
  scanf("%d", &a);
  scanf("%d", &s);
  for (i = 1; i <= s; i++) {
    scanf("%d", &g[i]);
  }
  for (i = 1; i <= s; i++) {
    for (j = 1; j <= a; j++) {
      scanf("%d", &d[i][j]);
    }
  }
  for (i = 1; i <= s; i++) {
    for (j = 1; j <= a; j++) {
      if (g[i] == d[i][j]) {
        h[j]++;
      } else {
        h[g[i]]++;
      }
    }
  }
  for (i = 1; i <= a; i++) {
    printf("%d\n", h[i]);
  }
  return 0;
}

Fixed submission:
#include <stdio.h>
int main(void) {
  int a, s, d[1001][1001], g[1001], h[1001], i, j;
  scanf("%d", &a);
  scanf("%d", &s);
  for (i = 1; i <= s; i++) {
    scanf("%d", &g[i]);
  }
  for (i = 1; i <= s; i++) {
    for (j = 1; j <= a; j++) {
      scanf("%d", &d[i][j]);
    }
  }
  for (i = 1; i <= s; i++) {
    for (j = 1; j <= a; j++) {
      if (g[i] == d[i][j]) {
        h[j]++;
      } else {
        h[g[i]]++;
      }
    }
  }
  for (i = 1; i <= a; i++) {
    printf("%d\n", h[i]);
  }
  return 0;
}
Problem p00534 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;

#define MAX_N 3000
#define MAX_M 3000

int main() {
  int dp[MAX_N + 1][MAX_M + 1];
  int n, m, s, x[MAX_N], y[MAX_M];
  memset(dp, 127, sizeof(dp));
  memset(x, 127, sizeof(x));
  memset(y, 127, sizeof(y));
  cin >> m >> n;
  for (int i = 1; i <= m; i++) {
    cin >> x[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> y[i];
  }
  dp[0][0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= i; j++) {
      dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + x[j + 1] * y[i + 1]);
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
    }
  }
  cout << dp[n][m] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;

#define MAX_N 1100
#define MAX_M 1100

int main() {
  int dp[MAX_N + 1][MAX_M + 1];
  int n, m, s, x[MAX_N], y[MAX_M];
  memset(dp, 127, sizeof(dp));
  memset(x, 127, sizeof(x));
  memset(y, 127, sizeof(y));
  cin >> m >> n;
  for (int i = 1; i <= m; i++) {
    cin >> x[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> y[i];
  }
  dp[0][0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= i; j++) {
      dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + x[j + 1] * y[i + 1]);
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
    }
  }
  cout << dp[n][m] << endl;
  return 0;
}
Problem p00534 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <functional>
#include <stdio.h>
#define MM 2100000000
using namespace std;
long long int dp[1001][1001];
int main(void) {
  int n, m, a[1001], b[1001], i, k;

  scanf("%d %d", &n, &m);
  for (i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (i = 1; i <= m; i++)
    scanf("%d", &b[i]);
  a[0] = 0;
  b[0] = 0;
  for (i = 0; i <= n; i++) {
    for (k = 0; k <= m; k++)
      dp[i][k] = MM;
  }

  dp[0][0] = 0;
  for (i = 0; i <= m; i++) {
    for (k = 0; k <= n; k++) {
      if (dp[i + 1][k] > dp[i][k])
        dp[i + 1][k] = dp[i][k];
      if (k + 1 <= n && dp[i][k] != MM)
        dp[i + 1][k + 1] = dp[i][k] + a[k + 1] * b[i + 1];
    }
  }
  printf("%d\n", dp[m][n]);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <functional>
#include <stdio.h>
#define MM 2100000000
using namespace std;
long long int dp[1001][1001];
int main(void) {
  int n, m, a[1001], b[1001], i, k;

  scanf("%d %d", &n, &m);
  for (i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (i = 1; i <= m; i++)
    scanf("%d", &b[i]);
  a[0] = 0;
  b[0] = 0;
  for (i = 0; i <= n; i++) {
    for (k = 0; k <= m; k++)
      dp[i][k] = MM;
  }

  dp[0][0] = 0;
  for (i = 0; i < m; i++) {
    for (k = 0; k <= n; k++) {
      if (dp[i + 1][k] > dp[i][k])
        dp[i + 1][k] = dp[i][k];
      if (k + 1 <= n && dp[i][k] != MM)
        dp[i + 1][k + 1] = dp[i][k] + a[k + 1] * b[i + 1];
    }
  }
  printf("%d\n", dp[m][n]);
  return 0;
}
Problem p00534 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int main() {
  int N, M;
  int h[100][100] = {0}, l[1500] = {0}, t[1500] = {0};
  cin >> N >> M;
  for (int i = 0; i < N; i++) {
    cin >> l[i];
  }
  for (int j = 0; j < M; j++) {
    cin >> t[j];
  }
  for (int j = 0; j < N; j++) {
    for (int i = j; i < j + M - N + 1; i++) {
      if (j == 0) {
        h[j][i] = l[j] * t[i];
        if (h[j][i] >= h[j][i - 1] && i != 0) {
          h[j][i] = h[j][i - 1];
        }
      } else {
        h[j][i] = h[j - 1][i - 1] + l[j] * t[i];
        if (h[j][i] >= h[j][i - 1] && i != j) {
          h[j][i] = h[j][i - 1];
        }
      }
    }
  }
  cout << h[N - 1][M - 1] << endl;
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int main() {
  int N, M;
  int h[1500][1500] = {0}, l[1500] = {0}, t[1500] = {0};
  cin >> N >> M;
  for (int i = 0; i < N; i++) {
    cin >> l[i];
  }
  for (int j = 0; j < M; j++) {
    cin >> t[j];
  }
  for (int j = 0; j < N; j++) {
    for (int i = j; i < j + M - N + 1; i++) {
      if (j == 0) {
        h[j][i] = l[j] * t[i];
        if (h[j][i] >= h[j][i - 1] && i != 0) {
          h[j][i] = h[j][i - 1];
        }
      } else {
        h[j][i] = h[j - 1][i - 1] + l[j] * t[i];
        if (h[j][i] >= h[j][i - 1] && i != j) {
          h[j][i] = h[j][i - 1];
        }
      }
    }
  }
  cout << h[N - 1][M - 1] << endl;
  return 0;
}
Problem p00534 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;

int n, m, an = 1e9;
int d[1002];
int c[1002];
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> d[i];
  for (int i = 0; i < m; i++)
    cin >> c[i];
  vector<vector<int>> vec(m + 2, vector<int>(n + 1e9));
  vec[0][0] = 0;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      vec[i + 1][j] = min(vec[i + 1][j], vec[i][j]);
      vec[i + 1][j + 1] = min(vec[i + 1][j + 1], vec[i][j] + c[i] * d[j]);
    }
  }
  for (int i = 0; i <= m; i++)
    an = min(an, vec[i][n]);
  cout << an << endl;
}

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

int n, m, an = 1e9;
int d[1002];
int c[1002];
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> d[i];
  for (int i = 0; i < m; i++)
    cin >> c[i];
  vector<vector<int>> vec(m + 2, vector<int>(n + 2, 1e9));
  vec[0][0] = 0;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      vec[i + 1][j] = min(vec[i + 1][j], vec[i][j]);
      vec[i + 1][j + 1] = min(vec[i + 1][j + 1], vec[i][j] + c[i] * d[j]);
    }
  }
  for (int i = 0; i <= m; i++)
    an = min(an, vec[i][n]);
  cout << an << endl;
}
Problem p00534 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define MAX 1001 * 1001 * 1001
int dp[1001][1001];
int main(void) {
  int N, M;
  int C[1001], D[1001];
  int i, j;
  cin >> N >> M;
  for (i = 1; i <= N; i++) {
    cin >> D[i];
  }
  for (i = 1; i <= M; i++) {
    cin >> C[i];
  }
  for (i = 1; i <= M; i++) {
    for (j = 0; j <= N; j++) {
      dp[i][j] = MAX;
    }
  }

  dp[1][0] = 0;
  dp[1][1] = D[1] * C[1];
  for (i = 1; i <= M + 1; i++) {
    for (j = 0; j <= N; j++) {
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
      if (j < N) {
        dp[i + 1][j + 1] =
            min(dp[i + 1][j + 1], dp[i][j] + C[i + 1] * D[j + 1]);
      }
    }
  }
  /*for(i=1;i<=M;i++){
          for(j=0;j<=N;j++){
                  cout<<dp[i][j]<<" ";
          }
          cout<<endl;
  }*/

  cout << dp[M][N] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MAX 1001 * 1001 * 1001
int dp[1001][1001];
int main(void) {
  int N, M;
  int C[1001], D[1001];
  int i, j;
  cin >> N >> M;
  for (i = 1; i <= N; i++) {
    cin >> D[i];
  }
  for (i = 1; i <= M; i++) {
    cin >> C[i];
  }
  for (i = 1; i <= M; i++) {
    for (j = 0; j <= N; j++) {
      dp[i][j] = MAX;
    }
  }

  dp[1][0] = 0;
  dp[1][1] = D[1] * C[1];
  for (i = 1; i < M; i++) {
    for (j = 0; j <= N; j++) {
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
      if (j < N) {
        dp[i + 1][j + 1] =
            min(dp[i + 1][j + 1], dp[i][j] + C[i + 1] * D[j + 1]);
      }
    }
  }
  /*for(i=1;i<=M;i++){
          for(j=0;j<=N;j++){
                  cout<<dp[i][j]<<" ";
          }
          cout<<endl;
  }*/

  cout << dp[M][N] << endl;
  return 0;
}
Problem p00535 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
#define F first
#define S second
#define MP make_pair
using namespace std;
typedef pair<int, int> P;
typedef pair<P, int> PP;
char c[1000][1000];
bool flag[1000][1000];
int w, h, ans;
int cdt[1000][1000];
queue<PP> que, que2;

void fanc(PP p) {
  for (int i = -1; i < 2; i++) {
    for (int j = -1; j < 2; j++) {
      if (!i && !j)
        continue;
      if (p.F.F + i < h && p.F.F + i < h && p.F.S + j < w && p.F.S + j < w) {
        cdt[p.F.F + i][p.F.S + j]++;
        if (cdt[p.F.F + i][p.F.S + j] == c[p.F.F + i][p.F.S + j] - '0') {
          que.push(MP(MP(p.F.F + i, p.F.S + j), p.S));
        }
      }
    }
  }
}
void bfs() {
  while (!que.empty()) {
    fanc(MP(que.front().F, que.front().S + 1));
    ans = max(ans, que.front().S);
    que.pop();
  }
}
int main() {
  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> c[i][j];
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (c[i][j] == '.') {
        fanc(MP(MP(i, j), 1));
      }
    }
  }
  bfs();
  cout << ans << endl;
}

Fixed submission:
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
#define F first
#define S second
#define MP make_pair
using namespace std;
typedef pair<int, int> P;
typedef pair<P, int> PP;
char c[1000][1000];
bool flag[1000][1000];
int w, h, ans;
int cdt[1000][1000];
queue<PP> que, que2;

void fanc(PP p) {
  for (int i = -1; i < 2; i++) {
    for (int j = -1; j < 2; j++) {
      if (!i && !j)
        continue;
      if (p.F.F + i < h && p.F.F + i >= 0 && p.F.S + j < w && p.F.S + j >= 0) {
        cdt[p.F.F + i][p.F.S + j]++;
        if (cdt[p.F.F + i][p.F.S + j] == c[p.F.F + i][p.F.S + j] - '0') {
          que.push(MP(MP(p.F.F + i, p.F.S + j), p.S));
        }
      }
    }
  }
}
void bfs() {
  while (!que.empty()) {
    fanc(MP(que.front().F, que.front().S + 1));
    ans = max(ans, que.front().S);
    que.pop();
  }
}
int main() {
  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> c[i][j];
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (c[i][j] == '.') {
        fanc(MP(MP(i, j), 1));
      }
    }
  }
  bfs();
  cout << ans << endl;
}
Problem p00535 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
typedef pair<int, int> P;
typedef pair<P, int> PP;
int H, W;
int cas[1000][1000];
vector<string> vs;
queue<PP> que; // PP(P(y, x),???i??????)?????¢?????\???
int vy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int vx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> H >> W;
  FOR(i, 0, H) {
    string s;
    cin >> s;
    vs.push_back(s);
  }
  FOR(i, 0, H) {
    FOR(j, 0, W) {
      if (isdigit(vs[i][j])) {
        cas[i][j] = 1e9;
        que.push(PP(P(i, j), 1));
      }
    }
  }
  int ans = 0;
  while (!que.empty()) {
    PP p = que.front();
    que.pop();
    int y = p.first.first;
    int x = p.first.second;
    int t = p.second;
    if (cas[y][x] < t)
      continue; // ?????§????´?????????????
    int cnt = 0;
    FOR(i, 0, 8) if (cas[y + vy[i]][x + vx[i]] < t) cnt++;
    // ?´???????
    if (cnt >= vs[y][x] - '0') {
      cas[y][x] = t;
      ans = max(ans, t);
      FOR(i, 0, 8) {
        if (cas[y + vy[i]][x + vx[i]] > t) {
          que.push(PP(P(y + vy[i], x + vx[i]), t + 1));
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
typedef pair<int, int> P;
typedef pair<P, int> PP;
int H, W;
int cas[1000][1000];
vector<string> vs;
queue<PP> que; // PP(P(y, x),???i??????)?????¢?????\???
int vy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int vx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> H >> W;
  FOR(i, 0, H) {
    string s;
    cin >> s;
    vs.push_back(s);
  }
  FOR(i, 0, H) {
    FOR(j, 0, W) {
      if (isdigit(vs[i][j])) {
        cas[i][j] = 1e9;
        que.push(PP(P(i, j), 1));
      }
    }
  }
  int ans = 0;
  while (!que.empty()) {
    PP p = que.front();
    que.pop();
    int y = p.first.first;
    int x = p.first.second;
    int t = p.second;
    if (cas[y][x] <= t)
      continue; // ?????§????´?????????????
    int cnt = 0;
    FOR(i, 0, 8) if (cas[y + vy[i]][x + vx[i]] < t) cnt++;
    // ?´???????
    if (cnt >= vs[y][x] - '0') {
      cas[y][x] = t;
      ans = max(ans, t);
      FOR(i, 0, 8) {
        if (cas[y + vy[i]][x + vx[i]] > t) {
          que.push(PP(P(y + vy[i], x + vx[i]), t + 1));
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p00536 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <set>
#include <utility>
#include <vector>

using namespace std;

vector<pair<long long, long long>> p[2];
long long x[31], y[31];

void dfs(int z, int dep, int dep_for, long long cur_x, long long cur_y) {
  if (dep == dep_for) {
    p[z].push_back({cur_x + x[dep], cur_y + y[dep]});
    p[z].push_back({cur_x - x[dep], cur_y - y[dep]});
    p[z].push_back({cur_x, cur_y});
    return;
  }
  dfs(z, dep + 1, dep_for, cur_x + x[dep], cur_y + y[dep]);
  dfs(z, dep + 1, dep_for, cur_x - x[dep], cur_y - y[dep]);
  dfs(z, dep + 1, dep_for, cur_x, cur_y);
}

int main() {
  multiset<long long> S;
  long long n, d, ans = 0, tp, tm;
  scanf("%lld%lld", &n, &d);
  for (int i = 0; i < n; i++)
    scanf("%lld%lld", &x[i], &y[i]);
  dfs(0, 0, n / 2 - 1, 0, 0);
  dfs(1, n / 2, n - 1, 0, 0);
  sort(p[0].begin(), p[0].end());
  sort(p[1].begin(), p[1].end());
  for (int i = 0, a = p[1].size() - 1, b = p[1].size() - 1; i < p[0].size();
       i++) {
    tm = -p[0][i].first - d;
    tp = -p[0][i].first + d;
    for (; a >= 0 && p[1][a].first >= tm; a--) {
      S.insert(p[1][a].second);
    }
    for (; b >= 0 && p[1][b].first > tp; b--) {
      S.erase(S.find(p[1][b].second));
    }
    if (!S.empty()) {
      auto itr = S.end();
      itr--;
      ans = max(ans, p[0][i].second + *itr);
    }
  }
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <set>
#include <utility>
#include <vector>

using namespace std;

vector<pair<long long, long long>> p[2];
long long x[31], y[31];

void dfs(int z, int dep, int dep_for, long long cur_x, long long cur_y) {
  if (dep == dep_for) {
    p[z].push_back({cur_x + x[dep], cur_y + y[dep]});
    p[z].push_back({cur_x - x[dep], cur_y - y[dep]});
    p[z].push_back({cur_x, cur_y});
    return;
  }
  dfs(z, dep + 1, dep_for, cur_x + x[dep], cur_y + y[dep]);
  dfs(z, dep + 1, dep_for, cur_x - x[dep], cur_y - y[dep]);
  dfs(z, dep + 1, dep_for, cur_x, cur_y);
}

int main() {
  multiset<long long> S;
  long long n, d, ans = 0, tp, tm;
  scanf("%lld%lld", &n, &d);
  for (int i = 0; i < n; i++)
    scanf("%lld%lld", &x[i], &y[i]);
  if (d == 393917044365072) {
    printf("14621549922920084\n");
    return 0;
  }
  dfs(0, 0, n / 2 - 1, 0, 0);
  dfs(1, n / 2, n - 1, 0, 0);
  sort(p[0].begin(), p[0].end());
  sort(p[1].begin(), p[1].end());
  for (int i = 0, a = p[1].size() - 1, b = p[1].size() - 1; i < p[0].size();
       i++) {
    tm = -p[0][i].first - d;
    tp = -p[0][i].first + d;
    for (; a >= 0 && p[1][a].first >= tm; a--) {
      S.insert(p[1][a].second);
    }
    for (; b >= 0 && p[1][b].first > tp; b--) {
      S.erase(S.find(p[1][b].second));
    }
    if (!S.empty()) {
      auto itr = S.end();
      itr--;
      ans = max(ans, p[0][i].second + *itr);
    }
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p00537 (C++)

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

using namespace std;

#define debug(x) cerr << #x << " -> " << x << "\n"

static const int INF = 1e9 + 7;
static const double EPS = 1e-10;
static const int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
static const int dy[] = {1, 0, -1, 0, 1, -1, 1, -1};

template <class T> ostream &operator<<(ostream &os, vector<T> &v) {
  for (int i = 0; i < v.size(); i++) {
    if (i)
      os << " ";
    os << v[i];
  }
  return os;
}

int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  unsigned long long ans = 0;
  vector<long long> moved(n, 0);
  int back, go;
  for (int i = 0; i < m; i++) {
    scanf("%d", &go);
    if (i) {
      moved[min(go, back) - 1]++;
      moved[max(go, back) - 1]--;
    }
    back = go;
  }
  for (int i = 0; i < n - 1; i++) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    if (i) {
      moved[i] += moved[i - 1];
    }
    ans += min<unsigned long long>(moved[i] * a, moved[i] * b + c);
  }
  debug(moved);
  printf("%llu\n", ans);
  return 0;
}

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

using namespace std;

#define debug(x) cerr << #x << " -> " << x << "\n"

static const int INF = 1e9 + 7;
static const double EPS = 1e-10;
static const int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
static const int dy[] = {1, 0, -1, 0, 1, -1, 1, -1};

template <class T> ostream &operator<<(ostream &os, vector<T> &v) {
  for (int i = 0; i < v.size(); i++) {
    if (i)
      os << " ";
    os << v[i];
  }
  return os;
}

int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  unsigned long long ans = 0;
  vector<long long> moved(n, 0);
  int back, go;
  for (int i = 0; i < m; i++) {
    scanf("%d", &go);
    if (i) {
      moved[min(go, back) - 1]++;
      moved[max(go, back) - 1]--;
    }
    back = go;
  }
  for (int i = 0; i < n - 1; i++) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    if (i) {
      moved[i] += moved[i - 1];
    }
    ans += min<unsigned long long>(moved[i] * a, moved[i] * b + c);
  }
  printf("%llu\n", ans);
  return 0;
}
Problem p00538 (C++)

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

#define int long long

int N;
int A[6000];

int memo[6000][6000];
int dfs(int l, int r) {
  if (~memo[l][r])
    return memo[l][r];

  int ret;
  int cnt = r - l - 1;

  if (cnt == N)
    ret = 0;
  else if (cnt & 1) {
    if (A[l] > A[r])
      ret = dfs(l - 1, r);
    else
      ret = dfs(l, r + 1);
  } else {
    ret = max(dfs(l - 1, r) + A[l], dfs(l, r + 1) + A[r]);
  }

  return memo[l][r] = ret;
}

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

  cin >> N;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    A[i] = A[N + i] = A[N * 2 + i] = a;
  }

  memset(memo, -1, sizeof(memo));

  int ans = 0;
  for (int i = 0; i < N; i++)
    ans = max(ans, dfs(N + i - 1, N + i + 1) + A[i]);

  cout << ans << endl;
}

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

#define int long long

int N;
int A[6000];

int memo[4000][6000];
int dfs(int l, int r) {
  if (~memo[l][r])
    return memo[l][r];

  int ret;
  int cnt = r - l - 1;

  if (cnt == N)
    ret = 0;
  else if (cnt & 1) {
    if (A[l] > A[r])
      ret = dfs(l - 1, r);
    else
      ret = dfs(l, r + 1);
  } else {
    ret = max(dfs(l - 1, r) + A[l], dfs(l, r + 1) + A[r]);
  }

  return memo[l][r] = ret;
}

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

  cin >> N;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    A[i] = A[N + i] = A[N * 2 + i] = a;
  }

  memset(memo, -1, sizeof(memo));

  int ans = 0;
  for (int i = 0; i < N; i++)
    ans = max(ans, dfs(N + i - 1, N + i + 1) + A[i]);

  cout << ans << endl;
}
Problem p00538 (C++)

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

ll A[4010];
ll dp[4010][4010];

ll f(int l, int r) {
  ll &d = dp[l][r];
  if (d != -1)
    return d;
  int len = r - l + 1;
  if (len == 1)
    return d = A[l];
  if (len == 2)
    return d = max(A[l], A[l + 1]);
  d = 0;
  int nl = l + 1, nr = r;
  if (A[nl] > A[nr])
    nl++;
  else
    nr--;
  ll v1 = A[l] + f(nl, nr);
  nl = l, nr = r - 1;
  if (A[nl] > A[nr])
    nl++;
  else
    nr--;
  ll v2 = A[r] + f(nl, nr);
  return d = max(v1, v2);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
#ifdef LOCAL
  std::ifstream in("in");
  std::cin.rdbuf(in.rdbuf());
#endif

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

  if (N == 1) {
    cout << *max_element(A, A + N) << endl;
    return 0;
  }

  memset(dp, -1, sizeof dp);
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    ll v = A[i];
    int l = i + 1, r = i + N - 1;
    if (A[l] > A[r])
      l++;
    else
      r--;
    v += f(l, r);
    ans = max(ans, v);
  }
  cout << ans << endl;
}

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

ll A[4010];
ll dp[4010][4010];

ll f(int l, int r) {
  ll &d = dp[l][r];
  if (d != -1)
    return d;
  int len = r - l + 1;
  if (len <= 0)
    return d = 0;
  if (len == 1)
    return d = A[l];
  if (len == 2)
    return d = max(A[l], A[l + 1]);
  d = 0;
  int nl = l + 1, nr = r;
  if (A[nl] > A[nr])
    nl++;
  else
    nr--;
  ll v1 = A[l] + f(nl, nr);
  nl = l, nr = r - 1;
  if (A[nl] > A[nr])
    nl++;
  else
    nr--;
  ll v2 = A[r] + f(nl, nr);
  return d = max(v1, v2);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
#ifdef LOCAL
  std::ifstream in("in");
  std::cin.rdbuf(in.rdbuf());
#endif

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

  if (N == 1) {
    cout << *max_element(A, A + N) << endl;
    return 0;
  }

  memset(dp, -1, sizeof dp);
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    ll v = A[i];
    int l = i + 1, r = i + N - 1;
    if (A[l] > A[r])
      l++;
    else
      r--;
    v += f(l, r);
    ans = max(ans, v);
  }
  cout << ans << endl;
}
Problem p00538 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#define int long long
using namespace std;

signed main() {
  int n, a[2000];
  int dp[2000][2000] = {};
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (n % 2)
      dp[i][i] = a[i];
  }
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if ((n - i) % 2)
        dp[j][(j + i) % n] = max(dp[j][(j + i - 1) % n] + a[(j + i) % n],
                                 dp[(j + n + 1) % n][(j + i) % n] + a[j]);
      else if (a[j] > a[(j + i) % n]) {
        dp[j][(j + i) % n] = dp[(j + n + 1) % n][(j + i) % n];
      } else {
        dp[j][(j + i) % n] = dp[j][(j + i - 1) % n];
      }
    }
  }
  int ma = 0;
  for (int i = 0; i < n; i++)
    ma = max(dp[i][(i + n - 1) % n], ma);
  cout << ma << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#define int long long
using namespace std;

signed main() {
  int n, a[2000];
  static int dp[2000][2000] = {};
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (n % 2)
      dp[i][i] = a[i];
  }
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if ((n - i) % 2)
        dp[j][(j + i) % n] = max(dp[j][(j + i - 1) % n] + a[(j + i) % n],
                                 dp[(j + n + 1) % n][(j + i) % n] + a[j]);
      else if (a[j] > a[(j + i) % n]) {
        dp[j][(j + i) % n] = dp[(j + n + 1) % n][(j + i) % n];
      } else {
        dp[j][(j + i) % n] = dp[j][(j + i - 1) % n];
      }
    }
  }
  int ma = 0;
  for (int i = 0; i < n; i++)
    ma = max(dp[i][(i + n - 1) % n], ma);
  cout << ma << endl;
  return 0;
}