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++: #include<iostream>
#include<vector>
#include<climits>

#define MAX 101
#define Inf (UINT_MAX-1)

typedef unsigned long long int uint;
using namespace std;

struct State{
	State():cost(Inf),bottom(1),top(1){}
	uint cost;
	uint bottom;
	uint top;
};

State m[MAX][MAX];

void CardOrder(vector<pair<uint, uint> > &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){
				uint 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( cost < m[i][i+l-1].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;
					}
				}
			}
		}
	}
}
void Reset(){
	for(int i = 0; i < MAX; ++i){
		for(int j = 0; j < MAX; ++j){
			m[i][j].cost = Inf;
			m[i][j].bottom = 1;
			m[i][j].top = 1;
		}
	}
}
int main()
{
	while( true ){
		int n;
		vector< pair<uint, uint> > vCards;
	
		cin >> n;

		if( cin.eof() )
			break;

		Reset();

		for(int x = 0; x < n; ++x){
			uint top, btm;
			cin >> top >> btm;

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

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

const unsigned int INF = 1 << 31;
unsigned int dp[101][101], a[101], b[101];
int n;

// l..r の最小コストを返す
// dp[i][j] := 山i..jの最小コスト
unsigned int solve(int l, int r){
	if( dp[l][r] != INF ) return dp[l][r];
	if( l+1 == r ) return dp[l][r] = a[l] * b[l] * a[r] * b[r];
	if( l == r ) return dp[l][r] = 0;
	
	unsigned int res = INF;
	for(int i=l ; i < r ; i++ ){
		// l..i i..r に分割
		unsigned int cost = b[l] * a[i] * b[i+1] * a[r] + solve(l,i) + solve(i+1,r);
		res = min( res , cost );
	}
	return dp[l][r] = res;
}

void ini(){
	for(int i=0 ; i < 101 ; i++ ){
		for(int j=0 ; j < 101 ; j++ ){
			dp[i][j] = INF;
		}
	}
}

int main(){
	cin >> n;
	for(int i=0 ; i < n ; i++ ){
		cin >> a[i] >> b[i];
	}
	ini();
	unsigned int ans = solve(0,n-1);
	cout << ans << endl;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <sstream>
#include <cassert>
#include <list>
#include <ctime>
#include <numeric>
#include <fstream>
#include <valarray>
using namespace std;
static const double EPS = 1e-6;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
#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()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define SZ(a) (int(a.size()))
#define F first
#define S second
const long double pi=acos(-1.0);
int dx[]={0,1,0,-1,1,1,-1,-1,0},dy[]={1,0,-1,0,1,-1,1,-1,0};

int n;
pair<ll,ll> in[100];
ll dp[100][101];
bool vis[100][101];

ll rec(int a,int b){
  if(a+2>b)return 0;
  if(a+2==b)
    return in[a].F*in[a].S*in[b-1].F*in[b-1].S;
  if(vis[a][b])return dp[a][b];
  vis[a][b]=true;
  ll& ret=dp[a][b]=1LL<<50LL;
  
  for(int s=a+1;s<b;++s)
    ret=min(ret,
            rec(a,s)+rec(s,b)+
            in[a].F*in[s-1].S*in[s].F*in[b-1].S);
  return ret;
}

main(){
  cin>>n;
  rep(i,n)cin>>in[i].F>>in[i].S;

  cout<<rec(0,n)<<endl;
}

C++: #include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<climits>
using namespace std;
#define INF INT_MAX
#define P pair<int, int>
#define N 101

int loop(int left, int right);
void resetDp();

int n;
vector<P> cost;
int dp[N][N];

int main(){
    resetDp();
    cin >>n;
    for(int i = 0; i < n; i++){
        int top, bottom;
        cin >>top >>bottom;
        cost.push_back( make_pair(top, bottom) );
    }
    cout <<loop(0, n - 1) <<endl;
    return 0;
}

int loop(int left, int right){
    if(left == right) return 0;
    else if(dp[left][right] != -1) return dp[left][right];
    else {
        int ret = INF;
        for(int i = left; i < right; i++){
            ret = min(ret, loop(left, i) + loop(i + 1, right) + (cost[left].first * cost[i].second * cost[i + 1].first * cost[right].second));
        }
        return dp[left][right] = ret;
    }
}

void resetDp(){
    for(int i = 0; i < N; i++){
        for(int j = 0; j < N; j++){
            dp[i][j] = INF;
        }
    }
}

C++: #include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>

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::iretator 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;

int main() {
	int n;
	cin >> n;
	vvi cost(n, vi(2));

	REP(i, n) {
		cin >> cost[i][0] >> cost[i][1];
	}

	ll num = 0;
	int max00, max11;
	int max01, max10;
	int p0, p1;

	REP(i, n-1) {
		max00 = max11 = max01 = max10 = 1;
		REP(j, n) {
			if(cost[j][0] > max00) {
				max00 = cost[j][0];
				max01 = cost[j][1];
				p0 = j;
			}
		}

		REP(j, n) {
			if(cost[j][1] > max11 && p0 != j) {
				max10 = cost[j][0];
				max11 = cost[j][1];
				p1 = j;
			}
		}

		cost[p0][0] = cost[p1][0]; 
		cost[p1][0] = cost[p1][1] = 0;
		p0 = p1;

		num += max00*max01*max10*max11;
	}

	cout << num << endl;
}