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 <algorithm>
#include <string>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <numeric>
#include <cctype>

#define rep(i, n) for(int i = 0; i < (n); i++)
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define ALL(v) (v).begin(), (v).end()
#define REV(s) (s).rbegin(), (s).rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define MP make_pair
#define X first
#define Y second

using namespace std;

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

const int N = 110;
ll dp[N][N];
int top[N], bot[N];

ll dfs(int l, int r){ // [l, r]
	if(l == r) return 0;
	ll &res = dp[l][r];
	if(res >= 0) return res;

	res = 1ll<<32;
	for(int i = l; i <= r; ++i){
		ll tmp = dfs(l, i)+dfs(i+1, r);
		tmp += top[l]*bot[i]*top[i+1]*bot[r];
		res = min(res, tmp);
	}

	return res;
}

int main(){
	int n;
	cin >> n;
	rep(i, n) cin >> top[i] >> bot[i];

	MEMSET(dp, -1);
	cout << dfs(0, n-1) << endl;

	return 0;
}

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

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

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

int main() {
    int n, a, b;
    cin >> n;
    vector<pair<int,int> > v;
    for (int i=0; i<n; ++i) {
        cin >> a >> b;
        v.push_back(make_pair(a, b));
    }
    int cost = 0;
    while (1 < (int)v.size()) {
        int up, down, max_cost = 0;
        for (int i=0; i<(int)v.size(); ++i) {
            for (int j=0; j<(int)v.size(); ++j) {
                if (i == j) continue;
                if (max_cost < v[i].second * v[j].first) {
                    max_cost = v[i].second * v[j].first;
                    up = i;
                    down = j;
                }

            }
        }
        cost += v[up].first * v[up].second * v[down].first * v[down].second;
        v.push_back(make_pair(v[up].first, v[down].second));
        v.erase(v.begin() + max(up, down));
        v.erase(v.begin() + min(up, down));
    }
    cout << cost << endl;
    return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
ll n;
ll INF = (1LL<<32);
P A,B;
P t[100];
ll dp[100][100];

P on(P x,P y){
  return P(x.first,y.second);
}
int cost(P x,P y){
  return x.first*x.second*y.first*y.second;
}

int compute(int a,int b){
  if(dp[a][b]!=-1)return dp[a][b];
  if(a==b)return 0;

  if(a+1==b)return cost(t[a],t[b]);
  if(a+2==b)return min(cost(t[a],t[a+1])+cost(on(t[a],t[a+1]),t[a+2]) , cost(t[a+1],t[a+2])+cost(t[a],on(t[a+1],t[a+2])) );

  ll res=INF;
  ll cost;

  for(int i=a+1;i+1<b;i++){

    cost=t[a].first*t[i].second*t[i+1].first*t[b].second;

    res=min(res, compute(a,i)+ cost +compute(i+1,b) );

  }
  return dp[a][b]=res;
}

int main(){
  cin>>n;
  for(int i=0;i<n;i++)cin>>t[i].first>>t[i].second;

  if(n==1){cout<<"0"<<endl;return 0;}

  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 <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, i + w) {
		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 <bits/stdc++.h>
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;

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){ //????????????????±????
    	  cout<<i<<" "<<j<<" "<<k<<endl;
        int cost = v[j].first * v[k].second * v[k+1].first * v[j+i].second;
        cout<<cost<<endl;
        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<algorithm>
#include<vector>
#include<cstring>

#define F first
#define S second

using namespace std;

typedef pair<int,int> P;
const int MAX = 101;
int N;
vector<P> V;

int dp[MAX][MAX];

int rec(int l, int r){

  if(dp[l][r] != -1) return dp[l][r];
  if(l+1 == r) return dp[l][r] = V[l].F*V[l].S*V[r].F*V[r].S;
  if(l == r) return dp[l][r] = 0;

  for(int c = l; c < r; c++){
    if(dp[l][r] == -1) dp[l][r] = rec(l,c)+rec(c+1,r)+V[l].F*V[c].S*V[c+1].F*V[r].S;
    else dp[l][r] = min(dp[l][r], rec(l,c)+rec(c+1,r)+V[l].F*V[c].S*V[c+1].F*V[r].S);
  }
  return dp[l][r];
}

int main(){
  memset(dp,-1,sizeof(dp));
  cin >> N;
  V.resize(N);
  for(int i = 0; i < N; i++) cin >> V[i].first >> V[i].second;
  cout << rec(0,N-1) << endl;
  return 0;
}