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<vector>
#include<string>
#include<algorithm>
#include<map>

using namespace std;

int dp[1024][1024], num;
struct deck { int top, bottom; };
const int  INF = (1 << 30);

int main()
{

	cin >> num;
	vector<deck> mount(128);

	for(int i = 0; i < num; i++)
	{
		cin >> mount[i].top >> mount[i].bottom;

		for(int j = 0; j < num; j++)
		{
			if(i == j) continue;
			dp[i][j] = INF;
		}

	}


	for(int w = 1; w < num; w++)
	{
		for(int i = 0; i + w < num; i++)
		{
			int j = i + w;
			for(int k = i; k < j; k++)
			{
				dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + mount[i].top * mount[k].bottom * mount[k+1].top * mount[j].bottom );
			}
		}
	}

	cout << dp[0][num -1] << endl;
}

C++: #include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

int main() {
    int n;
    cin >> n;
    vector<int> A(n), B(n);
    vector<vector<ll>> dp(n, vector<ll>(n+1, INF<ll>));
    for (int i = 0; i < n; ++i) {
        cin >> A[i] >> B[i];
        dp[i][i+1] = 0;
    }
    for (int len = 2; len <= n; ++len) {
        for (int i = 0, j = len; j <= n; ++i, ++j) {
            for (int k = 1; k < j; ++k) {
                dp[i][j] = min(dp[i][j], dp[i][k]+dp[k][j]+A[i]*B[k-1]*A[k]*B[j-1]);
            }
        }
    }
    cout << dp[0][n] << "\n";
    return 0;
}

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

#define REP(i,n,m) for(int i=n;i<m;i++)
#define rep(i,n) REP(i,0,n)
#define EREP(i,n,m) for(int i=n;i<=m;i++)
#define erep(i,n) EREP(i,0,n)

typedef pair<int,int> P;

unsigned int dp[101][101];

int main(void){
  int n; cin>>n;
  vector<P> v(n);
  rep(i,n) cin>>v[i].first>>v[i].second;

  erep(i,n){
    erep(j,n) dp[i][j] = INT_MAX;
    dp[i][i] = 0;
  }

  REP(i,1,n){ //???????±????
    EREP(j,0,n-i){ //????§???????????±????
      REP(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;
}

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;

typedef pair<int,int> P;

int dp[101][101];
vector<P> v;

const int INF=(1<<31)-1;

int dfs(int d,int u){
	if(u==d)
		return 0;
	if(dp[d][u]!=INF)
		return dp[d][u];
	int minCost=INF;
	for(int i = d; i < u; i++){
		minCost=min(minCost,v[d].second*v[u].first*v[i].first*v[i+1].second+dfs(d,i)+dfs(i+1,u));
	}
	dp[d][u]=minCost;
	return dp[d][u];
}

int main(){

	int n;
	cin>>n;
	for(int i = 0; i < n; i++){
		int over,down;
		cin>>over>>down;
		v.push_back(P(over,down));
	}
	reverse(v.begin(),v.end());
	for(int i = 0; i < 101; i++)
		fill(dp[i],dp[i]+101,INF);
	cout<<dfs(0,n-1)<<endl;

	return 0;
}