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

#define _CRT_SECURE_NO_WARNINGS 1
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <forward_list>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define INF ((int)(1 << 25))
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, n) REP(i, n)
#define TORAD 2.0 * M_PI / 360.0
#define INT(x)                                                                 \\
  int x;                                                                       \\
  cin >> x;
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define DEBUG(x) cout << #x << " :" << x << endl;
#define EPS 1e-12

template <class T> void debug(T a) {
  for (auto i : a)
    cout << i << endl;
}

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};

int n;
vector<pii> a;
int memo[120][120];
int dp(int l, int r) {
  if (memo[l][r] != -1)
    return memo[l][r];
  if (l == r)
    return 0;
  int ans = INT_MAX;
  for (int i = l; i < r; i++) {
    ans = min(ans, dp(l, i) + dp(i + 1, r) +
                       a[l].first * a[i].second * a[i + 1].first * a[r].second);
  }
  return memo[l][r] = ans;
}

int main() {
  memset(memo, -1, sizeof memo);
  cin >> n;
  FOR(i, n) {
    pii p;
    cin >> p.first >> p.second;
    a.push_back(p);
  }
  cout << dp(0, n - 1) << endl;
}

Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS 1
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <forward_list>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define INF ((int)(1 << 25))
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, n) REP(i, n)
#define TORAD 2.0 * M_PI / 360.0
#define INT(x)                                                                 \\
  int x;                                                                       \\
  cin >> x;
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define DEBUG(x) cout << #x << " :" << x << endl;
#define EPS 1e-12

template <class T> void debug(T a) {
  for (auto i : a)
    cout << i << endl;
}

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};

int n;
vector<pii> a;

int hoge(int t, int d, vi &v) {
  if (accumulate(ALL(v), 0) == n)
    return 0;
  int ans = INT_MAX;
  for (int i = 0; i < n; i++) {
    if (v[i])
      continue;
    v[i] = 1;
    ans = min(ans, hoge(t, a[i].second, v) + t * d * a[i].first * a[i].second);
    v[i] = 0;
  }
  return ans;
}

int main() {

  cin >> n;
  FOR(i, n) {
    pii p;
    cin >> p.first >> p.second;
    a.push_back(p);
  }
  vi v(n, 0);
  int ans = INT_MAX;
  FOR(i, n) {
    v[i] = 1;
    ans = min(ans, hoge(a[i].first, a[i].second, v));
    v[i] = 0;
  }
  cout << ans << endl;
}