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 <list>
using namespace std;

typedef pair<unsigned int, unsigned int> puiui;

#define fi first
#define se second

int main() {
	int n;
	list<puiui> cards;
	cin >> n;
	for(int i = 0; i < n; i++) {
		unsigned int a, b;
		cin >> a >> b;
		cards.push_back(puiui(a,b));
	}

	unsigned int cost = 0;
	while(cards.size() > 1) {
		list<puiui>::iterator it;
		unsigned int maximum = 0;
		for(list<puiui>::iterator i = cards.begin(); i != cards.end(); i++) {
			if(maximum < ((i == cards.begin()) ? 0 : i->fi)) {
				maximum = i->fi;
				it = i;
				it--;
			}
			list<puiui>::iterator nexti = i; nexti++;
			if(maximum < ((nexti == cards.end()) ? 0 : i->se)) {
				maximum = i->se;
				it = i;
			}
		}

		list<puiui>::iterator tmp = it;
		tmp++;
		cost += it->fi * it->se * tmp->fi * tmp->se;
		it->se = tmp->se;
		cards.erase(tmp);
	}

	cout << cost << endl;
}

C++: #include <bits/stdc++.h>
typedef long long lli;

using namespace std;

const int MAXN = 104;
const lli INF = 1LL<<60;

int n;
lli a[MAXN], b[MAXN];
lli dp[MAXN][MAXN];

lli rec(int s, int t) {
  lli &res = dp[s][t];
  if(res != INF) return res;
  if(t-s == 1) return res = 0;
  for(int i = s+1; i < t; ++i) {
    res = min(res, rec(s,i) + rec(i,t) + a[s]*b[i-1]*a[i]*b[t-1]);
  }
  return res;
}

int main() {
  cin >> n;
  for(int i = 0; i < n; ++i) {
    cin >> a[i] >> b[i];
  }
  fill(dp[0], dp[MAXN], INF);
  cout << rec(0,n) << endl;
  return 0;
}

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

using namespace std;

#define reps(i,f,n) for(int i=f;i<int(n);i++)
#define rep(i,n) reps(i,0,n)

#define INF 1000000000

int top[111];
int bot[111];

int memo[111][111];
int saiki(int st,int en){
	if(st+1 == en)return 0;
	if(memo[st][en]!=0)return memo[st][en];
	
	int cost = INF;
	
	for(int i=st+1;i<en;i++){
		int next = 0;
		next += saiki(st,i);
		next += saiki(i,en);
		next += top[st]*bot[i-1]*top[i]*bot[en-1];
		
		cost = min(next,cost);
	}
	
	return memo[st][en] = cost;
}

int main(){
	int n;
	cin>>n;
	
	rep(i,n)cin>>top[i]>>bot[i];
	
	int ans = saiki(0,n);
	printf("%d\n",ans);
}

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;
const long long INF = (1LL << 60);
using namespace std;

long long dp[205][205][205];

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

    pair<int, int> p[205];
    rep(i,n) cin >> p[i].first >> p[i].second;

    rep(i,205) rep(j,205) rep(k,205) dp[i][j][k] = INF;

    rep(l,n){
        for(int i = n; i > 1; i--){
            range(j,1,201){
                range(k,1,201){
                    long long next = dp[i - 1][j][k] + j * k * p[l].first * p[l].second;
                    dp[i][j][p[l].second] = min(dp[i][j][p[l].second], next);
                    dp[i][p[l].first][k] = min(dp[i][p[l].first][k], next);
                }
            }
        }
        dp[1][p[l].first][p[l].second] = 0;
    }

    long long mini = INF;
    rep(i,201) rep(j,201)  mini = min(mini, dp[n][i][j]);
    cout << mini << endl;
}

C++: #include <cstdio>
#include <cstring>
#include <climits>
#include <algorithm>

using namespace std;

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

int getMin(int l, int r)
{
	int ans = INT_MAX;
	
	if (l == r) return (0);
	if (memo[l][r] >= 0) return (memo[l][r]);
	
	for (int i = l; i < r; i++){
		ans = min(ans, getMin(l, i) + getMin(i + 1, r) + a[l] * b[i] * a[i + 1] * b[r]);
	}
	
	return (memo[l][r] = ans);
}

int main()
{
	int n;
	
	scanf("%d", &n);
	
	for (int i = 0; i < n; i++){
		scanf("%d %d", &a[i], &b[i]);
	}
	memset(memo, -1, sizeof(memo));
	
	printf("%d\n", getMin(0, n - 1));
	
	return (0);
}

C++: #include<iostream>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
#define INF 1 <<21
#define P pair<int, int>

int dp(int left, int right);

int n;
vector<P> cost;

int main(){
    cin >>n;
    for(int i = 0; i < n; i++){
        int top, bottom;
        cin >>top >>bottom;
        cost.push_back( make_pair(top, bottom) );
    }
    cout <<dp(0, n - 1) <<endl;
    return 0;
}

int dp(int left, int right){
    if(left == right) return 0;
    int ret = INF;
    for(int i = left; i < right; i++){
        ret = min(ret, dp(left, i) + dp(i + 1, right) + (cost[left].first * cost[i].second * cost[i + 1].first * cost[right].second));
    }
    return ret;
}

C++: #include <iostream>
#include <fstream>
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iomanip>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define INF 1e8
#define EPS 1e-9

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

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

int main(){
	cin>>n;
	rep(i,n)fill(dp[i],dp[i]+n,INF);
	rep(i,n){
		cin>>a[i]>>b[i];
		dp[i][i]=0;
	}
	rep2(i,1,n)rep(j,n-i+1)rep(k,i)dp[j][j+i]=min(dp[j][j+i],dp[j][j+k]+dp[j+k+1][j+i]+a[j]*b[j+k]*a[j+k+1]*b[j+i]);
	cout<<dp[0][n-1]<<endl;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
ll n;
ll INF = (1LL<<34);
P t[100];
ll dp[100][100];
 
P on(P x,P y){
  return P(x.first,y.second);
}
 
ll cost(P x,P y){
  return x.first*x.second*y.first*y.second;
}
 
int compute(int a,int b){
  ll A,B;
  if(dp[a][b]!=-1)return dp[a][b];
  if(a==b)return 0;
  
 
  ll res=INF;
  ll sum;
  P X,Y;
  for(int i=a;i<b;i++){
    X=on(t[a],t[i]);
    Y=on(t[i+1],t[b]);
     
    sum=cost(X,Y);
    sum = compute(a,i)+ sum +compute(i+1,b) ;
    res=(sum<res?sum:res);
 
  }
  dp[a][b]=res;
  return res;
}
 
int main(){
 
  while(cin>>n&&n){
    for(int i=0;i<n;i++)cin>>t[i].first>>t[i].second;
    for(int i=0;i<100;i++)for(int j=0;j<100;j++)dp[i][j]=-1;
    cout<<compute(0,n-1)<<endl;
  }
  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;
}