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;
 
const int INF = 1e9;
const int MAX = 100;
 
int main()
{
    int n, r[MAX], c[MAX];
    int dp[MAX][MAX];
 
    scanf("%d" ,&n);
    for (int i = 0; i < n; i++) {
	scanf("%d %d", &r[i], &c[i]);
    }
 
    for (int i = 0; i < n; i++) {
	for (int j = 0; j < n; j++) {
	    dp[i][j] = INF;
	}
    }
 
    for (int i = 0; i < n; i++) {
	dp[i][i] = 0;
    }
    for (int i = 1; i < n; i++) {
	for (int j = 0; j < n-i; j++) {
	    for (int k = j; k < i + j; k++) {
		int M = r[j] * c[k] * r[k+1] * c[i+j];
		dp[j][i+j] = min(dp[j][i+j], dp[j][k] + dp[k+1][i+j] + M);
	    }
	}
    }
    printf("%d\n", dp[0][n-1]);
    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <complex>
#include <map>
#include <climits>
#include <sstream>
using namespace std;

#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 EPS 1e-8
#define F first
#define S second
#define mkp make_pair

static const double PI=6*asin(0.5);
typedef long long ll;
typedef complex<double> CP;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vint;
static const int INF=1<<24;

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

//v.erase(unique(v.begin(),v.end()),v.end());


int main(){
	ll n;
	cin>>n;
	vector<pii> v(n);
	rep(i,n){
		cin>>v[i].F>>v[i].S;
	}
	ll ans=0;

	while(v.size()>1){
		// cout<<v.size()<<endl;
		// rep(i,v.size()){
			// cout<<v[i].F<<" "<<v[i].S<<endl;
		// }
		// cout<<endl;
		int a=0,b=1;
		rep(i,v.size()){
			rep(j,v.size()){
				if(i==j) continue;
				if(v[a].F*v[a].S*v[b].F*v[b].S<v[i].F*v[j].F*v[i].S*v[j].S){
					a=i;
					b=j;
				}
			}
		}
		// cout<<"a  b  "<<a<<" "<<b<<endl;
		vector<pii> t;
		ans+=v[a].F*v[b].F*v[a].S*v[b].S;
		rep(i,v.size()){
			if(i!=a&&i!=b){
				t.PB(v[i]);
			}
		}
		t.PB(v[a].F*v[b].S<v[a].S*v[b].F ? mkp(v[a].F,v[b].S):mkp(v[b].F,v[a].S));
		v=t;
	}
	cout<<ans<<endl;
	// while(1){
		// if(v.size()==1){
			// break;
		// }
		// int a=0,b=0;
		// rep(i,v.size()){
			// if(v[a].F<v[i].F){
				// a=i;
			// }
			// if(v[b].S<v[i].S){
				// b=i;
			// }
		// }
		// vint x,y;
		// rep(i,v.size()){
			// if(v[a].F==v[i].F){
				// x.PB(a);
			// }
			// if(v[b].S==v[i].S){
				// y.PB(i);
			// }
		// }
		// if(x.size()==1&&y.size()==1&&x[0]==y[0]){
		// }
	// }
}

C++: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back
#define INF (1e9+1)
//#define INF (1LL<<59)

#define int ll

signed main(){
	int n;
	cin>>n;
	vector<pii> card(n);
	rep(i,n)cin>>card[i].first>>card[i].second;
	
	
	ll ans = 0;
	while(card.size()>1){
		int maxvalue=-1;
		pii maxpos=pii(-1,-1);
		rep(i,card.size()){
			rep(j,card.size()){
				if(i==j)continue;
				if(maxvalue<card[i].second*card[j].first){
					maxpos=pii(i,j);
					maxvalue=card[i].second*card[j].first;
				}
			}
		}
		
		ans+=maxvalue*card[maxpos.first].first*card[maxpos.second].second;
//		cout<<maxvalue*card[maxpos.first].first*card[maxpos.second].second<<endl;
		
		card[maxpos.first]=pii(card[maxpos.first].first,card[maxpos.second].second);
		card.erase(card.begin()+maxpos.second);
	}
	cout<<ans<<endl;
}

C++: #include <iostream>
#include <climits>
#include <queue>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<n;i++)
struct BOOK{
	int a,b,c,d;
	BOOK(int x,int y,int z,int aa){a = x , b = y , c = z , d = aa;}
};
bool operator < (const BOOK &a,const BOOK &b){return a.a * a.b > b.a * b.b;}

bool done[100][100][100];

int main(){
	int n;
	ll a,b;
	cin >> n;
	priority_queue<BOOK> Q;
	rep(i,n) cin >> a >> b , Q.push(BOOK(a,b,1,0));
	while(Q.top().c != n){
		BOOK one = Q.top(); Q.pop();
		BOOK two = Q.top(); Q.pop();
		Q.push(BOOK(one.a , two.b , one.c+two.c , one.d + two.d + one.a * one.b * two.a * two.b));
		Q.push(BOOK(two.a , one.b , one.c+two.c , one.d + two.d + one.a * one.b * two.a * two.b));		
	}
	cout << Q.top().d << endl;
}

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

int n,a[101],b[101],dp[111][111];

int dfs(int l,int r){
    if(dp[l][r]!=-1)return dp[l][r];
    int res=1e17;
    for(int i=l;i<r;i++){
        res=min(res,dfs(l,i)+dfs(i+1,r)+a[l]*b[i]*a[i+1]*b[r]);
    }
    return dp[l][r]=res;
}

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

C++: // 基本テンプレート
 
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
using namespace std;
 
#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++)
#define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define int long long int
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
typedef pair<int, int> pii;
typedef long long ll;
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1001001001001001LL;
const ll MOD = 1000000007LL;

int dp[110][110], A[110], B[110];
 
signed main() {
    int N; cin >> N;
    fill(dp[0], dp[N+1], INF);
    for(int i=0; i<N; i++) {
        cin >> A[i] >> B[i];
        dp[i][i+1] = 0;
    }

    for(int d=1; d<=N; d++) {
        for(int l=0; l<N-d+1; l++) {
            int r = l + d;
            for(int k=l+1; k<r; k++) {
                int cost = A[l] * B[k-1] * A[k] * B[r-1];
                chmin(dp[l][r], dp[l][k] + dp[k][r] + cost);
            }
        }
    }
    cout << dp[0][N] << endl;
    return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll INF = 1ll << 35;
int a[100], b[100];
ll dp[100][100];
ll rec(int x, int y){
  if(x == y) return 0;
  if(~dp[x][y]) return dp[x][y];
  ll ret = INF;
  for(int i = x; i < y; i++){
    ret = min( ret, rec( x, i) + rec( i + 1, y) + a[x] * b[i] * a[i + 1] * b[y]);
  }
  return dp[x][y] = ret;
}

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