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 <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];
  }
  int ans = 0;
  while(v1.size()>1) {
    pii t1(INF,0),t2(INF,0),b1(INF,0),b2(INF,0);
    REP(i,v1.size()) {
      if (t1>pii(v1[i],i)) {
        t2 = t1;
        t1 = pii(v1[i],i);
      } else {
        t2 = min(pii(v1[i],i),t2);
      }
      if (b1>pii(v2[i],i)) {
        b2 = b1;
        b1 = pii(v2[i],i);
      } else {
        b2 = min(pii(v2[i],i),b2);
      }
    }
    int ue,shita;
    if (t1.second == b1.second) {
      if (t2.first < b2.first) {
        ue = t2.second;
        shita = b1.second;
      } else {
        ue = t1.second;
        shita = b2.second;
      }
    } else {
      ue = t1.second;
      shita = b1.second;
    }
    // cout << ue << " " << shita<< endl;
    // cout << v1[ue] << endl;
    ans += v1[ue]*v2[ue]*v1[shita]*v2[shita];
    v1.push_back(v1[ue]);
    v2.push_back(v2[shita]);
    // REP(i,v1.size()) {
    //   cout << v1[i] << " " <<v2[i] << endl;
    // }
    if (ue<shita) shita--;
    v1.erase(v1.begin()+ue);
    v1.erase(v1.begin()+shita);
    v2.erase(v2.begin()+ue);
    v2.erase(v2.begin()+shita);
  }
  cout << ans << endl;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef complex<double> P;
static const double EPS = 1e-8;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
int dfs(int sum,vector<PII> yama){
  if(yama.size()==1) return sum;
  int MAX=0;
  int ret = 1e9;
  REP(i,yama.size()-1){
    MAX = max(yama[i].second*yama[i+1].first,MAX);
  }
  REP(i,yama.size()-1){
    if(yama[i].second*yama[i+1].first==MAX){
      vector<PII> tmp=yama;
      int a=yama[i].first,b=yama[i].second,c=yama[i+1].first,d=yama[i+1].second;
      tmp.erase(tmp.begin()+i);
      tmp.erase(tmp.begin()+i);
      tmp.insert(tmp.begin()+i,PII(a,d));
      ret = min(ret,dfs(sum+a*b*c*d,tmp));
    }
  }
  return ret;
}
int main(void){
  int n;
  cin>>n;
  vector<PII> yama(n);
  REP(i,n){
    int a,b;
    cin>>a>>b;
    yama[i] = PII(a,b);
  }
  cout<<dfs(0,yama)<<endl;
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  
  int n;
  cin >> n;
  int a[110], b[110];
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
  }
  const int INF = 1 << 30;
  int dp[110][110];
  fill_n((int*)dp, 110 * 110, INF);
  for (int w = 0; w < n; w++) {
    for (int i = 0; i + w < n; i++) {
      int j = i + w;
      if (w == 0) {
        dp[i][j] = 0;
      } else {
        for (int k = i; k < j; k++) {
          dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j] + a[i] * b[k] * a[k + 1] * b[j]);
        }
      }
    }
  }
  cout << dp[0][n - 1] << endl;

  return 0;
}

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

using namespace std;

typedef pair<int,int> P;

int dp[101][101];
vector<P> v;

const int INF=(1<<30);

int dfs(int d,int u){
	if(u==d)
		return 0;
	if(dp[d][u]!=INF)
		return dp[d][u];
	int minCost=INF;
	for(int i = d; i < u; i++){
		minCost=min(minCost,v[d].second*v[u].first*v[i].first*v[i+1].second+dfs(d,i)+dfs(i+1,u));
	}
	dp[d][u]=minCost;
	return dp[d][u];
}

int main(){

	int n;
	cin>>n;
	for(int i = 0; i < n; i++){
		int over,down;
		cin>>over>>down;
		v.push_back(P(over,down));
	}
	for(int i = 0; i < 101; i++)
		fill(dp[i],dp[i]+101,INF);
	cout<<dfs(0,n-1)<<endl;

	return 0;
}

C++: #include<deque>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<cmath>
#include<cstdio>
#include<cstring>
using namespace std;
int main(){
  int i,j,k;
  int n;
  cin>>n;
  pair<int,int> a[100];
  for(i=0;i<n;++i)
    cin>>a[i].first>>a[i].second;
  int b[100][100];
  fill(b[0],b[0]+100*100,1<<30);
  for(i=0;i<n;++i)
    b[i][i]=0;
  for(i=1;i<n;++i){
    for(j=0;j<n-i;++j){
      for(k=j;k<i+j;++k){
	b[j][i+j]=min(b[j][i+j],b[j][k]+b[k+1][i+j]+a[j].first*a[k].second*a[k+1].first*a[i+j].second);
      }
    }
  }
  cout<<b[0][n-1]<<endl;
  return 0;
}

C++: #include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

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

    vector<vector<int> > cost(n, vector<int>(n, INT_MAX));
    for(int i=0; i<n; ++i)
        cost[i][i] = 0;
    for(int i=1; i<n; ++i){
        for(int j=0; j+i<n; ++j){
            for(int k=j; k<j+i; ++k){
                int c = cost[j][k] + cost[k+1][j+i] + a[j] * b[k] * a[k+1] * b[j+i];
                cost[j][j+i] = min(cost[j][j+i], c);
            }
        }
    }
    cout << cost[0][n-1] << endl;

    return 0;
}

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

#define TOP 0
#define BOTTOM 1

int main()
{
	int n, a, b, i;
	int min, cost;
	int v[100][2];
	vector<int> order;

	cin>>n;
	order.resize(n);
	for (i = 0; i < n; i++) {
		cin>>a>>b;
		v[i][0] = a;
		v[i][1] = b;
		order[i]=i;
	}

	min = INT_MAX;
	do {
		cost = 0;
		for (i = 0; i < n-1; i++)
			cost += v[order[0]][TOP]*v[order[i]][BOTTOM] * v[order[i+1]][TOP]*v[order[i+1]][BOTTOM];
		if (min > cost) min = cost;
	} while (next_permutation(order.begin(), order.end()));
	cout<<min<<endl;

	return 0;
}