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>
#include <cstdlib>
#include <memory.h>
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, m, n) for(int i = m; i < n; i++)
using namespace std;


typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P> data;
const ll INF = 1LL <<50;

data dp[200][200][2];

ll mul(data d1, data d2){
  return d1.second.first * d1.second.second * d2.second.first * d2.second.second;
}

P get_minP(data d1, data d2){
  ll a = d1.second.first * d2.second.second;
  ll b = d2.second.first * d1.second.second;
  if(a < b) return P(d1.second.first, d2.second.second);
  else return P(d2.second.first, d1.second.second);
}

int main(){
  int n;
  while(cin >> n && n){
    fill(&dp[0][0][0], &dp[199][199][1] + 1, data(INF, P(0, 0)));
    rep(i, n){
      cin >> dp[i][1][0].second.first >> dp[i][1][0].second.second;
      dp[i][1][1].second.first = dp[i][1][0].second.first;
      dp[i][1][1].second.second = dp[i][1][0].second.second;
      dp[i][1][0].first = dp[i][1][1].first= 0;
    }
    for(int i = 2; i <= n; i++){
      for(int j = 0; j <= n - i; j++){
	for(int k = 1; k < i; k++){
	  ll cost = dp[j][k][0].first + dp[j+k][i-k][0].first 
	    + mul(dp[j][k][0], dp[j+k][i-k][0]);
	  
	  P m1 = get_minP(dp[j][k][0], dp[j+k][i-k][1]);
	  P m2 = get_minP(dp[j][k][1], dp[j+k][i-k][0]);
	  
	  if(cost < dp[j][i][0].first || 
	     cost == dp[j][i][0].first && m1.first * m1.second < 
	     dp[j][i][0].second.first * dp[j][i][0].second.second){
	    
	    dp[j][i][0] = dp[j][i][1] = data(cost, m1);
	    
	  }else if(cost == dp[j][i][0].first && m1.first * m1.second == 
		   dp[j][i][0].second.first * dp[j][i][0].second.second){
	    
	    if(m1.first < dp[j][i][0].second.first){
	      dp[j][i][0].second = m1;
	    }
	    if(m1.second < dp[j][i][1].second.second){
	      dp[j][i][1].second = m1;
	    }
	    
	    if(cost < dp[j][i][0].first || 
	       cost == dp[j][i][0].first && m2.first * m2.second < 
	       dp[j][i][0].second.first * dp[j][i][0].second.second){
	      
	      dp[j][i][0] = dp[j][i][1] = data(cost, m2);

	    }else if(cost == dp[j][i][0].first && m2.first * m2.second == 
		     dp[j][i][0].second.first * dp[j][i][0].second.second){
	      if(m2.first < dp[j][i][0].second.first){
		dp[j][i][0].second = m2;
	      }
	      if(m2.second < dp[j][i][1].second.second){
		dp[j][i][1].second = m2;
	      }
	    } 
	    
	  }
	}
	//	cout << dp[j][i][0].first << " " << "i " << i << " j " << j << endl;
      }
    }
    cout << dp[0][n][0].first << endl;
  }
  return 0;
}

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,3){
		cin>>v[i].F>>v[i].S;
	}
	ll ans=0;
	while(v.size()!=1){
		// rep(i,v.size()){
			// cout<<v[i].F<<" "<<v[i].S<<endl;
		// }
		// cout<<endl;
		ll x=-1,y=0,z=0;
		bool f=false;
		rep(i,v.size()-1){
			if(y<v[i].F*v[i+1].S){
				x=i;
				y=v[i].F*v[i+1].S;
				z=y*v[i].S*v[i+1].F;
				f=true;
			}
			else if(y==v[i].F*v[i+1].S&&z>y*v[i].S*v[i+1].F){
				x=i;
				y=v[i].F*v[i+1].S;
				z=y*v[i].S*v[i+1].F;
				f=true;
			}
			if(y<v[i].S*v[i+1].F){
				x=i;
				y=v[i].S*v[i+1].F;
				z=y*v[i].F*v[i+1].S;
				f=false;
			}
			else if(y==v[i].S*v[i+1].F&&z>y*v[i].F*v[i+1].S){
				x=i;
				y=v[i].S*v[i+1].F;
				z=y*v[i].F*v[i+1].S;
				f=false;
			}
		}
		ans+=v[x].F*v[x].S*v[x+1].F*v[x+1].S;
		if(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 <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef complex<double> P;
static const double EPS = 1e-8;
static const int INF = (int)1e8;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
unsigned int dp[101][101];
int main(void){
  int n;  cin>>n;
  vector<PII> v(n);
  REP(i,n)cin>>v[i].first>>v[i].second;

  REP(i,n+1)REP(j,n+1)dp[i][j] = (i==j)?0:INF;

  FOR(i,1,n){
    REP(j,n-i+1){
      FOR(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;
}