alexjercan/bugnet huggingface dataset:
Problem p00820 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define INF 1001000100010001000
#define MOD 1000000007
#define EPS 1e-10
#define int long long
#define rep(i, N) for (int i = 0; i < N; i++)
#define Rep(i, N) for (int i = 1; i < N; i++)
#define For(i, a, b) for (int i = (a); i < (b); i++)
#define pb push_back
#define mp make_pair
#define i_i pair<int, int>
#define vi vector<int>
#define vvi vector<vi>
#define vb vector<bool>
#define vvb vector<vb>
#define vp vector<i_i>
#define all(a) (a).begin(), (a).end()
#define Int(x)                                                                 \\
  int x;                                                                       \\
  scanf("%lld", &x);

// int dxy[5] = {0, 1, 0, -1, 0};
//  assign

signed main() {
  Int(n);
  while (n) {
    int ret = 0;
    for (int a = 1; a * a <= n; a++) {
      if (n - a * a == 0) {
        ret++;
      } else {
        for (int b = a; a * a + b * b <= n; b++) {
          if (n - a * a - b * b == 0) {
            ret++;
          } else {
            for (int c = b; a * a + b * b + c * c <= n; c++) {
              if (n - a * a - b * b - c * c == 0) {
                ret++;
              } else {
                for (int d = c; a * a + b * b + c * c + d * d <= n; d++) {
                  if (n - a * a - b * b - c * c - d * d == 0) {
                    ret++;
                  }
                }
              }
            }
          }
        }
      }
    }
    cout << ret << endl;
    cin >> n;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define INF 1001000100010001000
#define MOD 1000000007
#define EPS 1e-10
#define int long long
#define rep(i, N) for (int i = 0; i < N; i++)
#define Rep(i, N) for (int i = 1; i < N; i++)
#define For(i, a, b) for (int i = (a); i < (b); i++)
#define pb push_back
#define mp make_pair
#define i_i pair<int, int>
#define vi vector<int>
#define vvi vector<vi>
#define vb vector<bool>
#define vvb vector<vb>
#define vp vector<i_i>
#define all(a) (a).begin(), (a).end()
#define Int(x)                                                                 \\
  int x;                                                                       \\
  scanf("%lld", &x);

// int dxy[5] = {0, 1, 0, -1, 0};
//  assign

vvi memo(5, vi(32768, 0));

int solve(int n, int i, int cnt);

signed main() {
  Int(n);
  while (n) {
    cout << solve(n, 1, 0) << endl;
    cin >> n;
  }
  return 0;
}

int solve(int n, int i, int cnt) {
  if (cnt > 4) {
    return 0;
  } else if (!n) {
    return 1;
  } else {
    int ans = 0;
    if (memo[cnt][n]) {
      return memo[cnt][n];
    } else {
      while (i * i <= n) {
        ans += solve(n - i * i, i, cnt + 1);
        i++;
      }
      memo[cnt][n] = ans;
      return ans;
    }
  }
}
Problem p00822 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cstdio>
const int MAXN = 366;

const int move[] = {0, 1, 2, -1, -2, 4, 8, -4, -8};
int Day;
int rain[MAXN + 10][20];
int vis[MAXN][12][7][7][7][7], T;

int dfs(int D, int pos, int n1, int n4, int n13, int n16) {
  if (pos == 1)
    n1 = 0; // 1  2  3  4
  if (pos == 3)
    n4 = 0; // 5  6  7  8
  if (pos == 9)
    n13 = 0; // 9  10 11 12
  if (pos == 11)
    n16 = 0; // 13 14 15 16
  if (n1 >= 7 || n4 >= 7 || n13 >= 7 || n16 >= 7)
    return 0;
  if (D == Day + 1)
    return 1;
  if (rain[D][pos])
    return 0;
  if (rain[D][pos + 1])
    return 0;
  if (rain[D][pos + 4])
    return 0;
  if (rain[D][pos + 5])
    return 0;
  if (vis[D][pos][n1][n4][n13][n16] == T)
    return 0;
  vis[D][pos][n1][n4][n13][n16] = T;
  for (int i = 0; i < 9; i++) {
    int npos = pos + move[i];
    if (npos % 4 != (pos % 4) + (move[i] % 4))
      continue;
    if (npos < 1 || 11 < npos || npos == 4 || npos == 8)
      continue;
    if (dfs(D + 1, npos, n1 + 1, n4 + 1, n13 + 1, n16 + 1))
      return 1;
  }
  return 0;
}

int main() {
  while (scanf("%d", &Day)) {
    if (Day == 0)
      break;
    for (int i = 1; i <= Day; i++) {
      for (int j = 1; j <= 16; j++) {
        scanf("%d", &rain[i][j]);
      }
    }
    T++;
    printf("%d\\n", dfs(1, 6, 1, 1, 1, 1));
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <cstdio>
const int MAXN = 366;

const int dir[] = {1, -1, 4, -4};
int Day;
int rain[MAXN + 10][20];
int vis[MAXN][20][7][7][7][7], T;

int dfs(int D, int pos, int n1, int n4, int n13, int n16) {
  if (pos < 1 || pos > 11 || pos % 4 == 0)
    return 0;
  if (pos == 1)
    n1 = 0; // 1  2  3  4
  if (pos == 3)
    n4 = 0; // 5  6  7  8
  if (pos == 9)
    n13 = 0; // 9  10 11 12
  if (pos == 11)
    n16 = 0; // 13 14 15 16
  if (n1 > 6 || n4 > 6 || n13 > 6 || n16 > 6)
    return 0;
  if (rain[D][pos])
    return 0;
  if (rain[D][pos + 1])
    return 0;
  if (rain[D][pos + 4])
    return 0;
  if (rain[D][pos + 5])
    return 0;
  if (D == Day + 1)
    return 1;
  if (vis[D][pos][n1][n4][n13][n16] == T)
    return 0;
  vis[D][pos][n1][n4][n13][n16] = T;
  for (int i = 0; i < 4; i++) {
    int npos = pos;
    while (npos % 4 && 1 <= npos && npos <= 11) {
      if (dfs(D + 1, npos, n1 + 1, n4 + 1, n13 + 1, n16 + 1))
        return 1;
      npos += dir[i];
    }
  }
  return 0;
}

int main() {
  while (scanf("%d", &Day)) {
    if (Day == 0)
      break;
    for (int i = 1; i <= Day; i++) {
      for (int j = 1; j <= 16; j++) {
        scanf("%d", &rain[i][j]);
      }
    }
    T++;
    printf("%d\\n", dfs(1, 6, 0, 0, 0, 0));
  }
  return 0;
}
Problem p00822 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <set>

using namespace std;

struct S {
  int g[4][4];
  int y, x;
  bool operator<(S s) const {
    return lexicographical_compare(g[0], g[4], s.g[0], s.g[4]);
  }
};

int main() {
  for (int N; cin >> N, N;) {
    set<S> d[366];
    S s{{}, 1, 1};
    d[0].insert(s);
    for (int i = 0; i < N; i++) {
      int s[16];
      for (int j = 0; j < 16; j++) {
        cin >> s[j];
      }
      for (auto e : d[i]) {
        for (int k = 0; k < 3; k++) {
          for (int l = 0; l < 3; l++) {
            if (k != e.y && l != e.x || i == 0 && (k != 1 || l != 1))
              continue;
            auto cs = e;
            cs.y = k;
            cs.x = l;
            for (int m = 0; m < 4; m++) {
              for (int n = 0; n < 4; n++) {
                cs.g[m][n]++;
              }
            }
            for (int m = 0; m < 2; m++) {
              for (int n = 0; n < 2; n++) {
                int y = k + m;
                int x = l + n;
                cs.g[y][x] = 0;
                if (s[y * 4 + x])
                  goto next;
              }
            }
            if (*max_element(cs.g[0], cs.g[4]) >= 7)
              goto next;
            d[i + 1].insert(cs);
          next:;
          }
        }
      }
    }
    cout << !d[N].empty() << endl;
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

struct S {
  int g[4][4];
  bool r[4][4];
  int y, x;
};

int main() {
  for (int N; cin >> N, N;) {
    vector<S> d[366];
    d[0].push_back({{}, {}, 1, 1});
    for (int i = 0; i < N; i++) {
      int s[16];
      for (int j = 0; j < 16; j++) {
        cin >> s[j];
      }
      for (auto e : d[i]) {
        for (int k = 0; k < 3; k++) {
          for (int l = 0; l < 3; l++) {
            if (k != e.y && l != e.x)
              continue;
            auto cs = e;
            cs.y = k;
            cs.x = l;
            for (int m = 0; m < 4; m++) {
              for (int n = 0; n < 4; n++) {
                cs.g[m][n]++;
              }
            }
            for (int m = 0; m < 2; m++) {
              for (int n = 0; n < 2; n++) {
                int y = k + m;
                int x = l + n;
                cs.g[y][x] = 0;
                cs.r[y][x] = true;
                if (s[y * 4 + x])
                  goto next;
              }
            }
            if (*max_element(cs.g[0], cs.g[4]) >= 7)
              goto next;
            d[i + 1].push_back(cs);
          next:;
          }
        }
      }
      d[i].clear();
    }
    bool f = false;
    for (auto e : d[N]) {
      f |= count(e.r[0], e.r[4], true) == 16;
    }
    cout << f << endl;
  }
}
Problem p00822 (C++) — buggy submission, verdict: Runtime Error

#include <array>
#include <iostream>
#include <set>
#include <vector>

using namespace std;

struct S {
  int y, x;
  array<array<int, 4>, 4> b;
  bool operator<(S s) const {
    return (y != s.y) ? y < s.y : (x != s.x) ? x < s.x : b < s.b;
  }
};

int main() {
  for (int N; cin >> N, N;) {
    set<S> v[366];
    v[0].insert({1, 1, {}});
    for (int i = 0; i < N; i++) {
      int s[4][4];
      for (int j = 0; j < 4; j++) {
        for (int k = 0; k < 4; k++) {
          cin >> s[j][k];
        }
      }
      for (auto &e : v[i]) {
        for (int j = 0; j < 3; j++) {
          for (int k = 0; k < 3; k++) {
            if (i == 0 && (j != 1 || k != 1))
              continue;
            if (e.y != j && e.x != k)
              continue;
            auto n = e.b;
            bool fail = false;
            for (int l = 0; l < 2; l++) {
              for (int m = 0; m < 2; m++) {
                fail |= s[j + l][k + m];
                n[j + l][k + m] = -1;
              }
            }
            for (auto &f : n) {
              for (auto &g : f) {
                fail |= ++g >= 7;
              }
            }
            if (!fail) {
              v[i + 1].insert({j, k, n});
            }
          }
        }
      }
    }
    cout << !v[N].empty() << endl;
  }
}

Error: 0

Fixed submission:

#include <array>
#include <iostream>
#include <vector>

using namespace std;

struct S {
  int y, x;
  array<array<int, 4>, 4> b;
};

int main() {
  for (int N; cin >> N, N;) {
    vector<S> v[366];
    v[0].push_back({1, 1, {}});
    for (int i = 0; i < N; i++) {
      int s[4][4];
      for (int j = 0; j < 4; j++) {
        for (int k = 0; k < 4; k++) {
          cin >> s[j][k];
        }
      }
      for (auto &e : v[i]) {
        for (int j = 0; j < 3; j++) {
          for (int k = 0; k < 3; k++) {
            if (i == 0 && (j != 1 || k != 1))
              continue;
            if (e.y != j && e.x != k)
              continue;
            auto n = e.b;
            bool fail = false;
            for (int l = 0; l < 2; l++) {
              for (int m = 0; m < 2; m++) {
                fail |= s[j + l][k + m];
                n[j + l][k + m] = -1;
              }
            }
            for (auto &f : n) {
              for (auto &g : f) {
                fail |= ++g >= 7;
              }
            }
            if (!fail) {
              v[i + 1].push_back({j, k, n});
            }
          }
        }
      }
    }
    cout << !v[N].empty() << endl;
  }
}
Problem p00822 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <set>
#include <string>
#include <tuple>
#include <unordered_set>
#include <vector>
using namespace std;
#define int long long
set<tuple<int, int, int>> done;
int D;
int ds[365];

int dfs(int cl, int d, int cnt) {
  if (d < D && (cl & ds[d]))
    return 0;
  auto v = make_tuple(cl, d, cnt);
  if (done.count(v))
    return 0;
  done.insert(v);
  for (int i = 0; i < 16; i++) {
    int c = (cnt >> (i * 3)) & 7;
    cnt ^= c << (i * 3);
    if (cl & (1LL << i))
      c = 0;
    else
      c++;
    if (c == 7)
      return 0;
    cnt ^= c << (i * 3);
  }
  if (d == D)
    return 1;

  for (int i = 0; i < 2; i++) {
    if (dfs(cl, d + 1, cnt))
      return 1;
    if (!((4369 << i) & cl) && dfs(cl >> (i + 1), d + 1, cnt))
      return 1;
    if (!((34952 >> i) & cl) && dfs(cl << (i + 1), d + 1, cnt))
      return 1;
    if (!((15 << (i * 4)) & cl) && dfs(cl >> ((i + 1) * 4), d + 1, cnt))
      return 1;
    if (!((61440 >> (i * 4)) & cl) && dfs(cl << ((i + 1) * 4), d + 1, cnt))
      return 1;
  }
  return 0;
}

void solve() {
  for (int i = 0; i < D; i++) {
    ds[i] = 0;
    for (int j = 0; j < 16; j++) {
      int a;
      cin >> a;
      ds[i] |= a << j;
    }
  }
  done.clear();

  cout << dfs(1632, 0, 0) << endl;
}

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

  while (cin >> D, D) {
    solve();
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <set>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define int long long
set<tuple<int, int, int>> done;
int D;
int ds[365];

int dfs(int cl, int d, int cnt) {
  if (d < D && (cl & ds[d]))
    return 0;
  auto v = make_tuple(cl, d, cnt);
  if (done.count(v))
    return 0;
  for (int i = 0; i < 16; i++) {
    int c = (cnt >> (i * 3)) & 7;
    cnt ^= c << (i * 3);
    if (cl & (1LL << i))
      c = 0;
    else
      c++;
    if (c == 7)
      return 0;
    cnt ^= c << (i * 3);
  }
  if (d == D)
    return 1;

  for (int i = 0; i < 2; i++) {
    if (dfs(cl, d + 1, cnt))
      return 1;
    if (!((4369 << i) & cl) && dfs(cl >> (i + 1), d + 1, cnt))
      return 1;
    if (!((34952 >> i) & cl) && dfs(cl << (i + 1), d + 1, cnt))
      return 1;
    if (!((15 << (i * 4)) & cl) && dfs(cl >> ((i + 1) * 4), d + 1, cnt))
      return 1;
    if (!((61440 >> (i * 4)) & cl) && dfs(cl << ((i + 1) * 4), d + 1, cnt))
      return 1;
  }
  return 0;
}

void solve() {
  for (int i = 0; i < D; i++) {
    ds[i] = 0;
    for (int j = 0; j < 16; j++) {
      int a;
      cin >> a;
      ds[i] |= a << j;
    }
  }
  done.clear();

  cout << dfs(1632, 0, 0) << endl;
}

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

  while (cin >> D, D) {
    solve();
  }
}
Problem p00823 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <stack>
using std::stack;

char s[2000];
int Atoms[30 * 30];

int getAtom(int x) {
  int Atom = (s[x] - 'A' + 1) * 28;
  if ('a' <= s[x + 1] && s[x + 1] <= 'z') {
    Atom += (s[x + 1] - 'a' + 1);
  }
  return Atom;
}

int getNum(int x) {
  int Num = 1;
  if ('0' <= s[x] && s[x] <= '9') {
    Num = s[x] - '0';
  } else
    return Num;
  if ('0' <= s[x + 1] && s[x + 1] <= '9') {
    Num = Num * 10 + s[x + 1] - '0';
  }
  return Num;
}

int solve() {
  scanf("%s", s);
  if (s[0] == '0')
    return 0;
  stack<int> S;
  int tmp = 0;
  for (int i = 0; s[i] != '\\0'; i++) {
    if (s[i] == '(') {
      S.push(tmp);
      tmp = 0;
    }
    if (s[i] == ')') {
      int Num = getNum(i + 1);
      tmp *= Num;
      tmp += S.top();
      S.pop();
    }
    if ('A' <= s[i] && s[i] <= 'Z') {
      int Atom = getAtom(i);
      if (Atoms[Atom] == 0) {
        puts("UNKNOWN");
        return 1;
      }
      if ('a' <= s[i + 1] && s[i + 1] <= 'z')
        i++;
      int Num = getNum(i + 1);
      tmp += Atoms[Atom] * Num;
    }
  }
  printf("%d\\n", tmp);
  return 1;
}

int main() {
  while (true) {
    int val;
    scanf("%s", s);
    if (s[0] == 'E' && s[1] == 'N' && s[2] == 'D') {
      break;
    }
    scanf("%d", &val);
    Atoms[getAtom(0)] = val;
  }
  // puts("READ DONE");
  while (solve())
    ;
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <stack>
using std::stack;

int Atoms[30 * 30];

int getAtom(char s[]) {
  int Atom = (s[0] - 'A') * 27;
  if ('a' <= s[1] && s[1] <= 'z') {
    Atom += (s[1] - 'a');
  }
  return Atom;
}

int getNum(char s[]) {
  int Num = 1;
  if ('0' <= s[0] && s[0] <= '9') {
    Num = s[0] - '0';
  } else
    return Num;
  if ('0' <= s[1] && s[1] <= '9') {
    Num = Num * 10 + s[1] - '0';
  }
  return Num;
}

int solve() {
  char s[2000];
  scanf("%s", s);
  if (s[0] == 'O' && s[1] == 'F' && s[2] == '\\0')
    while (1)
      ;
  if (s[0] == '0')
    return 0;
  stack<int> S;
  S.push(0);
  int tmp = 0;
  for (int i = 0; s[i] != '\\0'; i++) {
    if (s[i] == '(') {
      S.push(tmp);
      tmp = 0;
    }
    if (s[i] == ')') {
      int Num = getNum(s + i + 1);
      tmp *= Num;
      tmp += S.top();
      S.pop();
    }
    if ('A' <= s[i] && s[i] <= 'Z') {
      int Atom = getAtom(s + i);
      if (Atoms[Atom] == 0) {
        puts("UNKNOWN");
        return 1;
      }
      if ('a' <= s[i + 1] && s[i + 1] <= 'z')
        i++;
      int Num = getNum(s + i + 1);
      tmp += Atoms[Atom] * Num;
    }
  }
  printf("%d\\n", S.top() + tmp);
  return 1;
}

int main() {
  while (true) {
    char s[2000];
    int val;
    scanf("%s", s);
    if (s[1] == 'N')
      break;
    scanf("%d", &val);
    Atoms[getAtom(s)] = val;
  }
  // puts("READ DONE");
  while (solve())
    ;
  return 0;
}
Problem p00823 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cctype>
#include <iostream>
#include <map>
#include <string>
using namespace std;

bool err;
int p;
string formula;
map<string, int> dic;

int parseAtom() {
  string name;

  name += formula[p++];
  if (islower(formula[p]))
    name += formula[p++];

  int res = 0;
  if (!dic.count(name))
    err = true;
  else
    res = dic[name];

  return res;
}

int parseInt() {
  int res = 0;
  while (isdigit(formula[p])) {
    res *= 10;
    res += formula[p] - '0';
    p++;
  }

  return res;
}

int parseMolecule() {
  int res = 0;
  while (formula[p] == '(' || isupper(formula[p])) {
    int val = 0;
    if (formula[p] == '(') {
      p++;
      val = parseMolecule();
      p++;
    } else {
      val = parseAtom();
    }

    int mul = max(1, parseInt());
    res += val * mul;
  }

  return res;
}

int solve() {
  p = 0;
  err = false;

  formula += '$';

  int res = 0;
  while (formula[p] != '$') {
    res += parseMolecule();
  }

  if (err)
    res = -1;

  return res;
}

int main() {
  dic.clear();
  string str;
  int w;

  while (1) {
    cin >> str;
    if (str == "END_OF_FIRST_PART")
      break;

    cin >> w;
    dic[str] = w;
  }

  while (cin >> formula, formula != "0") {
    int res = solve();
    if (res == -1)
      cout << "UNKNOWN" << endl;
    else
      cout << res << endl;
  }
}

Error: TLE

Fixed submission:

#include <cctype>
#include <iostream>
#include <map>
#include <string>
using namespace std;

bool err;
int p;
string formula;
map<string, int> dic;

int parseAtom() {
  string name;

  name += formula[p++];
  if (islower(formula[p]))
    name += formula[p++];

  int res = 0;
  if (!dic.count(name))
    err = true;
  else
    res = dic[name];

  return res;
}

int parseInt() {
  int res = 0;
  while (isdigit(formula[p])) {
    res *= 10;
    res += formula[p] - '0';
    p++;
  }

  return res;
}

int parseMolecule() {
  int res = 0;
  if (formula[p] == '(') {
    p++;
    int tmp = parseMolecule();
    p++;

    int mul = max(1, parseInt());
    res += tmp * mul;
  }

  while (isupper(formula[p])) {
    int weight = parseAtom();
    int mul = max(1, parseInt());

    res += weight * mul;
  }

  return res;
}

int solve() {
  p = 0;
  err = false;

  formula += '$';

  int res = 0;
  while (formula[p] != '$') {
    res += parseMolecule();
  }

  if (err)
    res = -1;

  return res;
}

int main() {
  dic.clear();
  string str;
  int w;

  while (1) {
    cin >> str;
    if (str == "END_OF_FIRST_PART")
      break;

    cin >> w;
    dic[str] = w;
  }

  while (cin >> formula, formula != "0") {
    int res = solve();
    if (res == -1)
      cout << "UNKNOWN" << endl;
    else
      cout << res << endl;
  }
}
Problem p00825 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

struct data {
  int l, r, c;
  bool operator<(const data &d) const { return r == d.r ? c > d.c : r < d.r; }
};

const int N = 1000;
const int M = 365;
data v[N];
int dp[2][M + 1][M + 1];
int n;

void solve() {
  fill(dp[0][0], dp[2][0], 0);
  sort(v, v + n);

  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= M; j++) {
      for (int k = 0; k <= M; k++) {
        dp[0][j][k] = dp[1][j][k];
      }
    }

    for (int j = v[i].r + 1; j <= M; j++) {
      for (int k = 0; k <= M; k++) {
        dp[1][j][k] = max(dp[1][j][k], dp[0][v[i].l][k] + v[i].c);
        dp[1][k][j] = max(dp[1][k][j], dp[0][k][v[i].l] + v[i].c);
      }
    }
  }

  cout << dp[1][M][M] << endl;
}

int main() {
  while (cin >> n && n) {
    for (int i = 0; i < n; i++) {
      cin >> v[i].l >> v[i].r >> v[i].c;
      v[i].l--;
      v[i].r--;
    }
    solve();
  }
}

Error: MLE

Fixed submission:

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

struct data {
  int l, r, c;
  bool operator<(const data &d) const { return r == d.r ? c > d.c : r < d.r; }
};

const int N = 1000;
const int M = 365;
data v[N];
int dp[N + 1][M + 1][M + 1];
int n;

void solve() {
  fill(dp[0][0], dp[n + 1][0], 0);
  sort(v, v + n);

  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= M; j++) {
      for (int k = 0; k <= M; k++) {
        dp[i + 1][j][k] = dp[i][j][k];
      }
    }

    for (int j = v[i].r + 1; j <= M; j++) {
      for (int k = 0; k <= M; k++) {
        dp[i + 1][j][k] = max(dp[i + 1][j][k], dp[i][v[i].l][k] + v[i].c);
        dp[i + 1][k][j] = max(dp[i + 1][k][j], dp[i][k][v[i].l] + v[i].c);
      }
    }
  }

  cout << dp[n][M][M] << endl;
}

int main() {
  while (cin >> n && n) {
    for (int i = 0; i < n; i++) {
      cin >> v[i].l >> v[i].r >> v[i].c;
      v[i].l--;
      v[i].r--;
    }
    solve();
  }
}
Problem p00825 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
using std::max;
using std::sort;

int dp[400][400];
struct apply {
  int a, b, c;
} applys[10000];
bool comp(apply a, apply b) { return a.a < b.a; }

int main() {
  int N;
  while (scanf("%d", &N) && N) {
    for (int i = 0; i <= 366; i++)
      for (int j = 0; j <= 366; j++)
        dp[i][j] = 0;
    for (int i = 0; i < N; i++) {
      scanf("%d %d %d", &applys[i].a, &applys[i].b, &applys[i].c);
    }
    sort(applys, applys + N, comp);
    int ptr = 0;
    for (int i = 0; i < 366; i++) {
      while (ptr < N && applys[ptr].a == i) {
        int k = applys[ptr].b + 1;
        int c = applys[ptr].c;
        for (int j = 366; j >= i; j--) {
          dp[k][j] = max(dp[k][j], dp[i][j] + c);
          dp[j][k] = max(dp[j][k], dp[j][i] + c);
        }
        ptr++;
      }
      for (int j = i; j <= 366; j++) {
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
        dp[j][i + 1] = max(dp[j][i + 1], dp[j][i]);
      }
      dp[i + 1][i + 1] = max(dp[i + 1][i + 1], dp[i][i + 1]);
    }
    printf("%d\\n", dp[366][366]);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
using std::max;
using std::sort;

int dp[366][366];
struct apply {
  int a, b, c;
} applys[10000];
bool comp(apply a, apply b) { return a.a < b.a; }

int main() {
  int N;
  while (scanf("%d", &N) && N) {
    for (int i = 0; i <= 365; i++)
      for (int j = 0; j <= 365; j++)
        dp[i][j] = 0;
    for (int i = 0; i < N; i++) {
      scanf("%d %d %d", &applys[i].a, &applys[i].b, &applys[i].c);
    }
    sort(applys, applys + N, comp);
    int ptr = 0;
    for (int i = 0; i < 365; i++) {
      dp[i][i] = max(dp[i][i], dp[i - 1][i - 1]);
      while (ptr < N && applys[ptr].a == i) {
        int k = applys[ptr].b + 1;
        int c = applys[ptr].c;
        for (int j = 365; j >= i; j--) {
          dp[k][j] = max(dp[k][j], dp[i][j] + c);
          dp[j][k] = max(dp[j][k], dp[j][i] + c);
        }
        ptr++;
      }
      for (int j = i; j <= 365; j++) {
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
        dp[j][i + 1] = max(dp[j][i + 1], dp[j][i]);
      }
    }
    printf("%d\\n", dp[365][365]);
  }
  return 0;
}