Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"strings"
	"sort"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)
const INF = 0x7fffffff
func main() {
	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	top := make([]int, 0)
	bottom := make([]int, 0)
	for i := 0; i < n; i++ {
		scanner.Scan()
		buf := strings.Split(scanner.Text(), " ")
		a, _ := strconv.Atoi(buf[0])
		b, _ := strconv.Atoi(buf[1])
		top = append(top, a)
		bottom = append(bottom, b)
	}
	dp := make([][]int, n+1)
	for i := range dp {
		dp[i] = make([]int, n+1)
	}
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			dp[i][j] = INF
		}
	}
	for j := 0; j < n; j++ {
		dp[0][j] = 0
	}
	for i := 1; i < n; i++ {
		for j := 0; j < n-i; j++ {
			tmp := make([]int, 0)
			for k := 0; k < i; k++ {
				tmp = append(tmp, dp[k][j] + dp[i - k - 1][j + k + 1] +top[j] * bottom[j + k] * top[j + k + 1] * bottom[j + i])
			}
			sort.Ints(tmp)
			dp[i][j] = tmp[0]
		}
	}
	fmt.Println(dp[n-1][0])
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

C++: #define DEBUG_ON
#define CONDITION true
using namespace std;/*{{{*/

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

static const double PI (3.14159265358979323846);
static const double EPS (1e-10);

typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef complex<double> P;

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();}

#define FOR(i, b, e) for (typeof(b) i = (b); assert((i) <= (e)), i != (e); ++i)
#define REP(i, n) FOR(i, 0, n)

#define OPOVER(_op, _type) inline bool operator _op (const _type &t) const

#define arrsz(a) ( sizeof(a) / sizeof(a[0]) )

#define F first
#define S second
#define MP(a, b) make_pair(a, b)

#define SZ(a) ((ll)a.size())
#define PB(e) push_back(e)
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())

#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 BIT(n) (1ULL << (n))
#define BITOF(n, m) ((n) >> (m) & 1)

#define RANGE(a, b, c) ((a) <= (b) && (b) <= (c))

#ifdef DEBUG_ON
#define dprt(fmt, ...) if (CONDITION) fprintf(stderr, fmt, __VA_ARGS__)
#define darr(a) if (CONDITION) copy( (a), (a) + arrsz(a), ostream_iterator<int>(cerr, " ") ); cerr << endl
#define darr_range(a, f, t) if (CONDITION) copy( (a) + (f), (a) + (t), ostream_iterator<int>(cerr, " ") ); cerr << endl
#define dvec(v) if (CONDITION) copy( ALL(v), ostream_iterator<int>(cerr, " ") ); cerr << endl
#define darr2(a, n, m) if (CONDITION) FOR(i, 0, (n)){ darr_range( (a)[i], 0, (m) ); }
#define dvec2(v) if (CONDITION) FOR(i, 0, SZ(v)){ dvec( (v)[i] ); }
#define WAIT() if (CONDITION) { string _wait_; cerr << "(hit return to continue)" << endl; getline(cin, _wait_); }
#else
#define cerr if(0) cerr
#define dprt(fmt, ...)
#define darr(a)
#define darr_range(a, f, t)
#define dvec(v)
#define darr2(a, n, m)
#define dvec2(v)
#define WAIT()
#endif
/*}}}*/

int main(int argc, char const* argv[])
{
	int cost[101];
	vector<pii> cards;
	int n;
	cin >> n;
	REP (i, n) {
		pii c;
		cin >> c.F >> c.S;
		cards.PB(c);
	}
	cost[0] = 0;
	cost[1] = cards[0].F * cards[0].S * cards[1].F * cards[1].S;

	FOR (i, 2, n) {
		cost[i] = min( cost[i-1] + ( cards[0].F * cards[i-1].S * cards[i].F * cards[i].S),
						cost[i-2] + (cards[i-1].F * cards[i-1].S * cards[i].F * cards[i].S) + (cards[0].F * cards[i-2].S * cards[i-1].F * cards[i].S) );
	}
	cout << cost[n-1] << endl;

	return 0;
}

C++: #include <iostream>
#include <vector>
#include <map>

using namespace std;

typedef pair<int, int> P;
vector<P> D;

int n, a, b;
int DP[101][101];

void solve()
{
	for(int i=0; i<n; i++)
	{
		for(int j=0; j<n; j++)
		{
			if(i!=j) DP[i][j] = INT_MAX;
			else DP[i][j] = 0;
		}
	}	

	for(int i=1; i<n; i++)
	{
		for(int j=0; j<n-i; j++)
		{
			for(int k=j; k<j+i; k++)
			{
				int cost = D[j].first * D[k].second * D[k+1].first * D[j+i].second;
				DP[j][j+i] = min(DP[j][j+i], cost + DP[j][k] + DP[k+1][j+i]);
			}
		}
	}

	cout << DP[0][n-1] << endl;
}

int main(int argc, char** argv)
{
	cin >> n;
	for(int i=0; i<n; i++)
	{
		cin >> a >> b;
		P p(a, b);
		D.push_back(p);
	}

	solve();
}

C++: #include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <utility>
#include <algorithm>
#include <numeric>
#include <typeinfo>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <ctime>

using namespace std;

#define dump(n) cout<<"# "<<#n<<"="<<(n)<<endl
#define debug(n) cout<<__FILE__<<","<<__LINE__<<": #"<<#n<<"="<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,n) repi(i,0,n)
#define iter(c) __typeof((c).begin())
#define foreach(i,c) for(iter(c) i=(c).begin();i!=(c).end();i++)
#define allof(c) (c).begin(),(c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int,int> pii;

int main()
{
	int n; cin>>n;
	vector<pii> a(n);
	rep(i,n)
		cin>>a[i].first>>a[i].second;
	
	vvi dp(n,vi(n,1<<29));
	rep(i,n)
		dp[i][i]=0;
	for(int i=n;i--;){
		for(int j=i+1;j<n;j++){
			for(int k=i;k<j;k++){
				int mul=a[i].first*a[k].second*a[k+1].first*a[j].second;
				dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]+mul);
			}
		}
	}
	
	cout<<dp[0][n-1]<<endl;
	
	return 0;
}

C++: #include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

int main()
{
    int n;
    cin >> n;
    vector<int> a(n), b(n);
    for(int i=0; i<n; ++i)
        cin >> a[i] >> b[i];

    vector<vector<int> > cost(n, vector<int>(n, INT_MAX));
    for(int i=0; i<n; ++i)
        cost[i][i] = 0;
    for(int i=1; i<n; ++i){
        for(int j=0; j+i<n; ++j){
            for(int k=j; k<j+i; ++k){
                int c = cost[j][k] + cost[k+1][j+i] + b[j] * a[k] * b[k+1] * a[j+i];
                cost[j][j+i] = min(cost[j][j+i], c);
            }
        }
    }
    cout << cost[0][n-1] << endl;

    return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<21

using namespace std;

int main()
{
	int n;
	cin >> n;
	
	vector<pair<int,int> > v;
	long long dp[101][101];

	rep(i,n)
	{
		int a,b;
		cin >> a >> b;
		v.push_back(make_pair(a,b));
	}
	
	memset(dp,0x2,sizeof(dp));
	rep(i,n+1) dp[i][i] = 0;
	
	REP(i,1,n)
	{
		rep(j,n-i+1)
		{
			REP(k,j,j+i)
			{
				int cost = v[j].first * v[k].second * v[k+1].first *  v[j+i].second;
				dp[j][j+i] = min(dp[j][j+i],cost+dp[j][k]+dp[k+1][j+i]);
			}
		}
	}

	cout << dp[0][n-1] << endl;

	return 0;
}