Problem p00453 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <stdio.h>
#include <stdlib.h>
#define INF 1000000000
int n, m;
int d[80][150][10];
int r[150][1000];
int f[150][10];
int c[150];
int i, j, k, l;
int t;
int a, b;

int min(int a, int b) {
  if (a >= b)
    return b;
  else
    return a;
}

int main() {
  while (1) {
    scanf("%d %d", &n, &m);
    if (n == 0 && m == 0)
      return 0;
    for (i = 0; i < n; i++)
      c[i] = 0;
    for (i = 0; i < n; i++) {
      scanf("%d", &t);
      for (j = 0; j < t; j++) {
        scanf("%d %d", &a, &b);
        r[i][a - 1] = b;
        f[i][c[i]++] = a - 1;
      }
    }
    for (k = 0; k <= m; k++)
      for (i = 0; i < n; i++)
        for (j = 0; j < c[i]; j++) {
          d[k][i][j] = INF;
        }
    for (i = 0; i < c[0]; i++) {
      d[m][0][i] = 0;
    }
    if (m >= 1)
      for (j = 0; j < c[1]; j++) {
        d[m - 1][1][j] = 0;
      }
    for (i = 1; i < n; i++) {
      if (i > 1) {
        for (j = 0; j <= m - 1; j++) {
          for (k = 0; k < c[i]; k++) {
            for (l = 0; l < c[i - 2]; l++) {
              d[j][i][k] = min(d[j][i][k],
                               d[j + 1][i - 2][l] +
                                   abs(f[i][k] - f[i - 2][l]) *
                                       (r[i][f[i][k]] + r[i - 2][f[i - 2][l]]));
            }
          }
        }
      }
      for (j = 0; j <= m; j++) {
        for (k = 0; k < c[i]; k++) {
          for (l = 0; l < c[i - 1]; l++) {
            d[j][i][k] = min(d[j][i][k],
                             d[j][i - 1][l] +
                                 abs(f[i][k] - f[i - 1][l]) *
                                     (r[i][f[i][k]] + r[i - 1][f[i - 1][l]]));
          }
        }
      }
    }
    int ans = INF;
    for (i = 0; i <= m; i++)
      for (j = 0; j < c[n - 1]; j++) {
        ans = min(ans, d[i][n - 1][j]);
      }
    for (i = 1; i <= m; i++)
      for (j = 0; j < c[n - 2]; j++) {
        ans = min(ans, d[i][n - 2][j]);
      }
    printf("%d\\n", ans);
  }
}

Error: MLE

Fixed submission:

#include <stdio.h>
#include <stdlib.h>
#define INF 1000000000
int n, m;
int d[77][150][1000];
int r[150][1000];
int f[150][1000];
int c[150];
int i, j, k, l;
int t;
int a, b;

int min(int a, int b) {
  if (a >= b)
    return b;
  else
    return a;
}

int main() {
  while (1) {
    scanf("%d %d", &n, &m);
    if (n == 0 && m == 0)
      return 0;
    for (i = 0; i < n; i++)
      c[i] = 0;
    for (i = 0; i < n; i++) {
      scanf("%d", &t);
      for (j = 0; j < t; j++) {
        scanf("%d %d", &a, &b);
        r[i][a - 1] = b;
        f[i][c[i]++] = a - 1;
      }
    }
    for (k = 0; k <= m; k++)
      for (i = 0; i < n; i++)
        for (j = 0; j < c[i]; j++) {
          d[k][i][f[i][j]] = INF;
        }
    for (i = 0; i <= c[0]; i++)
      d[m][0][f[0][i]] = 0;
    if (m >= 1)
      for (j = 0; j < c[1]; j++)
        d[m - 1][1][f[1][j]] = 0;
    for (i = 1; i < n; i++) {
      if (i > 1) {
        for (j = 0; j <= m - 1; j++) {
          for (k = 0; k < c[i]; k++) {
            for (l = 0; l < c[i - 2]; l++) {
              d[j][i][f[i][k]] =
                  min(d[j][i][f[i][k]],
                      d[j + 1][i - 2][f[i - 2][l]] +
                          abs(f[i][k] - f[i - 2][l]) *
                              (r[i][f[i][k]] + r[i - 2][f[i - 2][l]]));
            }
          }
        }
      }
      for (j = 0; j <= m; j++) {
        for (k = 0; k < c[i]; k++) {
          for (l = 0; l < c[i - 1]; l++) {
            d[j][i][f[i][k]] =
                min(d[j][i][f[i][k]],
                    d[j][i - 1][f[i - 1][l]] +
                        abs(f[i][k] - f[i - 1][l]) *
                            (r[i][f[i][k]] + r[i - 1][f[i - 1][l]]));
          }
        }
      }
    }
    int ans = INF;
    for (i = 0; i <= m; i++)
      for (j = 0; j < c[n - 1]; j++) {
        ans = min(ans, d[i][n - 1][f[n - 1][j]]);
      }
    for (i = 1; i <= m; i++)
      for (j = 0; j < c[n - 2]; j++) {
        ans = min(ans, d[i][n - 2][f[n - 2][j]]);
      }
    printf("%d\\n", ans);
  }
}