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>

using namespace std;

typedef long long ll;
const ll INF = (1LL<<50);
const int MAX = 101;

ll dp[MAX][MAX];
int T[MAX],B[MAX];

int main(){

  int n;
  cin >> n;
  for(int i = 0; i < n; i++) cin >> T[i] >> B[i];
 
  fill(dp[0],dp[0]+MAX*MAX,INF);
  for(int i = 0; i < MAX; i++) dp[i][i] = 0;

  for(int i = 0; i < n; i++)
    for(int j = i; j < n; j++)
      for(int k = i+1; k <= j; k++)
	dp[i][j] = min(dp[i][j],dp[i][k-1]+dp[k][j]+B[i]*T[k-1]*B[k]*T[j]);
  
  cout << dp[0][n-1] << endl;
  return 0;
}

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

using namespace std;

vector<pair<int, int> > cards;

main(){
  int n;
  cin >> n;
  for(int i=0;i<n;i++){
    int a, b;
    cin >> a >> b;
    cards.push_back(make_pair(a, b));
  }
  int ans = 0;
  while(cards.size() > 1){
    //cout << ans << endl;
    /*for(int i=0;i<cards.size();i++){
      cout << cards[i].first << ' ' << cards[i].second << endl;
      }cout << endl;*/
    int fmax = 0, smax = 0;
    int pf, ps;
    for(int i=0;i<cards.size();i++){
      if(fmax < cards[i].first){
	fmax = cards[i].first;
	pf = i;
      }else if(fmax == cards[i].first && cards[pf].second > cards[i].second){
	pf = i;
      }
      if(smax < cards[i].second){
	smax = cards[i].second;
	ps = i;
      }else if(smax == cards[i].second && cards[ps].first > cards[i].first){
	ps = i;
      }
    }
    if(pf == ps){
      if(cards[pf].first > cards[ps].second){
	smax = 0;
	for(int i=0;i<cards.size();i++){
	  if(i == pf) continue;
	  if(smax < cards[i].second){
	    smax = cards[i].second;
	    ps = i;
	  }else if(smax == cards[i].second && cards[ps].first > cards[i].first){
	    ps = i;
	  }
	}
      }else if(cards[pf].first < cards[ps].second){
	fmax = 0;
	for(int i=0;i<cards.size();i++){
	  if(i == ps) continue;
	  if(fmax < cards[i].first){
	    fmax = cards[i].first;
	    pf = i;
	  }else if(fmax == cards[i].first && cards[pf].second > cards[i].second){
	    pf = i;
	  }
	}
      }else{
	int ffmax = 0;
	int ssmax = 0;
	int pff, pss;
	for(int i=0;i<cards.size();i++){
	  if(i == pf || i == ps) continue;
	  if(ffmax < cards[i].first){
	    ffmax = cards[i].first;
	    pff = i;
	  }else if(ffmax == cards[i].first && cards[pff].second > cards[i].second){
	    pff = i;
	  }
	  if(ssmax < cards[i].second){
	    ssmax = cards[i].second;
	    pss = i;
	  }else if(ssmax == cards[i].second && cards[pss].first > cards[i].first){
	    pss = i;
	  }
	}
	if(ffmax >= ssmax){
	  fmax = ffmax;
	  pf = pff;
	}else if(ffmax < ssmax){
	  smax = ssmax;
	  ps = pss;
	}
      }
    }
    vector<pair<int, int> >::iterator ite = cards.begin();
    int a = cards[ps].first, b = cards[pf].second;
    ans += fmax * b * a * smax;
    //cout << "========= " << a << ' ' << b << endl;
    if(pf < ps){
      cards[ps] = make_pair(a, b);
      for(int i=0;i<pf;i++) ++ite;
      cards.erase(ite);
    }else{
      cards[pf] = make_pair(a, b);
      for(int i=0;i<ps;i++) ++ite;
      cards.erase(ite);
    }
  }
  cout << ans << endl;
  return 0;
}

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

using namespace std;

struct DATA{
  int a;
  int b;
};

DATA MakeOneData(int, int);
int CalculateSum(vector<DATA>);
bool Compare1(const DATA&, const DATA&);
bool Compare2(const DATA&, const DATA&);

int main(){
  int i, n, a, b;
  vector<DATA> data;

  cin >> n;
  for(i=0; i<n; ++i){
    cin >> a >> b;
    data.push_back(MakeOneData(a, b));
  }
  cout << CalculateSum(data) << endl;
  return 0;
}

DATA MakeOneData(int a, int b){
  DATA data;
  data.a = a;
  data.b = b;
  return data;
}

int CalculateSum(vector<DATA> data){
  int sum = 0, a1, b1, a2, b2;

  while(1){
    sort(data.begin(), data.end(), Compare1);
    a1 = data[0].a;
    b1 = data[0].b;
    data.erase(data.begin());

    sort(data.begin(), data.end(), Compare2);
    a2 = data[0].a;
    b2 = data[0].b;
    data.erase(data.begin());

    sum += (a1 * b1 * a2 * b2);
    if(data.size() == 0) return sum;
    data.push_back(MakeOneData(a2, b1));
  }
}

bool Compare1(const DATA& x, const DATA& y){
  if(x.a == y.a) return x.b < y.b;
  else return x.a > y.a;
}

bool Compare2(const DATA& x, const DATA& y){
  if(x.b == y.b) return x.a < y.a;
  else return x.b > y.b;
}

C++: #include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cctype>

#include <iostream>
#include <vector>
#include <deque>
#include <queue>
#include <map>
#include <algorithm>

using namespace std;

#define rep(i, n) for(int i = 0; i < n; i++)
#define repi(i,l,n) for(int (i)=(int)(l);(i)<(int)(n);(i)++)
#define d_arr(arr, h, w) rep(i,(h) ){ cout << "["; rep(j,(w) ) { cout << (arr)[i][j] << ", "; } cout << "]" << endl;}
#define IN(x,s,g) ((x) >= (s) && (x) < (g))
#define ISIN(x,y,w,h) (IN((x),0,(w)) && IN((y),0,(h)))
#define print(x) printf("%d\n",x);

typedef pair<int ,int> P;

/*
1行目 カードの山の個数n(100以下の正の整数)
2行目 左から1番目の山の情報 a b（整数；半角空白区切り）
a 山の1番上のカードに書かれた数
b 山の1番下のカードに書かれた数
3行目 左から2番目の山の情報a b（整数；半角空白区切り）
：
：
n+1行目 左からn番目の山の情報a b（整数；半角空白区切り）
 */


//dp[l][r] = index-lから始まって、index-rで終わる配列の最小値コスト
//

int dp[100][100] = {0};

int main()
{
    int n;
    cin >> n;
    vector<int> a,b;
    rep(i,n) 
    {
        int tmp;
        cin >> tmp;
        a.push_back(tmp);
        cin >> tmp;
        b.push_back(tmp);
    }

    rep(i,100){ rep(j,100){ dp[i][j] = 100000; } }
    rep(i,100){  dp[i][i+1] = 0;  }
    repi(len,1,a.size()+1)
    {
        rep(l,n)
        {
            if( n  < l + len  ) { break; }
            int r = l + len;
            repi(m,l+1,r)
            {
                cout << "(m,l,r) = (" << m << "," << l << " " << r << ")" << endl;
                dp[l][r] = min(dp[l][r],  dp[l][m] + dp[m][r] + a[l]*b[m-1]*a[m]*b[r-1]);
            }
        }
    }
    //rep(i,n+1){
    //    rep(j,n+1){ 
    //    
    //        cout << dp[i][j] << ", ";
    //    } 
    //    cout << endl;
    //}
    cout << dp[0][n] << endl;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
#define INF 2147483647
int dp[120][120];
int n,a[120],b[120];
int main(){
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a[i]>>b[i];
	}
	for(int i=0;i<n;i++){
		dp[i][i]=0;
		for(int j=0;j<n;j++){
			if(i!=j)dp[i][j]=INF;
		}
	}
	for(int i=0;i<=n;i++){
		for(int j=0;j<n;j++){
			for(int k=0;k<n;k++){
				for(int l=j;l<=k-1;l++){
					if(dp[j][l]==INF || dp[l+1][k]==INF)continue;
					int S=dp[j][l]+dp[l+1][k];
					dp[j][k]=min(dp[j][k],S+a[j]*b[l]*a[l+1]*b[k]);
				}
			}
		}
	}
	cout<<dp[0][n-1]<<endl;
	return 0;
}