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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

typedef long long LL;
typedef vector<int> Array;
typedef pair<int, int> P;

const int INF = 1 << 29;
const LL MOD = 100000;
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define all(c) (c).begin(), (c).end()

struct edge {
  LL to, cost;
  edge(int t, LL c) : to(t), cost(c) {}
};

int dxk[] = {1, 1, 1, 0, -1, 0};
int dxg[] = {0, 1, 0, -1, -1, -1};
int dy[] = {-1, 0, 1, 1, 0, -1};

int bitcount(int a) {
  int ret = 0;
  while (a != 0) {
    ret++;
    a = (a - 1) & a;
  }
  return ret;
}

int dp[151][1001][77];
int stage[151][1001];

int main() {
  int n, m;
  while (cin >> n >> m && n) {
    rep(i, n) rep(j, 1001) stage[i][j] = INF;
    int width = 0;
    rep(i, n) {
      int num;
      cin >> num;
      rep(j, num) {
        int idx, slip;
        cin >> idx >> slip;
        width = max(width, idx);
        stage[i][idx - 1] = slip;
      }
    }
    rep(i, n + 1) rep(j, width) rep(k, m + 1) dp[i][j][k] = INF;
    rep(i, width) dp[0][i][0] = stage[n][i] = 0;
    if (m > 0) {
      rep(i, width) dp[1][i][1] = 0;
    }
    rep(i, n) {
      rep(j, width) if (stage[i][j] < INF) {
        rep(l, m + 1) if (dp[i][j][l] < INF) {
          rep(k, width) {
            dp[i + 1][k][l] =
                min(dp[i + 1][k][l],
                    dp[i][j][l] + (stage[i][j] + stage[i + 1][k]) * abs(j - k));
            if (i + 2 <= n)
              dp[i + 2][k][l + 1] = min(
                  dp[i + 2][k][l + 1],
                  dp[i][j][l] + (stage[i][j] + stage[i + 2][k]) * abs(j - k));
          }
        }
      }
    }
    int ans = INF;
    rep(i, width) rep(j, m + 1) ans = min(ans, dp[n][i][j]);
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

typedef long long LL;
typedef vector<int> Array;
typedef pair<int, int> P;

const int INF = 1 << 29;
const LL MOD = 100000;
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define all(c) (c).begin(), (c).end()

struct edge {
  LL to, cost;
  edge(int t, LL c) : to(t), cost(c) {}
};

int dxk[] = {1, 1, 1, 0, -1, 0};
int dxg[] = {0, 1, 0, -1, -1, -1};
int dy[] = {-1, 0, 1, 1, 0, -1};

int bitcount(int a) {
  int ret = 0;
  while (a != 0) {
    ret++;
    a = (a - 1) & a;
  }
  return ret;
}

int dp[151][1001][77];
int stage[151][1001];

int main() {
  int n, m;
  while (cin >> n >> m && n) {
    rep(i, n) rep(j, 1001) stage[i][j] = INF;
    int width = 0;
    rep(i, n) {
      int num;
      cin >> num;
      rep(j, num) {
        int idx, slip;
        cin >> idx >> slip;
        width = max(width, idx);
        stage[i][idx - 1] = slip;
      }
    }
    rep(i, n + 1) rep(j, width) rep(k, m + 1) dp[i][j][k] = INF;
    rep(i, width) dp[0][i][0] = stage[n][i] = 0;
    if (m > 0) {
      rep(i, width) dp[1][i][1] = 0;
    }
    rep(i, n) {
      rep(j, width) {
        rep(k, width) {
          rep(l, m + 1) if (dp[i][j][l] < INF && stage[i][j] < INF) {
            dp[i + 1][k][l] =
                min(dp[i + 1][k][l],
                    dp[i][j][l] + (stage[i][j] + stage[i + 1][k]) * abs(j - k));
            if (i + 2 <= n)
              dp[i + 2][k][l + 1] = min(
                  dp[i + 2][k][l + 1],
                  dp[i][j][l] + (stage[i][j] + stage[i + 2][k]) * abs(j - k));
          }
        }
      }
    }
    int ans = INF;
    rep(i, width) rep(j, m + 1) ans = min(ans, dp[n][i][j]);
    cout << ans << endl;
  }

  return 0;
}