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 <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 <deque>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <ctime>
#include <iterator>
#include <bitset>
#include <numeric>
#include <list>
#include <iomanip>
 
#if __cplusplus >= 201103L
#include <array>
#include <tuple>
#include <initializer_list>
#include <unordered_set>
#include <unordered_map>
#include <forward_list>
 
#define cauto const auto&
#else
 
#endif
 
using namespace std;
 
 
namespace{
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
 
typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;
 
#define VV(T) vector<vector< T > >
 
template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}
 
template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}
 
 
#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()
 
 
#define MOD 1000000007LL
#define EPS 1e-8
static const int INF=1<<24;
 
void mainmain(){
	int n;
	cin>>n;
	vector<pll> v(n);
	rep(i,n){
		cin>>v[i].F>>v[i].S;
	}
	vvll dp;
	initvv(dp,n+1,n+1,(ll)INT_MAX);
	rep(i,n){
		dp[i][i+1]=0;
	}
	reep(i,2,n+1){
		reep(j,0,n-i+1){
			reep(k,j+1,j+i){
				dp[j][i+j]=min(dp[j][i+j],dp[j][k]+dp[k][i+j]+v[j].F*v[k-1].S*v[k].F*v[i+j-1].S);
			}
		}
	}
	cout<<dp[0][n]<<endl;
}
 
 
 
}
main() try{
    mainmain();
}
catch(...){
}

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
#define MAX_N_P 5151

int n, dp[MAX_N][MAX_N];
vector<P> cards, cardsCpy;
bool useFlag[MAX_N];
map<P, int> toM;
int m[MAX_N_P][MAX_N_P];
vector<P> vec;

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

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();
    setToM();
    setM();
    P tmp, tmp2;
    //while(cin >>tmp.first >>tmp.second >>tmp2.first >>tmp2.second) cout <<m[ toM[tmp] ][ toM[tmp2] ] <<endl;
    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;
        }
    }
}

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;
        if(left != i) loop(left, i);
        if(i + 1 != right) loop(i + 1, right);
        int tmp = m[ toM[ cards[left] ] ][ toM[ cards[right] ] ] + dp[left][i] + dp[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;
}

void setToM(){
    int cnt = 0;
    for(int i = 0; i < cards.size(); i++){
        for(int j = i; j < cards.size(); j++){
            P tmp;
            tmp.first = cards[i].first;
            tmp.second = cards[j].second;
            toM.insert( map<P, int>::value_type(tmp, cnt) );
            cnt++;
            vec.push_back(tmp);
        }
    }
}

void setM(){
    for(int i = 0; i < vec.size(); i++){
        for(int j = i; j < vec.size(); j++){
            if(i == j){
                m[i][j] = 0;
            } else{
                m[i][j] = vec[i].first * vec[i].second * vec[j].first * vec[j].second;
            }
        }
    }
}

C++: #include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;

int dp[100][100], a[100], b[100];
int main() {
	int n; scanf("%d", &n);
	rep(i, n)scanf("%d%d", &a[i], &b[i]);
	fill(dp[0], dp[n], INT_MAX);
	rep(i, n)dp[i][i] = 0;
	for (int i = 1; i < n; i++)rep(j, n - i + 1) {
		if (dp[j][j + i - 1] == INT_MAX)continue;
		for (int k = 0; k < j; k++) {
			if (dp[k][j - 1] == INT_MAX)continue;
			dp[k][j + i - 1] = min(dp[k][j + i - 1], dp[k][j - 1] + dp[j][j + i - 1] + a[k] * b[j - 1] * a[j] * b[j + i - 1]);
		}
		for (int k = j + i; k < n; k++) {
			if (dp[j + i][k] == INT_MAX)continue;
			dp[j][k] = min(dp[j][k], dp[j][j + i - 1] + dp[j + i][k] + a[j] * b[j + i - 1] * a[j + i] * b[k]);
		}
	}
	printf("%d\n", dp[0][n - 1]);
}