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

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

using namespace std;

#define pb push_back

int main() {

  int n, m;

  while (cin >> n >> m && (n | m)) {
    vector<int> data;
    vector<int> Q;

    n++;
    data.pb(0);
    for (int i = 1; i < n; i++) {
      int a;
      cin >> a;
      data.pb(a);
    }

    for (int i = 0; i < n; i++) {
      Q.pb(data[i]);
    }

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        Q.pb(data[i] + data[j]);
      }
    }

    sort(Q.begin(), Q.end());

    // for(int i=0; i<T; i++) cout << Q[i] << endl;

    int ans = 0;

    for (int i = 0; i < Q.size(); i++) {
      int M = m - Q[i];
      if (M < 0)
        break;

      int lb = 0, ub = Q.size();

      while (ub - lb > 1) {
        int mid = (lb + ub) / 2;

        if (Q[mid] <= M) {
          lb = mid;
        } else {
          ub = mid;
        }
      }
      ans = max(ans, Q[i] + Q[lb]);
    }

    cout << ans << endl;
  }

  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

int main() {

  int n, m;

  while (cin >> n >> m && (n | m)) {
    int data[1002];
    int Q[10000005];

    n++;
    data[0] = 0;
    for (int i = 1; i < n; i++) {
      cin >> data[i];
    }

    int T = 0;

    for (int i = 0; i < n; i++) {
      Q[T++] = data[i];
    }

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        Q[T++] = data[i] + data[j];
      }
    }

    sort(Q, Q + T);

    // for(int i=0; i<T; i++) cout << Q[i] << endl;

    int ans = 0;

    for (int i = 0; i < T; i++) {
      int M = m - Q[i];
      if (M < 0)
        break;

      int lb = 0, ub = T;

      while (ub - lb > 1) {
        int mid = (lb + ub) / 2;

        if (Q[mid] <= M) {
          lb = mid;
        } else {
          ub = mid;
        }
      }
      ans = max(ans, Q[i] + Q[lb]);
    }

    cout << ans << endl;
  }

  return 0;
}
Problem p00453 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define INF (1 << 30)
typedef pair<int, int> P;
int n, k;
vector<P> stone[200];
int dp[151][11][78];
int solve(int h, int idx, int k) {
  if (h == n - 1 || (h == n - 2 && k != 0))
    return 0;
  if (dp[h][idx][k] != -1)
    return dp[h][idx][k];
  int ret = INF;
  int s = stone[h][idx].second;
  int w = stone[h][idx].first;
  for (int i = 0; i < stone[h + 1].size(); i++) {
    int nw = stone[h + 1][i].first;
    int ns = stone[h + 1][i].second;
    ret = min(ret, solve(h + 1, i, k) + ((s + ns) * abs(w - nw)));
  }
  if (k != 0 && h + 2 <= n - 1) {
    for (int i = 0; i < stone[h + 2].size(); i++) {
      int nw = stone[h + 2][i].first;
      int ns = stone[h + 2][i].second;
      ret = min(ret, solve(h + 2, i, k - 1) + ((s + ns) * abs(w - nw)));
    }
  }
  return dp[h][idx][k] = ret;
}

int main() {
  while (cin >> n >> k, n) {
    memset(dp, -1, sizeof(dp));
    for (int i = 0; i < n; i++) {
      int m;
      cin >> m;
      for (int j = 0; j < m; j++) {
        int a, b;
        cin >> a >> b;
        stone[i].push_back(P(a, b));
      }
    }
    int ans = INF;
    for (int i = 0; i < stone[0].size(); i++) {
      ans = min(ans, solve(0, i, k));
    }
    if (k != 0) {
      for (int i = 0; i < stone[1].size(); i++) {
        ans = min(ans, solve(1, i, k - 1));
      }
    }
    cout << ans << endl;
    for (int i = 0; i < n; i++) {
      stone[i].clear();
    }
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define INF (1 << 24)
typedef pair<int, int> P;
int n, k;
vector<P> stone[200];
int dp[151][11][78];
int solve(int h, int idx, int k) {
  if (h == n - 1 || (h == n - 2 && k != 0))
    return 0;
  if (dp[h][idx][k] != INF)
    return dp[h][idx][k];
  int ret = INF;
  int s = stone[h][idx].second;
  int w = stone[h][idx].first;
  for (int i = 0; i < stone[h + 1].size(); i++) {
    int nw = stone[h + 1][i].first;
    int ns = stone[h + 1][i].second;
    ret = min(ret, solve(h + 1, i, k) + ((s + ns) * abs(w - nw)));
  }
  if (k != 0 && h + 2 <= n - 1) {
    for (int i = 0; i < stone[h + 2].size(); i++) {
      int nw = stone[h + 2][i].first;
      int ns = stone[h + 2][i].second;
      ret = min(ret, solve(h + 2, i, k - 1) + ((s + ns) * abs(w - nw)));
    }
  }
  return dp[h][idx][k] = ret;
}

int main() {
  while (cin >> n >> k, n) {
    for (int i = 0; i < 151; i++) {
      for (int j = 0; j < 11; j++) {
        for (int k = 0; k < 78; k++) {
          dp[i][j][k] = INF;
        }
      }
      stone[i].clear();
    }
    for (int i = 0; i < n; i++) {
      int m;
      cin >> m;
      for (int j = 0; j < m; j++) {
        int a, b;
        cin >> a >> b;
        stone[i].push_back(P(a, b));
      }
    }
    int ans = INF;
    for (int i = 0; i < stone[0].size(); i++) {
      ans = min(ans, solve(0, i, k));
    }
    if (k != 0) {
      for (int i = 0; i < stone[1].size(); i++) {
        ans = min(ans, solve(1, i, k - 1));
      }
    }
    cout << ans << endl;
  }
}
Problem p00453 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

// #define int long long
#define REP(i, n) for (int i = 0; i < n; i++)
#define INF 1000000000
struct S {
  int x, d;
};
int n, m;
int dp[152][1002][80];
vector<S> a[152];

signed main() {
  while (cin >> n >> m, n) {
    REP(i, n + 2) a[i].clear();
    REP(i, n) {
      int k, x, d;
      cin >> k;
      REP(j, k) {
        cin >> x >> d;
        a[i + 1].push_back({x, d});
      }
    }
    REP(i, n + 2) REP(j, 1000) REP(k, m + 1) dp[i][j + 1][k] = INF;
    REP(j, 1000) a[0].push_back({j + 1, 0});
    REP(j, 1000) a[n + 1].push_back({j + 1, 0});
    REP(j, 1000) dp[0][j + 1][0] = 0;
    REP(i, n + 2) {
      for (S s : a[i]) {
        for (S t : a[i + 1]) {
          REP(c, m + 1) {
            dp[i + 1][t.x][c] =
                min(dp[i + 1][t.x][c],
                    dp[i][s.x][c] + (s.d + t.d) * abs(s.x - t.x));
          }
        }
        if (i + 1 > n)
          continue;
        for (S u : a[i + 2]) {
          REP(c, m) {
            dp[i + 2][u.x][c + 1] =
                min(dp[i + 2][u.x][c + 1],
                    dp[i][s.x][c] + (s.d + u.d) * abs(s.x - u.x));
          }
        }
      }
    }
    int ans = INF;
    REP(j, 1000) REP(c, m + 1) ans = min(ans, dp[n + 1][j + 1][c]);
    cout << ans << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

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

#define int long long
#define REP(i, n) for (int i = 0; i < n; i++)
#define INF 10000000000000000
struct S {
  int x, d;
};
int n, m;
int dp[152][1002][80]; // n,x,m
vector<S> a[152];

signed main() {
  while (cin >> n >> m, n) {
    REP(i, n + 2) a[i].clear();
    REP(i, n) {
      int k, x, d;
      cin >> k;
      REP(j, k) {
        cin >> x >> d;
        a[i + 1].push_back({x, d});
      }
    }
    REP(i, n + 2) REP(j, 1000) REP(k, m + 1) dp[i][j + 1][k] = INF;
    REP(j, 1000) a[0].push_back({j + 1, 0});
    REP(j, 1000) a[n + 1].push_back({j + 1, 0});
    REP(j, 1000) dp[0][j + 1][0] = 0;
    REP(i, n + 2) {
      for (S s : a[i]) {
        for (S t : a[i + 1]) {
          REP(c, m + 1) {
            dp[i + 1][t.x][c] =
                min(dp[i + 1][t.x][c],
                    dp[i][s.x][c] + (s.d + t.d) * abs(s.x - t.x));
          }
        }
        if (i + 1 > n)
          continue;
        for (S u : a[i + 2]) {
          REP(c, m) {
            dp[i + 2][u.x][c + 1] =
                min(dp[i + 2][u.x][c + 1],
                    dp[i][s.x][c] + (s.d + u.d) * abs(s.x - u.x));
          }
        }
      }
    }
    int ans = INF;
    REP(j, 1000) REP(c, m + 1) ans = min(ans, dp[n + 1][j + 1][c]);
    cout << ans << endl;
  }
  return 0;
}
Problem p00453 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, a) FOR(i, 0, a)
#define SQR(a) ((a) * (a))
using namespace std;
typedef long long int ll;
typedef pair<int, int> pr;
typedef pair<pr, ll> ppr;
typedef vector<pr> vpr;
const ll INF = (ll)1 << 60;
ll DP[152][152][11];
int main() {
  int n, m, times, tmp;
  while (cin >> n >> m && n) {
    ll block[152][11] = {};
    ll x[152][11] = {};
    REP(i, 152)
    REP(j, 152)
    REP(k, 11)
    DP[i][j][k] = INF;
    REP(i, n) {
      cin >> times;
      REP(j, times) {
        cin >> x[i][j];
        cin >> block[i][j];
      }
    }
    REP(i, 11) {
      DP[0][0][i] = 0;
      DP[1][1][i] = 0;
    }
    FOR(i, 1, n)
    REP(j, 11)
    if (block[i][j])
      REP(l, m + 1)
    REP(k, 11) {
      if (block[i - 1][k])
        DP[i][l][j] = min(DP[i][l][j], DP[i - 1][l][k] +
                                           abs(x[i][j] - x[i - 1][k]) *
                                               (block[i - 1][k] + block[i][j]));
      if (block[i - 2][k] && l && i != 1)
        DP[i][l][j] = min(DP[i][l][j], DP[i - 2][l - 1][k] +
                                           abs(x[i][j] - x[i - 2][k]) *
                                               (block[i - 2][k] + block[i][j]));
    }
    ll minium = INF;
    REP(i, 11)
    REP(j, m + 1)
    minium = min(minium, DP[n - 1][j][i]);
    if (m)
      REP(i, 11)
    REP(j, m)
    minium = min(minium, DP[n - 2][j][i]);
    cout << minium << endl;
    // your code goes here
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, a) FOR(i, 0, a)
#define SQR(a) ((a) * (a))
using namespace std;
typedef long long int ll;
typedef pair<int, int> pr;
typedef pair<pr, ll> ppr;
typedef vector<pr> vpr;
const int INF = 1 << 30;
int DP[152][152][11];
int main() {
  int n, m, times, tmp;
  while (cin >> n >> m && n) {
    int block[152][11] = {};
    REP(i, 152)
    REP(j, 152)
    REP(k, 11)
    DP[i][j][k] = INF;
    REP(i, n) {
      cin >> times;
      REP(j, times) {
        cin >> tmp;
        cin >> block[i][tmp - 1];
      }
    }
    REP(i, 11) {
      DP[0][0][i] = 0;
      DP[1][1][i] = 0;
      block[n][i] = 1;
    }
    FOR(i, 1, n + 1)
    REP(j, 11)
    if (block[i][j])
      REP(l, m + 1)
    REP(k, 11) {
      if (block[i - 1][k])
        DP[i][l][j] =
            min(DP[i][l][j],
                DP[i - 1][l][k] + abs(j - k) * (block[i - 1][k] + block[i][j]));
      if (block[i - 2][k] && l && i != 1)
        DP[i][l][j] =
            min(DP[i][l][j], DP[i - 2][l - 1][k] +
                                 abs(j - k) * (block[i - 2][k] + block[i][j]));
    }
    int minium = INF;
    REP(i, 11)
    REP(j, m + 1)
    minium = min(minium, DP[n][j][i]);
    cout << minium << endl;
    // your code goes here
  }
  return 0;
}
Problem p00453 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;

#define REP(i, a, b) for (i = a; i < b; ++i)
#define rep(i, n) REP(i, 0, n)

int numstone[151];
pair<int, int> stones[151][11];
int memo
    [151][151]
    [11]; // memo[ツ甘敖つゥツづァツづ個仰猟猟」][ツ氾イツづ篠つオツ氾イツづ堕づーツつオツつスツ嘉アツ青脳[ツ債。ツづ個暗環置]
int n, m;

int solve(int i, int j, int k) {
  if (memo[i][j][k] != -1)
    return memo[i][j][k];
  if (i >= n - 1 || (j < m && i == n - 2))
    return 0;

  int l, ret = 1 << 29;
  if (j < m) {
    rep(l, numstone[i + 2]) {
      int cc = stones[i][k].second + stones[i + 2][l].second;
      ret = min(ret, solve(i + 2, j + 1, l) +
                         cc * abs(stones[i][k].first - stones[i + 2][l].first));
    }
  }

  rep(l, numstone[i + 1]) {
    int cc = stones[i][k].second + stones[i + 1][l].second;
    ret = min(ret, solve(i + 1, j, l) +
                       cc * abs(stones[i][k].first - stones[i + 1][l].first));
  }
  // printf("row: %d double: %d nowpos: %d ret: %d\\n", i,j,k,ret);
  return memo[i][j][k] = ret;
}

int main() {
  int k, i, j;
  while (scanf("%d%d", &n, &m), n | m) {
    memset(memo, -1, sizeof(memo));

    rep(i, 151) rep(j, 151) rep(k, 11) memo[i][j][k] = -1;
    rep(i, n) {
      scanf("%d", &k);
      numstone[i] = k;
      rep(j, k) {
        pair<int, int> p;
        scanf("%d%d", &(p.first), &(p.second));
        stones[i][j] = p;
      }
    }

    int ans = 1 << 29;
    rep(i, numstone[0]) { ans = min(ans, solve(0, 0, i)); }
    if (m >= 1) {
      rep(i, numstone[1]) { ans = min(ans, solve(1, 1, i)); }
    }
    printf("%d\\n", ans);
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;

#define REP(i, a, b) for (i = a; i < b; ++i)
#define rep(i, n) REP(i, 0, n)

int stones[151][1005];
int memo[151][1005][100];
int n, m;

int solve(int i, int j, int l) {
  if (memo[i][j][l] != -1)
    return memo[i][j][l];
  if (i >= n - 1)
    return 0;

  int res = 1000000000, k;
  if (l < m) {
    rep(k, 1005) {
      if (stones[i + 2][k] == 0)
        continue;
      res = min(res, (stones[i][j] + stones[i + 2][k]) * abs(k - j) +
                         solve(i + 2, k, l + 1));
    }
  }

  rep(k, 1005) {
    if (stones[i + 1][k] == 0)
      continue;
    res = min(res, (stones[i][j] + stones[i + 1][k]) * abs(k - j) +
                       solve(i + 1, k, l));
  }

  // printf("%d %d %d : %d\\n", i,j,l, res);
  return memo[i][j][l] = res;
}

int main() {
  int k, l, i, j, x, d;
  while (scanf("%d %d", &n, &m), n | m) {
    memset(stones, 0, sizeof(stones));
    memset(memo, -1, sizeof(memo));
    rep(i, n) {
      scanf("%d", &k);
      rep(j, k) {
        scanf("%d %d", &x, &d);
        stones[i][x - 1] = d;
      }
    }

    int ans = 1000000000;
    rep(i, 150) {
      if (stones[0][i] != 0) {
        ans = min(ans, solve(0, i, 0));
      } else if (m > 1 && stones[1][i] != 0) {
        ans = min(ans, solve(1, i, 1));
      }
    }
    printf("%d\\n", ans);
  }
}
Problem p00453 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
struct P {
  int x, c;
  P(int x, int c) : x(x), c(c) {}
};
int n;
vector<P> s[155];
int dp[155][11][77];
int solve(int y, int i, int m) {
  if (y >= n)
    return 0;
  if (m && y + 1 >= n)
    return 0;
  if (dp[y][i][m] >= 0)
    return dp[y][i][m];

  int ret = 1 << 30;
  for (int j = 0; j < (int)s[y + 1].size(); j++) {
    ret =
        min(ret, solve(y + 1, j, m) + ((y) ? (s[y][i].c + s[y + 1][j].c) *
                                                 abs(s[y][i].x - s[y + 1][j].x)
                                           : 0));
  }
  if (m) {
    for (int j = 0; j < (int)s[y + 2].size(); j++) {
      ret = min(ret, solve(y + 2, j, m - 1) +
                         ((y) ? (s[y][i].c + s[y + 2][j].c) *
                                    abs(s[y][i].x - s[y + 2][j].x)
                              : 0));
    }
  }
  return dp[y][i][m] = ret;
}
int main(void) {
  while (1) {
    int m;
    scanf("%d%d", &n, &m);
    if (!n)
      break;
    memset(dp, -1, sizeof(dp));
    for (int i = 1; i <= n; i++) {
      s[i].clear();
      int k;
      scanf("%d", &k);
      for (int j = 0; j < k; j++) {
        int x, d;
        scanf("%d%d", &x, &d);
        s[i].push_back(P(x, d));
      }
    }
    s[0].push_back(P(0, 0));
    /*
    for(int i = 0; i < (int)s[0].size(); i++){
      ret = min(ret,solve(0,i,m));
    }
    */
    printf("%d\\n", solve(0, 0, m));
    // break;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
struct P {
  int x, c;
  P(int x, int c) : x(x), c(c) {}
};
int n;
vector<P> s[150];
int dp[155][11][77];
int solve(int y, int i, int m) {
  if (y >= n)
    return 0;
  if (dp[y][i][m] >= 0)
    return dp[y][i][m];

  int ret = 1 << 22;
  for (int j = 0; j < (int)s[y + 1].size(); j++) {
    ret =
        min(ret, solve(y + 1, j, m) + ((y) ? (s[y][i].c + s[y + 1][j].c) *
                                                 abs(s[y][i].x - s[y + 1][j].x)
                                           : 0));
  }
  if (m) {
    for (int j = 0; j < (int)s[y + 2].size(); j++) {
      ret = min(ret, solve(y + 2, j, m - 1) +
                         ((y) ? (s[y][i].c + s[y + 2][j].c) *
                                    abs(s[y][i].x - s[y + 2][j].x)
                              : 0));
    }
  }
  return dp[y][i][m] = ret;
}
int main(void) {
  while (1) {
    int m;
    scanf("%d%d", &n, &m);
    if (!n)
      break;
    memset(dp, -1, sizeof(dp));
    for (int i = 1; i <= n; i++) {
      s[i].clear();
      int k;
      scanf("%d", &k);
      for (int j = 0; j < k; j++) {
        int x, d;
        scanf("%d%d", &x, &d);
        s[i].push_back(P(x, d));
      }
    }
    s[0].push_back(P(0, 0));
    /*
    for(int i = 0; i < (int)s[0].size(); i++){
      ret = min(ret,solve(0,i,m));
    }
    */
    printf("%d\\n", solve(0, 0, m));
  }
}
Problem p00453 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;

int dp[151][1001][76];
int main() {
  int n, m;
  while (scanf("%d%d", &n, &m), n) {
    memset(dp, 0x3f, sizeof(dp));
    vector<P> E[151];
    rep(i, n) {
      int k;
      scanf("%d", &k);
      rep(t, k) {
        int x, d;
        scanf("%d%d", &x, &d);
        E[i].push_back(P(d, --x));
      }
    }
    for (P i : E[0])
      dp[0][i.second][0] = 0;
    for (P i : E[1])
      dp[1][i.second][1] = 0;
    rep(i, n - 1) {
      for (P j : E[i]) {
        for (int t = 0; t <= m; t++) {
          if (dp[i][j.second][t] == INF)
            continue;
          for (P k : E[i + 1])
            dp[i + 1][k.second][t] =
                min(dp[i + 1][k.second][t],
                    dp[i][j.second][t] +
                        (j.first + k.first) * abs(j.second - k.second));
          if (i < n - 2) {
            for (P k : E[i + 2])
              dp[i + 2][k.second][t + 1] =
                  min(dp[i + 2][k.second][t + 1],
                      dp[i][j.second][t] +
                          (j.first + k.first) * abs(j.second - k.second));
          }
        }
      }
    }
    int Min = INF;
    for (P i : E[n - 1]) {
      for (int j = 0; j <= m; j++)
        Min = min(Min, dp[n - 1][i.second][j]);
    }
    for (P i : E[n - 2]) {
      for (int j = 0; j < m; j++)
        Min = min(Min, dp[n - 2][i.second][j]);
    }
    printf("%d\\n", Min);
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;

int dp[200][1001][80];
int main() {
  int n, m;
  while (scanf("%d%d", &n, &m), n) {
    memset(dp, 0x3f, sizeof(dp));
    vector<P> E[200];
    rep(i, n) {
      int k;
      scanf("%d", &k);
      rep(t, k) {
        int x, d;
        scanf("%d%d", &x, &d);
        E[i].push_back(P(d, --x));
      }
    }
    for (P i : E[0])
      dp[0][i.second][0] = 0;
    for (P i : E[1])
      dp[0][i.second][1] = 0;
    rep(i, n - 1) {
      for (P j : E[i]) {
        for (int t = 0; t <= m; t++) {
          if (dp[i][j.second][t] == INF)
            continue;
          for (P k : E[i + 1])
            dp[i + 1][k.second][t] =
                min(dp[i + 1][k.second][t],
                    dp[i][j.second][t] +
                        (j.first + k.first) * abs(j.second - k.second));
          if (i < n - 2) {
            for (P k : E[i + 2])
              dp[i + 2][k.second][t + 1] =
                  min(dp[i + 2][k.second][t + 1],
                      dp[i][j.second][t] +
                          (j.first + k.first) * abs(j.second - k.second));
          }
        }
      }
    }
    int Min = INF;
    for (P i : E[n - 1]) {
      for (int j = 0; j <= m; j++)
        Min = min(Min, dp[n - 1][i.second][j]);
    }
    for (P i : E[n - 2]) {
      for (int j = 0; j < m; j++)
        Min = min(Min, dp[n - 2][i.second][j]);
    }
    printf("%d\\n", Min);
  }
}
Problem p00453 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <queue>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
struct S {
  int x, y, r;
};
int main() {
  int m, n, t, u, ans, INF = 1 << 30;
  while (cin >> n >> m, n) {
    int d[n][10], cost[n][10][m + 1], x[n][10];
    rep(i, n) {
      rep(j, 10) {
        d[i][j] = x[i][j] = 0;
        rep(k, m + 1) cost[i][j][k] = INF;
      }
      cin >> t;
      rep(j, t) cin >> x[i][j], cin >> d[i][j];
    }
    queue<S> F;
    S s;
    ans = INF;
    rep(i, 10) rep(j, (m ? 2 : 1)) if (d[j][i]) {
      s.x = i, s.y = j, s.r = m - j;
      F.push(s), cost[j][i][m - j] = 0;
    }
    while (!F.empty()) {
      S cs = F.front();
      F.pop();
      rep(j, (cs.r ? 2 : 1)) {
        if (cs.y + j + 1 < n)
          rep(i, 10) if (d[cs.y + j + 1][i]) {
            s = cs;
            s.y = cs.y + j + 1, s.x = i, s.r -= j;
            u = x[s.y][i] - x[cs.y][cs.x];
            t = cost[cs.y][cs.x][cs.r] +
                (d[cs.y][cs.x] + d[s.y][i]) * (u > 0 ? u : -u);
            if (cost[s.y][i][s.r] > t)
              cost[s.y][i][s.r] = t, F.push(s);
          }
        if (cs.y + j + 1 >= n && ans > cost[cs.y][cs.x][cs.r])
          ans = cost[cs.y][cs.x][cs.r];
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <queue>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
struct S {
  int x, y, r;
};
int main() {
  int m, n, t, u, ans, INF = 1 << 30;
  while (cin >> n >> m, n) {
    int d[n][10], cost[n][10][m + 1];
    rep(i, n) {
      rep(j, 10) {
        d[i][j] = 0;
        rep(k, m + 1) cost[i][j][k] = INF;
      }
      cin >> t;
      rep(j, t) cin >> u, cin >> d[i][u - 1];
    }
    queue<S> F;
    S s;
    ans = INF;
    rep(i, 10) rep(j, (m ? 2 : 1)) if (d[j][i]) {
      s.x = i, s.y = j, s.r = m - j;
      F.push(s), cost[j][i][m - j] = 0;
    }
    while (!F.empty()) {
      S cs = F.front();
      F.pop();
      rep(j, (cs.r ? 2 : 1)) {
        if (cs.y + j + 1 < n)
          rep(i, 10) {
            s = cs;
            s.x = i, s.y = s.y + j + 1, s.r -= j;
            t = cost[cs.y][cs.x][cs.r] +
                (d[cs.y][cs.x] + d[s.y][i]) *
                    (i - cs.x > 0 ? i - cs.x : cs.x - i);
            if (d[s.y][i] && cost[s.y][i][s.r] > t)
              cost[s.y][i][s.r] = t, F.push(s);
          }
        if (cs.y + j + 1 >= n && ans > cost[cs.y][cs.x][cs.r])
          ans = cost[cs.y][cs.x][cs.r];
      }
    }
    cout << ans << endl;
  }
  return 0;
}