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];

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], &dp[199][199] + 1, data(INF, P(0, 0)));
    rep(i, n){
      cin >> dp[i][1].second.first >> dp[i][1].second.second;
      dp[i][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].first + dp[k][i-(k-j)].first + mul(dp[j][k-j], dp[k][i-(k-j)]);
	  P m = get_minP(dp[j][k-j], dp[k][i-(k-j)]);
	  if(cost < dp[j][i].first || cost == dp[j][i].first
	     && m.first * m.second < dp[j][i].second.first * dp[j][i].second.second){
	    dp[j][i] = data(cost, m);
	  }
	}
      }
    }
    cout << dp[0][n].first << endl;
  }
  return 0;
}

C++: #include <cstring>
#include <iostream>
using namespace std;
const int MAX_N = 100;
int a[MAX_N], b[MAX_N];
int memo[MAX_N + 1][MAX_N + 1];

int getMinCost(int bottom, int top) {
	if (bottom == top) return 0;
	if (memo[bottom][top] != -1) {
		return memo[bottom][top];
	}
	int cost = 0x7ffffff;
	for (int i = bottom; i < top; i++) {
		cost = min(cost, getMinCost(bottom, i) + getMinCost(i + 1, top) + a[bottom] * b[i] * a[i + 1] * b[top]);
	}
	return memo[bottom][top] = cost;
}

int main () {
	int n;
	cin >> n;
	memset(memo, -1, sizeof(memo));
	for (int i = 0; i < n; i++) {
		cin >> a[i] >> b[i];
	}
	cout << getMinCost(0, n - 1) << endl;
	return 0;
}

C++: #include <iostream>
#include <string.h>
#include <vector>
#include <algorithm>
using namespace std;

const int SIZE = 100;

int main()
{
	int costs[ SIZE ][ SIZE ];
	int n;

	while( cin >> n ){
		for( int i = 0; i < n; ++i ){
			for( int j = 0; j < n; ++j ){
				costs[ i ][ j ] = 0x7fffffff;
			}
		}
		for( int i = 0; i < n; ++i ){
			costs[ i ][ i ] = 0;
		}
		vector< pair< int, int > > decks;
		for( int i = 0; i < n; ++i ){
			int f, b;
			cin >> f >> b;
			decks.push_back( make_pair( f, b ) );
		}
		/*
		for( int s = 0; s < n - 1; ++s ){
			int e = s + 1;
			costs[ s ][ e ] = decks[ s ].first * decks[ s ].second * decks[ e ].first * decks[ e ].second;
		}
		*/
		for( int span = 1; span < n; ++span ){
			for( int s = 0; s < n - span; ++s ){
				int e = s + span;
				for( int k = s; k < e; ++k ){
					costs[ s ][ e ] = min( costs[ s ][ e ], costs[ s ][ k ] + costs[ k + 1 ][ e ] + decks[ s ].first * decks[ k ].second * decks[ k + 1 ].first * decks[ e ].second );
				}
			}
		}
		cout << costs[ 0 ][ n - 1 ] << endl;
	}

	return 0;
}

C++: #include<iostream>
using namespace std;
typedef pair<int,int> P;
int n;
P A,B;
P t[100];
P u[100];
int dp[100];
int costA,costB;

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 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;}
  dp[0]=0;
  u[0]=t[0];

  dp[1]=cost(t[0],t[1]);
  u[1]=on(t[0],t[1]);
  if(n==2){cout<<dp[1]<<endl;return 0;}

  for(int i=2;i<n;i++){
    A=on(u[i-1],t[i]);
    costA=cost(u[i-1],t[i])+dp[i-1];
    
    B=on(u[i-2],on(t[i-1],t[i]));
    costB=cost(t[i-1],t[i]) + cost(u[i-2],on(t[i-1],t[i])) + dp[i-2];
    
    if(costA<costB || (costA==costB && A.first<B.first) ){
      u[i]=A;
      dp[i]=costA;
    }else{
      u[i]=B;
      dp[i]=costB;
    }
  }

  cout<<dp[n-1]<<endl;

  return 0;
}

C++: #include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x)  cerr << #x << " = " << (x) << endl;
using namespace std;

// O(n^3)
// dp[i][j] := [i,j]????¨???????????????????????°?????????????????????°
// i??????????????????(?????°????????°)??????(p[i - 1], p[i])
int matrixChainMultiplication(int n, pair<int, int> p[105]){
    int dp[105][105] = {{0}};
    range(seg, 2, n + 1){ //?¨????????????????????????°
        range(i, 0, n - seg + 1){
            int j = i + seg - 1;
            dp[i][j] = INT_MAX;
            range(k,i,j){
                //cout << i << ' ' << k << ' ' << j << endl;
                //show(dp[i][k])
                //show(dp[k + 1][j])
                //show(p[i].second * p[k].first * p[k + 1].second * p[j].first);
                int cost = dp[i][k] + dp[k + 1][j] + p[i].second * p[k].first * p[k + 1].second * p[j].first;
                //show(cost)
                dp[i][j] = min(dp[i][j], cost);
            }
        }
    }
    return dp[0][n - 1];
}

int main(){
    int n;
    cin >> n;

    pair<int, int> p[105];
    rep(i,n) cin >> p[i].second >> p[i].first;
    cout << matrixChainMultiplication(n,p) << endl;
}