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
/*}}}*/

#define INF INT_MAX

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

	REP (i, 100) {
		REP (j, 100) {
			if (i == j) {
				dp[i][j] = 0;
			} else {
				dp[i][j] = INF;
			}
		}
	}
	FOR (w, 1, n) {
		cerr << "w = " << w << endl;
		REP (i, n-w) {
			cerr << "i = " << i << endl;
			int j = i + w;
			FOR (k, i, j) {
				cerr << "\tk = " << k << endl;
				dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + cards[i].F * cards[k].S * cards[k+1].F * cards[j].S);
			}
		}
	}
	cout << dp[0][n-1] << endl;
}

C++: #include <iostream>
#include <climits>
using namespace std;

int a[100],b[100];
int dp[100][100];

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

C++: # define _CRT_SECURE_NO_WARNINGS 1
# include <iostream>
# include <numeric>
# include <string>
# include <bitset>
# include <vector>
# include <algorithm>
# include <cstdlib>
# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iomanip>
# include <queue>
# include <sstream>
# include <climits>
# include <cmath>
# include <list>
# include <functional>
# include <string>
# include <ctime>
# include <set>
# include <forward_list>
# include <map>
# include <stack>
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;
}

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

#define MAX 101
#define Inf (UINT_MAX-1)
using namespace std;

struct State{
	State():cost(Inf),bottom(-1),top(-1){
	}
	unsigned int cost;
	int bottom;
	int top;
};

State m[MAX][MAX];

void CardOrder(vector<pair<int, int> > &vc){
	int n = vc.size();
	for(int i = 1; i <= n; ++i){
		m[i][i].cost = 0;
		m[i][i].top = vc[i-1].first;
		m[i][i].bottom = vc[i-1].second;
	}
	for(int l = 2; l <= n; ++l){
		for(int i = 1; i <= n - l + 1; ++i){
			for(int j = i; j < i + l; ++j){
				unsigned int cost = m[i][j].cost + m[j+1][i+l-1].cost +
					m[i][j].top * m[i][j].bottom * m[j+1][i+l-1].top * m[j+1][i+l-1].bottom;
				if( m[i][i+l-1].cost > cost ){
					m[i][i+l-1].cost = cost;
					if( m[i][j].top * m[j+1][i+l-1].bottom < m[j+1][i+l-1].top * m[i][j].bottom ){
						m[i][i+l-1].top = m[i][j].top;
						m[i][i+l-1].bottom = m[j+1][i+l-1].bottom;
					}else{
						m[i][i+l-1].top = m[j+1][i+l-1].top;
						m[i][i+l-1].bottom = m[i][j].bottom;
					}
				}
			}
		}
	}
}

int main()
{
	int n;
	vector< pair<int, int> > vCards;
	
	cin >> n;
	for(int x = 0; x < n; ++x){
		int top, btm;
		cin >> top >> btm;

		vCards.push_back( make_pair(top, btm) );
	}
	
	CardOrder( vCards );
	cout << m[1][n].cost << '\n';
	return 0;
}