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 <string>
#include <vector>
#include <climits>
using namespace std;

int main() {
    int n, top[100], bottom[100], dp[100][100];
    cin >> n;
    for (int i=0; i<n; ++i) {
        cin >> top[i] >> bottom[i];
    }
    for (int i=1; i<=n; ++i) {
        for (int j=0; j+i-1<n; ++j) {
            dp[j][j+i-1] = INT_MAX;
            if (j == j+i-1) {
                dp[j][j+i-1] = 0;
            } else {
                for (int k=j; k<j+i-1; ++k) {
                    dp[j][j+i-1] = min(dp[j][j+i-1], dp[j][k] + dp[k+1][j+i-1] + top[j] * bottom[k] * top[k+1] * bottom[j+i-1]);
                }
            }
        }
    }
    cout << dp[0][n-1] << endl;
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
constexpr int IINF = INT_MAX;

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

int rec(int l, int r){
    if(dp[l][r] != -1) return dp[l][r];
    if(r==l) return 0;

    int res = IINF;
    for(int i=l;i<r;i++){
        res = min(res, rec(l,i)+rec(i+1,r)+a[l]*b[i]*a[i+1]*b[r]);
    }
    return dp[l][r] = res;
}

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

C++: #define _USE_MATH_DEFINES
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<climits>
#include<string>
#include<vector>
#include<list>
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<cstring>
#include<stack>
using namespace std;

int main(){
	int N, C[100][2], dp[100][100];
	cin >> N;
	for(int i = 0; i < N; i++) cin >> C[i][0] >> C[i][1];
	for(int i = 2; i <= N; i++){
		for(int j = 0; j + i - 1 < N; j++){
			int *dp_p = &dp[j][j + i - 1];
			*dp_p = INT_MAX;
			for(int k = j; k < j + i - 1; k++){
				*dp_p = min( *dp_p, 
					dp[j][k] + dp[k+1][j+i-1]
					+ C[j][0]*C[k][1]*C[k+1][0]*C[j+i-1][1] );
			}
		}
	}
	printf("%d\n", dp[0][N-1] );
	return 0;
}

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

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

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

  for(int i = 0; i < n; i++){
    for(int j = 0; j < n; j++){
      if(i != j) dp[i][j] = INT_MAX;
    }
    dp[i][i] = 0;
  }

  for(int i = 1; i <= n; i++){
    for(int j = 0; j < n - i; j++){
      for(int k = j; k < j + i; k++){
        dp[j][j+i] = min(dp[j][j+i], dp[j][k] + dp[k+1][j+i] + u[j] * b[k] * u[k+1] * b[j+i]);
      }
    }
  }

  cout << dp[0][n-1] << 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 i = 0; i < n; i++)
    for(int j = i+1; j < n; j++)
      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 <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>

using namespace std;

inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str();}

typedef vector<int> vi;
typedef vector<vi>  vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EACH(t,i,c) for(t::iretator i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)

const double EPS = 1e-10;

int main() {
	int n;
	cin >> n;
	vvi cost(n, vi(2));

	REP(i, n) {
		cin >> cost[i][0] >> cost[i][1];
	}

	ll num = 0;
	int max00, max11;
	int max01, max10;
	int p0, p1;

	REP(i, n-1) {
		max00 = max11 = max01 = max10 = 1;
		REP(j, n) {
			if(cost[j][0] >= max00) {
				max00 = cost[j][0];
				max01 = cost[j][1];
				p0 = j;
			}
		}

		REP(j, n) {
			if(cost[j][1] >= max11 && p0 != j) {
				max10 = cost[j][0];
				max11 = cost[j][1];
				p1 = j;
			}
		}

		cost[p0][0] = cost[p1][0]; 
		cost[p1][0] = cost[p1][1] = 0;
		p0 = p1;

		num += max00*max01*max10*max11;
	}

	cout << num << endl;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
long long dp[101][101][101];
long long a[101],b[101];
#define rep(i,n) for(int i=0;i<n;i++)
int main(){
	int n;
	cin >> n;
	rep(i,n) cin >> a[i+1] >> b[i+1];
	
	rep(i,101)rep(j,101)rep(k,101) dp[i][j][k] = INT_MAX;
	for(int j=1;j<=n;j++)
			dp[0][j][j] = 0;
	for(int j=1;j<=n;j++){
		for(int k=1;k<=n;k++){
			if(j != k)dp[1][j][k] = a[j] * b[j] * a[k] * b[k];
		}
	}
	for(int i=2;i<=n;i++){
		for(int j=1;j<=n;j++){
			for(int k=1;k<=n;k++){
				dp[i][i][k] = min( dp[i][i][k] , dp[i-1][j][k] + a[i] * b[i] * a[j] * b[k] );
				dp[i][k][i] = min( dp[i][k][i] , dp[i-1][j][k] + a[i] * b[i] * a[k] * b[j] );
			}
		}
	}
	long long ans = INT_MAX;
	rep(i,n+1)rep(j,n+1) ans = min(ans,dp[n-1][i][j]);
	cout << ans << endl;
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
using namespace std;

#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()

typedef pair<int,int> pii;
int const INF = 1<<30;
int main() {
  int n;
  cin >> n;
  vector<int> v1(n),v2(n);
  REP(i,n) {
    cin >> v1[i] >> v2[i];
  }
  long long dp[n][n];
  REP(i,n) REP(j,n) dp[i][j] = INF;
  REP(i,n) dp[i][i] = 0;

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