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 <bits/stdc++.h>

using namespace std;

#define INF 1001000100010001000
#define MOD 1000000007
#define EPS 1e-10
#define int long long
#define rep(i, N) for (int i = 0; i < N; i++)
#define Rep(i, N) for (int i = 1; i < N; i++)
#define For(i, a, b) for (int i = (a); i < (b); i++)
#define pb push_back
#define eb emplece_back
#define mp make_pair
#define i_i pair<int, int>
#define vi vector<int>
#define vvi vector<vi >
#define vb vector<bool>
#define vvb vector<vb >
#define vp vector< i_i >
#define Edge pair< i_i, int>
#define all(a) (a).begin(), (a).end()
#define Int(x) int x; scanf("%lld", &x);
#define int2(x, y) int x, y; scanf("%lld %lld", &x, &y);
#define fir first
#define sec second
#define ffir first.first
#define fsec first.second
#define sfir second.first
#define ssec second.second
 
//int dxy[5] = {0, 1, 0, -1, 0};
// assign

vvi dp;
vp data(510);
int n;

int solve(int l, int r)
{
    int ret = INF;
    if (dp[l][r] != INF) {
        return dp[l][r];
    } else if (l == r) {
        dp[l][r] = 0;
        return 0;
    } else {
//        cout << l << " " << r << endl;
        for (int i = l; i < r; i++) {
            ret = min(ret, solve(l, i) + solve(i+1, r) + 
                    data[l].fir * data[i].sec *
                    data[i+1].fir * data[r].sec);
        }
    }
    dp[l][r] = ret;
    return ret;
}

signed main()
{
    cin >> n;
    dp = vvi(n, vi(n, INF));
    rep(i, n) {
        cin >> data[i].fir >> data[i].sec;
    }
    /*
    Rep(i, n) {
        dp[i-1][i] = data[i-1].fir*data[i-1].sec*data[i].fir*data[i].sec;
    }*/
    cout << solve(0, n-1) << endl;


    return 0;
}

C++: #include<stdio.h>
#include<limits.h>
#include<algorithm>
int memo[100][100];
int t[100],b[100];
int n;
int F(int l,int r)
{
	if(memo[l][r])return memo[l][r];
	if(l==r)return 0;
	int a=INT_MAX,i;
	for(i=l;i+1<=r;++i)a=std::min(a,t[l]*b[i]*t[i+1]*b[r]+F(l,i)+F(i+1,r));
	return memo[l][r]=a;
}
int main()
{
	scanf("%d",&n);
	for(int i=0;i<n;++i)scanf("%d%d",&t[i],&b[i]);
	printf("%d\n",F(0,n-1));
	return 0;
}

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

const unsigned int INF = 1 << 31;
unsigned int dp[101][101], a[101], b[101];
int n;

// l..r の最小コストを返す
// dp[i][j] := 山i..jの最小コスト
unsigned int solve(int l, int r){
	if( dp[l][r] != INF ) return dp[l][r];
	//if( l+1 == r ) return dp[l][r] = a[l] * b[l] * a[r] * b[r];
	if( l == r ) return dp[l][r] = 0;
	
	unsigned int res = INF;
	for(int i=l ; i < r ; i++ ){
		// l..i i..r に分割
		unsigned int cost = b[l] * a[i] * b[i+1] * a[r];
		res = min( res , cost + solve(l,i) + solve(i+1,r) );
	}
	return dp[l][r] = res;
}

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

int main(){
	cin >> n;
	for(int i=0 ; i < n ; i++ ){
		cin >> a[i] >> b[i];
	}
	ini();
	unsigned int ans = solve(0,n-1);
	cout << ans << endl;
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <cstring>
#include <cctype>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <complex>
using namespace std;
#define REP(i,n) for(int i = 0; i < (int)n; i++)
#define FOR(i,a,b) for(int i = a; i < (int)b; i++)
const int INF = 1<<28;

int ans = INF;

void dfs(int cost, vector< pair<int, int> > v) {
//	REP(i, v.size()) cout << cost << ' ' << v[i].first << ' ' << v[i].second << endl;
//	cout << endl;
	if(v.size() == 1)
		ans = min(ans, cost);
	REP(i, v.size()-1) {
		vector< pair<int, int> > v2(v);
//		cout << v2[i].first << ' ' << v2[i+1].first << endl;
		int cost2 = cost + v2[i].first * v2[i].second * v2[i+1].first * v2[i+1].second;
		v2[i].second = v2[i+1].second;
		v2.erase(v2.begin()+ i + 1);
		dfs(cost2, v2);
	}
	return;
}

int main() {
	int n; cin >> n;
	vector< pair<int, int> > cards(n);
	REP(i, n)
		cin >> cards[i].first >> cards[i].second;
	dfs(0, cards);
	cout << ans << endl;
	return 0;
}

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

using namespace std;

typedef long long ll;
const ll INF = (1LL<<50);
const int MAX = 101;

ll dp[MAX][MAX];
int T[MAX],B[MAX];

int main(){

  int n;
  cin >> n;
  for(int i = 0; i < n; i++) cin >> T[i] >> B[i];
 
  fill(dp[0],dp[0]+MAX*MAX,INF);
  for(int i = 0; i < MAX; i++) dp[i][i] = 0;

  for(int j = 1; j < n; j++)
    for(int i = 0; i < j; i++)
      for(int k = i+1; k <= j; k++)
	dp[i][j] = min(dp[i][j],dp[i][k-1]+dp[k][j]+B[i]*T[k-1]*B[k]*T[j]);
  
  cout << dp[0][n-1] << endl;
  return 0;
}

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

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define INF (1<<29)

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

int main() {
	int n;
	cin >> n;
	REP(i, n) scanf("%d %d", &a[i], &b[i]);
	
	fill(dp[0], dp[100], INF);
	REP(i, n) dp[i][i] = 0;
	FOR(w, 1, n) REP(i, n - w) FOR(j, i, n) {
		dp[i][i + w] = min(dp[i][i + w], dp[i][j] + dp[j + 1][i + w] + a[i] * b[j] * a[j + 1] * b[i + w]);
	}
	cout << dp[0][n - 1] << endl;
	return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
long memo[100][100];
long a[100],b[100];
long f(int l,int r)
{
	if(memo[l][r]>0)return memo[l][r];
	else if(l+1==r)return a[l]*b[l]*a[r]*b[r];
	long ans=1LL<<32;
	for(int i=l+1;i<r;i++)
	{
		long now;
		if(i==l+1)
		{
			now=f(i,r)+a[l]*b[l]*a[i]*b[r];
		}
		else if(i==r-1)
		{
			now=f(l,i)+a[l]*b[i]*a[r]*b[r];
		}
		else
		{
			now=min(f(i,r)+a[l]*b[l]*a[i]*b[r],f(l,i)+a[l]*b[i]*a[r]*b[r]);
		}
		ans=min(ans,now);
	}
	return memo[l][r]=ans;
}
main()
{
	int n;
	cin>>n;
	for(int i=0;i<n;i++)cin>>a[i]>>b[i];
	cout<<(n==1?0:f(0,n-1))<<endl;
}