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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;
int a[100], b[100], w, h;
int solve(int i) {
  int x = 0, y = 0, ans = 0, j;
  x = max(a[i] - a[i + 1], a[i + 1] - a[i]);
  y = max(b[i] - b[i + 1], b[i + 1] - b[i]);
  ans = ans + x + y;
  for (j = 0; j <= min(w, h); j++) {
    if (a[i] - a[i + 1] > j && b[i] - b[i + 1] > j)
      ans--;
    else if (a[i + 1] - a[i] > j && b[i + 1] - b[i] > j)
      ans--;
  }
  return ans;
}
int main() {
  int n, i, answer = 0;
  cin >> w >> h >> n;
  for (i = 1; i <= n; i++) {
    cin >> a[i] >> b[i];
  }
  for (i = 1; i < n; i++) {
    answer = solve(i) + answer;
  }
  cout << answer << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
int a[10000], b[10000], w, h;
int solve(int i) {
  int x = 0, y = 0, ans = 0, j;
  x = max(a[i] - a[i + 1], a[i + 1] - a[i]);
  y = max(b[i] - b[i + 1], b[i + 1] - b[i]);
  ans = ans + x + y;
  for (j = 0; j <= min(w, h); j++) {
    if (a[i] - a[i + 1] > j && b[i] - b[i + 1] > j)
      ans--;
    else if (a[i + 1] - a[i] > j && b[i + 1] - b[i] > j)
      ans--;
  }
  return ans;
}
int main() {
  int n, i, answer = 0;
  cin >> w >> h >> n;
  for (i = 1; i <= n; i++) {
    cin >> a[i] >> b[i];
  }
  for (i = 1; i < n; i++) {
    answer = solve(i) + answer;
  }
  cout << answer << endl;
  return 0;
}
Problem p00518 (C++)

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

using namespace std;
const int MOD = 10007;

int main() {
  int N;
  cin >> N;
  string s;
  cin >> s;
  int dp[1000][8] = {};
  dp[0][1] = 1;
  map<char, int> dic;
  for (int i = 0; i < 3; i++)
    dic["JOI"[i]] = i;

  for (int i = 0; i < N; i++) {
    for (int s1 = 0; s1 < 8; s1++) {
      for (int s2 = 0; s2 < 8; s2++) {
        if ((s2 >> dic[s[i]] & 1) && (s2 & s1) != 0) {
          dp[i + 1][s2] += dp[i][s1];
          dp[i + 1][s2] %= MOD;
        }
      }
    }
  }
  int ans = 0;
  for (int s = 0; s < 8; s++) {
    ans += dp[N][s];
    ans %= MOD;
  }
  cout << ans << endl;
  return 0;
}

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

using namespace std;
const int MOD = 10007;

int main() {
  int N;
  cin >> N;
  string s;
  cin >> s;
  int dp[1010][8] = {};
  dp[0][1] = 1;
  map<char, int> dic;
  for (int i = 0; i < 3; i++)
    dic["JOI"[i]] = i;

  for (int i = 0; i < N; i++) {
    for (int s1 = 0; s1 < 8; s1++) {
      for (int s2 = 0; s2 < 8; s2++) {
        if ((s2 >> dic[s[i]] & 1) && (s2 & s1) != 0) {
          dp[i + 1][s2] += dp[i][s1];
          dp[i + 1][s2] %= MOD;
        }
      }
    }
  }
  int ans = 0;
  for (int s = 0; s < 8; s++) {
    ans += dp[N][s];
    ans %= MOD;
  }
  cout << ans << endl;
  return 0;
}
Problem p00518 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;
int n;
string ta;
int memo[200][200];
int solve(int day, int f) {
  if (memo[day][f] != 0)
    return memo[day][f];
  if (day == n) {
    return memo[day][f] = 1;
  }

  int tmp, sum = 0;
  ;
  for (int i = 0; i < 8; i++) {
    if (ta[day] == 'J')
      tmp = 4;
    else if (ta[day] == 'O')
      tmp = 2;
    else if (ta[day] == 'I')
      tmp = 1;
    if ((i & tmp) != 0 && (i & f) != 0)
      sum += solve(day + 1, i);
  }

  return memo[day][f] = sum % 10007;
}

int main() {
  cin >> n;
  cin >> ta;
  cout << solve(0, 4) << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
int n;
string ta;
int memo[20000][10];
int solve(int day, int f) {
  if (memo[day][f] != 0)
    return memo[day][f];
  if (day == n) {
    return memo[day][f] = 1;
  }

  int tmp, sum = 0;
  ;
  for (int i = 0; i < 8; i++) {
    if (ta[day] == 'J')
      tmp = 4;
    else if (ta[day] == 'O')
      tmp = 2;
    else if (ta[day] == 'I')
      tmp = 1;
    if ((i & tmp) != 0 && (i & f) != 0)
      sum += solve(day + 1, i);
  }

  return memo[day][f] = sum % 10007;
}

int main() {
  cin >> n;
  cin >> ta;
  cout << solve(0, 4) << endl;
  return 0;
}
Problem p00518 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <map>
#include <string>
#define MOD 10007
using namespace std;

int n;
long long dp[1111][8];
string s;
map<char, int> shift;

long long rec(int idx, int prev) {

  if (idx == n)
    return 1;
  if (dp[idx][prev])
    return dp[idx][prev];

  long long ret = 0;
  int sft = shift[s[idx]];
  for (int i = 1; i < 8; i++) {
    if ((i >> sft & 1) && (i & prev)) {
      (ret += rec(idx + 1, i)) % MOD;
    }
  }
  return dp[idx][prev] = ret;
}

int main() {

  shift['I'] = 0, shift['O'] = 1, shift['J'] = 2;
  cin >> n >> s;
  cout << rec(0, 4) % MOD << endl;
}

Fixed submission:
#include <iostream>
#include <map>
#include <string>
#define MOD 10007
using namespace std;

int n;
long long dp[1111][8];
string s;
map<char, int> shift;

long long rec(int idx, int prev) {

  if (idx == n)
    return 1;
  if (dp[idx][prev])
    return dp[idx][prev];

  long long ret = 0;
  int sft = shift[s[idx]];
  for (int i = 1; i < 8; i++) {
    if ((i >> sft & 1) && (i & prev)) {
      ret += rec(idx + 1, i) % MOD;
    }
  }
  return dp[idx][prev] = ret;
}

int main() {

  shift['I'] = 0, shift['O'] = 1, shift['J'] = 2;
  cin >> n >> s;
  cout << rec(0, 4) % MOD << endl;
}
Problem p00518 (C++)

Buggy submission (status: Runtime Error):
// メモリ数確認したか?
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long int ll;

int main() {
  int n;
  string ss;
  cin >> n >> ss;
  const int MAX_N = 1000;
  const int mod = 10007;
  int dp[MAX_N + 1][7];
  for (int j = 0; j < 7; j++) {
    dp[0][j] = 0;
    if (j == 0)
      dp[0][j] = 1;
  }
  int ans[MAX_N];
  ans[0] = 1;
  for (int i = 1; i <= n; i++) {
    ans[i] = 0;
    if (ss[i - 1] == 'J') {
      dp[i][0] = dp[i - 1][0] + dp[i - 1][3] + dp[i - 1][4] + dp[i - 1][6];
      dp[i][1] = 0;
      dp[i][2] = 0;
      dp[i][3] = ans[i - 1] - dp[i - 1][2];
      dp[i][4] = ans[i - 1] - dp[i - 1][1];
      dp[i][5] = 0;
      dp[i][6] = ans[i - 1];
    }
    if (ss[i - 1] == 'O') {
      dp[i][0] = 0;
      dp[i][1] = dp[i - 1][1] + dp[i - 1][3] + dp[i - 1][5] + dp[i - 1][6];
      dp[i][2] = 0;
      dp[i][3] = ans[i - 1] - dp[i - 1][2];
      dp[i][4] = 0;
      dp[i][5] = ans[i - 1] - dp[i - 1][0];
      dp[i][6] = ans[i - 1];
    }
    if (ss[i - 1] == 'I') {
      dp[i][0] = 0;
      dp[i][1] = 0;
      dp[i][2] = dp[i - 1][2] + dp[i - 1][4] + dp[i - 1][5] + dp[i - 1][6];
      dp[i][3] = 0;
      dp[i][4] = ans[i - 1] - dp[i - 1][1];
      dp[i][5] = ans[i - 1] - dp[i - 1][0];
      dp[i][6] = ans[i - 1];
    }
    for (int j = 0; j < 7; j++) {
      ans[i] += dp[i][j];
      dp[i][j] += mod;
      dp[i][j] %= mod;
    }
    ans[i] %= mod;
  }
  printf("%d\n", ans[n]);
  return 0;
}
// メモリ数確認したか?

Fixed submission:
// メモリ数確認したか?
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long int ll;

int main() {
  int n;
  string ss;
  cin >> n >> ss;
  const int MAX_N = 1000;
  const int mod = 10007;
  int dp[MAX_N + 1][7];
  for (int j = 0; j < 7; j++) {
    dp[0][j] = 0;
    if (j == 0)
      dp[0][j] = 1;
  }
  int ans[MAX_N + 1];
  ans[0] = 1;
  for (int i = 1; i <= n; i++) {
    ans[i] = 0;
    if (ss[i - 1] == 'J') {
      dp[i][0] = dp[i - 1][0] + dp[i - 1][3] + dp[i - 1][4] + dp[i - 1][6];
      dp[i][1] = 0;
      dp[i][2] = 0;
      dp[i][3] = ans[i - 1] - dp[i - 1][2];
      dp[i][4] = ans[i - 1] - dp[i - 1][1];
      dp[i][5] = 0;
      dp[i][6] = ans[i - 1];
    }
    if (ss[i - 1] == 'O') {
      dp[i][0] = 0;
      dp[i][1] = dp[i - 1][1] + dp[i - 1][3] + dp[i - 1][5] + dp[i - 1][6];
      dp[i][2] = 0;
      dp[i][3] = ans[i - 1] - dp[i - 1][2];
      dp[i][4] = 0;
      dp[i][5] = ans[i - 1] - dp[i - 1][0];
      dp[i][6] = ans[i - 1];
    }
    if (ss[i - 1] == 'I') {
      dp[i][0] = 0;
      dp[i][1] = 0;
      dp[i][2] = dp[i - 1][2] + dp[i - 1][4] + dp[i - 1][5] + dp[i - 1][6];
      dp[i][3] = 0;
      dp[i][4] = ans[i - 1] - dp[i - 1][1];
      dp[i][5] = ans[i - 1] - dp[i - 1][0];
      dp[i][6] = ans[i - 1];
    }
    for (int j = 0; j < 7; j++) {
      ans[i] += dp[i][j];
      dp[i][j] += mod;
      dp[i][j] %= mod;
    }
    ans[i] %= mod;
  }
  printf("%d\n", ans[n]);
  return 0;
}
// メモリ数確認したか?
Problem p00519 (C++)

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

using namespace std;
typedef vector<int> Node;
typedef vector<Node> Graph;
typedef pair<int, int> P;

void bfs(int s, int MAX, const Graph &G, vector<int> &res) {
  vector<int> dist(G.size(), -1);
  queue<int> que;
  que.push(s);
  dist[s] = 0;
  while (!que.empty()) {
    int u = que.front();
    que.pop();
    if (dist[u] + 1 > MAX)
      break;
    for (int v : G[u]) {
      if (dist[v] == -1) {
        dist[v] = dist[u] + 1;
        que.push(v);
        res.push_back(v);
      }
    }
  }
}

int main() {
  int N, K;
  while (cin >> N >> K) {
    vector<int> C(N), R(N);
    REP(i, N) cin >> C[i] >> R[i];
    Graph G(N);
    REP(i, N) {
      int a, b;
      cin >> a >> b;
      G[a - 1].push_back(b - 1);
      G[b - 1].push_back(a - 1);
    }
    vector<int> dist(N, INT_MAX);
    priority_queue<P, vector<P>, greater<P>> que;
    que.push(P(C[0], 0));
    dist[0] = 0;
    while (!que.empty()) {
      int u = que.top().second;
      int c = que.top().first;
      que.pop();
      if (u == N - 1)
        break;
      if (dist[u] < c - C[u])
        continue;

      vector<int> next;
      bfs(u, R[u], G, next);
      for (int v : next) {
        if (dist[v] > c) {
          dist[v] = c;
          que.push(P(c + C[v], v));
        }
      }
    }
    cout << dist[N - 1] << endl;
  }
  return 0;
}

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

using namespace std;
typedef vector<int> Node;
typedef vector<Node> Graph;
typedef pair<int, int> P;

void bfs(int s, int MAX, const Graph &G, vector<int> &res) {
  vector<int> dist(G.size(), -1);
  queue<int> que;
  que.push(s);
  dist[s] = 0;
  while (!que.empty()) {
    int u = que.front();
    que.pop();
    if (dist[u] + 1 > MAX)
      break;
    for (int v : G[u]) {
      if (dist[v] == -1) {
        dist[v] = dist[u] + 1;
        que.push(v);
        res.push_back(v);
      }
    }
  }
}

int main() {
  int N, K;
  while (cin >> N >> K) {
    vector<int> C(N), R(N);
    REP(i, N) cin >> C[i] >> R[i];
    Graph G(N);
    REP(i, K) {
      int a, b;
      cin >> a >> b;
      G[a - 1].push_back(b - 1);
      G[b - 1].push_back(a - 1);
    }
    vector<int> dist(N, INT_MAX);
    priority_queue<P, vector<P>, greater<P>> que;
    que.push(P(C[0], 0));
    dist[0] = 0;
    while (!que.empty()) {
      int u = que.top().second;
      int c = que.top().first;
      que.pop();
      if (u == N - 1)
        break;
      if (dist[u] < c - C[u])
        continue;

      vector<int> next;
      bfs(u, R[u], G, next);
      for (int v : next) {
        if (dist[v] > c) {
          dist[v] = c;
          que.push(P(c + C[v], v));
        }
      }
    }
    cout << dist[N - 1] << endl;
  }
  return 0;
}
Problem p00521 (C++)

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

#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define FORD(i, a, b) for (int i = a; i >= b; i--)
#define FORV(i, a, b) for (int i = a; i * i <= b; i++)
#define Forv(i, a, b) for (int i = a; i * i < b; i++)
#define For(i, a, b) for (int i = a; i < b; i++)
#define Ford(i, a, b) for (int i = a; i > b; i--)
#define Fill(s, a) memset(s, a, sizeof(s))
#define pb push_back
#define mp make_pair
#define fi first
#define se second

int n, m, cnt = 0;
char c[1001][1001], x[3][3];

bool check(int i, int j) {
  FOR(k, 1, 2)
  FOR(l, 1, 2)
  if (c[i + k - 1][j + l - 1] != x[k][l])
    return false;
  return true;
}

int solve(char y, int i, int j) {
  int cnt1 = 0, cnt2 = 0;
  char z = c[i][j];
  if (check(i, j))
    cnt1++;
  if (check(i - 1, j - 1))
    cnt1++;
  if (check(i - 1, j))
    cnt1++;
  if (check(i, j - 1))
    cnt1++;
  c[i][j] = y;
  if (check(i, j))
    cnt2++;
  if (check(i - 1, j - 1))
    cnt2++;
  if (check(i - 1, j))
    cnt2++;
  if (check(i, j - 1))
    cnt2++;
  c[i][j] = z;
  return (cnt2 - cnt1);
}

int main() {
  cin >> m >> n;
  FOR(i, 1, m) {
    scanf("\n");
    FOR(j, 1, n)
    scanf("%c", &c[i][j]);
  }
  scanf("\n%c%c\n%c%c", &x[1][1], &x[1][2], &x[2][1], &x[2][2]);
  For(i, 1, m) For(j, 1, n) {
    if (check(i, j))
      cnt++;
  }
  int res = 0;
  FOR(i, 1, m)
  FOR(j, 1, n) {
    res = max(res, solve('J', i, j));
    res = max(res, solve('O', i, j));
    res = max(res, solve('I', i, j));
  }
  cout << cnt + res << '\n';
}

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

#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define FORD(i, a, b) for (int i = a; i >= b; i--)
#define FORV(i, a, b) for (int i = a; i * i <= b; i++)
#define Forv(i, a, b) for (int i = a; i * i < b; i++)
#define For(i, a, b) for (int i = a; i < b; i++)
#define Ford(i, a, b) for (int i = a; i > b; i--)
#define Fill(s, a) memset(s, a, sizeof(s))
#define pb push_back
#define mp make_pair
#define fi first
#define se second

int n, m, cnt = 0;
char c[1002][1002], x[3][3];

bool check(int i, int j) {
  FOR(k, 1, 2)
  FOR(l, 1, 2)
  if (c[i + k - 1][j + l - 1] != x[k][l])
    return false;
  return true;
}

int solve(char y, int i, int j) {
  int cnt1 = 0, cnt2 = 0;
  char z = c[i][j];
  if (check(i, j))
    cnt1++;
  if (check(i - 1, j - 1))
    cnt1++;
  if (check(i - 1, j))
    cnt1++;
  if (check(i, j - 1))
    cnt1++;
  c[i][j] = y;
  if (check(i, j))
    cnt2++;
  if (check(i - 1, j - 1))
    cnt2++;
  if (check(i - 1, j))
    cnt2++;
  if (check(i, j - 1))
    cnt2++;
  c[i][j] = z;
  return (cnt2 - cnt1);
}

int main() {
  cin >> m >> n;
  FOR(i, 1, m) {
    scanf("\n");
    FOR(j, 1, n)
    scanf("%c", &c[i][j]);
  }
  scanf("\n%c%c\n%c%c", &x[1][1], &x[1][2], &x[2][1], &x[2][2]);
  For(i, 1, m) For(j, 1, n) {
    if (check(i, j))
      cnt++;
  }
  int res = 0;
  FOR(i, 1, m)
  FOR(j, 1, n) {
    res = max(res, solve('J', i, j));
    res = max(res, solve('O', i, j));
    res = max(res, solve('I', i, j));
  }
  cout << cnt + res << '\n';
}
Problem p00521 (C++)

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

using namespace std;

int mm[1001][1001][3];
string flag[1000], emb[2];
int main() {
  int m, n;
  cin >> m >> n;
  int ans = 0;
  for (int i = 0; i < m; ++i)
    cin >> flag[i];
  for (int i = 0; i < 2; ++i)
    cin >> emb[i];
  for (int i = 0; i < m; ++i) {
    for (int j = 0; j < n; ++j) {
      int cnt = 0, mx, my;
      for (int k = 0; k < 2; ++k) {
        for (int l = 0; l < 2; ++l) {
          if (flag[i + k][j + l] == emb[k][l])
            ++cnt;
          else {
            mx = l;
            my = k;
          }
        }
      }
      if (cnt == 4)
        ++ans;
      else if (cnt == 3) {
        if (emb[my][mx] == 'J')
          ++mm[i + my][j + mx][0];
        else if (emb[my][mx] == 'O')
          ++mm[i + my][j + mx][1];
        else
          ++mm[i + my][j + mx][2];
      }
    }
  }
  int w = 0;
  for (int i = 0; i < m; ++i) {
    for (int j = 0; j < n; ++j) {
      for (int k = 0; k < 3; ++k)
        w = max(w, mm[i][j][k]);
    }
  }
  cout << ans + w << endl;
  return 0;
}

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

using namespace std;

int mm[1005][1005][3];
string flag[1005], emb[2];
int main() {
  int m, n;
  cin >> m >> n;
  int ans = 0;
  for (int i = 0; i < m; ++i)
    cin >> flag[i];
  for (int i = 0; i < 2; ++i)
    cin >> emb[i];
  for (int i = 0; i < m; ++i) {
    for (int j = 0; j < n; ++j) {
      int cnt = 0, mx, my;
      for (int k = 0; k < 2; ++k) {
        for (int l = 0; l < 2; ++l) {
          if (flag[i + k][j + l] == emb[k][l])
            ++cnt;
          else {
            mx = l;
            my = k;
          }
        }
      }
      if (cnt == 4)
        ++ans;
      else if (cnt == 3) {
        if (emb[my][mx] == 'J')
          ++mm[i + my][j + mx][0];
        else if (emb[my][mx] == 'O')
          ++mm[i + my][j + mx][1];
        else
          ++mm[i + my][j + mx][2];
      }
    }
  }
  int w = 0;
  for (int i = 0; i < m; ++i) {
    for (int j = 0; j < n; ++j) {
      for (int k = 0; k < 3; ++k)
        w = max(w, mm[i][j][k]);
    }
  }
  cout << ans + w << endl;
  return 0;
}
Problem p00522 (C++)

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

int m, n, p[10100], sum[10100], c[550], e[550];
int dp[10100];

int main() {
  cin >> m >> n;
  for (int i = 0; i < m; i++)
    cin >> p[i];
  sort(p, p + m);
  for (int i = 0; i < m; i++)
    sum[i + 1] = sum[i] + p[m - 1 - i];

  for (int i = 0; i < n; i++)
    cin >> c[i] >> e[i];
  for (int i = 0; i <= m; i++)
    dp[i] = 1e9;
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = m + c[i]; j >= 0; j--) {
      int nxt = min(j, m);
      dp[nxt] = min(dp[nxt], dp[j - c[i]] + e[i]);
    }
  }

  int ans = 0;
  for (int i = 0; i <= m; i++)
    ans = max(ans, sum[i] - dp[i]);
  cout << ans << endl;
}

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

int m, n, p[10100], sum[10100], c[550], e[550];
int dp[10100];

int main() {
  cin >> m >> n;
  for (int i = 0; i < m; i++)
    cin >> p[i];
  sort(p, p + m);
  for (int i = 0; i < m; i++)
    sum[i + 1] = sum[i] + p[m - 1 - i];

  for (int i = 0; i < n; i++)
    cin >> c[i] >> e[i];
  for (int i = 0; i <= m; i++)
    dp[i] = 1e9;
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = m + c[i]; j >= c[i]; j--) {
      int nxt = min(j, m);
      dp[nxt] = min(dp[nxt], dp[j - c[i]] + e[i]);
    }
  }

  int ans = 0;
  for (int i = 0; i <= m; i++)
    ans = max(ans, sum[i] - dp[i]);
  cout << ans << endl;
}
Problem p00522 (C++)

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

using namespace std;

#define rep(x, y) for (int x = 0; x < (y); x++)

typedef long long ll;

int M, N;
int P[10005], C[512], E[512];
ll dp[10005][512];

ll dfs(int m, int n) {
  if (m > M || n >= N)
    return 0;
  ll res = 0;
  if (m <= M)
    res = max(res,
              dfs(min(M, m + C[n]), n + 1) + P[min(M, m + C[n])] - P[m] - E[n]);
  res = max(res, dfs(m, n + 1));
  return res;
}

int main() {
  scanf("%d%d", &M, &N);
  P[0] = 0;
  rep(i, M) scanf("%d", P + i + 1);
  sort(P + 1, P + M + 1, greater<int>());
  rep(i, M) P[i + 1] += P[i];
  rep(i, N) scanf("%d%d", C + i, E + i);
  memset(dp, -1, sizeof(dp));
  printf("%lld\n", dfs(0, 0));
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

#define rep(x, y) for (int x = 0; x < (y); x++)

typedef long long ll;

int M, N;
int P[10005], C[512], E[512];
ll dp[10005][512];

ll dfs(int m, int n) {
  if (m > M || n >= N)
    return 0;
  ll &res = dp[m][n];
  if (res >= 0)
    return res;
  res = 0;
  if (m <= M)
    res = max(res,
              dfs(min(M, m + C[n]), n + 1) + P[min(M, m + C[n])] - P[m] - E[n]);
  res = max(res, dfs(m, n + 1));
  return res;
}

int main() {
  scanf("%d%d", &M, &N);
  P[0] = 0;
  rep(i, M) scanf("%d", P + i + 1);
  sort(P + 1, P + M + 1, greater<int>());
  rep(i, M) P[i + 1] += P[i];
  rep(i, N) scanf("%d%d", C + i, E + i);
  memset(dp, -1, sizeof(dp));
  printf("%lld\n", dfs(0, 0));
}
Problem p00522 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
const int INF = 1e9;
int dp[100100];
int main() {
  int m, n;
  cin >> m >> n;
  int c[550], e[550];
  vector<int> p(m);
  REP(i, 100100) dp[i] = INF;
  REP(i, m) cin >> p[i];
  dp[0] = 0;
  int sum = 0;
  REP(i, n) {
    cin >> c[i] >> e[i];
    for (int j = sum; j >= 0; j--) {
      dp[j + c[i]] = min(dp[j + c[i]], dp[j] + e[i]);
    }
    sum += c[i];
    sum = min(sum, 100000);
  }
  for (int i = sum; i >= 0; i--) {
    dp[i] = min(dp[i], dp[i + 1]);
  }
  sort(ALL(p), greater<int>());
  sum = 0;
  int ans = 0;
  REP(i, m) {
    sum += p[i];
    ans = max(ans, sum - dp[i + 1]);
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
const int INF = 1e9;
int dp[100100];
int main() {
  int m, n;
  cin >> m >> n;
  int c[550], e[550];
  vector<int> p(m);
  REP(i, 100100) dp[i] = INF;
  REP(i, m) cin >> p[i];
  dp[0] = 0;
  int sum = 0;
  REP(i, n) {
    cin >> c[i] >> e[i];
    for (int j = sum; j >= 0; j--) {
      dp[j + c[i]] = min(dp[j + c[i]], dp[j] + e[i]);
    }
    sum += c[i];
    sum = min(sum, 20000);
  }
  for (int i = sum; i >= 0; i--) {
    dp[i] = min(dp[i], dp[i + 1]);
  }
  sort(ALL(p), greater<int>());
  sum = 0;
  int ans = 0;
  REP(i, m) {
    sum += p[i];
    ans = max(ans, sum - dp[i + 1]);
  }
  cout << ans << endl;
}
Problem p00522 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1 << 30;
int main() {
  int n, m;
  cin >> n >> m;
  vi in(n);
  rep(i, n) cin >> in[i];
  rep(i, n) in[i] *= -1;
  sort(all(in));
  vi dp(10010, inf);
  dp[0] = 0;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    for (int j = 10010; j >= 0; j--) {
      dp[j] = min(dp[j], dp[max(0, j - a)] + b);
    }
  }
  int out = 0;
  int sum = 0;
  rep(i, n) {
    sum -= in[i];
    out = max(out, sum - dp[i + 1]);
  }
  cout << out << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1 << 30;
int main() {
  int n, m;
  cin >> n >> m;
  vi in(n);
  rep(i, n) cin >> in[i];
  rep(i, n) in[i] *= -1;
  sort(all(in));
  vi dp(10010, inf);
  dp[0] = 0;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    for (int j = 10009; j >= 0; j--) {
      dp[j] = min(dp[j], dp[max(0, j - a)] + b);
    }
  }
  int out = 0;
  int sum = 0;
  rep(i, n) {
    sum -= in[i];
    out = max(out, sum - dp[i + 1]);
  }
  cout << out << endl;
  return 0;
}
Problem p00522 (C++)

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

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define mp(a, b) make_pair((a), (b))
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;

const int INF = 1 << 30;
const long long int INF_ = 1LL << 58;
const double EPS = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

void Solve() {
  int m, n;
  cin >> m >> n;
  vector<int> p(m), c(n), e(n);
  rep(i, 0, m) cin >> p[i];
  rep(i, 0, n) cin >> c[i] >> e[i];

  sort(p.rbegin(), p.rend());
  vector<int> sum(m + 1);
  rep(i, 0, m) sum[i + 1] = sum[i] + p[i];

  int ans = max(0, sum[c[0]] - e[0]);
  static int dp[500][10001];
  fill_n((int *)dp, 500 * 10001, INF);
  rep(i, 0, c[0] + 1) dp[0][i] = e[0];
  rep(i, 1, n) {
    rep(j, 0, c[i] + 1) dp[i][j] = e[i];
    rep(j, 0, m + 1) {
      dp[i][j] =
          min(dp[i][j], min(dp[i - 1][j], dp[i - 1][max(0, j - c[i])] + e[i]));
      ans = max(ans, sum[j] - dp[i][j]);
    }
  }

  cout << ans << endl;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  Solve();
  return 0;
}

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

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define mp(a, b) make_pair((a), (b))
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;

const int INF = 1 << 30;
const long long int INF_ = 1LL << 58;
const double EPS = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

void Solve() {
  int m, n;
  cin >> m >> n;
  vector<int> p(m), c(n), e(n);
  rep(i, 0, m) cin >> p[i];
  rep(i, 0, n) cin >> c[i] >> e[i];

  sort(p.rbegin(), p.rend());
  vector<int> sum(m + 1);
  rep(i, 0, m) sum[i + 1] = sum[i] + p[i];

  int ans = max(0, sum[min(c[0], m)] - e[0]);
  static int dp[500][10001];
  fill_n((int *)dp, 500 * 10001, INF);
  rep(i, 0, c[0] + 1) dp[0][i] = e[0];
  rep(i, 1, n) {
    rep(j, 0, c[i] + 1) dp[i][j] = e[i];
    rep(j, 0, m + 1) {
      dp[i][j] =
          min(dp[i][j], min(dp[i - 1][j], dp[i - 1][max(0, j - c[i])] + e[i]));
      ans = max(ans, sum[j] - dp[i][j]);
    }
  }

  cout << ans << endl;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  Solve();
  return 0;
}
Problem p00522 (C++)

Buggy submission (status: Time Limit Exceeded):
// AOJ 0599
#include <algorithm>
#include <cstdio>
#include <functional>
#include <vector>
#define rep(i, a) for (int i = 0; i < (a); ++i)

const int MAX_M = 10000, MAX_N = 500, INF = 1 << 29;

int M, N;
int P[MAX_M];
int C[MAX_N], E[MAX_N];
int dp[MAX_N + 1][MAX_M + 1];

int main() {
  scanf("%d%d", &M, &N);
  rep(i, M) scanf("%d", P + i);
  rep(i, N) scanf("%d%d", C + i, E + i);

  std::sort(P, P + M, std::greater<int>());

  rep(i, N + 1) rep(j, M + 1) dp[i][j] = INF;
  dp[0][0] = 0;
  rep(i, N) rep(j, M + 1) rep(k, C[i] + 1) if (j + k <= M) dp[i + 1][j + k] =
      std::min(dp[i + 1][j + k], dp[i][j] + (k ? E[i] : 0));

  int ans = 0, sum = 0;
  rep(j, M + 1) {
    ans = std::max(ans, sum - dp[N][j]);

    if (j < M)
      sum += P[j];
  }

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

  return 0;
}

Fixed submission:
// AOJ 0599
#include <algorithm>
#include <cstdio>
#include <functional>
#include <vector>
#define rep(i, a) for (int i = 0; i < (a); ++i)

const int MAX_M = 10000, MAX_N = 500, INF = 1 << 29;

int M, N;
int P[MAX_M];
int C[MAX_N], E[MAX_N];
int dp[MAX_N + 1][MAX_M + 1];

int main() {
  scanf("%d%d", &M, &N);
  rep(i, M) scanf("%d", P + i);
  rep(i, N) scanf("%d%d", C + i, E + i);

  std::sort(P, P + M, std::greater<int>());

  rep(i, N + 1) rep(j, M + 1) dp[i][j] = INF;
  dp[0][0] = 0;
  rep(i, N) rep(j, M + 1) {
    dp[i + 1][j] = std::min(dp[i + 1][j], dp[i][j]);

    dp[i + 1][std::min(M, j + C[i])] =
        std::min(dp[i + 1][std::min(M, j + C[i])], dp[i][j] + E[i]);
  }

  int ans = 0, sum = 0;
  rep(j, M + 1) {
    ans = std::max(ans, sum - dp[N][j]);

    if (j < M)
      sum += P[j];
  }

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

  return 0;
}
Problem p00522 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;
struct box {
  int c, e;
};
int dp[10010];
int main() {
  int m, n;
  cin >> m >> n;
  int p[n], sum[m + 1];
  sum[0] = 0;
  box b[n];

  for (int i = 0; i < m; i++)
    cin >> p[i];
  for (int i = 0; i < n; i++)
    cin >> b[i].c >> b[i].e;

  sort(p, p + m, greater<int>());
  for (int i = 0; i < m; i++)
    sum[i + 1] = sum[i] + p[i];
  for (int i = 0; i <= m; i++)
    dp[i] = 1e9;
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = m + b[i].c; j >= b[i].c; j--) {
      int next = min(j, m);
      dp[next] = min(dp[next], dp[j - b[i].c] + b[i].e);
    }
  }
  int ans = 0;
  for (int i = 0; i <= m; i++)
    ans = max(ans, sum[i] - dp[i]);
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
struct box {
  int c, e;
};
int dp[10010];
int main() {
  int m, n;
  cin >> m >> n;
  int p[m], sum[m + 1];
  sum[0] = 0;
  box b[n];

  for (int i = 0; i < m; i++)
    cin >> p[i];
  for (int i = 0; i < n; i++)
    cin >> b[i].c >> b[i].e;

  sort(p, p + m, greater<int>());
  for (int i = 0; i < m; i++)
    sum[i + 1] = sum[i] + p[i];
  for (int i = 0; i <= m; i++)
    dp[i] = 1e9;
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = m + b[i].c; j >= b[i].c; j--) {
      int next = min(j, m);
      dp[next] = min(dp[next], dp[j - b[i].c] + b[i].e);
    }
  }
  int ans = 0;
  for (int i = 0; i <= m; i++)
    ans = max(ans, sum[i] - dp[i]);
  cout << ans << endl;
}
Problem p00522 (C++)

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

#define INF (1 << 29)

using namespace std;

int main() {
  int N, M;

  scanf("%d", &N);
  scanf("%d", &M);

  vector<int> P(N);
  vector<int> C(M);
  vector<int> E(M);

  for (int i = 0; i < N; i++) {
    scanf("%d", &P[i]);
  }

  for (int i = 0; i < M; i++) {
    scanf("%d", &C[i]);
    scanf("%d", &E[i]);
  }

  vector<vector<int>> dp(M + 1, vector<int>(N + 1, INF));
  dp[0][0] = 0;

  for (int i = 1; i <= M; i++) {
    for (int j = 0; j <= N; j++) {
      dp[i][j] = dp[i - 1][j];

      for (int k = max(0, j - C[i - 1]); k < j; k++) {
        dp[i][j] = min(dp[i][j], dp[i - 1][k] + E[i - 1]);
      }
    }
  }

  sort(P.begin(), P.end(), greater<int>());

  vector<int> S(N + 1);
  S[0] = 0;

  for (int i = 1; i <= N; i++) {
    S[i] = S[i - 1] + P[i - 1];
  }

  int ret = 0;

  for (int i = 0; i <= N; i++) {
    ret = max(ret, S[i] - dp[M][i]);
  }

  printf("%d\n", ret);

  return 0;
}

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

#define INF (1 << 29)

using namespace std;

int main() {
  int N, M;

  scanf("%d", &N);
  scanf("%d", &M);

  vector<int> P(N);
  vector<int> C(M);
  vector<int> E(M);

  for (int i = 0; i < N; i++) {
    scanf("%d", &P[i]);
  }

  for (int i = 0; i < M; i++) {
    scanf("%d", &C[i]);
    scanf("%d", &E[i]);
  }

  vector<vector<int>> dp(M + 1, vector<int>(N + 1, INF));
  dp[0][0] = 0;

  for (int i = 1; i <= M; i++) {
    for (int j = 0; j <= N; j++) {
      dp[i][j] = min(dp[i - 1][j], dp[i - 1][max(0, j - C[i - 1])] + E[i - 1]);
    }
  }

  sort(P.begin(), P.end(), greater<int>());

  vector<int> S(N + 1);
  S[0] = 0;

  for (int i = 1; i <= N; i++) {
    S[i] = S[i - 1] + P[i - 1];
  }

  int ret = 0;

  for (int i = 0; i <= N; i++) {
    ret = max(ret, S[i] - dp[M][i]);
  }

  printf("%d\n", ret);

  return 0;
}
Problem p00522 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
int x[1000][50000], n, m, a, b, c, s[100000], t[1000][2], sum, MAX;
int main() {
  memset(x, 127, sizeof(x));
  x[0][0] = 0;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    sum += s[i];
  }
  for (int i = 1; i <= m; i++) {
    cin >> t[i][0] >> t[i][1];
    for (int j = 0; j <= n; j++) {
      x[i][j] = min(x[i][j], x[i - 1][j]);
      a = min(t[i][0], n - j);
      c = 1;
      if (t[i][0] == 10000) {
        c = 10000;
      }
      if (sum == n && n == 10000) {
        c = 10000;
      }
      for (int k = c; k <= a; k++) {
        x[i][j + k] = min(x[i][j + k], x[i - 1][j] + t[i][1]);
      }
    }
  }
  sort(s, s + n);
  sum = 0;
  for (int i = 0; i <= n; i++) {
    MAX = max(MAX, sum - x[m][i]);
    if (i != n) {
      sum += s[n - i - 1];
    }
  }
  cout << MAX << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
int x[1000][50000], n, m, a, b, c, s[100000], t[1000][2], sum, MAX;
int main() {
  memset(x, 127, sizeof(x));
  x[0][0] = 0;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    sum += s[i];
  }
  for (int i = 1; i <= m; i++) {
    cin >> t[i][0] >> t[i][1];
    for (int j = 0; j <= n; j++) {
      x[i][j] = min(x[i][j], x[i - 1][j]);
      a = min(t[i][0], n - j);
      c = 1;
      if (t[i][0] == 10000) {
        c = 10000;
      } else if (t[i][0] > 3000 && n == 10000) {
        c = t[i][0] / 2;
      }
      if (sum == n && n == 10000) {
        c = 10000;
      }
      for (int k = c; k <= a; k++) {
        x[i][j + k] = min(x[i][j + k], x[i - 1][j] + t[i][1]);
      }
    }
  }
  sort(s, s + n);
  sum = 0;
  for (int i = 0; i <= n; i++) {
    MAX = max(MAX, sum - x[m][i]);
    if (i != n) {
      sum += s[n - i - 1];
    }
  }
  cout << MAX << endl;
  return 0;
}
Problem p00522 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 30;

int main() {
  int M, N, P[10000], C[100], E[100];
  cin >> M >> N;
  for (int i = 0; i < M; i++) {
    cin >> P[i];
  }
  sort(P, P + M, greater<int>());

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

  int dp[50001]; // dp[i]:饅頭をi個以上入れるための箱の値段の最小値
  fill_n(dp, 50001, INF);
  dp[0] = 0;
  for (int i = 0; i < N; i++) {
    for (int j = M; j >= 0; j--) {
      if (dp[j] != INF) {
        dp[min(M, j + C[i])] = min(dp[min(M, j + C[i])], dp[j] + E[i]);
      }
    }
  }

  int sum[10001];
  sum[1] = P[0];
  for (int i = 2; i <= M; i++) {
    sum[i] = sum[i - 1] + P[i - 1];
  }

  int ret = 0;
  for (int i = 1; i <= M; i++) {
    if (dp[i] != INF)
      ret = max(ret, sum[i] - dp[i]);
  }
  cout << ret << endl;
  return (0);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 30;

int main() {
  int M, N, P[10000], C[500], E[500];
  cin >> M >> N;
  for (int i = 0; i < M; i++) {
    cin >> P[i];
  }
  sort(P, P + M, greater<int>());

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

  int dp[50001]; // dp[i]:饅頭をi個以上入れるための箱の値段の最小値
  fill_n(dp, 50001, INF);
  dp[0] = 0;
  for (int i = 0; i < N; i++) {
    for (int j = M; j >= 0; j--) {
      if (dp[j] != INF) {
        dp[min(M, j + C[i])] = min(dp[min(M, j + C[i])], dp[j] + E[i]);
      }
    }
  }

  int sum[10001];
  sum[1] = P[0];
  for (int i = 2; i <= M; i++) {
    sum[i] = sum[i - 1] + P[i - 1];
  }

  int ret = 0;
  for (int i = 1; i <= M; i++) {
    if (dp[i] != INF)
      ret = max(ret, sum[i] - dp[i]);
  }
  cout << ret << endl;
  return (0);
}
Problem p00522 (C++)

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

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define INF (1 << 29)

int manju[10000], c[10000], e[10000];
int m, n;
int sum[10000], dp[501][10001];

int main() {
  cin >> m >> n;
  REP(i, m) scanf("%d", &manju[i]);
  REP(i, n) scanf("%d %d", &c[i], &e[i]);

  sort(manju, manju + m, greater<int>());
  sum[0] = 0;
  REP(i, m) sum[i + 1] = sum[i] + manju[i];

  fill(dp[0], dp[501], INF);
  dp[0][0] = 0;

  REP(i, n) {
    REP(j, m + 1) {
      dp[i + 1][j] = dp[i][j];
      if (j >= c[i])
        dp[i + 1][j] = min(dp[i + 1][j], dp[i][j - c[i]] + e[i]);
      else
        dp[i + 1][j] = min(dp[i + 1][j], dp[i + 1][0] + e[i]);
    }
  }

  int ans = 0;
  REP(i, m + 1) ans = max(ans, sum[i] - dp[n][i]);
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define INF (1 << 29)

int manju[10000], c[10000], e[10000];
int m, n;
int sum[10001], dp[501][10001];

int main() {
  cin >> m >> n;
  REP(i, m) scanf("%d", &manju[i]);
  REP(i, n) scanf("%d %d", &c[i], &e[i]);

  sort(manju, manju + m, greater<int>());
  sum[0] = 0;
  REP(i, m) sum[i + 1] = sum[i] + manju[i];

  fill(dp[0], dp[501], INF);
  dp[0][0] = 0;

  REP(i, n) {
    REP(j, m + 1) {
      dp[i + 1][j] = dp[i][j];
      if (j >= c[i])
        dp[i + 1][j] = min(dp[i + 1][j], dp[i][j - c[i]] + e[i]);
      else
        dp[i + 1][j] = min(dp[i + 1][j], dp[i + 1][0] + e[i]);
    }
  }

  int ans = 0;
  REP(i, m + 1) ans = max(ans, sum[i] - dp[n][i]);
  cout << ans << endl;

  return 0;
}
Problem p00523 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#define INF (1e+15)
using namespace std;

typedef long long ll;

int main() {

  int n, a[100001];
  ll ssum[100001 * 2] = {0};

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

  ssum[0] = a[0];
  for (int i = 1; i < n * 2; i++)
    ssum[i] = ssum[i - 1] + a[i % n];

  ll al = 0, ar = INF, amid;
  int c2;
  while (al < ar) {
    amid = (al + ar) / 2; // 解を二分探索
    bool judge = false;
    for (int i = 0; i < n; i++) {
      int l = i, r = n + i; // 添字を二分探索
      while (l < r) {
        int mid = (l + r) / 2;
        ll sub = ssum[mid] - ssum[i];
        if (sub < amid)
          l = mid + 1;
        else
          r = mid;
      }
      c2 = l++;
      r = n + i;
      while (l < r) {
        int mid = (l + r) / 2;
        ll sub = ssum[mid] - ssum[c2];
        if (sub < amid)
          l = mid + 1;
        else
          r = mid;
      }

      if (ssum[i + n] - ssum[r] >= amid) {
        judge = true;
        break;
      }
    }
    if (judge)
      al = amid;
    else
      ar = amid - 1;
  }
  cout << al << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#define INF (1e+15)
using namespace std;

typedef long long ll;

int main() {

  int n, a[100001];
  ll ssum[100001 * 2] = {0};

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

  ssum[0] = a[0];
  for (int i = 1; i < n * 2; i++)
    ssum[i] = ssum[i - 1] + a[i % n];

  ll al = 0, ar = INF, amid;
  int c2;
  while (al < ar) {
    amid = ((al + 1) + ar) / 2; // 解を二分探索
    bool judge = false;
    for (int i = 0; i < n; i++) {
      int l = i, r = n + i; // 添字を二分探索
      while (l < r) {
        int mid = (l + r) / 2;
        ll sub = ssum[mid] - ssum[i];
        if (sub < amid)
          l = mid + 1;
        else
          r = mid;
      }
      c2 = l++;
      r = n + i;
      while (l < r) {
        int mid = (l + r) / 2;
        ll sub = ssum[mid] - ssum[c2];
        if (sub < amid)
          l = mid + 1;
        else
          r = mid;
      }

      if (ssum[i + n] - ssum[r] >= amid) {
        judge = true;
        break;
      }
    }
    if (judge)
      al = amid;
    else
      ar = amid - 1;
  }
  cout << al << endl;
}
Problem p00523 (C++)

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

using namespace std;

#define For(i, a, b) for (int i = (a); i <= (b); i++)
#define Rep(i, n) for (int i = 0; i < (n); i++)
#define Ford(i, a, b) for (int i = (a); i >= (b); i--)

const int maxn = 100000 + 1912;
int n, a[maxn];
long long totalSum = 0;
long long sum[maxn];

void ReadData() {
  scanf("%d", &n);
  For(i, 1, n) scanf("%d", &a[i]);
  For(i, 1, n) a[i + n] = a[i];
  For(i, 1, n << 1) sum[i] = sum[i - 1] + a[i];
}

long long GetSum(const int l, const int r) { return sum[r] - sum[l - 1]; }

bool check(long long limit) {
  int ptr1 = 1, ptr2 = 2;
  For(i, 1, n - 1) {
    int last = i + n - 1;
    while (ptr1 < last - 1 && GetSum(i, ptr1) < limit)
      ptr1++;
    ptr2 = max(ptr2, ptr1 + 1);
    while (ptr2 < last && GetSum(ptr1 + 1, ptr2) < limit)
      ptr2++;

    if ((ptr2 <= last) &&
        (GetSum(i, ptr1) >= limit && GetSum(ptr1 + 1, ptr2) >= limit &&
         GetSum(ptr2 + 1, last) >= limit))
      return true;
  }
  return false;
}

void Process() {
  long long res = 0;
  long long l = 1, r = 1000LL * 1000 * 1000 * 1000 * 1000 + 1912;
  while (l <= r) {
    long long mid = (l + r) >> 1;
    if (check(mid)) {
      res = mid;
      l = mid + 1;
    } else
      r = mid - 1;
  }
  cout << res << endl;
}

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

  // freopen("./baumkuchen.inp" , "r" , stdin);
  ReadData();
  Process();

  return 0;
}

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

using namespace std;

#define For(i, a, b) for (int i = (a); i <= (b); i++)
#define Rep(i, n) for (int i = 0; i < (n); i++)
#define Ford(i, a, b) for (int i = (a); i >= (b); i--)

const int maxn = 200000 + 1912;
int n, a[maxn];
long long totalSum = 0;
long long sum[maxn];

void ReadData() {
  scanf("%d", &n);
  For(i, 1, n) scanf("%d", &a[i]);
  For(i, 1, n) a[i + n] = a[i];
  For(i, 1, n << 1) sum[i] = sum[i - 1] + a[i];
}

long long GetSum(const int l, const int r) { return sum[r] - sum[l - 1]; }

bool check(long long limit) {
  int ptr1 = 1, ptr2 = 2;
  For(i, 1, n - 1) {
    int last = i + n - 1;
    while (ptr1 < last - 1 && GetSum(i, ptr1) < limit)
      ptr1++;
    ptr2 = max(ptr2, ptr1 + 1);
    while (ptr2 < last && GetSum(ptr1 + 1, ptr2) < limit)
      ptr2++;

    if ((ptr2 <= last) &&
        (GetSum(i, ptr1) >= limit && GetSum(ptr1 + 1, ptr2) >= limit &&
         GetSum(ptr2 + 1, last) >= limit))
      return true;
  }
  return false;
}

void Process() {
  long long res = 0;
  long long l = 1, r = 1000LL * 1000 * 1000 * 1000 * 1000 + 1912;
  while (l <= r) {
    long long mid = (l + r) >> 1;
    if (check(mid)) {
      res = mid;
      l = mid + 1;
    } else
      r = mid - 1;
  }
  cout << res << endl;
}

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

  // freopen("./baumkuchen.inp" , "r" , stdin);
  ReadData();
  Process();

  return 0;
}
Problem p00523 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cstdio>

using namespace std;

int a[111111 * 2];
long long sum[111111 * 2];
long long res = 0;
int n;

long long C(int s, int e) {
  long long lim = sum[e] - sum[s];
  // printf("[%d,%d) [%d,%d)\n",s,e,e,s+n);
  s += n;
  int l = e, r = s;
  while (e < s) {
    int m = (s + e) / 2;
    assert(sum[m] - sum[l] + lim + sum[r] - sum[m] == sum[n]);
    // printf("%d %d %d %lld %lld %lld
    // %lld\n",e,m,s,sum[m]-sum[l],lim,sum[r]-sum[m],sum[m]-sum[l]+lim+sum[r]-sum[m]);
    if (sum[m] - sum[l] >= sum[r] - sum[m]) {
      s = m;
    } else {
      e = m + 1;
    }
  }
  // s = e;
  assert(lim + sum[r] - sum[s] + sum[s] - sum[l] == sum[n]);
  // printf(":%d %d %lld %lld %lld %lld %d
  // %d\n",l,r,lim,sum[r]-sum[s],sum[s]-sum[l],lim+sum[r]-sum[s]+sum[s]-sum[l],sum[n],l,r);
  long long ret = min(min(lim, sum[r] - sum[s]), sum[s] - sum[l]);
  res = max(res, ret);
  return ret;
}

int main(void) {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", a + i);
    a[i + n] = a[i];
    a[i + 2 * n] = a[i];
  }
  for (int j = 0; j < 3 * n; j++) {
    sum[j + 1] = sum[j] + a[j];
  }
  int s, e;
  s = 0;
  e = 1;
  for (; e < 2 * n; ++e) {
    while (e < 2 * n && C(s, e) > sum[e] - sum[s]) {
      ++e;
    }
    while (s < e && C(s, e) < sum[e] - sum[s]) {
      ++s;
    }
  }
  printf("%lld\n", res);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cstdio>

using namespace std;

int a[111111 * 3];
long long sum[111111 * 3];
long long res = 0;
int n;

long long C(int s, int e) {
  long long lim = sum[e] - sum[s];
  // printf("[%d,%d) [%d,%d)\n",s,e,e,s+n);
  s += n;
  int l = e, r = s;
  while (e < s) {
    int m = (s + e) / 2;
    assert(sum[m] - sum[l] + lim + sum[r] - sum[m] == sum[n]);
    // printf("%d %d %d %lld %lld %lld
    // %lld\n",e,m,s,sum[m]-sum[l],lim,sum[r]-sum[m],sum[m]-sum[l]+lim+sum[r]-sum[m]);
    if (sum[m] - sum[l] >= sum[r] - sum[m]) {
      s = m;
    } else {
      e = m + 1;
    }
  }
  // s = e;
  assert(lim + sum[r] - sum[s] + sum[s] - sum[l] == sum[n]);
  // printf(":%d %d %lld %lld %lld %lld %d
  // %d\n",l,r,lim,sum[r]-sum[s],sum[s]-sum[l],lim+sum[r]-sum[s]+sum[s]-sum[l],sum[n],l,r);
  long long ret = min(min(lim, sum[r] - sum[s]), sum[s] - sum[l]);
  res = max(res, ret);
  return ret;
}

int main(void) {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", a + i);
    a[i + n] = a[i];
    a[i + 2 * n] = a[i];
  }
  for (int j = 0; j < 3 * n; j++) {
    sum[j + 1] = sum[j] + a[j];
  }
  int s, e;
  s = 0;
  e = 1;
  for (; e < 2 * n; ++e) {
    while (e < 2 * n && C(s, e) > sum[e] - sum[s]) {
      ++e;
    }
    while (s < e && C(s, e) < sum[e] - sum[s]) {
      ++s;
    }
  }
  printf("%lld\n", res);
  return 0;
}
Problem p00524 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 1001001001001001001LL;
typedef pair<int, int> P;
struct edge {
  int to, cost;
  edge(int a, int b) : to(a), cost(b) {}
};

int N, M, X;
int H[100000];

vector<edge> G[100000];
int dist[100000];

signed main() {
  cin >> N >> M >> X;
  for (int i = 0; i < N; i++)
    cin >> H[i];
  for (int i = 0; i < M; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    G[a].push_back(edge(b, c));
    G[b].push_back(edge(a, c));
  }

  fill_n(dist, N, INF);

  priority_queue<P> que;
  que.push(P(0, 0));
  dist[0] = 0;
  while (que.size()) {
    P p = que.top();
    que.pop();
    if (dist[p.second] < p.first)
      continue;
    int h = (p.first >= X ? 0 : X - p.first);

    for (int i = 0; i < G[p.second].size(); i++) {
      edge &e = G[p.second][i];
      if (H[p.second] < e.cost)
        continue;
      int cost;
      if (h - e.cost > H[e.to])
        cost = h - H[e.to];
      else if (h - e.cost >= 0)
        cost = e.cost;
      else
        cost = e.cost - (h - e.cost);
      if (dist[e.to] <= p.first + cost)
        continue;
      dist[e.to] = p.first + cost;
      que.push(P(dist[e.to], e.to));
    }
  }
  if (dist[N - 1] == INF)
    cout << -1 << endl;
  else
    cout << dist[N - 1] + H[N - 1] - (dist[N - 1] >= X ? 0 : X - dist[N - 1])
         << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 1001001001001001001LL;
typedef pair<int, int> P;
struct edge {
  int to, cost;
  edge(int a, int b) : to(a), cost(b) {}
};

int N, M, X;
int H[100000];

vector<edge> G[100000];
int dist[100000];

signed main() {
  cin >> N >> M >> X;
  for (int i = 0; i < N; i++)
    cin >> H[i];
  for (int i = 0; i < M; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    G[a].push_back(edge(b, c));
    G[b].push_back(edge(a, c));
  }

  fill_n(dist, N, INF);

  priority_queue<P, vector<P>, greater<P>> que;
  que.push(P(0, 0));
  dist[0] = 0;
  while (que.size()) {
    P p = que.top();
    que.pop();
    if (dist[p.second] < p.first)
      continue;
    int h = (p.first >= X ? 0 : X - p.first);

    for (int i = 0; i < G[p.second].size(); i++) {
      edge &e = G[p.second][i];
      if (H[p.second] < e.cost)
        continue;
      int cost;
      if (h - e.cost > H[e.to])
        cost = h - H[e.to];
      else if (h - e.cost >= 0)
        cost = e.cost;
      else
        cost = e.cost - (h - e.cost);
      if (dist[e.to] <= p.first + cost)
        continue;
      dist[e.to] = p.first + cost;
      que.push(P(dist[e.to], e.to));
    }
  }
  if (dist[N - 1] == INF)
    cout << -1 << endl;
  else
    cout << dist[N - 1] + H[N - 1] - (dist[N - 1] >= X ? 0 : X - dist[N - 1])
         << endl;
  return 0;
}