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 <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <deque>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <ctime>
#include <iterator>
#include <bitset>
#include <numeric>
#include <list>
#include <iomanip>

#if __cplusplus >= 201103L
#include <array>
#include <tuple>
#include <initializer_list>
#include <unordered_set>
#include <unordered_map>
#include <forward_list>

#define cauto const auto&
#else

#endif

using namespace std;


namespace{
typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}


#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()


#define MOD 1000000007LL
#define EPS 1e-8
static const int INF=1<<24;

void mainmain(){
	int n;
	cin>>n;
	vector<pii> v(n);
	rep(i,3){
		cin>>v[i].F>>v[i].S;
	}
	int ans=0;
	while(v.size()!=1){
		int x=-1,y=0;
		rep(i,v.size()-1){
			if(y<v[i].F*v[i+1].S){
				x=i;
				y=v[i].F*v[i+1].S;
			}
			if(y<v[i].S*v[i+1].F){
				x=i;
				y=v[i].S*v[i+1].F;
			}
		}
		ans+=v[x].F*v[x].S*v[x+1].F*v[x+1].S;
		if(v[x].F*v[x+1].S>v[x].S*v[x+1].F) v[x]=mkp(v[x+1].F,v[x].S);
		else v[x]=mkp(v[x].F,v[x+1].S);
		reep(i,x+1,v.size()-1){
			swap(v[i],v[i+1]);
		}
		v.erase(v.end()-1);
	}
	cout<<ans<<endl;
}



}
main() try{
    mainmain();
}
catch(...){
}

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

int dp[100][100] = {}, a[100], b[100], n;
bool visit[100][100] = {};

int dfs(int s, int t){
    if(visit[s][t]) return dp[s][t];
    visit[s][t] = true;
    if(s == t)  return dp[s][t] = 0;
    if(s+1 == t){
        return dp[s][t] = a[s]*a[t]*b[s]*b[t];
    }
    int score = INT_MAX;
    for(int j = s; j < t; j++){
        score = min(score, a[s]*b[j]*a[j+1]*b[t]+dfs(s,j)+dfs(j+1,t));
    }
    return dp[s][t] = score;
}

int main(){
    int n;
    cin >> n;

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

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

    return 0;
}

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

using namespace std;

int fr[100];
int bc[100];

int mem[100][100];

int dfs(int f,int l){
  if(mem[f][l]!=-1){
    return mem[f][l];
  }else if(f==l){
    return 0;
  }else{
    long long mc=1LL<<31;
    for(int m=f;m!=l;m++){
      mc=min(mc,(long long)dfs(f,m)+dfs(m+1,l)+fr[f]*bc[m]*fr[m+1]*bc[l]);
    }
    mem[f][l]=mc;
    return mc;
  }
}

int main(){
  int n;
  cin>>n;
  fill(mem[0],mem[100],-1);
  for(int i=0;i<n;i++){
    cin>>fr[i]>>bc[i];
  }
  cout<<dfs(0,n-1)<<endl;
  return 0;
}

C++: #include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <functional>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <deque>
#include <ctime>
  
using namespace std;
  
#define rep(i,n) REP(i,0,n)
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define pb push_back
#define all(r) (r).begin(),(r).end()
#define rall(r) (r).rbegin(),(r).rend()
#define fi first
#define se second
#define println(X) cout<<X<<endl;
#define DBG(X) cout<<#X<<" : "<<X<<endl;
  
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 
const int INF = 1e9;
const ll LINF = 1e18;
const ll MOD = 1e9 + 7;
double EPS = 1e-8;
const double PI = acos(-1);

int main(){
	int n;
	cin>>n;
	vector<pii> v(n);
	rep(i, n) cin>>v[i].fi>>v[i].se;
	ll ans = 0LL;
	while(v.size()>1) {
		int idx = -1, val = 1e9;
		for(int i = 0; i+1 < v.size(); i++) {
			int nval = v[i].fi * v[i+1].se;
			if(nval < val) val = nval, idx = i;
		}
		ans += v[idx].fi * v[idx].se * v[idx + 1].fi * v[idx + 1].se;
		v[idx] = {v[idx].fi,v[idx+1].se};
		for(int i = idx+1; i +1 < v.size(); i++){
			v[i] = v[i+1];
		}
		v.pop_back();
	}
	cout<<ans<<endl;
}

C++: #include<iostream>
#include<algorithm>
#include<climits>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<string>
#include<cstring>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<bitset>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<long long, int> ll_i;

#define PI 3.141592653589793238462643383279
#define mod 1000000007LL
#define rep(i, n) for(i = 0;i < n;++i)
#define rep1(i, n) for(i = 1;i < n;++i)
#define per(i, n) for(i = n - 1;i > -1;--i)
#define int(x) int x; scanf("%d",&x)
#define int2(x, y) int x, y; scanf("%d%d",&x, &y)
#define int3(x, y, z) int x, y, z; scanf("%d%d%d",&x, &y, &z)
#define int4(v, x, y, z) int v, x, y, z; scanf("%d%d%d%d", &v, &x, &y, &z)
#define int5(v, w, x, y, z) int v, w, x, y, z; scanf("%d%d%d%d%d", &v, &w, &x, &y, &z)
#define pri(x) cout << (x) << "\n"
#define pri2(x, y) cout << (x) << " " << (y) << "\n"
#define pri3(x, y, z) cout << (x) << " " << (y) << " " << (z) << "\n"
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define kabe puts("---------------------------")
#define kara puts("")
#define debug(x) cout << " --- " << (x) << "\n"
#define debug2(x, y) cout << " --- " << (x) << " " << (y) << "\n"
#define debug3(x, y, z) cout << " --- " << (x) << " " << (y) << " " << (z) << "\n"
#define X first
#define Y second
#define eps 0.0001
#define prid(x) printf("%.15lf\n", x)

int a[100], b[100];
ll dp[100][101] = {0};

signed main(void){
  int i, j;
  for(int testcase = 0;testcase >= 0;testcase++){
    int(n);
    rep(i, n)rep(j, n + 1)dp[i][j] = (1LL << 32);
    rep(i, n)scanf("%d%d", a+i, b+i);
    rep(i, n)dp[i][i + 1] = 0;
    rep(i, n - 1)dp[i][i + 2] = a[i] * b[i] * a[i + 1] * b[i + 1];

    for(j = 3;j <= n;++j)rep(i, n - j + 1)for(int k = 1;k < j;++k)
    dp[i][i + j] = min(dp[i][i + j], dp[i][i + k] + dp[i + k][i + j] + a[i] * b[i + k - 1] * a[i + k] * b[i + j - 1]);

    pri(dp[0][n]);
    //rep(i, n)rep(j, n + 1)if(i < j)pri3(i, j, dp[i][j]);

/*/

//*/ break;
  }
  return 0;
}