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<string>
#include<algorithm>	
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<queue>
#include<cstdio>
#include<sstream>
#include<iomanip>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
using namespace std;
//kaewasuretyuui
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int,pii> pip;
typedef vector<pip>vip	;
const double PI=acos(-1);
const double EPS=1e-8;
const ll inf=1e17;
vp in;
vvi dp;
int f(int a,int b){
	if(dp[a][b]!=inf)return dp[a][b];
	if(b==2){
		return dp[a][b]=in[a].first*in[a].second*in[a+b-1].first*in[a+b-1].second;
	}
	ll mi=f(a,b-1)+in[a].first*in[a+b-2].second*in[a+b-1].first*in[a+b-1].second;
	mi=min<ll>(mi,f(a+1,b-1)+in[a].first*in[a].second*in[a+1].first*in[a+b-1].second);
	rep(i,b-3)mi=min<ll>(mi,f(a,i+2)+f(a+i+2,b-i-2)+in[a].first*in[a+i+1].second*in[a+i+2].first*in[a+b-1].second);
	return dp[a][b]=mi;
}
int main(){
	int n;
	cin>>n;
	in=vp(n);
	dp=vvi(n,vi(n+1,inf));
	rep(i,n)cin>>in[i].first>>in[i].second;
	if(n==1)cout<<0<<endl;
	else cout<<f(0,n)<<endl;
}

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

#define MAX 101
#define Inf (UINT_MAX-1)

typedef unsigned long long int uint;
using namespace std;

struct State{
	State():cost(Inf),bottom(1),top(1){}
	uint cost;
	uint bottom;
	uint top;
};

State m[MAX][MAX];

void CardOrder(vector<pair<uint, uint> > &vc){
	int n = vc.size();
	for(int i = 1; i <= n; ++i){
		m[i][i].cost = 0;
		m[i][i].top = vc[i-1].first;
		m[i][i].bottom = vc[i-1].second;
	}
	for(int l = 2; l <= n; ++l){
		for(int i = 1; i <= n - l + 1; ++i){
			for(int j = i; j < i + l; ++j){
				uint cost = m[i][j].cost + m[j+1][i+l-1].cost +
					m[i][j].top * m[i][j].bottom * m[j+1][i+l-1].top * m[j+1][i+l-1].bottom;
				if( cost < m[i][i+l-1].cost ){
					m[i][i+l-1].cost = cost;
					m[i][i+l-1].top = m[i][j].top;
					m[i][i+l-1].bottom = m[j+1][i+l-1].bottom;
				}
			}
		}
	}
}
void Reset(){
	for(int i = 0; i < MAX; ++i){
		for(int j = 0; j < MAX; ++j){
			m[i][j].cost = Inf;
			m[i][j].bottom = 1;
			m[i][j].top = 1;
		}
	}
}
int main()
{
	while( true ){
		int n;
		vector< pair<uint, uint> > vCards;
	
		cin >> n;

		if( cin.eof() )
			break;

		Reset();

		for(int x = 0; x < n; ++x){
			uint top, btm;
			cin >> top >> btm;

			vCards.push_back( make_pair(top, btm) );
		}
	
		CardOrder( vCards );
		cout << (n==0?0:m[1][n].cost) << '\n';
	}
	return 0;
}

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

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<21

using namespace std;

int main()
{
	int n;
	cin >> n;
	
	vector<pair<int,int> > v;
	long long dp[101][101];

	rep(i,n)
	{
		int a,b;
		cin >> a >> b;
		v.push_back(make_pair(a,b));
	}

	rep(i,n)
	{
		rep(j,n)
		{
			dp[i][j] = INF;
		}
		dp[i][i] = 0;
	}
	
	REP(i,1,n)
	{
		rep(j,n-i+1)
		{
			REP(k,j,j+i)
			{
				int cost = v[j].first * v[k].second * v[k+1].first *  v[j+i].second;
				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 <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cctype>
#include <climits>

#include <iostream>
#include <vector>
#include <deque>
#include <queue>
#include <map>
#include <algorithm>

using namespace std;

#define rep(i, n) for(int i = 0; i < n; i++)
#define repi(i,l,n) for(int (i)=(int)(l);(i)<(int)(n);(i)++)
#define d_arr(arr, h, w) rep(i,(h) ){ cout << "["; rep(j,(w) ) { cout << (arr)[i][j] << ", "; } cout << "]" << endl;}
#define IN(x,s,g) ((x) >= (s) && (x) < (g))
#define ISIN(x,y,w,h) (IN((x),0,(w)) && IN((y),0,(h)))
#define print(x) printf("%d\n",x);

typedef pair<int ,int> P;

/*
1行目 カードの山の個数n(100以下の正の整数)
2行目 左から1番目の山の情報 a b（整数；半角空白区切り）
a 山の1番上のカードに書かれた数
b 山の1番下のカードに書かれた数
3行目 左から2番目の山の情報a b（整数；半角空白区切り）
：
：
n+1行目 左からn番目の山の情報a b（整数；半角空白区切り）
 */


//dp[l][r] = index-lから始まって、index-rで終わる配列の最小値コスト
//

int dp[100][100] = {0};

int main()
{
    int n;
    cin >> n;
    vector<int> a,b;
    rep(i,n) 
    {
        int tmp;
        cin >> tmp;
        a.push_back(tmp);
        cin >> tmp;
        b.push_back(tmp);
    }

//    rep(i,100){ rep(j,100){ dp[i][j] = (1<<32)-1; } }
    rep(i,100){ rep(j,100){ dp[i][j] = INT_MAX; } }
    rep(i,100){  dp[i][i+1] = 0;  }
    repi(len,1,a.size()+1)
    {
        rep(l,n)
        {
            if( n  < l + len  ) { break; }
            int r = l + len;
            repi(m,l+1,r)
            {
                //cout << "(m,l,r) = (" << m << "," << l << " " << r << ")" << endl;
                dp[l][r] = min(dp[l][r],  dp[l][m] + dp[m][r] + a[l]*b[m-1]*a[m]*b[r-1]);
            }
        }
    }
    //rep(i,n+1){
    //    rep(j,n+1){ 
    //    
    //        cout << dp[i][j] << ", ";
    //    } 
    //    cout << endl;
    //}
    cout << dp[0][n] << endl;
}

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)

typedef pair<int, int> pii;

int main() {
	int n;
	cin >> n;
	vector<pii> card(n);
	vector<bool> used(n, false);
	REP(i, n) {
		int a, b;
		scanf("%d %d", &a, &b);
		card[i] = pii(a, b);
	}
	
	int ans = 0;
	REP(i, n - 1) {
		vector<pair<int, pii> > v;
		REP(j, n - 1) if (!used[j]) {
			FOR(k, j + 1, n) if (!used[k]) {
				v.push_back(make_pair(card[j].first * card[k].second, pii(j, k)));
			}
		}
		sort(v.begin(), v.end());
		int p1 = v[0].second.first, p2 = v[0].second.second;
		ans += card[p1].first * card[p1].second * card[p2].first * card[p2].second;
		used[p2] = true;
		card[p1].second = card[p2].second;
	}
	cout << ans << endl;
	return 0;
}