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 <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;
//typedef long long ll;

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

using namespace std;

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

int main()
{

	while(cin >> num)
	{
		vector<deck> mount(100);

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

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

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

		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<vector>
#include<stack>
#include<queue>
#include<cstdio>
#include<climits>
#include<cmath>
#include<cstring>
#include<string>
#include<sstream>

#define f first
#define s second
#define mp make_pair

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define ALL(c) (c).begin(), (c).end()

using namespace std;

typedef unsigned int uint;
typedef long long ll;

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

int solve(int pos, int num){
  int ret = INT_MAX;
  if(num == 1) return 0;
  if(memo[pos][num]) return memo[pos][num];
  REP(i,num-1){
    int t = pos + i;
    int cost = solve(pos,i+1) + solve(pos+i+1,num-i-1);
    cost += a[pos] * b[t] * a[t+1] * b[pos+num-1];
    ret = min(ret, cost);
  }
  return memo[pos][num] = ret;
}

int main(){
  int n;
  scanf("%d",&n);
  REP(i,n) scanf("%d%d",&a[i],&b[i]);
  printf("%d\n",solve(0,n));
  return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#define lol(i,s,n) for(int i=s;i<n;i++)
typedef long long ll;
using namespace std;
ll dp[110][110],a[110],b[110];
int main(){
  int n;cin>>n;
  lol(i,0,n){
    cin>>a[i]>>b[i];
    lol(j,0,n)dp[i][j]=(1<<30);
  }
  lol(i,0,n)dp[i][i]=0;
  lol(k,1,n){
    lol(i,0,n-k){
      int to=i+k;
      lol(j,i,to){
	dp[i][to]=min(dp[i][to],dp[i][j]+dp[j+1][to]+a[i]*b[j]*a[j+1]*b[to]);
      }
    }
  }
  cout<<dp[0][n-1]<<endl;
  return 0;
}

C++: #include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

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

    vector<vector<long long> > cost(n, vector<long long>(n, INT_MAX));
    for(int i=0; i<n; ++i)
        cost[i][i] = 0;
    for(int i=1; i<n; ++i){
        for(int j=0; j+i<n; ++j){
            for(int k=j; k<j+i; ++k){
                long long c = cost[j][k] + cost[k+1][j+i] + b[j] * a[k] * b[k+1] * a[j+i];
                cost[j][j+i] = min(cost[j][j+i], c);
            }
        }
    }
    cout << cost[0][n-1] << endl;

    return 0;
}

C++: #include <stdio.h>
#include <algorithm>
#include <iostream>
#include <stack>
#include <vector>
#include <math.h>
#include <queue>

using namespace std;

struct Block{
	Block(){
		cost = top = bottom = 0;
	}
	int cost,top,bottom;
};

int main(){
	int n,minimum,tmp;
	scanf("%d",&n);
	Block table[n][n];

	for(int i=0;i<n;i++){
		scanf("%d %d",&table[0][i].top,&table[0][i].bottom);
	}

	for(int col = 0; col <= n-2; col++){
		table[1][col].cost = table[0][col].top*table[0][col].bottom*table[0][col+1].top*table[0][col+1].bottom;
		//??????????±±??????????????????????????????????????????°?????????????????????????????????????
		if(table[0][col].top*table[0][col+1].bottom < table[0][col].bottom*table[0][col+1].top){
			table[1][col].top = table[0][col].top;
			table[1][col].bottom = table[0][col+1].bottom;
		}else{
			table[1][col].top = table[0][col+1].top;
			table[1][col].bottom = table[0][col].bottom;
		}
	}

	int min_div,min_multi;
	for(int row = 2; row <= n-1; row++){
		for(int col = 0; col <= n-(row+1); col++){
			minimum = 2147483647;

			for(int div = 0; div <= row-1;div++){
				tmp = (table[div][col+div].cost+table[row-div-1][col+div+1].cost)
						+(table[div][col+div].top*table[div][col+div].bottom*table[row-div-1][col+div+1].top*table[row-div-1][col+div+1].bottom);
				if(minimum > tmp){
					minimum = tmp;
					min_div = div;

					//????????¨????????????????°????????±?????????????
					if(table[div][col+div].top*table[row-div-1][col+div+1].bottom < table[div][col+div].bottom*table[row-div-1][col+div+1].top){
						min_multi = table[div][col+div].top*table[row-div-1][col+div+1].bottom;
					}else{
						min_multi = table[div][col+div].bottom*table[row-div-1][col+div+1].top;
					}
				}else if(minimum == tmp){ //????????????????????§??????????????¨????????????????????????????°???????????????????????????????????????¶
					if(table[div][col+div].top*table[row-div-1][col+div+1].bottom < table[div][col+div].bottom*table[row-div-1][col+div+1].top){
						if(min_multi > table[div][col+div].top*table[row-div-1][col+div+1].bottom){
							min_div = div;
						}
					}else{
						if(min_multi > table[div][col+div].bottom*table[row-div-1][col+div+1].top){
							min_div = div;
						}
					}
				}
			}
			table[row][col].cost = minimum;
			//???????????????????????¨?????????????¨??????????
			if(table[min_div][col+min_div].top*table[row-min_div-1][col+min_div+1].bottom < table[min_div][col+min_div].bottom*table[row-min_div-1][col+min_div+1].top){
				table[row][col].top = table[min_div][col+min_div].top;
				table[row][col].bottom = table[row-min_div-1][col+min_div+1].bottom;
			}else{
				table[row][col].top = table[row-min_div-1][col+min_div+1].top;
				table[row][col].bottom = table[min_div][col+min_div].bottom;
			}
		}
	}

	printf("%d\n",table[n-1][0].cost);

    return 0;
}