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<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string>
#include<algorithm>
#include<vector> 
#include<limits>
#include<numeric>
#include<math.h>
using namespace std;

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define in(x) cin >> x
#define out(str) cout << str << endl

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

int N,mini=1000,abpos,ipos,miniab=0,ans=0,a1,a2,b1,b2;

int main(){
    cin>>N;
    int a[N],b[N];
    rep(i,N){
        cin>>a[i]>>b[i];
        if(mini>a[i]){
            if(miniab<b[i]){
                mini=a[i];
                abpos=0;
                ipos=i;
                miniab=b[i];
            }
        }
        if(mini>b[i]){
            if(miniab<a[i]){
                mini=b[i];
                abpos=1;
                ipos=i;
                miniab=a[i];
            }
        }
    }
    if(abpos==0){
        a1=mini;
        b1=b[ipos];
        rep(i,N){
            if(i==ipos)continue;
            a2=a[i];
            b2=b[i];
            ans+=a1*b1*a2*b2;
            b1=b2;
        }
    }
    else{
        a2=a[ipos];
        b2=mini;
        rep(i,N){
            if(i==ipos)continue;
            a1=a[i];
            b1=b[i];
            ans+=a1*b1*a2*b2;
            a2=a1;
        }
    }
    out(ans);
}

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) break;
		}
		if (min > cost) min = cost;
	} while (next_permutation(v.begin(), v.end()));
	cout<<min<<endl;

	return 0;
}

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

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef complex<double> P;
static const double EPS = 1e-8;
static const int INF = (int)1e8;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
unsigned int dp[101][101];
int main(void){
  int n;  cin>>n;
  vector<PII> v(n);
  REP(i,n)cin>>v[i].first>>v[i].second;

  REP(i,n+1)REP(j,n+1)dp[i][j] = (i==j)?0:INF;

  FOR(i,1,n){
    REP(j,n-i+1){
      FOR(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+1],cost+dp[j][k]+dp[k+1][j+i]);
      }
    }
  }
  cout<<dp[0][n-1]

  return 0;
}

C++: #include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <functional>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <deque>
 
using namespace std;
 
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define rep(i,n) REP(i,0,(n))
#define pb(n) push_back(n)
#define mp(n,m) make_pair(n, m)
#define all(r) r.begin(),r.end()
#define fi first
#define se second
#define println(X) cout<<X<<endl;
 
 
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 1e9;

int calc(pii p1, pii p2){
	return p1.fi*p1.se*p2.fi*p2.se;
}

int main(){
	int n;
	cin>>n;
	vector<pii> v(n);
	rep(i, n) cin>>v[i].fi>>v[i].se;
	int ans = 0;
	while(v.size()>1){
		int idx1,idx2, m = INF;
		for(int i = 0; i < v.size()-1; i++){
			for(int j = i +1; j < v.size(); j++){
				if(calc(v[i], v[j]) < m){
					m = calc(v[i], v[j]);
					idx1 = i;
					idx2 = j;
				}
			}
		}
		pii p1 = v[idx1], p2 = v[idx2];
		ans += m;
		v.erase(v.begin()+idx2);
		v.erase(v.begin()+idx1);
		if(p1.fi*p2.se < p1.se*p2.fi){
			v.pb(pii(p1.fi,p2.se));
		}else{
			v.pb(pii(p2.fi,p1.se));
		}
	}println(ans);
}

C++: #include<bits/stdc++.h>
#define int long long
using namespace std;

int n,a[101],b[101],dp[111][111];

int dfs(int l,int r){
    if(dp[l][r]!=-1)return dp[l][r];
    int res=1e17;
    for(int i=l;i<r;i++){
        res=min(res,dfs(l,i)+dfs(i+1,r)+a[l]*b[i]*a[i+1]*b[r]);
    }
    return dp[l][r]=res;
}

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

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

int n;
int a[100], b[100];
int memo[100][100];

int recur(int left, int right){
	if(memo[left][right] == -1){
		int ret = INT_MAX;

		if(left == right){
			ret = 0;
		}
		else{
			for(int mid = left; mid < right; ++mid){
				int cost = recur(left, mid) + recur(mid + 1, right) + a[left] * b[mid] * a[mid + 1] * b[right];
				ret = min(ret, cost);
			}
		}
		memo[left][right] = ret;
	}

	return memo[left][right];
}

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

	for(int i = 0; i < n; ++i){
		for(int j = 0; j < n; ++j){
			memo[i][j] = -1;
		}
	}

	int ans = recur(0, n - 1);
	cout << ans << endl;
}

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

int main(){
  int n;
  int a[100],b[100];
  long long dp[101][101];

  cin >> n;
  for(int i=0;i<n;i++)cin >> a[i] >> b[i];

  for(int i=0;i<=n;i++){
    for(int j=0;j<=n;j++)dp[i][j] = 1e10;
  }

  for(int i=0;i<n;i++)dp[i][i+1] = 0;

  for(int len = 1;len<=n;len++){
    for(int l=0;l+len<=n;l++){
      int r = l + len;
      for(int m=l+1;m<r;m++){
	dp[l][r] = min(dp[l][r], dp[l][m] + dp[m][r] + a[l]*b[m-1]*a[m]*b[r-1]);
      }
    }
  }

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

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

struct Pair{int top,bottom;};

int main()
{
  vector<int> order;
  vector<Pair> card;
  Pair p;
  int n,i,min,cost;
  cin>>n;
  order.resize(n);
  card.resize(n);
  for(i=0;i<n;i++){
    cin>>p.top>>p.bottom;
    card[i]=p;
    order[i]=i;
  }
  min=INT_MAX;
  do{
    cost=0;
    for(i=1;i<n;i++)cost+=card[0].bottom*card[i-1].top*card[i].bottom*card[i].top;
    if(cost<min)min=cost;
  }while(next_permutation(order.begin(),order.end()));
  cout<<min<<endl;
}