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 <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<long long> vll;
typedef pair<int,int> pint;

#define REP(i, n) for ((i) = 0; (i) < (int)(n); ++(i))
#define ALL(s) (s).begin(),(s).end()
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P) 
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P) 
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P) 
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; }



const int MAX = 110;
const ll INF = 1LL<<50;
int n;
int h[MAX], l[MAX];
ll dp[MAX][MAX];

int main() {
    while (cin >> n) {
        int i, j, k, b;

        for (i = 0; i < n; ++i) cin >> h[i] >> l[i];
        for (i = 0; i < MAX; ++i) for (j = 0; j < MAX; ++j) dp[i][j] = INF;
        
        for (i = 0; i < n; ++i) {
            dp[i][i+1] = 0;
            dp[i][i+2] = h[i] * l[i] * h[i+1] * l[i+1];
        }
        for (b = 3; b <= n; ++b) {
            for (i = 0; i + b <= n; ++i) {
                j = i+b;
                for (k = i+1; k+1 <= j; ++k) {
                    chmin(dp[i][j], dp[i][k] + dp[k][j] + h[i] * l[k-1] * h[k] * l[j-1]);
                }
            }
        }
        
        cout << dp[0][n] << endl;
    }
    
    return 0;
}

C++: #define _USE_MATH_DEFINES
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>

using namespace std;

typedef long long ll;
typedef pair<int,int> P;

static const double eps = 10e-6;

int dp[101][101];

int main(){
	int n;
	scanf("%d",&n);

	vector<P> cards;
	memset(dp,0x2,sizeof(dp));
	for(int i=0;i<n;i++){
		int a,b;
		scanf("%d %d",&a,&b);
		cards.push_back(P(a,b));
	}

	for(int i=0;i<=n;i++) dp[i][i]=0;

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

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

C++: #include<iostream>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
#define P pair<int, int>
#define MAX_N 101
#define INF 1 << 21

int n, dp[MAX_N][MAX_N];
vector<P> cards, cardsCpy;
bool useFlag[MAX_N];

void setDp();
void loop(int left, int right);
void disp();
void dispCards();
void resetUseFlag();

int main(){
    resetUseFlag();
    cards.clear();
    cin >>n;
    for(int i = 0; i < n; i++){
        int x, y;
        cin >>x >>y;
        cards.push_back( make_pair(x, y) );
    }
    cardsCpy = cards;
    setDp();
    loop(0, n - 1);
    cout <<dp[0][n - 1] <<endl;
    return 0;
}

void setDp(){
    for(int i = 0; i < MAX_N; i++){
        for(int j = 0; j < MAX_N; j++){
            if(i == j) dp[i][j] = 0;
            else dp[i][j] = INF;
        }
    }
}

/*int loop(int left, int right, int deep){
    for(int i = left; i < right; i++){
        cards[i].first = cardsCpy[i].first;
        useFlag[left] = false;
        if(right != (left + 1)) loop(i, i + 1, deep + 1);
        cout <<i <<", " <<i + 1 <<", " <<deep <<endl;
        if(!useFlag[i]){
            cout <<"!cal" <<endl;
            int nowCost = cards[i].first * cards[i].second * cards[i + 1].first * cards[i + 1].second + dp[left][i] + dp[i + 1][right];
            cout <<"left = " <<left <<" i = " <<i <<" right = " <<right <<" " <<cards[i].first <<" * " <<cards[i].second <<" * " <<cards[i].first <<" * " <<cards[i].second <<" + " <<dp[left][i] << " + " <<dp[i + 1][right] <<endl;
            cout <<"nowCost = " <<nowCost <<endl;
            if(nowCost < dp[left][right]){
                cout <<"! dp[" <<left <<"][" <<right <<"]" <<endl;
                dp[right][left] = nowCost;
            }
            cards[i + 1].first = cards[i].first;
            useFlag[i] = true;
            cout <<"useFlag[" <<i <<"] = true" <<endl;
            dispCards();
            cout <<endl;
        }
    }
    return dp[left][right];
}*/


void loop(int left, int right){
    for(int i = left; i < right; i++){
        cards[i + 1].first = cardsCpy[i + 1].first;
        //cout <<"! " <<i + 1 <<endl;
        loop(left, i);
        loop(i + 1, right);
        int tmp = cards[left].first * cards[left].second * cards[right].first * cards[right].second + dp[left][i] + dp[i + 1][right];
        //cout <<cards[left].first <<" * " <<cards[left].second <<" * " <<cards[right].first <<" * " <<cards[right].second <<" + " <<dp[left][i] <<" + " <<dp[i + 1][right] <<" = " <<tmp <<endl;
        if(dp[left][right] > tmp) dp[left][right] = tmp;
        cards[i + 1].first = cards[i].first;
    }
    //cout <<left <<", " <<right <<", " <<dp[left][right] <<endl;
    //dispCards();
    //disp();
    //cout <<endl;
}


void disp(){
    for(int i= 0; i < n; i++){
        for(int j = 0; j < n; j++){
            cout <<dp[i][j] <<" ";
        }
        cout <<endl;
    }
}

void dispCards(){
    for(int i = 0; i < cards.size(); i++){
        cout <<"(" <<cards[i].first <<", " <<cards[i].second <<") ";
    }
    cout <<endl;
}

void resetUseFlag(){
    for(int i = 0; i < MAX_N; i++) useFlag[i] = false;
}