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 <algorithm>
using namespace std;

int main( void )
{
    int n, a, b;
    int CT[101];
    int CD[101];
    int dp[101][101];
    int top[101][101];
    int down[101][101];

    cin >> n;
    for ( int i = 0; i < n; i++ )
    {
        cin >> a >> b;
        CT[i] = a;
        CD[i] = b;
    }

    for ( int i = 0; i+1 < n; i++ )
    {
        dp[0][i]    = CT[i] * CD[i] * CT[i+1] * CD[i+1];
        if ( CT[i] * CD[i+1] < CT[i+1] * CD[i] )
        {
            top[0][i]   = CT[i];
            down[0][i]  = CD[i+1];
        }
        else
        {
            top[0][i]   = CT[i+1];
            down[0][i]  = CD[i];
        }
    }

    for ( int i = 1; i < n; i++ )
    {
        for ( int j = 0; j + i < n; j++ )
        {
            dp[i][j] = dp[i-1][j] + top[i-1][j] * down[i-1][j] * CT[j+i+1] * CD[j+i+1];
            if ( top[i-1][j] * CD[j+i+1] < CT[j+i+1] * down[i-1][j] )
            {
                top[i][j]   = top[i-1][j];
                down[i][j]  = CD[j+i+1];
            }
            else
            {
                top[i][j]   = CT[j+i+1];
                down[i][j]  = down[i-1][j];
            }

            if ( dp[i][j] > dp[i-1][j+1] + top[i-1][j+1] * down[i-1][j+1] * CT[j] * CD[j] )
            {
                dp[i][j] = dp[i-1][j+1] + top[i-1][j+1] * down[i-1][j+1] * CT[j] * CD[j];
                if ( top[i-1][j+1] * CD[j] < CT[j] * down[i-1][j+1] )
                {
                    top[i][j]   = top[i-1][j+1];
                    down[i][j]  = CD[j];
                }
                else
                {
                    top[i][j]   = CT[j];
                    down[i][j]  = down[i-1][j+1];
                }
            }
        }
    }

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

    return 0;
}

C++: 
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <memory>
#include <string>

#include <algorithm>
#include <complex>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <bitset>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
#define strtok_r strtok_s
#endif

#define ITER(c)             __typeof__((c).begin())
#define FOREACH(it, c)      for (ITER(c) it=(c).begin(); it != (c).end(); ++it)
#define RITER(c)            __typeof__((c).rbegin())
#define RFOREACH(it, c)     for (RITER(c) it=(c).rbegin(); it != (c).rend(); ++it)
#define REP(i, n)           REPEAT(i, 0, n)
#define RREP(i, n)          RREPEAT(i, 0, n)
#define REPEAT(i, k, n)     for(int i = (k); i < (k+n); ++i)
#define RREPEAT(i, k, n)    for(int i = (k)+(n)-1; i >= (k); --i)
#define FROMTO(i,f,t)       for(int i = f; i < t; i++)
#define ALL(c)              (c).begin(), (c).end()
#define LLPOW(p1,p2)        ((ll)pow((double)(p1), (int)p2))
#define ESIZEOF(A)          (sizeof(A)/sizeof((A)[0]))
#define CIN_NO_SYNC         do { cin.tie(0); ios_base::sync_with_stdio(false); } while(0)
#define GETSTR(p)           fgets((p), sizeof(p), stdin)
#define CHOMP(p)            do{ char *_q = (p) + strlen(p)-1; if(*_q == '\n' || *_q == '\r') *_q = 0; } while(0)
#define FILL(m,v)           memset(m, v, sizeof(m))
#define mp                  make_pair
#define pb                  push_back
template<class _T> _T MAX(_T p1,_T p2){return (p1>p2)?p1:p2;} 
template<class _T> _T MIN(_T p1,_T p2){return (p1<p2)?p1:p2;} 
template <class _Tv> inline string join(const _Tv &v,string sep=" "){
    ostringstream s;FOREACH(it,v){if(it!=v.begin())s<<sep;s<<*it;};return s.str();
}

typedef long long ll;
typedef unsigned long long ull;
typedef double D;
typedef complex<D> P;
#define X real()
#define Y imag()
#define EPS (1e-9)
#define DEQ(p1,p2) (abs((p1)-(p2)) < EPS)

#ifdef _DEBUG
template<class _Tv> inline void _prfe(const char *_n,const _Tv _c,bool _p=false){
    ITER(_c) _it=_c.begin();
    if(_p){cout<<_n<<" = ["<<endl;for(;_it!=_c.end();++_it)cout<<"    "<<*_it<<endl;cout<<"]"<<endl; }
    else{cout<<_n<<" = [ "<<*_it++;for(;_it!=_c.end();++_it)cout<<", "<<*_it;cout<<" ]"<<endl; }
}
#define pf printf
#define pr(n) do{cout<<#n" = "<<(n)<<endl;}while(0)
#define prfe(n) _prfe(#n, n)
#define prfep(n) _prfe(#n, n, true)
#define dbgchk(n) do{if(n)throw;}while(0)
#else
#define pf(...) /* */
#define pr(...) /* */
#define prfe(...) /* */
#define prfep(...) /* */
#define dbgchk(...) /* */
#endif

int dp[101][101];
const int inf = 1<<29;

int solve(vector<pair<int,int> > &v){
    int n = v.size();
    REP(i,n){
        REP(j,n) dp[i][j] = inf;
        dp[i][i] = 0;
    }
    RREP(l,n){
        FROMTO(u,l+1,n){
            FROMTO(k,l,u){
                dp[l][u] = MIN(dp[l][u],
                    dp[l][k] + v[l].first * v[k].second * v[k+1].first * v[u].second + dp[k+1][u]);
                pf("dp[%d][%d] = dp[%d][%d] + ... + dp[%d][%d]\n", l, u, l, k, k+1, u);
            }
        }
    }
    return dp[0][n-1];
}

int main(){
    int n;
    cin >> n;
    vector<pair<int,int> > v;
    REP(i,n){
        int a,b;
        cin >> a >> b;
        v.pb(mp(a,b));
    }
    cout << solve(v) << endl;
    return 0;
}

C++: #include<iostream>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
#define INF 1 << 21
#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;
        }
    }
}