alexjercan/bugnet huggingface dataset:
Problem p00557 (C++) — buggy submission, verdict: Runtime Error

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

using Point = pair<int, int>;

const int INF = 1e6 + 1;
int H, W;
vector<vector<int>> height, way;

vector<Point> next(int y, int x) {
  return {{y - 1, x}, {y + 1, x}, {y, x - 1}, {y, x + 1}};
}

bool bottom(int y, int x) {
  for (auto p : next(y, x))
    if (height[p.first][p.second] < height[y][x])
      return false;
  return true;
}

void mark(int sy, int sx) {
  queue<Point> q;
  q.emplace(sy, sx);
  vector<vector<bool>> visited(H + 2, vector<bool>(W + 2, false));
  while (!q.empty()) {
    int y, x;
    tie(y, x) = q.front();
    q.pop();
    if (height[y][x] == INF)
      continue;
    if (visited[y][x])
      continue;
    visited[y][x] = true;
    if (2 <= way[y][x])
      continue;
    ++way[y][x];
    for (auto p : next(y, x))
      if (height[y][x] < height[p.first][p.second])
        if (!visited[p.first][p.second])
          q.emplace(p.first, p.second);
  }
}

int main() {
  cin >> H >> W;
  height.assign(H + 2, vector<int>(W + 2, INF));
  for (auto i = 1; i <= H; ++i)
    for (auto j = 1; j <= W; ++j)
      cin >> height[i][j];

  way.assign(H + 2, vector<int>(W + 2, 0));
  for (auto i = 1; i <= H; ++i)
    for (auto j = 1; j <= W; ++j)
      if (bottom(i, j)) {
        mark(i, j);
      }

  auto ans = 0;
  for (auto i = 1; i <= H; ++i)
    for (auto j = 1; j <= W; ++j)
      if (2 <= way[i][j])
        ++ans;
  cout << ans << endl;
}

Error: 0

Fixed submission:

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

const int INF = 1e6 + 1;
vector<vector<int>> height, way;
vector<vector<bool>> visited;

vector<pair<int, int>> next(int y, int x) {
  return {{y - 1, x}, {y + 1, x}, {y, x - 1}, {y, x + 1}};
}

bool bottom(int y, int x) {
  for (auto p : next(y, x))
    if (height[p.first][p.second] < height[y][x])
      return false;
  return true;
}

void mark(int y, int x) {
  if (visited[y][x])
    return;
  visited[y][x] = true;
  if (height[y][x] == INF)
    return;
  if (2 <= way[y][x])
    return;
  ++way[y][x];
  for (auto p : next(y, x))
    if (height[y][x] < height[p.first][p.second])
      mark(p.first, p.second);
}

int main() {
  int H, W;
  cin >> H >> W;
  height.assign(H + 2, vector<int>(W + 2, INF));
  for (auto i = 1; i <= H; ++i)
    for (auto j = 1; j <= W; ++j)
      cin >> height[i][j];

  way.assign(H + 2, vector<int>(W + 2, 0));
  for (auto i = 1; i <= H; ++i)
    for (auto j = 1; j <= W; ++j)
      if (bottom(i, j)) {
        visited.assign(H + 2, vector<bool>(W + 2, false));
        mark(i, j);
      }

  auto ans = 0;
  for (auto i = 1; i <= H; ++i)
    for (auto j = 1; j <= W; ++j)
      if (2 <= way[i][j])
        ++ans;
  cout << ans << endl;
}
Problem p00557 (C++) — buggy submission, verdict: 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(i, 0, n)
#define pb emplace_back
typedef long long ll;
typedef pair<int, int> pint;

int dx[] = {-1, 0, 0, 1}, dy[] = {0, -1, 1, 0};
int a[1001][1001];
pint p[1000002];
int b[1000002], c[1000002];
int main() {
  int h, w;
  cin >> h >> w;
  rep(i, h) rep(j, w) {
    cin >> a[i][j];
    p[a[i][j]] = {j, i};
  }
  int ans = 0;
  rep(l, h * w) {
    int j = p[l + 1].first, i = p[l + 1].second;
    int cnt = 0;
    rep(k, 4) {
      int ty = i + dy[k], tx = j + dx[k];
      if (ty >= 0 && tx >= 0 && ty < h && tx < w && a[ty][tx] < a[i][j]) {
        if (b[a[ty][tx]] == -1) {
          if (b[l + 1] == 0)
            c[l + 1] = a[ty][tx], b[l + 1] = 1;
          else if (b[l + 1] == 1 && c[l + 1] != a[ty][tx]) {
            ++ans, b[l + 1] = 2;
            cnt = 4;
            break;
          }
        } else if (b[a[ty][tx]] == 1) {
          if (b[l + 1] == 0) {
            c[l + 1] = c[a[ty][tx]];
            b[l + 1] = 1;
          } else {
            if (c[l + 1] != c[a[ty][tx]]) {
              b[l + 1] = 2;
              ++ans;
              cnt = 4;
              break;
            }
          }
        } else if (b[a[ty][tx]] == 2) {
          b[l + 1] = 2;
          ++ans;
          cnt = 4;
          break;
        }
      }
      if (ty >= 0 && tx >= 0 && ty < h && tx < w && a[ty][tx] < a[i][j])
        ++cnt;
    }
    if (cnt == 0)
      b[l + 1] = -1;
  }
  cout << ans << endl;
  return 0;
}


Error: 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(i, 0, n)
#define pb emplace_back
typedef long long ll;
typedef pair<int, int> pint;

int dx[] = {-1, 0, 0, 1}, dy[] = {0, -1, 1, 0};
int a[1001][1001];
pint p[100002];
int b[100001], c[100001];
int main() {
  int h, w;
  cin >> h >> w;
  rep(i, h) rep(j, w) {
    cin >> a[i][j];
    p[a[i][j]] = {j, i};
  }
  int ans = 0;
  rep(l, h * w) {
    int j = p[l + 1].first, i = p[l + 1].second;
    int cnt = 0;
    rep(k, 4) {
      int ty = i + dy[k], tx = j + dx[k];
      if (ty >= 0 && tx >= 0 && ty < h && tx < w && a[ty][tx] < a[i][j]) {
        if (b[a[ty][tx]] == -1) {
          if (b[l + 1] == 0)
            c[l + 1] = a[ty][tx], b[l + 1] = 1;
          else if (b[l + 1] == 1 && c[l + 1] != a[ty][tx]) {
            ++ans, b[l + 1] = 2;
            cnt = 4;
            break;
          }
        } else if (b[a[ty][tx]] == 1) {
          if (b[l + 1] == 0) {
            c[l + 1] = c[a[ty][tx]];
            b[l + 1] = 1;
          } else {
            if (c[l + 1] != c[a[ty][tx]]) {
              b[l + 1] = 2;
              ++ans;
              cnt = 4;
              break;
            }
          }
        } else if (b[a[ty][tx]] == 2) {
          b[l + 1] = 2;
          ++ans;
          cnt = 4;
          break;
        }
      }
      if (ty >= 0 && tx >= 0 && ty < h && tx < w && a[ty][tx] < a[i][j])
        ++cnt;
    }
    if (cnt == 0)
      b[l + 1] = -1;
  }
  cout << ans << endl;
  return 0;
}
Problem p00557 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define dmp make_pair
typedef pair<int, int> P;
typedef pair<int, P> PP;
const int inf = 1e9;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = 1; i <= n; i++)

int a[1002][1002], b[1002][1002];
int dy[] = {-1, 0, 0, 1}, dx[] = {0, -1, 1, 0};
priority_queue<PP, vector<PP>, greater<PP>> q;
int main() {
  int h, w, ans = 0;
  scanf("%d%d", &h, &w);
  fill((int *)a, (int *)(a + 1002), inf);
  rrep(i, h) rrep(j, w) scanf("%d", &a[i][j]);
  rrep(y, h) rrep(x, w) {
    int f = 0;
    rep(i, 4) if (a[y][x] < a[y + dy[i]][x + dx[i]]) f++;
    if (f == 4)
      b[y][x] = a[y][x];
    q.push(dmp(a[y][x], P(y, x)));
  }

  while (!q.empty()) {
    PP p = q.top();
    q.pop();
    int y = p.se.fi, x = p.se.se;
    if (b[y][x] == -1)
      ans++;
    for (int i = 0; i < 4; i++) {
      int yy = y + dy[i], xx = x + dx[i];
      int f = (a[y][x] < a[yy][xx]);
      if (f && b[yy][xx] == 0) {
        b[yy][xx] = b[y][x];
      } else if (f && b[y][x] != b[yy][xx] && b[yy][xx] != -1) {
        b[yy][xx] = -1;
      }
    }
  }

  printf("%d\\n", ans);
  return 0;
}


Error: 0

Fixed submission:

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

int a[1002][1002], b[20004];
vector<int> G[1002];
int dy[4] = {-1, 0, 0, 1}, dx[4] = {0, -1, 1, 0};
queue<int> q;
int main() {
  int h, w, ans = 0;
  scanf("%d%d", &h, &w);
  rrep(i, h) rrep(j, w) scanf("%d", &a[i][j]);
  rrep(y, h) rrep(x, w) {
    int f = 0;
    for (int i = 0; i < 4; i++) {
      if (a[y][x] < a[y + dy[i]][x + dx[i]]) {
        G[a[y][x]].push_back(a[y + dy[i]][x + dx[i]]);
        f++;
      }
      if (a[y + dy[i]][x + dx[i]] == 0)
        f++;
    }
    if (f == 4)
      q.push(a[y][x]);
  }

  while (!q.empty()) {
    int p = q.front();
    q.pop();
    if (b[p] == 0)
      b[p] = p;
    for (int i = 0; i < G[p].size(); i++) {
      int t = G[p][i];
      if (b[t] == 0) {
        b[t] = b[p];
        q.push(t);
      } else if (b[t] != b[p] && b[t] != -1) {
        b[t] = -1;
        ans++;
      }
    }
  }

  printf("%d\\n", ans);
  return 0;
}
Problem p00557 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

int H, W;
int M[1000][1000];
int reach[1000][1000];
int x[1000000], y[1000000];
const int vy[] = {0, 1, 0, -1}, vx[] = {1, 0, -1, 0};

int main() {
  scanf("%d %d", &H, &W);
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      scanf("%d", &M[i][j]);
      --M[i][j];
      x[M[i][j]] = j;
      y[M[i][j]] = i;
    }
  }
  int ret = 0;
  for (int i = 0; i < H * W; i++) {
    int proc = i;
    for (int j = 0; j < 4; j++) {
      int nx = x[i] + vx[j], ny = y[i] + vy[j];
      if (nx < 0 || ny < 0 || nx >= W || ny >= H)
        continue;
      if (M[ny][nx] < i) {
        if (proc == i)
          proc = reach[ny][nx];
        else if (proc != reach[ny][nx])
          proc = -1;
      }
    }
    reach[y[i]][x[i]] = proc;
    ret += proc == -1;
  }

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

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int H, W;
int M[1000][1000];
int reach[1000][1000];
int x[100000], y[100000];
const int vy[] = {0, 1, 0, -1}, vx[] = {1, 0, -1, 0};

int main() {
  scanf("%d %d", &H, &W);
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      scanf("%d", &M[i][j]);
      --M[i][j];
      x[M[i][j]] = j;
      y[M[i][j]] = i;
    }
  }
  int ret = 0;
  for (int i = 0; i < H * W; i++) {
    int proc = i;
    for (int j = 0; j < 4; j++) {
      int nx = x[i] + vx[j], ny = y[i] + vy[j];
      if (nx < 0 || ny < 0 || nx >= W || ny >= H)
        continue;
      if (M[ny][nx] < i) {
        if (proc == i)
          proc = reach[ny][nx];
        else if (proc != reach[ny][nx])
          proc = -1;
      }
    }
    reach[y[i]][x[i]] = proc;
    ret += proc == -1;
  }

  printf("%d\\n", ret);
}
Problem p00557 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define P pair<int, int>
using namespace std;

int h[1000000];
int ANS = 0;
int a, b, c[1002][1002], x[4] = {-1, 0, 0, 1}, y[4] = {0, -1, 1, 0};
P d[1000000];
int main() {
  memset(h, -1, sizeof(h));
  for (int i = 0; i < 1002; i++) {
    for (int j = 0; j < 1002; j++) {
      c[i][j] = 1 << 29;
    }
  }
  cin >> a >> b;
  for (int e = 0; e < a; e++) {
    for (int f = 0; f < b; f++) {
      int g;
      scanf("%d", &g);
      g--;
      c[e + 1][f + 1] = g;
      d[g] = P(e + 1, f + 1);
    }
  }
  for (int i = 0; i < a * b; i++) {
    int dx = d[i].first, dy = d[i].second;
    bool k = false;
    int sum = 0;
    for (int j = 0; j < 4; j++) {
      if (c[dx + x[j]][dy + y[j]] < i) {
        sum++;
        if (h[c[dx + x[j]][dy + y[j]]] == -1) {
          k = true;
        } else {
          if (h[i] == -1) {
            h[i] = h[c[dx + x[j]][dy + y[j]]];
          } else if (h[i] != h[c[dx + x[j]][dy + y[j]]])
            k = true;
        }
      }
    }
    if (sum != 0) {
      if (k)
        h[i] = -1;
      else {
        ANS++;
      }
    } else {
      h[i] = i;
      ANS++;
    }
  }
  cout << a * b - ANS << endl;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define p pair<int, int>
#define P pair<int, p>
using namespace std;

int hyoukou[1002][1002];
int x[4] = {-1, 0, 0, 1}, y[4] = {0, -1, 1, 0};
int main() {
  unordered_map<int, unordered_set<int>> U;
  memset(hyoukou, 1, sizeof(hyoukou));
  priority_queue<P, vector<P>, greater<P>> Q;
  int c, d;
  cin >> c >> d;
  for (int e = 0; e < c; e++) {
    for (int f = 0; f < d; f++) {
      int g;
      scanf("%d", &g);
      g--;
      hyoukou[e + 1][f + 1] = g;
      Q.push(P(g, p(e + 1, f + 1)));
    }
  }
  while (Q.size()) {
    P h = Q.top();
    Q.pop();
    for (int i = 0; i < 4; i++) {
      if (h.first > hyoukou[h.second.first + x[i]][h.second.second + y[i]]) {
        for (auto k = U[hyoukou[h.second.first + x[i]][h.second.second + y[i]]]
                          .begin();
             k !=
             U[hyoukou[h.second.first + x[i]][h.second.second + y[i]]].end();
             k++) {
          U[h.first].insert(*k);
        }
      }
    }
    if (U[h.first].size() == 0)
      U[h.first].insert(h.first);
  }
  int S = 0;
  for (int i = 1; i <= c; i++) {
    for (int h = 1; h <= d; h++) {
      if (U[hyoukou[i][h]].size() != 1)
        S++;
    }
  }
  cout << S << endl;
}
Problem p00557 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 28;
int H, W;
int dp[1000005]; // 0 ?°????, 1??\\??? ???????????????
int X[1000005], Y[1000005];
int mas[1005][1005];
int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};

int main() {
  int ans = 0;
  fill_n(*mas, 1005 * 1005, INF);
  cin >> H >> W;
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      cin >> mas[i][j];
      Y[mas[i][j]] = i;
      X[mas[i][j]] = j;
    }
  }

  for (int i = 1; i <= H * W; i++) {
    int c = -1;
    for (int j = 0; j < 4; j++) {
      int ny = Y[i] + dy[j], nx = X[i] + dx[j];
      if (mas[ny][nx] < i && c != 0) {
        if (c == -1)
          c = dp[mas[ny][nx]];
        else if (c != dp[mas[ny][nx]])
          c = 0;
      }
    }
    if (c == -1)
      dp[i] = i;
    else
      dp[i] = c;
    ans += dp[i] == 0;
  }

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

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 28;
int H, W;
int memo[1005][1005];
int mas[1005][1005];
int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};

int dp(int y, int x) {
  int c = -1;
  if (memo[y][x] != -1)
    return (memo[y][x]);
  for (int i = 0; i < 4; i++) {
    int ny = y + dy[i], nx = x + dx[i];
    if (mas[ny][nx] < mas[y][x] && c != 0) {
      int val = dp(ny, nx);
      if (c == -1)
        c = val;
      else if (c != val)
        c = 0;
    }
  }
  if (c == -1)
    return (memo[y][x] = (y - 1) * 1000 + (x - 1) + 1);
  else
    return (memo[y][x] = c);
}

int main() {
  int ans = 0;
  fill_n(*mas, 1005 * 1005, INF);
  memset(memo, -1, sizeof(memo));
  cin >> H >> W;
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      cin >> mas[i][j];
    }
  }
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      ans += dp(i, j) == 0;
    }
  }
  cout << ans << endl;
  return (0);
}
Problem p00558 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
struct st {
  int p, d, c, h;
  st() {}
  st(int p, int d, int c, int h) : p(p), d(d), c(c), h(h) {}
  bool operator<(const st &a) const { return d > a.d; }
};
struct edge {
  int to, cost;
  edge() {}
  edge(int to, int cost) : to(to), cost(cost) {}
};
vector<edge> G[10001];
short dp[10001][201][201];
int main() {
  int n, m, x;
  cin >> n >> m >> x;
  int s[n];
  for (int i = 0; i < n; i++)
    cin >> s[i];
  for (int i = 0; i < m; i++) {
    int a, b, d;
    cin >> a >> b >> d;
    a--;
    b--;
    G[a].push_back(edge(b, d));
    G[b].push_back(edge(a, d));
  }
  memset(dp, -1, sizeof(dp));
  priority_queue<st> q;
  q.push(st(0, 0, x, 0));
  int ans = 0;
  while (!q.empty()) {
    st t = q.top();
    q.pop();
    if (~dp[t.p][t.c][t.h] && dp[t.p][t.c][t.h] <= t.d)
      continue;
    dp[t.p][t.c][t.h] = t.d;
    if (t.p == n - 1) {
      ans = t.d;
      break;
    }
    for (int i = 0; i < (int)G[t.p].size(); i++) {
      int v = G[t.p][i].to, u = G[t.p][i].cost;
      int c = max(0, t.c - u), h = max(0, t.h - u);
      if (~dp[v][c][h] && dp[v][c][h] <= t.d + u)
        continue;
      if (s[v] == 0) {
        if (h > 0)
          continue;
        q.push(st(v, t.d + u, x, h));
      }
      if (s[v] == 1) {
        q.push(st(v, t.d + u, c, h));
      }
      if (s[v] == 2) {
        if (c > 0)
          continue;
        q.push(st(v, t.d + u, c, x));
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
struct st {
  int p, d, c, h;
  st() {}
  st(int p, int d, int c, int h) : p(p), d(d), c(c), h(h) {}
  bool operator<(const st &a) const { return d > a.d; }
};
struct edge {
  int to, cost;
  edge() {}
  edge(int to, int cost) : to(to), cost(cost) {}
};
vector<edge> G[10001];
int dp[10001][201][201];
int main() {
  int n, m, x;
  cin >> n >> m >> x;
  int s[n];
  for (int i = 0; i < n; i++)
    cin >> s[i];
  for (int i = 0; i < m; i++) {
    int a, b, d;
    cin >> a >> b >> d;
    a--;
    b--;
    G[a].push_back(edge(b, d));
    G[b].push_back(edge(a, d));
  }
  memset(dp, -1, sizeof(dp));
  priority_queue<st> q;
  q.push(st(0, 0, x, 0));
  int ans = 0;
  while (!q.empty()) {
    st t = q.top();
    q.pop();
    if (~dp[t.p][t.c][t.h] && dp[t.p][t.c][t.h] <= t.d)
      continue;
    dp[t.p][t.c][t.h] = t.d;
    if (t.p == n - 1) {
      ans = t.d;
      break;
    }
    for (int i = 0; i < (int)G[t.p].size(); i++) {
      int v = G[t.p][i].to, u = G[t.p][i].cost;
      int c = max(0, t.c - u), h = max(0, t.h - u);
      if (~dp[v][c][h] && dp[v][c][h] <= t.d + u)
        continue;
      if (s[v] == 0) {
        if (h > 0)
          continue;
        q.push(st(v, t.d + u, x, h));
      }
      if (s[v] == 1) {
        q.push(st(v, t.d + u, c, h));
      }
      if (s[v] == 2) {
        if (c > 0)
          continue;
        q.push(st(v, t.d + u, c, x));
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p00558 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define EPS (1e-10)
#define rep(i, n) for (ll i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int t[10000];
vector<P> E[10000];
int d[10000][3][201];

struct st {
  int u, c, i, a;
};
bool operator<(st a, st b) { return a.c > b.c; }
int main() {
  int n, m, x;
  scanf("%d%d%d", &n, &m, &x);
  rep(i, n) scanf("%d", &t[i]);
  rep(i, m) {
    int a, b, d;
    scanf("%d%d%d", &a, &b, &d);
    a--;
    b--;
    E[a].push_back(P(d, b));
    E[b].push_back(P(d, a));
  }
  priority_queue<st> que;
  memset(d, 0x3f, sizeof(d));
  d[0][0][0] = 0;
  que.push({0, 0, 0, 0});
  while (!que.empty()) {
    st s = que.top();
    que.pop();
    if (d[s.u][s.i][s.a] != s.c)
      continue;
    for (P v : E[s.u]) {
      int a = min(x, s.a + v.first), k = t[v.second];
      if (abs(s.i - t[v.second]) == 2) {
        if (a < x)
          continue;
      }
      if (k == 1)
        k = s.i;
      else
        a = 0;
      if (d[v.second][k][a] > s.c + v.first) {
        d[v.second][k][a] = s.c + v.first;
        que.push({v.second, d[v.second][k][a], k, a});
      }
    }
  }
  int Min = INF;
  rep(i, 3) rep(j, x + 1) { Min = min(Min, d[n - 1][i][j]); }
  printf("%d\\n", Min);
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define EPS (1e-10)
#define rep(i, n) for (ll i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int t[10000];
vector<P> E[10000];
int d[10000][201][201]; // cold,hot

struct st {
  int u, c, a, b;
};
bool operator<(st a, st b) { return a.c > b.c; }
int main() {
  int n, m, x;
  scanf("%d%d%d", &n, &m, &x);
  rep(i, n) scanf("%d", &t[i]);
  rep(i, m) {
    int a, b, d;
    scanf("%d%d%d", &a, &b, &d);
    a--;
    b--;
    E[a].push_back(P(d, b));
    E[b].push_back(P(d, a));
  }
  priority_queue<st> que;
  memset(d, 0x3f, sizeof(d));
  d[0][0][x] = 0;
  que.push({0, 0, 0, x});
  while (!que.empty()) {
    st s = que.top();
    que.pop();
    if (d[s.u][s.a][s.b] != s.c)
      continue;
    for (P v : E[s.u]) {
      int a = min(x, s.a + v.first), b = min(x, s.b + v.first);
      if (t[v.second] == 0) {
        if (b < x)
          continue;
        a = 0;
      }
      if (t[v.second] == 2) {
        if (a < x)
          continue;
        b = 0;
      }
      if (d[v.second][a][b] > s.c + v.first) {
        d[v.second][a][b] = s.c + v.first;
        que.push({v.second, d[v.second][a][b], a, b});
      }
    }
  }
  int Min = INF;
  rep(i, x + 1) rep(j, x + 1) { Min = min(Min, d[n - 1][i][j]); }
  printf("%d\\n", Min);
}
Problem p00558 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define rep(i, x) for (int i = 0; i < x; ++i)
#define fst first
#define scd second
#define PB push_back

using data = tuple<int, int, int>;
using P = pair<int, data>;

const int inf = 1e9;

struct edge {
  int to, cost;
};
vector<edge> G[10010];

int d[10010][2][205];

int n, m, x;
int t[10010];

signed main() {
  cin >> n >> m >> x;

  rep(i, n) {
    cin >> t[i];
    if (t[i] == 1)
      t[i] = 2;
    else if (t[i] == 2)
      t[i] = 1;
  }

  rep(i, m) {
    int a, b, d;
    cin >> a >> b >> d;
    --a, --b;
    G[a].PB(edge{b, d});
    G[b].PB(edge{a, d});
  }

  priority_queue<P, vector<P>, greater<P>> q;

  rep(i, n) rep(j, 2) rep(k, 205) d[i][j][k] = inf;
  d[0][0][0] = 0;

  q.push(P(0, data(0, 0, 0)));

  while (q.size()) {
    P p = q.top();
    q.pop();

    int dist = p.fst;
    int v, type, tim;
    tie(v, type, tim) = p.scd;

    if (d[v][type][tim] < dist)
      continue;

    if (v == n - 1) {
      cout << dist << endl;
      return 0;
    }

    for (edge &e : G[v]) {
      if (t[e.to] == 2) {
        int ntim = min(x, tim + e.cost);
        if (d[e.to][type][ntim] > d[v][type][tim] + e.cost) {
          d[e.to][type][ntim] = d[v][type][tim] + e.cost;
          q.push(P(d[e.to][type][ntim], data(e.to, type, ntim)));
        }
      } else {
        if (type != t[e.to] && tim + e.cost < x)
          continue;
        if (d[e.to][t[e.to]][0] > d[v][type][tim] + e.cost) {
          d[e.to][t[e.to]][0] = d[v][type][tim] + e.cost;
          q.push(P(d[e.to][t[e.to]][0], data(e.to, t[e.to], 0)));
        }
      }
    }
  }
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

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

const int inf = 1e9;

int sum[25][100010];
int cnt[25];
int dp[1 << 22];

signed main() {
  int n, m;
  cin >> n >> m;

  rep(i, n) {
    int a;
    cin >> a;
    --a;
    sum[a][i + 1]++;
    cnt[a]++;
  }

  rep(i, m) {
    rep(j, n) { sum[i][j + 1] += sum[i][j]; }
  }

  fill_n(dp, 1 << 22, inf);

  dp[0] = 0;

  rep(bit, 1 << m) {
    rep(i, m) {
      if (!(bit & (1 << i))) {
        int pos = 0;
        rep(j, m) if (bit & 1 << j) pos += cnt[j];

        int v = cnt[i] - sum[i][pos + cnt[i]] + sum[i][pos];

        dp[bit | 1 << i] = min(dp[bit | 1 << i], dp[bit] + v);
      }
    }
  }

  cout << dp[(1 << m) - 1] << endl;
}
Problem p00570 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <math.h>
using namespace std;
int main() {
  int temp, n, k, t[100000], m, s[100000];
  cin >> n >> k;
  m = n;
  for (int i = 0; i < n; i++) {
    cin >> t[i];
  }
  for (int i = 0; i < n - 1; i++) {
    s[i] = t[i + 1] - t[i] - 1;
  }

  sort(s, s + n - 1);

  for (int i = 0; i < n - k; i++) {
    m += s[i];
  }
  cout << m << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <math.h>
using namespace std;
int main() {
  int temp, n, k, t[100000], m, s[100000];
  cin >> n >> k;
  m = n;
  for (int i = 0; i < n; i++) {
    cin >> t[i];
  }
  for (int i = 0; i < n - 1; i++) {
    s[i] = t[i + 1] - t[i] - 1;
  }
  for (int i = 0; i < n - 2; i++) {
    for (int j = i + 1; j < n - 1; j++) {
      if (s[i] > s[j]) {
        temp = s[i];
        s[i] = s[j];
        s[j] = temp;
      }
    }
  }
  for (int i = 0; i < n - k; i++) {
    m += s[i];
  }
  cout << m << endl;
  return 0;
}