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<map>
#include<vector>
#include<algorithm>
using namespace std;
#define INF 1 <<21
#define P pair<int, int>
#define N 101

int loop(int left, int right);

int n;
vector<P> cost;
int dp[N][N];
void resetDp();


int main(){
    resetDp();
    cin >>n;
    for(int i = 0; i < n; i++){
        int top, bottom;
        cin >>top >>bottom;
        cost.push_back( make_pair(top, bottom) );
    }
    cout <<loop(0, n - 1) <<endl;
    return 0;
}

int loop(int left, int right){
    if(left == right) return 0;
    else if(dp[left][right] != -1) return dp[left][right];
    else {
        int ret = INF;
        for(int i = left; i < right; i++){
            int ans = loop(left, i) + loop(i + 1, right) + (cost[left].first * cost[i].second * cost[i + 1].first * cost[right].second);
            if(ret == -1) ret = ans;
            else ret = min(ret, ans);
        }
        return dp[left][right] = ret;
    }
}

void resetDp(){
    for(int i = 0; i < N; i++){
        for(int j = 0; j < N; j++){
            dp[i][j] = -1;
        }
    }
}

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>
 
using namespace std;
 
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define rep(i,n) REP(i,0,(n))
#define pb(n) push_back(n)
#define mp(n,m) make_pair(n, m)
#define all(r) r.begin(),r.end()
#define fi first
#define se second
#define println(X) cout<<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;


bool compFi(pii p1, pii p2){
	if(p1.fi!=p2.fi){
		return p1.fi > p2.fi;
	}
	return p1.se < p2.se;
}
bool compSe(pii p1, pii p2){
	if(p1.se != p2.se){
		return p1.se > p2.se;
	}
	return p1.fi < p1.se;
}

int main(){
	int n;
	cin>>n;
	vector<pii> v(n);
	rep(i, n){
		cin>>v[i].fi>>v[i].se;
	}
	int ans = 0;
	while(v.size() > 1){
		vector<pii> s = v, t = v;
		sort(all(s), compFi);
		sort(all(t), compSe);
		pii cs = s.back(), ct = t.back();
		s.pop_back(); t.pop_back();
		sort(all(s), compSe);
		sort(all(t), compFi);
		if(cs.fi * s.back().se < ct.se * t.back().fi){
			pii ss = s.back(); s.pop_back();
			ans += cs.fi * cs.se * ss.fi * ss.se;
			s.push_back(pii(cs.fi, ss.se));
			v = s;
		}else{
			pii tt = t.back(); t.pop_back();
			ans += ct.fi * ct.se * tt.fi * tt.se;
			t.push_back(pii(tt.fi, ct.se));
			v = t;
		}
	}
	println(ans);
}

C++: #include <bits/stdc++.h>

#define FOR(i,a,b) for(int i= (a); i<((int)b); ++i)
#define RFOR(i,a) for(int i=(a); i >= 0; --i)
#define FOE(i,a) for(auto i : a)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define DUMP(x)  cerr << #x << " = " << (x) << endl;
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v,x) (std::find(v.begin(), v.end(), x) != v.end())
#define BIT(n) (1LL<<(n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());

typedef long long LL;
template<typename T> using V = std::vector<T>;
template<typename T> using VV = std::vector<std::vector<T>>;
template<typename T> using VVV = std::vector<std::vector<std::vector<T>>>;

template<class T> inline T ceil(T a, T b) { return (a + b - 1) / b; }
template<class T> inline void print(T x) { std::cout << x << std::endl; }
template<class T> inline void print_vec(const std::vector<T> &v) { for (int i = 0; i < v.size(); ++i) {  if (i != 0) {std::cout << " ";} std::cout << v[i];} std::cout << "\n"; }
template<class T> inline bool inside(T y, T x, T H, T W) {return 0 <= y and y < H and 0 <= x and x < W; }
template<class T> inline double euclidean_distance(T y1, T x1, T y2, T x2) { return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); }
template<class T> inline double manhattan_distance(T y1, T x1, T y2, T x2) { return abs(x1 - x2) + abs(y1 - y2); }

const int INF = 1L << 30;
const double EPS = 1e-9;
const std::string YES = "YES", Yes = "Yes", NO = "NO", No = "No";
const std::vector<int> dy4 = { 0, 1, 0, -1 }, dx4 = { 1, 0, -1, 0 };    // 4近傍（右, 下, 左, 上）
const std::vector<int> dy8 = { 0, -1, 0, 1, 1, -1, -1, 1 }, dx8 = { 1, 0, -1, 0, 1, 1, -1, -1 };

using namespace std;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int N;
    cin >> N;

    vector<pair<int, int>> v;
    FOR(i, 0, N) {
        int a, b;
        cin >> a >> b;
        v.emplace_back(make_pair(a, b));
    }

    VV<int> dp(N + 1, V<int>(N + 1, INF));
    FOR(i, 0, dp.size()) {
        dp[i][i] = 0;
    }

    FOR(width, 0, N) {
        FOR(left, 0, N - width) {
            int right = left + width;
            FOR(k, left + 1, right + 1) {
                int cost = dp[left][k - 1] + dp[k][right] + v[left].first * v[k - 1].second * v[k].first * v[right].second;
                dp[left][right] = min(dp[left][right], cost);
            }
        }
    }
    print(dp[0][N - 1]);
    return 0;
}

C++: #include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<climits>
using namespace std;
#define INF LLONG_MAX
#define P pair<ll, ll>
#define N 101
#define ll long long int

ll loop(ll left, ll right);
void resetDp();
ll n;
vector<P> cost;
ll dp[N][N];

int main(){
    resetDp();
    cin >>n;
    for(int i = 0; i < n; i++){
        ll top, bottom;
        cin >>top >>bottom;
        cost.push_back( make_pair(top, bottom) );
    }
    cout <<loop(0, n - 1) <<endl;
    return 0;
}

ll loop(ll left, ll right){
    if(left == right) return 0;
    else if(dp[left][right] != INF) return dp[left][right];
    else {
        ll ret = INF;
        for(int i = left; i < right; i++){
            ret = min(ret, loop(left, i) + loop(i + 1, right) + (cost[left].first * cost[i].second * cost[i + 1].first * cost[right].second));
        }
        return dp[left][right] = ret;
    }
}

void resetDp(){
    for(int i = 0; i < N; i++){
        for(int j = 0; j < N; j++){
            dp[i][j] = INF;
        }
    }
}