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 <cstring>
#include <iostream>
#include <algorithm>
#include <numeric>
using namespace std;

#define DEBUG(x) cerr << #x << " = " << x << endl

int N;
int top[100], bottom[100];
int dp[100][100];

int solve(int s, int e) {
  //cerr << "solve(" << s << "," << e << ")" << endl;
  int &res = dp[s][e];
  if(res != -1) return res;
  if(e - s == 1) return 0;
  res = numeric_limits<int>::max();
  for(int i = s + 1; i <= e - 1; ++i) {
    res = min(res, solve(s, i) + solve(i, e) + top[s]*bottom[i-1]*top[i]*bottom[e-1]);
  }
  return res;
}

int main() {
  ios::sync_with_stdio(false);
  cin >> N;
  for(int i = 0; i < N; ++i) {
    cin >> top[i] >> bottom[i];
  }
  memset(dp, -1, sizeof(dp));
  cout << solve(0, N) << endl;
}

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

#define FOR(i,b,n) for(int i=b;i<n;i++)
#define RFOR(i,b,n) for(int i=n-1;i>=b;i--)
#define CLR(mat) memset(mat, 0, sizeof(mat))
#define NCLR(mat) memset(mat, -1, sizeof(mat))
#define FILL(mat, x) memset(mat, x, sizeof(mat))
#define BIT(n, b) ((n>>b) & 1)

#define INF 1<<31-1
using namespace std;

typedef pair<int,int> paii;
typedef long long ll;

int n;
ll top[256], btm[256], dp[256][256];

ll rec(int lo, int up){
	
	ll res = INF;
	
	if(up <= lo)
		return 0;
	if(dp[lo][up] >= 0)
		return dp[lo][up];
	
	for(int k = lo+1; k <= up; ++k){
		ll tmp = rec(lo, k-1) + rec(k, up) + top[lo]* btm[k-1]* top[k]* btm[up];
		res = min(res, tmp);
	}
	
	return dp[lo][up] = res;	
}

int main(){
	
	cin >> n;
	
	for(int i = 0; i < n; ++i){	
		cin >> top[i] >> btm[i];
	}
	
	NCLR(dp);
	cout << rec(0,n-1) << endl;
	
	return 0;
}

C++: #include<iostream>
#include<math.h>
#include<vector>
#include<algorithm>
#include<cstdio>
#include <string>
using namespace std;

int a[100],b[100];
int dp[100][100];
 
int main(){
    int n,i,j,k;
    cin >> n;
    for(i=0;i<n;i++) cin >> a[i] >> b[i];
    for(i=0;i<n;i++) for(j=0;j<n;j++) dp[i][j] = INT_MAX;
    for(i=0;i<n;i++) dp[i][i] = 0;
    for(i=2;i<=n;i++){
        for(j=0;j<=n-i;j++){
            for(k=j+1;k<j+i;k++){
                dp[j][j+i-1] = min(dp[j][j+i-1] , dp[j][k-1] + dp[k][j+i-1] + a[j] * b[k-1] * a[k] * b[j+i-1]);
            }
        }
    }
    cout << dp[0][n-1] << endl;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int n, a[100], b[100], dp[100][101];
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++) dp[i][j] = j - i - 1 ? 999999999 : 0;
	}
	for (int i = 2; i <= n; i++) {
		for (int j = 0; i + j <= n; j++) {
			for (int k = j; k < i + j; k++) dp[j][i + j] = min(dp[j][i + j], dp[j][k] + dp[k][i + j] + a[j] * b[k - 1] * a[k] * b[i + j - 1]);
		}
	}
	cout << dp[0][n] << endl;
	return 0;
}

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

using namespace std;

pair<int, int> cards[100];
int dp[100][100];

int rec(int begin, int end) {
  if (!(dp[begin][end] == -1))
    return dp[begin][end];
  if (begin == end) {
    return dp[begin][end] = 0;
  }
  int m = INT_MAX;
  for (int i = begin; i < end; i++)
    m = min(m, cards[begin].first * cards[i].second
             * cards[i+1].first * cards[end].second
             + rec(begin, i) + rec(i+1, end));
  return dp[begin][end] = m;
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> cards[i].first >> cards[i].second;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      dp[i][j] = -1;
  cout << rec(0, n-1) << endl;
  return 0;
}