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 <vector>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
using namespace std;
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EACH(t,i,c) for(t::iterator i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
const double EPS = 1e-10;
const double PI  = acos(-1.0);

ll dp[101][101];
ll a[101],b[101];

const ll MAX=INT_MAX;
ll dfs(int l,int r){
	if(dp[l][r]!=MAX){
		return dp[l][r];
	}
	if(r-l==1){
		return dp[l][r]=min(dp[l][r],a[l]*b[l]*a[r]*b[r]);
	}else if(r==l){
		return dp[l][r]=0;
	}
	ll best=MAX;
	FOR(i,l,r){
		ll cost=a[l]*b[i]*a[i+1]*b[r];
		best=min(best,dfs(l,i)+dfs(i+1,r)+cost);
	}
	return dp[l][r]=min(dp[l][r],best);
}

int main(){
	int n;
	while(cin>>n,n){
		REP(i,n+1)REP(j,n+1){
			dp[i][j]=MAX;
		}

		REP(i,n){
			cin>>a[i]>>b[i];
		}

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

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

int main(){
	int n;
	int a[100],b[100];
	int cb[100][100];
	//b > a
	for(int i = 0; i < 100; i++){
		for(int j = i; j < 100; j++){
			cb[i][j] = 2147483647;
		}
	}

	cin >> n;

	for (int i = 0; i < n; i++){
		cin >> a[i] >> b[i];
	}		// 入力

	for(int i = 0; i < n; i++){
		cb[i][i] = 0;
		if(i != (n - 1)){
			cb[i][i+1] = a[i] * b[i] * a[i+1] * b[i+1];
		}
	}
	for(int i = 0; i < n; i++){
		for(int j = i; j < (n-i); j++){		//cd[i][j]
			for(int k = i; k < (j-1); k++){
				cb[i][j] = min(cb[i][j],cb[i][k]+cb[k+1][j]+a[i]*b[k]*a[k+1]*b[j]);
			}
		}
	}
	cout << cb[0][n-1] << endl;
	return 0;
}

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


int cost(int,int,int,int);
int main(){
	int n;
	int a[100],b[100];
	int cb[100][100];
	//b > a
	for(int i = 0; i < 100; i++){
		for(int j = i; j < 100; j++){
			cb[i][j] = 2147483647;
		}
	}

	cin >> n;

	for (int i = 0; i < n; i++){
		cin >> a[i] >> b[i];
	}		// 入力

	for(int i = 0; i < n; i++){
		cb[i][i] = 0;
		if(i != (n - 1)){
			cb[i][i+1] = a[i] * b[i] * a[i+1] * b[i+1];
		}
	}
	for(int i = 0; i < n; i++){
		for(int j = i; j < n; j++){		//cd[i][j]
			for(int k = i; k < j; k++){
				if(cb[i][j] > cb[i][k]+cb[k+1][j]+a[i]*b[k]*a[k+1]*b[j]){
					cb[i][j] = cb[i][k]+cb[k+1][j]+a[i]*b[k]*a[k+1]*b[j];
				}
			}
		}
	}
	cout << cb[0][n-1] << endl;
	return 0;
}

int cost(int a1,int b1,int a2,int b2){
	int co;
	co = a1 * b1 * a2 * b2;
	return co;
}

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

typedef pair <int, int> PII;
typedef vector <PII> VPII;
typedef long long LL;
const int NONE = -1;
int n;
VPII P;
LL memo[101][101];

LL solve( int l, int r ) {
    if ( memo[l][r] != NONE ) return memo[l][r];
    if ( l == r ) return memo[l][r] = 0;
    LL res = 1LL<<60;
    for ( int i = l; i < r; ++ i ) {
        res = min( res, solve( l, i ) + solve( i + 1, r ) + P[l].first * P[i].second * P[i+1].first * P[r].second );
    }
    return memo[l][r] = res;
}

int main() {
    cin >> n;
    P = VPII(n);
    for ( int i = 0; i < n; ++ i ) {
        cin >> P[i].first >> P[i].second;
    }
    for ( int i = 0; i < n; ++ i ) {
        for ( int j = 0; j < n; ++ j ) {
            memo[i][j] = NONE;
        }
    }
    cout << solve( 0, n - 1 ) << endl;
    return 0;
}

C++: #include <stdio.h>
#include <algorithm>
#include <iostream>
#include <stack>
#include <vector>
#include <math.h>
#include <queue>

using namespace std;

int main(){
	int n,minimum,tmp;
	scanf("%d",&n);
	int table[n][n];
	int data[n][2];

	for(int i=0;i<n;i++){
		for(int k=0;k<n;k++)table[i][k] = 0;
		scanf("%d %d",&data[i][0],&data[i][1]);
	}

	for(int col = 0; col <= n-2; col++){
		table[1][col] = data[col][0]*data[col][1]*data[col+1][0]*data[col+1][1];
	}

	for(int row = 2; row <= n-1; row++){
		for(int col = 0; col <= n-(row+1); col++){
			minimum = 2147483647;

			for(int div = 0; div <= row-1;div++){
				tmp = (table[div][col]+table[row-div-1][col+div+1])+(data[col][0]*data[col+div][1]*data[col+div+1][0]*data[col+row][1]);
				minimum = min(minimum,tmp);
			}
			table[row][col] = minimum;
		}
	}

	printf("%d\n",table[n-1][0]);

    return 0;
}

C++: #include <cstdio>
#include <algorithm>
#include <cstring>
#define INF (1LL<<31)-1
using namespace std;
int n;
int a[101],b[101];
int dp[101][101];

int main(void){
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d%d",&a[i],&b[i]);
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<=n;j++){
			dp[i][j]=INF;
		}
		dp[i][i+1]=0;
	}
	for(int i=2;i<=n;i++){
		for(int j=0;j<=n-i;j++){
			for(int k=1;k<i;k++){
				dp[j][j+i]=min(dp[j][j+i],dp[j][j+k]+dp[j+k][j+i]+a[j]*b[j+k-1]*a[j+k]*b[j+i-1]);
			}
		}
	}
	printf("%d\n",dp[0][n]);
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#define mod 1000000007
typedef long long ll;
using namespace std;
int n;
vector<ll> v;
int main(){
  cin>>n;
  for(int i=0;i<2*n;i++){
    ll a;cin>>a;
    v.push_back(a);
  }
  ll ans=0;
  while(v.size()>2){
    ll maxi=0,cnt;
    vector<ll>::iterator it;
    for(int i=1;i<v.size()-2;i+=2){
      if(maxi<v[i]*v[i+1]){
	maxi=v[i]*v[i+1];
	cnt=v[i-1]*v[i]*v[i+1]*v[i+2];
	it=v.begin()+i;
      }
    }
    ans+=cnt;
    v.erase(it);
    v.erase(it);
  }
  cout<<ans<<endl;
  return 0;
}

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

struct cell {
	int top, bottom;
};
bool operator<(cell a, cell b) {
	return (a.top!=b.top)? a.top<b.top : a.bottom<b.bottom;
}

int main()
{
	int n, a, b, i;
	int min, cost;
	vector<cell> v;

	cin>>n;
	v.resize(n);
	for (i = 0; i < n; i++) {
		cin>>a>>b;
		v[i].top = a;
		v[i].bottom = b;
	}

	sort(v.begin(), v.end());
	min = INT_MAX;
	do {
		cost = 0;
		for (i = 0; i < n-1; i++) cost += v[0].top*v[i].bottom * v[i+1].top*v[i+1].bottom;
		if (min > cost) min = cost;
	} while (next_permutation(v.begin(), v.end()));
	cout<<min<<endl;

	return 0;
}