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;

int main(){
	const unsigned int INF = 1 << 31;
	unsigned int dp[101][101], a[101], b[101];
	int n;
	// dp[i][j] := 山 i..j をひとつにまとめるのにかかるコスト
	// 初期化
	for(int i=0 ; i < 101 ; i++ ){
		for(int j=0 ; j < 101 ; j++ ){
			dp[i][j] = (i == j)? 0 : INF ;
		}
	}
	
	cin >> n;
	for(int i=0 ; i < n ; i++ ){
		cin >> a[i] >> b[i];
	}
	for(int k = 1 ; k < n ; k++ ){
		for(int i = 0 ; i+k < n ; i++ ){
			for(int j = i ; j < i+k ; j++ ){
				// cost := i..j と j+1..i+k をまとめるときにかかるコスト
				unsigned int cost = a[i] * b[j] * a[j+1] * b[i+k];
				dp[i][i+k] = min( dp[i][i+k] , dp[i][j] + dp[j+1][i+k] + cost );
			}
		}
	}
	cout << dp[0][n-1] << endl;
}

C++: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back
#define INF (1e9+1)
//#define INF (1LL<<59)


int main(){
	int n;
	cin>>n;
	vector<pii> card(n);
	rep(i,n)cin>>card[i].first>>card[i].second;
	
	
	ll ans = 0;
	while(card.size()>1){
		int maxvalue=-1;
		pii maxpos=pii(-1,-1);
		rep(i,card.size()){
			rep(j,card.size()){
				if(i==j)continue;
				if(maxvalue<card[i].second*card[j].first){
					maxpos=pii(i,j);
					maxvalue=card[i].second*card[j].first;
				}
			}
		}
		
		ans+=maxvalue*card[maxpos.first].first*card[maxpos.second].second;
//		cout<<maxvalue*card[maxpos.first].first*card[maxpos.second].second<<endl;
		
		card[maxpos.first]=pii(card[maxpos.first].first,card[maxpos.second].second);
		card.erase(card.begin()+maxpos.second);
	}
	cout<<ans<<endl;
}

C++: #include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <iostream>
#include <sstream>
#include <climits>
#include <cfloat>
#include <complex>


typedef long long ll;
const double Eps = 1e-6;

using namespace std;

int main()
{
	int n;
	scanf("%d", &n);
	int cards[100][2];
	for (int i = 0; i < n; ++i)
		scanf("%d %d", &cards[i][0], &cards[i][1]);

	bool used[100];
	memset(used, 0, sizeof(used));
	int sum = 0;
	for (int left = n; left > 1; --left)
	{
		vector<int> canUse;
		for (int i = 0; i < n; ++i)
			if (!used[i])
				canUse.push_back(i);
		int put;
		int putted;
		pair<int, int> mini(INT_MAX, INT_MAX);
		for (int i = 1; i < canUse.size(); ++i)
		{
			int a = canUse[i-1];
			int b = canUse[i];
			pair<int, int> t(cards[a][0]*cards[b][1]
			, cards[a][0]*cards[a][1]*cards[b][0]*cards[b][1]);

			if (t < mini)
			{
				mini = t;
				put = a;
				putted = b;
			}
		}
		used[put] = true;
		cards[putted][0] = cards[put][0];
		sum += mini.second;
	}
	printf("%d\n", sum);

	return 0;
}

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

#define rep(i, a, b) for(int (i) = (a); (i) < (b); (i) ++)

using namespace std;

int main() {
  int n, a[110], b[110];
  cin >> n;
  rep(i, 0, n) {
    cin >> a[i] >> b[i];
  }

  int dp[110][110];
  rep(i, 0, 110) {
    rep(j, 0, 110) {
      dp[i][j] = 2<<29;
    }
  }

  for(int len = 1; len <= n; len ++) {
    if(len == 1) {
      for(int p = 0; p < n; p ++) {
	dp[p][p+1] = 0;
      }
    }
    else {
      for(int p = 0; p <= n; p ++) {
	int r = p + len;
	if(r > n) continue;

	for(int q = p + 1; q < r; q ++) {
	  dp[p][r] = min(dp[p][r], dp[p][q] + dp[q][r] + a[p]*b[q-1]*a[q]*b[r-1]);
	}
      }
    }
  }

  cout << dp[0][n] << endl;

  return 0;
}

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 = 0; j < n-i; j++){		//cd[i][j]
			for(int k = j; k < i; k++){
				cb[j][i] = min(cb[j][i] , cb[j][k]+cb[k+1][i]+a[j]*b[k]*a[k+1]*b[i]);
			}
		}
	}
	cout << cb[0][n-1] << 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<<30);

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[i].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));
	}
	for(int i = 0; i < 101; i++)
		fill(dp[i],dp[i]+101,INF);
	cout<<dfs(0,n-1)<<endl;

	return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back
#define INF (1e9+1)
//#define INF (1LL<<59)

ll dp[102][102];
vector<pii> c;

ll solve(int left,int right){
	if(dp[left][right]!=INF)return dp[left][right];
	
	for(int k=left+1;k<right;k++){
		dp[left][right] = min(solve(left,k)+solve(k,right)+c[left].first*c[k-1].second*c[k].first*c[right-1].second,dp[left][right]);
	}
	return dp[left][right];
}

int main(){
	int n;
	cin>>n;
	c.resize(n);
	rep(i,n)cin>>c[i].first>>c[i].second;
	
	rep(i,102)rep(j,102)dp[i][j]=INF;
	rep(i,n)dp[i][i+1]=0;
	
	cout<<solve(0,n)<<endl;
}

C++: #include<iostream>
#include<cstdio>
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define inf (1<<28)
using namespace std;
typedef pair<int,int> ii;
ii p[200];
int n;

void solve()
{
  int M[n+1][n+1];
  rep(i,n+1)rep(j,n+1)M[i][j] = inf;
  rep(i,n+1)M[i][i] = 0;

  rep(j,n)
    {
      rep(i,n-j)
	{
	  REP(k,i+1,i+j+1)
	    {
	      int cost = M[i][k-1] + M[k][i+j] + p[i].first*p[k-1].second*p[k].first*p[i+j].second;
	      if(cost < M[i][i+j])
		  M[i][i+j] = cost;
	    }
	}
    }
  cout << M[0][n-1] << endl;
}

int main()
{

  while(cin >> n)
    {
      rep(i,n)cin >> p[i].first >> p[i].second;
      solve();
    }
  return 0;
}

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

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