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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

typedef double Weight;
typedef vector<Weight> Array;
typedef vector<Array> Matrix;

Matrix mat(15, Array(15));
vector<vector<Weight>> best(1 << 15, vector<Weight>(15, INF));
vector<vector<int>> prev(1 << 15, vector<int>(15));

int main() {
  int n;
  cin >> n;

  int a[n], l[n], m[n];
  REP(i, n)
  cin >> a[i] >> l[i] >> m[i];

  REP(i, n)
  REP(j, n)
  mat[i][j] = abs(l[j] - l[i]);

  int N = 1 << n;

  REP(i, n) best[1 << i][i] = 0;
  REP(S, N) {
    int we = 0;
    REP(i, n) // ツ個サツ催敖づ個重ツつウツづーツ計ツ算
    if (S & (1 << i))
      we += m[i];
    we = 70 + 20 * we;
    REP(i, n)
    if (S & (1 << i))
      REP(j, n) {
        if (best[S | (1 << j)][j] > best[S][i] + mat[i][j] / 2000.0 * we) {
          best[S | (1 << j)][j] = best[S][i] + mat[i][j] / 2000.0 * we;
          prev[S | (1 << j)][j] = i;
        }
      }
  }

  vector<int> path;
  int t =
      min_element(ALL(best[N - 1])) -
      best[N - 1]
          .begin(); // ツ可スツ氾板姪堋づ個陳クツ点ツづ終ツづュツづゥツづ個つェツ暗ェツ氾板短ツつュツづ按づゥツつゥ

  int S = N - 1;
  int count = 0;
  while (S) {
    path.push_back(t);
    int tmp = prev[S][t];
    S ^= (1 << t);
    t = tmp;
    count++;
    if (count == n)
      break;
  }
  reverse(ALL(path));

  REP(i, path.size()) {
    if (i)
      cout << " ";
    cout << a[path[i]];
  }
  cout << endl;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

typedef double Weight;
typedef vector<Weight> Array;
typedef vector<Array> Matrix;

int main() {
  int n;
  cin >> n;

  int a[n], l[n], m[n];
  REP(i, n)
  cin >> a[i] >> l[i] >> m[i];
  Matrix mat(n, Array(n));
  REP(i, n)
  REP(j, n)
  mat[i][j] = abs(l[j] - l[i]);

  int N = 1 << n;
  vector<vector<Weight>> best(N, vector<Weight>(n, INF));
  vector<vector<int>> prev(N, vector<int>(n));

  REP(i, n) best[1 << i][i] = 0;
  REP(S, N) {
    int we = 0;
    REP(i, n) // ツ個サツ催敖づ個重ツつウツづーツ計ツ算
    if (S & (1 << i))
      we += m[i];
    we = 70 + 20 * we;
    REP(i, n)
    if (S & (1 << i))
      REP(j, n) {
        if (best[S | (1 << j)][j] > best[S][i] + mat[i][j] * (we + 70)) {
          best[S | (1 << j)][j] = best[S][i] + mat[i][j] * (we + 70);
          prev[S | (1 << j)][j] = i;
        }
      }
  }

  vector<int> path;
  int t =
      min_element(ALL(best[N - 1])) -
      best[N - 1]
          .begin(); // ツ可スツ氾板姪堋づ個陳クツ点ツづ終ツづュツづゥツづ個つェツ暗ェツ氾板短ツつュツづ按づゥツつゥ

  int S = N - 1;
  while (S) {
    path.push_back(t);
    int tmp = prev[S][t];
    S ^= (1 << t);
    t = tmp;
  }
  reverse(ALL(path));

  REP(i, path.size()) {
    if (i)
      cout << " ";
    cout << a[path[i]];
  }
  cout << endl;
}