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<int, int> 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){
  int a = d1.second.first * d2.second.second;
  int 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;
    }
    rep2(i, 2, n + 1){
      rep(j, n - i + 1){
	rep2(k, j + 1, j + i){
	  ll cost = dp[j][k-j][0].first + dp[k][i-(k-j)][0].first 
	    + mul(dp[j][k-j][0], dp[k][i-(k-j)][0]);
	  
	  P m1 = get_minP(dp[j][k-j][0], dp[k][i-(k-j)][1]);
	  P m2 = get_minP(dp[j][k-j][1], dp[k][i-(k-j)][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[0][n][0].first << endl;
  }
  return 0;
}

C++: #include<iostream>
#include<vector>
using namespace std;

int a[100],b[100];
int n;

int pile(int l,int r){
  int res = 0;
  while(l!=r){
    int pos = l+1;
    for(int i=l+2;i<=r;i++){
      if(b[pos]>b[i])pos = i;
    }
    if(pos != l+1){
      int N = n;
      res += pile(l+1,pos);
      r -= N-n;
    }
    res += a[l]*b[l]*a[l+1]*b[l+1];
    b[l] = b[l+1];
    n--;
    for(int i=l+1;i<n;i++){
      a[i] = a[i+1];
      b[i] = b[i+1];
    }
    r--;    
  }

  return res;
}

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

  cout << pile(0,n-1) << endl;
}

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;
	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 <iostream>
#include <vector>

using namespace std;
typedef pair<int, int> P;
static const int INF = 99999999;

typedef pair<int,int> P;

unsigned int dp[101][101];

void solve()
{
	int n; 
	cin >> n;
	vector<P> v(n);
	for(int i = 0; i < n; ++i)
	{
		cin >> v[i].first >> v[i].second;
	}

	for(int i = 0; i <= n; ++i)
	{
		for(int j = 0; j <= n; ++j)
		{
			dp[i][j] = INF;
		}	
		dp[i][i] = 0;
	}
	for(int i = 1; i < n; ++i)//ツ閉敖づ個個按津ィ
	{
		for(int j = 0; j < n - i; ++j)//ツ開ツ始ツ暗環置ツづ個個按津ィ
		{
			for(int k = j; k < j + i; ++k)//ツ静伉断ツ暗環置ツづ個個按津ィ
			{
				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;
}

int main(void)
{
	solve();
	return(0);
}