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

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EACH(t, i, c) for (t::iterator i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
const double EPS = 1e-10;
const double PI = acos(-1.0);

ll dp[101][101];
ll a[101], b[101];

const ll MAX = INT_MAX;
ll dfs(int l, int r) {
  if (dp[l][r] != MAX) {
    return dp[l][r];
  }
  if (r - l == 1) {
    return dp[l][r] = min(dp[l][r], a[l] * b[l] * a[r] * b[r]);
  } else if (r == l) {
    return dp[l][r] = 0;
  }
  ll best = MAX;
  FOR(i, l, r) {
    ll cost = a[l] * b[i] * a[i + 1] * b[r];
    best = min(best, dfs(l, i) + dfs(i + 1, r) + cost);
  }
  return dp[l][r] = min(dp[l][r], best);
}

int main() {
  int n;
  cin >> n;
  REP(i, n + 1) REP(j, n + 1) { dp[i][j] = MAX; }

  REP(i, n) { cin >> a[i] >> b[i]; }

  cout << dfs(0, n - 1) << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EACH(t, i, c) for (t::iterator i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
const double EPS = 1e-10;
const double PI = acos(-1.0);

ll dp[101][101];
ll a[101], b[101];

const ll MAX = INT_MAX;
ll dfs(int l, int r) {
  if (dp[l][r] != MAX) {
    return dp[l][r];
  }
  if (r - l == 1) {
    return dp[l][r] = min(dp[l][r], a[l] * b[l] * a[r] * b[r]);
  } else if (r == l) {
    return dp[l][r] = 0;
  }
  ll best = MAX;
  FOR(i, l, r) {
    ll cost = a[l] * b[i] * a[i + 1] * b[r];
    best = min(best, dfs(l, i) + dfs(i + 1, r) + cost);
  }
  return dp[l][r] = min(dp[l][r], best);
}

int main() {
  int n;
  while (cin >> n, n) {
    REP(i, n + 1) REP(j, n + 1) { dp[i][j] = MAX; }

    REP(i, n) { cin >> a[i] >> b[i]; }

    cout << dfs(0, n - 1) << endl;
  }
}