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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef double db;

#define fr first
#define sc second
#define pb push_back
#define rep(i, x) for (ll i = 0; i < x; i++)
#define rep1(i, x) for (ll i = 1; i <= x; i++)
#define rrep(i, x) for (ll i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (ll i = x; i > 0; i--)

ll n, m;
vector<P> rocks[150];
ll k;
ll dp[150][10][77]; // dp[i][j][k] rock[i][j]?????????k???????????£??????
const ll INF = 0xffffffffffff;
int main() {
  while (1) {
    scanf("%lld%lld", &n, &m);
    if (!n)
      return 0;
    rep(i, 150) rocks[i].clear();
    fill(dp[0][0], dp[149][10], INF);
    rep(i, n) {
      scanf("%lld", &k);
      rep(j, k) {
        ll x, y;
        scanf("%lld%lld", &x, &y);
        x--;
        rocks[i].pb(P(x, y));
      }
    }
    rep(i, rocks[0].size()) { dp[0][i][m] = 0; }
    rep(i, rocks[1].size()) {
      if (m > 1)
        dp[1][i][m - 1] = 0;
    }
    rep(i, n - 1) {
      rep(j, rocks[i].size()) {
        rep(k, m + 1) {
          if (dp[i][j][k] == INF)
            continue;
          //??????
          rep(l, rocks[i + 1].size()) {
            dp[i + 1][l][k] =
                min(dp[i + 1][l][k],
                    dp[i][j][k] + abs(rocks[i][j].fr - rocks[i + 1][l].fr) *
                                      (rocks[i][j].sc + rocks[i + 1][l].sc));
          }
          //??????
          if (k > 0 && i < n - 2) {
            rep(l, rocks[i + 2].size()) {
              dp[i + 2][l][k - 1] =
                  min(dp[i + 2][l][k - 1],
                      dp[i][j][k] + abs(rocks[i][j].fr - rocks[i + 2][l].fr) *
                                        (rocks[i][j].sc + rocks[i + 2][l].sc));
            }
          }
        }
      }
    }
    ll ans = INF;
    rep(i, rocks[n - 1].size()) {
      rep(j, m + 1) { ans = min(ans, dp[n - 1][i][j]); }
    }
    rep(i, rocks[n - 2].size()) { rep1(j, m) ans = min(ans, dp[n - 2][i][j]); }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef double db;

#define fr first
#define sc second
#define pb push_back
#define rep(i, x) for (ll i = 0; i < x; i++)
#define rep1(i, x) for (ll i = 1; i <= x; i++)
#define rrep(i, x) for (ll i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (ll i = x; i > 0; i--)

ll n, m;
vector<P> rocks[150];
ll k;
ll dp[150][10][77]; // dp[i][j][k] rock[i][j]?????????k???????????£??????
const ll INF = 0xffffffffffff;
int main() {
  while (1) {
    cin >> n >> m;
    rep(i, 150) rocks[i].clear();
    fill(dp[0][0], dp[149][10], INF);
    rep(i, n) {
      cin >> k;
      rep(j, k) {
        ll x, y;
        cin >> x >> y;
        x--;
        rocks[i].pb(P(x, y));
      }
    }
    rep(i, rocks[0].size()) { dp[0][i][m] = 0; }
    rep(i, n - 1) {
      rep(j, rocks[i].size()) {
        rep(k, m + 1) {
          if (dp[i][j][k] == INF)
            continue;
          //??????
          rep(l, rocks[i + 1].size()) {
            dp[i + 1][l][k] =
                min(dp[i + 1][l][k],
                    dp[i][j][k] + abs(rocks[i][j].fr - rocks[i + 1][l].fr) *
                                      (rocks[i][j].sc + rocks[i + 1][l].sc));
          }
          //??????
          if (k > 0 && i < n - 2) {
            rep(l, rocks[i + 2].size()) {
              dp[i + 2][l][k - 1] =
                  min(dp[i + 2][l][k - 1],
                      dp[i][j][k] + abs(rocks[i][j].fr - rocks[i + 2][l].fr) *
                                        (rocks[i][j].sc + rocks[i + 2][l].sc));
            }
          }
        }
      }
    }
    ll ans = INF;
    rep(i, rocks[n - 1].size()) {
      rep(j, m + 1) { ans = min(ans, dp[n - 1][i][j]); }
    }
    cout << ans << endl;
  }
}