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;
	}
	ll ans=0;
	if(n==1){
		cout<<0<<endl;
		return;
	}
	while(v.size()!=1){
		// rep(i,v.size()){
			// cout<<v[i].F<<" "<<v[i].S<<endl;
		// }
		// cout<<endl;
		ll x=-1,y=0;
		rep(i,v.size()-1){
			if(y<v[i].F*v[i+1].S){
				x=i;
				y=v[i].F*v[i+1].S;
			}
			if(y<v[i].S*v[i+1].F){
				x=i;
				y=v[i].S*v[i+1].F;
			}
		}
		ans+=v[x].F*v[x].S*v[x+1].F*v[x+1].S;
		if(v[x].F*v[x+1].S>v[x].S*v[x+1].F) v[x]=mkp(v[x+1].F,v[x].S);
		else v[x]=mkp(v[x].F,v[x+1].S);
		reep(i,x+1,v.size()-1){
			swap(v[i],v[i+1]);
		}
		v.pop_back();
	}
	cout<<ans<<endl;
}



}
main() try{
    mainmain();
}
catch(...){
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
long memo[100][100];
long a[100],b[100];
long f(int l,int r)
{
	if(memo[l][r]>0)return memo[l][r];
	else if(l+1==r)return a[l]*b[l]*a[r]*b[r];
	long ans=1LL<<32;
	for(int i=l+1;i<r;i++)
	{
		long now;
		if(i==l+1)
		{
			now=f(i,r)+a[l]*b[l]*a[i]*b[r];
		}
		else if(i==r-1)
		{
			now=f(l,i)+a[l]*b[i]*a[r]*b[r];
		}
		else
		{
			now=min(f(i,r)+a[l]*b[l]*a[i]*b[r],f(l,i)+a[l]*b[i]*a[r]*b[r]);
		}
		ans=min(ans,now);
	}
	return memo[l][r]=ans;
}
main()
{
	int n;
	cin>>n;
	for(int i=0;i<n;i++)cin>>a[i]>>b[i];
	cout<<f(0,n-1)<<endl;
}

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

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<21

using namespace std;

int main()
{
	int n;
	cin >> n;
	
	vector<pair<int,int> > v;
	long long dp[101][101];

	rep(i,n)
	{
		int a,b;
		cin >> a >> b;
		v.push_back(make_pair(a,b));
	}

	rep(i,n+1)
	{
		rep(j,n+1)
		{
			dp[i][j] = INF;
		}
		dp[i][i] = 0;
	}
	
	REP(i,1,n)
	{
		rep(j,n-i+1)
		{
			REP(k,j,j+i)
			{
				int cost = v[j].first * v[k].second * v[k+1].first *  v[j+i].second;
				dp[j][j+i] = min(dp[j][j+i],cost+dp[j][k]+dp[k+1][j+i]);
			}
		}
	}
	
	cout << dp[0][n-1] << endl;

	return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
ll n;
ll INF = (1LL<<34);
P t[100];
ll dp[100][100];
  
P on(P x,P y){
  return P(x.first,y.second);
}
  
ll cost(P x,P y){
  return x.first*x.second*y.first*y.second;
}
  
int compute(int a,int b){
  if(dp[a][b]!=INF)return dp[a][b];
  if(a==b)return 0;

  ll sum;
  P X,Y;
  for(int i=a;i<b;i++){
    X=on(t[a],t[i]);
    Y=on(t[i+1],t[b]);
      
    sum=cost(X,Y);
    sum = compute(a,i)+ sum +compute(i+1,b) ;
    dp[a][b]=(sum<dp[a][b]?sum:dp[a][b]);
  
  }
  return dp[a][b];
}
  
int main(){
  
  while(cin>>n&&n){
    for(int i=0;i<n;i++)cin>>t[i].first>>t[i].second;
    for(int i=0;i<100;i++)for(int j=0;j<100;j++)dp[i][j]=INF;
    cout<<compute(0,n-1)<<endl;
  }
  return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
ll n;
ll INF = (1LL<<32);
P A,B;
P t[100];
ll dp[100][100];

P on(P x,P y){
  return P(x.first,y.second);
}
int cost(P x,P y){
  return x.first*x.second*y.first*y.second;
}

int compute(int a,int b){
  if(dp[a][b]!=-1)return dp[a][b];
  if(a==b)return 0;

  if(a+1==b)return cost(t[a],t[b]);
  if(a+2==b)return min(cost(t[a],t[a+1])+cost(on(t[a],t[a+1]),t[a+2]) , cost(t[a+1],t[a+2])+cost(t[a],on(t[a+1],t[a+2])) );

  ll res=INF;
  ll cost;

  for(int i=a+1;i+1<b;i++){

    cost=t[a].first*t[i].second*t[i+1].first*t[b].second;

    res=min(res, compute(a,i)+ cost +compute(i+1,b) );

  }
  return res;
}

int main(){
  cin>>n;
  for(int i=0;i<n;i++)cin>>t[i].first>>t[i].second;

  if(n==1){cout<<"0"<<endl;return 0;}

  for(int i=0;i<100;i++)for(int j=0;j<100;j++)dp[i][j]=-1;

  cout<<compute(0,n-1)<<endl;

  return 0;
}